diff --git a/parser/parser_test.go b/parser/parser_test.go index 1e6d556d..272c5601 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -440,6 +440,35 @@ a: | ccccccc d: eeeeeeeeeeeeeeeee +`, + }, + { + ` +a: b + c +`, + ` +a: b c +`, + }, + { + ` +a: + b: c +`, + ` +a: + b: c +`, + }, + { + ` +a: b +c: d +`, + ` +a: b +c: d `, }, } diff --git a/scanner/context.go b/scanner/context.go index 0ca04e14..0f500fa3 100644 --- a/scanner/context.go +++ b/scanner/context.go @@ -64,6 +64,17 @@ func (c *Context) addOriginBuf(r rune) { c.obuf = append(c.obuf, r) } +func (c *Context) removeRightSpaceFromBuf() int { + trimmedBuf := strings.TrimRight(string(c.obuf), " ") + buflen := len([]rune(trimmedBuf)) + diff := len(c.obuf) - buflen + if diff > 0 { + c.obuf = c.obuf[:buflen] + c.buf = []rune(c.bufferedSrc()) + } + return diff +} + func (c *Context) isDocument() bool { return c.isLiteral || c.isFolded || c.isRawFolded } diff --git a/scanner/scanner.go b/scanner/scanner.go index dc686593..d8bb770a 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -296,6 +296,19 @@ func (s *Scanner) scanNewLine(ctx *Context, c rune) { s.savedPos = s.pos() s.savedPos.Column -= len([]rune(ctx.bufferedSrc())) } + + // if the following case, origin buffer has unnecessary two spaces. + // So, `removeRightSpaceFromOriginBuf` remove them, also fix column number too. + // --- + // a:[space][space] + // b: c + removedNum := ctx.removeRightSpaceFromBuf() + if removedNum > 0 { + s.column -= removedNum + s.offset -= removedNum + s.savedPos.Column -= removedNum + } + if ctx.isEOS() { s.addBufferedTokenIfExists(ctx) } else if s.isAnchor {