From 0e676032de96c2d9641a8ff467d068fc7685cf46 Mon Sep 17 00:00:00 2001 From: Aleksandr Petrukhin Date: Wed, 11 Jan 2017 19:49:25 +0300 Subject: [PATCH] Removed context --- jlexer/context.go | 92 ------------------------------------------ jlexer/context_test.go | 85 -------------------------------------- 2 files changed, 177 deletions(-) delete mode 100644 jlexer/context.go delete mode 100644 jlexer/context_test.go diff --git a/jlexer/context.go b/jlexer/context.go deleted file mode 100644 index ec53902..0000000 --- a/jlexer/context.go +++ /dev/null @@ -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() -} diff --git a/jlexer/context_test.go b/jlexer/context_test.go deleted file mode 100644 index 3b7ee58..0000000 --- a/jlexer/context_test.go +++ /dev/null @@ -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) - } - } -}