a lot of bugfixes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / test / PHPParser.jj
1
2 options {
3   LOOKAHEAD = 1;
4   CHOICE_AMBIGUITY_CHECK = 2;
5   OTHER_AMBIGUITY_CHECK = 1;
6   STATIC = true;
7   DEBUG_PARSER = false;
8   DEBUG_LOOKAHEAD = false;
9   DEBUG_TOKEN_MANAGER = false;
10   OPTIMIZE_TOKEN_MANAGER = false;
11   ERROR_REPORTING = true;
12   JAVA_UNICODE_ESCAPE = false;
13   UNICODE_INPUT = false;
14   IGNORE_CASE = true;
15   USER_TOKEN_MANAGER = false;
16   USER_CHAR_STREAM = false;
17   BUILD_PARSER = true;
18   BUILD_TOKEN_MANAGER = true;
19   SANITY_CHECK = true;
20   FORCE_LA_CHECK = false;
21   COMMON_TOKEN_ACTION = true;
22 }
23
24 PARSER_BEGIN(PHPParser)
25 package test;
26
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.resources.IMarker;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.ui.texteditor.MarkerUtilities;
31 import org.eclipse.jface.preference.IPreferenceStore;
32
33 import java.util.Hashtable;
34 import java.util.ArrayList;
35 import java.io.StringReader;
36 import java.io.*;
37 import java.text.MessageFormat;
38
39 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
40 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
41 import net.sourceforge.phpdt.internal.compiler.ast.*;
42 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
43 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
44 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
45 import net.sourceforge.phpdt.internal.corext.Assert;
46
47 /**
48  * A new php parser.
49  * This php parser is inspired by the Java 1.2 grammar example
50  * given with JavaCC. You can get JavaCC at http://www.webgain.com
51  * You can test the parser with the PHPParserTestCase2.java
52  * @author Matthieu Casanova
53  */
54 public final class PHPParser extends PHPParserSuperclass {
55
56 //todo : fix the variables names bug
57 //todo : handle tilde operator
58
59
60   /** The current segment. */
61   private static OutlineableWithChildren currentSegment;
62
63   private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
64   private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
65   static PHPOutlineInfo outlineInfo;
66
67   /** The error level of the current ParseException. */
68   private static int errorLevel = ERROR;
69   /** The message of the current ParseException. If it's null it's because the parse exception wasn't handled */
70   private static String errorMessage;
71
72   private static int errorStart = -1;
73   private static int errorEnd = -1;
74   private static PHPDocument phpDocument;
75
76   private static final String SYNTAX_ERROR_CHAR = "syntax error";
77   /**
78    * The point where html starts.
79    * It will be used by the token manager to create HTMLCode objects
80    */
81   public static int htmlStart;
82
83   //ast stack
84   private final static int AstStackIncrement = 100;
85   /** The stack of node. */
86   private static AstNode[] nodes;
87   /** The cursor in expression stack. */
88   private static int nodePtr;
89
90   private static final boolean PARSER_DEBUG = true;
91
92   public final void setFileToParse(final IFile fileToParse) {
93     PHPParser.fileToParse = fileToParse;
94   }
95
96   public PHPParser() {
97   }
98
99   public PHPParser(final IFile fileToParse) {
100     this(new StringReader(""));
101     PHPParser.fileToParse = fileToParse;
102   }
103
104   public static final void phpParserTester(final String strEval) throws ParseException {
105     final StringReader stream = new StringReader(strEval);
106     if (jj_input_stream == null) {
107       jj_input_stream = new SimpleCharStream(stream, 1, 1);
108     }
109     ReInit(new StringReader(strEval));
110     init();
111     phpDocument = new PHPDocument(null,"_root".toCharArray());
112     currentSegment = phpDocument;
113     outlineInfo = new PHPOutlineInfo(null, currentSegment);
114     PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING);
115     phpTest();
116   }
117
118   public static final void htmlParserTester(final File fileName) throws FileNotFoundException, ParseException {
119     final Reader stream = new FileReader(fileName);
120     if (jj_input_stream == null) {
121       jj_input_stream = new SimpleCharStream(stream, 1, 1);
122     }
123     ReInit(stream);
124     init();
125     phpDocument = new PHPDocument(null,"_root".toCharArray());
126     currentSegment = phpDocument;
127     outlineInfo = new PHPOutlineInfo(null, currentSegment);
128     phpFile();
129   }
130
131   public static final void htmlParserTester(final String strEval) throws ParseException {
132     final StringReader stream = new StringReader(strEval);
133     if (jj_input_stream == null) {
134       jj_input_stream = new SimpleCharStream(stream, 1, 1);
135     }
136     ReInit(stream);
137     init();
138     phpDocument = new PHPDocument(null,"_root".toCharArray());
139     currentSegment = phpDocument;
140     outlineInfo = new PHPOutlineInfo(null, currentSegment);
141     phpFile();
142   }
143
144   /**
145    * Reinitialize the parser.
146    */
147   private static final void init() {
148     nodes = new AstNode[AstStackIncrement];
149     nodePtr = -1;
150     htmlStart = 0;
151   }
152
153   /**
154    * Add an php node on the stack.
155    * @param node the node that will be added to the stack
156    */
157   private static final void pushOnAstNodes(final AstNode node) {
158     try {
159       nodes[++nodePtr] = node;
160     } catch (IndexOutOfBoundsException e) {
161       final int oldStackLength = nodes.length;
162       final AstNode[] oldStack = nodes;
163       nodes = new AstNode[oldStackLength + AstStackIncrement];
164       System.arraycopy(oldStack, 0, nodes, 0, oldStackLength);
165       nodePtr = oldStackLength;
166       nodes[nodePtr] = node;
167     }
168   }
169
170   public final PHPOutlineInfo parseInfo(final Object parent, final String s) {
171     phpDocument = new PHPDocument(parent,"_root".toCharArray());
172     currentSegment = phpDocument;
173     outlineInfo = new PHPOutlineInfo(parent, currentSegment);
174     final StringReader stream = new StringReader(s);
175     if (jj_input_stream == null) {
176       jj_input_stream = new SimpleCharStream(stream, 1, 1);
177     }
178     ReInit(stream);
179     init();
180     try {
181       parse();
182       phpDocument.nodes = new AstNode[nodes.length];
183       System.arraycopy(nodes,0,phpDocument.nodes,0,nodes.length);
184       if (PHPeclipsePlugin.DEBUG) {
185         PHPeclipsePlugin.log(1,phpDocument.toString());
186       }
187     } catch (ParseException e) {
188       processParseException(e);
189     }
190     return outlineInfo;
191   }
192
193   private static void processParseExceptionDebug(final ParseException e) throws ParseException {
194     if (PARSER_DEBUG) {
195       throw e;
196     }
197     processParseException(e);
198   }
199   /**
200    * This method will process the parse exception.
201    * If the error message is null, the parse exception wasn't catched and a trace is written in the log
202    * @param e the ParseException
203    */
204   private static void processParseException(final ParseException e) {
205     if (errorMessage == null) {
206       PHPeclipsePlugin.log(e);
207       errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it";
208       errorStart = e.currentToken.sourceStart;
209       errorEnd   = e.currentToken.sourceEnd;
210     }
211     setMarker(e);
212     errorMessage = null;
213   //  if (PHPeclipsePlugin.DEBUG) PHPeclipsePlugin.log(e);
214   }
215
216   /**
217    * Create marker for the parse error.
218    * @param e the ParseException
219    */
220   private static void setMarker(final ParseException e) {
221     try {
222       if (errorStart == -1) {
223         setMarker(fileToParse,
224                   errorMessage,
225                   e.currentToken.sourceStart,
226                   e.currentToken.sourceEnd,
227                   errorLevel,
228                   "Line " + e.currentToken.beginLine);
229       } else {
230         setMarker(fileToParse,
231                   errorMessage,
232                   errorStart,
233                   errorEnd,
234                   errorLevel,
235                   "Line " + e.currentToken.beginLine);
236         errorStart = -1;
237         errorEnd = -1;
238       }
239     } catch (CoreException e2) {
240       PHPeclipsePlugin.log(e2);
241     }
242   }
243
244   private static void scanLine(final String output,
245                                final IFile file,
246                                final int indx,
247                                final int brIndx) throws CoreException {
248     String current;
249     final StringBuffer lineNumberBuffer = new StringBuffer(10);
250     char ch;
251     current = output.substring(indx, brIndx);
252
253     if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
254       final int onLine = current.indexOf("on line <b>");
255       if (onLine != -1) {
256         lineNumberBuffer.delete(0, lineNumberBuffer.length());
257         for (int i = onLine; i < current.length(); i++) {
258           ch = current.charAt(i);
259           if ('0' <= ch && '9' >= ch) {
260             lineNumberBuffer.append(ch);
261           }
262         }
263
264         final int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
265
266         final Hashtable attributes = new Hashtable();
267
268         current = current.replaceAll("\n", "");
269         current = current.replaceAll("<b>", "");
270         current = current.replaceAll("</b>", "");
271         MarkerUtilities.setMessage(attributes, current);
272
273         if (current.indexOf(PARSE_ERROR_STRING) != -1)
274           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
275         else if (current.indexOf(PARSE_WARNING_STRING) != -1)
276           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
277         else
278           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
279         MarkerUtilities.setLineNumber(attributes, lineNumber);
280         MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
281       }
282     }
283   }
284
285   public final void parse(final String s) {
286     final StringReader stream = new StringReader(s);
287     if (jj_input_stream == null) {
288       jj_input_stream = new SimpleCharStream(stream, 1, 1);
289     }
290     ReInit(stream);
291     init();
292     try {
293       parse();
294     } catch (ParseException e) {
295       processParseException(e);
296     }
297   }
298
299   /**
300    * Call the php parse command ( php -l -f &lt;filename&gt; )
301    * and create markers according to the external parser output
302    */
303   public static void phpExternalParse(final IFile file) {
304     final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
305     final String filename = file.getLocation().toString();
306
307     final String[] arguments = { filename };
308     final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
309     final String command = form.format(arguments);
310
311     final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
312
313     try {
314       // parse the buffer to find the errors and warnings
315       createMarkers(parserResult, file);
316     } catch (CoreException e) {
317       PHPeclipsePlugin.log(e);
318     }
319   }
320
321   /**
322    * Put a new html block in the stack.
323    */
324   public static final void createNewHTMLCode() {
325     final int currentPosition = token.sourceStart;
326     if (currentPosition == htmlStart ||
327           currentPosition > SimpleCharStream.currentBuffer.length()) {
328       return;
329     }
330     final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
331     pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition));
332   }
333
334   /** Create a new task. */
335   public static final void createNewTask() {
336     final int currentPosition = token.sourceStart;
337     final String  todo = SimpleCharStream.currentBuffer.substring(currentPosition-3,
338                                                                   SimpleCharStream.currentBuffer.indexOf("\n",
339                                                                                                          currentPosition)-1);
340     if (!PARSER_DEBUG) {
341       try {
342         setMarker(fileToParse,
343                   todo,
344                   SimpleCharStream.getBeginLine(),
345                   TASK,
346                   "Line "+SimpleCharStream.getBeginLine());
347       } catch (CoreException e) {
348         PHPeclipsePlugin.log(e);
349       }
350     }
351   }
352
353   private static final void parse() throws ParseException {
354           phpFile();
355   }
356 }
357
358 PARSER_END(PHPParser)
359
360 TOKEN_MGR_DECLS:
361 {
362   // CommonTokenAction: use the begins/ends fields added to the Jack
363   // CharStream class to set corresponding fields in each Token (which was
364   // also extended with new fields). By default Jack doesn't supply absolute
365   // offsets, just line/column offsets
366   static void CommonTokenAction(Token t) {
367     t.sourceStart = input_stream.beginOffset;
368     t.sourceEnd = input_stream.endOffset;
369   } // CommonTokenAction
370 } // TOKEN_MGR_DECLS
371
372 <DEFAULT> TOKEN :
373 {
374   <PHPSTARTSHORT : "<?">    {PHPParser.createNewHTMLCode();} : PHPPARSING
375 | <PHPSTARTLONG  : "<?php"> {PHPParser.createNewHTMLCode();} : PHPPARSING
376 | <PHPECHOSTART  : "<?=">   {PHPParser.createNewHTMLCode();} : PHPPARSING
377 }
378
379 <PHPPARSING, IN_SINGLE_LINE_COMMENT> TOKEN :
380 {
381   <PHPEND :"?>"> {PHPParser.htmlStart = PHPParser.token.sourceEnd;} : DEFAULT
382 }
383
384 /* Skip any character if we are not in php mode */
385 <DEFAULT> SKIP :
386 {
387  < ~[] >
388 }
389
390
391 /* WHITE SPACE */
392 <PHPPARSING> SKIP :
393 {
394   " "
395 | "\t"
396 | "\n"
397 | "\r"
398 | "\f"
399 }
400
401 /* COMMENTS */
402 <PHPPARSING> SPECIAL_TOKEN :
403 {
404   "//" : IN_SINGLE_LINE_COMMENT
405 | "#"  : IN_SINGLE_LINE_COMMENT
406 | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
407 | "/*" : IN_MULTI_LINE_COMMENT
408 }
409
410 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
411 {
412   <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : PHPPARSING
413 | < ~[] >
414 }
415
416 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
417 {
418  "todo" {PHPParser.createNewTask();}
419 }
420
421 <IN_FORMAL_COMMENT> SPECIAL_TOKEN :
422 {
423   "*/" : PHPPARSING
424 }
425
426 <IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
427 {
428   "*/" : PHPPARSING
429 }
430
431 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
432 MORE :
433 {
434   < ~[] >
435 }
436
437 /* KEYWORDS */
438 <PHPPARSING> TOKEN :
439 {
440   <CLASS    : "class">
441 | <FUNCTION : "function">
442 | <VAR      : "var">
443 | <IF       : "if">
444 | <ELSEIF   : "elseif">
445 | <ELSE     : "else">
446 | <ARRAY    : "array">
447 | <BREAK    : "break">
448 | <LIST     : "list">
449 }
450
451 /* LANGUAGE CONSTRUCT */
452 <PHPPARSING> TOKEN :
453 {
454   <PRINT              : "print">
455 | <ECHO               : "echo">
456 | <INCLUDE            : "include">
457 | <REQUIRE            : "require">
458 | <INCLUDE_ONCE       : "include_once">
459 | <REQUIRE_ONCE       : "require_once">
460 | <GLOBAL             : "global">
461 | <DEFINE             : "define">
462 | <STATIC             : "static">
463 | <CLASSACCESS        : "->">
464 | <STATICCLASSACCESS  : "::">
465 | <ARRAYASSIGN        : "=>">
466 }
467
468 /* RESERVED WORDS AND LITERALS */
469
470 <PHPPARSING> TOKEN :
471 {
472   <CASE     : "case">
473 | <CONST    : "const">
474 | <CONTINUE : "continue">
475 | <_DEFAULT : "default">
476 | <DO       : "do">
477 | <EXTENDS  : "extends">
478 | <FOR      : "for">
479 | <GOTO     : "goto">
480 | <NEW      : "new">
481 | <NULL     : "null">
482 | <RETURN   : "return">
483 | <SUPER    : "super">
484 | <SWITCH   : "switch">
485 | <THIS     : "this">
486 | <TRUE     : "true">
487 | <FALSE    : "false">
488 | <WHILE    : "while">
489 | <ENDWHILE : "endwhile">
490 | <ENDSWITCH: "endswitch">
491 | <ENDIF    : "endif">
492 | <ENDFOR   : "endfor">
493 | <FOREACH  : "foreach">
494 | <AS       : "as" >
495 }
496
497 /* TYPES */
498 <PHPPARSING> TOKEN :
499 {
500   <STRING  : "string">
501 | <OBJECT  : "object">
502 | <BOOL    : "bool">
503 | <BOOLEAN : "boolean">
504 | <REAL    : "real">
505 | <DOUBLE  : "double">
506 | <FLOAT   : "float">
507 | <INT     : "int">
508 | <INTEGER : "integer">
509 }
510
511 //Misc token
512 <PHPPARSING> TOKEN :
513 {
514   <AT                 : "@">
515 | <DOLLAR             : "$">
516 | <BANG               : "!">
517 | <TILDE              : "~">
518 | <HOOK               : "?">
519 | <COLON              : ":">
520 }
521
522 /* OPERATORS */
523 <PHPPARSING> TOKEN :
524 {
525   <OR_OR              : "||">
526 | <AND_AND            : "&&">
527 | <PLUS_PLUS          : "++">
528 | <MINUS_MINUS        : "--">
529 | <PLUS               : "+">
530 | <MINUS              : "-">
531 | <STAR               : "*">
532 | <SLASH              : "/">
533 | <BIT_AND            : "&">
534 | <BIT_OR             : "|">
535 | <XOR                : "^">
536 | <REMAINDER          : "%">
537 | <LSHIFT             : "<<">
538 | <RSIGNEDSHIFT       : ">>">
539 | <RUNSIGNEDSHIFT     : ">>>">
540 | <_ORL               : "OR">
541 | <_ANDL              : "AND">
542 }
543
544 /* LITERALS */
545 <PHPPARSING> TOKEN :
546 {
547   <INTEGER_LITERAL:
548         <DECIMAL_LITERAL> (["l","L"])?
549       | <HEX_LITERAL> (["l","L"])?
550       | <OCTAL_LITERAL> (["l","L"])?
551   >
552 |
553   <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
554 |
555   <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
556 |
557   <#OCTAL_LITERAL: "0" (["0"-"7"])* >
558 |
559   <FLOATING_POINT_LITERAL:
560         (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
561       | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
562       | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
563       | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
564   >
565 |
566   <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
567 |
568   <STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
569 |   <STRING_1: "\"" ( ~["\"","\\"] | "\\" ~[] )* "\"">
570 |   <STRING_2: "'"  ( ~["'","\\"]  | "\\" ~[] )* "'">
571 |   <STRING_3: "`"  ( ~["`","\\"]  | "\\" ~[] )* "`">
572 }
573
574 /* IDENTIFIERS */
575
576 <PHPPARSING> TOKEN :
577 {
578   <IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
579 |
580   < #LETTER:
581       ["a"-"z"] | ["A"-"Z"]
582   >
583 |
584   < #DIGIT:
585       ["0"-"9"]
586   >
587 |
588   < #SPECIAL:
589     "_" | ["\u007f"-"\u00ff"]
590   >
591 }
592
593 /* SEPARATORS */
594
595 <PHPPARSING> TOKEN :
596 {
597   <LPAREN    : "(">
598 | <RPAREN    : ")">
599 | <LBRACE    : "{">
600 | <RBRACE    : "}">
601 | <LBRACKET  : "[">
602 | <RBRACKET  : "]">
603 | <SEMICOLON : ";">
604 | <COMMA     : ",">
605 | <DOT       : ".">
606 }
607
608
609 /* COMPARATOR */
610 <PHPPARSING> TOKEN :
611 {
612   <GT                 : ">">
613 | <LT                 : "<">
614 | <EQUAL_EQUAL        : "==">
615 | <LE                 : "<=">
616 | <GE                 : ">=">
617 | <NOT_EQUAL          : "!=">
618 | <DIF                : "<>">
619 | <BANGDOUBLEEQUAL    : "!==">
620 | <TRIPLEEQUAL        : "===">
621 }
622
623 /* ASSIGNATION */
624 <PHPPARSING> TOKEN :
625 {
626   <ASSIGN             : "=">
627 | <PLUSASSIGN         : "+=">
628 | <MINUSASSIGN        : "-=">
629 | <STARASSIGN         : "*=">
630 | <SLASHASSIGN        : "/=">
631 | <ANDASSIGN          : "&=">
632 | <ORASSIGN           : "|=">
633 | <XORASSIGN          : "^=">
634 | <DOTASSIGN          : ".=">
635 | <REMASSIGN          : "%=">
636 | <TILDEEQUAL         : "~=">
637 | <LSHIFTASSIGN       : "<<=">
638 | <RSIGNEDSHIFTASSIGN : ">>=">
639 }
640
641 <PHPPARSING> TOKEN :
642 {
643   <DOLLAR_ID: <DOLLAR> <IDENTIFIER>>
644 }
645
646 void phpTest() :
647 {}
648 {
649   Php()
650   <EOF>
651 }
652
653 void phpFile() :
654 {}
655 {
656   try {
657     (PhpBlock())*
658     {PHPParser.createNewHTMLCode();}
659   } catch (TokenMgrError e) {
660     PHPeclipsePlugin.log(e);
661     errorStart   = SimpleCharStream.getPosition();
662     errorEnd     = errorStart + 1;
663     errorMessage = e.getMessage();
664     errorLevel   = ERROR;
665     throw generateParseException();
666   }
667 }
668
669 /**
670  * A php block is a <?= expression [;]?>
671  * or <?php somephpcode ?>
672  * or <? somephpcode ?>
673  */
674 void PhpBlock() :
675 {
676   final PHPEchoBlock phpEchoBlock;
677   final Token token;
678 }
679 {
680   phpEchoBlock = phpEchoBlock()
681   {pushOnAstNodes(phpEchoBlock);}
682 |
683   [   <PHPSTARTLONG>
684     | token = <PHPSTARTSHORT>
685     {try {
686       setMarker(fileToParse,
687                 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
688                 token.sourceStart,
689                 token.sourceEnd,
690                 INFO,
691                 "Line " + token.beginLine);
692     } catch (CoreException e) {
693       PHPeclipsePlugin.log(e);
694     }}
695   ]
696   Php()
697   try {
698     <PHPEND>
699   } catch (ParseException e) {
700     errorMessage = "'?>' expected";
701     errorLevel   = ERROR;
702     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
703     errorEnd   = SimpleCharStream.getPosition() + 1;
704     processParseExceptionDebug(e);
705   }
706 }
707
708 PHPEchoBlock phpEchoBlock() :
709 {
710   final Expression expr;
711   final PHPEchoBlock echoBlock;
712   final Token token, token2;
713 }
714 {
715   token = <PHPECHOSTART> expr = Expression() [ <SEMICOLON> ] token2 = <PHPEND>
716   {
717   echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd);
718   pushOnAstNodes(echoBlock);
719   return echoBlock;}
720 }
721
722 void Php() :
723 {}
724 {
725   (BlockStatement())*
726 }
727
728 ClassDeclaration ClassDeclaration() :
729 {
730   final ClassDeclaration classDeclaration;
731   Token className = null;
732   final Token superclassName, token;
733   String classNameImage = SYNTAX_ERROR_CHAR;
734   String superclassNameImage = null;
735 }
736 {
737   token = <CLASS>
738   try {
739     className = <IDENTIFIER>
740     {classNameImage = className.image;}
741   } catch (ParseException e) {
742     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
743     errorLevel   = ERROR;
744     errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
745     errorEnd     = SimpleCharStream.getPosition() + 1;
746     processParseExceptionDebug(e);
747   }
748   [
749     <EXTENDS>
750     try {
751       superclassName = <IDENTIFIER>
752       {superclassNameImage = superclassName.image;}
753     } catch (ParseException e) {
754       errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
755       errorLevel   = ERROR;
756       errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
757       errorEnd   = SimpleCharStream.getPosition() + 1;
758       processParseExceptionDebug(e);
759       superclassNameImage = SYNTAX_ERROR_CHAR;
760     }
761   ]
762   {
763     int start, end;
764     if (className == null) {
765       start = token.sourceStart;
766       end = token.sourceEnd;
767     } else {
768       start = className.sourceStart;
769       end = className.sourceEnd;
770     }
771     if (superclassNameImage == null) {
772
773       classDeclaration = new ClassDeclaration(currentSegment,
774                                               classNameImage,
775                                               start,
776                                               end);
777     } else {
778       classDeclaration = new ClassDeclaration(currentSegment,
779                                               classNameImage,
780                                               superclassNameImage,
781                                               start,
782                                               end);
783     }
784       currentSegment.add(classDeclaration);
785       currentSegment = classDeclaration;
786   }
787   ClassBody(classDeclaration)
788   {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
789    classDeclaration.sourceEnd = SimpleCharStream.getPosition();
790    pushOnAstNodes(classDeclaration);
791    return classDeclaration;}
792 }
793
794 void ClassBody(final ClassDeclaration classDeclaration) :
795 {}
796 {
797   try {
798     <LBRACE>
799   } catch (ParseException e) {
800     errorMessage = "unexpected token : '"+ e.currentToken.next.image + "'. '{' expected";
801     errorLevel   = ERROR;
802     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
803     errorEnd   = SimpleCharStream.getPosition() + 1;
804     processParseExceptionDebug(e);
805   }
806   ( ClassBodyDeclaration(classDeclaration) )*
807   try {
808     <RBRACE>
809   } catch (ParseException e) {
810     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected";
811     errorLevel   = ERROR;
812     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
813     errorEnd   = SimpleCharStream.getPosition() + 1;
814     processParseExceptionDebug(e);
815   }
816 }
817
818 /**
819  * A class can contain only methods and fields.
820  */
821 void ClassBodyDeclaration(final ClassDeclaration classDeclaration) :
822 {
823   final MethodDeclaration method;
824   final FieldDeclaration field;
825 }
826 {
827   method = MethodDeclaration() {method.analyzeCode();
828                                 classDeclaration.addMethod(method);}
829 | field = FieldDeclaration()   {classDeclaration.addField(field);}
830 }
831
832 /**
833  * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;.
834  * it is only used by ClassBodyDeclaration()
835  */
836 FieldDeclaration FieldDeclaration() :
837 {
838   VariableDeclaration variableDeclaration;
839   final VariableDeclaration[] list;
840   final ArrayList arrayList = new ArrayList();
841   final Token token;
842   Token token2 = null;
843 }
844 {
845   token = <VAR> variableDeclaration = VariableDeclaratorNoSuffix()
846   {arrayList.add(variableDeclaration);
847    outlineInfo.addVariable(variableDeclaration.name());}
848   (
849     <COMMA> variableDeclaration = VariableDeclaratorNoSuffix()
850       {arrayList.add(variableDeclaration);
851        outlineInfo.addVariable(variableDeclaration.name());}
852   )*
853   try {
854     token2 = <SEMICOLON>
855   } catch (ParseException e) {
856     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
857     errorLevel   = ERROR;
858     errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
859     errorEnd     = SimpleCharStream.getPosition() + 1;
860     processParseExceptionDebug(e);
861   }
862
863   {list = new VariableDeclaration[arrayList.size()];
864    arrayList.toArray(list);
865    int end;
866    if (token2 == null) {
867      end = list[list.length-1].sourceEnd;
868    } else {
869      end = token2.sourceEnd;
870    }
871    return new FieldDeclaration(list,
872                                token.sourceStart,
873                                end,
874                                currentSegment);}
875 }
876
877 /**
878  * a strict variable declarator : there cannot be a suffix here.
879  * It will be used by fields and formal parameters
880  */
881 VariableDeclaration VariableDeclaratorNoSuffix() :
882 {
883   final Token varName;
884   Expression initializer = null;
885   Token assignToken;
886 }
887 {
888   varName = <DOLLAR_ID>
889   [
890     assignToken = <ASSIGN>
891     try {
892       initializer = VariableInitializer()
893     } catch (ParseException e) {
894       errorMessage = "Literal expression expected in variable initializer";
895       errorLevel   = ERROR;
896       errorStart = assignToken.sourceEnd +1;
897       errorEnd   = assignToken.sourceEnd +1;
898       processParseExceptionDebug(e);
899     }
900   ]
901   {
902   if (initializer == null) {
903     return new VariableDeclaration(currentSegment,
904                                    new Variable(varName.image.substring(1),
905                                                 varName.sourceStart+1,
906                                                 varName.sourceEnd+1),
907                                    varName.sourceStart+1,
908                                    varName.sourceEnd+1);
909   }
910   return new VariableDeclaration(currentSegment,
911                                  new Variable(varName.image.substring(1),
912                                               varName.sourceStart+1,
913                                               varName.sourceEnd+1),
914                                  initializer,
915                                  VariableDeclaration.EQUAL,
916                                  varName.sourceStart+1);
917   }
918 }
919
920 /**
921  * this will be used by static statement
922  */
923 VariableDeclaration VariableDeclarator() :
924 {
925   final AbstractVariable variable;
926   Expression initializer = null;
927 }
928 {
929   variable = VariableDeclaratorId()
930   [
931     <ASSIGN>
932     try {
933       initializer = VariableInitializer()
934     } catch (ParseException e) {
935       errorMessage = "Literal expression expected in variable initializer";
936       errorLevel   = ERROR;
937       errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
938       errorEnd   = SimpleCharStream.getPosition() + 1;
939       processParseExceptionDebug(e);
940     }
941   ]
942   {
943   if (initializer == null) {
944     return new VariableDeclaration(currentSegment,
945                                    variable,
946                                    variable.sourceStart,
947                                    variable.sourceEnd);
948   }
949     return new VariableDeclaration(currentSegment,
950                                    variable,
951                                    initializer,
952                                    VariableDeclaration.EQUAL,
953                                    variable.sourceStart);
954   }
955 }
956
957 /**
958  * A Variable name.
959  * @return the variable name (with suffix)
960  */
961 AbstractVariable VariableDeclaratorId() :
962 {
963   final Variable var;
964   AbstractVariable expression = null;
965 }
966 {
967   try {
968     var = Variable()
969     (
970       LOOKAHEAD(2)
971       expression = VariableSuffix(var)
972     )*
973     {
974      if (expression == null) {
975        return var;
976      }
977      return expression;
978     }
979   } catch (ParseException e) {
980     errorMessage = "'$' expected for variable identifier";
981     errorLevel   = ERROR;
982     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
983     errorEnd   = SimpleCharStream.getPosition() + 1;
984     throw e;
985   }
986 }
987
988 /**
989  * Return a variablename without the $.
990  * @return a variable name
991  *//*
992 Variable Variable():
993 {
994   final StringBuffer buff;
995   Expression expression = null;
996   final Token token;
997   Variable expr;
998   final int pos;
999 }
1000 {
1001   token = <DOLLAR_ID>
1002   [<LBRACE> expression = Expression() <RBRACE>]
1003   {
1004     if (expression == null) {
1005       return new Variable(token.image.substring(1),
1006                           token.sourceStart+1,
1007                           token.sourceEnd+1);
1008     }
1009     String s = expression.toStringExpression();
1010     buff = new StringBuffer(token.image.length()+s.length()+2);
1011     buff.append(token.image);
1012     buff.append("{");
1013     buff.append(s);
1014     buff.append("}");
1015     s = buff.toString();
1016     return new Variable(s,token.sourceStart+1,token.sourceEnd+1);
1017   }
1018 |
1019   token = <DOLLAR>
1020   expr = VariableName()
1021   {return new Variable(expr,token.sourceStart,expr.sourceEnd);}
1022 }   */
1023
1024 Variable Variable() :
1025 {
1026   Variable variable = null;
1027   final Token token;
1028 }
1029 {
1030  token = <DOLLAR_ID> [variable = Var(token)]
1031   {
1032     if (variable == null) {
1033       return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1);
1034     }
1035     final StringBuffer buff = new StringBuffer();
1036     buff.append(token.image.substring(1));
1037     buff.append(variable.toStringExpression());
1038     return new Variable(buff.toString(),token.sourceStart+1,variable.sourceEnd+1);
1039   }
1040 |
1041   token = <DOLLAR> variable = Var(token)
1042   {
1043     return new Variable(variable,token.sourceStart,variable.sourceEnd);
1044   }
1045 }
1046
1047 Variable Var(final Token dollar) :
1048 {
1049   Variable variable = null;
1050   final Token token;
1051   ConstantIdentifier constant;
1052 }
1053 {
1054   token = <DOLLAR_ID> [variable = Var(token)]
1055   {if (variable == null) {
1056      return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1);
1057    }
1058    final StringBuffer buff = new StringBuffer();
1059    buff.append(token.image.substring(1));
1060    buff.append(variable.toStringExpression());
1061    return new Variable(buff.toString(),dollar.sourceStart,variable.sourceEnd);
1062    }
1063 |
1064   LOOKAHEAD(<DOLLAR> <DOLLAR>)
1065   token = <DOLLAR> variable = Var(token)
1066   {return new Variable(variable,dollar.sourceStart,variable.sourceEnd);}
1067 |
1068   constant = VariableName()
1069   {return new Variable(constant.name,dollar.sourceStart,constant.sourceEnd);}
1070 }
1071
1072 /**
1073  * A Variable name (without the $)
1074  * @return a variable name String
1075  */
1076 ConstantIdentifier VariableName():
1077 {
1078   final StringBuffer buff;
1079   String expr;
1080   Expression expression = null;
1081   final Token token;
1082   Token token2 = null;
1083 }
1084 {
1085   token = <LBRACE> expression = Expression() token2 = <RBRACE>
1086   {expr = expression.toStringExpression();
1087    buff = new StringBuffer(expr.length()+2);
1088    buff.append("{");
1089    buff.append(expr);
1090    buff.append("}");
1091    expr = buff.toString();
1092    return new ConstantIdentifier(expr,
1093                                  token.sourceStart,
1094                                  token2.sourceEnd);
1095
1096    }
1097 |
1098   token = <IDENTIFIER>
1099   [<LBRACE> expression = Expression() token2 = <RBRACE>]
1100   {
1101     if (expression == null) {
1102       return new ConstantIdentifier(token.image,
1103                                     token.sourceStart,
1104                                     token.sourceEnd);
1105     }
1106     expr = expression.toStringExpression();
1107     buff = new StringBuffer(token.image.length()+expr.length()+2);
1108     buff.append(token.image);
1109     buff.append("{");
1110     buff.append(expr);
1111     buff.append("}");
1112     expr = buff.toString();
1113     return new ConstantIdentifier(expr,
1114                                   token.sourceStart,
1115                                   token2.sourceEnd);
1116   }
1117 /*|
1118   <DOLLAR>
1119   var = VariableName()
1120   {
1121     return new Variable(var,
1122                         var.sourceStart-1,
1123                         var.sourceEnd);
1124   }
1125 |
1126   token = <DOLLAR_ID>
1127   {
1128   return new Variable(token.image,
1129                       token.sourceStart+1,
1130                       token.sourceEnd+1);
1131   } */
1132 }
1133
1134 Expression VariableInitializer() :
1135 {
1136   final Expression expr;
1137   final Token token, token2;
1138 }
1139 {
1140   expr = Literal()
1141   {return expr;}
1142 |
1143   token2 = <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1144   {return new PrefixedUnaryExpression(new NumberLiteral(token),
1145                                       OperatorIds.MINUS,
1146                                       token2.sourceStart);}
1147 |
1148   token2 = <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1149   {return new PrefixedUnaryExpression(new NumberLiteral(token),
1150                                       OperatorIds.PLUS,
1151                                       token2.sourceStart);}
1152 |
1153   expr = ArrayDeclarator()
1154   {return expr;}
1155 |
1156   token = <IDENTIFIER>
1157   {return new ConstantIdentifier(token);}
1158 }
1159
1160 ArrayVariableDeclaration ArrayVariable() :
1161 {
1162 final Expression expr,expr2;
1163 }
1164 {
1165   expr = Expression()
1166   [
1167     <ARRAYASSIGN> expr2 = Expression()
1168     {return new ArrayVariableDeclaration(expr,expr2);}
1169   ]
1170   {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());}
1171 }
1172
1173 ArrayVariableDeclaration[] ArrayInitializer() :
1174 {
1175   ArrayVariableDeclaration expr;
1176   final ArrayList list = new ArrayList();
1177 }
1178 {
1179   <LPAREN>
1180     [
1181       expr = ArrayVariable()
1182       {list.add(expr);}
1183       ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
1184       {list.add(expr);}
1185       )*
1186     ]
1187     [
1188       <COMMA> {list.add(null);}
1189     ]
1190   <RPAREN>
1191   {
1192   final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1193   list.toArray(vars);
1194   return vars;}
1195 }
1196
1197 /**
1198  * A Method Declaration.
1199  * <b>function</b> MetodDeclarator() Block()
1200  */
1201 MethodDeclaration MethodDeclaration() :
1202 {
1203   final MethodDeclaration functionDeclaration;
1204   final Block block;
1205   final OutlineableWithChildren seg = currentSegment;
1206   final Token token;
1207 }
1208 {
1209   token = <FUNCTION>
1210   try {
1211     functionDeclaration = MethodDeclarator(token.sourceStart)
1212     {outlineInfo.addVariable(new String(functionDeclaration.name));}
1213   } catch (ParseException e) {
1214     if (errorMessage != null)  throw e;
1215     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1216     errorLevel   = ERROR;
1217     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1218     errorEnd   = SimpleCharStream.getPosition() + 1;
1219     throw e;
1220   }
1221   {currentSegment = functionDeclaration;}
1222   block = Block()
1223   {functionDeclaration.statements = block.statements;
1224    currentSegment = seg;
1225    return functionDeclaration;}
1226 }
1227
1228 /**
1229  * A MethodDeclarator.
1230  * [&] IDENTIFIER(parameters ...).
1231  * @return a function description for the outline
1232  */
1233 MethodDeclaration MethodDeclarator(final int start) :
1234 {
1235   Token identifier = null;
1236   Token reference = null;
1237   final Hashtable formalParameters = new Hashtable();
1238   String identifierChar = SYNTAX_ERROR_CHAR;
1239   final int end;
1240 }
1241 {
1242   [reference = <BIT_AND>]
1243   try {
1244     identifier = <IDENTIFIER>
1245     {identifierChar = identifier.image;}
1246   } catch (ParseException e) {
1247     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1248     errorLevel   = ERROR;
1249     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1250     errorEnd   = SimpleCharStream.getPosition() + 1;
1251     processParseExceptionDebug(e);
1252   }
1253   end = FormalParameters(formalParameters)
1254   {
1255   int nameStart, nameEnd;
1256   if (identifier == null) {
1257     if (reference == null) {
1258       nameStart = start + 9;
1259       nameEnd = start + 10;
1260     } else {
1261       nameStart = reference.sourceEnd + 1;
1262       nameEnd = reference.sourceEnd + 2;
1263     }
1264   } else {
1265       nameStart = identifier.sourceStart;
1266       nameEnd = identifier.sourceEnd;
1267   }
1268   return new MethodDeclaration(currentSegment,
1269                                identifierChar,
1270                                formalParameters,
1271                                reference != null,
1272                                nameStart,
1273                                nameEnd,
1274                                start,
1275                                end);
1276   }
1277 }
1278
1279 /**
1280  * FormalParameters follows method identifier.
1281  * (FormalParameter())
1282  */
1283 int FormalParameters(final Hashtable parameters) :
1284 {
1285   VariableDeclaration var;
1286   final Token token;
1287   int end;
1288 }
1289 {
1290   try {
1291   <LPAREN>
1292   } catch (ParseException e) {
1293     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1294     errorLevel   = ERROR;
1295     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1296     errorEnd   = SimpleCharStream.getPosition() + 1;
1297     processParseExceptionDebug(e);
1298   }
1299   [
1300     var = FormalParameter()
1301     {parameters.put(new String(var.name()),var);}
1302     (
1303       <COMMA> var = FormalParameter()
1304       {parameters.put(new String(var.name()),var);}
1305     )*
1306   ]
1307   try {
1308     token = <RPAREN>
1309     {end = token.sourceEnd;}
1310   } catch (ParseException e) {
1311     errorMessage = "')' expected";
1312     errorLevel   = ERROR;
1313     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1314     errorEnd   = SimpleCharStream.getPosition() + 1;
1315     processParseExceptionDebug(e);
1316     end = e.currentToken.sourceStart;
1317   }
1318  {return end;}
1319 }
1320
1321 /**
1322  * A formal parameter.
1323  * $varname[=value] (,$varname[=value])
1324  */
1325 VariableDeclaration FormalParameter() :
1326 {
1327   final VariableDeclaration variableDeclaration;
1328   Token token = null;
1329 }
1330 {
1331   [token = <BIT_AND>] variableDeclaration = VariableDeclaratorNoSuffix()
1332   {
1333     if (token != null) {
1334       variableDeclaration.setReference(true);
1335     }
1336     return variableDeclaration;}
1337 }
1338
1339 ConstantIdentifier Type() :
1340 {final Token token;}
1341 {
1342   token = <STRING>    {return new ConstantIdentifier(token);}
1343 | token = <BOOL>      {return new ConstantIdentifier(token);}
1344 | token = <BOOLEAN>   {return new ConstantIdentifier(token);}
1345 | token = <REAL>      {return new ConstantIdentifier(token);}
1346 | token = <DOUBLE>    {return new ConstantIdentifier(token);}
1347 | token = <FLOAT>     {return new ConstantIdentifier(token);}
1348 | token = <INT>       {return new ConstantIdentifier(token);}
1349 | token = <INTEGER>   {return new ConstantIdentifier(token);}
1350 | token = <OBJECT>    {return new ConstantIdentifier(token);}
1351 }
1352
1353 Expression Expression() :
1354 {
1355   final Expression expr;
1356   Expression initializer = null;
1357   int assignOperator = -1;
1358 }
1359 {
1360   LOOKAHEAD(1)
1361   expr = ConditionalExpression()
1362   [
1363     assignOperator = AssignmentOperator()
1364     try {
1365       initializer = Expression()
1366     } catch (ParseException e) {
1367       if (errorMessage != null) {
1368         throw e;
1369       }
1370       errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1371       errorLevel   = ERROR;
1372       errorEnd   = SimpleCharStream.getPosition();
1373       throw e;
1374     }
1375   ]
1376   {
1377     if (assignOperator != -1) {// todo : change this, very very bad :(
1378         if (expr instanceof AbstractVariable) {
1379           return new VariableDeclaration(currentSegment,
1380                                          (AbstractVariable) expr,
1381                                          initializer,
1382                                          expr.sourceStart,
1383                                          initializer.sourceEnd);
1384         }
1385         String varName = expr.toStringExpression().substring(1);
1386         return new VariableDeclaration(currentSegment,
1387                                        new Variable(varName,
1388                                                     expr.sourceStart,
1389                                                     expr.sourceEnd),
1390                                        expr.sourceStart,
1391                                        initializer.sourceEnd);
1392     }
1393     return expr;
1394   }
1395 | expr = ExpressionWBang()       {return expr;}
1396 }
1397
1398 Expression ExpressionWBang() :
1399 {
1400   final Expression expr;
1401   final Token token;
1402 }
1403 {
1404   token = <BANG> expr = ExpressionWBang()
1405   {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1406 | expr = ExpressionNoBang() {return expr;}
1407 }
1408
1409 Expression ExpressionNoBang() :
1410 {
1411   Expression expr;
1412 }
1413 {
1414   expr = ListExpression()    {return expr;}
1415 |
1416   expr = PrintExpression()   {return expr;}
1417 }
1418
1419 /**
1420  * Any assignement operator.
1421  * @return the assignement operator id
1422  */
1423 int AssignmentOperator() :
1424 {}
1425 {
1426   <ASSIGN>             {return VariableDeclaration.EQUAL;}
1427 | <STARASSIGN>         {return VariableDeclaration.STAR_EQUAL;}
1428 | <SLASHASSIGN>        {return VariableDeclaration.SLASH_EQUAL;}
1429 | <REMASSIGN>          {return VariableDeclaration.REM_EQUAL;}
1430 | <PLUSASSIGN>         {return VariableDeclaration.PLUS_EQUAL;}
1431 | <MINUSASSIGN>        {return VariableDeclaration.MINUS_EQUAL;}
1432 | <LSHIFTASSIGN>       {return VariableDeclaration.LSHIFT_EQUAL;}
1433 | <RSIGNEDSHIFTASSIGN> {return VariableDeclaration.RSIGNEDSHIFT_EQUAL;}
1434 | <ANDASSIGN>          {return VariableDeclaration.AND_EQUAL;}
1435 | <XORASSIGN>          {return VariableDeclaration.XOR_EQUAL;}
1436 | <ORASSIGN>           {return VariableDeclaration.OR_EQUAL;}
1437 | <DOTASSIGN>          {return VariableDeclaration.DOT_EQUAL;}
1438 | <TILDEEQUAL>         {return VariableDeclaration.TILDE_EQUAL;}
1439 }
1440
1441 Expression ConditionalExpression() :
1442 {
1443   final Expression expr;
1444   Expression expr2 = null;
1445   Expression expr3 = null;
1446 }
1447 {
1448   expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1449 {
1450   if (expr3 == null) {
1451     return expr;
1452   }
1453   return new ConditionalExpression(expr,expr2,expr3);
1454 }
1455 }
1456
1457 Expression ConditionalOrExpression() :
1458 {
1459   Expression expr,expr2;
1460   int operator;
1461 }
1462 {
1463   expr = ConditionalAndExpression()
1464   (
1465     (
1466         <OR_OR> {operator = OperatorIds.OR_OR;}
1467       | <_ORL>  {operator = OperatorIds.ORL;}
1468     )
1469     expr2 = ConditionalAndExpression()
1470     {
1471       expr = new BinaryExpression(expr,expr2,operator);
1472     }
1473   )*
1474   {return expr;}
1475 }
1476
1477 Expression ConditionalAndExpression() :
1478 {
1479   Expression expr,expr2;
1480   int operator;
1481 }
1482 {
1483   expr = ConcatExpression()
1484   (
1485   (  <AND_AND> {operator = OperatorIds.AND_AND;}
1486    | <_ANDL>   {operator = OperatorIds.ANDL;})
1487    expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1488   )*
1489   {return expr;}
1490 }
1491
1492 Expression ConcatExpression() :
1493 {
1494   Expression expr,expr2;
1495 }
1496 {
1497   expr = InclusiveOrExpression()
1498   (
1499     <DOT> expr2 = InclusiveOrExpression()
1500     {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1501   )*
1502   {return expr;}
1503 }
1504
1505 Expression InclusiveOrExpression() :
1506 {
1507   Expression expr,expr2;
1508 }
1509 {
1510   expr = ExclusiveOrExpression()
1511   (<BIT_OR> expr2 = ExclusiveOrExpression()
1512    {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1513   )*
1514   {return expr;}
1515 }
1516
1517 Expression ExclusiveOrExpression() :
1518 {
1519   Expression expr,expr2;
1520 }
1521 {
1522   expr = AndExpression()
1523   (
1524     <XOR> expr2 = AndExpression()
1525     {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1526   )*
1527   {return expr;}
1528 }
1529
1530 Expression AndExpression() :
1531 {
1532   Expression expr,expr2;
1533 }
1534 {
1535   expr = EqualityExpression()
1536   (
1537     LOOKAHEAD(1)
1538     <BIT_AND> expr2 = EqualityExpression()
1539     {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1540   )*
1541   {return expr;}
1542 }
1543
1544 Expression EqualityExpression() :
1545 {
1546   Expression expr,expr2;
1547   int operator;
1548 }
1549 {
1550   expr = RelationalExpression()
1551   (
1552   (   <EQUAL_EQUAL>      {operator = OperatorIds.EQUAL_EQUAL;}
1553     | <DIF>              {operator = OperatorIds.DIF;}
1554     | <NOT_EQUAL>        {operator = OperatorIds.DIF;}
1555     | <BANGDOUBLEEQUAL>  {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1556     | <TRIPLEEQUAL>      {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1557   )
1558   try {
1559     expr2 = RelationalExpression()
1560   } catch (ParseException e) {
1561     if (errorMessage != null) {
1562       throw e;
1563     }
1564     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1565     errorLevel   = ERROR;
1566     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1567     errorEnd   = SimpleCharStream.getPosition() + 1;
1568     throw e;
1569   }
1570   {
1571     expr = new BinaryExpression(expr,expr2,operator);
1572   }
1573   )*
1574   {return expr;}
1575 }
1576
1577 Expression RelationalExpression() :
1578 {
1579   Expression expr,expr2;
1580   int operator;
1581 }
1582 {
1583   expr = ShiftExpression()
1584   (
1585   ( <LT> {operator = OperatorIds.LESS;}
1586   | <GT> {operator = OperatorIds.GREATER;}
1587   | <LE> {operator = OperatorIds.LESS_EQUAL;}
1588   | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1589    expr2 = ShiftExpression()
1590   {expr = new BinaryExpression(expr,expr2,operator);}
1591   )*
1592   {return expr;}
1593 }
1594
1595 Expression ShiftExpression() :
1596 {
1597   Expression expr,expr2;
1598   int operator;
1599 }
1600 {
1601   expr = AdditiveExpression()
1602   (
1603   ( <LSHIFT>         {operator = OperatorIds.LEFT_SHIFT;}
1604   | <RSIGNEDSHIFT>   {operator = OperatorIds.RIGHT_SHIFT;}
1605   | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1606   expr2 = AdditiveExpression()
1607   {expr = new BinaryExpression(expr,expr2,operator);}
1608   )*
1609   {return expr;}
1610 }
1611
1612 Expression AdditiveExpression() :
1613 {
1614   Expression expr,expr2;
1615   int operator;
1616 }
1617 {
1618   expr = MultiplicativeExpression()
1619   (
1620     LOOKAHEAD(1)
1621      ( <PLUS>  {operator = OperatorIds.PLUS;}
1622      | <MINUS> {operator = OperatorIds.MINUS;}
1623   )
1624    expr2 = MultiplicativeExpression()
1625   {expr = new BinaryExpression(expr,expr2,operator);}
1626    )*
1627   {return expr;}
1628 }
1629
1630 Expression MultiplicativeExpression() :
1631 {
1632   Expression expr,expr2;
1633   int operator;
1634 }
1635 {
1636   try {
1637     expr = UnaryExpression()
1638   } catch (ParseException e) {
1639     if (errorMessage != null) throw e;
1640     errorMessage = "unexpected token '"+e.currentToken.next.image+"'";
1641     errorLevel   = ERROR;
1642     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1643     errorEnd   = SimpleCharStream.getPosition() + 1;
1644     throw e;
1645   }
1646   (
1647    (  <STAR>      {operator = OperatorIds.MULTIPLY;}
1648     | <SLASH>     {operator = OperatorIds.DIVIDE;}
1649     | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1650     expr2 = UnaryExpression()
1651     {expr = new BinaryExpression(expr,expr2,operator);}
1652   )*
1653   {return expr;}
1654 }
1655
1656 /**
1657  * An unary expression starting with @, & or nothing
1658  */
1659 Expression UnaryExpression() :
1660 {
1661   final Expression expr;
1662 }
1663 {
1664  /* <BIT_AND> expr = UnaryExpressionNoPrefix()             //why did I had that ?
1665   {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1666 |      */
1667   expr = AtNotUnaryExpression() {return expr;}
1668 }
1669
1670 /**
1671  * An expression prefixed (or not) by one or more @ and !.
1672  * @return the expression
1673  */
1674 Expression AtNotUnaryExpression() :
1675 {
1676   final Expression expr;
1677   final Token token;
1678 }
1679 {
1680   token = <AT>
1681   expr = AtNotUnaryExpression()
1682   {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1683 |
1684   token = <BANG>
1685   expr = AtNotUnaryExpression()
1686   {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1687 |
1688   expr = UnaryExpressionNoPrefix()
1689   {return expr;}
1690 }
1691
1692 Expression UnaryExpressionNoPrefix() :
1693 {
1694   final Expression expr;
1695   final Token token;
1696 }
1697 {
1698   token = <PLUS> expr = AtNotUnaryExpression()   {return new PrefixedUnaryExpression(expr,
1699                                                                                      OperatorIds.PLUS,
1700                                                                                      token.sourceStart);}
1701 |
1702   token = <MINUS> expr = AtNotUnaryExpression()  {return new PrefixedUnaryExpression(expr,
1703                                                                                      OperatorIds.MINUS,
1704                                                                                      token.sourceStart);}
1705 |
1706   expr = PreIncDecExpression()
1707   {return expr;}
1708 |
1709   expr = UnaryExpressionNotPlusMinus()
1710   {return expr;}
1711 }
1712
1713
1714 Expression PreIncDecExpression() :
1715 {
1716 final Expression expr;
1717 final int operator;
1718 final Token token;
1719 }
1720 {
1721   (
1722       token = <PLUS_PLUS>   {operator = OperatorIds.PLUS_PLUS;}
1723     |
1724       token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1725   )
1726   expr = PrimaryExpression()
1727   {return new PrefixedUnaryExpression(expr,operator,token.sourceStart);}
1728 }
1729
1730 Expression UnaryExpressionNotPlusMinus() :
1731 {
1732   final Expression expr;
1733 }
1734 {
1735   LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1736   expr = CastExpression()         {return expr;}
1737 | expr = PostfixExpression()      {return expr;}
1738 | expr = Literal()                {return expr;}
1739 | <LPAREN> expr = Expression()
1740   try {
1741     <RPAREN>
1742   } catch (ParseException e) {
1743     errorMessage = "')' expected";
1744     errorLevel   = ERROR;
1745     errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1746     errorEnd     = SimpleCharStream.getPosition() + 1;
1747     throw e;
1748   }
1749   {return expr;}
1750 }
1751
1752 CastExpression CastExpression() :
1753 {
1754 final ConstantIdentifier type;
1755 final Expression expr;
1756 final Token token,token1;
1757 }
1758 {
1759   token1 = <LPAREN>
1760   (
1761       type = Type()
1762     |
1763       token = <ARRAY> {type = new ConstantIdentifier(token);}
1764   )
1765   <RPAREN> expr = UnaryExpression()
1766   {return new CastExpression(type,expr,token1.sourceStart,expr.sourceEnd);}
1767 }
1768
1769 Expression PostfixExpression() :
1770 {
1771   final Expression expr;
1772   int operator = -1;
1773   Token token = null;
1774 }
1775 {
1776   expr = PrimaryExpression()
1777   [
1778       token = <PLUS_PLUS>   {operator = OperatorIds.PLUS_PLUS;}
1779     |
1780       token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1781   ]
1782   {
1783     if (operator == -1) {
1784       return expr;
1785     }
1786     return new PostfixedUnaryExpression(expr,operator,token.sourceEnd);
1787   }
1788 }
1789
1790 Expression PrimaryExpression() :
1791 {
1792   Expression expr;
1793   Token token = null;
1794 }
1795 {
1796   [token = <BIT_AND>] expr = refPrimaryExpression(token)
1797   {return expr;}
1798 |
1799   expr = ArrayDeclarator()
1800   {return expr;}
1801 }
1802
1803 Expression refPrimaryExpression(final Token reference) :
1804 {
1805   Expression expr;
1806   Expression expr2 = null;
1807   final Token identifier;
1808 }
1809 {
1810   identifier = <IDENTIFIER>
1811   {
1812     expr = new ConstantIdentifier(identifier);
1813   }
1814   (
1815     <STATICCLASSACCESS> expr2 = ClassIdentifier()
1816     {expr = new ClassAccess(expr,
1817                             expr2,
1818                             ClassAccess.STATIC);}
1819   )*
1820   [ expr2 = Arguments(expr) ]
1821   {
1822     if (expr2 == null) {
1823       if (reference != null) {
1824         ParseException e = generateParseException();
1825         errorMessage = "you cannot use a constant by reference";
1826         errorLevel   = ERROR;
1827         errorStart   = reference.sourceStart;
1828         errorEnd     = reference.sourceEnd;
1829         processParseExceptionDebug(e);
1830       }
1831       return expr;
1832     }
1833     return expr2;
1834   }
1835 |
1836   expr = VariableDeclaratorId()  //todo use the reference parameter ...
1837   [ expr = Arguments(expr) ]
1838   {return expr;}
1839 |
1840   token = <NEW>
1841   expr = ClassIdentifier()
1842   {
1843     int start;
1844     if (reference == null) {
1845       start = token.sourceStart;
1846     } else {
1847       start = reference.sourceStart;
1848     }
1849     expr = new ClassInstantiation(expr,
1850                                   reference != null,
1851                                   start);
1852   }
1853   [ expr = Arguments(expr) ]
1854   {return expr;}
1855 }
1856
1857 /**
1858  * An array declarator.
1859  * array(vars)
1860  * @return an array
1861  */
1862 ArrayInitializer ArrayDeclarator() :
1863 {
1864   final ArrayVariableDeclaration[] vars;
1865   final Token token;
1866 }
1867 {
1868   token = <ARRAY> vars = ArrayInitializer()
1869   {return new ArrayInitializer(vars,token.sourceStart,SimpleCharStream.getPosition());}
1870 }
1871
1872 Expression ClassIdentifier():
1873 {
1874   final Expression expr;
1875   final Token token;
1876 }
1877 {
1878   token = <IDENTIFIER>          {return new ConstantIdentifier(token);}
1879 | expr = Type()                 {return expr;}
1880 | expr = VariableDeclaratorId() {return expr;}
1881 }
1882
1883 /**
1884  * Used by Variabledeclaratorid and primarysuffix
1885  */
1886 AbstractVariable VariableSuffix(final AbstractVariable prefix) :
1887 {
1888   Expression expression = null;
1889   final Token classAccessToken;
1890 }
1891 {
1892   classAccessToken = <CLASSACCESS>
1893   try {
1894     ( expression = VariableName() | expression = Variable() )
1895   } catch (ParseException e) {
1896     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1897     errorLevel   = ERROR;
1898     errorStart = classAccessToken.sourceEnd +1;
1899     errorEnd   = classAccessToken.sourceEnd +1;
1900     processParseExceptionDebug(e);
1901   }
1902   {return new ClassAccess(prefix,
1903                           expression,
1904                           ClassAccess.NORMAL);}
1905 |
1906   <LBRACKET> [ expression = Expression() | expression = Type() ]  //Not good
1907   try {
1908     <RBRACKET>
1909   } catch (ParseException e) {
1910     errorMessage = "']' expected";
1911     errorLevel   = ERROR;
1912     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1913     errorEnd   = SimpleCharStream.getPosition() + 1;
1914     throw e;
1915   }
1916   {return new ArrayDeclarator(prefix,expression,SimpleCharStream.getPosition());}
1917 }
1918
1919 Literal Literal() :
1920 {
1921   final Token token;
1922 }
1923 {
1924   token = <INTEGER_LITERAL>        {return new NumberLiteral(token);}
1925 | token = <FLOATING_POINT_LITERAL> {return new NumberLiteral(token);}
1926 | token = <STRING_LITERAL>         {return new StringLiteral(token);}
1927 | token = <TRUE>                   {return new TrueLiteral(token);}
1928 | token = <FALSE>                  {return new FalseLiteral(token);}
1929 | token = <NULL>                   {return new NullLiteral(token);}
1930 }
1931
1932 FunctionCall Arguments(final Expression func) :
1933 {
1934 Expression[] args = null;
1935 final Token token;
1936 }
1937 {
1938   <LPAREN> [ args = ArgumentList() ]
1939   try {
1940     token = <RPAREN>
1941   } catch (ParseException e) {
1942     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
1943     errorLevel   = ERROR;
1944     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1945     errorEnd   = SimpleCharStream.getPosition() + 1;
1946     throw e;
1947   }
1948   {return new FunctionCall(func,args,token.sourceEnd);}
1949 }
1950
1951 /**
1952  * An argument list is a list of arguments separated by comma :
1953  * argumentDeclaration() (, argumentDeclaration)*
1954  * @return an array of arguments
1955  */
1956 Expression[] ArgumentList() :
1957 {
1958 Expression arg;
1959 final ArrayList list = new ArrayList();
1960 }
1961 {
1962   arg = Expression()
1963   {list.add(arg);}
1964   ( <COMMA>
1965       try {
1966         arg = Expression()
1967         {list.add(arg);}
1968       } catch (ParseException e) {
1969         errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
1970         errorLevel   = ERROR;
1971         errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1972         errorEnd     = SimpleCharStream.getPosition() + 1;
1973         throw e;
1974       }
1975    )*
1976    {
1977    final Expression[] arguments = new Expression[list.size()];
1978    list.toArray(arguments);
1979    return arguments;}
1980 }
1981
1982 /**
1983  * A Statement without break.
1984  * @return a statement
1985  */
1986 Statement StatementNoBreak() :
1987 {
1988   final Statement statement;
1989   Token token = null;
1990 }
1991 {
1992   LOOKAHEAD(2)
1993   statement = expressionStatement()     {return statement;}
1994 | LOOKAHEAD(1)
1995   statement = LabeledStatement()        {return statement;}
1996 | statement = Block()                   {return statement;}
1997 | statement = EmptyStatement()          {return statement;}
1998 | statement = SwitchStatement()         {return statement;}
1999 | statement = IfStatement()             {return statement;}
2000 | statement = WhileStatement()          {return statement;}
2001 | statement = DoStatement()             {return statement;}
2002 | statement = ForStatement()            {return statement;}
2003 | statement = ForeachStatement()        {return statement;}
2004 | statement = ContinueStatement()       {return statement;}
2005 | statement = ReturnStatement()         {return statement;}
2006 | statement = EchoStatement()           {return statement;}
2007 | [token=<AT>] statement = IncludeStatement()
2008   {if (token != null) {
2009     ((InclusionStatement)statement).silent = true;
2010     statement.sourceStart = token.sourceStart;
2011   }
2012   return statement;}
2013 | statement = StaticStatement()         {return statement;}
2014 | statement = GlobalStatement()         {return statement;}
2015 | statement = defineStatement()         {currentSegment.add((Outlineable)statement);return statement;}
2016 }
2017
2018 /**
2019  * A statement expression.
2020  * expression ;
2021  * @return an expression
2022  */
2023 Statement expressionStatement() :
2024 {
2025   final Statement statement;
2026   final Token token;
2027 }
2028 {
2029   statement = Expression()
2030   try {
2031     token = <SEMICOLON>
2032     {statement.sourceEnd = token.sourceEnd;}
2033   } catch (ParseException e) {
2034     if (e.currentToken.next.kind != PHPParserConstants.PHPEND) {
2035       errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2036       errorLevel   = ERROR;
2037       errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2038       errorEnd   = SimpleCharStream.getPosition() + 1;
2039       throw e;
2040     }
2041   }
2042   {return statement;}
2043 }
2044
2045 Define defineStatement() :
2046 {
2047   final int start = SimpleCharStream.getPosition();
2048   Expression defineName,defineValue;
2049 }
2050 {
2051   <DEFINE>
2052   try {
2053     <LPAREN>
2054   } catch (ParseException e) {
2055     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2056     errorLevel   = ERROR;
2057     errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2058     errorEnd     = SimpleCharStream.getPosition() + 1;
2059     processParseExceptionDebug(e);
2060   }
2061   try {
2062     defineName = Expression()
2063   } catch (ParseException e) {
2064     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2065     errorLevel   = ERROR;
2066     errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2067     errorEnd     = SimpleCharStream.getPosition() + 1;
2068     throw e;
2069   }
2070   try {
2071     <COMMA>
2072   } catch (ParseException e) {
2073     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2074     errorLevel   = ERROR;
2075     errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2076     errorEnd     = SimpleCharStream.getPosition() + 1;
2077     processParseExceptionDebug(e);
2078   }
2079   try {
2080     defineValue = Expression()
2081   } catch (ParseException e) {
2082     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2083     errorLevel   = ERROR;
2084     errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2085     errorEnd     = SimpleCharStream.getPosition() + 1;
2086     throw e;
2087   }
2088   try {
2089     <RPAREN>
2090   } catch (ParseException e) {
2091     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2092     errorLevel   = ERROR;
2093     errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2094     errorEnd     = SimpleCharStream.getPosition() + 1;
2095     processParseExceptionDebug(e);
2096   }
2097   {return new Define(currentSegment,
2098                      defineName,
2099                      defineValue,
2100                      start,
2101                      SimpleCharStream.getPosition());}
2102 }
2103
2104 /**
2105  * A Normal statement.
2106  */
2107 Statement Statement() :
2108 {
2109   final Statement statement;
2110 }
2111 {
2112   statement = StatementNoBreak() {return statement;}
2113 | statement = BreakStatement()   {return statement;}
2114 }
2115
2116 /**
2117  * An html block inside a php syntax.
2118  */
2119 HTMLBlock htmlBlock() :
2120 {
2121   final int startIndex = nodePtr;
2122   final AstNode[] blockNodes;
2123   final int nbNodes;
2124 }
2125 {
2126   <PHPEND> (phpEchoBlock())*
2127   try {
2128     (<PHPSTARTLONG> | <PHPSTARTSHORT>)
2129   } catch (ParseException e) {
2130     errorMessage = "unexpected end of file , '<?php' expected";
2131     errorLevel   = ERROR;
2132     errorStart   = SimpleCharStream.getPosition();
2133     errorEnd     = SimpleCharStream.getPosition();
2134     throw e;
2135   }
2136   {
2137   nbNodes    = nodePtr - startIndex;
2138   blockNodes = new AstNode[nbNodes];
2139   System.arraycopy(nodes,startIndex,blockNodes,0,nbNodes);
2140   nodePtr = startIndex;
2141   return new HTMLBlock(blockNodes);}
2142 }
2143
2144 /**
2145  * An include statement. It's "include" an expression;
2146  */
2147 InclusionStatement IncludeStatement() :
2148 {
2149   final Expression expr;
2150   final int keyword;
2151   final InclusionStatement inclusionStatement;
2152   final Token token, token2;
2153 }
2154 {
2155       (  token = <REQUIRE>      {keyword = InclusionStatement.REQUIRE;}
2156        | token = <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;}
2157        | token = <INCLUDE>      {keyword = InclusionStatement.INCLUDE;}
2158        | token = <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;})
2159   try {
2160     expr = Expression()
2161   } catch (ParseException e) {
2162     if (errorMessage != null) {
2163       throw e;
2164     }
2165     errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
2166     errorLevel   = ERROR;
2167     errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2168     errorEnd     = SimpleCharStream.getPosition() + 1;
2169     throw e;
2170   }
2171   {inclusionStatement = new InclusionStatement(currentSegment,
2172                                                keyword,
2173                                                expr,
2174                                                token.sourceStart);
2175    currentSegment.add(inclusionStatement);
2176   }
2177   try {
2178     token2 = <SEMICOLON>
2179   } catch (ParseException e) {
2180     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2181     errorLevel   = ERROR;
2182     errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2183     errorEnd     = SimpleCharStream.getPosition() + 1;
2184     throw e;
2185   }
2186   {inclusionStatement.sourceEnd = token2.sourceEnd;
2187   return inclusionStatement;}
2188 }
2189
2190 PrintExpression PrintExpression() :
2191 {
2192   final Expression expr;
2193   final int pos = SimpleCharStream.getPosition();
2194 }
2195 {
2196   <PRINT> expr = Expression() {return new PrintExpression(expr,pos,SimpleCharStream.getPosition());}
2197 }
2198
2199 ListExpression ListExpression() :
2200 {
2201   Expression expr = null;
2202   final Expression expression;
2203   final ArrayList list = new ArrayList();
2204   final int pos = SimpleCharStream.getPosition();
2205   final Token listToken;
2206 }
2207 {
2208   listToken = <LIST>
2209   try {
2210     <LPAREN>
2211   } catch (ParseException e) {
2212     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2213     errorLevel   = ERROR;
2214     errorStart   = listToken.sourceEnd+1;
2215     errorEnd     = listToken.sourceEnd+1;
2216     processParseExceptionDebug(e);
2217   }
2218   [
2219     expr = VariableDeclaratorId()
2220     {list.add(expr);}
2221   ]
2222   {if (expr == null) list.add(null);}
2223   (
2224     try {
2225       <COMMA>
2226     } catch (ParseException e) {
2227       errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2228       errorLevel   = ERROR;
2229       errorStart   = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2230       errorEnd     = SimpleCharStream.getPosition() + 1;
2231       throw e;
2232     }
2233     [expr = VariableDeclaratorId() {list.add(expr);}]
2234   )*
2235   try {
2236     <RPAREN>
2237   } catch (ParseException e) {
2238     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2239     errorLevel   = ERROR;
2240     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2241     errorEnd   = SimpleCharStream.getPosition() + 1;
2242     throw e;
2243   }
2244   [ <ASSIGN> expression = Expression()
2245     {
2246     final AbstractVariable[] vars = new AbstractVariable[list.size()];
2247     list.toArray(vars);
2248     return new ListExpression(vars,
2249                               expression,
2250                               pos,
2251                               SimpleCharStream.getPosition());}
2252   ]
2253   {
2254     final Variable[] vars = new Variable[list.size()];
2255     list.toArray(vars);
2256     return new ListExpression(vars,pos,SimpleCharStream.getPosition());}
2257 }
2258
2259 /**
2260  * An echo statement.
2261  * echo anyexpression (, otherexpression)*
2262  */
2263 EchoStatement EchoStatement() :
2264 {
2265   final ArrayList expressions = new ArrayList();
2266   Expression expr;
2267   Token token;
2268   Token token2 = null;
2269 }
2270 {
2271   token = <ECHO> expr = Expression()
2272   {expressions.add(expr);}
2273   (
2274     <COMMA> expr = Expression()
2275     {expressions.add(expr);}
2276   )*
2277   try {
2278     token2 = <SEMICOLON>
2279   } catch (ParseException e) {
2280     if (e.currentToken.next.kind != 4) {
2281       errorMessage = "';' expected after 'echo' statement";
2282       errorLevel   = ERROR;
2283       errorStart   = e.currentToken.sourceEnd;
2284       errorEnd     = e.currentToken.sourceEnd;
2285       processParseExceptionDebug(e);
2286     }
2287   }
2288   {
2289    final Expression[] exprs = new Expression[expressions.size()];
2290    expressions.toArray(exprs);
2291    if (token2 == null) {
2292      return new EchoStatement(exprs,token.sourceStart, exprs[exprs.length-1].sourceEnd);
2293    }
2294    return new EchoStatement(exprs,token.sourceStart, token2.sourceEnd);
2295    }
2296 }
2297
2298 GlobalStatement GlobalStatement() :
2299 {
2300    Variable expr;
2301    final ArrayList vars = new ArrayList();
2302    final GlobalStatement global;
2303    final Token token, token2;
2304 }
2305 {
2306   token = <GLOBAL>
2307     expr = Variable()
2308     {vars.add(expr);}
2309   (<COMMA>
2310     expr = Variable()
2311     {vars.add(expr);}
2312   )*
2313   try {
2314     token2 = <SEMICOLON>
2315     {
2316     final Variable[] variables = new Variable[vars.size()];
2317     vars.toArray(variables);
2318     global = new GlobalStatement(currentSegment,
2319                                  variables,
2320                                  token.sourceStart,
2321                                  token2.sourceEnd);
2322     currentSegment.add(global);
2323     return global;}
2324   } catch (ParseException e) {
2325     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2326     errorLevel   = ERROR;
2327     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2328     errorEnd   = SimpleCharStream.getPosition() + 1;
2329     throw e;
2330   }
2331 }
2332
2333 StaticStatement StaticStatement() :
2334 {
2335   final ArrayList vars = new ArrayList();
2336   VariableDeclaration expr;
2337   final Token token, token2;
2338 }
2339 {
2340   token = <STATIC> expr = VariableDeclarator() {vars.add(expr);}
2341   (
2342     <COMMA> expr = VariableDeclarator() {vars.add(expr);}
2343   )*
2344   try {
2345     token2 = <SEMICOLON>
2346     {
2347     final VariableDeclaration[] variables = new VariableDeclaration[vars.size()];
2348     vars.toArray(variables);
2349     return new StaticStatement(variables,
2350                                token.sourceStart,
2351                                token2.sourceEnd);}
2352   } catch (ParseException e) {
2353     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2354     errorLevel   = ERROR;
2355     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2356     errorEnd   = SimpleCharStream.getPosition() + 1;
2357     throw e;
2358   }
2359 }
2360
2361 LabeledStatement LabeledStatement() :
2362 {
2363   final Token label;
2364   final Statement statement;
2365 }
2366 {
2367   label = <IDENTIFIER> <COLON> statement = Statement()
2368   {return new LabeledStatement(label.image,statement,label.sourceStart,statement.sourceEnd);}
2369 }
2370
2371 /**
2372  * A Block is
2373  * {
2374  * statements
2375  * }.
2376  * @return a block
2377  */
2378 Block Block() :
2379 {
2380   final ArrayList list = new ArrayList();
2381   Statement statement;
2382   final Token token, token2;
2383 }
2384 {
2385   try {
2386     token = <LBRACE>
2387   } catch (ParseException e) {
2388     errorMessage = "'{' expected";
2389     errorLevel   = ERROR;
2390     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2391     errorEnd   = SimpleCharStream.getPosition() + 1;
2392     throw e;
2393   }
2394   ( statement = BlockStatement() {list.add(statement);}
2395   | statement = htmlBlock()      {list.add(statement);})*
2396   try {
2397     token2 = <RBRACE>
2398   } catch (ParseException e) {
2399     errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2400     errorLevel   = ERROR;
2401     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2402     errorEnd   = SimpleCharStream.getPosition() + 1;
2403     throw e;
2404   }
2405   {
2406   final Statement[] statements = new Statement[list.size()];
2407   list.toArray(statements);
2408   return new Block(statements,token.sourceStart,token2.sourceEnd);}
2409 }
2410
2411 Statement BlockStatement() :
2412 {
2413   final Statement statement;
2414 }
2415 {
2416   try {
2417     statement = Statement()         {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2418                                      return statement;}
2419   } catch (ParseException e) {
2420     errorMessage = "unexpected token : '"+ e.currentToken.image +"', a statement was expected";
2421     errorLevel   = ERROR;
2422     errorStart = e.currentToken.sourceStart;
2423     errorEnd   = e.currentToken.sourceEnd;
2424     throw e;
2425   }
2426 | statement = ClassDeclaration()  {return statement;}
2427 | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2428                                    currentSegment.add((MethodDeclaration) statement);
2429                                    ((MethodDeclaration) statement).analyzeCode();
2430                                    return statement;}
2431 }
2432
2433 /**
2434  * A Block statement that will not contain any 'break'
2435  */
2436 Statement BlockStatementNoBreak() :
2437 {
2438   final Statement statement;
2439 }
2440 {
2441   statement = StatementNoBreak()  {return statement;}
2442 | statement = ClassDeclaration()  {return statement;}
2443 | statement = MethodDeclaration() {currentSegment.add((MethodDeclaration) statement);
2444                                    ((MethodDeclaration) statement).analyzeCode();
2445                                    return statement;}
2446 }
2447
2448 /**
2449  * used only by ForInit()
2450  */
2451 Expression[] LocalVariableDeclaration() :
2452 {
2453   final ArrayList list = new ArrayList();
2454   Expression var;
2455 }
2456 {
2457   var = Expression()
2458   {list.add(var);}
2459   ( <COMMA> var = Expression() {list.add(var);})*
2460   {
2461     final Expression[] vars = new Expression[list.size()];
2462     list.toArray(vars);
2463     return vars;
2464   }
2465 }
2466
2467 /**
2468  * used only by LocalVariableDeclaration().
2469  */
2470 VariableDeclaration LocalVariableDeclarator() :
2471 {
2472   final Variable varName;
2473   Expression initializer = null;
2474 }
2475 {
2476   varName = Variable() [ <ASSIGN> initializer = Expression() ]
2477   {
2478    if (initializer == null) {
2479     return new VariableDeclaration(currentSegment,
2480                                    varName,
2481                                    varName.sourceStart,
2482                                    varName.sourceEnd);
2483    }
2484     return new VariableDeclaration(currentSegment,
2485                                    varName,
2486                                    initializer,
2487                                    VariableDeclaration.EQUAL,
2488                                    varName.sourceStart);
2489   }
2490 }
2491
2492 EmptyStatement EmptyStatement() :
2493 {
2494   final Token token;
2495 }
2496 {
2497   token = <SEMICOLON>
2498   {return new EmptyStatement(token.sourceStart,token.sourceEnd);}
2499 }
2500
2501 /**
2502  * used only by StatementExpressionList() which is used only by ForInit() and ForStatement()
2503  */
2504 Expression StatementExpression() :
2505 {
2506   final Expression expr;
2507 }
2508 {
2509   expr = PreIncDecExpression() {return expr;}
2510 |
2511   expr = PrimaryExpression()
2512   [ <PLUS_PLUS> {return new PostfixedUnaryExpression(expr,
2513                                                      OperatorIds.PLUS_PLUS,
2514                                                      SimpleCharStream.getPosition());}
2515   | <MINUS_MINUS> {return new PostfixedUnaryExpression(expr,
2516                                                        OperatorIds.MINUS_MINUS,
2517                                                        SimpleCharStream.getPosition());}
2518   ]
2519   {return expr;}
2520 }
2521
2522 SwitchStatement SwitchStatement() :
2523 {
2524   final Expression variable;
2525   final AbstractCase[] cases;
2526   final int pos = SimpleCharStream.getPosition();
2527   final Token switchToken;
2528 }
2529 {
2530   switchToken = <SWITCH>
2531   try {
2532     <LPAREN>
2533   } catch (ParseException e) {
2534     errorMessage = "'(' expected after 'switch'";
2535     errorLevel   = ERROR;
2536     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2537     errorEnd   = SimpleCharStream.getPosition() + 1;
2538     throw e;
2539   }
2540   try {
2541     variable = Expression()
2542   } catch (ParseException e) {
2543     if (errorMessage != null) {
2544       throw e;
2545     }
2546     errorMessage = "expression expected";
2547     errorLevel   = ERROR;
2548     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2549     errorEnd   = SimpleCharStream.getPosition() + 1;
2550     throw e;
2551   }
2552   try {
2553     <RPAREN>
2554   } catch (ParseException e) {
2555     errorMessage = "')' expected";
2556     errorLevel   = ERROR;
2557     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2558     errorEnd   = SimpleCharStream.getPosition() + 1;
2559     throw e;
2560   }
2561   (cases = switchStatementBrace() | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
2562   {return new SwitchStatement(variable,cases,switchToken.sourceStart,SimpleCharStream.getPosition());}
2563 }
2564
2565 AbstractCase[] switchStatementBrace() :
2566 {
2567   AbstractCase cas;
2568   final ArrayList cases = new ArrayList();
2569 }
2570 {
2571   <LBRACE>
2572  ( cas = switchLabel0() {cases.add(cas);})*
2573   try {
2574     <RBRACE>
2575     {
2576     final AbstractCase[] abcase = new AbstractCase[cases.size()];
2577     cases.toArray(abcase);
2578     return abcase;}
2579   } catch (ParseException e) {
2580     errorMessage = "'}' expected";
2581     errorLevel   = ERROR;
2582     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2583     errorEnd   = SimpleCharStream.getPosition() + 1;
2584     throw e;
2585   }
2586 }
2587 /**
2588  * A Switch statement with : ... endswitch;
2589  * @param start the begin offset of the switch
2590  * @param end the end offset of the switch
2591  */
2592 AbstractCase[] switchStatementColon(final int start, final int end) :
2593 {
2594   AbstractCase cas;
2595   final ArrayList cases = new ArrayList();
2596 }
2597 {
2598   <COLON>
2599   {try {
2600   setMarker(fileToParse,
2601             "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2602             start,
2603             end,
2604             INFO,
2605             "Line " + token.beginLine);
2606   } catch (CoreException e) {
2607     PHPeclipsePlugin.log(e);
2608   }}
2609   ( cas = switchLabel0() {cases.add(cas);})*
2610   try {
2611     <ENDSWITCH>
2612   } catch (ParseException e) {
2613     errorMessage = "'endswitch' expected";
2614     errorLevel   = ERROR;
2615     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2616     errorEnd   = SimpleCharStream.getPosition() + 1;
2617     throw e;
2618   }
2619   try {
2620     <SEMICOLON>
2621     {
2622     final AbstractCase[] abcase = new AbstractCase[cases.size()];
2623     cases.toArray(abcase);
2624     return abcase;}
2625   } catch (ParseException e) {
2626     errorMessage = "';' expected after 'endswitch' keyword";
2627     errorLevel   = ERROR;
2628     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2629     errorEnd   = SimpleCharStream.getPosition() + 1;
2630     throw e;
2631   }
2632 }
2633
2634 AbstractCase switchLabel0() :
2635 {
2636   final Expression expr;
2637   Statement statement;
2638   final ArrayList stmts = new ArrayList();
2639   final int pos = SimpleCharStream.getPosition();
2640 }
2641 {
2642   expr = SwitchLabel()
2643   ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2644   | statement = htmlBlock()             {stmts.add(statement);})*
2645   [ statement = BreakStatement()        {stmts.add(statement);}]
2646   {
2647   final Statement[] stmtsArray = new Statement[stmts.size()];
2648   stmts.toArray(stmtsArray);
2649   if (expr == null) {//it's a default
2650     return new DefaultCase(stmtsArray,pos,SimpleCharStream.getPosition());
2651   }
2652   return new Case(expr,stmtsArray,pos,SimpleCharStream.getPosition());}
2653 }
2654
2655 /**
2656  * A SwitchLabel.
2657  * case Expression() :
2658  * default :
2659  * @return the if it was a case and null if not
2660  */
2661 Expression SwitchLabel() :
2662 {
2663   final Expression expr;
2664 }
2665 {
2666   token = <CASE>
2667   try {
2668     expr = Expression()
2669   } catch (ParseException e) {
2670     if (errorMessage != null) throw e;
2671     errorMessage = "expression expected after 'case' keyword";
2672     errorLevel   = ERROR;
2673     errorStart = token.sourceEnd +1;
2674     errorEnd   = token.sourceEnd +1;
2675     throw e;
2676   }
2677   try {
2678     token = <COLON>
2679     {return expr;}
2680   } catch (ParseException e) {
2681     errorMessage = "':' expected after case expression";
2682     errorLevel   = ERROR;
2683     errorStart = expr.sourceEnd+1;
2684     errorEnd   = expr.sourceEnd+1;
2685     processParseExceptionDebug(e);
2686   }
2687 |
2688   token = <_DEFAULT>
2689   try {
2690     <COLON>
2691     {return null;}
2692   } catch (ParseException e) {
2693     errorMessage = "':' expected after 'default' keyword";
2694     errorLevel   = ERROR;
2695     errorStart = token.sourceEnd+1;
2696     errorEnd   = token.sourceEnd+1;
2697     processParseExceptionDebug(e);
2698   }
2699 }
2700
2701 Break BreakStatement() :
2702 {
2703   Expression expression = null;
2704   final Token token, token2;
2705 }
2706 {
2707   token = <BREAK> [ expression = Expression() ]
2708   try {
2709     token2 = <SEMICOLON>
2710   } catch (ParseException e) {
2711     errorMessage = "';' expected after 'break' keyword";
2712     errorLevel   = ERROR;
2713     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2714     errorEnd   = SimpleCharStream.getPosition() + 1;
2715     throw e;
2716   }
2717   {return new Break(expression, token.sourceStart, token2.sourceEnd);}
2718 }
2719
2720 IfStatement IfStatement() :
2721 {
2722   final Expression condition;
2723   final IfStatement ifStatement;
2724   Token token;
2725 }
2726 {
2727   token = <IF> condition = Condition("if")
2728   ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
2729   {return ifStatement;}
2730 }
2731
2732
2733 Expression Condition(final String keyword) :
2734 {
2735   final Expression condition;
2736 }
2737 {
2738   try {
2739     <LPAREN>
2740   } catch (ParseException e) {
2741     errorMessage = "'(' expected after " + keyword + " keyword";
2742     errorLevel   = ERROR;
2743     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length();
2744     errorEnd   = errorStart +1;
2745     processParseExceptionDebug(e);
2746   }
2747   condition = Expression()
2748   try {
2749      <RPAREN>
2750   } catch (ParseException e) {
2751     errorMessage = "')' expected after " + keyword + " keyword";
2752     errorLevel   = ERROR;
2753     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2754     errorEnd   = SimpleCharStream.getPosition() + 1;
2755     processParseExceptionDebug(e);
2756   }
2757   {return condition;}
2758 }
2759
2760 IfStatement IfStatement0(final Expression condition, final int start,final int end) :
2761 {
2762   Statement statement;
2763   final Statement stmt;
2764   final Statement[] statementsArray;
2765   ElseIf elseifStatement;
2766   Else elseStatement = null;
2767   final ArrayList stmts;
2768   final ArrayList elseIfList = new ArrayList();
2769   final ElseIf[] elseIfs;
2770   int pos = SimpleCharStream.getPosition();
2771   final int endStatements;
2772 }
2773 {
2774   <COLON>
2775   {stmts = new ArrayList();}
2776   (  statement = Statement() {stmts.add(statement);}
2777    | statement = htmlBlock() {stmts.add(statement);})*
2778    {endStatements = SimpleCharStream.getPosition();}
2779    (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2780    [elseStatement = ElseStatementColon()]
2781
2782   {try {
2783   setMarker(fileToParse,
2784             "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2785             start,
2786             end,
2787             INFO,
2788             "Line " + token.beginLine);
2789   } catch (CoreException e) {
2790     PHPeclipsePlugin.log(e);
2791   }}
2792   try {
2793     <ENDIF>
2794   } catch (ParseException e) {
2795     errorMessage = "'endif' expected";
2796     errorLevel   = ERROR;
2797     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2798     errorEnd   = SimpleCharStream.getPosition() + 1;
2799     throw e;
2800   }
2801   try {
2802     <SEMICOLON>
2803   } catch (ParseException e) {
2804     errorMessage = "';' expected after 'endif' keyword";
2805     errorLevel   = ERROR;
2806     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2807     errorEnd   = SimpleCharStream.getPosition() + 1;
2808     throw e;
2809   }
2810     {
2811     elseIfs = new ElseIf[elseIfList.size()];
2812     elseIfList.toArray(elseIfs);
2813     if (stmts.size() == 1) {
2814       return new IfStatement(condition,
2815                              (Statement) stmts.get(0),
2816                               elseIfs,
2817                               elseStatement,
2818                               pos,
2819                               SimpleCharStream.getPosition());
2820     } else {
2821       statementsArray = new Statement[stmts.size()];
2822       stmts.toArray(statementsArray);
2823       return new IfStatement(condition,
2824                              new Block(statementsArray,pos,endStatements),
2825                              elseIfs,
2826                              elseStatement,
2827                              pos,
2828                              SimpleCharStream.getPosition());
2829     }
2830     }
2831
2832 |
2833   (stmt = Statement() | stmt = htmlBlock())
2834   ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
2835   [ LOOKAHEAD(1)
2836     <ELSE>
2837     try {
2838       {pos = SimpleCharStream.getPosition();}
2839       statement = Statement()
2840       {elseStatement = new Else(statement,pos,SimpleCharStream.getPosition());}
2841     } catch (ParseException e) {
2842       if (errorMessage != null) {
2843         throw e;
2844       }
2845       errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
2846       errorLevel   = ERROR;
2847       errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2848       errorEnd   = SimpleCharStream.getPosition() + 1;
2849       throw e;
2850     }
2851   ]
2852   {
2853     elseIfs = new ElseIf[elseIfList.size()];
2854     elseIfList.toArray(elseIfs);
2855     return new IfStatement(condition,
2856                            stmt,
2857                            elseIfs,
2858                            elseStatement,
2859                            pos,
2860                            SimpleCharStream.getPosition());}
2861 }
2862
2863 ElseIf ElseIfStatementColon() :
2864 {
2865   final Expression condition;
2866   Statement statement;
2867   final ArrayList list = new ArrayList();
2868   final int pos = SimpleCharStream.getPosition();
2869 }
2870 {
2871   <ELSEIF> condition = Condition("elseif")
2872   <COLON> (  statement = Statement() {list.add(statement);}
2873            | statement = htmlBlock() {list.add(statement);})*
2874   {
2875   final Statement[] stmtsArray = new Statement[list.size()];
2876   list.toArray(stmtsArray);
2877   return new ElseIf(condition,stmtsArray ,pos,SimpleCharStream.getPosition());}
2878 }
2879
2880 Else ElseStatementColon() :
2881 {
2882   Statement statement;
2883   final ArrayList list = new ArrayList();
2884   final int pos = SimpleCharStream.getPosition();
2885 }
2886 {
2887   <ELSE> <COLON> (  statement = Statement() {list.add(statement);}
2888                   | statement = htmlBlock() {list.add(statement);})*
2889   {
2890   final Statement[] stmtsArray = new Statement[list.size()];
2891   list.toArray(stmtsArray);
2892   return new Else(stmtsArray,pos,SimpleCharStream.getPosition());}
2893 }
2894
2895 ElseIf ElseIfStatement() :
2896 {
2897   final Expression condition;
2898   final Statement statement;
2899   final ArrayList list = new ArrayList();
2900   final int pos = SimpleCharStream.getPosition();
2901 }
2902 {
2903   <ELSEIF> condition = Condition("elseif") statement = Statement() {list.add(statement);/*todo:do better*/}
2904   {
2905   final Statement[] stmtsArray = new Statement[list.size()];
2906   list.toArray(stmtsArray);
2907   return new ElseIf(condition,stmtsArray,pos,SimpleCharStream.getPosition());}
2908 }
2909
2910 WhileStatement WhileStatement() :
2911 {
2912   final Expression condition;
2913   final Statement action;
2914   final int pos = SimpleCharStream.getPosition();
2915 }
2916 {
2917   <WHILE>
2918     condition = Condition("while")
2919     action    = WhileStatement0(pos,pos + 5)
2920     {return new WhileStatement(condition,action,pos,SimpleCharStream.getPosition());}
2921 }
2922
2923 Statement WhileStatement0(final int start, final int end) :
2924 {
2925   Statement statement;
2926   final ArrayList stmts = new ArrayList();
2927   final int pos = SimpleCharStream.getPosition();
2928 }
2929 {
2930   <COLON> (statement = Statement() {stmts.add(statement);})*
2931   {try {
2932   setMarker(fileToParse,
2933             "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
2934             start,
2935             end,
2936             INFO,
2937             "Line " + token.beginLine);
2938   } catch (CoreException e) {
2939     PHPeclipsePlugin.log(e);
2940   }}
2941   try {
2942     <ENDWHILE>
2943   } catch (ParseException e) {
2944     errorMessage = "'endwhile' expected";
2945     errorLevel   = ERROR;
2946     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2947     errorEnd   = SimpleCharStream.getPosition() + 1;
2948     throw e;
2949   }
2950   try {
2951     <SEMICOLON>
2952     {
2953     final Statement[] stmtsArray = new Statement[stmts.size()];
2954     stmts.toArray(stmtsArray);
2955     return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
2956   } catch (ParseException e) {
2957     errorMessage = "';' expected after 'endwhile' keyword";
2958     errorLevel   = ERROR;
2959     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2960     errorEnd   = SimpleCharStream.getPosition() + 1;
2961     throw e;
2962   }
2963 |
2964   statement = Statement()
2965   {return statement;}
2966 }
2967
2968 DoStatement DoStatement() :
2969 {
2970   final Statement action;
2971   final Expression condition;
2972   final Token token, token2;
2973 }
2974 {
2975   token = <DO> action = Statement() <WHILE> condition = Condition("while")
2976   try {
2977     token2 = <SEMICOLON>
2978     {return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);}
2979   } catch (ParseException e) {
2980     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2981     errorLevel   = ERROR;
2982     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2983     errorEnd   = SimpleCharStream.getPosition() + 1;
2984     throw e;
2985   }
2986 }
2987
2988 ForeachStatement ForeachStatement() :
2989 {
2990   Statement statement;
2991   Expression expression;
2992   ArrayVariableDeclaration variable;
2993   Token token;
2994 }
2995 {
2996   token = <FOREACH>
2997     try {
2998     <LPAREN>
2999   } catch (ParseException e) {
3000     errorMessage = "'(' expected after 'foreach' keyword";
3001     errorLevel   = ERROR;
3002     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3003     errorEnd   = SimpleCharStream.getPosition() + 1;
3004     throw e;
3005   }
3006   try {
3007     expression = Expression()
3008   } catch (ParseException e) {
3009     errorMessage = "variable expected";
3010     errorLevel   = ERROR;
3011     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3012     errorEnd   = SimpleCharStream.getPosition() + 1;
3013     throw e;
3014   }
3015   try {
3016     <AS>
3017   } catch (ParseException e) {
3018     errorMessage = "'as' expected";
3019     errorLevel   = ERROR;
3020     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3021     errorEnd   = SimpleCharStream.getPosition() + 1;
3022     throw e;
3023   }
3024   try {
3025     variable = ArrayVariable()
3026   } catch (ParseException e) {
3027     errorMessage = "variable expected";
3028     errorLevel   = ERROR;
3029     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3030     errorEnd   = SimpleCharStream.getPosition() + 1;
3031     throw e;
3032   }
3033   try {
3034     <RPAREN>
3035   } catch (ParseException e) {
3036     errorMessage = "')' expected after 'foreach' keyword";
3037     errorLevel   = ERROR;
3038     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3039     errorEnd   = SimpleCharStream.getPosition() + 1;
3040     throw e;
3041   }
3042   try {
3043     statement = Statement()
3044   } catch (ParseException e) {
3045     if (errorMessage != null) throw e;
3046     errorMessage = "statement expected";
3047     errorLevel   = ERROR;
3048     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3049     errorEnd   = SimpleCharStream.getPosition() + 1;
3050     throw e;
3051   }
3052   {return new ForeachStatement(expression,
3053                                variable,
3054                                statement,
3055                                token.sourceStart,
3056                                statement.sourceEnd);}
3057
3058 }
3059
3060 /**
3061  * a for declaration.
3062  * @return a node representing the for statement
3063  */
3064 ForStatement ForStatement() :
3065 {
3066 final Token token,token2;
3067 final int pos = SimpleCharStream.getPosition();
3068 Expression[] initializations = null;
3069 Expression condition = null;
3070 Expression[] increments = null;
3071 Statement action;
3072 final ArrayList list = new ArrayList();
3073 }
3074 {
3075   token = <FOR>
3076   try {
3077     <LPAREN>
3078   } catch (ParseException e) {
3079     errorMessage = "'(' expected after 'for' keyword";
3080     errorLevel   = ERROR;
3081     errorStart = token.sourceEnd;
3082     errorEnd   = token.sourceEnd +1;
3083     processParseExceptionDebug(e);
3084   }
3085      [ initializations = ForInit() ] <SEMICOLON>
3086      [ condition = Expression() ] <SEMICOLON>
3087      [ increments = StatementExpressionList() ] <RPAREN>
3088     (
3089       action = Statement()
3090       {return new ForStatement(initializations,
3091                                condition,
3092                                increments,
3093                                action,
3094                                token.sourceStart,
3095                                action.sourceEnd);}
3096     |
3097       <COLON>
3098       (action = Statement() {list.add(action);})*
3099       {
3100         try {
3101         setMarker(fileToParse,
3102                   "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
3103                   token.sourceStart,
3104                   token.sourceEnd,
3105                   INFO,
3106                   "Line " + token.beginLine);
3107         } catch (CoreException e) {
3108           PHPeclipsePlugin.log(e);
3109         }
3110       }
3111       try {
3112         <ENDFOR>
3113       } catch (ParseException e) {
3114         errorMessage = "'endfor' expected";
3115         errorLevel   = ERROR;
3116         errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3117         errorEnd   = SimpleCharStream.getPosition() + 1;
3118         throw e;
3119       }
3120       try {
3121         token2 = <SEMICOLON>
3122         {
3123         final Statement[] stmtsArray = new Statement[list.size()];
3124         list.toArray(stmtsArray);
3125         return new ForStatement(initializations,
3126                                 condition,
3127                                 increments,
3128                                 new Block(stmtsArray,
3129                                           stmtsArray[0].sourceStart,
3130                                           stmtsArray[stmtsArray.length-1].sourceEnd),
3131                                 token.sourceStart,
3132                                 token2.sourceEnd);}
3133       } catch (ParseException e) {
3134         errorMessage = "';' expected after 'endfor' keyword";
3135         errorLevel   = ERROR;
3136         errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3137         errorEnd   = SimpleCharStream.getPosition() + 1;
3138         throw e;
3139       }
3140     )
3141 }
3142
3143 Expression[] ForInit() :
3144 {
3145   final Expression[] exprs;
3146 }
3147 {
3148   LOOKAHEAD(LocalVariableDeclaration())
3149   exprs = LocalVariableDeclaration()
3150   {return exprs;}
3151 |
3152   exprs = StatementExpressionList()
3153   {return exprs;}
3154 }
3155
3156 Expression[] StatementExpressionList() :
3157 {
3158   final ArrayList list = new ArrayList();
3159   final Expression expr;
3160 }
3161 {
3162   expr = Expression()   {list.add(expr);}
3163   (<COMMA> Expression() {list.add(expr);})*
3164   {
3165     final Expression[] exprsArray = new Expression[list.size()];
3166     list.toArray(exprsArray);
3167     return exprsArray;
3168   }
3169 }
3170
3171 Continue ContinueStatement() :
3172 {
3173   Expression expr = null;
3174   final Token token,token2;
3175 }
3176 {
3177   token = <CONTINUE> [ expr = Expression() ]
3178   try {
3179     token2 = <SEMICOLON>
3180     {return new Continue(expr,token.sourceStart,token2.sourceEnd);}
3181   } catch (ParseException e) {
3182     errorMessage = "';' expected after 'continue' statement";
3183     errorLevel   = ERROR;
3184     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3185     errorEnd   = SimpleCharStream.getPosition() + 1;
3186     throw e;
3187   }
3188 }
3189
3190 ReturnStatement ReturnStatement() :
3191 {
3192   Expression expr = null;
3193   final Token token,token2;
3194 }
3195 {
3196   token = <RETURN> [ expr = Expression() ]
3197   try {
3198     token2 = <SEMICOLON>
3199     {return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd);}
3200   } catch (ParseException e) {
3201     errorMessage = "';' expected after 'return' statement";
3202     errorLevel   = ERROR;
3203     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3204     errorEnd   = SimpleCharStream.getPosition() + 1;
3205     throw e;
3206  }
3207 }
3208