Search in sources :

Example 1 with TokenStreamHiddenTokenFilter

use of antlr.TokenStreamHiddenTokenFilter in project checkstyle by checkstyle.

the class TreeWalker method parse.

/**
     * Static helper method to parses a Java source file.
     *
     * @param contents
     *                contains the contents of the file
     * @return the root of the AST
     * @throws TokenStreamException
     *                 if lexing failed
     * @throws RecognitionException
     *                 if parsing failed
     */
public static DetailAST parse(FileContents contents) throws RecognitionException, TokenStreamException {
    final String fullText = contents.getText().getFullText().toString();
    final Reader reader = new StringReader(fullText);
    final GeneratedJavaLexer lexer = new GeneratedJavaLexer(reader);
    lexer.setFilename(contents.getFileName());
    lexer.setCommentListener(contents);
    lexer.setTreatAssertAsKeyword(true);
    lexer.setTreatEnumAsKeyword(true);
    lexer.setTokenObjectClass("antlr.CommonHiddenStreamToken");
    final TokenStreamHiddenTokenFilter filter = new TokenStreamHiddenTokenFilter(lexer);
    filter.hide(TokenTypes.SINGLE_LINE_COMMENT);
    filter.hide(TokenTypes.BLOCK_COMMENT_BEGIN);
    final GeneratedJavaRecognizer parser = new GeneratedJavaRecognizer(filter);
    parser.setFilename(contents.getFileName());
    parser.setASTNodeClass(DetailAST.class.getName());
    parser.compilationUnit();
    return (DetailAST) parser.getAST();
}
Also used : GeneratedJavaRecognizer(com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaRecognizer) GeneratedJavaLexer(com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaLexer) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) TokenStreamHiddenTokenFilter(antlr.TokenStreamHiddenTokenFilter)

Example 2 with TokenStreamHiddenTokenFilter

use of antlr.TokenStreamHiddenTokenFilter in project cxf by apache.

the class IDLProcessor method process.

public void process() throws ToolException {
    String location = env.get(ToolCorbaConstants.CFG_IDLFILE).toString();
    File file = new File(location).getAbsoluteFile();
    if (!file.exists()) {
        throw new ToolException("IDL file " + file.getName() + " doesn't exist");
    }
    try {
        URL orig = file.toURI().toURL();
        DefaultIncludeResolver includeResolver = getDefaultIncludeResolver(file.getParentFile());
        DefineState defineState = new DefineState(new HashMap<String, String>());
        preprocessor = new IdlPreprocessorReader(orig, location, includeResolver, defineState);
        IDLLexer lexer = new IDLLexer(new java.io.LineNumberReader(preprocessor));
        lexer.setTokenObjectClass("antlr.CommonHiddenStreamToken");
        TokenStreamHiddenTokenFilter filter = new TokenStreamHiddenTokenFilter(lexer);
        filter.discard(IDLTokenTypes.WS);
        filter.hide(IDLTokenTypes.SL_COMMENT);
        filter.hide(IDLTokenTypes.ML_COMMENT);
        parser = new IDLParser(filter);
        parser.setASTNodeClass("antlr.CommonASTWithHiddenTokens");
        parser.specification();
    } catch (Exception ex) {
        throw new ToolException(ex);
    }
}
Also used : DefaultIncludeResolver(org.apache.cxf.tools.corba.idlpreprocessor.DefaultIncludeResolver) TokenStreamHiddenTokenFilter(antlr.TokenStreamHiddenTokenFilter) URL(java.net.URL) IdlPreprocessorReader(org.apache.cxf.tools.corba.idlpreprocessor.IdlPreprocessorReader) ToolException(org.apache.cxf.tools.common.ToolException) DefineState(org.apache.cxf.tools.corba.idlpreprocessor.DefineState) ToolException(org.apache.cxf.tools.common.ToolException) File(java.io.File)

Aggregations

TokenStreamHiddenTokenFilter (antlr.TokenStreamHiddenTokenFilter)2 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)1 GeneratedJavaLexer (com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaLexer)1 GeneratedJavaRecognizer (com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaRecognizer)1 File (java.io.File)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 ToolException (org.apache.cxf.tools.common.ToolException)1 DefaultIncludeResolver (org.apache.cxf.tools.corba.idlpreprocessor.DefaultIncludeResolver)1 DefineState (org.apache.cxf.tools.corba.idlpreprocessor.DefineState)1 IdlPreprocessorReader (org.apache.cxf.tools.corba.idlpreprocessor.IdlPreprocessorReader)1