yaml_parser.anubis
4.3 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
/*
* 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 indent_size, Int level).
define Maybe(String)
_read_key
(
Stream current_line,
List(Word8) so_far
)=
failure.
if read_byte(current_line) is
{
failure then failure,
success(char) then
if in_word then
else if in_single_quote then
else if
if char
.
define Maybe(String)
read_key
(
Stream current_line
)=
_read_key(current_line, []).
define Maybe(YAML_Key_Value)
read_key_value
(
Stream s,
YAML_Parser_Info p_info,
Stream current_line,
)=
if read_key(current_line) is
{
failure then failure,
success(key) then failure
// if read_value(s, p_info, current_line) is
// {
// failure then failure,
// success(value) then
// success(key_value(key, var(value)))
// }
}
.
public define List(YAML_Key_Value)
get_yaml_map
(
Stream s,
YAML_Parser_Info p_info,
Stream current_line,
List(YAML_Key_Value) so_far
)=
if read_key_value(s, p_info, current_line) is
{
failure then
println("can't read key value");
reverse(so_far),
success(key_value) then
if read_line(s) is
{
failure then reverse(so_far),
success(line) then
with line_s = make_stream(line),
//skip all blanks of begin of line
skip_blanks(line_s);
with indent = current_column(line_s),
get_yaml_map(s, p_info, line_s, [key_value . so_far])
}
}.
public define Maybe(YAML_Document)
get_yaml_document_content
(
Stream s,
YAML_Document doc
)=
if read_line(s) is
{
failure then success(doc)
success(line) then
with line_s = make_stream(line),
//skip all blanks of begin of line
skip_blanks(line_s);
with indent = current_column(line_s),
if read_byte(line_s) is
{
failure then
println("skipping blank line");
get_yaml_document_content(s, doc), //blank line
success(c) then
//check for comment
if c = '#' then
//println("skipping comment ["+line+"]");
get_yaml_document_content(s, doc)
//check for item of the list
else if c = '-' then
println("list item indent["+indent+"]["+line+"]");
get_yaml_document_content(s, doc)
//check for entry of the map
else
println("map entry indent["+indent+"]["+line+"]");
with list_key_val = get_yaml_map(s, yaml_p_info(indent, 0), line_s, []),
if list_key_val is
{
[] then
println("Can't get map entries");
get_yaml_document_content(s, doc),
[_ . _] then
since doc is yaml_document(nodes),
nodes <- [map(var(list_key_val)) . *nodes];
get_yaml_document_content(s, yaml_document(nodes))
}
} //read_byte
}. //read_line
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([]))
}.