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);
}
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);
}
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;
}
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;
}
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());
}
Aggregations