X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/test/PHPParser.jj b/net.sourceforge.phpeclipse/src/test/PHPParser.jj index b49b15a..e69a928 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj @@ -29,21 +29,19 @@ import org.eclipse.ui.texteditor.MarkerUtilities; import org.eclipse.jface.preference.IPreferenceStore; import java.util.Hashtable; +import java.util.ArrayList; +import java.util.Enumeration; import java.io.StringReader; +import java.io.*; import java.text.MessageFormat; import net.sourceforge.phpeclipse.actions.PHPStartApacheAction; import net.sourceforge.phpeclipse.PHPeclipsePlugin; -import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo; -import net.sourceforge.phpdt.internal.compiler.parser.PHPSegmentWithChildren; -import net.sourceforge.phpdt.internal.compiler.parser.PHPFunctionDeclaration; -import net.sourceforge.phpdt.internal.compiler.parser.PHPClassDeclaration; -import net.sourceforge.phpdt.internal.compiler.parser.PHPVarDeclaration; -import net.sourceforge.phpdt.internal.compiler.parser.PHPReqIncDeclaration; +import net.sourceforge.phpdt.internal.compiler.parser.*; /** * A new php parser. - * This php parser is inspired by the Java 1.2 grammar example + * This php parser is inspired by the Java 1.2 grammar example * given with JavaCC. You can get JavaCC at http://www.webgain.com * You can test the parser with the PHPParserTestCase2.java * @author Matthieu Casanova @@ -60,6 +58,8 @@ public final class PHPParser extends PHPParserSuperclass { private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$ PHPOutlineInfo outlineInfo; + private static PHPFunctionDeclaration currentFunction; + /** The error level of the current ParseException. */ private static int errorLevel = ERROR; /** The message of the current ParseException. If it's null it's because the parse exception wasn't handled */ @@ -90,6 +90,19 @@ public final class PHPParser extends PHPParserSuperclass { phpTest(); } + public static final void htmlParserTester(final File fileName) throws CoreException, ParseException { + try { + final Reader stream = new FileReader(fileName); + if (jj_input_stream == null) { + jj_input_stream = new SimpleCharStream(stream, 1, 1); + } + ReInit(stream); + phpFile(); + } catch (FileNotFoundException e) { + e.printStackTrace(); //To change body of catch statement use Options | File Templates. + } + } + public static final void htmlParserTester(final String strEval) throws CoreException, ParseException { final StringReader stream = new StringReader(strEval); if (jj_input_stream == null) { @@ -124,6 +137,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 = jj_input_stream.getPosition(); + errorEnd = errorStart + 1; } setMarker(e); errorMessage = null; @@ -277,6 +292,7 @@ PARSER_END(PHPParser) "> : DEFAULT } +/* Skip any character if we are not in php mode */ SKIP : { < ~[] > @@ -284,7 +300,6 @@ PARSER_END(PHPParser) /* WHITE SPACE */ - SKIP : { " " @@ -295,7 +310,6 @@ PARSER_END(PHPParser) } /* COMMENTS */ - SPECIAL_TOKEN : { "//" : IN_SINGLE_LINE_COMMENT @@ -346,6 +360,7 @@ MORE : | | | +| } /* LANGUAGE CONSTRUCT */ @@ -364,10 +379,6 @@ MORE : | "> } - TOKEN : -{ - -} /* RESERVED WORDS AND LITERALS */ TOKEN : @@ -390,6 +401,7 @@ MORE : | | | +| | | | @@ -397,7 +409,6 @@ MORE : } /* TYPES */ - TOKEN : { @@ -418,7 +429,6 @@ MORE : } /* LITERALS */ - TOKEN : { < INTEGER_LITERAL: @@ -446,9 +456,10 @@ MORE : | < STRING_1: "\"" ( - ~["\""] + ~["\""] | ~["{"] | "\\\"" + | "{" ~["\""] "}" )* "\"" > @@ -544,6 +555,7 @@ MORE : | | +| | | | @@ -584,18 +596,26 @@ void phpFile() : (PhpBlock())* } catch (TokenMgrError e) { + PHPeclipsePlugin.log(e); + errorStart = SimpleCharStream.getPosition(); + errorEnd = errorStart + 1; errorMessage = e.getMessage(); errorLevel = ERROR; throw generateParseException(); } } +/** + * A php block is a + * or + * or + */ void PhpBlock() : { - final int start = jj_input_stream.bufpos; + final int start = jj_input_stream.getPosition(); } { - Expression() [ ] + phpEchoBlock() | [ | @@ -603,7 +623,7 @@ void PhpBlock() : setMarker(fileToParse, "You should use '' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } +void phpEchoBlock() : +{} +{ + Expression() [ ] +} + void Php() : {} { @@ -635,11 +663,13 @@ void ClassDeclaration() : { try { - {pos = jj_input_stream.bufpos;} + {pos = jj_input_stream.getPosition();} className = } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } [ @@ -649,6 +679,8 @@ void ClassDeclaration() : } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } ] @@ -673,20 +705,27 @@ void ClassBody() : try { } catch (ParseException e) { - errorMessage = "'{' expected"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image + "', '{' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } ( ClassBodyDeclaration() )* try { } catch (ParseException e) { - errorMessage = "'var', 'function' or '}' expected"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', 'var', 'function' or '}' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } +/** + * A class can contain only methods and fields. + */ void ClassBodyDeclaration() : {} { @@ -695,6 +734,9 @@ void ClassBodyDeclaration() : FieldDeclaration() } +/** + * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;. + */ void FieldDeclaration() : { PHPVarDeclaration variableDeclaration; @@ -717,17 +759,18 @@ void FieldDeclaration() : try { } catch (ParseException e) { - errorMessage = "';' expected after variable declaration"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } PHPVarDeclaration VariableDeclarator() : { - final String varName; - String varValue; - final int pos = jj_input_stream.bufpos; + final String varName, varValue; + final int pos = jj_input_stream.getPosition(); } { varName = VariableDeclaratorId() @@ -735,10 +778,12 @@ PHPVarDeclaration VariableDeclarator() : try { varValue = VariableInitializer() - {return new PHPVarDeclaration(currentSegment,varName,pos,varValue);} + {return new PHPVarDeclaration(currentSegment,varName.substring(1),pos,varValue);} } catch (ParseException e) { errorMessage = "Literal expression expected in variable initializer"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } ] @@ -761,6 +806,8 @@ String VariableDeclaratorId() : } catch (ParseException e) { errorMessage = "'$' expected for variable identifier"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } @@ -774,6 +821,12 @@ String Variable(): token = [ expr = Expression() ] { if (expr == null) { + if (currentFunction != null) { + PHPVarDeclaration var = currentFunction.getParameter(token.image.substring(1)); + if (var != null) { + var.getVariable().setUsed(true); + } + } return token.image; } return token + "{" + expr + "}"; @@ -795,21 +848,46 @@ final Token token; token = [ expr = Expression() ] { if (expr == null) { + if (currentFunction != null) { + PHPVarDeclaration var = currentFunction.getParameter(token.image); + if (var != null) { + var.getVariable().setUsed(true); + } + } return token.image; } return token + "{" + expr + "}"; } | expr = VariableName() - {return "$" + expr;} + { + if (currentFunction != null) { + PHPVarDeclaration var = currentFunction.getParameter(expr); + if (var != null) { + var.getVariable().setUsed(true); + } + } + return "$" + expr; + } | + token = + { + if (currentFunction != null) { + PHPVarDeclaration var = currentFunction.getParameter(token.image.substring(1)); + if (var != null) { + var.getVariable().setUsed(true); + } + } + return token.image + expr; + } +/*| pas besoin ? token = [expr = VariableName()] { if (expr == null) { return token.image; } return token.image + expr; - } + }*/ } String VariableInitializer() : @@ -857,7 +935,9 @@ final StringBuffer buff = new StringBuffer("("); {buff.append(expr);} ( LOOKAHEAD(2) expr = ArrayVariable() {buff.append(",").append(expr);} - )* ] + )* + ] + [ {buff.append(",");}] { buff.append(")"); @@ -865,12 +945,17 @@ final StringBuffer buff = new StringBuffer("("); } } +/** + * A Method Declaration. + * function MetodDeclarator() Block() + */ void MethodDeclaration() : { final PHPFunctionDeclaration functionDeclaration; + Token functionToken; } { - + functionToken = try { functionDeclaration = MethodDeclarator() } catch (ParseException e) { @@ -879,6 +964,8 @@ void MethodDeclaration() : } errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } { @@ -886,76 +973,111 @@ void MethodDeclaration() : currentSegment.add(functionDeclaration); currentSegment = functionDeclaration; } + currentFunction = functionDeclaration; } Block() { + Hashtable parameters = currentFunction.getParameters(); + Enumeration vars = parameters.elements(); + while (vars.hasMoreElements()) { + PHPVarDeclaration o = (PHPVarDeclaration) vars.nextElement(); + if (!o.getVariable().isUsed()) { + try { + setMarker(fileToParse, + "Parameter "+o.getVariable().getName()+" is never used in function", + functionToken.beginLine, + WARNING, + "Line " + token.beginLine); + } catch (CoreException e) { + PHPeclipsePlugin.log(e); + } + } + } + currentFunction = null; if (currentSegment != null) { currentSegment = (PHPSegmentWithChildren) currentSegment.getParent(); } } } +/** + * A MethodDeclarator. + * [&] IDENTIFIER(parameters ...). + * @return a function description for the outline + */ PHPFunctionDeclaration MethodDeclarator() : { final Token identifier; final StringBuffer methodDeclaration = new StringBuffer(); - final String formalParameters; - final int pos = jj_input_stream.bufpos; + final Hashtable formalParameters; + final int pos = jj_input_stream.getPosition(); } { [ {methodDeclaration.append("&");} ] identifier = - {methodDeclaration.append(identifier);} - formalParameters = FormalParameters() + formalParameters = FormalParameters() { - methodDeclaration.append(formalParameters); - return new PHPFunctionDeclaration(currentSegment,methodDeclaration.toString(),pos); + methodDeclaration.append(identifier); + return new PHPFunctionDeclaration(currentSegment,methodDeclaration.toString(),pos,formalParameters); } } -String FormalParameters() : +/** + * FormalParameters follows method identifier. + * (FormalParameter()) + */ +Hashtable FormalParameters() : { String expr; final StringBuffer buff = new StringBuffer("("); + PHPVarDeclaration var; + final Hashtable parameters = new Hashtable(); } { try { } catch (ParseException e) { - errorMessage = "Formal parameter expected after function identifier"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier"; errorLevel = ERROR; - jj_consume_token(token.kind); + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; + throw e; } - [ expr = FormalParameter() - {buff.append(expr);} - ( - expr = FormalParameter() - {buff.append(",").append(expr);} - )* + [ var = FormalParameter() + {parameters.put(var.getVariable().getName(),var);} + ( + var = FormalParameter() + {parameters.put(var.getVariable().getName(),var);} + )* ] try { } catch (ParseException e) { errorMessage = "')' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } - { - buff.append(")"); - return buff.toString(); - } + {return parameters;} } -String FormalParameter() : +/** + * A formal parameter. + * $varname[=value] (,$varname[=value]) + */ +PHPVarDeclaration FormalParameter() : { final PHPVarDeclaration variableDeclaration; - final StringBuffer buff = new StringBuffer(); + Token token = null; } { - [ {buff.append("&");}] variableDeclaration = VariableDeclarator() + [token = ] variableDeclaration = VariableDeclarator() { - buff.append(variableDeclaration.toString()); - return buff.toString(); + if (token != null) { + variableDeclaration.getVariable().setPrefix("@"); + } + return variableDeclaration; } } @@ -1010,8 +1132,13 @@ String Expression() : expr2 = Expression() {return expr + assignOperator + expr2;} } catch (ParseException e) { + if (errorMessage != null) { + throw e; + } errorMessage = "expression expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } ] @@ -1198,6 +1325,8 @@ String EqualityExpression() : } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected after '"+operator.image+"'"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } { @@ -1268,14 +1397,22 @@ String MultiplicativeExpression() : Token operator; final StringBuffer buff = new StringBuffer();} { - expr = UnaryExpression() + try { + expr = UnaryExpression() + } catch (ParseException e) { + errorMessage = "unexpected token '"+e.currentToken.next.image+"'"; + errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; + throw e; + } {buff.append(expr);} ( - ( operator = | operator = | operator = ) expr = UnaryExpression() - { - buff.append(operator.image); - buff.append(expr); - } + ( operator = | operator = | operator = ) expr = UnaryExpression() + { + buff.append(operator.image); + buff.append(expr); + } )* {return buff.toString();} } @@ -1313,10 +1450,7 @@ String UnaryExpressionNoPrefix() : return token.image + expr; } | - expr = PreIncrementExpression() - {return expr;} -| - expr = PreDecrementExpression() + expr = PreIncDecExpression() {return expr;} | expr = UnaryExpressionNotPlusMinus() @@ -1324,22 +1458,14 @@ String UnaryExpressionNoPrefix() : } -String PreIncrementExpression() : -{ -final String expr; -} -{ - expr = PrimaryExpression() - {return "++"+expr;} -} - -String PreDecrementExpression() : +String PreIncDecExpression() : { final String expr; +final Token token; } { - expr = PrimaryExpression() - {return "--"+expr;} + (token = | token = ) expr = PrimaryExpression() + {return token.image + expr;} } String UnaryExpressionNotPlusMinus() : @@ -1350,7 +1476,7 @@ String UnaryExpressionNotPlusMinus() : expr = UnaryExpression() {return "!" + expr;} | - LOOKAHEAD( Type() ) + LOOKAHEAD( (Type() | ) ) expr = CastExpression() {return expr;} | @@ -1366,6 +1492,8 @@ String UnaryExpressionNotPlusMinus() : } catch (ParseException e) { errorMessage = "')' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } {return "("+expr+")";} @@ -1376,7 +1504,7 @@ String CastExpression() : final String type, expr; } { - type = Type() expr = UnaryExpression() + (type = Type() | {type = "array";}) expr = UnaryExpression() {return "(" + type + ")" + expr;} } @@ -1441,11 +1569,26 @@ String PrimaryPrefix() : { return "new " + expr; } -| +| expr = VariableDeclaratorId() {return expr;} } +String classInstantiation() : +{ + String expr; + final StringBuffer buff = new StringBuffer("new "); +} +{ + expr = ClassIdentifier() + {buff.append(expr);} + [ + expr = PrimaryExpression() + {buff.append(expr);} + ] + {return buff.toString();} +} + String ClassIdentifier(): { final String expr; @@ -1482,16 +1625,20 @@ String VariableSuffix() : } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } {return "->" + expr;} -| - [ expr = Expression() ] +| + [ expr = Expression() | expr = Type() ] //Not good try { } catch (ParseException e) { errorMessage = "']' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } { @@ -1520,8 +1667,8 @@ String Literal() : expr = BooleanLiteral() {return expr;} | - expr = NullLiteral() - {return expr;} + + {return "null";} } String BooleanLiteral() : @@ -1534,13 +1681,6 @@ String BooleanLiteral() : {return "false";} } -String NullLiteral() : -{} -{ - - {return "null";} -} - String Arguments() : { String expr = null; @@ -1552,6 +1692,8 @@ String expr = null; } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } { @@ -1576,6 +1718,8 @@ final StringBuffer buff = new StringBuffer(); } catch (ParseException e) { errorMessage = "expression expected after a comma in argument list"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } { @@ -1594,11 +1738,15 @@ void StatementNoBreak() : LOOKAHEAD(2) Expression() try { - ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) + } catch (ParseException e) { - errorMessage = "';' expected"; - errorLevel = ERROR; - throw e; + if (e.currentToken.next.kind != 4) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; + errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; + throw e; + } } | LOOKAHEAD(2) @@ -1612,8 +1760,10 @@ void StatementNoBreak() : try { } catch (ParseException e) { - errorMessage = "';' expected after expression"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } | @@ -1653,69 +1803,39 @@ void Statement() : BreakStatement() } +void htmlBlock() : +{} +{ + (phpEchoBlock())* ( | ) +} + +/** + * An include statement. It's "include" an expression; + */ void IncludeStatement() : { final String expr; - final int pos = jj_input_stream.bufpos; + final Token token; + final int pos = jj_input_stream.getPosition(); } { - + ( token = + | token = + | token = + | token = ) expr = Expression() { if (currentSegment != null) { - currentSegment.add(new PHPReqIncDeclaration(currentSegment, "require",pos,expr)); + currentSegment.add(new PHPReqIncDeclaration(currentSegment, token.image,pos,expr)); } } try { - ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) - } catch (ParseException e) { - errorMessage = "';' expected"; - errorLevel = ERROR; - throw e; - } -| - - expr = Expression() - { - if (currentSegment != null) { - currentSegment.add(new PHPReqIncDeclaration(currentSegment, "require_once",pos,expr)); - } - } - try { - ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) - } catch (ParseException e) { - errorMessage = "';' expected"; - errorLevel = ERROR; - throw e; - } -| - - expr = Expression() - { - if (currentSegment != null) { - currentSegment.add(new PHPReqIncDeclaration(currentSegment, "include",pos,expr)); - } - } - try { - ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) - } catch (ParseException e) { - errorMessage = "';' expected"; - errorLevel = ERROR; - throw e; - } -| - - expr = Expression() - { - if (currentSegment != null) { - currentSegment.add(new PHPReqIncDeclaration(currentSegment, "include_once",pos,expr)); - } - } - try { - ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) + } catch (ParseException e) { - errorMessage = "';' expected"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } @@ -1745,57 +1865,85 @@ String ListExpression() : } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } [ expr = VariableDeclaratorId() {buff.append(expr);} ] - [ + ( try { } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } expr = VariableDeclaratorId() {buff.append(",").append(expr);} - ] + )* {buff.append(")");} try { } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } [ expr = Expression() {buff.append("(").append(expr);}] {return buff.toString();} } +/** + * An echo statement is like this : echo anyexpression (, otherexpression)* + */ void EchoStatement() : {} { Expression() ( Expression())* try { - ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) + } catch (ParseException e) { - errorMessage = "';' expected after 'echo' statement"; - errorLevel = ERROR; - throw e; + if (e.currentToken.next.kind != 4) { + errorMessage = "';' expected after 'echo' statement"; + errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; + throw e; + } } } void GlobalStatement() : -{} { - VariableDeclaratorId() ( VariableDeclaratorId())* + final int pos = jj_input_stream.getPosition(); + String expr; +} +{ + + expr = VariableDeclaratorId() + {if (currentSegment != null) { + currentSegment.add(new PHPGlobalDeclaration(currentSegment, "global",pos,expr)); + }} + ( + expr = VariableDeclaratorId() + {if (currentSegment != null) { + currentSegment.add(new PHPGlobalDeclaration(currentSegment, "global",pos,expr)); + }} + )* try { - ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) + } catch (ParseException e) { - errorMessage = "';' expected"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } @@ -1805,10 +1953,12 @@ void StaticStatement() : { VariableDeclarator() ( VariableDeclarator())* try { - ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) + } catch (ParseException e) { - errorMessage = "';' expected"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } @@ -1827,14 +1977,18 @@ void Block() : } catch (ParseException e) { errorMessage = "'{' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } - ( BlockStatement() )* + ( BlockStatement() | htmlBlock())* try { } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } @@ -1883,9 +2037,7 @@ void EmptyStatement() : void StatementExpression() : {} { - PreIncrementExpression() -| - PreDecrementExpression() + PreIncDecExpression() | PrimaryExpression() [ @@ -1899,8 +2051,7 @@ void StatementExpression() : void SwitchStatement() : { - Token breakToken = null; - int line; + final int pos = jj_input_stream.getPosition(); } { @@ -1909,6 +2060,8 @@ void SwitchStatement() : } catch (ParseException e) { errorMessage = "'(' expected after 'switch'"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } Expression() @@ -1917,40 +2070,90 @@ void SwitchStatement() : } catch (ParseException e) { errorMessage = "')' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } - try { + (switchStatementBrace() | switchStatementColon(pos, pos + 6)) +} + +void switchStatementBrace() : +{} +{ + ( switchLabel0() )* + try { + } catch (ParseException e) { - errorMessage = "'{' expected"; + errorMessage = "'}' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } - ( - line = SwitchLabel() - ( BlockStatementNoBreak() )* - [ breakToken = BreakStatement() ] - { - try { - if (breakToken == null) { - setMarker(fileToParse, - "You should use put a 'break' at the end of your statement", - line, - INFO, - "Line " + line); - } - } catch (CoreException e) { - PHPeclipsePlugin.log(e); - } - } - )* +} +/** + * A Switch statement with : ... endswitch; + * @param start the begin offset of the switch + * @param end the end offset of the switch + */ +void switchStatementColon(final int start, final int end) : +{} +{ + + {try { + setMarker(fileToParse, + "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;", + start, + end, + INFO, + "Line " + token.beginLine); + } catch (CoreException e) { + PHPeclipsePlugin.log(e); + }} + (switchLabel0())* try { - + } catch (ParseException e) { - errorMessage = "'}' expected"; + errorMessage = "'endswitch' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } + try { + + } catch (ParseException e) { + errorMessage = "';' expected after 'endswitch' keyword"; + errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; + throw e; + } +} + +void switchLabel0() : +{ + Token breakToken = null; + final int line; +} +{ + line = SwitchLabel() + ( BlockStatementNoBreak() | htmlBlock() )* + [ breakToken = BreakStatement() ] + { + try { + if (breakToken == null) { + setMarker(fileToParse, + "You should use put a 'break' at the end of your statement", + line, + INFO, + "Line " + line); + } + } catch (CoreException e) { + PHPeclipsePlugin.log(e); + } + } } Token BreakStatement() : @@ -1964,6 +2167,8 @@ Token BreakStatement() : } catch (ParseException e) { errorMessage = "';' expected after 'break' keyword"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } {return token;} @@ -1981,6 +2186,8 @@ int SwitchLabel() : if (errorMessage != null) throw e; errorMessage = "expression expected after 'case' keyword"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } try { @@ -1988,6 +2195,8 @@ int SwitchLabel() : } catch (ParseException e) { errorMessage = "':' expected after case expression"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } {return token.beginLine;} @@ -1998,6 +2207,8 @@ int SwitchLabel() : } catch (ParseException e) { errorMessage = "':' expected after 'default' keyword"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } {return token.beginLine;} @@ -2006,7 +2217,7 @@ int SwitchLabel() : void IfStatement() : { final Token token; - final int pos = jj_input_stream.bufpos; + final int pos = jj_input_stream.getPosition(); } { token = Condition("if") IfStatement0(pos,pos+token.image.length()) @@ -2020,7 +2231,9 @@ void Condition(final String keyword) : } catch (ParseException e) { errorMessage = "'(' expected after " + keyword + " keyword"; errorLevel = ERROR; - throw e; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length(); + errorEnd = errorStart +1; + processParseException(e); } Expression() try { @@ -2028,6 +2241,8 @@ void Condition(final String keyword) : } catch (ParseException e) { errorMessage = "')' expected after " + keyword + " keyword"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } @@ -2035,7 +2250,7 @@ void Condition(final String keyword) : void IfStatement0(final int start,final int end) : {} { - (Statement())* (ElseIfStatementColon())* [ElseStatementColon()] + (Statement() | htmlBlock())* (ElseIfStatementColon())* [ElseStatementColon()] {try { setMarker(fileToParse, @@ -2052,6 +2267,8 @@ void IfStatement0(final int start,final int end) : } catch (ParseException e) { errorMessage = "'endif' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } try { @@ -2059,22 +2276,40 @@ void IfStatement0(final int start,final int end) : } catch (ParseException e) { errorMessage = "';' expected after 'endif' keyword"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } | - Statement() ( LOOKAHEAD(1) ElseIfStatement() )* [ LOOKAHEAD(1) Statement() ] + (Statement() | htmlBlock()) + ( LOOKAHEAD(1) ElseIfStatement() )* + [ LOOKAHEAD(1) + + try { + Statement() + } catch (ParseException e) { + if (errorMessage != null) { + throw e; + } + errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected"; + errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; + throw e; + } + ] } void ElseIfStatementColon() : {} { - Condition("elseif") (Statement())* + Condition("elseif") (Statement() | htmlBlock())* } void ElseStatementColon() : {} { - (Statement())* + (Statement() | htmlBlock())* } void ElseIfStatement() : @@ -2086,7 +2321,7 @@ void ElseIfStatement() : void WhileStatement() : { final Token token; - final int pos = jj_input_stream.bufpos; + final int pos = jj_input_stream.getPosition(); } { token = Condition("while") WhileStatement0(pos,pos + token.image.length()) @@ -2111,13 +2346,17 @@ void WhileStatement0(final int start, final int end) : } catch (ParseException e) { errorMessage = "'endwhile' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } try { - ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) + } catch (ParseException e) { errorMessage = "';' expected after 'endwhile' keyword"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } | @@ -2129,10 +2368,12 @@ void DoStatement() : { Statement() Condition("while") try { - ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) + } catch (ParseException e) { - errorMessage = "';' expected"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } @@ -2146,6 +2387,8 @@ void ForeachStatement() : } catch (ParseException e) { errorMessage = "'(' expected after 'foreach' keyword"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } try { @@ -2153,14 +2396,18 @@ void ForeachStatement() : } catch (ParseException e) { errorMessage = "variable expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } - [ VariableSuffix() ] + ( VariableSuffix() )* try { } catch (ParseException e) { errorMessage = "'as' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } try { @@ -2168,6 +2415,8 @@ void ForeachStatement() : } catch (ParseException e) { errorMessage = "variable expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } [ Expression() ] @@ -2176,6 +2425,8 @@ void ForeachStatement() : } catch (ParseException e) { errorMessage = "')' expected after 'foreach' keyword"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } try { @@ -2184,6 +2435,8 @@ void ForeachStatement() : if (errorMessage != null) throw e; errorMessage = "statement expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } @@ -2191,7 +2444,7 @@ void ForeachStatement() : void ForStatement() : { final Token token; -final int pos = jj_input_stream.bufpos; +final int pos = jj_input_stream.getPosition(); } { token = @@ -2200,6 +2453,8 @@ final int pos = jj_input_stream.bufpos; } catch (ParseException e) { errorMessage = "'(' expected after 'for' keyword"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } [ ForInit() ] [ Expression() ] [ StatementExpressionList() ] @@ -2224,6 +2479,8 @@ final int pos = jj_input_stream.bufpos; } catch (ParseException e) { errorMessage = "'endfor' expected"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } try { @@ -2231,6 +2488,8 @@ final int pos = jj_input_stream.bufpos; } catch (ParseException e) { errorMessage = "';' expected after 'endfor' keyword"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } ) @@ -2254,12 +2513,14 @@ void StatementExpressionList() : void ContinueStatement() : {} { - [ ] + [ Expression() ] try { } catch (ParseException e) { errorMessage = "';' expected after 'continue' statement"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } @@ -2273,6 +2534,8 @@ void ReturnStatement() : } catch (ParseException e) { errorMessage = "';' expected after 'return' statement"; errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; throw e; } } \ No newline at end of file