ANTLR's CharStream
class has a method getText(Interval interval)
which will return the original source in the give range. The Context
object has methods to get the beginning and end. Assuming you have a field in your listener called input
which has the CharStream being parsed, you can do this:
int a = ctx.start.getStartIndex();
int b = ctx.stop.getStopIndex();
Interval interval = new Interval(a,b);
input.getText(interval);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…