[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Help with Java coding utility



Hi Emmanuelle.
I think, if you have preserve original formatting of your source file, I
should try this algorithm:
1. build your AST Tree
2. for each class node 
	- build list of all declared declared fields
	- find "}" ending class declaration
	- put code corresponding to public fields just before this point

If you want to leave your source file unchanged you should rewrite all
passage from beginning of class to ending "}" (without it) prior writing
code corresponding to declared fields 

Pawel

-----Original Message-----
From: Emmanuele Sordini [mailto:vega@ulisse.it]
Sent: Monday, May 22, 2000 8:11 PM
To: SableCC Mailing List
Subject: Re: Help with Java coding utility


Hello!
Thank everybody for your suggestions. Looks like I plunged into a not
so easy task. However, I will try to do my best and I'll let you know,
if I come up with something usable.

Bye
Emmanuele

"Etienne M. Gagnon" wrote:
> 
> Hi Emmanuele,
> 
> I fully agree with Othman's suggestions.  I just wanted to add a little
something...
> 
> I know that this hasn't been advertized very well in the documentation
(all my fault;-), but the generated parser class has a public
"ignoredTokens" field that keeps record of all ignored tokens.  This means
that all comments & blanks are not lost at parse time.  This is important
for your project,
> as you will need these tokens to write back your modified classes.
> 
> Here's how it works:
> 
> Assume you have the following input program:
> 
> ---- BEGIN ----
> /* an empty class */
> class Empty{}
> ---- END ----
> 
> Here's the resulting token stream:
> Ign: [/* an empty class */]
>      [\n]
> AST: [class]
> Ign: [ ]
> AST: [class]
> Ign: [ ]
> AST: [Empty]
> AST: [{]
> AST: [}]
> AST: [EOF]
> 
> When an Ignored Token is matched, it is added into a Linked list.  This
linked list is then associated with the next "following" AST "leaf" through
the Parser.ignoredTokens map.
> 
> So, for example, if you are on token node [class] in the AST, you can
write the following:
> 
> LinkedList list = (LinkedList) Parser.ignoredTokens.getIn(classnode);
> for(Iterator i = list.iterator(); i.hasNext(); )
> {
>   System.out.println(i.next());
> }
> 
> This will print:
> /* an empty class */
>         <- newline ;-)
> 
> If you do Parser.ignoredTokens.getIn(somenode) for a token that isn't
preceeded by ignored tokens, then you get "null".
> 
> I hope this helps!
> 
> Etienne
> 
> Othman Alaoui wrote:
> >
> > Hi Emmanuele,
> >
> > on the top of my head, this seems doable as an AST transformation:
> > - build your AST
> > - iterate on the list of (public?) fields of each class node:
> >     - for each field, create a method node in a bottom-up fashion that
> > contains the corresponding simple statement (AAssignStatement or
something
> > like that). Do this for both the setter and the getter.
> >     - add the 2 methods to the list of methods embedded in the class
node by
> > using the appropriate setter of the class node
> > - pretty-print the transformed AST (preferably with your own
pretty-printer,
> > if you want the original formatting of your source file: this actually
may
> > not be that obvious...)
> >
> > Hope it helps!
> > Othman
> >