-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cs
455 lines (451 loc) · 14.8 KB
/
Parser.cs
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
using System.Collections.Generic;
namespace GalgameNovelScript
{
public class Parser
{
public Lexer Lexer { get; set; }
public Token CurrentToken { get; set; }
public Parser(Lexer lexer)
{
Lexer = lexer;
CurrentToken = Lexer.GetNextToken();
}
public void Error(ErrorCode errorCode, Token token)
{
var message = string.Format("错误:{0}->{1}", ErrorCodeHandle.GetError(errorCode), token);
throw new ParserException(errorCode, token, message);
}
/// <summary>
/// 将当前令牌类型与传递的令牌进行比较输入,
/// 如果匹配则“吃掉”当前令牌并将下一个令牌分配给CurrentToken,
/// 否则引发异常。
/// </summary>
/// <param name="tokenType"></param>
public void Eat(TokenType tokenType)
{
if (CurrentToken.Type == tokenType)
{
CurrentToken = Lexer.GetNextToken();
}
else
{
Error(ErrorCode.UNEXPECTED_TOKEN, CurrentToken);
}
}
public AST Program()
{
// program: (NEWLINE | stmt)*
var node = new Program();
while (true)
{
if (CurrentToken.Type == TokenType.NEWLINE)
{
Eat(TokenType.NEWLINE);
}
else if (CurrentToken.Type == TokenType.EOF)
{
break;
}
else
{
var stmt = Stmt();
if (stmt != null)
node.Stmts.Add(stmt);
}
}
return node;
}
public AST Stmt()
{
// stmt: simple_stmt | compound_stmt
if (CurrentToken.Type == TokenType.IF || CurrentToken.Type == TokenType.CASE)
{
return CompoundStmt();
}
else
{
return SimpleStmt();
}
}
public AST SimpleStmt()
{
// simple_stmt: small_stmt NEWLINE
var node = SmallStmt();
Eat(TokenType.NEWLINE);
return node;
}
public AST SmallStmt()
{
// small_stmt: (fun | assign_stmt)?
if (CurrentToken.Type == TokenType.NAME)
{
var var = new Var(CurrentToken);
Eat(TokenType.NAME);
if (CurrentToken.Type == TokenType.COLON)
return Fun(var);
else
return AssginStmt(var);
}
else if (CurrentToken.Type == TokenType.COLON)
return Fun(null);
return null;
}
public AST Fun(Var var)
{
// fun: (NAME)? : args
// 上一步已经吃掉了NAME
Eat(TokenType.COLON);
return new FunCall(var, Args());
}
public AST AssginStmt(Var var)
{
// assign_stmt: NAME trailer* = expr
// 上一步已经吃掉了NAME
AST node = var;
Token op;
while (CurrentToken.Type == TokenType.LBRACK || CurrentToken.Type == TokenType.DOT)
{
op = CurrentToken;
var trailer = Trailer();
node = new BinOp(node, op, trailer);
}
op = CurrentToken;
Eat(TokenType.ASSIGN);
var right = Expr();
return new BinOp(node, op, right);
}
public AST CompoundStmt()
{
// compound_stmt: if_stmt | case_stmt
if (CurrentToken.Type == TokenType.IF)
return IfStmt();
else
return CaseStmt();
}
public AST IfStmt()
{
// if_stmt: IF expr suite (ELIF expr suite)* (ELSE suite)?
Eat(TokenType.IF);
var ifStmt = new List<(AST Condition, AST ThenStmt)>();
var expr = Expr();
var thenSuite = Suite();
ifStmt.Add((expr, thenSuite));
List<IfStmt> elifs = null;
if (CurrentToken.Type == TokenType.ELIF)
{
elifs = new List<IfStmt>();
while (CurrentToken.Type == TokenType.ELIF)
{
Eat(TokenType.ELIF);
var elifExpr = Expr();
var elifSuite = Suite();
ifStmt.Add((elifExpr, elifSuite));
}
}
if (CurrentToken.Type == TokenType.ELSE)
{
if (elifs == null)
elifs = new List<IfStmt>();
Eat(TokenType.ELSE);
var elseSuite = Suite();
ifStmt.Add((null, elseSuite));
}
return new IfStmt(ifStmt);
}
public AST CaseStmt()
{
// case_stmt: CASE expr? NEWLINE (WHEN expr? suite)*
Eat(TokenType.CASE);
AST expr = null;
if (CurrentToken.Type != TokenType.NEWLINE)
expr = Expr();
Eat(TokenType.NEWLINE);
var whens = new List<WhenStmt>();
while (CurrentToken.Type == TokenType.WHEN)
{
Eat(TokenType.WHEN);
AST whenExpr = null;
if (CurrentToken.Type != TokenType.NEWLINE)
whenExpr = Expr();
var whenSuite = Suite();
whens.Add(new WhenStmt(whenExpr, whenSuite));
}
var node = new CaseStmt(expr, whens);
return node;
}
public AST Suite()
{
// suite: NEWLINE INDENT stmt* DEDENT
Eat(TokenType.NEWLINE);
Eat(TokenType.INDENT);
var stmts = new List<AST>();
while (CurrentToken.Type != TokenType.DEDENT)
{
var stmt = Stmt();
if (stmt != null)
stmts.Add(stmt);
}
Eat(TokenType.DEDENT);
return new Suite(stmts);
}
public AST Expr()
{
// expr: test
return OrTest();
}
public AST OrTest()
{
// or_test: and_test (OR and_test)*
var node = AndTest();
while (CurrentToken.Type == TokenType.OR)
{
var op = CurrentToken;
Eat(TokenType.OR);
var right = AndTest();
node = new BinOp(node, op, right);
}
return node;
}
public AST AndTest()
{
// and_test: not_test (AND not_test)*
var node = NotTest();
while (CurrentToken.Type == TokenType.AND)
{
var op = CurrentToken;
Eat(TokenType.AND);
var right = NotTest();
node = new BinOp(node, op, right);
}
return node;
}
public AST NotTest()
{
// not_test: NOT not_test | comparison
if (CurrentToken.Type == TokenType.NOT)
{
var op = CurrentToken;
Eat(TokenType.NOT);
var right = NotTest();
return new UnaryOp(op, right);
}
else
{
return Comparsion();
}
}
public AST Comparsion()
{
// comparsion: calculator (comp_op calculator)*
var node = Calculator();
while (CurrentToken.Type == TokenType.EQ || CurrentToken.Type == TokenType.NE
|| CurrentToken.Type == TokenType.LT || CurrentToken.Type == TokenType.LE
|| CurrentToken.Type == TokenType.GT || CurrentToken.Type == TokenType.GE)
{
var op = CurrentToken;
CompOp();
var right = Calculator();
node = new BinOp(node, op, right);
}
return node;
}
public void CompOp()
{
// comp_op: < | > | == | >= | <= | !=
if (CurrentToken.Type == TokenType.EQ)
Eat(TokenType.EQ);
else if (CurrentToken.Type == TokenType.NE)
Eat(TokenType.NE);
else if (CurrentToken.Type == TokenType.LT)
Eat(TokenType.LT);
else if (CurrentToken.Type == TokenType.LE)
Eat(TokenType.LE);
else if (CurrentToken.Type == TokenType.GT)
Eat(TokenType.GT);
else if (CurrentToken.Type == TokenType.GE)
Eat(TokenType.GE);
}
public AST Calculator()
{
// calculator: term (CalculatorOp term)*
var node = Term();
while (CurrentToken.Type == TokenType.PLUS || CurrentToken.Type == TokenType.MINUS)
{
var op = CurrentToken;
CalculatorOp();
var right = Term();
node = new BinOp(node, op, right);
}
return node;
}
public void CalculatorOp()
{
// calculator_op: + | -
if (CurrentToken.Type == TokenType.PLUS)
Eat(TokenType.PLUS);
else if (CurrentToken.Type == TokenType.MINUS)
Eat(TokenType.MINUS);
}
public AST Term()
{
// term: factor (( * | / | // | %) factor)*
var node = Factor();
while (CurrentToken.Type == TokenType.MUL || CurrentToken.Type == TokenType.DIV
|| CurrentToken.Type == TokenType.FLOORDIV || CurrentToken.Type == TokenType.MOD)
{
var op = CurrentToken;
TermOp();
var right = Factor();
node = new BinOp(node, op, right);
}
return node;
}
public void TermOp()
{
// term_op: * | / | // | %
if (CurrentToken.Type == TokenType.MUL)
Eat(TokenType.MUL);
else if (CurrentToken.Type == TokenType.DIV)
Eat(TokenType.DIV);
else if (CurrentToken.Type == TokenType.FLOORDIV)
Eat(TokenType.FLOORDIV);
else if (CurrentToken.Type == TokenType.MOD)
Eat(TokenType.MOD);
}
public AST Factor()
{
// factor: ( +|- ) factor | power
if (CurrentToken.Type == TokenType.PLUS || CurrentToken.Type == TokenType.MINUS)
{
var op = CurrentToken;
CalculatorOp();
var right = Factor();
return new UnaryOp(op, right);
}
else
return Power();
}
public AST Power()
{
// power: atom trailer* (POW factor)*
var node = Atom();
if (CurrentToken.Type == TokenType.LPAREN || CurrentToken.Type == TokenType.DOT)
{
var op = CurrentToken;
var trailer = Trailer();
node = new BinOp(node, op, trailer);
}
while (CurrentToken.Type == TokenType.POW)
{
var op = CurrentToken;
Eat(TokenType.POW);
var right = Factor();
node = new BinOp(node, op, right);
}
return node;
}
public AST Atom()
{
// atom: LPAREN expr RPAREN | fun | NAME | INT_CONST | REAL_CONST | STR | TRUE | FALSE | NONE
if (CurrentToken.Type == TokenType.LPAREN)
{
Eat(TokenType.LPAREN);
var node = Expr();
Eat(TokenType.RPAREN);
return node;
}
else if (CurrentToken.Type == TokenType.NAME)
{
var node = new Var(CurrentToken);
Eat(TokenType.NAME);
if (CurrentToken.Type == TokenType.COLON)
return Fun(node);
return node;
}
else if (CurrentToken.Type == TokenType.INT_CONST)
{
var node = new Num(CurrentToken);
Eat(TokenType.INT_CONST);
return node;
}
else if (CurrentToken.Type == TokenType.REAL_CONST)
{
var node = new Num(CurrentToken);
Eat(TokenType.REAL_CONST);
return node;
}
else if (CurrentToken.Type == TokenType.STR)
{
var node = new Str(CurrentToken);
Eat(TokenType.STR);
return node;
}
else if (CurrentToken.Type == TokenType.TRUE)
{
var node = new Boolean(CurrentToken);
Eat(TokenType.TRUE);
return node;
}
else if (CurrentToken.Type == TokenType.FALSE)
{
var node = new Boolean(CurrentToken);
Eat(TokenType.FALSE);
return node;
}
else if (CurrentToken.Type == TokenType.NONE)
{
var node = new None(CurrentToken);
Eat(TokenType.NONE);
return node;
}
else
{
Error(ErrorCode.UNEXPECTED_TOKEN, CurrentToken);
return null;
}
}
public AST Trailer()
{
// trailer: [ args ] | . NAME
if (CurrentToken.Type == TokenType.LBRACK)
{
Eat(TokenType.LBRACK);
var node = Args();
Eat(TokenType.RBRACK);
return node;
}
else if (CurrentToken.Type == TokenType.DOT)
{
Eat(TokenType.DOT);
var node = new Var(CurrentToken);
Eat(TokenType.NAME);
return node;
}
else
{
Error(ErrorCode.UNEXPECTED_TOKEN, CurrentToken);
return null;
}
}
public Args Args()
{
// arglist: (expr)*
var args = new List<AST>();
while (CurrentToken.Type != TokenType.RBRACK
&& CurrentToken.Type != TokenType.NEWLINE
&& CurrentToken.Type != TokenType.EOF)
{
var expr = Expr();
args.Add(expr);
}
return new Args(args);
}
public AST Parse()
{
var node = Program();
if (CurrentToken.Type != TokenType.EOF)
Error(ErrorCode.UNEXPECTED_TOKEN, CurrentToken);
return node;
}
}
}