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
// Copyright 2022 Gregory Petrosyan <pgregory@pgregory.net>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#![forbid(unsafe_code)]

use lalrpop_util::lalrpop_mod;

mod ast;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Error {
    InputTooBig,
}

lalrpop_mod!(
    #[allow(dead_code, clippy::all, clippy::pedantic, clippy::unwrap_used)]
    fennec
);

#[test]
fn lalrpop_example() {
    use lalrpop_util::ParseError;

    let mut errors = Vec::new();

    let expr = fennec::ExprsParser::new().parse(&mut errors, "2147483648");
    assert!(expr.is_err());
    assert_eq!(
        expr.unwrap_err(),
        ParseError::User {
            error: Error::InputTooBig
        }
    );

    let expr = fennec::ExprsParser::new()
        .parse(&mut errors, "22 * + 3")
        .unwrap();
    assert_eq!(&format!("{:?}", expr), "[((22 * error) + 3)]");

    let expr = fennec::ExprsParser::new()
        .parse(&mut errors, "22 * 44 + 66, *3")
        .unwrap();
    assert_eq!(&format!("{:?}", expr), "[((22 * 44) + 66), (error * 3)]");

    let expr = fennec::ExprsParser::new().parse(&mut errors, "*").unwrap();
    assert_eq!(&format!("{:?}", expr), "[(error * error)]");

    assert_eq!(errors.len(), 4);
}