From aa9660c27035d35d9ca45aeeb27637ffc1a7f31d Mon Sep 17 00:00:00 2001
From: Johannes Mey <johannes.mey@tu-dresden.de>
Date: Mon, 25 Nov 2019 16:41:48 +0100
Subject: [PATCH] fix bug that disregarded some relations, do not uppercase
 relation roles

---
 src/main/jastadd/grammar/EcoreToGrammar.jrag | 99 +++++++++++---------
 1 file changed, 54 insertions(+), 45 deletions(-)

diff --git a/src/main/jastadd/grammar/EcoreToGrammar.jrag b/src/main/jastadd/grammar/EcoreToGrammar.jrag
index 2f65e47..272d39e 100644
--- a/src/main/jastadd/grammar/EcoreToGrammar.jrag
+++ b/src/main/jastadd/grammar/EcoreToGrammar.jrag
@@ -3,7 +3,7 @@ aspect EcoreToGrammar {
   /**
    * transform the name of the ecore reference into a name that generates a nice API for JastAdd
    */
-  syn String EReference.grammarName() {
+  syn String EReference.grammarName(boolean enforceUpperCase) {
     String name = getName();
 
     if (name == null || name.isEmpty()) {
@@ -16,7 +16,9 @@ aspect EcoreToGrammar {
     }
 
     // upper case it
-    name = name.substring(0, 1).toUpperCase() + name.substring(1);
+    if (enforceUpperCase) {
+      name = name.substring(0, 1).toUpperCase() + name.substring(1);
+    }
 
     // remove plural s
     if (many() && name.endsWith("s")) {
@@ -96,7 +98,7 @@ aspect EcoreToGrammar {
           throw new RuntimeException("EClassifier " + (eType == null ? "null" : eType.getName()) + " not found in classifier map");
         }
 
-        component.setName(containmentReference.grammarName());
+        component.setName(containmentReference.grammarName(true));
         component.setNTA(containmentReference.getDerived());
 
         typeDecl.addComponent(component);
@@ -119,61 +121,68 @@ aspect EcoreToGrammar {
       for (EStructuralFeature eStructuralFeature : eClass.getEStructuralFeatureList()) {
         if (eStructuralFeature.isEReference()) {
           EReference eReference = eStructuralFeature.asEReference();
-          if (!eReference.getContainment() && !eReference.getDerived() && !(eReference.hasEOpposite() && !eReference.getEOpposite().getContainment())) {
-            if (eReference.hasEOpposite() && !eReference.getEOpposite().getDerived()) {
-              // bidirectional relation
 
-              // only add the bidirectional relation once
-              if (this.hashCode() < eReference.getEOpposite().hashCode()) {
-                BidirectionalRelation relation = new BidirectionalRelation();
+          // skip containment references and derived references
+          if (!eReference.getContainment() && !eReference.getDerived()) {
 
-                NavigableRole left;
-                // is this a normal, list, or opt role?
-                if (eReference.many()) {
-                  left = new ListRole();
-                } else {
-                  if (eReference.required()) {
-                    left = new NormalRole();
+            // it is a bidirectional relation if it has an opposite that is not derived
+            if (eReference.hasEOpposite() && !eReference.getEOpposite().getDerived()) {
+              // if the opposite is a containment relation, we skip it
+              if (!eReference.getEOpposite().getContainment()) {
+                // bidirectional relation
+
+                // only add the bidirectional relation once
+                if (eReference.hashCode() < eReference.getEOpposite().hashCode()) {
+                  BidirectionalRelation relation = new BidirectionalRelation();
+
+                  NavigableRole left;
+                  // is this a normal, list, or opt role?
+                  if (eReference.many()) {
+                    left = new ListRole();
                   } else {
-                    left = new OptRole();
+                    if (eReference.required()) {
+                      left = new NormalRole();
+                    } else {
+                      left = new OptRole();
+                    }
                   }
-                }
 
-                left.setName(eReference.grammarName());
+                  left.setName(eReference.grammarName(false));
 
-                if (eClassToTypeDecl.containsKey(eClass)) {
-                  left.setType(eClassToTypeDecl.get(eClass));
-                } else {
-                  EClassifier eType = eClass;
-                  throw new RuntimeException("EClassifier " + (eType == null ? "null" : eType.getName()) + " not found in classifier map.");
-                }
+                  if (eClassToTypeDecl.containsKey(eClass)) {
+                    left.setType(eClassToTypeDecl.get(eClass));
+                  } else {
+                    EClassifier eType = eClass;
+                    throw new RuntimeException("EClassifier " + (eType == null ? "null" : eType.getName()) + " not found in classifier map.");
+                  }
 
-                relation.setLeft(left);
+                  relation.setLeft(left);
 
-                NavigableRole right;
-                // is this a normal, list, or opt role?
-                if (eReference.getEOpposite().many()) {
-                  right = new ListRole();
-                } else {
-                  if (eReference.getEOpposite().required()) {
-                    right = new NormalRole();
+                  NavigableRole right;
+                  // is this a normal, list, or opt role?
+                  if (eReference.getEOpposite().many()) {
+                    right = new ListRole();
                   } else {
-                    right = new OptRole();
+                    if (eReference.getEOpposite().required()) {
+                      right = new NormalRole();
+                    } else {
+                      right = new OptRole();
+                    }
                   }
-                }
 
-                right.setName(eReference.getEOpposite().grammarName());
+                  right.setName(eReference.getEOpposite().grammarName(false));
 
-                if (eClassToTypeDecl.containsKey(eReference.getEType())) {
-                  right.setType(eClassToTypeDecl.get(eReference.getEType()));
-                } else {
-                  EClassifier eType = eReference.getEType();
-                  throw new RuntimeException("EClassifier " + (eType == null ? "null" : eType.getName()) + " not found in classifier map.");
-                }
+                  if (eClassToTypeDecl.containsKey(eReference.getEType())) {
+                    right.setType(eClassToTypeDecl.get(eReference.getEType()));
+                  } else {
+                    EClassifier eType = eReference.getEType();
+                    throw new RuntimeException("EClassifier " + (eType == null ? "null" : eType.getName()) + " not found in classifier map.");
+                  }
 
-                relation.setRight(right);
+                  relation.setRight(right);
 
-                grammar.addRelation(relation);
+                  grammar.addRelation(relation);
+                }
               }
             } else {
 
@@ -190,7 +199,7 @@ aspect EcoreToGrammar {
                 }
               }
 
-              source.setName(eReference.grammarName());
+              source.setName(eReference.grammarName(false));
 
               if (eClassToTypeDecl.containsKey(eClass)) {
                 source.setType(eClassToTypeDecl.get(eClass));
-- 
GitLab