Pārlūkot izejas kodu

Associations are like Classes and can also have attributes

Joeri Exelmans 1 gadu atpakaļ
vecāks
revīzija
107f2f750e
1 mainītis faili ar 15 papildinājumiem un 11 dzēšanām
  1. 15 11
      concrete_syntax/textual_cd/parser.py

+ 15 - 11
concrete_syntax/textual_cd/parser.py

@@ -42,7 +42,7 @@ constraint: CODE | INDENTED_CODE
 
 class_: [ABSTRACT] "class" IDENTIFIER [multiplicity] ["(" superclasses ")"]  ["{" attrs [constraint] "}"]
 
-association: "association" IDENTIFIER  [multiplicity] IDENTIFIER "->" IDENTIFIER [multiplicity] ["{" [constraint] "}"]
+association: "association" IDENTIFIER  [multiplicity] IDENTIFIER "->" IDENTIFIER [multiplicity] ["{" attrs [constraint] "}"]
 
 OPTIONAL: "optional"
 
@@ -113,6 +113,16 @@ def parse_cd(state, m_text):
             od.create_object(name, "GlobalConstraint")
             _add_constraint_to_obj(name, constraint)
 
+        def process_attrs(self, attrs, class_name):
+            if attrs != None:
+                for attr in attrs:
+                    (optional, attr_type, attr_name, constraint) = attr
+                    if state.read_dict(m, attr_type) == None:
+                        cd.create_model_ref(attr_type, primitive_types[attr_type])
+                    cd.create_attribute_link(class_name, attr_type, attr_name, optional)
+                    if constraint != None:
+                        _add_constraint_to_obj(f"{class_name}_{attr_name}", constraint)
+
         def class_(self, el):
             [abstract, class_name, multiplicity, super_classes, attrs, constraint] = el
             (lower, upper) = _handle_missing_multiplicity(multiplicity)
@@ -122,22 +132,16 @@ def parse_cd(state, m_text):
                     cd.create_inheritance(class_name, super_class)
             if constraint != None:
                 _add_constraint_to_obj(class_name, constraint)
-            if attrs != None:
-                for attr in attrs:
-                    (optional, attr_type, attr_name, constraint) = attr
-                    if state.read_dict(m, attr_type) == None:
-                        cd.create_model_ref(attr_type, primitive_types[attr_type])
-                    cd.create_attribute_link(class_name, attr_type, attr_name, optional)
-                    if constraint != None:
-                        _add_constraint_to_obj(f"{class_name}_{attr_name}", constraint)
+            self.process_attrs(attrs, class_name)
 
         def association(self, el):
-            [assoc_name, src_multiplicity, src_name, tgt_name, tgt_multiplicity, constraint] = el
+            [assoc_name, src_multiplicity, src_name, tgt_name, tgt_multiplicity, attrs, constraint] = el
             (src_lower, src_upper) = _handle_missing_multiplicity(src_multiplicity)
             (tgt_lower, tgt_upper) = _handle_missing_multiplicity(tgt_multiplicity)
             cd.create_association(assoc_name, src_name, tgt_name, src_lower, src_upper, tgt_lower, tgt_upper)
             if constraint != None:
-                _add_constraint_to_obj(class_name, constraint)
+                _add_constraint_to_obj(assoc_name, constraint)
+            self.process_attrs(attrs, assoc_name)
 
     tree = parser.parse(m_text)
     t = T(visit_tokens=True).transform(tree)