Search in sources :

Example 96 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream 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)

Example 97 with ANTLRInputStream

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

the class TestBufferedTokenStream method testCompleteBuffer.

@Test
public void testCompleteBuffer() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "ASSIGN : '=';\n" + "PLUS : '+';\n" + "MULT : '*';\n" + "WS : ' '+;\n");
    // Tokens: 012345678901234567
    // Input:  x = 3 * 0 + 2 * 0;
    CharStream input = new ANTLRInputStream("x = 3 * 0 + 2 * 0;");
    LexerInterpreter lexEngine = g.createLexerInterpreter(input);
    TokenStream tokens = createTokenStream(lexEngine);
    int i = 1;
    Token t = tokens.LT(i);
    while (t.getType() != Token.EOF) {
        i++;
        t = tokens.LT(i);
    }
    // push it past end
    tokens.LT(i++);
    tokens.LT(i++);
    String result = tokens.getText();
    String expecting = "x = 3 * 0 + 2 * 0;";
    assertEquals(expecting, result);
}
Also used : LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) TokenStream(org.antlr.v4.runtime.TokenStream) BufferedTokenStream(org.antlr.v4.runtime.BufferedTokenStream) Token(org.antlr.v4.runtime.Token) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

Example 98 with ANTLRInputStream

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

the class TestGrammarParserInterpreter method testInterp.

InterpreterRuleContext testInterp(LexerGrammar lg, Grammar g, String startRule, String input, String expectedParseTree) {
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    ParseTree t = parser.parse(g.rules.get(startRule).index);
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    String treeStr = Trees.toStringTree(t, nodeTextProvider);
    //		System.out.println("parse tree: "+treeStr);
    assertEquals(expectedParseTree, treeStr);
    return (InterpreterRuleContext) t;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) InterpreterRuleContext(org.antlr.v4.runtime.InterpreterRuleContext) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 99 with ANTLRInputStream

use of org.antlr.v4.runtime.ANTLRInputStream in project L42 by ElvisResearchGroup.

the class Parser method getParser.

public static antlrGenerated.L42Parser getParser(String s) {
    ANTLRInputStream in = new ANTLRInputStream(s);
    antlrGenerated.L42Lexer l = new antlrGenerated.L42Lexer(in);
    /*{
      @Override public void recover(LexerNoViableAltException e) {
        throw new IllegalArgumentException(e); // Bail out
      }
    @Override public void recover(RecognitionException re) {
        throw new IllegalArgumentException(re); // Bail out
      }};*/
    CommonTokenStream t = new CommonTokenStream(l);
    antlrGenerated.L42Parser p = new antlrGenerated.L42Parser(t);
    //    p.setErrorHandler(new BailErrorStrategy());
    return p;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) L42Lexer(antlrGenerated.L42Lexer) L42Lexer(antlrGenerated.L42Lexer) L42Parser(antlrGenerated.L42Parser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) L42Parser(antlrGenerated.L42Parser)

Example 100 with ANTLRInputStream

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

the class TestUnbufferedTokenStream method testMarkStart.

@Test
public void testMarkStart() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "ASSIGN : '=';\n" + "PLUS : '+';\n" + "MULT : '*';\n" + "WS : ' '+;\n");
    // Tokens: 012345678901234567
    // Input:  x = 302;
    CharStream input = new ANTLRInputStream(new StringReader("x = 302;"));
    LexerInterpreter lexEngine = g.createLexerInterpreter(input);
    TestingUnbufferedTokenStream<Token> tokens = new TestingUnbufferedTokenStream<Token>(lexEngine);
    int m = tokens.mark();
    assertEquals("[[@0,0:0='x',<1>,1:0]]", tokens.getBuffer().toString());
    assertEquals("x", tokens.LT(1).getText());
    // consume x
    tokens.consume();
    assertEquals("[[@0,0:0='x',<1>,1:0], [@1,1:1=' ',<7>,1:1]]", tokens.getBuffer().toString());
    // ' '
    tokens.consume();
    // =
    tokens.consume();
    // ' '
    tokens.consume();
    // 302
    tokens.consume();
    // ;
    tokens.consume();
    assertEquals("[[@0,0:0='x',<1>,1:0], [@1,1:1=' ',<7>,1:1]," + " [@2,2:2='=',<4>,1:2], [@3,3:3=' ',<7>,1:3]," + " [@4,4:6='302',<2>,1:4], [@5,7:7=';',<3>,1:7]," + " [@6,8:7='<EOF>',<-1>,1:8]]", tokens.getBuffer().toString());
}
Also used : LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) StringReader(java.io.StringReader) Token(org.antlr.v4.runtime.Token) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

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