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 e8ab5f2..16847d8 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj @@ -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 */ @@ -572,16 +589,80 @@ MORE : | <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | - | | )> -| + | )> +//| | | } -/* IDENTIFIERS */ + SKIP : +{ + : IN_STRING +} TOKEN : { + : IN_STRING +} + + + TOKEN : +{ + : DOLLAR_IN_STRING +} + + TOKEN : +{ + : PHPPARSING +} + + TOKEN : +{ + : DOLLAR_IN_STRING_EXPR +} + + SPECIAL_TOKEN : +{ + <"{"> : SKIPSTRING +} + + SPECIAL_TOKEN : +{ + <"}"> : IN_STRING +} + + SKIP : +{ + <~[]> +} + + TOKEN : +{ + : DOLLAR_IN_STRING +} + + TOKEN : +{ + +} + + SKIP : +{ + <~[]> +} + + SKIP : +{ + <~[]> +} +/* IDENTIFIERS */ + + + TOKEN : { : IN_VARIABLE} + + + TOKEN : +{ |) (||)* > | < #LETTER: @@ -597,57 +678,56 @@ MORE : > } + SPECIAL_TOKEN : +{ + < ~[] > : IN_STRING +} /* 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 +761,7 @@ void phpFile() : void PhpBlock() : { final PHPEchoBlock phpEchoBlock; - final Token token; + final Token token,phpEnd; } { phpEchoBlock = phpEchoBlock() @@ -700,9 +780,11 @@ void PhpBlock() : PHPeclipsePlugin.log(e); }} ] + {PHPParser.createNewHTMLCode();} Php() try { - + phpEnd = + {htmlStart = phpEnd.sourceEnd;} } catch (ParseException e) { errorMessage = "'?>' expected"; errorLevel = ERROR; @@ -719,8 +801,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 +824,7 @@ ClassDeclaration ClassDeclaration() : final Token superclassName, token, extendsToken; String classNameImage = SYNTAX_ERROR_CHAR; String superclassNameImage = null; + final int classEnd; } { token = @@ -791,15 +877,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 +900,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 +984,20 @@ FieldDeclaration FieldDeclaration() : */ VariableDeclaration VariableDeclaratorNoSuffix() : { - final Token varName; - Expression initializer = null; + final Token token, lbrace,rbrace; + Expression expr, initializer = null; Token assignToken; + Variable variable; } { - varName = + + ( + token = + {variable = new Variable(token.image,token.sourceStart,token.sourceEnd);} + | + lbrace = expr = Expression() rbrace = + {variable = new Variable(expr,lbrace.sourceStart,rbrace.sourceEnd);} + ) [ assignToken = try { @@ -915,19 +1013,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,150 +1094,38 @@ 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) + token = variable = Var() { - return new Variable(variable,token.sourceStart,variable.sourceEnd); + return variable; } } -Variable Var(final Token dollar) : +Variable Var() : { 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);} + token = variable = Var() + {return new Variable(variable,variable.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, + token.sourceStart, + token2.sourceEnd); } | - token = - { - return new Variable(token.image, - token.sourceStart+1, - token.sourceEnd+1); - } */ + token = + {return new Variable(token.image,token.sourceStart,token.sourceEnd);} } Expression VariableInitializer() : @@ -1685,7 +1667,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 +1720,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 +1913,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 ConstantIdentifier(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,11 +1958,27 @@ 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() : { final Token token; + StringLiteral literal; } { token = {return new NumberLiteral(token);} @@ -1955,23 +1987,65 @@ Literal Literal() : | token = {return new TrueLiteral(token);} | token = {return new FalseLiteral(token);} | token = {return new NullLiteral(token);} +| literal = evaluableString() {return literal;} +} + +StringLiteral evaluableString() : +{ + ArrayList list = new ArrayList(); + Token start,end; + Token token,lbrace,rbrace; + AbstractVariable var; + Expression expr; +} +{ + start = + ( + + ( + token = {list.add(new Variable(token.image, + token.sourceStart, + token.sourceEnd));} + | + lbrace = + token = + {list.add(new Variable(token.image, + token.sourceStart, + token.sourceEnd));} + rbrace = + ) + )* + end = + { + AbstractVariable[] vars = new AbstractVariable[list.size()]; + list.toArray(vars); + return new StringLiteral(SimpleCharStream.currentBuffer.substring(start.sourceEnd,end.sourceStart), + start.sourceStart, + end.sourceEnd, + vars); + } } FunctionCall Arguments(final Expression func) : { Expression[] args = null; -final Token token; +final Token token,lparen; } { - [ args = ArgumentList() ] + lparen = [ args = ArgumentList() ] try { token = {return new FunctionCall(func,args,token.sourceEnd);} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list"; errorLevel = ERROR; - errorStart = args[args.length-1].sourceEnd+1; - errorEnd = args[args.length-1].sourceEnd+1; + if (args == null) { + errorStart = lparen.sourceEnd+1; + errorEnd = lparen.sourceEnd+2; + } else { + errorStart = args[args.length-1].sourceEnd+1; + errorEnd = args[args.length-1].sourceEnd+2; + } processParseExceptionDebug(e); } {return new FunctionCall(func,args,args[args.length-1].sourceEnd);} @@ -2162,11 +2236,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 +2531,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 +2741,7 @@ AbstractCase[] switchStatementBrace() : return abcase; } } + /** * A Switch statement with : ... endswitch; * @param start the begin offset of the switch @@ -2712,8 +2802,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]; @@ -2765,7 +2856,6 @@ Expression SwitchLabel() : token = <_DEFAULT> try { - {return null;} } catch (ParseException e) { errorMessage = "':' expected after 'default' keyword"; errorLevel = ERROR; @@ -2773,6 +2863,7 @@ Expression SwitchLabel() : errorEnd = token.sourceEnd+1; processParseExceptionDebug(e); } + {return null;} } Break BreakStatement() : @@ -2854,7 +2945,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 +3041,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 +3059,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];