Skip to content

Instantly share code, notes, and snippets.

@alexeygrigorev
Last active December 7, 2023 03:39
Show Gist options
  • Save alexeygrigorev/6028776 to your computer and use it in GitHub Desktop.
Save alexeygrigorev/6028776 to your computer and use it in GitHub Desktop.
ANTLR4 grammar for rule-based DSL
grammar RuleDSL;
rules: (basic_rule)+ EOF;
basic_rule: 'rule' SPACE rule_name SPACE '{' EOL conditions '}' EOL;
name: ID;
list_index: '[' IND ']';
name_expr: name list_index*;
rule_name: name_expr ('.' name_expr)*;
conditions: when_condition_expr+ otherwise_condition_expr?;
condition: .*?;
result: ~EOL* ;
when_condition_expr: result WHEN condition;
otherwise_condition_expr: result IN_OTHER_CASES;
WHEN: 'when';
IN_OTHER_CASES: 'in' 'other' 'cases';
ID: CHAR+;
IND: DIGIT+;
fragment DIGIT: '0'..'9';
fragment CHAR: 'a'..'z' | 'A'..'Z';
SYMBOL: '?' | '!' | '&' | '.' | ',' | '(' | ')' | '[' | ']' | '\\' | '/' | '%'
| '*' | '-' | '+' | '=' | '<' | '>' | '_' | '|' | '"' | '\'' | '~';
// Whitespace and comments
SPACE: [ \t]+;
EOL: ('\n' | '\r\n')+;
WS: [\t\u000C]+ -> skip;
COMMENT: '/*' .*? '*/' -> skip;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment