yaml_parser.anubis
14.5 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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 07/06/2016
* Time: 23:15
* © Calexium
*/
read yaml_type.anubis
read tools/streams.anubis
read tools/string.anubis
read system/string.anubis
type YAML_Parser_Info:
yaml_p_info(Int parent_indent, Int context_indent, Int level).
type YAML_Parser_Node_Info:
none,
unquoted,
in_single_quote,
in_double_quote.
type YAML_Parser_Context_Info:
none,
line,
block_start,
block.
type YAML_Parser_Node:
map(List(YAML_Key_Value) kvl),
list(List(YAML_Value) vl),
value(YAML_Value v).
define Maybe(YAML_Parser_Node)
_read_node
(
Stream s,
YAML_Parser_Info p_info,
YAML_Parser_Node_Info node_info,
List(Word8) so_far
).
define Bool eol(Stream s, Word8 c) =
if c = 10 then true // LF
else if c = 13 then // CR
if read_byte(s) is // for CR LF
{
failure then true,
success(next_char) then
if next_char = 10 then
true
else
unput_byte(next_char, s);
true
}
else false.
define One
skip_to_next_line
(
Stream s
)=
if read_byte(s) is
{
failure then unique,
success(c) then
if eol(s, c) then
unique
else
skip_to_next_line(s)
}.
define One
skip_spaces
(
Stream s
)=
if read_byte(s) is
{
failure then unique,
success(c) then
if c = ' ' then skip_spaces(s)
else
unput_byte(c, s);
unique
}.
define One
skip_to_next_node
(
Stream s
)=
if read_byte(s) is
{
failure then unique,
success(c) then
if eol(s, c) then
skip_to_next_node(s)
else if c = ' ' then
skip_to_next_node(s)
else if c = '#' then
//skip_spaces(s);
skip_to_next_line(s);
skip_to_next_node(s)
else
unput_byte(c, s);
unique
}.
define List(Word8)
trim_right
(
List(Word8) char_list,
Word8 token
)=
if char_list is
{
[ ] then [ ],
[h . t] then
if h = token
then trim_right(t, token)
else char_list
}.
define String
trim_right
(
String str,
Word8 token
)=
implode(reverse(trim_right(reverse(explode(str)), token))).
define YAML_Value
get_yaml_value
(
String str
)=
with str = trim_token(str, ' '),
if str = "yes" then
bool(var(true))
else if str = "no" then
bool(var(false))
else
if decimal_scan(str) is
{
failure then
string(var(str)),
success(integer) then
int(var(integer))
}.
define Maybe(List(YAML_Key_Value))
read_block_maps
(
Stream s,
YAML_Parser_Info p_info,
Int block_indent,
List(YAML_Key_Value) so_far
)=
since p_info is yaml_p_info(parent_indent, context_indent, level),
skip_spaces(s);
skip_to_next_node(s);
with current_indent = current_column(s),
println("reading block map entries");
println("current_indent: " + current_indent);
println("block_indent: " + block_indent);
if current_indent = block_indent then
if _read_node(s, yaml_p_info(block_indent, current_indent, level), none, []) is
{
failure then
success([]),
success(node_type) then
if node_type is
{
map(yaml_kvl) then
if yaml_kvl is
{
[ ] then
failure,
[h . _] then
read_block_maps(s, p_info, block_indent, [ h . so_far ]),
}
list(yaml_lv) then
success(so_far),
value(yaml_value) then
success(so_far)
}
}
else
success(so_far).
define Maybe(List(YAML_Value))
read_block_list
(
Stream s,
YAML_Parser_Info p_info,
Int block_indent,
List(YAML_Value) so_far
)=
since p_info is yaml_p_info(parent_indent, context_indent, level),
skip_spaces(s);
skip_to_next_node(s);
with current_indent = current_column(s),
//println("current_indent: " + current_indent);
//println("block_indent: " + block_indent);
if current_indent = block_indent then
if _read_node(s, yaml_p_info(block_indent, current_indent, level), none, []) is
{
failure then
success([]),
success(node_type) then
if node_type is
{
map(yaml_kvl) then
failure,
list(yaml_lv) then
if yaml_lv is
{
[ ] then
failure,
[h . _] then
read_block_list(s, p_info, block_indent, [ h . so_far ]),
}
value(yaml_value) then
failure
}
}
else
success(so_far).
define Maybe(YAML_Value)
read_value
(
Stream s,
YAML_Parser_Info p_info,
YAML_Parser_Context_Info ctx_info,
List(Word8) so_far
)=
since p_info is yaml_p_info(parent_indent, context_indent, level),
//skip_spaces(s);
with current_indent = current_column(s),
if read_byte(s) is
{
failure then success(null),
success(c) then
if eol(s, c) then // \n
if ctx_info is
{
none then
read_value(s, p_info, block_start, so_far),
line then
with value_str = implode(reverse(so_far)),
//println(" value: " + value_str);
success(get_yaml_value(value_str)),
block_start then
read_value(s, p_info, block_start, so_far),
block then
read_value(s, p_info, block, so_far)
}
else if c = '#' then
skip_to_next_line(s);
read_value(s, p_info, ctx_info, so_far)
else
if ctx_info is
{
none then
read_value(s, p_info, line, [c . so_far]),
line then
//println(implode(reverse([c . so_far])));
read_value(s, p_info, line, [c . so_far]),
block_start then
println("block_start");
unput_byte(c, s);
skip_to_next_node(s);
with current_indent = current_column(s),
if current_indent =< parent_indent then
success(null)
else
read_value(s, yaml_p_info(parent_indent, current_indent, level + 1), block, so_far),
block then
unput_byte(c, s);
//println("block level " + level);
skip_to_next_node(s);
with current_indent = current_column(s),
if _read_node(s, yaml_p_info(current_indent, current_indent, level), none, []) is
{
failure then
success(null),
success(node_type) then
if node_type is
{
map(yaml_kvl) then
println("map node");
if read_block_maps(s, p_info, current_indent, yaml_kvl) is
{
failure then
success(map(var(reverse(yaml_kvl)))),
success(list_kv) then
success(map(var(reverse(list_kv))))
}
list(yaml_lv) then
println("list node");
if read_block_list(s, p_info, current_indent, yaml_lv) is
{
failure then
success(list(var(yaml_lv))),
success(list_v) then
success(list(var(reverse(list_v))))
}
value(yaml_value) then
//println("value node");
success(yaml_value)
}
}
}
}.
define Maybe(List(YAML_Value))
read_values
(
Word8 c,
Stream s,
YAML_Parser_Info p_info,
List(YAML_Value) so_far
)=
if c = '-' then
if read_byte(s) is
{
failure then
success(reverse(so_far)),
success(next_char) then
if next_char = ' ' then
skip_spaces(s);
println("found a list");
since p_info is yaml_p_info(parent_indent, context_indent, level),
with current_indent = current_column(s),
if (current_indent-1) =< parent_indent then
success(so_far)
else
if read_value(s, yaml_p_info((current_indent-1), current_indent, level), block, []) is
{
failure then
success(so_far),
success(v) then
//skip_to_next_node(s);
if read_byte(s) is
{
failure then
success(so_far),
success(next_next_char) then
read_values(next_next_char, s, p_info, [v . so_far])
}
}
else
unput_byte(next_char, s);
success(so_far),
}
else
success(so_far).
define Maybe(YAML_Parser_Node)
_read_node
(
Stream s,
YAML_Parser_Info p_info,
YAML_Parser_Node_Info node_info,
List(Word8) so_far
)=
if read_byte(s) is
{
failure then failure,
success(c) then
if node_info is
{
none then
if c = '-' then
if read_values(c, s, p_info, []) is
{
failure then
failure,
success(lv) then
success(list(lv))
}
else if c = '\"' then
_read_node(s, p_info, in_double_quote, so_far)
else if c = '\'' then
_read_node(s, p_info, in_single_quote, so_far)
else
_read_node(s, p_info, unquoted, [ c . so_far ]),
unquoted then
if c = ':' then
if read_byte(s) is
{
failure then
with key_name = implode(reverse(so_far)),
success(map([key_value(key_name, var(null))])),
success(next_char) then
if eol(s, next_char) then // \n
unput_byte(next_char, s);
with key_name = implode(reverse(so_far)),
println("key found [LF] \n name: " + key_name);
skip_spaces(s);
with value = read_value(s, p_info, none, []),
if value is
{
failure then failure,
success(v) then
success(map([key_value(key_name, var(v))]))
}
else if next_char = ' ' then
with key_name = implode(reverse(so_far)),
println("key found [ ]\n name: " + key_name);
skip_spaces(s);
with value = read_value(s, p_info, none, []),
if value is
{
failure then failure,
success(v) then
success(map([key_value(key_name, var(v))]))
}
else
todo
//_read_node(s, p_info, unquoted, [next_char . [c . so_far]])
}
else if eol(s, c) then
with str = implode(reverse(so_far)),
//println("value (str): " + str);
success(value(get_yaml_value(str)))
else
_read_node(s, p_info, unquoted, [ c . so_far ]),
in_single_quote then
todo,
in_double_quote then
todo
}
}.
public define Maybe(YAML_Document)
get_yaml_document_content
(
Stream s,
YAML_Document doc
)=
//skip all blanks of begin of line
skip_spaces(s);
with indent = current_column(s),
if read_byte(s) is
{
failure then
//println("skipping blank line");
//get_yaml_document_content(s, doc), //blank line
success(doc)
success(c) then
//check for comment
if eol(s, c) then
get_yaml_document_content(s, doc)
else if c = '#' then
//println("skipping comment ["+line+"]");
skip_to_next_line(s);
get_yaml_document_content(s, doc)
//check for entry of the map
else
unput_byte(c, s);
//println("map entry indent["+indent+"]");//["+line+"]");
with mb_node = _read_node(s, yaml_p_info(indent, indent, 0), none, /*make_stream(line)*/[]),
if mb_node is
{
failure then
println("Can't parse YAML node at column " + current_column(s) + " line " + current_line(s));
get_yaml_document_content(s, doc),
success(node_type) then
since doc is yaml_document(nodes),
if node_type is
{
map(yaml_kv) then
nodes <- [map(var(yaml_kv)) . *nodes];
get_yaml_document_content(s, yaml_document(nodes))
list(yaml_lv) then
nodes <- [list(var(yaml_lv)) . *nodes];
get_yaml_document_content(s, yaml_document(nodes))
value(yaml_value) then
failure
}
}
}. //read_byte
public define Maybe(YAML_Document)
get_yaml_document
(
Stream s,
)=
if read_line(s) is
{
failure then failure,
success(line) then
if start_with(line, "---") then
println("Start parsing YAML document");
get_yaml_document_content(s, yaml_document(var([])))
else
get_yaml_document(s)
}.
public define YAML
parse_YAML_file
(
Stream s,
Var(List(YAML_Document)) docs
)=
if get_yaml_document(s) is
{
failure then yaml(var(reverse(*docs))),
success(doc) then
docs <- [doc . *docs];
parse_YAML_file(s, docs)
}.
public define YAML
parse_YAML_file
(
Stream s
)=
if read_line(s) is
{
failure then yaml(var([])),
success(line) then
if start_with(line, "%YAML 1.1") then
println("%YAML 1.1 file");
parse_YAML_file(s, var([]))
else
yaml(var([]))
}.