From 4e14b770b34ffdaf528db79c642614708548c2e4 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 10 Dec 2024 10:24:51 +0900 Subject: [PATCH 1/2] fix node-anchor-not-indented --- parser/parser.go | 39 +++++++++++++++++++++++++++++++++++++++ yaml_test_suite_test.go | 1 - 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/parser/parser.go b/parser/parser.go index 08f9dfe..fe868a0 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -754,6 +754,11 @@ func (p *parser) parseMapValue(ctx *context, key ast.MapKeyNode, colonTk *Token) // &anchor return nil, errors.ErrSyntax("anchor is not allowed in this context", tk.RawToken()) } + if tk.Column() <= keyCol && tk.Type() == token.TagType { + // key: + // !!tag + return nil, errors.ErrSyntax("tag is not allowed in this context", tk.RawToken()) + } if tk.Column() < keyCol { // in this case, @@ -785,9 +790,38 @@ func (p *parser) parseMapValue(ctx *context, key ast.MapKeyNode, colonTk *Token) if err != nil { return nil, err } + if err := p.validateAnchorValueInMap(value, keyCol); err != nil { + return nil, err + } return value, nil } +func (p *parser) validateAnchorValueInMap(value ast.Node, keyCol int) error { + anchor, ok := value.(*ast.AnchorNode) + if !ok { + return nil + } + tag, ok := anchor.Value.(*ast.TagNode) + if !ok { + return nil + } + anchorTk := anchor.GetToken() + tagTk := tag.GetToken() + + if anchorTk.Position.Line == tagTk.Position.Line { + // key: + // &anchor !!tag + return nil + } + + if tagTk.Position.Column <= keyCol { + // key: &anchor + // !!tag + return errors.ErrSyntax("tag is not allowed in this context", tagTk) + } + return nil +} + func (p *parser) parseAnchor(ctx *context, g *TokenGroup) (*ast.AnchorNode, error) { anchorNameGroup := g.First().Group anchor, err := p.parseAnchorName(ctx.withGroup(anchorNameGroup)) @@ -1089,6 +1123,11 @@ func (p *parser) parseSequenceValue(ctx *context, seqTk *Token) (ast.Node, error // &anchor return nil, errors.ErrSyntax("anchor is not allowed in this sequence context", tk.RawToken()) } + if tk.Column() <= seqCol && tk.Type() == token.TagType { + // - + // !!tag + return nil, errors.ErrSyntax("tag is not allowed in this sequence context", tk.RawToken()) + } if tk.Column() < seqCol { // in this case, diff --git a/yaml_test_suite_test.go b/yaml_test_suite_test.go index edfe3f1..346ecc9 100644 --- a/yaml_test_suite_test.go +++ b/yaml_test_suite_test.go @@ -45,7 +45,6 @@ var failureTestNames = []string{ "multiline-scalar-at-top-level", // pass yamlv3. "multiline-scalar-at-top-level-1-3", // pass yamlv3. "nested-implicit-complex-keys", // no json. - "node-anchor-not-indented", // pass yamlv3. "plain-dashes-in-flow-sequence", "question-mark-edge-cases/00", // no json. "question-mark-edge-cases/01", // no json. From 3989c513a5e5ccb50da2ea8a842480b6f348edc6 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 10 Dec 2024 10:31:12 +0900 Subject: [PATCH 2/2] add validation for sequence value --- parser/parser.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index fe868a0..7e2370b 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -790,13 +790,13 @@ func (p *parser) parseMapValue(ctx *context, key ast.MapKeyNode, colonTk *Token) if err != nil { return nil, err } - if err := p.validateAnchorValueInMap(value, keyCol); err != nil { + if err := p.validateAnchorValueInMapOrSeq(value, keyCol); err != nil { return nil, err } return value, nil } -func (p *parser) validateAnchorValueInMap(value ast.Node, keyCol int) error { +func (p *parser) validateAnchorValueInMapOrSeq(value ast.Node, col int) error { anchor, ok := value.(*ast.AnchorNode) if !ok { return nil @@ -811,12 +811,17 @@ func (p *parser) validateAnchorValueInMap(value ast.Node, keyCol int) error { if anchorTk.Position.Line == tagTk.Position.Line { // key: // &anchor !!tag + // + // - &anchor !!tag return nil } - if tagTk.Position.Column <= keyCol { + if tagTk.Position.Column <= col { // key: &anchor // !!tag + // + // - &anchor + // !!tag return errors.ErrSyntax("tag is not allowed in this context", tagTk) } return nil @@ -1159,6 +1164,9 @@ func (p *parser) parseSequenceValue(ctx *context, seqTk *Token) (ast.Node, error if err != nil { return nil, err } + if err := p.validateAnchorValueInMapOrSeq(value, seqCol); err != nil { + return nil, err + } return value, nil }