-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c3
254 lines (222 loc) · 5.75 KB
/
parser.c3
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
module xml;
import std::io;
import std::core;
import std::math;
import std::collections::list;
import std::collections::map;
import std::ascii;
def Attribute = HashMap(<String, String>);
def NodePointers = List(<Node*>);
def NodeList = List(<Node>);
struct Node {
String name;
String value;
String header;
Attribute attributes;
NodeList children;
}
struct Parser {
Allocator allocator;
InStream stream;
DString buffer;
XMLToken token;
Node* active_node;
NodePointers open_nodes;
usz line;
}
fault XMLParserError
{
EOF,
UNEXPECTED_CHARACTER,
FILE_NOT_FOUND
}
enum XMLToken @local
{
START_TAG,
CLOSE_TAG,
END_TAG,
ATTRIBUTE,
VALUE,
HEADER,
}
def NodeSearch = fn bool (Node);
def ParserSearch = fn bool (char value);
fn void Node.find(self, NodeSearch search_fn, NodeList* cache) {
if (search_fn(self)) cache.push(self);
foreach (child: self.children) child.find(search_fn, cache);
}
fn void! Parser.advance(&self)
{
char c;
while WS: (c = self.stream.read_byte()!)
{
switch (c)
{
case '\n':
self.line++;
nextcase;
case ' ':
case '\t':
case '\r':
case '\v':
continue;
default:
break WS;
}
}
switch (true)
{
case c == '<' && self.peek(fn (character) => character == '!' || character == '?')! : {
self.token = HEADER;
self.stream.seek(1, CURSOR)!;
// Parses header value
while (self.peek(fn (character) => character != '>')!) {
self.buffer.append(self.stream.read_byte()!);
}
break;
}
case c == '<' && self.peek(fn (character) => character != '/')!: {
self.token = START_TAG;
// Parser tag name
while (self.peek(fn (character) => character != '>' && character != ' ')!) {
self.buffer.append(self.stream.read_byte()!);
}
break;
}
case c == '>' && (self.token == START_TAG || self.token == ATTRIBUTE || self.token == HEADER): {
self.token = END_TAG;
break;
}
case c == '/' && self.peek(fn (character) => character == '>')!: {
self.token = CLOSE_TAG;
self.seek(fn (character) => character == '>')!;
break;
}
case c == '<' && self.peek(fn (character) => character == '/')!: {
self.token = CLOSE_TAG;
self.seek(fn (character) => character == '>')!;
break;
}
case std::ascii::is_print(c) && (self.token == START_TAG || self.token == ATTRIBUTE): {
self.token = ATTRIBUTE;
//Parser attribute value
usz size = self.seek(fn (character) => character == '"', skip: 1, return_pos: true)!;
for (uint i = 0; i < size + 1; i++) self.buffer.append(self.stream.read_byte()!);
break;
}
case std::ascii::is_print(c): {
self.token = VALUE;
self.stream.seek(-1, CURSOR)!;
// Parses value
while (self.peek(fn (character) => character != '<')!) {
self.buffer.append(self.stream.read_byte()!);
}
break;
}
default:
io::printfn("not found char %c", c);
return XMLParserError.UNEXPECTED_CHARACTER?;
}
}
fn usz! Parser.seek(&self, ParserSearch search, uint skip = 0, bool return_pos = false)
{
usz offset;
uint skipped = 0;
char c;
// Find the char
while (c = self.stream.read_byte()!) {
offset++;
if (search(c)) {
if (skipped == skip) {break;}
else {
skipped += 1;
continue;
};
};
};
if (return_pos) self.stream.seek(-(offset + 1), Seek.CURSOR)!;
return offset;
}
fn bool! Parser.peek(&self, ParserSearch search)
{
bool response = search(self.stream.read_byte()!);
// Set stream back to start pos
self.stream.seek(-1, CURSOR)!;
return response;
}
fn Node! parse_string(String s, Allocator allocator = allocator::heap()) => parse(ByteReader{}.init(s), allocator);
fn Node! load_file (String path, Allocator allocator = allocator::heap()) {
File! file = file::open(path, "r");
defer file.close()!!;
if (catch err = file) {
io::printfn("Failed to find the xml file");
return XMLParserError.FILE_NOT_FOUND?;
}
return parse(&file, allocator);
}
fn Node! parse(InStream s, Allocator allocator = allocator::heap())
{
Node root_node;
Parser parser = { .buffer = dstring::new_with_capacity(2024), .stream = s, .allocator = allocator, .active_node = &root_node};
defer parser.free();
while (try parser.advance()) {
switch (parser.token) {
case START_TAG: {
if (parser.open_nodes.len() == 0) {
// Root node
parser.active_node.name = parser.buffer.copy_str();
} else {
// Push node as child
Node* parent_node = parser.open_nodes[parser.open_nodes.len() - 1];
parent_node.children.push(Node {
.name = parser.buffer.copy_str()
});
parser.active_node = &parent_node.children[parent_node.children.len() - 1];
}
parser.open_nodes.push(parser.active_node);
parser.buffer.clear();
}
case END_TAG: {}
case CLOSE_TAG: {
if (parser.open_nodes.len() > 0) parser.open_nodes.pop()!;
}
case VALUE: {
if (parser.open_nodes.len() - 1 > 0) {
Node* node = parser.open_nodes[parser.open_nodes.len() - 1];
node.value = parser.buffer.copy_str();
}
parser.buffer.clear();
}
case ATTRIBUTE: {
String data = parser.buffer.str_view();
usz split_index = data.index_of_char('=')!;
bool empty_value = (data.len - split_index) <= 3;
// Add attribute to hashmap
parser.active_node.attributes.set(data[0..split_index - 1].copy(), empty_value ? "" : data[split_index + 2..parser.buffer.len() - 2].copy());
parser.buffer.clear();
}
case HEADER: {
parser.active_node.header = parser.buffer.copy_str();
parser.buffer.clear();
}
default: {
break;
}
}
}
return root_node;
}
fn void Parser.free(&self)
{
self.buffer.free();
self.open_nodes.free();
}
fn void Node.free(&self)
{
foreach (child: self.children) child.free();
self.attributes.free();
self.children.free();
self.name.free();
self.value.free();
self.header.free();
}