Skip to content
intersystems-communityPublic

About

TYpes into Grammar Routines (Parser, Printer, Presentation in BNF)

Resources

Contributing

Security policy

Stars

11 stars

Watchers

1 watching

Forks

Repository files navigation

🐯 tygr: TYpe into Grammar Routines

Define your grammar once as Rust types and get a parser, printer, and presentation in EBNF for free.

Get Started

Start with the crate documentation on docs.rs: the API and grammar concepts, plus a complete arithmetic grammar with parsing, printing, and EBNF generation.

For a runnable example, see examples/json.rs: the full JSON grammar of json.org, with a small CLI around it. It parses input into a syntax tree, a value of the grammar's types, or says where the input went wrong and what could have come next:

$ echo '{"a": [1, true]}' | cargo run --example json -- parse
Json(
    Wrap {
        prefix: Ws(
            [],
        ),
        wrapped: Object(
            ...
$ echo '{"a" [1, true]}' | cargo run --example json -- parse
parse error at byte 5, expected:
	- " "
	- ":"
	- "\n"
	- "\r"
	- "\t"

The grammar also prints itself as EBNF:

$ cargo run --example json -- bnf
Json = Ws Value Ws .
Ws = { " " | "\n" | "\r" | "\t" } .
Value = Object | Array | String | Number | "true" | "false" | "null" .
Object = "{" Ws [ Members ] "}" .
Members = Member Ws { "," Ws Member Ws } .
Member = String Ws ":" Ws Value .
String = "\"" Characters "\"" .
Characters = { Character } .
Character = '0x20-0x10FFFF except for " and \\' | "\\" Escape .
Escape = "\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t" | "u" Hex Hex Hex Hex .
Hex = Digit | 'A-F' | 'a-f' .
Digit = "0" | OneNine .
OneNine = '1-9' .
Array = "[" Ws [ Elements ] "]" .
Elements = Value Ws { "," Ws Value Ws } .
Number = Integer [ Fraction ] [ Exponent ] .
Integer = [ "-" ] ( OneNine Digits | Digit ) .
Digits = Digit { Digit } .
Fraction = "." Digits .
Exponent = ( "E" | "e" ) [ Sign ] Digits .
Sign = "+" | "-" .

Related Work

RELATED_WORK.md places tygr among the tools that derive several routines from one grammar, and among Rust parser libraries by how their grammars are written.

Contributing

Please open an issue before starting a pull request. See CONTRIBUTING.md for the development workflow.

License

This software is provided AS-IS, with no implied warranties, support, or conditions of any kind.

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

TYpes into Grammar Routines (Parser, Printer, Presentation in BNF)

Resources

Contributing

Security policy

Stars

11 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages