-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJLangParser.g4
248 lines (197 loc) · 6.31 KB
/
JLangParser.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
parser grammar JLangParser;
options { tokenVocab=JLangLexer; }
template
: elements EOF
;
elements
: element *
;
element
: echo_script
| plain_script
| fragmented_script_if
| fragmented_script
| RAW
;
echo_script
: OPEN_ECHOSCRIPT_BLOCK statement_list CLOSE_ECHOSCRIPT_BLOCK
;
plain_script
: OPEN_SCRIPT_BLOCK statement_list CLOSE_SCRIPT_BLOCK
;
fragmented_script
: fragmented_script_start elements (fragmented_script_middle elements)* fragmented_script_end
;
fragmented_script_if
: fragmented_script_start_if elements
( (fragmented_script_middle elements)+
| (fragmented_script_middle_else elements)
)?
(fragmented_script_end | fragmented_script_end_else)
;
fragmented_script_start
: OPEN_SCRIPT_BLOCK (statement_list ';')? fragmented_statement (statement_list ';')? CLOSE_SCRIPT_BLOCK
;
fragmented_script_start_if
: OPEN_SCRIPT_BLOCK (statement_list ';')? fragmented_statement_if (statement_list ';')? CLOSE_SCRIPT_BLOCK
;
fragmented_script_middle_else
: OPEN_SCRIPT_BLOCK (statement_list ';')? '}' ELSE '{' (statement_list ';')? CLOSE_SCRIPT_BLOCK
| OPEN_SCRIPT_BLOCK (statement_list ';')? '}' ELSE if_body fragmented_statement (statement_list ';')? CLOSE_SCRIPT_BLOCK
;
fragmented_script_middle
: OPEN_SCRIPT_BLOCK (statement_list ';')? '}' (statement_list)? fragmented_statement (statement_list ';')? CLOSE_SCRIPT_BLOCK
;
fragmented_script_end_else
: OPEN_SCRIPT_BLOCK (statement_list ';')? '}' (ELSE if_body)? (statement_list)? CLOSE_SCRIPT_BLOCK
| OPEN_SCRIPT_BLOCK (statement_list ';')? '}' ELSE fragmented_statement_if (statement_list ';')? CLOSE_SCRIPT_BLOCK elements
( (fragmented_script_middle elements)+
| (fragmented_script_middle_else elements)
)?
(fragmented_script_end | fragmented_script_end_else)
;
fragmented_script_end
: OPEN_SCRIPT_BLOCK (statement_list ';')? '}' (statement_list)? CLOSE_SCRIPT_BLOCK
;
statement_list
: statement (';' statement)*
;
statement
: IDENTIFIER ':' statement
| local_variable_declaration
| embedded_statement
;
embedded_statement
: block_statement
| simple_embedded_statement
;
block_statement
: OPEN_BRACE statement_list? CLOSE_BRACE
;
local_variable_declaration
: type local_variable_declarator ( ',' local_variable_declarator)*
;
local_variable_declarator
: IDENTIFIER ('=' expression)?
;
type
: simple_type
| class_type
;
simple_type
: numeric_type
| TYPE_BOOL
;
numeric_type
: integral_type
| floating_point_type
| TYPE_DECIMAL
;
integral_type
: TYPE_BYTE
| TYPE_SHORT
| TYPE_INT
| TYPE_LONG
| TYPE_CHAR
;
floating_point_type
: TYPE_FLOAT
| TYPE_DOUBLE
;
class_type
: namespace_or_type_name
| TYPE_OBJECT
| TYPE_STRING
;
namespace_or_type_name
: (IDENTIFIER) ('.' IDENTIFIER)*
;
simple_embedded_statement
: ';'
| expression ';'?
| IF LPAREN expression RPAREN if_body (ELSE if_body)?
| WHILE LPAREN expression RPAREN embedded_statement
| DO embedded_statement WHILE LPAREN expression RPAREN
| FOR LPAREN for_initializer? ';' expression? ';' for_iterator? RPAREN embedded_statement
| FOREACH LPAREN type IDENTIFIER IN expression RPAREN embedded_statement
| GOTO IDENTIFIER
| BREAK
| CONTINUE
;
fragmented_statement_if
: IF LPAREN expression RPAREN OPEN_BRACE
;
fragmented_statement
: WHILE LPAREN expression RPAREN OPEN_BRACE
| FOR LPAREN for_initializer? ';' expression? ';' for_iterator? RPAREN OPEN_BRACE
| FOREACH LPAREN type IDENTIFIER IN expression RPAREN OPEN_BRACE
;
for_initializer
: local_variable_declaration
| expression (',' expression)*
;
for_iterator
: expression (',' expression)*
;
expression
: assign # AssignExpression
| primary_expression # PrimaryExpression
| op=(PLUS|MINUS|INC|DEC) expression # UnaryExpression
| EXCLAMATION expression # BooleanNotExpression
| POW LPAREN expression COMMA expression RPAREN # ExpoExpression
| expression op=(STAR|DIV|MOD) expression # MultiplicativeExpression
| expression op=(PLUS|MINUS) expression # AdditiveExpression
| expression op=(SHIFTL|SHIFTR) expression # ShiftExpression
| expression op=('<=' | '<' | '>=' | '>') expression # RelationalExpression
| expression op=('==' | '!=') expression # EqualityExpression
| expression LOGICAL_AND expression # LogicalAndExpression
| expression LOGICAL_XOR expression # LogicalXorExpression
| expression LOGICAL_OR expression # LogicalOrExpression
| expression CONDITIONAL_AND expression # ConditionalAndExpression
| expression CONDITIONAL_OR expression # ConditionalOrExpression
| expression '?' expression ':' expression # TernaryExpression
| '(' expression ')' # ParensExpression
;
primary_expression
: literal bracket_expression*
((member_access | method_invocation | INC | DEC) bracket_expression*)*
;
literal
: INTEGER_LITERAL # IntegerExpression
| REAL_LITERAL # DoubleExpression
| boolean_literal # BooleanExpression
| STRING # StringExpression
| IDENTIFIER # IdentifierExpression
;
method_invocation
: LPAREN argument_list? RPAREN
;
argument_list
: argument ( ',' argument)*
;
argument
: expression
;
bracket_expression
: '?'? '[' indexer_argument ( ',' indexer_argument)* ']'
;
indexer_argument
: (IDENTIFIER ':')? expression
;
member_access
: '?'? '.' IDENTIFIER type_argument_list?
;
type_argument_list
: '<' type ( ',' type)* '>'
;
if_body
: block_statement
| simple_embedded_statement
;
boolean_literal
: TRUE
| FALSE
;
assign
: IDENTIFIER op=('=' | '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=') expression
;