toml: improve array parsing (#12322)

pull/12314/head
Larpon 2021-10-27 19:26:33 +02:00 committed by GitHub
parent 43fbc68f1e
commit bc3827ae15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View File

@ -508,15 +508,32 @@ pub fn (mut p Parser) array() ?[]ast.Value {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing array...') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing array...')
mut arr := []ast.Value{} mut arr := []ast.Value{}
p.expect(.lsbr) ? // '[' bracket p.expect(.lsbr) ? // '[' bracket
mut previous_token_was_value := false
for p.tok.kind != .eof { for p.tok.kind != .eof {
p.next() ? p.next() ?
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing token "$p.tok.kind" "$p.tok.lit"') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing token "$p.tok.kind" "$p.tok.lit"')
if previous_token_was_value {
if p.tok.kind != .rsbr && p.tok.kind != .hash {
p.expect(.comma) ?
}
previous_token_was_value = false
}
match p.tok.kind { match p.tok.kind {
.boolean { .boolean {
arr << ast.Value(p.boolean() ?) arr << ast.Value(p.boolean() ?)
previous_token_was_value = true
} }
.comma { .comma {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping comma array value seperator "$p.tok.lit"') // Trailing commas before array close is allowed
// so we skip `if p.peek_tok.kind == .rsbr { ... }`
if p.peek_tok.kind == .comma {
p.next() ? // Forward to the peek_tok
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' unexpected "$p.tok.kind" "$p.tok.lit" at this (excerpt): "...${p.excerpt()}..."')
}
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping comma table value seperator "$p.tok.lit"')
continue continue
} }
.eof { .eof {
@ -524,25 +541,29 @@ pub fn (mut p Parser) array() ?[]ast.Value {
' could not parse array. Reached EOF "$p.tok.kind" "$p.tok.lit" ("$p.tok.lit") in this (excerpt): "...${p.excerpt()}..."') ' could not parse array. Reached EOF "$p.tok.kind" "$p.tok.lit" ("$p.tok.lit") in this (excerpt): "...${p.excerpt()}..."')
} }
.hash { .hash {
// TODO array.comments << p.comment()
c := p.comment() c := p.comment()
p.ast_root.comments << c
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping comment "$c.text"') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping comment "$c.text"')
} }
.lcbr { .lcbr {
mut t := map[string]ast.Value{} mut t := map[string]ast.Value{}
p.inline_table(mut t) ? p.inline_table(mut t) ?
ast.Value(t) arr << ast.Value(t)
previous_token_was_value = true
} }
.number { .number {
val := p.number_or_date() ? val := p.number_or_date() ?
arr << val arr << val
previous_token_was_value = true
} }
.quoted { .quoted {
arr << ast.Value(p.quoted()) arr << ast.Value(p.quoted())
previous_token_was_value = true
} }
.lsbr { .lsbr {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing array in array "$p.tok.kind" "$p.tok.lit"') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing array in array "$p.tok.kind" "$p.tok.lit"')
arr << ast.Value(p.array() ?) arr << ast.Value(p.array() ?)
previous_token_was_value = true
} }
.rsbr { .rsbr {
break break

View File

@ -23,7 +23,7 @@ const (
'table/duplicate-table-array.toml', 'table/duplicate-table-array.toml',
// Array // Array
'array/tables-1.toml', 'array/tables-1.toml',
'array/missing-separator.toml', //'array/missing-separator.toml',
'array/text-after-array-entries.toml', 'array/text-after-array-entries.toml',
'array/text-before-array-separator.toml', 'array/text-before-array-separator.toml',
// Date / Time // Date / Time