[antlr-interest] handling /// comments

Johannes Luber jaluber at gmx.de
Fri Sep 19 07:47:55 PDT 2008


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


More information about the antlr-interest mailing list