Skip to content

Instantly share code, notes, and snippets.

@bdowning bdowning/grammar.js
Created Sep 19, 2018

Embed
What would you like to do?
const grammar = compileGrammar`
Expr
= Alternation ${ nth(1) }
Alternation
= Alternation '|' Sequence ${ leftAssoc(pick('alternation', 1, 3)) }
| Sequence ${ nth(1) }
Sequence
= Sequence Kleene ${ leftAssoc(pick('seq', 1, 2)) }
| Kleene ${ nth(1) }
Kleene
= Element '*' ${ pick('kleene', 1) }
| Element '?' ${ pick('alternation', 1, null) }
| Element '+' ${ pick('plus', 1) }
| Element '{' 'int' '}' ${ pick('repeat', 1, 3) }
| Element '{' 'int' ',' '}' ${ pick('repeat', 1, 3, true) }
| Element ${ nth(1) }
Element
= '(' Expr ')' ${ pick('group', 2) }
| Charset ${ nth(1) }
| '.' ${ () => 'any' }
| 'char' ${ nth(1) }
Charset
= '[' CharsetInner ']' ${ pick('charset', -2) }
| '[' '^' CharsetInner ']' ${ pick('charset^', -3) }
CharsetInner
= CharsetInner 'char' '-' 'char' ${ pick(-1, ['range', 2, 4]) }
| CharsetInner 'char' ${ pick(-1, 2) }
| null ${ () => [] }
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.