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
use std::iter;

use crate::grammar::parse_tree::*;
use crate::grammar::pattern::*;
use crate::tok;

#[cfg(not(feature = "test"))]
#[rustfmt::skip]
#[allow(dead_code)]
#[allow(clippy::all)]
mod lrgrammar;

#[cfg(feature = "test")]
lalrpop_mod!(
    #[rustfmt::skip]
    #[allow(dead_code)]
    #[allow(clippy::all)]
    lrgrammar,
    "/src/parser/lrgrammar.rs"
);

#[cfg(test)]
mod test;

pub enum Top {
    Grammar(Grammar),
    Pattern(Pattern<TypeRef>),
    MatchMapping(MatchMapping),
    TypeRef(TypeRef),
    GrammarWhereClauses(Vec<WhereClause<TypeRef>>),
}

pub type ParseError<'input> = lalrpop_util::ParseError<usize, tok::Tok<'input>, tok::Error>;

macro_rules! parser {
    ($input:expr, $offset:expr, $pat:ident, $tok:ident) => {{
        let input = $input;
        let tokenizer =
            iter::once(Ok((0, tok::Tok::$tok, 0))).chain(tok::Tokenizer::new(input, $offset));
        lrgrammar::TopParser::new()
            .parse(input, tokenizer)
            .map(|top| match top {
                Top::$pat(x) => x,
                _ => unreachable!(),
            })
    }};
}

pub fn parse_grammar(input: &str) -> Result<Grammar, ParseError<'_>> {
    let mut grammar = parser!(input, 0, Grammar, StartGrammar)?;

    // find a unique prefix that does not appear anywhere in the input
    while input.contains(&grammar.prefix) {
        grammar.prefix.push('_');
    }

    Ok(grammar)
}

fn parse_pattern(input: &str, offset: usize) -> Result<Pattern<TypeRef>, ParseError<'_>> {
    parser!(input, offset, Pattern, StartPattern)
}

fn parse_match_mapping(input: &str, offset: usize) -> Result<MatchMapping, ParseError<'_>> {
    parser!(input, offset, MatchMapping, StartMatchMapping)
}

#[cfg(test)]
pub fn parse_type_ref(input: &str) -> Result<TypeRef, ParseError<'_>> {
    parser!(input, 0, TypeRef, StartTypeRef)
}

#[cfg(test)]
pub fn parse_where_clauses(input: &str) -> Result<Vec<WhereClause<TypeRef>>, ParseError<'_>> {
    parser!(input, 0, GrammarWhereClauses, StartGrammarWhereClauses)
}