Another point of no return - remove fall back parsing to Java 1.3 syntax.

This commit is contained in:
Oliver Burn 2007-12-20 06:43:52 +00:00
parent f38d8abd42
commit 7065b0d9dc
5 changed files with 3 additions and 146 deletions

View File

@ -19,7 +19,6 @@
package com.puppycrawl.tools.checkstyle;
import antlr.RecognitionException;
import antlr.TokenStream;
import antlr.TokenStreamException;
import antlr.TokenStreamRecognitionException;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
@ -58,55 +57,6 @@ import org.apache.commons.logging.LogFactory;
public final class TreeWalker
extends AbstractFileSetCheck
{
/**
* Overrides ANTLR error reporting so we completely control
* checkstyle's output during parsing. This is important because
* we try parsing with several grammers (with/without support for
* <code>assert</code>). We must not write any error messages when
* parsing fails because with the next grammar it might succeed
* and the user will be confused.
*/
private static final class SilentJavaRecognizer
extends GeneratedJavaRecognizer
{
/**
* Creates a new <code>SilentJavaRecognizer</code> instance.
*
* @param aLexer the tokenstream the recognizer operates on.
*/
public SilentJavaRecognizer(TokenStream aLexer)
{
super(aLexer);
}
/**
* Parser error-reporting function, does nothing.
* @param aRex the exception to be reported
*/
@Override
public void reportError(RecognitionException aRex)
{
}
/**
* Parser error-reporting function, does nothing.
* @param aMsg the error message
*/
@Override
public void reportError(String aMsg)
{
}
/**
* Parser warning-reporting function, does nothing.
* @param aMsg the error message
*/
@Override
public void reportWarning(String aMsg)
{
}
}
/** default distance between tab stops */
private static final int DEFAULT_TAB_WIDTH = 8;
@ -521,52 +471,16 @@ public final class TreeWalker
*/
public static DetailAST parse(FileContents aContents)
throws RecognitionException, TokenStreamException
{
DetailAST rootAST = null;
try {
rootAST = parse(aContents, true, true, true);
}
catch (final RecognitionException exception) {
try {
rootAST = parse(aContents, true, true, false);
}
catch (final RecognitionException exception2) {
rootAST = parse(aContents, false, false, false);
}
}
return rootAST;
}
/**
* Static helper method to parses a Java source file with a given
* lexer class and parser class.
* @param aContents contains the contents of the file
* @param aSilentlyConsumeErrors flag to output errors to stdout or not
* @param aTreatAssertAsKeyword flag to treat 'assert' as a keyowrd
* @param aTreatEnumAsKeyword flag to treat 'enum' as a keyowrd
* @throws TokenStreamException if lexing failed
* @throws RecognitionException if parsing failed
* @return the root of the AST
*/
private static DetailAST parse(
FileContents aContents,
boolean aSilentlyConsumeErrors,
boolean aTreatAssertAsKeyword,
boolean aTreatEnumAsKeyword)
throws RecognitionException, TokenStreamException
{
final Reader sar = new StringArrayReader(aContents.getLines());
final GeneratedJavaLexer lexer = new GeneratedJavaLexer(sar);
lexer.setFilename(aContents.getFilename());
lexer.setCommentListener(aContents);
lexer.setTreatAssertAsKeyword(aTreatAssertAsKeyword);
lexer.setTreatEnumAsKeyword(aTreatEnumAsKeyword);
lexer.setTreatAssertAsKeyword(true);
lexer.setTreatEnumAsKeyword(true);
final GeneratedJavaRecognizer parser =
aSilentlyConsumeErrors
? new SilentJavaRecognizer(lexer)
: new GeneratedJavaRecognizer(lexer);
new GeneratedJavaRecognizer(lexer);
parser.setFilename(aContents.getFilename());
parser.setASTNodeClass(DetailAST.class.getName());
parser.compilationUnit();

View File

@ -1,24 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
// Test case file for checkstyle.
// Created: 2001
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
import java.util.Properties;
/**
* Test case for assert handling fallback.
* This file uses "assert" is as an identifier, so the default grammar
* fails and the JDK 1.3 grammar has to be used.
* @author Lars Kühne
**/
class InputAssertIdentifier
{
/** test method **/
void assert()
{
int assert = 1;
int a = assert;
final Properties p = null;
}
}

View File

@ -1,11 +0,0 @@
package com.puppycrawl.tools.checkstyle.grammars;
/**
* @author Michael Studman
* @see
*/
public class Post13KeywordsAsIdentifiersOK
{
public int assert = 0;
public int enum = 0;
}

View File

@ -15,7 +15,6 @@ public class AllTests {
new TestSuite("Test for com.puppycrawl.tools.checkstyle.grammars");
//$JUnit-BEGIN$
suite.addTest(new TestSuite(GeneratedJava14LexerTest.class));
suite.addTest(new TestSuite(Post13KeywordsAsIdentifiersOKTest.class));
suite.addTest(new TestSuite(HexFloatsTest.class));
suite.addTest(new TestSuite(EmbeddedNullCharTest.class));
suite.addTest(new TestSuite(VarargTest.class));

View File

@ -1,21 +0,0 @@
package com.puppycrawl.tools.checkstyle.grammars;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck;
import java.io.IOException;
/**
* @author Michael Studman
*/
public class Post13KeywordsAsIdentifiersOKTest
extends BaseCheckTestCase
{
public void testUnexpectedChar() throws IOException, Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(MemberNameCheck.class);
verify(checkConfig, getPath("grammars/Post13KeywordsAsIdentifiersOK.java"), new String[0]);
}
}