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 e3b05cf..b49b15a 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj @@ -50,6 +50,7 @@ import net.sourceforge.phpdt.internal.compiler.parser.PHPReqIncDeclaration; */ public final class PHPParser extends PHPParserSuperclass { + /** The file that is parsed. */ private static IFile fileToParse; /** The current segment */ @@ -58,24 +59,30 @@ public final class PHPParser extends PHPParserSuperclass { 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; + + /** 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; + private static int errorStart = -1; + private static int errorEnd = -1; + public PHPParser() { } - public final void setFileToParse(IFile fileToParse) { + public final void setFileToParse(final IFile fileToParse) { this.fileToParse = fileToParse; } - public PHPParser(IFile fileToParse) { + public PHPParser(final IFile fileToParse) { this(new StringReader("")); this.fileToParse = fileToParse; } - public static final void phpParserTester(String strEval) throws CoreException, ParseException { + public static final void phpParserTester(final String strEval) throws CoreException, ParseException { PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING); - StringReader stream = new StringReader(strEval); + final StringReader stream = new StringReader(strEval); if (jj_input_stream == null) { jj_input_stream = new SimpleCharStream(stream, 1, 1); } @@ -83,8 +90,8 @@ public final class PHPParser extends PHPParserSuperclass { phpTest(); } - public static final void htmlParserTester(String strEval) throws CoreException, ParseException { - StringReader stream = new StringReader(strEval); + public static final void htmlParserTester(final String strEval) throws CoreException, ParseException { + final StringReader stream = new StringReader(strEval); if (jj_input_stream == null) { jj_input_stream = new SimpleCharStream(stream, 1, 1); } @@ -92,10 +99,10 @@ public final class PHPParser extends PHPParserSuperclass { phpFile(); } - public final PHPOutlineInfo parseInfo(Object parent, String s) { + public final PHPOutlineInfo parseInfo(final Object parent, final String s) { outlineInfo = new PHPOutlineInfo(parent); currentSegment = outlineInfo.getDeclarations(); - StringReader stream = new StringReader(s); + final StringReader stream = new StringReader(s); if (jj_input_stream == null) { jj_input_stream = new SimpleCharStream(stream, 1, 1); } @@ -124,15 +131,27 @@ public final class PHPParser extends PHPParserSuperclass { /** * Create marker for the parse error + * @param e the ParseException */ - private static void setMarker(ParseException e) { + 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); } @@ -141,12 +160,12 @@ public final class PHPParser extends PHPParserSuperclass { /** * Create markers according to the external parser output */ - private static void createMarkers(String output, IFile file) throws CoreException { + private static void createMarkers(final String output, final IFile file) throws CoreException { // delete all markers file.deleteMarkers(IMarker.PROBLEM, false, 0); int indx = 0; - int brIndx = 0; + int brIndx; boolean flag = true; while ((brIndx = output.indexOf("
", indx)) != -1) { // newer php error output (tested with 4.2.3) @@ -163,7 +182,10 @@ public final class PHPParser extends PHPParserSuperclass { } } - private static void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException { + private static void scanLine(final String output, + final IFile file, + final int indx, + final int brIndx) throws CoreException { String current; StringBuffer lineNumberBuffer = new StringBuffer(10); char ch; @@ -201,8 +223,8 @@ public final class PHPParser extends PHPParserSuperclass { } } - public final void parse(String s) throws CoreException { - StringReader stream = new StringReader(s); + public final void parse(final String s) throws CoreException { + final StringReader stream = new StringReader(s); if (jj_input_stream == null) { jj_input_stream = new SimpleCharStream(stream, 1, 1); } @@ -218,15 +240,15 @@ public final class PHPParser extends PHPParserSuperclass { * Call the php parse command ( php -l -f <filename> ) * and create markers according to the external parser output */ - public static void phpExternalParse(IFile file) { - IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); - String filename = file.getLocation().toString(); + public static void phpExternalParse(final IFile file) { + final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); + final String filename = file.getLocation().toString(); - String[] arguments = { filename }; - MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF)); - String command = form.format(arguments); + final String[] arguments = { filename }; + final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF)); + final String command = form.format(arguments); - String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: "); + final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: "); try { // parse the buffer to find the errors and warnings @@ -245,7 +267,9 @@ PARSER_END(PHPParser) TOKEN : { - : PHPPARSING + : PHPPARSING +| : PHPPARSING +| { input_stream.backup(1); } : IN_FORMAL_COMMENT | "/*" : IN_MULTI_LINE_COMMENT @@ -286,7 +312,7 @@ PARSER_END(PHPParser) : PHPPARSING } - TOKEN : + SPECIAL_TOKEN : { " > : DEFAULT } @@ -319,72 +345,76 @@ MORE : | | | +| } /* LANGUAGE CONSTRUCT */ TOKEN : { - -| -| -| -| -| -| -| -| "> -| -| "> + +| +| +| +| +| +| +| +| "> +| +| "> } + TOKEN : +{ + +} /* RESERVED WORDS AND LITERALS */ TOKEN : { - < BREAK: "break" > -| < CASE: "case" > -| < CONST: "const" > -| < CONTINUE: "continue" > -| < _DEFAULT: "default" > -| < DO: "do" > -| < EXTENDS: "extends" > -| < FALSE: "false" > -| < FOR: "for" > -| < GOTO: "goto" > -| < NEW: "new" > -| < NULL: "null" > -| < RETURN: "return" > -| < SUPER: "super" > -| < SWITCH: "switch" > -| < THIS: "this" > -| < TRUE: "true" > -| < WHILE: "while" > -| < ENDWHILE : "endwhile" > -| -| -| -| + +| +| +| <_DEFAULT : "default"> +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| } /* TYPES */ TOKEN : { - -| -| + +| +| | -| -| -| -| +| +| +| +| | } TOKEN : { - < _ORL : "OR" > -| < _ANDL: "AND"> + <_ORL : "OR"> +| <_ANDL : "AND"> } /* LITERALS */ @@ -466,77 +496,79 @@ MORE : TOKEN : { - < LPAREN: "(" > -| < RPAREN: ")" > -| < LBRACE: "{" > -| < RBRACE: "}" > -| < LBRACKET: "[" > -| < RBRACKET: "]" > -| < SEMICOLON: ";" > -| < COMMA: "," > -| < DOT: "." > + +| +| +| +| +| +| +| +| } -/* OPERATORS */ +/* COMPARATOR */ TOKEN : { - -| -| " > -| -| -| -| -| -| =" > -| " > -| -| -| -| -| -| -| -| -| -| -| -| -| -| >" > -| >>" > -| -| >=" > -| -| -| "> +| +| +| ="> +| "> +| +| } +/* ASSIGNATION */ TOKEN : { - < DOLLAR_ID: > + +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| >"> +| >>"> +| >="> +} -/* - * Program structuring syntax follows. - */ + TOKEN : +{ + < DOLLAR_ID: > +} void phpTest() : {} @@ -549,7 +581,36 @@ void phpFile() : {} { try { - ( Php() + (PhpBlock())* + + } catch (TokenMgrError e) { + errorMessage = e.getMessage(); + errorLevel = ERROR; + throw generateParseException(); + } +} + +void PhpBlock() : +{ + final int start = jj_input_stream.bufpos; +} +{ + Expression() [ ] +| + [ + | + {try { + setMarker(fileToParse, + "You should use ' } catch (ParseException e) { @@ -557,13 +618,6 @@ void phpFile() : errorLevel = ERROR; throw e; } - )* - - } catch (TokenMgrError e) { - errorMessage = e.getMessage(); - errorLevel = ERROR; - throw generateParseException(); - } } void Php() : @@ -574,12 +628,30 @@ void Php() : void ClassDeclaration() : { - PHPClassDeclaration classDeclaration; - Token className; - final int pos = jj_input_stream.bufpos; + final PHPClassDeclaration classDeclaration; + final Token className; + final int pos; } { - className = [ ] + + try { + {pos = jj_input_stream.bufpos;} + className = + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected"; + errorLevel = ERROR; + throw e; + } + [ + + try { + + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected"; + errorLevel = ERROR; + throw e; + } + ] { if (currentSegment != null) { classDeclaration = new PHPClassDeclaration(currentSegment,className.image,pos); @@ -653,8 +725,8 @@ void FieldDeclaration() : PHPVarDeclaration VariableDeclarator() : { - String varName; - String varValue = null; + final String varName; + String varValue; final int pos = jj_input_stream.bufpos; } { @@ -663,18 +735,14 @@ PHPVarDeclaration VariableDeclarator() : try { varValue = VariableInitializer() + {return new PHPVarDeclaration(currentSegment,varName,pos,varValue);} } catch (ParseException e) { errorMessage = "Literal expression expected in variable initializer"; errorLevel = ERROR; throw e; } ] - { - if (varValue == null) { - return new PHPVarDeclaration(currentSegment,varName,pos); - } - return new PHPVarDeclaration(currentSegment,varName,pos,varValue); - } + {return new PHPVarDeclaration(currentSegment,varName,pos);} } String VariableDeclaratorId() : @@ -700,7 +768,7 @@ String VariableDeclaratorId() : String Variable(): { String expr = null; - Token token; + final Token token; } { token = [ expr = Expression() ] @@ -718,7 +786,7 @@ String Variable(): String VariableName(): { String expr = null; -Token token; +final Token token; } { expr = Expression() @@ -746,8 +814,8 @@ Token token; String VariableInitializer() : { - String expr; - Token token; + final String expr; + final Token token; } { expr = Literal() @@ -781,7 +849,7 @@ final StringBuffer buff = new StringBuffer(); String ArrayInitializer() : { -String expr = null; +String expr; final StringBuffer buff = new StringBuffer("("); } { @@ -799,10 +867,20 @@ final StringBuffer buff = new StringBuffer("("); void MethodDeclaration() : { - PHPFunctionDeclaration functionDeclaration; + final PHPFunctionDeclaration functionDeclaration; } { - functionDeclaration = MethodDeclarator() + + try { + functionDeclaration = MethodDeclarator() + } catch (ParseException e) { + if (errorMessage != null) { + throw e; + } + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected"; + errorLevel = ERROR; + throw e; + } { if (currentSegment != null) { currentSegment.add(functionDeclaration); @@ -819,9 +897,9 @@ void MethodDeclaration() : PHPFunctionDeclaration MethodDeclarator() : { - Token identifier; - StringBuffer methodDeclaration = new StringBuffer(); - String formalParameters; + final Token identifier; + final StringBuffer methodDeclaration = new StringBuffer(); + final String formalParameters; final int pos = jj_input_stream.bufpos; } { @@ -870,7 +948,7 @@ String FormalParameters() : String FormalParameter() : { - PHPVarDeclaration variableDeclaration; + final PHPVarDeclaration variableDeclaration; final StringBuffer buff = new StringBuffer(); } { @@ -914,32 +992,30 @@ String Type() : String Expression() : { - String expr; - String assignOperator = null; - String expr2 = null; + final String expr; + final String assignOperator; + final String expr2; } { expr = PrintExpression() {return expr;} | + expr = ListExpression() + {return expr;} +| expr = ConditionalExpression() [ assignOperator = AssignmentOperator() try { expr2 = Expression() + {return expr + assignOperator + expr2;} } catch (ParseException e) { errorMessage = "expression expected"; errorLevel = ERROR; throw e; } ] - { - if (expr2 == null) { - return expr; - } else { - return expr + assignOperator + expr2; - } - } + {return expr;} } String AssignmentOperator() : @@ -975,7 +1051,7 @@ String AssignmentOperator() : String ConditionalExpression() : { - String expr; + final String expr; String expr2 = null; String expr3 = null; } @@ -994,19 +1070,16 @@ String ConditionalOrExpression() : { String expr; Token operator; - String expr2 = null; final StringBuffer buff = new StringBuffer(); } { expr = ConditionalAndExpression() - { - buff.append(expr); - } + {buff.append(expr);} ( - (operator = | operator = <_ORL>) expr2 = ConditionalAndExpression() + (operator = | operator = <_ORL>) expr = ConditionalAndExpression() { buff.append(operator.image); - buff.append(expr2); + buff.append(expr); } )* { @@ -1018,76 +1091,54 @@ String ConditionalAndExpression() : { String expr; Token operator; - String expr2 = null; final StringBuffer buff = new StringBuffer(); } { expr = ConcatExpression() - { - buff.append(expr); - } + {buff.append(expr);} ( - (operator = | operator = <_ANDL>) expr2 = ConcatExpression() + (operator = | operator = <_ANDL>) expr = ConcatExpression() { buff.append(operator.image); - buff.append(expr2); + buff.append(expr); } )* - { - return buff.toString(); - } + {return buff.toString();} } String ConcatExpression() : { String expr; - String expr2 = null; final StringBuffer buff = new StringBuffer(); } { expr = InclusiveOrExpression() - { - buff.append(expr); - } + {buff.append(expr);} ( - expr2 = InclusiveOrExpression() - { - buff.append("."); - buff.append(expr2); - } + expr = InclusiveOrExpression() + {buff.append(".").append(expr);} )* - { - return buff.toString(); - } + {return buff.toString();} } String InclusiveOrExpression() : { String expr; - String expr2 = null; final StringBuffer buff = new StringBuffer(); } { expr = ExclusiveOrExpression() - { - buff.append(expr); - } + {buff.append(expr);} ( - expr2 = ExclusiveOrExpression() - { - buff.append("|"); - buff.append(expr2); - } + expr = ExclusiveOrExpression() + {buff.append("|").append(expr);} )* - { - return buff.toString(); - } + {return buff.toString();} } String ExclusiveOrExpression() : { String expr; - String expr2 = null; final StringBuffer buff = new StringBuffer(); } { @@ -1096,10 +1147,10 @@ String ExclusiveOrExpression() : buff.append(expr); } ( - expr2 = AndExpression() + expr = AndExpression() { buff.append("^"); - buff.append(expr2); + buff.append(expr); } )* { @@ -1109,8 +1160,7 @@ String ExclusiveOrExpression() : String AndExpression() : { - final String expr; - String expr2 = null; + String expr; final StringBuffer buff = new StringBuffer(); } { @@ -1119,22 +1169,18 @@ String AndExpression() : buff.append(expr); } ( - expr2 = EqualityExpression() + expr = EqualityExpression() { - buff.append("&"); - buff.append(expr2); + buff.append("&").append(expr); } )* - { - return buff.toString(); - } + {return buff.toString();} } String EqualityExpression() : { String expr; Token operator; - String expr2; final StringBuffer buff = new StringBuffer(); } { @@ -1147,10 +1193,16 @@ String EqualityExpression() : | operator = | operator = ) - expr2 = RelationalExpression() + try { + expr = RelationalExpression() + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected after '"+operator.image+"'"; + errorLevel = ERROR; + throw e; + } { buff.append(operator.image); - buff.append(expr2); + buff.append(expr); } )* {return buff.toString();} @@ -1160,18 +1212,14 @@ String RelationalExpression() : { String expr; Token operator; - String expr2; final StringBuffer buff = new StringBuffer(); } { expr = ShiftExpression() {buff.append(expr);} ( - ( operator = | operator = | operator = | operator = ) expr2 = ShiftExpression() - { - buff.append(operator.image); - buff.append(expr2); - } + ( operator = | operator = | operator = | operator = ) expr = ShiftExpression() + {buff.append(operator.image).append(expr);} )* {return buff.toString();} } @@ -1237,8 +1285,8 @@ String MultiplicativeExpression() : */ String UnaryExpression() : { - String expr; - Token token; + final String expr; + final Token token; final StringBuffer buff = new StringBuffer(); } { @@ -1256,8 +1304,8 @@ String UnaryExpression() : String UnaryExpressionNoPrefix() : { - String expr; - Token token; + final String expr; + final Token token; } { ( token = | token = ) expr = UnaryExpression() @@ -1278,7 +1326,7 @@ String UnaryExpressionNoPrefix() : String PreIncrementExpression() : { -String expr; +final String expr; } { expr = PrimaryExpression() @@ -1287,7 +1335,7 @@ String expr; String PreDecrementExpression() : { -String expr; +final String expr; } { expr = PrimaryExpression() @@ -1296,7 +1344,7 @@ String expr; String UnaryExpressionNotPlusMinus() : { - String expr; + final String expr; } { expr = UnaryExpression() @@ -1312,7 +1360,14 @@ String UnaryExpressionNotPlusMinus() : expr = Literal() {return expr;} | - expr = Expression() + expr = Expression() + try { + + } catch (ParseException e) { + errorMessage = "')' expected"; + errorLevel = ERROR; + throw e; + } {return "("+expr+")";} } @@ -1327,7 +1382,7 @@ final String type, expr; String PostfixExpression() : { - String expr; + final String expr; Token operator = null; } { @@ -1342,7 +1397,7 @@ String PostfixExpression() : String PrimaryExpression() : { - Token identifier; + final Token identifier; String expr; final StringBuffer buff = new StringBuffer(); } @@ -1375,8 +1430,8 @@ String ArrayDeclarator() : String PrimaryPrefix() : { - String expr; - Token token = null; + final String expr; + final Token token; } { token = @@ -1393,8 +1448,8 @@ String PrimaryPrefix() : String ClassIdentifier(): { - String expr; - Token token; + final String expr; + final Token token; } { token = @@ -1406,7 +1461,7 @@ String ClassIdentifier(): String PrimarySuffix() : { - String expr; + final String expr; } { expr = Arguments() @@ -1421,7 +1476,14 @@ 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; + throw e; + } {return "->" + expr;} | [ expr = Expression() ] @@ -1442,8 +1504,8 @@ String VariableSuffix() : String Literal() : { - String expr; - Token token; + final String expr; + final Token token; } { token = @@ -1488,7 +1550,7 @@ 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; throw e; } @@ -1517,23 +1579,22 @@ final StringBuffer buff = new StringBuffer(); throw e; } { - buff.append(",").append("expr"); + buff.append(",").append(expr); } )* {return buff.toString();} } -/* - * Statement syntax follows. +/** + * A Statement without break */ - -void Statement() : +void StatementNoBreak() : {} { LOOKAHEAD(2) Expression() try { - ( | "?>") + ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) } catch (ParseException e) { errorMessage = "';' expected"; errorLevel = ERROR; @@ -1568,8 +1629,6 @@ void Statement() : | ForeachStatement() | - BreakStatement() -| ContinueStatement() | ReturnStatement() @@ -1583,9 +1642,20 @@ void Statement() : GlobalStatement() } +/** + * A Normal statement + */ +void Statement() : +{} +{ + StatementNoBreak() +| + BreakStatement() +} + void IncludeStatement() : { - String expr; + final String expr; final int pos = jj_input_stream.bufpos; } { @@ -1597,7 +1667,7 @@ void IncludeStatement() : } } try { - ( | "?>") + ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) } catch (ParseException e) { errorMessage = "';' expected"; errorLevel = ERROR; @@ -1612,7 +1682,7 @@ void IncludeStatement() : } } try { - ( | "?>") + ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) } catch (ParseException e) { errorMessage = "';' expected"; errorLevel = ERROR; @@ -1627,7 +1697,7 @@ void IncludeStatement() : } } try { - ( | "?>") + ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) } catch (ParseException e) { errorMessage = "';' expected"; errorLevel = ERROR; @@ -1642,7 +1712,7 @@ void IncludeStatement() : } } try { - ( | "?>") + ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) } catch (ParseException e) { errorMessage = "';' expected"; errorLevel = ERROR; @@ -1653,7 +1723,7 @@ void IncludeStatement() : String PrintExpression() : { final StringBuffer buff = new StringBuffer("print "); - String expr; + final String expr; } { expr = Expression() @@ -1663,12 +1733,53 @@ 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; + throw e; + } + [ + expr = VariableDeclaratorId() + {buff.append(expr);} + ] + [ + try { + + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected"; + errorLevel = ERROR; + throw e; + } + expr = VariableDeclaratorId() + {buff.append(",").append(expr);} + ] + {buff.append(")");} + try { + + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected"; + errorLevel = ERROR; + throw e; + } + [ expr = Expression() {buff.append("(").append(expr);}] + {return buff.toString();} +} + void EchoStatement() : {} { Expression() ( Expression())* try { - ( | "?>") + ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) } catch (ParseException e) { errorMessage = "';' expected after 'echo' statement"; errorLevel = ERROR; @@ -1681,7 +1792,7 @@ void GlobalStatement() : { VariableDeclaratorId() ( VariableDeclaratorId())* try { - ( | "?>") + ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) } catch (ParseException e) { errorMessage = "';' expected"; errorLevel = ERROR; @@ -1694,7 +1805,7 @@ void StaticStatement() : { VariableDeclarator() ( VariableDeclarator())* try { - ( | "?>") + ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) } catch (ParseException e) { errorMessage = "';' expected"; errorLevel = ERROR; @@ -1719,7 +1830,13 @@ void Block() : throw e; } ( BlockStatement() )* - + try { + + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected"; + errorLevel = ERROR; + throw e; + } } void BlockStatement() : @@ -1732,10 +1849,29 @@ void BlockStatement() : MethodDeclaration() } +/** + * A Block statement that will not contain any 'break' + */ +void BlockStatementNoBreak() : +{} +{ + StatementNoBreak() +| + ClassDeclaration() +| + MethodDeclaration() +} + void LocalVariableDeclaration() : {} { - VariableDeclarator() ( VariableDeclarator() )* + LocalVariableDeclarator() ( LocalVariableDeclarator() )* +} + +void LocalVariableDeclarator() : +{} +{ + VariableDeclaratorId() [ Expression() ] } void EmptyStatement() : @@ -1762,36 +1898,121 @@ void StatementExpression() : } void SwitchStatement() : -{} { - Expression() - ( SwitchLabel() ( BlockStatement() )* )* - + Token breakToken = null; + int line; +} +{ + + try { + + } catch (ParseException e) { + errorMessage = "'(' expected after 'switch'"; + errorLevel = ERROR; + throw e; + } + Expression() + try { + + } catch (ParseException e) { + errorMessage = "')' expected"; + errorLevel = ERROR; + throw e; + } + try { + + } catch (ParseException e) { + errorMessage = "'{' expected"; + errorLevel = ERROR; + 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); + } + } + )* + try { + + } catch (ParseException e) { + errorMessage = "'}' expected"; + errorLevel = ERROR; + throw e; + } } -void SwitchLabel() : -{} +Token BreakStatement() : +{ + final Token token; +} +{ + token = [ Expression() ] + try { + + } catch (ParseException e) { + errorMessage = "';' expected after 'break' keyword"; + errorLevel = ERROR; + throw e; + } + {return token;} +} + +int SwitchLabel() : { - Expression() + final Token token; +} +{ + token = + try { + Expression() + } catch (ParseException e) { + if (errorMessage != null) throw e; + errorMessage = "expression expected after 'case' keyword"; + errorLevel = ERROR; + throw e; + } + try { + + } catch (ParseException e) { + errorMessage = "':' expected after case expression"; + errorLevel = ERROR; + throw e; + } + {return token.beginLine;} | - <_DEFAULT> + token = <_DEFAULT> + try { + + } catch (ParseException e) { + errorMessage = "':' expected after 'default' keyword"; + errorLevel = ERROR; + throw e; + } + {return token.beginLine;} } void IfStatement() : -/* - * The disambiguating algorithm of JavaCC automatically binds dangling - * else's to the innermost if statement. The LOOKAHEAD specification - * is to tell JavaCC that we know what we are doing. - */ { - Token token; + final Token token; final int pos = jj_input_stream.bufpos; } { token = Condition("if") IfStatement0(pos,pos+token.image.length()) } -void Condition(String keyword) : +void Condition(final String keyword) : {} { try { @@ -1811,9 +2032,8 @@ void Condition(String keyword) : } } -void IfStatement0(int start,int end) : -{ -} +void IfStatement0(final int start,final int end) : +{} { (Statement())* (ElseIfStatementColon())* [ElseStatementColon()] @@ -1837,7 +2057,7 @@ void IfStatement0(int start,int end) : try { } catch (ParseException e) { - errorMessage = "';' expected 'endif' keyword"; + errorMessage = "';' expected after 'endif' keyword"; errorLevel = ERROR; throw e; } @@ -1865,7 +2085,7 @@ void ElseIfStatement() : void WhileStatement() : { - Token token; + final Token token; final int pos = jj_input_stream.bufpos; } { @@ -1894,7 +2114,7 @@ void WhileStatement0(final int start, final int end) : throw e; } try { - ( | "?>") + ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) } catch (ParseException e) { errorMessage = "';' expected after 'endwhile' keyword"; errorLevel = ERROR; @@ -1909,7 +2129,7 @@ void DoStatement() : { Statement() Condition("while") try { - ( | "?>") + ( | {PHPParserTokenManager.SwitchTo(PHPParserTokenManager.DEFAULT);}) } catch (ParseException e) { errorMessage = "';' expected"; errorLevel = ERROR; @@ -1935,6 +2155,7 @@ void ForeachStatement() : errorLevel = ERROR; throw e; } + [ VariableSuffix() ] try { } catch (ParseException e) { @@ -1958,7 +2179,7 @@ void ForeachStatement() : throw e; } try { - Statement() + Statement() } catch (ParseException e) { if (errorMessage != null) throw e; errorMessage = "statement expected"; @@ -1969,7 +2190,7 @@ void ForeachStatement() : void ForStatement() : { -Token token; +final Token token; final int pos = jj_input_stream.bufpos; } { @@ -1981,7 +2202,7 @@ final int pos = jj_input_stream.bufpos; errorLevel = ERROR; throw e; } - [ ForInit() ] [ Expression() ] [ ForUpdate() ] + [ ForInit() ] [ Expression() ] [ StatementExpressionList() ] ( Statement() | @@ -2008,7 +2229,7 @@ final int pos = jj_input_stream.bufpos; try { } catch (ParseException e) { - errorMessage = "';' expected 'endfor' keyword"; + errorMessage = "';' expected after 'endfor' keyword"; errorLevel = ERROR; throw e; } @@ -2030,26 +2251,28 @@ void StatementExpressionList() : StatementExpression() ( StatementExpression() )* } -void ForUpdate() : -{} -{ - StatementExpressionList() -} - -void BreakStatement() : -{} -{ - [ ] -} - void ContinueStatement() : {} { - [ ] + [ ] + try { + + } catch (ParseException e) { + errorMessage = "';' expected after 'continue' statement"; + errorLevel = ERROR; + throw e; + } } void ReturnStatement() : {} { - [ Expression() ] + [ Expression() ] + try { + + } catch (ParseException e) { + errorMessage = "';' expected after 'return' statement"; + errorLevel = ERROR; + throw e; + } } \ No newline at end of file