-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringParser.cs
More file actions
50 lines (40 loc) · 1.17 KB
/
StringParser.cs
File metadata and controls
50 lines (40 loc) · 1.17 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
using System.Diagnostics;
using System.Net.Http.Headers;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using SPADE.Grammar;
namespace SPADE;
public class StringParser
{
readonly string format;
public Dictionary<string, UtilCollection> map = new();
private bool hasParsed;
public StringParser(string f)
{
format = f;
hasParsed = false;
}
public void parse(string instance)
{
hasParsed = true;
AntlrInputStream stream = new AntlrInputStream(format);
collLexer lex = new collLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
collParser parser = new collParser(tokens);
parser.BuildParseTree = true;
IParseTree parseTree = parser.start();
codeGen gen = new codeGen();
ParseTreeWalker.Default.Walk(gen, parseTree);
gen.Parse(new UtilCollection(instance));
map = gen.map;
}
public UtilCollection this[string key]
{
get
{
if (!hasParsed) throw new InvalidOperationException("Must parse an instance string first");
return map[key];
}
}
}