X-Git-Url: http://secure.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/test/PHPParser.jj b/net.sourceforge.phpeclipse/src/test/PHPParser.jj index d45ca07..afc5890 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj @@ -87,7 +87,7 @@ public final class PHPParser extends PHPParserSuperclass { /** The cursor in expression stack. */ private static int nodePtr; - private static final boolean PARSER_DEBUG = true; + public static final boolean PARSER_DEBUG = true; public final void setFileToParse(final IFile fileToParse) { PHPParser.fileToParse = fileToParse; @@ -190,6 +190,13 @@ public final class PHPParser extends PHPParserSuperclass { return outlineInfo; } + /** + * This function will throw the exception if we are in debug mode + * and process it if we are in production mode. + * this should be fast since the PARSER_DEBUG is static final so the difference will be at compile time + * @param e the exception + * @throws ParseException the thrown exception + */ private static void processParseExceptionDebug(final ParseException e) throws ParseException { if (PARSER_DEBUG) { throw e; @@ -225,14 +232,14 @@ public final class PHPParser extends PHPParserSuperclass { e.currentToken.sourceStart, e.currentToken.sourceEnd, errorLevel, - "Line " + e.currentToken.beginLine); + "Line " + e.currentToken.beginLine+", "+e.currentToken.sourceStart+":"+e.currentToken.sourceEnd); } else { setMarker(fileToParse, errorMessage, errorStart, errorEnd, errorLevel, - "Line " + e.currentToken.beginLine); + "Line " + e.currentToken.beginLine+", "+errorStart+":"+errorEnd); errorStart = -1; errorEnd = -1; } @@ -324,19 +331,20 @@ public final class PHPParser extends PHPParserSuperclass { public static final void createNewHTMLCode() { final int currentPosition = token.sourceStart; if (currentPosition == htmlStart || + currentPosition < htmlStart || currentPosition > SimpleCharStream.currentBuffer.length()) { return; } - final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray(); + final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart, + currentPosition).toCharArray(); pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition)); } /** Create a new task. */ - public static final void createNewTask() { - final int currentPosition = token.sourceStart; - final String todo = SimpleCharStream.currentBuffer.substring(currentPosition-3, + public static final void createNewTask(final int todoStart) { + final String todo = SimpleCharStream.currentBuffer.substring(todoStart, SimpleCharStream.currentBuffer.indexOf("\n", - currentPosition)-1); + todoStart)-1); if (!PARSER_DEBUG) { try { setMarker(fileToParse, @@ -371,14 +379,14 @@ TOKEN_MGR_DECLS: TOKEN : { - {PHPParser.createNewHTMLCode();} : PHPPARSING -| {PHPParser.createNewHTMLCode();} : PHPPARSING -| : PHPPARSING +| : PHPPARSING +| "> {PHPParser.htmlStart = PHPParser.token.sourceEnd;} : DEFAULT + "> : DEFAULT } /* Skip any character if we are not in php mode */ @@ -398,6 +406,14 @@ TOKEN_MGR_DECLS: | "\f" } + SPECIAL_TOKEN : +{ + " " : PHPPARSING +| "\t" : PHPPARSING +| "\n" : PHPPARSING +| "\r" : PHPPARSING +| "\f" : PHPPARSING +} /* COMMENTS */ SPECIAL_TOKEN : { @@ -415,9 +431,14 @@ TOKEN_MGR_DECLS: SPECIAL_TOKEN : { - "todo" {PHPParser.createNewTask();} + "todo" } +void todo() : +{Token todoToken;} +{ + todoToken = "TODO" {createNewTask(todoToken.sourceStart);} +} SPECIAL_TOKEN : { "*/" : PHPPARSING @@ -460,9 +481,13 @@ MORE : | | | -| "> -| -| "> +} + + TOKEN : +{ + "> : PHPPARSING +| : PHPPARSING +| "> : PHPPARSING } /* RESERVED WORDS AND LITERALS */ @@ -509,36 +534,35 @@ MORE : } //Misc token - TOKEN : + TOKEN : { - -| -| -| -| -| + : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING } /* OPERATORS */ - TOKEN : -{ - -| -| -| -| -| -| -| -| -| -| -| -| -| >"> -| >>"> -| <_ORL : "OR"> -| <_ANDL : "AND"> + TOKEN : +{ + : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| >"> : PHPPARSING +| >>"> : PHPPARSING +| <_ORL : "OR"> : PHPPARSING +| <_ANDL : "AND"> : PHPPARSING } /* LITERALS */ @@ -573,7 +597,11 @@ MORE : /* IDENTIFIERS */ - TOKEN : + + TOKEN : { : IN_VARIABLE} + + + TOKEN : { |) (||)* > | @@ -592,55 +620,50 @@ MORE : /* SEPARATORS */ - TOKEN : + TOKEN : { - -| -| -| -| -| -| -| -| + : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING } /* COMPARATOR */ - TOKEN : + TOKEN : { - "> -| -| -| ="> -| "> -| -| + "> : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| ="> : PHPPARSING +| "> : PHPPARSING +| : PHPPARSING +| : PHPPARSING } /* ASSIGNATION */ - TOKEN : -{ - -| >="> -} - - TOKEN : -{ - > + TOKEN : +{ + : PHPPARSING +| >="> : PHPPARSING } void phpTest() : @@ -658,8 +681,8 @@ void phpFile() : {PHPParser.createNewHTMLCode();} } catch (TokenMgrError e) { PHPeclipsePlugin.log(e); - errorStart = SimpleCharStream.getPosition(); - errorEnd = errorStart + 1; + errorStart = SimpleCharStream.beginOffset; + errorEnd = SimpleCharStream.endOffset; errorMessage = e.getMessage(); errorLevel = ERROR; throw generateParseException(); @@ -674,7 +697,7 @@ void phpFile() : void PhpBlock() : { final PHPEchoBlock phpEchoBlock; - final Token token; + final Token token,phpEnd; } { phpEchoBlock = phpEchoBlock() @@ -693,9 +716,11 @@ void PhpBlock() : PHPeclipsePlugin.log(e); }} ] + {PHPParser.createNewHTMLCode();} Php() try { - + phpEnd = + {htmlStart = phpEnd.sourceEnd;} } catch (ParseException e) { errorMessage = "'?>' expected"; errorLevel = ERROR; @@ -712,8 +737,11 @@ PHPEchoBlock phpEchoBlock() : final Token token, token2; } { - token = expr = Expression() [ ] token2 = + token = {PHPParser.createNewHTMLCode();} + expr = Expression() [ ] token2 = { + htmlStart = token2.sourceEnd; + echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd); pushOnAstNodes(echoBlock); return echoBlock;} @@ -729,9 +757,10 @@ ClassDeclaration ClassDeclaration() : { final ClassDeclaration classDeclaration; Token className = null; - final Token superclassName, token; + final Token superclassName, token, extendsToken; String classNameImage = SYNTAX_ERROR_CHAR; String superclassNameImage = null; + final int classEnd; } { token = @@ -741,20 +770,20 @@ ClassDeclaration ClassDeclaration() : } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = token.sourceEnd+1; + errorEnd = token.sourceEnd+1; processParseExceptionDebug(e); } [ - + extendsToken = try { superclassName = {superclassNameImage = superclassName.image;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = extendsToken.sourceEnd+1; + errorEnd = extendsToken.sourceEnd+1; processParseExceptionDebug(e); superclassNameImage = SYNTAX_ERROR_CHAR; } @@ -784,15 +813,17 @@ ClassDeclaration ClassDeclaration() : currentSegment.add(classDeclaration); currentSegment = classDeclaration; } - ClassBody(classDeclaration) + classEnd = ClassBody(classDeclaration) {currentSegment = (OutlineableWithChildren) currentSegment.getParent(); - classDeclaration.sourceEnd = SimpleCharStream.getPosition(); + classDeclaration.sourceEnd = classEnd; pushOnAstNodes(classDeclaration); return classDeclaration;} } -void ClassBody(final ClassDeclaration classDeclaration) : -{} +int ClassBody(final ClassDeclaration classDeclaration) : +{ +Token token; +} { try { @@ -805,13 +836,15 @@ void ClassBody(final ClassDeclaration classDeclaration) : } ( ClassBodyDeclaration(classDeclaration) )* try { - + token = + {return token.sourceEnd;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected"; errorLevel = ERROR; errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = SimpleCharStream.getPosition() + 1; processParseExceptionDebug(e); + return PHPParser.token.sourceEnd; } } @@ -840,23 +873,30 @@ FieldDeclaration FieldDeclaration() : final ArrayList arrayList = new ArrayList(); final Token token; Token token2 = null; + int pos; } { token = variableDeclaration = VariableDeclaratorNoSuffix() - {arrayList.add(variableDeclaration); - outlineInfo.addVariable(variableDeclaration.name());} + { + arrayList.add(variableDeclaration); + outlineInfo.addVariable(variableDeclaration.name()); + pos = variableDeclaration.sourceEnd; + } ( variableDeclaration = VariableDeclaratorNoSuffix() - {arrayList.add(variableDeclaration); - outlineInfo.addVariable(variableDeclaration.name());} + { + arrayList.add(variableDeclaration); + outlineInfo.addVariable(variableDeclaration.name()); + pos = variableDeclaration.sourceEnd; + } )* try { token2 = } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = pos+1; + errorEnd = pos+1; processParseExceptionDebug(e); } @@ -880,12 +920,20 @@ FieldDeclaration FieldDeclaration() : */ VariableDeclaration VariableDeclaratorNoSuffix() : { - final Token varName; - Expression initializer = null; + final Token token, lbrace,rbrace; + Expression expr, initializer = null; Token assignToken; + Variable variable; } { - varName = + + ( + token = + {variable = new Variable(token.image,token.sourceStart,token.sourceEnd);} + | + lbrace = expr = Expression() rbrace = + {variable = new Variable(expr,lbrace.sourceStart,rbrace.sourceEnd);} + ) [ assignToken = try { @@ -901,19 +949,15 @@ VariableDeclaration VariableDeclaratorNoSuffix() : { if (initializer == null) { return new VariableDeclaration(currentSegment, - new Variable(varName.image.substring(1), - varName.sourceStart+1, - varName.sourceEnd+1), - varName.sourceStart+1, - varName.sourceEnd+1); + variable, + variable.sourceStart, + variable.sourceEnd); } return new VariableDeclaration(currentSegment, - new Variable(varName.image.substring(1), - varName.sourceStart+1, - varName.sourceEnd+1), + variable, initializer, VariableDeclaration.EQUAL, - varName.sourceStart+1); + variable.sourceStart); } } @@ -924,18 +968,19 @@ VariableDeclaration VariableDeclarator() : { final AbstractVariable variable; Expression initializer = null; + final Token token; } { variable = VariableDeclaratorId() [ - + token = try { initializer = VariableInitializer() } catch (ParseException e) { errorMessage = "Literal expression expected in variable initializer"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = token.sourceEnd+1; + errorEnd = token.sourceEnd+1; processParseExceptionDebug(e); } ] @@ -985,150 +1030,38 @@ AbstractVariable VariableDeclaratorId() : } } -/** - * Return a variablename without the $. - * @return a variable name - *//* -Variable Variable(): -{ - final StringBuffer buff; - Expression expression = null; - final Token token; - Variable expr; - final int pos; -} -{ - token = - [ expression = Expression() ] - { - if (expression == null) { - return new Variable(token.image.substring(1), - token.sourceStart+1, - token.sourceEnd+1); - } - String s = expression.toStringExpression(); - buff = new StringBuffer(token.image.length()+s.length()+2); - buff.append(token.image); - buff.append("{"); - buff.append(s); - buff.append("}"); - s = buff.toString(); - return new Variable(s,token.sourceStart+1,token.sourceEnd+1); - } -| - token = - expr = VariableName() - {return new Variable(expr,token.sourceStart,expr.sourceEnd);} -} */ - Variable Variable() : { Variable variable = null; final Token token; } { - token = [variable = Var(token)] - { - if (variable == null) { - return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1); - } - final StringBuffer buff = new StringBuffer(); - buff.append(token.image.substring(1)); - buff.append(variable.toStringExpression()); - return new Variable(buff.toString(),token.sourceStart+1,variable.sourceEnd+1); - } -| token = variable = Var(token) { - return new Variable(variable,token.sourceStart,variable.sourceEnd); + return new Variable(variable,token.sourceEnd,variable.sourceEnd); } } Variable Var(final Token dollar) : { Variable variable = null; - final Token token; + final Token token,token2; ConstantIdentifier constant; + Expression expression; } { - token = [variable = Var(token)] - {if (variable == null) { - return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1); - } - final StringBuffer buff = new StringBuffer(); - buff.append(token.image.substring(1)); - buff.append(variable.toStringExpression()); - return new Variable(buff.toString(),dollar.sourceStart,variable.sourceEnd); - } -| - LOOKAHEAD( ) token = variable = Var(token) - {return new Variable(variable,dollar.sourceStart,variable.sourceEnd);} + {return new Variable(variable,dollar.sourceEnd,variable.sourceEnd);} | - constant = VariableName() - {return new Variable(constant.name,dollar.sourceStart,constant.sourceEnd);} -} - -/** - * A Variable name (without the $) - * @return a variable name String - */ -ConstantIdentifier VariableName(): -{ - final StringBuffer buff; - String expr; - Expression expression = null; - final Token token; - Token token2 = null; -} -{ token = expression = Expression() token2 = - {expr = expression.toStringExpression(); - buff = new StringBuffer(expr.length()+2); - buff.append("{"); - buff.append(expr); - buff.append("}"); - expr = buff.toString(); - return new ConstantIdentifier(expr, - token.sourceStart, - token2.sourceEnd); - - } -| - token = - [ expression = Expression() token2 = ] { - if (expression == null) { - return new ConstantIdentifier(token.image, - token.sourceStart, - token.sourceEnd); - } - expr = expression.toStringExpression(); - buff = new StringBuffer(token.image.length()+expr.length()+2); - buff.append(token.image); - buff.append("{"); - buff.append(expr); - buff.append("}"); - expr = buff.toString(); - return new ConstantIdentifier(expr, - token.sourceStart, - token2.sourceEnd); - } -/*| - - var = VariableName() - { - return new Variable(var, - var.sourceStart-1, - var.sourceEnd); + return new Variable(expression, + dollar.sourceStart, + token2.sourceEnd); } | - token = - { - return new Variable(token.image, - token.sourceStart+1, - token.sourceEnd+1); - } */ + token = + {return new Variable(token.image,dollar.sourceStart,token.sourceEnd);} } Expression VariableInitializer() : @@ -1209,7 +1142,7 @@ MethodDeclaration MethodDeclaration() : token = try { functionDeclaration = MethodDeclarator(token.sourceStart) - {outlineInfo.addVariable(new String(functionDeclaration.name));} + {outlineInfo.addVariable(functionDeclaration.name);} } catch (ParseException e) { if (errorMessage != null) throw e; errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected"; @@ -1236,18 +1169,21 @@ MethodDeclaration MethodDeclarator(final int start) : Token reference = null; final Hashtable formalParameters = new Hashtable(); String identifierChar = SYNTAX_ERROR_CHAR; - final int end; + int end = start; } { - [reference = ] + [reference = {end = reference.sourceEnd;}] try { identifier = - {identifierChar = identifier.image;} + { + identifierChar = identifier.image; + end = identifier.sourceEnd; + } } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = e.currentToken.sourceEnd; + errorEnd = e.currentToken.next.sourceStart; processParseExceptionDebug(e); } end = FormalParameters(formalParameters) @@ -1284,24 +1220,26 @@ int FormalParameters(final Hashtable parameters) : { VariableDeclaration var; final Token token; - int end; + Token tok = PHPParser.token; + int end = tok.sourceEnd; } { try { - + tok = + {end = tok.sourceEnd;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = e.currentToken.next.sourceStart; + errorEnd = e.currentToken.next.sourceEnd; processParseExceptionDebug(e); } [ var = FormalParameter() - {parameters.put(new String(var.name()),var);} + {parameters.put(var.name(),var);end = var.sourceEnd;} ( var = FormalParameter() - {parameters.put(new String(var.name()),var);} + {parameters.put(var.name(),var);end = var.sourceEnd;} )* ] try { @@ -1310,10 +1248,9 @@ int FormalParameters(final Hashtable parameters) : } catch (ParseException e) { errorMessage = "')' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = e.currentToken.next.sourceStart; + errorEnd = e.currentToken.next.sourceEnd; processParseExceptionDebug(e); - end = e.currentToken.sourceStart; } {return end;} } @@ -1545,15 +1482,16 @@ Expression EqualityExpression() : { Expression expr,expr2; int operator; + Token token; } { expr = RelationalExpression() ( - ( {operator = OperatorIds.EQUAL_EQUAL;} - | {operator = OperatorIds.DIF;} - | {operator = OperatorIds.DIF;} - | {operator = OperatorIds.BANG_EQUAL_EQUAL;} - | {operator = OperatorIds.EQUAL_EQUAL_EQUAL;} + ( token = {operator = OperatorIds.EQUAL_EQUAL;} + | token = {operator = OperatorIds.DIF;} + | token = {operator = OperatorIds.DIF;} + | token = {operator = OperatorIds.BANG_EQUAL_EQUAL;} + | token = {operator = OperatorIds.EQUAL_EQUAL_EQUAL;} ) try { expr2 = RelationalExpression() @@ -1563,9 +1501,10 @@ Expression EqualityExpression() : } errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = token.sourceEnd +1; + errorEnd = token.sourceEnd +1; + expr2 = new ConstantIdentifier(SYNTAX_ERROR_CHAR,token.sourceEnd +1,token.sourceEnd +1); + processParseExceptionDebug(e); } { expr = new BinaryExpression(expr,expr2,operator); @@ -1639,8 +1578,8 @@ Expression MultiplicativeExpression() : if (errorMessage != null) throw e; errorMessage = "unexpected token '"+e.currentToken.next.image+"'"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = PHPParser.token.sourceStart; + errorEnd = PHPParser.token.sourceEnd; throw e; } ( @@ -1664,7 +1603,29 @@ Expression UnaryExpression() : /* expr = UnaryExpressionNoPrefix() //why did I had that ? {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);} | */ - expr = AtNotUnaryExpression() {return expr;} + expr = AtNotTildeUnaryExpression() {return expr;} +} + +Expression AtNotTildeUnaryExpression() : +{ + final Expression expr; + final Token token; +} +{ + token = + expr = AtNotTildeUnaryExpression() + {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);} +| + token = + expr = AtNotTildeUnaryExpression() + {return new PrefixedUnaryExpression(expr,OperatorIds.TWIDDLE,token.sourceStart);} +| + token = + expr = AtNotUnaryExpression() + {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);} +| + expr = UnaryExpressionNoPrefix() + {return expr;} } /** @@ -1695,11 +1656,11 @@ Expression UnaryExpressionNoPrefix() : final Token token; } { - token = expr = AtNotUnaryExpression() {return new PrefixedUnaryExpression(expr, + token = expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr, OperatorIds.PLUS, token.sourceStart);} | - token = expr = AtNotUnaryExpression() {return new PrefixedUnaryExpression(expr, + token = expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr, OperatorIds.MINUS, token.sourceStart);} | @@ -1742,9 +1703,9 @@ Expression UnaryExpressionNotPlusMinus() : } catch (ParseException e) { errorMessage = "')' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = expr.sourceEnd +1; + errorEnd = expr.sourceEnd +1; + processParseExceptionDebug(e); } {return expr;} } @@ -1866,7 +1827,9 @@ ArrayInitializer ArrayDeclarator() : } { token = vars = ArrayInitializer() - {return new ArrayInitializer(vars,token.sourceStart,SimpleCharStream.getPosition());} + {return new ArrayInitializer(vars, + token.sourceStart, + PHPParser.token.sourceEnd);} } Expression ClassIdentifier(): @@ -1886,12 +1849,26 @@ Expression ClassIdentifier(): AbstractVariable VariableSuffix(final AbstractVariable prefix) : { Expression expression = null; - final Token classAccessToken; + final Token classAccessToken,lbrace,rbrace; + Token token; + int pos; } { classAccessToken = try { - ( expression = VariableName() | expression = Variable() ) + ( + lbrace = expression = Expression() rbrace = + { + expression = new Variable(expression, + lbrace.sourceStart, + rbrace.sourceEnd); + } + | + token = + {expression = new ConstantIdentifier(token.image,token.sourceStart,token.sourceEnd);} + | + expression = Variable() + ) } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected"; errorLevel = ERROR; @@ -1903,17 +1880,35 @@ AbstractVariable VariableSuffix(final AbstractVariable prefix) : expression, ClassAccess.NORMAL);} | - [ expression = Expression() | expression = Type() ] //Not good + token = {pos = token.sourceEnd+1;} + [ expression = Expression() {pos = expression.sourceEnd+1;} + | expression = Type() {pos = expression.sourceEnd+1;}] //Not good try { - + token = + {pos = token.sourceEnd;} } catch (ParseException e) { errorMessage = "']' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); + } + {return new ArrayDeclarator(prefix,expression,pos);} +| + token = {pos = token.sourceEnd+1;} + [ expression = Expression() {pos = expression.sourceEnd+1;} + | expression = Type() {pos = expression.sourceEnd+1;}] //Not good + try { + token = + {pos = token.sourceEnd;} + } catch (ParseException e) { + errorMessage = "']' expected"; + errorLevel = ERROR; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } - {return new ArrayDeclarator(prefix,expression,SimpleCharStream.getPosition());} + {return new ArrayDeclarator(prefix,expression,pos);}//todo : check braces here } Literal Literal() : @@ -1938,14 +1933,15 @@ final Token token; [ args = ArgumentList() ] try { token = + {return new FunctionCall(func,args,token.sourceEnd);} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = args[args.length-1].sourceEnd+1; + errorEnd = args[args.length-1].sourceEnd+1; + processParseExceptionDebug(e); } - {return new FunctionCall(func,args,token.sourceEnd);} + {return new FunctionCall(func,args,args[args.length-1].sourceEnd);} } /** @@ -1957,20 +1953,23 @@ Expression[] ArgumentList() : { Expression arg; final ArrayList list = new ArrayList(); +int pos; +Token token; } { arg = Expression() - {list.add(arg);} - ( + {list.add(arg);pos = arg.sourceEnd;} + ( token = {pos = token.sourceEnd;} try { arg = Expression() - {list.add(arg);} + {list.add(arg); + pos = arg.sourceEnd;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos+1; + errorEnd = pos+1; + processParseException(e); } )* { @@ -2034,9 +2033,9 @@ Statement expressionStatement() : if (e.currentToken.next.kind != PHPParserConstants.PHPEND) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = statement.sourceEnd+1; + errorEnd = statement.sourceEnd+1; + processParseExceptionDebug(e); } } {return statement;} @@ -2044,61 +2043,70 @@ Statement expressionStatement() : Define defineStatement() : { - final int start = SimpleCharStream.getPosition(); Expression defineName,defineValue; + final Token defineToken; + Token token; + int pos; } { - + defineToken = {pos = defineToken.sourceEnd+1;} try { - + token = + {pos = token.sourceEnd+1;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = pos; + errorEnd = pos; processParseExceptionDebug(e); } try { defineName = Expression() + {pos = defineName.sourceEnd+1;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); + defineName = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos); } try { - + token = + {pos = defineName.sourceEnd+1;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = pos; + errorEnd = pos; processParseExceptionDebug(e); } try { defineValue = Expression() + {pos = defineValue.sourceEnd+1;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); + defineValue = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos); } try { - + token = + {pos = token.sourceEnd+1;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = pos; + errorEnd = pos; processParseExceptionDebug(e); } {return new Define(currentSegment, defineName, defineValue, - start, - SimpleCharStream.getPosition());} + defineToken.sourceStart, + pos);} } /** @@ -2121,11 +2129,15 @@ HTMLBlock htmlBlock() : final int startIndex = nodePtr; final AstNode[] blockNodes; final int nbNodes; + final Token phpEnd; } { - (phpEchoBlock())* + phpEnd = + {htmlStart = phpEnd.sourceEnd;} + (phpEchoBlock())* try { ( | ) + {PHPParser.createNewHTMLCode();} } catch (ParseException e) { errorMessage = "unexpected end of file , ' {keyword = InclusionStatement.REQUIRE;} - | token = {keyword = InclusionStatement.REQUIRE_ONCE;} - | token = {keyword = InclusionStatement.INCLUDE;} - | token = {keyword = InclusionStatement.INCLUDE_ONCE;}) + ( token = {keyword = InclusionStatement.REQUIRE;pos=token.sourceEnd;} + | token = {keyword = InclusionStatement.REQUIRE_ONCE;pos=token.sourceEnd;} + | token = {keyword = InclusionStatement.INCLUDE;pos=token.sourceEnd;} + | token = {keyword = InclusionStatement.INCLUDE_ONCE;pos=token.sourceEnd;}) try { expr = Expression() + {pos = expr.sourceEnd;} } catch (ParseException e) { if (errorMessage != null) { throw e; } errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; - } - {inclusionStatement = new InclusionStatement(currentSegment, - keyword, - expr, - token.sourceStart); - currentSegment.add(inclusionStatement); + errorStart = e.currentToken.next.sourceStart; + errorEnd = e.currentToken.next.sourceEnd; + expr = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos); + processParseExceptionDebug(e); } try { token2 = + {pos=token2.sourceEnd;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = e.currentToken.next.sourceStart; + errorEnd = e.currentToken.next.sourceEnd; + processParseExceptionDebug(e); + } + { + inclusionStatement = new InclusionStatement(currentSegment, + keyword, + expr, + token.sourceStart, + pos); + currentSegment.add(inclusionStatement); + return inclusionStatement; } - {inclusionStatement.sourceEnd = token2.sourceEnd; - return inclusionStatement;} } PrintExpression PrintExpression() : { final Expression expr; - final int pos = SimpleCharStream.getPosition(); + final Token printToken; } { - expr = Expression() {return new PrintExpression(expr,pos,SimpleCharStream.getPosition());} + token = expr = Expression() + {return new PrintExpression(expr,token.sourceStart,expr.sourceEnd);} } ListExpression ListExpression() : @@ -2201,13 +2222,14 @@ ListExpression ListExpression() : Expression expr = null; final Expression expression; final ArrayList list = new ArrayList(); - final int pos = SimpleCharStream.getPosition(); - final Token listToken; + int pos; + final Token listToken, rParen; + Token token; } { - listToken = + listToken = {pos = listToken.sourceEnd;} try { - + token = {pos = token.sourceEnd;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected"; errorLevel = ERROR; @@ -2217,29 +2239,31 @@ ListExpression ListExpression() : } [ expr = VariableDeclaratorId() - {list.add(expr);} + {list.add(expr);pos = expr.sourceEnd;} ] {if (expr == null) list.add(null);} ( try { - + token = + {pos = token.sourceEnd;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos+1; + errorEnd = pos+1; + processParseExceptionDebug(e); } - [expr = VariableDeclaratorId() {list.add(expr);}] + [expr = VariableDeclaratorId() {list.add(expr);pos = expr.sourceEnd;}] )* try { - + rParen = + {pos = rParen.sourceEnd;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos+1; + errorEnd = pos+1; + processParseExceptionDebug(e); } [ expression = Expression() { @@ -2247,13 +2271,13 @@ ListExpression ListExpression() : list.toArray(vars); return new ListExpression(vars, expression, - pos, - SimpleCharStream.getPosition());} + listToken.sourceStart, + expression.sourceEnd);} ] { - final Variable[] vars = new Variable[list.size()]; + final AbstractVariable[] vars = new AbstractVariable[list.size()]; list.toArray(vars); - return new ListExpression(vars,pos,SimpleCharStream.getPosition());} + return new ListExpression(vars,listToken.sourceStart,pos);} } /** @@ -2301,33 +2325,35 @@ GlobalStatement GlobalStatement() : final ArrayList vars = new ArrayList(); final GlobalStatement global; final Token token, token2; + int pos; } { token = expr = Variable() - {vars.add(expr);} + {vars.add(expr);pos = expr.sourceEnd+1;} ( expr = Variable() - {vars.add(expr);} + {vars.add(expr);pos = expr.sourceEnd+1;} )* try { token2 = + {pos = token2.sourceEnd+1;} + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected"; + errorLevel = ERROR; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); + } { final Variable[] variables = new Variable[vars.size()]; vars.toArray(variables); global = new GlobalStatement(currentSegment, variables, token.sourceStart, - token2.sourceEnd); + pos); currentSegment.add(global); return global;} - } catch (ParseException e) { - errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected"; - errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; - } } StaticStatement StaticStatement() : @@ -2335,27 +2361,29 @@ StaticStatement StaticStatement() : final ArrayList vars = new ArrayList(); VariableDeclaration expr; final Token token, token2; + int pos; } { - token = expr = VariableDeclarator() {vars.add(expr);} + token = expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;} ( - expr = VariableDeclarator() {vars.add(expr);} + expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;} )* try { token2 = + {pos = token2.sourceEnd+1;} + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected"; + errorLevel = ERROR; + errorStart = pos; + errorEnd = pos; + processParseException(e); + } { final VariableDeclaration[] variables = new VariableDeclaration[vars.size()]; vars.toArray(variables); return new StaticStatement(variables, token.sourceStart, - token2.sourceEnd);} - } catch (ParseException e) { - errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected"; - errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; - } + pos);} } LabeledStatement LabeledStatement() : @@ -2380,32 +2408,43 @@ Block Block() : final ArrayList list = new ArrayList(); Statement statement; final Token token, token2; + int pos,start; } { try { token = + {pos = token.sourceEnd+1;start=token.sourceStart;} } catch (ParseException e) { errorMessage = "'{' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + pos = PHPParser.token.sourceEnd+1; + start=pos; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } - ( statement = BlockStatement() {list.add(statement);} - | statement = htmlBlock() {list.add(statement);})* + ( statement = BlockStatement() {list.add(statement);pos = statement.sourceEnd+1;} + | statement = htmlBlock() {if (statement != null) { + list.add(statement); + pos = statement.sourceEnd+1; + } + pos = PHPParser.token.sourceEnd+1; + } + )* try { token2 = + {pos = token2.sourceEnd+1;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } { final Statement[] statements = new Statement[list.size()]; list.toArray(statements); - return new Block(statements,token.sourceStart,token2.sourceEnd);} + return new Block(statements,start,pos);} } Statement BlockStatement() : @@ -2504,86 +2543,98 @@ EmptyStatement EmptyStatement() : Expression StatementExpression() : { final Expression expr; + final Token operator; } { expr = PreIncDecExpression() {return expr;} | expr = PrimaryExpression() - [ {return new PostfixedUnaryExpression(expr, - OperatorIds.PLUS_PLUS, - SimpleCharStream.getPosition());} - | {return new PostfixedUnaryExpression(expr, - OperatorIds.MINUS_MINUS, - SimpleCharStream.getPosition());} + [ operator = {return new PostfixedUnaryExpression(expr, + OperatorIds.PLUS_PLUS, + operator.sourceEnd);} + | operator = {return new PostfixedUnaryExpression(expr, + OperatorIds.MINUS_MINUS, + operator.sourceEnd);} ] {return expr;} } SwitchStatement SwitchStatement() : { - final Expression variable; + Expression variable; final AbstractCase[] cases; - final int pos = SimpleCharStream.getPosition(); - final Token switchToken; + final Token switchToken,lparenToken,rparenToken; + int pos; } { - switchToken = + switchToken = {pos = switchToken.sourceEnd+1;} try { - + lparenToken = + {pos = lparenToken.sourceEnd+1;} } catch (ParseException e) { errorMessage = "'(' expected after 'switch'"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } try { - variable = Expression() + variable = Expression() {pos = variable.sourceEnd+1;} } catch (ParseException e) { if (errorMessage != null) { throw e; } errorMessage = "expression expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); + variable = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos); } try { - + rparenToken = {pos = rparenToken.sourceEnd+1;} } catch (ParseException e) { errorMessage = "')' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } - (cases = switchStatementBrace() | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd)) - {return new SwitchStatement(variable,cases,switchToken.sourceStart,SimpleCharStream.getPosition());} + ( cases = switchStatementBrace() + | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd)) + {return new SwitchStatement(variable, + cases, + switchToken.sourceStart, + PHPParser.token.sourceEnd);} } AbstractCase[] switchStatementBrace() : { AbstractCase cas; final ArrayList cases = new ArrayList(); + Token token; + int pos; } { - - ( cas = switchLabel0() {cases.add(cas);})* + token = {pos = token.sourceEnd;} + ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})* try { - - { - final AbstractCase[] abcase = new AbstractCase[cases.size()]; - cases.toArray(abcase); - return abcase;} + token = + {pos = token.sourceEnd;} } catch (ParseException e) { errorMessage = "'}' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos+1; + errorEnd = pos+1; + processParseExceptionDebug(e); + } + { + final AbstractCase[] abcase = new AbstractCase[cases.size()]; + cases.toArray(abcase); + return abcase; } } + /** * A Switch statement with : ... endswitch; * @param start the begin offset of the switch @@ -2593,9 +2644,11 @@ AbstractCase[] switchStatementColon(final int start, final int end) : { AbstractCase cas; final ArrayList cases = new ArrayList(); + Token token; + int pos; } { - + token = {pos = token.sourceEnd;} {try { setMarker(fileToParse, "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;", @@ -2606,28 +2659,29 @@ AbstractCase[] switchStatementColon(final int start, final int end) : } catch (CoreException e) { PHPeclipsePlugin.log(e); }} - ( cas = switchLabel0() {cases.add(cas);})* + ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})* try { - + token = {pos = token.sourceEnd;} } catch (ParseException e) { errorMessage = "'endswitch' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos+1; + errorEnd = pos+1; + processParseExceptionDebug(e); } try { - - { - final AbstractCase[] abcase = new AbstractCase[cases.size()]; - cases.toArray(abcase); - return abcase;} + token = {pos = token.sourceEnd;} } catch (ParseException e) { errorMessage = "';' expected after 'endswitch' keyword"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos+1; + errorEnd = pos+1; + processParseExceptionDebug(e); + } + { + final AbstractCase[] abcase = new AbstractCase[cases.size()]; + cases.toArray(abcase); + return abcase; } } @@ -2636,20 +2690,27 @@ AbstractCase switchLabel0() : final Expression expr; Statement statement; final ArrayList stmts = new ArrayList(); - final int pos = SimpleCharStream.getPosition(); + final Token token = PHPParser.token; } { expr = SwitchLabel() ( statement = BlockStatementNoBreak() {stmts.add(statement);} - | statement = htmlBlock() {stmts.add(statement);})* - [ statement = BreakStatement() {stmts.add(statement);}] + | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}} + | statement = BreakStatement() {stmts.add(statement);})* + //[ statement = BreakStatement() {stmts.add(statement);}] { - final Statement[] stmtsArray = new Statement[stmts.size()]; - stmts.toArray(stmtsArray); - if (expr == null) {//it's a default - return new DefaultCase(stmtsArray,pos,SimpleCharStream.getPosition()); + final int listSize = stmts.size(); + final Statement[] stmtsArray = new Statement[listSize]; + stmts.toArray(stmtsArray); + if (expr == null) {//it's a default + return new DefaultCase(stmtsArray,token.sourceStart,stmtsArray[listSize-1].sourceEnd); + } + if (listSize != 0) { + return new Case(expr,stmtsArray,expr.sourceStart,stmtsArray[listSize-1].sourceEnd); + } else { + return new Case(expr,stmtsArray,expr.sourceStart,expr.sourceEnd); + } } - return new Case(expr,stmtsArray,pos,SimpleCharStream.getPosition());} } /** @@ -2702,19 +2763,22 @@ Break BreakStatement() : { Expression expression = null; final Token token, token2; + int pos; } { - token = [ expression = Expression() ] + token = {pos = token.sourceEnd+1;} + [ expression = Expression() {pos = expression.sourceEnd+1;}] try { token2 = + {pos = token2.sourceEnd;} } catch (ParseException e) { errorMessage = "';' expected after 'break' keyword"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } - {return new Break(expression, token.sourceStart, token2.sourceEnd);} + {return new Break(expression, token.sourceStart, pos);} } IfStatement IfStatement() : @@ -2740,8 +2804,8 @@ Expression Condition(final String keyword) : } catch (ParseException e) { errorMessage = "'(' expected after " + keyword + " keyword"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length(); - errorEnd = errorStart +1; + errorStart = PHPParser.token.sourceEnd + 1; + errorEnd = PHPParser.token.sourceEnd + 1; processParseExceptionDebug(e); } condition = Expression() @@ -2750,8 +2814,8 @@ Expression Condition(final String keyword) : } catch (ParseException e) { errorMessage = "')' expected after " + keyword + " keyword"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorStart = condition.sourceEnd+1; + errorEnd = condition.sourceEnd+1; processParseExceptionDebug(e); } {return condition;} @@ -2774,7 +2838,7 @@ IfStatement IfStatement0(final Expression condition, final int start,final int e {stmts = new ArrayList();} ( statement = Statement() {stmts.add(statement);} - | statement = htmlBlock() {stmts.add(statement);})* + | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}})* {endStatements = SimpleCharStream.getPosition();} (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})* [elseStatement = ElseStatementColon()] @@ -2865,59 +2929,61 @@ ElseIf ElseIfStatementColon() : final Expression condition; Statement statement; final ArrayList list = new ArrayList(); - final int pos = SimpleCharStream.getPosition(); + final Token elseifToken; } { - condition = Condition("elseif") + elseifToken = condition = Condition("elseif") ( statement = Statement() {list.add(statement);} - | statement = htmlBlock() {list.add(statement);})* + | statement = htmlBlock() {if (statement != null) {list.add(statement);}})* { - final Statement[] stmtsArray = new Statement[list.size()]; + final int sizeList = list.size(); + final Statement[] stmtsArray = new Statement[sizeList]; list.toArray(stmtsArray); - return new ElseIf(condition,stmtsArray ,pos,SimpleCharStream.getPosition());} + return new ElseIf(condition,stmtsArray , + elseifToken.sourceStart, + stmtsArray[sizeList-1].sourceEnd);} } Else ElseStatementColon() : { Statement statement; final ArrayList list = new ArrayList(); - final int pos = SimpleCharStream.getPosition(); + final Token elseToken; } { - ( statement = Statement() {list.add(statement);} - | statement = htmlBlock() {list.add(statement);})* + elseToken = ( statement = Statement() {list.add(statement);} + | statement = htmlBlock() {if (statement != null) {list.add(statement);}})* { - final Statement[] stmtsArray = new Statement[list.size()]; + final int sizeList = list.size(); + final Statement[] stmtsArray = new Statement[sizeList]; list.toArray(stmtsArray); - return new Else(stmtsArray,pos,SimpleCharStream.getPosition());} + return new Else(stmtsArray,elseToken.sourceStart,stmtsArray[sizeList-1].sourceEnd);} } ElseIf ElseIfStatement() : { final Expression condition; - final Statement statement; - final ArrayList list = new ArrayList(); - final int pos = SimpleCharStream.getPosition(); + //final Statement statement; + final Token elseifToken; + final Statement[] statement = new Statement[1]; } { - condition = Condition("elseif") statement = Statement() {list.add(statement);/*todo:do better*/} + elseifToken = condition = Condition("elseif") statement[0] = Statement() { - final Statement[] stmtsArray = new Statement[list.size()]; - list.toArray(stmtsArray); - return new ElseIf(condition,stmtsArray,pos,SimpleCharStream.getPosition());} + return new ElseIf(condition,statement,elseifToken.sourceStart,statement[0].sourceEnd);} } WhileStatement WhileStatement() : { final Expression condition; final Statement action; - final int pos = SimpleCharStream.getPosition(); + final Token whileToken; } { - + whileToken = condition = Condition("while") - action = WhileStatement0(pos,pos + 5) - {return new WhileStatement(condition,action,pos,SimpleCharStream.getPosition());} + action = WhileStatement0(whileToken.sourceStart,whileToken.sourceEnd) + {return new WhileStatement(condition,action,whileToken.sourceStart,action.sourceEnd);} } Statement WhileStatement0(final int start, final int end) : @@ -2969,90 +3035,108 @@ DoStatement DoStatement() : { final Statement action; final Expression condition; - final Token token, token2; + final Token token; + Token token2 = null; } { token = action = Statement() condition = Condition("while") try { token2 = - {return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = condition.sourceEnd+1; + errorEnd = condition.sourceEnd+1; + processParseExceptionDebug(e); + } + { + if (token2 == null) { + return new DoStatement(condition,action,token.sourceStart,condition.sourceEnd); + } + return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd); } } ForeachStatement ForeachStatement() : { - Statement statement; - Expression expression; - ArrayVariableDeclaration variable; - Token token; + Statement statement = null; + Expression expression = null; + ArrayVariableDeclaration variable = null; + Token foreachToken; + Token lparenToken = null; + Token asToken = null; + Token rparenToken = null; + int pos; } { - token = - try { - + foreachToken = + try { + lparenToken = + {pos = lparenToken.sourceEnd+1;} } catch (ParseException e) { errorMessage = "'(' expected after 'foreach' keyword"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = foreachToken.sourceEnd+1; + errorEnd = foreachToken.sourceEnd+1; + processParseExceptionDebug(e); + {pos = foreachToken.sourceEnd+1;} } try { expression = Expression() + {pos = expression.sourceEnd+1;} } catch (ParseException e) { errorMessage = "variable expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } try { - + asToken = + {pos = asToken.sourceEnd+1;} } catch (ParseException e) { errorMessage = "'as' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } try { variable = ArrayVariable() + {pos = variable.sourceEnd+1;} } catch (ParseException e) { + if (errorMessage != null) throw e; errorMessage = "variable expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } try { - + rparenToken = + {pos = rparenToken.sourceEnd+1;} } catch (ParseException e) { errorMessage = "')' expected after 'foreach' keyword"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } try { statement = Statement() + {pos = rparenToken.sourceEnd+1;} } catch (ParseException e) { if (errorMessage != null) throw e; errorMessage = "statement expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } {return new ForeachStatement(expression, variable, statement, - token.sourceStart, + foreachToken.sourceStart, statement.sourceEnd);} } @@ -3063,8 +3147,8 @@ ForeachStatement ForeachStatement() : */ ForStatement ForStatement() : { -final Token token,token2; -final int pos = SimpleCharStream.getPosition(); +final Token token,tokenEndFor,token2,tokenColon; +int pos; Expression[] initializations = null; Expression condition = null; Expression[] increments = null; @@ -3094,8 +3178,8 @@ final ArrayList list = new ArrayList(); token.sourceStart, action.sourceEnd);} | - - (action = Statement() {list.add(action);})* + tokenColon = {pos = tokenColon.sourceEnd+1;} + (action = Statement() {list.add(action);pos = action.sourceEnd+1;})* { try { setMarker(fileToParse, @@ -3109,34 +3193,36 @@ final ArrayList list = new ArrayList(); } } try { - + tokenEndFor = + {pos = tokenEndFor.sourceEnd+1;} } catch (ParseException e) { errorMessage = "'endfor' expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } try { token2 = - { - final Statement[] stmtsArray = new Statement[list.size()]; - list.toArray(stmtsArray); - return new ForStatement(initializations, - condition, - increments, - new Block(stmtsArray, - stmtsArray[0].sourceStart, - stmtsArray[stmtsArray.length-1].sourceEnd), - token.sourceStart, - token2.sourceEnd);} + {pos = token2.sourceEnd+1;} } catch (ParseException e) { errorMessage = "';' expected after 'endfor' keyword"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); } + { + final Statement[] stmtsArray = new Statement[list.size()]; + list.toArray(stmtsArray); + return new ForStatement(initializations, + condition, + increments, + new Block(stmtsArray, + stmtsArray[0].sourceStart, + stmtsArray[stmtsArray.length-1].sourceEnd), + token.sourceStart, + pos);} ) } @@ -3171,38 +3257,66 @@ Expression[] StatementExpressionList() : Continue ContinueStatement() : { Expression expr = null; - final Token token,token2; + final Token token; + Token token2 = null; } { token = [ expr = Expression() ] try { token2 = - {return new Continue(expr,token.sourceStart,token2.sourceEnd);} } catch (ParseException e) { errorMessage = "';' expected after 'continue' statement"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + if (expr == null) { + errorStart = token.sourceEnd+1; + errorEnd = token.sourceEnd+1; + } else { + errorStart = expr.sourceEnd+1; + errorEnd = expr.sourceEnd+1; + } + processParseExceptionDebug(e); + } + { + if (token2 == null) { + if (expr == null) { + return new Continue(expr,token.sourceStart,token.sourceEnd); + } + return new Continue(expr,token.sourceStart,expr.sourceEnd); + } + return new Continue(expr,token.sourceStart,token2.sourceEnd); } } ReturnStatement ReturnStatement() : { Expression expr = null; - final Token token,token2; + final Token token; + Token token2 = null; } { token = [ expr = Expression() ] try { token2 = - {return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd);} } catch (ParseException e) { errorMessage = "';' expected after 'return' statement"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; - } + if (expr == null) { + errorStart = token.sourceEnd+1; + errorEnd = token.sourceEnd+1; + } else { + errorStart = expr.sourceEnd+1; + errorEnd = expr.sourceEnd+1; + } + processParseExceptionDebug(e); + } + { + if (token2 == null) { + if (expr == null) { + return new ReturnStatement(expr,token.sourceStart,token.sourceEnd); + } + return new ReturnStatement(expr,token.sourceStart,expr.sourceEnd); + } + return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd); + } }