mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Removed context
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
package jlexer
|
||||
|
||||
import "io"
|
||||
|
||||
type Walker interface {
|
||||
OnEnterObject()
|
||||
OnNextObjectKey(key string)
|
||||
OnExitObject()
|
||||
|
||||
OnEnterArray()
|
||||
OnNextArrayElement()
|
||||
OnExitArray()
|
||||
}
|
||||
|
||||
func (l *Lexer) WalkUpToPosition(w Walker) error {
|
||||
l1 := &Lexer{}
|
||||
l1.Data = l.Data
|
||||
|
||||
type StackItem int
|
||||
const (
|
||||
Object StackItem = iota
|
||||
Array
|
||||
)
|
||||
|
||||
var stack []StackItem
|
||||
haveKey := false
|
||||
|
||||
for {
|
||||
l1.fetchToken()
|
||||
if l1.pos > l.pos || !l1.Ok() {
|
||||
break
|
||||
}
|
||||
switch {
|
||||
case l1.IsDelim('{'):
|
||||
l1.Skip()
|
||||
|
||||
stack = append(stack, Object)
|
||||
w.OnEnterObject()
|
||||
haveKey = false
|
||||
|
||||
case l1.IsDelim('}'):
|
||||
l1.Skip()
|
||||
|
||||
if len(stack) > 0 {
|
||||
stack = stack[:len(stack)-1]
|
||||
l1.WantComma()
|
||||
}
|
||||
w.OnExitObject()
|
||||
haveKey = false
|
||||
|
||||
case l1.IsDelim('['):
|
||||
l1.Skip()
|
||||
|
||||
stack = append(stack, Array)
|
||||
w.OnEnterArray()
|
||||
haveKey = false
|
||||
|
||||
case l1.IsDelim(']'):
|
||||
l1.Skip()
|
||||
|
||||
if len(stack) > 0 {
|
||||
stack = stack[:len(stack)-1]
|
||||
l1.WantComma()
|
||||
}
|
||||
w.OnExitArray()
|
||||
haveKey = false
|
||||
|
||||
case len(stack) > 0 && stack[len(stack)-1] == Object && !haveKey:
|
||||
key := l1.UnsafeString()
|
||||
w.OnNextObjectKey(key)
|
||||
|
||||
l1.WantColon()
|
||||
haveKey = true
|
||||
|
||||
case len(stack) > 0 && stack[len(stack)-1] == Array:
|
||||
w.OnNextArrayElement()
|
||||
l1.Skip()
|
||||
l1.WantComma()
|
||||
|
||||
default:
|
||||
l1.Skip()
|
||||
l1.WantComma()
|
||||
haveKey = false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if l1.Error() == io.EOF {
|
||||
return nil
|
||||
}
|
||||
return l1.Error()
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package jlexer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type TestWalker struct {
|
||||
ErrorPrefix string
|
||||
T *testing.T
|
||||
Items []string
|
||||
}
|
||||
|
||||
func (w *TestWalker) item(s string) {
|
||||
if len(w.Items) == 0 {
|
||||
w.T.Errorf("%sTestWalker(): no items left; want %q", w.ErrorPrefix, s)
|
||||
} else if w.Items[0] != s {
|
||||
w.T.Errorf("%sTestWalker(): got %q; want %q", w.ErrorPrefix, s, w.Items[0])
|
||||
w.Items = w.Items[1:]
|
||||
} else {
|
||||
w.Items = w.Items[1:]
|
||||
}
|
||||
}
|
||||
|
||||
func (w *TestWalker) OnEnterObject() { w.item("{") }
|
||||
func (w *TestWalker) OnExitObject() { w.item("}") }
|
||||
func (w *TestWalker) OnNextObjectKey(key string) { w.item("e:" + key) }
|
||||
func (w *TestWalker) OnEnterArray() { w.item("[") }
|
||||
func (w *TestWalker) OnNextArrayElement() { w.item("e") }
|
||||
func (w *TestWalker) OnExitArray() { w.item("]") }
|
||||
|
||||
func TestWalkUpToPosition(t *testing.T) {
|
||||
for i, test := range []struct {
|
||||
JSON string
|
||||
Items []string
|
||||
Start, End int
|
||||
}{
|
||||
{
|
||||
JSON: ``,
|
||||
Items: []string{},
|
||||
End: -1,
|
||||
}, {
|
||||
JSON: `{"aaa": 5, "qqq": 10}`,
|
||||
Items: []string{"{", "e:aaa", "e:qqq", "}"},
|
||||
End: -1,
|
||||
}, {
|
||||
JSON: `{"aaa": 5, "qqq": {"\t\t": null}}`,
|
||||
Items: []string{"{", "e:aaa", "e:qqq", "{", "e:\t\t", "}", "}"},
|
||||
End: -1,
|
||||
}, {
|
||||
JSON: `{"aaa": 5, "qqq": 10}`,
|
||||
End: len(`{"aaa": 5, "qqq": `),
|
||||
Items: []string{"{", "e:aaa", "e:qqq"},
|
||||
}, {
|
||||
JSON: `{"aaa": 5, "qqq": 10}`,
|
||||
End: len(`{"aaa": 5, `),
|
||||
Items: []string{"{", "e:aaa"},
|
||||
}, {
|
||||
JSON: `[null, false, {"aaa": 5}]`,
|
||||
Items: []string{"[", "e", "e", "{", "e:aaa", "}", "]"},
|
||||
End: -1,
|
||||
}, {
|
||||
JSON: `[null, "aaa"]`,
|
||||
Items: []string{"[", "e", "e", "]"},
|
||||
End: -1,
|
||||
},
|
||||
} {
|
||||
l := &Lexer{Data: []byte(test.JSON)}
|
||||
|
||||
if test.End != -1 {
|
||||
l.pos = test.End
|
||||
} else {
|
||||
l.pos = len(test.JSON)
|
||||
}
|
||||
w := &TestWalker{T: t, Items: test.Items, ErrorPrefix: fmt.Sprintf("[%d,%q] ", i, test.JSON)}
|
||||
|
||||
if err := l.WalkUpToPosition(w); err != nil {
|
||||
t.Errorf("[%d,%q] WalkUpToPosition() error: %v", i, test.JSON, err)
|
||||
}
|
||||
|
||||
if len(w.Items) > 0 {
|
||||
t.Errorf("[%d,%q] WalkUpToPosition: items %q left", i, test.JSON, w.Items)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user