import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
-import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
/**
* @author khartlage
}
public void checkParse(
char[] source,
- String expectedSyntaxErrorDiagnosis,
- String testName) {
+ String expectedSyntaxErrorDiagnosis) {
+// String testName) {
UnitParser parser =
new UnitParser(
//new CompilerOptions(getCompilerOptions()),
new DefaultProblemFactory(Locale.getDefault())));
- ICompilationUnit sourceUnit = new CompilationUnit(source, testName, null);
+ ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
}
assertEquals(
- "Invalid syntax error diagnosis" + testName,
+ "Invalid syntax error diagnosis",
expectedSyntaxErrorDiagnosis,
computedSyntaxErrorDiagnosis);
}
-
public void test01() {
String s =
+ " ";
+
+ String expectedSyntaxErrorDiagnosis =
+ "";
+
+ String testName = "<test01>";
+ checkParse(
+ s.toCharArray(),
+ expectedSyntaxErrorDiagnosis);
+// testName);
+ }
+ public void test02() {
+ String s =
+ "class test { \n"+
+ " function f0() \n"+
+ " { \n"+
+ " } \n"+
+ "} \n";
+
+ String expectedSyntaxErrorDiagnosis =
+ "";
+
+ String testName = "<test02>";
+ checkParse(
+ s.toCharArray(),
+ expectedSyntaxErrorDiagnosis);
+// testName);
+ }
+
+ public void test97() {
+ String s =
"class class { \n"+
" function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n"+
" { \n"+
"Parse error \"Class name expected after keyword \'class\'.\"\n" +
"----------\n";
- String testName = "<test1>";
checkParse(
s.toCharArray(),
- expectedSyntaxErrorDiagnosis,
- testName);
+ expectedSyntaxErrorDiagnosis);
+// testName);
}
-
- public void test02() {
+ public void test98() {
String s =
"if(!$result = mysql_query($sql)) return(array());\n";
String expectedSyntaxErrorDiagnosis =
"";
- String testName = "<test2>";
checkParse(
s.toCharArray(),
- expectedSyntaxErrorDiagnosis,
- testName);
+ expectedSyntaxErrorDiagnosis);
+// testName);
}
- public void test03() {
+ public void test99() {
String s =
"class test { \n"+
" murks; \n"+
String expectedSyntaxErrorDiagnosis =
"";
- String testName = "<test3>";
checkParse(
s.toCharArray(),
- expectedSyntaxErrorDiagnosis,
- testName);
+ expectedSyntaxErrorDiagnosis);
+// testName);
}
}
/**
* Create marker for the parse error
*/
- private void setMarker(String message, int charStart, int charEnd, int errorLevel) throws CoreException {
- setMarker(fileToParse, message, charStart, charEnd, errorLevel);
- }
+// private void setMarker(String message, int charStart, int charEnd, int errorLevel) throws CoreException {
+// setMarker(fileToParse, message, charStart, charEnd, errorLevel);
+// }
/**
* This method will throw the SyntaxError.
* @throws SyntaxError the error raised
*/
private void throwSyntaxError(String error) {
-
- // if (str.length() < chIndx) {
- // chIndx--;
- // }
- // // read until end-of-line
- // int eol = chIndx;
- // while (str.length() > eol) {
- // ch = str.charAt(eol++);
- // if (ch == '\n') {
- // eol--;
- // break;
- // }
- // }
- // throw new SyntaxError(
- // rowCount,
- // chIndx - columnCount + 1,
- // str.substring(columnCount, eol),
- // error);
- throw new SyntaxError(1, 1, "", error);
+ int problemStartPosition = scanner.getCurrentTokenStartPosition();
+ int problemEndPosition = scanner.getCurrentTokenEndPosition();
+ reportSyntaxError(error,problemStartPosition,problemEndPosition);
}
/**
* @param error the error message
* @throws SyntaxError the error raised
*/
- private void throwSyntaxError(String error, int startRow) {
- throw new SyntaxError(startRow, 0, " ", error);
- }
+// private void throwSyntaxError(String error, int startRow) {
+// throw new SyntaxError(startRow, 0, " ", error);
+// }
private void reportSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext, compilationUnit.compilationResult);
throw err;
} else {
// setMarker(err.getMessage(), err.getLine(), ERROR);
- setMarker(err.getMessage(), scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), ERROR);
+// setMarker(err.getMessage(), scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), ERROR);
}
// if an error occured,
// try to find keywords 'class' or 'function'
return;
} catch (SyntaxError sytaxErr1) {
// setMarker(sytaxErr1.getMessage(), sytaxErr1.getLine(), ERROR);
- setMarker(sytaxErr1.getMessage(), scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), ERROR);
+// setMarker(sytaxErr1.getMessage(), scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), ERROR);
try {
// if an error occured,
// try to find keywords 'class' or 'function'
}
} catch (SyntaxError sytaxErr2) {
// setMarker(sytaxErr2.getMessage(), sytaxErr2.getLine(), ERROR);
- setMarker(sytaxErr2.getMessage(), scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), ERROR);
+// setMarker(sytaxErr2.getMessage(), scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), ERROR);
return;
}
}
}
} catch (CoreException e) {
} catch (SyntaxError sytaxErr) {
- try {
- // setMarker(sytaxErr.getMessage(), sytaxErr.getLine(), ERROR);
- setMarker(sytaxErr.getMessage(), scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), ERROR);
- } catch (CoreException e) {
- }
+// try {
+// // setMarker(sytaxErr.getMessage(), sytaxErr.getLine(), ERROR);
+// setMarker(sytaxErr.getMessage(), scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), ERROR);
+// } catch (CoreException e) {
+// }
}
}
//identifier
//identifier 'extends' identifier
- if (token == TokenNameIdentifier) {
+
+ if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
typeDecl.name = scanner.getCurrentIdentifierSource();
+ if (token > TokenNameKEYWORD) {
+ reportSyntaxError(
+ "Don't use keyword for class declaration [" + scanner.toStringAction(token) + "].",
+ typeDecl.sourceStart,
+ typeDecl.sourceEnd);
+ }
getNextToken();
if (token == TokenNameextends) {
do {
getNextToken();
} else {
reportSyntaxError("Class name expected after keyword 'extends'.", scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
- // throwSyntaxError("ClassDeclaration name expected after keyword 'extends'.");
}
} while (token == TokenNameCOMMA);
}
typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
- if (token > TokenNameKEYWORD) {
- typeDecl.name = scanner.getCurrentIdentifierSource();
- reportSyntaxError(
- "Don't use keyword for class declaration [" + scanner.toStringAction(token) + "].",
- typeDecl.sourceStart,
- typeDecl.sourceEnd);
- // throwSyntaxError("Don't use keyword for class declaration [" + token + "].");
- }
typeDecl.name = new char[] { ' ' };
reportSyntaxError("Class name expected after keyword 'class'.", typeDecl.sourceStart, typeDecl.sourceEnd);
// throwSyntaxError("ClassDeclaration name expected after keyword 'class'.");
if (token == TokenNamecase) {
getNextToken();
expression(); //constant();
- if (token == TokenNameCOLON) {
+ if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
getNextToken();
if (token == TokenNamecase || token == TokenNamedefault) { // empty case statement ?
continue;
}
statementList();
- } else if (token == TokenNameSEMICOLON) {
- // setMarker(
- // "':' expected after 'case' keyword (Found token: "
- // + scanner.toStringAction(token)
- // + ")",
- // rowCount,
- // PHPParser.INFO);
- setMarker(
- "':' expected after 'case' keyword (Found token: " + scanner.toStringAction(token) + ")",
- scanner.getCurrentTokenStartPosition(),
- scanner.getCurrentTokenEndPosition(),
- INFO);
- getNextToken();
- if (token == TokenNamecase) { // empty case statement ?
- continue;
- }
- statementList();
- } else {
+ }
+// else if (token == TokenNameSEMICOLON) {
+// setMarker(
+// "':' expected after 'case' keyword (Found token: " + scanner.toStringAction(token) + ")",
+// scanner.getCurrentTokenStartPosition(),
+// scanner.getCurrentTokenEndPosition(),
+// INFO);
+// getNextToken();
+// if (token == TokenNamecase) { // empty case statement ?
+// continue;
+// }
+// statementList();
+// }
+ else {
throwSyntaxError("':' character after 'case' constant expected (Found token: " + scanner.toStringAction(token) + ")");
}
} else { // TokenNamedefault
// + "' as variable name.",
// rowCount,
// PHPParser.INFO);
- setMarker(
- "Avoid using keyword '" + new String(ident) + "' as variable name.",
- scanner.getCurrentTokenStartPosition(),
- scanner.getCurrentTokenEndPosition(),
- INFO);
+// setMarker(
+// "Avoid using keyword '" + new String(ident) + "' as variable name.",
+// scanner.getCurrentTokenStartPosition(),
+// scanner.getCurrentTokenEndPosition(),
+// INFO);
}
switch (token) {
case TokenNameVariable :