Skip to content

Commit

Permalink
fix sequence with null
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Nov 30, 2024
1 parent 239a991 commit 27d57b4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 15 deletions.
92 changes: 78 additions & 14 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,9 +680,6 @@ func (p *parser) parseMapValue(ctx *context, key ast.MapKeyNode, colonTk *Token)
if tk.Column() <= keyCol && tk.GroupType() == TokenGroupAnchorName {
// key: <value does not defined>
// &anchor
//
// key: <value does not defined>
// &anchor
return nil, errors.ErrSyntax("anchor is not allowed in this context", tk.RawToken())
}

Expand Down Expand Up @@ -939,17 +936,7 @@ func (p *parser) parseSequence(ctx *context) (*ast.SequenceNode, error) {
comment := p.parseHeadComment(ctx)
ctx.goNext() // skip sequence entry token

valueTk := ctx.currentToken()
if valueTk == nil {
node, err := newNullNode(ctx, ctx.createNullToken(seqTk))
if err != nil {
return nil, err
}
seqNode.Values = append(seqNode.Values, node)
break
}

value, err := p.parseToken(ctx.withIndex(uint(len(seqNode.Values))), valueTk)
value, err := p.parseSequenceValue(ctx.withIndex(uint(len(seqNode.Values))), seqTk)
if err != nil {
return nil, err
}
Expand All @@ -975,6 +962,83 @@ func (p *parser) parseSequence(ctx *context) (*ast.SequenceNode, error) {
return seqNode, nil
}

func (p *parser) parseSequenceValue(ctx *context, seqTk *Token) (ast.Node, error) {
tk := ctx.currentToken()
if tk == nil {
return newNullNode(ctx, ctx.insertNullToken(seqTk))
}

if ctx.isComment() {
tk = ctx.nextNotCommentToken()
}
seqCol := seqTk.Column()
seqLine := seqTk.Line()

if tk.Column() == seqCol && tk.Type() == token.SequenceEntryType {
// in this case,
// ----
// - <value does not defined>
// -
return newNullNode(ctx, ctx.insertNullToken(seqTk))
}

if tk.Line() == seqLine && tk.GroupType() == TokenGroupAnchorName &&
ctx.nextToken().Column() == seqCol && ctx.nextToken().Type() == token.SequenceEntryType {
// in this case,
// ----
// - &anchor
// -
group := &TokenGroup{
Type: TokenGroupAnchor,
Tokens: []*Token{tk, ctx.createNullToken(tk)},
}
anchor, err := p.parseAnchor(ctx.withGroup(group), group)
if err != nil {
return nil, err
}
ctx.goNext()
return anchor, nil
}

if tk.Column() <= seqCol && tk.GroupType() == TokenGroupAnchorName {
// - <value does not defined>
// &anchor
return nil, errors.ErrSyntax("anchor is not allowed in this sequence context", tk.RawToken())
}

if tk.Column() < seqCol {
// in this case,
// ----
// - <value does not defined>
// next
return newNullNode(ctx, ctx.insertNullToken(seqTk))
}

if tk.Line() == seqLine && tk.GroupType() == TokenGroupAnchorName &&
ctx.nextToken().Column() < seqCol {
// in this case,
// ----
// - &anchor
// next
group := &TokenGroup{
Type: TokenGroupAnchor,
Tokens: []*Token{tk, ctx.createNullToken(tk)},
}
anchor, err := p.parseAnchor(ctx.withGroup(group), group)
if err != nil {
return nil, err
}
ctx.goNext()
return anchor, nil
}

value, err := p.parseToken(ctx, ctx.currentToken())
if err != nil {
return nil, err
}
return value, nil
}

func (p *parser) parseDirective(ctx *context, g *TokenGroup) (*ast.DirectiveNode, error) {
node, err := newDirectiveNode(ctx, g.First())
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion yaml_test_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ var failureTestNames = []string{
"spec-example-6-6-line-folding-1-3",
"spec-example-6-8-flow-folding",
"spec-example-8-10-folded-lines-8-13-final-empty-lines",
"spec-example-8-15-block-sequence-entry-types",
"spec-example-8-17-explicit-block-mapping-entries",
"spec-example-8-2-block-indentation-indicator",
"spec-example-9-3-bare-documents",
Expand Down

0 comments on commit 27d57b4

Please sign in to comment.