]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added module header feature to SCL language. 15/315/3
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 1 Feb 2017 14:40:11 +0000 (16:40 +0200)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 2 Feb 2017 11:31:35 +0000 (13:31 +0200)
Module header must be in the beginning of the module. However, comments
are allowed before the header. Header has the following form:
    module {
        field = value,
        field = value
    }

Currently field name classLoader is supported. It makes it possible to
specify which classLoader is used to resolve importJava declarations.
    module {
        classLoader = "my.bundle.name.somewhere"
    }

refs #6931

Change-Id: Ibca2152597a738d1e4641cf6262af06069b38af5

30 files changed:
bundles/org.simantics.modeling/src/org/simantics/modeling/internal/Activator.java
bundles/org.simantics.modeling/src/org/simantics/modeling/scl/GraphModuleSourceRepository.java
bundles/org.simantics.modeling/src/org/simantics/modeling/scl/OntologyModule.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/DeclarationClassification.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/Elaboration.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/SCLCompiler.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/annotations/AnnotationUtils.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/Builtins.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/JavaModule.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/MinigraphModule.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/types/DummyJavaReferenceValidator.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/types/JavaReferenceValidator.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/types/JavaReferenceValidatorFactory.java [new file with mode: 0644]
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/types/RuntimeJavaReferenceValidator.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/header/ModuleHeader.java [new file with mode: 0644]
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/declarations/DModuleHeader.java [new file with mode: 0644]
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCL.grammar
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.dat
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParserImpl.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParserOptions.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLPostLexer.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLTerminals.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ConcreteModule.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/Module.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/ModuleRepository.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/runtime/RuntimeModule.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/TextualModuleSource.java
bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleModuleSource.java
bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/OsgiJavaReferenceValidatorFactory.java [new file with mode: 0644]

index f090be06c6bc1d1335a28d53a8b1b673820b2674..3fc0d46fa58cb34e2f5c77f39696b816cc244f67 100644 (file)
@@ -10,9 +10,12 @@ import org.simantics.scl.compiler.source.repository.ModuleSourceRepository;
 
 public class Activator implements BundleActivator {
 
+    private static BundleContext context;
+    
     @SuppressWarnings({ "rawtypes", "unchecked" })
     @Override
     public void start(BundleContext context) throws Exception {
+        Activator.context = context;
         Hashtable properties = new Hashtable();
         
         context.registerService(ModuleSourceRepository.class, OntologyModuleSourceRepository.INSTANCE, properties);
@@ -23,5 +26,7 @@ public class Activator implements BundleActivator {
     public void stop(BundleContext context) throws Exception {
     }
 
-    
+    public static BundleContext getContext() {
+        return context;
+    }
 }
index 07af09e557109046575ee8556abf20c5cca6ec84..c75f7a941a55f76c2bd668b0646a5fa76f40d826 100644 (file)
@@ -15,11 +15,14 @@ import org.simantics.db.procedure.SyncListener;
 import org.simantics.db.request.Read;
 import org.simantics.layer0.Layer0;
 import org.simantics.modeling.ModelingUtils;
+import org.simantics.modeling.internal.Activator;
+import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
 import org.simantics.scl.compiler.module.repository.UpdateListener;
 import org.simantics.scl.compiler.module.repository.UpdateListener.Observable;
 import org.simantics.scl.compiler.source.ModuleSource;
 import org.simantics.scl.compiler.source.StringModuleSource;
 import org.simantics.scl.compiler.source.repository.ModuleSourceRepository;
+import org.simantics.scl.osgi.internal.OsgiJavaReferenceValidatorFactory;
 import org.simantics.scl.runtime.SCLContext;
 import org.simantics.scl.runtime.tuple.Tuple0;
 import org.simantics.structural2.utils.StructuralUtils;
@@ -29,6 +32,8 @@ import gnu.trove.set.hash.THashSet;
 
 public enum GraphModuleSourceRepository implements ModuleSourceRepository {
     INSTANCE;
+    
+    private static final OsgiJavaReferenceValidatorFactory REFERENCE_VALIDATOR_FACTORY = new OsgiJavaReferenceValidatorFactory(Activator.getContext().getBundle());
 
     @Override
     public ModuleSource getModuleSource(final String moduleName, UpdateListener listener) {
@@ -123,6 +128,11 @@ public enum GraphModuleSourceRepository implements ModuleSourceRepository {
                 e.printStackTrace();
             }
         }
+        
+        @Override
+        public JavaReferenceValidatorFactory getJavaReferenceValidatorFactory() {
+            return REFERENCE_VALIDATOR_FACTORY;
+        }
     }
 
     static class ReadModuleSource extends UnaryRead<String, ModuleSource> {
index 17aba97ea1e9165c4b50222e99d10adc63fee5f9..1c8c91927fc9cc525f966555fbbe1515c6ae012d 100644 (file)
@@ -312,4 +312,9 @@ public class OntologyModule extends LazyModule {
     public String toString() {
         return new StringBuilder().append("OntologyModule ").append(getName()).toString();
     }
+
+    @Override
+    public ClassLoader getParentClassLoader() {
+        return getClass().getClassLoader();
+    }
 }
index 7c6d53ac8f698171b3275f533925afb68bee1698..652adf93efc769cb30cce495d99a53ed665bd35b 100644 (file)
@@ -23,6 +23,7 @@ import org.simantics.scl.compiler.internal.parsing.declarations.DFixityAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DImportJavaAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DInstanceAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DMappingRelationAst;
+import org.simantics.scl.compiler.internal.parsing.declarations.DModuleHeader;
 import org.simantics.scl.compiler.internal.parsing.declarations.DRelationAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DRuleAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DTypeAst;
@@ -38,6 +39,7 @@ import org.simantics.scl.compiler.module.ImportDeclaration;
 import gnu.trove.map.hash.THashMap;
 
 public class DeclarationClassification {
+    DModuleHeader moduleHeader;
     ArrayList<ImportDeclaration> importsAst = new ArrayList<ImportDeclaration>();
     ArrayList<DDataAst> dataTypesAst = new ArrayList<DDataAst>();
     ArrayList<DTypeAst> typeAliasesAst = new ArrayList<DTypeAst>();
@@ -106,6 +108,8 @@ public class DeclarationClassification {
             handle((DMappingRelationAst)declaration);
         else if(declaration instanceof DRelationAst)
             handle((DRelationAst)declaration);
+        else if(declaration instanceof DModuleHeader)
+            handle((DModuleHeader)declaration);
         else
             throw new InternalCompilerError("Unknown declaration " + declaration.getClass().getSimpleName());
     }
@@ -380,6 +384,10 @@ public class DeclarationClassification {
         mappingRelationsAst.add(declaration);
     }
     
+    public void handle(DModuleHeader declaration) {
+        moduleHeader = declaration;
+    }
+    
     public void addValueDocumentation(String valueName, DDocumentationAst documentation) {
         DDocumentationAst oldDoc = valueDocumentation.put(valueName, documentation);
         if(oldDoc != null) {
index 0971641e8b4470b2938f2cc8ac9660dc58a7d316..5461d7a29a0c680bb2752dc4b20da355daa6b78b 100644 (file)
@@ -59,6 +59,7 @@ import org.simantics.scl.compiler.errors.Locations;
 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
 import org.simantics.scl.compiler.internal.codegen.effects.ThreadLocalVariable;
 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidator;
+import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
 import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
@@ -68,6 +69,7 @@ import org.simantics.scl.compiler.internal.deriving.InstanceDeriver;
 import org.simantics.scl.compiler.internal.deriving.InstanceDerivers;
 import org.simantics.scl.compiler.internal.elaboration.profiling.BranchPointInjector;
 import org.simantics.scl.compiler.internal.elaboration.utils.StronglyConnectedComponents;
+import org.simantics.scl.compiler.internal.header.ModuleHeader;
 import org.simantics.scl.compiler.internal.parsing.declarations.ConstructorAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DAnnotationAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DClassAst;
@@ -116,7 +118,9 @@ public class Elaboration {
     private final CompilationContext compilationContext;
     private final ErrorLog errorLog;
     private final String moduleName;
+    private final ModuleHeader moduleHeader;
     private final ArrayList<ImportDeclaration> importsAst;
+    private final JavaReferenceValidatorFactory jrvFactory;
     final JavaReferenceValidator<Object, Object, Object, Object> javaReferenceValidator;
     private final ValueRepository valueDefinitionsAst;
     private final RelationRepository relationDefinitionsAst;
@@ -131,18 +135,23 @@ public class Elaboration {
     THashMap<String, ClassRef> classRefs = new THashMap<String, ClassRef>();
     THashMap<String, BranchPoint[]> branchPoints; 
     
-    @SuppressWarnings("unchecked")
     public Elaboration(CompilationContext compilationContext, CompilationTimer timer, EnvironmentFactory localEnvironmentFactory,
-            String moduleName, ArrayList<ImportDeclaration> importsAst,
-            JavaReferenceValidator<?, ?, ?, ?> javaReferenceValidator,
+            String moduleName, ModuleHeader moduleHeader, ArrayList<ImportDeclaration> importsAst,
+            JavaReferenceValidatorFactory jrvFactory,
             ValueRepository valueDefinitionsAst,
             RelationRepository relationDefinitionsAst) {
         this.compilationContext = compilationContext;
         this.errorLog = compilationContext.errorLog;
         this.moduleName = moduleName;
+        this.moduleHeader = moduleHeader;
         importsAst = processRelativeImports(importsAst);
         this.importsAst = importsAst;
-        this.javaReferenceValidator = (JavaReferenceValidator<Object, Object, Object, Object>)javaReferenceValidator;
+        this.jrvFactory = jrvFactory;
+        this.javaReferenceValidator = moduleHeader == null || moduleHeader.classLoader == null
+                ? jrvFactory.getDefaultJavaReferenceValidator()
+                : jrvFactory.getJavaReferenceValidator(moduleHeader.classLoader);
+        if(javaReferenceValidator == null)
+            errorLog.log(moduleHeader.classLoaderLocation, "Didn't find the specified class loader.");
         this.valueDefinitionsAst = valueDefinitionsAst;
         this.relationDefinitionsAst = relationDefinitionsAst;
 
@@ -653,15 +662,12 @@ public class Elaboration {
             if(annotations != null) {
                 for(DAnnotationAst annotation : annotations)
                     if(annotation.id.text.equals("@JavaName")) {
-                        Expression p0 = annotation.parameters[0];
-                        if(p0 instanceof EVar)
-                            javaName = ((EVar)p0).name;
-                        else if(p0 instanceof ELiteral) {
-                            ELiteral lit = (ELiteral)p0;
-                            javaName = ((StringConstant)lit.getValue()).getValue();
-                        }
+                        String temp = AnnotationUtils.processStringAnnotation(errorLog, annotation);
+                        if(temp != null)
+                            javaName = temp;
                     }
                     else if(annotation.id.text.equals("@private")) {
+                        AnnotationUtils.processTagAnnotation(errorLog, annotation);
                         isPrivate = true;
                     }
             }
index 29136b60935662d0e57bf91534959dfa4b9e7bf7..65e89edf1bc7c26fb4e8c1563083647b6d1cf231 100644 (file)
@@ -8,9 +8,12 @@ import java.util.Map;
 import org.simantics.scl.compiler.environment.EnvironmentFactory;
 import org.simantics.scl.compiler.errors.ErrorLog;
 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidator;
+import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
+import org.simantics.scl.compiler.internal.header.ModuleHeader;
 import org.simantics.scl.compiler.internal.parsing.declarations.DeclarationAst;
 import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
 import org.simantics.scl.compiler.internal.parsing.parser.SCLParserImpl;
+import org.simantics.scl.compiler.internal.parsing.parser.SCLParserOptions;
 import org.simantics.scl.compiler.module.ConcreteModule;
 import org.simantics.scl.compiler.module.options.ModuleCompilationOptions;
 import org.simantics.scl.compiler.top.ModuleInitializer;
@@ -29,8 +32,11 @@ public class SCLCompiler {
     private CompilationTimer timer;
     private ModuleCompilationOptions options;
     
-    public SCLCompiler(ModuleCompilationOptions options) {
+    JavaReferenceValidatorFactory jrvFactory;
+    
+    public SCLCompiler(ModuleCompilationOptions options, JavaReferenceValidatorFactory jrvFactory) {
         this.options = options == null ? ModuleCompilationOptions.STANDARD_OPTIONS : options;
+        this.jrvFactory = jrvFactory;
     }
 
     @SuppressWarnings("unchecked")
@@ -38,6 +44,7 @@ public class SCLCompiler {
         if(SCLCompilerConfiguration.ENABLE_TIMING) initializeTiming();
         try {
             SCLParserImpl parser = new SCLParserImpl(sourceReader);
+            parser.setParserOptions(SCLParserOptions.MODULE_DEFAULT);
             if(!parser.isEmpty())
             for(DeclarationAst declaration : (ArrayList<DeclarationAst>)parser.parseModule())
                 declarations.handle(declaration);
@@ -61,16 +68,16 @@ public class SCLCompiler {
     
     public void compile(
             EnvironmentFactory localEnvironmentFactory,
-            String moduleName,
-            JavaReferenceValidator<?, ?, ?, ?> javaReferenceValidator) {
+            String moduleName) {
         try {
             if(hasErrors()) return;
             Elaboration elaboration = new Elaboration(compilationContext,
                     timer,
                     localEnvironmentFactory,
                     moduleName,
+                    ModuleHeader.process(compilationContext.errorLog, declarations.moduleHeader),
                     declarations.importsAst,
-                    javaReferenceValidator,
+                    jrvFactory,
                     declarations.valueDefinitionsAst,
                     declarations.relationDefinitionsAst);
             if(options.computeCoverage)
@@ -159,6 +166,7 @@ public class SCLCompiler {
                     codeGeneration.externalConstants);
             
             module.setClasses(classes);
+            module.setParentClassLoader(elaboration.javaReferenceValidator.getClassLoader());
             module.setModuleInitializer(moduleInitializer);
             module.setBranchPoints(elaboration.branchPoints);
             if(compilationContext.errorLog.hasErrorsOrWarnings())
index fb7334cd2b2cf52cdbd1faf85dc266b762af9bc8..8cc088b955b8ded475ba86d150551f5a5f2ec44c 100644 (file)
@@ -5,8 +5,30 @@ import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
 import org.simantics.scl.compiler.elaboration.expressions.EStringLiteral;
 import org.simantics.scl.compiler.elaboration.expressions.EVar;
 import org.simantics.scl.compiler.elaboration.expressions.Expression;
+import org.simantics.scl.compiler.errors.ErrorLog;
+import org.simantics.scl.compiler.internal.parsing.declarations.DAnnotationAst;
 
 public class AnnotationUtils {
+    /**
+     * Processes an annotation of form
+     * <pre>&#64;Annotation "text"</pre>
+     * or
+     * <pre>&#64;Annotation text</pre>
+     */
+    public static String processStringAnnotation(ErrorLog errorLog, DAnnotationAst annotation) {
+        if(annotation.parameters.length != 1)
+            errorLog.log(annotation.location, "Expected one string parameter for " + annotation.id.text);
+        String result = extractString(annotation.parameters[0]);
+        if(result == null)
+            errorLog.log(annotation.location, "Expected a string parameter for " + annotation.id.text);
+        return result;
+    }
+    
+    public static void processTagAnnotation(ErrorLog errorLog, DAnnotationAst annotation) {
+        if(annotation.parameters.length != 0)
+            errorLog.log(annotation.location, "Expected no parameters for " + annotation.id.text);
+    }
+    
     public static String extractString(Expression expression) {
         if(expression instanceof EVar)
             return ((EVar)expression).name;
index 7935257ace56ba7db959cb6b2d985225a42f74d8..14808c820f3b4c91f5ac305645ff9b9ffc1d3260 100644 (file)
@@ -348,6 +348,8 @@ public class Builtins extends ConcreteModule {
             
             addValue("visitBranchPoint", VisitBranchPoint.INSTANCE);
         }
+        
+        setParentClassLoader(getClass().getClassLoader());
     }
     
     @Override
index cb39346e4a730483b2948e304c803d4805b9af20..e412a11689f708e3dd27679a82d468173743a9d8 100644 (file)
@@ -137,6 +137,8 @@ public class JavaModule extends ConcreteModule {
                 "java/lang/Object", "hashCode", Types.NO_EFFECTS, Types.INTEGER, A));
         addValue("toString", new JavaMethod(true, 
                 "java/lang/Object", "toString", Types.NO_EFFECTS, Types.STRING, A));
+        
+        setParentClassLoader(getClass().getClassLoader());
     }
     
     static Expression createLiteral(FunctionValue value) {
index 6bdaaea7bfda2b0d26a89944b09e6f6be0a1d289..434f2da6e13ff7b8249d6373aa7595c87eb7aca6 100644 (file)
@@ -282,6 +282,7 @@ public class MinigraphModule extends ConcreteModule {
                 return new ResourceAttribute(name);
             }
         });
+        setParentClassLoader(getClass().getClassLoader());
     }
     
     private static class ResourceAttribute implements SCLEntityType.Attribute {
index 84289af0d2f2672ed40669bfc83c5b98a7f7a968..994e2d63f75cc1c8d26804e672fd15cdf6b8c9a1 100644 (file)
@@ -76,5 +76,10 @@ public enum DummyJavaReferenceValidator implements JavaReferenceValidator<Object
     public boolean isPublic(Object clazz) {
         return false;
     }
+
+    @Override
+    public ClassLoader getClassLoader() {
+        throw new UnsupportedOperationException();
+    }
     
 }
index d4fb5769b86c526a76e83f16a5a418f762ce6638..189dcb2eb385a9d88029a9b9dddaad916d055b0f 100644 (file)
@@ -40,4 +40,6 @@ public interface JavaReferenceValidator<Class,Method,Field,Constructor> {
      * class is not found.
      */
     ClassRef getClassRef(String className);
+
+    ClassLoader getClassLoader();
 }
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/types/JavaReferenceValidatorFactory.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/types/JavaReferenceValidatorFactory.java
new file mode 100644 (file)
index 0000000..55d42c2
--- /dev/null
@@ -0,0 +1,6 @@
+package org.simantics.scl.compiler.internal.codegen.types;
+
+public interface JavaReferenceValidatorFactory {
+    JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(String context);
+    JavaReferenceValidator<Object, Object, Object, Object> getDefaultJavaReferenceValidator();
+}
index 8b36a8148f794f31164c3422cf56b65e955a8cb3..4ee0ab4f870653b310609836597a42d0541dcd02 100644 (file)
@@ -14,5 +14,10 @@ public class RuntimeJavaReferenceValidator extends AbstractRuntimeJavaReferenceV
     public Class<?> findClass(TypeDesc name) {
         return name.toClass(classLoader);
     }
+
+    @Override
+    public ClassLoader getClassLoader() {
+        return classLoader;
+    }
     
 }
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/header/ModuleHeader.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/header/ModuleHeader.java
new file mode 100644 (file)
index 0000000..c49b49a
--- /dev/null
@@ -0,0 +1,38 @@
+package org.simantics.scl.compiler.internal.header;
+
+import org.simantics.scl.compiler.elaboration.expressions.annotations.AnnotationUtils;
+import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
+import org.simantics.scl.compiler.errors.ErrorLog;
+import org.simantics.scl.compiler.internal.parsing.declarations.DModuleHeader;
+
+public class ModuleHeader {
+    public String classLoader;
+    public long classLoaderLocation;
+    
+    private void read(ErrorLog errorLog, DModuleHeader header) {
+        for(FieldAssignment assignment : header.fields)
+            switch(assignment.name) {
+            case "classLoader":
+                if(assignment.value == null)
+                    errorLog.log(assignment.location, "Property classLoader needs to be given a string value.");
+                else {
+                    classLoader = AnnotationUtils.extractString(assignment.value);
+                    if(classLoader == null)
+                        errorLog.log(assignment.value.location, "Expected bundle name here.");
+                    else 
+                        classLoaderLocation = assignment.location;
+                }
+                break;
+            default:
+                errorLog.logWarning(assignment.location, "Unknown module header field was skipped.");
+            }
+    }
+    
+    public static ModuleHeader process(ErrorLog errorLog, DModuleHeader header) {
+        if(header == null)
+            return null;
+        ModuleHeader result = new ModuleHeader();
+        result.read(errorLog, header);
+        return result;
+    }
+}
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/declarations/DModuleHeader.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/declarations/DModuleHeader.java
new file mode 100644 (file)
index 0000000..25d14e2
--- /dev/null
@@ -0,0 +1,36 @@
+package org.simantics.scl.compiler.internal.parsing.declarations;
+
+import org.simantics.scl.compiler.elaboration.expressions.printing.ExpressionToStringVisitor;
+import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
+
+
+
+public class DModuleHeader extends DeclarationAst {
+    public final FieldAssignment[] fields;
+    
+    public DModuleHeader(FieldAssignment[] fields) {
+        this.fields = fields;
+    }
+
+    @Override
+    public void toString(int indentation, StringBuilder b) {
+        for(int i=0;i<indentation;++i) b.append("    ");
+        b.append("module {");
+        ExpressionToStringVisitor visitor = new ExpressionToStringVisitor(b);
+        boolean first = true;
+        for(FieldAssignment field : fields) {
+            if(first)
+                first = false;
+            else
+                b.append(',');
+            b.append('\n');
+            for(int i=0;i<=indentation;++i) b.append("    ");
+            b.append(field.name);
+            b.append(" = ");
+            field.value.accept(visitor);
+        }
+        b.append('\n');
+        for(int i=0;i<indentation;++i) b.append("    ");
+        b.append('}');
+    }
+}
index 6bcffa14789b96542b16cccf2a5bbf8d7163f0c7..8ff94fd4b8f4db278f4f79f3b57010219f362a90 100644 (file)
@@ -33,7 +33,8 @@ declarations
     ;
 
 declaration
-    = var (COMMA var)* HASTYPE type                          # TypeAnnotation
+    = MODULE LBRACE (field (COMMA field)*)? RBRACE           # ModuleHeader
+    | var (COMMA var)* HASTYPE type                          # TypeAnnotation
     | bexp rhs                                               # ValueDefinition
     | DATA ID+ (EQUALS constructor (BAR constructor)*)?      # DataDefinition
     | TYPE ID+ EQUALS type                                   # TypeDefinition
index e6534c5527cdbda79d88ba7cd2e43c9815041c62..da6ec2969c2b796a91d045b6977a1de2f7b867bd 100644 (file)
Binary files a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.dat and b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.dat differ
index 6c8ee5be9c0de160fd7123f0027c9dc5a9ff18d4..362bc51420da61eccbac64be9e5165a84bf88cf9 100644 (file)
@@ -13,18 +13,18 @@ public abstract class SCLParser {
     public static final boolean TRACE = false;
 
     private static final int INITIAL_CAPACITY = 16;
-    private static final int STATE_COUNT = 344;
-    private static final int TERMINAL_COUNT = 82;
+    private static final int STATE_COUNT = 349;
+    private static final int TERMINAL_COUNT = 83;
     private static final int NONTERMINAL_COUNT = 51;
-    private static final int PRODUCT_COUNT = 132;
+    private static final int PRODUCT_COUNT = 133;
     
     private static final int[] ACTION_ROW_ID = new int[STATE_COUNT];
     private static final int[] ACTION_COLUMN_ID = new int[TERMINAL_COUNT];
-    private static final short[] ACTION_TABLE = new short[6120];
-    private static final int[] ERROR_TABLE = new int[882];
+    private static final short[] ACTION_TABLE = new short[6396];
+    private static final int[] ERROR_TABLE = new int[906];
     private static final int[] GOTO_ROW_ID = new int[STATE_COUNT];
     private static final int[] GOTO_COLUMN_ID = new int[NONTERMINAL_COUNT];
-    private static final short[] GOTO_TABLE = new short[1829];
+    private static final short[] GOTO_TABLE = new short[1652];
     private static final int[] PRODUCT_LHS = new int[PRODUCT_COUNT];
 
     private static final short STATE_MASK = (short)0x0fff;
@@ -38,6 +38,7 @@ public abstract class SCLParser {
         "SEMICOLON",
         "LBRACE",
         "RBRACE",
+        "MODULE",
         "COMMA",
         "HASTYPE",
         "DATA",
@@ -130,6 +131,7 @@ public abstract class SCLParser {
         "command",
         "statement",
         "declarations",
+        "field",
         "var",
         "bexp",
         "rhs",
@@ -156,7 +158,6 @@ public abstract class SCLParser {
         "stringLiteral",
         "symbolWithoutMinus",
         "listQualifier",
-        "field",
         "chrQuery",
         "verboseChrQuery",
         "caseRhs",
@@ -392,19 +393,19 @@ public abstract class SCLParser {
         return parse(0);
     }
     public Object parseCommands() {
-        return parse(329);
+        return parse(334);
     }
     public Object parseImport() {
-        return parse(336);
+        return parse(341);
     }
     public Object parseType() {
-        return parse(338);
+        return parse(343);
     }
     public Object parseExp() {
-        return parse(340);
+        return parse(345);
     }
     public Object parseEquationBlock() {
-        return parse(342);
+        return parse(347);
     }
 
 
@@ -428,240 +429,242 @@ public abstract class SCLParser {
         case 7:
             return reduceEquationBlock();
         case 8:
-            return reduceTypeAnnotation();
+            return reduceModuleHeader();
         case 9:
-            return reduceValueDefinition();
+            return reduceTypeAnnotation();
         case 10:
-            return reduceDataDefinition();
+            return reduceValueDefinition();
         case 11:
-            return reduceTypeDefinition();
+            return reduceDataDefinition();
         case 12:
-            return reduceClassDefinition();
+            return reduceTypeDefinition();
         case 13:
-            return reduceInstanceDefinition();
+            return reduceClassDefinition();
         case 14:
-            return reduceDerivingInstanceDefinition();
+            return reduceInstanceDefinition();
         case 15:
-            return reduceDocumentationString();
+            return reduceDerivingInstanceDefinition();
         case 16:
-            return reduceAnnotation();
+            return reduceDocumentationString();
         case 17:
-            return reducePrecedenceDefinition();
+            return reduceAnnotation();
         case 18:
-            return reduceJustImport();
+            return reducePrecedenceDefinition();
         case 19:
-            return reduceImportJava();
+            return reduceJustImport();
         case 20:
-            return reduceEffectDefinition();
+            return reduceImportJava();
         case 21:
-            return reduceRuleDefinition();
+            return reduceEffectDefinition();
         case 22:
-            return reduceMappingRelationDefinition();
+            return reduceRuleDefinition();
         case 23:
-            return reduceRelationDefinition();
+            return reduceMappingRelationDefinition();
         case 24:
-            return reduceStatementCommand();
+            return reduceRelationDefinition();
         case 25:
-            return reduceImportCommand();
+            return reduceStatementCommand();
         case 26:
-            return reduceGuardStatement();
+            return reduceImportCommand();
         case 27:
-            return reduceLetStatement();
+            return reduceGuardStatement();
         case 28:
-            return reduceBindStatement();
+            return reduceLetStatement();
         case 29:
-            return reduceRuleStatement();
+            return reduceBindStatement();
         case 30:
-            return reduceCHRStatement();
+            return reduceRuleStatement();
         case 31:
-            return reduceVerboseCHRStatement();
+            return reduceCHRStatement();
         case 32:
-            return reduceConstraintStatement();
+            return reduceVerboseCHRStatement();
         case 33:
-            return reduceDeclarations();
+            return reduceConstraintStatement();
         case 34:
-            return reduceVarId();
+            return reduceDeclarations();
         case 35:
-            return reduceEscapedSymbol();
+            return reduceField();
         case 36:
-            return reduceTupleConstructor();
+            return reduceFieldShorthand();
         case 37:
-            return reduceBinary();
+            return reduceVarId();
         case 38:
-            return reduceSimpleRhs();
+            return reduceEscapedSymbol();
         case 39:
-            return reduceGuardedRhs();
+            return reduceTupleConstructor();
         case 40:
-            return reduceConstructor();
+            return reduceBinary();
         case 41:
-            return reduceRecordConstructor();
+            return reduceSimpleRhs();
         case 42:
-            return reduceContext();
+            return reduceGuardedRhs();
         case 43:
-            return reduceFundeps();
+            return reduceConstructor();
         case 44:
-            return reduceTypeVar();
+            return reduceRecordConstructor();
         case 45:
-            return reduceTupleType();
+            return reduceContext();
         case 46:
-            return reduceListType();
+            return reduceFundeps();
         case 47:
-            return reduceListTypeConstructor();
+            return reduceTypeVar();
         case 48:
-            return reduceTupleTypeConstructor();
+            return reduceTupleType();
         case 49:
-            return reduceLambda();
+            return reduceListType();
         case 50:
-            return reduceLambdaMatch();
+            return reduceListTypeConstructor();
         case 51:
-            return reduceLet();
+            return reduceTupleTypeConstructor();
         case 52:
-            return reduceIf();
+            return reduceLambda();
         case 53:
-            return reduceMatch();
+            return reduceLambdaMatch();
         case 54:
-            return reduceDo();
+            return reduceLet();
         case 55:
-            return reduceSelect();
+            return reduceIf();
         case 56:
-            return reduceEnforce();
+            return reduceMatch();
         case 57:
-            return reduceVar();
+            return reduceDo();
         case 58:
-            return reduceHashedId();
+            return reduceSelect();
         case 59:
-            return reduceBlank();
+            return reduceEnforce();
         case 60:
-            return reduceInteger();
+            return reduceVar();
         case 61:
-            return reduceFloat();
+            return reduceHashedId();
         case 62:
-            return reduceString();
+            return reduceBlank();
         case 63:
-            return reduceChar();
+            return reduceInteger();
         case 64:
-            return reduceTuple();
+            return reduceFloat();
         case 65:
-            return reduceViewPattern();
+            return reduceString();
         case 66:
-            return reduceRightSection();
+            return reduceChar();
         case 67:
-            return reduceLeftSection();
+            return reduceTuple();
         case 68:
-            return reduceListLiteral();
+            return reduceViewPattern();
         case 69:
-            return reduceRange();
+            return reduceRightSection();
         case 70:
-            return reduceListComprehension();
+            return reduceLeftSection();
         case 71:
-            return reduceAs();
+            return reduceListLiteral();
         case 72:
-            return reduceRecord();
+            return reduceRange();
         case 73:
-            return reduceTransformation();
+            return reduceListComprehension();
         case 74:
-            return reduceEq();
+            return reduceAs();
         case 75:
-            return reduceRuleDeclarations();
+            return reduceRecord();
         case 76:
-            return reduceImportShowing();
+            return reduceTransformation();
         case 77:
-            return reduceImportHiding();
+            return reduceEq();
         case 78:
-            return reduceImportValueItem();
+            return reduceRuleDeclarations();
         case 79:
-            return reduceFieldDescription();
+            return reduceImportShowing();
         case 80:
-            return reduceStatements();
+            return reduceImportHiding();
         case 81:
-            return reduceGuardedExpEq();
+            return reduceImportValueItem();
         case 82:
-            return reduceFundep();
+            return reduceFieldDescription();
         case 83:
-            return reduceQueryRuleDeclaration();
+            return reduceStatements();
         case 84:
-            return reduceAnnotation();
+            return reduceGuardedExpEq();
         case 85:
-            return reduceGuardQuery();
+            return reduceFundep();
         case 86:
-            return reduceEqualsQuery();
+            return reduceQueryRuleDeclaration();
         case 87:
-            return reduceBindQuery();
+            return reduceAnnotation();
         case 88:
-            return reduceCompositeQuery();
+            return reduceGuardQuery();
         case 89:
-            return reduceQueryBlock();
+            return reduceEqualsQuery();
         case 90:
-            return reduceApply();
+            return reduceBindQuery();
         case 91:
-            return reduceSymbol();
+            return reduceCompositeQuery();
         case 92:
-            return reduceEscapedId();
+            return reduceQueryBlock();
         case 93:
-            return reduceMinus();
+            return reduceApply();
         case 94:
-            return reduceLess();
+            return reduceSymbol();
         case 95:
-            return reduceGreater();
+            return reduceEscapedId();
         case 96:
-            return reduceDot();
+            return reduceMinus();
         case 97:
-            return reduceFieldAccess();
+            return reduceLess();
         case 98:
-            return reduceIdAccessor();
+            return reduceGreater();
         case 99:
-            return reduceStringAccessor();
+            return reduceDot();
         case 100:
-            return reduceExpAccessor();
+            return reduceFieldAccess();
         case 101:
-            return reduceCase();
+            return reduceIdAccessor();
         case 102:
-            return reduceStringLiteral();
+            return reduceStringAccessor();
         case 103:
-            return reduceSymbol();
+            return reduceExpAccessor();
         case 104:
-            return reduceEscapedId();
+            return reduceCase();
         case 105:
-            return reduceLess();
+            return reduceStringLiteral();
         case 106:
-            return reduceGreater();
+            return reduceSymbol();
         case 107:
-            return reduceDot();
+            return reduceEscapedId();
         case 108:
-            return reduceGuardQualifier();
+            return reduceLess();
         case 109:
-            return reduceLetQualifier();
+            return reduceGreater();
         case 110:
-            return reduceBindQualifier();
+            return reduceDot();
         case 111:
-            return reduceThenQualifier();
+            return reduceGuardQualifier();
         case 112:
-            return reduceField();
+            return reduceLetQualifier();
         case 113:
-            return reduceFieldShorthand();
+            return reduceBindQualifier();
         case 114:
-            return reduceCHRQuery();
+            return reduceThenQualifier();
         case 115:
-            return reduceVerboseCHRQuery();
+            return reduceCHRQuery();
         case 116:
-            return reduceSimpleCaseRhs();
+            return reduceVerboseCHRQuery();
         case 117:
-            return reduceGuardedCaseRhs();
+            return reduceSimpleCaseRhs();
         case 118:
-            return reduceGuardedExpArrow();
+            return reduceGuardedCaseRhs();
         case 119:
-            return reduceGuardEquation();
+            return reduceGuardedExpArrow();
         case 120:
-            return reduceBasicEquation();
+            return reduceGuardEquation();
         case 121:
-            return reduceEffect();
+            return reduceBasicEquation();
         case 122:
-            return reduceJustEtype();
+            return reduceEffect();
         case 123:
-            return reduceForAll();
+            return reduceJustEtype();
         case 124:
-            return reduceApplyType();
+            return reduceForAll();
         case 125:
+            return reduceApplyType();
+        case 126:
             return reduceDummy();
 
         default:
@@ -714,6 +717,10 @@ public abstract class SCLParser {
      * equationBlock ::= (equation (SEMICOLON equation)&#42;)?
      */
     protected abstract Object reduceEquationBlock();
+    /**
+     * declaration ::= MODULE LBRACE (field (COMMA field)&#42;)? RBRACE
+     */
+    protected abstract Object reduceModuleHeader();
     /**
      * declaration ::= (var COMMA)&#42; var HASTYPE type
      */
@@ -818,6 +825,14 @@ public abstract class SCLParser {
      * declarations ::= LBRACE (declaration (SEMICOLON (declaration SEMICOLON)&#42; declaration)?)? RBRACE
      */
     protected abstract Object reduceDeclarations();
+    /**
+     * field ::= ID EQUALS exp
+     */
+    protected abstract Object reduceField();
+    /**
+     * field ::= ID
+     */
+    protected abstract Object reduceFieldShorthand();
     /**
      * var ::= ID
      */
@@ -1106,14 +1121,6 @@ public abstract class SCLParser {
      * listQualifier ::= THEN exp (BY exp)?
      */
     protected abstract Object reduceThenQualifier();
-    /**
-     * field ::= ID EQUALS exp
-     */
-    protected abstract Object reduceField();
-    /**
-     * field ::= ID
-     */
-    protected abstract Object reduceFieldShorthand();
     /**
      * chrQuery ::= (listQualifier COMMA)&#42; listQualifier
      */
index 5498b16b5f0552df54669752cd807e6dde64a53b..90c801403c859ab2218b47000daf86ceb8851bd7 100644 (file)
@@ -87,6 +87,7 @@ import org.simantics.scl.compiler.internal.parsing.declarations.DFixityAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DImportJavaAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DInstanceAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DMappingRelationAst;
+import org.simantics.scl.compiler.internal.parsing.declarations.DModuleHeader;
 import org.simantics.scl.compiler.internal.parsing.declarations.DRelationAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DRuleAst;
 import org.simantics.scl.compiler.internal.parsing.declarations.DTypeAst;
@@ -155,10 +156,22 @@ public class SCLParserImpl extends SCLParser {
     @Override
     protected Object reduceModule() {
         ArrayList<DeclarationAst> declarations = new ArrayList<DeclarationAst>(length()/2+1);
-        for(int i=0;i<length();i+=2)
-            declarations.add((DeclarationAst)get(i));
+        for(int i=0;i<length();i+=2) {
+            DeclarationAst declaration = (DeclarationAst)get(i);
+            if(declaration == null)
+                throw new NullPointerException();
+            declarations.add(declaration);
+        }
         return declarations;
     }
+    
+    @Override
+    protected Object reduceModuleHeader() {
+        FieldAssignment[] fields = new FieldAssignment[length()/2-1];
+        for(int i=0;i<fields.length;++i)
+            fields[i] = (FieldAssignment)get(2+i*2);
+        return new DModuleHeader(fields);
+    }
 
     @Override
     protected Object reduceLocalTypeAnnotation() {
index 002e2d62db11fc399c24aec912b98c593bdb6068..fed87b1bad73910bf8f497f201623f7cc4f35d92 100644 (file)
@@ -2,6 +2,12 @@ package org.simantics.scl.compiler.internal.parsing.parser;
 
 public class SCLParserOptions {
     public static final SCLParserOptions DEFAULT = new SCLParserOptions();
+    public static final SCLParserOptions MODULE_DEFAULT = new SCLParserOptions();
+    
+    static {
+        MODULE_DEFAULT.isModule = true;
+    }
     
     public boolean supportEq;
+    public boolean isModule;
 }
index efc799322b3d37c4220a90de6f8061154d3ace35..9cc1ff27b16a288afb11f138b9d8755a84f642d0 100644 (file)
@@ -57,6 +57,7 @@ public class SCLPostLexer {
     int lineStart = 0;
     boolean firstTokenOfLine = true;
     private SCLParserOptions options;
+    private boolean isFirstToken = true;
             
     {
         indentations.add(0);
@@ -151,6 +152,13 @@ public class SCLPostLexer {
                 }
             }
             firstTokenOfLine = false;
+            if(isFirstToken) {
+                isFirstToken = false;
+                if(symbol.id == SCLTerminals.ID && symbol.text.equals("module") && options != null && options.isModule) {
+                    push(new Token(SCLTerminals.MODULE, symbol.location, symbol.text));
+                    return;
+                }
+            }
         }
         
         switch(symbolId) {
@@ -166,8 +174,8 @@ public class SCLPostLexer {
             return;
         case SCLTerminals.THEN:
             /*for(int tt : indentationTokens.toArray())
-                System.out.print(SCLParser.TERMINAL_NAMES[tt] + " ");
-            System.out.println();*/
+                    System.out.print(SCLParser.TERMINAL_NAMES[tt] + " ");
+                System.out.println();*/
             if(prevTokenId == SCLTerminals.COMMA) {
                 // for list comprehension syntax
                 push(symbol);
index e676b4d482335035311231453363ede2b2683cdc..98ef910f375e417c1668d175026180fed0a9ab7f 100644 (file)
@@ -4,83 +4,84 @@ public interface SCLTerminals {
     public static final int SEMICOLON = 0;
     public static final int LBRACE = 1;
     public static final int RBRACE = 2;
-    public static final int COMMA = 3;
-    public static final int HASTYPE = 4;
-    public static final int DATA = 5;
-    public static final int ID = 6;
-    public static final int EQUALS = 7;
-    public static final int BAR = 8;
-    public static final int TYPE = 9;
-    public static final int CLASS = 10;
-    public static final int WHERE = 11;
-    public static final int INSTANCE = 12;
-    public static final int DERIVING = 13;
-    public static final int BEGIN_STRING = 14;
-    public static final int END_STRING = 15;
-    public static final int ANNOTATION_ID = 16;
-    public static final int INFIX = 17;
-    public static final int INFIXL = 18;
-    public static final int INFIXR = 19;
-    public static final int INTEGER = 20;
-    public static final int IMPORTJAVA = 21;
-    public static final int EFFECT = 22;
-    public static final int RULE = 23;
-    public static final int ABSTRACT_RULE = 24;
-    public static final int EXTENDS = 25;
-    public static final int MAPPING_RELATION = 26;
-    public static final int FOLLOWS = 27;
-    public static final int IMPORT = 28;
-    public static final int INCLUDE = 29;
-    public static final int AS = 30;
-    public static final int LPAREN = 31;
-    public static final int RPAREN = 32;
-    public static final int HIDING = 33;
-    public static final int ARROW = 34;
-    public static final int COLON = 35;
-    public static final int WITH = 36;
-    public static final int MINUS = 37;
-    public static final int SYMBOL = 38;
-    public static final int LESS = 39;
-    public static final int GREATER = 40;
-    public static final int SEPARATED_DOT = 41;
-    public static final int ESCAPED_ID = 42;
-    public static final int LAMBDA = 43;
-    public static final int LAMBDA_MATCH = 44;
-    public static final int LET = 45;
-    public static final int IF = 46;
-    public static final int MATCH = 47;
-    public static final int DO = 48;
-    public static final int MDO = 49;
-    public static final int ENFORCE = 50;
-    public static final int BLANK = 51;
-    public static final int FLOAT = 52;
-    public static final int LBRACKET = 53;
-    public static final int ESCAPED_SYMBOL = 54;
-    public static final int CHAR = 55;
-    public static final int WHEN = 56;
-    public static final int ATTACHED_HASH = 57;
-    public static final int SELECT = 58;
-    public static final int SELECT_FIRST = 59;
-    public static final int SELECT_DISTINCT = 60;
-    public static final int TRANSFORMATION = 61;
-    public static final int EQ = 62;
-    public static final int ATTACHED_DOT = 63;
-    public static final int IN = 64;
-    public static final int THEN = 65;
-    public static final int ELSE = 66;
-    public static final int RBRACKET = 67;
-    public static final int DOTDOT = 68;
-    public static final int AT = 69;
-    public static final int SUSPEND_STRING = 70;
-    public static final int CONTINUE_STRING = 71;
-    public static final int BINDS = 72;
-    public static final int IMPLIES = 73;
-    public static final int THEN_AFTER_WHEN = 74;
-    public static final int CONSTRAINT = 75;
-    public static final int BY = 76;
-    public static final int QUERY_OP = 77;
-    public static final int FORALL = 78;
-    public static final int COMMENT = 79;
-    public static final int EOL = 80;
-    public static final int EOF = 81;
+    public static final int MODULE = 3;
+    public static final int COMMA = 4;
+    public static final int HASTYPE = 5;
+    public static final int DATA = 6;
+    public static final int ID = 7;
+    public static final int EQUALS = 8;
+    public static final int BAR = 9;
+    public static final int TYPE = 10;
+    public static final int CLASS = 11;
+    public static final int WHERE = 12;
+    public static final int INSTANCE = 13;
+    public static final int DERIVING = 14;
+    public static final int BEGIN_STRING = 15;
+    public static final int END_STRING = 16;
+    public static final int ANNOTATION_ID = 17;
+    public static final int INFIX = 18;
+    public static final int INFIXL = 19;
+    public static final int INFIXR = 20;
+    public static final int INTEGER = 21;
+    public static final int IMPORTJAVA = 22;
+    public static final int EFFECT = 23;
+    public static final int RULE = 24;
+    public static final int ABSTRACT_RULE = 25;
+    public static final int EXTENDS = 26;
+    public static final int MAPPING_RELATION = 27;
+    public static final int FOLLOWS = 28;
+    public static final int IMPORT = 29;
+    public static final int INCLUDE = 30;
+    public static final int AS = 31;
+    public static final int LPAREN = 32;
+    public static final int RPAREN = 33;
+    public static final int HIDING = 34;
+    public static final int ARROW = 35;
+    public static final int COLON = 36;
+    public static final int WITH = 37;
+    public static final int MINUS = 38;
+    public static final int SYMBOL = 39;
+    public static final int LESS = 40;
+    public static final int GREATER = 41;
+    public static final int SEPARATED_DOT = 42;
+    public static final int ESCAPED_ID = 43;
+    public static final int LAMBDA = 44;
+    public static final int LAMBDA_MATCH = 45;
+    public static final int LET = 46;
+    public static final int IF = 47;
+    public static final int MATCH = 48;
+    public static final int DO = 49;
+    public static final int MDO = 50;
+    public static final int ENFORCE = 51;
+    public static final int BLANK = 52;
+    public static final int FLOAT = 53;
+    public static final int LBRACKET = 54;
+    public static final int ESCAPED_SYMBOL = 55;
+    public static final int CHAR = 56;
+    public static final int WHEN = 57;
+    public static final int ATTACHED_HASH = 58;
+    public static final int SELECT = 59;
+    public static final int SELECT_FIRST = 60;
+    public static final int SELECT_DISTINCT = 61;
+    public static final int TRANSFORMATION = 62;
+    public static final int EQ = 63;
+    public static final int ATTACHED_DOT = 64;
+    public static final int IN = 65;
+    public static final int THEN = 66;
+    public static final int ELSE = 67;
+    public static final int RBRACKET = 68;
+    public static final int DOTDOT = 69;
+    public static final int AT = 70;
+    public static final int SUSPEND_STRING = 71;
+    public static final int CONTINUE_STRING = 72;
+    public static final int BINDS = 73;
+    public static final int IMPLIES = 74;
+    public static final int THEN_AFTER_WHEN = 75;
+    public static final int CONSTRAINT = 76;
+    public static final int BY = 77;
+    public static final int QUERY_OP = 78;
+    public static final int FORALL = 79;
+    public static final int COMMENT = 80;
+    public static final int EOL = 81;
+    public static final int EOF = 82;
 }
index a8b51a8be96cad3a0aa1933758e89cc4201707bf..697e64ef805e1eafc53300cb37c49c79c66b4e8e 100644 (file)
@@ -45,6 +45,7 @@ public class ConcreteModule implements Module {
     CompilationError[] warnings = CompilationError.EMPTY_ARRAY;
     
     Map<String, byte[]> classes = Collections.emptyMap();
+    ClassLoader parentClassLoader;
     ModuleInitializer moduleInitializer;
 
     protected Documentation documentation;
@@ -278,4 +279,15 @@ public class ConcreteModule implements Module {
     public CompilationError[] getWarnings() {
         return warnings;
     }
+    
+    @Override
+    public ClassLoader getParentClassLoader() {
+        return parentClassLoader;
+    }
+    
+    public void setParentClassLoader(ClassLoader parentClassLoader) {
+        if(parentClassLoader == null)
+            throw new NullPointerException();
+        this.parentClassLoader = parentClassLoader;
+    }
 }
index 180509521312ca6e7bb014ec0d99e8ba7ab015b2..6f13ed426940e11a2d59e949a670c1cedb7a7d6f 100644 (file)
@@ -54,5 +54,5 @@ public interface Module {
     void dispose();
 
     CompilationError[] getWarnings();
-    
+    ClassLoader getParentClassLoader();
 }
index 7170b992b41d87be51e0e615d4723985bb03478e..40c69f4348861e2d4bd58877d762f4677d90c7f1 100644 (file)
@@ -173,7 +173,7 @@ public class ModuleRepository {
                         if(parentModule != null)
                             parentModules.add(parentModule);
                     }*/
-                    RuntimeModule rm = new RuntimeModule(module, parentModules, source.getClassLoader());
+                    RuntimeModule rm = new RuntimeModule(module, parentModules, module.getParentClassLoader());
                     ModuleInitializer initializer = module.getModuleInitializer();
                     if(initializer != null)
                         try {
index 2415e652074384c500ee8a0cc0914f0eb22f568a..93ecb2f1721adbb86b649ef58395cd5a813accad 100644 (file)
@@ -209,6 +209,8 @@ public class RuntimeModule {
     
     public RuntimeModule(Module module, RuntimeModuleMap parentModuleMap,
             ClassLoader parentClassLoader) {
+        if(parentClassLoader == null)
+            throw new NullPointerException();
         this.module = module;
         this.parentModuleMap = parentModuleMap;
         this.classLoader = new ModuleClassLoader(parentClassLoader);
index 4f22022e5f46cd6ba5cb60767fbbb05f425f86bf..9a6f4c97fb07c33689e21885a1a641d4bdcadf01 100644 (file)
@@ -11,6 +11,7 @@ import org.simantics.scl.compiler.errors.Failable;
 import org.simantics.scl.compiler.errors.Failure;
 import org.simantics.scl.compiler.errors.Success;
 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidator;
+import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
 import org.simantics.scl.compiler.internal.codegen.types.RuntimeJavaReferenceValidator;
 import org.simantics.scl.compiler.module.ImportDeclaration;
 import org.simantics.scl.compiler.module.Module;
@@ -80,7 +81,7 @@ public abstract class TextualModuleSource implements ModuleSource {
     @SuppressWarnings("unchecked")
     @Override
     public Failable<Module> compileModule(final ModuleRepository environment, final UpdateListener listener, ModuleCompilationOptions options) {
-        SCLCompiler compiler = new SCLCompiler(options);
+        SCLCompiler compiler = new SCLCompiler(options, getJavaReferenceValidatorFactory());
         try {
             compiler.addSource(getSourceReader(listener));
             compiler.compile(
@@ -88,8 +89,7 @@ public abstract class TextualModuleSource implements ModuleSource {
                             environment, 
                             getBuiltinImports(listener),
                             listener),
-                    moduleName,
-                    getJavaReferenceValidator());
+                    moduleName);
             if(compiler.getErrorLog().hasNoErrors())
                 return new Success<Module>(compiler.getModule());
             else {
@@ -102,6 +102,21 @@ public abstract class TextualModuleSource implements ModuleSource {
         }
     }
     
+    public JavaReferenceValidatorFactory getJavaReferenceValidatorFactory() {
+        return new JavaReferenceValidatorFactory() {
+            
+            @Override
+            public JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(String context) {
+                return (JavaReferenceValidator<Object, Object, Object, Object>)TextualModuleSource.this.getJavaReferenceValidator();
+            }
+            
+            @Override
+            public JavaReferenceValidator<Object, Object, Object, Object> getDefaultJavaReferenceValidator() {
+                return (JavaReferenceValidator<Object, Object, Object, Object>)TextualModuleSource.this.getJavaReferenceValidator();
+            }
+        };
+    }
+
     @Override
     public double getPriority() {
         return priority;
index 244c7463f0d5ade4ba574a373fd13ee9940a99b0..3213cf9b5419e7d501fdba77afd93218babc0c9e 100644 (file)
@@ -15,6 +15,7 @@ import java.util.Arrays;
 import org.eclipse.core.runtime.FileLocator;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.wiring.BundleWiring;
+import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
 import org.simantics.scl.compiler.module.ImportDeclaration;
 import org.simantics.scl.compiler.module.repository.UpdateListener;
 import org.simantics.scl.compiler.source.EncodedTextualModuleSource;
@@ -161,4 +162,7 @@ public class BundleModuleSource extends EncodedTextualModuleSource implements Up
         }
     }
 
+    public JavaReferenceValidatorFactory getJavaReferenceValidatorFactory() {
+        return new OsgiJavaReferenceValidatorFactory(bundle);
+    }
 }
diff --git a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/OsgiJavaReferenceValidatorFactory.java b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/OsgiJavaReferenceValidatorFactory.java
new file mode 100644 (file)
index 0000000..03524e8
--- /dev/null
@@ -0,0 +1,58 @@
+package org.simantics.scl.osgi.internal;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.wiring.BundleWiring;
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
+import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidator;
+import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
+import org.simantics.scl.compiler.internal.codegen.types.RuntimeJavaReferenceValidator;
+import org.simantics.scl.compiler.types.Type;
+
+public class OsgiJavaReferenceValidatorFactory implements JavaReferenceValidatorFactory {
+
+    private final Bundle bundle;
+
+    public OsgiJavaReferenceValidatorFactory(Bundle bundle) {
+        this.bundle = bundle;
+    }
+
+    private static ClassLoader getClassLoader(Bundle bundle) {
+        if(bundle.getSymbolicName().equals("org.simantics.scl.runtime"))
+            return Type.class.getClassLoader();
+        else {
+            BundleWiring wiring = bundle.adapt(BundleWiring.class);
+            if(wiring != null)
+                return wiring.getClassLoader();
+            throw new InternalCompilerError("Cannot get the class loader for bundle " + bundle.getSymbolicName() + ".");
+        }
+    }
+
+    private static JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(Bundle bundle) {
+        if(bundle == null)
+            return null;
+        return (JavaReferenceValidator<Object, Object, Object, Object>)
+                (JavaReferenceValidator<?, ?, ?, ?>)new RuntimeJavaReferenceValidator(getClassLoader(bundle));
+    }
+
+    private static Bundle getBundle(BundleContext bundleContext, String symbolicName) {
+        Bundle result = null;
+        for (Bundle candidate : bundleContext.getBundles())
+            if (candidate.getSymbolicName().equals(symbolicName))
+                if (result == null || result.getVersion().compareTo(candidate.getVersion()) < 0)
+                    result = candidate;
+        return result;
+    }
+    
+    @Override
+    public JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(String bundleName) {
+        System.out.println("getJavaReferenceValidator(" + bundleName + ")");
+        return getJavaReferenceValidator(getBundle(bundle.getBundleContext(), bundleName));
+    }
+
+    @Override
+    public JavaReferenceValidator<Object, Object, Object, Object> getDefaultJavaReferenceValidator() {
+        return getJavaReferenceValidator(bundle);
+    }
+
+}