-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGroup.g4
More file actions
147 lines (117 loc) · 3.45 KB
/
Group.g4
File metadata and controls
147 lines (117 loc) · 3.45 KB
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
grammar Group;
/*
* Parser Rules
*/
tokens {INDENT , DEDENT}
python_file: (function_def_expr | multi_input | NEWLINE) (function_def_expr | multi_input | NEWLINE)*;
multi_input: (single_input | multiLine_input) (single_input | multiLine_input)*;
multiLine_input: if_expr
| for_expr
| while_expr;
single_input: assignment_expr NEWLINE
| mathmatical_expr NEWLINE
| function_call_expr
| NEWLINE
;
assignment_expr: left=variable_expr op=ASSIGNS right=mathmatical_expr;
mathmatical_expr: left=mathmatical_expr op=(MULTIPLY | DIVIDE | MOD) right=mathmatical_expr
| left=mathmatical_expr op=(ADD | MINUS) right=mathmatical_expr
| OPENPAR mathmatical_expr CLOSEPAR
| INTEGER
| function_call_expr
| variable_expr;
list_expr: variable_expr ASSIGN OPENBRACKET (variable_expr | INTEGER | STRING | function_call_expr)( COMMA ((variable_expr | INTEGER | STRING | function_call_expr)))* CLOSEBRACKET ;
variable_expr: ID;
conditional_expr: mathmatical_expr CONDITIONAL mathmatical_expr
| variable_expr;
star_conditional_expr:(NOT)? conditional_expr ( (AND|OR) (NOT)? conditional_expr )*;
if_expr :IF OPENPAR star_conditional_expr CLOSEPAR COLON
(INDENT multi_input DEDENT)
( ELIF OPENPAR star_conditional_expr CLOSEPAR COLON
INDENT multi_input DEDENT)*
( ELSE COLON
INDENT multi_input DEDENT)?;
for_expr: FOR variable_expr IN list_expr COLON
(INDENT multi_input DEDENT);
while_expr: WHILE star_conditional_expr COLON
(INDENT multi_input DEDENT);
function_def_expr: DEF variable_expr OPENPAR((assignment_expr)(COMMA assignment_expr)*)? CLOSEPAR COLON
(INDENT multi_input DEDENT);
function_call_expr: variable_expr OPENPAR((assignment_expr)(COMMA assignment_expr)*)? CLOSEPAR;
/*
* Lexer Rules
* Character interpreter and tokenizer
*/
ID : ID_START ID_CONTINUE* ;
INTEGER : NON_ZERO_DIGIT DIGIT*
| '0'
;
STRING : '"' ( [a-z] | [A-Z] | SPACES | NEWLINE )* '"';
BOOL : TRUE|FALSE;
ASSIGN : '=';
ADD : '+';
MINUS : '-';
MULTIPLY : '*';
DIVIDE : '/';
MOD : '%';
ADD_ASSIGN : '+=';
SUB_ASSIGN : '-=';
MUL_ASSIGN : '*=';
DIV_ASSIGN : '/=';
LESS : '<';
GREATER : '>';
EQLESS : '<=';
EQGREATER : '>=';
NOTEQ : '!=';
EQ : '==';
AND : 'and';
OR : 'or';
NOT : 'not';
TRUE : 'True';
FALSE : 'False';
OPENPAR : '(';
CLOSEPAR : ')';
COLON : ':';
ELSE : 'else';
IF : 'if';
ELIF : 'elif';
OPENBRACKET : '[';
CLOSEBRACKET : ']';
BREAK :'break';
CONTINUE :'continue';
NEWLINE : '\n';
COMMA : ',';
FOR : 'for';
IN : 'in';
WHILE : 'while';
DEF : 'def';
LOOPCONTROL : BREAK | CONTINUE;
ASSIGNS : ASSIGN | ADD_ASSIGN | SUB_ASSIGN | MUL_ASSIGN | DIV_ASSIGN ;
CONDITIONAL: LESS | GREATER | EQLESS | EQGREATER | EQ | NOTEQ;
COMMENTS : ('#' . NEWLINE | '"""' ( . | NEWLINE)*? '"""') -> skip;
WHITESPACE : SPACES -> skip;
/*
* Fragments
* Base Characters to be used in lexer rules
*/
fragment NON_ZERO_DIGIT
: [1-9]
;
fragment DIGIT
: [0-9]
;
fragment SPACES
: [ \t\r]+
;
fragment ID_START
: [A-Z]
| [a-z]
| '_'
;
fragment ID_CONTINUE
: ID_START
| [A-Z]
| [a-z]
| '_'
| [0-9]
;