pest_meta/optimizer/
factorizer.rs

1// pest. The Elegant Parser
2// Copyright (c) 2018 DragoČ™ Tiselice
3//
4// Licensed under the Apache License, Version 2.0
5// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
6// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. All files in the project carrying such notice may not be copied,
8// modified, or distributed except according to those terms.
9
10use crate::ast::*;
11
12pub fn factor(rule: Rule) -> Rule {
13    let Rule { name, ty, expr } = rule;
14    Rule {
15        name,
16        ty,
17        expr: expr.map_top_down(|expr| {
18            // TODO: Use box syntax when it gets stabilized.
19            match expr {
20                Expr::Choice(lhs, rhs) => match (*lhs, *rhs) {
21                    (Expr::Seq(l1, r1), Expr::Seq(l2, r2)) => {
22                        if l1 == l2 {
23                            Expr::Seq(l1, Box::new(Expr::Choice(r1, r2)))
24                        } else {
25                            Expr::Choice(Box::new(Expr::Seq(l1, r1)), Box::new(Expr::Seq(l2, r2)))
26                        }
27                    }
28                    // Converts `(rule ~ rest) | rule` to `rule ~ rest?`, avoiding trying to match `rule` twice.
29                    // This is only done for atomic rules, because other rule types have implicit whitespaces.
30                    // FIXME: "desugar" implicit whitespace rules before applying any optimizations
31                    (Expr::Seq(l1, l2), r)
32                        if matches!(ty, RuleType::Atomic | RuleType::CompoundAtomic) =>
33                    {
34                        if *l1 == r {
35                            Expr::Seq(l1, Box::new(Expr::Opt(l2)))
36                        } else {
37                            Expr::Choice(Box::new(Expr::Seq(l1, l2)), Box::new(r))
38                        }
39                    }
40                    // Converts `rule | (rule ~ rest)` to `rule` since `(rule ~ rest)`
41                    // will never match if `rule` didn't.
42                    (l, Expr::Seq(r1, r2)) => {
43                        if l == *r1 {
44                            l
45                        } else {
46                            Expr::Choice(Box::new(l), Box::new(Expr::Seq(r1, r2)))
47                        }
48                    }
49                    (lhs, rhs) => Expr::Choice(Box::new(lhs), Box::new(rhs)),
50                },
51                expr => expr,
52            }
53        }),
54    }
55}