Skip to content

Commit

Permalink
fix invalid sequence at map context (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy authored Oct 31, 2024
1 parent bbcd692 commit 3788afc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
14 changes: 9 additions & 5 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,21 @@ func (p *parser) createMapValueNode(ctx *context, key ast.MapKeyNode, colonToken
}

func (p *parser) validateMapValue(ctx *context, key, value ast.Node) error {
keyColumn := key.GetToken().Position.Column
valueColumn := value.GetToken().Position.Column
if keyColumn != valueColumn {
keyTk := key.GetToken()
valueTk := value.GetToken()

if keyTk.Position.Line == valueTk.Position.Line && valueTk.Type == token.SequenceEntryType {
return errors.ErrSyntax("block sequence entries are not allowed in this context", valueTk)
}
if keyTk.Position.Column != valueTk.Position.Column {
return nil
}
if value.Type() != ast.StringType {
return nil
}
ntk := ctx.nextToken()
if ntk == nil || (ntk.Type != token.MappingValueType && ntk.Type != token.SequenceEntryType) {
return errors.ErrSyntax("could not find expected ':' token", value.GetToken())
return errors.ErrSyntax("could not find expected ':' token", valueTk)
}
return nil
}
Expand Down Expand Up @@ -352,7 +356,7 @@ func (p *parser) parseSequenceEntry(ctx *context) (*ast.SequenceNode, error) {
ctx.progress(1) // skip sequence token
tk = ctx.currentToken()
if tk == nil {
return nil, errors.ErrSyntax("empty sequence entry", ctx.previousToken())
return nil, errors.ErrSyntax("empty sequence value", ctx.previousToken())
}
var comment *ast.CommentGroupNode
if tk.Type == token.CommentType {
Expand Down
25 changes: 24 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ a:
- b: c
- `,
`
[4:1] empty sequence entry
[4:1] empty sequence value
2 | a:
3 | - b: c
> 4 | -
Expand Down Expand Up @@ -774,6 +774,29 @@ b
^
`,
},
{
`
a: -
b: -
`,
`
[3:4] empty sequence value
2 | a: -
> 3 | b: -
^
`,
},
{
`
a: - 1
b: - 2
`,
`
[2:4] block sequence entries are not allowed in this context
> 2 | a: - 1
^
3 | b: - 2`,
},
}
for _, test := range tests {
t.Run(test.source, func(t *testing.T) {
Expand Down

0 comments on commit 3788afc

Please sign in to comment.