Repository: goccy/go-yaml (go)

Issue:
not parsed after newline in block string
複数行文字列を単体でパースしようと思うと、二行目以降が切り捨てられてしまうようです。

https://play.golang.org/p/iHIxeMMDhT5

親構造体で `UnmarshalYAML`を定義していて、文字列のフィールドを個別にUnmarshal()する必要がある場合に問題がおきました。

Pull request description:
Fix parsing of literal token
resolve #33 #34 

Patch:
diff --git a/scanner/context.go b/scanner/context.go
index 2292f9b..d7f70df 100644
--- a/scanner/context.go
+++ b/scanner/context.go
@@ -123,6 +123,10 @@ func (c *Context) nextPos() int {
 func (c *Context) bufferedSrc() string {
 	src := strings.TrimLeft(string(c.buf), " ")
 	src = strings.TrimRight(src, " ")
+	if len(src) > 0 && src[len(src)-1] == '\n' && c.isDocument() && c.literalOpt == "-" {
+		// remove end '\n' character
+		src = src[:len(src)-1]
+	}
 	return src
 }
 
diff --git a/scanner/scanner.go b/scanner/scanner.go
index da73fce..907ede6 100644
--- a/scanner/scanner.go
+++ b/scanner/scanner.go
@@ -227,12 +227,14 @@ func (s *Scanner) scanComment(ctx *Context) (tk *token.Token, pos int) {
 }
 
 func (s *Scanner) scanLiteral(ctx *Context, c rune) {
+	ctx.addOriginBuf(c)
 	if ctx.isEOS() {
+		ctx.addBuf(c)
 		value := ctx.bufferedSrc()
 		ctx.addToken(token.New(value, string(ctx.obuf), s.pos()))
 		ctx.resetBuffer()
-	}
-	if c == '\n' {
+		s.progressColumn(ctx, 1)
+	} else if c == '\n' {
 		if ctx.isLiteral {
 			ctx.addBuf(c)
 		} else {
@@ -251,7 +253,6 @@ func (s *Scanner) scanLiteral(ctx *Context, c rune) {
 		ctx.addBuf(c)
 		s.progressColumn(ctx, 1)
 	}
-	ctx.addOriginBuf(c)
 }
 
 func (s *Scanner) scanLiteralHeader(ctx *Context) (pos int, err error) {
@@ -275,6 +276,7 @@ func (s *Scanner) scanLiteralHeader(ctx *Context) (pos int, err error) {
 					ctx.addToken(token.Folded(">"+opt, string(ctx.obuf), s.pos()))
 					ctx.isFolded = true
 				}
+				s.indentState = IndentStateKeep
 				ctx.resetBuffer()
 				ctx.literalOpt = opt
 				return


Test patch:
diff --git a/parser/parser_test.go b/parser/parser_test.go
index 49c09ac..2abd84b 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -393,6 +393,19 @@ a:
       [ 1, 2 ]
     }
   c: d
+`,
+		},
+		{
+			`
+|
+    hoge
+    fuga
+    piyo`,
+			`
+|
+    hoge
+    fuga
+    piyo
 `,
 		},
 	}