    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153

    // pest. The Elegant Parser
    // Copyright (c) 2018 Dragoș Tiselice
    //
    // Licensed under the Apache License, Version 2.0
    // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
    // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. All files in the project carrying such notice may not be copied,
    // modified, or distributed except according to those terms.
    use std::collections::HashMap;

    use crate::optimizer::*;

    pub fn restore_on_err(
        rule: OptimizedRule,
        rules: &HashMap<String, OptimizedExpr>,
    ) -> OptimizedRule {
        let OptimizedRule { name, ty, expr } = rule;
        let expr = expr.map_bottom_up(|expr| wrap_branching_exprs(expr, rules));
        OptimizedRule { name, ty, expr }
    }

    fn wrap_branching_exprs(
        expr: OptimizedExpr,
        rules: &HashMap<String, OptimizedExpr>,
    ) -> OptimizedExpr {
        match expr {
            OptimizedExpr::Opt(expr) => {
                if child_modifies_state(&expr, rules, &mut HashMap::new()) {
                    OptimizedExpr::Opt(Box::new(OptimizedExpr::RestoreOnErr(expr)))
                } else {
                    OptimizedExpr::Opt(expr)
                }
            }
            OptimizedExpr::Choice(lhs, rhs) => {
                let wrapped_lhs = if child_modifies_state(&lhs, rules, &mut HashMap::new()) {
                    Box::new(OptimizedExpr::RestoreOnErr(lhs))
                } else {
                    lhs
                };
                let wrapped_rhs = if child_modifies_state(&rhs, rules, &mut HashMap::new()) {
                    Box::new(OptimizedExpr::RestoreOnErr(rhs))
                } else {
                    rhs
                };
                OptimizedExpr::Choice(wrapped_lhs, wrapped_rhs)
            }
            OptimizedExpr::Rep(expr) => {
                if child_modifies_state(&expr, rules, &mut HashMap::new()) {
                    OptimizedExpr::Rep(Box::new(OptimizedExpr::RestoreOnErr(expr)))
                } else {
                    OptimizedExpr::Rep(expr)
                }
            }
            _ => expr,
        }
    }

    fn child_modifies_state(
        expr: &OptimizedExpr,
        rules: &HashMap<String, OptimizedExpr>,
        cache: &mut HashMap<String, Option<bool>>,
    ) -> bool {
        expr.iter_top_down().any(|expr| match expr {
            OptimizedExpr::Push(_) => true,
            OptimizedExpr::Ident(ref name) if name == "DROP" => true,
            OptimizedExpr::Ident(ref name) if name == "POP" => true,
            OptimizedExpr::Ident(ref name) => match cache.get(name).cloned() {
                Some(option) => match option {
                    Some(cached) => cached,
                    None => {
                        cache.insert(name.to_owned(), Some(false));
                        false
                    }
                },
                None => {
                    cache.insert(name.to_owned(), None);

                    let result = match rules.get(name) {
                        Some(expr) => child_modifies_state(expr, rules, cache),
                        None => false,
                    };

                    cache.insert(name.to_owned(), Some(result));

                    result
                }
            },
            _ => false,
        })
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use crate::optimizer::OptimizedExpr::*;

        #[test]
        fn restore_no_stack_children() {
            let rules = vec![OptimizedRule {
                name: "rule".to_owned(),
                ty: RuleType::Normal,
                expr: box_tree!(Opt(Str("a".to_string()))),
            }];

            assert_eq!(
                restore_on_err(rules[0].clone(), &to_hash_map(&rules)),
                rules[0].clone()
            );
        }

        #[test]
        fn restore_with_child_stack_ops() {
            let rules = vec![OptimizedRule {
                name: "rule".to_owned(),
                ty: RuleType::Normal,
                expr: box_tree!(Rep(Push(Str("a".to_string())))),
            }];

            let restored = OptimizedRule {
                name: "rule".to_owned(),
                ty: RuleType::Normal,
                expr: box_tree!(Rep(RestoreOnErr(Push(Str("a".to_string()))))),
            };

            assert_eq!(
                restore_on_err(rules[0].clone(), &to_hash_map(&rules)),
                restored
            );
        }

        #[test]
        fn restore_choice_branch_with_and_branch_without() {
            let rules = vec![OptimizedRule {
                name: "rule".to_owned(),
                ty: RuleType::Normal,
                expr: box_tree!(Choice(Push(Str("a".to_string())), Str("a".to_string()))),
            }];

            let restored = OptimizedRule {
                name: "rule".to_owned(),
                ty: RuleType::Normal,
                expr: box_tree!(Choice(
                    RestoreOnErr(Push(Str("a".to_string()))),
                    Str("a".to_string())
                )),
            };

            assert_eq!(
                restore_on_err(rules[0].clone(), &to_hash_map(&rules)),
                restored
            );
        }
    }