Extends the grammar to include the annotation type declaration. The current impl...
authoryeom <yeom>
Wed, 27 Apr 2011 02:00:10 +0000 (02:00 +0000)
committeryeom <yeom>
Wed, 27 Apr 2011 02:00:10 +0000 (02:00 +0000)
Annotation type is represented by class descriptor since annotation type declaration is a special kind of interface declaration. Later, we need to implement semantic checkings for annotations.

Robust/src/IR/Tree/BuildIR.java
Robust/src/Parse/java14.cup

index 89d0a5c46c66b4a389a1518f3625969e4789ca3e..e31d6d14f50b06db2bb234f4bc5e30d5aa99a7de 100644 (file)
@@ -165,6 +165,12 @@ public class BuildIR {
             toanalyze.add(cn);
           cn.setSourceFileName(sourcefile);
           state.addClass(cn);
+        } else if(isNode(type_pn,"annotation_type_declaration")){
+          ClassDescriptor cn=parseAnnotationTypeDecl(type_pn);
+          if (toanalyze != null)
+            toanalyze.add(cn);
+          cn.setSourceFileName(sourcefile);
+          state.addClass(cn);
         } else {
           throw new Error(type_pn.getLabel());
         }
@@ -172,6 +178,8 @@ public class BuildIR {
     }
   }
   
+  
+  
   //This kind of breaks away from tradition a little bit by doing the file checks here
   // instead of in Semantic check, but doing it here is easier because we have a mapping early on
   // if I wait until semantic check, I have to change ALL the type descriptors to match the new
@@ -265,6 +273,44 @@ public class BuildIR {
     cn.addEnumConstant(pn.getChild("name").getTerminal());
   }
   
+  private ClassDescriptor parseAnnotationTypeDecl(ParseNode pn){
+    ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
+    cn.setImports(mandatoryImports);
+    ParseNode modifiers=pn.getChild("modifiers");
+    if(modifiers!=null){
+      cn.setModifiers(parseModifiersList(modifiers));
+    }
+    parseAnnotationTypeBody(cn,pn.getChild("body"));
+    return cn;
+  }
+  
+  private void parseAnnotationTypeBody(ClassDescriptor cn, ParseNode pn){
+    ParseNode list_node=pn.getChild("annotation_type_element_list");
+    if(list_node!=null){
+      ParseNodeVector pnv = list_node.getChildren();
+      for (int i = 0; i < pnv.size(); i++) {
+        ParseNode element_node = pnv.elementAt(i);
+        if (isNode(element_node, "annotation_type_element_declaration")) {
+          ParseNodeVector elementProps = element_node.getChildren();
+          String identifier=null;
+          TypeDescriptor type=null;
+          Modifiers modifiers=new Modifiers();
+          for(int eidx=0; eidx<elementProps.size(); eidx++) {
+            ParseNode prop_node=elementProps.elementAt(eidx);
+            if(isNode(prop_node,"name")){
+              identifier=prop_node.getTerminal();
+            }else if(isNode(prop_node,"type")){
+              type=parseTypeDescriptor(prop_node);
+            }else if(isNode(prop_node,"modifier")){
+              modifiers=parseModifiersList(prop_node);
+            }
+          }
+          cn.addField(new FieldDescriptor(modifiers, type, identifier, null, false));
+        }
+      }
+    }
+  }
+  
   public ClassDescriptor parseInterfaceDecl(ParseNode pn, String packageName) {
     ClassDescriptor cn;
     if(packageName == null) {
@@ -309,6 +355,8 @@ public class BuildIR {
     }
   }
   
+  
+  
   private void parseInterfaceConstant(ClassDescriptor cn, ParseNode pn) {
     if (pn!=null) {
       parseFieldDecl(cn,pn.getChild("field_declaration"));
index 87079c4f0bdd9e204fdc443a66c237de9a88b4d5..ab4a13f7ad940bf41ec102d688284d6b4c460c57 100644 (file)
@@ -164,7 +164,7 @@ non terminal ParseNode explicit_constructor_invocation;
 non terminal ParseNode location_order_declaration, location_order_list, location_order;
 // 19.9.1) Interface Declarations
 non terminal ParseNode interface_declaration;
-non terminal normal_interface_declaration, annotation_type_declaration;
+non terminal ParseNode normal_interface_declaration, annotation_type_declaration;
 non terminal ParseNode extends_interfaces_opt, extends_interfaces;
 non terminal ParseNode interface_body;
 non terminal ParseNode interface_member_declarations_opt, interface_member_declarations;
@@ -1338,7 +1338,9 @@ interface_declaration ::=
        pn.addChild("interfacebody").addChild(body);
        RESULT=pn;
        :}
-       | annotation_type_declaration
+       | annotation_type_declaration:atd{:
+        RESULT=atd;
+       :}
        ;
 extends_interfaces_opt ::=
        {: RESULT=new ParseNode("empty",parser.lexer.line_num); :}
@@ -1406,30 +1408,83 @@ abstract_method_declaration ::=
                RESULT=pn;
        :}
        ;
-/*
+
 annotation_type_declaration ::=
-               AT INTERFACE IDENTIFIER annotation_type_body
-       |       modifiers_at INTERFACE IDENTIFIER annotation_type_body
+               AT INTERFACE IDENTIFIER:id annotation_type_body:atb{:
+               ParseNode pn=new ParseNode("annotation_type_declaration",parser.lexer.line_num);
+               pn.addChild("name").addChild(id);
+               pn.addChild("body").addChild(atb);
+               RESULT=pn;
+
+       :}
+       |       modifiers_at:ma INTERFACE IDENTIFIER:id annotation_type_body:atb{:
+               ParseNode pn=new ParseNode("annotation_type_declaration",parser.lexer.line_num);
+               pn.addChild("name").addChild(id);
+               pn.addChild("modifiers").addChild(ma);
+               pn.addChild("body").addChild(atb);              
+               RESULT=pn;              
+       :}
        ;
 annotation_type_body ::=
-               LBRACE annotation_type_element_declarations_opt RBRACE
+               LBRACE annotation_type_element_declarations_opt:atedo RBRACE{:
+               RESULT=atedo;
+       :}
        ;
-annotation_type_element_declarations_opt ::=
-       |       annotation_type_element_declarations
+annotation_type_element_declarations_opt ::={:
+               RESULT=new ParseNode("empty",parser.lexer.line_num);
+        :}
+       |       annotation_type_element_declarations:ated{:
+               RESULT=ated;
+       :}
        ;
 annotation_type_element_declarations ::=
-               annotation_type_element_declaration
-        |      annotation_type_element_declarations annotation_type_element_declaration
+               annotation_type_element_declaration:ated{:
+               ParseNode pn=new ParseNode("annotation_type_element_list",parser.lexer.line_num);
+               pn.addChild(ated);
+               RESULT=pn;
+       :}
+        |      annotation_type_element_declarations:ateds annotation_type_element_declaration:ated{:
+               ateds.addChild(ated);
+               RESULT=ateds;   
+       :}
        ;
 annotation_type_element_declaration ::=
-               constant_declaration
-       |       modifiers_opt type IDENTIFIER LPAREN RPAREN default_value_opt SEMICOLON
-        |      class_declaration 
-        |      enum_declaration 
-        |      interface_declaration 
-       |       SEMICOLON
+               constant_declaration:cd {:
+               RESULT=cd;              
+       :}
+       |       modifiers_opt:mo type:type IDENTIFIER:id LPAREN RPAREN default_value_opt:dvo SEMICOLON {:
+               ParseNode pn=new ParseNode("annotation_type_element_declaration",parser.lexer.line_num);        
+               pn.addChild("modifier").addChild(mo);   
+               pn.addChild("type").addChild(type);
+               pn.addChild("name").addChild(id);
+               pn.addChild("defaultvalue").addChild(dvo);
+               RESULT=pn;
+       :}
+        |      class_declaration:cd{:
+               RESULT=cd;
+       :} 
+        |      enum_declaration:ed{:
+               RESULT=ed;
+       :}
+        |      interface_declaration:id{:
+               RESULT=id;
+       :}
+       |       SEMICOLON{:
+               RESULT=new ParseNode("empty",parser.lexer.line_num);
+       :}
         ;
-*/
+default_value_opt ::= {:
+               RESULT=new ParseNode("empty",parser.lexer.line_num);
+        :}
+        |      default_value:dv{:
+               RESULT=dv;
+       :}
+        ;
+default_value ::= DEFAULT element_value:ev {:
+             RESULT=ev;
+       :}
+        ;
+
 // 19.10) Arrays
 array_initializer ::=
                LBRACE variable_initializers:var_init_list COMMA RBRACE {: