[antlr-interest] Re: Trouble modifying Java source w/ ANTLR

cliftonccraig ccc at icsaward.com
Fri Feb 20 13:41:18 PST 2004


Last Stretch:

I have everything working wonderfully and now I'm in the home stretch.
I need a clever way of doing one of the following:

Either remove an inserted version string after detecting an already
coded one. (RewriteEngine.rollback()? How does this work?)
Or inserting the version strings at all pre-recorded classBlock
indices after reaching end of file and not finding them. I perferr the
1st option if possible as it seems cleaner. However I think I might be
able to hack the second option. Any suggestions?

Thanx in advance,
Cliff


--- In antlr-interest at yahoogroups.com, "cliftonccraig" <ccc at i...> wrote:
> I think I finally got it! I just start with the index of the
> identifier under the variableDeclarator rule and advance past the
> white space to get my starting point. I then advance to the SEMI token
> to get my stopping point and viola! Heres what I have so far:
> 
> variableDeclarator![AST mods, AST t]
> 	:	id:IDENT d:declaratorBrackets[t] v:varInitializer
> 		{
> 		    //*CCC- scan here for version indentifiers and replace the
> initializer with the new value.
> 		    if(id.getText().equals("VERSION"))
> 		    {
> 		        int start = ((TokenWithIndex)id).getIndex() + 1, stop = start;
> 		        TokenWithIndex next = engine.getToken(start);
> 		        //Skip white space...
> 		        while(next.getType()==WS) {start++; next =
> engine.getToken(start);}
> 		        //Advance to SEMI identifier...
> 		        stop = start;
> 		        while(next.getType()!=SEMI) {stop++; next =
> engine.getToken(stop);}
>                 stop--; //Don't include the SEMI in the replacement.
> 		        engine.replace(start,stop,"= \"MODIFIED!\"");
> 		    }
> 		    #variableDeclarator = #(#[VARIABLE_DEF,"VARIABLE_DEF"], mods,
> #(#[TYPE,"TYPE"],d), id, v);
> 		}
> 	;
> 
> Whew! I thought this would've ended up to be over a week-long project
> but Ive only spent a couple days. All I need now is to parameterize
> the whole thing so it can be a usefule re-usable component. Thnax
> everyone!
> 
> Cliff
> 
> 
> --- In antlr-interest at yahoogroups.com, "cliftonccraig" <ccc at i...> wrote:
> > Thank you everyone,
> > 
> > The thing is I'm new to both grammar engines and sed. So to me it's
> > the lesser of two evils. I've gotten a little further in this as I've
> > been able to figure out the Java grammar a little more. So far I've
> > narrowed where my mods go to the variableDeclarator rule. This is what
> > I have so far:
> > variableDeclarator![AST mods, AST t]
> > 	:	id:IDENT d:declaratorBrackets[t] v:varInitializer
> > 		{
> > 		    //*CCC- scan here for version indentifiers and replace the
> > initializer with the new value.
> > 		    if(id.getText().equals("VERSION"))
> > 		    {
> > 		        int start = ((TokenWithIndex)id).getIndex() + 1,
> > 		        stop = start + #d.getNumberOfChildren() +
> > #v.getNumberOfChildren() - 1;
> > 		        //System.out.println("Starting from " + start + " ending at
> > " + stop);
> > 		        //System.out.println("prevToken ==" + prevToken);
> > 		        engine.replace(start,stop,"\"MODIFIED!\"");
> > 		    }
> > 		    #variableDeclarator = #(#[VARIABLE_DEF,"VARIABLE_DEF"], mods,
> > #(#[TYPE,"TYPE"],d), id, v);
> > 		}
> > 	;
> > 
> > It gets a little hairy because the "stuff" I want to replace is not
> > defined as tokens in the grammar, rather they're defined as parser
> > rules. The rewrite engine example shows replacment using tokens but I
> > don't know how to get an accurate token count from the two AST objects
> >  at this point. I know the one (#d) is really just a place holder as
> > there will be no array declaration here but the other does not seem to
> > be yielding the right amount of child nodes. I think I'm at a
> > misunderstanding here (child-nodes != tokens)? When I run everything
> > with my above mods I get something like the following:
> > public final static String VERSION"MODIFIED!"= "2.3";
> >  where it clearly looks like the node count is evaluating to one and
> > the token count is much higher (accomodating for whitespace and such).
> > I don't want to hard code this because the version assignment may not
> > always be a simple assignment. It could be an expression and I want to
> > be able to replace the whole expression. Could you steer me in the
> > right direction here?
> > 
> > Cliff
> >



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list