/
tokenizer.v
546 lines (492 loc) · 12.2 KB
/
tokenizer.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
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
module template
import strings
import math { abs }
const default_start_delimiter = '{{'
const default_end_delimiter = '}}'
const default_content_size = 16
enum TokenType {
// Normal tokens appear verbatim in the final result
normal
// Comment tokens are absent in the output
comment
// Tags are used for simple interpolation queries
tag
// Raw tags are still interpolated but are not HTML escaped
raw_tag
// The different types of sections:
// Positive section is activated if the key is true
positive_section
// Negative section is activated if the key is false
negative_section
// Map section is activated for every key value pair
map_section
// List section is activated for every index item pair
iteration_section
// Close section marks the end of a section
close_section
// Partials are plugged into the current template
partial_section
// INTERNAL Program Execution Token
// Expanded list sections are associated with only one index of a list
expanded_iteration_section
}
struct Token {
content string
token_type TokenType
index int = -1
key string
}
fn (token Token) str() string {
return '${token.token_type}: "${token.content}"'
}
fn tokenize(input string) ![]Token {
// Validate the line endings in the template
line_ending := extract_line_ending(input)!
// Validate the use of indentation in the template
validate_indentation(input.split(line_ending))!
// Extract tokens ignoring line endings and indentation information
raw_tokens := extract_tokens(input)!
// Further refine in lines of tokens
token_lines := split_into_lines(raw_tokens, line_ending)
// Flatten lines of tokens into a regular sequence of tokens
clean_tokens := flatten_lines(token_lines, line_ending)
// Return tokens after one final simplification pass
return simplify_tokens(clean_tokens)
}
fn extract_tokens(input string) ![]Token {
mut tokens := []Token{}
mut buffer := strings.new_builder(template.default_content_size)
mut start_delim := template.default_start_delimiter
mut end_delim := template.default_end_delimiter
mut index := 0
for index < input.len {
// Skip to the beginning of a tag
mut continue_scanning := true
for input[index] != start_delim[0] {
buffer.write_u8(input[index])
index++
if index == input.len {
// We've reached the end
if buffer.len > 0 {
tokens << Token{
token_type: .normal
content: buffer.str()
}
}
continue_scanning = false
break
}
}
if !continue_scanning {
break
}
// The first character matches:
// Check to see if the full delimiter matches
mut delim_index := index
mut start_delim_found := true
for ch in start_delim {
if delim_index >= input.len {
return error('Reached an unexpected end of input while trying to scan opening delimiter.')
}
if input[delim_index] == ch {
delim_index++
} else {
// False alarm
start_delim_found = false
break
}
}
if !start_delim_found {
// False alarm, add all that we've matched so far to the buffer.
for _ in index .. delim_index {
buffer.write_u8(input[index])
index++
}
continue
}
// First, we save the buffer contents to the array
if buffer.len > 0 {
tokens << Token{
content: buffer.str()
token_type: .normal
}
}
// Next, we move the index to the start of the tag
index += start_delim.len
if index > input.len {
return error('Reached an unexpected end of input when trying to scan tag content.')
}
// Do we need to swap?
if input[index] == `=` {
index++
mut new_start := strings.new_builder(template.default_content_size)
mut new_end := strings.new_builder(template.default_content_size)
if index > input.len {
return error('Reached an unexpected end of input while trying to swap delimiters.')
}
// Skip whitespace
for input[index] in [` `, `\t`] {
index++
if index > input.len {
return error('Reached an unexpected end of input when trying to scan tag content.')
}
}
for input[index] !in [` `, `\t`] {
new_start.write_u8(input[index])
index++
if index > input.len {
return error('Reached an unexpected end of input while trying to scan new opening delimiter.')
}
}
// Skip whitespace
for input[index] in [` `, `\t`] {
index++
if index > input.len {
return error('Reached an unexpected end of input while trying to find new closing delimiter.')
}
}
for input[index] !in [` `, `\t`, `=`] {
new_end.write_u8(input[index])
index++
if index > input.len {
return error('Reached an unexpected end of input while trying to scan new closing delimiter.')
}
}
// Skip whitespace until `=`
for input[index] in [` `, `\t`] {
index++
if index > input.len {
return error('Reached an unexpected end of input while trying to find new closing delimiter.')
}
}
if input[index] != `=` {
return error('Expected "=" for finishing delimiter swap.')
}
index++
// Make sure the delimiter closes
for ch in end_delim {
if input[index] != ch {
return error('Closing delimiter not found for delimiter swap.')
}
index++
if index > input.len {
return error('Reached an unexpected end of input while trying to end swap delimiter section.')
}
}
// We've extracted the new start and end delimiters so apply them
start_delim = new_start.str()
end_delim = new_end.str()
// Add a dummy comment tag so that it can be removed if it is standalone
tokens << Token{
content: ''
token_type: .comment
}
// Move on to parsing rest of the input
continue
}
// We don't need to swap so read the tag contents
mut found_closing := false
for !found_closing {
for input[index] != end_delim[0] {
buffer.write_u8(input[index])
index++
if index >= input.len {
return error('Reached an unexpected end of input while trying to scan tag contents.')
}
}
// Check if we have found the closing delimiter
found_closing = true
delim_index = index
for ch in end_delim {
if input[delim_index] == ch {
delim_index++
} else {
found_closing = false
break
}
}
if !found_closing {
// Add the content to the buffer
for _ in index .. delim_index {
buffer.write_u8(input[index])
index++
}
}
}
// We've got the closing tag
index += end_delim.len
tag_content := buffer.str().trim_space()
tokens << match tag_content[0] {
`!` {
Token{
token_type: .comment
content: ''
}
}
`+` {
Token{
token_type: .positive_section
content: tag_content[1..].trim_space()
}
}
`-` {
Token{
token_type: .negative_section
content: tag_content[1..].trim_space()
}
}
`#` {
Token{
token_type: .map_section
content: tag_content[1..].trim_space()
}
}
`*` {
Token{
token_type: .iteration_section
content: tag_content[1..].trim_space()
}
}
`/` {
Token{
token_type: .close_section
content: tag_content[1..].trim_space()
}
}
`>` {
Token{
token_type: .partial_section
content: tag_content[1..].trim_space()
}
}
`{` {
// If we're using triple braces, we should remember to skip the last brace
offset := tag_content.len - if index < input.len && input[index] == `}` {
index++
0
} else {
1
}
Token{
token_type: .raw_tag
content: tag_content[1..offset].trim_space()
}
}
`&` {
Token{
token_type: .raw_tag
content: tag_content[1..].trim_space()
}
}
else {
Token{
token_type: .tag
content: tag_content
}
}
}
}
return tokens
}
struct TokenLine {
tokens []Token
}
fn (line TokenLine) str() string {
mut buffer := strings.new_builder(template.default_content_size)
buffer.write_string('\n((')
for index, token in line.tokens {
if index != 0 {
buffer.write_string(', ')
}
buffer.write_string(token.str())
}
buffer.write_string('))\n')
return buffer.str()
}
fn split_into_lines(tokens []Token, line_ending string) []TokenLine {
mut lines := []TokenLine{}
mut current_line := []Token{cap: tokens.len}
for token in tokens {
match token.token_type {
.normal {
mut local_lines := token.content.split(line_ending)
if local_lines.len == 0 {
continue
}
current_line << Token{
token_type: .normal
content: local_lines.first()
}
if local_lines.len == 1 {
continue
}
lines << TokenLine{
tokens: current_line.clone()
}
current_line.clear()
for index in 1 .. local_lines.len - 1 {
lines << TokenLine{
tokens: [
Token{
token_type: .normal
content: local_lines[index]
},
]
}
}
current_line << Token{
token_type: .normal
content: local_lines.last()
}
}
else {
current_line << token
}
}
}
if current_line.len > 0 {
lines << TokenLine{
tokens: current_line
}
}
return lines
}
fn simplify_tokens(tokens []Token) ![]Token {
mut simplified_tokens := []Token{cap: tokens.len}
mut buffer := strings.new_builder(template.default_content_size)
for token in tokens {
match token.token_type {
.comment {
// Empty buffer and skip adding the comment tag
if buffer.len > 0 {
simplified_tokens << Token{
token_type: .normal
content: buffer.str()
}
}
}
.normal {
buffer.write_string(token.content)
}
else {
// Empty buffer and add the token
if buffer.len > 0 {
simplified_tokens << Token{
token_type: .normal
content: buffer.str()
}
}
simplified_tokens << token
}
}
}
if buffer.len > 0 {
simplified_tokens << Token{
token_type: .normal
content: buffer.str()
}
}
return simplified_tokens
}
fn extract_indentation(line string) string {
mut indent_buffer := strings.new_builder(line.len)
for ch in line {
if ch in [` `, `\t`] {
indent_buffer.write_u8(ch)
} else {
break
}
}
return indent_buffer.str()
}
fn validate_indentation(lines []string) ! {
mut space_count, mut tab_count := 0, 0
for line in lines {
indent := extract_indentation(line)
for ch in indent {
match ch {
` ` {
space_count++
}
`\t` {
tab_count++
}
else {
return error('Indentation processing failed. Obtained a non-whitespace character: "${ch}"')
}
}
}
}
if space_count * tab_count != 0 {
// Both were non-zero indicating mixed character usage
return error('Please use consistent indentation. Do not mix spaces and tabs.')
}
}
fn extract_line_ending(input string) !string {
// Identify the line breaks
mut r_count, mut n_count := 0, 0
for ch in input {
match ch {
`\r` {
r_count++
}
`\n` {
n_count++
}
else {}
}
}
// Does the template have only one line
if r_count == 0 && n_count == 0 {
return '\n'
} else if r_count == 0 && n_count != 0 {
return '\n'
} else if r_count != 0 && n_count == 0 {
return '\r'
} else if r_count == n_count {
lines := input.split('\r\n')
if abs(n_count - lines.len) > 2 {
return error('Improper line endings used. Please use only "\\n", "\\r", or "\\r\\n".')
}
return '\r\n'
} else {
return error('Mixed line endings used. Please use only "\\n", "\\r", or "\\r\\n".')
}
}
fn flatten_lines(lines []TokenLine, line_ending string) []Token {
mut tokens := []Token{}
mut add_newline := false
for line in lines {
if add_newline {
tokens << Token{
token_type: .normal
content: line_ending
}
}
mut simple_line := []Token{cap: line.tokens.len}
for token in line.tokens {
if token.token_type == .normal && token.content.len == 0 {
continue
}
simple_line << token
}
// Check for standalone comment and section tags.
// The idea is that they are completely removed if they are not inline
// and the indentation and newlines do not persist.
if simple_line.len > 0 && simple_line.last().token_type !in [.normal, .tag] {
if simple_line.len == 2 && simple_line.first().content.trim_space().len == 0 {
simple_line.delete(0)
add_newline = false
} else if simple_line.len == 1 {
add_newline = false
} else {
// It is inline
add_newline = true
}
} else {
add_newline = true
}
tokens << simple_line
}
return tokens
}