-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cs
184 lines (153 loc) · 5.7 KB
/
Parser.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
namespace AWPM
{
internal class SyntaxErrorException : Exception
{
public List<(int, string)> DataList { get; }
public SyntaxErrorException(List<(int, string)> dataList)
: base("An error occurred with the provided data.")
{
DataList = dataList;
}
}
internal enum EListValidator
{
LIST_NOT_CLOSED,
LIST_CHILD_SYNTAX,
UNKNOWN,
OK
}
internal class Parser
{
private Regex variablePattern = new Regex(@"(\w+)\s*=\s*""([^""]*)""");
private Regex arrayPattern = new Regex(@"(\w+)\s*=\s*\[\s*((?:\s*""[^""]*""(?:,\s*)?)+)\s*\]");
private Dictionary<string, string> variables =
new Dictionary<string, string>();
private Dictionary<string, List<string>> lists =
new Dictionary<string, List<string>>();
private List<(int line, string text)> notMatching =
new List<(int line, string text)>();
public Dictionary<string, string> Variables
{
get => variables;
}
public Dictionary<string, List<string>> Lists
{
get => lists;
}
private bool syntacticCheck(string[] text)
{
var errbuf = new List<(int, string)>();
for (int i = 0; i < text.Length; i++)
{
string line = text[i].TrimStart();
if (string.IsNullOrWhiteSpace(line)) continue;
switch (validateList(text, ref i, line, errbuf))
{
case EListValidator.LIST_NOT_CLOSED:
case EListValidator.LIST_CHILD_SYNTAX:
notMatching.AddRange(errbuf);
return false;
case EListValidator.OK:
continue;
case EListValidator.UNKNOWN:
break;
}
if (!this.variablePattern.IsMatch(line))
{
notMatching.Add((i + 1, $"{line} <-- unexpected token"));
return false;
}
}
return true;
}
private EListValidator validateList(string[] text, ref int i,
string line, List<(int, string)> errbuf)
{
if (line.Contains("[") && line.Contains("]")
&& !this.arrayPattern.IsMatch(line))
{
if (line.Count(c => c == '"') % 2 != 0)
{
errbuf.Add((i + 1, $"\t{line}" +
" <-- quote must be closed"));
return EListValidator.LIST_CHILD_SYNTAX;
}
return EListValidator.UNKNOWN;
}
if (line.Contains("[") && !line.Contains("]"))
{
errbuf.Add((i + 1, line));
for (int j = i + 1; j < text.Length; j++)
{
string processLineBuf = text[j].TrimStart();
if (processLineBuf.Contains("=")) break;
if (processLineBuf.Count(c => c == '"') % 2 != 0)
{
errbuf.Add((j + 1, $"\t{processLineBuf}" +
" <-- quote must be closed"));
return EListValidator.LIST_CHILD_SYNTAX;
}
if (processLineBuf.Contains("]"))
{
line += processLineBuf;
errbuf.Add((j + 1, "\t" + processLineBuf));
i = j;
return EListValidator.OK;
}
errbuf.Add((j + 1, "\t" + processLineBuf));
line += processLineBuf;
}
var firstEntry = errbuf[0];
errbuf[0] = (firstEntry.Item1, firstEntry.Item2 +
"<-- bracket was never closed");
return EListValidator.LIST_NOT_CLOSED;
}
if (!line.Contains("[") && line.Contains("]"))
{
return EListValidator.UNKNOWN;
}
return EListValidator.OK;
}
private void parseData(string text)
{
foreach (Match match in this.variablePattern.Matches(text))
{
this.variables[match.Groups[1].Value] = match.Groups[2].Value
.Trim('"');
}
foreach (Match match in arrayPattern.Matches(text))
{
List<string> items = new List<string>();
var itemMatches = Regex.Matches(match.Groups[0].Value, "\"([^\"]*)\"");
foreach (Match itemMatch in itemMatches)
{
items.Add(itemMatch.Groups[1].Value);
}
lists[match.Groups[1].Value] = items;
}
}
#if DEBUG
public Parser(string data)
{
if (!this.syntacticCheck(data.Split('\n')))
{
throw new SyntaxErrorException(this.notMatching);
}
this.parseData(data);
}
#else
//public Parser(string relativePath)
//{
// if(!this.validateData(File.ReadAllLines(relativePath)))
// {
// throw new FormatException("Found some unvalid strings");
// }
// this.parseData(File.ReadAllText(relativePath));
//}
#endif
}
}