Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validation of map at flow mode #523

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading