[antlr-interest] handling /// comments
Johannes Luber
jaluber at gmx.de
Sun Sep 21 03:56:43 PDT 2008
ajk . schrieb:
>
>
> On Fri, Sep 19, 2008 at 10:47 PM, Johannes Luber <jaluber at gmx.de
> <mailto:jaluber at gmx.de>> wrote:
>
> ajk . schrieb:
> > Hi
> >
> > I was wondering if somebody has an idea how to solve the following
> > problem. Lets see if I can describe the issue in a proper way.
> >
> > We have a translator that translates a scripting language to C#.
> >
> > We now need to support ///<summary>.. in C#
> >
> > By adding the /// in the scripting language we were hoping just to
> move
> > this over to C#:
> >
> > example
> >
> > /// <summary>
> > /// some function
> > /// </summary>
> > myfunction returns int
> > begin
> > bla();
> > end
> >
> > ->
> >
> > C#
> > /// <summary>
> > /// some function
> > /// </summary>
> > int myfunction()
> > {
> > bla();
> > }
> >
> > So far so good, I would associate the comments to the following
> function
> > which is a node in the AST, simple to do a parser rule for this.
> >
> > But in C# (as well as the scripting language) you can write comments
> > anywhere in the code in whatever form you want e.g.
> >
> > /// hello
> >
> > or
> >
> > ///////////////////////////////////////////
> > //// this freestanding comment
> > ////////////////////////////////////////////
> >
> > How do you filter away these things? How do I write a parser rule that
> > allows comments anywhere in the original code but only move the
> relevant
> > ones? i.e. the ones associated with certain elements like functions,
> > variable declarations etc.
> >
> > TIA
> > Anders.
>
> There are two possible ways to solve this issue. The first is to add
> COMMENT nodes everywhere they may appear. But this reduces the
> readability a lot. The other way is to put off the COMMENT tokens
> off-channel and to scan in front of matched function definitions for a
> COMMENT token yourself.
>
> Johannes
>
>
>
> The latter method sounds better, our language is very large so the less
> changes to the grammar the better. In v2 (which I am not familiar with)
> how to set tokens to come into the hidden channel? In v3 you just write
> $Channel=HIDDEN right?
Are you using v2? I haven't used it myself, too, so I can't help you
there. In v3 one uses "{$channel=HIDDEN;}", but theoretically one can
use other values, which is useful to distinguish between comments and
other whitespace.
Johannes
>
> thanks
> Anders.
More information about the antlr-interest
mailing list