Skip to content

Commit

Permalink
Merge pull request #25 from goccy/feature/fix-nested-flow-style
Browse files Browse the repository at this point in the history
Fix nested flow style
  • Loading branch information
goccy authored Nov 7, 2019
2 parents e334b05 + 57e63c3 commit e7aa107
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
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

0 comments on commit e7aa107

Please sign in to comment.