Skip to content

Commit

Permalink
Merge pull request #35 from goccy/feature/fix-document
Browse files Browse the repository at this point in the history
Fix parsing of literal token
  • Loading branch information
goccy authored Nov 8, 2019
2 parents 1bfda0f + 7729266 commit d5d303c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
13 changes: 13 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,19 @@ a:
[ 1, 2 ]
}
c: d
`,
},
{
`
|
hoge
fuga
piyo`,
`
|
hoge
fuga
piyo
`,
},
}
Expand Down
4 changes: 4 additions & 0 deletions scanner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 5 additions & 3 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit d5d303c

Please sign in to comment.