toml: improve parsing of bare keys to include `-` and `_` (#12588)

pull/12590/head
Larpon 2021-11-27 14:46:05 +01:00 committed by GitHub
parent 8315e82188
commit d52b62a4f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 13 deletions

View File

@ -452,7 +452,7 @@ pub fn (mut p Parser) root_table() ? {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping formatting "$p.tok.kind" "$p.tok.lit"') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping formatting "$p.tok.kind" "$p.tok.lit"')
continue continue
} }
.bare, .quoted, .boolean, .number, .underscore { // NOTE .boolean allows for use of "true" and "false" as table keys .bare, .quoted, .boolean, .number, .minus, .underscore { // NOTE .boolean allows for use of "true" and "false" as table keys
// Peek forward as far as we can skipping over space formatting tokens. // Peek forward as far as we can skipping over space formatting tokens.
peek_tok, _ := p.peek_over(1, parser.keys_and_space_formatting) ? peek_tok, _ := p.peek_over(1, parser.keys_and_space_formatting) ?
@ -645,7 +645,7 @@ pub fn (mut p Parser) table_contents(mut tbl map[string]ast.Value) ? {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping formatting "$p.tok.kind" "$p.tok.lit"') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping formatting "$p.tok.kind" "$p.tok.lit"')
continue continue
} }
.bare, .quoted, .boolean, .number, .underscore { // NOTE .boolean allows for use of "true" and "false" as table keys .bare, .quoted, .boolean, .number, .minus, .underscore { // NOTE .boolean allows for use of "true" and "false" as table keys
// Peek forward as far as we can skipping over space formatting tokens. // Peek forward as far as we can skipping over space formatting tokens.
peek_tok, _ := p.peek_over(1, parser.keys_and_space_formatting) ? peek_tok, _ := p.peek_over(1, parser.keys_and_space_formatting) ?
@ -728,7 +728,7 @@ pub fn (mut p Parser) inline_table(mut tbl map[string]ast.Value) ? {
// '}' bracket // '}' bracket
return return
} }
.bare, .quoted, .boolean, .number, .underscore { .bare, .quoted, .boolean, .number, .minus, .underscore {
// Peek forward as far as we can skipping over space formatting tokens. // Peek forward as far as we can skipping over space formatting tokens.
peek_tok, _ := p.peek_over(1, parser.space_formatting) ? peek_tok, _ := p.peek_over(1, parser.space_formatting) ?
@ -829,7 +829,7 @@ pub fn (mut p Parser) array_of_tables_contents() ?[]ast.Value {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing token "$p.tok.kind"') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'parsing token "$p.tok.kind"')
match p.tok.kind { match p.tok.kind {
.bare, .quoted, .boolean, .number, .underscore { .bare, .quoted, .boolean, .number, .minus, .underscore {
// Peek forward as far as we can skipping over space formatting tokens. // Peek forward as far as we can skipping over space formatting tokens.
peek_tok, _ := p.peek_over(1, parser.space_formatting) ? peek_tok, _ := p.peek_over(1, parser.space_formatting) ?
@ -965,7 +965,7 @@ pub fn (mut p Parser) double_array_of_tables_contents(target_key DottedKey) ?[]a
} }
match p.tok.kind { match p.tok.kind {
.bare, .quoted, .boolean, .number, .underscore { .bare, .quoted, .boolean, .number, .minus, .underscore {
// Peek forward as far as we can skipping over space formatting tokens. // Peek forward as far as we can skipping over space formatting tokens.
peek_tok, _ = p.peek_over(1, parser.space_formatting) ? peek_tok, _ = p.peek_over(1, parser.space_formatting) ?
@ -1148,12 +1148,11 @@ pub fn (mut p Parser) key() ?ast.Key {
pos: pos pos: pos
}) })
} }
// number := p.number() as ast.Number
key = ast.Key(p.number()) key = ast.Key(p.number())
} else { } else {
key = match p.tok.kind { key = match p.tok.kind {
.bare, .underscore { .bare, .underscore, .minus {
ast.Key(p.bare()) ast.Key(p.bare() ?)
} }
.boolean { .boolean {
ast.Key(p.boolean() ?) ast.Key(p.boolean() ?)
@ -1296,10 +1295,22 @@ pub fn (mut p Parser) number_or_date() ?ast.Value {
} }
// bare parse and returns an `ast.Bare` type. // bare parse and returns an `ast.Bare` type.
pub fn (mut p Parser) bare() ast.Bare { pub fn (mut p Parser) bare() ?ast.Bare {
mut lits := p.tok.lit
pos := p.tok.position()
for p.peek_tok.kind != .assign && p.peek_tok.kind != .period && p.peek_tok.kind != .rsbr
&& p.peek_tok.kind !in parser.space_formatting {
p.next() ?
if p.tok.kind == .bare || p.tok.kind == .minus || p.tok.kind == .underscore {
lits += p.tok.lit
continue
}
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' bare key expected .bare, .minus, or .underscore but got "$p.tok.kind"')
}
return ast.Bare{ return ast.Bare{
text: p.tok.lit text: lits
pos: p.tok.position() pos: pos
} }
} }

View File

@ -15,7 +15,6 @@ const (
// Kept for easier handling of future updates to the tests // Kept for easier handling of future updates to the tests
valid_exceptions = [ valid_exceptions = [
'values/spec-key-value-pair-8.toml',
'values/spec-float-3.toml', 'values/spec-float-3.toml',
'values/spec-float-10.toml', 'values/spec-float-10.toml',
'values/spec-float-11.toml', 'values/spec-float-11.toml',
@ -23,7 +22,6 @@ const (
'values/spec-float-13.toml', 'values/spec-float-13.toml',
'values/spec-float-14.toml', 'values/spec-float-14.toml',
'values/spec-float-15.toml', 'values/spec-float-15.toml',
'values/spec-key-value-pair-6.toml',
] ]
invalid_exceptions = [ invalid_exceptions = [
'errors/table-3.toml', 'errors/table-3.toml',