diff --git a/scanner/context.go b/scanner/context.go index 8cd18cf7..c6ec5686 100644 --- a/scanner/context.go +++ b/scanner/context.go @@ -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 { @@ -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 +} diff --git a/scanner/scanner.go b/scanner/scanner.go index 1e09190e..c0072cb9 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -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) @@ -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':