-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathCompiler.java
2913 lines (2684 loc) · 96.7 KB
/
Compiler.java
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
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
package com.tangosol.dev.compiler.java;
import com.tangosol.dev.assembler.CodeAttribute;
import com.tangosol.dev.assembler.Return;
import com.tangosol.dev.compiler.CompilerErrorInfo;
import com.tangosol.dev.compiler.CompilerException;
import com.tangosol.dev.compiler.SyntaxException;
import com.tangosol.dev.compiler.Context;
import com.tangosol.dev.compiler.TypeInfo;
import com.tangosol.dev.compiler.MethodInfo;
import com.tangosol.dev.compiler.ParamInfo;
import com.tangosol.dev.component.DataType;
import com.tangosol.util.Base;
import com.tangosol.util.ErrorList;
import com.tangosol.util.NullImplementation;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.HashMap;
/**
* This class implements the Java script compiler.
*
* 1. The Java script is passed as a string. The first step is to create
* a character stream (using the Script interface) which understands
* Unicode escape sequences; this is done within the Tokenizer by using
* the UnicodeScript class.
*
* 2. The second step is to lexically analyze and parse the Java script.
* This is done by the Tokenizer class.
*
* 3. The third step is to parse the tokens into a parse tree. This is
* done by this class (Compiler) using the Statement and Expression
* classes.
*
* 4. The fourth step is to semantically analyze the parse tree. This is
* handled by the various statements and expressions within the parse
* tree. This step is referred to as "precompile".
*
* 5. The last step is to generate the Java byte codes necessary for each
* statement and expression. This step is referred to as "compile".
*
* The following is the hierarchy of language elements. Note that only the
* leaf elements are non-abstract:
*
* Element
* Statement
* EmptyStatement
* DeclarationStatement
* ExpressionStatement
* ConditionalStatement (note: does not include "for")
* IfStatement
* DoStatement
* WhileStatement
* Block
* StatementBlock
* ForStatement
* CatchClause
* SwitchStatement
* TargetStatement
* LabelStatement
* CaseClause
* DefaultClause
* GuardedStatement
* TryStatement
* SynchronizedStatement
* FinallyClause
* BranchStatement
* BreakStatement
* ContinueStatement
* ExitStatement
* ReturnStatement
* ThrowStatement
* Expression
* NameExpression
* TypeExpression
* DimensionedExpression
* LiteralExpression
* NullExpression
* BooleanExpression
* CharExpression
* IntExpression
* LongExpression
* FloatExpression
* DoubleExpression
* StringExpression
* ArrayExpression
* VariableExpression
* ConditionalExpression
* NewExpression
* NewClassExpression
* NewArrayExpression
* UnaryExpression
* IncExpression
* PreIncExpression
* PostIncExpression
* PreDecExpression
* PostDecExpression
* PlusExpression
* MinusExpression
* NotExpression
* BitNotExpression
* CastExpression
* ArrayAccessExpression
* FieldAccessExpression
* InvocationExpression
* BinaryExpression
* AssignExpression
* CastAssignExpression
* LogicalExpression
* AndExpression
* OrExpression
* BitwiseExpression
* BitAndExpression
* BitOrExpression
* BitXorExpression
* EqualityExpression
* EqualExpression
* NotEqualExpression
* RelationalExpression
* LessExpression
* NotLessExpression
* GreaterExpression
* NotGreaterExpression
* InstanceOfExpression
* ShiftExpression
* LeftShiftExpression
* RightShiftExpression
* UnsignedShiftExpression
* AdditiveExpression
* AddExpression
* SubtractExpression
* MultiplicativeExpression
* MultiplyExpression
* DivideExpression
* ModuloExpression
*
* @version 1.00, 09/14/98
* @author Cameron Purdy
*/
public class Compiler
extends Base
implements com.tangosol.dev.compiler.Compiler, Constants, TokenConstants
{
// ----- construction ---------------------------------------------------
/**
* Construct a Java compiler. A public default constructor is required.
*/
public Compiler()
{
}
// ----- compiler interface ---------------------------------------------
/**
* Compile the passed script.
*
* @param ctx the compiler context
* @param sScript the script to compile (as a string)
* @param errlist the error list to log to
*
* @exception CompilerException thrown if the compilation of this script
* fails
*/
public void compile(Context ctx, String sScript, ErrorList errlist)
throws CompilerException
{
// parameters are required
if (ctx == null || sScript == null || errlist == null)
{
throw new IllegalArgumentException(CLASS + ".compile: "
+ "Parameters required!");
}
// register parameter names/types
Block block = new StatementBlock();
// for instance methods, there is an implied final parameter "this"
MethodInfo method = ctx.getMethodInfo();
azzert(method != null, "Failed to retrieve the context method");
if (DEBUG)
{
out();
printMethodInfo(method);
}
if (!method.isStatic())
{
block.addStatement(createParameterDeclaration(block, true,
method.getTypeInfo().getDataType(), "this"));
}
int cParams = method.getParamCount();
for (int i = 0; i < cParams; ++i)
{
ParamInfo param = method.getParamInfo(i);
block.addStatement(createParameterDeclaration(block,
param.isFinal(), param.getDataType(), param.getName()));
}
// store the information used by parsing/code generation
CodeAttribute code = ctx.getCode();
this.errlist = errlist;
this.toker = new Tokenizer(sScript, code.getLine(), errlist);
this.token = next();
// parse the script
parseCompilationUnit(block);
if (DEBUG)
{
block.print();
}
if (errlist.isSevere())
{
throw new CompilerException();
}
// check the imports
checkImports(ctx);
if (errlist.isSevere())
{
throw new CompilerException();
}
// check the parse tree
DualSet setUVars = new DualSet(NullImplementation.getSet());
DualSet setFVars = new DualSet(NullImplementation.getSet());
HashMap mapThrown = new HashMap();
block.precompile(ctx, setUVars, setFVars, mapThrown, errlist);
if (!mapThrown.isEmpty())
{
for (Enumeration enmr = method.exceptionTypes(); enmr.hasMoreElements(); )
{
Expression.catchException(ctx, (DataType) enmr.nextElement(), mapThrown);
if (mapThrown.isEmpty())
{
break;
}
}
}
// uncaught/undeclared exceptions
if (!mapThrown.isEmpty())
{
for (Iterator iterThrown = mapThrown.entrySet().iterator(); iterThrown.hasNext(); )
{
// map key is the data type of the exception; map value is a
// set of expressions that throw the exception
Entry entry = (Entry) iterThrown.next();
DataType dtThrown = (DataType) entry.getKey();
for (Iterator iterExpr = ((Set) entry.getValue()).iterator(); iterExpr.hasNext(); )
{
Expression expr = (Expression) iterExpr.next();
expr.logError(ERROR, EXCEPT_UNCAUGHT,
new String[] {dtThrown.getClassName()}, errlist);
}
}
}
// check for errors from the pre-compile pass
if (errlist.isSevere())
{
throw new CompilerException();
}
// generate code
if (block.compile(ctx, code, true, errlist))
{
// the main block completes; if the method is void, then add
// the implied return, otherwise it is an error
if (method.getDataType() == DataType.VOID)
{
code.add(new Return());
}
else
{
logError(ERROR, RETURN_MISSING, null,
block.getEndLine(), block.getEndOffset(), 0);
}
}
if (DEBUG)
{
code.print();
out();
}
// check for errors from the compile pass
if (errlist.isSevere())
{
throw new CompilerException();
}
}
// ----- script parsing -------------------------------------------------
/**
* Parse the script.
*
* Goal:
* CompilationUnit
* CompilationUnit:
* ImportDeclarations-opt BlockStatements-opt
*/
protected void parseCompilationUnit(Block block)
throws CompilerException
{
parseImportDeclarations();
parseBlockStatements(block);
// after parsing the block, the remaining token should be the pretend
// closing curly brace for the block
if (token != null && token.id == SEP_RCURLYBRACE && token.length == 0)
{
// the block was started with a corresponding pretend open curly
block.setEndToken(token);
}
else
{
// log error - tokens remaining, probably missing {
logError(ERROR, UNBALANCED_BRACE, null, token.getLine(), token.getOffset(), 0);
}
}
/**
* Parse the "import" declarations and register each imported class under
* its short name.
*/
protected void parseImportDeclarations()
throws CompilerException
{
while (peek(KEY_IMPORT) != null)
{
try
{
Token tokName; // name token
String sFull; // fully qualified name
// parse name "n.n.n"
StringBuffer sb = new StringBuffer();
boolean fFirst = true;
do
{
if (fFirst)
{
fFirst = false;
}
else
{
sb.append('.');
}
tokName = match(IDENT);
sb.append(tokName.getText());
}
while (peek(SEP_DOT) != null);
sFull = sb.toString();
// check for optional alias
if (peek(KEY_AS) != null)
{
tokName = match(IDENT);
}
match(SEP_SEMICOLON);
// register the import name
tblImports.put(tokName, sFull);
}
catch (SyntaxException e)
{
Expurgate: while (true)
{
switch (token.id)
{
// end of an import or statement
case SEP_SEMICOLON:
next();
// start of an import
case KEY_IMPORT:
// start of a statement
case KEY_BREAK:
case KEY_CASE:
case KEY_CONTINUE:
case KEY_DEFAULT:
case KEY_DO:
case KEY_FINAL:
case KEY_FOR:
case KEY_IF:
case KEY_RETURN:
case KEY_SWITCH:
case KEY_SYNCHRONIZED:
case KEY_THROW:
case KEY_TRY:
case KEY_WHILE:
// could be end of script - definitely not part of
// the imports!
case SEP_RCURLYBRACE:
break Expurgate;
default:
next();
}
}
}
}
}
// ----- statement parsing ----------------------------------------------
/**
* Parse a statement block.
*
* Block:
* { BlockStatements-opt }
*/
protected Block parseBlock(Statement outer)
throws CompilerException
{
Block block = new StatementBlock(outer, token);
if (token.id == SEP_LCURLYBRACE)
{
// { BlockStatements-opt }
match(SEP_LCURLYBRACE);
parseBlockStatements(block);
block.setEndToken(match(SEP_RCURLYBRACE));
}
else
{
// open curly expected; someone probably just forgot their curlies
logError(ERROR, TOKEN_EXPECTED, new String[] {"{"},
token.getLine(), token.getOffset(), 0);
// question: if the statement parsing fails, would it be
// better to handle a syntax error here or to let the caller
// deal with it? for now, we'll assume the latter
block.addStatement(parseStatement(block));
}
return block;
}
/**
* Parse a sequence of statements.
*
* BlockStatements:
* BlockStatement
* BlockStatements BlockStatement
*/
protected void parseBlockStatements(Block block)
throws CompilerException
{
while (token.id != SEP_RCURLYBRACE)
{
try
{
block.addStatement(parseStatement(block));
}
catch (SyntaxException e)
{
// an error occurred parsing the statement ... skip it
expurgateStatement();
}
}
}
/**
* Parse a statement.
*
* @param outer contains the statement being parsed
*/
protected Statement parseStatement(Statement outer)
throws CompilerException
{
// determine the containing block
Block block = (outer instanceof Block ? (Block) outer : outer.getBlock());
switch (token.id)
{
// EmptyStatement:
// ;
case SEP_SEMICOLON:
return new EmptyStatement(outer, current());
// Block:
// { BlockStatements-opt }
case SEP_LCURLYBRACE:
return parseBlock(outer);
// TryStatement:
// try Block Catches
// try Block Catches-opt Finally
// Catches:
// CatchClause
// Catches CatchClause
// CatchClause:
// catch ( FormalParameter ) Block
// Finally:
// finally Block
case KEY_TRY:
{
TryStatement stmt = new TryStatement(outer, current());
stmt.setInnerStatement(parseBlock(stmt));
CatchClause last = null;
boolean fNoClauses = true;
while (token.id == KEY_CATCH)
{
CatchClause clause = new CatchClause(stmt, current());
match(SEP_LPARENTHESIS);
// note that the CatchClause itself is the Block (and
// that the exception variable is treated as a parameter)
DeclarationStatement stmtDecl = parseDeclaration(clause);
stmtDecl.setParameter(true);
clause.addStatement(stmtDecl);
match(SEP_RPARENTHESIS);
clause.addStatement(parseBlock(clause));
if (fNoClauses)
{
stmt.setCatchClause(clause);
}
else
{
last.setNextStatement(clause);
}
last = clause;
fNoClauses = false;
}
if (token.id == KEY_FINALLY)
{
FinallyClause clause = new FinallyClause(stmt, current());
clause.setInnerStatement(parseBlock(clause));
stmt.setFinallyClause(clause);
fNoClauses = false;
}
if (fNoClauses)
{
// no deadly parsing errors have occurred, but parts of
// the try statement are missing
// TODO log error
}
return stmt;
}
// SynchronizedStatement:
// synchronized ( Expression ) Block
case KEY_SYNCHRONIZED:
{
SynchronizedStatement stmt = new SynchronizedStatement(outer, current());
match(SEP_LPARENTHESIS);
stmt.setExpression(parseExpression(block));
match(SEP_RPARENTHESIS);
Statement inner = parseBlock(stmt);
stmt.setInnerStatement(inner);
stmt.setEndToken(inner.getEndToken());
return stmt;
}
// DoStatement:
// do Statement while ( Expression ) ;
case KEY_DO:
{
DoStatement stmt = new DoStatement(outer, current());
stmt.setInnerStatement(parseStatement(stmt));
match(KEY_WHILE);
match(SEP_LPARENTHESIS);
stmt.setTest(parseExpression(block));
match(SEP_RPARENTHESIS);
stmt.setEndToken(match(SEP_SEMICOLON));
return stmt;
}
// WhileStatement:
// while ( Expression ) Statement
case KEY_WHILE:
{
WhileStatement stmt = new WhileStatement(outer, current());
match(SEP_LPARENTHESIS);
stmt.setTest(parseExpression(block));
match(SEP_RPARENTHESIS);
stmt.setInnerStatement(parseStatement(stmt));
return stmt;
}
// ForStatement:
// for ( ForInit-opt ; Expression-opt ; ForUpdate-opt ) Statement
// ForInit:
// StatementExpressionList
// LocalVariableDeclaration
// ForUpdate:
// StatementExpressionList
// StatementExpressionList:
// StatementExpression
// StatementExpressionList , StatementExpression
case KEY_FOR:
{
ForStatement stmt = new ForStatement(outer, current());
match(SEP_LPARENTHESIS);
if (token.id != SEP_SEMICOLON)
{
stmt.setInit(parseStatementList(stmt, true));
}
match(SEP_SEMICOLON);
if (token.id != SEP_SEMICOLON)
{
// note that the ForStatement itself is the Block
stmt.setTest(parseExpression(stmt));
}
match(SEP_SEMICOLON);
if (token.id != SEP_RPARENTHESIS)
{
stmt.setUpdate(parseStatementList(stmt, false));
}
match(SEP_RPARENTHESIS);
stmt.setInnerStatement(parseStatement(stmt));
return stmt;
}
// IfThenStatement:
// if ( Expression ) Statement
// IfThenElseStatement:
// if ( Expression ) StatementNoShortIf else Statement
case KEY_IF:
{
IfStatement stmt = new IfStatement(outer, current());
match(SEP_LPARENTHESIS);
stmt.setTest(parseExpression(block));
match(SEP_RPARENTHESIS);
stmt.setThenStatement(parseStatement(stmt));
// although the LALR(1) grammar goes into great detail about
// the "NoShortIf" constructs, recursive decent parsing only
// cares whether or not an else exists at this point
if (peek(KEY_ELSE) != null)
{
stmt.setElseStatement(parseStatement(stmt));
}
return stmt;
}
// ReturnStatement:
// return Expression-opt ;
case KEY_RETURN:
{
ReturnStatement stmt = new ReturnStatement(outer, current());
if (token.id != SEP_SEMICOLON)
{
stmt.setExpression(parseExpression(block));
}
stmt.setEndToken(match(SEP_SEMICOLON));
return stmt;
}
// ThrowStatement:
// throw Expression ;
case KEY_THROW:
{
ThrowStatement stmt = new ThrowStatement(outer, current());
stmt.setExpression(parseExpression(block));
stmt.setEndToken(match(SEP_SEMICOLON));
return stmt;
}
// SwitchStatement:
// switch ( Expression ) SwitchBlock
// SwitchBlock:
// { SwitchBlockStatementGroups-opt SwitchLabels-opt }
// SwitchBlockStatementGroups:
// SwitchBlockStatementGroup
// SwitchBlockStatementGroups SwitchBlockStatementGroup
// SwitchBlockStatementGroup:
// SwitchLabels BlockStatements
// SwitchLabels:
// SwitchLabel
// SwitchLabels SwitchLabel
// SwitchLabel:
// case ConstantExpression :
// default :
case KEY_SWITCH:
{
SwitchStatement stmt = new SwitchStatement(outer, current());
match(SEP_LPARENTHESIS);
stmt.setTest(parseExpression(block));
match(SEP_RPARENTHESIS);
match(SEP_LCURLYBRACE);
SwitchBlock: while (true)
{
try
{
switch (token.id)
{
case KEY_CASE:
{
CaseClause clause = new CaseClause(stmt, current());
// note that SwitchStatement itself is the block
clause.setTest(parseExpression(stmt));
clause.setEndToken(match(OP_COLON));
stmt.addStatement(clause);
}
break;
case KEY_DEFAULT:
{
DefaultClause clause = new DefaultClause(stmt, current());
clause.setEndToken(match(OP_COLON));
stmt.addStatement(clause);
}
break;
default:
stmt.addStatement(parseStatement(stmt));
break;
case SEP_RCURLYBRACE:
break SwitchBlock;
}
}
catch (SyntaxException e)
{
expurgateStatement();
}
}
stmt.setEndToken(current()); // right curly
return stmt;
}
// BreakStatement:
// break Identifier-opt ;
case KEY_BREAK:
{
BreakStatement stmt = new BreakStatement(outer, current(), peek(IDENT));
stmt.setEndToken(match(SEP_SEMICOLON));
return stmt;
}
// ContinueStatement:
// continue Identifier-opt ;
case KEY_CONTINUE:
{
ContinueStatement stmt = new ContinueStatement(outer, current(), peek(IDENT));
stmt.setEndToken(match(SEP_SEMICOLON));
return stmt;
}
// VariableDeclaration:
// Modifiers-opt Type VariableDeclarators
case KEY_FINAL:
case KEY_BOOLEAN:
case KEY_BYTE:
case KEY_CHAR:
case KEY_SHORT:
case KEY_INT:
case KEY_LONG:
case KEY_FLOAT:
case KEY_DOUBLE:
{
Statement stmt = parseDeclaration(outer);
stmt.setEndToken(match(SEP_SEMICOLON));
return stmt;
}
// LabeledStatement:
// Identifier : Statement
// VariableDeclaration:
// Modifiers-opt Type VariableDeclarators
// ExpressionStatement:
// StatementExpression ;
case IDENT:
{
Expression expr = parseExpression(block);
switch (token.id)
{
case IDENT:
{
// expr must be the type in a variable declaration
Statement stmt = parseDeclaration(outer, null, toTypeExpression(expr));
stmt.setEndToken(match(SEP_SEMICOLON));
return stmt;
}
case OP_COLON:
{
// expr must be a label, which is a simple identifier
Token tokLabel = expr.getStartToken();
if (tokLabel == expr.getEndToken() && tokLabel.getCategory() == IDENTIFIER)
{
LabelStatement stmt = new LabelStatement(outer, tokLabel, match(OP_COLON));
stmt.setInnerStatement(parseStatement(stmt));
return stmt;
}
// assume it was supposed to be a statement expression
// (so fall through)
}
default:
{
// expr must be a statement expression
// turn it into a ExpressionStatement
Statement stmt = createExpressionStatement(outer, expr);
stmt.setEndToken(match(SEP_SEMICOLON));
return stmt;
}
}
}
// ExpressionStatement:
// StatementExpression ;
case KEY_NEW:
case KEY_THIS:
case KEY_SUPER:
case SEP_LPARENTHESIS:
case OP_INCREMENT:
case OP_DECREMENT:
{
Expression expr = parseExpression(block);
Statement stmt = createExpressionStatement(outer, expr);
stmt.setEndToken(match(SEP_SEMICOLON));
return stmt;
}
// unexpected statement continuations
case KEY_ELSE:
logError(ERROR, ELSE_NO_IF, new String[] {token.getText()}, token);
throw new SyntaxException();
case KEY_CATCH:
logError(ERROR, CATCH_NO_TRY, new String[] {token.getText()}, token);
throw new SyntaxException();
case KEY_FINALLY:
logError(ERROR, FINALLY_NO_TRY, new String[] {token.getText()}, token);
throw new SyntaxException();
case KEY_CASE:
case KEY_DEFAULT:
logError(ERROR, LABEL_NO_SWITCH, new String[] {token.getText()}, token);
throw new SyntaxException();
// unexpected separator; probably recoverable
case SEP_DOT:
case SEP_COMMA:
case SEP_RPARENTHESIS:
case SEP_LBRACKET:
case SEP_RBRACKET:
// unexpected keyword; probably recoverable
case KEY_INSTANCEOF:
// unexpected operator; probably recoverable
case OP_ADD:
case OP_SUB:
case OP_MUL:
case OP_DIV:
case OP_REM:
case OP_SHL:
case OP_SHR:
case OP_USHR:
case OP_BITAND:
case OP_BITOR:
case OP_BITXOR:
case OP_BITNOT:
case OP_ASSIGN:
case OP_ASSIGN_ADD:
case OP_ASSIGN_SUB:
case OP_ASSIGN_MUL:
case OP_ASSIGN_DIV:
case OP_ASSIGN_REM:
case OP_ASSIGN_SHL:
case OP_ASSIGN_SHR:
case OP_ASSIGN_USHR:
case OP_ASSIGN_BITAND:
case OP_ASSIGN_BITOR:
case OP_ASSIGN_BITXOR:
case OP_TEST_EQ:
case OP_TEST_NE:
case OP_TEST_GT:
case OP_TEST_GE:
case OP_TEST_LT:
case OP_TEST_LE:
case OP_LOGICAL_AND:
case OP_LOGICAL_OR:
case OP_LOGICAL_NOT:
case OP_CONDITIONAL: // identifier missing?
case OP_COLON: // label missing?
// unexpected literal value; probably recoverable
case LIT_NULL:
case LIT_TRUE:
case LIT_FALSE:
case LIT_CHAR:
case LIT_INT:
case LIT_LONG:
case LIT_FLOAT:
case LIT_DOUBLE:
case LIT_STRING:
logError(ERROR, TOKEN_UNEXPECTED, new String[] {token.getText()}, token);
throw new SyntaxException();
// totally unexpected tokens ... assume unrecoverable
case SEP_RCURLYBRACE:
case KEY_IMPORT:
logError(ERROR, TOKEN_PANIC, new String[] {token.getText()}, token);
throw new CompilerException();
// illegal keywords - not used in Java script language
case KEY_ABSTRACT:
case KEY_CLASS:
case KEY_EXTENDS:
case KEY_IMPLEMENTS:
case KEY_INTERFACE:
case KEY_NATIVE:
case KEY_PACKAGE:
case KEY_PRIVATE:
case KEY_PROTECTED:
case KEY_PUBLIC:
case KEY_STATIC:
case KEY_THROWS:
case KEY_TRANSIENT:
case KEY_VOID:
case KEY_VOLATILE:
// unsupported keyword in a statement
logError(ERROR, TOKEN_UNSUPP, new String[] {token.getText()}, token);
throw new CompilerException();
// illegal keywords - not used in Java
case KEY_CONST:
case KEY_GOTO:
logError(ERROR, TOKEN_ILLEGAL, new String[] {token.getText()}, token);
throw new CompilerException();
default:
logError(ERROR, TOKEN_UNKNOWN, new String[] {token.getText()}, token);
throw new CompilerException();
}
}
/**
* Parse a statement expression list. Additional statements are linked
* after the first (via the Statement.next field). This is used only
* within the "for" statement.
*
* ForInit:
* StatementExpressionList
* LocalVariableDeclaration
* ForUpdate:
* StatementExpressionList
* StatementExpressionList:
* StatementExpression
* StatementExpressionList , StatementExpression
*
* @param outer contains the statement being parsed
* @param fDeclare true if the statement expression list may be a
* variable declaration
*
* @return the parsed statement(s)
*/
protected Statement parseStatementList(Statement outer, boolean fDeclare)
throws CompilerException
{
Block block = (outer instanceof Block ? (Block) outer : outer.getBlock());
// check for a variable declaration
Token tokFinal = (fDeclare ? peek(KEY_FINAL) : null);
Expression expr = parseExpression(block);
if (fDeclare && (tokFinal != null || token.id == IDENT))
{
return parseDeclaration(outer, tokFinal, toTypeExpression(expr));
}
// build the StatementExpressionList
ExpressionStatement stmt = createExpressionStatement(outer, expr);
ExpressionStatement last = stmt;