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.RuntimeJavaReferenceValidator;
15 import org.simantics.scl.compiler.module.ImportDeclaration;
16 import org.simantics.scl.compiler.module.Module;
17 import org.simantics.scl.compiler.module.options.ModuleCompilationOptions;
18 import org.simantics.scl.compiler.module.repository.ModuleRepository;
19 import org.simantics.scl.compiler.module.repository.UpdateListener;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public abstract class TextualModuleSource implements ModuleSource {
24 private static final Logger LOGGER = LoggerFactory.getLogger(TextualModuleSource.class);
26 public static final ImportDeclaration[] DEFAULT_IMPORTS = new ImportDeclaration[] {
27 new ImportDeclaration("Builtin", ""),
28 new ImportDeclaration("Prelude", "")
31 private final String moduleName;
32 private final double priority;
34 public TextualModuleSource(String moduleName, double priority) {
35 this.moduleName = moduleName;
36 this.priority = priority;
39 public TextualModuleSource(String moduleName) {
40 this(moduleName, 0.0);
44 public ClassLoader getClassLoader() {
45 return getClass().getClassLoader();
48 protected abstract Reader getSourceReader(UpdateListener listener) throws IOException;
49 protected JavaReferenceValidator<?, ?, ?, ?> getJavaReferenceValidator() {
50 return new RuntimeJavaReferenceValidator(getClassLoader());
53 public String getSourceText(UpdateListener listener) throws IOException {
54 Reader reader = getSourceReader(listener);
55 char[] buffer = new char[65536];
59 int count = reader.read(buffer, pos, buffer.length-pos);
61 return new String(buffer, 0, pos);
63 if(pos == buffer.length)
64 buffer = Arrays.copyOf(buffer, 2*buffer.length);
71 protected ImportDeclaration[] getBuiltinImports(UpdateListener listener) {
72 return DEFAULT_IMPORTS;
76 public String getModuleName() {
80 @SuppressWarnings("unchecked")
82 public Failable<Module> compileModule(final ModuleRepository environment, final UpdateListener listener, ModuleCompilationOptions options) {
83 SCLCompiler compiler = new SCLCompiler(options);
85 compiler.addSource(getSourceReader(listener));
87 new EnvironmentFactoryImpl(
89 getBuiltinImports(listener),
92 getJavaReferenceValidator());
93 if(compiler.getErrorLog().isEmpty())
94 return new Success<Module>(compiler.getModule());
96 LOGGER.error("While compiling " + getModuleName() + ":");
97 LOGGER.error(CompilationErrorFormatter.toString(getSourceReader(null), compiler.getErrorLog().getErrors()));
98 return new Failure(compiler.getErrorLog().getErrors());
100 } catch (IOException e) {
101 return new Failure(e);
106 public double getPriority() {
111 public String toString() {
112 return getClass().getSimpleName() + "(" + moduleName + ")";
115 public boolean isUpdateable() {
119 public void update(String newSourceText) {
120 throw new UnsupportedOperationException();