X-Git-Url: http://secure.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java index 7c99c2d..6cb4c91 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java @@ -11,17 +11,16 @@ Contributors: package net.sourceforge.phpdt.internal.compiler.parser; import java.util.ArrayList; -import java.util.Hashtable; -import net.sourceforge.phpdt.core.compiler.*; +import net.sourceforge.phpdt.core.compiler.ITerminalSymbols; +import net.sourceforge.phpdt.core.compiler.InvalidInputException; import net.sourceforge.phpeclipse.PHPeclipsePlugin; import net.sourceforge.phpeclipse.phpeditor.PHPString; import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IMarker; import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.preference.IPreferenceStore; -import org.eclipse.ui.texteditor.MarkerUtilities; + import test.PHPParserSuperclass; public class Parser extends PHPParserSuperclass implements ITerminalSymbols { @@ -262,617 +261,8 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { } return; - // boolean phpFound = false; - // char ch2; - // - // phpEnd = false; - // try { - // if (!phpMode) { - // - // while (str.length() > chIndx) { - // token = TokenNameERROR; - // ch = str.charAt(chIndx++); - // - // if (ch == '\n') { - // rowCount++; - // } - // if (ch == '<') { - // ch2 = str.charAt(chIndx++); - // if (ch2 == '?') { - // ch2 = str.charAt(chIndx++); - // if (Character.isWhitespace(ch2)) { - // // php start - // phpMode = true; - // phpFound = true; - // break; - // } else if (ch2 == 'p' || ch2 == 'P') { - // ch2 = str.charAt(chIndx++); - // if (ch2 == 'h' || ch2 == 'H') { - // ch2 = str.charAt(chIndx++); - // if (ch2 == 'p' || ch2 == 'P') { - // phpMode = true; - // phpFound = true; - // break; - // } - // chIndx--; - // } - // chIndx--; - // } - // chIndx--; - // } - // chIndx--; - // } - // } - // - // } - // - // if (phpMode) { - // while (str.length() > chIndx) { - // ch = str.charAt(chIndx++); - // token = TokenNameERROR; - // if (ch == '\n') { - // rowCount++; - // columnCount = chIndx; - // continue; // while loop - // } - // if (str.length() == chIndx) { - // phpEnd = true; - // } - // if (!Character.isWhitespace(ch)) { - // if (ch == '$') { - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '{') { - // chIndx++; - // token = TokenNameDOLLAROPEN; - // return; - // } - // } - // getIdentifier(); - // return; - // } - // if ((ch >= 'a' && ch <= 'z') - // || (ch >= 'A' && ch <= 'Z') - // || (ch == '_') - // || (ch == '$')) { - // getIdentifier(); - // return; - // } - // if (ch >= '0' && ch <= '9') { - // getNumber(); - // return; - // } - // if (ch == '/') { - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '/') { - // ch = '/'; - // chIndx++; - // // read comment until end of line: - // while ((str.length() > chIndx) - // && (ch != '\n')) { - // ch = str.charAt(chIndx++); - // if (ch == '?') { - // ch2 = str.charAt(chIndx); - // if (ch2 == '>') { - // chIndx++; - // token = TokenNameHTML; - // // php end - // phpMode = false; - // phpEnd = true; - // return; - // } - // } - // } - // rowCount++; - // continue; - // - // } else if (str.charAt(chIndx) == '*') { - // chIndx++; - // // multi line comment: - // while (str.length() > chIndx) { - // if (str.charAt(chIndx) == '*' - // && (str.length() > (chIndx + 1)) - // && str.charAt(chIndx + 1) == '/') { - // chIndx += 2; - // break; - // } - // ch = str.charAt(chIndx++); - // if (ch == '\n') { - // rowCount++; - // columnCount = chIndx; - // } - // } - // continue; - // } - // } - // } else if (ch == '#') { - // // read comment until end of line: - // while ((str.length() > chIndx) && (ch != '\n')) { - // ch = str.charAt(chIndx++); - // if (ch == '?') { - // ch2 = str.charAt(chIndx); - // if (ch2 == '>') { - // chIndx++; - // token = TokenNameHTML; - // // php end - // phpMode = false; - // phpEnd = true; - // return; - // } - // } - // } - // rowCount++; - // continue; - // - // } else if (ch == '"') { - // getString( - // '"', - // TokenNameStringInterpolated, - // "Open string character '\"' at end of file."); - // return; - // } else if (ch == '\'') { - // getString( - // '\'', - // TokenNameStringConstant, - // "Open string character \"'\" at end of file."); - // return; - // } else if (ch == '`') { - // getString( - // '`', - // TokenNameStringConstant, - // "Open string character \"`\" at end of file."); - // setMarker( - // "Other string delimiters prefered (found \"`\").", - // rowCount, - // PHPParser.INFO); - // return; - // } - // - // switch (ch) { - // - // case '(' : - // token = TokenNameLPAREN; - // - // break; - // case ')' : - // token = TokenNameRPAREN; - // - // break; - // case '{' : - // token = TokenNameLBRACE; - // - // break; - // case '}' : - // token = TokenNameRBRACE; - // - // break; - // case '[' : - // token = TokenNameLBRACKET; - // - // break; - // case ']' : - // token = TokenNameRBRACKET; - // - // break; - // case ',' : - // token = TokenNameCOMMA; - // - // break; - // case '?' : - // token = TokenNameQUESTION; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '>') { - // chIndx++; - // token = TokenNameHTML; - // // php end - // phpMode = false; - // phpEnd = true; - // break; - // } - // } - // - // break; - // case '@' : - // token = TokenNameAT; - // break; - // case '~' : - // token = TokenNameTWIDDLE; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameTWIDDLE_EQUAL; - // - // break; - // } - // } - // break; - // case '.' : - // token = TokenNameDOT; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameDOT_EQUAL; - // - // break; - // } - // } - // - // break; - // case '"' : - // token = TokenNameStringLiteral; - // - // break; - // case '%' : - // token = TokenNameREMAINDER; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameREMAINDER_EQUAL; - // - // break; - // } - // } - // break; - // case ';' : - // token = TokenNameSEMICOLON; - // - // break; - // case '^' : - // token = TokenNameXOR; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameXOR_EQUAL; - // - // break; - // } - // } - // break; - // case '/' : - // token = TokenNameDIVIDE; - // - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameDIVIDE_EQUAL; - // - // break; - // } - // } - // - // break; - // case '*' : - // token = TokenNameMULTIPLY; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '*') { - // chIndx++; - // token = TokenNameXOR; - // - // break; - // } - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameMULTIPLY_EQUAL; - // - // break; - // } - // } - // - // break; - // case '+' : - // token = TokenNamePLUS; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '+') { - // chIndx++; - // token = TokenNamePLUS_PLUS; - // - // break; - // } - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNamePLUS_EQUAL; - // - // break; - // } - // } - // break; - // case '-' : - // token = TokenNameMINUS; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '-') { - // chIndx++; - // token = TokenNameMINUS_MINUS; - // - // break; - // } - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameMINUS_EQUAL; - // - // break; - // } - // if (str.charAt(chIndx) == '>') { - // chIndx++; - // token = TokenNameMINUS_GREATER; - // - // break; - // } - // } - // - // break; - // case '=' : - // token = TokenNameEQUAL; - // - // if (str.length() > chIndx) { - // ch = str.charAt(chIndx); - // - // if (ch == '=') { - // chIndx++; - // token = TokenNameEQUAL_EQUAL; - // if (str.length() > chIndx) { - // ch = str.charAt(chIndx); - // - // if (ch == '=') { - // chIndx++; - // token = - // TokenNameEQUAL_EQUAL_EQUAL; - // } - // } - // break; - // } - // if (ch == '>') { - // chIndx++; - // token = TokenNameEQUAL_GREATER; - // - // break; - // } - // } - // - // break; - // case '!' : - // token = TokenNameNOT; - // - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameNOT_EQUAL; - // if (str.length() > chIndx) { - // ch = str.charAt(chIndx); - // - // if (ch == '=') { - // chIndx++; - // token = - // TokenNameNOT_EQUAL_EQUAL; - // } - // } - // break; - // } - // } - // - // break; - // case '>' : - // token = TokenNameGREATER; - // - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameGREATER_EQUAL; - // break; - // } - // if (str.charAt(chIndx) == '>') { - // chIndx++; - // token = TokenNameRIGHT_SHIFT; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = - // TokenNameRIGHT_SHIFT_EQUAL; - // break; - // } - // } - // break; - // } - // } - // - // break; - // case '<' : - // token = TokenNameLESS; - // - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameLESS_EQUAL; - // - // break; - // } - // if (str.charAt(chIndx) == '<') { - // chIndx++; - // token = TokenNameLEFT_SHIFT; - // if (str.charAt(chIndx) == '<') { - // // heredoc - // int startRow = rowCount; - // if (str.length() > chIndx) { - // - // ch = str.charAt(++chIndx); - // if ((ch >= 'a' && ch <= 'z') - // || (ch >= 'A' && ch <= 'Z') - // || (ch == '_')) { - // chIndx++; - // getIdentifier(); - // token = - // TokenNameStringConstant; - // while (str.length() - // > chIndx) { - // ch = - // str.charAt( - // chIndx++); - // if (ch == '\n') { - // if (str.length() - // >= chIndx - // + identifier - // .length()) { - // if (str - // .substring( - // chIndx, - // chIndx - // + identifier - // .length()) - // .equals(identifier)) { - // chIndx - // += identifier - // .length(); - // return; - // } - // } - // } - // } - // } - // } - // throwSyntaxError( - // "Open heredoc syntax after operator '<<<'.", - // startRow); - // } else if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameLEFT_SHIFT_EQUAL; - // break; - // } - // break; - // } - // } - // - // break; - // - // case '|' : - // token = TokenNameOR; - // - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '|') { - // chIndx++; - // token = TokenNameOR_OR; - // break; - // } - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameOR_EQUAL; - // break; - // } - // } - // - // break; - // case '&' : - // token = TokenNameAND; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == '&') { - // chIndx++; - // token = TokenNameAND_AND; - // break; - // } - // if (str.charAt(chIndx) == '=') { - // chIndx++; - // token = TokenNameAND_EQUAL; - // break; - // } - // break; - // } - // - // break; - // case ':' : - // token = TokenNameCOLON; - // if (str.length() > chIndx) { - // if (str.charAt(chIndx) == ':') { - // chIndx++; - // token = TokenNameCOLON_COLON; - // } - // } - // break; - // // case '#' : - // // token = TokenNameHASH; - // // - // // break; - // // case '@' : - // // token = TokenNameAT; - // // - // // break; - // default : - // throwSyntaxError( - // "unexpected character: '" + ch + "'"); - // } - // - // if (token == TokenNameERROR) { - // throwSyntaxError("token not found"); - // } - // - // return; - // } - // } - // } - // } catch (StringIndexOutOfBoundsException e) { - // // catched from charAt - // } - // - // chIndx = str.length() + 1; - // ch = ' '; - // token = TokenNameEOF; - // phpEnd = true; - // //PHPString temp; - // // if (phpList != null) { - // // if (currentPHPString < phpList.size()) { - // // token = TokenNameUNDEFINED; - // // temp = (PHPString) phpList.get(currentPHPString++); - // // this.str = temp.getPHPString(); - // // this.token = TokenNameEOF; - // // this.chIndx = 0; - // // this.rowCount = temp.getLineNumber(); - // // this.columnCount = 0; - // // getNextToken(); - // // phpEnd = true; - // // } else { - // // token = TokenNameUNDEFINED; - // // return; - // // } - // // } } - // /** - // * Get an identifier. - // */ - // private void getIdentifier() { - // // StringBuffer ident = new StringBuffer(); - // int startPosition = chIndx - 1; - // // ident.append(ch); - // if (ch == '$') { - // getChar(); - // // attention recursive call: - // getIdentifier(); - // token = TokenNameVariable; - // return; - // } else { - // token = TokenNameIdentifier; - // } - // - // getChar(); - // - // //this will read the buffer until the next character is a forbidden character for identifier - // while ((ch >= 'a' && ch <= 'z') - // || (ch >= 'A' && ch <= 'Z') - // || (ch >= '0' && ch <= '9') - // || (ch == '_')) { - // // ident.append(ch); - // getChar(); - // } - // int endPosition = chIndx--; - // int length = (--endPosition) - startPosition; - // - // identifier = str.substring(startPosition, endPosition); - // // System.out.println(identifier); - // - // // determine if this identitfer is a keyword - // // @todo improve this in future version - // Integer i = (Integer) keywordMap.get(identifier.toLowerCase()); - // if (i != null) { - // token = i.intValue(); - // } - // } - /** * Get a number. * if it's a double the number will be stored in doubleNumber and the token will have the @@ -1391,6 +781,10 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { return outlineInfo; } + private boolean isVariable() { + return token == TokenNameVariable || token == TokenNamethis; + } + private void parseDeclarations(PHPOutlineInfo outlineInfo, OutlineableWithChildren current, boolean goBack) { char[] ident; // PHPClassDeclaration current = (PHPClassDeclaration) stack.peek(); @@ -1423,6 +817,7 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { } else { switch (token) { case TokenNameVariable : + case TokenNamethis : current.add(new PHPVarDeclaration(current, variableName, // chIndx - ident.length, scanner.getCurrentTokenStartPosition(), new String(ident))); @@ -1548,6 +943,7 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) + || (token == TokenNameelse) || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor) @@ -1989,6 +1385,9 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { constant(); } } else { + if (token == TokenNamethis) { + throwSyntaxError("Reserved word '$this' not allowed after keyword 'var'."); + } throwSyntaxError("Variable expected after keyword 'var'."); } if (token != TokenNameCOMMA) { @@ -2053,7 +1452,7 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { //variable-reference if (token == TokenNameAND) { getNextToken(); - if (token == TokenNameVariable) { + if (isVariable()) { getNextToken(); } else { throwSyntaxError("Variable expected after reference operator '&'."); @@ -2068,6 +1467,9 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { } return; } + if (token == TokenNamethis) { + throwSyntaxError("Reserved word '$this' not allowed in parameter declaration."); + } } private void labeledStatementList() throws CoreException { @@ -2077,7 +1479,7 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { do { if (token == TokenNamecase) { getNextToken(); - constant(); + expression(); //constant(); if (token == TokenNameCOLON) { getNextToken(); if (token == TokenNamecase || token == TokenNamedefault) { // empty case statement ? @@ -2173,26 +1575,30 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { // ':' statement-list [elseif-list] [else-colon-statement] 'endif' ';' if (token == TokenNameCOLON) { getNextToken(); - statementList(); - switch (token) { - case TokenNameelse : - getNextToken(); - if (token == TokenNameCOLON) { + if (token != TokenNameendif) { + statementList(); + switch (token) { + case TokenNameelse : getNextToken(); - statementList(); - } else { - if (token == TokenNameif) { //'else if' + if (token == TokenNameCOLON) { getNextToken(); - elseifStatementList(); + if (token != TokenNameendif) { + statementList(); + } } else { - throwSyntaxError("':' expected after 'else'."); + if (token == TokenNameif) { //'else if' + getNextToken(); + elseifStatementList(); + } else { + throwSyntaxError("':' expected after 'else'."); + } } - } - break; - case TokenNameelseif : - getNextToken(); - elseifStatementList(); - break; + break; + case TokenNameelseif : + getNextToken(); + elseifStatementList(); + break; + } } if (token != TokenNameendif) { @@ -2235,7 +1641,9 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { getNextToken(); if (token == TokenNameCOLON) { getNextToken(); - statementList(); + if (token != TokenNameendif) { + statementList(); + } return; } else { if (token == TokenNameif) { //'else if' @@ -2258,7 +1666,7 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { if (token == TokenNameLPAREN) { getNextToken(); expression(); - if (token != TokenNameLPAREN) { + if (token != TokenNameRPAREN) { throwSyntaxError("')' expected in else-if-statement."); } getNextToken(); @@ -2266,7 +1674,9 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { throwSyntaxError("':' expected in else-if-statement."); } getNextToken(); - statementList(); + if (token != TokenNameendif) { + statementList(); + } } } @@ -2471,6 +1881,7 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { getNextToken(); break; case TokenNameVariable : + case TokenNamethis : ident = scanner.getCurrentIdentifierSource(); getNextToken(); if (token == TokenNameLBRACE) { @@ -2655,16 +2066,14 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { throwSyntaxError("] expected after '->'."); } getNextToken(); - } - if (token == TokenNameLPAREN) { + } else if (token == TokenNameLPAREN) { getNextToken(); expressionList(); if (token != TokenNameRPAREN) { throwSyntaxError(") expected after '->'."); } getNextToken(); - } - if (token == TokenNameLBRACE) { + } else if (token == TokenNameLBRACE) { getNextToken(); expression(); if (token != TokenNameRBRACE) { @@ -2701,7 +2110,14 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { // '@' '&' '*' '+' '-' '~' '!' case TokenNameAT : getNextToken(); - castExpression(); + if (token == TokenNameinclude + || token == TokenNameinclude_once + || token == TokenNamerequire + || token == TokenNamerequire_once) { + statement(); + } else { + postfixExpression(); // castExpression(); + } break; case TokenNameAND : getNextToken(); @@ -2744,27 +2160,6 @@ public class Parser extends PHPParserSuperclass implements ITerminalSymbols { unaryExpression(); } - // private void typeName() throws CoreException { - // //'string' 'unset' 'array' 'object' - // //'bool' 'boolean' - // //'real' 'double' 'float' - // //'int' 'integer' - // String identifier = ""; - // if (token == TokenNameIdentifier) { - // char[] ident = scanner.getCurrentIdentifierSource(); - // identifier = new String(ident); - // String str = identifier.toLowerCase(); - // getNextToken(); - // for (int i = 0; i < PHP_TYPES.length; i++) { - // if (PHP_TYPES[i].equals(str)) { - // return; - // } - // } - // } - // throwSyntaxError( - // "Expected type cast '( )'; Got '" + identifier + "'."); - // } - private void assignExpression() throws CoreException { castExpression(); if (token == TokenNameEQUAL) { // =