I have a language that extends Java. I.e. I can compile Java, but have some additional declarations.
So, I want to see those declarations with my compiler, but hide then from javac. For this I use a special not a comment block, like
package foo;
/*{
pragma enable feature;
}*/
include java.util.*;
The idea is that javac will think /*{
… }*/
is a multiline comment, but my lexer just ignores /*{
and }*/
tokens. That’s how it was implemented by JavaCC. Now I need to switch to use JFlex. And I can’t find a way to specify these not a comment tokens.
Let’s say I start with
LINE_COMMENT= "//" .*
BLOCK_COMMENT= "/" * ( [^*] | *+ [^*/] )* ( *+ "/" )?
Then add my special comments
SPECIAL_COMMENT= "/*{" | "}*/"
This does not help, since JFlex always try to find the longest match, and
/*{
pragma enable feature;
}*/
Is matched by BLOCK_COMMENT
(it’s longer then SPECIAL_COMMENT
).
Ok, I reject {
to be the third character, like
LINE_COMMENT= "//" .*
SPECIAL_COMMENT= "/*{" | "}*/"
BLOCK_COMMENT= "/" * [^{] ( [^*] | *+ [^*/] )* ( *+ "/" )?
Now block comment cannot start with /*{
and lexer returns SPECIAL_COMMENT
. But for
/**/
/*{
pragma enable feature;
}*/
code my trick does not work, since BLOCK_COMMENT
starts with /*
, then eats any character except {
(the second *
character) and starts to look over for the comment end (*/
is not the end, sine *
was already consumed).
I spent many hours trying to find a way for this /**/
case, but no luck. Any sugestions?
Maxim Kizub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.