[antlr-interest] tweaking input files

Terence Parr parrt at cs.usfca.edu
Tue Oct 10 17:29:45 PDT 2006


Hi,

In case you weren't aware of the TokenRewriteStream (or  
TokenStreamRewriteEngine in v2), it kicks butt.  For example, I'm  
experimenting with a v2 to v3 converter and so I built a v2  
recognizer in v3 by manually converting the grammar to v3 syntax.   
Then I decided to start flipping keywords that have changed like  
protected->fragment in the lexer.  I added a simple action to the  
rule rule:

rule:	DOC_COMMENT?
		(	p='protected' {tokens.replace($p,"fragment");}
		|	'public'
		|	'private'
		)?
		id BANG? ARG_ACTION? ( 'returns' ARG_ACTION )?
		throwsSpec?
		ruleOptionsSpec?
		ACTION?
		COLON block ';'
		exceptionGroup?
	;

When I call toString() on the buffer it shows me the modified  
buffer.  All comments/whitespace come through unharmed. :)

Cool.  This TokenRewriteStream works really well when you want to  
tweak a file rather than completely modify it, though it will work  
just as well when I move the "header {...}" stream of tokens from  
before the class def to after the grammar def and call it @header.

Ter


More information about the antlr-interest mailing list