question on enable comments

May 26, 2010 at 2:51 PM

Thank you for giving us the excellent project: Antlr Csharp, which
help us a lot!
   I have some quetions on using it:
  1. I want to disable the filter on Comments, so I tried to do it by
comment out the {Skip();}

   :   ('///' ~('\n'|'\r')*  ('\r' | '\n')+);
   //{ Skip(); } ;
   :   ('//' ~('\n'|'\r')*  ('\r' | '\n')+);
   //{ Skip(); } ;
  (options {greedy=false;} : . )*
       //{ Skip(); } ;

   but the parser generated by it failed to parse a cs file which was
parsed successfully before. Do you know what is the problem?

Thank you very much

Jun 2, 2010 at 9:51 PM

After you comment out Skip(), those tokens now end up in the token stream.  There's no place in the grammar that expects LINE_COMMENT.  You would have to add it between every rule and sub-rule which is just impossible.  Your best bet is to put all those comments on another channel if you need to get to them.  Like this.
   :   ('///' ~('\n'|'\r')*  ('\r' | '\n')+);

   { channel = 4; } ;

That's about all I know about manipulating channels.  Just that they exist and that you can manipulate them with the Antlr.Runtime.  I hope that helps.

Dec 21, 2010 at 5:53 PM

This sure works. But perhaps there should be a dollar sign before "channel", and instead of using explicit number 4, maybe better use macro HIDDEN. So it's something like this:

{ $channel = HIDDEN; }

Note, however, that you can't do this in fragment rules (although Skip() is allowed !).

If you want to parse the hidden text in the parser rule, you may do somthing like this:


t = bar


     System.out.println($text.substring(0, $text.length() - $t.text.length() - 1)); //print out all hidden text before bar rule



Hope this helps.