1f05db8f59404de515026c80c1358b8388ece1f4
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / parser / Parser.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2002 www.phpeclipse.de All rights reserved. This program and the accompanying material are made available under the
3  * terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  *
6  * Contributors: www.phpeclipse.de
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.compiler.parser;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.HashSet;
13
14 import net.sourceforge.phpdt.core.compiler.CharOperation;
15 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
16 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
17 import net.sourceforge.phpdt.internal.compiler.ast.AND_AND_Expression;
18 import net.sourceforge.phpdt.internal.compiler.ast.ASTNode;
19 import net.sourceforge.phpdt.internal.compiler.ast.AbstractMethodDeclaration;
20 import net.sourceforge.phpdt.internal.compiler.ast.BinaryExpression;
21 import net.sourceforge.phpdt.internal.compiler.ast.Block;
22 import net.sourceforge.phpdt.internal.compiler.ast.BreakStatement;
23 import net.sourceforge.phpdt.internal.compiler.ast.CompilationUnitDeclaration;
24 import net.sourceforge.phpdt.internal.compiler.ast.ConditionalExpression;
25 import net.sourceforge.phpdt.internal.compiler.ast.ContinueStatement;
26 import net.sourceforge.phpdt.internal.compiler.ast.EqualExpression;
27 import net.sourceforge.phpdt.internal.compiler.ast.Expression;
28 import net.sourceforge.phpdt.internal.compiler.ast.FieldDeclaration;
29 import net.sourceforge.phpdt.internal.compiler.ast.FieldReference;
30 import net.sourceforge.phpdt.internal.compiler.ast.IfStatement;
31 import net.sourceforge.phpdt.internal.compiler.ast.ImportReference;
32 import net.sourceforge.phpdt.internal.compiler.ast.InstanceOfExpression;
33 import net.sourceforge.phpdt.internal.compiler.ast.MethodDeclaration;
34 import net.sourceforge.phpdt.internal.compiler.ast.OR_OR_Expression;
35 import net.sourceforge.phpdt.internal.compiler.ast.OperatorIds;
36 import net.sourceforge.phpdt.internal.compiler.ast.ReturnStatement;
37 import net.sourceforge.phpdt.internal.compiler.ast.SingleTypeReference;
38 import net.sourceforge.phpdt.internal.compiler.ast.Statement;
39 import net.sourceforge.phpdt.internal.compiler.ast.StringLiteral;
40 import net.sourceforge.phpdt.internal.compiler.ast.StringLiteralDQ;
41 import net.sourceforge.phpdt.internal.compiler.ast.StringLiteralSQ;
42 import net.sourceforge.phpdt.internal.compiler.ast.TypeDeclaration;
43 import net.sourceforge.phpdt.internal.compiler.ast.TypeReference;
44 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
45 import net.sourceforge.phpdt.internal.compiler.impl.ReferenceContext;
46 import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
47 import net.sourceforge.phpdt.internal.compiler.lookup.TypeConstants;
48 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
49 import net.sourceforge.phpdt.internal.compiler.problem.ProblemSeverities;
50 import net.sourceforge.phpdt.internal.compiler.util.Util;
51 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
52 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
53 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
54
55 import org.eclipse.core.resources.IFile;
56 import org.eclipse.core.resources.IProject;
57 import org.eclipse.core.resources.IResource;
58 import org.eclipse.core.runtime.IPath;
59
60 public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicInformation {
61         protected final static int StackIncrement = 255;
62
63         protected int stateStackTop;
64
65         // protected int[] stack = new int[StackIncrement];
66
67         public int firstToken; // handle for multiple parsing goals
68
69         public int lastAct; // handle for multiple parsing goals
70
71         // protected RecoveredElement currentElement;
72
73         public static boolean VERBOSE_RECOVERY = false;
74
75         protected boolean diet = false; // tells the scanner to jump over some
76
77         /**
78          * the PHP token scanner
79          */
80         public Scanner scanner;
81
82         int token;
83
84         protected int modifiers;
85
86         protected int modifiersSourceStart;
87
88         protected Parser(ProblemReporter problemReporter) {
89                 this.problemReporter = problemReporter;
90                 this.options = problemReporter.options;
91                 this.token = TokenNameEOF;
92                 this.initializeScanner();
93         }
94
95         public void setFileToParse(IFile fileToParse) {
96                 this.token = TokenNameEOF;
97                 this.initializeScanner();
98         }
99
100         /**
101          * ClassDeclaration Constructor.
102          *
103          * @param s
104          * @param sess
105          *          Description of Parameter
106          * @see
107          */
108         public Parser(IFile fileToParse) {
109                 // if (keywordMap == null) {
110                 // keywordMap = new HashMap();
111                 // for (int i = 0; i < PHP_KEYWORS.length; i++) {
112                 // keywordMap.put(PHP_KEYWORS[i], new Integer(PHP_KEYWORD_TOKEN[i]));
113                 // }
114                 // }
115                 // this.currentPHPString = 0;
116                 // PHPParserSuperclass.fileToParse = fileToParse;
117                 // this.phpList = null;
118                 this.includesList = null;
119                 // this.str = "";
120                 this.token = TokenNameEOF;
121                 // this.chIndx = 0;
122                 // this.rowCount = 1;
123                 // this.columnCount = 0;
124                 // this.phpEnd = false;
125                 // getNextToken();
126                 this.initializeScanner();
127         }
128
129         public void initializeScanner() {
130                 this.scanner = new Scanner(false /* comment */, false /* whitespace */, this.options
131                                 .getSeverity(CompilerOptions.NonExternalizedString) != ProblemSeverities.Ignore /* nls */, false, false,
132                                 this.options.taskTags/* taskTags */, this.options.taskPriorites/* taskPriorities */, true/* isTaskCaseSensitive */);
133         }
134
135         /**
136          * Create marker for the parse error
137          */
138         // private void setMarker(String message, int charStart, int charEnd, int
139         // errorLevel) {
140         // setMarker(fileToParse, message, charStart, charEnd, errorLevel);
141         // }
142         /**
143          * This method will throw the SyntaxError. It will add the good lines and
144          * columns to the Error
145          *
146          * @param error
147          *          the error message
148          * @throws SyntaxError
149          *           the error raised
150          */
151         private void throwSyntaxError(String error) {
152                 int problemStartPosition = scanner.getCurrentTokenStartPosition();
153                 int problemEndPosition = scanner.getCurrentTokenEndPosition() + 1;
154                 if (scanner.source.length <= problemEndPosition && problemEndPosition > 0) {
155                         problemEndPosition = scanner.source.length - 1;
156                         if (problemStartPosition > 0 && problemStartPosition >= problemEndPosition && problemEndPosition > 0) {
157                                 problemStartPosition = problemEndPosition - 1;
158                         }
159                 }
160                 throwSyntaxError(error, problemStartPosition, problemEndPosition);
161         }
162
163         /**
164          * This method will throw the SyntaxError. It will add the good lines and
165          * columns to the Error
166          *
167          * @param error
168          *          the error message
169          * @throws SyntaxError
170          *           the error raised
171          */
172         // private void throwSyntaxError(String error, int startRow) {
173         // throw new SyntaxError(startRow, 0, " ", error);
174         // }
175         private void throwSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
176                 if (referenceContext != null) {
177                         problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
178                                         compilationUnit.compilationResult);
179                 }
180                 throw new SyntaxError(1, 0, " ", error);
181         }
182
183         private void reportSyntaxError(String error) {
184                 int problemStartPosition = scanner.getCurrentTokenStartPosition();
185                 int problemEndPosition = scanner.getCurrentTokenEndPosition();
186                 reportSyntaxError(error, problemStartPosition, problemEndPosition + 1);
187         }
188
189         private void reportSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
190                 if (referenceContext != null) {
191                         problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
192                                         compilationUnit.compilationResult);
193                 }
194         }
195
196         // private void reportSyntaxWarning(String error, int problemStartPosition,
197         // int problemEndPosition) {
198         // if (referenceContext != null) {
199         // problemReporter.phpParsingWarning(new String[] { error },
200         // problemStartPosition, problemEndPosition, referenceContext,
201         // compilationUnit.compilationResult);
202         // }
203         // }
204
205         /**
206          * gets the next token from input
207          */
208         private void getNextToken() {
209                 try {
210                         token = scanner.getNextToken();
211                         if (Scanner.DEBUG) {
212                                 int currentEndPosition = scanner.getCurrentTokenEndPosition();
213                                 int currentStartPosition = scanner.getCurrentTokenStartPosition();
214                                 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
215                                 System.out.println(scanner.toStringAction(token));
216                         }
217                 } catch (InvalidInputException e) {
218                         token = TokenNameERROR;
219                         String detailedMessage = e.getMessage();
220
221                         if (detailedMessage == Scanner.UNTERMINATED_STRING) {
222                                 throwSyntaxError("Unterminated string.");
223                         } else if (detailedMessage == Scanner.UNTERMINATED_COMMENT) {
224                                 throwSyntaxError("Unterminated commment.");
225                         }
226                 }
227                 return;
228         }
229
230         public void init(String s) {
231                 // this.str = s;
232                 this.token = TokenNameEOF;
233                 this.includesList = new ArrayList();
234                 // this.chIndx = 0;
235                 // this.rowCount = 1;
236                 // this.columnCount = 0;
237                 // this.phpEnd = false;
238                 // this.phpMode = false;
239                 /* scanner initialization */
240                 scanner.setSource(s.toCharArray());
241                 scanner.setPHPMode(false);
242                 astPtr = 0;
243         }
244
245         protected void initialize(boolean phpMode) {
246                 initialize(phpMode, null);
247         }
248
249         protected void initialize(boolean phpMode, IdentifierIndexManager indexManager) {
250                 compilationUnit = null;
251                 referenceContext = null;
252                 this.includesList = new ArrayList();
253                 // this.indexManager = indexManager;
254                 // this.str = "";
255                 this.token = TokenNameEOF;
256                 // this.chIndx = 0;
257                 // this.rowCount = 1;
258                 // this.columnCount = 0;
259                 // this.phpEnd = false;
260                 // this.phpMode = phpMode;
261                 scanner.setPHPMode(phpMode);
262                 astPtr = 0;
263         }
264
265         /**
266          * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt;
267          * &lt;/body&gt;'
268          */
269         public void parse(String s) {
270                 parse(s, null);
271         }
272
273         /**
274          * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt;
275          * &lt;/body&gt;'
276          */
277         public void parse(String s, HashMap variables) {
278                 fMethodVariables = variables;
279                 fStackUnassigned = new ArrayList();
280                 init(s);
281                 parse();
282         }
283
284         /**
285          * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt;
286          * &lt;/body&gt;'
287          */
288         protected void parse() {
289                 if (scanner.compilationUnit != null) {
290                         IResource resource = scanner.compilationUnit.getResource();
291                         if (resource != null && resource instanceof IFile) {
292                                 // set the package name
293                                 consumePackageDeclarationName((IFile) resource);
294                         }
295                 }
296                 getNextToken();
297                 do {
298                         try {
299                                 if (token != TokenNameEOF && token != TokenNameERROR) {
300                                         statementList();
301                                 }
302                                 if (token != TokenNameEOF) {
303                                         if (token == TokenNameERROR) {
304                                                 throwSyntaxError("Scanner error (Found unknown token: " + scanner.toStringAction(token) + ")");
305                                         }
306                                         if (token == TokenNameRPAREN) {
307                                                 throwSyntaxError("Too many closing ')'; end-of-file not reached.");
308                                         }
309                                         if (token == TokenNameRBRACE) {
310                                                 throwSyntaxError("Too many closing '}'; end-of-file not reached.");
311                                         }
312                                         if (token == TokenNameRBRACKET) {
313                                                 throwSyntaxError("Too many closing ']'; end-of-file not reached.");
314                                         }
315                                         if (token == TokenNameLPAREN) {
316                                                 throwSyntaxError("Read character '('; end-of-file not reached.");
317                                         }
318                                         if (token == TokenNameLBRACE) {
319                                                 throwSyntaxError("Read character '{';  end-of-file not reached.");
320                                         }
321                                         if (token == TokenNameLBRACKET) {
322                                                 throwSyntaxError("Read character '[';  end-of-file not reached.");
323                                         }
324                                         throwSyntaxError("End-of-file not reached.");
325                                 }
326                                 break;
327                         } catch (SyntaxError syntaxError) {
328                                 // syntaxError.printStackTrace();
329                                 break;
330                         }
331                 } while (true);
332
333                 endParse(0);
334         }
335
336         /**
337          * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt;
338          * &lt;/body&gt;'
339          */
340         public void parseFunction(String s, HashMap variables) {
341                 init(s);
342                 scanner.phpMode = true;
343                 parseFunction(variables);
344         }
345
346         /**
347          * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt;
348          * &lt;/body&gt;'
349          */
350         protected void parseFunction(HashMap variables) {
351                 getNextToken();
352                 boolean hasModifiers = member_modifiers();
353                 if (token == TokenNamefunction) {
354                         if (!hasModifiers) {
355                                 checkAndSetModifiers(AccPublic);
356                         }
357                         this.fMethodVariables = variables;
358
359                         MethodDeclaration methodDecl = new MethodDeclaration(null);
360                         methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
361                         methodDecl.modifiers = this.modifiers;
362                         methodDecl.type = MethodDeclaration.METHOD_DEFINITION;
363                         try {
364                                 getNextToken();
365                                 functionDefinition(methodDecl);
366                         } catch (SyntaxError sytaxErr1) {
367                                 return;
368                         } finally {
369                                 int sourceEnd = methodDecl.sourceEnd;
370                                 if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
371                                         sourceEnd = methodDecl.declarationSourceStart + 1;
372                                 }
373                                 methodDecl.sourceEnd = sourceEnd;
374                                 methodDecl.declarationSourceEnd = sourceEnd;
375                         }
376                 }
377         }
378
379         protected CompilationUnitDeclaration endParse(int act) {
380
381                 this.lastAct = act;
382
383                 // if (currentElement != null) {
384                 // currentElement.topElement().updateParseTree();
385                 // if (VERBOSE_RECOVERY) {
386                 // System.out.print(Util.bind("parser.syntaxRecovery")); //$NON-NLS-1$
387                 // System.out.println("--------------------------"); //$NON-NLS-1$
388                 // System.out.println(compilationUnit);
389                 // System.out.println("----------------------------------"); //$NON-NLS-1$
390                 // }
391                 // } else {
392                 if (diet & VERBOSE_RECOVERY) {
393                         System.out.print(Util.bind("parser.regularParse")); //$NON-NLS-1$
394                         System.out.println("--------------------------"); //$NON-NLS-1$
395                         System.out.println(compilationUnit);
396                         System.out.println("----------------------------------"); //$NON-NLS-1$
397                 }
398                 // }
399                 if (scanner.recordLineSeparator) {
400                         compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
401                 }
402                 if (scanner.taskTags != null) {
403                         for (int i = 0; i < scanner.foundTaskCount; i++) {
404                                 problemReporter().task(new String(scanner.foundTaskTags[i]), new String(scanner.foundTaskMessages[i]),
405                                                 scanner.foundTaskPriorities[i] == null ? null : new String(scanner.foundTaskPriorities[i]),
406                                                 scanner.foundTaskPositions[i][0], scanner.foundTaskPositions[i][1]);
407                         }
408                 }
409                 compilationUnit.imports = new ImportReference[includesList.size()];
410                 for (int i = 0; i < includesList.size(); i++) {
411                         compilationUnit.imports[i] = (ImportReference) includesList.get(i);
412                 }
413                 return compilationUnit;
414         }
415
416         private Block statementList() {
417                 boolean branchStatement = false;
418                 Statement statement;
419                 int blockStart = scanner.getCurrentTokenStartPosition();
420                 ArrayList blockStatements = new ArrayList();
421                 do {
422                         try {
423                                 statement = statement();
424                                 blockStatements.add(statement);
425                                 if (branchStatement && statement != null) {
426                                         // reportSyntaxError("Unreachable code", statement.sourceStart,
427                                         // statement.sourceEnd);
428                                         problemReporter.unreachableCode(new String(scanner.getCurrentIdentifierSource()), statement.sourceStart,
429                                                         statement.sourceEnd, referenceContext, compilationUnit.compilationResult);
430                                 }
431                                 if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
432                                                 || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
433                                                 || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
434                                                 || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
435                                         return createBlock(blockStart, blockStatements);
436                                 }
437                                 branchStatement = checkUnreachableStatements(statement);
438                         } catch (SyntaxError sytaxErr1) {
439                                 // if an error occured,
440                                 // try to find keywords
441                                 // to parse the rest of the string
442                                 boolean tokenize = scanner.tokenizeStrings;
443                                 if (!tokenize) {
444                                         scanner.tokenizeStrings = true;
445                                 }
446                                 try {
447                                         while (token != TokenNameEOF) {
448                                                 if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
449                                                                 || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
450                                                                 || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
451                                                                 || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
452                                                         return createBlock(blockStart, blockStatements);
453                                                 }
454                                                 if (token == TokenNameif || token == TokenNameswitch || token == TokenNamefor || token == TokenNamewhile
455                                                                 || token == TokenNamedo || token == TokenNameforeach || token == TokenNamecontinue || token == TokenNamebreak
456                                                                 || token == TokenNamereturn || token == TokenNameexit || token == TokenNameecho || token == TokenNameglobal
457                                                                 || token == TokenNamestatic || token == TokenNameunset || token == TokenNamefunction || token == TokenNamedeclare
458                                                                 || token == TokenNametry || token == TokenNamecatch || token == TokenNamethrow || token == TokenNamefinal
459                                                                 || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
460                                                         break;
461                                                 }
462                                                 // System.out.println(scanner.toStringAction(token));
463                                                 getNextToken();
464                                                 // System.out.println(scanner.toStringAction(token));
465                                         }
466                                         if (token == TokenNameEOF) {
467                                                 throw sytaxErr1;
468                                         }
469                                 } finally {
470                                         scanner.tokenizeStrings = tokenize;
471                                 }
472                         }
473                 } while (true);
474         }
475
476         /**
477          * @param statement
478          * @return
479          */
480         private boolean checkUnreachableStatements(Statement statement) {
481                 if (statement instanceof ReturnStatement || statement instanceof ContinueStatement || statement instanceof BreakStatement) {
482                         return true;
483                 } else if (statement instanceof IfStatement && ((IfStatement) statement).checkUnreachable) {
484                         return true;
485                 }
486                 return false;
487         }
488
489         /**
490          * @param blockStart
491          * @param blockStatements
492          * @return
493          */
494         private Block createBlock(int blockStart, ArrayList blockStatements) {
495                 int blockEnd = scanner.getCurrentTokenEndPosition();
496                 Block b = Block.EmptyWith(blockStart, blockEnd);
497                 b.statements = new Statement[blockStatements.size()];
498                 blockStatements.toArray(b.statements);
499                 return b;
500         }
501
502         private void functionBody(MethodDeclaration methodDecl) {
503                 // '{' [statement-list] '}'
504                 if (token == TokenNameLBRACE) {
505                         getNextToken();
506                 } else {
507                         methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
508                         throwSyntaxError("'{' expected in compound-statement.");
509                 }
510                 if (token != TokenNameRBRACE) {
511                         statementList();
512                 }
513                 if (token == TokenNameRBRACE) {
514                         methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
515                         getNextToken();
516                 } else {
517                         methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
518                         throwSyntaxError("'}' expected in compound-statement.");
519                 }
520         }
521
522         private Statement statement() {
523                 Statement statement = null;
524                 Expression expression;
525                 int sourceStart = scanner.getCurrentTokenStartPosition();
526                 int sourceEnd;
527                 if (token == TokenNameif) {
528                         // T_IF '(' expr ')' statement elseif_list else_single
529                         // T_IF '(' expr ')' ':' inner_statement_list new_elseif_list
530                         // new_else_single T_ENDIF ';'
531                         getNextToken();
532                         if (token == TokenNameLPAREN) {
533                                 getNextToken();
534                         } else {
535                                 throwSyntaxError("'(' expected after 'if' keyword.");
536                         }
537                         expression = expr();
538                         if (token == TokenNameRPAREN) {
539                                 getNextToken();
540                         } else {
541                                 throwSyntaxError("')' expected after 'if' condition.");
542                         }
543                         // create basic IfStatement
544                         IfStatement ifStatement = new IfStatement(expression, null, null, sourceStart, -1);
545                         if (token == TokenNameCOLON) {
546                                 getNextToken();
547                                 ifStatementColon(ifStatement);
548                         } else {
549                                 ifStatement(ifStatement);
550                         }
551                         return ifStatement;
552                 } else if (token == TokenNameswitch) {
553                         getNextToken();
554                         if (token == TokenNameLPAREN) {
555                                 getNextToken();
556                         } else {
557                                 throwSyntaxError("'(' expected after 'switch' keyword.");
558                         }
559                         expr();
560                         if (token == TokenNameRPAREN) {
561                                 getNextToken();
562                         } else {
563                                 throwSyntaxError("')' expected after 'switch' condition.");
564                         }
565                         switchStatement();
566                         return statement;
567                 } else if (token == TokenNamefor) {
568                         getNextToken();
569                         if (token == TokenNameLPAREN) {
570                                 getNextToken();
571                         } else {
572                                 throwSyntaxError("'(' expected after 'for' keyword.");
573                         }
574                         if (token == TokenNameSEMICOLON) {
575                                 getNextToken();
576                         } else {
577                                 expressionList();
578                                 if (token == TokenNameSEMICOLON) {
579                                         getNextToken();
580                                 } else {
581                                         throwSyntaxError("';' expected after 'for'.");
582                                 }
583                         }
584                         if (token == TokenNameSEMICOLON) {
585                                 getNextToken();
586                         } else {
587                                 expressionList();
588                                 if (token == TokenNameSEMICOLON) {
589                                         getNextToken();
590                                 } else {
591                                         throwSyntaxError("';' expected after 'for'.");
592                                 }
593                         }
594                         if (token == TokenNameRPAREN) {
595                                 getNextToken();
596                         } else {
597                                 expressionList();
598                                 if (token == TokenNameRPAREN) {
599                                         getNextToken();
600                                 } else {
601                                         throwSyntaxError("')' expected after 'for'.");
602                                 }
603                         }
604                         forStatement();
605                         return statement;
606                 } else if (token == TokenNamewhile) {
607                         getNextToken();
608                         if (token == TokenNameLPAREN) {
609                                 getNextToken();
610                         } else {
611                                 throwSyntaxError("'(' expected after 'while' keyword.");
612                         }
613                         expr();
614                         if (token == TokenNameRPAREN) {
615                                 getNextToken();
616                         } else {
617                                 throwSyntaxError("')' expected after 'while' condition.");
618                         }
619                         whileStatement();
620                         return statement;
621                 } else if (token == TokenNamedo) {
622                         getNextToken();
623                         if (token == TokenNameLBRACE) {
624                                 getNextToken();
625                                 if (token != TokenNameRBRACE) {
626                                         statementList();
627                                 }
628                                 if (token == TokenNameRBRACE) {
629                                         getNextToken();
630                                 } else {
631                                         throwSyntaxError("'}' expected after 'do' keyword.");
632                                 }
633                         } else {
634                                 statement();
635                         }
636                         if (token == TokenNamewhile) {
637                                 getNextToken();
638                                 if (token == TokenNameLPAREN) {
639                                         getNextToken();
640                                 } else {
641                                         throwSyntaxError("'(' expected after 'while' keyword.");
642                                 }
643                                 expr();
644                                 if (token == TokenNameRPAREN) {
645                                         getNextToken();
646                                 } else {
647                                         throwSyntaxError("')' expected after 'while' condition.");
648                                 }
649                         } else {
650                                 throwSyntaxError("'while' expected after 'do' keyword.");
651                         }
652                         if (token == TokenNameSEMICOLON) {
653                                 getNextToken();
654                         } else {
655                                 if (token != TokenNameINLINE_HTML) {
656                                         throwSyntaxError("';' expected after do-while statement.");
657                                 }
658                                 getNextToken();
659                         }
660                         return statement;
661                 } else if (token == TokenNameforeach) {
662                         getNextToken();
663                         if (token == TokenNameLPAREN) {
664                                 getNextToken();
665                         } else {
666                                 throwSyntaxError("'(' expected after 'foreach' keyword.");
667                         }
668                         expr();
669                         if (token == TokenNameas) {
670                                 getNextToken();
671                         } else {
672                                 throwSyntaxError("'as' expected after 'foreach' exxpression.");
673                         }
674                         // variable();
675                         foreach_variable();
676                         foreach_optional_arg();
677                         if (token == TokenNameEQUAL_GREATER) {
678                                 getNextToken();
679                                 variable(false, false);
680                         }
681                         if (token == TokenNameRPAREN) {
682                                 getNextToken();
683                         } else {
684                                 throwSyntaxError("')' expected after 'foreach' expression.");
685                         }
686                         foreachStatement();
687                         return statement;
688                 } else if (token == TokenNamebreak) {
689                         expression = null;
690                         getNextToken();
691                         if (token != TokenNameSEMICOLON) {
692                                 expression = expr();
693                         }
694                         if (token == TokenNameSEMICOLON) {
695                                 sourceEnd = scanner.getCurrentTokenEndPosition();
696                                 getNextToken();
697                         } else {
698                                 if (token != TokenNameINLINE_HTML) {
699                                         throwSyntaxError("';' expected after 'break'.");
700                                 }
701                                 sourceEnd = scanner.getCurrentTokenEndPosition();
702                                 getNextToken();
703                         }
704                         return new BreakStatement(null, sourceStart, sourceEnd);
705                 } else if (token == TokenNamecontinue) {
706                         expression = null;
707                         getNextToken();
708                         if (token != TokenNameSEMICOLON) {
709                                 expression = expr();
710                         }
711                         if (token == TokenNameSEMICOLON) {
712                                 sourceEnd = scanner.getCurrentTokenEndPosition();
713                                 getNextToken();
714                         } else {
715                                 if (token != TokenNameINLINE_HTML) {
716                                         throwSyntaxError("';' expected after 'continue'.");
717                                 }
718                                 sourceEnd = scanner.getCurrentTokenEndPosition();
719                                 getNextToken();
720                         }
721                         return new ContinueStatement(null, sourceStart, sourceEnd);
722                 } else if (token == TokenNamereturn) {
723                         expression = null;
724                         getNextToken();
725                         if (token != TokenNameSEMICOLON) {
726                                 expression = expr();
727                         }
728                         if (token == TokenNameSEMICOLON) {
729                                 sourceEnd = scanner.getCurrentTokenEndPosition();
730                                 getNextToken();
731                         } else {
732                                 if (token != TokenNameINLINE_HTML) {
733                                         throwSyntaxError("';' expected after 'return'.");
734                                 }
735                                 sourceEnd = scanner.getCurrentTokenEndPosition();
736                                 getNextToken();
737                         }
738                         return new ReturnStatement(expression, sourceStart, sourceEnd);
739                 } else if (token == TokenNameecho) {
740                         getNextToken();
741                         expressionList();
742                         if (token == TokenNameSEMICOLON) {
743                                 getNextToken();
744                         } else {
745                                 if (token != TokenNameINLINE_HTML) {
746                                         throwSyntaxError("';' expected after 'echo' statement.");
747                                 }
748                                 getNextToken();
749                         }
750                         return statement;
751                 } else if (token == TokenNameINLINE_HTML) {
752                         if (scanner.phpExpressionTag) {
753                                 // start of <?= ... ?> block
754                                 getNextToken();
755                                 expr();
756                                 if (token == TokenNameSEMICOLON) {
757                                         getNextToken();
758                                 }
759                                 if (token != TokenNameINLINE_HTML) {
760                                         throwSyntaxError("Missing '?>' for open PHP expression block ('<?=').");
761                                 }
762                         } else {
763                                 getNextToken();
764                         }
765                         return statement;
766                         // } else if (token == TokenNameprint) {
767                         // getNextToken();
768                         // expression();
769                         // if (token == TokenNameSEMICOLON) {
770                         // getNextToken();
771                         // } else {
772                         // if (token != TokenNameStopPHP) {
773                         // throwSyntaxError("';' expected after 'print' statement.");
774                         // }
775                         // getNextToken();
776                         // }
777                         // return;
778                 } else if (token == TokenNameglobal) {
779                         getNextToken();
780                         global_var_list();
781                         if (token == TokenNameSEMICOLON) {
782                                 getNextToken();
783                         } else {
784                                 if (token != TokenNameINLINE_HTML) {
785                                         throwSyntaxError("';' expected after 'global' statement.");
786                                 }
787                                 getNextToken();
788                         }
789                         return statement;
790                 } else if (token == TokenNamestatic) {
791                         getNextToken();
792                         static_var_list();
793                         if (token == TokenNameSEMICOLON) {
794                                 getNextToken();
795                         } else {
796                                 if (token != TokenNameINLINE_HTML) {
797                                         throwSyntaxError("';' expected after 'static' statement.");
798                                 }
799                                 getNextToken();
800                         }
801                         return statement;
802                 } else if (token == TokenNameunset) {
803                         getNextToken();
804                         if (token == TokenNameLPAREN) {
805                                 getNextToken();
806                         } else {
807                                 throwSyntaxError("'(' expected after 'unset' statement.");
808                         }
809                         unset_variables();
810                         if (token == TokenNameRPAREN) {
811                                 getNextToken();
812                         } else {
813                                 throwSyntaxError("')' expected after 'unset' statement.");
814                         }
815                         if (token == TokenNameSEMICOLON) {
816                                 getNextToken();
817                         } else {
818                                 if (token != TokenNameINLINE_HTML) {
819                                         throwSyntaxError("';' expected after 'unset' statement.");
820                                 }
821                                 getNextToken();
822                         }
823                         return statement;
824                 } else if (token == TokenNamefunction) {
825                         MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
826                         methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
827                         methodDecl.modifiers = AccDefault;
828                         methodDecl.type = MethodDeclaration.FUNCTION_DEFINITION;
829                         try {
830                                 getNextToken();
831                                 functionDefinition(methodDecl);
832                         } finally {
833                                 sourceEnd = methodDecl.sourceEnd;
834                                 if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
835                                         sourceEnd = methodDecl.declarationSourceStart + 1;
836                                 }
837                                 methodDecl.declarationSourceEnd = sourceEnd;
838                                 methodDecl.sourceEnd = sourceEnd;
839                         }
840                         return statement;
841                 } else if (token == TokenNamedeclare) {
842                         // T_DECLARE '(' declare_list ')' declare_statement
843                         getNextToken();
844                         if (token != TokenNameLPAREN) {
845                                 throwSyntaxError("'(' expected in 'declare' statement.");
846                         }
847                         getNextToken();
848                         declare_list();
849                         if (token != TokenNameRPAREN) {
850                                 throwSyntaxError("')' expected in 'declare' statement.");
851                         }
852                         getNextToken();
853                         declare_statement();
854                         return statement;
855                 } else if (token == TokenNametry) {
856                         getNextToken();
857                         if (token != TokenNameLBRACE) {
858                                 throwSyntaxError("'{' expected in 'try' statement.");
859                         }
860                         getNextToken();
861                         statementList();
862                         if (token != TokenNameRBRACE) {
863                                 throwSyntaxError("'}' expected in 'try' statement.");
864                         }
865                         getNextToken();
866                         return statement;
867                 } else if (token == TokenNamecatch) {
868                         getNextToken();
869                         if (token != TokenNameLPAREN) {
870                                 throwSyntaxError("'(' expected in 'catch' statement.");
871                         }
872                         getNextToken();
873                         fully_qualified_class_name();
874                         if (token != TokenNameVariable) {
875                                 throwSyntaxError("Variable expected in 'catch' statement.");
876                         }
877                         addVariableSet();
878                         getNextToken();
879                         if (token != TokenNameRPAREN) {
880                                 throwSyntaxError("')' expected in 'catch' statement.");
881                         }
882                         getNextToken();
883                         if (token != TokenNameLBRACE) {
884                                 throwSyntaxError("'{' expected in 'catch' statement.");
885                         }
886                         getNextToken();
887                         if (token != TokenNameRBRACE) {
888                                 statementList();
889                                 if (token != TokenNameRBRACE) {
890                                         throwSyntaxError("'}' expected in 'catch' statement.");
891                                 }
892                         }
893                         getNextToken();
894                         additional_catches();
895                         return statement;
896                 } else if (token == TokenNamethrow) {
897                         getNextToken();
898                         expr();
899                         if (token == TokenNameSEMICOLON) {
900                                 getNextToken();
901                         } else {
902                                 throwSyntaxError("';' expected after 'throw' exxpression.");
903                         }
904                         return statement;
905                 } else if (token == TokenNamefinal || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
906                         try {
907                                 TypeDeclaration typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
908                                 typeDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
909                                 typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
910                                 typeDecl.name = new char[] { ' ' };
911                                 // default super class
912                                 typeDecl.superclass = new SingleTypeReference(TypeConstants.OBJECT, 0);
913                                 compilationUnit.types.add(typeDecl);
914                                 pushOnAstStack(typeDecl);
915                                 unticked_class_declaration_statement(typeDecl);
916                         } finally {
917                                 // reduce stack:
918                                 astPtr--;
919                                 astLengthPtr--;
920                         }
921                         return statement;
922                         // } else {
923                         // throwSyntaxError("Unexpected keyword '" + keyword + "'");
924                 } else if (token == TokenNameLBRACE) {
925                         getNextToken();
926                         if (token != TokenNameRBRACE) {
927                                 statement = statementList();
928                         }
929                         if (token == TokenNameRBRACE) {
930                                 getNextToken();
931                                 return statement;
932                         } else {
933                                 throwSyntaxError("'}' expected.");
934                         }
935                 } else {
936                         if (token != TokenNameSEMICOLON) {
937                                 expr();
938                         }
939                         if (token == TokenNameSEMICOLON) {
940                                 getNextToken();
941                                 return statement;
942                         } else {
943                                 if (token == TokenNameRBRACE) {
944                                         reportSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
945                                 } else {
946                                         if (token != TokenNameINLINE_HTML && token != TokenNameEOF) {
947                                                 throwSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
948                                         }
949                                         getNextToken();
950                                 }
951                         }
952                 }
953                 // may be null
954                 return statement;
955         }
956
957         private void declare_statement() {
958                 // statement
959                 // | ':' inner_statement_list T_ENDDECLARE ';'
960                 // ;
961                 if (token == TokenNameCOLON) {
962                         getNextToken();
963                         // TODO: implement inner_statement_list();
964                         statementList();
965                         if (token != TokenNameenddeclare) {
966                                 throwSyntaxError("'enddeclare' expected in 'declare' statement.");
967                         }
968                         getNextToken();
969                         if (token != TokenNameSEMICOLON) {
970                                 throwSyntaxError("';' expected after 'enddeclare' keyword.");
971                         }
972                         getNextToken();
973                 } else {
974                         statement();
975                 }
976         }
977
978         private void declare_list() {
979                 // T_STRING '=' static_scalar
980                 // | declare_list ',' T_STRING '=' static_scalar
981                 while (true) {
982                         if (token != TokenNameIdentifier) {
983                                 throwSyntaxError("Identifier expected in 'declare' list.");
984                         }
985                         getNextToken();
986                         if (token != TokenNameEQUAL) {
987                                 throwSyntaxError("'=' expected in 'declare' list.");
988                         }
989                         getNextToken();
990                         static_scalar();
991                         if (token != TokenNameCOMMA) {
992                                 break;
993                         }
994                         getNextToken();
995                 }
996         }
997
998         private void additional_catches() {
999                 while (token == TokenNamecatch) {
1000                         getNextToken();
1001                         if (token != TokenNameLPAREN) {
1002                                 throwSyntaxError("'(' expected in 'catch' statement.");
1003                         }
1004                         getNextToken();
1005                         fully_qualified_class_name();
1006                         if (token != TokenNameVariable) {
1007                                 throwSyntaxError("Variable expected in 'catch' statement.");
1008                         }
1009                         addVariableSet();
1010                         getNextToken();
1011                         if (token != TokenNameRPAREN) {
1012                                 throwSyntaxError("')' expected in 'catch' statement.");
1013                         }
1014                         getNextToken();
1015                         if (token != TokenNameLBRACE) {
1016                                 throwSyntaxError("'{' expected in 'catch' statement.");
1017                         }
1018                         getNextToken();
1019                         if (token != TokenNameRBRACE) {
1020                                 statementList();
1021                         }
1022                         if (token != TokenNameRBRACE) {
1023                                 throwSyntaxError("'}' expected in 'catch' statement.");
1024                         }
1025                         getNextToken();
1026                 }
1027         }
1028
1029         private void foreach_variable() {
1030                 // w_variable
1031                 // | '&' w_variable
1032                 if (token == TokenNameAND) {
1033                         getNextToken();
1034                 }
1035                 w_variable(true);
1036         }
1037
1038         private void foreach_optional_arg() {
1039                 // /* empty */
1040                 // | T_DOUBLE_ARROW foreach_variable
1041                 if (token == TokenNameEQUAL_GREATER) {
1042                         getNextToken();
1043                         foreach_variable();
1044                 }
1045         }
1046
1047         private void global_var_list() {
1048                 // global_var_list:
1049                 // global_var_list ',' global_var
1050                 // | global_var
1051                 HashSet set = peekVariableSet();
1052                 while (true) {
1053                         global_var(set);
1054                         if (token != TokenNameCOMMA) {
1055                                 break;
1056                         }
1057                         getNextToken();
1058                 }
1059         }
1060
1061         private void global_var(HashSet set) {
1062                 // global_var:
1063                 // T_VARIABLE
1064                 // | '$' r_variable
1065                 // | '$' '{' expr '}'
1066                 if (token == TokenNameVariable) {
1067                         if (fMethodVariables != null) {
1068                                 VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_GLOBAL_VAR);
1069                                 fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1070                         }
1071                         addVariableSet(set);
1072                         getNextToken();
1073                 } else if (token == TokenNameDOLLAR) {
1074                         getNextToken();
1075                         if (token == TokenNameLBRACE) {
1076                                 getNextToken();
1077                                 expr();
1078                                 if (token != TokenNameRBRACE) {
1079                                         throwSyntaxError("'}' expected in global variable.");
1080                                 }
1081                                 getNextToken();
1082                         } else {
1083                                 r_variable();
1084                         }
1085                 }
1086         }
1087
1088         private void static_var_list() {
1089                 // static_var_list:
1090                 // static_var_list ',' T_VARIABLE
1091                 // | static_var_list ',' T_VARIABLE '=' static_scalar
1092                 // | T_VARIABLE
1093                 // | T_VARIABLE '=' static_scalar,
1094                 HashSet set = peekVariableSet();
1095                 while (true) {
1096                         if (token == TokenNameVariable) {
1097                                 if (fMethodVariables != null) {
1098                                         VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_STATIC_VAR);
1099                                         fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1100                                 }
1101                                 addVariableSet(set);
1102                                 getNextToken();
1103                                 if (token == TokenNameEQUAL) {
1104                                         getNextToken();
1105                                         static_scalar();
1106                                 }
1107                                 if (token != TokenNameCOMMA) {
1108                                         break;
1109                                 }
1110                                 getNextToken();
1111                         } else {
1112                                 break;
1113                         }
1114                 }
1115         }
1116
1117         private void unset_variables() {
1118                 // unset_variables:
1119                 // unset_variable
1120                 // | unset_variables ',' unset_variable
1121                 // unset_variable:
1122                 // variable
1123                 while (true) {
1124                         variable(false, false);
1125                         if (token != TokenNameCOMMA) {
1126                                 break;
1127                         }
1128                         getNextToken();
1129                 }
1130         }
1131
1132         private final void initializeModifiers() {
1133                 this.modifiers = 0;
1134                 this.modifiersSourceStart = -1;
1135         }
1136
1137         private final void checkAndSetModifiers(int flag) {
1138                 this.modifiers |= flag;
1139                 if (this.modifiersSourceStart < 0)
1140                         this.modifiersSourceStart = this.scanner.startPosition;
1141         }
1142
1143         private void unticked_class_declaration_statement(TypeDeclaration typeDecl) {
1144                 initializeModifiers();
1145                 if (token == TokenNameinterface) {
1146                         // interface_entry T_STRING
1147                         // interface_extends_list
1148                         // '{' class_statement_list '}'
1149                         checkAndSetModifiers(AccInterface);
1150                         getNextToken();
1151                         typeDecl.modifiers = this.modifiers;
1152                         typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1153                         typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1154                         if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1155                                 typeDecl.name = scanner.getCurrentIdentifierSource();
1156                                 if (token > TokenNameKEYWORD) {
1157                                         problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1158                                                         scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1159                                         // throwSyntaxError("Don't use a keyword for interface declaration ["
1160                                         // + scanner.toStringAction(token) + "].",
1161                                         // typeDecl.sourceStart, typeDecl.sourceEnd);
1162                                 }
1163                                 getNextToken();
1164                                 interface_extends_list(typeDecl);
1165                         } else {
1166                                 typeDecl.name = new char[] { ' ' };
1167                                 throwSyntaxError("Interface name expected after keyword 'interface'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1168                                 return;
1169                         }
1170                 } else {
1171                         // class_entry_type T_STRING extends_from
1172                         // implements_list
1173                         // '{' class_statement_list'}'
1174                         class_entry_type();
1175                         typeDecl.modifiers = this.modifiers;
1176                         typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1177                         typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1178                         // identifier
1179                         // identifier 'extends' identifier
1180                         if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1181                                 typeDecl.name = scanner.getCurrentIdentifierSource();
1182                                 if (token > TokenNameKEYWORD) {
1183                                         problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1184                                                         scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1185                                         // throwSyntaxError("Don't use a keyword for class declaration [" +
1186                                         // scanner.toStringAction(token) + "].",
1187                                         // typeDecl.sourceStart, typeDecl.sourceEnd);
1188                                 }
1189                                 getNextToken();
1190                                 // extends_from:
1191                                 // /* empty */
1192                                 // | T_EXTENDS fully_qualified_class_name
1193                                 if (token == TokenNameextends) {
1194                                         interface_extends_list(typeDecl);
1195                                         // getNextToken();
1196                                         // if (token != TokenNameIdentifier) {
1197                                         // throwSyntaxError("Class name expected after keyword
1198                                         // 'extends'.",
1199                                         // scanner.getCurrentTokenStartPosition(), scanner
1200                                         // .getCurrentTokenEndPosition());
1201                                         // }
1202                                 }
1203                                 implements_list(typeDecl);
1204                         } else {
1205                                 typeDecl.name = new char[] { ' ' };
1206                                 throwSyntaxError("Class name expected after keyword 'class'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1207                                 return;
1208                         }
1209                 }
1210                 // '{' class_statement_list '}'
1211                 if (token == TokenNameLBRACE) {
1212                         getNextToken();
1213                         if (token != TokenNameRBRACE) {
1214                                 ArrayList list = new ArrayList();
1215                                 class_statement_list(list);
1216                                 typeDecl.fields = new FieldDeclaration[list.size()];
1217                                 for (int i = 0; i < list.size(); i++) {
1218                                         typeDecl.fields[i] = (FieldDeclaration) list.get(i);
1219                                 }
1220                         }
1221                         if (token == TokenNameRBRACE) {
1222                                 typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1223                                 getNextToken();
1224                         } else {
1225                                 throwSyntaxError("'}' expected at end of class body.");
1226                         }
1227                 } else {
1228                         throwSyntaxError("'{' expected at start of class body.");
1229                 }
1230         }
1231
1232         private void class_entry_type() {
1233                 // T_CLASS
1234                 // | T_ABSTRACT T_CLASS
1235                 // | T_FINAL T_CLASS
1236                 if (token == TokenNameclass) {
1237                         getNextToken();
1238                 } else if (token == TokenNameabstract) {
1239                         checkAndSetModifiers(AccAbstract);
1240                         getNextToken();
1241                         if (token != TokenNameclass) {
1242                                 throwSyntaxError("Keyword 'class' expected after keyword 'abstract'.");
1243                         }
1244                         getNextToken();
1245                 } else if (token == TokenNamefinal) {
1246                         checkAndSetModifiers(AccFinal);
1247                         getNextToken();
1248                         if (token != TokenNameclass) {
1249                                 throwSyntaxError("Keyword 'class' expected after keyword 'final'.");
1250                         }
1251                         getNextToken();
1252                 } else {
1253                         throwSyntaxError("Keyword 'class' 'final' or 'abstract' expected");
1254                 }
1255         }
1256
1257         // private void class_extends(TypeDeclaration typeDecl) {
1258         // // /* empty */
1259         // // | T_EXTENDS interface_list
1260         // if (token == TokenNameextends) {
1261         // getNextToken();
1262         //
1263         // if (token == TokenNameIdentifier) {
1264         // getNextToken();
1265         // } else {
1266         // throwSyntaxError("Class name expected after keyword 'extends'.");
1267         // }
1268         // }
1269         // }
1270
1271         private void interface_extends_list(TypeDeclaration typeDecl) {
1272                 // /* empty */
1273                 // | T_EXTENDS interface_list
1274                 if (token == TokenNameextends) {
1275                         getNextToken();
1276                         interface_list();
1277                 }
1278         }
1279
1280         private void implements_list(TypeDeclaration typeDecl) {
1281                 // /* empty */
1282                 // | T_IMPLEMENTS interface_list
1283                 if (token == TokenNameimplements) {
1284                         getNextToken();
1285                         interface_list();
1286                 }
1287         }
1288
1289         private void interface_list() {
1290                 // interface_list:
1291                 // fully_qualified_class_name
1292                 // | interface_list ',' fully_qualified_class_name
1293                 do {
1294                         if (token == TokenNameIdentifier) {
1295                                 getNextToken();
1296                         } else {
1297                                 throwSyntaxError("Interface name expected after keyword 'implements'.");
1298                         }
1299                         if (token != TokenNameCOMMA) {
1300                                 return;
1301                         }
1302                         getNextToken();
1303                 } while (true);
1304         }
1305
1306         // private void classBody(TypeDeclaration typeDecl) {
1307         // //'{' [class-element-list] '}'
1308         // if (token == TokenNameLBRACE) {
1309         // getNextToken();
1310         // if (token != TokenNameRBRACE) {
1311         // class_statement_list();
1312         // }
1313         // if (token == TokenNameRBRACE) {
1314         // typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1315         // getNextToken();
1316         // } else {
1317         // throwSyntaxError("'}' expected at end of class body.");
1318         // }
1319         // } else {
1320         // throwSyntaxError("'{' expected at start of class body.");
1321         // }
1322         // }
1323         private void class_statement_list(ArrayList list) {
1324                 do {
1325                         try {
1326                                 class_statement(list);
1327                                 if (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1328                                                 || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1329                                                 || token == TokenNameconst) {
1330                                         continue;
1331                                 }
1332                                 if (token == TokenNameRBRACE) {
1333                                         break;
1334                                 }
1335                                 throwSyntaxError("'}' at end of class statement.");
1336                         } catch (SyntaxError sytaxErr1) {
1337                                 boolean tokenize = scanner.tokenizeStrings;
1338                                 if (!tokenize) {
1339                                         scanner.tokenizeStrings = true;
1340                                 }
1341                                 try {
1342                                         // if an error occured,
1343                                         // try to find keywords
1344                                         // to parse the rest of the string
1345                                         while (token != TokenNameEOF) {
1346                                                 if (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1347                                                                 || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1348                                                                 || token == TokenNameconst) {
1349                                                         break;
1350                                                 }
1351                                                 // System.out.println(scanner.toStringAction(token));
1352                                                 getNextToken();
1353                                         }
1354                                         if (token == TokenNameEOF) {
1355                                                 throw sytaxErr1;
1356                                         }
1357                                 } finally {
1358                                         scanner.tokenizeStrings = tokenize;
1359                                 }
1360                         }
1361                 } while (true);
1362         }
1363
1364         private void class_statement(ArrayList list) {
1365                 // class_statement:
1366                 // variable_modifiers class_variable_declaration ';'
1367                 // | class_constant_declaration ';'
1368                 // | method_modifiers T_FUNCTION is_reference T_STRING
1369                 // '(' parameter_list ')' method_body
1370                 initializeModifiers();
1371                 int declarationSourceStart = scanner.getCurrentTokenStartPosition();
1372
1373                 if (token == TokenNamevar) {
1374                         checkAndSetModifiers(AccPublic);
1375                         problemReporter.phpVarDeprecatedWarning(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(),
1376                                         referenceContext, compilationUnit.compilationResult);
1377                         getNextToken();
1378                         class_variable_declaration(declarationSourceStart, list);
1379                 } else if (token == TokenNameconst) {
1380                         checkAndSetModifiers(AccFinal | AccPublic);
1381                         class_constant_declaration(declarationSourceStart, list);
1382                         if (token != TokenNameSEMICOLON) {
1383                                 throwSyntaxError("';' expected after class const declaration.");
1384                         }
1385                         getNextToken();
1386                 } else {
1387                         boolean hasModifiers = member_modifiers();
1388                         if (token == TokenNamefunction) {
1389                                 if (!hasModifiers) {
1390                                         checkAndSetModifiers(AccPublic);
1391                                 }
1392                                 MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
1393                                 methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
1394                                 methodDecl.modifiers = this.modifiers;
1395                                 methodDecl.type = MethodDeclaration.METHOD_DEFINITION;
1396                                 try {
1397                                         getNextToken();
1398                                         functionDefinition(methodDecl);
1399                                 } finally {
1400                                         int sourceEnd = methodDecl.sourceEnd;
1401                                         if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
1402                                                 sourceEnd = methodDecl.declarationSourceStart + 1;
1403                                         }
1404                                         methodDecl.declarationSourceEnd = sourceEnd;
1405                                         methodDecl.sourceEnd = sourceEnd;
1406                                 }
1407                         } else {
1408                                 if (!hasModifiers) {
1409                                         throwSyntaxError("'public' 'private' or 'protected' modifier expected for field declarations.");
1410                                 }
1411                                 class_variable_declaration(declarationSourceStart, list);
1412                         }
1413                 }
1414         }
1415
1416         private void class_constant_declaration(int declarationSourceStart, ArrayList list) {
1417                 // class_constant_declaration ',' T_STRING '=' static_scalar
1418                 // | T_CONST T_STRING '=' static_scalar
1419                 if (token != TokenNameconst) {
1420                         throwSyntaxError("'const' keyword expected in class declaration.");
1421                 } else {
1422                         getNextToken();
1423                 }
1424                 while (true) {
1425                         if (token != TokenNameIdentifier) {
1426                                 throwSyntaxError("Identifier expected in class const declaration.");
1427                         }
1428                         FieldDeclaration fieldDeclaration = new FieldDeclaration(scanner.getCurrentIdentifierSource(), scanner
1429                                         .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
1430                         fieldDeclaration.modifiers = this.modifiers;
1431                         fieldDeclaration.declarationSourceStart = declarationSourceStart;
1432                         fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1433                         fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1434                         // fieldDeclaration.type
1435                         list.add(fieldDeclaration);
1436                         getNextToken();
1437                         if (token != TokenNameEQUAL) {
1438                                 throwSyntaxError("'=' expected in class const declaration.");
1439                         }
1440                         getNextToken();
1441                         static_scalar();
1442                         if (token != TokenNameCOMMA) {
1443                                 break; // while(true)-loop
1444                         }
1445                         getNextToken();
1446                 }
1447         }
1448
1449         // private void variable_modifiers() {
1450         // // variable_modifiers:
1451         // // non_empty_member_modifiers
1452         // //| T_VAR
1453         // initializeModifiers();
1454         // if (token == TokenNamevar) {
1455         // checkAndSetModifiers(AccPublic);
1456         // reportSyntaxError(
1457         // "Keyword 'var' is deprecated. Please use 'public' 'private' or
1458         // 'protected'
1459         // modifier for field declarations.",
1460         // scanner.getCurrentTokenStartPosition(), scanner
1461         // .getCurrentTokenEndPosition());
1462         // getNextToken();
1463         // } else {
1464         // if (!member_modifiers()) {
1465         // throwSyntaxError("'public' 'private' or 'protected' modifier expected for
1466         // field declarations.");
1467         // }
1468         // }
1469         // }
1470         // private void method_modifiers() {
1471         // //method_modifiers:
1472         // // /* empty */
1473         // //| non_empty_member_modifiers
1474         // initializeModifiers();
1475         // if (!member_modifiers()) {
1476         // checkAndSetModifiers(AccPublic);
1477         // }
1478         // }
1479         private boolean member_modifiers() {
1480                 // T_PUBLIC
1481                 // | T_PROTECTED
1482                 // | T_PRIVATE
1483                 // | T_STATIC
1484                 // | T_ABSTRACT
1485                 // | T_FINAL
1486                 boolean foundToken = false;
1487                 while (true) {
1488                         if (token == TokenNamepublic) {
1489                                 checkAndSetModifiers(AccPublic);
1490                                 getNextToken();
1491                                 foundToken = true;
1492                         } else if (token == TokenNameprotected) {
1493                                 checkAndSetModifiers(AccProtected);
1494                                 getNextToken();
1495                                 foundToken = true;
1496                         } else if (token == TokenNameprivate) {
1497                                 checkAndSetModifiers(AccPrivate);
1498                                 getNextToken();
1499                                 foundToken = true;
1500                         } else if (token == TokenNamestatic) {
1501                                 checkAndSetModifiers(AccStatic);
1502                                 getNextToken();
1503                                 foundToken = true;
1504                         } else if (token == TokenNameabstract) {
1505                                 checkAndSetModifiers(AccAbstract);
1506                                 getNextToken();
1507                                 foundToken = true;
1508                         } else if (token == TokenNamefinal) {
1509                                 checkAndSetModifiers(AccFinal);
1510                                 getNextToken();
1511                                 foundToken = true;
1512                         } else {
1513                                 break;
1514                         }
1515                 }
1516                 return foundToken;
1517         }
1518
1519         private void class_variable_declaration(int declarationSourceStart, ArrayList list) {
1520                 // class_variable_declaration:
1521                 // class_variable_declaration ',' T_VARIABLE
1522                 // | class_variable_declaration ',' T_VARIABLE '=' static_scalar
1523                 // | T_VARIABLE
1524                 // | T_VARIABLE '=' static_scalar
1525                 char[] classVariable;
1526                 do {
1527                         if (token == TokenNameVariable) {
1528                                 classVariable = scanner.getCurrentIdentifierSource();
1529                                 // indexManager.addIdentifierInformation('v', classVariable, buf, -1,
1530                                 // -1);
1531                                 FieldDeclaration fieldDeclaration = new FieldDeclaration(classVariable, scanner.getCurrentTokenStartPosition(), scanner
1532                                                 .getCurrentTokenEndPosition());
1533                                 fieldDeclaration.modifiers = this.modifiers;
1534                                 fieldDeclaration.declarationSourceStart = declarationSourceStart;
1535                                 fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1536                                 fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1537                                 list.add(fieldDeclaration);
1538                                 if (fTypeVariables != null) {
1539                                         VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_CLASS_UNIT);
1540                                         fTypeVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1541                                 }
1542                                 getNextToken();
1543                                 if (token == TokenNameEQUAL) {
1544                                         getNextToken();
1545                                         static_scalar();
1546                                 }
1547                         } else {
1548                                 // if (token == TokenNamethis) {
1549                                 // throwSyntaxError("'$this' not allowed after keyword 'public'
1550                                 // 'protected' 'private' 'var'.");
1551                                 // }
1552                                 throwSyntaxError("Variable expected after keyword 'public' 'protected' 'private' 'var'.");
1553                         }
1554                         if (token != TokenNameCOMMA) {
1555                                 break;
1556                         }
1557                         getNextToken();
1558                 } while (true);
1559                 if (token != TokenNameSEMICOLON) {
1560                         throwSyntaxError("';' expected after field declaration.");
1561                 }
1562                 getNextToken();
1563         }
1564
1565         private void functionDefinition(MethodDeclaration methodDecl) {
1566                 boolean isAbstract = false;
1567                 if (astPtr == 0) {
1568                         if (compilationUnit != null) {
1569                                 compilationUnit.types.add(methodDecl);
1570                         }
1571                 } else {
1572                         ASTNode node = astStack[astPtr];
1573                         if (node instanceof TypeDeclaration) {
1574                                 TypeDeclaration typeDecl = ((TypeDeclaration) node);
1575                                 if (typeDecl.methods == null) {
1576                                         typeDecl.methods = new AbstractMethodDeclaration[] { methodDecl };
1577                                 } else {
1578                                         AbstractMethodDeclaration[] newMethods;
1579                                         System.arraycopy(typeDecl.methods, 0, newMethods = new AbstractMethodDeclaration[typeDecl.methods.length + 1], 0,
1580                                                         typeDecl.methods.length);
1581                                         newMethods[typeDecl.methods.length] = methodDecl;
1582                                         typeDecl.methods = newMethods;
1583                                 }
1584                                 if ((typeDecl.modifiers & AccAbstract) == AccAbstract) {
1585                                         isAbstract = true;
1586                                 } else if ((typeDecl.modifiers & AccInterface) == AccInterface) {
1587                                         isAbstract = true;
1588                                 }
1589                         }
1590                 }
1591                 try {
1592                         pushFunctionVariableSet();
1593                         functionDeclarator(methodDecl);
1594                         if (token == TokenNameSEMICOLON) {
1595                                 if (!isAbstract) {
1596                                         methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1597                                         throwSyntaxError("Body declaration expected for method: " + new String(methodDecl.selector));
1598                                 }
1599                                 getNextToken();
1600                                 return;
1601                         }
1602                         functionBody(methodDecl);
1603                 } finally {
1604                         if (!fStackUnassigned.isEmpty()) {
1605                                 fStackUnassigned.remove(fStackUnassigned.size() - 1);
1606                         }
1607                 }
1608         }
1609
1610         private void functionDeclarator(MethodDeclaration methodDecl) {
1611                 // identifier '(' [parameter-list] ')'
1612                 if (token == TokenNameAND) {
1613                         getNextToken();
1614                 }
1615                 methodDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1616                 methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1617                 if (Scanner.isIdentifierOrKeyword(token)) {
1618                         methodDecl.selector = scanner.getCurrentIdentifierSource();
1619                         if (token > TokenNameKEYWORD) {
1620                                 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1621                                                 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1622                         }
1623                         getNextToken();
1624                         if (token == TokenNameLPAREN) {
1625                                 getNextToken();
1626                         } else {
1627                                 methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1628                                 throwSyntaxError("'(' expected in function declaration.");
1629                         }
1630                         if (token != TokenNameRPAREN) {
1631                                 parameter_list(methodDecl);
1632                         }
1633                         if (token != TokenNameRPAREN) {
1634                                 methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1635                                 throwSyntaxError("')' expected in function declaration.");
1636                         } else {
1637                                 methodDecl.bodyStart = scanner.getCurrentTokenEndPosition() + 1;
1638                                 getNextToken();
1639                         }
1640                 } else {
1641                         methodDecl.selector = "<undefined>".toCharArray();
1642                         methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1643                         throwSyntaxError("Function name expected after keyword 'function'.");
1644                 }
1645         }
1646
1647         //
1648         private void parameter_list(MethodDeclaration methodDecl) {
1649                 // non_empty_parameter_list
1650                 // | /* empty */
1651                 non_empty_parameter_list(methodDecl, true);
1652         }
1653
1654         private void non_empty_parameter_list(MethodDeclaration methodDecl, boolean empty_allowed) {
1655                 // optional_class_type T_VARIABLE
1656                 // | optional_class_type '&' T_VARIABLE
1657                 // | optional_class_type '&' T_VARIABLE '=' static_scalar
1658                 // | optional_class_type T_VARIABLE '=' static_scalar
1659                 // | non_empty_parameter_list ',' optional_class_type T_VARIABLE
1660                 // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE
1661                 // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE '='
1662                 // static_scalar
1663                 // | non_empty_parameter_list ',' optional_class_type T_VARIABLE '='
1664                 // static_scalar
1665                 char[] typeIdentifier = null;
1666                 if (token == TokenNameIdentifier || token == TokenNameVariable || token == TokenNameAND) {
1667                         HashSet set = peekVariableSet();
1668                         while (true) {
1669                                 if (token == TokenNameIdentifier) {
1670                                         typeIdentifier = scanner.getCurrentIdentifierSource();
1671                                         getNextToken();
1672                                 }
1673                                 if (token == TokenNameAND) {
1674                                         getNextToken();
1675                                 }
1676                                 if (token == TokenNameVariable) {
1677                                         if (fMethodVariables != null) {
1678                                                 VariableInfo info;
1679                                                 if (methodDecl.type == MethodDeclaration.FUNCTION_DEFINITION) {
1680                                                         info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_FUNCTION_DEFINITION);
1681                                                 } else {
1682                                                         info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_METHOD_DEFINITION);
1683                                                 }
1684                                                 info.typeIdentifier = typeIdentifier;
1685                                                 fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1686                                         }
1687                                         addVariableSet(set);
1688                                         getNextToken();
1689                                         if (token == TokenNameEQUAL) {
1690                                                 getNextToken();
1691                                                 static_scalar();
1692                                         }
1693                                 } else {
1694                                         throwSyntaxError("Variable expected in parameter list.");
1695                                 }
1696                                 if (token != TokenNameCOMMA) {
1697                                         break;
1698                                 }
1699                                 getNextToken();
1700                         }
1701                         return;
1702                 }
1703                 if (!empty_allowed) {
1704                         throwSyntaxError("Identifier expected in parameter list.");
1705                 }
1706         }
1707
1708         private void optional_class_type() {
1709                 // /* empty */
1710                 // | T_STRING
1711         }
1712
1713         // private void parameterDeclaration() {
1714         // //variable
1715         // //variable-reference
1716         // if (token == TokenNameAND) {
1717         // getNextToken();
1718         // if (isVariable()) {
1719         // getNextToken();
1720         // } else {
1721         // throwSyntaxError("Variable expected after reference operator '&'.");
1722         // }
1723         // }
1724         // //variable '=' constant
1725         // if (token == TokenNameVariable) {
1726         // getNextToken();
1727         // if (token == TokenNameEQUAL) {
1728         // getNextToken();
1729         // static_scalar();
1730         // }
1731         // return;
1732         // }
1733         // // if (token == TokenNamethis) {
1734         // // throwSyntaxError("Reserved word '$this' not allowed in parameter
1735         // // declaration.");
1736         // // }
1737         // }
1738
1739         private void labeledStatementList() {
1740                 if (token != TokenNamecase && token != TokenNamedefault) {
1741                         throwSyntaxError("'case' or 'default' expected.");
1742                 }
1743                 do {
1744                         if (token == TokenNamecase) {
1745                                 getNextToken();
1746                                 expr(); // constant();
1747                                 if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1748                                         getNextToken();
1749                                         if (token == TokenNamecase || token == TokenNamedefault) {
1750                                                 // empty case statement ?
1751                                                 continue;
1752                                         }
1753                                         statementList();
1754                                 }
1755                                 // else if (token == TokenNameSEMICOLON) {
1756                                 // setMarker(
1757                                 // "':' expected after 'case' keyword (Found token: " +
1758                                 // scanner.toStringAction(token) + ")",
1759                                 // scanner.getCurrentTokenStartPosition(),
1760                                 // scanner.getCurrentTokenEndPosition(),
1761                                 // INFO);
1762                                 // getNextToken();
1763                                 // if (token == TokenNamecase) { // empty case statement ?
1764                                 // continue;
1765                                 // }
1766                                 // statementList();
1767                                 // }
1768                                 else {
1769                                         throwSyntaxError("':' character expected after 'case' constant (Found token: " + scanner.toStringAction(token) + ")");
1770                                 }
1771                         } else { // TokenNamedefault
1772                                 getNextToken();
1773                                 if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1774                                         getNextToken();
1775                                         if (token == TokenNameRBRACE) {
1776                                                 // empty default case
1777                                                 break;
1778                                         }
1779                                         if (token != TokenNamecase) {
1780                                                 statementList();
1781                                         }
1782                                 } else {
1783                                         throwSyntaxError("':' character expected after 'default'.");
1784                                 }
1785                         }
1786                 } while (token == TokenNamecase || token == TokenNamedefault);
1787         }
1788
1789         private void ifStatementColon(IfStatement iState) {
1790                 // T_IF '(' expr ')' ':' inner_statement_list new_elseif_list
1791                 // new_else_single T_ENDIF ';'
1792                 HashSet assignedVariableSet = null;
1793                 try {
1794                         Block b = inner_statement_list();
1795                         iState.thenStatement = b;
1796                         checkUnreachable(iState, b);
1797                 } finally {
1798                         assignedVariableSet = removeIfVariableSet();
1799                 }
1800                 if (token == TokenNameelseif) {
1801                         try {
1802                                 pushIfVariableSet();
1803                                 new_elseif_list(iState);
1804                         } finally {
1805                                 HashSet set = removeIfVariableSet();
1806                                 if (assignedVariableSet != null && set != null) {
1807                                         assignedVariableSet.addAll(set);
1808                                 }
1809                         }
1810                 }
1811                 try {
1812                         pushIfVariableSet();
1813                         new_else_single(iState);
1814                 } finally {
1815                         HashSet set = removeIfVariableSet();
1816                         if (assignedVariableSet != null) {
1817                                 HashSet topSet = peekVariableSet();
1818                                 if (topSet != null) {
1819                                         if (set != null) {
1820                                                 topSet.addAll(set);
1821                                         }
1822                                         topSet.addAll(assignedVariableSet);
1823                                 }
1824                         }
1825                 }
1826                 if (token != TokenNameendif) {
1827                         throwSyntaxError("'endif' expected.");
1828                 }
1829                 getNextToken();
1830                 if (token != TokenNameSEMICOLON) {
1831                         reportSyntaxError("';' expected after if-statement.");
1832                         iState.sourceEnd = scanner.getCurrentTokenStartPosition();
1833                 } else {
1834                         iState.sourceEnd = scanner.getCurrentTokenEndPosition();
1835                         getNextToken();
1836                 }
1837         }
1838
1839         private void ifStatement(IfStatement iState) {
1840                 // T_IF '(' expr ')' statement elseif_list else_single
1841                 HashSet assignedVariableSet = null;
1842                 try {
1843                         pushIfVariableSet();
1844                         Statement s = statement();
1845                         iState.thenStatement = s;
1846                         checkUnreachable(iState, s);
1847                 } finally {
1848                         assignedVariableSet = removeIfVariableSet();
1849                 }
1850
1851                 if (token == TokenNameelseif) {
1852                         try {
1853                                 pushIfVariableSet();
1854                                 elseif_list(iState);
1855                         } finally {
1856                                 HashSet set = removeIfVariableSet();
1857                                 if (assignedVariableSet != null && set != null) {
1858                                         assignedVariableSet.addAll(set);
1859                                 }
1860                         }
1861                 }
1862                 try {
1863                         pushIfVariableSet();
1864                         else_single(iState);
1865                 } finally {
1866                         HashSet set = removeIfVariableSet();
1867                         if (assignedVariableSet != null) {
1868                                 HashSet topSet = peekVariableSet();
1869                                 if (topSet != null) {
1870                                         if (set != null) {
1871                                                 topSet.addAll(set);
1872                                         }
1873                                         topSet.addAll(assignedVariableSet);
1874                                 }
1875                         }
1876                 }
1877         }
1878
1879         private void elseif_list(IfStatement iState) {
1880                 // /* empty */
1881                 // | elseif_list T_ELSEIF '(' expr ')' statement
1882                 ArrayList conditionList = new ArrayList();
1883                 ArrayList statementList = new ArrayList();
1884                 Expression e;
1885                 Statement s;
1886                 while (token == TokenNameelseif) {
1887                         getNextToken();
1888                         if (token == TokenNameLPAREN) {
1889                                 getNextToken();
1890                         } else {
1891                                 throwSyntaxError("'(' expected after 'elseif' keyword.");
1892                         }
1893                         e = expr();
1894                         conditionList.add(e);
1895                         if (token == TokenNameRPAREN) {
1896                                 getNextToken();
1897                         } else {
1898                                 throwSyntaxError("')' expected after 'elseif' condition.");
1899                         }
1900                         s = statement();
1901                         statementList.add(s);
1902                         checkUnreachable(iState, s);
1903                 }
1904                 iState.elseifConditions = new Expression[conditionList.size()];
1905                 iState.elseifStatements = new Statement[statementList.size()];
1906                 conditionList.toArray(iState.elseifConditions);
1907                 statementList.toArray(iState.elseifStatements);
1908         }
1909
1910         private void new_elseif_list(IfStatement iState) {
1911                 // /* empty */
1912                 // | new_elseif_list T_ELSEIF '(' expr ')' ':' inner_statement_list
1913                 ArrayList conditionList = new ArrayList();
1914                 ArrayList statementList = new ArrayList();
1915                 Expression e;
1916                 Block b;
1917                 while (token == TokenNameelseif) {
1918                         getNextToken();
1919                         if (token == TokenNameLPAREN) {
1920                                 getNextToken();
1921                         } else {
1922                                 throwSyntaxError("'(' expected after 'elseif' keyword.");
1923                         }
1924                         e = expr();
1925                         conditionList.add(e);
1926                         if (token == TokenNameRPAREN) {
1927                                 getNextToken();
1928                         } else {
1929                                 throwSyntaxError("')' expected after 'elseif' condition.");
1930                         }
1931                         if (token == TokenNameCOLON) {
1932                                 getNextToken();
1933                         } else {
1934                                 throwSyntaxError("':' expected after 'elseif' keyword.");
1935                         }
1936                         b = inner_statement_list();
1937                         statementList.add(b);
1938                         checkUnreachable(iState, b);
1939                 }
1940                 iState.elseifConditions = new Expression[conditionList.size()];
1941                 iState.elseifStatements = new Statement[statementList.size()];
1942                 conditionList.toArray(iState.elseifConditions);
1943                 statementList.toArray(iState.elseifStatements);
1944         }
1945
1946         private void else_single(IfStatement iState) {
1947                 // /* empty */
1948                 // T_ELSE statement
1949                 if (token == TokenNameelse) {
1950                         getNextToken();
1951                         Statement s = statement();
1952                         iState.elseStatement = s;
1953                         checkUnreachable(iState, s);
1954                 } else {
1955                         iState.checkUnreachable = false;
1956                 }
1957                 iState.sourceEnd = scanner.getCurrentTokenStartPosition();
1958         }
1959
1960         private void new_else_single(IfStatement iState) {
1961                 // /* empty */
1962                 // | T_ELSE ':' inner_statement_list
1963                 if (token == TokenNameelse) {
1964                         getNextToken();
1965                         if (token == TokenNameCOLON) {
1966                                 getNextToken();
1967                         } else {
1968                                 throwSyntaxError("':' expected after 'else' keyword.");
1969                         }
1970                         Block b = inner_statement_list();
1971                         iState.elseStatement = b;
1972                         checkUnreachable(iState, b);
1973                 } else {
1974                         iState.checkUnreachable = false;
1975                 }
1976         }
1977
1978         private Block inner_statement_list() {
1979                 // inner_statement_list inner_statement
1980                 // /* empty */
1981                 return statementList();
1982         }
1983
1984         /**
1985          * @param iState
1986          * @param b
1987          */
1988         private void checkUnreachable(IfStatement iState, Statement s) {
1989                 if (s instanceof Block) {
1990                         Block b = (Block) s;
1991                         if (b.statements == null || b.statements.length == 0) {
1992                                 iState.checkUnreachable = false;
1993                         } else {
1994                                 int off = b.statements.length - 1;
1995                                 if (!(b.statements[off] instanceof ReturnStatement) && !(b.statements[off] instanceof ContinueStatement)
1996                                                 && !(b.statements[off] instanceof BreakStatement)) {
1997                                         if (!(b.statements[off] instanceof IfStatement) || !((IfStatement) b.statements[off]).checkUnreachable) {
1998                                                 iState.checkUnreachable = false;
1999                                         }
2000                                 }
2001                         }
2002                 } else {
2003                         if (!(s instanceof ReturnStatement) && !(s instanceof ContinueStatement) && !(s instanceof BreakStatement)) {
2004                                 if (!(s instanceof IfStatement) || !((IfStatement) s).checkUnreachable) {
2005                                         iState.checkUnreachable = false;
2006                                 }
2007                         }
2008                 }
2009         }
2010
2011         // private void elseifStatementList() {
2012         // do {
2013         // elseifStatement();
2014         // switch (token) {
2015         // case TokenNameelse:
2016         // getNextToken();
2017         // if (token == TokenNameCOLON) {
2018         // getNextToken();
2019         // if (token != TokenNameendif) {
2020         // statementList();
2021         // }
2022         // return;
2023         // } else {
2024         // if (token == TokenNameif) { //'else if'
2025         // getNextToken();
2026         // } else {
2027         // throwSyntaxError("':' expected after 'else'.");
2028         // }
2029         // }
2030         // break;
2031         // case TokenNameelseif:
2032         // getNextToken();
2033         // break;
2034         // default:
2035         // return;
2036         // }
2037         // } while (true);
2038         // }
2039
2040         // private void elseifStatement() {
2041         // if (token == TokenNameLPAREN) {
2042         // getNextToken();
2043         // expr();
2044         // if (token != TokenNameRPAREN) {
2045         // throwSyntaxError("')' expected in else-if-statement.");
2046         // }
2047         // getNextToken();
2048         // if (token != TokenNameCOLON) {
2049         // throwSyntaxError("':' expected in else-if-statement.");
2050         // }
2051         // getNextToken();
2052         // if (token != TokenNameendif) {
2053         // statementList();
2054         // }
2055         // }
2056         // }
2057
2058         private void switchStatement() {
2059                 if (token == TokenNameCOLON) {
2060                         // ':' [labeled-statement-list] 'endswitch' ';'
2061                         getNextToken();
2062                         labeledStatementList();
2063                         if (token != TokenNameendswitch) {
2064                                 throwSyntaxError("'endswitch' expected.");
2065                         }
2066                         getNextToken();
2067                         if (token != TokenNameSEMICOLON) {
2068                                 throwSyntaxError("';' expected after switch-statement.");
2069                         }
2070                         getNextToken();
2071                 } else {
2072                         // '{' [labeled-statement-list] '}'
2073                         if (token != TokenNameLBRACE) {
2074                                 throwSyntaxError("'{' expected in switch statement.");
2075                         }
2076                         getNextToken();
2077                         if (token != TokenNameRBRACE) {
2078                                 labeledStatementList();
2079                         }
2080                         if (token != TokenNameRBRACE) {
2081                                 throwSyntaxError("'}' expected in switch statement.");
2082                         }
2083                         getNextToken();
2084                 }
2085         }
2086
2087         private void forStatement() {
2088                 if (token == TokenNameCOLON) {
2089                         getNextToken();
2090                         statementList();
2091                         if (token != TokenNameendfor) {
2092                                 throwSyntaxError("'endfor' expected.");
2093                         }
2094                         getNextToken();
2095                         if (token != TokenNameSEMICOLON) {
2096                                 throwSyntaxError("';' expected after for-statement.");
2097                         }
2098                         getNextToken();
2099                 } else {
2100                         statement();
2101                 }
2102         }
2103
2104         private void whileStatement() {
2105                 // ':' statement-list 'endwhile' ';'
2106                 if (token == TokenNameCOLON) {
2107                         getNextToken();
2108                         statementList();
2109                         if (token != TokenNameendwhile) {
2110                                 throwSyntaxError("'endwhile' expected.");
2111                         }
2112                         getNextToken();
2113                         if (token != TokenNameSEMICOLON) {
2114                                 throwSyntaxError("';' expected after while-statement.");
2115                         }
2116                         getNextToken();
2117                 } else {
2118                         statement();
2119                 }
2120         }
2121
2122         private void foreachStatement() {
2123                 if (token == TokenNameCOLON) {
2124                         getNextToken();
2125                         statementList();
2126                         if (token != TokenNameendforeach) {
2127                                 throwSyntaxError("'endforeach' expected.");
2128                         }
2129                         getNextToken();
2130                         if (token != TokenNameSEMICOLON) {
2131                                 throwSyntaxError("';' expected after foreach-statement.");
2132                         }
2133                         getNextToken();
2134                 } else {
2135                         statement();
2136                 }
2137         }
2138
2139         // private void exitStatus() {
2140         // if (token == TokenNameLPAREN) {
2141         // getNextToken();
2142         // } else {
2143         // throwSyntaxError("'(' expected in 'exit-status'.");
2144         // }
2145         // if (token != TokenNameRPAREN) {
2146         // expression();
2147         // }
2148         // if (token == TokenNameRPAREN) {
2149         // getNextToken();
2150         // } else {
2151         // throwSyntaxError("')' expected after 'exit-status'.");
2152         // }
2153         // }
2154         private void expressionList() {
2155                 do {
2156                         expr();
2157                         if (token == TokenNameCOMMA) {
2158                                 getNextToken();
2159                         } else {
2160                                 break;
2161                         }
2162                 } while (true);
2163         }
2164
2165         private Expression expr() {
2166                 // r_variable
2167                 // | expr_without_variable
2168                 // if (token!=TokenNameEOF) {
2169                 if (Scanner.TRACE) {
2170                         System.out.println("TRACE: expr()");
2171                 }
2172                 return expr_without_variable(true);
2173                 // }
2174         }
2175
2176         private Expression expr_without_variable(boolean only_variable) {
2177                 int exprSourceStart = scanner.getCurrentTokenStartPosition();
2178                 int exprSourceEnd = scanner.getCurrentTokenEndPosition();
2179                 Expression expression = new Expression();
2180                 expression.sourceStart = exprSourceStart;
2181                 // default, may be overwritten
2182                 expression.sourceEnd = exprSourceEnd;
2183                 try {
2184                         // internal_functions_in_yacc
2185                         // | T_CLONE expr
2186                         // | T_PRINT expr
2187                         // | '(' expr ')'
2188                         // | '@' expr
2189                         // | '+' expr
2190                         // | '-' expr
2191                         // | '!' expr
2192                         // | '~' expr
2193                         // | T_INC rw_variable
2194                         // | T_DEC rw_variable
2195                         // | T_INT_CAST expr
2196                         // | T_DOUBLE_CAST expr
2197                         // | T_STRING_CAST expr
2198                         // | T_ARRAY_CAST expr
2199                         // | T_OBJECT_CAST expr
2200                         // | T_BOOL_CAST expr
2201                         // | T_UNSET_CAST expr
2202                         // | T_EXIT exit_expr
2203                         // | scalar
2204                         // | T_ARRAY '(' array_pair_list ')'
2205                         // | '`' encaps_list '`'
2206                         // | T_LIST '(' assignment_list ')' '=' expr
2207                         // | T_NEW class_name_reference ctor_arguments
2208                         // | variable '=' expr
2209                         // | variable '=' '&' variable
2210                         // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2211                         // | variable T_PLUS_EQUAL expr
2212                         // | variable T_MINUS_EQUAL expr
2213                         // | variable T_MUL_EQUAL expr
2214                         // | variable T_DIV_EQUAL expr
2215                         // | variable T_CONCAT_EQUAL expr
2216                         // | variable T_MOD_EQUAL expr
2217                         // | variable T_AND_EQUAL expr
2218                         // | variable T_OR_EQUAL expr
2219                         // | variable T_XOR_EQUAL expr
2220                         // | variable T_SL_EQUAL expr
2221                         // | variable T_SR_EQUAL expr
2222                         // | rw_variable T_INC
2223                         // | rw_variable T_DEC
2224                         // | expr T_BOOLEAN_OR expr
2225                         // | expr T_BOOLEAN_AND expr
2226                         // | expr T_LOGICAL_OR expr
2227                         // | expr T_LOGICAL_AND expr
2228                         // | expr T_LOGICAL_XOR expr
2229                         // | expr '|' expr
2230                         // | expr '&' expr
2231                         // | expr '^' expr
2232                         // | expr '.' expr
2233                         // | expr '+' expr
2234                         // | expr '-' expr
2235                         // | expr '*' expr
2236                         // | expr '/' expr
2237                         // | expr '%' expr
2238                         // | expr T_SL expr
2239                         // | expr T_SR expr
2240                         // | expr T_IS_IDENTICAL expr
2241                         // | expr T_IS_NOT_IDENTICAL expr
2242                         // | expr T_IS_EQUAL expr
2243                         // | expr T_IS_NOT_EQUAL expr
2244                         // | expr '<' expr
2245                         // | expr T_IS_SMALLER_OR_EQUAL expr
2246                         // | expr '>' expr
2247                         // | expr T_IS_GREATER_OR_EQUAL expr
2248                         // | expr T_INSTANCEOF class_name_reference
2249                         // | expr '?' expr ':' expr
2250                         if (Scanner.TRACE) {
2251                                 System.out.println("TRACE: expr_without_variable() PART 1");
2252                         }
2253                         switch (token) {
2254                         case TokenNameisset:
2255                                 // T_ISSET '(' isset_variables ')'
2256                                 getNextToken();
2257                                 if (token != TokenNameLPAREN) {
2258                                         throwSyntaxError("'(' expected after keyword 'isset'");
2259                                 }
2260                                 getNextToken();
2261                                 isset_variables();
2262                                 if (token != TokenNameRPAREN) {
2263                                         throwSyntaxError("')' expected after keyword 'isset'");
2264                                 }
2265                                 getNextToken();
2266                                 break;
2267                         case TokenNameempty:
2268                                 getNextToken();
2269                                 if (token != TokenNameLPAREN) {
2270                                         throwSyntaxError("'(' expected after keyword 'empty'");
2271                                 }
2272                                 getNextToken();
2273                                 variable(true, false);
2274                                 if (token != TokenNameRPAREN) {
2275                                         throwSyntaxError("')' expected after keyword 'empty'");
2276                                 }
2277                                 getNextToken();
2278                                 break;
2279                         case TokenNameeval:
2280                         case TokenNameinclude:
2281                         case TokenNameinclude_once:
2282                         case TokenNamerequire:
2283                         case TokenNamerequire_once:
2284                                 internal_functions_in_yacc();
2285                                 break;
2286                         // | '(' expr ')'
2287                         case TokenNameLPAREN:
2288                                 getNextToken();
2289                                 expr();
2290                                 if (token == TokenNameRPAREN) {
2291                                         getNextToken();
2292                                 } else {
2293                                         throwSyntaxError("')' expected in expression.");
2294                                 }
2295                                 break;
2296                         // | T_CLONE expr
2297                         // | T_PRINT expr
2298                         // | '@' expr
2299                         // | '+' expr
2300                         // | '-' expr
2301                         // | '!' expr
2302                         // | '~' expr
2303                         // | T_INT_CAST expr
2304                         // | T_DOUBLE_CAST expr
2305                         // | T_STRING_CAST expr
2306                         // | T_ARRAY_CAST expr
2307                         // | T_OBJECT_CAST expr
2308                         // | T_BOOL_CAST expr
2309                         // | T_UNSET_CAST expr
2310                         case TokenNameclone:
2311                         case TokenNameprint:
2312                         case TokenNameAT:
2313                         case TokenNamePLUS:
2314                         case TokenNameMINUS:
2315                         case TokenNameNOT:
2316                         case TokenNameTWIDDLE:
2317                         case TokenNameintCAST:
2318                         case TokenNamedoubleCAST:
2319                         case TokenNamestringCAST:
2320                         case TokenNamearrayCAST:
2321                         case TokenNameobjectCAST:
2322                         case TokenNameboolCAST:
2323                         case TokenNameunsetCAST:
2324                                 getNextToken();
2325                                 expr();
2326                                 break;
2327                         case TokenNameexit:
2328                                 getNextToken();
2329                                 exit_expr();
2330                                 break;
2331                         // scalar:
2332                         // T_STRING
2333                         // | T_STRING_VARNAME
2334                         // | class_constant
2335                         // | T_START_HEREDOC encaps_list T_END_HEREDOC
2336                         // | '`' encaps_list '`'
2337                         // | common_scalar
2338                         // | '`' encaps_list '`'
2339                         // case TokenNameEncapsedString0:
2340                         // scanner.encapsedStringStack.push(new Character('`'));
2341                         // getNextToken();
2342                         // try {
2343                         // if (token == TokenNameEncapsedString0) {
2344                         // } else {
2345                         // encaps_list();
2346                         // if (token != TokenNameEncapsedString0) {
2347                         // throwSyntaxError("\'`\' expected at end of string" + "(Found token: " +
2348                         // scanner.toStringAction(token) + " )");
2349                         // }
2350                         // }
2351                         // } finally {
2352                         // scanner.encapsedStringStack.pop();
2353                         // getNextToken();
2354                         // }
2355                         // break;
2356                         // // | '\'' encaps_list '\''
2357                         // case TokenNameEncapsedString1:
2358                         // scanner.encapsedStringStack.push(new Character('\''));
2359                         // getNextToken();
2360                         // try {
2361                         // exprSourceStart = scanner.getCurrentTokenStartPosition();
2362                         // if (token == TokenNameEncapsedString1) {
2363                         // expression = new
2364                         // StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2365                         // exprSourceStart, scanner
2366                         // .getCurrentTokenEndPosition());
2367                         // } else {
2368                         // encaps_list();
2369                         // if (token != TokenNameEncapsedString1) {
2370                         // throwSyntaxError("\'\'\' expected at end of string" + "(Found token: "
2371                         // + scanner.toStringAction(token) + " )");
2372                         // } else {
2373                         // expression = new
2374                         // StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2375                         // exprSourceStart, scanner
2376                         // .getCurrentTokenEndPosition());
2377                         // }
2378                         // }
2379                         // } finally {
2380                         // scanner.encapsedStringStack.pop();
2381                         // getNextToken();
2382                         // }
2383                         // break;
2384                         // //| '"' encaps_list '"'
2385                         // case TokenNameEncapsedString2:
2386                         // scanner.encapsedStringStack.push(new Character('"'));
2387                         // getNextToken();
2388                         // try {
2389                         // exprSourceStart = scanner.getCurrentTokenStartPosition();
2390                         // if (token == TokenNameEncapsedString2) {
2391                         // expression = new
2392                         // StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2393                         // exprSourceStart, scanner
2394                         // .getCurrentTokenEndPosition());
2395                         // } else {
2396                         // encaps_list();
2397                         // if (token != TokenNameEncapsedString2) {
2398                         // throwSyntaxError("'\"' expected at end of string" + "(Found token: " +
2399                         // scanner.toStringAction(token) + " )");
2400                         // } else {
2401                         // expression = new
2402                         // StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2403                         // exprSourceStart, scanner
2404                         // .getCurrentTokenEndPosition());
2405                         // }
2406                         // }
2407                         // } finally {
2408                         // scanner.encapsedStringStack.pop();
2409                         // getNextToken();
2410                         // }
2411                         // break;
2412                         case TokenNameStringDoubleQuote:
2413                                 expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2414                                                 .getCurrentTokenEndPosition());
2415                                 common_scalar();
2416                                 break;
2417                         case TokenNameStringSingleQuote:
2418                                 expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2419                                                 .getCurrentTokenEndPosition());
2420                                 common_scalar();
2421                                 break;
2422                         case TokenNameIntegerLiteral:
2423                         case TokenNameDoubleLiteral:
2424                         case TokenNameStringInterpolated:
2425                         case TokenNameFILE:
2426                         case TokenNameLINE:
2427                         case TokenNameCLASS_C:
2428                         case TokenNameMETHOD_C:
2429                         case TokenNameFUNC_C:
2430                                 common_scalar();
2431                                 break;
2432                         case TokenNameHEREDOC:
2433                                 getNextToken();
2434                                 break;
2435                         case TokenNamearray:
2436                                 // T_ARRAY '(' array_pair_list ')'
2437                                 getNextToken();
2438                                 if (token == TokenNameLPAREN) {
2439                                         getNextToken();
2440                                         if (token == TokenNameRPAREN) {
2441                                                 getNextToken();
2442                                                 break;
2443                                         }
2444                                         array_pair_list();
2445                                         if (token != TokenNameRPAREN) {
2446                                                 throwSyntaxError("')' or ',' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2447                                         }
2448                                         getNextToken();
2449                                 } else {
2450                                         throwSyntaxError("'(' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2451                                 }
2452                                 break;
2453                         case TokenNamelist:
2454                                 // | T_LIST '(' assignment_list ')' '=' expr
2455                                 getNextToken();
2456                                 if (token == TokenNameLPAREN) {
2457                                         getNextToken();
2458                                         assignment_list();
2459                                         if (token != TokenNameRPAREN) {
2460                                                 throwSyntaxError("')' expected after 'list' keyword.");
2461                                         }
2462                                         getNextToken();
2463                                         if (token != TokenNameEQUAL) {
2464                                                 throwSyntaxError("'=' expected after 'list' keyword.");
2465                                         }
2466                                         getNextToken();
2467                                         expr();
2468                                 } else {
2469                                         throwSyntaxError("'(' expected after 'list' keyword.");
2470                                 }
2471                                 break;
2472                         case TokenNamenew:
2473                                 // | T_NEW class_name_reference ctor_arguments
2474                                 getNextToken();
2475                                 Expression typeRef = class_name_reference();
2476                                 ctor_arguments();
2477                                 if (typeRef != null) {
2478                                         expression = typeRef;
2479                                 }
2480                                 break;
2481                         // | T_INC rw_variable
2482                         // | T_DEC rw_variable
2483                         case TokenNamePLUS_PLUS:
2484                         case TokenNameMINUS_MINUS:
2485                                 getNextToken();
2486                                 rw_variable();
2487                                 break;
2488                         // | variable '=' expr
2489                         // | variable '=' '&' variable
2490                         // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2491                         // | variable T_PLUS_EQUAL expr
2492                         // | variable T_MINUS_EQUAL expr
2493                         // | variable T_MUL_EQUAL expr
2494                         // | variable T_DIV_EQUAL expr
2495                         // | variable T_CONCAT_EQUAL expr
2496                         // | variable T_MOD_EQUAL expr
2497                         // | variable T_AND_EQUAL expr
2498                         // | variable T_OR_EQUAL expr
2499                         // | variable T_XOR_EQUAL expr
2500                         // | variable T_SL_EQUAL expr
2501                         // | variable T_SR_EQUAL expr
2502                         // | rw_variable T_INC
2503                         // | rw_variable T_DEC
2504                         case TokenNameIdentifier:
2505                         case TokenNameVariable:
2506                         case TokenNameDOLLAR:
2507                                 Expression lhs = null;
2508                                 boolean rememberedVar = false;
2509                                 if (token == TokenNameIdentifier) {
2510                                         lhs = identifier(true, true);
2511                                         if (lhs != null) {
2512                                                 expression = lhs;
2513                                         }
2514                                 } else {
2515                                         lhs = variable(true, true);
2516                                         if (lhs != null) {
2517                                                 expression = lhs;
2518                                         }
2519                                         if (lhs != null && lhs instanceof FieldReference && token != TokenNameEQUAL && token != TokenNamePLUS_EQUAL
2520                                                         && token != TokenNameMINUS_EQUAL && token != TokenNameMULTIPLY_EQUAL && token != TokenNameDIVIDE_EQUAL
2521                                                         && token != TokenNameDOT_EQUAL && token != TokenNameREMAINDER_EQUAL && token != TokenNameAND_EQUAL
2522                                                         && token != TokenNameOR_EQUAL && token != TokenNameXOR_EQUAL && token != TokenNameRIGHT_SHIFT_EQUAL
2523                                                         && token != TokenNameLEFT_SHIFT_EQUAL) {
2524                                                 FieldReference ref = (FieldReference) lhs;
2525                                                 if (!containsVariableSet(ref.token)) {
2526                                                         problemReporter.uninitializedLocalVariable(new String(ref.token), ref.sourceStart(), ref.sourceEnd(),
2527                                                                         referenceContext, compilationUnit.compilationResult);
2528                                                         addVariableSet(ref.token);
2529                                                 }
2530                                         }
2531                                 }
2532                                 switch (token) {
2533                                 case TokenNameEQUAL:
2534                                         if (lhs != null && lhs instanceof FieldReference) {
2535                                                 addVariableSet(((FieldReference) lhs).token);
2536                                         }
2537                                         getNextToken();
2538                                         if (token == TokenNameAND) {
2539                                                 getNextToken();
2540                                                 if (token == TokenNamenew) {
2541                                                         // | variable '=' '&' T_NEW class_name_reference
2542                                                         // ctor_arguments
2543                                                         getNextToken();
2544                                                         SingleTypeReference classRef = class_name_reference();
2545                                                         ctor_arguments();
2546                                                         if (classRef != null) {
2547                                                                 if (lhs != null && lhs instanceof FieldReference) {
2548                                                                         // example:
2549                                                                         // $var = & new Object();
2550                                                                         if (fMethodVariables != null) {
2551                                                                                 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2552                                                                                 lhsInfo.reference = classRef;
2553                                                                                 lhsInfo.typeIdentifier = classRef.token;
2554                                                                                 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2555                                                                                 rememberedVar = true;
2556                                                                         }
2557                                                                 }
2558                                                         }
2559                                                 } else {
2560                                                         Expression rhs = variable(false, false);
2561                                                         if (rhs != null && rhs instanceof FieldReference && lhs != null && lhs instanceof FieldReference) {
2562                                                                 // example:
2563                                                                 // $var = &$ref;
2564                                                                 if (fMethodVariables != null) {
2565                                                                         VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2566                                                                         if (rhsInfo != null && rhsInfo.reference != null) {
2567                                                                                 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2568                                                                                 lhsInfo.reference = rhsInfo.reference;
2569                                                                                 lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2570                                                                                 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2571                                                                                 rememberedVar = true;
2572                                                                         }
2573                                                                 }
2574                                                         }
2575                                                 }
2576                                         } else {
2577                                                 Expression rhs = expr();
2578                                                 if (lhs != null && lhs instanceof FieldReference) {
2579                                                         if (rhs != null && rhs instanceof FieldReference) {
2580                                                                 // example:
2581                                                                 // $var = $ref;
2582                                                                 if (fMethodVariables != null) {
2583                                                                         VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2584                                                                         if (rhsInfo != null && rhsInfo.reference != null) {
2585                                                                                 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2586                                                                                 lhsInfo.reference = rhsInfo.reference;
2587                                                                                 lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2588                                                                                 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2589                                                                                 rememberedVar = true;
2590                                                                         }
2591                                                                 }
2592                                                         } else if (rhs != null && rhs instanceof SingleTypeReference) {
2593                                                                 // example:
2594                                                                 // $var = new Object();
2595                                                                 if (fMethodVariables != null) {
2596                                                                         VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2597                                                                         lhsInfo.reference = (SingleTypeReference) rhs;
2598                                                                         lhsInfo.typeIdentifier = ((SingleTypeReference) rhs).token;
2599                                                                         fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2600                                                                         rememberedVar = true;
2601                                                                 }
2602                                                         }
2603                                                 }
2604                                         }
2605                                         if (rememberedVar == false && lhs != null && lhs instanceof FieldReference) {
2606                                                 if (fMethodVariables != null) {
2607                                                         VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2608                                                         fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2609                                                 }
2610                                         }
2611                                         break;
2612                                 case TokenNamePLUS_EQUAL:
2613                                 case TokenNameMINUS_EQUAL:
2614                                 case TokenNameMULTIPLY_EQUAL:
2615                                 case TokenNameDIVIDE_EQUAL:
2616                                 case TokenNameDOT_EQUAL:
2617                                 case TokenNameREMAINDER_EQUAL:
2618                                 case TokenNameAND_EQUAL:
2619                                 case TokenNameOR_EQUAL:
2620                                 case TokenNameXOR_EQUAL:
2621                                 case TokenNameRIGHT_SHIFT_EQUAL:
2622                                 case TokenNameLEFT_SHIFT_EQUAL:
2623                                         if (lhs != null && lhs instanceof FieldReference) {
2624                                                 addVariableSet(((FieldReference) lhs).token);
2625                                         }
2626                                         getNextToken();
2627                                         expr();
2628                                         break;
2629                                 case TokenNamePLUS_PLUS:
2630                                 case TokenNameMINUS_MINUS:
2631                                         getNextToken();
2632                                         break;
2633                                 default:
2634                                         if (!only_variable) {
2635                                                 throwSyntaxError("Variable expression not allowed (found token '" + scanner.toStringAction(token) + "').");
2636                                         }
2637                                         if (lhs != null) {
2638                                                 expression = lhs;
2639                                         }
2640                                 }
2641                                 break;
2642                         default:
2643                                 if (token != TokenNameINLINE_HTML) {
2644                                         if (token > TokenNameKEYWORD) {
2645                                                 getNextToken();
2646                                                 break;
2647                                         } else {
2648                                                 // System.out.println(scanner.getCurrentTokenStartPosition());
2649                                                 // System.out.println(scanner.getCurrentTokenEndPosition());
2650
2651                                                 throwSyntaxError("Error in expression (found token '" + scanner.toStringAction(token) + "').");
2652                                         }
2653                                 }
2654                                 return expression;
2655                         }
2656                         if (Scanner.TRACE) {
2657                                 System.out.println("TRACE: expr_without_variable() PART 2");
2658                         }
2659                         // | expr T_BOOLEAN_OR expr
2660                         // | expr T_BOOLEAN_AND expr
2661                         // | expr T_LOGICAL_OR expr
2662                         // | expr T_LOGICAL_AND expr
2663                         // | expr T_LOGICAL_XOR expr
2664                         // | expr '|' expr
2665                         // | expr '&' expr
2666                         // | expr '^' expr
2667                         // | expr '.' expr
2668                         // | expr '+' expr
2669                         // | expr '-' expr
2670                         // | expr '*' expr
2671                         // | expr '/' expr
2672                         // | expr '%' expr
2673                         // | expr T_SL expr
2674                         // | expr T_SR expr
2675                         // | expr T_IS_IDENTICAL expr
2676                         // | expr T_IS_NOT_IDENTICAL expr
2677                         // | expr T_IS_EQUAL expr
2678                         // | expr T_IS_NOT_EQUAL expr
2679                         // | expr '<' expr
2680                         // | expr T_IS_SMALLER_OR_EQUAL expr
2681                         // | expr '>' expr
2682                         // | expr T_IS_GREATER_OR_EQUAL expr
2683                         while (true) {
2684                                 switch (token) {
2685                                 case TokenNameOR_OR:
2686                                         getNextToken();
2687                                         expression = new OR_OR_Expression(expression, expr(), token);
2688                                         break;
2689                                 case TokenNameAND_AND:
2690                                         getNextToken();
2691                                         expression = new AND_AND_Expression(expression, expr(), token);
2692                                         break;
2693                                 case TokenNameEQUAL_EQUAL:
2694                                         getNextToken();
2695                                         expression = new EqualExpression(expression, expr(), token);
2696                                         break;
2697                                 case TokenNameand:
2698                                 case TokenNameor:
2699                                 case TokenNamexor:
2700                                 case TokenNameAND:
2701                                 case TokenNameOR:
2702                                 case TokenNameXOR:
2703                                 case TokenNameDOT:
2704                                 case TokenNamePLUS:
2705                                 case TokenNameMINUS:
2706                                 case TokenNameMULTIPLY:
2707                                 case TokenNameDIVIDE:
2708                                 case TokenNameREMAINDER:
2709                                 case TokenNameLEFT_SHIFT:
2710                                 case TokenNameRIGHT_SHIFT:
2711                                 case TokenNameEQUAL_EQUAL_EQUAL:
2712                                 case TokenNameNOT_EQUAL_EQUAL:
2713                                 case TokenNameNOT_EQUAL:
2714                                 case TokenNameLESS:
2715                                 case TokenNameLESS_EQUAL:
2716                                 case TokenNameGREATER:
2717                                 case TokenNameGREATER_EQUAL:
2718                                         getNextToken();
2719                                         expression = new BinaryExpression(expression, expr(), token);
2720                                         break;
2721                                 // | expr T_INSTANCEOF class_name_reference
2722                                 // | expr '?' expr ':' expr
2723                                 case TokenNameinstanceof:
2724                                         getNextToken();
2725                                         TypeReference classRef = class_name_reference();
2726                                         if (classRef != null) {
2727                                                 expression = new InstanceOfExpression(expression, classRef, OperatorIds.INSTANCEOF);
2728                                                 expression.sourceStart = exprSourceStart;
2729                                                 expression.sourceEnd = scanner.getCurrentTokenEndPosition();
2730                                         }
2731                                         break;
2732                                 case TokenNameQUESTION:
2733                                         getNextToken();
2734                                         Expression valueIfTrue = expr();
2735                                         if (token != TokenNameCOLON) {
2736                                                 throwSyntaxError("':' expected in conditional expression.");
2737                                         }
2738                                         getNextToken();
2739                                         Expression valueIfFalse = expr();
2740
2741                                         expression = new ConditionalExpression(expression, valueIfTrue, valueIfFalse);
2742                                         break;
2743                                 default:
2744                                         return expression;
2745                                 }
2746                         }
2747                 } catch (SyntaxError e) {
2748                         // try to find next token after expression with errors:
2749                         if (token == TokenNameSEMICOLON) {
2750                                 getNextToken();
2751                                 return expression;
2752                         }
2753                         if (token == TokenNameRBRACE || token == TokenNameRPAREN || token == TokenNameRBRACKET) {
2754                                 getNextToken();
2755                                 return expression;
2756                         }
2757                         throw e;
2758                 }
2759         }
2760
2761         private SingleTypeReference class_name_reference() {
2762                 // class_name_reference:
2763                 // T_STRING
2764                 // | dynamic_class_name_reference
2765                 SingleTypeReference ref = null;
2766                 if (Scanner.TRACE) {
2767                         System.out.println("TRACE: class_name_reference()");
2768                 }
2769                 if (token == TokenNameIdentifier) {
2770                         ref = new SingleTypeReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
2771                         getNextToken();
2772                 } else {
2773                         ref = null;
2774                         dynamic_class_name_reference();
2775                 }
2776                 return ref;
2777         }
2778
2779         private void dynamic_class_name_reference() {
2780                 // dynamic_class_name_reference:
2781                 // base_variable T_OBJECT_OPERATOR object_property
2782                 // dynamic_class_name_variable_properties
2783                 // | base_variable
2784                 if (Scanner.TRACE) {
2785                         System.out.println("TRACE: dynamic_class_name_reference()");
2786                 }
2787                 base_variable();
2788                 if (token == TokenNameMINUS_GREATER) {
2789                         getNextToken();
2790                         object_property();
2791                         dynamic_class_name_variable_properties();
2792                 }
2793         }
2794
2795         private void dynamic_class_name_variable_properties() {
2796                 // dynamic_class_name_variable_properties:
2797                 // dynamic_class_name_variable_properties
2798                 // dynamic_class_name_variable_property
2799                 // | /* empty */
2800                 if (Scanner.TRACE) {
2801                         System.out.println("TRACE: dynamic_class_name_variable_properties()");
2802                 }
2803                 while (token == TokenNameMINUS_GREATER) {
2804                         dynamic_class_name_variable_property();
2805                 }
2806         }
2807
2808         private void dynamic_class_name_variable_property() {
2809                 // dynamic_class_name_variable_property:
2810                 // T_OBJECT_OPERATOR object_property
2811                 if (Scanner.TRACE) {
2812                         System.out.println("TRACE: dynamic_class_name_variable_property()");
2813                 }
2814                 if (token == TokenNameMINUS_GREATER) {
2815                         getNextToken();
2816                         object_property();
2817                 }
2818         }
2819
2820         private void ctor_arguments() {
2821                 // ctor_arguments:
2822                 // /* empty */
2823                 // | '(' function_call_parameter_list ')'
2824                 if (token == TokenNameLPAREN) {
2825                         getNextToken();
2826                         if (token == TokenNameRPAREN) {
2827                                 getNextToken();
2828                                 return;
2829                         }
2830                         non_empty_function_call_parameter_list();
2831                         if (token != TokenNameRPAREN) {
2832                                 throwSyntaxError("')' expected in ctor_arguments.");
2833                         }
2834                         getNextToken();
2835                 }
2836         }
2837
2838         private void assignment_list() {
2839                 // assignment_list:
2840                 // assignment_list ',' assignment_list_element
2841                 // | assignment_list_element
2842                 while (true) {
2843                         assignment_list_element();
2844                         if (token != TokenNameCOMMA) {
2845                                 break;
2846                         }
2847                         getNextToken();
2848                 }
2849         }
2850
2851         private void assignment_list_element() {
2852                 // assignment_list_element:
2853                 // variable
2854                 // | T_LIST '(' assignment_list ')'
2855                 // | /* empty */
2856                 if (token == TokenNameVariable) {
2857                         variable(true, false);
2858                 } else if (token == TokenNameDOLLAR) {
2859                         variable(false, false);
2860                 } else {
2861                         if (token == TokenNamelist) {
2862                                 getNextToken();
2863                                 if (token == TokenNameLPAREN) {
2864                                         getNextToken();
2865                                         assignment_list();
2866                                         if (token != TokenNameRPAREN) {
2867                                                 throwSyntaxError("')' expected after 'list' keyword.");
2868                                         }
2869                                         getNextToken();
2870                                 } else {
2871                                         throwSyntaxError("'(' expected after 'list' keyword.");
2872                                 }
2873                         }
2874                 }
2875         }
2876
2877         private void array_pair_list() {
2878                 // array_pair_list:
2879                 // /* empty */
2880                 // | non_empty_array_pair_list possible_comma
2881                 non_empty_array_pair_list();
2882                 if (token == TokenNameCOMMA) {
2883                         getNextToken();
2884                 }
2885         }
2886
2887         private void non_empty_array_pair_list() {
2888                 // non_empty_array_pair_list:
2889                 // non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr
2890                 // | non_empty_array_pair_list ',' expr
2891                 // | expr T_DOUBLE_ARROW expr
2892                 // | expr
2893                 // | non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable
2894                 // | non_empty_array_pair_list ',' '&' w_variable
2895                 // | expr T_DOUBLE_ARROW '&' w_variable
2896                 // | '&' w_variable
2897                 while (true) {
2898                         if (token == TokenNameAND) {
2899                                 getNextToken();
2900                                 variable(true, false);
2901                         } else {
2902                                 expr();
2903                                 if (token == TokenNameAND) {
2904                                         getNextToken();
2905                                         variable(true, false);
2906                                 } else if (token == TokenNameEQUAL_GREATER) {
2907                                         getNextToken();
2908                                         if (token == TokenNameAND) {
2909                                                 getNextToken();
2910                                                 variable(true, false);
2911                                         } else {
2912                                                 expr();
2913                                         }
2914                                 }
2915                         }
2916                         if (token != TokenNameCOMMA) {
2917                                 return;
2918                         }
2919                         getNextToken();
2920                         if (token == TokenNameRPAREN) {
2921                                 return;
2922                         }
2923                 }
2924         }
2925
2926         // private void variableList() {
2927         // do {
2928         // variable();
2929         // if (token == TokenNameCOMMA) {
2930         // getNextToken();
2931         // } else {
2932         // break;
2933         // }
2934         // } while (true);
2935         // }
2936         private Expression variable_without_objects(boolean lefthandside, boolean ignoreVar) {
2937                 // variable_without_objects:
2938                 // reference_variable
2939                 // | simple_indirect_reference reference_variable
2940                 if (Scanner.TRACE) {
2941                         System.out.println("TRACE: variable_without_objects()");
2942                 }
2943                 while (token == TokenNameDOLLAR) {
2944                         getNextToken();
2945                 }
2946                 return reference_variable(lefthandside, ignoreVar);
2947         }
2948
2949         private Expression function_call(boolean lefthandside, boolean ignoreVar) {
2950                 // function_call:
2951                 // T_STRING '(' function_call_parameter_list ')'
2952                 // | class_constant '(' function_call_parameter_list ')'
2953                 // | static_member '(' function_call_parameter_list ')'
2954                 // | variable_without_objects '(' function_call_parameter_list ')'
2955                 char[] defineName = null;
2956                 char[] ident = null;
2957                 int startPos = 0;
2958                 int endPos = 0;
2959                 Expression ref = null;
2960                 if (Scanner.TRACE) {
2961                         System.out.println("TRACE: function_call()");
2962                 }
2963                 if (token == TokenNameIdentifier) {
2964                         ident = scanner.getCurrentIdentifierSource();
2965                         defineName = ident;
2966                         startPos = scanner.getCurrentTokenStartPosition();
2967                         endPos = scanner.getCurrentTokenEndPosition();
2968                         getNextToken();
2969                         switch (token) {
2970                         case TokenNamePAAMAYIM_NEKUDOTAYIM:
2971                                 // static member:
2972                                 defineName = null;
2973                                 getNextToken();
2974                                 if (token == TokenNameIdentifier) {
2975                                         // class _constant
2976                                         getNextToken();
2977                                 } else {
2978                                         // static member:
2979                                         variable_without_objects(true, false);
2980                                 }
2981                                 break;
2982                         }
2983                 } else {
2984                         ref = variable_without_objects(lefthandside, ignoreVar);
2985                 }
2986                 if (token != TokenNameLPAREN) {
2987                         if (defineName != null) {
2988                                 // does this identifier contain only uppercase characters?
2989                                 if (defineName.length == 3) {
2990                                         if (defineName[0] == 'd' && defineName[1] == 'i' && defineName[2] == 'e') {
2991                                                 defineName = null;
2992                                         }
2993                                 } else if (defineName.length == 4) {
2994                                         if (defineName[0] == 't' && defineName[1] == 'r' && defineName[2] == 'u' && defineName[3] == 'e') {
2995                                                 defineName = null;
2996                                         } else if (defineName[0] == 'n' && defineName[1] == 'u' && defineName[2] == 'l' && defineName[3] == 'l') {
2997                                                 defineName = null;
2998                                         }
2999                                 } else if (defineName.length == 5) {
3000                                         if (defineName[0] == 'f' && defineName[1] == 'a' && defineName[2] == 'l' && defineName[3] == 's' && defineName[4] == 'e') {
3001                                                 defineName = null;
3002                                         }
3003                                 }
3004                                 if (defineName != null) {
3005                                         for (int i = 0; i < defineName.length; i++) {
3006                                                 if (Character.isLowerCase(defineName[i])) {
3007                                                         problemReporter.phpUppercaseIdentifierWarning(startPos, endPos, referenceContext, compilationUnit.compilationResult);
3008                                                         break;
3009                                                 }
3010                                         }
3011                                 }
3012                         }
3013                         // TODO is this ok ?
3014                         return ref;
3015                         // throwSyntaxError("'(' expected in function call.");
3016                 }
3017                 getNextToken();
3018                 if (token == TokenNameRPAREN) {
3019                         getNextToken();
3020                         return ref;
3021                 }
3022                 non_empty_function_call_parameter_list();
3023                 if (token != TokenNameRPAREN) {
3024                         String functionName;
3025                         if (ident == null) {
3026                                 functionName = new String(" ");
3027                         } else {
3028                                 functionName = new String(ident);
3029                         }
3030                         throwSyntaxError("')' expected in function call (" + functionName + ").");
3031                 }
3032                 getNextToken();
3033                 return ref;
3034         }
3035
3036         // private void function_call_parameter_list() {
3037         // function_call_parameter_list:
3038         // non_empty_function_call_parameter_list { $$ = $1; }
3039         // | /* empty */
3040         // }
3041         private void non_empty_function_call_parameter_list() {
3042                 // non_empty_function_call_parameter_list:
3043                 // expr_without_variable
3044                 // | variable
3045                 // | '&' w_variable
3046                 // | non_empty_function_call_parameter_list ',' expr_without_variable
3047                 // | non_empty_function_call_parameter_list ',' variable
3048                 // | non_empty_function_call_parameter_list ',' '&' w_variable
3049                 if (Scanner.TRACE) {
3050                         System.out.println("TRACE: non_empty_function_call_parameter_list()");
3051                 }
3052                 while (true) {
3053                         if (token == TokenNameAND) {
3054                                 getNextToken();
3055                                 w_variable(true);
3056                         } else {
3057                                 // if (token == TokenNameIdentifier || token ==
3058                                 // TokenNameVariable
3059                                 // || token == TokenNameDOLLAR) {
3060                                 // variable();
3061                                 // } else {
3062                                 expr_without_variable(true);
3063                                 // }
3064                         }
3065                         if (token != TokenNameCOMMA) {
3066                                 break;
3067                         }
3068                         getNextToken();
3069                 }
3070         }
3071
3072         private void fully_qualified_class_name() {
3073                 if (token == TokenNameIdentifier) {
3074                         getNextToken();
3075                 } else {
3076                         throwSyntaxError("Class name expected.");
3077                 }
3078         }
3079
3080         private void static_member() {
3081                 // static_member:
3082                 // fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM
3083                 // variable_without_objects
3084                 if (Scanner.TRACE) {
3085                         System.out.println("TRACE: static_member()");
3086                 }
3087                 fully_qualified_class_name();
3088                 if (token != TokenNamePAAMAYIM_NEKUDOTAYIM) {
3089                         throwSyntaxError("'::' expected after class name (static_member).");
3090                 }
3091                 getNextToken();
3092                 variable_without_objects(false, false);
3093         }
3094
3095         private Expression base_variable_with_function_calls(boolean lefthandside, boolean ignoreVar) {
3096                 // base_variable_with_function_calls:
3097                 // base_variable
3098                 // | function_call
3099                 if (Scanner.TRACE) {
3100                         System.out.println("TRACE: base_variable_with_function_calls()");
3101                 }
3102                 return function_call(lefthandside, ignoreVar);
3103         }
3104
3105         private Expression base_variable() {
3106                 // base_variable:
3107                 // reference_variable
3108                 // | simple_indirect_reference reference_variable
3109                 // | static_member
3110                 Expression ref = null;
3111                 if (Scanner.TRACE) {
3112                         System.out.println("TRACE: base_variable()");
3113                 }
3114                 if (token == TokenNameIdentifier) {
3115                         static_member();
3116                 } else {
3117                         while (token == TokenNameDOLLAR) {
3118                                 getNextToken();
3119                         }
3120                         reference_variable(false, false);
3121                 }
3122                 return ref;
3123         }
3124
3125         // private void simple_indirect_reference() {
3126         // // simple_indirect_reference:
3127         // // '$'
3128         // //| simple_indirect_reference '$'
3129         // }
3130         private Expression reference_variable(boolean lefthandside, boolean ignoreVar) {
3131                 // reference_variable:
3132                 // reference_variable '[' dim_offset ']'
3133                 // | reference_variable '{' expr '}'
3134                 // | compound_variable
3135                 Expression ref = null;
3136                 if (Scanner.TRACE) {
3137                         System.out.println("TRACE: reference_variable()");
3138                 }
3139                 ref = compound_variable(lefthandside, ignoreVar);
3140                 while (true) {
3141                         if (token == TokenNameLBRACE) {
3142                                 ref = null;
3143                                 getNextToken();
3144                                 expr();
3145                                 if (token != TokenNameRBRACE) {
3146                                         throwSyntaxError("'}' expected in reference variable.");
3147                                 }
3148                                 getNextToken();
3149                         } else if (token == TokenNameLBRACKET) {
3150                                 if (ref != null && ref instanceof FieldReference) {
3151                                         FieldReference fref = (FieldReference) ref;
3152                                         addVariableSet(fref.token);
3153                                 }
3154                                 ref = null;
3155                                 getNextToken();
3156                                 if (token != TokenNameRBRACKET) {
3157                                         expr();
3158                                         // dim_offset();
3159                                         if (token != TokenNameRBRACKET) {
3160                                                 throwSyntaxError("']' expected in reference variable.");
3161                                         }
3162                                 }
3163                                 getNextToken();
3164                         } else {
3165                                 break;
3166                         }
3167                 }
3168                 return ref;
3169         }
3170
3171         private Expression compound_variable(boolean lefthandside, boolean ignoreVar) {
3172                 // compound_variable:
3173                 // T_VARIABLE
3174                 // | '$' '{' expr '}'
3175                 if (Scanner.TRACE) {
3176                         System.out.println("TRACE: compound_variable()");
3177                 }
3178                 if (token == TokenNameVariable) {
3179                         if (!lefthandside) {
3180                                 if (!containsVariableSet()) {
3181                                         // reportSyntaxError("The local variable " + new
3182                                         // String(scanner.getCurrentIdentifierSource())
3183                                         // + " may not have been initialized");
3184                                         problemReporter.uninitializedLocalVariable(new String(scanner.getCurrentIdentifierSource()), scanner
3185                                                         .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), referenceContext,
3186                                                         compilationUnit.compilationResult);
3187                                 }
3188                         } else {
3189                                 if (!ignoreVar) {
3190                                         addVariableSet();
3191                                 }
3192                         }
3193                         FieldReference ref = new FieldReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
3194                         getNextToken();
3195                         return ref;
3196                 } else {
3197                         // because of simple_indirect_reference
3198                         while (token == TokenNameDOLLAR) {
3199                                 getNextToken();
3200                         }
3201                         if (token != TokenNameLBRACE) {
3202                                 reportSyntaxError("'{' expected after compound variable token '$'.");
3203                                 return null;
3204                         }
3205                         getNextToken();
3206                         expr();
3207                         if (token != TokenNameRBRACE) {
3208                                 throwSyntaxError("'}' expected after compound variable token '$'.");
3209                         }
3210                         getNextToken();
3211                 }
3212                 return null;
3213         } // private void dim_offset() { // // dim_offset: // // /* empty */
3214
3215         // // | expr
3216         // expr();
3217         // }
3218         private void object_property() {
3219                 // object_property:
3220                 // object_dim_list
3221                 // | variable_without_objects
3222                 if (Scanner.TRACE) {
3223                         System.out.println("TRACE: object_property()");
3224                 }
3225                 if (token == TokenNameVariable || token == TokenNameDOLLAR) {
3226                         variable_without_objects(false, false);
3227                 } else {
3228                         object_dim_list();
3229                 }
3230         }
3231
3232         private void object_dim_list() {
3233                 // object_dim_list:
3234                 // object_dim_list '[' dim_offset ']'
3235                 // | object_dim_list '{' expr '}'
3236                 // | variable_name
3237                 if (Scanner.TRACE) {
3238                         System.out.println("TRACE: object_dim_list()");
3239                 }
3240                 variable_name();
3241                 while (true) {
3242                         if (token == TokenNameLBRACE) {
3243                                 getNextToken();
3244                                 expr();
3245                                 if (token != TokenNameRBRACE) {
3246                                         throwSyntaxError("'}' expected in object_dim_list.");
3247                                 }
3248                                 getNextToken();
3249                         } else if (token == TokenNameLBRACKET) {
3250                                 getNextToken();
3251                                 if (token == TokenNameRBRACKET) {
3252                                         getNextToken();
3253                                         continue;
3254                                 }
3255                                 expr();
3256                                 if (token != TokenNameRBRACKET) {
3257                                         throwSyntaxError("']' expected in object_dim_list.");
3258                                 }
3259                                 getNextToken();
3260                         } else {
3261                                 break;
3262                         }
3263                 }
3264         }
3265
3266         private void variable_name() {
3267                 // variable_name:
3268                 // T_STRING
3269                 // | '{' expr '}'
3270                 if (Scanner.TRACE) {
3271                         System.out.println("TRACE: variable_name()");
3272                 }
3273                 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3274                         if (token > TokenNameKEYWORD) {
3275                                 // TODO show a warning "Keyword used as variable" ?
3276                         }
3277                         getNextToken();
3278                 } else {
3279                         if (token != TokenNameLBRACE) {
3280                                 throwSyntaxError("'{' expected in variable name.");
3281                         }
3282                         getNextToken();
3283                         expr();
3284                         if (token != TokenNameRBRACE) {
3285                                 throwSyntaxError("'}' expected in variable name.");
3286                         }
3287                         getNextToken();
3288                 }
3289         }
3290
3291         private void r_variable() {
3292                 variable(false, false);
3293         }
3294
3295         private void w_variable(boolean lefthandside) {
3296                 variable(lefthandside, false);
3297         }
3298
3299         private void rw_variable() {
3300                 variable(false, false);
3301         }
3302
3303         private Expression variable(boolean lefthandside, boolean ignoreVar) {
3304                 // variable:
3305                 // base_variable_with_function_calls T_OBJECT_OPERATOR
3306                 // object_property method_or_not variable_properties
3307                 // | base_variable_with_function_calls
3308                 Expression ref = base_variable_with_function_calls(lefthandside, ignoreVar);
3309                 if (token == TokenNameMINUS_GREATER) {
3310                         ref = null;
3311                         getNextToken();
3312                         object_property();
3313                         method_or_not();
3314                         variable_properties();
3315                 }
3316                 return ref;
3317         }
3318
3319         private void variable_properties() {
3320                 // variable_properties:
3321                 // variable_properties variable_property
3322                 // | /* empty */
3323                 while (token == TokenNameMINUS_GREATER) {
3324                         variable_property();
3325                 }
3326         }
3327
3328         private void variable_property() {
3329                 // variable_property:
3330                 // T_OBJECT_OPERATOR object_property method_or_not
3331                 if (Scanner.TRACE) {
3332                         System.out.println("TRACE: variable_property()");
3333                 }
3334                 if (token == TokenNameMINUS_GREATER) {
3335                         getNextToken();
3336                         object_property();
3337                         method_or_not();
3338                 } else {
3339                         throwSyntaxError("'->' expected in variable_property.");
3340                 }
3341         }
3342
3343         private Expression identifier(boolean lefthandside, boolean ignoreVar) {
3344                 // variable:
3345                 // base_variable_with_function_calls T_OBJECT_OPERATOR
3346                 // object_property method_or_not variable_properties
3347                 // | base_variable_with_function_calls
3348
3349                 // Expression ref = function_call(lefthandside, ignoreVar);
3350
3351                 // function_call:
3352                 // T_STRING '(' function_call_parameter_list ')'
3353                 // | class_constant '(' function_call_parameter_list ')'
3354                 // | static_member '(' function_call_parameter_list ')'
3355                 // | variable_without_objects '(' function_call_parameter_list ')'
3356                 char[] defineName = null;
3357                 char[] ident = null;
3358                 int startPos = 0;
3359                 int endPos = 0;
3360                 Expression ref = null;
3361                 if (Scanner.TRACE) {
3362                         System.out.println("TRACE: function_call()");
3363                 }
3364                 if (token == TokenNameIdentifier) {
3365                         ident = scanner.getCurrentIdentifierSource();
3366                         defineName = ident;
3367                         startPos = scanner.getCurrentTokenStartPosition();
3368                         endPos = scanner.getCurrentTokenEndPosition();
3369                         getNextToken();
3370
3371                         if (token == TokenNameEQUAL || token == TokenNamePLUS_EQUAL || token == TokenNameMINUS_EQUAL
3372                                         || token == TokenNameMULTIPLY_EQUAL || token == TokenNameDIVIDE_EQUAL || token == TokenNameDOT_EQUAL
3373                                         || token == TokenNameREMAINDER_EQUAL || token == TokenNameAND_EQUAL || token == TokenNameOR_EQUAL
3374                                         || token == TokenNameXOR_EQUAL || token == TokenNameRIGHT_SHIFT_EQUAL || token == TokenNameLEFT_SHIFT_EQUAL) {
3375                                 String error = "Assignment operator '" + scanner.toStringAction(token) + "' not allowed after identifier '"
3376                                                 + new String(ident) + "' (use 'define(...)' to define constants).";
3377                                 reportSyntaxError(error);
3378                         }
3379
3380                         switch (token) {
3381                         case TokenNamePAAMAYIM_NEKUDOTAYIM:
3382                                 // static member:
3383                                 defineName = null;
3384                                 getNextToken();
3385                                 if (token == TokenNameIdentifier) {
3386                                         // class _constant
3387                                         getNextToken();
3388                                 } else {
3389                                         // static member:
3390                                         variable_without_objects(true, false);
3391                                 }
3392                                 break;
3393                         }
3394                 } else {
3395                         ref = variable_without_objects(lefthandside, ignoreVar);
3396                 }
3397                 if (token != TokenNameLPAREN) {
3398                         if (defineName != null) {
3399                                 // does this identifier contain only uppercase characters?
3400                                 if (defineName.length == 3) {
3401                                         if (defineName[0] == 'd' && defineName[1] == 'i' && defineName[2] == 'e') {
3402                                                 defineName = null;
3403                                         }
3404                                 } else if (defineName.length == 4) {
3405                                         if (defineName[0] == 't' && defineName[1] == 'r' && defineName[2] == 'u' && defineName[3] == 'e') {
3406                                                 defineName = null;
3407                                         } else if (defineName[0] == 'n' && defineName[1] == 'u' && defineName[2] == 'l' && defineName[3] == 'l') {
3408                                                 defineName = null;
3409                                         }
3410                                 } else if (defineName.length == 5) {
3411                                         if (defineName[0] == 'f' && defineName[1] == 'a' && defineName[2] == 'l' && defineName[3] == 's' && defineName[4] == 'e') {
3412                                                 defineName = null;
3413                                         }
3414                                 }
3415                                 if (defineName != null) {
3416                                         for (int i = 0; i < defineName.length; i++) {
3417                                                 if (Character.isLowerCase(defineName[i])) {
3418                                                         problemReporter.phpUppercaseIdentifierWarning(startPos, endPos, referenceContext, compilationUnit.compilationResult);
3419                                                         break;
3420                                                 }
3421                                         }
3422                                 }
3423                         }
3424                         // TODO is this ok ?
3425                         return ref;
3426                         // throwSyntaxError("'(' expected in function call.");
3427                 }
3428                 getNextToken();
3429                 if (token == TokenNameRPAREN) {
3430                         getNextToken();
3431                         return ref;
3432                 }
3433                 non_empty_function_call_parameter_list();
3434                 if (token != TokenNameRPAREN) {
3435                         String functionName;
3436                         if (ident == null) {
3437                                 functionName = new String(" ");
3438                         } else {
3439                                 functionName = new String(ident);
3440                         }
3441                         throwSyntaxError("')' expected in function call (" + functionName + ").");
3442                 }
3443                 getNextToken();
3444
3445                 if (token == TokenNameMINUS_GREATER) {
3446                         ref = null;
3447                         getNextToken();
3448                         object_property();
3449                         method_or_not();
3450                         variable_properties();
3451                 }
3452                 return ref;
3453         }
3454
3455         private void method_or_not() {
3456                 // method_or_not:
3457                 // '(' function_call_parameter_list ')'
3458                 // | /* empty */
3459                 if (Scanner.TRACE) {
3460                         System.out.println("TRACE: method_or_not()");
3461                 }
3462                 if (token == TokenNameLPAREN) {
3463                         getNextToken();
3464                         if (token == TokenNameRPAREN) {
3465                                 getNextToken();
3466                                 return;
3467                         }
3468                         non_empty_function_call_parameter_list();
3469                         if (token != TokenNameRPAREN) {
3470                                 throwSyntaxError("')' expected in method_or_not.");
3471                         }
3472                         getNextToken();
3473                 }
3474         }
3475
3476         private void exit_expr() {
3477                 // /* empty */
3478                 // | '(' ')'
3479                 // | '(' expr ')'
3480                 if (token != TokenNameLPAREN) {
3481                         return;
3482                 }
3483                 getNextToken();
3484                 if (token == TokenNameRPAREN) {
3485                         getNextToken();
3486                         return;
3487                 }
3488                 expr();
3489                 if (token != TokenNameRPAREN) {
3490                         throwSyntaxError("')' expected after keyword 'exit'");
3491                 }
3492                 getNextToken();
3493         }
3494
3495         // private void encaps_list() {
3496         // // encaps_list encaps_var
3497         // // | encaps_list T_STRING
3498         // // | encaps_list T_NUM_STRING
3499         // // | encaps_list T_ENCAPSED_AND_WHITESPACE
3500         // // | encaps_list T_CHARACTER
3501         // // | encaps_list T_BAD_CHARACTER
3502         // // | encaps_list '['
3503         // // | encaps_list ']'
3504         // // | encaps_list '{'
3505         // // | encaps_list '}'
3506         // // | encaps_list T_OBJECT_OPERATOR
3507         // // | /* empty */
3508         // while (true) {
3509         // switch (token) {
3510         // case TokenNameSTRING:
3511         // getNextToken();
3512         // break;
3513         // case TokenNameLBRACE:
3514         // // scanner.encapsedStringStack.pop();
3515         // getNextToken();
3516         // break;
3517         // case TokenNameRBRACE:
3518         // // scanner.encapsedStringStack.pop();
3519         // getNextToken();
3520         // break;
3521         // case TokenNameLBRACKET:
3522         // // scanner.encapsedStringStack.pop();
3523         // getNextToken();
3524         // break;
3525         // case TokenNameRBRACKET:
3526         // // scanner.encapsedStringStack.pop();
3527         // getNextToken();
3528         // break;
3529         // case TokenNameMINUS_GREATER:
3530         // // scanner.encapsedStringStack.pop();
3531         // getNextToken();
3532         // break;
3533         // case TokenNameVariable:
3534         // case TokenNameDOLLAR_LBRACE:
3535         // case TokenNameLBRACE_DOLLAR:
3536         // encaps_var();
3537         // break;
3538         // default:
3539         // char encapsedChar = ((Character)
3540         // scanner.encapsedStringStack.peek()).charValue();
3541         // if (encapsedChar == '$') {
3542         // scanner.encapsedStringStack.pop();
3543         // encapsedChar = ((Character)
3544         // scanner.encapsedStringStack.peek()).charValue();
3545         // switch (encapsedChar) {
3546         // case '`':
3547         // if (token == TokenNameEncapsedString0) {
3548         // return;
3549         // }
3550         // token = TokenNameSTRING;
3551         // continue;
3552         // case '\'':
3553         // if (token == TokenNameEncapsedString1) {
3554         // return;
3555         // }
3556         // token = TokenNameSTRING;
3557         // continue;
3558         // case '"':
3559         // if (token == TokenNameEncapsedString2) {
3560         // return;
3561         // }
3562         // token = TokenNameSTRING;
3563         // continue;
3564         // }
3565         // }
3566         // return;
3567         // }
3568         // }
3569         // }
3570
3571         // private void encaps_var() {
3572         // // T_VARIABLE
3573         // // | T_VARIABLE '[' encaps_var_offset ']'
3574         // // | T_VARIABLE T_OBJECT_OPERATOR T_STRING
3575         // // | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
3576         // // | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
3577         // // | T_CURLY_OPEN variable '}'
3578         // switch (token) {
3579         // case TokenNameVariable:
3580         // getNextToken();
3581         // if (token == TokenNameLBRACKET) {
3582         // getNextToken();
3583         // expr(); //encaps_var_offset();
3584         // if (token != TokenNameRBRACKET) {
3585         // throwSyntaxError("']' expected after variable.");
3586         // }
3587         // // scanner.encapsedStringStack.pop();
3588         // getNextToken();
3589         // // }
3590         // } else if (token == TokenNameMINUS_GREATER) {
3591         // getNextToken();
3592         // if (token != TokenNameIdentifier) {
3593         // throwSyntaxError("Identifier expected after '->'.");
3594         // }
3595         // // scanner.encapsedStringStack.pop();
3596         // getNextToken();
3597         // }
3598         // // else {
3599         // // // scanner.encapsedStringStack.pop();
3600         // // int tempToken = TokenNameSTRING;
3601         // // if (!scanner.encapsedStringStack.isEmpty()
3602         // // && (token == TokenNameEncapsedString0
3603         // // || token == TokenNameEncapsedString1
3604         // // || token == TokenNameEncapsedString2 || token ==
3605         // // TokenNameERROR)) {
3606         // // char encapsedChar = ((Character)
3607         // // scanner.encapsedStringStack.peek())
3608         // // .charValue();
3609         // // switch (token) {
3610         // // case TokenNameEncapsedString0 :
3611         // // if (encapsedChar == '`') {
3612         // // tempToken = TokenNameEncapsedString0;
3613         // // }
3614         // // break;
3615         // // case TokenNameEncapsedString1 :
3616         // // if (encapsedChar == '\'') {
3617         // // tempToken = TokenNameEncapsedString1;
3618         // // }
3619         // // break;
3620         // // case TokenNameEncapsedString2 :
3621         // // if (encapsedChar == '"') {
3622         // // tempToken = TokenNameEncapsedString2;
3623         // // }
3624         // // break;
3625         // // case TokenNameERROR :
3626         // // if (scanner.source[scanner.currentPosition - 1] == '\\') {
3627         // // scanner.currentPosition--;
3628         // // getNextToken();
3629         // // }
3630         // // break;
3631         // // }
3632         // // }
3633         // // token = tempToken;
3634         // // }
3635         // break;
3636         // case TokenNameDOLLAR_LBRACE:
3637         // getNextToken();
3638         // if (token == TokenNameDOLLAR_LBRACE) {
3639         // encaps_var();
3640         // } else if (token == TokenNameIdentifier) {
3641         // getNextToken();
3642         // if (token == TokenNameLBRACKET) {
3643         // getNextToken();
3644         // // if (token == TokenNameRBRACKET) {
3645         // // getNextToken();
3646         // // } else {
3647         // expr();
3648         // if (token != TokenNameRBRACKET) {
3649         // throwSyntaxError("']' expected after '${'.");
3650         // }
3651         // getNextToken();
3652         // // }
3653         // }
3654         // } else {
3655         // expr();
3656         // }
3657         // if (token != TokenNameRBRACE) {
3658         // throwSyntaxError("'}' expected.");
3659         // }
3660         // getNextToken();
3661         // break;
3662         // case TokenNameLBRACE_DOLLAR:
3663         // getNextToken();
3664         // if (token == TokenNameLBRACE_DOLLAR) {
3665         // encaps_var();
3666         // } else if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3667         // getNextToken();
3668         // if (token == TokenNameLBRACKET) {
3669         // getNextToken();
3670         // // if (token == TokenNameRBRACKET) {
3671         // // getNextToken();
3672         // // } else {
3673         // expr();
3674         // if (token != TokenNameRBRACKET) {
3675         // throwSyntaxError("']' expected.");
3676         // }
3677         // getNextToken();
3678         // // }
3679         // } else if (token == TokenNameMINUS_GREATER) {
3680         // getNextToken();
3681         // if (token != TokenNameIdentifier && token != TokenNameVariable) {
3682         // throwSyntaxError("String or Variable token expected.");
3683         // }
3684         // getNextToken();
3685         // if (token == TokenNameLBRACKET) {
3686         // getNextToken();
3687         // // if (token == TokenNameRBRACKET) {
3688         // // getNextToken();
3689         // // } else {
3690         // expr();
3691         // if (token != TokenNameRBRACKET) {
3692         // throwSyntaxError("']' expected after '${'.");
3693         // }
3694         // getNextToken();
3695         // // }
3696         // }
3697         // }
3698         // // if (token != TokenNameRBRACE) {
3699         // // throwSyntaxError("'}' expected after '{$'.");
3700         // // }
3701         // // // scanner.encapsedStringStack.pop();
3702         // // getNextToken();
3703         // } else {
3704         // expr();
3705         // if (token != TokenNameRBRACE) {
3706         // throwSyntaxError("'}' expected.");
3707         // }
3708         // // scanner.encapsedStringStack.pop();
3709         // getNextToken();
3710         // }
3711         // break;
3712         // }
3713         // }
3714
3715         // private void encaps_var_offset() {
3716         // // T_STRING
3717         // // | T_NUM_STRING
3718         // // | T_VARIABLE
3719         // switch (token) {
3720         // case TokenNameSTRING:
3721         // getNextToken();
3722         // break;
3723         // case TokenNameIntegerLiteral:
3724         // getNextToken();
3725         // break;
3726         // case TokenNameVariable:
3727         // getNextToken();
3728         // break;
3729         // case TokenNameIdentifier:
3730         // getNextToken();
3731         // break;
3732         // default:
3733         // throwSyntaxError("Variable or String token expected.");
3734         // break;
3735         // }
3736         // }
3737
3738         private void internal_functions_in_yacc() {
3739                 // int start = 0;
3740                 switch (token) {
3741                 // case TokenNameisset:
3742                 // // T_ISSET '(' isset_variables ')'
3743                 // getNextToken();
3744                 // if (token != TokenNameLPAREN) {
3745                 // throwSyntaxError("'(' expected after keyword 'isset'");
3746                 // }
3747                 // getNextToken();
3748                 // isset_variables();
3749                 // if (token != TokenNameRPAREN) {
3750                 // throwSyntaxError("')' expected after keyword 'isset'");
3751                 // }
3752                 // getNextToken();
3753                 // break;
3754                 // case TokenNameempty:
3755                 // // T_EMPTY '(' variable ')'
3756                 // getNextToken();
3757                 // if (token != TokenNameLPAREN) {
3758                 // throwSyntaxError("'(' expected after keyword 'empty'");
3759                 // }
3760                 // getNextToken();
3761                 // variable(false);
3762                 // if (token != TokenNameRPAREN) {
3763                 // throwSyntaxError("')' expected after keyword 'empty'");
3764                 // }
3765                 // getNextToken();
3766                 // break;
3767                 case TokenNameinclude:
3768                         // T_INCLUDE expr
3769                         checkFileName(token);
3770                         break;
3771                 case TokenNameinclude_once:
3772                         // T_INCLUDE_ONCE expr
3773                         checkFileName(token);
3774                         break;
3775                 case TokenNameeval:
3776                         // T_EVAL '(' expr ')'
3777                         getNextToken();
3778                         if (token != TokenNameLPAREN) {
3779                                 throwSyntaxError("'(' expected after keyword 'eval'");
3780                         }
3781                         getNextToken();
3782                         expr();
3783                         if (token != TokenNameRPAREN) {
3784                                 throwSyntaxError("')' expected after keyword 'eval'");
3785                         }
3786                         getNextToken();
3787                         break;
3788                 case TokenNamerequire:
3789                         // T_REQUIRE expr
3790                         checkFileName(token);
3791                         break;
3792                 case TokenNamerequire_once:
3793                         // T_REQUIRE_ONCE expr
3794                         checkFileName(token);
3795                         break;
3796                 }
3797         }
3798
3799         /**
3800          * Parse and check the include file name
3801          *
3802          * @param includeToken
3803          */
3804         private void checkFileName(int includeToken) {
3805                 // <include-token> expr
3806                 int start = scanner.getCurrentTokenStartPosition();
3807                 boolean hasLPAREN = false;
3808                 getNextToken();
3809                 if (token == TokenNameLPAREN) {
3810                         hasLPAREN = true;
3811                         getNextToken();
3812                 }
3813                 Expression expression = expr();
3814                 if (hasLPAREN) {
3815                         if (token == TokenNameRPAREN) {
3816                                 getNextToken();
3817                         } else {
3818                                 throwSyntaxError("')' expected for keyword '" + scanner.toStringAction(includeToken) + "'");
3819                         }
3820                 }
3821                 char[] currTokenSource = scanner.getCurrentTokenSource(start);
3822                 IFile file = null;
3823                 if (scanner.compilationUnit != null) {
3824                         IResource resource = scanner.compilationUnit.getResource();
3825                         if (resource != null && resource instanceof IFile) {
3826                                 file = (IFile) resource;
3827                         }
3828                 }
3829                 char[][] tokens;
3830                 tokens = new char[1][];
3831                 tokens[0] = currTokenSource;
3832
3833                 ImportReference impt = new ImportReference(tokens, currTokenSource, start, scanner.getCurrentTokenEndPosition(), false);
3834                 impt.declarationSourceEnd = impt.sourceEnd;
3835                 impt.declarationEnd = impt.declarationSourceEnd;
3836                 // endPosition is just before the ;
3837                 impt.declarationSourceStart = start;
3838                 includesList.add(impt);
3839
3840                 if (expression instanceof StringLiteral) {
3841                         StringLiteral literal = (StringLiteral) expression;
3842                         char[] includeName = literal.source();
3843                         if (includeName.length == 0) {
3844                                 reportSyntaxError("Empty filename after keyword '" + scanner.toStringAction(includeToken) + "'", literal.sourceStart,
3845                                                 literal.sourceStart + 1);
3846                         }
3847                         String includeNameString = new String(includeName);
3848                         if (literal instanceof StringLiteralDQ) {
3849                                 if (includeNameString.indexOf('$') >= 0) {
3850                                         // assuming that the filename contains a variable => no filename check
3851                                         return;
3852                                 }
3853                         }
3854                         if (includeNameString.startsWith("http://")) {
3855                                 // assuming external include location
3856                                 return;
3857                         }
3858                         if (file != null) {
3859                                 // check the filename:
3860                                 // System.out.println(new String(compilationUnit.getFileName())+" - "+
3861                                 // expression.toStringExpression());
3862                                 IProject project = file.getProject();
3863                                 if (project != null) {
3864                                         IPath path = PHPFileUtil.determineFilePath(includeNameString, file, project);
3865
3866                                         if (path == null) {
3867                                                 // SyntaxError: "File: << >> doesn't exist in project."
3868                                                 String[] args = { expression.toStringExpression(), project.getLocation().toString() };
3869                                                 problemReporter.phpIncludeNotExistWarning(args, literal.sourceStart, literal.sourceEnd, referenceContext,
3870                                                                 compilationUnit.compilationResult);
3871                                         } else {
3872                                                 try {
3873                                                         String filePath = path.toString();
3874                                                         String ext = file.getRawLocation().getFileExtension();
3875                                                         int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
3876
3877                                                         impt.tokens = CharOperation.splitOn('/', filePath.toCharArray(), 0, filePath.length() - fileExtensionLength);
3878                                                         impt.setFile(PHPFileUtil.createFile(path, project));
3879                                                 } catch (Exception e) {
3880                                                         // the file is outside of the workspace
3881                                                 }
3882                                         }
3883                                 }
3884                         }
3885                 }
3886         }
3887
3888         private void isset_variables() {
3889                 // variable
3890                 // | isset_variables ','
3891                 if (token == TokenNameRPAREN) {
3892                         throwSyntaxError("Variable expected after keyword 'isset'");
3893                 }
3894                 while (true) {
3895                         variable(true, false);
3896                         if (token == TokenNameCOMMA) {
3897                                 getNextToken();
3898                         } else {
3899                                 break;
3900                         }
3901                 }
3902         }
3903
3904         private boolean common_scalar() {
3905                 // common_scalar:
3906                 // T_LNUMBER
3907                 // | T_DNUMBER
3908                 // | T_CONSTANT_ENCAPSED_STRING
3909                 // | T_LINE
3910                 // | T_FILE
3911                 // | T_CLASS_C
3912                 // | T_METHOD_C
3913                 // | T_FUNC_C
3914                 switch (token) {
3915                 case TokenNameIntegerLiteral:
3916                         getNextToken();
3917                         return true;
3918                 case TokenNameDoubleLiteral:
3919                         getNextToken();
3920                         return true;
3921                 case TokenNameStringDoubleQuote:
3922                         getNextToken();
3923                         return true;
3924                 case TokenNameStringSingleQuote:
3925                         getNextToken();
3926                         return true;
3927                 case TokenNameStringInterpolated:
3928                         getNextToken();
3929                         return true;
3930                 case TokenNameFILE:
3931                         getNextToken();
3932                         return true;
3933                 case TokenNameLINE:
3934                         getNextToken();
3935                         return true;
3936                 case TokenNameCLASS_C:
3937                         getNextToken();
3938                         return true;
3939                 case TokenNameMETHOD_C:
3940                         getNextToken();
3941                         return true;
3942                 case TokenNameFUNC_C:
3943                         getNextToken();
3944                         return true;
3945                 }
3946                 return false;
3947         }
3948
3949         private void scalar() {
3950                 // scalar:
3951                 // T_STRING
3952                 // | T_STRING_VARNAME
3953                 // | class_constant
3954                 // | common_scalar
3955                 // | '"' encaps_list '"'
3956                 // | '\'' encaps_list '\''
3957                 // | T_START_HEREDOC encaps_list T_END_HEREDOC
3958                 throwSyntaxError("Not yet implemented (scalar).");
3959         }
3960
3961         private void static_scalar() {
3962                 // static_scalar: /* compile-time evaluated scalars */
3963                 // common_scalar
3964                 // | T_STRING
3965                 // | '+' static_scalar
3966                 // | '-' static_scalar
3967                 // | T_ARRAY '(' static_array_pair_list ')'
3968                 // | static_class_constant
3969                 if (common_scalar()) {
3970                         return;
3971                 }
3972                 switch (token) {
3973                 case TokenNameIdentifier:
3974                         getNextToken();
3975                         // static_class_constant:
3976                         // T_STRING T_PAAMAYIM_NEKUDOTAYIM T_STRING
3977                         if (token == TokenNamePAAMAYIM_NEKUDOTAYIM) {
3978                                 getNextToken();
3979                                 if (token == TokenNameIdentifier) {
3980                                         getNextToken();
3981                                 } else {
3982                                         throwSyntaxError("Identifier expected after '::' operator.");
3983                                 }
3984                         }
3985                         break;
3986                 case TokenNameEncapsedString0:
3987                         try {
3988                                 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3989                                 while (scanner.currentCharacter != '`') {
3990                                         if (scanner.currentCharacter == '\\') {
3991                                                 scanner.currentPosition++;
3992                                         }
3993                                         scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3994                                 }
3995                                 getNextToken();
3996                         } catch (IndexOutOfBoundsException e) {
3997                                 throwSyntaxError("'`' expected at end of static string.");
3998                         }
3999                         break;
4000                 // case TokenNameEncapsedString1:
4001                 // try {
4002                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
4003                 // while (scanner.currentCharacter != '\'') {
4004                 // if (scanner.currentCharacter == '\\') {
4005                 // scanner.currentPosition++;
4006                 // }
4007                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
4008                 // }
4009                 // getNextToken();
4010                 // } catch (IndexOutOfBoundsException e) {
4011                 // throwSyntaxError("'\'' expected at end of static string.");
4012                 // }
4013                 // break;
4014                 // case TokenNameEncapsedString2:
4015                 // try {
4016                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
4017                 // while (scanner.currentCharacter != '"') {
4018                 // if (scanner.currentCharacter == '\\') {
4019                 // scanner.currentPosition++;
4020                 // }
4021                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
4022                 // }
4023                 // getNextToken();
4024                 // } catch (IndexOutOfBoundsException e) {
4025                 // throwSyntaxError("'\"' expected at end of static string.");
4026                 // }
4027                 // break;
4028                 case TokenNameStringSingleQuote:
4029                         getNextToken();
4030                         break;
4031                 case TokenNameStringDoubleQuote:
4032                         getNextToken();
4033                         break;
4034                 case TokenNamePLUS:
4035                         getNextToken();
4036                         static_scalar();
4037                         break;
4038                 case TokenNameMINUS:
4039                         getNextToken();
4040                         static_scalar();
4041                         break;
4042                 case TokenNamearray:
4043                         getNextToken();
4044                         if (token != TokenNameLPAREN) {
4045                                 throwSyntaxError("'(' expected after keyword 'array'");
4046                         }
4047                         getNextToken();
4048                         if (token == TokenNameRPAREN) {
4049                                 getNextToken();
4050                                 break;
4051                         }
4052                         non_empty_static_array_pair_list();
4053                         if (token != TokenNameRPAREN) {
4054                                 throwSyntaxError("')' or ',' expected after keyword 'array'");
4055                         }
4056                         getNextToken();
4057                         break;
4058                 // case TokenNamenull :
4059                 // getNextToken();
4060                 // break;
4061                 // case TokenNamefalse :
4062                 // getNextToken();
4063                 // break;
4064                 // case TokenNametrue :
4065                 // getNextToken();
4066                 // break;
4067                 default:
4068                         throwSyntaxError("Static scalar/constant expected.");
4069                 }
4070         }
4071
4072         private void non_empty_static_array_pair_list() {
4073                 // non_empty_static_array_pair_list:
4074                 // non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW
4075                 // static_scalar
4076                 // | non_empty_static_array_pair_list ',' static_scalar
4077                 // | static_scalar T_DOUBLE_ARROW static_scalar
4078                 // | static_scalar
4079                 while (true) {
4080                         static_scalar();
4081                         if (token == TokenNameEQUAL_GREATER) {
4082                                 getNextToken();
4083                                 static_scalar();
4084                         }
4085                         if (token != TokenNameCOMMA) {
4086                                 break;
4087                         }
4088                         getNextToken();
4089                         if (token == TokenNameRPAREN) {
4090                                 break;
4091                         }
4092                 }
4093         }
4094
4095         // public void reportSyntaxError() { //int act, int currentKind, int
4096         // // stateStackTop) {
4097         // /* remember current scanner position */
4098         // int startPos = scanner.startPosition;
4099         // int currentPos = scanner.currentPosition;
4100         //
4101         // this.checkAndReportBracketAnomalies(problemReporter());
4102         // /* reset scanner where it was */
4103         // scanner.startPosition = startPos;
4104         // scanner.currentPosition = currentPos;
4105         // }
4106
4107         public static final int RoundBracket = 0;
4108
4109         public static final int SquareBracket = 1;
4110
4111         public static final int CurlyBracket = 2;
4112
4113         public static final int BracketKinds = 3;
4114
4115         protected int[] nestedMethod; // the ptr is nestedType
4116
4117         protected int nestedType, dimensions;
4118
4119         // variable set stack
4120         final static int VariableStackIncrement = 10;
4121
4122         HashMap fTypeVariables = null;
4123
4124         HashMap fMethodVariables = null;
4125
4126         ArrayList fStackUnassigned = new ArrayList();
4127
4128         // ast stack
4129         final static int AstStackIncrement = 100;
4130
4131         protected int astPtr;
4132
4133         protected ASTNode[] astStack = new ASTNode[AstStackIncrement];
4134
4135         protected int astLengthPtr;
4136
4137         protected int[] astLengthStack;
4138
4139         ASTNode[] noAstNodes = new ASTNode[AstStackIncrement];
4140
4141         public CompilationUnitDeclaration compilationUnit; /*
4142                                                                                                                                                                                                                          * the result from parse()
4143                                                                                                                                                                                                                          */
4144
4145         protected ReferenceContext referenceContext;
4146
4147         protected ProblemReporter problemReporter;
4148
4149         protected CompilerOptions options;
4150
4151         private ArrayList includesList;
4152
4153         // protected CompilationResult compilationResult;
4154         /**
4155          * Returns this parser's problem reporter initialized with its reference
4156          * context. Also it is assumed that a problem is going to be reported, so
4157          * initializes the compilation result's line positions.
4158          */
4159         public ProblemReporter problemReporter() {
4160                 if (scanner.recordLineSeparator) {
4161                         compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
4162                 }
4163                 problemReporter.referenceContext = referenceContext;
4164                 return problemReporter;
4165         }
4166
4167         /*
4168          * Reconsider the entire source looking for inconsistencies in {} () []
4169          */
4170         // public boolean checkAndReportBracketAnomalies(ProblemReporter
4171         // problemReporter) {
4172         // scanner.wasAcr = false;
4173         // boolean anomaliesDetected = false;
4174         // try {
4175         // char[] source = scanner.source;
4176         // int[] leftCount = { 0, 0, 0 };
4177         // int[] rightCount = { 0, 0, 0 };
4178         // int[] depths = { 0, 0, 0 };
4179         // int[][] leftPositions = new int[][] { new int[10], new int[10], new int[10]
4180         // };
4181         // int[][] leftDepths = new int[][] { new int[10], new int[10], new int[10] };
4182         // int[][] rightPositions = new int[][] { new int[10], new int[10], new
4183         // int[10] };
4184         // int[][] rightDepths = new int[][] { new int[10], new int[10], new int[10]
4185         // };
4186         // scanner.currentPosition = scanner.initialPosition; //starting
4187         // // point
4188         // // (first-zero-based
4189         // // char)
4190         // while (scanner.currentPosition < scanner.eofPosition) { //loop for
4191         // // jumping
4192         // // over
4193         // // comments
4194         // try {
4195         // // ---------Consume white space and handles
4196         // // startPosition---------
4197         // boolean isWhiteSpace;
4198         // do {
4199         // scanner.startPosition = scanner.currentPosition;
4200         // // if (((scanner.currentCharacter =
4201         // // source[scanner.currentPosition++]) == '\\') &&
4202         // // (source[scanner.currentPosition] == 'u')) {
4203         // // isWhiteSpace = scanner.jumpOverUnicodeWhiteSpace();
4204         // // } else {
4205         // if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') ||
4206         // (scanner.currentCharacter == '\n'))) {
4207         // if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4208         // // only record line positions we have not
4209         // // recorded yet
4210         // scanner.pushLineSeparator();
4211         // }
4212         // }
4213         // isWhiteSpace = CharOperation.isWhitespace(scanner.currentCharacter);
4214         // // }
4215         // } while (isWhiteSpace && (scanner.currentPosition < scanner.eofPosition));
4216         // // -------consume token until } is found---------
4217         // switch (scanner.currentCharacter) {
4218         // case '{': {
4219         // int index = leftCount[CurlyBracket]++;
4220         // if (index == leftPositions[CurlyBracket].length) {
4221         // System.arraycopy(leftPositions[CurlyBracket], 0,
4222         // (leftPositions[CurlyBracket] = new int[index * 2]), 0, index);
4223         // System.arraycopy(leftDepths[CurlyBracket], 0, (leftDepths[CurlyBracket] =
4224         // new int[index * 2]), 0, index);
4225         // }
4226         // leftPositions[CurlyBracket][index] = scanner.startPosition;
4227         // leftDepths[CurlyBracket][index] = depths[CurlyBracket]++;
4228         // }
4229         // break;
4230         // case '}': {
4231         // int index = rightCount[CurlyBracket]++;
4232         // if (index == rightPositions[CurlyBracket].length) {
4233         // System.arraycopy(rightPositions[CurlyBracket], 0,
4234         // (rightPositions[CurlyBracket] = new int[index * 2]), 0, index);
4235         // System.arraycopy(rightDepths[CurlyBracket], 0, (rightDepths[CurlyBracket] =
4236         // new int[index * 2]), 0, index);
4237         // }
4238         // rightPositions[CurlyBracket][index] = scanner.startPosition;
4239         // rightDepths[CurlyBracket][index] = --depths[CurlyBracket];
4240         // }
4241         // break;
4242         // case '(': {
4243         // int index = leftCount[RoundBracket]++;
4244         // if (index == leftPositions[RoundBracket].length) {
4245         // System.arraycopy(leftPositions[RoundBracket], 0,
4246         // (leftPositions[RoundBracket] = new int[index * 2]), 0, index);
4247         // System.arraycopy(leftDepths[RoundBracket], 0, (leftDepths[RoundBracket] =
4248         // new int[index * 2]), 0, index);
4249         // }
4250         // leftPositions[RoundBracket][index] = scanner.startPosition;
4251         // leftDepths[RoundBracket][index] = depths[RoundBracket]++;
4252         // }
4253         // break;
4254         // case ')': {
4255         // int index = rightCount[RoundBracket]++;
4256         // if (index == rightPositions[RoundBracket].length) {
4257         // System.arraycopy(rightPositions[RoundBracket], 0,
4258         // (rightPositions[RoundBracket] = new int[index * 2]), 0, index);
4259         // System.arraycopy(rightDepths[RoundBracket], 0, (rightDepths[RoundBracket] =
4260         // new int[index * 2]), 0, index);
4261         // }
4262         // rightPositions[RoundBracket][index] = scanner.startPosition;
4263         // rightDepths[RoundBracket][index] = --depths[RoundBracket];
4264         // }
4265         // break;
4266         // case '[': {
4267         // int index = leftCount[SquareBracket]++;
4268         // if (index == leftPositions[SquareBracket].length) {
4269         // System.arraycopy(leftPositions[SquareBracket], 0,
4270         // (leftPositions[SquareBracket] = new int[index * 2]), 0, index);
4271         // System.arraycopy(leftDepths[SquareBracket], 0, (leftDepths[SquareBracket] =
4272         // new int[index * 2]), 0, index);
4273         // }
4274         // leftPositions[SquareBracket][index] = scanner.startPosition;
4275         // leftDepths[SquareBracket][index] = depths[SquareBracket]++;
4276         // }
4277         // break;
4278         // case ']': {
4279         // int index = rightCount[SquareBracket]++;
4280         // if (index == rightPositions[SquareBracket].length) {
4281         // System.arraycopy(rightPositions[SquareBracket], 0,
4282         // (rightPositions[SquareBracket] = new int[index * 2]), 0, index);
4283         // System.arraycopy(rightDepths[SquareBracket], 0, (rightDepths[SquareBracket]
4284         // = new int[index * 2]), 0, index);
4285         // }
4286         // rightPositions[SquareBracket][index] = scanner.startPosition;
4287         // rightDepths[SquareBracket][index] = --depths[SquareBracket];
4288         // }
4289         // break;
4290         // case '\'': {
4291         // if (scanner.getNextChar('\\')) {
4292         // scanner.scanEscapeCharacter();
4293         // } else { // consume next character
4294         // scanner.unicodeAsBackSlash = false;
4295         // // if (((scanner.currentCharacter =
4296         // // source[scanner.currentPosition++]) ==
4297         // // '\\') &&
4298         // // (source[scanner.currentPosition] ==
4299         // // 'u')) {
4300         // // scanner.getNextUnicodeChar();
4301         // // } else {
4302         // if (scanner.withoutUnicodePtr != 0) {
4303         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4304         // scanner.currentCharacter;
4305         // }
4306         // // }
4307         // }
4308         // scanner.getNextChar('\'');
4309         // break;
4310         // }
4311         // case '"':
4312         // // consume next character
4313         // scanner.unicodeAsBackSlash = false;
4314         // // if (((scanner.currentCharacter =
4315         // // source[scanner.currentPosition++]) == '\\') &&
4316         // // (source[scanner.currentPosition] == 'u')) {
4317         // // scanner.getNextUnicodeChar();
4318         // // } else {
4319         // if (scanner.withoutUnicodePtr != 0) {
4320         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4321         // scanner.currentCharacter;
4322         // }
4323         // // }
4324         // while (scanner.currentCharacter != '"') {
4325         // if (scanner.currentCharacter == '\r') {
4326         // if (source[scanner.currentPosition] == '\n')
4327         // scanner.currentPosition++;
4328         // break; // the string cannot go further that
4329         // // the line
4330         // }
4331         // if (scanner.currentCharacter == '\n') {
4332         // break; // the string cannot go further that
4333         // // the line
4334         // }
4335         // if (scanner.currentCharacter == '\\') {
4336         // scanner.scanEscapeCharacter();
4337         // }
4338         // // consume next character
4339         // scanner.unicodeAsBackSlash = false;
4340         // // if (((scanner.currentCharacter =
4341         // // source[scanner.currentPosition++]) == '\\')
4342         // // && (source[scanner.currentPosition] == 'u'))
4343         // // {
4344         // // scanner.getNextUnicodeChar();
4345         // // } else {
4346         // if (scanner.withoutUnicodePtr != 0) {
4347         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4348         // scanner.currentCharacter;
4349         // }
4350         // // }
4351         // }
4352         // break;
4353         // case '/': {
4354         // int test;
4355         // if ((test = scanner.getNextChar('/', '*')) == 0) { //line
4356         // // comment
4357         // //get the next char
4358         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4359         // '\\')
4360         // && (source[scanner.currentPosition] == 'u')) {
4361         // //-------------unicode traitement
4362         // // ------------
4363         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4364         // scanner.currentPosition++;
4365         // while (source[scanner.currentPosition] == 'u') {
4366         // scanner.currentPosition++;
4367         // }
4368         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4369         // 15 || c1 < 0
4370         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4371         // || c2 < 0
4372         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4373         // || c3 < 0
4374         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4375         // || c4 < 0) { //error
4376         // // don't
4377         // // care of the
4378         // // value
4379         // scanner.currentCharacter = 'A';
4380         // } //something different from \n and \r
4381         // else {
4382         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4383         // }
4384         // }
4385         // while (scanner.currentCharacter != '\r' && scanner.currentCharacter !=
4386         // '\n') {
4387         // //get the next char
4388         // scanner.startPosition = scanner.currentPosition;
4389         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4390         // '\\')
4391         // && (source[scanner.currentPosition] == 'u')) {
4392         // //-------------unicode traitement
4393         // // ------------
4394         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4395         // scanner.currentPosition++;
4396         // while (source[scanner.currentPosition] == 'u') {
4397         // scanner.currentPosition++;
4398         // }
4399         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4400         // 15 || c1 < 0
4401         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4402         // || c2 < 0
4403         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4404         // || c3 < 0
4405         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4406         // || c4 < 0) { //error
4407         // // don't
4408         // // care of the
4409         // // value
4410         // scanner.currentCharacter = 'A';
4411         // } //something different from \n
4412         // // and \r
4413         // else {
4414         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4415         // }
4416         // }
4417         // }
4418         // if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') ||
4419         // (scanner.currentCharacter == '\n'))) {
4420         // if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4421         // // only record line positions we
4422         // // have not recorded yet
4423         // scanner.pushLineSeparator();
4424         // if (this.scanner.taskTags != null) {
4425         // this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(),
4426         // this.scanner
4427         // .getCurrentTokenEndPosition());
4428         // }
4429         // }
4430         // }
4431         // break;
4432         // }
4433         // if (test > 0) { //traditional and annotation
4434         // // comment
4435         // boolean star = false;
4436         // // consume next character
4437         // scanner.unicodeAsBackSlash = false;
4438         // // if (((scanner.currentCharacter =
4439         // // source[scanner.currentPosition++]) ==
4440         // // '\\') &&
4441         // // (source[scanner.currentPosition] ==
4442         // // 'u')) {
4443         // // scanner.getNextUnicodeChar();
4444         // // } else {
4445         // if (scanner.withoutUnicodePtr != 0) {
4446         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4447         // scanner.currentCharacter;
4448         // }
4449         // // }
4450         // if (scanner.currentCharacter == '*') {
4451         // star = true;
4452         // }
4453         // //get the next char
4454         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4455         // '\\')
4456         // && (source[scanner.currentPosition] == 'u')) {
4457         // //-------------unicode traitement
4458         // // ------------
4459         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4460         // scanner.currentPosition++;
4461         // while (source[scanner.currentPosition] == 'u') {
4462         // scanner.currentPosition++;
4463         // }
4464         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4465         // 15 || c1 < 0
4466         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4467         // || c2 < 0
4468         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4469         // || c3 < 0
4470         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4471         // || c4 < 0) { //error
4472         // // don't
4473         // // care of the
4474         // // value
4475         // scanner.currentCharacter = 'A';
4476         // } //something different from * and /
4477         // else {
4478         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4479         // }
4480         // }
4481         // //loop until end of comment */
4482         // while ((scanner.currentCharacter != '/') || (!star)) {
4483         // star = scanner.currentCharacter == '*';
4484         // //get next char
4485         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4486         // '\\')
4487         // && (source[scanner.currentPosition] == 'u')) {
4488         // //-------------unicode traitement
4489         // // ------------
4490         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4491         // scanner.currentPosition++;
4492         // while (source[scanner.currentPosition] == 'u') {
4493         // scanner.currentPosition++;
4494         // }
4495         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4496         // 15 || c1 < 0
4497         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4498         // || c2 < 0
4499         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4500         // || c3 < 0
4501         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4502         // || c4 < 0) { //error
4503         // // don't
4504         // // care of the
4505         // // value
4506         // scanner.currentCharacter = 'A';
4507         // } //something different from * and
4508         // // /
4509         // else {
4510         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4511         // }
4512         // }
4513         // }
4514         // if (this.scanner.taskTags != null) {
4515         // this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(),
4516         // this.scanner.getCurrentTokenEndPosition());
4517         // }
4518         // break;
4519         // }
4520         // break;
4521         // }
4522         // default:
4523         // if (Scanner.isPHPIdentifierStart(scanner.currentCharacter)) {
4524         // scanner.scanIdentifierOrKeyword(false);
4525         // break;
4526         // }
4527         // if (Character.isDigit(scanner.currentCharacter)) {
4528         // scanner.scanNumber(false);
4529         // break;
4530         // }
4531         // }
4532         // //-----------------end switch while
4533         // // try--------------------
4534         // } catch (IndexOutOfBoundsException e) {
4535         // break; // read until EOF
4536         // } catch (InvalidInputException e) {
4537         // return false; // no clue
4538         // }
4539         // }
4540         // if (scanner.recordLineSeparator) {
4541         // compilationUnit.compilationResult.lineSeparatorPositions =
4542         // scanner.getLineEnds();
4543         // }
4544         // // check placement anomalies against other kinds of brackets
4545         // for (int kind = 0; kind < BracketKinds; kind++) {
4546         // for (int leftIndex = leftCount[kind] - 1; leftIndex >= 0; leftIndex--) {
4547         // int start = leftPositions[kind][leftIndex]; // deepest
4548         // // first
4549         // // find matching closing bracket
4550         // int depth = leftDepths[kind][leftIndex];
4551         // int end = -1;
4552         // for (int i = 0; i < rightCount[kind]; i++) {
4553         // int pos = rightPositions[kind][i];
4554         // // want matching bracket further in source with same
4555         // // depth
4556         // if ((pos > start) && (depth == rightDepths[kind][i])) {
4557         // end = pos;
4558         // break;
4559         // }
4560         // }
4561         // if (end < 0) { // did not find a good closing match
4562         // problemReporter.unmatchedBracket(start, referenceContext,
4563         // compilationUnit.compilationResult);
4564         // return true;
4565         // }
4566         // // check if even number of opening/closing other brackets
4567         // // in between this pair of brackets
4568         // int balance = 0;
4569         // for (int otherKind = 0; (balance == 0) && (otherKind < BracketKinds);
4570         // otherKind++) {
4571         // for (int i = 0; i < leftCount[otherKind]; i++) {
4572         // int pos = leftPositions[otherKind][i];
4573         // if ((pos > start) && (pos < end))
4574         // balance++;
4575         // }
4576         // for (int i = 0; i < rightCount[otherKind]; i++) {
4577         // int pos = rightPositions[otherKind][i];
4578         // if ((pos > start) && (pos < end))
4579         // balance--;
4580         // }
4581         // if (balance != 0) {
4582         // problemReporter.unmatchedBracket(start, referenceContext,
4583         // compilationUnit.compilationResult); //bracket
4584         // // anomaly
4585         // return true;
4586         // }
4587         // }
4588         // }
4589         // // too many opening brackets ?
4590         // for (int i = rightCount[kind]; i < leftCount[kind]; i++) {
4591         // anomaliesDetected = true;
4592         // problemReporter.unmatchedBracket(leftPositions[kind][leftCount[kind] - i -
4593         // 1], referenceContext,
4594         // compilationUnit.compilationResult);
4595         // }
4596         // // too many closing brackets ?
4597         // for (int i = leftCount[kind]; i < rightCount[kind]; i++) {
4598         // anomaliesDetected = true;
4599         // problemReporter.unmatchedBracket(rightPositions[kind][i], referenceContext,
4600         // compilationUnit.compilationResult);
4601         // }
4602         // if (anomaliesDetected)
4603         // return true;
4604         // }
4605         // return anomaliesDetected;
4606         // } catch (ArrayStoreException e) { // jdk1.2.2 jit bug
4607         // return anomaliesDetected;
4608         // } catch (NullPointerException e) { // jdk1.2.2 jit bug
4609         // return anomaliesDetected;
4610         // }
4611         // }
4612         protected void pushOnAstLengthStack(int pos) {
4613                 try {
4614                         astLengthStack[++astLengthPtr] = pos;
4615                 } catch (IndexOutOfBoundsException e) {
4616                         int oldStackLength = astLengthStack.length;
4617                         int[] oldPos = astLengthStack;
4618                         astLengthStack = new int[oldStackLength + StackIncrement];
4619                         System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4620                         astLengthStack[astLengthPtr] = pos;
4621                 }
4622         }
4623
4624         protected void pushOnAstStack(ASTNode node) {
4625                 /*
4626                  * add a new obj on top of the ast stack
4627                  */
4628                 try {
4629                         astStack[++astPtr] = node;
4630                 } catch (IndexOutOfBoundsException e) {
4631                         int oldStackLength = astStack.length;
4632                         ASTNode[] oldStack = astStack;
4633                         astStack = new ASTNode[oldStackLength + AstStackIncrement];
4634                         System.arraycopy(oldStack, 0, astStack, 0, oldStackLength);
4635                         astPtr = oldStackLength;
4636                         astStack[astPtr] = node;
4637                 }
4638                 try {
4639                         astLengthStack[++astLengthPtr] = 1;
4640                 } catch (IndexOutOfBoundsException e) {
4641                         int oldStackLength = astLengthStack.length;
4642                         int[] oldPos = astLengthStack;
4643                         astLengthStack = new int[oldStackLength + AstStackIncrement];
4644                         System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4645                         astLengthStack[astLengthPtr] = 1;
4646                 }
4647         }
4648
4649         protected void resetModifiers() {
4650                 this.modifiers = AccDefault;
4651                 this.modifiersSourceStart = -1; // <-- see comment into
4652                 // modifiersFlag(int)
4653                 this.scanner.commentPtr = -1;
4654         }
4655
4656         protected void consumePackageDeclarationName(IFile file) {
4657                 // create a package name similar to java package names
4658                 String projectPath = ProjectPrefUtil.getDocumentRoot(file.getProject()).toString();
4659                 String filePath = file.getRawLocation().toString();
4660                 String ext = file.getRawLocation().getFileExtension();
4661                 int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
4662                 ImportReference impt;
4663                 char[][] tokens;
4664                 if (filePath.startsWith(projectPath)) {
4665                         tokens = CharOperation
4666                                         .splitOn('/', filePath.toCharArray(), projectPath.length() + 1, filePath.length() - fileExtensionLength);
4667                 } else {
4668                         String name = file.getName();
4669                         tokens = new char[1][];
4670                         tokens[0] = name.substring(0, name.length() - fileExtensionLength).toCharArray();
4671                 }
4672
4673                 this.compilationUnit.currentPackage = impt = new ImportReference(tokens, new char[0], 0, 0, true);
4674
4675                 impt.declarationSourceStart = 0;
4676                 impt.declarationSourceEnd = 0;
4677                 impt.declarationEnd = 0;
4678                 // endPosition is just before the ;
4679
4680         }
4681
4682         public final static String[] GLOBALS = { "$this", "$_COOKIE", "$_ENV", "$_FILES", "$_GET", "$GLOBALS", "$_POST", "$_REQUEST",
4683                         "$_SESSION", "$_SERVER" };
4684
4685         /**
4686          *
4687          */
4688         private void pushFunctionVariableSet() {
4689                 HashSet set = new HashSet();
4690                 if (fStackUnassigned.isEmpty()) {
4691                         for (int i = 0; i < GLOBALS.length; i++) {
4692                                 set.add(GLOBALS[i]);
4693                         }
4694                 }
4695                 fStackUnassigned.add(set);
4696         }
4697
4698         private void pushIfVariableSet() {
4699                 if (!fStackUnassigned.isEmpty()) {
4700                         HashSet set = new HashSet();
4701                         fStackUnassigned.add(set);
4702                 }
4703         }
4704
4705         private HashSet removeIfVariableSet() {
4706                 if (!fStackUnassigned.isEmpty()) {
4707                         return (HashSet) fStackUnassigned.remove(fStackUnassigned.size() - 1);
4708                 }
4709                 return null;
4710         }
4711
4712         /**
4713          * Returns the <i>set of assigned variables </i> returns null if no Set is
4714          * defined at the current scanner position
4715          */
4716         private HashSet peekVariableSet() {
4717                 if (!fStackUnassigned.isEmpty()) {
4718                         return (HashSet) fStackUnassigned.get(fStackUnassigned.size() - 1);
4719                 }
4720                 return null;
4721         }
4722
4723         /**
4724          * add the current identifier source to the <i>set of assigned variables </i>
4725          *
4726          * @param set
4727          */
4728         private void addVariableSet(HashSet set) {
4729                 if (set != null) {
4730                         set.add(new String(scanner.getCurrentTokenSource()));
4731                 }
4732         }
4733
4734         /**
4735          * add the current identifier source to the <i>set of assigned variables </i>
4736          *
4737          */
4738         private void addVariableSet() {
4739                 HashSet set = peekVariableSet();
4740                 if (set != null) {
4741                         set.add(new String(scanner.getCurrentTokenSource()));
4742                 }
4743         }
4744
4745         /**
4746          * add the current identifier source to the <i>set of assigned variables </i>
4747          *
4748          */
4749         private void addVariableSet(char[] token) {
4750                 HashSet set = peekVariableSet();
4751                 if (set != null) {
4752                         set.add(new String(token));
4753                 }
4754         }
4755
4756         /**
4757          * check if the current identifier source is in the <i>set of assigned
4758          * variables </i> Returns true, if no set is defined for the current scanner
4759          * position
4760          *
4761          */
4762         private boolean containsVariableSet() {
4763                 return containsVariableSet(scanner.getCurrentTokenSource());
4764         }
4765
4766         private boolean containsVariableSet(char[] token) {
4767
4768                 if (!fStackUnassigned.isEmpty()) {
4769                         HashSet set;
4770                         String str = new String(token);
4771                         for (int i = 0; i < fStackUnassigned.size(); i++) {
4772                                 set = (HashSet) fStackUnassigned.get(i);
4773                                 if (set.contains(str)) {
4774                                         return true;
4775                                 }
4776                         }
4777                         return false;
4778                 }
4779                 return true;
4780         }
4781 }