toml: improve inline-table parsing (#12307)

pull/12310/head
Larpon 2021-10-26 16:00:41 +02:00 committed by GitHub
parent c526752419
commit 1b9eef74aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 21 deletions

View File

@ -318,24 +318,38 @@ fn (p Parser) excerpt() string {
pub fn (mut p Parser) inline_table(mut tbl map[string]ast.Value) ? {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing inline table into ${ptr_str(tbl)}...')
mut previous_token_was_value := false
for p.tok.kind != .eof {
p.next() ?
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing token "$p.tok.kind"')
match p.tok.kind {
.hash {
// TODO table.comments << p.comment()
c := p.comment()
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping comment "$c.text"')
if previous_token_was_value {
if p.tok.kind != .rcbr {
p.expect(.comma) ?
}
previous_token_was_value = false
}
match p.tok.kind {
//.whitespace, .tab, .nl {
// util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping "$p.tok.kind "$p.tok.lit"')
//}
.comma {
if p.peek_tok.kind == .rcbr {
p.next() ? // Forward to the peek_tok
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' unexpected "$p.tok.kind" "$p.tok.lit" at this (excerpt): "...${p.excerpt()}..."')
}
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
}
.rcbr {
// ']' bracket
// '}' bracket
return
}
.bare, .quoted, .boolean, .number, .underscore {
@ -359,23 +373,17 @@ pub fn (mut p Parser) inline_table(mut tbl map[string]ast.Value) ? {
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' dead end at "$p.tok.kind" "$p.tok.lit"')
}
}
.lsbr {
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' unexpected "$p.tok.kind" "$p.tok.lit" at this (excerpt): "...${p.excerpt()}..."')
}
.eof {
return
previous_token_was_value = true
}
else {
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' could not parse $p.tok.kind ("$p.tok.lit") in this (excerpt): "...${p.excerpt()}..." token \n$p.tok')
' unexpected "$p.tok.kind" "$p.tok.lit" at this (excerpt): "...${p.excerpt()}..."')
}
}
if p.peek_tok.kind == .lsbr {
return
}
}
// Make sure the inline-table actually use the return at .rcbr match branch.
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' unexpected end of inline-table "$p.tok.kind" "$p.tok.lit" at this (excerpt): "...${p.excerpt()}..."')
}
// array_of_tables parses next tokens into an array of `ast.Value`s.

View File

@ -38,14 +38,10 @@ const (
'datetime/no-leads-with-milli.toml',
'datetime/no-leads.toml',
// Inline table
'inline-table/empty.toml',
'inline-table/double-comma.toml',
'inline-table/trailing-comma.toml',
'inline-table/linebreak-4.toml',
'inline-table/linebreak-3.toml',
'inline-table/linebreak-1.toml',
'inline-table/linebreak-2.toml',
'inline-table/no-comma.toml',
// Key
'key/duplicate.toml',
'key/after-table.toml',