This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathParser.cs
More file actions
2477 lines (2070 loc) · 86.8 KB
/
Parser.cs
File metadata and controls
2477 lines (2070 loc) · 86.8 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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// <license file="Parser.cs">
//
// The use and distribution terms for this software are contained in the file
// named 'LICENSE', which can be found in the resources directory of this
// distribution.
//
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// </license>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Text.RegularExpressions;
using EcmaScript.NET.Collections;
namespace EcmaScript.NET
{
/// <summary> This class implements the JavaScript parser.
///
/// It is based on the C source files jsparse.c and jsparse.h
/// in the jsref package.
///
/// </summary>
public class Parser
{
public string EncodedSource
{
get
{
return encodedSource;
}
}
// TokenInformation flags : currentFlaggedToken stores them together
// with token type
internal const int CLEAR_TI_MASK = 0xFFFF;
internal const int TI_AFTER_EOL = 1 << 16;
internal const int TI_CHECK_LABEL = 1 << 17; // indicates to check for label
internal readonly Regex SIMPLE_IDENTIFIER_NAME_PATTERN = new Regex("^[a-zA-Z_][a-zA-Z0-9_]*$", RegexOptions.Compiled);
internal CompilerEnvirons compilerEnv;
ErrorReporter errorReporter;
string sourceURI;
internal bool calledByCompileFunction;
TokenStream ts;
int currentFlaggedToken;
int syntaxErrorCount;
NodeFactory nf;
int nestingOfFunction;
Decompiler decompiler;
string encodedSource;
// The following are per function variables and should be saved/restored
// during function parsing.
// TODO: Move to separated class?
internal ScriptOrFnNode currentScriptOrFn;
int nestingOfWith;
Hashtable labelSet; // map of label names into nodes
ObjArray loopSet;
ObjArray loopAndSwitchSet;
// end of per function variables
// Exception to unwind
class ParserException : Exception
{
}
public Parser(CompilerEnvirons compilerEnv, ErrorReporter errorReporter)
{
this.compilerEnv = compilerEnv;
this.errorReporter = errorReporter;
}
Decompiler CreateDecompiler(CompilerEnvirons compilerEnv)
{
return new Decompiler();
}
internal void AddWarning(string messageId, string messageArg)
{
string message = ScriptRuntime.GetMessage(messageId, messageArg);
errorReporter.Warning(message, sourceURI, ts.Lineno, ts.Line, ts.Offset);
}
internal void AddError(string messageId)
{
++syntaxErrorCount;
string message = ScriptRuntime.GetMessage(messageId);
errorReporter.Error(message, sourceURI, ts.Lineno, ts.Line, ts.Offset);
}
internal Exception ReportError(string messageId)
{
AddError(messageId);
// Throw a ParserException exception to unwind the recursive descent
// parse.
throw new ParserException();
}
int peekToken()
{
int tt = currentFlaggedToken;
if (tt == Token.EOF)
{
while ((tt = ts.Token) == Token.CONDCOMMENT || tt == Token.KEEPCOMMENT)
{
if (tt == Token.CONDCOMMENT)
{
/* Support for JScript conditional comments */
decompiler.AddJScriptConditionalComment(ts.String);
}
else
{
/* Support for preserved comments */
decompiler.AddPreservedComment(ts.String);
}
}
if (tt == Token.EOL)
{
do
{
tt = ts.Token;
if (tt == Token.CONDCOMMENT)
{
/* Support for JScript conditional comments */
decompiler.AddJScriptConditionalComment(ts.String);
}
else if (tt == Token.KEEPCOMMENT)
{
/* Support for preserved comments */
decompiler.AddPreservedComment(ts.String);
}
}
while (tt == Token.EOL || tt == Token.CONDCOMMENT || tt == Token.KEEPCOMMENT);
tt |= TI_AFTER_EOL;
}
currentFlaggedToken = tt;
}
return tt & CLEAR_TI_MASK;
}
int peekFlaggedToken()
{
peekToken();
return currentFlaggedToken;
}
void consumeToken()
{
currentFlaggedToken = Token.EOF;
}
int nextToken()
{
int tt = peekToken();
consumeToken();
return tt;
}
int nextFlaggedToken()
{
peekToken();
int ttFlagged = currentFlaggedToken;
consumeToken();
return ttFlagged;
}
bool matchToken(int toMatch)
{
int tt = peekToken();
if (tt != toMatch)
{
return false;
}
consumeToken();
return true;
}
int peekTokenOrEOL()
{
int tt = peekToken();
// Check for last peeked token flags
if ((currentFlaggedToken & TI_AFTER_EOL) != 0)
{
tt = Token.EOL;
}
return tt;
}
void setCheckForLabel()
{
if ((currentFlaggedToken & CLEAR_TI_MASK) != Token.NAME)
throw Context.CodeBug();
currentFlaggedToken |= TI_CHECK_LABEL;
}
void mustMatchToken(int toMatch, string messageId)
{
if (!matchToken(toMatch))
{
ReportError(messageId);
}
}
void mustHaveXML()
{
if (!compilerEnv.isXmlAvailable())
{
ReportError("msg.XML.not.available");
}
}
public bool Eof
{
get
{
return ts.eof();
}
}
internal bool insideFunction()
{
return nestingOfFunction != 0;
}
Node enterLoop(Node loopLabel)
{
Node loop = nf.CreateLoopNode(loopLabel, ts.Lineno);
if (loopSet == null)
{
loopSet = new ObjArray();
if (loopAndSwitchSet == null)
{
loopAndSwitchSet = new ObjArray();
}
}
loopSet.push(loop);
loopAndSwitchSet.push(loop);
return loop;
}
void exitLoop()
{
loopSet.pop();
loopAndSwitchSet.pop();
}
Node enterSwitch(Node switchSelector, int lineno, Node switchLabel)
{
Node switchNode = nf.CreateSwitch(switchSelector, lineno);
if (loopAndSwitchSet == null)
{
loopAndSwitchSet = new ObjArray();
}
loopAndSwitchSet.push(switchNode);
return switchNode;
}
void exitSwitch()
{
loopAndSwitchSet.pop();
}
/*
* Build a parse tree from the given sourceString.
*
* @return an Object representing the parsed
* program. If the parse fails, null will be returned. (The
* parse failure will result in a call to the ErrorReporter from
* CompilerEnvirons.)
*/
public ScriptOrFnNode Parse(string sourceString, string sourceURI, int lineno)
{
this.sourceURI = sourceURI;
this.ts = new TokenStream(this, null, sourceString, lineno);
try
{
return Parse();
}
catch (System.IO.IOException)
{
// Should never happen
throw new Exception();
}
}
/*
* Build a parse tree from the given sourceString.
*
* @return an Object representing the parsed
* program. If the parse fails, null will be returned. (The
* parse failure will result in a call to the ErrorReporter from
* CompilerEnvirons.)
*/
public ScriptOrFnNode Parse(System.IO.StreamReader sourceReader, string sourceURI, int lineno)
{
this.sourceURI = sourceURI;
this.ts = new TokenStream(this, sourceReader, null, lineno);
return Parse();
}
ScriptOrFnNode Parse()
{
this.decompiler = CreateDecompiler(compilerEnv);
this.nf = new NodeFactory(this);
currentScriptOrFn = nf.CreateScript();
int sourceStartOffset = decompiler.CurrentOffset;
this.encodedSource = null;
decompiler.AddToken(Token.SCRIPT);
this.currentFlaggedToken = Token.EOF;
this.syntaxErrorCount = 0;
int baseLineno = ts.Lineno; // line number where source starts
/* so we have something to add nodes to until
* we've collected all the source */
Node pn = nf.CreateLeaf(Token.BLOCK);
for (; ; )
{
int tt = peekToken();
if (tt <= Token.EOF)
{
break;
}
Node n;
if (tt == Token.FUNCTION)
{
consumeToken();
try
{
n = function(calledByCompileFunction ? FunctionNode.FUNCTION_EXPRESSION : FunctionNode.FUNCTION_STATEMENT);
}
catch (ParserException)
{
break;
}
}
else
{
n = statement();
}
nf.addChildToBack(pn, n);
}
if (this.syntaxErrorCount != 0)
{
string msg = Convert.ToString(this.syntaxErrorCount);
msg = ScriptRuntime.GetMessage("msg.got.syntax.errors", msg);
throw errorReporter.RuntimeError(msg, sourceURI, baseLineno, null, 0);
}
currentScriptOrFn.SourceName = sourceURI;
currentScriptOrFn.BaseLineno = baseLineno;
currentScriptOrFn.EndLineno = ts.Lineno;
int sourceEndOffset = decompiler.CurrentOffset;
currentScriptOrFn.setEncodedSourceBounds(sourceStartOffset, sourceEndOffset);
nf.initScript(currentScriptOrFn, pn);
if (compilerEnv.isGeneratingSource())
{
encodedSource = decompiler.EncodedSource;
}
this.decompiler = null; // It helps GC
return currentScriptOrFn;
}
/*
* The C version of this function takes an argument list,
* which doesn't seem to be needed for tree generation...
* it'd only be useful for checking argument hiding, which
* I'm not doing anyway...
*/
Node parseFunctionBody()
{
++nestingOfFunction;
Node pn = nf.CreateBlock(ts.Lineno);
try
{
for (; ; )
{
Node n;
int tt = peekToken();
switch (tt)
{
case Token.ERROR:
case Token.EOF:
case Token.RC:
goto bodyLoop_brk;
case Token.FUNCTION:
consumeToken();
n = function(FunctionNode.FUNCTION_STATEMENT);
break;
default:
n = statement();
break;
}
nf.addChildToBack(pn, n);
}
bodyLoop_brk:
;
}
catch (ParserException)
{
// Ignore it
}
finally
{
--nestingOfFunction;
}
return pn;
}
Node function(int functionType)
{
using (Helpers.StackOverflowVerifier sov = new Helpers.StackOverflowVerifier(1024))
{
int syntheticType = functionType;
int baseLineno = ts.Lineno; // line number where source starts
int functionSourceStart = decompiler.MarkFunctionStart(functionType);
string name;
Node memberExprNode = null;
if (matchToken(Token.NAME))
{
name = ts.String;
decompiler.AddName(name);
if (!matchToken(Token.LP))
{
if (compilerEnv.isAllowMemberExprAsFunctionName())
{
// Extension to ECMA: if 'function <name>' does not follow
// by '(', assume <name> starts memberExpr
Node memberExprHead = nf.CreateName(name);
name = "";
memberExprNode = memberExprTail(false, memberExprHead);
}
mustMatchToken(Token.LP, "msg.no.paren.parms");
}
}
else if (matchToken(Token.LP))
{
// Anonymous function
name = "";
}
else
{
name = "";
if (compilerEnv.isAllowMemberExprAsFunctionName())
{
// Note that memberExpr can not start with '(' like
// in function (1+2).toString(), because 'function (' already
// processed as anonymous function
memberExprNode = memberExpr(false);
}
mustMatchToken(Token.LP, "msg.no.paren.parms");
}
if (memberExprNode != null)
{
syntheticType = FunctionNode.FUNCTION_EXPRESSION;
}
bool nested = insideFunction();
FunctionNode fnNode = nf.CreateFunction(name);
if (nested || nestingOfWith > 0)
{
// 1. Nested functions are not affected by the dynamic scope flag
// as dynamic scope is already a parent of their scope.
// 2. Functions defined under the with statement also immune to
// this setup, in which case dynamic scope is ignored in favor
// of with object.
fnNode.itsIgnoreDynamicScope = true;
}
int functionIndex = currentScriptOrFn.addFunction(fnNode);
int functionSourceEnd;
ScriptOrFnNode savedScriptOrFn = currentScriptOrFn;
currentScriptOrFn = fnNode;
int savedNestingOfWith = nestingOfWith;
nestingOfWith = 0;
Hashtable savedLabelSet = labelSet;
labelSet = null;
ObjArray savedLoopSet = loopSet;
loopSet = null;
ObjArray savedLoopAndSwitchSet = loopAndSwitchSet;
loopAndSwitchSet = null;
Node body;
try
{
decompiler.AddToken(Token.LP);
if (!matchToken(Token.RP))
{
bool first = true;
do
{
if (!first)
decompiler.AddToken(Token.COMMA);
first = false;
mustMatchToken(Token.NAME, "msg.no.parm");
string s = ts.String;
if (fnNode.hasParamOrVar(s))
{
AddWarning("msg.dup.parms", s);
}
fnNode.addParam(s);
decompiler.AddName(s);
}
while (matchToken(Token.COMMA));
mustMatchToken(Token.RP, "msg.no.paren.after.parms");
}
decompiler.AddToken(Token.RP);
mustMatchToken(Token.LC, "msg.no.brace.body");
decompiler.AddEol(Token.LC);
body = parseFunctionBody();
mustMatchToken(Token.RC, "msg.no.brace.after.body");
decompiler.AddToken(Token.RC);
functionSourceEnd = decompiler.MarkFunctionEnd(functionSourceStart);
if (functionType != FunctionNode.FUNCTION_EXPRESSION)
{
if (compilerEnv.LanguageVersion >= Context.Versions.JS1_2)
{
// function f() {} function g() {} is not allowed in 1.2
// or later but for compatibility with old scripts
// the check is done only if language is
// explicitly set.
// TODO: warning needed if version == VERSION_DEFAULT ?
int tt = peekTokenOrEOL();
if (tt == Token.FUNCTION)
{
ReportError("msg.no.semi.stmt");
}
}
// Add EOL only if function is not part of expression
// since it gets SEMI + EOL from Statement in that case
decompiler.AddToken(Token.EOL);
}
}
finally
{
loopAndSwitchSet = savedLoopAndSwitchSet;
loopSet = savedLoopSet;
labelSet = savedLabelSet;
nestingOfWith = savedNestingOfWith;
currentScriptOrFn = savedScriptOrFn;
}
fnNode.setEncodedSourceBounds(functionSourceStart, functionSourceEnd);
fnNode.SourceName = sourceURI;
fnNode.BaseLineno = baseLineno;
fnNode.EndLineno = ts.Lineno;
Node pn = nf.initFunction(fnNode, functionIndex, body, syntheticType);
if (memberExprNode != null)
{
pn = nf.CreateAssignment(Token.ASSIGN, memberExprNode, pn);
if (functionType != FunctionNode.FUNCTION_EXPRESSION)
{
// TOOD: check JScript behavior: should it be createExprStatement?
pn = nf.CreateExprStatementNoReturn(pn, baseLineno);
}
}
return pn;
}
}
Node statements()
{
Node pn = nf.CreateBlock(ts.Lineno);
int tt;
while ((tt = peekToken()) > Token.EOF && tt != Token.RC)
{
nf.addChildToBack(pn, statement());
}
return pn;
}
Node condition()
{
Node pn;
mustMatchToken(Token.LP, "msg.no.paren.cond");
decompiler.AddToken(Token.LP);
pn = expr(false);
mustMatchToken(Token.RP, "msg.no.paren.after.cond");
decompiler.AddToken(Token.RP);
// there's a check here in jsparse.c that corrects = to ==
return pn;
}
// match a NAME; return null if no match.
Node matchJumpLabelName()
{
Node label = null;
int tt = peekTokenOrEOL();
if (tt == Token.NAME)
{
consumeToken();
string name = ts.String;
decompiler.AddName(name);
if (labelSet != null)
{
label = (Node)labelSet[name];
}
if (label == null)
{
ReportError("msg.undef.label");
}
}
return label;
}
Node statement()
{
using (Helpers.StackOverflowVerifier sov = new Helpers.StackOverflowVerifier(512))
{
try
{
Node pn = statementHelper(null);
if (pn != null)
{
return pn;
}
}
catch (ParserException)
{
}
}
// skip to end of statement
int lineno = ts.Lineno;
for (; ; )
{
int tt = peekTokenOrEOL();
consumeToken();
switch (tt)
{
case Token.ERROR:
case Token.EOF:
case Token.EOL:
case Token.SEMI:
goto guessingStatementEnd_brk;
}
}
guessingStatementEnd_brk:
;
return nf.CreateExprStatement(nf.CreateName("error"), lineno);
}
/// <summary> Whether the "catch (e: e instanceof Exception) { ... }" syntax
/// is implemented.
/// </summary>
Node statementHelper(Node statementLabel)
{
Node pn = null;
int tt;
tt = peekToken();
switch (tt)
{
case Token.IF:
{
consumeToken();
decompiler.AddToken(Token.IF);
int lineno = ts.Lineno;
Node cond = condition();
decompiler.AddEol(Token.LC);
Node ifTrue = statement();
Node ifFalse = null;
if (matchToken(Token.ELSE))
{
decompiler.AddToken(Token.RC);
decompiler.AddToken(Token.ELSE);
decompiler.AddEol(Token.LC);
ifFalse = statement();
}
decompiler.AddEol(Token.RC);
pn = nf.CreateIf(cond, ifTrue, ifFalse, lineno);
return pn;
}
case Token.SWITCH:
{
consumeToken();
decompiler.AddToken(Token.SWITCH);
int lineno = ts.Lineno;
mustMatchToken(Token.LP, "msg.no.paren.switch");
decompiler.AddToken(Token.LP);
pn = enterSwitch(expr(false), lineno, statementLabel);
try
{
mustMatchToken(Token.RP, "msg.no.paren.after.switch");
decompiler.AddToken(Token.RP);
mustMatchToken(Token.LC, "msg.no.brace.switch");
decompiler.AddEol(Token.LC);
bool hasDefault = false;
for (; ; )
{
tt = nextToken();
Node caseExpression;
switch (tt)
{
case Token.RC:
goto switchLoop_brk;
case Token.CASE:
decompiler.AddToken(Token.CASE);
caseExpression = expr(false);
mustMatchToken(Token.COLON, "msg.no.colon.case");
decompiler.AddEol(Token.COLON);
break;
case Token.DEFAULT:
if (hasDefault)
{
ReportError("msg.double.switch.default");
}
decompiler.AddToken(Token.DEFAULT);
hasDefault = true;
caseExpression = null;
mustMatchToken(Token.COLON, "msg.no.colon.case");
decompiler.AddEol(Token.COLON);
break;
default:
ReportError("msg.bad.switch");
goto switchLoop_brk;
}
Node block = nf.CreateLeaf(Token.BLOCK);
while ((tt = peekToken()) != Token.RC && tt != Token.CASE && tt != Token.DEFAULT && tt != Token.EOF)
{
nf.addChildToBack(block, statement());
}
// caseExpression == null => add default lable
nf.addSwitchCase(pn, caseExpression, block);
}
switchLoop_brk:
;
decompiler.AddEol(Token.RC);
nf.closeSwitch(pn);
}
finally
{
exitSwitch();
}
return pn;
}
case Token.WHILE:
{
consumeToken();
decompiler.AddToken(Token.WHILE);
Node loop = enterLoop(statementLabel);
try
{
Node cond = condition();
decompiler.AddEol(Token.LC);
Node body = statement();
decompiler.AddEol(Token.RC);
pn = nf.CreateWhile(loop, cond, body);
}
finally
{
exitLoop();
}
return pn;
}
case Token.DO:
{
consumeToken();
decompiler.AddToken(Token.DO);
decompiler.AddEol(Token.LC);
Node loop = enterLoop(statementLabel);
try
{
Node body = statement();
decompiler.AddToken(Token.RC);
mustMatchToken(Token.WHILE, "msg.no.while.do");
decompiler.AddToken(Token.WHILE);
Node cond = condition();
pn = nf.CreateDoWhile(loop, body, cond);
}
finally
{
exitLoop();
}
// Always auto-insert semicon to follow SpiderMonkey:
// It is required by EMAScript but is ignored by the rest of
// world, see bug 238945
matchToken(Token.SEMI);
decompiler.AddEol(Token.SEMI);
return pn;
}
case Token.FOR:
{
consumeToken();
bool isForEach = false;
decompiler.AddToken(Token.FOR);
Node loop = enterLoop(statementLabel);
try
{
Node init; // Node init is also foo in 'foo in Object'
Node cond; // Node cond is also object in 'foo in Object'
Node incr = null; // to kill warning
Node body;
// See if this is a for each () instead of just a for ()
if (matchToken(Token.NAME))
{
decompiler.AddName(ts.String);
if (ts.String.Equals("each"))
{
isForEach = true;
}
else
{
ReportError("msg.no.paren.for");
}
}
mustMatchToken(Token.LP, "msg.no.paren.for");
decompiler.AddToken(Token.LP);
tt = peekToken();
if (tt == Token.SEMI)
{
init = nf.CreateLeaf(Token.EMPTY);
}
else
{
if (tt == Token.VAR)
{
// set init to a var list or initial
consumeToken(); // consume the 'var' token
init = variables(true);
}
else
{
init = expr(true);
}
}
if (matchToken(Token.IN))
{
decompiler.AddToken(Token.IN);
// 'cond' is the object over which we're iterating
cond = expr(false);
}
else
{
// ordinary for loop
mustMatchToken(Token.SEMI, "msg.no.semi.for");
decompiler.AddToken(Token.SEMI);
if (peekToken() == Token.SEMI)
{
// no loop condition
cond = nf.CreateLeaf(Token.EMPTY);
}
else
{
cond = expr(false);
}
mustMatchToken(Token.SEMI, "msg.no.semi.for.cond");
decompiler.AddToken(Token.SEMI);
if (peekToken() == Token.RP)
{
incr = nf.CreateLeaf(Token.EMPTY);
}
else
{
incr = expr(false);
}
}
mustMatchToken(Token.RP, "msg.no.paren.for.ctrl");
decompiler.AddToken(Token.RP);
decompiler.AddEol(Token.LC);
body = statement();
decompiler.AddEol(Token.RC);
if (incr == null)
{
// cond could be null if 'in obj' got eaten
// by the init node.
pn = nf.CreateForIn(loop, init, cond, body, isForEach);
}
else
{
pn = nf.CreateFor(loop, init, cond, incr, body);
}
}
finally
{
exitLoop();
}
return pn;
}
case Token.TRY:
{
consumeToken();
int lineno = ts.Lineno;
Node tryblock;
Node catchblocks = null;
Node finallyblock = null;
decompiler.AddToken(Token.TRY);
decompiler.AddEol(Token.LC);
tryblock = statement();
decompiler.AddEol(Token.RC);
catchblocks = nf.CreateLeaf(Token.BLOCK);
bool sawDefaultCatch = false;
int peek = peekToken();
if (peek == Token.CATCH)
{
while (matchToken(Token.CATCH))
{
if (sawDefaultCatch)
{
ReportError("msg.catch.unreachable");
}
decompiler.AddToken(Token.CATCH);