]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/Environments.java
(refs #7386) Minor SCL tools improvements
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / environment / Environments.java
index 11bc2577dfcaf41d92186bcbfa584387f440d16a..2ce0ced5bd5e46438a93a54b3c7651fab130cba9 100644 (file)
@@ -1,24 +1,22 @@
 package org.simantics.scl.compiler.environment;
 
-import gnu.trove.procedure.TObjectProcedure;
-
 import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.function.Consumer;
 
 import org.simantics.scl.compiler.common.names.Name;
+import org.simantics.scl.compiler.compilation.CompilationContext;
+import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
 import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext;
 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
-import org.simantics.scl.compiler.elaboration.modules.TypeAlias;
 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
-import org.simantics.scl.compiler.elaboration.modules.TypeConstructor;
+import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
 import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
 import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;
-import org.simantics.scl.compiler.errors.ErrorLog;
 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
 import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
 import org.simantics.scl.compiler.internal.parsing.parser.SCLParserImpl;
@@ -27,6 +25,8 @@ import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
 import org.simantics.scl.compiler.types.TCon;
 import org.simantics.scl.compiler.types.Type;
 
+import gnu.trove.procedure.TObjectProcedure;
+
 public class Environments {
     /**
      * Get the SCLValue object representing an SCL value defined in a given environment.
@@ -80,8 +80,8 @@ public class Environments {
      * @return  A TypeConstructor instance, or null if not found.
      * @throws AmbiguousNameException  if the same name is found in multiple imported modules.
      */
-    public static TypeConstructor getTypeConstructor(Environment environment, String localName) throws AmbiguousNameException {
-       return getEnvironmentEntry(environment, localName, getTypeConstructor);
+    public static TypeDescriptor getTypeDescriptor(Environment environment, String localName) throws AmbiguousNameException {
+       return getEnvironmentEntry(environment, localName, getTypeDescriptor);
     }
     
     /**
@@ -107,19 +107,11 @@ public class Environments {
     public static TypeClass getTypeClass(Environment environment, String localName) throws AmbiguousNameException {
        return getEnvironmentEntry(environment, localName, getTypeClass);
     }
-    
-    /**
-     * Get the TypeAlias object representing a type alias defined in a given environment.
-     * The name can be a local name or a fully scoped name with modules separated by periods. 
-     * @param environment  the environment
-     * @param localName  the name to be searched for
-     * @return  A TypeAlias instance, or null if not found.
-     * @throws AmbiguousNameException  if the same name is found in multiple imported modules.
-     */
-    public static TypeAlias getTypeAlias(Environment environment, String localName) throws AmbiguousNameException {
-       return getEnvironmentEntry(environment, localName, getTypeAlias);
-    }
 
+    public static CHRRuleset getRuleset(Environment environment, String localName) throws AmbiguousNameException {
+        return getEnvironmentEntry(environment, localName, getRuleset);
+    }
+    
     /**
      * Get the Name object representing an SCL value defined in a given environment.
      * The name can be a local name or a fully scoped name with modules separated by periods. 
@@ -144,12 +136,12 @@ public class Environments {
      * @return  A TCon instance, or null if not found.
      * @throws AmbiguousNameException  if the same name is used in multiple imported modules.
      */
-    public static TCon getTypeConstructorName(Environment environment, String localName) throws AmbiguousNameException {
-        TypeConstructor typeConstructor = getTypeConstructor(environment, localName);
-        if(typeConstructor == null)
+    public static TCon getTypeDescriptorName(Environment environment, String localName) throws AmbiguousNameException {
+        TypeDescriptor typeDescriptor = getTypeDescriptor(environment, localName);
+        if(typeDescriptor == null)
             return null;
         else
-            return typeConstructor.name;
+            return typeDescriptor.name;
     }
     
     /**
@@ -193,19 +185,20 @@ public class Environments {
      */
     public static Type getType(Environment environment, String typeText) throws SCLExpressionCompilationException {
         SCLParserImpl parser = new SCLParserImpl(new StringReader(typeText));
-        ErrorLog errorLog = new ErrorLog();
+        CompilationContext compilationContext = new CompilationContext();
+        compilationContext.environment = environment;
         try {
             TypeAst typeAst = (TypeAst)parser.parseType();
-            TypeTranslationContext context = new TypeTranslationContext(errorLog, environment);
+            TypeTranslationContext context = new TypeTranslationContext(compilationContext);
             Type type = context.toType(typeAst);
-            if(errorLog.isEmpty())
+            if(compilationContext.errorLog.hasNoErrors())
                 return type;
         } catch(SCLSyntaxErrorException e) {
-            errorLog.log(e.location, e.getMessage());
+            compilationContext.errorLog.log(e.location, e.getMessage());
         } catch(Exception e) {
-            errorLog.log(e);
+            compilationContext.errorLog.log(e);
         }
-        throw new SCLExpressionCompilationException(errorLog.getErrors());
+        throw new SCLExpressionCompilationException(compilationContext.errorLog.getErrors());
     }
 
     /**
@@ -248,6 +241,8 @@ public class Environments {
             Namespace childNamespace = namespace.getNamespace(prefix.substring(0, p));
             if(childNamespace != null)
                 findValuesForPrefix(childNamespace, prefix.substring(p+1), proc);
+            else
+                namespace.findValuesForPrefix(prefix, AcceptAllNamespaceFilter.INSTANCE, proc);
         }
         else
             namespace.findValuesForPrefix(prefix, AcceptAllNamespaceFilter.INSTANCE, proc);
@@ -324,10 +319,10 @@ public class Environments {
                }               
        };
     
-    private static final NamespaceValueAccessor<TypeConstructor> getTypeConstructor = new NamespaceValueAccessor<TypeConstructor>() {
+    private static final NamespaceValueAccessor<TypeDescriptor> getTypeDescriptor = new NamespaceValueAccessor<TypeDescriptor>() {
                @Override
-               public TypeConstructor get(Namespace ns, String name) throws AmbiguousNameException {
-                       return ns.getTypeConstructor(name);
+               public TypeDescriptor get(Namespace ns, String name) throws AmbiguousNameException {
+                       return ns.getTypeDescriptor(name);
                }               
        };
     
@@ -344,13 +339,13 @@ public class Environments {
                        return ns.getTypeClass(name);
                }               
        };
-    
-    private static final NamespaceValueAccessor<TypeAlias> getTypeAlias = new NamespaceValueAccessor<TypeAlias>() {
-               @Override
-               public TypeAlias get(Namespace ns, String name) throws AmbiguousNameException {
-                       return ns.getTypeAlias(name);
-               }               
-       };
+       
+    private static final NamespaceValueAccessor<CHRRuleset> getRuleset = new NamespaceValueAccessor<CHRRuleset>() {
+        @Override
+        public CHRRuleset get(Namespace ns, String name) throws AmbiguousNameException {
+            return ns.getRuleset(name);
+        }
+    };
     
        private static <T> T getEnvironmentEntry(Environment environment, String localName, NamespaceValueAccessor<T> accessor) throws AmbiguousNameException {
         Namespace namespace = environment.getLocalNamespace();
@@ -359,9 +354,10 @@ public class Environments {
             int pos = localName.indexOf('.', curPos);
             if(pos < 0)
                 return accessor.get(namespace, localName.substring(curPos));
-            namespace = namespace.getNamespace(localName.substring(curPos, pos));
-            if(namespace == null)
-                return null;
+            Namespace newNamespace = namespace.getNamespace(localName.substring(curPos, pos));
+            if(newNamespace == null)
+                return accessor.get(namespace, localName.substring(curPos));
+            namespace = newNamespace;
             curPos = pos + 1;
         }
     }