Search in sources :

Example 6 with TokenStreamException

use of antlr.TokenStreamException in project checkstyle by checkstyle.

the class TreeWalker method processFiltered.

@Override
protected void processFiltered(File file, List<String> lines) throws CheckstyleException {
    // check if already checked and passed the file
    if (CommonUtils.matchesFileExtension(file, getFileExtensions())) {
        final String msg = "%s occurred during the analysis of file %s.";
        final String fileName = file.getPath();
        try {
            final FileText text = FileText.fromLines(file, lines);
            final FileContents contents = new FileContents(text);
            final DetailAST rootAST = parse(contents);
            getMessageCollector().reset();
            walk(rootAST, contents, AstState.ORDINARY);
            final DetailAST astWithComments = appendHiddenCommentNodes(rootAST);
            walk(astWithComments, contents, AstState.WITH_COMMENTS);
        } catch (final TokenStreamRecognitionException tre) {
            final String exceptionMsg = String.format(Locale.ROOT, msg, "TokenStreamRecognitionException", fileName);
            throw new CheckstyleException(exceptionMsg, tre);
        } catch (RecognitionException | TokenStreamException ex) {
            final String exceptionMsg = String.format(Locale.ROOT, msg, ex.getClass().getSimpleName(), fileName);
            throw new CheckstyleException(exceptionMsg, ex);
        }
    }
}
Also used : TokenStreamException(antlr.TokenStreamException) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) FileText(com.puppycrawl.tools.checkstyle.api.FileText) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) RecognitionException(antlr.RecognitionException)

Example 7 with TokenStreamException

use of antlr.TokenStreamException in project checkstyle by checkstyle.

the class AstTreeStringPrinter method parseFileText.

/**
     * Parse a text and return the parse tree.
     * @param text the text to parse.
     * @param withComments true to include comment nodes to the tree
     * @return the root node of the parse tree.
     * @throws CheckstyleException if the file is not a Java source.
     */
private static DetailAST parseFileText(FileText text, boolean withComments) throws CheckstyleException {
    final FileContents contents = new FileContents(text);
    final DetailAST result;
    try {
        if (withComments) {
            result = TreeWalker.parseWithComments(contents);
        } else {
            result = TreeWalker.parse(contents);
        }
    } catch (RecognitionException | TokenStreamException ex) {
        final String exceptionMsg = String.format(Locale.ROOT, "%s occurred during the analysis of file %s.", ex.getClass().getSimpleName(), text.getFile().getPath());
        throw new CheckstyleException(exceptionMsg, ex);
    }
    return result;
}
Also used : TokenStreamException(antlr.TokenStreamException) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) RecognitionException(antlr.RecognitionException)

Example 8 with TokenStreamException

use of antlr.TokenStreamException in project groovy-core by groovy.

the class GenericsUtils method parseClassNodesFromString.

public static ClassNode[] parseClassNodesFromString(final String option, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage) {
    GroovyLexer lexer = new GroovyLexer(new StringReader("DummyNode<" + option + ">"));
    final GroovyRecognizer rn = GroovyRecognizer.make(lexer);
    try {
        rn.classOrInterfaceType(true);
        final AtomicReference<ClassNode> ref = new AtomicReference<ClassNode>();
        AntlrParserPlugin plugin = new AntlrParserPlugin() {

            @Override
            public ModuleNode buildAST(final SourceUnit sourceUnit, final ClassLoader classLoader, final Reduction cst) throws ParserException {
                ref.set(makeTypeWithArguments(rn.getAST()));
                return null;
            }
        };
        plugin.buildAST(null, null, null);
        ClassNode parsedNode = ref.get();
        // the returned node is DummyNode<Param1, Param2, Param3, ...)
        GenericsType[] parsedNodeGenericsTypes = parsedNode.getGenericsTypes();
        if (parsedNodeGenericsTypes == null) {
            return null;
        }
        ClassNode[] signature = new ClassNode[parsedNodeGenericsTypes.length];
        for (int i = 0; i < parsedNodeGenericsTypes.length; i++) {
            final GenericsType genericsType = parsedNodeGenericsTypes[i];
            signature[i] = resolveClassNode(sourceUnit, compilationUnit, mn, usage, genericsType.getType());
        }
        return signature;
    } catch (RecognitionException e) {
        sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
    } catch (TokenStreamException e) {
        sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
    } catch (ParserException e) {
        sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
    }
    return null;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ParserException(org.codehaus.groovy.syntax.ParserException) IncorrectTypeHintException(groovy.transform.stc.IncorrectTypeHintException) AtomicReference(java.util.concurrent.atomic.AtomicReference) SourceUnit(org.codehaus.groovy.control.SourceUnit) TokenStreamException(antlr.TokenStreamException) Reduction(org.codehaus.groovy.syntax.Reduction) AntlrParserPlugin(org.codehaus.groovy.antlr.AntlrParserPlugin) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) StringReader(java.io.StringReader) GenericsType(org.codehaus.groovy.ast.GenericsType) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer) RecognitionException(antlr.RecognitionException)

Example 9 with TokenStreamException

use of antlr.TokenStreamException in project querydsl by querydsl.

the class IntegrationBase method query.

@Override
protected QueryHelper<?> query() {
    return new QueryHelper<Void>(HQLTemplates.DEFAULT) {

        @Override
        public void parse() throws RecognitionException, TokenStreamException {
            try {
                System.out.println("query : " + toString().replace('\n', ' '));
                JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT);
                serializer.serialize(getMetadata(), false, null);
                Query query = session.createQuery(serializer.toString());
                HibernateUtil.setConstants(query, serializer.getConstantToLabel(), getMetadata().getParams());
                query.list();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } finally {
                System.out.println();
            }
        }
    };
}
Also used : HibernateQuery(com.querydsl.jpa.hibernate.HibernateQuery) Query(org.hibernate.Query) RecognitionException(antlr.RecognitionException) TokenStreamException(antlr.TokenStreamException)

Aggregations

RecognitionException (antlr.RecognitionException)9 TokenStreamException (antlr.TokenStreamException)9 TokenStreamRecognitionException (antlr.TokenStreamRecognitionException)4 GroovyLexer (org.codehaus.groovy.antlr.parser.GroovyLexer)4 GroovyRecognizer (org.codehaus.groovy.antlr.parser.GroovyRecognizer)4 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)2 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)2 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)2 IncorrectTypeHintException (groovy.transform.stc.IncorrectTypeHintException)2 StringReader (java.io.StringReader)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 AntlrParserPlugin (org.codehaus.groovy.antlr.AntlrParserPlugin)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 GenericsType (org.codehaus.groovy.ast.GenericsType)2 SourceUnit (org.codehaus.groovy.control.SourceUnit)2 ParserException (org.codehaus.groovy.syntax.ParserException)2 Reduction (org.codehaus.groovy.syntax.Reduction)2 CharStreamException (antlr.CharStreamException)1 CharStreamIOException (antlr.CharStreamIOException)1 CommonToken (antlr.CommonToken)1