| 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 rotate(rule: Rule) -> Rule { |
| 13 | fn rotate_internal(expr: Expr) -> Expr { |
| 14 | match expr { |
| 15 | Expr::Seq(lhs, rhs) => { |
| 16 | let lhs = *lhs; |
| 17 | match lhs { |
| 18 | Expr::Seq(ll, lr) => { |
| 19 | rotate_internal(Expr::Seq(ll, Box::new(Expr::Seq(lr, rhs)))) |
| 20 | } |
| 21 | lhs => Expr::Seq(Box::new(lhs), rhs), |
| 22 | } |
| 23 | } |
| 24 | Expr::Choice(lhs, rhs) => { |
| 25 | let lhs = *lhs; |
| 26 | match lhs { |
| 27 | Expr::Choice(ll, lr) => { |
| 28 | rotate_internal(Expr::Choice(ll, Box::new(Expr::Choice(lr, rhs)))) |
| 29 | } |
| 30 | lhs => Expr::Choice(Box::new(lhs), rhs), |
| 31 | } |
| 32 | } |
| 33 | expr => expr, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | let Rule { name, ty, expr } = rule; |
| 38 | Rule { |
| 39 | name, |
| 40 | ty, |
| 41 | expr: expr.map_top_down(rotate_internal), |
| 42 | } |
| 43 | } |
| 44 | |