[antlr-interest] Switching Java and JavaDoc with HiddenTokens

Jörg Rech joerg.rech at gmail.com
Sun Jun 26 13:58:41 PDT 2005


Hi,
  I'm looking for a convienient way to parse Java source code and 
accociate comments and JavaDoc to the proper element in the resulting 
datastructures (e.g. ASTTree). If I use the example from the tutorial 
and hide JavaDoc comments in a second stream I can only access them if I 
have the linked Tokens from the Java-Stream. Now if I want to collect 
every comment or javadoc before a class-definition some of them are 
hidden before the class-token in import or package declarations. As the 
standard Tokens do not have a "father"-link (only child and sibling) I 
have no way of collecting comments by traversing the AST up to the root.

Package
|
Import -> getHiddenBefore() = Comment1
|
Class -> getHiddenBefore() = null (e.g. I cannot access Comment1 nor 
Import)

Now I think that I either need a Token that stores father-elements or I 
would have to store evere hidden element from every element from the 
grammar (statements, declarations, etc.) in a external data structure 
and collect the relevant one by myself. But the last solution would 
require to rewrite either the JavaParser or the JavaTreeWalker heavily.

And here a short example:

PACKAGE abc;
// Comment 1
IMPORT xyz;
/** javadoc1 for class */
public class X {
  Field1;
  /* Comment2 */
  Field2;
  /** javadoc2 for method */
  public void doSomethin() {
    // Comment3 in method
    Statement1;
  }
}

And I want to extract the following elements (e.g., CLASS) with 
associated comment in braces:
CLASS(comment1, javadoc1, comment2)
METHOD(javaDoc2, comment3)

So every javadoc should be associated this the following class or method 
and comments should be associated with the enclosing body

I hope someone had a similar problem and knows a convienient solution.
Many thanks in advance,
Joerg.

-- 
Joerg Rech
Computer Scientist        Web:   http://www.joerg-rech.de
Tel.: (+49|0) 631/10653   email: joerg.rech at gmail.com



More information about the antlr-interest mailing list