-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuilder.cs
More file actions
54 lines (48 loc) · 1.66 KB
/
Builder.cs
File metadata and controls
54 lines (48 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Collections.Generic;
using Elk; using Elk.Basic.Graph;
namespace Elk.Basic{
public class Builder{
Reader reader;
string shebang;
PathHandler pathHandler;
public Builder(Reader reader, PathHandler pathHandler,
string shebang=null){
this.reader = reader;
this.pathHandler = pathHandler;
this.shebang = shebang;
}
public Module Build(
string path,
IEnumerable<string> submodules,
List<string> paths=null
){
if(paths == null) paths = new List<string>();
paths.Add(path);
var module = Parse(path, submodules);
if(module == null) return null;
var includes = module.includes;
if(includes != null) foreach(var inc in includes){
if(paths.Contains(inc.module)) continue;
module.Merge(Build(inc.module, submodules:null, paths), inc.module);
}
return module;
}
Module Parse(string path, IEnumerable<string> submodules){
var src = pathHandler.Load(path);
if(src == null) return null;
if(shebang != null && src.StartsWith(shebang))
src = src.Substring(shebang.Length);
// TODO submodule injection will confuse line numbers
if(submodules != null) foreach(var e in submodules){
if(string.IsNullOrEmpty(e)) continue;
src = "with " + e + ";\n" + src;
}
try{
return (Module)reader.Parse(src, path, debug: null);
}catch(ElkParsingException ex){
throw new ElkParsingException(
ex.Message + $" in {path}.txt", ex
);
}
}
}}