Augmented Backus-Naur Form (ABNF) parsing. See RFC 5234.
npm install abnfFor more information on the flavor of ABNF
(Augmented Backus-Naur Form) supported by this project,
see RFC 5234
and RFC 7405.
npm install -g abnf
import { parseFile } from "abnf";
const rules = await parseFile("myfile.abnf");
There are a few binaries included:
Check the given ABNF file for correctness.
``txt
Usage: abnf_check [options] [abnfFile...]
Check ABNF files for syntax, unused rules, and undefined rules
Options:
-h, --help display help for command
`
Output the generated abstract syntax tree for the ABNF input. This output is
mostly diagnostic in nature, not really meant to be parsed.
`txt
Usage: abnf_ast [options] [abnfFile...]
Output all of the rules derived from a given ABNF file
Options:
-l,--location don't remove location information
-h, --help display help for command
`
Generate a Peggy or Pest grammar
from the ABNF. The idea is that you could then annotate this grammar with
actions in order to create a useful parser.
`txt
Usage: abnf_gen [options] [abnfFile...]
Create a grammar from an ABNF file
Arguments:
abnfFile ABNF files to turn into grammars.
Options:
-f, --format
"peggy")
-s, --startRule
first rule in ABNF grammar. Can be specified
multiple times.
--stubs Generate stubs for rules that do not exist, rather
than failing.
-o, --output
name if not specified. (default: "stdin.peggy")
-u, --unused Output rules that are not reachable from the start
rule
-c, --core Include core rules from RFC 5234, Appendix B.
-h, --help display help for command
`
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.
`txt
Usage: abnf_test [options] [abnfFile...]
Send test inputs to an ABNF grammar
Arguments:
abnfFile The ABNF to test.
Options:
-o, --output Output grammar source, if not testing. Generated
from peggyFile name if needed.
-s, --startRule
-t, --test
-T, --testFile
--trace Turn on peggy tracing
-h, --help display help for command
`
`sh`
$ 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
| ^
Parse the file with the given name, returning a promise for a Rules object.
Parse the given string and return a Rules object. The grammarSource is
the name of the file that the input came from.
Read the stream, parse it, and return a promise for a Rules object. The
grammarSource is the name of the file that the input came from.
This is used by the abnf_check` utility, and returns null if there are no reference errors, otherwise returns an array of error strings. Checks for unused or undefined rules.
The name of the first rule in the input grammar.
A hash of Rule objects indexed by uppercase rulename.
An array of RuleRef objects.
The name of the rule
The Peggy location in the input file where the rule name was defined
The definition of the rule. More information forthcoming.
The name of the rule that was referenced
The Peggy location in the input file where the rule name was referenced.
---

