-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.nr
416 lines (361 loc) · 13.7 KB
/
parse.nr
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use crate::globals::BEGIN_ARRAY;
use crate::globals::END_ARRAY;
use crate::globals::BEGIN_OBJECT;
use crate::globals::END_OBJECT;
use crate::globals::KEY_DELIMITER;
use crate::globals::VALUE_DELIMITER;
use crate::globals::QUOTATION_MARK;
use crate::globals::BACKSLASH;
use crate::globals::TAB;
use crate::globals::NEWLINE;
use crate::globals::RETURN;
use crate::globals::SPACE;
use crate::globals::MINUS;
use crate::globals::PLUS;
use crate::globals::ZERO;
use crate::globals::POINT;
use crate::globals::CHAR_e;
use crate::globals::CHAR_E;
use crate::globals::FIELD_true;
use crate::globals::FIELD_null;
use crate::globals::FIELD_false;
use crate::JSON;
use crate::Object;
use crate::Property;
trait PropertyLookup
{
fn get<N> (self, key : str<N>) -> [u8];
fn linear_search<N>(self, key : [u8; N], index : Field) -> [u8];
fn binary_search<N>(self, key : [u8; N], begin : Field, end : Field) -> [u8];
}
trait ByteArrayParser
{
fn parse_list(self) -> [[u8]];
fn parse(self, begin : &mut Field, end : Field, child_index : Field) -> JSON;
}
impl JSON
{
unconstrained
pub fn parse<SIZE>(string : str<SIZE>) -> Self { string.as_bytes().parse(&mut 0, SIZE, -1) }
unconstrained
fn store(self, prop : Property) -> Object
{
let mut copy : Object = [];
// linear insert
for current in self.doc { if (!prop.key.eq(current.key)) { copy = copy.push_back(current); } }
copy.push_back(prop)
// // sorted insert
// let mut inserted = false;
// for current in self.doc
// {
// if (inserted) { copy = copy.push_back(current); }
// else
// {
// let (less_than, equal) = prop.key.less_than_or_eq(current.key);
// if (less_than | equal)
// {
// copy = copy.push_back(prop);
// inserted = true;
// }
// if (!equal) { copy = copy.push_back(current); }
// }
// }
// if (inserted) { copy } else { copy.push_back(prop) }
}
unconstrained
pub fn len(self) -> Field { self.doc.len() }
unconstrained
pub fn get<N> (self, key : str<N>) -> [u8] { self.doc.get(key) }
}
impl PropertyLookup for Object
{
unconstrained
pub fn get<N> (self, key : str<N>) -> [u8] { self.linear_search(key.as_bytes(), (self.len() - 1)) }
// pub fn get<N> (self, key : str<N>) -> [u8] { self.binary_search(key.as_bytes(), 0, (self.len() - 1)) }
unconstrained
pub fn linear_search<N>(self, key : [u8; N], index : Field) -> [u8]
{
if (index == -1) { [] }
else if (key.eq(self[index].key)) { self[index].value }
else { self.linear_search(key, (index - 1)) }
}
unconstrained
pub fn binary_search<N>(self, key : [u8; N], begin : Field, end : Field) -> [u8]
{
if (begin == (end + 1)) { [] }
else
{
let middle : Field = ((((begin + end) as u64) / 2) as Field);
let (less_than, equal) = key.less_than_or_eq(self[middle].key);
if (equal) { self[middle].value }
else if (less_than) { self.binary_search(key, begin, (middle - 1)) }
else { self.binary_search(key, (middle + 1), end) }
}
}
}
impl<N> ByteArrayParser for [u8; N]
{
unconstrained
fn parse_list(self) -> [[u8]]
{
let mut size : Field = self.len();
if (size == 0) | (size == 1) { [] }
else
{
size -= 1;
if ((self[0] == BEGIN_ARRAY) & (self[size] == END_ARRAY))
{
let mut result : [[u8]] = [];
let mut value : [u8] = [];
let mut objects : Field = 0;
let mut arrays : Field = 0;
for i in 1..size
{
let byte = self[i];
arrays += (((byte == BEGIN_ARRAY) as Field) - ((byte == END_ARRAY) as Field));
objects += (((byte == BEGIN_OBJECT) as Field) - ((byte == END_OBJECT) as Field));
if ((byte == VALUE_DELIMITER) & (arrays == 0) & (objects == 0))
{
result = result.push_back(value);
value = [];
}
else { value = value.push_back(byte); }
}
result.push_back(value)
}
else { [] }
}
}
unconstrained
fn parse(self, mut begin : &mut Field, end : Field, child_index : Field) -> JSON
{
let mut json = JSON::none();
let mut prop = Property::none();
let mut arrays : Field = 0;
let mut literal_field : Field = 0;
let mut index : Field = *begin;
let mut byte_prev : u8 = 0;
let child = (index != 0); // this works equally well to ((child_index + 1) != 0)
let mut OK = (index != end);
let mut value = true;
let mut done = false;
let mut escaped = false;
let mut has_key = false;
let mut key_delimiter = false;
let mut value_delimiter = false;
let mut object = false;
let mut array = false;
let mut string = false;
let mut number = false;
let mut literal = false;
let mut array_value = false;
let mut digit_0 = false;
let mut digit_1_9 = false;
let mut fraction = false;
let mut fraction_digit = false;
let mut e_E = false;
let mut e_minus = false;
let mut e_plus = false;
let mut e_digit = false;
for _ in index..end
{
if (OK & (index != end))
{
let byte = self[index];
index += 1;
if (string)
{
string = (byte != QUOTATION_MARK)
| ((byte == QUOTATION_MARK) & (byte_prev == BACKSLASH) & !escaped);
let safe = !((byte == BACKSLASH) & escaped);
if (safe & value) { prop.value = prop.value.push_back(byte); done = !(string | array); }
else if (safe & string) { prop.key = prop.key.push_back(byte); has_key = true; }
else if (safe) { OK = (object & has_key); }
byte_prev = byte;
}
else if (byte == BEGIN_OBJECT)
{
if (object | array)
{
let mut index_local : Field = (index - 1); // will be overwritten to store child object's end index
let child_index : Field = (json.children.len() + child_index + 1);
let child_json = self.parse(&mut index_local, (end - 1), child_index);
OK = (child_json.doc.len() != 0);
if (OK)
{
prop.value = prop.value.push_back(BEGIN_OBJECT);
prop.value = prop.value.push_back(child_index as u8);
prop.value = prop.value.push_back(END_OBJECT);
json.children = json.children.push_back(child_json.doc);
for child in child_json.children { json.children = json.children.push_back(child); }
index = index_local;
array_value = true;
}
}
else
{
object = true;
has_key = false;
value = false;
value_delimiter = false;
}
}
else if (byte == END_OBJECT)
{
OK = object;
object = false;
done = true;
if (child)
{
*begin = index; // return child object's end index
index = end;
}
}
else if (byte == BEGIN_ARRAY)
{
prop.value = prop.value.push_back(byte);
array_value = array;
arrays += 1;
array = true;
value_delimiter = false;
}
else if (byte == END_ARRAY)
{
OK = (!value_delimiter | array_value);
prop.value = prop.value.push_back(byte);
arrays -= 1;
array = (arrays != 0);
done = (OK & !array);
}
else if (byte == QUOTATION_MARK)
{
OK = (array | !(number | literal));
string = true;
escaped = (self[index - (1 + ((index != 1) as Field))] == BACKSLASH);
if (value)
{
prop.value = prop.value.push_back(byte);
array_value = true;
}
}
else if (byte == KEY_DELIMITER)
{
OK = (object & has_key & !key_delimiter);
key_delimiter = true;
value = true;
if (array) { prop.value = prop.value.push_back(byte); }
}
else if (byte == VALUE_DELIMITER)
{
OK = (((object | array) & array_value) | (object & !array));
key_delimiter = false;
value_delimiter = true;
done = (OK & !array);
if (array)
{
prop.value = prop.value.push_back(byte);
literal_field = 0;
array_value = false;
number = false;
literal = false;
digit_0 = false;
digit_1_9 = false;
fraction = false;
fraction_digit = false;
e_E = false;
e_minus = false;
e_plus = false;
e_digit = false;
}
}
else if ((byte > SPACE) & (byte != BACKSLASH))
{
prop.value = prop.value.push_back(byte);
array_value = true;
if ((byte - ZERO) < 10)
{
OK = (value & !literal & (!digit_0 | fraction | e_E));
number = true;
digit_0 |= ((byte == ZERO) & !digit_1_9);
digit_1_9 |= !digit_0;
fraction_digit = fraction;
e_digit = e_E;
}
else if (byte == POINT)
{
OK = (number & !(fraction | e_E));
fraction = true;
}
else if (((byte == CHAR_e) | (byte == CHAR_E)) & !literal)
{
OK = (number & !e_E);
e_E = true;
}
else if (byte == MINUS)
{
OK = (!literal & (!number | (e_E & !(e_minus | e_plus | e_digit))));
number = true;
e_minus = e_E;
}
else if (byte == PLUS)
{
OK = (e_E & !(e_plus | e_minus | e_digit ));
e_plus = true;
}
else
{
OK = !number;
literal = true;
literal_field *= 256;
literal_field += (byte as Field);
}
}
else
{
OK = ((byte != 0x00) | (index == end));
done = (!array & (number | literal));
}
if (done)
{
OK = ((digit_0 | digit_1_9) & (!fraction | fraction_digit) & (!e_E | e_digit))
| ((literal_field == FIELD_true) | (literal_field == FIELD_null) | (literal_field == FIELD_false))
| !(number | literal);
if (OK & value)
{
json.doc = json.store(prop);
prop = Property::none();
}
literal_field = 0;
done = false;
value = false;
number = false;
literal = false;
digit_0 = false;
digit_1_9 = false;
fraction = false;
fraction_digit = false;
e_E = false;
e_minus = false;
e_plus = false;
e_digit = false;
}
}
}
if (OK & !(object | array))
{
if (json.is_none())
{
OK = ((digit_0 | digit_1_9) & (!fraction | fraction_digit) & (!e_E | e_digit))
| ((literal_field == FIELD_true) | (literal_field == FIELD_null) | (literal_field == FIELD_false))
| !value;
if (OK) { json.doc = [prop]; }
}
json
}
else
{
// dep::std::println(f"Unexpected character at position {index}");
JSON::none()
}
}
}