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..e4026ea 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,9 +59,15 @@ 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() { } @@ -128,12 +135,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); } @@ -282,6 +300,8 @@ PARSER_END(PHPParser) { "//" : IN_SINGLE_LINE_COMMENT | + "#" : IN_SINGLE_LINE_COMMENT +| <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT | "/*" : IN_MULTI_LINE_COMMENT @@ -344,6 +364,10 @@ MORE : | "> } + TOKEN : +{ + +} /* RESERVED WORDS AND LITERALS */ TOKEN : @@ -574,18 +598,19 @@ void PhpBlock() : Expression() [ ] | [ - | - {try { - setMarker(fileToParse, - "You should use ' + {try { + setMarker(fileToParse, + "You should use ' } catch (ParseException e) { @@ -605,10 +630,28 @@ void ClassDeclaration() : { final PHPClassDeclaration classDeclaration; final Token className; - final int pos = jj_input_stream.bufpos; + 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); @@ -683,7 +726,7 @@ void FieldDeclaration() : PHPVarDeclaration VariableDeclarator() : { final String varName; - String varValue = null; + String varValue; final int pos = jj_input_stream.bufpos; } { @@ -827,7 +870,17 @@ void MethodDeclaration() : 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); @@ -947,6 +1000,9 @@ String Expression() : expr = PrintExpression() {return expr;} | + expr = ListExpression() + {return expr;} +| expr = ConditionalExpression() [ assignOperator = AssignmentOperator() @@ -1298,7 +1354,14 @@ String UnaryExpressionNotPlusMinus() : expr = Literal() {return expr;} | - expr = Expression() + expr = Expression() + try { + + } catch (ParseException e) { + errorMessage = "')' expected"; + errorLevel = ERROR; + throw e; + } {return "("+expr+")";} } @@ -1407,7 +1470,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() ] @@ -1474,7 +1544,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; } @@ -1509,11 +1579,10 @@ final StringBuffer buff = new StringBuffer(); {return buff.toString();} } -/* - * Statement syntax follows. +/** + * A Statement without break */ - -void Statement() : +void StatementNoBreak() : {} { LOOKAHEAD(2) @@ -1554,8 +1623,6 @@ void Statement() : | ForeachStatement() | - BreakStatement() -| ContinueStatement() | ReturnStatement() @@ -1569,6 +1636,17 @@ void Statement() : GlobalStatement() } +/** + * A Normal statement + */ +void Statement() : +{} +{ + StatementNoBreak() +| + BreakStatement() +} + void IncludeStatement() : { final String expr; @@ -1649,6 +1727,47 @@ 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() : {} { @@ -1705,7 +1824,13 @@ void Block() : throw e; } ( BlockStatement() )* - + try { + + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected"; + errorLevel = ERROR; + throw e; + } } void BlockStatement() : @@ -1718,6 +1843,19 @@ void BlockStatement() : MethodDeclaration() } +/** + * A Block statement that will not contain any 'break' + */ +void BlockStatementNoBreak() : +{} +{ + StatementNoBreak() +| + ClassDeclaration() +| + MethodDeclaration() +} + void LocalVariableDeclaration() : {} { @@ -1754,19 +1892,101 @@ 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 = + try { + + } catch (ParseException e) { + errorMessage = "';' expected after 'break' keyword"; + errorLevel = ERROR; + throw e; + } + ] + { + 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() : -{} +int SwitchLabel() : +{ + final Token token; +} { - Expression() + 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() : @@ -1799,8 +2019,7 @@ void Condition(final String keyword) : } void IfStatement0(final int start,final int end) : -{ -} +{} { (Statement())* (ElseIfStatementColon())* [ElseStatementColon()] @@ -1824,7 +2043,7 @@ void IfStatement0(final int start,final int end) : try { } catch (ParseException e) { - errorMessage = "';' expected 'endif' keyword"; + errorMessage = "';' expected after 'endif' keyword"; errorLevel = ERROR; throw e; } @@ -1969,7 +2188,7 @@ final int pos = jj_input_stream.bufpos; errorLevel = ERROR; throw e; } - [ ForInit() ] [ Expression() ] [ ForUpdate() ] + [ ForInit() ] [ Expression() ] [ StatementExpressionList() ] ( Statement() | @@ -1996,7 +2215,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; } @@ -2018,26 +2237,41 @@ void StatementExpressionList() : StatementExpression() ( StatementExpression() )* } -void ForUpdate() : -{} -{ - StatementExpressionList() -} - void BreakStatement() : {} { - [ ] + [ ] + try { + + } catch (ParseException e) { + errorMessage = "';' expected after 'break' statement"; + errorLevel = ERROR; + throw e; + } } 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