[antlr-interest] ANTLR 3.0 template files

Monty Zukowski monty at codetransform.com
Mon Jun 13 10:06:36 PDT 2005


Terence Parr wrote:


> I hate having to leave holes in the main tokenRef even as that will  get 
> more and more complicated as I add debugging and all that.  I  need a 
> way to specify locations in a template after-the-fact--w/o  having to 
> actually alter the original template.  I read some aspect- oriented 
> programming stuff but it seems pretty complicated how they  specify 
> where to add function calls etc...  Hmm... anybody have some  suggestions?
> 

Perfect fit for tree rewriting!

AOP, as I understand if from a few years ago, let you say thing like 
"wrap this method with this code" or "wrap all invocations of this 
method with this code" or "add these variables and methods to all 
classes matching this pattern", etc.  And what's underneath?  A grammar 
that can find all the appropriate spots and merge in the code.

If you don't have holes, you need some way to specify a pattern to match 
against--how to find the holes.  You can predefine them a la AOP, or you 
can do it with a grammar.  Doing it the AOP style also brings up a 
problem of ordering -- if you have two aspects modifying the same spot 
which gets to go first?  It has to be specificed somewhere.

In fact, I'd be curious to see what "language" underlies ANTLR 3.  Your 
primitives are things like "change state" and "match" and "call rule" 
and "action" and "variable assignment".  I think you need an 
intermediate language which is more refined than text yet not as 
specific as a programming language.  StringTemplate might just be a 
little to stringy...just a little more structure than chunks of code and 
chunk references could allow you to write a grammar.

before chunk "match" add chunk "label"
after chunk "match" add chunk "build tree"

or, with ST translating chunk names to rule names and some predefined 
rules like chunk, text, and chunk_reference:

match:
	{label}
	chunk
	{build tree}

which doesn't look much different than what you have except that it is 
completely separate--the match template gets to stay simple.  The groovy 
thing is that now you can push and pop attributes so that the actual 
"match invocation" can know if it happening in a lexer, parser or tree 
parser.  I know there's an elegant way to make this happen--I need some 
more difficult examples though.  Or give me a call.

Monty


More information about the antlr-interest mailing list