Browse Source

Made invalid imports log errors

Jared 5 years ago
parent
commit
88138a8d75
No account linked to committer's email address
1 changed files with 46 additions and 36 deletions
  1. 46
    36
      Parser/src/main/java/org/openzen/zenscript/parser/ParsedFile.java

+ 46
- 36
Parser/src/main/java/org/openzen/zenscript/parser/ParsedFile.java View File

5
  */
5
  */
6
 package org.openzen.zenscript.parser;
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
 import org.openzen.zencode.shared.CodePosition;
8
 import org.openzen.zencode.shared.CodePosition;
16
 import org.openzen.zencode.shared.CompileException;
9
 import org.openzen.zencode.shared.CompileException;
10
+import org.openzen.zencode.shared.CompileExceptionCode;
17
 import org.openzen.zencode.shared.FileSourceFile;
11
 import org.openzen.zencode.shared.FileSourceFile;
18
 import org.openzen.zencode.shared.LiteralSourceFile;
12
 import org.openzen.zencode.shared.LiteralSourceFile;
19
 import org.openzen.zencode.shared.SourceFile;
13
 import org.openzen.zencode.shared.SourceFile;
31
 import org.openzen.zenscript.codemodel.context.ModuleTypeResolutionContext;
25
 import org.openzen.zenscript.codemodel.context.ModuleTypeResolutionContext;
32
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
26
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
33
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
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
 import org.openzen.zenscript.codemodel.scope.FileScope;
28
 import org.openzen.zenscript.codemodel.scope.FileScope;
38
 import org.openzen.zenscript.codemodel.scope.GlobalScriptScope;
29
 import org.openzen.zenscript.codemodel.scope.GlobalScriptScope;
39
-import org.openzen.zenscript.codemodel.type.ISymbol;
40
 import org.openzen.zenscript.codemodel.scope.StatementScope;
30
 import org.openzen.zenscript.codemodel.scope.StatementScope;
31
+import org.openzen.zenscript.codemodel.statement.Statement;
41
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
32
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
33
+import org.openzen.zenscript.codemodel.type.ISymbol;
42
 import org.openzen.zenscript.lexer.ParseException;
34
 import org.openzen.zenscript.lexer.ParseException;
35
+import org.openzen.zenscript.lexer.ZSTokenParser;
43
 import org.openzen.zenscript.parser.statements.ParsedStatement;
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
  * @author Hoofdgebruiker
52
  * @author Hoofdgebruiker
80
 				pkg,
85
 				pkg,
81
 				globals);
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
 		for (ParsedFile file : files) {
91
 		for (ParsedFile file : files) {
84
-			file.registerTypes(moduleContext, rootPackage, pkg);
92
+			file.registerTypes(moduleContext, rootPackage, pkg, importErrors);
85
 		}
93
 		}
86
 		
94
 		
87
 		for (ParsedFile file : files) {
95
 		for (ParsedFile file : files) {
88
 			// compileMembers will register all definition members to their
96
 			// compileMembers will register all definition members to their
89
 			// respective definitions, such as fields, constructors, methods...
97
 			// respective definitions, such as fields, constructors, methods...
90
 			// It doesn't yet compile the method contents.
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
 		if (failed)
102
 		if (failed)
97
 		// scripts will store all the script blocks encountered in the files
105
 		// scripts will store all the script blocks encountered in the files
98
 		PrecompilationState precompiler = new PrecompilationState();
106
 		PrecompilationState precompiler = new PrecompilationState();
99
 		for (ParsedFile file : files) {
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
 		List<ScriptBlock> scripts = new ArrayList<>();
111
 		List<ScriptBlock> scripts = new ArrayList<>();
106
 			// compileCode will convert the parsed statements and expressions
114
 			// compileCode will convert the parsed statements and expressions
107
 			// into semantic code. This semantic code can then be compiled
115
 			// into semantic code. This semantic code can then be compiled
108
 			// to various targets.
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
 		return new SemanticModule(
123
 		return new SemanticModule(
113
 				pkg.module,
124
 				pkg.module,
114
 				dependencies,
125
 				dependencies,
231
 	public void registerTypes(
242
 	public void registerTypes(
232
 			ModuleTypeResolutionContext moduleContext,
243
 			ModuleTypeResolutionContext moduleContext,
233
 			ZSPackage rootPackage,
244
 			ZSPackage rootPackage,
234
-			CompilingPackage modulePackage) {
245
+			CompilingPackage modulePackage, Map<String, CompileException> importErrors) {
235
 		FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
246
 		FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
236
-		loadImports(context, rootPackage, modulePackage);
247
+		loadImports(context, rootPackage, modulePackage, importErrors);
237
 		
248
 		
238
 		for (ParsedDefinition definition : this.definitions) {
249
 		for (ParsedDefinition definition : this.definitions) {
239
 			if (definition.getName() != null)
250
 			if (definition.getName() != null)
244
 	public void compileTypes(
255
 	public void compileTypes(
245
 			ModuleTypeResolutionContext moduleContext,
256
 			ModuleTypeResolutionContext moduleContext,
246
 			ZSPackage rootPackage,
257
 			ZSPackage rootPackage,
247
-			CompilingPackage modulePackage) {
258
+			CompilingPackage modulePackage, Map<String, CompileException> importErrors) {
248
 		FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
259
 		FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
249
-		loadImports(context, rootPackage, modulePackage);
260
+		loadImports(context, rootPackage, modulePackage, importErrors);
250
 		
261
 		
251
 		for (ParsedDefinition definition : this.definitions) {
262
 		for (ParsedDefinition definition : this.definitions) {
252
 			if (definition.getName() != null)
263
 			if (definition.getName() != null)
264
 			ZSPackage rootPackage,
275
 			ZSPackage rootPackage,
265
 			CompilingPackage modulePackage,
276
 			CompilingPackage modulePackage,
266
 			List<ExpansionDefinition> expansions,
277
 			List<ExpansionDefinition> expansions,
267
-			Map<String, ISymbol> globals) {
278
+			Map<String, ISymbol> globals, Map<String, CompileException> importErrors) {
268
 		FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
279
 		FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
269
-		loadImports(context, rootPackage, modulePackage);
280
+		loadImports(context, rootPackage, modulePackage, importErrors);
270
 		
281
 		
271
 		FileScope scope = new FileScope(context, expansions, globals, precompiler);
282
 		FileScope scope = new FileScope(context, expansions, globals, precompiler);
272
 		for (ParsedDefinition definition : this.definitions) {
283
 		for (ParsedDefinition definition : this.definitions) {
275
 	}
286
 	}
276
 	
287
 	
277
 	public void compileCode(
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
 		FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
298
 		FileResolutionContext context = new FileResolutionContext(moduleContext, rootPackage, modulePackage);
288
-		loadImports(context, rootPackage, modulePackage);
299
+		loadImports(context, rootPackage, modulePackage, importErrors);
289
 		
300
 		
290
 		FileScope scope = new FileScope(context, expansions, globals, precompiler);
301
 		FileScope scope = new FileScope(context, expansions, globals, precompiler);
291
 		for (ParsedDefinition definition : this.definitions) {
302
 		for (ParsedDefinition definition : this.definitions) {
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
 		for (ParsedImport importEntry : imports) {
324
 		for (ParsedImport importEntry : imports) {
314
 			HighLevelDefinition definition;
325
 			HighLevelDefinition definition;
315
 			if (importEntry.isRelative()) {
326
 			if (importEntry.isRelative()) {
318
 				definition = rootPackage.getImport(importEntry.getPath(), 0);
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
 			if (definition != null)
335
 			if (definition != null)
326
 				context.addImport(importEntry.getName(), definition);
336
 				context.addImport(importEntry.getName(), definition);

Loading…
Cancel
Save