-
Notifications
You must be signed in to change notification settings - Fork 88
/
calc.rs.g
53 lines (45 loc) · 1.07 KB
/
calc.rs.g
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
/**
* Generated parser in Rust.
*
* ./bin/syntax -g examples/calc.rs.g -m lalr1 -o lib.rs
*
* use syntax::Parser;
*
* let parser = Parser::new();
*
* println!("{:?}", parser.parse("2 + 2 * 2")); // 6
* println!("{:?}", parser.parse("(2 + 2) * 2")); // 8
*/
{
"lex": {
"rules": [
["\\s+", '/* skip whitespace */ ""'],
["\\d+", '"NUMBER"'],
["\\*", '"*"'],
["\\+", '"+"'],
["\\(", '"("'],
["\\)", '")"'],
]
},
"operators": [
["left", "+"],
["left", "*"],
],
"moduleInclude": `
type TResult = i32;
fn on_parse_begin(_parser: &mut Parser, string: &str) {
println!("on_parse_begin: {:?}", string);
}
fn on_parse_end(_parser: &mut Parser, parsed: &TResult) {
println!("on_parse_end: {:?}", parsed);
}
`,
"bnf": {
"E": [
["E + E", "|$1: i32, $3: i32| -> i32; $$ = $1 + $3"],
["E * E", "|$1: i32, $3: i32| -> i32; $$ = $1 * $3"],
["NUMBER", "|| -> i32; $$ = yytext.parse::<i32>().unwrap()"],
["( E )", "$$ = $2"],
],
},
}