Search in sources :

Example 1 with RecognitionException

use of antlr.RecognitionException in project groovy by apache.

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 2 with RecognitionException

use of antlr.RecognitionException in project groovy by apache.

the class ClassicGroovyTestGeneratorHelper method parse.

/** run the JSR parser implementation over the supplied source text*/
public void parse(String theSrcText, String testName) throws Exception {
    System.out.println("-------------------------------");
    System.out.println("  " + testName);
    System.out.println("-------------------------------");
    try {
        Reader reader = new BufferedReader(new StringReader(theSrcText));
        GroovyRecognizer recognizer = GroovyRecognizer.make(reader);
        recognizer.compilationUnit();
        System.out.println(decorateWithLineNumbers(theSrcText));
    } catch (RecognitionException parseException) {
        System.out.println(decorateWithLineNumbersAndErrorMessage(theSrcText, parseException));
        throw parseException;
    }
    System.out.println("-------------------------------");
}
Also used : BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer) RecognitionException(antlr.RecognitionException)

Example 3 with RecognitionException

use of antlr.RecognitionException in project groovy by apache.

the class AntlrParserPlugin method transformCSTIntoAST.

protected void transformCSTIntoAST(SourceUnit sourceUnit, Reader reader, SourceBuffer sourceBuffer) throws CompilationFailedException {
    ast = null;
    setController(sourceUnit);
    // TODO find a way to inject any GroovyLexer/GroovyRecognizer
    UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(reader, sourceBuffer);
    UnicodeLexerSharedInputState inputState = new UnicodeLexerSharedInputState(unicodeReader);
    GroovyLexer lexer = new GroovyLexer(inputState);
    unicodeReader.setLexer(lexer);
    GroovyRecognizer parser = GroovyRecognizer.make(lexer);
    parser.setSourceBuffer(sourceBuffer);
    tokenNames = parser.getTokenNames();
    parser.setFilename(sourceUnit.getName());
    // start parsing at the compilationUnit rule
    try {
        parser.compilationUnit();
    } catch (TokenStreamRecognitionException tsre) {
        RecognitionException e = tsre.recog;
        SyntaxException se = new SyntaxException(e.getMessage(), e, e.getLine(), e.getColumn());
        se.setFatal(true);
        sourceUnit.addError(se);
    } catch (RecognitionException e) {
        SyntaxException se = new SyntaxException(e.getMessage(), e, e.getLine(), e.getColumn());
        se.setFatal(true);
        sourceUnit.addError(se);
    } catch (TokenStreamException e) {
        sourceUnit.addException(e);
    }
    ast = parser.getAST();
}
Also used : TokenStreamException(antlr.TokenStreamException) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer) RecognitionException(antlr.RecognitionException) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException)

Example 4 with RecognitionException

use of antlr.RecognitionException in project sonarqube by SonarSource.

the class ValidWhenLexer method nextToken.

public Token nextToken() throws TokenStreamException {
    Token theRetToken = null;
    tryAgain: for (; ; ) {
        Token _token = null;
        int _ttype = Token.INVALID_TYPE;
        resetText();
        try {
            // for char stream error handling
            try {
                // for lexical error handling
                switch(LA(1)) {
                    case '\t':
                    case '\n':
                    case '\r':
                    case ' ':
                        {
                            mWS(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    case '-':
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                        {
                            mDECIMAL_LITERAL(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    case '"':
                    case '\'':
                        {
                            mSTRING_LITERAL(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    case '[':
                        {
                            mLBRACKET(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    case ']':
                        {
                            mRBRACKET(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    case '(':
                        {
                            mLPAREN(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    case ')':
                        {
                            mRPAREN(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    case '*':
                        {
                            mTHIS(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    case '.':
                    case '_':
                    case 'a':
                    case 'b':
                    case 'c':
                    case 'd':
                    case 'e':
                    case 'f':
                    case 'g':
                    case 'h':
                    case 'i':
                    case 'j':
                    case 'k':
                    case 'l':
                    case 'm':
                    case 'n':
                    case 'o':
                    case 'p':
                    case 'q':
                    case 'r':
                    case 's':
                    case 't':
                    case 'u':
                    case 'v':
                    case 'w':
                    case 'x':
                    case 'y':
                    case 'z':
                        {
                            mIDENTIFIER(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    case '=':
                        {
                            mEQUALSIGN(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    case '!':
                        {
                            mNOTEQUALSIGN(true);
                            theRetToken = _returnToken;
                            break;
                        }
                    default:
                        if ((LA(1) == '0') && (LA(2) == 'x')) {
                            mHEX_LITERAL(true);
                            theRetToken = _returnToken;
                        } else if ((LA(1) == '<') && (LA(2) == '=')) {
                            mLESSEQUALSIGN(true);
                            theRetToken = _returnToken;
                        } else if ((LA(1) == '>') && (LA(2) == '=')) {
                            mGREATEREQUALSIGN(true);
                            theRetToken = _returnToken;
                        } else if ((LA(1) == '0') && (true)) {
                            mOCTAL_LITERAL(true);
                            theRetToken = _returnToken;
                        } else if ((LA(1) == '<') && (true)) {
                            mLESSTHANSIGN(true);
                            theRetToken = _returnToken;
                        } else if ((LA(1) == '>') && (true)) {
                            mGREATERTHANSIGN(true);
                            theRetToken = _returnToken;
                        } else {
                            if (LA(1) == EOF_CHAR) {
                                uponEOF();
                                _returnToken = makeToken(Token.EOF_TYPE);
                            } else {
                                throw new NoViableAltForCharException((char) LA(1), getFilename(), getLine(), getColumn());
                            }
                        }
                }
                // found SKIP token
                if (_returnToken == null)
                    continue tryAgain;
                _ttype = _returnToken.getType();
                _ttype = testLiteralsTable(_ttype);
                _returnToken.setType(_ttype);
                return _returnToken;
            } catch (RecognitionException e) {
                throw new TokenStreamRecognitionException(e);
            }
        } catch (CharStreamException cse) {
            if (cse instanceof CharStreamIOException) {
                throw new TokenStreamIOException(((CharStreamIOException) cse).io);
            } else {
                throw new TokenStreamException(cse.getMessage());
            }
        }
    }
}
Also used : TokenStreamException(antlr.TokenStreamException) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) NoViableAltForCharException(antlr.NoViableAltForCharException) CharStreamException(antlr.CharStreamException) CharStreamIOException(antlr.CharStreamIOException) Token(antlr.Token) CommonToken(antlr.CommonToken) TokenStreamIOException(antlr.TokenStreamIOException) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) RecognitionException(antlr.RecognitionException)

Example 5 with RecognitionException

use of antlr.RecognitionException in project querydsl by querydsl.

the class JPAIntegrationBase method query.

@Override
protected QueryHelper<?> query() {
    return new QueryHelper<Void>(templates) {

        @Override
        public void parse() throws RecognitionException, TokenStreamException {
            JPQLSerializer serializer = new JPQLSerializer(templates);
            serializer.serialize(getMetadata(), false, null);
            Query query = em.createQuery(serializer.toString());
            JPAUtil.setConstants(query, serializer.getConstantToLabel(), getMetadata().getParams());
            try {
                query.getResultList();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    };
}
Also used : Query(javax.persistence.Query) RecognitionException(antlr.RecognitionException) TokenStreamException(antlr.TokenStreamException)

Aggregations

RecognitionException (antlr.RecognitionException)15 TokenStreamException (antlr.TokenStreamException)9 GroovyRecognizer (org.codehaus.groovy.antlr.parser.GroovyRecognizer)6 TokenStreamRecognitionException (antlr.TokenStreamRecognitionException)4 StringReader (java.io.StringReader)4 GroovyLexer (org.codehaus.groovy.antlr.parser.GroovyLexer)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 BufferedReader (java.io.BufferedReader)2 Reader (java.io.Reader)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 QueryException (org.hibernate.QueryException)2