import java.util.ArrayList;
import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Stack;
import net.sourceforge.phpdt.core.compiler.CharOperation;
import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
*/
public void parse(String s, HashMap variables) {
fMethodVariables = variables;
+ fStackUnassigned = new Stack();
init(s);
parse();
}
try {
statement = statement();
blockStatements.add(statement);
- if (branchStatement && statement!=null) {
- reportSyntaxError("Unreachable code", statement.sourceStart, statement.sourceEnd);
+ if (branchStatement && statement != null) {
+// reportSyntaxError("Unreachable code", statement.sourceStart, statement.sourceEnd);
+ problemReporter.unreachableCode(new String(scanner.getCurrentIdentifierSource()),statement.sourceStart, statement.sourceEnd,
+ referenceContext, compilationUnit.compilationResult);
}
if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
|| (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
private boolean checkUnreachableStatements(Statement statement) {
if (statement instanceof ReturnStatement || statement instanceof ContinueStatement || statement instanceof BreakStatement) {
return true;
- } else if (statement instanceof IfStatement && ((IfStatement)statement).checkUnreachable) {
+ } else if (statement instanceof IfStatement && ((IfStatement) statement).checkUnreachable) {
return true;
}
return false;
foreach_optional_arg();
if (token == TokenNameEQUAL_GREATER) {
getNextToken();
- variable();
+ variable(false);
}
if (token == TokenNameRPAREN) {
getNextToken();
if (token == TokenNameAND) {
getNextToken();
}
- w_variable();
+ w_variable(true);
}
private void foreach_optional_arg() {
// global_var_list:
// global_var_list ',' global_var
//| global_var
+ HashSet set = peekVariableSet();
while (true) {
- global_var();
+ global_var(set);
if (token != TokenNameCOMMA) {
break;
}
}
}
- private void global_var() {
+ private void global_var(HashSet set) {
//global_var:
// T_VARIABLE
//| '$' r_variable
//| '$' '{' expr '}'
if (token == TokenNameVariable) {
- VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_GLOBAL_VAR);
if (fMethodVariables != null) {
+ VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_GLOBAL_VAR);
fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
}
+ addVariableSet(set);
getNextToken();
} else if (token == TokenNameDOLLAR) {
getNextToken();
// static_var_list ',' T_VARIABLE
//| static_var_list ',' T_VARIABLE '=' static_scalar
//| T_VARIABLE
- //| T_VARIABLE '=' static_scalar
+ //| T_VARIABLE '=' static_scalar,
+ HashSet set = peekVariableSet();
while (true) {
if (token == TokenNameVariable) {
- VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_STATIC_VAR);
if (fMethodVariables != null) {
+ VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_STATIC_VAR);
fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
- }
+ }
+ addVariableSet(set);
getNextToken();
if (token == TokenNameEQUAL) {
getNextToken();
// unset_variable:
// variable
while (true) {
- variable();
+ variable(false);
if (token != TokenNameCOMMA) {
break;
}
}
}
}
- functionDeclarator(methodDecl);
- if (token == TokenNameSEMICOLON) {
- if (!isAbstract) {
- throwSyntaxError("Body declaration expected for method: " + new String(methodDecl.selector));
+ try {
+ pushVariableSet();
+ functionDeclarator(methodDecl);
+ if (token == TokenNameSEMICOLON) {
+ if (!isAbstract) {
+ throwSyntaxError("Body declaration expected for method: " + new String(methodDecl.selector));
+ }
+ getNextToken();
+ return;
}
- getNextToken();
- return;
+ functionBody(methodDecl);
+ } finally {
+ fStackUnassigned.pop();
}
- functionBody(methodDecl);
}
private void functionDeclarator(MethodDeclaration methodDecl) {
// static_scalar
char[] typeIdentifier = null;
if (token == TokenNameIdentifier || token == TokenNameVariable || token == TokenNameAND) {
+ HashSet set = peekVariableSet();
while (true) {
if (token == TokenNameIdentifier) {
typeIdentifier = scanner.getCurrentIdentifierSource();
info.typeIdentifier = typeIdentifier;
fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
}
+ addVariableSet(set);
getNextToken();
if (token == TokenNameEQUAL) {
getNextToken();
checkUnreachable(iState, b);
if (token == TokenNameelseif) {
new_elseif_list(iState);
- }
+ }
new_else_single(iState);
if (token != TokenNameendif) {
throwSyntaxError("'endif' expected.");
int off = b.statements.length - 1;
if (!(b.statements[off] instanceof ReturnStatement) && !(b.statements[off] instanceof ContinueStatement)
&& !(b.statements[off] instanceof BreakStatement)) {
- if (!(b.statements[off] instanceof IfStatement) || !((IfStatement)b.statements[off]).checkUnreachable) {
+ if (!(b.statements[off] instanceof IfStatement) || !((IfStatement) b.statements[off]).checkUnreachable) {
iState.checkUnreachable = false;
}
- }
+ }
}
} else {
if (!(s instanceof ReturnStatement) && !(s instanceof ContinueStatement) && !(s instanceof BreakStatement)) {
- if (!(s instanceof IfStatement) || !((IfStatement)s).checkUnreachable) {
+ if (!(s instanceof IfStatement) || !((IfStatement) s).checkUnreachable) {
iState.checkUnreachable = false;
}
}
case TokenNameVariable:
case TokenNameDOLLAR:
boolean rememberedVar = false;
- Expression lhs = variable();
+// char[] lhsVar = null;
+// if (token==TokenNameVariable) {
+// lhsVar = scanner.getCurrentTokenSource();
+// }
+ Expression lhs = variable(true);
+
switch (token) {
case TokenNameEQUAL:
+// if (lhsVar != null) {
+// addVariableSet(lhsVar);
+// }
getNextToken();
if (token == TokenNameAND) {
getNextToken();
}
}
} else {
- Expression rhs = variable();
+ Expression rhs = variable(false);
if (rhs != null && rhs instanceof FieldReference && lhs != null && lhs instanceof FieldReference) {
// example:
// $var = &$ref;
// variable
//| T_LIST '(' assignment_list ')'
//| /* empty */
- if (token == TokenNameVariable || token == TokenNameDOLLAR) {
- variable();
+ if (token == TokenNameVariable) {
+ variable(true);
+ } else if (token == TokenNameDOLLAR) {
+ variable(false);
} else {
if (token == TokenNamelist) {
getNextToken();
while (true) {
if (token == TokenNameAND) {
getNextToken();
- variable();
+ variable(false);
} else {
expr();
if (token == TokenNameAND) {
getNextToken();
- variable();
+ variable(false);
} else if (token == TokenNameEQUAL_GREATER) {
getNextToken();
if (token == TokenNameAND) {
getNextToken();
- variable();
+ variable(false);
} else {
expr();
}
// }
// } while (true);
// }
- private Expression variable_without_objects() {
+ private Expression variable_without_objects(boolean lefthandside) {
// variable_without_objects:
// reference_variable
// | simple_indirect_reference reference_variable
while (token == TokenNameDOLLAR) {
getNextToken();
}
- return reference_variable();
+ return reference_variable(lefthandside);
}
- private Expression function_call() {
+ private Expression function_call(boolean lefthandside) {
// function_call:
// T_STRING '(' function_call_parameter_list ')'
//| class_constant '(' function_call_parameter_list ')'
getNextToken();
} else {
// static member:
- variable_without_objects();
+ variable_without_objects(false);
}
break;
}
} else {
- ref = variable_without_objects();
+ ref = variable_without_objects(lefthandside);
}
if (token != TokenNameLPAREN) {
if (defineName != null) {
while (true) {
if (token == TokenNameAND) {
getNextToken();
- w_variable();
+ w_variable(false);
} else {
// if (token == TokenNameIdentifier || token ==
// TokenNameVariable
throwSyntaxError("'::' expected after class name (static_member).");
}
getNextToken();
- variable_without_objects();
+ variable_without_objects(false);
}
- private Expression base_variable_with_function_calls() {
+ private Expression base_variable_with_function_calls(boolean lefthandside) {
// base_variable_with_function_calls:
// base_variable
//| function_call
// scanner.phpMode = true;
// }
// if (functionCall) {
- return function_call();
+ return function_call(lefthandside);
// } else {
// base_variable();
// }
while (token == TokenNameDOLLAR) {
getNextToken();
}
- reference_variable();
+ reference_variable(false);
}
return ref;
}
// // '$'
// //| simple_indirect_reference '$'
// }
- private Expression reference_variable() {
+ private Expression reference_variable(boolean lefthandside) {
// reference_variable:
// reference_variable '[' dim_offset ']'
// | reference_variable '{' expr '}'
if (Scanner.TRACE) {
System.out.println("TRACE: reference_variable()");
}
- ref = compound_variable();
+ ref = compound_variable(lefthandside);
while (true) {
if (token == TokenNameLBRACE) {
ref = null;
return ref;
}
- private Expression compound_variable() {
+ private Expression compound_variable(boolean lefthandside) {
// compound_variable:
// T_VARIABLE
// | '$' '{' expr '}'
System.out.println("TRACE: compound_variable()");
}
if (token == TokenNameVariable) {
+ if (!lefthandside) {
+ if (!containsVariableSet()) {
+// reportSyntaxError("The local variable " + new String(scanner.getCurrentIdentifierSource())
+// + " may not have been initialized");
+ problemReporter.uninitializedLocalVariable(new String(scanner.getCurrentIdentifierSource()),scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(),
+ referenceContext, compilationUnit.compilationResult);
+ }
+ } else {
+ addVariableSet();
+ }
FieldReference ref = new FieldReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
getNextToken();
return ref;
System.out.println("TRACE: object_property()");
}
if (token == TokenNameVariable || token == TokenNameDOLLAR) {
- variable_without_objects();
+ variable_without_objects(false);
} else {
object_dim_list();
}
}
private void r_variable() {
- variable();
+ variable(false);
}
- private void w_variable() {
- variable();
+ private void w_variable(boolean lefthandside) {
+ variable(lefthandside);
}
private void rw_variable() {
- variable();
+ variable(false);
}
- private Expression variable() {
+ private Expression variable(boolean lefthandside) {
// variable:
// base_variable_with_function_calls T_OBJECT_OPERATOR
// object_property method_or_not variable_properties
// | base_variable_with_function_calls
- Expression ref = base_variable_with_function_calls();
+ Expression ref = base_variable_with_function_calls(lefthandside);
if (token == TokenNameMINUS_GREATER) {
ref = null;
getNextToken();
throwSyntaxError("'(' expected after keyword 'empty'");
}
getNextToken();
- variable();
+ variable(false);
if (token != TokenNameRPAREN) {
throwSyntaxError("')' expected after keyword 'empty'");
}
throwSyntaxError("Variable expected after keyword 'isset'");
}
while (true) {
- variable();
+ variable(false);
if (token == TokenNameCOMMA) {
getNextToken();
} else {
HashMap fMethodVariables = null;
+ Stack fStackUnassigned = new Stack();
+
//ast stack
final static int AstStackIncrement = 100;
}
}
if (scanner.recordLineSeparator) {
- compilationUnit.compilationResult.lineSeparatorPositions =
- scanner.getLineEnds();
+ compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
}
// check placement anomalies against other kinds of brackets
for (int kind = 0; kind < BracketKinds; kind++) {
//endPosition is just before the ;
}
+
+ public final static String[] GLOBALS = {
+ "$this",
+ "$_COOKIE",
+ "$_ENV",
+ "$_FILES",
+ "$_GET",
+ "$GLOBALS",
+ "$_POST",
+ "$_REQUEST",
+ "$_SESSION",
+ "$_SERVER"
+ };
+ /**
+ *
+ */
+ private void pushVariableSet() {
+ HashSet set =new HashSet();
+ for (int i = 0; i < GLOBALS.length; i++) {
+ set.add(GLOBALS[i]);
+ }
+ fStackUnassigned.push(set);
+ }
+
+ /**
+ * Returns the <i>set of assigned variables </i> returns null if no Set is defined at the current scanner position
+ */
+ private HashSet peekVariableSet() {
+ if (!fStackUnassigned.isEmpty()) {
+ return (HashSet) fStackUnassigned.peek();
+ }
+ return null;
+ }
+
+ /**
+ * add the current identifier source to the <i>set of assigned variables </i>
+ *
+ * @param set
+ */
+ private void addVariableSet(HashSet set) {
+ if (set != null) {
+ set.add(new String(scanner.getCurrentTokenSource()));
+ }
+ }
+
+ /**
+ * add the current identifier source to the <i>set of assigned variables </i>
+ *
+ */
+ private void addVariableSet() {
+ HashSet set = peekVariableSet();
+ if (set != null) {
+ set.add(new String(scanner.getCurrentTokenSource()));
+ }
+ }
+
+ /**
+ * add the current identifier source to the <i>set of assigned variables </i>
+ *
+ */
+ private void addVariableSet(char[] token) {
+ HashSet set = peekVariableSet();
+ if (set != null) {
+ set.add(new String(token));
+ }
+ }
+
+ /**
+ * check if the current identifier source is in the <i>set of assigned variables </i> Returns true, if no set is defined for the
+ * current scanner position
+ *
+ */
+ private boolean containsVariableSet() {
+ HashSet set = peekVariableSet();
+ if (set != null) {
+ return set.contains(new String(scanner.getCurrentTokenSource()));
+ }
+ return true;
+ }
}
\ No newline at end of file