Search in sources :

Example 81 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project drools by kiegroup.

the class JavaExprAnalyzer method parse.

private JavaParser parse(final String expr) {
    final CharStream charStream = new ANTLRStringStream(expr);
    final JavaLexer lexer = new JavaLexer(charStream);
    final TokenStream tokenStream = new CommonTokenStream(lexer);
    return new JavaParser(tokenStream);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) JavaParser(org.drools.compiler.rule.builder.dialect.java.parser.JavaParser) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) JavaLexer(org.drools.compiler.rule.builder.dialect.java.parser.JavaLexer) CharStream(org.antlr.runtime.CharStream)

Example 82 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project binnavi by google.

the class DebuggerMemoryExpressionParser method parse.

/**
 * Parses a single memory expression string.
 *
 * @param memoryExpression The memory expression string to parse.
 *
 * @return The parsed memory expression tree.
 *
 * @throws RecognitionException Thrown if parsing failed.
 */
public static MemoryExpressionElement parse(final String memoryExpression) throws RecognitionException {
    final CharStream charStream = new ANTLRStringStream(memoryExpression);
    final MemoryExpressionLexer lexer = new MemoryExpressionLexer(charStream);
    final CommonTokenStream tokens = new CommonTokenStream();
    tokens.setTokenSource(lexer);
    final MemoryExpressionParser parser = new MemoryExpressionParser(tokens) {

        @Override
        public void recover(final IntStream input, final RecognitionException exception) {
        // Nothing to do
        }

        @Override
        public Object recoverFromMismatchedToken(final IntStream input, final int ttype, final org.antlr.runtime.BitSet follow) throws RecognitionException {
            throw new MismatchedTokenException(ttype, input);
        }

        @Override
        public void reportError(final RecognitionException exception) {
        // Nothing to do
        }
    };
    parser.setTreeAdaptor(adaptor);
    final MemoryExpressionParser.prog_return parserResult = parser.prog();
    final CommonTree ast = (CommonTree) parserResult.getTree();
    return convert(ast);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) MemoryExpressionParser(com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionParser) CommonTokenStream(org.antlr.runtime.CommonTokenStream) MemoryExpressionLexer(com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionLexer) CommonTree(org.antlr.runtime.tree.CommonTree) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) IntStream(org.antlr.runtime.IntStream) CharStream(org.antlr.runtime.CharStream) RecognitionException(org.antlr.runtime.RecognitionException)

Example 83 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project yamcs-studio by yamcs.

the class FormulaAst method createParser.

/**
 * Creates a parser for the given text.
 *
 * @param text
 *            the string to be parsed
 * @return the new parser
 */
static FormulaParser createParser(String text) {
    CharStream stream = new ANTLRStringStream(text);
    var lexer = new FormulaLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    return new FormulaParser(tokenStream);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) CharStream(org.antlr.runtime.CharStream)

Example 84 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project cassandra by apache.

the class CqlParserTest method parseAndCountErrors.

private void parseAndCountErrors(String cql, int expectedErrors, ParserOperation operation) throws RecognitionException {
    SyntaxErrorCounter counter = new SyntaxErrorCounter();
    CharStream stream = new ANTLRStringStream(cql);
    CqlLexer lexer = new CqlLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(counter);
    operation.perform(parser);
    assertEquals(expectedErrors, counter.count);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) CharStream(org.antlr.runtime.CharStream)

Example 85 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project cassandra by apache.

the class CqlParserTest method testAddErrorListener.

@Test
public void testAddErrorListener() throws Exception {
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();
    CharStream stream = new ANTLRStringStream("SELECT * FORM FROM test");
    CqlLexer lexer = new CqlLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(firstCounter);
    parser.addErrorListener(secondCounter);
    // By default CqlParser should recover from the syntax error by removing FORM
    // but as recoverFromMismatchedToken and recover have been overloaded, it will not
    // and the returned ParsedStatement will be null.
    assertNull(parser.query());
    // Only one error must be reported (mismatched: FORM).
    assertEquals(1, firstCounter.count);
    assertEquals(1, secondCounter.count);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) CharStream(org.antlr.runtime.CharStream) Test(org.junit.Test)

Aggregations

CommonTokenStream (org.antlr.runtime.CommonTokenStream)93 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)62 RecognitionException (org.antlr.runtime.RecognitionException)25 CharStream (org.antlr.runtime.CharStream)22 CommonTree (org.antlr.runtime.tree.CommonTree)21 TokenStream (org.antlr.runtime.TokenStream)17 File (java.io.File)12 Test (org.junit.Test)12 CommonToken (org.antlr.runtime.CommonToken)10 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)10 ExprLexer (org.apache.drill.common.expression.parser.ExprLexer)10 ExprParser (org.apache.drill.common.expression.parser.ExprParser)10 FileInputStream (java.io.FileInputStream)9 InputStream (java.io.InputStream)8 ANTLRInputStream (org.antlr.runtime.ANTLRInputStream)8 InternalSimpleExpressionsTestLanguageLexer (org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer)8 InputStreamReader (java.io.InputStreamReader)6 Token (org.antlr.runtime.Token)6 JPA2Lexer (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer)5 JPA2Parser (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser)5