Convert ABNF like grammar to rust-peg build rowan lossless syntax tree
Using rowan library lossless syntax tree
How To Use
Add dependencies:
char-classes = "0.3.4"
peg = "0.8.5"
rowan = "0.15.15"
rowan-peg-utils = "0.2.3"
Edit your grammar declaration:
exports [ pair-list ]
pair-list = whitespace *pair
pair = key whitespace "=" whitespace val whitespace
key = text
val = number / text
number = int ["." int]
int = $(+<0-9>) @int ; Add `int` to the expected set when parsing fails
text = $(+<a-zA-Z_->) @text
whitespace = $(*< \t\r\n>)
Generate grammar from your grammar declaration:
Example
Cargo build
If you want to use cargo to build grammar, you can use rowan-peg-macro
A simple json parser example-json
cd examples/example-json
cargo run -- example.json
[examples/example-json/src/main.rs:23:5] &json = JsonText(
JSON_TEXT@0..476
WS@0..0 ""
VALUE@0..476
OBJECT@0..476
BEGIN_OBJECT@0..6
WS@0..0 ""
L_CURLY@0..1 "{"
WS@1..6 "\n "
MEMBER@6..136
STRING@6..15
QUOTATION_MARK@6..7 "\""
CHAR@7..8
UNESCAPED@7..8 "s"
CHAR@8..9
UNESCAPED@8..9 "t"
CHAR@9..10
...
...
Generate Grammar Declaration
cargo run -- examples/example-json/json.abnf