Here is what I am trying to make an AST of it:
{{ name }}
{{ name | option }}
{{ name | option1 | option2 }}
{{ name | key=value }}
{{ name | option1 | key=value }}
{{ name | option1 | {{ another }} | option3 }}
So in practice there is always a name (a..zA..Z0..9) and options sometimes are in key-value format and sometimes in simple and without value format.
I am trying to write a lexer/parser grammar for it by ANTLR but it keeps nagging about different stuff. Here is my best shot:
start : box+;
box : '{{' Name ('|' Options )* '}}';
Options : (SimpleOption | KeyValue | box);
Name : ID;
SimpleOption: ID;
KeyValue: ID '=' ID;
fragment
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
WS : ( ' ' | '\t' | '\r' | '\n' {$channel=HIDDEN;} ;
Which is obviously wrong because Name and SimpleOption are ambiguous. Even an inline rule is useless:
box : '{{' Name ('|' (ID | KeyValue | box) )* '}}';
Because it never picks KeyValue up and gives a Mismatch exception on the encounter with '='.
How would you write this grammar?