[antlr-interest] [v3]Tracking Hidden Channels

Mark Mandel mark.mandel at gmail.com
Thu May 17 20:48:37 PDT 2007


Hi there,

I have a grammar that has has C style comments that are possible in it:

So,


COMMENT
	:
	'/*' ( options {greedy=false;} : . )* '*/'
	{
		$channel=HIDDEN;
	}
	;

LINE_COMMENT
	:
	'//' ~('\n'|'\r')* '\r'? '\n'
	{
		$channel=HIDDEN;
	}
	;

Now, when using this grammar to build an IDE, I want to be able to
track comments, so that I can colour code them as required.

My question here is, what is my best way to get at these comments, as
they are hidden tokens?

Reading:
http://www.antlr.org/wiki/pages/viewpage.action?pageId=1057

Says that I can walk backwards and forwards through the
CommonTokenStream to look for the my comment tokens - honestly, I'm
not sure how to do that, could someone possibly provide an example?

On that note however, I was thinking, maybe I should have my
whitespace on HIDDEN, but have my comments on channel 98, so I can
continue to ignore the WS, but just grab the comments I want.  Would
that be a good way to go about it?

The other thought I had was to simply have a LinkedList on my Lexer,
and do something like this:

LINE_COMMENT
	:
	'//' ~('\n'|'\r')* '\r'? '\n'
	{
		$channel=HIDDEN;
                commentList.add($token);
	}
(I believe $token will resolve to the current token, yes?)

Then I could retrieve my LinkedList off the Lexer after parsing was
complete, and loop through it to colour my comments as required.

I hope I'm making sense, and one of these approaches proves to be the
best one, if not, please point me in the right direction.

Thanks in advance,

Mark

-- 
E: mark.mandel at gmail.com
W: www.compoundtheory.com


More information about the antlr-interest mailing list