Skip to content

Commit

Permalink
fix validation of map at flow mode
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Nov 12, 2024
1 parent c82f89f commit 0ba8d9c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
37 changes: 28 additions & 9 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (p *parser) parseSequence(ctx *context) (*ast.SequenceNode, error) {
break
}

value, err := p.parseToken(ctx.withIndex(uint(len(node.Values))), p.currentToken())
value, err := p.parseToken(ctx.withIndex(uint(len(node.Values))).withFlow(true), p.currentToken())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -481,14 +481,8 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) {
ntk = p.nextNotCommentToken()
antk = p.afterNextNotCommentToken()
}
validationTk := node.Start
if len(node.Values) != 0 {
validationTk = node.Values[len(node.Values)-1].Key.GetToken()
}
if tk := p.nextNotCommentToken(); tk != nil && tk.Position.Line > validationTk.Position.Line && tk.Position.Column > validationTk.Position.Column {
// a: b
// c <= this token is invalid.
return nil, errors.ErrSyntax("value is not allowed in this context", tk)
if err := p.validateMapNextToken(ctx, node); err != nil {
return nil, err
}
if len(node.Values) == 1 {
mapKeyCol := mvnode.Key.GetToken().Position.Column
Expand All @@ -512,6 +506,31 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) {
return node, nil
}

func (p *parser) validateMapNextToken(ctx *context, node *ast.MappingNode) error {
keyTk := node.Start
if len(node.Values) != 0 {
keyTk = node.Values[len(node.Values)-1].Key.GetToken()
}
tk := p.nextNotCommentToken()
if tk == nil {
return nil
}

if ctx.isFlow && (tk.Type == token.CollectEntryType || tk.Type == token.SequenceEndType || tk.Type == token.MappingEndType) {
// a: {
// key: value
// } , <= if context is flow mode, "," or "]" or "}" is allowed.
return nil
}

if tk.Position.Line > keyTk.Position.Line && tk.Position.Column > keyTk.Position.Column {
// a: b
// c <= this token is invalid.
return errors.ErrSyntax("value is not allowed in this context", tk)
}
return nil
}

func (p *parser) parseFlowMapNullValue(ctx *context, key ast.MapKeyNode) (*ast.MappingValueNode, error) {
tk := p.currentToken()
if tk == nil {
Expand Down
20 changes: 20 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ func TestParser(t *testing.T) {
"a:\n-",
"a: {foo}",
"a: {foo,bar}",
`
{
a: {
b: c
},
d: e
}
`,
`
[
a: {
b: c
}]
`,
`
{
a: {
b: c
}}
`,
}
for _, src := range sources {
if _, err := parser.Parse(lexer.Tokenize(src), 0); err != nil {
Expand Down

0 comments on commit 0ba8d9c

Please sign in to comment.