Skip to content

Commit

Permalink
Merge pull request #52 from goccy/feature/fix-parser-error
Browse files Browse the repository at this point in the history
Fix parsing error of document like  `a: 0 - 1`
  • Loading branch information
goccy authored Dec 4, 2019
2 parents b24bdb8 + dd6a892 commit 7cba845
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
8 changes: 8 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ r: s
- b
- c - d - e
- f
`,
},
{
`
a: 0 - 1
`,
`
a: 0 - 1
`,
},
{`
Expand Down
34 changes: 18 additions & 16 deletions scanner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@ import (

// Context context at scanning
type Context struct {
idx int
size int
src []rune
buf []rune
obuf []rune
tokens token.Tokens
isRawFolded bool
isLiteral bool
isFolded bool
literalOpt string
idx int
size int
src []rune
buf []rune
obuf []rune
tokens token.Tokens
isRawFolded bool
isLiteral bool
isFolded bool
isSingleLine bool
literalOpt string
}

func newContext(src []rune) *Context {
return &Context{
idx: 0,
size: len(src),
src: src,
tokens: token.Tokens{},
buf: make([]rune, 0, len(src)),
obuf: make([]rune, 0, len(src)),
idx: 0,
size: len(src),
src: src,
tokens: token.Tokens{},
buf: make([]rune, 0, len(src)),
obuf: make([]rune, 0, len(src)),
isSingleLine: true,
}
}

Expand Down
8 changes: 8 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func (s *Scanner) scanNewLine(ctx *Context, c rune) {
}
ctx.addBuf(' ')
ctx.addOriginBuf(c)
ctx.isSingleLine = false
s.progressLine(ctx)
}

Expand Down Expand Up @@ -378,6 +379,13 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
s.progressColumn(ctx, 1)
continue
}
if ctx.bufferedSrc() != "" && ctx.isSingleLine {
// '-' is literal
ctx.addBuf(c)
ctx.addOriginBuf(c)
s.progressColumn(ctx, 1)
continue
}
nc := ctx.nextChar()
if nc == ' ' {
s.addBufferedTokenIfExists(ctx)
Expand Down

0 comments on commit 7cba845

Please sign in to comment.