-
Notifications
You must be signed in to change notification settings - Fork 1
/
maple.v
340 lines (299 loc) · 8.83 KB
/
maple.v
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
module maple
import os
import strings
import strings.textscanner
import datatypes
// A sum type to represent any possible value in Maple
pub type ValueT = string | int | f32 | bool | map[string]ValueT | []ValueT
@[inline]
pub fn (value ValueT) to_str() string {
return value as string
}
@[inline]
pub fn (value ValueT) to_int() int {
return value as int
}
@[inline]
pub fn (value ValueT) to_f32() f32 {
return value as f32
}
@[inline]
pub fn (value ValueT) to_bool() bool {
return value as bool
}
@[inline]
pub fn (value ValueT) to_map() map[string]ValueT {
return value as map[string]ValueT
}
@[inline]
pub fn (value ValueT) to_array() []ValueT {
return value as []ValueT
}
@[inline]
pub fn (value ValueT) get(key string) ValueT {
if value is map[string]ValueT {
return value[key] or { panic('Failed to index map with key: ${key}') }
}
panic('Cannot invoke .get() on a non-map ValueT.')
}
@[inline]
pub fn (m map[string]ValueT) get(key string) ValueT {
return m[key] or { panic('Failed to index map with key: ${key}') }
}
// str converts the value to a parsable string
@[inline]
pub fn (value ValueT) str() string {
return value.serialize()
}
@[params]
pub struct SerializeOptions {
pub:
indents int
indent_str string = '\t'
}
// serialize converts a value to a parsable string.
pub fn (value ValueT) serialize(opts SerializeOptions) string {
match value {
string { return '\'${value}\'' }
int { return value.str() }
f32 { return value.str() }
bool { return value.str() }
map[string]ValueT {
indent_string := opts.indent_str.repeat(opts.indents + 1)
indented_opts := SerializeOptions{
...opts
indents: opts.indents + 1
}
mut s := '{\n'
for key, val in value {
s += '${indent_string}${key} = ${val.serialize(indented_opts)}\n'
}
return '${s}${opts.indent_str.repeat(opts.indents)}}'
}
[]ValueT {
indent_string := opts.indent_str.repeat(opts.indents + 1)
mut s := '[\n'
for val in value {
s += indent_string + val.serialize(SerializeOptions{
...opts
indents: opts.indents + 1
}) + ',\n'
}
return '${s}${opts.indent_str.repeat(opts.indents)}]'
}
}
}
// Represents an open brace on the brace stack. Used primarily for error messages.
struct Brace {
pub:
line int
col int
ch rune
}
// Used to hold data regarding line, column, and brace stack.
// Used primarily for descriptive error messages.
pub struct DeserializationContext {
pub mut:
line int = 1
col int = 1
brace_stack datatypes.Stack[Brace] = datatypes.Stack[Brace]{}
in_string bool
string_kind rune
string_start_line int = 1
string_start_col int = 1
}
// Splits a serialized array.
fn split_array(value string, mut con DeserializationContext) []string {
mut values := []string{}
mut builder := strings.new_builder(0)
mut prev := ` `
for ch in value#[1..-1] {
if ch == `'` && prev != `\\` {
con.in_string = !con.in_string
} else if !con.in_string {
if ch == `,` && con.brace_stack.is_empty() {
values << builder.str()
builder = strings.new_builder(0)
prev = ` `
continue
} else if ch == `{` || ch == `[` {
con.brace_stack.push(Brace{con.line, con.col, ch})
} else if ch == `}` || ch == `]` {
peeked := con.brace_stack.peek() or {
panic('Unexpected brace: ${ch} (at ${con.line}:${con.col})')
}
if (peeked.ch == `{` && ch != `}`) || (peeked.ch == `[` && ch != `]`) {
panic('Mismatched brace: ${ch} (at ${con.line}:${con.col})')
}
con.brace_stack.pop() or {
panic('Unexpected brace: ${ch} (at ${con.line}:${con.col})')
}
}
}
builder.write_u8(ch)
prev = ch
}
s := builder.str().trim_space()
if s.len > 0 {
values << s
}
return values
}
// Deserialize a value to a ValueT. See maple.laod for deserializing more than just a value.
pub fn deserialize(value string, mut con DeserializationContext) ValueT {
if value[0] == `{` && value[value.len - 1] == `}` {
l := load(value.all_after_first('{').all_before_last('}')) or {
println(err)
panic('Failed to load table value: ${value} (at ${con.line}:${con.col})')
}
return l
} else if value[0] == `[` && value[value.len - 1] == `]` {
return split_array(value, mut con).map(fn [mut con] (it string) ValueT {
return deserialize(it.trim_space(), mut con)
})
} else if (value[0] == `'` && value[value.len - 1] == `'`)
|| (value[0] == `"` && value[value.len - 1] == `"`) {
return value.substr_ni(1, -1)
} else if value == 'true' {
return true
} else if value == 'false' {
return false
} else if value.is_int() {
return value.int()
} else if value.count('.') == 1 && value.before('.').is_int() && value.after('.').is_int() {
return value.f32()
} else {
panic('Invalid value: ${value} (at ${con.line}:${con.col})')
}
}
// Serialize data to a string
pub fn save(data map[string]ValueT) string {
// We set an initial buffer of 1024 here because it will prevent smaller configs
// from needing to grow_len so often.
mut string_builder := strings.new_builder(1024)
for key, value in data {
string_builder.write_string('${key} = ${value.serialize()}\n')
}
return string_builder.str()
}
// Save a map[string]ValueT to a file
@[inline]
pub fn save_file(fp string, data map[string]ValueT) ! {
mut file := os.create(fp)!
defer { file.close() }
file.write_string(save(data))!
}
// Any form of whitespace recognized by the deserializer
pub const whitespace = ' \t\r\n\f'
// Deserialize code to a map[string]ValueT
pub fn load(code string) !map[string]ValueT {
mut context := DeserializationContext{}
mut table := map[string]ValueT{}
mut scanner := textscanner.new(code)
mut ch := ` `
mut buf := strings.new_builder(0)
mut buffered_key := ''
for {
ch = scanner.next()
if ch == `\n` {
context.col = 1
context.line++
}
if ch == -1 {
break
} else if (ch == `'` || ch == `"`) && scanner.peek_back() != `\\` {
if context.in_string && context.string_kind != ch {
buf.write_rune(ch)
context.col++
continue
}
if buffered_key.len == 0 {
panic('Unexpected string (at ${context.line}:${context.col})')
}
context.in_string = !context.in_string
context.string_kind = ch
if context.in_string {
context.string_start_line = context.line
context.string_start_col = context.col
} else {
context.string_start_line = -1
context.string_start_col = -1
}
} else if context.in_string {
// We check for a string early so that we do not have to
// spend a precious CPU cycle checking that in every if
// check after this
buf.write_rune(ch)
context.col++
continue
} else if ch == `/` && scanner.peek() == `/` {
for {
ch = scanner.next()
if ch == `\n` || ch == -1 {
break
}
}
continue
} else if ch == `=` && context.brace_stack.is_empty() {
if buf.len <= 0 {
panic('Unexpected `=` (at ${context.line}:${context.col})')
}
buffered_key = buf.str().trim_space()
buf = strings.new_builder(0)
continue
} else if (ch == `;` || ch == `\n`) && buf.len > 0 && context.brace_stack.is_empty()
&& buffered_key.len != 0 {
statement := buf.str()
table[buffered_key] = deserialize(statement.trim_space(), mut context)
buf = strings.new_builder(0)
buffered_key = ''
continue
} else if ch == `{` || ch == `[` {
context.brace_stack.push(Brace{context.line, context.col, ch})
} else if ch == `}` || ch == `]` {
peeked := context.brace_stack.peek() or {
panic('Unexpected brace: ${ch} (at ${context.line}:${context.col})')
}
if (peeked.ch == `{` && ch != `}`) || (peeked.ch == `[` && ch != `]`) {
panic('Mismatched brace: ${ch} (at ${context.line}:${context.col})')
}
context.brace_stack.pop() or {
panic('Unexpected brace: ${ch} (at ${context.line}:${context.col})')
}
if context.brace_stack.is_empty() && buffered_key.len != 0 {
buf.write_rune(ch)
statement := buf.str()
table[buffered_key] = deserialize(statement.trim_space(), mut context)
buf = strings.new_builder(0)
buffered_key = ''
continue
}
}
buf.write_rune(ch)
context.col++
}
if !context.brace_stack.is_empty() {
top_ch := context.brace_stack.peek()!.ch
kind := if top_ch == `[` {
'array'
} else if top_ch == `{` {
'map'
} else {
'unknown brace type (${top_ch})'
}
panic('Reached EOL before ${kind} ending. Brace stack: ${context.brace_stack} (started at ${context.brace_stack.peek()!.line}:${context.brace_stack.peek()!.col})')
} else if context.in_string {
panic('Reached EOL before string ending (string started at ${context.string_start_line}:${context.string_start_col})')
}
// Check for a final variable
if buffered_key.len != 0 {
statement := buf.str()
table[buffered_key] = deserialize(statement.trim_space(), mut context)
}
return table
}
// Load a map[string]ValueT from a file
@[inline]
pub fn load_file(fp string) !map[string]ValueT {
return load(os.read_file(fp)!)!
}