java - How do I get the original text that an antlr4 rule matched? -


using java 7 grammar https://github.com/antlr/grammars-v4/blob/master/java7/java7.g4 want find methods specific name , print out method. see can use methoddeclaration rule when match. subclass java7baselistener , override listener method:

@override public void entermethoddeclaration(java7parser.methoddeclarationcontext ctx) { } 

how original text out? ctx.gettext() gives me string whitespace stripped out. want comments , original formatting.

antlr's charstream class has method gettext(interval interval) return original source in give range. context object has methods beginning , end. assuming have field in listener called input has charstream being parsed, can this:

    int = ctx.start.getstartindex();     int b = ctx.stop.getstopindex();     interval interval = new interval(a,b);     input.gettext(interval); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -