[antlr-interest] On how to insert logging calls at the beginning of each method

Vinicius Durelli vinicius.durelli at gmail.com
Sat Jul 28 09:56:19 PDT 2012

Hi all,

I am designing a rewriter that inserts calls to a method named log at the
beginning of every method definition. The rewriter in question does
something similar to the following whenever it runs into a method:

int doSomeStuff() {
   log(); //logs this method call
   //method body

In order to do that I am using StringTemplates and rewriting the token
buffer. So far, I came up with a temporary solution that gets the job done
but doesn't look right to me:

body : '{'  statement+ '}' -> template(x={$text.substring(1,
$text.length())}) "{ log(); <x>"

Is there a better/clever way of getting this done without having to resort
to Java String's methods (i.e., length and substring)?


More information about the antlr-interest mailing list