-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathflow.v
More file actions
180 lines (171 loc) · 3.43 KB
/
flow.v
File metadata and controls
180 lines (171 loc) · 3.43 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
module yaml
struct FlowParser {
src string
mut:
pos int
}
fn parse_flow_value(src string) !Any {
mut parser := FlowParser{
src: src
}
value := parser.parse_value()!
parser.skip_space()
if parser.pos != parser.src.len {
return error('yaml: unexpected trailing flow content')
}
return value
}
fn (mut p FlowParser) parse_value() !Any {
p.skip_space()
if p.pos >= p.src.len {
return error('yaml: unexpected end of flow value')
}
return match p.src[p.pos] {
`[` { p.parse_array() }
`{` { p.parse_object() }
`"`, `'` { Any(parse_quoted_flow_string(mut p)!) }
else { parse_scalar(p.parse_plain_token()) }
}
}
fn (mut p FlowParser) parse_array() !Any {
p.pos++
mut items := []Any{}
for {
p.skip_space()
if p.pos >= p.src.len {
return error('yaml: unterminated flow array')
}
if p.src[p.pos] == `]` {
p.pos++
break
}
items << p.parse_value()!
p.skip_space()
if p.pos >= p.src.len {
return error('yaml: unterminated flow array')
}
if p.src[p.pos] == `,` {
p.pos++
continue
}
if p.src[p.pos] == `]` {
p.pos++
break
}
return error('yaml: expected `,` or `]` in flow array')
}
return Any(items)
}
fn (mut p FlowParser) parse_object() !Any {
p.pos++
mut result := map[string]Any{}
for {
p.skip_space()
if p.pos >= p.src.len {
return error('yaml: unterminated flow object')
}
if p.src[p.pos] == `}` {
p.pos++
break
}
key := p.parse_key()!
p.skip_space()
if p.pos >= p.src.len || p.src[p.pos] != `:` {
return error('yaml: expected `:` in flow object')
}
p.pos++
result[key] = p.parse_value()!
p.skip_space()
if p.pos >= p.src.len {
return error('yaml: unterminated flow object')
}
if p.src[p.pos] == `,` {
p.pos++
continue
}
if p.src[p.pos] == `}` {
p.pos++
break
}
return error('yaml: expected `,` or `}` in flow object')
}
return Any(result)
}
fn (mut p FlowParser) parse_key() !string {
p.skip_space()
if p.pos >= p.src.len {
return error('yaml: unexpected end of flow key')
}
if p.src[p.pos] in [`"`, `'`] {
return parse_quoted_flow_string(mut p)
}
start := p.pos
for p.pos < p.src.len {
ch := p.src[p.pos]
if ch == `:` {
break
}
p.pos++
}
return p.src[start..p.pos].trim_space()
}
fn (mut p FlowParser) parse_plain_token() string {
start := p.pos
mut bracket_depth := 0
mut brace_depth := 0
for p.pos < p.src.len {
ch := p.src[p.pos]
if ch == `[` {
bracket_depth++
} else if ch == `]` {
if bracket_depth == 0 {
break
}
bracket_depth--
} else if ch == `{` {
brace_depth++
} else if ch == `}` {
if brace_depth == 0 {
break
}
brace_depth--
} else if ch == `,` && bracket_depth == 0 && brace_depth == 0 {
break
}
p.pos++
}
return p.src[start..p.pos].trim_space()
}
fn (mut p FlowParser) skip_space() {
for p.pos < p.src.len && p.src[p.pos].is_space() {
p.pos++
}
}
fn parse_quoted_flow_string(mut p FlowParser) !string {
start := p.pos
quote := p.src[p.pos]
p.pos++
mut escape := false
for p.pos < p.src.len {
ch := p.src[p.pos]
if quote == `"` {
if escape {
escape = false
} else if ch == `\\` {
escape = true
} else if ch == `"` {
p.pos++
return parse_quoted_string(p.src[start..p.pos])
}
} else if ch == `'` {
if p.pos + 1 < p.src.len && p.src[p.pos + 1] == `'` {
p.pos += 2
continue
}
p.pos++
return parse_quoted_string(p.src[start..p.pos])
}
p.pos++
}
return error('yaml: unterminated quoted flow string')
}