Search in sources :

Example 71 with ANTLRInputStream

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

the class TestVisitors method testShouldNotVisitTerminal.

/**
	 * This test verifies that {@link AbstractParseTreeVisitor#shouldVisitNextChild} is called before visiting the first
	 * child. It also verifies that {@link AbstractParseTreeVisitor#defaultResult} provides the default return value for
	 * visiting a tree.
	 */
@Test
public void testShouldNotVisitTerminal() {
    String input = "A";
    VisitorBasicLexer lexer = new VisitorBasicLexer(new ANTLRInputStream(input));
    VisitorBasicParser parser = new VisitorBasicParser(new CommonTokenStream(lexer));
    VisitorBasicParser.SContext context = parser.s();
    Assert.assertEquals("(s A <EOF>)", context.toStringTree(parser));
    VisitorBasicVisitor<String> listener = new VisitorBasicBaseVisitor<String>() {

        @Override
        public String visitTerminal(TerminalNode node) {
            throw new RuntimeException("Should not be reachable");
        }

        @Override
        protected String defaultResult() {
            return "default result";
        }

        @Override
        protected boolean shouldVisitNextChild(RuleNode node, String currentResult) {
            return false;
        }
    };
    String result = listener.visit(context);
    String expected = "default result";
    Assert.assertEquals(expected, result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) RuleNode(org.antlr.v4.runtime.tree.RuleNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

Example 72 with ANTLRInputStream

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

the class TestVisitors method testShouldNotVisitEOF.

/**
	 * This test verifies that {@link AbstractParseTreeVisitor#visitChildren} does not call
	 * {@link ParseTreeVisitor#visit} after {@link AbstractParseTreeVisitor#shouldVisitNextChild} returns
	 * {@code false}.
	 */
@Test
public void testShouldNotVisitEOF() {
    String input = "A";
    VisitorBasicLexer lexer = new VisitorBasicLexer(new ANTLRInputStream(input));
    VisitorBasicParser parser = new VisitorBasicParser(new CommonTokenStream(lexer));
    VisitorBasicParser.SContext context = parser.s();
    Assert.assertEquals("(s A <EOF>)", context.toStringTree(parser));
    VisitorBasicVisitor<String> listener = new VisitorBasicBaseVisitor<String>() {

        @Override
        public String visitTerminal(TerminalNode node) {
            return node.getSymbol().toString() + "\n";
        }

        @Override
        protected boolean shouldVisitNextChild(RuleNode node, String currentResult) {
            return currentResult == null || currentResult.isEmpty();
        }
    };
    String result = listener.visit(context);
    String expected = "[@0,0:0='A',<1>,1:0]\n";
    Assert.assertEquals(expected, result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) RuleNode(org.antlr.v4.runtime.tree.RuleNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

Example 73 with ANTLRInputStream

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

the class BasePythonTest method getTokenTypesViaATN.

public IntegerList getTokenTypesViaATN(String input, LexerATNSimulator lexerATN) {
    ANTLRInputStream in = new ANTLRInputStream(input);
    IntegerList tokenTypes = new IntegerList();
    int ttype;
    do {
        ttype = lexerATN.match(in, Lexer.DEFAULT_MODE);
        tokenTypes.add(ttype);
    } while (ttype != Token.EOF);
    return tokenTypes;
}
Also used : IntegerList(org.antlr.v4.runtime.misc.IntegerList) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 74 with ANTLRInputStream

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

the class BaseBrowserTest method getTokenTypesViaATN.

public IntegerList getTokenTypesViaATN(String input, LexerATNSimulator lexerATN) {
    ANTLRInputStream in = new ANTLRInputStream(input);
    IntegerList tokenTypes = new IntegerList();
    int ttype;
    do {
        ttype = lexerATN.match(in, Lexer.DEFAULT_MODE);
        tokenTypes.add(ttype);
    } while (ttype != Token.EOF);
    return tokenTypes;
}
Also used : IntegerList(org.antlr.v4.runtime.misc.IntegerList) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 75 with ANTLRInputStream

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

the class TestTokenStream method testBufferedTokenStreamReuseAfterFill.

/**
	 * This is a targeted regression test for antlr/antlr4#1584 ({@link BufferedTokenStream} cannot be reused after EOF).
	 */
@Test
public void testBufferedTokenStreamReuseAfterFill() {
    CharStream firstInput = new ANTLRInputStream("A");
    BufferedTokenStream tokenStream = new BufferedTokenStream(new VisitorBasicLexer(firstInput));
    tokenStream.fill();
    Assert.assertEquals(2, tokenStream.size());
    Assert.assertEquals(VisitorBasicLexer.A, tokenStream.get(0).getType());
    Assert.assertEquals(Token.EOF, tokenStream.get(1).getType());
    CharStream secondInput = new ANTLRInputStream("AA");
    tokenStream.setTokenSource(new VisitorBasicLexer(secondInput));
    tokenStream.fill();
    Assert.assertEquals(3, tokenStream.size());
    Assert.assertEquals(VisitorBasicLexer.A, tokenStream.get(0).getType());
    Assert.assertEquals(VisitorBasicLexer.A, tokenStream.get(1).getType());
    Assert.assertEquals(Token.EOF, tokenStream.get(2).getType());
}
Also used : BufferedTokenStream(org.antlr.v4.runtime.BufferedTokenStream) 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