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
#[cfg(test)]
use cfgrammar::Span;
use lrlex::lrlex_mod;
use lrpar::lrpar_mod;
#[cfg(test)]
use lrpar::{Lexeme, Lexer, NonStreamingLexer};

lrlex_mod!("calc_multitypes.l");
lrpar_mod!("calc_multitypes.y");

lrlex_mod!("calc_actiontype.l");
lrpar_mod!("calc_actiontype.y");

lrlex_mod!("calc_noactions.l");
lrpar_mod!("calc_noactions.y");

lrlex_mod!("expect.l");
lrpar_mod!("expect.y");

lrlex_mod!("lexer_lifetime.l");
lrpar_mod!("lexer_lifetime.y");

lrlex_mod!("multitypes.l");
lrpar_mod!("multitypes.y");

lrlex_mod!("parseparam.l");
lrpar_mod!("parseparam.y");

lrlex_mod!("passthrough.l");
lrpar_mod!("passthrough.y");

lrlex_mod!("span.l");
lrpar_mod!("span.y");

#[test]
fn multitypes() {
    let lexerdef = multitypes_l::lexerdef();
    let lexer = lexerdef.lexer("aa");
    let (r, errs) = multitypes_y::parse(&lexer);
    assert_eq!(r.unwrap().len(), 2);
    assert_eq!(errs.len(), 0);
}

#[test]
fn test_no_actions() {
    let lexerdef = calc_noactions_l::lexerdef();
    let lexer = lexerdef.lexer("2+3");
    if !calc_noactions_y::parse(&lexer).is_empty() {
        panic!();
    }
    let lexer = lexerdef.lexer("2++3");
    if calc_noactions_y::parse(&lexer).len() != 1 {
        panic!();
    }
}

#[test]
fn test_basic_actions() {
    let lexerdef = calc_actiontype_l::lexerdef();
    let lexer = lexerdef.lexer("2+3");
    match calc_actiontype_y::parse(&lexer) {
        (Some(Ok(5)), ref errs) if errs.is_empty() => (),
        _ => unreachable!(),
    }
}

#[test]
fn test_error_recovery_and_actions() {
    use lrpar::LexParseError;

    let lexerdef = calc_actiontype_l::lexerdef();

    let lexer = lexerdef.lexer("2++3");
    let (r, errs) = calc_actiontype_y::parse(&lexer);
    match r {
        Some(Ok(5)) => (),
        _ => unreachable!(),
    }
    match errs[0] {
        LexParseError::ParseError(..) => (),
        _ => unreachable!(),
    }

    let lexer = lexerdef.lexer("2+3)");
    let (r, errs) = calc_actiontype_y::parse(&lexer);
    assert_eq!(r, Some(Ok(5)));
    assert_eq!(errs.len(), 1);
    match errs[0] {
        LexParseError::ParseError(..) => (),
        _ => unreachable!(),
    }

    let lexer = lexerdef.lexer("2+3+18446744073709551616");
    let (r, errs) = calc_actiontype_y::parse(&lexer);
    assert_eq!(r, Some(Err(())));
    assert!(errs.is_empty());
}

#[test]
fn test_calc_multitypes() {
    let lexerdef = calc_multitypes_l::lexerdef();
    let lexer = lexerdef.lexer("1+2*3");
    let (res, _errs) = calc_multitypes_y::parse(&lexer);
    assert_eq!(res, Some(Ok(7)));

    let lexer = lexerdef.lexer("1++2");
    let (res, _errs) = calc_multitypes_y::parse(&lexer);
    assert_eq!(res, Some(Ok(3)));
}

#[test]
fn test_input_lifetime() {
    // This test only exists to make sure that this code compiles: there's no need for us to
    // actually run anything.
    let lexerdef = lexer_lifetime_l::lexerdef();
    let input = "a";
    let _ = {
        let lexer = lexerdef.lexer(input);
        let lx = lexer.iter().next().unwrap().unwrap();
        lexer.span_str(lx.span())
    };
}

#[test]
fn test_lexer_lifetime() {
    // This test only exists to make sure that this code compiles: there's no need for us to
    // actually run anything.

    #[allow(clippy::needless_lifetimes)]
    pub(crate) fn parse_data<'a>(input: &'a str) -> Option<&'a str> {
        let lexer_def = crate::lexer_lifetime_l::lexerdef();
        let l = lexer_def.lexer(input);
        match crate::lexer_lifetime_y::parse(&l) {
            (Option::Some(x), _) => Some(x),
            _ => None,
        }
    }
    parse_data("abc");
}

#[test]
fn test_span() {
    let lexerdef = span_l::lexerdef();
    let lexer = lexerdef.lexer("2+3");
    match span_y::parse(&lexer) {
        (Some(ref spans), _)
            if spans
                == &vec![
                    Span::new(0, 1),
                    Span::new(0, 1),
                    Span::new(0, 1),
                    Span::new(2, 3),
                    Span::new(2, 3),
                    Span::new(0, 3),
                ] => {}
        _ => unreachable!(),
    }

    let lexer = lexerdef.lexer("2 + 3");
    match span_y::parse(&lexer) {
        (Some(ref spans), _)
            if spans
                == &vec![
                    Span::new(0, 1),
                    Span::new(0, 1),
                    Span::new(0, 1),
                    Span::new(4, 5),
                    Span::new(4, 5),
                    Span::new(0, 5),
                ] => {}
        _ => unreachable!(),
    }

    let lexer = lexerdef.lexer("2+3*4");
    match span_y::parse(&lexer) {
        (Some(ref spans), _)
            if spans
                == &vec![
                    Span::new(0, 1),
                    Span::new(0, 1),
                    Span::new(0, 1),
                    Span::new(2, 3),
                    Span::new(2, 3),
                    Span::new(4, 5),
                    Span::new(2, 5),
                    Span::new(0, 5),
                ] => {}
        _ => unreachable!(),
    }

    let lexer = lexerdef.lexer("2++3");
    match span_y::parse(&lexer) {
        (Some(ref spans), _)
            if spans
                == &vec![
                    Span::new(0, 1),
                    Span::new(0, 1),
                    Span::new(0, 1),
                    Span::new(3, 4),
                    Span::new(3, 4),
                    Span::new(0, 4),
                ] => {}
        _ => unreachable!(),
    }

    let lexer = lexerdef.lexer("(2)))");
    match dbg!(span_y::parse(&lexer)) {
        (Some(ref spans), _)
            if spans
                == &vec![
                    Span::new(1, 2),
                    Span::new(1, 2),
                    Span::new(1, 2),
                    Span::new(0, 3),
                    Span::new(0, 3),
                    Span::new(0, 3),
                ] => {}
        _ => unreachable!(),
    }
}

#[test]
fn test_parseparam() {
    let lexerdef = parseparam_l::lexerdef();
    let lexer = lexerdef.lexer("101");
    match parseparam_y::parse(&lexer, &3) {
        (Some(i), _) if i == 104 => (),
        _ => unreachable!(),
    }
}

#[test]
fn test_passthrough() {
    let lexerdef = passthrough_l::lexerdef();
    let lexer = lexerdef.lexer("101");
    match passthrough_y::parse(&lexer) {
        (Some(Ok(ref s)), _) if s == "$101" => (),
        _ => unreachable!(),
    }
}

#[test]
fn test_expect() {
    // This test merely needs to compile in order to be successful.
}