fennec_parser/
lib.rs

1// Copyright 2022 Gregory Petrosyan <pgregory@pgregory.net>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7#![forbid(unsafe_code)]
8
9use lalrpop_util::lalrpop_mod;
10
11mod ast;
12
13#[derive(Debug, Copy, Clone, PartialEq, Eq)]
14pub enum Error {
15    InputTooBig,
16}
17
18lalrpop_mod!(
19    #[allow(dead_code, clippy::all, clippy::pedantic, clippy::unwrap_used)]
20    fennec
21);
22
23#[test]
24fn lalrpop_example() {
25    use lalrpop_util::ParseError;
26
27    let mut errors = Vec::new();
28
29    let expr = fennec::ExprsParser::new().parse(&mut errors, "2147483648");
30    assert!(expr.is_err());
31    assert_eq!(
32        expr.unwrap_err(),
33        ParseError::User {
34            error: Error::InputTooBig
35        }
36    );
37
38    let expr = fennec::ExprsParser::new()
39        .parse(&mut errors, "22 * + 3")
40        .unwrap();
41    assert_eq!(&format!("{:?}", expr), "[((22 * error) + 3)]");
42
43    let expr = fennec::ExprsParser::new()
44        .parse(&mut errors, "22 * 44 + 66, *3")
45        .unwrap();
46    assert_eq!(&format!("{:?}", expr), "[((22 * 44) + 66), (error * 3)]");
47
48    let expr = fennec::ExprsParser::new().parse(&mut errors, "*").unwrap();
49    assert_eq!(&format!("{:?}", expr), "[(error * error)]");
50
51    assert_eq!(errors.len(), 4);
52}