From 2e63415e8147fc273d87ac9e91c3a05c6069a8e5 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sun, 3 Nov 2024 21:32:51 +0900 Subject: [PATCH] fix invalid map token (#504) --- parser/parser.go | 5 +++++ parser/parser_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/parser/parser.go b/parser/parser.go index 7a765c85..5d7bfb35 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -447,6 +447,11 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) { ntk = p.nextNotCommentToken() antk = p.afterNextNotCommentToken() } + if tk := p.nextNotCommentToken(); tk != nil && tk.Position.Line > node.Start.Position.Line && tk.Position.Column > node.Start.Position.Column { + // a: b + // c <= this token is invalid. + return nil, errors.ErrSyntax("value is not allowed in this context", tk) + } if len(node.Values) == 1 { mapKeyCol := mvnode.Key.GetToken().Position.Column commentTk := p.nextToken() diff --git a/parser/parser_test.go b/parser/parser_test.go index 6b8b3ae9..7adfe77e 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -998,6 +998,44 @@ b }, { ` +a: 'b' + c: d +`, + ` +[3:3] value is not allowed in this context + 2 | a: 'b' +> 3 | c: d + ^ +`, + }, + { + ` +a: 'b' + - c +`, + ` +[3:3] value is not allowed in this context + 2 | a: 'b' +> 3 | - c + ^ +`, + }, + { + ` +a: 'b' + # comment + - c +`, + ` +[4:3] value is not allowed in this context + 2 | a: 'b' + 3 | # comment +> 4 | - c + ^ +`, + }, + { + ` a: 1 b - c