Ordeal::Model::Parser: grammar

TL;DR

Here you find the grammar for Ordeal::Model::Parser. Hopefully.

I didnā€™t write the grammar anywhere for documentation purposes - my bad actually, so letā€™s pay this technical debt now. Ehr yesā€¦ by reverse engineering the code šŸ˜…

The entry point in the grammar is expression:

expression: addend ( addop addend )*
addend: ( positive_int multop )* atom ( multop positive_int )*
atom: ( identifier | sub_expression ) unaryop* 
sub_expression: '(' expression ')'

addop:  '-' | '+'
multop: '*' | 'x'
unaryop: shuffler | simple_slicer | slicer | sorter
shuffler: '@'
simple_slicer: int
slicer: '[' int_item_list ']'
sorter: '!'

int: simple_int | random_int
simple_int: /0|-?[1-9][0-9]*/
random_int: '{' int_item_list '}'

int_item_list: int_item ( ',' int_item )* (',')?
int_item: int_simple_range | int_range | int
int_simple_range: '#' positive_int
int_range: int '..' int

identifier: token | quoted_string
token: /[a-zA-Z][a-zA-Z0-9_]*/
quoted_string: /"([^\\"]|\\.)"/

positive_int: ...

I hope I didnā€™t get the symbols wrong:

  • whitespacey stuff is not explicitly indicated to make stuff more readable
  • quoted stuff is just what appears inside
  • stuff between slashes are regular expressions (Backus and Naur will hopefully forgive me)
  • the | character separates alternatives
  • the * character means ā€œzero or more of thisā€
  • the ā€˜?ā€™ character means ā€œzero or one of thisā€
  • parentheses group stuff
  • stuff in a sequence must appear in that sequence
  • a positive_int is just like an int where all actual integers inside are constrained to be strictly positive (it could be expanded in the grammar but it would be more boring than it already is!)

As anticipated, not too much of a grammar!

The whole series

Want to look at the other parts of this series? Hereā€™s a list of them:


Comments? Octodon, , GitHub, Reddit, or drop me a line!