Browse Source

PreProcessor tests

Jared 5 years ago
parent
commit
8c67415263
No account linked to committer's email address

+ 34
- 0
JavaIntegration/src/main/java/org/openzen/zencode/java/PreProcessor.java View File

@@ -0,0 +1,34 @@
1
+package org.openzen.zencode.java;
2
+
3
+import org.openzen.zenscript.parser.*;
4
+
5
+import java.io.*;
6
+import java.util.function.*;
7
+
8
+public class PreProcessor implements Predicate<ParsedFile> {
9
+    
10
+    private final String name;
11
+    private final Supplier<Boolean> call;
12
+    
13
+    public PreProcessor(String name, Supplier<Boolean> call) {
14
+        this.name = name;
15
+        this.call = call;
16
+    }
17
+    
18
+    @Override
19
+    public boolean test(ParsedFile parsedFile) {
20
+        try(BufferedReader reader = new BufferedReader(parsedFile.file.open())) {
21
+            if(reader.lines().anyMatch(line -> line.trim().startsWith("#" + name))) {
22
+                return call.get();
23
+            } else {
24
+                return false;
25
+            }
26
+        } catch(IOException e) {
27
+            e.printStackTrace();
28
+        }
29
+        return false;
30
+    }
31
+    
32
+    
33
+    
34
+}

+ 16
- 4
JavaIntegration/src/main/java/org/openzen/zencode/java/ScriptingEngine.java View File

@@ -78,7 +78,7 @@ public class ScriptingEngine {
78 78
 			FunctionParameter[] scriptParameters,
79 79
 			String... dependencies) throws ParseException
80 80
 	{
81
-		return createScriptedModule(name, sources, bracketParser, scriptParameters, Throwable::printStackTrace, System.out::println, sourceFile -> System.out.println("Loading " + sourceFile.getFilename()), dependencies);
81
+		return createScriptedModule(name, sources, bracketParser, scriptParameters, Throwable::printStackTrace, System.out::println, sourceFile -> System.out.println("Loading " + sourceFile.getFilename()), new PreProcessor[]{},dependencies);
82 82
 	}
83 83
 	
84 84
 	public SemanticModule createScriptedModule(
@@ -89,15 +89,27 @@ public class ScriptingEngine {
89 89
 			Consumer<CompileException> compileExceptionConsumer,
90 90
 			Consumer<ValidationLogEntry> validatorErrorConsumer,
91 91
 			Consumer<SourceFile> sourceFileConsumer,
92
+			PreProcessor[] preProcessors,
92 93
 			String... dependencies) throws ParseException
93 94
 	{
94 95
 		Module scriptModule = new Module(name);
95 96
 		CompilingPackage scriptPackage = new CompilingPackage(new ZSPackage(space.rootPackage, name), scriptModule);
96 97
 		
97
-		ParsedFile[] files = new ParsedFile[sources.length];
98
+		List<ParsedFile> files = new ArrayList<>();
99
+        Predicate<ParsedFile> preProcessor = null;
100
+        for(PreProcessor processor : preProcessors) {
101
+            if(preProcessor == null) {
102
+                preProcessor = processor;
103
+                continue;
104
+            }
105
+            preProcessor = preProcessor.and(processor);
106
+        }
98 107
 		for (int i = 0; i < sources.length; i++) {
99 108
 			sourceFileConsumer.accept(sources[i]);
100
-			files[i] = ParsedFile.parse(scriptPackage, bracketParser, sources[i]);
109
+            ParsedFile parsed = ParsedFile.parse(scriptPackage, bracketParser, sources[i]);
110
+            if(preProcessor.test(parsed)){
111
+                files.add(parsed);
112
+            }
101 113
 		}
102 114
 		
103 115
 		SemanticModule[] dependencyModules = new SemanticModule[dependencies.length + 1];
@@ -109,7 +121,7 @@ public class ScriptingEngine {
109 121
 		SemanticModule scripts = ParsedFile.compileSyntaxToSemantic(
110 122
 				dependencyModules,
111 123
 				scriptPackage,
112
-				files,
124
+				files.toArray(new ParsedFile[0]),
113 125
 				space,
114 126
 				scriptParameters,
115 127
 				compileExceptionConsumer);

Loading…
Cancel
Save