From: axelcl Date: Sat, 16 Apr 2005 22:42:28 +0000 (+0000) Subject: Added error "Unreachable code" for some very simple cases X-Git-Url: http://git.phpeclipse.com Added error "Unreachable code" for some very simple cases --- 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 6824bd6..0bfc22c 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 @@ -26,9 +26,10 @@ import net.sourceforge.phpeclipse.internal.compiler.ast.AND_AND_Expression; import net.sourceforge.phpeclipse.internal.compiler.ast.ASTNode; import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration; import net.sourceforge.phpeclipse.internal.compiler.ast.BinaryExpression; +import net.sourceforge.phpeclipse.internal.compiler.ast.BreakStatement; import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration; import net.sourceforge.phpeclipse.internal.compiler.ast.ConditionalExpression; -import net.sourceforge.phpeclipse.internal.compiler.ast.EmptyStatement; +import net.sourceforge.phpeclipse.internal.compiler.ast.ContinueStatement; import net.sourceforge.phpeclipse.internal.compiler.ast.EqualExpression; import net.sourceforge.phpeclipse.internal.compiler.ast.Expression; import net.sourceforge.phpeclipse.internal.compiler.ast.FieldDeclaration; @@ -39,6 +40,7 @@ import net.sourceforge.phpeclipse.internal.compiler.ast.InstanceOfExpression; import net.sourceforge.phpeclipse.internal.compiler.ast.MethodDeclaration; import net.sourceforge.phpeclipse.internal.compiler.ast.OR_OR_Expression; import net.sourceforge.phpeclipse.internal.compiler.ast.OperatorIds; +import net.sourceforge.phpeclipse.internal.compiler.ast.ReturnStatement; import net.sourceforge.phpeclipse.internal.compiler.ast.SingleTypeReference; import net.sourceforge.phpeclipse.internal.compiler.ast.Statement; import net.sourceforge.phpeclipse.internal.compiler.ast.StringLiteral; @@ -458,20 +460,33 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI return compilationUnit; } - private boolean isVariable() { - return token == TokenNameVariable; // || token == TokenNamethis; - } +// private boolean isVariable() { +// return token == TokenNameVariable; // || token == TokenNamethis; +// } private void statementList() { + boolean branchStatement = false; + Statement statement; + int sourceStart; + int sourceEnd; do { try { - statement(TokenNameEOF); + sourceStart = scanner.getCurrentTokenStartPosition(); + statement = statement(TokenNameEOF); + if (branchStatement) { + sourceEnd = scanner.getCurrentTokenEndPosition(); + reportSyntaxError("Unreachable code", sourceStart, sourceEnd); + } if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse) || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor) || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch) || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) { return; } + branchStatement = false; + if (statement instanceof ReturnStatement ||statement instanceof ContinueStatement || statement instanceof BreakStatement) { + branchStatement = true; + } } catch (SyntaxError sytaxErr1) { // if an error occured, // try to find keywords @@ -532,6 +547,7 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI Statement statement = null; Expression expression; int sourceStart = scanner.getCurrentTokenStartPosition(); + int sourceEnd; if (token == TokenNameif) { getNextToken(); if (token == TokenNameLPAREN) { @@ -683,20 +699,57 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI } foreachStatement(); return statement; - } else if (token == TokenNamecontinue || token == TokenNamebreak || token == TokenNamereturn) { + } else if (token == TokenNamebreak) { + expression = null; getNextToken(); if (token != TokenNameSEMICOLON) { - expr(); + expression = expr(); } if (token == TokenNameSEMICOLON) { + sourceEnd = scanner.getCurrentTokenEndPosition(); getNextToken(); } else { if (token != TokenNameINLINE_HTML) { - throwSyntaxError("';' expected after 'continue', 'break' or 'return'."); + throwSyntaxError("';' expected after 'break'."); } + sourceEnd = scanner.getCurrentTokenEndPosition(); getNextToken(); } - return statement; + return new BreakStatement(null,sourceStart, sourceEnd); + } else if (token == TokenNamecontinue) { + expression = null; + getNextToken(); + if (token != TokenNameSEMICOLON) { + expression = expr(); + } + if (token == TokenNameSEMICOLON) { + sourceEnd = scanner.getCurrentTokenEndPosition(); + getNextToken(); + } else { + if (token != TokenNameINLINE_HTML) { + throwSyntaxError("';' expected after 'continue'."); + } + sourceEnd = scanner.getCurrentTokenEndPosition(); + getNextToken(); + } + return new ContinueStatement(null,sourceStart, sourceEnd); + } else if (token == TokenNamereturn) { + expression = null; + getNextToken(); + if (token != TokenNameSEMICOLON) { + expression = expr(); + } + if (token == TokenNameSEMICOLON) { + sourceEnd = scanner.getCurrentTokenEndPosition(); + getNextToken(); + } else { + if (token != TokenNameINLINE_HTML) { + throwSyntaxError("';' expected after 'return'."); + } + sourceEnd = scanner.getCurrentTokenEndPosition(); + getNextToken(); + } + return new ReturnStatement(expression,sourceStart, sourceEnd); } else if (token == TokenNameecho) { getNextToken(); expressionList(); @@ -779,7 +832,7 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI getNextToken(); functionDefinition(methodDecl); } finally { - int sourceEnd = scanner.getCurrentTokenStartPosition(); + sourceEnd = scanner.getCurrentTokenStartPosition(); if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) { sourceEnd = methodDecl.declarationSourceStart + 1; }