Skip to content

Commit

Permalink
Fix handling of quoted map key
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Dec 1, 2022
1 parent 045101d commit db6dd54
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion scanner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ func (c *Context) previousChar() rune {
}

func (c *Context) currentChar() rune {
return c.src[c.idx]
if c.size > c.idx {
return c.src[c.idx]
}
return rune(0)
}

func (c *Context) nextChar() rune {
Expand Down Expand Up @@ -203,3 +206,10 @@ func (c *Context) bufferedToken(pos *token.Position) *token.Token {
c.resetBuffer()
return tk
}

func (c *Context) lastToken() *token.Token {
if len(c.tokens) != 0 {
return c.tokens[len(c.tokens)-1]
}
return nil
}
11 changes: 11 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,12 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
if tk != nil {
s.prevIndentColumn = tk.Position.Column
ctx.addToken(tk)
} else if tk := ctx.lastToken(); tk != nil {
// If the map key is quote, the buffer does not exist because it has already been cut into tokens.
// Therefore, we need to check the last token.
if tk.Indicator == token.QuotedScalarIndicator {
s.prevIndentColumn = tk.Position.Column
}
}
ctx.addToken(token.MappingValue(s.pos()))
s.progressColumn(ctx, 1)
Expand Down Expand Up @@ -805,6 +811,11 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
token, progress := s.scanQuote(ctx, c)
ctx.addToken(token)
pos += progress
// If the quote is immediately followed by ':', the quote should be treated as a map key.
// Therefore, do not return and continue processing as a normal map key.
if ctx.currentChar() == ':' {
continue
}
return
}
case '\r', '\n':
Expand Down

0 comments on commit db6dd54

Please sign in to comment.