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 46fb115..e3b05cf 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj @@ -48,7 +48,7 @@ import net.sourceforge.phpdt.internal.compiler.parser.PHPReqIncDeclaration; * You can test the parser with the PHPParserTestCase2.java * @author Matthieu Casanova */ -public class PHPParser extends PHPParserSuperclass { +public final class PHPParser extends PHPParserSuperclass { private static IFile fileToParse; @@ -64,7 +64,7 @@ public class PHPParser extends PHPParserSuperclass { public PHPParser() { } - public void setFileToParse(IFile fileToParse) { + public final void setFileToParse(IFile fileToParse) { this.fileToParse = fileToParse; } @@ -73,7 +73,7 @@ public class PHPParser extends PHPParserSuperclass { this.fileToParse = fileToParse; } - public void phpParserTester(String strEval) throws CoreException, ParseException { + public static final void phpParserTester(String strEval) throws CoreException, ParseException { PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING); StringReader stream = new StringReader(strEval); if (jj_input_stream == null) { @@ -83,7 +83,7 @@ public class PHPParser extends PHPParserSuperclass { phpTest(); } - public void htmlParserTester(String strEval) throws CoreException, ParseException { + public static final void htmlParserTester(String strEval) throws CoreException, ParseException { StringReader stream = new StringReader(strEval); if (jj_input_stream == null) { jj_input_stream = new SimpleCharStream(stream, 1, 1); @@ -92,7 +92,7 @@ public class PHPParser extends PHPParserSuperclass { phpFile(); } - public PHPOutlineInfo parseInfo(Object parent, String s) { + public final PHPOutlineInfo parseInfo(Object parent, String s) { outlineInfo = new PHPOutlineInfo(parent); currentSegment = outlineInfo.getDeclarations(); StringReader stream = new StringReader(s); @@ -201,8 +201,12 @@ public class PHPParser extends PHPParserSuperclass { } } - public void parse(String s) throws CoreException { - ReInit(new StringReader(s)); + public final void parse(String s) throws CoreException { + StringReader stream = new StringReader(s); + if (jj_input_stream == null) { + jj_input_stream = new SimpleCharStream(stream, 1, 1); + } + ReInit(stream); try { parse(); } catch (ParseException e) { @@ -232,7 +236,7 @@ public class PHPParser extends PHPParserSuperclass { } } - public void parse() throws ParseException { + public static final void parse() throws ParseException { phpFile(); } } @@ -356,6 +360,8 @@ MORE : | < TRUE: "true" > | < WHILE: "while" > | < ENDWHILE : "endwhile" > +| +| | | } @@ -487,6 +493,7 @@ MORE : | =" > | " > | | | @@ -542,7 +549,15 @@ void phpFile() : {} { try { - ( Php() )* + ( Php() + try { + + } catch (ParseException e) { + errorMessage = "'?>' expected"; + errorLevel = ERROR; + throw e; + } + )* } catch (TokenMgrError e) { errorMessage = e.getMessage(); @@ -561,18 +576,22 @@ void ClassDeclaration() : { PHPClassDeclaration classDeclaration; Token className; - int pos = jj_input_stream.bufpos; + final int pos = jj_input_stream.bufpos; } { className = [ ] { - classDeclaration = new PHPClassDeclaration(currentSegment,className.image,pos); - currentSegment.add(classDeclaration); - currentSegment = classDeclaration; + if (currentSegment != null) { + classDeclaration = new PHPClassDeclaration(currentSegment,className.image,pos); + currentSegment.add(classDeclaration); + currentSegment = classDeclaration; + } } ClassBody() { - currentSegment = (PHPSegmentWithChildren) currentSegment.getParent(); + if (currentSegment != null) { + currentSegment = (PHPSegmentWithChildren) currentSegment.getParent(); + } } } @@ -610,10 +629,18 @@ void FieldDeclaration() : } { variableDeclaration = VariableDeclarator() - {currentSegment.add(variableDeclaration);} + { + if (currentSegment != null) { + currentSegment.add(variableDeclaration); + } + } ( - variableDeclaration = VariableDeclarator() - {currentSegment.add(variableDeclaration);} + variableDeclaration = VariableDeclarator() + { + if (currentSegment != null) { + currentSegment.add(variableDeclaration); + } + } )* try { @@ -628,7 +655,7 @@ PHPVarDeclaration VariableDeclarator() : { String varName; String varValue = null; - int pos = jj_input_stream.bufpos; + final int pos = jj_input_stream.bufpos; } { varName = VariableDeclaratorId() @@ -653,7 +680,7 @@ PHPVarDeclaration VariableDeclarator() : String VariableDeclaratorId() : { String expr; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { try { @@ -742,7 +769,7 @@ String VariableInitializer() : String ArrayVariable() : { String expr; -StringBuffer buff = new StringBuffer(); +final StringBuffer buff = new StringBuffer(); } { expr = Expression() @@ -755,7 +782,7 @@ StringBuffer buff = new StringBuffer(); String ArrayInitializer() : { String expr = null; -StringBuffer buff = new StringBuffer("("); +final StringBuffer buff = new StringBuffer("("); } { [ expr = ArrayVariable() @@ -777,12 +804,16 @@ void MethodDeclaration() : { functionDeclaration = MethodDeclarator() { - currentSegment.add(functionDeclaration); - currentSegment = functionDeclaration; + if (currentSegment != null) { + currentSegment.add(functionDeclaration); + currentSegment = functionDeclaration; + } } Block() { - currentSegment = (PHPSegmentWithChildren) currentSegment.getParent(); + if (currentSegment != null) { + currentSegment = (PHPSegmentWithChildren) currentSegment.getParent(); + } } } @@ -791,7 +822,7 @@ PHPFunctionDeclaration MethodDeclarator() : Token identifier; StringBuffer methodDeclaration = new StringBuffer(); String formalParameters; - int pos = jj_input_stream.bufpos; + final int pos = jj_input_stream.bufpos; } { [ {methodDeclaration.append("&");} ] @@ -840,7 +871,7 @@ String FormalParameters() : String FormalParameter() : { PHPVarDeclaration variableDeclaration; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { [ {buff.append("&");}] variableDeclaration = VariableDeclarator() @@ -964,7 +995,7 @@ String ConditionalOrExpression() : String expr; Token operator; String expr2 = null; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { expr = ConditionalAndExpression() @@ -988,7 +1019,7 @@ String ConditionalAndExpression() : String expr; Token operator; String expr2 = null; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { expr = ConcatExpression() @@ -1011,7 +1042,7 @@ String ConcatExpression() : { String expr; String expr2 = null; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { expr = InclusiveOrExpression() @@ -1034,7 +1065,7 @@ String InclusiveOrExpression() : { String expr; String expr2 = null; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { expr = ExclusiveOrExpression() @@ -1057,7 +1088,7 @@ String ExclusiveOrExpression() : { String expr; String expr2 = null; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { expr = AndExpression() @@ -1078,9 +1109,9 @@ String ExclusiveOrExpression() : String AndExpression() : { - String expr; + final String expr; String expr2 = null; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { expr = EqualityExpression() @@ -1104,13 +1135,14 @@ String EqualityExpression() : String expr; Token operator; String expr2; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { expr = RelationalExpression() {buff.append(expr);} ( ( operator = + | operator = | operator = | operator = | operator = @@ -1129,7 +1161,7 @@ String RelationalExpression() : String expr; Token operator; String expr2; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { expr = ShiftExpression() @@ -1148,17 +1180,16 @@ String ShiftExpression() : { String expr; Token operator; - String expr2; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { expr = AdditiveExpression() {buff.append(expr);} ( - (operator = | operator = | operator = ) expr2 = AdditiveExpression() + (operator = | operator = | operator = ) expr = AdditiveExpression() { buff.append(operator.image); - buff.append(expr2); + buff.append(expr); } )* {return buff.toString();} @@ -1168,17 +1199,16 @@ String AdditiveExpression() : { String expr; Token operator; - String expr2; - StringBuffer buff = new StringBuffer(); + final StringBuffer buff = new StringBuffer(); } { expr = MultiplicativeExpression() {buff.append(expr);} ( - ( operator = | operator = ) expr2 = MultiplicativeExpression() + ( operator = | operator = ) expr = MultiplicativeExpression() { buff.append(operator.image); - buff.append(expr2); + buff.append(expr); } )* {return buff.toString();} @@ -1186,17 +1216,17 @@ String AdditiveExpression() : String MultiplicativeExpression() : { - String expr, expr2; + String expr; Token operator; final StringBuffer buff = new StringBuffer();} { expr = UnaryExpression() {buff.append(expr);} ( - ( operator = | operator = | operator = ) expr2 = UnaryExpression() + ( operator = | operator = | operator = ) expr = UnaryExpression() { buff.append(operator.image); - buff.append(expr2); + buff.append(expr); } )* {return buff.toString();} @@ -1288,8 +1318,7 @@ String UnaryExpressionNotPlusMinus() : String CastExpression() : { -String type; -String expr; +final String type, expr; } { type = Type() expr = UnaryExpression() @@ -1337,7 +1366,7 @@ String PrimaryExpression() : String ArrayDeclarator() : { - String expr; + final String expr; } { expr = ArrayInitializer() @@ -1474,7 +1503,7 @@ String expr = null; String ArgumentList() : { String expr; -StringBuffer buff = new StringBuffer(); +final StringBuffer buff = new StringBuffer(); } { expr = Expression() @@ -1557,12 +1586,16 @@ void Statement() : void IncludeStatement() : { String expr; - int pos = jj_input_stream.bufpos; + final int pos = jj_input_stream.bufpos; } { expr = Expression() - {currentSegment.add(new PHPReqIncDeclaration(currentSegment, "require",pos,expr));} + { + if (currentSegment != null) { + currentSegment.add(new PHPReqIncDeclaration(currentSegment, "require",pos,expr)); + } + } try { ( | "?>") } catch (ParseException e) { @@ -1573,7 +1606,11 @@ void IncludeStatement() : | expr = Expression() - {currentSegment.add(new PHPReqIncDeclaration(currentSegment, "require_once",pos,expr));} + { + if (currentSegment != null) { + currentSegment.add(new PHPReqIncDeclaration(currentSegment, "require_once",pos,expr)); + } + } try { ( | "?>") } catch (ParseException e) { @@ -1584,7 +1621,11 @@ void IncludeStatement() : | expr = Expression() - {currentSegment.add(new PHPReqIncDeclaration(currentSegment, "include",pos,expr));} + { + if (currentSegment != null) { + currentSegment.add(new PHPReqIncDeclaration(currentSegment, "include",pos,expr)); + } + } try { ( | "?>") } catch (ParseException e) { @@ -1595,7 +1636,11 @@ void IncludeStatement() : | expr = Expression() - {currentSegment.add(new PHPReqIncDeclaration(currentSegment, "include_once",pos,expr));} + { + if (currentSegment != null) { + currentSegment.add(new PHPReqIncDeclaration(currentSegment, "include_once",pos,expr)); + } + } try { ( | "?>") } catch (ParseException e) { @@ -1607,7 +1652,7 @@ void IncludeStatement() : String PrintExpression() : { - StringBuffer buff = new StringBuffer("print "); + final StringBuffer buff = new StringBuffer("print "); String expr; } { @@ -1738,9 +1783,12 @@ void IfStatement() : * else's to the innermost if statement. The LOOKAHEAD specification * is to tell JavaCC that we know what we are doing. */ -{} { - Condition("if") Statement() ( LOOKAHEAD(1) ElseIfStatement() )* [ LOOKAHEAD(1) Statement() ] + Token token; + final int pos = jj_input_stream.bufpos; +} +{ + token = Condition("if") IfStatement0(pos,pos+token.image.length()) } void Condition(String keyword) : @@ -1763,6 +1811,52 @@ void Condition(String keyword) : } } +void IfStatement0(int start,int end) : +{ +} +{ + (Statement())* (ElseIfStatementColon())* [ElseStatementColon()] + + {try { + setMarker(fileToParse, + "Ugly syntax detected, you should if () {...} instead of if (): ... endif;", + start, + end, + INFO, + "Line " + token.beginLine); + } catch (CoreException e) { + PHPeclipsePlugin.log(e); + }} + try { + + } catch (ParseException e) { + errorMessage = "'endif' expected"; + errorLevel = ERROR; + throw e; + } + try { + + } catch (ParseException e) { + errorMessage = "';' expected 'endif' keyword"; + errorLevel = ERROR; + throw e; + } +| + Statement() ( LOOKAHEAD(1) ElseIfStatement() )* [ LOOKAHEAD(1) Statement() ] +} + +void ElseIfStatementColon() : +{} +{ + Condition("elseif") (Statement())* +} + +void ElseStatementColon() : +{} +{ + (Statement())* +} + void ElseIfStatement() : {} { @@ -1770,19 +1864,39 @@ void ElseIfStatement() : } void WhileStatement() : -{} { - Condition("while") WhileStatement0() + Token token; + final int pos = jj_input_stream.bufpos; +} +{ + token = Condition("while") WhileStatement0(pos,pos + token.image.length()) } -void WhileStatement0() : +void WhileStatement0(final int start, final int end) : {} { - (Statement())* + (Statement())* + {try { + setMarker(fileToParse, + "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;", + start, + end, + INFO, + "Line " + token.beginLine); + } catch (CoreException e) { + PHPeclipsePlugin.log(e); + }} + try { + + } catch (ParseException e) { + errorMessage = "'endwhile' expected"; + errorLevel = ERROR; + throw e; + } try { ( | "?>") } catch (ParseException e) { - errorMessage = "';' expected"; + errorMessage = "';' expected after 'endwhile' keyword"; errorLevel = ERROR; throw e; } @@ -1806,13 +1920,99 @@ void DoStatement() : void ForeachStatement() : {} { - Variable() Variable() [ Expression() ] Statement() + + try { + + } catch (ParseException e) { + errorMessage = "'(' expected after 'foreach' keyword"; + errorLevel = ERROR; + throw e; + } + try { + Variable() + } catch (ParseException e) { + errorMessage = "variable expected"; + errorLevel = ERROR; + throw e; + } + try { + + } catch (ParseException e) { + errorMessage = "'as' expected"; + errorLevel = ERROR; + throw e; + } + try { + Variable() + } catch (ParseException e) { + errorMessage = "variable expected"; + errorLevel = ERROR; + throw e; + } + [ Expression() ] + try { + + } catch (ParseException e) { + errorMessage = "')' expected after 'foreach' keyword"; + errorLevel = ERROR; + throw e; + } + try { + Statement() + } catch (ParseException e) { + if (errorMessage != null) throw e; + errorMessage = "statement expected"; + errorLevel = ERROR; + throw e; + } } void ForStatement() : -{} { - [ ForInit() ] [ Expression() ] [ ForUpdate() ] Statement() +Token token; +final int pos = jj_input_stream.bufpos; +} +{ + token = + try { + + } catch (ParseException e) { + errorMessage = "'(' expected after 'for' keyword"; + errorLevel = ERROR; + throw e; + } + [ ForInit() ] [ Expression() ] [ ForUpdate() ] + ( + Statement() + | + (Statement())* + { + try { + setMarker(fileToParse, + "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;", + pos, + pos+token.image.length(), + INFO, + "Line " + token.beginLine); + } catch (CoreException e) { + PHPeclipsePlugin.log(e); + } + } + try { + + } catch (ParseException e) { + errorMessage = "'endfor' expected"; + errorLevel = ERROR; + throw e; + } + try { + + } catch (ParseException e) { + errorMessage = "';' expected 'endfor' keyword"; + errorLevel = ERROR; + throw e; + } + ) } void ForInit() :