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 34ee7b7..abf9f51 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj @@ -29,45 +29,52 @@ import org.eclipse.ui.texteditor.MarkerUtilities; import org.eclipse.jface.preference.IPreferenceStore; import java.util.Hashtable; +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 */ public final class PHPParser extends PHPParserSuperclass { + /** The file that is parsed. */ private static IFile fileToParse; - /** The current segment */ + /** The current segment. */ private static PHPSegmentWithChildren currentSegment; private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$ private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$ PHPOutlineInfo outlineInfo; + + private static PHPFunctionDeclaration currentFunction; + private static boolean assigning; + + /** 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 */ private static String errorMessage; - public PHPParser() { - } + private static int errorStart = -1; + private static int errorEnd = -1; public final void setFileToParse(final IFile fileToParse) { this.fileToParse = fileToParse; } + public PHPParser() { + } + public PHPParser(final IFile fileToParse) { this(new StringReader("")); this.fileToParse = fileToParse; @@ -83,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) { @@ -117,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; @@ -128,12 +150,23 @@ public final class PHPParser extends PHPParserSuperclass { */ private static void setMarker(final ParseException e) { try { - setMarker(fileToParse, - errorMessage, - jj_input_stream.tokenBegin, - jj_input_stream.tokenBegin + e.currentToken.image.length(), - errorLevel, - "Line " + e.currentToken.beginLine); + if (errorStart == -1) { + setMarker(fileToParse, + errorMessage, + jj_input_stream.tokenBegin, + jj_input_stream.tokenBegin + e.currentToken.image.length(), + errorLevel, + "Line " + e.currentToken.beginLine); + } else { + setMarker(fileToParse, + errorMessage, + errorStart, + errorEnd, + errorLevel, + "Line " + e.currentToken.beginLine); + errorStart = -1; + errorEnd = -1; + } } catch (CoreException e2) { PHPeclipsePlugin.log(e2); } @@ -249,9 +282,9 @@ PARSER_END(PHPParser) TOKEN : { - : PHPPARSING + : PHPPARSING | : PHPPARSING -| "> : DEFAULT } +/* Skip any character if we are not in php mode */ SKIP : { < ~[] > @@ -266,7 +300,6 @@ PARSER_END(PHPParser) /* WHITE SPACE */ - SKIP : { " " @@ -277,11 +310,12 @@ PARSER_END(PHPParser) } /* COMMENTS */ - SPECIAL_TOKEN : { "//" : IN_SINGLE_LINE_COMMENT | + "#" : IN_SINGLE_LINE_COMMENT +| <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT | "/*" : IN_MULTI_LINE_COMMENT @@ -326,6 +360,7 @@ MORE : | | | +| } /* LANGUAGE CONSTRUCT */ @@ -366,6 +401,7 @@ MORE : | | | +| | | | @@ -373,7 +409,6 @@ MORE : } /* TYPES */ - TOKEN : { @@ -394,7 +429,6 @@ MORE : } /* LITERALS */ - TOKEN : { < INTEGER_LITERAL: @@ -422,9 +456,10 @@ MORE : | < STRING_1: "\"" ( - ~["\""] + ~["\""] | ~["{"] | "\\\"" + | "{" ~["\""] "}" )* "\"" > @@ -520,6 +555,7 @@ MORE : | | +| | | | @@ -560,41 +596,58 @@ 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() | [ - | - {try { - setMarker(fileToParse, - "You should use ' + {try { + setMarker(fileToParse, + "You should use ' } 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; } } +void phpEchoBlock() : +{} +{ + Expression() [ ] +} + void Php() : {} { @@ -605,10 +658,32 @@ void ClassDeclaration() : { final PHPClassDeclaration classDeclaration; final Token className; - final int pos = jj_input_stream.bufpos; + final int pos; } { - className = [ ] + + try { + {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; + } + [ + + try { + + } 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; + } + ] { if (currentSegment != null) { classDeclaration = new PHPClassDeclaration(currentSegment,className.image,pos); @@ -630,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() : {} { @@ -652,6 +734,9 @@ void ClassBodyDeclaration() : FieldDeclaration() } +/** + * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;. + */ void FieldDeclaration() : { PHPVarDeclaration variableDeclaration; @@ -674,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 = null; - final int pos = jj_input_stream.bufpos; + final String varName, varValue; + final int pos = jj_input_stream.getPosition(); } { varName = VariableDeclaratorId() @@ -696,6 +782,8 @@ PHPVarDeclaration VariableDeclarator() : } 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; } ] @@ -718,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; } } @@ -730,14 +820,20 @@ String Variable(): { token = [ expr = Expression() ] { - if (expr == null) { - return token.image; + if (expr == null && !assigning) { + if (currentFunction != null) { + PHPVarDeclaration var = currentFunction.getParameter(token.image.substring(1)); + if (var != null) { + var.getVariable().setUsed(true); + } + } + return token.image.substring(1); } return token + "{" + expr + "}"; } | expr = VariableName() - {return "$" + expr;} + {return expr;} } String VariableName(): @@ -752,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() : @@ -814,7 +935,9 @@ final StringBuffer buff = new StringBuffer("("); {buff.append(expr);} ( LOOKAHEAD(2) expr = ArrayVariable() {buff.append(",").append(expr);} - )* ] + )* + ] + [ {buff.append(",");}] { buff.append(")"); @@ -822,87 +945,139 @@ final StringBuffer buff = new StringBuffer("("); } } +/** + * A Method Declaration. + * function MetodDeclarator() Block() + */ void MethodDeclaration() : { final PHPFunctionDeclaration functionDeclaration; + Token functionToken; } { - functionDeclaration = MethodDeclarator() + functionToken = + try { + functionDeclaration = MethodDeclarator() + } catch (ParseException e) { + if (errorMessage != null) { + throw e; + } + 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; + } { if (currentSegment != null) { 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().setReference(true); + } + return variableDeclaration; } } @@ -947,19 +1122,43 @@ String Expression() : expr = PrintExpression() {return expr;} | + expr = ListExpression() + {return expr;} +| + LOOKAHEAD(varAssignation()) + expr = varAssignation() + {return expr;} +| expr = ConditionalExpression() - [ - assignOperator = AssignmentOperator() + {return expr;} +} + +/** + * A Variable assignation. + * varName (an assign operator) any expression + */ +String varAssignation() : +{ + String varName,assignOperator,expr2; + PHPVarDeclaration variable; + final int pos = SimpleCharStream.getPosition(); +} +{ + varName = VariableDeclaratorId() + assignOperator = AssignmentOperator() try { 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; } - ] - {return expr;} + {return varName + assignOperator + expr2;} } String AssignmentOperator() : @@ -1137,7 +1336,15 @@ String EqualityExpression() : | operator = | operator = ) - expr = RelationalExpression() + try { + expr = RelationalExpression() + } 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; + } { buff.append(operator.image); buff.append(expr); @@ -1206,14 +1413,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();} } @@ -1251,10 +1466,7 @@ String UnaryExpressionNoPrefix() : return token.image + expr; } | - expr = PreIncrementExpression() - {return expr;} -| - expr = PreDecrementExpression() + expr = PreIncDecExpression() {return expr;} | expr = UnaryExpressionNotPlusMinus() @@ -1262,22 +1474,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() : @@ -1288,7 +1492,7 @@ String UnaryExpressionNotPlusMinus() : expr = UnaryExpression() {return "!" + expr;} | - LOOKAHEAD( Type() ) + LOOKAHEAD( (Type() | ) ) expr = CastExpression() {return expr;} | @@ -1298,7 +1502,16 @@ String UnaryExpressionNotPlusMinus() : expr = Literal() {return expr;} | - expr = Expression() + expr = Expression() + 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; + } {return "("+expr+")";} } @@ -1307,7 +1520,7 @@ String CastExpression() : final String type, expr; } { - type = Type() expr = UnaryExpression() + (type = Type() | {type = "array";}) expr = UnaryExpression() {return "(" + type + ")" + expr;} } @@ -1372,11 +1585,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; @@ -1407,15 +1635,26 @@ String VariableSuffix() : String expr = null; } { - expr = VariableName() + + try { + expr = VariableName() + } 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; } { @@ -1444,8 +1683,8 @@ String Literal() : expr = BooleanLiteral() {return expr;} | - expr = NullLiteral() - {return expr;} + + {return "null";} } String BooleanLiteral() : @@ -1458,13 +1697,6 @@ String BooleanLiteral() : {return "false";} } -String NullLiteral() : -{} -{ - - {return "null";} -} - String Arguments() : { String expr = null; @@ -1474,8 +1706,10 @@ String expr = null; try { } catch (ParseException e) { - errorMessage = "')' expected to close the argument list"; + 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; } { @@ -1500,6 +1734,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; } { @@ -1509,21 +1745,24 @@ final StringBuffer buff = new StringBuffer(); {return buff.toString();} } -/* - * Statement syntax follows. +/** + * A Statement without break */ - -void Statement() : +void StatementNoBreak() : {} { LOOKAHEAD(2) Expression() try { - ( | ) + } 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) @@ -1537,8 +1776,10 @@ void Statement() : 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; } | @@ -1554,8 +1795,6 @@ void Statement() : | ForeachStatement() | - BreakStatement() -| ContinueStatement() | ReturnStatement() @@ -1569,69 +1808,50 @@ void Statement() : GlobalStatement() } +/** + * A Normal statement + */ +void Statement() : +{} +{ + StatementNoBreak() +| + 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(); } { - - expr = Expression() - { - if (currentSegment != null) { - currentSegment.add(new PHPReqIncDeclaration(currentSegment, "require",pos,expr)); - } - } - try { - ( | "?>") - } catch (ParseException e) { - errorMessage = "';' expected"; - errorLevel = ERROR; - throw e; - } -| - - expr = Expression() - { - if (currentSegment != null) { - currentSegment.add(new PHPReqIncDeclaration(currentSegment, "require_once",pos,expr)); - } - } - try { - ( | "?>") - } catch (ParseException e) { - errorMessage = "';' expected"; - errorLevel = ERROR; - throw e; - } -| - - expr = Expression() - { - if (currentSegment != null) { - currentSegment.add(new PHPReqIncDeclaration(currentSegment, "include",pos,expr)); - } - } - try { - ( | "?>") - } catch (ParseException e) { - errorMessage = "';' expected"; - errorLevel = ERROR; - throw e; - } -| - + ( token = + | token = + | token = + | token = ) expr = Expression() { if (currentSegment != null) { - currentSegment.add(new PHPReqIncDeclaration(currentSegment, "include_once",pos,expr)); + currentSegment.add(new PHPReqIncDeclaration(currentSegment, token.image,pos,expr)); } } try { - ( | "?>") + } 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; } } @@ -1649,28 +1869,97 @@ String PrintExpression() : } } +String ListExpression() : +{ + final StringBuffer buff = new StringBuffer("list("); + String 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(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 { - ( | "?>") + } 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 { - ( | "?>") + } 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; } } @@ -1680,10 +1969,12 @@ void StaticStatement() : { VariableDeclarator() ( VariableDeclarator())* try { - ( | "?>") + } 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; } } @@ -1702,10 +1993,20 @@ 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() | 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; } - ( BlockStatement() )* - } void BlockStatement() : @@ -1718,6 +2019,19 @@ void BlockStatement() : MethodDeclaration() } +/** + * A Block statement that will not contain any 'break' + */ +void BlockStatementNoBreak() : +{} +{ + StatementNoBreak() +| + ClassDeclaration() +| + MethodDeclaration() +} + void LocalVariableDeclaration() : {} { @@ -1739,9 +2053,7 @@ void EmptyStatement() : void StatementExpression() : {} { - PreIncrementExpression() -| - PreDecrementExpression() + PreIncDecExpression() | PrimaryExpression() [ @@ -1754,25 +2066,174 @@ void StatementExpression() : } void SwitchStatement() : -{} { - Expression() - ( SwitchLabel() ( BlockStatement() )* )* - + final int pos = jj_input_stream.getPosition(); +} +{ + + try { + + } 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() + 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; + } + (switchStatementBrace() | switchStatementColon(pos, pos + 6)) } -void SwitchLabel() : +void switchStatementBrace() : {} { - Expression() + + ( switchLabel0() )* + 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; + } +} +/** + * 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 = "'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() : +{ + final Token token; +} +{ + token = [ Expression() ] + try { + + } 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;} +} + +int SwitchLabel() : +{ + final Token token; +} +{ + token = + try { + Expression() + } catch (ParseException e) { + 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 { + + } 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;} | - <_DEFAULT> + token = <_DEFAULT> + try { + + } 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;} } 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()) @@ -1786,7 +2247,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 { @@ -1794,15 +2257,16 @@ 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; } } void IfStatement0(final int start,final int end) : +{} { -} -{ - (Statement())* (ElseIfStatementColon())* [ElseStatementColon()] + (Statement() | htmlBlock())* (ElseIfStatementColon())* [ElseStatementColon()] {try { setMarker(fileToParse, @@ -1819,29 +2283,49 @@ 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 { } catch (ParseException e) { - errorMessage = "';' expected 'endif' keyword"; + 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() : @@ -1853,7 +2337,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()) @@ -1878,13 +2362,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 { - ( | "?>") + } 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; } | @@ -1896,10 +2384,12 @@ void DoStatement() : { Statement() Condition("while") try { - ( | "?>") + } 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; } } @@ -1913,6 +2403,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 { @@ -1920,14 +2412,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 { @@ -1935,6 +2431,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() ] @@ -1943,6 +2441,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 { @@ -1951,6 +2451,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; } } @@ -1958,7 +2460,7 @@ void ForeachStatement() : void ForStatement() : { final Token token; -final int pos = jj_input_stream.bufpos; +final int pos = jj_input_stream.getPosition(); } { token = @@ -1967,9 +2469,11 @@ 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() ] [ ForUpdate() ] + [ ForInit() ] [ Expression() ] [ StatementExpressionList() ] ( Statement() | @@ -1991,13 +2495,17 @@ 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 { } catch (ParseException e) { - errorMessage = "';' expected 'endfor' keyword"; + 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; } ) @@ -2018,26 +2526,32 @@ void StatementExpressionList() : StatementExpression() ( StatementExpression() )* } -void ForUpdate() : -{} -{ - StatementExpressionList() -} - -void BreakStatement() : -{} -{ - [ ] -} - 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; + } } void ReturnStatement() : {} { - [ Expression() ] + [ Expression() ] + try { + + } 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