From bc988135d8679376cbc01a0eb9e9d4e2b7798c6c Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sat, 30 Nov 2024 14:39:09 +0900 Subject: [PATCH] fix invalid map-value --- parser/parser.go | 9 +++++++++ parser/token.go | 4 ++++ yaml_test_suite_test.go | 3 +-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index 9c6218a..5173393 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -651,6 +651,15 @@ func (p *parser) parseMapValue(ctx *context, key ast.MapKeyNode, colonTk *Token) keyCol := key.GetToken().Position.Column keyLine := key.GetToken().Position.Line + if tk.Column() != keyCol && tk.Line() == keyLine && (tk.GroupType() == TokenGroupMapKey || tk.GroupType() == TokenGroupMapKeyValue) { + // a: b: + // ^ + // + // a: b: c + // ^ + return nil, errors.ErrSyntax("mapping value is not allowed in this context", tk.RawToken()) + } + if tk.Column() == keyCol && p.isMapToken(tk) { // in this case, // ---- diff --git a/parser/token.go b/parser/token.go index 2fd3127..c7bc5e8 100644 --- a/parser/token.go +++ b/parser/token.go @@ -630,6 +630,10 @@ func createDocumentTokens(tokens []*Token) ([]*Token, error) { } func isScalarType(tk *Token) bool { + switch tk.GroupType() { + case TokenGroupMapKey, TokenGroupMapKeyValue: + return false + } typ := tk.Type() return typ == token.AnchorType || typ == token.AliasType || diff --git a/yaml_test_suite_test.go b/yaml_test_suite_test.go index dd32639..eae8f16 100644 --- a/yaml_test_suite_test.go +++ b/yaml_test_suite_test.go @@ -17,8 +17,7 @@ var failureTestNames = []string{ "anchors-on-empty-scalars", // no json. "aliases-in-flow-objects", // no json. "aliases-in-explicit-block-mapping", // no json. - "bare-document-after-document-end-marker", - "block-mapping-with-missing-keys", // no json. + "block-mapping-with-missing-keys", // no json. "block-mapping-with-missing-values", "block-mapping-with-multiline-scalars", "block-scalar-with-more-spaces-than-first-content-line",