Skip to content

Instantly share code, notes, and snippets.

@anru
Created May 29, 2019 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anru/1c0681620f041604cff36093b549a647 to your computer and use it in GitHub Desktop.
Save anru/1c0681620f041604cff36093b549a647 to your computer and use it in GitHub Desktop.
syntax-rust
/**
* 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