toml: improve inline-table parsing (#12307)
parent
c526752419
commit
1b9eef74aa
|
@ -318,24 +318,38 @@ fn (p Parser) excerpt() string {
|
||||||
pub fn (mut p Parser) inline_table(mut tbl map[string]ast.Value) ? {
|
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)}...')
|
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing inline table into ${ptr_str(tbl)}...')
|
||||||
|
|
||||||
|
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"')
|
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing token "$p.tok.kind"')
|
||||||
match p.tok.kind {
|
|
||||||
.hash {
|
if previous_token_was_value {
|
||||||
// TODO table.comments << p.comment()
|
if p.tok.kind != .rcbr {
|
||||||
c := p.comment()
|
p.expect(.comma) ?
|
||||||
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping comment "$c.text"')
|
|
||||||
}
|
}
|
||||||
|
previous_token_was_value = false
|
||||||
|
}
|
||||||
|
|
||||||
|
match p.tok.kind {
|
||||||
//.whitespace, .tab, .nl {
|
//.whitespace, .tab, .nl {
|
||||||
// util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping "$p.tok.kind "$p.tok.lit"')
|
// util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping "$p.tok.kind "$p.tok.lit"')
|
||||||
//}
|
//}
|
||||||
.comma {
|
.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"')
|
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping comma table value seperator "$p.tok.lit"')
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
.rcbr {
|
.rcbr {
|
||||||
// ']' bracket
|
// '}' bracket
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
.bare, .quoted, .boolean, .number, .underscore {
|
.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 +
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
||||||
' dead end at "$p.tok.kind" "$p.tok.lit"')
|
' dead end at "$p.tok.kind" "$p.tok.lit"')
|
||||||
}
|
}
|
||||||
}
|
previous_token_was_value = true
|
||||||
.lsbr {
|
|
||||||
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
||||||
' unexpected "$p.tok.kind" "$p.tok.lit" at this (excerpt): "...${p.excerpt()}..."')
|
|
||||||
}
|
|
||||||
.eof {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
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.
|
// array_of_tables parses next tokens into an array of `ast.Value`s.
|
||||||
|
|
|
@ -38,14 +38,10 @@ const (
|
||||||
'datetime/no-leads-with-milli.toml',
|
'datetime/no-leads-with-milli.toml',
|
||||||
'datetime/no-leads.toml',
|
'datetime/no-leads.toml',
|
||||||
// Inline table
|
// Inline table
|
||||||
'inline-table/empty.toml',
|
|
||||||
'inline-table/double-comma.toml',
|
|
||||||
'inline-table/trailing-comma.toml',
|
|
||||||
'inline-table/linebreak-4.toml',
|
'inline-table/linebreak-4.toml',
|
||||||
'inline-table/linebreak-3.toml',
|
'inline-table/linebreak-3.toml',
|
||||||
'inline-table/linebreak-1.toml',
|
'inline-table/linebreak-1.toml',
|
||||||
'inline-table/linebreak-2.toml',
|
'inline-table/linebreak-2.toml',
|
||||||
'inline-table/no-comma.toml',
|
|
||||||
// Key
|
// Key
|
||||||
'key/duplicate.toml',
|
'key/duplicate.toml',
|
||||||
'key/after-table.toml',
|
'key/after-table.toml',
|
||||||
|
|
Loading…
Reference in New Issue