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);
public void stop(BundleContext context) throws Exception {
}
-
+ public static BundleContext getContext() {
+ return context;
+ }
}
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;
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) {
e.printStackTrace();
}
}
+
+ @Override
+ public JavaReferenceValidatorFactory getJavaReferenceValidatorFactory() {
+ return REFERENCE_VALIDATOR_FACTORY;
+ }
}
static class ReadModuleSource extends UnaryRead<String, ModuleSource> {
public String toString() {
return new StringBuilder().append("OntologyModule ").append(getName()).toString();
}
+
+ @Override
+ public ClassLoader getParentClassLoader() {
+ return getClass().getClassLoader();
+ }
}
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;
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>();
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());
}
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) {
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;
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;
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;
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;
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;
}
}
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;
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")
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);
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)
codeGeneration.externalConstants);
module.setClasses(classes);
+ module.setParentClassLoader(elaboration.javaReferenceValidator.getClassLoader());
module.setModuleInitializer(moduleInitializer);
module.setBranchPoints(elaboration.branchPoints);
if(compilationContext.errorLog.hasErrorsOrWarnings())
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>@Annotation "text"</pre>
+ * or
+ * <pre>@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;
addValue("visitBranchPoint", VisitBranchPoint.INSTANCE);
}
+
+ setParentClassLoader(getClass().getClassLoader());
}
@Override
"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) {
return new ResourceAttribute(name);
}
});
+ setParentClassLoader(getClass().getClassLoader());
}
private static class ResourceAttribute implements SCLEntityType.Attribute {
public boolean isPublic(Object clazz) {
return false;
}
+
+ @Override
+ public ClassLoader getClassLoader() {
+ throw new UnsupportedOperationException();
+ }
}
* class is not found.
*/
ClassRef getClassRef(String className);
+
+ ClassLoader getClassLoader();
}
--- /dev/null
+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();
+}
public Class<?> findClass(TypeDesc name) {
return name.toClass(classLoader);
}
+
+ @Override
+ public ClassLoader getClassLoader() {
+ return classLoader;
+ }
}
--- /dev/null
+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;
+ }
+}
--- /dev/null
+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('}');
+ }
+}
;
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
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;
"SEMICOLON",
"LBRACE",
"RBRACE",
+ "MODULE",
"COMMA",
"HASTYPE",
"DATA",
"command",
"statement",
"declarations",
+ "field",
"var",
"bexp",
"rhs",
"stringLiteral",
"symbolWithoutMinus",
"listQualifier",
- "field",
"chrQuery",
"verboseChrQuery",
"caseRhs",
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);
}
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:
* equationBlock ::= (equation (SEMICOLON equation)*)?
*/
protected abstract Object reduceEquationBlock();
+ /**
+ * declaration ::= MODULE LBRACE (field (COMMA field)*)? RBRACE
+ */
+ protected abstract Object reduceModuleHeader();
/**
* declaration ::= (var COMMA)* var HASTYPE type
*/
* declarations ::= LBRACE (declaration (SEMICOLON (declaration SEMICOLON)* declaration)?)? RBRACE
*/
protected abstract Object reduceDeclarations();
+ /**
+ * field ::= ID EQUALS exp
+ */
+ protected abstract Object reduceField();
+ /**
+ * field ::= ID
+ */
+ protected abstract Object reduceFieldShorthand();
/**
* var ::= ID
*/
* 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)* listQualifier
*/
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;
@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() {
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;
}
int lineStart = 0;
boolean firstTokenOfLine = true;
private SCLParserOptions options;
+ private boolean isFirstToken = true;
{
indentations.add(0);
}
}
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) {
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);
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;
}
CompilationError[] warnings = CompilationError.EMPTY_ARRAY;
Map<String, byte[]> classes = Collections.emptyMap();
+ ClassLoader parentClassLoader;
ModuleInitializer moduleInitializer;
protected Documentation documentation;
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;
+ }
}
void dispose();
CompilationError[] getWarnings();
-
+ ClassLoader getParentClassLoader();
}
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 {
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);
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;
@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(
environment,
getBuiltinImports(listener),
listener),
- moduleName,
- getJavaReferenceValidator());
+ moduleName);
if(compiler.getErrorLog().hasNoErrors())
return new Success<Module>(compiler.getModule());
else {
}
}
+ 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;
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;
}
}
+ public JavaReferenceValidatorFactory getJavaReferenceValidatorFactory() {
+ return new OsgiJavaReferenceValidatorFactory(bundle);
+ }
}
--- /dev/null
+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);
+ }
+
+}