@tabnas/ebnf
v0.1.17
Published
EBNF grammar compiler for the tabnas parser (W3C dialect, best effort).
Downloads
1,767
Maintainers
Readme
@tabnas/ebnf
EBNF grammar compiler for the Tabnas
parser: takes EBNF source in the W3C dialect and emits a tabnas
GrammarSpec.
This is a best-effort front-end. It implements one dialect properly
and refuses the rest by name: ISO/IEC 14977 subtraction (A - B),
special sequences (? … ?) and bracket repetition ({ A }) all raise a
named error rather than compiling to something plausible. The ISO option
[ A ] is the one to watch, because [ … ] is a character class in
this dialect: write A?. The itemised list is in
doc/reference.md; read
it before writing a grammar.
Postfix operators may be stacked, but every unbounded * or + layer
must consume input on each iteration. A form such as (A?)* is refused
at compile time instead of emitting a loop that can stay at one input
position forever.
Install
npm install @tabnas/parser @tabnas/bnf @tabnas/ebnf@tabnas/parser (the engine) and @tabnas/bnf (the shared compiler)
are peer dependencies.
One example
const { Tabnas } = require('@tabnas/parser')
const { ebnf } = require('@tabnas/ebnf')
const tn = new Tabnas({ plugins: [ebnf] })
tn.ebnf(`
Expr ::= Term ( ( "+" | "-" ) Term )*
Term ::= Factor ( ( "*" | "/" ) Factor )*
Factor ::= NR | "(" Expr ")"
`)
tn.parse('1 + 2 * 3').src // => '1+2*3'
tn.parse('(1+2)*3').rule // => 'Expr'Build the instance once and reuse it: compiling the grammar is the
expensive part. tn.ebnf.toSpec(src) builds the spec without installing
it.
Documentation
Four-quadrant Diátaxis docs:
| Quadrant | File | |---|---| | Tutorial (learn) | doc/tutorial.md | | How-to guide (tasks) | doc/guide.md | | Reference (API + dialect) | doc/reference.md | | Concepts (why) | doc/concepts.md |
Repo-level orientation, including the full supported/unsupported table, is in the top-level README.
License
MIT. Copyright (c) Richard Rodger.
