[antlr-interest] adding content at end of rule withTokenRewriteStream

Jean Marc Vanel JeanMarc.Vanel at mathworks.fr
Fri Feb 23 06:52:40 PST 2007



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Terence Parr
> Sent: Wednesday, February 21, 2007 8:51 PM
> To: ANTLR Interest
> Subject: Re: [antlr-interest] adding content at end of rule
> withTokenRewriteStream
> 
> Hi.  Try using $rule1.stop attribute to get last token.
[Jean-Marc Vanel] With that the generated Java doesn't compile.
If you want a ready to run test, here it is.
It produces this:
Exception in thread "main" java.lang.NullPointerException
	at org.antlr.runtime.TokenRewriteStream.insertAfter(TokenRewriteStream.java:300)
------------------
grammar testAddContentAtEndOfRule;

@parser::members {
	TokenRewriteStream tokens;
}

name_or_pointer
@init {
    tokens = (TokenRewriteStream)input;
}
    	:	NAME (star='*')?
		{ tokens.insertAfter( star.stop, " == after '*' ===" ); }
	;

NAME
    : LETTER LETTER *
    ;

fragment LETTER
    : 'a'..'z'
    | 'A'..'Z'
    ;

WS  : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=99;}
    ;
----------------------------
import org.antlr.runtime.*;
public class MainTweakTestBug {
	public static void main(String[] args) throws Exception {
		CharStream input = new ANTLRStringStream ( "var" );
		// doesn't crash with "var*"
		testAddContentAtEndOfRuleLexer lex = new testAddContentAtEndOfRuleLexer(input);
		TokenStream tokens = new TokenRewriteStream(lex);
		testAddContentAtEndOfRuleParser parser = new testAddContentAtEndOfRuleParser(tokens);
		parser.name_or_pointer();
		System.out.println(tokens);
	}
}
-------------------------------


> Ter
> On Feb 21, 2007, at 8:32 AM, Jean Marc Vanel wrote:
> 
> > Suppose I have such rules :
> >
> >
> >
> > rule1 : A B* C?
> >
> > rule2 : A BB? C*
> >
> > rule3 : A BBB* C*
> >
> >
> >
> > I'd like to add content after C (when C is present ), using
> > TokenRewriteStream.insertAfter() .
> >
> > When C is not present, calling . insertAfter( C.stop, "my
> > content" ) triggers a NullPointerException.
> >
> >
> >
> > In all the « tweak »  example, one only adds content around
> > unconditional items, i.e. ones with cardinality 1 or + .
> >
> >
> >
> > Thanks
> >
> >
> >
> > Jean Marc Vanel
> > Developer
> > Journal: file://///fr-vanelj-dev.mathworks.fr/Aslrtwn/scp_project/
> > perso/jvanel/journal.html
> > The MathWorks      Tel : +33 (0)1 41 14 67 24
> > 20 rue Troyon      Cell: +33 (0)6 89 16 29 52
> > 92316 Sèvres       Fax : +33 (0)1 41 14 67 15
> > France             Web : http://www.mathworks.fr
> >
> >
> >



More information about the antlr-interest mailing list