[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
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
> >