• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 use crate::ast::*;
11 
list(rule: Rule) -> Rule12 pub fn list(rule: Rule) -> Rule {
13     let Rule { name, ty, expr } = rule;
14     Rule {
15         name,
16         ty,
17         expr: expr.map_bottom_up(|expr| {
18             match expr {
19                 Expr::Seq(l, r) => match *l {
20                     Expr::Rep(l) => {
21                         let l = *l;
22                         match l {
23                             Expr::Seq(l1, l2) => {
24                                 // Converts `(rule ~ rest)* ~ rule` to `rule ~ (rest ~ rule)*`,
25                                 // avoiding matching the last `rule` twice.
26                                 if l1 == r {
27                                     Expr::Seq(l1, Box::new(Expr::Rep(Box::new(Expr::Seq(l2, r)))))
28                                 } else {
29                                     Expr::Seq(Box::new(Expr::Rep(Box::new(Expr::Seq(l1, l2)))), r)
30                                 }
31                             }
32                             expr => Expr::Seq(Box::new(Expr::Rep(Box::new(expr))), r),
33                         }
34                     }
35                     expr => Expr::Seq(Box::new(expr), r),
36                 },
37                 expr => expr,
38             }
39         }),
40     }
41 }
42