Search in sources :

Example 21 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project ksql by confluentinc.

the class KsqlParser method getParseTree.

private ParserRuleContext getParseTree(String sql) {
    SqlBaseLexer sqlBaseLexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
    CommonTokenStream tokenStream = new CommonTokenStream(sqlBaseLexer);
    SqlBaseParser sqlBaseParser = new SqlBaseParser(tokenStream);
    sqlBaseLexer.removeErrorListeners();
    sqlBaseLexer.addErrorListener(ERROR_LISTENER);
    sqlBaseParser.removeErrorListeners();
    sqlBaseParser.addErrorListener(ERROR_LISTENER);
    Function<SqlBaseParser, ParserRuleContext> parseFunction = SqlBaseParser::statements;
    ParserRuleContext tree;
    try {
        // first, try parsing with potentially faster SLL mode
        sqlBaseParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        tree = parseFunction.apply(sqlBaseParser);
    } catch (ParseCancellationException ex) {
        // if we fail, parse with LL mode
        // rewind input stream
        tokenStream.reset();
        sqlBaseParser.reset();
        sqlBaseParser.getInterpreter().setPredictionMode(PredictionMode.LL);
        tree = parseFunction.apply(sqlBaseParser);
    }
    return tree;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 22 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project java by wavefrontHQ.

the class AbstractIngesterFormatter method getQueue.

protected Queue<Token> getQueue(String input) {
    DSWrapperLexer lexer = dsWrapperLexerThreadLocal.get();
    lexer.setInputStream(new ANTLRInputStream(input));
    CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
    commonTokenStream.fill();
    List<Token> tokens = commonTokenStream.getTokens();
    if (tokens.isEmpty()) {
        throw new RuntimeException("Could not parse: " + input);
    }
    // this is sensitive to the grammar in DSQuery.g4. We could just use the visitor but doing so
    // means we need to be creating the AST and instead we could just use the lexer. in any case,
    // we don't expect the graphite format to change anytime soon.
    // filter all EOF tokens first.
    Queue<Token> queue = tokens.stream().filter(t -> t.getType() != Lexer.EOF).collect(Collectors.toCollection(ArrayDeque::new));
    return queue;
}
Also used : ReportSourceTag(wavefront.report.ReportSourceTag) Token(org.antlr.v4.runtime.Token) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Supplier(java.util.function.Supplier) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) ArrayList(java.util.ArrayList) Lexer(org.antlr.v4.runtime.Lexer) Lists(com.google.common.collect.Lists) DateUtils(org.apache.commons.lang.time.DateUtils) Matcher(java.util.regex.Matcher) Map(java.util.Map) ReportPoint(wavefront.report.ReportPoint) Nullable(javax.annotation.Nullable) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Recognizer(org.antlr.v4.runtime.Recognizer) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) DSWrapperLexer(queryserver.parser.DSWrapperLexer) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RecognitionException(org.antlr.v4.runtime.RecognitionException) Preconditions(com.google.common.base.Preconditions) Queue(java.util.Queue) Pattern(java.util.regex.Pattern) ArrayDeque(java.util.ArrayDeque) Histogram(wavefront.report.Histogram) HistogramType(wavefront.report.HistogramType) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) DSWrapperLexer(queryserver.parser.DSWrapperLexer) Token(org.antlr.v4.runtime.Token) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 23 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project parseq by linkedin.

the class RequestConfigElement method parse.

static RequestConfigElement parse(String property, String key, Object value) throws RequestConfigKeyParsingException {
    RequestConfigKeyParsingErrorListener errorListener = new RequestConfigKeyParsingErrorListener();
    ANTLRInputStream input = new ANTLRInputStream(key);
    RequestConfigKeyLexer lexer = new RequestConfigKeyLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(errorListener);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    RequestConfigKeyParser parser = new RequestConfigKeyParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);
    KeyContext keyTree = parser.key();
    if (!errorListener.hasErrors()) {
        InboundContext inbound = keyTree.inbound();
        OutboundContext outbound = keyTree.outbound();
        Optional<String> inboundName = handlingWildcard(inbound.restResource());
        Optional<String> outboundName = handlingWildcard(outbound.restResource());
        Optional<String> inboundOp = getOpIn(inbound.operationIn());
        Optional<ResourceMethod> outboundOp = getOpOut(outbound.operationOut());
        Optional<String> inboundOpName = inboundOp.flatMap(method -> getOpInName(method, inbound.operationIn()));
        Optional<String> outboundOpName = outboundOp.flatMap(method -> getOpOutName(method, outbound.operationOut()));
        return new RequestConfigElement(key, coerceValue(property, value), property, inboundName, outboundName, inboundOpName, outboundOpName, inboundOp, outboundOp);
    } else {
        throw new RequestConfigKeyParsingException("Error" + ((errorListener.errorsSize() > 1) ? "s" : "") + " parsing key: " + key + "\n" + errorListener);
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) InboundContext(com.linkedin.restli.client.config.RequestConfigKeyParser.InboundContext) OutboundContext(com.linkedin.restli.client.config.RequestConfigKeyParser.OutboundContext) KeyContext(com.linkedin.restli.client.config.RequestConfigKeyParser.KeyContext) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 24 with CommonTokenStream

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

the class SiddhiCompiler method parseQuery.

public static Query parseQuery(String query) throws SiddhiParserException {
    CharStream input = CharStreams.fromString(query);
    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.query_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (Query) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) OnDemandQuery(io.siddhi.query.api.execution.query.OnDemandQuery) Query(io.siddhi.query.api.execution.query.Query) StoreQuery(io.siddhi.query.api.execution.query.StoreQuery) SiddhiQLBaseVisitorImpl(io.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 25 with CommonTokenStream

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

the class SiddhiCompiler method parseFunctionDefinition.

public static FunctionDefinition parseFunctionDefinition(String functionDefinition) throws SiddhiParserException {
    CharStream input = CharStreams.fromString(functionDefinition);
    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.definition_function_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (FunctionDefinition) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) FunctionDefinition(io.siddhi.query.api.definition.FunctionDefinition) SiddhiQLBaseVisitorImpl(io.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

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