diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-30 21:54:38 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-30 21:54:38 -0700 |
commit | 8467bdaa181257d031a258a05012dc85adbcb233 (patch) | |
tree | bc9bf437a34905f0b7249352043aff9e9d80ebe8 /internal/parse.go | |
parent | 0b57145421e7e4f165f64e73ee7c5d8102945569 (diff) | |
parent | 2e48a42fb9b9e946958810cfbb90ae85bee997e4 (diff) |
Merge branch 'lukeshu/quality2'
Diffstat (limited to 'internal/parse.go')
-rw-r--r-- | internal/parse.go | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/internal/parse.go b/internal/parse.go index bb849e7..b11aae6 100644 --- a/internal/parse.go +++ b/internal/parse.go @@ -264,10 +264,11 @@ type Parser struct { // // [ array: waiting for item to start or ']' // a array: reading item / waiting for ',' or ']' - // ] array: waiting for item to start // // Within each element type, the stack item is replaced, not pushed. // + // (Keep each of these examples in-sync with parse_test.go.) + // // For example, given the input string // // {"x":"y","a":"b"} @@ -293,9 +294,34 @@ type Parser struct { // o" {"x":"y","a":"b // o {"x":"y","a":"b" // {"x":"y","a":"b"} + // + // Or, given the input string + // + // ["x","y"] + // + // The stack would be + // + // stack processed + // ? + // [ [ + // a" [" + // a" ["x + // a ["x" + // a? ["x", + // a" ["x"," + // a" ["x","y + // a ["x","y" + // ["x","y"] stack []RuneType } +func (par *Parser) init() { + if !par.initialized { + par.initialized = true + par.pushState(runeTypeAny) + } +} + func (par *Parser) pushState(state RuneType) RuneType { par.stack = append(par.stack, state) return state @@ -311,6 +337,7 @@ func (par *Parser) popState() { } func (par *Parser) stackString() string { + par.init() var buf strings.Builder for _, s := range par.stack { buf.WriteString(s.String()) @@ -351,10 +378,7 @@ func (par *Parser) HandleEOF() (RuneType, error) { if par.err != nil { return RuneTypeError, par.err } - if !par.initialized { - par.initialized = true - par.pushState(runeTypeAny) - } + par.init() switch len(par.stack) { case 0: return RuneTypeEOF, nil @@ -395,10 +419,7 @@ func (par *Parser) HandleRune(c rune) (RuneType, error) { if par.err != nil { return RuneTypeError, par.err } - if !par.initialized { - par.initialized = true - par.pushState(runeTypeAny) - } + par.init() if len(par.stack) == 0 { switch c { case 0x0020, 0x000A, 0x000D, 0x0009: @@ -491,21 +512,12 @@ func (par *Parser) HandleRune(c rune) (RuneType, error) { par.pushState(runeTypeAny) return par.HandleRune(c) } - case RuneTypeArrayEnd: // waiting for item - switch c { - case 0x0020, 0x000A, 0x000D, 0x0009: - return RuneTypeSpace, nil - default: - par.replaceState(RuneTypeArrayComma) - par.pushState(runeTypeAny) - return par.HandleRune(c) - } case RuneTypeArrayComma: // waiting for ',' or ']' switch c { case 0x0020, 0x000A, 0x000D, 0x0009: return RuneTypeSpace, nil case ',': - par.replaceState(RuneTypeArrayEnd) + par.pushState(runeTypeAny) return RuneTypeArrayComma, nil case ']': par.popState() |