-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutingA.g4
115 lines (93 loc) · 2.65 KB
/
routingA.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Define a grammar called routingA
grammar routingA;
// Fragments
fragment SAFE_ID_HEAD_CHAR: [a-zA-Z_] ;
fragment SAFE_NONID_HEAD_CHAR: [/\\^*.+0-9-] ;
fragment SAFE_INTERMEDIATE_CHAR: [@$!] ;
fragment SAFE_CHAR: ( SAFE_ID_HEAD_CHAR | SAFE_NONID_HEAD_CHAR | SAFE_INTERMEDIATE_CHAR ) ;
fragment DOUBLE_QUOTE_STRING : '"' ( '\\"' | . )*? '"' ; // match "foo", "\"", "x\"\"y", ...
fragment SINGLE_QUOTE_STRING : '\'' ( '\\\'' | . )*? '\'' ; // match 'foo', '\'', 'x\'\'y', ...
// Tokens
WHITESPACE : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
COMMENT_BLOCK : '/*' .*? '*/' -> skip ;
COMMENT_LINE_SHARP : '#' .*? ( [\r\n]+ | EOF ) -> skip ;
ID : SAFE_ID_HEAD_CHAR SAFE_CHAR* ;
NON_ID : SAFE_NONID_HEAD_CHAR SAFE_CHAR* ;
QUOTE_STRING : DOUBLE_QUOTE_STRING | SINGLE_QUOTE_STRING;
// Rules
start : input EOF;
bare_literal : ID | NON_ID ;
quote_literal : QUOTE_STRING;
literal : quote_literal | bare_literal ;
input
: programStructureBlcok
| input programStructureBlcok
| // empty
;
programStructureBlcok
: optConditionStatement expression
;
expression
: declaration
| routingRule
| '{' routingRuleOrDeclarationList '}'
;
optConditionStatement
: '@' ID '==' literal
| '@' ID '!=' literal
| // empty
;
declaration
: ID ':' assignmentExpression
| ID ':' literal
;
assignmentExpression
: ID '=' functionPrototype
;
functionPrototype
: '!'? ID '(' optParameterList ')'
;
optParameterList
: nonEmptyParameterList
| // empty
;
nonEmptyParameterList
: parameter
| nonEmptyParameterList ',' parameter
;
parameter
: ID ':' literal
| ID '=' literal
| literal
;
routingRule
: routingRuleLeft '->' bare_literal
| optFunctionPrototypeExpressionAnd '{' routingRuleList '}'
;
routingRuleLeft
: optFunctionPrototypeExpressionAnd '{' routingRuleLeftList '}'
| optFunctionPrototypeExpressionAnd functionPrototypeExpression
;
routingRuleLeftList
: routingRuleLeft
| routingRuleLeft routingRuleLeftList
;
optFunctionPrototypeExpressionAnd
: functionPrototypeExpression '&&'
| // empty
;
functionPrototypeExpression
: functionPrototype
| functionPrototypeExpression '&&' functionPrototypeExpression
// | functionPrototypeExpression '||' functionPrototypeExpression
;
routingRuleOrDeclarationList
: routingRule
| declaration
| routingRule routingRuleOrDeclarationList
| declaration routingRuleOrDeclarationList
;
routingRuleList
: routingRule
| routingRule routingRuleList
;