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 ac77fff..00cc6c9 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj @@ -29,7 +29,6 @@ import org.eclipse.ui.texteditor.MarkerUtilities; import org.eclipse.jface.preference.IPreferenceStore; import java.util.Hashtable; -import java.util.Enumeration; import java.util.ArrayList; import java.io.StringReader; import java.io.*; @@ -60,8 +59,6 @@ public final class PHPParser extends PHPParserSuperclass { private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$ static PHPOutlineInfo outlineInfo; - private static boolean assigning; - /** 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 */ @@ -70,6 +67,8 @@ public final class PHPParser extends PHPParserSuperclass { private static int errorStart = -1; private static int errorEnd = -1; private static PHPDocument phpDocument; + + private static final char[] SYNTAX_ERROR_CHAR = {'s','y','n','t','a','x',' ','e','r','r','o','r'}; /** * The point where html starts. * It will be used by the token manager to create HTMLCode objects @@ -188,31 +187,6 @@ public final class PHPParser extends PHPParserSuperclass { } } - /** - * Create markers according to the external parser output - */ - 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; - boolean flag = true; - while ((brIndx = output.indexOf("
", indx)) != -1) { - // newer php error output (tested with 4.2.3) - scanLine(output, file, indx, brIndx); - indx = brIndx + 6; - flag = false; - } - if (flag) { - while ((brIndx = output.indexOf("
", indx)) != -1) { - // older php error output (tested with 4.2.3) - scanLine(output, file, indx, brIndx); - indx = brIndx + 4; - } - } - } - private static void scanLine(final String output, final IFile file, final int indx, @@ -302,6 +276,25 @@ public final class PHPParser extends PHPParserSuperclass { pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition)); } + /** + * Create a new task. + */ + public static final void createNewTask() { + final int currentPosition = SimpleCharStream.getPosition(); + final String todo = SimpleCharStream.currentBuffer.substring(currentPosition+1, + SimpleCharStream.currentBuffer.indexOf("\n", + currentPosition)-1); + try { + setMarker(fileToParse, + "todo : " + todo, + SimpleCharStream.getBeginLine(), + TASK, + "Line "+SimpleCharStream.getBeginLine()); + } catch (CoreException e) { + PHPeclipsePlugin.log(e); + } + } + private static final void parse() throws ParseException { phpFile(); } @@ -342,34 +335,30 @@ PARSER_END(PHPParser) SPECIAL_TOKEN : { "//" : IN_SINGLE_LINE_COMMENT -| - "#" : IN_SINGLE_LINE_COMMENT -| - <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT -| - "/*" : IN_MULTI_LINE_COMMENT +| "#" : IN_SINGLE_LINE_COMMENT +| <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT +| "/*" : IN_MULTI_LINE_COMMENT } SPECIAL_TOKEN : { : PHPPARSING +| "?>" : DEFAULT } - SPECIAL_TOKEN : + SPECIAL_TOKEN : { - " > : DEFAULT + "todo" {PHPParser.createNewTask();} } - -SPECIAL_TOKEN : + SPECIAL_TOKEN : { - : PHPPARSING + "*/" : PHPPARSING } - -SPECIAL_TOKEN : + SPECIAL_TOKEN : { - : PHPPARSING + "*/" : PHPPARSING } @@ -467,8 +456,8 @@ MORE : { | -| -| +| +| | | | @@ -606,7 +595,7 @@ MORE : TOKEN : { - < DOLLAR_ID: > + > } void phpFile() : @@ -614,7 +603,7 @@ void phpFile() : { try { (PhpBlock())* - + {PHPParser.createNewHTMLCode();} } catch (TokenMgrError e) { PHPeclipsePlugin.log(e); errorStart = SimpleCharStream.getPosition(); @@ -633,9 +622,11 @@ void phpFile() : void PhpBlock() : { final int start = SimpleCharStream.getPosition(); + final PHPEchoBlock phpEchoBlock; } { - phpEchoBlock() + phpEchoBlock = phpEchoBlock() + {pushOnAstNodes(phpEchoBlock);} | [ | @@ -658,7 +649,7 @@ void PhpBlock() : errorLevel = ERROR; errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + processParseException(e); } } @@ -688,41 +679,46 @@ ClassDeclaration ClassDeclaration() : final Token className; Token superclassName = null; final int pos; + char[] classNameImage = SYNTAX_ERROR_CHAR; + char[] superclassNameImage = null; } { + {pos = SimpleCharStream.getPosition();} try { - {pos = SimpleCharStream.getPosition();} className = + {classNameImage = className.image.toCharArray();} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected"; errorLevel = ERROR; errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + processParseException(e); } [ try { superclassName = + {superclassNameImage = superclassName.image.toCharArray();} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected"; errorLevel = ERROR; errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + processParseException(e); + superclassNameImage = SYNTAX_ERROR_CHAR; } ] { - if (superclassName == null) { + if (superclassNameImage == null) { classDeclaration = new ClassDeclaration(currentSegment, - className.image.toCharArray(), + classNameImage, pos, 0); } else { classDeclaration = new ClassDeclaration(currentSegment, - className.image.toCharArray(), - superclassName.image.toCharArray(), + classNameImage, + superclassNameImage, pos, 0); } @@ -769,8 +765,8 @@ void ClassBodyDeclaration(ClassDeclaration classDeclaration) : FieldDeclaration field; } { - method = MethodDeclaration() {method.setParent(classDeclaration);} -| field = FieldDeclaration() + method = MethodDeclaration() {classDeclaration.addMethod(method);} +| field = FieldDeclaration() {classDeclaration.addField(field);} } /** @@ -786,12 +782,10 @@ FieldDeclaration FieldDeclaration() : { variableDeclaration = VariableDeclarator() {arrayList.add(variableDeclaration); - outlineInfo.addVariable(new String(variableDeclaration.name)); - currentSegment.add(variableDeclaration);} + outlineInfo.addVariable(new String(variableDeclaration.name));} ( variableDeclaration = VariableDeclarator() {arrayList.add(variableDeclaration); - outlineInfo.addVariable(new String(variableDeclaration.name)); - currentSegment.add(variableDeclaration);} + outlineInfo.addVariable(new String(variableDeclaration.name));} )* try { @@ -852,22 +846,26 @@ VariableDeclaration VariableDeclarator() : String VariableDeclaratorId() : { String expr; - Expression expression; + Expression expression = null; final StringBuffer buff = new StringBuffer(); final int pos = SimpleCharStream.getPosition(); ConstantIdentifier ex; } { try { - expr = Variable() {buff.append(expr);} + expr = Variable() ( LOOKAHEAD(2) {ex = new ConstantIdentifier(expr.toCharArray(), pos, SimpleCharStream.getPosition());} expression = VariableSuffix(ex) - {buff.append(expression.toStringExpression());} )* - {return buff.toString();} + { + if (expression == null) { + return expr; + } + return expression.toStringExpression(); + } } catch (ParseException e) { errorMessage = "'$' expected for variable identifier"; errorLevel = ERROR; @@ -877,6 +875,10 @@ String VariableDeclaratorId() : } } +/** + * Return a variablename without the $. + * @return a variable name + */ String Variable(): { final StringBuffer buff; @@ -887,7 +889,7 @@ String Variable(): { token = [ expression = Expression() ] { - if (expression == null && !assigning) { + if (expression == null) { return token.image.substring(1); } buff = new StringBuffer(token.image); @@ -898,9 +900,13 @@ String Variable(): } | expr = VariableName() - {return "$" + expr;} + {return expr;} } +/** + * A Variable name (without the $) + * @return a variable name String + */ String VariableName(): { final StringBuffer buff; @@ -1008,6 +1014,7 @@ MethodDeclaration MethodDeclaration() : { final MethodDeclaration functionDeclaration; final Block block; + final OutlineableWithChildren seg = currentSegment; } { @@ -1022,20 +1029,11 @@ MethodDeclaration MethodDeclaration() : errorEnd = SimpleCharStream.getPosition() + 1; throw e; } - { - if (currentSegment != null) { - currentSegment.add(functionDeclaration); - currentSegment = functionDeclaration; - } - } + {currentSegment = functionDeclaration;} block = Block() - { - functionDeclaration.statements = block.statements; - if (currentSegment != null) { - currentSegment = (OutlineableWithChildren) currentSegment.getParent(); - } - return functionDeclaration; - } + {functionDeclaration.statements = block.statements; + currentSegment = seg; + return functionDeclaration;} } /** @@ -1049,16 +1047,27 @@ MethodDeclaration MethodDeclarator() : Token reference = null; final Hashtable formalParameters; final int pos = SimpleCharStream.getPosition(); + char[] identifierChar = SYNTAX_ERROR_CHAR; } { - [reference = ] identifier = + [reference = ] + try { + identifier = + {identifierChar = identifier.image.toCharArray();} + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected"; + errorLevel = ERROR; + errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = SimpleCharStream.getPosition() + 1; + processParseException(e); + } formalParameters = FormalParameters() {return new MethodDeclaration(currentSegment, - identifier.image.toCharArray(), - formalParameters, - reference != null, - pos, - SimpleCharStream.getPosition());} + identifierChar, + formalParameters, + reference != null, + pos, + SimpleCharStream.getPosition());} } /** @@ -1078,7 +1087,7 @@ Hashtable FormalParameters() : errorLevel = ERROR; errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + processParseException(e); } [ var = FormalParameter() {parameters.put(new String(var.name),var);} @@ -1094,7 +1103,7 @@ Hashtable FormalParameters() : errorLevel = ERROR; errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + processParseException(e); } {return parameters;} } @@ -1159,7 +1168,7 @@ Expression Expression() : VarAssignation varAssignation() : { String varName; - final Expression expression; + final Expression initializer; final int assignOperator; final int pos = SimpleCharStream.getPosition(); } @@ -1167,7 +1176,7 @@ VarAssignation varAssignation() : varName = VariableDeclaratorId() assignOperator = AssignmentOperator() try { - expression = Expression() + initializer = Expression() } catch (ParseException e) { if (errorMessage != null) { throw e; @@ -1179,7 +1188,7 @@ VarAssignation varAssignation() : throw e; } {return new VarAssignation(varName.toCharArray(), - expression, + initializer, assignOperator, pos, SimpleCharStream.getPosition());} @@ -1471,8 +1480,8 @@ final int operator; final int pos = SimpleCharStream.getPosition(); } { - ( {operator = OperatorIds.PLUS_PLUS;} - | {operator = OperatorIds.MINUS_MINUS;}) + ( {operator = OperatorIds.PLUS_PLUS;} + | {operator = OperatorIds.MINUS_MINUS;}) expr = PrimaryExpression() {return new PrefixedUnaryExpression(expr,operator,pos);} } @@ -1523,8 +1532,8 @@ Expression PostfixExpression() : } { expr = PrimaryExpression() - [ {operator = OperatorIds.PLUS_PLUS;} - | {operator = OperatorIds.MINUS_MINUS;}] + [ {operator = OperatorIds.PLUS_PLUS;} + | {operator = OperatorIds.MINUS_MINUS;}] { if (operator == -1) { return expr; @@ -1558,6 +1567,11 @@ Expression PrimaryExpression() : {return expr;} } +/** + * An array declarator. + * array(vars) + * @return an array + */ ArrayInitializer ArrayDeclarator() : { final ArrayVariableDeclaration[] vars; @@ -1582,9 +1596,10 @@ Expression PrimaryPrefix() : | expr = ClassIdentifier() {return new PrefixedUnaryExpression(expr, OperatorIds.NEW, pos);} -| var = VariableDeclaratorId() {return new ConstantIdentifier(var.toCharArray(), - pos, - SimpleCharStream.getPosition());} +| var = VariableDeclaratorId() {return new VariableDeclaration(currentSegment, + var.toCharArray(), + pos, + SimpleCharStream.getPosition());} } PrefixedUnaryExpression classInstantiation() : @@ -1817,7 +1832,7 @@ HTMLBlock htmlBlock() : try { ( | ) } catch (ParseException e) { - errorMessage = "End of file unexpected, ' {return new PostfixedUnaryExpression(expr, + [ {return new PostfixedUnaryExpression(expr, OperatorIds.PLUS_PLUS, SimpleCharStream.getPosition());} - | {return new PostfixedUnaryExpression(expr, + | {return new PostfixedUnaryExpression(expr, OperatorIds.MINUS_MINUS, SimpleCharStream.getPosition());} | operator = AssignmentOperator() expr2 = Expression() @@ -2417,14 +2435,14 @@ Expression Condition(final String keyword) : condition = Expression() try { - {return condition;} } catch (ParseException e) { errorMessage = "')' expected after " + keyword + " keyword"; errorLevel = ERROR; errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + processParseException(e); } + {return condition;} } IfStatement IfStatement0(Expression condition, final int start,final int end) : @@ -2492,10 +2510,10 @@ IfStatement IfStatement0(Expression condition, final int start,final int end) : stmts.toArray(statementsArray); return new IfStatement(condition, new Block(statementsArray,pos,endStatements), - elseIfs, - elseStatement, - pos, - SimpleCharStream.getPosition()); + elseIfs, + elseStatement, + pos, + SimpleCharStream.getPosition()); } } @@ -2731,9 +2749,9 @@ ForStatement ForStatement() : { final Token token; final int pos = SimpleCharStream.getPosition(); -Statement[] initializations = null; +Expression[] initializations = null; Expression condition = null; -Statement[] increments = null; +Expression[] increments = null; Statement action; final ArrayList list = new ArrayList(); final int startBlock, endBlock; @@ -2797,31 +2815,31 @@ final int startBlock, endBlock; ) } -Statement[] ForInit() : +Expression[] ForInit() : { - Statement[] statements; + Expression[] exprs; } { LOOKAHEAD(LocalVariableDeclaration()) - statements = LocalVariableDeclaration() - {return statements;} + exprs = LocalVariableDeclaration() + {return exprs;} | - statements = StatementExpressionList() - {return statements;} + exprs = StatementExpressionList() + {return exprs;} } -Statement[] StatementExpressionList() : +Expression[] StatementExpressionList() : { final ArrayList list = new ArrayList(); - Statement expr; + Expression expr; } { expr = StatementExpression() {list.add(expr);} ( StatementExpression() {list.add(expr);})* { - Statement[] stmtsArray = new Statement[list.size()]; - list.toArray(stmtsArray); - return stmtsArray;} + Expression[] exprsArray = new Expression[list.size()]; + list.toArray(exprsArray); + return exprsArray;} } Continue ContinueStatement() :