6 releases
0.1.5 | Feb 10, 2025 |
---|---|
0.1.4 | Jun 28, 2024 |
0.1.2 | Apr 29, 2024 |
0.1.1 | Nov 26, 2023 |
#110 in Parser tooling
126 downloads per month
88KB
2K
SLoC
bnf_rules!
LR(1) parser generator
Generate LR(1) parser at compilation time.
use bnf_rules::bnf_rules_macro::bnf_rules;
// Grammar
bnf_rules!(
source ::= expr
expr ::= factor { "+" factor }
factor ::= "-" primary | primary
primary ::= "(" expr ")" | number
number ::= r"\d+" // regex
);
pub fn parse() {
// A function named "parse_source" is automatically generated.
let ast_node: Result<ASTNode, ParseError> = parse_source("(100 + 200) + -100");
dbg!(ast_node.unwrap());
}
Usage
bnf_rules = "0.1.5"
Extended BNF
Form | Semantic |
---|---|
source | An entire input source. |
ident | A non-terminal symbol named "ident". |
"something" | A terminal symbol for text. |
r"\d+" | A terminal symbol for regex. |
fn (function_name) | A custom tokenizer with user function.[^1] |
{ pattern } | Zero or more repetitions of "pattern". |
[ pattern ] | "pattern" or null. |
pattern1 | pattern2 | "pattern1" or "pattern2". |
( patterns ) | A group of patterns. |
[^1]: Generic parameters are also available.
Example 1: https://github.com/bea4dev/bnf_rules/blob/master/src/lib.rs
Example 2: https://github.com/bea4dev/catla/blob/master/catla_parser/src/grammar.rs
Dependencies
~3.5–5MB
~96K SLoC