diff options
Diffstat (limited to 'internal/parse.go')
-rw-r--r-- | internal/parse.go | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/internal/parse.go b/internal/parse.go index bb849e7..121857b 100644 --- a/internal/parse.go +++ b/internal/parse.go @@ -268,6 +268,8 @@ type Parser struct { // // 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 +295,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" + // ] ["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 +338,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 +379,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 +420,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: |