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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use crate::codegen_prelude::{ParsePairExpression, ParsePairSort};
use crate::parser::peg::parse_error::PEGParseError;
use crate::parser::peg::parser_core_ast::{CoreAst, CoreExpression, ParsePairRaw};
use crate::parser::peg::parser_core_file;
use crate::parser::peg::parser_sugar_ast::{Annotation, Expression, Sort, SyntaxFileAst};
use crate::sources::character_class::CharacterClass;
use crate::sources::source_file::SourceFile;
use itertools::Itertools;
use std::collections::HashMap;

/// Parse a file by:
/// 1. Desugaring the AST to core syntax
/// 2. Parsing the source file using core syntax
/// 3. Resugaring the resulting ParsePairRaw
pub fn parse_file<'src>(
    ast: &'src SyntaxFileAst,
    file: &'src SourceFile,
) -> (ParsePairSort<'src>, Vec<PEGParseError>) {
    //Desugar
    let core_ast = desugar_ast(ast);

    //Parse
    let (res, errs) = parser_core_file::parse_file(&core_ast, file);

    //Resugar
    let starting_sort = ast.sorts.get(&ast.starting_sort).unwrap();
    (resugar_sort(ast, starting_sort, res), errs)
}

fn desugar_ast(ast: &SyntaxFileAst) -> CoreAst {
    let mut sorts = HashMap::new();
    //Insert all sorts
    ast.sorts.values().for_each(|s| {
        sorts.insert(&s.name[..], desugar_sort(s));
    });
    //If there is no layout sort, insert one
    if !sorts.contains_key("layout") {
        sorts.insert(
            "layout",
            CoreExpression::CharacterClass(CharacterClass::Nothing),
        );
    }

    CoreAst {
        sorts,
        starting_sort: &ast.starting_sort,
    }
}

fn desugar_sort(sort: &Sort) -> CoreExpression {
    CoreExpression::Choice(
        sort.constructors
            .iter()
            .map(|c| {
                let mut base = desugar_expr(&c.expression);
                if c.annotations.contains(&Annotation::NoLayout) {
                    base = CoreExpression::FlagNoLayout(Box::new(base));
                    base = CoreExpression::FlagNoErrors(
                        Box::new(base),
                        String::from_iter([&sort.name, ".", &c.name]),
                    );
                }
                base
            })
            .collect(),
    )
}

fn desugar_expr(expr: &Expression) -> CoreExpression {
    match expr {
        Expression::Sort(name) => CoreExpression::Name(&name[..]),
        Expression::Sequence(constructors) => {
            CoreExpression::Sequence(constructors.iter().map(desugar_expr).collect_vec())
        }
        Expression::Repeat { e: c, min, max } => CoreExpression::Repeat {
            subexpr: Box::new(desugar_expr(c)),
            min: *min,
            max: *max,
        },
        Expression::CharacterClass(cc) => CoreExpression::CharacterClass(cc.clone()),
        Expression::Choice(constructors) => {
            CoreExpression::Choice(constructors.iter().map(desugar_expr).collect_vec())
        }
        //Literals are desugared to a sequence of character classes
        Expression::Literal(lit) => {
            CoreExpression::FlagNoLayout(Box::new(CoreExpression::FlagNoErrors(
                Box::new(CoreExpression::Sequence(
                    lit.chars()
                        .map(|c| CoreExpression::CharacterClass(c.into()))
                        .collect_vec(),
                )),
                String::from_iter(["'", lit, "'"]),
            )))
        }
        Expression::Negative(_) => {
            todo!()
        }
        Expression::Positive(_) => {
            todo!()
        }
        Expression::Delimited {
            e,
            delim,
            min,
            max,
            trailing,
        } => {
            let e = desugar_expr(e);
            let delim = desugar_expr(delim);

            let mut options = vec![];
            //Can parse count > 0
            if max.is_none() || max.unwrap() > 0 {
                options.push(CoreExpression::Sequence(vec![
                    e.clone(),
                    CoreExpression::Repeat {
                        subexpr: Box::new(CoreExpression::Sequence(vec![delim.clone(), e.clone()])),
                        min: min.saturating_sub(1),
                        max: max.map(|max| max.saturating_sub(1)),
                    },
                ]));
            }
            //Can parse count == 0
            if *min == 0 {
                options.push(CoreExpression::Sequence(vec![]));
            }

            let choice = CoreExpression::Choice(options);
            if *trailing {
                CoreExpression::Sequence(vec![
                    choice,
                    CoreExpression::Repeat {
                        subexpr: Box::new(delim),
                        min: 0,
                        max: Some(1),
                    },
                ])
            } else {
                CoreExpression::Sequence(vec![choice])
            }
        }
    }
}

fn resugar_sort<'src>(
    ast: &'src SyntaxFileAst,
    sort: &'src Sort,
    pair: ParsePairRaw,
) -> ParsePairSort<'src> {
    match pair {
        ParsePairRaw::Choice(_, i, subpair) => ParsePairSort {
            sort: &sort.name[..],
            constructor_name: &sort.constructors[i].name[..],
            constructor_value: resugar_expr(ast, &sort.constructors[i].expression, *subpair),
        },
        ParsePairRaw::Error(span) => ParsePairSort {
            sort: &sort.name[..],
            constructor_name: "ERROR",
            constructor_value: ParsePairExpression::Error(span),
        },
        _ => unreachable!(),
    }
}

fn resugar_expr<'src>(
    ast: &'src SyntaxFileAst,
    sort: &'src Expression,
    pair: ParsePairRaw,
) -> ParsePairExpression<'src> {
    match (sort, pair) {
        (Expression::Sort(name), ParsePairRaw::Name(span, val)) => ParsePairExpression::Sort(
            span,
            Box::new(resugar_sort(ast, ast.sorts.get(name).unwrap(), *val)),
        ),
        (Expression::Sequence(exprs), ParsePairRaw::List(span, vals)) => ParsePairExpression::List(
            span,
            exprs
                .iter()
                .zip(vals.into_iter())
                .map(|(e, v)| resugar_expr(ast, e, v))
                .collect_vec(),
        ),
        (Expression::Repeat { e: c, .. }, ParsePairRaw::List(span, vals)) => {
            ParsePairExpression::List(
                span,
                vals.into_iter()
                    .map(|v| resugar_expr(ast, c, v))
                    .collect_vec(),
            )
        }
        (Expression::CharacterClass(_), ParsePairRaw::Empty(span)) => {
            ParsePairExpression::Empty(span)
        }
        (Expression::Choice(constructors), ParsePairRaw::Choice(span, i, expr)) => {
            ParsePairExpression::Choice(
                span,
                i,
                Box::new(resugar_expr(ast, &constructors[i], *expr)),
            )
        }
        (Expression::Literal(_), ParsePairRaw::List(span, _)) => ParsePairExpression::Empty(span),
        (Expression::Delimited { e, max, .. }, ParsePairRaw::List(span, list)) => {
            //If max is 0, empty list
            if !max.is_none() && max.unwrap() == 0 {
                return ParsePairExpression::List(span, vec![]);
            };
            //Get choice
            let (i, choice) =
                if let ParsePairRaw::Choice(_, i, choice) = list.into_iter().next().unwrap() {
                    (i, choice)
                } else {
                    return ParsePairExpression::Error(span);
                };
            //If choice was not 0, empty list
            if i != 0 {
                return ParsePairExpression::List(span, vec![]);
            };
            //Find elements inside choice
            let seq = if let ParsePairRaw::List(_, seq) = *choice {
                seq
            } else {
                return ParsePairExpression::Error(span);
            };

            let mut result = vec![];
            let mut seq_iter = seq.into_iter();

            //Inside choice is first an expr, then a repeat of seq (delim, expr)
            //We first find the first expr
            let seq0 = seq_iter.next().unwrap();
            result.push(resugar_expr(ast, e, seq0));

            //See if the rest of the expr is present
            let next = seq_iter.next();
            if next.is_none() {
                return ParsePairExpression::List(span, result);
            }
            //It is present, lets get the list of them
            let seq1 = if let ParsePairRaw::List(_, list) = next.unwrap() {
                list
            } else {
                return ParsePairExpression::Error(span);
            };
            //Map each element in the list to get the expr
            seq1.into_iter().for_each(|pair| {
                result.push(if let ParsePairRaw::List(span, list) = pair {
                    if list.len() < 2 {
                        ParsePairExpression::Error(span)
                    } else {
                        resugar_expr(ast, e, list.into_iter().nth(1).unwrap())
                    }
                } else {
                    ParsePairExpression::Error(pair.span())
                });
            });

            ParsePairExpression::List(span, result)
        }
        (_, ParsePairRaw::Error(span)) => ParsePairExpression::Error(span),
        (_, _) => unreachable!(),
    }
}