Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested flow style #25

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,41 @@ func TestDecoder(t *testing.T) {
"v: 1[]{},!%?&*",
map[string]string{"v": "1[]{},!%?&*"},
},
{
"v: [1,[2,[3,[4,5],6],7],8]",
map[string]interface{}{
"v": []interface{}{
1,
[]interface{}{
2,
[]interface{}{
3,
[]int{4, 5},
6,
},
7,
},
8,
},
},
},
{
"v: {a: {b: {c: {d: e},f: g},h: i},j: k}",
map[string]interface{}{
"v": map[string]interface{}{
"a": map[string]interface{}{
"b": map[string]interface{}{
"c": map[string]string{
"d": "e",
},
"f": "g",
},
"h": "i",
},
"j": "k",
},
},
},
}
for _, test := range tests {
buf := bytes.NewBufferString(test.source)
Expand Down
48 changes: 24 additions & 24 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ const (
// Scanner holds the scanner's internal state while processing a given text.
// It can be allocated as part of another data structure but must be initialized via Init before use.
type Scanner struct {
source string
sourcePos int
sourceSize int
line int
column int
offset int
prevIndentLevel int
prevIndentNum int
prevIndentColumn int
indentLevel int
indentNum int
isFirstCharAtLine bool
isAnchor bool
isStartedFlowSequence bool
isStartedFlowMap bool
indentState IndentState
savedPos *token.Position
source string
sourcePos int
sourceSize int
line int
column int
offset int
prevIndentLevel int
prevIndentNum int
prevIndentColumn int
indentLevel int
indentNum int
isFirstCharAtLine bool
isAnchor bool
startedFlowSequenceNum int
startedFlowMapNum int
indentState IndentState
savedPos *token.Position
}

func (s *Scanner) pos() *token.Position {
Expand Down Expand Up @@ -299,16 +299,16 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
if ctx.bufferedSrc() == "" {
ctx.addOriginBuf(c)
ctx.addToken(token.MappingStart(string(ctx.obuf), s.pos()))
s.isStartedFlowMap = true
s.startedFlowMapNum++
s.progressColumn(ctx, 1)
return
}
case '}':
if ctx.bufferedSrc() == "" || s.isStartedFlowMap {
if ctx.bufferedSrc() == "" || s.startedFlowMapNum > 0 {
ctx.addToken(s.bufferedToken(ctx))
ctx.addOriginBuf(c)
ctx.addToken(token.MappingEnd(string(ctx.obuf), s.pos()))
s.isStartedFlowMap = false
s.startedFlowMapNum--
s.progressColumn(ctx, 1)
return
}
Expand Down Expand Up @@ -357,21 +357,21 @@ func (s *Scanner) scan(ctx *Context) (pos int) {
if ctx.bufferedSrc() == "" {
ctx.addOriginBuf(c)
ctx.addToken(token.SequenceStart(string(ctx.obuf), s.pos()))
s.isStartedFlowSequence = true
s.startedFlowSequenceNum++
s.progressColumn(ctx, 1)
return
}
case ']':
if ctx.bufferedSrc() == "" || s.isStartedFlowSequence {
if ctx.bufferedSrc() == "" || s.startedFlowSequenceNum > 0 {
s.addBufferedTokenIfExists(ctx)
ctx.addOriginBuf(c)
ctx.addToken(token.SequenceEnd(string(ctx.obuf), s.pos()))
s.isStartedFlowSequence = false
s.startedFlowSequenceNum--
s.progressColumn(ctx, 1)
return
}
case ',':
if s.isStartedFlowSequence || s.isStartedFlowMap {
if s.startedFlowSequenceNum > 0 || s.startedFlowMapNum > 0 {
s.addBufferedTokenIfExists(ctx)
ctx.addOriginBuf(c)
ctx.addToken(token.CollectEntry(string(ctx.obuf), s.pos()))
Expand Down