Search in sources :

Example 61 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project pinot by linkedin.

the class Pql2Compiler method compileToExpressionTree.

@Override
public TransformExpressionTree compileToExpressionTree(String expression) {
    CharStream charStream = new ANTLRInputStream(expression);
    PQL2Lexer lexer = new PQL2Lexer(charStream);
    lexer.setTokenFactory(new CommonTokenFactory(true));
    TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer);
    PQL2Parser parser = new PQL2Parser(tokenStream);
    parser.setErrorHandler(new BailErrorStrategy());
    // Parse
    ParseTree parseTree = parser.expression();
    ParseTreeWalker walker = new ParseTreeWalker();
    Pql2AstListener listener = new Pql2AstListener(expression);
    walker.walk(listener, parseTree);
    final AstNode rootNode = listener.getRootNode();
    return TransformExpressionTree.buildTree(rootNode);
}
Also used : TokenStream(org.antlr.v4.runtime.TokenStream) UnbufferedTokenStream(org.antlr.v4.runtime.UnbufferedTokenStream) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) UnbufferedTokenStream(org.antlr.v4.runtime.UnbufferedTokenStream) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) AstNode(com.linkedin.pinot.pql.parsers.pql2.ast.AstNode)

Example 62 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project rest.li by linkedin.

the class PdlSchemaParser method parse.

/**
   * Parse a JSON representation of a schema from a {{java.io.Reader}}.
   *
   * The top level {{DataSchema}}'s parsed are in {{#topLevelDataSchemas}}.
   * These are the types that are not defined within other types.
   * Parse errors are in {{#errorMessageBuilder}} and indicated
   * by {{#hasError()}}.
   *
   * @param reader with the JSON representation of the schema.
   */
public void parse(Reader reader) {
    try {
        ErrorRecorder errorRecorder = new ErrorRecorder();
        PdlLexer lexer;
        try {
            lexer = new PdlLexer(new ANTLRInputStream(reader));
        } catch (IOException e) {
            ParseError error = new ParseError(new ParseErrorLocation(0, 0), e.getMessage());
            startErrorMessage(error).append(error.message).append(NEWLINE);
            return;
        }
        lexer.removeErrorListeners();
        lexer.addErrorListener(errorRecorder);
        PdlParser parser = new PdlParser(new CommonTokenStream(lexer));
        parser.removeErrorListeners();
        parser.addErrorListener(errorRecorder);
        DocumentContext antlrDocument = parser.document();
        parse(antlrDocument);
        if (errorRecorder.errors.size() > 0) {
            for (ParseError error : errorRecorder.errors) {
                startErrorMessage(error).append(error.message).append(NEWLINE);
            }
        }
    } catch (ParseException e) {
        startErrorMessage(e.error).append(e.getMessage()).append(NEWLINE);
    } catch (Throwable t) {
        ParseError parseError = new ParseError(new ParseErrorLocation(0, 0), null);
        startErrorMessage(parseError).append("Unexpected parser error: ").append(ExceptionUtils.getStackTrace(t)).append(NEWLINE);
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) PdlLexer(com.linkedin.data.grammar.PdlLexer) IOException(java.io.IOException) DocumentContext(com.linkedin.data.grammar.PdlParser.DocumentContext) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) PdlParser(com.linkedin.data.grammar.PdlParser)

Example 63 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project druid by druid-io.

the class Parser method parse.

public static Expr parse(String in, boolean withFlatten) {
    ExprLexer lexer = new ExprLexer(new ANTLRInputStream(in));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ExprParser parser = new ExprParser(tokens);
    parser.setBuildParseTree(true);
    ParseTree parseTree = parser.expr();
    ParseTreeWalker walker = new ParseTreeWalker();
    ExprListenerImpl listener = new ExprListenerImpl(parseTree);
    walker.walk(listener, parseTree);
    return withFlatten ? flatten(listener.getAST()) : listener.getAST();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ExprLexer(io.druid.math.expr.antlr.ExprLexer) ExprParser(io.druid.math.expr.antlr.ExprParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 64 with ANTLRInputStream

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

the class JavadocParseTreeTest method parseJavadoc.

private ParseTree parseJavadoc(String aBlockComment) throws IOException {
    final Charset utf8Charset = Charset.forName("UTF-8");
    final InputStream in = new ByteArrayInputStream(aBlockComment.getBytes(utf8Charset));
    final ANTLRInputStream input = new ANTLRInputStream(in);
    final JavadocLexer lexer = new JavadocLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(errorListener);
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    parser = new JavadocParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);
    return parser.javadoc();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 65 with ANTLRInputStream

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

the class JavadocDetailNodeParser method parseJavadocAsParseTree.

/**
     * Parses block comment content as javadoc comment.
     * @param blockComment
     *        block comment content.
     * @return parse tree
     */
private ParseTree parseJavadocAsParseTree(String blockComment) {
    final ANTLRInputStream input = new ANTLRInputStream(blockComment);
    final JavadocLexer lexer = new JavadocLexer(input);
    // remove default error listeners
    lexer.removeErrorListeners();
    // add custom error listener that logs parsing errors
    lexer.addErrorListener(errorListener);
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final JavadocParser parser = new JavadocParser(tokens);
    // remove default error listeners
    parser.removeErrorListeners();
    // add custom error listener that logs syntax errors
    parser.addErrorListener(errorListener);
    // This strategy stops parsing when parser error occurs.
    // By default it uses Error Recover Strategy which is slow and useless.
    parser.setErrorHandler(new BailErrorStrategy());
    return parser.javadoc();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) JavadocParser(com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) JavadocLexer(com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocLexer) 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