Reputation: 95
So I am having a problem with javacc and skipping comments. I have a multi line comment skip that can contain multiply comments within itself (A comment is anything that appears within /*
and a */
), I also use this code segement <"//" (~["\n"])* "\n">
to skip a single line comment. Both function independently of one another but when combined, the single line comment seems to break my multi line comment.
The parser no longer recognises the multi line comment and instead parses it as a combination of OTHER(/*
), ID etc.
Below is my code for the multi line comment and single line comment skip:
SKIP:
{
"/*" {commentnesting++;} : IN_COMMENT
}
<IN_COMMENT> SKIP :
{
"/*" {commentnesting++;}
| "*/" {commentnesting--;
if(commentnesting == 0) {
SwitchTo(DEFAULT);
}
}
| <~[]>
}
SKIP :
{
<"//" (~["\n"])* "\n">
}
My Questions are:
Upvotes: 3
Views: 2592
Reputation: 551
I ended up doing something like:
TOKEN: {
< COMMENT_END : "*/" >
}
SPECIAL_TOKEN: {
< COMMENT_START : "/*" > {
/*currently commented contents are dropped, but they can be attached to the special token*/
do {
Token nextToken = this.getNextToken();
if ("*/".equals(nextToken.image)) {
break;
}
} while (true);
}
}
It is a bit hacky but it also works when commenting code that contains other comments and even strings like: "/* */"....
Upvotes: 0
Reputation: 75222
Looks like that should work, except you seem to be missing some angle brackets. Instead of:
"/*" {commentnesting++;} : IN_COMMENT
...shouldn't it be:
<"/*"> {commentnesting++;} : IN_COMMENT
...and similarly with your second rule?
Upvotes: 4