From 1c37779bd46d988c9e7b4bdb846b9daa89856b7c Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Sun, 13 Aug 2017 20:50:21 +1000 Subject: [PATCH] Use .Token to return from StringLit to avoid convT2E --- html/html.go | 2 +- json/json.go | 4 ++-- literals.go | 6 +++--- literals_test.go | 12 ++++++------ scripts/memprofile.sh | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/html/html.go b/html/html.go index 05e091b..f6a715b 100644 --- a/html/html.go +++ b/html/html.go @@ -34,7 +34,7 @@ var ( attr := map[string]string{} for _, attrNode := range node.Child { - attr[attrNode.Child[0].Token] = attrNode.Child[2].Result.(string) + attr[attrNode.Child[0].Token] = attrNode.Child[2].Token } node.Result = attr diff --git a/json/json.go b/json/json.go index 9b1e8e9..18f6870 100644 --- a/json/json.go +++ b/json/json.go @@ -9,7 +9,7 @@ var ( _null = Bind("null", nil) _true = Bind("true", true) _false = Bind("false", false) - _string = StringLit(`"`) + _string = Map(StringLit(`"`), func(r *Result) { r.Result = r.Token }) _number = NumberLit() _properties = Some(Seq(StringLit(`"`), ":", &_value), ",") @@ -25,7 +25,7 @@ var ( ret := map[string]interface{}{} for _, prop := range n.Child[2].Child { - ret[prop.Child[0].Result.(string)] = prop.Child[2].Result + ret[prop.Child[0].Token] = prop.Child[2].Result } n.Result = ret diff --git a/literals.go b/literals.go index 00e39f9..260a26e 100644 --- a/literals.go +++ b/literals.go @@ -6,7 +6,7 @@ import ( "unicode/utf8" ) -// StringLit matches a quoted string and returns it in .Result. It may contain: +// StringLit matches a quoted string and returns it in .Token. It may contain: // - unicode // - escaped characters, eg \" or \n // - unicode sequences, eg \uBEEF @@ -59,12 +59,12 @@ func StringLit(allowedQuotes string) Parser { } case quote: if buf == nil { - node.Result = ps.Input[ps.Pos+1 : end] + node.Token = ps.Input[ps.Pos+1 : end] ps.Pos = end + 1 return } ps.Pos = end + 1 - node.Result = buf.String() + node.Token = buf.String() return default: if buf == nil { diff --git a/literals_test.go b/literals_test.go index 7cdb5e1..78a4456 100644 --- a/literals_test.go +++ b/literals_test.go @@ -10,19 +10,19 @@ func TestStringLit(t *testing.T) { parser := StringLit(`"'`) t.Run("test double match", func(t *testing.T) { result, p := runParser(`"hello"`, parser) - require.Equal(t, `hello`, result.Result) + require.Equal(t, `hello`, result.Token) require.Equal(t, "", p.Get()) }) t.Run("test single match", func(t *testing.T) { result, p := runParser(`"hello"`, parser) - require.Equal(t, `hello`, result.Result) + require.Equal(t, `hello`, result.Token) require.Equal(t, "", p.Get()) }) t.Run("test nested quotes", func(t *testing.T) { result, p := runParser(`"hello 'world'"`, parser) - require.Equal(t, `hello 'world'`, result.Result) + require.Equal(t, `hello 'world'`, result.Token) require.Equal(t, "", p.Get()) }) @@ -52,20 +52,20 @@ func TestStringLit(t *testing.T) { t.Run("test escaping", func(t *testing.T) { result, p := runParser(`"hello \"world\""`, parser) - require.Equal(t, `hello "world"`, result.Result) + require.Equal(t, `hello "world"`, result.Token) require.Equal(t, ``, p.Get()) }) t.Run("test unicode chars", func(t *testing.T) { result, p := runParser(`"hello 👺 my little goblin"`, parser) - require.Equal(t, `hello 👺 my little goblin`, result.Result) + require.Equal(t, `hello 👺 my little goblin`, result.Token) require.Equal(t, ``, p.Get()) }) t.Run("test escaped unicode", func(t *testing.T) { result, p := runParser(`"hello \ubeef cake"`, parser) require.Equal(t, "", p.Error.expected) - require.Equal(t, "hello \uBEEF cake", result.Result) + require.Equal(t, "hello \uBEEF cake", result.Token) require.Equal(t, ``, p.Get()) }) diff --git a/scripts/memprofile.sh b/scripts/memprofile.sh index 0fdc5ec..07ddd11 100644 --- a/scripts/memprofile.sh +++ b/scripts/memprofile.sh @@ -4,4 +4,4 @@ set -eu go build ./json/profile/json.go ./json.exe -memprofile mem.out -go tool pprof json.exe mem.out +go tool pprof --inuse_objects json.exe mem.out