Last active
December 7, 2023 03:39
-
-
Save alexeygrigorev/6028776 to your computer and use it in GitHub Desktop.
ANTLR4 grammar for rule-based DSL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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