many changes and fixes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / test / PHPParser.jj
index 2c1b336..84d3954 100644 (file)
@@ -3,7 +3,7 @@ options {
   CHOICE_AMBIGUITY_CHECK = 2;
   OTHER_AMBIGUITY_CHECK = 1;
   STATIC = true;
-  DEBUG_PARSER = true;
+  DEBUG_PARSER = false;
   DEBUG_LOOKAHEAD = false;
   DEBUG_TOKEN_MANAGER = false;
   OPTIMIZE_TOKEN_MANAGER = false;
@@ -17,6 +17,7 @@ options {
   BUILD_TOKEN_MANAGER = true;
   SANITY_CHECK = true;
   FORCE_LA_CHECK = false;
+  COMMON_TOKEN_ACTION = true;
 }
 
 PARSER_BEGIN(PHPParser)
@@ -81,7 +82,7 @@ public final class PHPParser extends PHPParserSuperclass {
   /** The cursor in expression stack. */
   private static int nodePtr;
 
-  private static final boolean PARSER_DEBUG = true;
+  private static final boolean PARSER_DEBUG = false;
 
   public final void setFileToParse(final IFile fileToParse) {
     PHPParser.fileToParse = fileToParse;
@@ -199,8 +200,8 @@ public final class PHPParser extends PHPParserSuperclass {
     if (errorMessage == null) {
       PHPeclipsePlugin.log(e);
       errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it";
-      errorStart = SimpleCharStream.getPosition();
-      errorEnd   = errorStart + 1;
+      errorStart = e.currentToken.sourceStart;
+      errorEnd   = e.currentToken.sourceEnd;
     }
     setMarker(e);
     errorMessage = null;
@@ -349,6 +350,18 @@ public final class PHPParser extends PHPParserSuperclass {
 
 PARSER_END(PHPParser)
 
+TOKEN_MGR_DECLS:
+{
+  // CommonTokenAction: use the begins/ends fields added to the Jack
+  // CharStream class to set corresponding fields in each Token (which was
+  // also extended with new fields). By default Jack doesn't supply absolute
+  // offsets, just line/column offsets
+  static void CommonTokenAction(Token t) {
+    t.sourceStart = input_stream.beginOffset;
+    t.sourceEnd = input_stream.endOffset;
+  } // CommonTokenAction
+} // TOKEN_MGR_DECLS
+
 <DEFAULT> TOKEN :
 {
   <PHPSTARTSHORT : "<?">    {PHPParser.createNewHTMLCode();} : PHPPARSING
@@ -758,7 +771,7 @@ ClassDeclaration ClassDeclaration() :
   }
   ClassBody(classDeclaration)
   {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
-   classDeclaration.setSourceEnd(SimpleCharStream.getPosition());
+   classDeclaration.sourceEnd = SimpleCharStream.getPosition();
    pushOnAstNodes(classDeclaration);
    return classDeclaration;}
 }
@@ -866,12 +879,16 @@ VariableDeclaration VariableDeclaratorNoSuffix() :
   {
   if (initializer == null) {
     return new VariableDeclaration(currentSegment,
-                                   new Variable(varName.image.substring(1),SimpleCharStream.getPosition()-varName.image.length()-1,SimpleCharStream.getPosition()),
+                                   new Variable(varName.image.substring(1),
+                                                varName.sourceStart,
+                                                varName.sourceEnd),
                                    pos,
                                    SimpleCharStream.getPosition());
   }
   return new VariableDeclaration(currentSegment,
-                                 new Variable(varName.image.substring(1),SimpleCharStream.getPosition()-varName.image.length()-1,SimpleCharStream.getPosition()),
+                                 new Variable(varName.image.substring(1),
+                                              varName.sourceStart,
+                                              varName.sourceEnd),
                                  initializer,
                                  VariableDeclaration.EQUAL,
                                  pos);
@@ -965,7 +982,7 @@ Variable Variable():
   [<LBRACE> expression = Expression() <RBRACE>]
   {
     if (expression == null) {
-      return new Variable(token.image.substring(1),pos,SimpleCharStream.getPosition());
+      return new Variable(token.image.substring(1),token.sourceStart,token.sourceEnd);
     }
     String s = expression.toStringExpression();
     buff = new StringBuffer(token.image.length()+s.length()+2);
@@ -974,12 +991,12 @@ Variable Variable():
     buff.append(s);
     buff.append("}");
     s = buff.toString();
-    return new Variable(s,pos,SimpleCharStream.getPosition());
+    return new Variable(s,token.sourceStart,token.sourceEnd);
   }
 |
-  <DOLLAR> {pos = SimpleCharStream.getPosition()-1;}
+  token = <DOLLAR>
   expr = VariableName()
-  {return new Variable(expr,pos,SimpleCharStream.getPosition());}
+  {return new Variable(expr,token.sourceStart,expr.sourceEnd);}
 }
 
 /**
@@ -993,12 +1010,13 @@ Variable VariableName():
   final Variable var;
   Expression expression = null;
   final Token token;
+  Token token2 = null;
   int pos;
 }
 {
-  <LBRACE>
+  token = <LBRACE>
   {pos = SimpleCharStream.getPosition()-1;}
-  expression = Expression() <RBRACE>
+  expression = Expression() token2 = <RBRACE>
   {expr = expression.toStringExpression();
    buff = new StringBuffer(expr.length()+2);
    buff.append("{");
@@ -1007,19 +1025,19 @@ Variable VariableName():
    pos = SimpleCharStream.getPosition();
    expr = buff.toString();
    return new Variable(expr,
-                       pos,
-                       SimpleCharStream.getPosition());
+                       token.sourceStart,
+                       token2.sourceEnd);
 
    }
 |
   token = <IDENTIFIER>
   {pos = SimpleCharStream.getPosition() - token.image.length();}
-  [<LBRACE> expression = Expression() <RBRACE>]
+  [<LBRACE> expression = Expression() token2 = <RBRACE>]
   {
     if (expression == null) {
       return new Variable(token.image,
-                          pos,
-                          SimpleCharStream.getPosition());
+                          token.sourceStart,
+                          token.sourceEnd);
     }
     expr = expression.toStringExpression();
     buff = new StringBuffer(token.image.length()+expr.length()+2);
@@ -1029,56 +1047,50 @@ Variable VariableName():
     buff.append("}");
     expr = buff.toString();
     return new Variable(expr,
-                        pos,
-                        SimpleCharStream.getPosition());
+                        token.sourceStart,
+                        token2.sourceEnd);
   }
 |
-  <DOLLAR> {pos = SimpleCharStream.getPosition() - 1;}
+  <DOLLAR>
   var = VariableName()
   {
     return new Variable(var,
-                        pos,
-                        SimpleCharStream.getPosition());
+                        var.sourceStart-1,
+                        var.sourceEnd);
   }
 |
   token = <DOLLAR_ID>
   {
-  pos = SimpleCharStream.getPosition();
   return new Variable(token.image,
-                      pos-token.image.length(),
-                      pos);
+                      token.sourceStart,
+                      token.sourceEnd);
   }
 }
 
 Expression VariableInitializer() :
 {
   final Expression expr;
-  final Token token;
-  final int pos = SimpleCharStream.getPosition();
+  final Token token, token2;
 }
 {
   expr = Literal()
   {return expr;}
 |
-  <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
-  {return new PrefixedUnaryExpression(new NumberLiteral(token.image.toCharArray(),
-                                                        pos,
-                                                        SimpleCharStream.getPosition()),
+  token2 = <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
+  {return new PrefixedUnaryExpression(new NumberLiteral(token),
                                       OperatorIds.MINUS,
-                                      pos);}
+                                      token2.sourceStart);}
 |
-  <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
-  {return new PrefixedUnaryExpression(new NumberLiteral(token.image.toCharArray(),
-                                                        pos,
-                                                        SimpleCharStream.getPosition()),
+  token2 = <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
+  {return new PrefixedUnaryExpression(new NumberLiteral(token),
                                       OperatorIds.PLUS,
-                                      pos);}
+                                      token2.sourceStart);}
 |
   expr = ArrayDeclarator()
   {return expr;}
 |
   token = <IDENTIFIER>
-  {return new ConstantIdentifier(token.image.toCharArray(),pos,SimpleCharStream.getPosition());}
+  {return new ConstantIdentifier(token);}
 }
 
 ArrayVariableDeclaration ArrayVariable() :
@@ -1241,26 +1253,17 @@ VariableDeclaration FormalParameter() :
 }
 
 ConstantIdentifier Type() :
-{final int pos;}
-{
-  <STRING>             {pos = SimpleCharStream.getPosition();
-                        return new ConstantIdentifier(Types.STRING,pos,pos-6);}
-| <BOOL>               {pos = SimpleCharStream.getPosition();
-                        return new ConstantIdentifier(Types.BOOL,pos,pos-4);}
-| <BOOLEAN>            {pos = SimpleCharStream.getPosition();
-                        return new ConstantIdentifier(Types.BOOLEAN,pos,pos-7);}
-| <REAL>               {pos = SimpleCharStream.getPosition();
-                        return new ConstantIdentifier(Types.REAL,pos,pos-4);}
-| <DOUBLE>             {pos = SimpleCharStream.getPosition();
-                        return new ConstantIdentifier(Types.DOUBLE,pos,pos-5);}
-| <FLOAT>              {pos = SimpleCharStream.getPosition();
-                        return new ConstantIdentifier(Types.FLOAT,pos,pos-5);}
-| <INT>                {pos = SimpleCharStream.getPosition();
-                        return new ConstantIdentifier(Types.INT,pos,pos-3);}
-| <INTEGER>            {pos = SimpleCharStream.getPosition();
-                        return new ConstantIdentifier(Types.INTEGER,pos,pos-7);}
-| <OBJECT>             {pos = SimpleCharStream.getPosition();
-                        return new ConstantIdentifier(Types.OBJECT,pos,pos-6);}
+{final Token token;}
+{
+  token = <STRING>    {return new ConstantIdentifier(token);}
+| token = <BOOL>      {return new ConstantIdentifier(token);}
+| token = <BOOLEAN>   {return new ConstantIdentifier(token);}
+| token = <REAL>      {return new ConstantIdentifier(token);}
+| token = <DOUBLE>    {return new ConstantIdentifier(token);}
+| token = <FLOAT>     {return new ConstantIdentifier(token);}
+| token = <INT>       {return new ConstantIdentifier(token);}
+| token = <INTEGER>   {return new ConstantIdentifier(token);}
+| token = <OBJECT>    {return new ConstantIdentifier(token);}
 }
 
 Expression Expression() :
@@ -1292,14 +1295,17 @@ Expression Expression() :
         if (expr instanceof AbstractVariable) {
           return new VariableDeclaration(currentSegment,
                                          (AbstractVariable) expr,
-                                         pos,
-                                         SimpleCharStream.getPosition());
+                                         initializer,
+                                         expr.sourceStart,
+                                         initializer.sourceEnd);
         }
         String varName = expr.toStringExpression().substring(1);
         return new VariableDeclaration(currentSegment,
-                                       new Variable(varName,SimpleCharStream.getPosition()-varName.length()-1,SimpleCharStream.getPosition()),
-                                       pos,
-                                       SimpleCharStream.getPosition());
+                                       new Variable(varName,
+                                                    expr.sourceStart,
+                                                    expr.sourceEnd),
+                                       expr.sourceStart,
+                                       initializer.sourceEnd);
     }
     return expr;
   }
@@ -1662,37 +1668,37 @@ CastExpression CastExpression() :
 {
 final ConstantIdentifier type;
 final Expression expr;
-final int pos = SimpleCharStream.getPosition();
+final Token token,token1;
 }
 {
-  <LPAREN>
+  token1 = <LPAREN>
   (
       type = Type()
     |
-      <ARRAY> {type = new ConstantIdentifier(Types.ARRAY,pos,SimpleCharStream.getPosition());}
+      token = <ARRAY> {type = new ConstantIdentifier(token);}
   )
   <RPAREN> expr = UnaryExpression()
-  {return new CastExpression(type,expr,pos,SimpleCharStream.getPosition());}
+  {return new CastExpression(type,expr,token1.sourceStart,expr.sourceEnd);}
 }
 
 Expression PostfixExpression() :
 {
   final Expression expr;
   int operator = -1;
-  final int pos = SimpleCharStream.getPosition();
+  Token token = null;
 }
 {
   expr = PrimaryExpression()
   [
-      <PLUS_PLUS>   {operator = OperatorIds.PLUS_PLUS;}
+      token = <PLUS_PLUS>   {operator = OperatorIds.PLUS_PLUS;}
     |
-      <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
+      token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
   ]
   {
     if (operator == -1) {
       return expr;
     }
-    return new PostfixedUnaryExpression(expr,operator,pos);
+    return new PostfixedUnaryExpression(expr,operator,token.sourceEnd);
   }
 }
 
@@ -1708,10 +1714,7 @@ Expression PrimaryExpression() :
 {
   token = <IDENTIFIER>
   {
-    pos = SimpleCharStream.getPosition();
-    expr = new ConstantIdentifier(token.image.toCharArray(),
-                                  pos-token.image.length(),
-                                  pos);
+    expr = new ConstantIdentifier(token);
   }
   (
     <STATICCLASSACCESS> expr2 = ClassIdentifier()
@@ -1726,12 +1729,11 @@ Expression PrimaryExpression() :
   [ expr = Arguments(expr) ]
   {return expr;}
 |
-  <NEW>
-  {pos = SimpleCharStream.getPosition();}
+  token = <NEW>
   expr = ClassIdentifier()
   {expr = new PrefixedUnaryExpression(expr,
                                       OperatorIds.NEW,
-                                      pos-3);
+                                      token.sourceStart);
   }
   [ expr = Arguments(expr) ]
   {return expr;}
@@ -1759,21 +1761,23 @@ PrefixedUnaryExpression classInstantiation() :
 {
   Expression expr;
   final StringBuffer buff;
-  final int pos = SimpleCharStream.getPosition();
+  final int pos;
+  final Token token;
 }
 {
-  <NEW> expr = ClassIdentifier()
+  token = <NEW> expr = ClassIdentifier()
   [
-    {buff = new StringBuffer(expr.toStringExpression());}
+    {pos = expr.sourceStart;
+    buff = new StringBuffer(expr.toStringExpression());}
     expr = PrimaryExpression()
     {buff.append(expr.toStringExpression());
-    expr = new ConstantIdentifier(buff.toString().toCharArray(),
-                                  pos,
-                                  SimpleCharStream.getPosition());}
+    expr = new ConstantIdentifier(buff.toString(),
+                                  expr.sourceStart,
+                                  expr.sourceEnd);}
   ]
   {return new PrefixedUnaryExpression(expr,
                                       OperatorIds.NEW,
-                                      pos);}
+                                      token.sourceStart);}
 }
 
 Expression ClassIdentifier():
@@ -1783,11 +1787,7 @@ Expression ClassIdentifier():
   final ConstantIdentifier type;
 }
 {
-  token = <IDENTIFIER>
-  {final int pos = SimpleCharStream.getPosition();
-   return new ConstantIdentifier(token.image.toCharArray(),
-                                 pos-token.image.length(),
-                                 pos);}
+  token = <IDENTIFIER>          {return new ConstantIdentifier(token);}
 | expr = Type()                 {return expr;}
 | expr = VariableDeclaratorId() {return expr;}
 }
@@ -1832,21 +1832,14 @@ AbstractVariable VariableSuffix(final AbstractVariable prefix) :
 Literal Literal() :
 {
   final Token token;
-  final int pos;
 }
 {
-  token = <INTEGER_LITERAL>        {pos = SimpleCharStream.getPosition();
-                                    return new NumberLiteral(token.image.toCharArray(),pos-token.image.length(),pos);}
-| token = <FLOATING_POINT_LITERAL> {pos = SimpleCharStream.getPosition();
-                                    return new NumberLiteral(token.image.toCharArray(),pos-token.image.length(),pos);}
-| token = <STRING_LITERAL>         {pos = SimpleCharStream.getPosition();
-                                    return new StringLiteral(token.image.toCharArray(),pos-token.image.length());}
-| <TRUE>                           {pos = SimpleCharStream.getPosition();
-                                    return new TrueLiteral(pos-4,pos);}
-| <FALSE>                          {pos = SimpleCharStream.getPosition();
-                                    return new FalseLiteral(pos-4,pos);}
-| <NULL>                           {pos = SimpleCharStream.getPosition();
-                                    return new NullLiteral(pos-4,pos);}
+  token = <INTEGER_LITERAL>        {return new NumberLiteral(token);}
+| token = <FLOATING_POINT_LITERAL> {return new NumberLiteral(token);}
+| token = <STRING_LITERAL>         {return new StringLiteral(token);}
+| token = <TRUE>                   {return new TrueLiteral(token);}
+| token = <FALSE>                  {return new FalseLiteral(token);}
+| token = <NULL>                   {return new NullLiteral(token);}
 }
 
 FunctionCall Arguments(final Expression func) :