User: rinkrank
  Date: 02/02/19 11:13:51

  Modified:    javacc   Java1.2-b.jjt
  Log:
  Full refactoring:
  -cleaned up bad code ;-)
  -introduced proxy (GoF) for performance
  -moved most classes to main xjavadoc package
  
  Revision  Changes    Path
  1.6       +299 -68   xjavadoc/javacc/Java1.2-b.jjt
  
  Index: Java1.2-b.jjt
  ===================================================================
  RCS file: /cvsroot/xdoclet/xjavadoc/javacc/Java1.2-b.jjt,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -w -r1.5 -r1.6
  --- Java1.2-b.jjt     18 Feb 2002 01:17:10 -0000      1.5
  +++ Java1.2-b.jjt     19 Feb 2002 19:13:51 -0000      1.6
  @@ -53,17 +53,69 @@
   
   package xjavadoc.ast;
   
  -// TODO move SourceClass to xjavadoc package,
  -// making it package private
  -import xjavadoc.parser.SourceClass;
  +import java.lang.reflect.Modifier;
  +import java.util.StringTokenizer;
  +import xjavadoc.*;
   
   public class JavaParser
   {
  -   // TODO: we need to modify SourceClass' constructor
  -   private SourceClass _sourceClass; // = new SourceClass();
  +   private static org.apache.log4j.Category _log = 
org.apache.log4j.Category.getInstance(JavaParser.class.getName());
   
  -   public SourceClass getSourceClass() {
  -      return _sourceClass;
  +   private static SourceClass _sourceClass;
  +   private static MethodImpl _methodImpl;
  +   private static XExecutableMember _executableMember;
  +
  +   private static Token getJavaDocSpecialToken(Token t) {
  +      Token result = null;
  +
  +      Token tt = t.specialToken;
  +      if (tt != null) {
  +         while (tt.specialToken != null) {
  +            tt = tt.specialToken;
  +         }
  +         while (tt != null) {
  +            if (tt.kind == JavaParserConstants.FORMAL_COMMENT) {
  +               // it's JavaDoc
  +               //_log.debug(tt.image);
  +               result = tt;
  +            }
  +            else if (tt.kind == JavaParserConstants.SINGLE_LINE_COMMENT || tt.kind 
== JavaParserConstants.MULTI_LINE_COMMENT) {
  +               // reset it. some other comment is standalone or followed what could 
have been a javadoc comment
  +               //_log.debug("Not javadoc anyway:" + tt.image);
  +               result = null;
  +            }
  +            tt = tt.next;
  +         }
  +      }
  +      return result;
  +   }
  +
  +   private static void setInterfaces( String nameList) {
  +      if( nameList != null ) {
  +         StringTokenizer st = new StringTokenizer(nameList, ",");
  +         while( st.hasMoreTokens() ) {
  +            _sourceClass.addInterface( st.nextToken() );
  +         }
  +      }
  +   }
  +
  +   private static void setExceptions( String nameList) {
  +      if( nameList != null ) {
  +         StringTokenizer st = new StringTokenizer(nameList, ",");
  +         while( st.hasMoreTokens() ) {
  +            _methodImpl.addThrownException( st.nextToken() );
  +         }
  +      }
  +   }
  +
  +   private class Type {
  +      public String type;
  +      public int dimension;
  +      
  +      public Type( String t, int d ) {
  +         type = t;
  +         dimension = d;
  +      }
      }
   }
   
  @@ -336,15 +388,19 @@
    * Program structuring syntax follows.
    */
   
  -ASTCompilationUnit CompilationUnit() :
  -{}
  +void CompilationUnit( SourceClass sourceClass ) :
  +{
  +   _sourceClass = sourceClass;
  +}
   {
     [ PackageDeclaration() ]
     ( ImportDeclaration() )*
     ( TypeDeclaration() )*
     <EOF>
                   {
  -               return jjtThis;
  +      // _sourceClass must know about ASTCompilationUnit
  +      // if it wants to mutate or print the code
  +      _sourceClass.setCompilationUnit( jjtThis );
                }       
   }
   
  @@ -355,11 +411,7 @@
   {
     "package" packageName=Name() ";"
     {
  -     /*
  -     TODO:
  -     _sourceClass.setPackageName( packageName );
  -     */
  -     System.out.println("Package name:" + packageName);
  +     _sourceClass.setContainingPackage( packageName );
     }
   }
   
  @@ -377,11 +429,9 @@
     ";"
     {
        if( isPackage ) {
  -        // _sourceClass.addImportedPackage(importedElement);
  -        System.out.println( "Imported package:" + importedElement );
  +        _sourceClass.addImportedPackage(importedElement);
        } else {
  -        // _sourceClass.addImportedClass(importedElement);
  -        System.out.println( "Imported class:" + importedElement );
  +        _sourceClass.addImportedClass(importedElement);
        }
     }
   }
  @@ -402,24 +452,68 @@
    * Declaration syntax follows.
    */
   
  +/*
  +Javadoc is stored in special tokens, and will either be
  +attached to the first modifier token (if there are any modifiers)
  +or to the first token in UnmodifiedClassDeclaration
  +*/
   void ClassDeclaration() :
  -{}
   {
  -  ClassModifierDeclaration()
  -  UnmodifiedClassDeclaration()
  +   Token t;
   }
  -
  -void ClassModifierDeclaration() :
  -{}
   {
  -  ( "abstract" | "final" | "public" | "strictfp" )*
  +  ( 
  +    t="abstract" {
  +       _sourceClass.setToken( t );
  +       _sourceClass.addModifier( Modifier.ABSTRACT );
  +       _sourceClass.setDoc(getJavaDocSpecialToken( t ));
  +    }
  +  | t="final" {
  +       _sourceClass.setToken( t );
  +       _sourceClass.addModifier( Modifier.FINAL );
  +       _sourceClass.setDoc(getJavaDocSpecialToken( t ));
  +    }
  +  | t="public" { 
  +       _sourceClass.setToken( t );
  +       _sourceClass.addModifier( Modifier.PUBLIC );
  +       _sourceClass.setDoc(getJavaDocSpecialToken( t ));
  +    }
  +  | t="strictfp" {
  +       _sourceClass.setToken( t );
  +       _sourceClass.addModifier( Modifier.STRICT );
  +       _sourceClass.setDoc(getJavaDocSpecialToken( t ));
  +    }
  +  )*
  +  UnmodifiedClassDeclaration()
   }
   
   void UnmodifiedClassDeclaration() :
  -{}
   {
  -  "class" <IDENTIFIER> [ "extends" Name() ] [ "implements" NameList() ]
  -  ClassBody()
  +   Token ct;
  +   Token t;
  +   String superclass = null;
  +   String interfaces = null;
  +}
  +{
  +   ct="class" {
  +       _sourceClass.setToken( ct );
  +       _sourceClass.setDoc(getJavaDocSpecialToken( ct ));
  +       _sourceClass.setInterface( false );
  +   }
  +   t=<IDENTIFIER> {
  +      _sourceClass.setName(t.image);
  +   }
  +   [ "extends" superclass=Name() ]
  +   [ "implements" interfaces=NameList() ] {
  +      setInterfaces(interfaces);
  +   }
  +   ClassBody() {
  +      if( superclass != null ) {
  +         _sourceClass.setSuperclass(superclass);
  +      } else {
  +         _sourceClass.setSuperclass("java.lang.Object");
  +      }
  +   }
   }
   
   void ClassBody() :
  @@ -465,9 +559,26 @@
   }
   
   void InterfaceDeclaration() :
  -{}
   {
  -  ( "abstract" | "public" | "strictfp")*
  +   Token t;
  +}
  +{
  +  ( 
  +    t="abstract" {
  +       _sourceClass.setToken( t );
  +       _sourceClass.setDoc(getJavaDocSpecialToken( t ));
  +    }
  +  | t="public" { 
  +       _sourceClass.setToken( t );
  +       _sourceClass.addModifier( Modifier.PUBLIC );
  +       _sourceClass.setDoc(getJavaDocSpecialToken( t ));
  +    }
  +  | t="strictfp" {
  +       _sourceClass.setToken( t );
  +       _sourceClass.addModifier( Modifier.STRICT );
  +       _sourceClass.setDoc(getJavaDocSpecialToken( t ));
  +    }
  +  )*
     UnmodifiedInterfaceDeclaration()
   }
   
  @@ -479,10 +590,25 @@
   }
   
   void UnmodifiedInterfaceDeclaration() :
  -{}
   {
  -  "interface" <IDENTIFIER> [ "extends" NameList() ]
  +   Token it;
  +   Token t;
  +   String extendedInterfaces = null;
  +}
  +{
  +  it="interface" t=<IDENTIFIER> [ "extends" extendedInterfaces=NameList() ]
     "{" ( InterfaceMemberDeclaration() )* "}"
  +  {
  +      _sourceClass.setToken( it );
  +      _sourceClass.addModifier( Modifier.ABSTRACT );
  +      _sourceClass.setName( t.image );
  +      _sourceClass.setInterface( true );
  +      _sourceClass.setDoc(getJavaDocSpecialToken( t ));
  +      
  +      if( extendedInterfaces != null ) {
  +         setInterfaces(extendedInterfaces);
  +      }
  +  }
   }
   
   void InterfaceMemberDeclaration() :
  @@ -513,10 +639,16 @@
     VariableDeclaratorId() [ "=" VariableInitializer() ]
   }
   
  -void VariableDeclaratorId() :
  -{}
  +Type VariableDeclaratorId() :
  +{
  +   Token t;
  +   int dimension = 0;
  +}
   {
  -  <IDENTIFIER> ( "[" "]" )*
  +  t=<IDENTIFIER> ( "[" "]" { dimension++; } )*
  +  {
  +     return new Type(t.image, dimension);
  +  }
   }
   
   void VariableInitializer() :
  @@ -533,21 +665,78 @@
     "{" [ VariableInitializer() ( LOOKAHEAD(2) "," VariableInitializer() )* ] [ "," ] 
"}"
   }
   
  -ASTMethodDeclaration MethodDeclaration() :
  -{}
  +void MethodDeclaration() :
   {
  -  ( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" 
| "synchronized" | "strictfp")*
  -  ResultType() MethodDeclarator() [ "throws" NameList() ]
  -  ( Block() | ";" )
  +   Token t;
  +   _methodImpl = new MethodImpl(_sourceClass, null);
  +   _executableMember = _methodImpl;
  +   String exceptions = null;
  +}
  +{
  +  ( 
  +  t="public" { 
  +       _methodImpl.addModifier( Modifier.PUBLIC );
  +       _methodImpl.setDoc(getJavaDocSpecialToken( t ));
  +       _methodImpl.setToken( t );
  +    }
  +  | t="protected" { 
  +       _methodImpl.addModifier( Modifier.PROTECTED );
  +       _methodImpl.setDoc(getJavaDocSpecialToken( t ));
  +       _methodImpl.setToken( t );
  +    }
  +  | t="private" { 
  +       _methodImpl.addModifier( Modifier.PRIVATE );
  +       _methodImpl.setDoc(getJavaDocSpecialToken( t ));
  +       _methodImpl.setToken( t );
  +    }
  +  | t="static" { 
  +       _methodImpl.addModifier( Modifier.STATIC );
  +       _methodImpl.setDoc(getJavaDocSpecialToken( t ));
  +       _methodImpl.setToken( t );
  +    }
  +  | t="abstract" {
  +       _methodImpl.addModifier( Modifier.ABSTRACT );
  +       _methodImpl.setDoc(getJavaDocSpecialToken( t ));
  +       _methodImpl.setToken( t );
  +    }
  +  | t="final" {
  +       _methodImpl.addModifier( Modifier.FINAL );
  +       _methodImpl.setDoc(getJavaDocSpecialToken( t ));
  +       _methodImpl.setToken( t );
  +    }
  +  | t="native" {
  +       _methodImpl.addModifier( Modifier.NATIVE );
  +       _methodImpl.setDoc(getJavaDocSpecialToken( t ));
  +       _methodImpl.setToken( t );
  +    }
  +  | t="synchronized" {
  +       _methodImpl.addModifier( Modifier.SYNCHRONIZED );
  +       _methodImpl.setDoc(getJavaDocSpecialToken( t ));
  +       _methodImpl.setToken( t );
  +    }
  +  | t="strictfp" {
  +       _methodImpl.addModifier( Modifier.STRICT );
  +       _methodImpl.setDoc(getJavaDocSpecialToken( t ));
  +       _methodImpl.setToken( t );
  +    }
  +  )*
  +  ResultType() MethodDeclarator() [ "throws" exceptions=NameList() ]
     {
  -     return jjtThis;
  +     setExceptions( exceptions );
  +     _sourceClass.addMethod(_methodImpl);
     }
  +  ( Block() | ";" )
   }
   
   void MethodDeclarator() :
  -{}
   {
  -  <IDENTIFIER> FormalParameters() ( "[" "]" )*
  +   Token t;
  +}
  +{
  +  t=<IDENTIFIER> {
  +     _methodImpl.setName( t.image );
  +  }
  +  FormalParameters() ( "[" "]" )*
   }
   
   void FormalParameters() :
  @@ -557,9 +746,20 @@
   }
   
   void FormalParameter() :
  -{}
   {
  -  [ "final" ] Type() VariableDeclaratorId()
  +   Type type;
  +   Type variable;
  +}
  +{
  +  [ "final" ] type=Type() variable=VariableDeclaratorId()
  +  {
  +     ParameterImpl param = new ParameterImpl( 
  +        _executableMember,
  +        type.type,
  +        variable.type,
  +        type.dimension + variable.dimension
  +     );
  +  }
   }
   
   void ConstructorDeclaration() :
  @@ -593,38 +793,59 @@
    * Type, name and expression syntax follows.
    */
   
  -void Type() :
  -{}
  +Type Type() :
   {
  -  ( PrimitiveType() | Name() ) ( "[" "]" )*
  +   String type;
  +   int dimension = 0;
  +}
  +{
  +  ( type=PrimitiveType() | type=Name() ) 
  +  ( "[" "]" { dimension++; } )*
  +  {
  +     return new Type( type, dimension );
  +  }
   }
   
  -void PrimitiveType() :
  -{}
  +String PrimitiveType() :
   {
  -  "boolean"
  +   Token t;
  +}
  +{
  +  t="boolean" { return t.image; }
   |
  -  "char"
  +  t="char" { return t.image; }
   |
  -  "byte"
  +  t="byte" { return t.image; }
   |
  -  "short"
  +  t="short" { return t.image; }
   |
  -  "int"
  +  t="int" { return t.image; }
   |
  -  "long"
  +  t="long" { return t.image; }
   |
  -  "float"
  +  t="float" { return t.image; }
   |
  -  "double"
  +  t="double" { return t.image; }
   }
   
   void ResultType() :
  -{}
   {
  -  "void"
  +   Token t;
  +   Type type;
  +}
  +{
  +  t="void" {
  +     _methodImpl.setReturnType( "void" );
  +     _methodImpl.setReturnDimension( 0 );
  +     _methodImpl.setToken( t );
  +  }
   |
  -  Type()
  +  type=Type()
  +  {
  +     _methodImpl.setReturnType( type.type );
  +     _methodImpl.setReturnDimension( type.dimension );
  +     _methodImpl.setToken( ((SimpleNode)jjtThis).first );
  +  }
   }
   
   String Name() :
  @@ -651,12 +872,22 @@
     }
   }
   
  -void NameList() :
  -{}
  +String NameList() :
   {
  -  Name()
  -  ( "," Name()
  +   String s;
  +   StringBuffer sb = new StringBuffer();
  +}
  +{
  +  s=Name() {
  +     sb.append(s);
  +  }
  +  ( "," s=Name() {
  +     sb.append(",").append(s);
  +  }
     )*
  +  {
  +     return sb.toString();
  +  }
   }
   
   
  
  
  

_______________________________________________
Xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel

Reply via email to