use of org.antlr.v4.runtime.tree.TerminalNode in project antlr4 by antlr.
the class ParserRuleContext method getTokens.
public List<TerminalNode> getTokens(int ttype) {
if (children == null) {
return Collections.emptyList();
}
List<TerminalNode> tokens = null;
for (ParseTree o : children) {
if (o instanceof TerminalNode) {
TerminalNode tnode = (TerminalNode) o;
Token symbol = tnode.getSymbol();
if (symbol.getType() == ttype) {
if (tokens == null) {
tokens = new ArrayList<TerminalNode>();
}
tokens.add(tnode);
}
}
}
if (tokens == null) {
return Collections.emptyList();
}
return tokens;
}
use of org.antlr.v4.runtime.tree.TerminalNode in project antlr4 by antlr.
the class ParserRuleContext method getToken.
public TerminalNode getToken(int ttype, int i) {
if (children == null || i < 0 || i >= children.size()) {
return null;
}
// what token with ttype have we found?
int j = -1;
for (ParseTree o : children) {
if (o instanceof TerminalNode) {
TerminalNode tnode = (TerminalNode) o;
Token symbol = tnode.getSymbol();
if (symbol.getType() == ttype) {
j++;
if (j == i) {
return tnode;
}
}
}
}
return null;
}
use of org.antlr.v4.runtime.tree.TerminalNode in project antlr4 by antlr.
the class Trees method getNodeText.
public static String getNodeText(Tree t, List<String> ruleNames) {
if (ruleNames != null) {
if (t instanceof RuleContext) {
int ruleIndex = ((RuleContext) t).getRuleContext().getRuleIndex();
String ruleName = ruleNames.get(ruleIndex);
int altNumber = ((RuleContext) t).getAltNumber();
if (altNumber != ATN.INVALID_ALT_NUMBER) {
return ruleName + ":" + altNumber;
}
return ruleName;
} else if (t instanceof ErrorNode) {
return t.toString();
} else if (t instanceof TerminalNode) {
Token symbol = ((TerminalNode) t).getSymbol();
if (symbol != null) {
String s = symbol.getText();
return s;
}
}
}
// no recog for rule names
Object payload = t.getPayload();
if (payload instanceof Token) {
return ((Token) payload).getText();
}
return t.getPayload().toString();
}
use of org.antlr.v4.runtime.tree.TerminalNode in project antlr4 by antlr.
the class TestVisitors method testVisitTerminalNode.
/**
* This test verifies the basic behavior of visitors, with an emphasis on
* {@link AbstractParseTreeVisitor#visitTerminal}.
*/
@Test
public void testVisitTerminalNode() {
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 String defaultResult() {
return "";
}
@Override
protected String aggregateResult(String aggregate, String nextResult) {
return aggregate + nextResult;
}
};
String result = listener.visit(context);
String expected = "[@0,0:0='A',<1>,1:0]\n" + "[@1,1:0='<EOF>',<-1>,1:1]\n";
Assert.assertEquals(expected, result);
}
use of org.antlr.v4.runtime.tree.TerminalNode 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);
}
Aggregations