| 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 | |
| 12 | pub fn concatenate(rule: Rule) -> Rule { |
| 13 | let Rule { name: String, ty: RuleType, expr: Expr } = rule; |
| 14 | Rule { |
| 15 | name, |
| 16 | ty, |
| 17 | expr: expr.map_bottom_up(|expr: Expr| { |
| 18 | if ty == RuleType::Atomic { |
| 19 | match expr { |
| 20 | Expr::Seq(lhs: Box, rhs: Box) => match (*lhs, *rhs) { |
| 21 | (Expr::Str(lhs: String), Expr::Str(rhs: String)) => Expr::Str(lhs + &rhs), |
| 22 | (Expr::Insens(lhs: String), Expr::Insens(rhs: String)) => Expr::Insens(lhs + &rhs), |
| 23 | (lhs: Expr, rhs: Expr) => Expr::Seq(Box::new(lhs), Box::new(rhs)), |
| 24 | }, |
| 25 | expr: Expr => expr, |
| 26 | } |
| 27 | } else { |
| 28 | expr |
| 29 | } |
| 30 | }), |
| 31 | } |
| 32 | } |
| 33 | |