Search in sources :

Example 56 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream 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 57 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream 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 58 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.

the class BaseJavaTest method getParserAndLexer.

public Pair<Parser, Lexer> getParserAndLexer(String input, String parserName, String lexerName) throws Exception {
    final Class<? extends Lexer> lexerClass = loadLexerClassFromTempDir(lexerName);
    final Class<? extends Parser> parserClass = loadParserClassFromTempDir(parserName);
    ANTLRInputStream in = new ANTLRInputStream(new StringReader(input));
    Class<? extends Lexer> c = lexerClass.asSubclass(Lexer.class);
    Constructor<? extends Lexer> ctor = c.getConstructor(CharStream.class);
    Lexer lexer = ctor.newInstance(in);
    Class<? extends Parser> pc = parserClass.asSubclass(Parser.class);
    Constructor<? extends Parser> pctor = pc.getConstructor(TokenStream.class);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    Parser parser = pctor.newInstance(tokens);
    return new Pair<Parser, Lexer>(parser, lexer);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Lexer(org.antlr.v4.runtime.Lexer) StringReader(java.io.StringReader) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Parser(org.antlr.v4.runtime.Parser) Pair(org.antlr.v4.runtime.misc.Pair)

Example 59 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.

the class BaseCppTest method writeParserTestFile.

protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug, boolean trace) {
    if (!parserStartRuleName.endsWith(")"))
        parserStartRuleName += "()";
    ST outputFileST = new ST("#include \\<iostream>\n" + "\n" + "#include \"antlr4-runtime.h\"\n" + "#include \"<lexerName>.h\"\n" + "#include \"<parserName>.h\"\n" + "\n" + "using namespace antlr4;\n" + "\n" + "class TreeShapeListener : public tree::ParseTreeListener {\n" + "public:\n" + "  void visitTerminal(tree::TerminalNode *) override {}\n" + "  void visitErrorNode(tree::ErrorNode *) override {}\n" + "  void exitEveryRule(ParserRuleContext *) override {}\n" + "  void enterEveryRule(ParserRuleContext *ctx) override {\n" + "    for (auto child : ctx->children) {\n" + "      tree::ParseTree *parent = child->parent;\n" + "      ParserRuleContext *rule = dynamic_cast\\<ParserRuleContext *>(parent);\n" + "      if (rule != ctx) {\n" + "        throw \"Invalid parse tree shape detected.\";\n" + "      }\n" + "    }\n" + "  }\n" + "};\n" + "\n" + "\n" + "int main(int argc, const char* argv[]) {\n" + "  ANTLRFileStream input(argv[1]);\n" + "  <lexerName> lexer(&input);\n" + "  CommonTokenStream tokens(&lexer);\n" + "<createParser>" + "\n" + "  tree::ParseTree *tree = parser.<parserStartRuleName>;\n" + "  TreeShapeListener listener;\n" + "  tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);\n" + "\n" + "  return 0;\n" + "}\n");
    String stSource = "  <parserName> parser(&tokens);\n";
    if (debug) {
        stSource += "  DiagnosticErrorListener errorListener;\n";
        stSource += "  parser.addErrorListener(&errorListener);\n";
    }
    if (trace)
        stSource += "  parser.setTrace(true);\n";
    ST createParserST = new ST(stSource);
    outputFileST.add("createParser", createParserST);
    outputFileST.add("parserName", parserName);
    outputFileST.add("lexerName", lexerName);
    outputFileST.add("listenerName", listenerName);
    outputFileST.add("visitorName", visitorName);
    outputFileST.add("parserStartRuleName", parserStartRuleName);
    writeFile(tmpdir, "Test.cpp", outputFileST.render());
}
Also used : ST(org.stringtemplate.v4.ST) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

Example 60 with CommonTokenStream

use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.

the class TestAmbigParseTrees method testInterpAtSpecificAlt.

void testInterpAtSpecificAlt(LexerGrammar lg, Grammar g, String startRule, int startAlt, String input, String expectedParseTree) {
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    ParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    RuleStartState ruleStartState = g.atn.ruleToStartState[g.getRule(startRule).index];
    Transition tr = ruleStartState.transition(0);
    ATNState t2 = tr.target;
    if (!(t2 instanceof BasicBlockStartState)) {
        throw new IllegalArgumentException("rule has no decision: " + startRule);
    }
    parser.addDecisionOverride(((DecisionState) t2).decision, 0, startAlt);
    ParseTree t = parser.parse(g.rules.get(startRule).index);
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    assertEquals(expectedParseTree, Trees.toStringTree(t, nodeTextProvider));
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) ParserInterpreter(org.antlr.v4.runtime.ParserInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Transition(org.antlr.v4.runtime.atn.Transition) BasicBlockStartState(org.antlr.v4.runtime.atn.BasicBlockStartState) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ATNState(org.antlr.v4.runtime.atn.ATNState)

Aggregations

CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)94 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)81 Test (org.junit.Test)54 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)52 LexerGrammar (org.antlr.v4.tool.LexerGrammar)44 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 ParseTree (org.antlr.v4.runtime.tree.ParseTree)15 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)12 IOException (java.io.IOException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 RecognitionException (org.antlr.v4.runtime.RecognitionException)8 InputStream (java.io.InputStream)7 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)6 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)6 Token (org.antlr.v4.runtime.Token)6 GrammarParserInterpreter (org.antlr.v4.tool.GrammarParserInterpreter)6 ArrayList (java.util.ArrayList)5 CommonToken (org.antlr.v4.runtime.CommonToken)5 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)5