1 package org.simantics.scl.compiler.source;
3 import java.io.IOException;
5 import java.util.Arrays;
7 import org.simantics.scl.compiler.compilation.SCLCompiler;
8 import org.simantics.scl.compiler.environment.EnvironmentFactoryImpl;
9 import org.simantics.scl.compiler.errors.CompilationErrorFormatter;
10 import org.simantics.scl.compiler.errors.Failable;
11 import org.simantics.scl.compiler.errors.Failure;
12 import org.simantics.scl.compiler.errors.Success;
13 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidator;
14 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
15 import org.simantics.scl.compiler.internal.codegen.types.RuntimeJavaReferenceValidator;
16 import org.simantics.scl.compiler.module.ImportDeclaration;
17 import org.simantics.scl.compiler.module.Module;
18 import org.simantics.scl.compiler.module.options.ModuleCompilationOptions;
19 import org.simantics.scl.compiler.module.repository.ModuleRepository;
20 import org.simantics.scl.compiler.module.repository.UpdateListener;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public abstract class TextualModuleSource implements ModuleSource {
25 private static final Logger LOGGER = LoggerFactory.getLogger(TextualModuleSource.class);
27 public static final ImportDeclaration[] DEFAULT_IMPORTS = new ImportDeclaration[] {
28 new ImportDeclaration("Builtin", ""),
29 new ImportDeclaration("Prelude", "")
32 private final String moduleName;
33 private final double priority;
35 public TextualModuleSource(String moduleName, double priority) {
36 this.moduleName = moduleName;
37 this.priority = priority;
40 public TextualModuleSource(String moduleName) {
41 this(moduleName, 0.0);
45 public ClassLoader getClassLoader() {
46 return getClass().getClassLoader();
49 protected abstract Reader getSourceReader(UpdateListener listener) throws IOException;
50 protected JavaReferenceValidator<?, ?, ?, ?> getJavaReferenceValidator() {
51 return new RuntimeJavaReferenceValidator(getClassLoader());
54 public String getSourceText(UpdateListener listener) throws IOException {
55 Reader reader = getSourceReader(listener);
56 char[] buffer = new char[65536];
60 int count = reader.read(buffer, pos, buffer.length-pos);
62 return new String(buffer, 0, pos);
64 if(pos == buffer.length)
65 buffer = Arrays.copyOf(buffer, 2*buffer.length);
72 public ImportDeclaration[] getBuiltinImports(UpdateListener listener) {
73 return DEFAULT_IMPORTS;
77 public String getModuleName() {
81 @SuppressWarnings("unchecked")
83 public Failable<Module> compileModule(final ModuleRepository environment, final UpdateListener listener, ModuleCompilationOptions options) {
84 SCLCompiler compiler = new SCLCompiler(options, getJavaReferenceValidatorFactory());
86 compiler.addSource(getSourceReader(listener));
88 new EnvironmentFactoryImpl(
90 getBuiltinImports(listener),
93 if(compiler.getErrorLog().hasNoErrors())
94 return new Success<Module>(compiler.getModule());
96 if(options == null || !options.silent)
97 LOGGER.error("While compiling " + getModuleName() + ":\n " +
98 CompilationErrorFormatter.toString(getSourceReader(null), compiler.getErrorLog().getErrors()).replaceAll("\n", "\n "));
99 return new Failure(compiler.getErrorLog().getErrors());
101 } catch (IOException e) {
102 if(options == null || !options.silent)
103 LOGGER.error("Compilation of module " + moduleName + " failed.", e);
104 return new Failure(e);
108 public JavaReferenceValidatorFactory getJavaReferenceValidatorFactory() {
109 return new JavaReferenceValidatorFactory() {
112 public JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(String context) {
113 return (JavaReferenceValidator<Object, Object, Object, Object>)TextualModuleSource.this.getJavaReferenceValidator();
117 public JavaReferenceValidator<Object, Object, Object, Object> getDefaultJavaReferenceValidator() {
118 return (JavaReferenceValidator<Object, Object, Object, Object>)TextualModuleSource.this.getJavaReferenceValidator();
124 public double getPriority() {
129 public String toString() {
130 return getClass().getSimpleName() + "(" + moduleName + ")";
133 public boolean isUpdateable() {
137 public void update(String newSourceText) {
138 throw new UnsupportedOperationException();