Use .Token to return from StringLit to avoid convT2E

This commit is contained in:
Adam Scarr 2017-08-13 20:50:21 +10:00
parent 51ad26c781
commit 1c37779bd4
5 changed files with 13 additions and 13 deletions

View File

@ -34,7 +34,7 @@ var (
attr := map[string]string{} attr := map[string]string{}
for _, attrNode := range node.Child { 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 node.Result = attr

View File

@ -9,7 +9,7 @@ var (
_null = Bind("null", nil) _null = Bind("null", nil)
_true = Bind("true", true) _true = Bind("true", true)
_false = Bind("false", false) _false = Bind("false", false)
_string = StringLit(`"`) _string = Map(StringLit(`"`), func(r *Result) { r.Result = r.Token })
_number = NumberLit() _number = NumberLit()
_properties = Some(Seq(StringLit(`"`), ":", &_value), ",") _properties = Some(Seq(StringLit(`"`), ":", &_value), ",")
@ -25,7 +25,7 @@ var (
ret := map[string]interface{}{} ret := map[string]interface{}{}
for _, prop := range n.Child[2].Child { 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 n.Result = ret

View File

@ -6,7 +6,7 @@ import (
"unicode/utf8" "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 // - unicode
// - escaped characters, eg \" or \n // - escaped characters, eg \" or \n
// - unicode sequences, eg \uBEEF // - unicode sequences, eg \uBEEF
@ -59,12 +59,12 @@ func StringLit(allowedQuotes string) Parser {
} }
case quote: case quote:
if buf == nil { if buf == nil {
node.Result = ps.Input[ps.Pos+1 : end] node.Token = ps.Input[ps.Pos+1 : end]
ps.Pos = end + 1 ps.Pos = end + 1
return return
} }
ps.Pos = end + 1 ps.Pos = end + 1
node.Result = buf.String() node.Token = buf.String()
return return
default: default:
if buf == nil { if buf == nil {

View File

@ -10,19 +10,19 @@ func TestStringLit(t *testing.T) {
parser := StringLit(`"'`) parser := StringLit(`"'`)
t.Run("test double match", func(t *testing.T) { t.Run("test double match", func(t *testing.T) {
result, p := runParser(`"hello"`, parser) result, p := runParser(`"hello"`, parser)
require.Equal(t, `hello`, result.Result) require.Equal(t, `hello`, result.Token)
require.Equal(t, "", p.Get()) require.Equal(t, "", p.Get())
}) })
t.Run("test single match", func(t *testing.T) { t.Run("test single match", func(t *testing.T) {
result, p := runParser(`"hello"`, parser) result, p := runParser(`"hello"`, parser)
require.Equal(t, `hello`, result.Result) require.Equal(t, `hello`, result.Token)
require.Equal(t, "", p.Get()) require.Equal(t, "", p.Get())
}) })
t.Run("test nested quotes", func(t *testing.T) { t.Run("test nested quotes", func(t *testing.T) {
result, p := runParser(`"hello 'world'"`, parser) 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()) require.Equal(t, "", p.Get())
}) })
@ -52,20 +52,20 @@ func TestStringLit(t *testing.T) {
t.Run("test escaping", func(t *testing.T) { t.Run("test escaping", func(t *testing.T) {
result, p := runParser(`"hello \"world\""`, parser) 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()) require.Equal(t, ``, p.Get())
}) })
t.Run("test unicode chars", func(t *testing.T) { t.Run("test unicode chars", func(t *testing.T) {
result, p := runParser(`"hello 👺 my little goblin"`, parser) 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()) require.Equal(t, ``, p.Get())
}) })
t.Run("test escaped unicode", func(t *testing.T) { t.Run("test escaped unicode", func(t *testing.T) {
result, p := runParser(`"hello \ubeef cake"`, parser) result, p := runParser(`"hello \ubeef cake"`, parser)
require.Equal(t, "", p.Error.expected) 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()) require.Equal(t, ``, p.Get())
}) })

View File

@ -4,4 +4,4 @@ set -eu
go build ./json/profile/json.go go build ./json/profile/json.go
./json.exe -memprofile mem.out ./json.exe -memprofile mem.out
go tool pprof json.exe mem.out go tool pprof --inuse_objects json.exe mem.out