https://github.com/hildjj/node-abnf
Augmented Backus-Naur Form (ABNF) parsing for node.js. See RFC 5234.
https://github.com/hildjj/node-abnf
Last synced: about 2 months ago
JSON representation
Augmented Backus-Naur Form (ABNF) parsing for node.js. See RFC 5234.
- Host: GitHub
- URL: https://github.com/hildjj/node-abnf
- Owner: hildjj
- License: apache-2.0
- Created: 2012-05-29T08:00:40.000Z (about 13 years ago)
- Default Branch: main
- Last Pushed: 2025-01-19T15:49:02.000Z (4 months ago)
- Last Synced: 2025-03-31T10:09:30.407Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 358 KB
- Stars: 26
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Parse ABNF grammars
For more information on the flavor of ABNF
([Augmented Backus-Naur Form](https://en.wikipedia.org/wiki/Augmented_Backus%E2%80%93Naur_form)) supported by this project,
see [RFC 5234](http://tools.ietf.org/html/rfc5234)
and [RFC 7405](https://www.rfc-editor.org/rfc/rfc7405).## Installation:
npm install -g abnf
## Example:
import { parseFile } from "abnf";
const rules = await parseFile("myfile.abnf");## CLI
There are a few binaries included:
### abnf_check
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
```### abnf_ast
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
```### abnf_gen
Generate a [Peggy](https://peggyjs.org/) or [Pest](https://pest.rs/) 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 Output format (choices: "peggy", "pest", default:
"peggy")
-s, --startRule Start rule for generated grammar. Defaults to
first rule in ABNF grammar. Can be specified
multiple times.
--stubs Generate stubs for rules that do not exist, rather
than failing.
-o, --output Output grammar file name. Derived from input file
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
```### abnf_test
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 When testing, use this as the start rule.
-t, --test String to check against grammar.
-T, --testFile File contents to check against grammar.
--trace Turn on peggy tracing
-h, --help display help for command
```## Suggested Workflow
```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
| ^
```## 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. The `grammarSource` 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. The
`grammarSource` is the name of the file that the input came from.### .checkRefs(rules)
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.
## Returned Rules object shape
### Rules.first
The name of the first rule in the input grammar.
### Rules.defs
A hash of Rule objects indexed by uppercase rulename.
### Rules.refs
An array of RuleRef objects.
### Rule.name
The name of the rule
### Rule.loc
The Peggy location in the input file where the rule name was defined
### Rule.def
The definition of the rule. More information forthcoming.
### RuleRef.name
The name of the rule that was referenced
### RuleRef.loc
The Peggy location in the input file where the rule name was referenced.
---
[](https://github.com/hildjj/node-abnf/actions/workflows/node.js.yml)
[](https://codecov.io/gh/hildjj/node-abnf)