[antlr-interest] Conditionally parsing comments in Java language?

Anton Safonov anton.safonov at refactorit.com
Tue Apr 26 03:27:08 PDT 2005


Hi, All!

Let's start with a slightly special Java example:

public void method() {
   int type = 0;

/*#SomeName#*///<editor-fold>
//-- type = 1;
//-- callOtherMethod();
/*$SomeName$*///</editor-fold>

/*#SomeName2#*///<editor-fold>
//-- type = 2;
/*$SomeName2$*///</editor-fold>

}

Now I try to change the original java.g (1.5) to detect such blocks, 
"uncomment" lines inside it on the fly, so that it builds the tree for 
the commented code as well.
The problem is that the lexer has rules SL_COMMENT/ML_COMMENT which 
detect that these are the comments and comment tokens are filtered and 
never reach the parser to be able to use any predicates or whatever.

I cannot let lexer to parse every comment, since the most of them are 
naturally not a Java code.
But at the same time some of the comments must be split into tokens to 
feed it to the parser.
So, the possible solution so fer: let comments pass to the parser, 
detect there that the comment can be parsed, kill leading "//--" and 
feed the block back to lexer etc.  Sounds weird, especially trying to 
keep the correct coordinates of the tokens/ASTs.

Any suggestions? May be there is a known canonical way to solve this?

Regards,
Anton



More information about the antlr-interest mailing list