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 ast::*;
11 
concatenate(rule: Rule) -> Rule12 pub fn concatenate(rule: Rule) -> Rule {
13     match rule {
14         Rule { name, ty, expr } => Rule {
15             name,
16             ty,
17             expr: expr.map_bottom_up(|expr| {
18                 if ty == RuleType::Atomic {
19                     // TODO: Use box syntax when it gets stabilized.
20                     match expr {
21                         Expr::Seq(lhs, rhs) => match (*lhs, *rhs) {
22                             (Expr::Str(lhs), Expr::Str(rhs)) => Expr::Str(lhs + &rhs),
23                             (Expr::Insens(lhs), Expr::Insens(rhs)) => Expr::Insens(lhs + &rhs),
24                             (lhs, rhs) => Expr::Seq(Box::new(lhs), Box::new(rhs)),
25                         },
26                         expr => expr,
27                     }
28                 } else {
29                     expr
30                 }
31             }),
32         },
33     }
34 }
35