|
@@ -5,15 +5,9 @@
|
5
|
5
|
*/
|
6
|
6
|
package org.openzen.zenscript.parser;
|
7
|
7
|
|
8
|
|
-import java.io.File;
|
9
|
|
-import java.io.IOException;
|
10
|
|
-import java.util.ArrayList;
|
11
|
|
-import java.util.Collections;
|
12
|
|
-import java.util.List;
|
13
|
|
-import java.util.Map;
|
14
|
|
-import java.util.function.Consumer;
|
15
|
8
|
import org.openzen.zencode.shared.CodePosition;
|
16
|
9
|
import org.openzen.zencode.shared.CompileException;
|
|
10
|
+import org.openzen.zencode.shared.CompileExceptionCode;
|
17
|
11
|
import org.openzen.zencode.shared.FileSourceFile;
|
18
|
12
|
import org.openzen.zencode.shared.LiteralSourceFile;
|
19
|
13
|
import org.openzen.zencode.shared.SourceFile;
|
|
@@ -31,17 +25,28 @@ import org.openzen.zenscript.codemodel.context.FileResolutionContext;
|
31
|
25
|
import org.openzen.zenscript.codemodel.context.ModuleTypeResolutionContext;
|
32
|
26
|
import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
|
33
|
27
|
import org.openzen.zenscript.codemodel.definition.ZSPackage;
|
34
|
|
-import org.openzen.zenscript.codemodel.statement.Statement;
|
35
|
|
-import org.openzen.zenscript.lexer.ZSTokenParser;
|
36
|
|
-import static org.openzen.zenscript.lexer.ZSTokenType.*;
|
37
|
28
|
import org.openzen.zenscript.codemodel.scope.FileScope;
|
38
|
29
|
import org.openzen.zenscript.codemodel.scope.GlobalScriptScope;
|
39
|
|
-import org.openzen.zenscript.codemodel.type.ISymbol;
|
40
|
30
|
import org.openzen.zenscript.codemodel.scope.StatementScope;
|
|
31
|
+import org.openzen.zenscript.codemodel.statement.Statement;
|
41
|
32
|
import org.openzen.zenscript.codemodel.type.BasicTypeID;
|
|
33
|
+import org.openzen.zenscript.codemodel.type.ISymbol;
|
42
|
34
|
import org.openzen.zenscript.lexer.ParseException;
|
|
35
|
+import org.openzen.zenscript.lexer.ZSTokenParser;
|
43
|
36
|
import org.openzen.zenscript.parser.statements.ParsedStatement;
|
44
|
37
|
|
|
38
|
+import java.io.File;
|
|
39
|
+import java.io.IOException;
|
|
40
|
+import java.util.ArrayList;
|
|
41
|
+import java.util.Collections;
|
|
42
|
+import java.util.HashMap;
|
|
43
|
+import java.util.List;
|
|
44
|
+import java.util.Map;
|
|
45
|
+import java.util.function.Consumer;
|
|
46
|
+
|
|
47
|
+import static org.openzen.zenscript.lexer.ZSTokenType.EOF;
|
|
48
|
+import static org.openzen.zenscript.lexer.ZSTokenType.K_IMPORT;
|
|
49
|
+
|
45
|
50
|
/**
|
46
|
51
|
*
|
47
|
52
|
* @author Hoofdgebruiker
|
|
@@ -80,15 +85,18 @@ public class ParsedFile {
|
80
|
85
|
pkg,
|
81
|
86
|
globals);
|
82
|
87
|
|
|
88
|
+
|
|
89
|
+ //Map so we don't print multiple compile exceptions for a single import
|
|
90
|
+ Map<String, CompileException> importErrors = new HashMap<>();
|
83
|
91
|
for (ParsedFile file : files) {
|
84
|
|
- file.registerTypes(moduleContext, rootPackage, pkg);
|
|
92
|
+ file.registerTypes(moduleContext, rootPackage, pkg, importErrors);
|
85
|
93
|
}
|
86
|
94
|
|
87
|
95
|
for (ParsedFile file : files) {
|
88
|
96
|
// compileMembers will register all definition members to their
|
89
|
97
|
// respective definitions, such as fields, constructors, methods...
|
90
|
98
|
// It doesn't yet compile the method contents.
|
91
|
|
- file.compileTypes(moduleContext, rootPackage, pkg);
|
|
99
|
+ file.compileTypes(moduleContext, rootPackage, pkg, importErrors);
|
92
|
100
|
}
|
93
|
101
|
|
94
|
102
|
if (failed)
|
|
@@ -97,7 +105,7 @@ public class ParsedFile {
|
97
|
105
|
// scripts will store all the script blocks encountered in the files
|
98
|
106
|
PrecompilationState precompiler = new PrecompilationState();
|
99
|
107
|
for (ParsedFile file : files) {
|
100
|
|
- file.registerMembers(moduleContext, precompiler, rootPackage, pkg, expansions, globals);
|
|
108
|
+ file.registerMembers(moduleContext, precompiler, rootPackage, pkg, expansions, globals, importErrors);
|
101
|
109
|
}
|
102
|
110
|
|
103
|
111
|
List<ScriptBlock> scripts = new ArrayList<>();
|
|
@@ -106,9 +114,12 @@ public class ParsedFile {
|
106
|
114
|
// compileCode will convert the parsed statements and expressions
|
107
|
115
|
// into semantic code. This semantic code can then be compiled
|
108
|
116
|
// to various targets.
|
109
|
|
- file.compileCode(moduleContext, precompiler, rootPackage, pkg, expansions, scripts, globals, scriptHeader, exceptionLogger);
|
|
117
|
+ file.compileCode(moduleContext, precompiler, rootPackage, pkg, expansions, scripts, globals, scriptHeader, exceptionLogger, importErrors);
|
110
|
118
|
}
|
111
|
|
-
|
|
119
|
+
|
|
120
|
+ for(CompileException error : importErrors.values()) {
|
|
121
|
+ exceptionLogger.accept(error);
|
|
122
|
+ }
|
112
|
123
|
return new SemanticModule(
|
113
|
124
|
pkg.module,
|
114
|
125
|
dependencies,
|
|
@@ -231,9 +242,9 @@ public class ParsedFile {
|
231
|
242
|
public void registerTypes(
|
232
|
243
|
ModuleTypeResolutionContext moduleContext,
|
233
|
244
|
ZSPackage rootPackage,
|
234
|
|
- CompilingPackage modulePackage) {
|
|
245
|
+ CompilingPackage modulePackage, Map<String, CompileException> importErrors) {
|
235
|
246
|
FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
|
236
|
|
- loadImports(context, rootPackage, modulePackage);
|
|
247
|
+ loadImports(context, rootPackage, modulePackage, importErrors);
|
237
|
248
|
|
238
|
249
|
for (ParsedDefinition definition : this.definitions) {
|
239
|
250
|
if (definition.getName() != null)
|
|
@@ -244,9 +255,9 @@ public class ParsedFile {
|
244
|
255
|
public void compileTypes(
|
245
|
256
|
ModuleTypeResolutionContext moduleContext,
|
246
|
257
|
ZSPackage rootPackage,
|
247
|
|
- CompilingPackage modulePackage) {
|
|
258
|
+ CompilingPackage modulePackage, Map<String, CompileException> importErrors) {
|
248
|
259
|
FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
|
249
|
|
- loadImports(context, rootPackage, modulePackage);
|
|
260
|
+ loadImports(context, rootPackage, modulePackage, importErrors);
|
250
|
261
|
|
251
|
262
|
for (ParsedDefinition definition : this.definitions) {
|
252
|
263
|
if (definition.getName() != null)
|
|
@@ -264,9 +275,9 @@ public class ParsedFile {
|
264
|
275
|
ZSPackage rootPackage,
|
265
|
276
|
CompilingPackage modulePackage,
|
266
|
277
|
List<ExpansionDefinition> expansions,
|
267
|
|
- Map<String, ISymbol> globals) {
|
|
278
|
+ Map<String, ISymbol> globals, Map<String, CompileException> importErrors) {
|
268
|
279
|
FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
|
269
|
|
- loadImports(context, rootPackage, modulePackage);
|
|
280
|
+ loadImports(context, rootPackage, modulePackage, importErrors);
|
270
|
281
|
|
271
|
282
|
FileScope scope = new FileScope(context, expansions, globals, precompiler);
|
272
|
283
|
for (ParsedDefinition definition : this.definitions) {
|
|
@@ -275,17 +286,17 @@ public class ParsedFile {
|
275
|
286
|
}
|
276
|
287
|
|
277
|
288
|
public void compileCode(
|
278
|
|
- ModuleTypeResolutionContext moduleContext,
|
279
|
|
- PrecompilationState precompiler,
|
280
|
|
- ZSPackage rootPackage,
|
281
|
|
- CompilingPackage modulePackage,
|
282
|
|
- List<ExpansionDefinition> expansions,
|
283
|
|
- List<ScriptBlock> scripts,
|
284
|
|
- Map<String, ISymbol> globals,
|
285
|
|
- FunctionHeader scriptHeader,
|
286
|
|
- Consumer<CompileException> exceptionLogger) {
|
|
289
|
+ ModuleTypeResolutionContext moduleContext,
|
|
290
|
+ PrecompilationState precompiler,
|
|
291
|
+ ZSPackage rootPackage,
|
|
292
|
+ CompilingPackage modulePackage,
|
|
293
|
+ List<ExpansionDefinition> expansions,
|
|
294
|
+ List<ScriptBlock> scripts,
|
|
295
|
+ Map<String, ISymbol> globals,
|
|
296
|
+ FunctionHeader scriptHeader,
|
|
297
|
+ Consumer<CompileException> exceptionLogger, Map<String, CompileException> importErrors) {
|
287
|
298
|
FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
|
288
|
|
- loadImports(context, rootPackage, modulePackage);
|
|
299
|
+ loadImports(context, rootPackage, modulePackage, importErrors);
|
289
|
300
|
|
290
|
301
|
FileScope scope = new FileScope(context, expansions, globals, precompiler);
|
291
|
302
|
for (ParsedDefinition definition : this.definitions) {
|
|
@@ -309,7 +320,7 @@ public class ParsedFile {
|
309
|
320
|
}
|
310
|
321
|
}
|
311
|
322
|
|
312
|
|
- private void loadImports(FileResolutionContext context, ZSPackage rootPackage, CompilingPackage modulePackage) {
|
|
323
|
+ private void loadImports(FileResolutionContext context, ZSPackage rootPackage, CompilingPackage modulePackage, Map<String, CompileException> importErrors) {
|
313
|
324
|
for (ParsedImport importEntry : imports) {
|
314
|
325
|
HighLevelDefinition definition;
|
315
|
326
|
if (importEntry.isRelative()) {
|
|
@@ -318,9 +329,8 @@ public class ParsedFile {
|
318
|
329
|
definition = rootPackage.getImport(importEntry.getPath(), 0);
|
319
|
330
|
}
|
320
|
331
|
|
321
|
|
- // TODO: how to signal this?
|
322
|
|
- //if (definition == null)
|
323
|
|
- // importErrors.add(new CompileException(importEntry.position, CompileExceptionCode.IMPORT_NOT_FOUND, "Could not find type " + importEntry.toString()));
|
|
332
|
+ if (definition == null)
|
|
333
|
+ importErrors.put(importEntry.toString(), new CompileException(importEntry.position, CompileExceptionCode.IMPORT_NOT_FOUND, "Could not find type " + importEntry.toString()));
|
324
|
334
|
|
325
|
335
|
if (definition != null)
|
326
|
336
|
context.addImport(importEntry.getName(), definition);
|