yaml_parser.anubis
17.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 07/06/2016
* Time: 23:15
* © David RENÉ
*/
/*
* YAML parser
*
* Known issues:
* - A list at the same level of a key which is not in a root node is ignored
* Example ("Key" value will be of type "null"):
* RootNode:
* Key:
* - ignored list item
* - ignored list item
*
* - The parser fail to parse correctly this sort of list :
* RootNode:
* - Key1: value
* Key2: value
* Key3: value
* - Key4: value
*
* Key1, Key2, Key3 will be parsed as list items independently and not as a single list item made of multiple keys
*
* - Lists and "objects" which are not in a block are not implemented yet
* Example :
* Node: [item1, item2]
* or :
* Node: {key1: value, key2: value}
*
* - Double quoted and single quoted nodes are not implemented yet
* Example :
* "Node": value
* 'Node': value
*/
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_String_State:
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 Maybe(YAML_Value)
extract_yaml_string
(
String str,
Stream s,
YAML_Parser_String_State ss,
List(Word8) so_far
)=
if read_byte(s) is
{
failure then success(string(var(implode(reverse(so_far))))),
success(c) then
if ss is
{
none then
if c = '\"' then
extract_yaml_string(str, s, in_double_quote, so_far)
else if c = '\'' then
extract_yaml_string(str, s, in_single_quote, so_far)
else
extract_yaml_string(str, s, unquoted, so_far),
unquoted then
success(string(var(str))),
in_single_quote then
if read_byte(s) is
{
failure then
if c = '\'' then
success(string(var(implode(reverse(so_far)))))
else
failure,
success(nc) then
unput_byte(nc, s);
extract_yaml_string(str, s, in_single_quote, [c . so_far])
},
in_double_quote then
if read_byte(s) is
{
failure then
if c = '\"' then
success(string(var(implode(reverse(so_far)))))
else
failure,
success(nc) then
unput_byte(nc, s);
extract_yaml_string(str, s, in_double_quote, [c . so_far])
},
}
}.
// should be of type Maybe(YAML_Value)
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 str = "true" then
bool(var(true))
else if str = "false" then
bool(var(false))
else
if decimal_scan(str) is
{
failure then
if extract_yaml_string(str, make_stream(str), none, []) is
{
failure then null,
success(v) then v
}
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)),
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 ctx_info is
{
none then
read_value(s, p_info, line, [c . so_far]),
line then
//println(implode(reverse([c . so_far])));
if c = '#' then
skip_to_next_line(s);
success(get_yaml_value(implode(reverse(so_far))))
else
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),
// TODO : this return "null" if list character "- " is on the same column as the key first character (it should not)
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
if read_byte(s) is
{
failure then
success([v . so_far]),
success(next_next_char) then
read_values(next_next_char, s, p_info, [v . so_far])
}
}
else
unput_byte(next_char, s);
unput_byte(c, s);
success(so_far),
}
else
unput_byte(c, s);
success(so_far).
define Maybe(YAML_Parser_Node)
get_key_node
(
Stream s,
YAML_Parser_Info p_info,
List(Word8) so_far
)=
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
failure
//_read_node(s, p_info, unquoted, [next_char . [c . 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
get_key_node(s, p_info, 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 if c = '#' then
with str = implode(reverse(so_far)),
skip_to_next_line(s);
success(value(get_yaml_value(str)))
else
//println("" + implode(reverse(so_far)));
_read_node(s, p_info, unquoted, [ c . so_far ]),
in_single_quote then
todo,
in_double_quote then
todo
// _read_node(s, p_info, in_double_quote, [ c . so_far ])
}
}.
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
//println("Value found on root");
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([]))
}.