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 e8ab5f2..9ad3eff 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj @@ -87,7 +87,7 @@ public final class PHPParser extends PHPParserSuperclass { /** The cursor in expression stack. */ private static int nodePtr; - public static final boolean PARSER_DEBUG = false; + public static final boolean PARSER_DEBUG = true; public final void setFileToParse(final IFile fileToParse) { PHPParser.fileToParse = fileToParse; @@ -331,19 +331,20 @@ public final class PHPParser extends PHPParserSuperclass { public static final void createNewHTMLCode() { final int currentPosition = token.sourceStart; if (currentPosition == htmlStart || + currentPosition < htmlStart || currentPosition > SimpleCharStream.currentBuffer.length()) { return; } - final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray(); + final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart, + currentPosition).toCharArray(); pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition)); } /** Create a new task. */ - public static final void createNewTask() { - final int currentPosition = token.sourceStart; - final String todo = SimpleCharStream.currentBuffer.substring(currentPosition-3, + public static final void createNewTask(final int todoStart) { + final String todo = SimpleCharStream.currentBuffer.substring(todoStart, SimpleCharStream.currentBuffer.indexOf("\n", - currentPosition)-1); + todoStart)-1); if (!PARSER_DEBUG) { try { setMarker(fileToParse, @@ -378,14 +379,14 @@ TOKEN_MGR_DECLS: TOKEN : { - {PHPParser.createNewHTMLCode();} : PHPPARSING -| {PHPParser.createNewHTMLCode();} : PHPPARSING -| : PHPPARSING +| : PHPPARSING +| "> {PHPParser.htmlStart = PHPParser.token.sourceEnd;} : DEFAULT + "> : DEFAULT } /* Skip any character if we are not in php mode */ @@ -405,6 +406,14 @@ TOKEN_MGR_DECLS: | "\f" } + SPECIAL_TOKEN : +{ + " " : PHPPARSING +| "\t" : PHPPARSING +| "\n" : PHPPARSING +| "\r" : PHPPARSING +| "\f" : PHPPARSING +} /* COMMENTS */ SPECIAL_TOKEN : { @@ -422,9 +431,14 @@ TOKEN_MGR_DECLS: SPECIAL_TOKEN : { - "todo" {PHPParser.createNewTask();} + "todo" } +void todo() : +{Token todoToken;} +{ + todoToken = "TODO" {createNewTask(todoToken.sourceStart);} +} SPECIAL_TOKEN : { "*/" : PHPPARSING @@ -467,9 +481,13 @@ MORE : | | | -| "> -| -| "> +} + + TOKEN : +{ + "> : PHPPARSING +| : PHPPARSING +| "> : PHPPARSING } /* RESERVED WORDS AND LITERALS */ @@ -516,36 +534,35 @@ MORE : } //Misc token - TOKEN : + TOKEN : { - -| -| -| -| -| + : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING } /* OPERATORS */ - TOKEN : -{ - -| -| -| -| -| -| -| -| -| -| -| -| -| >"> -| >>"> -| <_ORL : "OR"> -| <_ANDL : "AND"> + TOKEN : +{ + : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| >"> : PHPPARSING +| >>"> : PHPPARSING +| <_ORL : "OR"> : PHPPARSING +| <_ANDL : "AND"> : PHPPARSING } /* LITERALS */ @@ -580,7 +597,11 @@ MORE : /* IDENTIFIERS */ - TOKEN : + + TOKEN : { : IN_VARIABLE} + + + TOKEN : { |) (||)* > | @@ -599,55 +620,50 @@ MORE : /* SEPARATORS */ - TOKEN : + TOKEN : { - -| -| -| -| -| -| -| -| + : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| : PHPPARSING } /* COMPARATOR */ - TOKEN : + TOKEN : { - "> -| -| -| ="> -| "> -| -| + "> : PHPPARSING +| : PHPPARSING +| : PHPPARSING +| ="> : PHPPARSING +| "> : PHPPARSING +| : PHPPARSING +| : PHPPARSING } /* ASSIGNATION */ - TOKEN : -{ - -| >="> -} - - TOKEN : -{ - > + TOKEN : +{ + : PHPPARSING +| >="> : PHPPARSING } void phpTest() : @@ -681,7 +697,7 @@ void phpFile() : void PhpBlock() : { final PHPEchoBlock phpEchoBlock; - final Token token; + final Token token,phpEnd; } { phpEchoBlock = phpEchoBlock() @@ -700,9 +716,11 @@ void PhpBlock() : PHPeclipsePlugin.log(e); }} ] + {PHPParser.createNewHTMLCode();} Php() try { - + phpEnd = + {htmlStart = phpEnd.sourceEnd;} } catch (ParseException e) { errorMessage = "'?>' expected"; errorLevel = ERROR; @@ -719,8 +737,11 @@ PHPEchoBlock phpEchoBlock() : final Token token, token2; } { - token = expr = Expression() [ ] token2 = + token = {PHPParser.createNewHTMLCode();} + expr = Expression() [ ] token2 = { + htmlStart = token2.sourceEnd; + echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd); pushOnAstNodes(echoBlock); return echoBlock;} @@ -739,6 +760,7 @@ ClassDeclaration ClassDeclaration() : final Token superclassName, token, extendsToken; String classNameImage = SYNTAX_ERROR_CHAR; String superclassNameImage = null; + final int classEnd; } { token = @@ -791,15 +813,17 @@ ClassDeclaration ClassDeclaration() : currentSegment.add(classDeclaration); currentSegment = classDeclaration; } - ClassBody(classDeclaration) + classEnd = ClassBody(classDeclaration) {currentSegment = (OutlineableWithChildren) currentSegment.getParent(); - classDeclaration.sourceEnd = SimpleCharStream.getPosition(); + classDeclaration.sourceEnd = classEnd; pushOnAstNodes(classDeclaration); return classDeclaration;} } -void ClassBody(final ClassDeclaration classDeclaration) : -{} +int ClassBody(final ClassDeclaration classDeclaration) : +{ +Token token; +} { try { @@ -812,13 +836,15 @@ void ClassBody(final ClassDeclaration classDeclaration) : } ( ClassBodyDeclaration(classDeclaration) )* try { - + token = + {return token.sourceEnd;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected"; errorLevel = ERROR; errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = SimpleCharStream.getPosition() + 1; processParseExceptionDebug(e); + return PHPParser.token.sourceEnd; } } @@ -894,12 +920,20 @@ FieldDeclaration FieldDeclaration() : */ VariableDeclaration VariableDeclaratorNoSuffix() : { - final Token varName; - Expression initializer = null; + final Token dollar, token, lbrace,rbrace; + Expression expr, initializer = null; Token assignToken; + Variable variable; } { - varName = + dollar = + ( + token = + {variable = new Variable(token.image,dollar.sourceStart,token.sourceEnd);} + | + lbrace = expr = Expression() rbrace = + {variable = new Variable(expr,dollar.sourceStart,rbrace.sourceEnd);} + ) [ assignToken = try { @@ -915,19 +949,15 @@ VariableDeclaration VariableDeclaratorNoSuffix() : { if (initializer == null) { return new VariableDeclaration(currentSegment, - new Variable(varName.image.substring(1), - varName.sourceStart+1, - varName.sourceEnd+1), - varName.sourceStart+1, - varName.sourceEnd+1); + variable, + variable.sourceStart, + variable.sourceEnd); } return new VariableDeclaration(currentSegment, - new Variable(varName.image.substring(1), - varName.sourceStart+1, - varName.sourceEnd+1), + variable, initializer, VariableDeclaration.EQUAL, - varName.sourceStart+1); + variable.sourceStart); } } @@ -1000,59 +1030,12 @@ AbstractVariable VariableDeclaratorId() : } } -/** - * Return a variablename without the $. - * @return a variable name - *//* -Variable Variable(): -{ - final StringBuffer buff; - Expression expression = null; - final Token token; - Variable expr; - final int pos; -} -{ - token = - [ expression = Expression() ] - { - if (expression == null) { - return new Variable(token.image.substring(1), - token.sourceStart+1, - token.sourceEnd+1); - } - String s = expression.toStringExpression(); - buff = new StringBuffer(token.image.length()+s.length()+2); - buff.append(token.image); - buff.append("{"); - buff.append(s); - buff.append("}"); - s = buff.toString(); - return new Variable(s,token.sourceStart+1,token.sourceEnd+1); - } -| - token = - expr = VariableName() - {return new Variable(expr,token.sourceStart,expr.sourceEnd);} -} */ - Variable Variable() : { Variable variable = null; final Token token; } { - token = [variable = Var(token)] - { - if (variable == null) { - return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1); - } - final StringBuffer buff = new StringBuffer(); - buff.append(token.image.substring(1)); - buff.append(variable.toStringExpression()); - return new Variable(buff.toString(),token.sourceStart+1,variable.sourceEnd+1); - } -| token = variable = Var(token) { return new Variable(variable,token.sourceStart,variable.sourceEnd); @@ -1062,88 +1045,23 @@ Variable Variable() : Variable Var(final Token dollar) : { Variable variable = null; - final Token token; + final Token token,token2; ConstantIdentifier constant; + Expression expression; } { - token = [variable = Var(token)] - {if (variable == null) { - return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1); - } - final StringBuffer buff = new StringBuffer(); - buff.append(token.image.substring(1)); - buff.append(variable.toStringExpression()); - return new Variable(buff.toString(),dollar.sourceStart,variable.sourceEnd); - } -| - LOOKAHEAD( ) token = variable = Var(token) {return new Variable(variable,dollar.sourceStart,variable.sourceEnd);} | - constant = VariableName() - {return new Variable(constant.name,dollar.sourceStart,constant.sourceEnd);} -} - -/** - * A Variable name (without the $) - * @return a variable name String - */ -ConstantIdentifier VariableName(): -{ - final StringBuffer buff; - String expr; - Expression expression = null; - final Token token; - Token token2 = null; -} -{ token = expression = Expression() token2 = - {expr = expression.toStringExpression(); - buff = new StringBuffer(expr.length()+2); - buff.append("{"); - buff.append(expr); - buff.append("}"); - expr = buff.toString(); - return new ConstantIdentifier(expr, - token.sourceStart, - token2.sourceEnd); - - } -| - token = - [ expression = Expression() token2 = ] { - if (expression == null) { - return new ConstantIdentifier(token.image, - token.sourceStart, - token.sourceEnd); - } - expr = expression.toStringExpression(); - buff = new StringBuffer(token.image.length()+expr.length()+2); - buff.append(token.image); - buff.append("{"); - buff.append(expr); - buff.append("}"); - expr = buff.toString(); - return new ConstantIdentifier(expr, - token.sourceStart, - token2.sourceEnd); - } -/*| - - var = VariableName() - { - return new Variable(var, - var.sourceStart-1, - var.sourceEnd); + return new Variable(expression, + dollar.sourceStart, + token2.sourceEnd); } | - token = - { - return new Variable(token.image, - token.sourceStart+1, - token.sourceEnd+1); - } */ + token = + {return new Variable(token.image,dollar.sourceStart,token.sourceEnd);} } Expression VariableInitializer() : @@ -1685,7 +1603,29 @@ Expression UnaryExpression() : /* expr = UnaryExpressionNoPrefix() //why did I had that ? {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);} | */ - expr = AtNotUnaryExpression() {return expr;} + expr = AtNotTildeUnaryExpression() {return expr;} +} + +Expression AtNotTildeUnaryExpression() : +{ + final Expression expr; + final Token token; +} +{ + token = + expr = AtNotTildeUnaryExpression() + {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);} +| + token = + expr = AtNotTildeUnaryExpression() + {return new PrefixedUnaryExpression(expr,OperatorIds.TWIDDLE,token.sourceStart);} +| + token = + expr = AtNotUnaryExpression() + {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);} +| + expr = UnaryExpressionNoPrefix() + {return expr;} } /** @@ -1716,11 +1656,11 @@ Expression UnaryExpressionNoPrefix() : final Token token; } { - token = expr = AtNotUnaryExpression() {return new PrefixedUnaryExpression(expr, + token = expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr, OperatorIds.PLUS, token.sourceStart);} | - token = expr = AtNotUnaryExpression() {return new PrefixedUnaryExpression(expr, + token = expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr, OperatorIds.MINUS, token.sourceStart);} | @@ -1909,14 +1849,26 @@ Expression ClassIdentifier(): AbstractVariable VariableSuffix(final AbstractVariable prefix) : { Expression expression = null; - final Token classAccessToken; + final Token classAccessToken,lbrace,rbrace; Token token; int pos; } { classAccessToken = try { - ( expression = VariableName() | expression = Variable() ) + ( + lbrace = expression = Expression() rbrace = + { + expression = new Variable(expression, + lbrace.sourceStart, + rbrace.sourceEnd); + } + | + token = + {expression = new Variable(token.image,token.sourceStart,token.sourceEnd);} + | + expression = Variable() + ) } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected"; errorLevel = ERROR; @@ -1942,6 +1894,21 @@ AbstractVariable VariableSuffix(final AbstractVariable prefix) : processParseExceptionDebug(e); } {return new ArrayDeclarator(prefix,expression,pos);} +| + token = {pos = token.sourceEnd+1;} + [ expression = Expression() {pos = expression.sourceEnd+1;} + | expression = Type() {pos = expression.sourceEnd+1;}] //Not good + try { + token = + {pos = token.sourceEnd;} + } catch (ParseException e) { + errorMessage = "']' expected"; + errorLevel = ERROR; + errorStart = pos; + errorEnd = pos; + processParseExceptionDebug(e); + } + {return new ArrayDeclarator(prefix,expression,pos);}//todo : check braces here } Literal Literal() : @@ -2162,11 +2129,15 @@ HTMLBlock htmlBlock() : final int startIndex = nodePtr; final AstNode[] blockNodes; final int nbNodes; + final Token phpEnd; } { - (phpEchoBlock())* + phpEnd = + {htmlStart = phpEnd.sourceEnd;} + (phpEchoBlock())* try { ( | ) + {PHPParser.createNewHTMLCode();} } catch (ParseException e) { errorMessage = "unexpected end of file , ' {keyword = InclusionStatement.INCLUDE_ONCE;pos=token.sourceEnd;}) try { expr = Expression() - {pos=expr.sourceEnd;} + {pos = expr.sourceEnd;} } catch (ParseException e) { if (errorMessage != null) { throw e; } errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected"; errorLevel = ERROR; - errorStart = pos+1; - errorEnd = pos+1; + errorStart = e.currentToken.next.sourceStart; + errorEnd = e.currentToken.next.sourceEnd; expr = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos); processParseExceptionDebug(e); } - {inclusionStatement = new InclusionStatement(currentSegment, - keyword, - expr, - token.sourceStart); - currentSegment.add(inclusionStatement); - } try { token2 = + {pos=token2.sourceEnd;} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + errorStart = e.currentToken.next.sourceStart; + errorEnd = e.currentToken.next.sourceEnd; + processParseExceptionDebug(e); + } + { + inclusionStatement = new InclusionStatement(currentSegment, + keyword, + expr, + token.sourceStart, + pos); + currentSegment.add(inclusionStatement); + return inclusionStatement; } - {inclusionStatement.sourceEnd = token2.sourceEnd; - return inclusionStatement;} } PrintExpression PrintExpression() : @@ -2448,7 +2424,13 @@ Block Block() : processParseExceptionDebug(e); } ( statement = BlockStatement() {list.add(statement);pos = statement.sourceEnd+1;} - | statement = htmlBlock() {list.add(statement);pos = statement.sourceEnd+1;})* + | statement = htmlBlock() {if (statement != null) { + list.add(statement); + pos = statement.sourceEnd+1; + } + pos = PHPParser.token.sourceEnd+1; + } + )* try { token2 = {pos = token2.sourceEnd+1;} @@ -2652,6 +2634,7 @@ AbstractCase[] switchStatementBrace() : return abcase; } } + /** * A Switch statement with : ... endswitch; * @param start the begin offset of the switch @@ -2712,8 +2695,9 @@ AbstractCase switchLabel0() : { expr = SwitchLabel() ( statement = BlockStatementNoBreak() {stmts.add(statement);} - | statement = htmlBlock() {stmts.add(statement);})* - [ statement = BreakStatement() {stmts.add(statement);}] + | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}} + | statement = BreakStatement() {stmts.add(statement);})* + //[ statement = BreakStatement() {stmts.add(statement);}] { final int listSize = stmts.size(); final Statement[] stmtsArray = new Statement[listSize]; @@ -2854,7 +2838,7 @@ IfStatement IfStatement0(final Expression condition, final int start,final int e {stmts = new ArrayList();} ( statement = Statement() {stmts.add(statement);} - | statement = htmlBlock() {stmts.add(statement);})* + | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}})* {endStatements = SimpleCharStream.getPosition();} (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})* [elseStatement = ElseStatementColon()] @@ -2950,7 +2934,7 @@ ElseIf ElseIfStatementColon() : { elseifToken = condition = Condition("elseif") ( statement = Statement() {list.add(statement);} - | statement = htmlBlock() {list.add(statement);})* + | statement = htmlBlock() {if (statement != null) {list.add(statement);}})* { final int sizeList = list.size(); final Statement[] stmtsArray = new Statement[sizeList]; @@ -2968,7 +2952,7 @@ Else ElseStatementColon() : } { elseToken = ( statement = Statement() {list.add(statement);} - | statement = htmlBlock() {list.add(statement);})* + | statement = htmlBlock() {if (statement != null) {list.add(statement);}})* { final int sizeList = list.size(); final Statement[] stmtsArray = new Statement[sizeList];