forked from phoedos/pmd
More assert lookahead stuff
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@2252 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -1,4 +1,9 @@
|
||||
/**
|
||||
*
|
||||
* Added in support for assert as a name using lookaheads
|
||||
*
|
||||
* Tom Copeland, 09/03
|
||||
* ===================================================================
|
||||
* Copied over the changes made by Andrea Gini and Marco Savard to
|
||||
* support JDK 1.4 language constructs, i.e., asserts.
|
||||
* See the java1_4c.jj distributed in the javacc2.1/examples/JavaGrammers directory.
|
||||
@ -60,6 +65,12 @@ PARSER_BEGIN(JavaParser)
|
||||
package net.sourceforge.pmd.ast;
|
||||
public class JavaParser {
|
||||
|
||||
private boolean usingAssertAsIdentifier;
|
||||
|
||||
public void setAssertAsIdentifier() {
|
||||
this.usingAssertAsIdentifier = true;
|
||||
}
|
||||
|
||||
private boolean assertLookahead() {
|
||||
return getToken(1).image.equals("assert");
|
||||
}
|
||||
@ -976,7 +987,9 @@ void ArrayDimsAndInits() :
|
||||
void Statement() :
|
||||
{}
|
||||
{
|
||||
LOOKAHEAD( { assertLookahead() } )
|
||||
LOOKAHEAD( {
|
||||
assertLookahead()
|
||||
} )
|
||||
AssertStatement()
|
||||
|
|
||||
LOOKAHEAD(2)
|
||||
@ -1193,6 +1206,9 @@ void TryStatement() :
|
||||
|
||||
void AssertStatement() :
|
||||
{
|
||||
if (usingAssertAsIdentifier) {
|
||||
throw new ParseException("Can't use 'assert' as a keyword when running in JDK 1.3 mode!");
|
||||
}
|
||||
Token tok;
|
||||
}
|
||||
{
|
||||
|
@ -3,20 +3,44 @@ package test.net.sourceforge.pmd.ast;
|
||||
import junit.framework.TestCase;
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.ast.JavaParser;
|
||||
import net.sourceforge.pmd.ast.ParseException;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
public class AssertTest extends TestCase {
|
||||
|
||||
public void testAssertAsKeyword() {
|
||||
new JavaParser(new StringReader(TEST1)).CompilationUnit();
|
||||
new JavaParser(new StringReader(TEST1)).CompilationUnit();
|
||||
new JavaParser(new StringReader(TEST1)).CompilationUnit();
|
||||
public void testAssertAsKeywordVariantsSucceedWith1_4() {
|
||||
new JavaParser(new StringReader(TEST1)).CompilationUnit();
|
||||
new JavaParser(new StringReader(TEST2)).CompilationUnit();
|
||||
new JavaParser(new StringReader(TEST3)).CompilationUnit();
|
||||
new JavaParser(new StringReader(TEST4)).CompilationUnit();
|
||||
}
|
||||
|
||||
public void testAssertAsName() {
|
||||
new JavaParser(new StringReader(TEST5)).CompilationUnit();
|
||||
/*
|
||||
TODO
|
||||
public void testAssertAsIdentifierFailsWith1_4() {
|
||||
try {
|
||||
new JavaParser(new StringReader(TEST5)).CompilationUnit();
|
||||
throw new RuntimeException("Usage of assert as identifier should have failed with 1.4");
|
||||
} catch (ParseException pe) {
|
||||
// cool
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public void testAssertAsIdentifierSucceedsWith1_3() {
|
||||
new JavaParser(new StringReader(TEST6)).CompilationUnit();
|
||||
}
|
||||
|
||||
public void testAssertAsKeywordFailsWith1_3() {
|
||||
try {
|
||||
JavaParser jp = new JavaParser(new StringReader(TEST7));
|
||||
jp.setAssertAsIdentifier();
|
||||
jp.CompilationUnit();
|
||||
throw new RuntimeException("Usage of assert as keyword should have failed with 1.3");
|
||||
} catch (ParseException pe) {
|
||||
// cool
|
||||
}
|
||||
}
|
||||
|
||||
private static final String TEST1 =
|
||||
@ -51,4 +75,18 @@ public class AssertTest extends TestCase {
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" int assert = 2;" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST6 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" int assert = 2;" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST7 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" void foo() {" + PMD.EOL +
|
||||
" assert (x>2) : \"hi!\";" + PMD.EOL +
|
||||
" }" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import net.sourceforge.pmd.ast.JavaParser;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
|
||||
public class JLS1_1 implements JLSVersion {
|
||||
|
||||
public JavaParser createParser(InputStream in) {
|
||||
throw new RuntimeException("Not yet implemented");
|
||||
}
|
||||
|
||||
public JavaParser createParser(Reader in) {
|
||||
throw new RuntimeException("Not yet implemented");
|
||||
}
|
||||
}
|
@ -23,14 +23,14 @@ public class PMD {
|
||||
|
||||
public static final String EOL = System.getProperty("line.separator", "\n");
|
||||
|
||||
private JLSVersion jlsVersion;
|
||||
private TargetJDKVersion targetJDKVersion;
|
||||
|
||||
public PMD() {
|
||||
jlsVersion = new JLS1_4();
|
||||
targetJDKVersion = new TargetJDK1_4();
|
||||
}
|
||||
|
||||
public PMD(JLSVersion jlsVersion) {
|
||||
this.jlsVersion = jlsVersion;
|
||||
public PMD(TargetJDKVersion targetJDKVersion) {
|
||||
this.targetJDKVersion = targetJDKVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -40,7 +40,7 @@ public class PMD {
|
||||
*/
|
||||
public void processFile(Reader reader, RuleSet ruleSet, RuleContext ctx) throws PMDException {
|
||||
try {
|
||||
JavaParser parser = jlsVersion.createParser(reader);
|
||||
JavaParser parser = targetJDKVersion.createParser(reader);
|
||||
ASTCompilationUnit c = parser.CompilationUnit();
|
||||
Thread.yield();
|
||||
SymbolFacade stb = new SymbolFacade();
|
||||
|
21
pmd/src/net/sourceforge/pmd/TargetJDK1_3.java
Normal file
21
pmd/src/net/sourceforge/pmd/TargetJDK1_3.java
Normal file
@ -0,0 +1,21 @@
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import net.sourceforge.pmd.ast.JavaParser;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
|
||||
public class TargetJDK1_3 implements TargetJDKVersion {
|
||||
|
||||
public JavaParser createParser(InputStream in) {
|
||||
JavaParser jp = new JavaParser(in);
|
||||
jp.setAssertAsIdentifier();
|
||||
return jp;
|
||||
}
|
||||
|
||||
public JavaParser createParser(Reader in) {
|
||||
JavaParser jp = new JavaParser(in);
|
||||
jp.setAssertAsIdentifier();
|
||||
return jp;
|
||||
}
|
||||
}
|
@ -5,7 +5,8 @@ import net.sourceforge.pmd.ast.JavaParser;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
|
||||
public class JLS1_4 implements JLSVersion {
|
||||
public class TargetJDK1_4 implements TargetJDKVersion {
|
||||
|
||||
public JavaParser createParser(InputStream in) {
|
||||
return new JavaParser(in);
|
||||
}
|
@ -5,7 +5,7 @@ import net.sourceforge.pmd.ast.JavaParser;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
|
||||
public interface JLSVersion {
|
||||
public interface TargetJDKVersion {
|
||||
public JavaParser createParser(InputStream in);
|
||||
public JavaParser createParser(Reader in);
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user