Search in sources :

Example 46 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project neo4j by neo4j.

the class ResultsParser method parseParameter.

Object parseParameter(String value, CypherParametersCreator listener) {
    lexer.setInputStream(new ANTLRInputStream(value));
    walker.walk(listener, parser.value());
    return listener.parsed();
}
Also used : ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 47 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project aic-expresso by aic-sri-international.

the class AntlrGrinderParserWrapper method parse.

@Override
public Expression parse(String string, Parser.ErrorListener parserEerrorListener) {
    Expression result = null;
    try {
        AntlrErrorListener antlrErrorListener = new AntlrErrorListener(parserEerrorListener);
        ANTLRInputStream input = new ANTLRInputStream(string);
        AntlrGrinderLexer lexer = new AntlrGrinderLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        AntlrGrinderParser parser = new AntlrGrinderParser(tokens);
        lexer.removeErrorListeners();
        parser.removeErrorListeners();
        lexer.addErrorListener(antlrErrorListener);
        parser.addErrorListener(antlrErrorListener);
        ParseTree tree = parser.expression();
        boolean eof = parser.getInputStream().LA(1) == Recognizer.EOF;
        if (!antlrErrorListener.errorsDetected) {
            if (!eof) {
                System.err.println("Unable to parse the complete input expression: " + input);
            } else {
                lexer.removeErrorListeners();
                parser.removeErrorListeners();
                ExpressionVisitor expressionVisitor = new ExpressionVisitor(getRandomPredicatesSignatures());
                result = expressionVisitor.visit(tree);
            }
        }
    } catch (RecognitionException re) {
        re.printStackTrace();
    } catch (RuntimeException re) {
        re.printStackTrace();
    }
    return result;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Expression(com.sri.ai.expresso.api.Expression) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 48 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project aic-praise by aic-sri-international.

the class HOGMCodeArea method computeHighlighting.

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    int lastTokenEnd = 0;
    ANTLRInputStream input = new ANTLRInputStream(text);
    HOGMLexer lexer = new HOGMLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();
    for (int i = 0; i < tokens.size(); i++) {
        Token t = tokens.get(i);
        if (t.getType() == Token.EOF) {
            break;
        }
        String styleClass;
        if (t.getType() == HOGMLexer.COMMENT || t.getType() == HOGMLexer.LINE_COMMENT) {
            styleClass = "hogmCodeComment";
        } else if (HOGMTerminalSymbols.isTerminalSymbol(t.getText())) {
            styleClass = "hogmCodeKeyword";
        } else {
            styleClass = "hogmCodeOther";
        }
        int spacing = t.getStartIndex() - lastTokenEnd;
        if (spacing > 0) {
            spansBuilder.add(Collections.emptyList(), spacing);
        }
        int stylesize = (t.getStopIndex() - t.getStartIndex()) + 1;
        spansBuilder.add(Collections.singleton(styleClass), stylesize);
        lastTokenEnd = t.getStopIndex() + 1;
    }
    return spansBuilder.create();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) HOGMLexer(com.sri.ai.praise.model.v1.hogm.antlr.HOGMLexer) Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.StyleSpansBuilder) Token(org.antlr.v4.runtime.Token) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 49 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project presto by prestodb.

the class SqlParser method invokeParser.

private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
    try {
        SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        SqlBaseParser parser = new SqlBaseParser(tokenStream);
        parser.addParseListener(new PostProcessor());
        lexer.removeErrorListeners();
        lexer.addErrorListener(ERROR_LISTENER);
        parser.removeErrorListeners();
        parser.addErrorListener(ERROR_LISTENER);
        ParserRuleContext tree;
        try {
            // first, try parsing with potentially faster SLL mode
            parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            tree = parseFunction.apply(parser);
        } catch (ParseCancellationException ex) {
            // if we fail, parse with LL mode
            // rewind input stream
            tokenStream.reset();
            parser.reset();
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            tree = parseFunction.apply(parser);
        }
        return new AstBuilder().visit(tree);
    } catch (StackOverflowError e) {
        throw new ParsingException(name + " is too large (stack overflow while parsing)");
    }
}
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 50 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project presto by prestodb.

the class StatementSplitter method getLexer.

private static TokenSource getLexer(String sql, Set<String> terminators) {
    requireNonNull(sql, "sql is null");
    CharStream stream = new CaseInsensitiveStream(new ANTLRInputStream(sql));
    return new DelimiterLexer(stream, terminators);
}
Also used : CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)110 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)86 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)59 Test (org.junit.Test)59 LexerGrammar (org.antlr.v4.tool.LexerGrammar)52 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 CharStream (org.antlr.v4.runtime.CharStream)15 ParseTree (org.antlr.v4.runtime.tree.ParseTree)15 Token (org.antlr.v4.runtime.Token)12 TokenStream (org.antlr.v4.runtime.TokenStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 StringReader (java.io.StringReader)7 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)7 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)7 BufferedTokenStream (org.antlr.v4.runtime.BufferedTokenStream)6 IntegerList (org.antlr.v4.runtime.misc.IntegerList)6 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)5