-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathParser.cs
514 lines (428 loc) · 18 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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using jamconverter.AST;
namespace jamconverter
{
public partial class Parser
{
private readonly ScanResult _scanResult;
public Parser(string input)
{
_scanResult = new Scanner(input).Scan();
}
public TopLevel ParseTopLevel()
{
try
{
var result = new TopLevel() {Statements = new NodeList<Statement>()};
while (true)
{
var statement = ParseStatement();
if (statement == null)
break;
result.Statements.Add(statement);
}
return result;
}
catch (Exception e)
{
Console.WriteLine("Parsing failed. Previous 50 tokens:");
var cursor = _scanResult.GetCursor();
var firstToken = Math.Max(0, cursor - 50);
_scanResult.SetCursor(firstToken);
for (int i = firstToken; i != cursor; i++)
{
var token = _scanResult.Next();
Console.WriteLine(string.Format("cursor={0} type={1} literal='{2}'", i, token.tokenType, token.literal));
}
throw;
}
}
public NodeList<Expression> ParseExpressionList()
{
var expressions = new List<Expression>();
while (true)
{
var expression = ParseExpression();
if (expression == null)
break;
expressions.Add(expression);
}
return expressions.ToNodeList();
}
public Statement ParseStatement()
{
var scanToken = _scanResult.Peek();
switch (scanToken.tokenType)
{
case TokenType.EOF:
case TokenType.Case:
case TokenType.AccoladeClose:
return null;
case TokenType.If:
return ParseIfStatement();
case TokenType.AccoladeOpen:
return ParseBlockStatement();
case TokenType.Rule:
return ParseRuleDeclarationStatement();
case TokenType.Actions:
return ParseActionsDeclarationStatement();
case TokenType.Return:
return ParseReturnStatement();
case TokenType.Literal:
case TokenType.VariableDereferencerOpen:
case TokenType.BracketOpen:
return ParseAssignmentOrExpressionStatement();
case TokenType.On:
return ParseOnStatement();
case TokenType.While:
return ParseWhileStatement();
case TokenType.Include:
return ParseIncludeStatement();
case TokenType.For:
return ParseForStatement();
case TokenType.Break:
_scanResult.Next();
_scanResult.Next().Is(TokenType.Terminator);
return new BreakStatement();
case TokenType.Continue:
_scanResult.Next();
_scanResult.Next().Is(TokenType.Terminator);
return new ContinueStatement();
case TokenType.Switch:
return ParseSwitchStatement();
case TokenType.Local:
return ParseLocalStatement();
default:
throw new ParsingException("Unexpected token: " + scanToken.tokenType);
}
}
private IncludeStatement ParseIncludeStatement()
{
_scanResult.Next().Is(TokenType.Include);
var expression = ParseExpression();
_scanResult.Next().Is(TokenType.Terminator);
return new IncludeStatement() {Expression = expression};
}
private LocalStatement ParseLocalStatement()
{
_scanResult.Next().Is(TokenType.Local);
var literalExpression = ParseExpression().As<LiteralExpression>();
var next = _scanResult.Next();
NodeList<Expression> value = new NodeList<Expression>();
if (next.tokenType != TokenType.Terminator)
{
next.Is(TokenType.Assignment);
value = ParseExpressionList();
_scanResult.Next().Is(TokenType.Terminator);
}
return new LocalStatement() {Variable = literalExpression, Value = value};
}
private SwitchStatement ParseSwitchStatement()
{
_scanResult.Next().Is(TokenType.Switch);
var result = new SwitchStatement() {Variable = ParseExpression()};
_scanResult.Next().Is(TokenType.AccoladeOpen);
var switchCases = new NodeList<SwitchCase>();
while (true)
{
var next = _scanResult.Next();
if (next.tokenType == TokenType.AccoladeClose)
break;
if (next.tokenType != TokenType.Case)
throw new ParsingException();
var switchCase = new SwitchCase() {CaseExpression = ParseExpression().As<LiteralExpression>()};
_scanResult.Next().Is(TokenType.Colon);
var statements = new NodeList<Statement>();
while (true)
{
var statement = ParseStatement();
if (statement == null)
break;
statements.Add(statement);
}
switchCase.Statements = statements;
switchCases.Add(switchCase);
}
result.Cases = switchCases;
return result;
}
private Statement ParseForStatement()
{
_scanResult.Next().Is(TokenType.For);
var loopVariable = ParseExpression();
_scanResult.Next().Is(TokenType.In);
return new ForStatement() {LoopVariable = loopVariable.As<LiteralExpression>(), List = ParseExpressionList(), Body = ParseBlockStatement()};
}
private OnStatement ParseOnStatement()
{
_scanResult.Next().Is(TokenType.On);
return new OnStatement() {Target = ParseExpression(), Body = ParseStatement()};
}
private WhileStatement ParseWhileStatement()
{
_scanResult.Next().Is(TokenType.While);
return new WhileStatement() {Condition = ParseCondition(), Body = ParseBlockStatement()};
}
private Statement ParseAssignmentOrExpressionStatement()
{
if (IsNextTokenAssignment())
{
var leftSideOfAssignment = ParseLeftSideOfAssignment();
var assignmentToken = _scanResult.Next();
var right = ParseExpressionList();
_scanResult.Next().Is(TokenType.Terminator);
return new AssignmentStatement() {Left = leftSideOfAssignment, Right = right, Operator = OperatorFor(assignmentToken.tokenType)};
}
var invocationExpression = ParseInvocationExpression();
_scanResult.Next().Is(TokenType.Terminator);
return new ExpressionStatement {Expression = invocationExpression};
}
private InvocationExpression ParseInvocationExpression()
{
var hasBracketSyntax = _scanResult.Peek().tokenType == TokenType.BracketOpen;
if (hasBracketSyntax)
_scanResult.Next();
var invocationExpression = new InvocationExpression {RuleExpression = ParseExpression(), Arguments = ParseArgumentList()};
if (hasBracketSyntax)
_scanResult.Next().Is(TokenType.BracketClose);
return invocationExpression;
}
private Expression ParseLeftSideOfAssignment()
{
var cursor = _scanResult.GetCursor();
ParseExpression();
var nextScanToken = _scanResult.Next();
_scanResult.SetCursor(cursor);
if (nextScanToken.tokenType == TokenType.On)
{
var variable = ParseExpression();
_scanResult.Next().Is(TokenType.On);
var targets = ParseExpressionList();
return new VariableOnTargetExpression() {Variable = variable, Targets = targets};
}
if (nextScanToken.tokenType == TokenType.Assignment || nextScanToken.tokenType == TokenType.AppendOperator || nextScanToken.tokenType == TokenType.SubtractOperator ||
nextScanToken.tokenType == TokenType.AssignmentIfEmpty)
{
return ParseExpression();
}
throw new ParsingException();
}
private bool IsNextTokenAssignment()
{
var cursor = _scanResult.GetCursor();
var leftSide = ParseExpression();
var nextScanToken = _scanResult.Next();
_scanResult.SetCursor(cursor);
switch (nextScanToken.tokenType)
{
case TokenType.Assignment:
case TokenType.AppendOperator:
case TokenType.SubtractOperator:
case TokenType.On:
case TokenType.AssignmentIfEmpty:
return true;
}
return false;
}
private Statement ParseReturnStatement()
{
_scanResult.Next().Is(TokenType.Return);
var returnExpression = ParseExpressionList();
_scanResult.Next().Is(TokenType.Terminator);
return new ReturnStatement {ReturnExpression = returnExpression};
}
private Statement ParseActionsDeclarationStatement()
{
_scanResult.Next().Is(TokenType.Actions);
var expressionList = ParseExpressionList();
var accoladeOpen = _scanResult.Next();
if (accoladeOpen.tokenType != TokenType.AccoladeOpen)
throw new ParsingException();
var result = new ActionsDeclarationStatement()
{
Name = expressionList.Last().As<LiteralExpression>().Value,
Modifiers = expressionList.Take(expressionList.Length - 1).ToNodeList(),
Actions = _scanResult.Next().literal
};
_scanResult.Next().Is(TokenType.AccoladeClose);
return result;
}
private Statement ParseRuleDeclarationStatement()
{
_scanResult.Next().Is(TokenType.Rule);
var ruleName = ParseExpression();
var arguments = ParseArgumentList().ToArray();
var body = ParseStatement();
var ruleNameStr = ruleName.As<LiteralExpression>().Value;
return new RuleDeclarationStatement
{
Name = ruleNameStr,
Arguments = arguments.SelectMany(ele => ele.Cast<LiteralExpression>()).Select(le => le.Value).ToArray(),
Body = (BlockStatement) body
};
}
private BlockStatement ParseBlockStatement()
{
_scanResult.Next().Is(TokenType.AccoladeOpen);
var statements = new List<Statement>();
while (true)
{
var peek = _scanResult.Peek();
if (peek.tokenType == TokenType.AccoladeClose)
{
_scanResult.Next();
return new BlockStatement {Statements = statements.ToNodeList()};
}
statements.Add(ParseStatement());
}
}
private Statement ParseIfStatement()
{
_scanResult.Next().Is(TokenType.If);
var ifStatement = new IfStatement {Condition = ParseCondition(), Body = ParseBlockStatement()};
var peek = _scanResult.Peek();
if (peek.tokenType == TokenType.Else)
{
_scanResult.Next();
ifStatement.Else = ParseStatement();
}
return ifStatement;
}
public Expression ParseExpression()
{
var scanToken = _scanResult.Peek();
var tokenType = scanToken.tokenType;
if (IsBinaryOperator(tokenType) && tokenType != TokenType.In)
return null;
switch (tokenType)
{
case TokenType.EOF:
case TokenType.Colon:
case TokenType.Terminator:
case TokenType.ParenthesisClose:
case TokenType.BracketClose:
return null;
case TokenType.VariableDereferencerOpen:
case TokenType.LiteralExpansionOpen:
return ParseExpansionStyleExpression();
case TokenType.AccoladeOpen:
return null;
case TokenType.BracketOpen:
return ParseInvocationExpression();
case TokenType.Not:
_scanResult.Next();
var expression = ParseExpression();
if (expression is BinaryOperatorExpression)
throw new ParsingException("see IfStatementWithNegationAndRightSide test");
return new NotOperatorExpression {Expression = expression};
default:
return ScanForCombineExpression(new LiteralExpression {Value = _scanResult.Next().literal});
}
}
private Expression ParseExpansionStyleExpression()
{
var token = _scanResult.Next();
var result = token.tokenType == TokenType.VariableDereferencerOpen ? (ExpansionStyleExpression) new VariableDereferenceExpression() : new LiteralExpansionExpression();
result.VariableExpression = ParseExpression();
var next = _scanResult.Next();
if (next.tokenType == TokenType.BracketOpen)
{
result.IndexerExpression = ParseExpression();
if (_scanResult.Next().tokenType != TokenType.BracketClose)
throw new ParsingException("Expected bracket close while parsing variable dereference expressions' indexer");
next = _scanResult.Next();
}
var modifiers = new NodeList<VariableDereferenceModifier>();
if (next.tokenType == TokenType.Colon)
{
while (true)
{
var modifier = _scanResult.Next();
if (modifier.tokenType == TokenType.Colon)
continue;
if (modifier.tokenType == TokenType.ParenthesisClose)
{
next = modifier;
break;
}
modifier.Is(TokenType.VariableExpansionModifier);
var peek = _scanResult.Peek();
Expression modifierValue = null;
if (peek.tokenType == TokenType.Assignment)
{
_scanResult.Next();
modifierValue = ParseExpression() ?? new EmptyExpression();
}
modifiers.Add(new VariableDereferenceModifier {Command = modifier.literal[0], Value = modifierValue});
}
}
result.Modifiers = modifiers;
next.Is(TokenType.ParenthesisClose);
return ScanForCombineExpression(result);
}
private static readonly Dictionary<TokenType, Operator> _binaryOperators = new Dictionary<TokenType, Operator>
{
{TokenType.AppendOperator, Operator.Append},
{TokenType.Assignment, Operator.Assignment},
{TokenType.SubtractOperator, Operator.Subtract},
{TokenType.In, Operator.In},
{TokenType.AssignmentIfEmpty, Operator.AssignmentIfEmpty},
{TokenType.And, Operator.And},
{TokenType.Or, Operator.Or},
{TokenType.NotEqual, Operator.NotEqual},
{TokenType.GreaterThan, Operator.GreaterThan},
{TokenType.LessThan, Operator.LessThan}
};
public static bool IsBinaryOperator(TokenType tokenType)
{
return _binaryOperators.ContainsKey(tokenType);
}
private static Operator OperatorFor(TokenType tokenType)
{
return _binaryOperators[tokenType];
}
Expression ScanForCombineExpression(Expression firstExpression)
{
var peek = _scanResult.Peek(false);
if (peek.tokenType == TokenType.EOF || (peek.tokenType != TokenType.Literal && peek.tokenType != TokenType.VariableDereferencerOpen && peek.tokenType != TokenType.LiteralExpansionOpen))
return firstExpression;
var tail = ParseExpression();
var combineExpressionTail = tail as CombineExpression;
var tailElements = combineExpressionTail != null
? combineExpressionTail.Elements
: new NodeList<Expression>(new[] {tail});
return new CombineExpression
{
Elements = tailElements.Prepend(firstExpression).ToNodeList()
};
}
private NodeList<NodeList<Expression>> ParseArgumentList()
{
var expressionLists = new NodeList<NodeList<Expression>> ();
while (true)
{
expressionLists.Add(ParseExpressionList());
var nextToken = _scanResult.Peek();
if (nextToken.tokenType == TokenType.Colon)
{
_scanResult.Next();
continue;
}
break;
}
return expressionLists;
}
}
public class ParsingException : Exception
{
public ParsingException(string s) : base(s)
{
}
public ParsingException()
{
}
}
}