|
@@ -0,0 +1,86 @@
|
|
1
|
+package org.openzen.zenscript.parser;
|
|
2
|
+
|
|
3
|
+import org.openzen.zencode.shared.LiteralSourceFile;
|
|
4
|
+import org.openzen.zencode.shared.SourceFile;
|
|
5
|
+import org.openzen.zenscript.codemodel.FunctionParameter;
|
|
6
|
+import org.openzen.zenscript.codemodel.Module;
|
|
7
|
+import org.openzen.zenscript.codemodel.ModuleSpace;
|
|
8
|
+import org.openzen.zenscript.codemodel.SemanticModule;
|
|
9
|
+import org.openzen.zenscript.codemodel.context.CompilingPackage;
|
|
10
|
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
|
|
11
|
+import org.openzen.zenscript.lexer.ParseException;
|
|
12
|
+
|
|
13
|
+import java.io.BufferedReader;
|
|
14
|
+import java.io.File;
|
|
15
|
+import java.io.FileNotFoundException;
|
|
16
|
+import java.io.FileReader;
|
|
17
|
+import java.util.ArrayList;
|
|
18
|
+import java.util.HashMap;
|
|
19
|
+import java.util.List;
|
|
20
|
+import java.util.Map;
|
|
21
|
+import java.util.stream.Collectors;
|
|
22
|
+
|
|
23
|
+public class FolderPackage {
|
|
24
|
+
|
|
25
|
+ private Map<String, List<SourceFile>> files = new HashMap<>();
|
|
26
|
+
|
|
27
|
+ public FolderPackage(File file) {
|
|
28
|
+ ArrayList<File> foundFiles = new ArrayList<>();
|
|
29
|
+ getFiles(file, foundFiles);
|
|
30
|
+
|
|
31
|
+ String srcString = File.separator + "src" + File.separator;
|
|
32
|
+ foundFiles.stream().filter(fil -> (fil.getPath()+File.separator).substring(file.getPath().length()+ File.separator.length()).indexOf(srcString) > 0).forEach(fil -> {
|
|
33
|
+ String name = (fil.getPath() + File.separator).substring(file.getPath().length() + File.separator.length());
|
|
34
|
+ int slash = name.indexOf(srcString);
|
|
35
|
+ String moduleName = name.substring(0, slash);
|
|
36
|
+ String filename = name.substring(slash + srcString.length());
|
|
37
|
+ if(!files.containsKey(moduleName))
|
|
38
|
+ files.put(moduleName, new ArrayList<>());
|
|
39
|
+
|
|
40
|
+ try {
|
|
41
|
+ if(!fil.isDirectory())
|
|
42
|
+ files.get(moduleName).add(new LiteralSourceFile(filename, new BufferedReader(new FileReader(fil)).lines().collect(Collectors.joining("\n"))));
|
|
43
|
+ else
|
|
44
|
+ files.get(moduleName).add(new LiteralSourceFile(filename, ""));
|
|
45
|
+ } catch(FileNotFoundException e) {
|
|
46
|
+ e.printStackTrace();
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ });
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ public void getFiles(File parent, List<File> files) {
|
|
53
|
+ if(parent.isDirectory()) {
|
|
54
|
+ for(File file : parent.listFiles()) {
|
|
55
|
+ if(file.isDirectory()) {
|
|
56
|
+ getFiles(file, files);
|
|
57
|
+ }
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ files.add(parent);
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ public SemanticModule loadModule(ModuleSpace space, String name, BracketExpressionParser bracketParser, SemanticModule[] dependencies, FunctionParameter[] scriptParameters) throws ParseException {
|
|
64
|
+ return loadModule(space, name, bracketParser, dependencies, scriptParameters, new ZSPackage(space.rootPackage, name));
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ public SemanticModule loadModule(ModuleSpace space, String name, BracketExpressionParser bracketParser, SemanticModule[] dependencies, FunctionParameter[] scriptParameters, ZSPackage pkg) throws ParseException {
|
|
68
|
+ List<SourceFile> sourceFiles = files.get(name);
|
|
69
|
+ if (sourceFiles == null)
|
|
70
|
+ return null; // no such module
|
|
71
|
+
|
|
72
|
+ Module scriptModule = new Module(name);
|
|
73
|
+ CompilingPackage scriptPackage = new CompilingPackage(pkg, scriptModule);
|
|
74
|
+ ParsedFile[] files = new ParsedFile[sourceFiles.size()];
|
|
75
|
+ for (int i = 0; i < files.length; i++)
|
|
76
|
+ files[i] = ParsedFile.parse(scriptPackage, bracketParser, sourceFiles.get(i));
|
|
77
|
+
|
|
78
|
+ SemanticModule scripts = ParsedFile.compileSyntaxToSemantic(
|
|
79
|
+ dependencies,
|
|
80
|
+ scriptPackage,
|
|
81
|
+ files,
|
|
82
|
+ space,
|
|
83
|
+ scriptParameters, Throwable::printStackTrace);
|
|
84
|
+ return scripts.normalize();
|
|
85
|
+ }
|
|
86
|
+}
|