toml: check for more wrong line ending cases (#12424)

pull/12427/head
Larpon 2021-11-10 14:03:51 +01:00 committed by GitHub
parent d5e767f389
commit 66e53279c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -132,6 +132,18 @@ fn (mut p Parser) check(check_token token.Kind) ? {
} }
} }
// peek_for_correct_line_ending_or_fail peeks past any formatting tokens
// and return an error if the next token is not one of [.cr, .nl, .hash, .eof].
fn (mut p Parser) peek_for_correct_line_ending_or_fail() ? {
// Disallow anything else than [.cr, .nl, .hash, .eof] after any space formatting.
peek_tok := p.peek_over(1, parser.space_formatting) ?
if peek_tok.kind !in [.cr, .nl, .hash, .eof] {
p.next() ? // Forward to the peek_tok
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' unexpected EOL "$p.tok.kind" "$p.tok.lit" expected one of [.cr, .nl, .hash, .eof] at this (excerpt): "...${p.excerpt()}..."')
}
}
// check_one_of forwards the parser to the next token if the current // check_one_of forwards the parser to the next token if the current
// token's `Kind` can be found in `tokens`. Otherwise it returns an error. // token's `Kind` can be found in `tokens`. Otherwise it returns an error.
fn (mut p Parser) check_one_of(tokens []token.Kind) ? { fn (mut p Parser) check_one_of(tokens []token.Kind) ? {
@ -360,6 +372,7 @@ pub fn (mut p Parser) root_table() ? {
t[key_str] = val t[key_str] = val
} }
} }
p.peek_for_correct_line_ending_or_fail() ?
} }
.lsbr { .lsbr {
p.check(.lsbr) ? // '[' bracket p.check(.lsbr) ? // '[' bracket
@ -392,6 +405,7 @@ pub fn (mut p Parser) root_table() ? {
p.ignore_while(parser.space_formatting) p.ignore_while(parser.space_formatting)
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'setting root map key to `$p.root_map_key` at "$p.tok.kind" "$p.tok.lit"') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'setting root map key to `$p.root_map_key` at "$p.tok.kind" "$p.tok.lit"')
p.expect(.rsbr) ? p.expect(.rsbr) ?
p.peek_for_correct_line_ending_or_fail() ?
} else { } else {
// Parse `[key]` // Parse `[key]`
key := p.key() ? key := p.key() ?
@ -399,6 +413,7 @@ pub fn (mut p Parser) root_table() ? {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'setting root map key to `$p.root_map_key` at "$p.tok.kind" "$p.tok.lit"') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'setting root map key to `$p.root_map_key` at "$p.tok.kind" "$p.tok.lit"')
p.next() ? p.next() ?
p.expect(.rsbr) ? p.expect(.rsbr) ?
p.peek_for_correct_line_ending_or_fail() ?
} }
} }
.eof { .eof {
@ -518,6 +533,7 @@ pub fn (mut p Parser) array_of_tables(mut table map[string]ast.Value) ? {
key := p.key() ? key := p.key() ?
p.next() ? p.next() ?
p.check(.rsbr) ? p.check(.rsbr) ?
p.peek_for_correct_line_ending_or_fail() ?
p.check(.rsbr) ? p.check(.rsbr) ?
p.ignore_while(parser.all_formatting) p.ignore_while(parser.all_formatting)

View File

@ -28,12 +28,6 @@ const (
'datetime/impossible-date.toml', 'datetime/impossible-date.toml',
'datetime/no-leads-with-milli.toml', 'datetime/no-leads-with-milli.toml',
'datetime/no-leads.toml', 'datetime/no-leads.toml',
// Key
'key/after-table.toml',
'key/after-value.toml',
//'key/no-eol.toml',
'key/no-eol.toml',
'key/after-array.toml',
] ]
) )