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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::borrow::Cow;
use std::collections::BTreeMap;

use input::ast::{Stmts, Rhs, RhsAst};
use input::FragmentId;
use middle::flatten_stmts::{Path, Position};

#[derive(Debug)]
pub struct Trace {
    pub tokens: BTreeMap<Path, TraceToken>,
    pub stmt_positions: Vec<StmtTokenPosition>,
    pub rule_tokens: Vec<BTreeMap<Option<usize>, usize>>,
}

#[derive(Debug)]
pub struct StmtTokenPosition {
    lhs: Option<FragmentId>,
    position: usize,
}

// #[derive(Clone, Debug, Eq, PartialEq)]
// pub struct SourceOrigin {
//     pub rule_id: u32,
//     pub rule_pos: Vec<u32>,
// }

#[derive(Debug, Eq, PartialEq)]
pub enum TraceToken {
    Fragment(FragmentId),
    Quote,
    String(String),
    LParen,
    RParen,
    Star,
    Plus,
    EmptyProduct,
    Alternative,
    PrecedencedAlternative,
}

impl TraceToken {
    pub fn as_str(&self, fragment_names: &BTreeMap<FragmentId, String>) -> Cow<str> {
        match self {
            &TraceToken::LParen => Cow::Borrowed("("),
            &TraceToken::RParen => Cow::Borrowed(")"),
            &TraceToken::Star => Cow::Borrowed("*"),
            &TraceToken::Plus => Cow::Borrowed("+"),
            &TraceToken::EmptyProduct => Cow::Borrowed("()"),
            &TraceToken::Alternative => Cow::Borrowed("|"),
            &TraceToken::PrecedencedAlternative => Cow::Borrowed("|>"),
            &TraceToken::Quote => Cow::Borrowed("\""),
            &TraceToken::String(ref name) => Cow::Owned(name.as_str().to_string()),

            &TraceToken::Fragment(fragment_id) => {
                Cow::Owned(fragment_names[&fragment_id].clone())
            },
        }
    }
}

impl Trace {
    pub fn from_stmts(stmts: &Stmts) -> Self {
        let mut trace = Trace {
            tokens: BTreeMap::new(),
            stmt_positions: vec![],
            rule_tokens: vec![],
        };
        trace.flatten_stmts(stmts);
        trace
    }

    fn flatten_stmts(&mut self, stmts: &Stmts) {
        for (stmt_idx, stmt) in stmts.stmts.iter().enumerate() {
            let mut last_level = None;
            let start_path = Path {
                position: vec![
                    Position::IdxWithFragment {
                        idx: stmt_idx,
                        fragment: stmt.lhs,
                    },
                ],
            };
            self.stmt_positions.push(StmtTokenPosition {
                lhs: Some(stmt.lhs),
                position: self.tokens.len()
            });
            for (alternative_idx, (level, ref rhs, ref _action)) in stmt.body.iter().enumerate() {
                let mut path = start_path.clone();
                if stmt.body.len() > 1 {
                    path.position.push(Position::Alternative(alternative_idx));
                }
                if alternative_idx != 0 {
                    let token = if last_level.is_none() || last_level == Some(level) {
                        TraceToken::Alternative
                    } else {
                        TraceToken::PrecedencedAlternative
                    };
                    self.tokens.insert(path.clone(), token);
                }
                last_level = Some(level);
                self.flatten_rhs(path, rhs);
            }
        }
        self.stmt_positions.push(StmtTokenPosition {
            lhs: None,
            position: self.tokens.len(),
        });
    }

    fn flatten_rhs(&mut self, path: Path, rhs: &Rhs) {
        for (rhs_idx, element) in rhs.0.iter().enumerate() {
            let mut path = path.clone();
            // if let Some(bind_id) = element.bind {
            //     path.position.push(Position::Bind(bind_id));
            // }
            match &element.elem {
                &RhsAst::Fragment(_) => {}
                _ => {
                    if rhs.0.len() > 1 {
                        path.position.push(Position::Idx(rhs_idx));
                    }
                }
            }
            match &element.elem {
                &RhsAst::Fragment(fragment_id) => {
                    path.position.push(Position::IdxWithFragment {
                        idx: rhs_idx,
                        fragment: fragment_id,
                    });
                    self.tokens.insert(path, TraceToken::Fragment(fragment_id));
                }
                &RhsAst::String(ref _string) => {
                    // path.position.push(Position::Idx(rhs_idx));
                    // let mut left_quote_path = path.clone();
                    // let mut right_quote_path = path.clone();
                    // left_quote_path.push(Position::LeftQuote);
                    // right_quote_path.push(Position::RightQuote);
                    // self.tokens.insert(left_quote_path, TraceToken::Quote);
                    // self.tokens.insert(right_quote_path, TraceToken::Quote);
                    // let mut regex_rewrite = RegexTranslation::new();
                    unimplemented!()
                }
                &RhsAst::Sequence(ref sequence) => {
                    path.position.push(Position::Sequence {
                        min: sequence.min,
                        max: sequence.max,
                    });
                    if sequence.rhs.0.len() > 1 {
                        let left_paren_path = path.clone();
                        let mut right_paren_path = path.clone();
                        right_paren_path.position.push(Position::SequenceEnd);
                        self.tokens.insert(left_paren_path, TraceToken::LParen);
                        self.tokens.insert(right_paren_path, TraceToken::RParen);
                    }
                    let mut star_or_plus_path = path.clone();
                    star_or_plus_path.position.push(Position::SequenceToken);
                    if sequence.min == 0 && sequence.max == None {
                        self.tokens.insert(star_or_plus_path, TraceToken::Star);
                    } else if sequence.min == 1 && sequence.max == None {
                        self.tokens.insert(star_or_plus_path, TraceToken::Plus);                        
                    }
                    self.flatten_rhs(path, &sequence.rhs);
                }
                &RhsAst::Sum(ref summands) => {
                    if summands.is_empty() {
                        self.tokens.insert(path.clone(), TraceToken::EmptyProduct);
                    }
                    for (summand_idx, summand) in summands.iter().enumerate() {
                        let mut path = path.clone();
                        if summands.len() > 1 {
                            path.position.push(Position::Alternative(summand_idx));
                        }
                        if summand_idx != 0 {
                            self.tokens.insert(path.clone(), TraceToken::Alternative);
                        }
                        self.flatten_rhs(path, summand);
                    }
                }
                &RhsAst::Product(ref rhs) => {
                    let mut left_paren_path = path.clone();
                    let mut right_paren_path = path.clone();
                    left_paren_path.position.push(Position::Idx(0));
                    right_paren_path.position.push(Position::Idx(1));
                    self.tokens.insert(left_paren_path, TraceToken::LParen);
                    self.tokens.insert(right_paren_path, TraceToken::RParen);
                    self.flatten_rhs(path, rhs);
                }
            }
        }
        if rhs.0.is_empty() {
            self.tokens.insert(path, TraceToken::EmptyProduct);
        }
    }

    pub fn traces_for_path(&self, path: &Path) -> (usize, usize) {
        let mut max_path = path.clone();
        max_path.position.push(Position::Max);
        (self.tokens.range(.. path).count(), self.tokens.range(.. max_path).count())
    }

    pub fn tokens(&self) -> &BTreeMap<Path, TraceToken> {
        &self.tokens
    }

    pub fn stringify_tokens(&self, fragment_names: &BTreeMap<FragmentId, String>) -> Vec<String> {
        self.tokens.iter().map(|(_, token)| {
            token.as_str(fragment_names).to_string()
        }).collect()
    }
}