]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/TextualModuleSource.java
New SCL completion implementation
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / source / TextualModuleSource.java
1 package org.simantics.scl.compiler.source;
2
3 import java.io.IOException;
4 import java.io.Reader;
5 import java.util.Arrays;
6
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;
23
24 public abstract class TextualModuleSource implements ModuleSource {
25     private static final Logger LOGGER = LoggerFactory.getLogger(TextualModuleSource.class);
26     
27     public static final ImportDeclaration[] DEFAULT_IMPORTS = new ImportDeclaration[] {
28         new ImportDeclaration("Builtin", ""),
29         new ImportDeclaration("Prelude", "")
30     };
31     
32     private final String moduleName;
33     private final double priority;
34     
35     public TextualModuleSource(String moduleName, double priority) {
36         this.moduleName = moduleName;
37         this.priority = priority;
38     }
39     
40     public TextualModuleSource(String moduleName) {
41         this(moduleName, 0.0);
42     }
43     
44     @Override
45     public ClassLoader getClassLoader() {
46         return getClass().getClassLoader();
47     }
48
49     protected abstract Reader getSourceReader(UpdateListener listener) throws IOException;
50     protected JavaReferenceValidator<?, ?, ?, ?> getJavaReferenceValidator() {
51         return new RuntimeJavaReferenceValidator(getClassLoader());
52     }
53     
54     public String getSourceText(UpdateListener listener) throws IOException {
55         Reader reader = getSourceReader(listener);
56         char[] buffer = new char[65536];
57         int pos = 0;
58         try {
59             while(true) {
60                 int count = reader.read(buffer, pos, buffer.length-pos);
61                 if(count == -1)
62                     return new String(buffer, 0, pos);
63                 pos += count;
64                 if(pos == buffer.length)
65                     buffer = Arrays.copyOf(buffer, 2*buffer.length);
66             }
67         } finally {
68             reader.close();
69         }
70     }
71     
72     public ImportDeclaration[] getBuiltinImports(UpdateListener listener) {
73         return DEFAULT_IMPORTS;
74     }
75     
76     @Override
77     public String getModuleName() {
78         return moduleName;
79     }
80     
81     @SuppressWarnings("unchecked")
82     @Override
83     public Failable<Module> compileModule(final ModuleRepository environment, final UpdateListener listener, ModuleCompilationOptions options) {
84         SCLCompiler compiler = new SCLCompiler(options, getJavaReferenceValidatorFactory());
85         try {
86             compiler.addSource(getSourceReader(listener));
87             compiler.compile(
88                     new EnvironmentFactoryImpl(
89                             environment, 
90                             getBuiltinImports(listener),
91                             listener),
92                     moduleName);
93             if(compiler.getErrorLog().hasNoErrors())
94                 return new Success<Module>(compiler.getModule());
95             else {
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());
100             }
101         } catch (IOException e) {
102             if(options == null || !options.silent)
103                 LOGGER.error("Compilation of module " + moduleName + " failed.", e);
104             return new Failure(e);
105         }
106     }
107     
108     public JavaReferenceValidatorFactory getJavaReferenceValidatorFactory() {
109         return new JavaReferenceValidatorFactory() {
110             
111             @Override
112             public JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(String context) {
113                 return (JavaReferenceValidator<Object, Object, Object, Object>)TextualModuleSource.this.getJavaReferenceValidator();
114             }
115             
116             @Override
117             public JavaReferenceValidator<Object, Object, Object, Object> getDefaultJavaReferenceValidator() {
118                 return (JavaReferenceValidator<Object, Object, Object, Object>)TextualModuleSource.this.getJavaReferenceValidator();
119             }
120         };
121     }
122
123     @Override
124     public double getPriority() {
125         return priority;
126     }
127     
128     @Override
129     public String toString() {
130         return getClass().getSimpleName() + "(" + moduleName + ")";
131     }
132     
133     public boolean isUpdateable() {
134         return false;
135     }
136     
137     public void update(String newSourceText) {
138         throw new UnsupportedOperationException();
139     }
140 }