Created
May 29, 2019 19:12
-
-
Save anru/1c0681620f041604cff36093b549a647 to your computer and use it in GitHub Desktop.
syntax-rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 = &'static str; | |
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: &'static str, $3: &'static str| -> &'static str; $$ = $1"], | |
["E * E", "|$1: &'static str, $3: &'static str| -> &'static str; $$ = $1"], | |
["NUMBER", "|| -> &'static str; $$ = yytext"], | |
["( E )", "$$ = $2"], | |
], | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment