Search in sources :

Example 26 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project siddhi by wso2.

the class SiddhiCompiler method parsePartition.

public static Partition parsePartition(String partition) throws SiddhiParserException {
    CharStream input = CharStreams.fromString(partition);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.partition_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (Partition) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Partition(io.siddhi.query.api.execution.partition.Partition) SiddhiQLBaseVisitorImpl(io.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 27 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project siddhi by wso2.

the class SiddhiCompiler method parseExpression.

public static Expression parseExpression(String expression) {
    CharStream input = CharStreams.fromString(expression);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.expression();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (Expression) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Expression(io.siddhi.query.api.expression.Expression) SiddhiQLBaseVisitorImpl(io.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 28 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project jaybird by FirebirdSQL.

the class TestGrammar method createParser.

// TODO Add more testcases for grammar
protected JaybirdSqlParser createParser(String testString) {
    CharStream stream = CharStreams.fromString(testString);
    JaybirdSqlLexer lexer = new JaybirdSqlLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    return new JaybirdSqlParser(tokenStream);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CharStream(org.antlr.v4.runtime.CharStream)

Example 29 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project alliance by codice.

the class BqsConverter method convertBQSToDDF.

public Filter convertBQSToDDF(String query) {
    query = query.trim();
    LOGGER.debug("Original Query: {}", query);
    ANTLRInputStream inputStream = new ANTLRInputStream(query);
    // transforms characters into tokens
    BqsLexer lex = new BqsLexer(inputStream);
    // a token stream
    CommonTokenStream tokens = new CommonTokenStream(lex);
    // transforms tokens into parse trees
    BqsParser parser = new BqsParser(tokens);
    BqsTreeWalkerListener bqsListener = new BqsTreeWalkerListener(filterBuilder);
    ParseTree tree = parser.query();
    ParseTreeWalker.DEFAULT.walk(bqsListener, tree);
    Filter filter = bqsListener.getFilter();
    if (filter != null && StringUtils.isNotBlank(filter.toString())) {
        LOGGER.debug("Parsed Query: {}", filter);
    } else {
        filter = filterBuilder.attribute(Metacard.ANY_TEXT).is().text("*");
        LOGGER.debug("After parsing filter, didn't have any query parameters. Defaulting to everything search: {}", filter);
    }
    return filter;
}
Also used : BqsLexer(org.codice.alliance.nsili.common.grammar.BqsLexer) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BqsParser(org.codice.alliance.nsili.common.grammar.BqsParser) Filter(org.opengis.filter.Filter) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 30 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project components by Talend.

the class SoqlQuery method init.

/**
 * init method parses input <code>queryString</code> and stores is as tree.
 * Should be invoked right after
 * {@link org.talend.components.salesforce.soql.SoqlQuery#getInstance()} method
 *
 * @param queryString SOQL query
 */
public void init(String queryString) {
    ANTLRInputStream is = new ANTLRInputStream(queryString);
    SoqlLexer lexer = new SoqlLexer(is);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    SoqlParser parser = new SoqlParser(tokenStream);
    queryTree = parser.query();
    fieldDescriptions = new ArrayList<>();
    FieldRetrieverListener listener = new FieldRetrieverListener();
    ParseTreeWalker.DEFAULT.walk(listener, queryTree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SoqlParser(org.talend.components.salesforce.soql.parser.SoqlParser) SoqlLexer(org.talend.components.salesforce.soql.parser.SoqlLexer) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)339 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)144 Test (org.junit.Test)110 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)104 ParseTree (org.antlr.v4.runtime.tree.ParseTree)97 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)90 LexerGrammar (org.antlr.v4.tool.LexerGrammar)88 CharStream (org.antlr.v4.runtime.CharStream)69 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)38 IOException (java.io.IOException)29 RecognitionException (org.antlr.v4.runtime.RecognitionException)27 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)26 Token (org.antlr.v4.runtime.Token)24 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)22 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)21 CancellationException (java.util.concurrent.CancellationException)20 ByteArrayInputStream (java.io.ByteArrayInputStream)16 Utils.toCharStream (clawfc.Utils.toCharStream)15 ArrayList (java.util.ArrayList)15