-
Notifications
You must be signed in to change notification settings - Fork 7
/
RiddleParser.g4
226 lines (189 loc) · 6.93 KB
/
RiddleParser.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
parser grammar RiddleParser;
options {
tokenVocab=RiddleLexer;
}
@Header{
}
@parserFile::members {
}
program
: statement_ed*
| EOF
;
null_cnt
: Semi
| Endl
;
statement_ed
: statement Semi? Endl?
| Semi
| Endl
;
statement
: packStatement
| importStatement
| classDefine
| funcDefine
| varDefineStatement
| forStatement
| whileStatement
| ifStatement
| returnStatement
| continueStatement
| breakStatement
| tryExpr
| expression
| bodyExpr
;
bodyExpr
: LeftCurly statement_ed* RightCurly
;
packStatement
: Package packName=id
;
importStatement
: Import libName=id
;
varDefineStatement
: Var name=Identifier Colon type=typeName
| Var name=Identifier Assign value=expression
| Var name=Identifier Colon type=typeName Assign value=expression
;
argsExpr
: ((expression Comma)* expression)?
;
defineArgs
: ((Identifier Colon typeName Comma)* (Identifier Colon typeName))?
;
funcDefine
: Func funcName=Identifier LeftBracket args=defineArgs RightBracket (Sub Greater returnType=typeName)? body=bodyExpr
;
forStatement
: For LeftBracket (init=statement)? Semi (termCond=statement)? Semi (selfVar=statement)? RightBracket body=statement_ed
;
whileStatement
: While LeftBracket runCond=expression RightBracket body=statement_ed
;
continueStatement
: Continue
;
breakStatement
: Break
;
ifStatement returns [bool hasElse]
: If LeftBracket cond=expression RightBracket body=statement_ed {$hasElse=false;}
| If LeftBracket cond=expression RightBracket body=statement_ed Else elseBody=statement_ed {$hasElse=true;}
;
returnStatement
: Return (result=statement_ed)?
;
classDefine
: Class className = id body=bodyExpr
;
tryExpr
: Try tryBody=bodyExpr null_cnt? catchExpr
;
catchExpr
: Catch LeftBracket varDefineStatement RightBracket
;
// 这一块就是使用
exprPtr
: funcName=Identifier LeftBracket args=argsExpr RightBracket #funcExpr
| Identifier #objectExpr
| parent=exprPtr Dot child=exprPtr #blendExpr
;
exprPtrParser
: exprPtr
;
expression
: Less type=typeName Greater LeftBracket value=exprPtrParser RightBracket #castExpr
| LeftBracket expr=expression RightBracket #bracketExpr // (x)
| Not expr=expression #notExpr // !x
| Add expr=expression #positiveExpr // +x
| Sub expr=expression #negativeExpr // -x
| Add Add expr=exprPtrParser #selfAddLeftExpr // ++x
| expr=exprPtrParser Add Add #selfAddRightExpr // x++
| Sub Sub expr=exprPtrParser #selfSubLeftExpr // ++x
| expr=exprPtrParser Sub Sub #selfSubRightExpr // x++
| exprPtr #ptrExpr
| left=expression Star right=expression #mulExpr // x*y
| left=expression Div right=expression #divExpr // x/y
| left=expression Mod right=expression #modExpr // x%y
| left=expression Add right=expression #addExpr // x+y
| left=expression Sub right=expression #subExpr // x-y
| left=expression LeftLeft right=expression #shlExpr // x<<y
| left=expression RightRight right=expression #aShrExpr // x>>y
| left=expression RightRightRight right=expression #lShrExpr // x>>>y
| left=expression Greater right=expression #greaterExpr // x>y
| left=expression Less right=expression #lessExpr // x<y
| left=expression Greater Assign right=expression #greaterEqualExpr // x>=y
| left=expression Less Assign right=expression #lessEqualExpr // x<=y
| left=expression Equal right=expression #equalExpr // x==y
| left=expression Not Assign right=expression #notEqualExpr // x!=y
| left=expression And right=expression #bitAndExpr // x&y
| left=expression Xor right=expression #bitXorExpr // x^y
| left=expression Or right=expression #bitOrExpr // x|y
| left=expression And And right=expression #andExpr // x&&y
| left=expression Or Or right=expression #orExpr // x||y
| left=exprPtrParser Assign right=expression #assignExpr // x=y
| left=exprPtrParser Add Assign right=expression #addAssignExpr // x+=y
| left=exprPtrParser Sub Assign right=expression #subAssignExpr // x-=y
| left=exprPtrParser Star Assign right=expression #mulAssignExpr // x*=y
| left=exprPtrParser Div Assign right=expression #divAssignExpr // x/=y
| left=exprPtrParser Mod Assign right=expression #modAssignExpr // x%=y
| left=exprPtrParser Add Assign right=expression #addAssignExpr // x+=y
| left=exprPtrParser And Assign right=expression #andAssignExpr // x&=y
| left=exprPtrParser Or Assign right=expression #orAssignExpr // x|=y
| left=exprPtrParser Xor Assign right=expression #xorAssignExpr // x^=y
| left=exprPtrParser LeftLeft Assign right=expression #shlAssignExpr // x<<=y
| left=exprPtrParser RightRight Assign right=expression #aShrAssignExpr // x>>=y
| left=exprPtrParser RightRightRight Assign right=expression #lShrAssignExpr // x>>>=y
| string #stringExpr
| number #numberExpr
| boolean #booleanExpr
| Null #nullExpr
;
id: Identifier;
//这里是指字面量
number
: integer
| float
;
boolean returns [bool value]
: True {$value=true;}
| False {$value=false;}
;
string
: STRING
;
float returns [double value]
: Float{
$value = stod($Float.text);
}
;
integer returns [int value]
: Decimal{
$value = stoi($Decimal.text);
}
| Hexadecimal{
$value = stoi($Hexadecimal.text.substr(2),nullptr,16);
}
| Binary{
$value = stoi($Binary.text.substr(2),nullptr,2);
}
| Octal{
$value = stoi($Octal.text.substr(1),nullptr,8);
}
;
templateArg
: expression
| typeName
;
templateArgs
: ((templateArg Comma)* templateArg)?
;
typeName
: name=id
| name=id Less args=templateArgs Greater
| baseType=typeName LeftSquare size=expression RightSquare
;