Skip to content

Instantly share code, notes, and snippets.

@bdowning
Created September 19, 2018 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdowning/4dca9f006f230bc7e8c18a2b9c95ff8e to your computer and use it in GitHub Desktop.
Save bdowning/4dca9f006f230bc7e8c18a2b9c95ff8e to your computer and use it in GitHub Desktop.
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