Parse ABNF grammars
For more information on the flavor of ABNF
(Augmented Backus-Naur Form) supported by this project,
see RFC 5234
and RFC 7405.Installation:
npm install -g abnf
Example:
import { parseFile } from "abnf";
const rules = await parseFile("myfile.abnf");
CLI
There are a few binaries included:abnfcheck
Check the given ABNF file for correctness.abnfast
Output the generated abstract syntax tree for the ABNF input. This output is mostly diagnostic in nature, not really meant to be parsed.abnfgen
Generate a Peggy grammar from the ABNF. The idea is that you could then annotate this grammar with actions in order to create a useful parser.abnftest
Using an ABNF, test inputs to see if they match. Returns the Peggy parse tree, which will likely be somewhat confusing until you're familiar with Peggy.Workflow
$ cat << EOF > foo.abnf
f = "abc"
EOF
$ abnf_gen foo.abnf
$ cat foo.peggy
f
= "abc"i
$ abnf_test foo.abnf -t abc
'abc'
$ abnf_test foo.peggy -t ab
Error: Expected "abc" but "a" found.
--> command line:1:1
|
1 | ab
| ^
API
.parseFile(input)
Parse the file with the given name, returning a promise for a Rules object..parseString(input, grammarSource = "unknown")
Parse the given string and return a Rules object. ThegrammarSource
is
the name of the file that the input came from..parseStream(stream, grammarSource = "stdin")
Read the stream, parse it, and return a promise for a Rules object. ThegrammarSource
is the name of the file that the input came from.