-
Notifications
You must be signed in to change notification settings - Fork 0
/
MxStar.g4
279 lines (211 loc) · 7.12 KB
/
MxStar.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
grammar MxStar;
// Add Package Automatically
@header {
package masterball.compiler.frontend.parser;
}
// Parser
// 0 Code
mxStarCode
: (classDef | funcDef | varDefStmt)* EOF
;
// 8 Class
classDef
: ClassKw Identifier LeftBrace
(classConstructorDef | varDefStmt | funcDef)*
RightBrace SemiColon
;
classConstructorDef
: Identifier LeftParen RightParen suite
;
// 9 Function
funcDef
: varDefType Identifier LeftParen funcDefArgs? RightParen suite
;
funcDefArgs
: varDefType Identifier (Comma varDefType Identifier)*
;
funcCallArgs
: LeftParen (expression (Comma expression)*)? RightParen
;
// 7 Variable
// 7.1 BuiltinType
builtinType: IntType | StringType | BoolType | VoidType;
// 7.2 ArrayType
varDefType
: builtinType
| Identifier
| (builtinType | Identifier) (LeftBracket RightBracket)+
;
newExpSizeDeclaration: LeftBracket expression? RightBracket;
varDefBody: varDefType varDefSingle (Comma varDefSingle)*;
varDefSingle: Identifier (AssignOp expression)?;
// 11 Statement
suite
: LeftBrace statement* RightBrace
;
varDefStmt
: varDefBody SemiColon
;
ifStmt: IfKw LeftParen expression RightParen statement (ElseKw statement)?;
whileStmt: WhileKw LeftParen expression RightParen statement;
forInit: (varDefBody | expression);
forStmt: ForKw LeftParen forInit? SemiColon
forCondition = expression? SemiColon
forIncr = expression?
RightParen statement;
returnStmt: ReturnKw expression? SemiColon;
controlStmt: (BreakKw | ContinueKw) SemiColon;
pureStmt: expression? SemiColon;
suiteStmt: suite;
statement
: suiteStmt
| ifStmt
| whileStmt
| forStmt
| controlStmt
| returnStmt
| varDefStmt
| pureStmt
;
// 10 Expression
// Warning: Avoid Mutual Left Recursion!!!
// 10.1 Op Set
prefixOps: (IncrementOp | DecrementOp);
postfixOps: (IncrementOp | DecrementOp);
unaryOps: (BitNotOp | LogicNotOp | AddOp | SubOp);
shiftOps: (ArithShiftLeftOp | ArithShiftRightOp);
mulLevelOps: (MulOp | DivOp | ModOp);
addLevelOps: (AddOp | SubOp);
compareOps: (GreaterOp | GreaterEqualOp | LessOp | LessEqualOp);
equalOps: (EqualOp | NotEqualOp);
// reference: https://en.cppreference.com/w/c/language/operator_precedence
expression
: atom #atomExp // 0
| LeftParen expression RightParen #parenExp // 1
| NewKw (builtinType | Identifier) newExpSizeDeclaration* (LeftParen RightParen)? #newExp // 1
| expression LeftBracket expression RightBracket #indexExp // 1
| expression MemberOp Identifier #memberExp // 1
| expression funcCallArgs #funcCallExp // 1
| LambdaStartSymbol (LeftParen funcDefArgs? RightParen)?
LambdaArrowSymbol suite funcCallArgs #lambdaExp // 1
| expression postfixOps #postfixExp // 2
| <assoc=right> prefixOps expression #prefixExp // 2
| <assoc=right> unaryOps expression #unaryExp // 2
| expression mulLevelOps expression #binaryExp // 3
| expression addLevelOps expression #binaryExp // 4
| expression shiftOps expression #binaryExp // 5
| expression compareOps expression #binaryExp // 6
| expression equalOps expression #binaryExp // 7
| expression BitAndOp expression #binaryExp // 8
| expression BitXorOp expression #binaryExp // 9
| expression BitOrOp expression #binaryExp // 10
| expression LogicAndOp expression #binaryExp // 11
| expression LogicOrOp expression #binaryExp // 12
// ? expr // 13
| <assoc=right> expression AssignOp expression #assignExp // 14
//| expression Comma expression #commaExpL // 15
;
atom
: Identifier
| IntegerConstant
| StringConstant
| FalseConstant
| TrueConstant
| NullConstant
| ThisPointer
;
// Lexer
// 1 Operator
// 1.1 Std Op
AddOp: '+' ;
SubOp: '-' ;
MulOp: '*' ;
DivOp: '/' ;
ModOp: '%' ;
// 1.2 Relation Op
GreaterOp: '>' ;
LessOp: '<' ;
GreaterEqualOp: '>=' ;
LessEqualOp: '<=' ;
NotEqualOp: '!=' ;
EqualOp: '==' ;
// 1.3 Logic Op
LogicAndOp: '&&' ;
LogicOrOp: '||' ;
LogicNotOp: '!' ;
// 1.4 Bit Op
ArithShiftLeftOp: '<<' ;
ArithShiftRightOp: '>>' ;
BitAndOp: '&' ;
BitOrOp: '|' ;
BitXorOp: '^' ;
BitNotOp: '~' ;
// 1.5 Assign Op
AssignOp: '=' ;
// 1.6 Increment & Decrement Op
IncrementOp: '++' ;
DecrementOp: '--' ;
// 1.7 Member Op
MemberOp: '.' ;
// 1.8 Index Op
LeftBracket: '[';
RightBracket: ']';
// 1.9 Priority Op
LeftParen: '(';
RightParen: ')';
// 1.10 Seperator Op
SemiColon: ';';
Comma: ',';
LeftBrace: '{';
RightBrace: '}';
// 1.11 String Op
QuoteOp: '"';
// 1.12 Lambda
LambdaStartSymbol: '[&]';
LambdaArrowSymbol: '->' ;
// 2.Keyword
// 2.1 Basic Type Keyword
IntType: 'int' ;
BoolType: 'bool' ;
StringType: 'string' ;
VoidType: 'void' ;
// 2.2 Constant Keyword
NullConstant: 'null' ;
TrueConstant: 'true' ;
FalseConstant: 'false' ;
// 2.3 Control Keyword
IfKw: 'if' ;
ElseKw: 'else' ;
ForKw: 'for' ;
WhileKw: 'while' ;
BreakKw: 'break' ;
ContinueKw: 'continue' ;
ReturnKw: 'return' ;
// 2.4 Class Related Keyword
NewKw: 'new' ;
ClassKw: 'class' ;
ThisPointer: 'this' ;
// 3 Blank
WhitespaceEater: [ \t]+ -> skip ;
NewlineEater: ('\r' '\n'?| '\n') -> skip ;
// 4 Comment
LineCommentEater: '//' ~[\r\n]* -> skip ;
BlockCommentEater: '/*' .*? '*/' -> skip ;
// 5 Identifier
Identifier: [a-zA-Z] [a-zA-Z_0-9]* ;
// 6 Constant
// 6.1 Integer Constant
IntegerConstant
: '0'
| [1-9][0-9]*
;
// 6.2 String Constant
EscapeEnter: '\\n';
EscapeBackslash: '\\\\';
EscapeQuote: '\\"';
StringContent: [ -~];
StringConstant
: QuoteOp (EscapeEnter | EscapeBackslash | EscapeQuote | StringContent)*? QuoteOp
;
// 6.3 Bool Constant -> Keyword
// 6.4 Null Constant -> Keyword