From 2886b1c517878abedb1271b6c5544cfb9592e51d Mon Sep 17 00:00:00 2001 From: khartlage Date: Fri, 2 Apr 2004 18:37:51 +0000 Subject: [PATCH] misc parser changes --- .../phpdt/internal/compiler/parser/Parser.java | 341 +++++++++++++------- .../phpdt/internal/compiler/parser/Scanner.java | 4 + .../phpeclipse/phpeditor/EditorUtility.java | 2 +- 3 files changed, 222 insertions(+), 125 deletions(-) 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 c9b73cf..12b9245 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 @@ -23,7 +23,6 @@ import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclarati import net.sourceforge.phpeclipse.internal.compiler.ast.MethodDeclaration; import net.sourceforge.phpeclipse.internal.compiler.ast.SingleTypeReference; import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration; - import org.eclipse.core.resources.IFile; public class Parser //extends PHPParserSuperclass implements ITerminalSymbols, CompilerModifiers, ParserBasicInformation { @@ -554,78 +553,78 @@ public class Parser //extends PHPParserSuperclass // } catch (CoreException e) { // } // } -// public void phpParserTester(String s, int rowCount) { -// this.str = s; -// if (s == null) { -// if (phpList.size() != 0) { -// this.str = ((PHPString) phpList.get(currentPHPString++)).getPHPString(); -// } -// } -// this.token = TokenNameEOF; -// // this.chIndx = 0; -// // this.rowCount = rowCount; -// // this.columnCount = 0; -// this.phpEnd = false; -// this.phpMode = true; -// scanner.setSource(s.toCharArray()); -// scanner.setPHPMode(true); -// getNextToken(); -// do { -// try { -// if (token != TokenNameEOF && token != TokenNameERROR) { -// statementList(); -// } -// if (token != TokenNameEOF) { -// if (token == TokenNameERROR) { -// throwSyntaxError("Scanner error (Found unknown token: " -// + scanner.toStringAction(token) + ")"); -// } -// if (token == TokenNameRPAREN) { -// throwSyntaxError("Too many closing ')'; end-of-file not reached."); -// } -// if (token == TokenNameRBRACE) { -// throwSyntaxError("Too many closing '}'; end-of-file not reached."); -// } -// if (token == TokenNameRBRACKET) { -// throwSyntaxError("Too many closing ']'; end-of-file not reached."); -// } -// if (token == TokenNameLPAREN) { -// throwSyntaxError("Read character '('; end-of-file not reached."); -// } -// if (token == TokenNameLBRACE) { -// throwSyntaxError("Read character '{'; end-of-file not reached."); -// } -// if (token == TokenNameLBRACKET) { -// throwSyntaxError("Read character '['; end-of-file not reached."); -// } -// throwSyntaxError("End-of-file not reached."); -// } -// return; -// } catch (SyntaxError err) { -// if (s != null) { -// throw err; -// } else { -// // setMarker(err.getMessage(), err.getLine(), ERROR); -// // setMarker(err.getMessage(), -// // scanner.getCurrentTokenStartPosition(), -// // scanner.getCurrentTokenEndPosition(), ERROR); -// } -// // if an error occured, -// // try to find keywords 'class' or 'function' -// // to parse the rest of the string -// while (token != TokenNameEOF && token != TokenNameERROR) { -// if (token == TokenNameabstract || token == TokenNamefinal -// || token == TokenNameclass || token == TokenNamefunction) { -// break; -// } -// getNextToken(); -// } -// if (token == TokenNameEOF || token == TokenNameERROR) { -// return; -// } -// } -// } while (true); -// } + // public void phpParserTester(String s, int rowCount) { + // this.str = s; + // if (s == null) { + // if (phpList.size() != 0) { + // this.str = ((PHPString) phpList.get(currentPHPString++)).getPHPString(); + // } + // } + // this.token = TokenNameEOF; + // // this.chIndx = 0; + // // this.rowCount = rowCount; + // // this.columnCount = 0; + // this.phpEnd = false; + // this.phpMode = true; + // scanner.setSource(s.toCharArray()); + // scanner.setPHPMode(true); + // getNextToken(); + // do { + // try { + // if (token != TokenNameEOF && token != TokenNameERROR) { + // statementList(); + // } + // if (token != TokenNameEOF) { + // if (token == TokenNameERROR) { + // throwSyntaxError("Scanner error (Found unknown token: " + // + scanner.toStringAction(token) + ")"); + // } + // if (token == TokenNameRPAREN) { + // throwSyntaxError("Too many closing ')'; end-of-file not reached."); + // } + // if (token == TokenNameRBRACE) { + // throwSyntaxError("Too many closing '}'; end-of-file not reached."); + // } + // if (token == TokenNameRBRACKET) { + // throwSyntaxError("Too many closing ']'; end-of-file not reached."); + // } + // if (token == TokenNameLPAREN) { + // throwSyntaxError("Read character '('; end-of-file not reached."); + // } + // if (token == TokenNameLBRACE) { + // throwSyntaxError("Read character '{'; end-of-file not reached."); + // } + // if (token == TokenNameLBRACKET) { + // throwSyntaxError("Read character '['; end-of-file not reached."); + // } + // throwSyntaxError("End-of-file not reached."); + // } + // return; + // } catch (SyntaxError err) { + // if (s != null) { + // throw err; + // } else { + // // setMarker(err.getMessage(), err.getLine(), ERROR); + // // setMarker(err.getMessage(), + // // scanner.getCurrentTokenStartPosition(), + // // scanner.getCurrentTokenEndPosition(), ERROR); + // } + // // if an error occured, + // // try to find keywords 'class' or 'function' + // // to parse the rest of the string + // while (token != TokenNameEOF && token != TokenNameERROR) { + // if (token == TokenNameabstract || token == TokenNamefinal + // || token == TokenNameclass || token == TokenNamefunction) { + // break; + // } + // getNextToken(); + // } + // if (token == TokenNameEOF || token == TokenNameERROR) { + // return; + // } + // } + // } while (true); + // } public void init(String s) { this.str = s; this.token = TokenNameEOF; @@ -633,7 +632,7 @@ public class Parser //extends PHPParserSuperclass // this.rowCount = 1; // this.columnCount = 0; this.phpEnd = false; -// this.phpMode = false; + // this.phpMode = false; /* scanner initialization */ scanner.setSource(s.toCharArray()); scanner.setPHPMode(false); @@ -647,7 +646,7 @@ public class Parser //extends PHPParserSuperclass // this.rowCount = 1; // this.columnCount = 0; this.phpEnd = false; -// this.phpMode = phpMode; + // this.phpMode = phpMode; scanner.setPHPMode(phpMode); } /** @@ -1130,7 +1129,9 @@ public class Parser //extends PHPParserSuperclass } else { throwSyntaxError("'as' expected after 'foreach' exxpression."); } - variable(); +// variable(); + foreach_variable(); + foreach_optional_arg(); if (token == TokenNameEQUAL_GREATER) { getNextToken(); variable(); @@ -1208,7 +1209,7 @@ public class Parser //extends PHPParserSuperclass getNextToken(); } return; - }else if (token == TokenNameunset) { + } else if (token == TokenNameunset) { getNextToken(); if (token == TokenNameLPAREN) { getNextToken(); @@ -1240,19 +1241,19 @@ public class Parser //extends PHPParserSuperclass return; } else if (token == TokenNametry) { getNextToken(); -if (token != TokenNameLBRACE) { + if (token != TokenNameLBRACE) { throwSyntaxError("'{' expected in 'try' statement."); } getNextToken(); statementList(); -if (token != TokenNameRBRACE) { + if (token != TokenNameRBRACE) { throwSyntaxError("'}' expected in 'try' statement."); } -getNextToken(); + getNextToken(); return; } else if (token == TokenNamecatch) { -getNextToken(); -if (token != TokenNameLPAREN) { + getNextToken(); + if (token != TokenNameLPAREN) { throwSyntaxError("'(' expected in 'catch' statement."); } getNextToken(); @@ -1261,33 +1262,29 @@ if (token != TokenNameLPAREN) { throwSyntaxError("Variable expected in 'catch' statement."); } getNextToken(); - - if (token != TokenNameRBRACE) { + if (token != TokenNameRPAREN) { throwSyntaxError("')' expected in 'catch' statement."); } getNextToken(); -getNextToken(); -if (token != TokenNameLBRACE) { + if (token != TokenNameLBRACE) { throwSyntaxError("'{' expected in 'catch' statement."); } getNextToken(); statementList(); -if (token != TokenNameRBRACE) { + if (token != TokenNameRBRACE) { throwSyntaxError("'}' expected in 'catch' statement."); } -getNextToken(); + getNextToken(); additional_catches(); return; } else if (token == TokenNamethrow) { getNextToken(); - expr(); if (token == TokenNameSEMICOLON) { getNextToken(); } else { throwSyntaxError("';' expected after 'throw' exxpression."); } - return; } else if (token == TokenNamefinal || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) { @@ -1347,12 +1344,10 @@ getNextToken(); throwSyntaxError("Variable expected in 'catch' statement."); } getNextToken(); - - if (token != TokenNameRBRACE) { + if (token != TokenNameRPAREN) { throwSyntaxError("')' expected in 'catch' statement."); } getNextToken(); - getNextToken(); if (token != TokenNameLBRACE) { throwSyntaxError("'{' expected in 'catch' statement."); } @@ -1364,7 +1359,24 @@ getNextToken(); getNextToken(); } } - + + private void foreach_variable() { + // w_variable + //| '&' w_variable + if (token == TokenNameAND) { + getNextToken(); + } + w_variable(); + } + private void foreach_optional_arg() { + // /* empty */ + //| T_DOUBLE_ARROW foreach_variable + if (token == TokenNameEQUAL_GREATER) { + getNextToken(); + foreach_variable(); + } + } + private void global_var_list() { // global_var_list: // global_var_list ',' global_var @@ -1607,7 +1619,8 @@ getNextToken(); } while (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic || token == TokenNameabstract || token == TokenNamefinal - || token == TokenNamefunction || token == TokenNamevar); + || token == TokenNamefunction || token == TokenNamevar + || token == TokenNameconst); } private void class_statement() { // class_statement: @@ -1624,6 +1637,12 @@ getNextToken(); compilationUnit.compilationResult); getNextToken(); class_variable_declaration(); + } else if (token == TokenNameconst) { + class_constant_declaration(); + if (token != TokenNameSEMICOLON) { + throwSyntaxError("';' expected after class const declaration."); + } + getNextToken(); } else { boolean hasModifiers = member_modifiers(); if (token == TokenNamefunction) { @@ -1658,6 +1677,30 @@ getNextToken(); // throwSyntaxError("'function' or 'var' expected."); // } } + private void class_constant_declaration() { + // class_constant_declaration ',' T_STRING '=' static_scalar + // | T_CONST T_STRING '=' static_scalar + if (token != TokenNameconst) { + throwSyntaxError("'const' keyword expected in class declaration."); + } else { + getNextToken(); + } + while (true) { + if (token != TokenNameIdentifier) { + throwSyntaxError("Identifier expected in class const declaration."); + } + getNextToken(); + if (token != TokenNameEQUAL) { + throwSyntaxError("'=' expected in class const declaration."); + } + getNextToken(); + static_scalar(); + if (token != TokenNameCOMMA) { + break; // while(true)-loop + } + getNextToken(); + } + } // private void variable_modifiers() { // // variable_modifiers: // // non_empty_member_modifiers @@ -1757,6 +1800,7 @@ getNextToken(); getNextToken(); } private void functionDefinition(MethodDeclaration methodDecl) { + boolean isAbstract = false; if (astPtr == 0) { compilationUnit.types.add(methodDecl); } else { @@ -1776,9 +1820,22 @@ getNextToken(); newMethods[0] = methodDecl; typeDecl.methods = newMethods; } + if ((typeDecl.modifiers & AccAbstract) == AccAbstract) { + isAbstract = true; + } else if ((typeDecl.modifiers & AccInterface) == AccInterface) { + isAbstract = true; + } } } functionDeclarator(methodDecl); + if (token == TokenNameSEMICOLON) { + if (!isAbstract) { + throwSyntaxError("Body declaration expected for method: " + + new String(methodDecl.selector)); + } + getNextToken(); + return; + } functionBody(methodDecl); } private void functionDeclarator(MethodDeclaration methodDecl) { @@ -1797,7 +1854,7 @@ getNextToken(); throwSyntaxError("'(' expected in function declaration."); } if (token != TokenNameRPAREN) { - parameterList(); + parameter_list(); } if (token != TokenNameRPAREN) { throwSyntaxError("')' expected in function declaration."); @@ -1814,16 +1871,54 @@ getNextToken(); } } // - private void parameterList() { - //parameter-declaration - //parameter-list ',' parameter-declaration - do { - parameterDeclaration(); - if (token != TokenNameCOMMA) { - break; + private void parameter_list() { + // non_empty_parameter_list + // | /* empty */ + non_empty_parameter_list(true); + } + private void non_empty_parameter_list(boolean empty_allowed) { + // optional_class_type T_VARIABLE + // | optional_class_type '&' T_VARIABLE + // | optional_class_type '&' T_VARIABLE '=' static_scalar + // | optional_class_type T_VARIABLE '=' static_scalar + // | non_empty_parameter_list ',' optional_class_type T_VARIABLE + // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE + // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE '=' + // static_scalar + // | non_empty_parameter_list ',' optional_class_type T_VARIABLE '=' + // static_scalar + if (token == TokenNameIdentifier || token == TokenNameVariable + || token == TokenNameAND) { + while (true) { + if (token == TokenNameIdentifier) { + getNextToken(); + } + if (token == TokenNameAND) { + getNextToken(); + } + if (token == TokenNameVariable) { + getNextToken(); + if (token == TokenNameEQUAL) { + getNextToken(); + static_scalar(); + } + } else { + throwSyntaxError("Variable expected in parameter list."); + } + if (token != TokenNameCOMMA) { + break; + } + getNextToken(); } - getNextToken(); - } while (true); + return; + } + if (!empty_allowed) { + throwSyntaxError("Identifier expected in parameter list."); + } + } + private void optional_class_type() { + // /* empty */ + //| T_STRING } private void parameterDeclaration() { //variable @@ -2491,8 +2586,7 @@ getNextToken(); // | expr '?' expr ':' expr case TokenNameinstanceof : getNextToken(); - throwSyntaxError("not yet implemented (class_name_reference)"); - // class_name_reference(); + class_name_reference(); break; case TokenNameQUESTION : getNextToken(); @@ -2653,16 +2747,16 @@ getNextToken(); } } } -// private void variableList() { -// do { -// variable(); -// if (token == TokenNameCOMMA) { -// getNextToken(); -// } else { -// break; -// } -// } while (true); -// } + // private void variableList() { + // do { + // variable(); + // if (token == TokenNameCOMMA) { + // getNextToken(); + // } else { + // break; + // } + // } while (true); + // } private void variable_without_objects() { // variable_without_objects: // reference_variable @@ -2739,12 +2833,12 @@ getNextToken(); getNextToken(); w_variable(); } else { -// if (token == TokenNameIdentifier || token == TokenNameVariable -// || token == TokenNameDOLLAR) { -// variable(); -// } else { - expr_without_variable(true); -// } + // if (token == TokenNameIdentifier || token == TokenNameVariable + // || token == TokenNameDOLLAR) { + // variable(); + // } else { + expr_without_variable(true); + // } } if (token != TokenNameCOMMA) { break; @@ -2944,7 +3038,6 @@ getNextToken(); } getNextToken(); } else { - if (token != TokenNameLBRACE) { throwSyntaxError("'{' expected in variable name."); } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java index ff11fa1..1ae5d16 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java @@ -3494,6 +3494,8 @@ public class Scanner implements IScanner, ITerminalSymbols { return "case"; //$NON-NLS-1$ case TokenNameclass : return "class"; //$NON-NLS-1$ + case TokenNamecatch : + return "catch"; //$NON-NLS-1$ case TokenNameclone : //$NON-NLS-1$ return "clone"; @@ -3546,6 +3548,8 @@ public class Scanner implements IScanner, ITerminalSymbols { return "include"; //$NON-NLS-1$ case TokenNameinclude_once : return "include_once"; //$NON-NLS-1$ + case TokenNameinstanceof : + return "instanceof"; //$NON-NLS-1$ case TokenNameinterface : return "interface"; //$NON-NLS-1$ case TokenNameisset : diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/EditorUtility.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/EditorUtility.java index b874f4f..097261a 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/EditorUtility.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/EditorUtility.java @@ -67,7 +67,7 @@ public class EditorUtility { IEditorInput input= null; try { - input= getEditorInput(inputElement); + input = getEditorInput(inputElement); } catch (JavaModelException x) { PHPeclipsePlugin.log(x.getStatus()); } -- 1.7.1