Search in sources :

Example 6 with GrammarParserInterpreter

use of org.antlr.v4.tool.GrammarParserInterpreter in project antlr4 by antlr.

the class TestUnicodeGrammar method parseTreeForGrammarWithInput.

private static String parseTreeForGrammarWithInput(String grammarText, String rootRule, String inputText) throws Exception {
    Grammar grammar = new Grammar(grammarText);
    LexerInterpreter lexEngine = grammar.createLexerInterpreter(CharStreams.fromString(inputText));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    GrammarParserInterpreter parser = grammar.createGrammarParserInterpreter(tokens);
    ParseTree parseTree = parser.parse(grammar.rules.get(rootRule).index);
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(grammar.getRuleNames());
    return Trees.toStringTree(parseTree, nodeTextProvider);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) Grammar(org.antlr.v4.tool.Grammar) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)5 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)5 GrammarParserInterpreter (org.antlr.v4.tool.GrammarParserInterpreter)5 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)4 ParseTree (org.antlr.v4.runtime.tree.ParseTree)4 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)2 Grammar (org.antlr.v4.tool.Grammar)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 CharStream (org.antlr.v4.runtime.CharStream)1 InterpreterRuleContext (org.antlr.v4.runtime.InterpreterRuleContext)1 ATN (org.antlr.v4.runtime.atn.ATN)1 ATNDeserializer (org.antlr.v4.runtime.atn.ATNDeserializer)1 AmbiguityInfo (org.antlr.v4.runtime.atn.AmbiguityInfo)1 LookaheadEventInfo (org.antlr.v4.runtime.atn.LookaheadEventInfo)1 Test (org.junit.Test)1