-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.g4
344 lines (311 loc) · 9.84 KB
/
Project.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
grammar Project;
/*
=== === === === Parser Rules === === === ===
*/
program: import_statement* statement*;
statement:
define_var_statement SEMICOLON
|
define_array_statement SEMICOLON
|
if_statement
|
for_loop_statement
|
foreach_loop_statement
|
while_loop_statement
|
do_while_loop_statement
|
switch_case_statement
|
class_definitiion_statement
|
function_definition_statement
|
function_call_statement
|
obj_instant SEMICOLON
|
try_catch
|
operation SEMICOLON;
// import statement
import_statement: (FROM chaining)? IMPORT
(
(IDENTIFIER(COMMA IDENTIFIER)*)
|
(chaining)
|
(IDENTIFIER ARROW IDENTIFIER)
|
STAR
)
SEMICOLON;
// define variable statement
define_var_statement: variable_type define_var (COMMA define_var)*;
define_var: define_var_with_type | define_var_without_type;
define_var_without_type: IDENTIFIER ASSIGN operation;
define_var_with_type: IDENTIFIER COLON data_type ( ASSIGN operation )?;
define_array_statement: define_array_with_initialization | define_array_without_initialization;
define_array_without_initialization: variable_type
IDENTIFIER
COLON NEW ARRAY
(BRACKET_BEGIN data_type BRACKET_END)?
PARENTHESE_BEGIN (INT_VALUE | DOUBLE_VALUE) PARENTHESE_END;
define_array_with_initialization: variable_type
IDENTIFIER
ASSIGN
ARRAY
PARENTHESE_BEGIN
args_list
PARENTHESE_END;
// if statement
if_statement: IF
PARENTHESE_BEGIN
(((NOT)? condition) ((AND | OR) (NOT)? condition)*)?
PARENTHESE_END
block
(
ELSE_IF
PARENTHESE_BEGIN
(((NOT)? condition) ((AND | OR) (NOT)? condition)*)?
PARENTHESE_END
block
)?
(ELSE block)?;
// for loop
for_loop_statement: FOR
PARENTHESE_BEGIN
(IDENTIFIER ASSIGN (INT_VALUE | DOUBLE_VALUE | EXP_VALUE))? SEMICOLON
(((NOT)? condition) ((AND | OR) (NOT)? condition)*)? SEMICOLON
(IDENTIFIER (PLUS_PLUS | MINUS_MINUS))?
PARENTHESE_END
block;
// foreach loop
foreach_loop_statement: FOR
PARENTHESE_BEGIN
VAR IDENTIFIER IN IDENTIFIER
PARENTHESE_END
block;
// while loop
while_loop_statement: WHILE
PARENTHESE_BEGIN
(((NOT)? condition) ((AND | OR) (NOT)? condition)*)?
PARENTHESE_END
block;
// do while loop
do_while_loop_statement: DO
block
WHILE
PARENTHESE_BEGIN
(((NOT)? condition) ((AND | OR) (NOT)? condition)*)?
PARENTHESE_END;
// switch/case statement
switch_case_statement: SWITCH
PARENTHESE_BEGIN IDENTIFIER PARENTHESE_END
BRACE_BEGIN
(
CASE value COLON
statement*
(BREAK SEMICOLON)?
)+
(
DEFAULT COLON
statement*
(BREAK SEMICOLON)?
)?
BRACE_END;
// class definition
class_definitiion_statement: CLASS
IDENTIFIER
(EXTENDS IDENTIFIER)?
(IMPLEMENTS IDENTIFIER (WITH IDENTIFIER)*)?
BRACE_BEGIN
class_body
BRACE_END;
class_body: (function_definition_statement | property_definition | constructor)*;
constructor: IDENTIFIER
PARENTHESE_BEGIN
params_list?
PARENTHESE_END
BRACE_BEGIN
statement*
BRACE_END;
// function definition
function_definition_statement: data_type IDENTIFIER
PARENTHESE_BEGIN
params_list?
PARENTHESE_END
BRACE_BEGIN
statement*
RETURN (value | IDENTIFIER)? SEMICOLON
BRACE_END;
// property definition ( in class )
property_definition: access_modifier define_var_statement SEMICOLON;
// object instantiation
obj_instant: variable_type IDENTIFIER COLON NEW IDENTIFIER PARENTHESE_BEGIN args_list? PARENTHESE_END;
// exception
try_catch: TRY block
(
ON IDENTIFIER (CATCH PARENTHESE_BEGIN IDENTIFIER PARENTHESE_END)? block
|
CATCH PARENTHESE_BEGIN IDENTIFIER PARENTHESE_END block
);
// function ( method ) call
function_call_statement: chaining
PARENTHESE_BEGIN
args_list?
PARENTHESE_END
SEMICOLON;
//// change variable value
//change_value_statement: chaining assign_operator operation;
// operations
operation: PARENTHESE_BEGIN operation PARENTHESE_END
|
operation STAR_STAR operation
|
TILDE operation
|
(PLUS | MINUS) operation
|
(PLUS_PLUS | MINUS_MINUS) operation
|
operation (STAR | SLASH_SLASH | DIVIDE | MOD) operation
|
operation (PLUS | MINUS) operation
|
operation (SHIFT_LEFT | SHIFT_RIGHT) operation
|
operation (BIT_AND | BIT_OR | BIT_XOR) operation
|
operation (EQUAL | NON_EQUAL) operation
|
operation (GREATHER_EQUAL | GREATER | LESS_EQUAL | LESS) operation
|
NOT operation
|
operation (AND | OR) operation
|
chaining assign_operator operation
|
value
|
chaining
;
// *************************************
params_list: data_type IDENTIFIER default_value? (COMMA data_type IDENTIFIER default_value?)*;
default_value: ASSIGN value;
args_list: ((value | chaining) (COMMA (value | chaining))*);
condition: BOOLEAN_VALUE | expression;
block: BRACE_BEGIN statement* BRACE_END;
expression: (value | IDENTIFIER) compare_sign (value | IDENTIFIER);
compare_sign: EQUAL | LESS | LESS_EQUAL | GREATER | GREATHER_EQUAL | NON_EQUAL;
access_modifier: PUBLIC | PRIVATE | PROTECTED;
variable_type: VAR | CONST;
data_type: INT | STRING | DOUBLE | BOOLEAN | CHARACTER | FLOAT | VOID;
value: INT_VALUE | BOOLEAN_VALUE | DOUBLE_VALUE | EXP_VALUE | STRING_VALUE;
chaining: IDENTIFIER (DOT IDENTIFIER)*;
assign_operator: ASSIGN | PLUS_ASSING | MULTIPLY_ASSING | MINUS_ASSING | DIVIDE_ASSING;
/*
=== === === === Lexer Rules === === === ===
*/
// skip
WHITE_SPACE:[ \t\n\r]+ -> skip;
COMMENT_TYPE1: '/*' .*? '*/' -> skip;
COMMENT_TYPE2: '#' ~[\r\n]* -> skip;
// keywords
VAR : 'var';
CONST : 'const';
IMPORT : 'import';
FROM : 'from';
NEW : 'new';
ARRAY : 'Array';
IF : 'if';
ELSE_IF : 'elif';
ELSE : 'else';
FOR : 'for';
IN : 'in';
WHILE : 'while';
DO : 'do';
SWITCH : 'switch';
CASE : 'case';
DEFAULT : 'default';
BREAK : 'break';
CLASS : 'class';
EXTENDS : 'extends';
IMPLEMENTS : 'implements';
WITH : 'with';
RETURN : 'return';
TRY : 'try';
ON : 'on';
CATCH : 'catch';
// symbols
DOT : '.';
COLON : ':';
SEMICOLON : ';';
COMMA : ',';
ARROW : '=>';
STAR : '*';
ASSIGN : '=';
BRACKET_BEGIN : '[';
BRACKET_END : ']';
BRACE_BEGIN : '{';
BRACE_END : '}';
PARENTHESE_BEGIN: '(';
PARENTHESE_END : ')';
DOUBLE_QUOTE : '"';
EXP_SYMBOL : 'e';
PLUS : '+';
MINUS : '-';
PLUS_PLUS : '++';
MINUS_MINUS : '--';
STAR_STAR : '**';
DIVIDE : '/';
SLASH_SLASH : '//';
MOD : '%';
SHIFT_LEFT : '<<';
SHIFT_RIGHT : '>>';
BIT_AND : '&' ;
BIT_OR : '|' ;
BIT_XOR : '^' ;
PLUS_ASSING : '+=';
MINUS_ASSING : '-=';
MULTIPLY_ASSING : '*=';
DIVIDE_ASSING : '/=';
TILDE : '~';
// compare signs
EQUAL : '==';
GREATER : '>';
LESS : '<';
GREATHER_EQUAL : '>=';
LESS_EQUAL : '<=';
NON_EQUAL : '!=';
// logical operators
AND : '&&' | 'and';
OR : '||' | 'or';
NOT : '!' | 'not';
// primitive data types
INT : 'Int';
STRING : 'String';
DOUBLE : 'Double';
BOOLEAN : 'Boolean';
CHARACTER : 'Char';
FLOAT : 'Float';
VOID : 'Void';
// Access Modifiers
PUBLIC : 'public';
PRIVATE : 'private';
PROTECTED : 'protected';
// values
STRING_VALUE: DOUBLE_QUOTE .*? DOUBLE_QUOTE;
BOOLEAN_VALUE: 'true' | 'false';
INT_VALUE: DIGIT+;
DOUBLE_VALUE: DIGIT+ DECIMAL_PART;
EXP_VALUE: DIGIT? '.' DIGIT+ EXP_PART?;
DECIMAL_PART: '.' DIGIT+;
EXP_PART: EXP_SYMBOL (PLUS | MINUS)? DIGIT+;
// variable naming rule
IDENTIFIER: [a-zA-Z$_]+ [a-zA-Z0-9$_]+;
DIGIT: [0-9];