From d933ff5f03e470e43ccaa6e8977e4f0efabb617c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 4 Jul 2022 20:59:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=87=8F=20"=E4=B8=8E:=E4=B9=8B?= =?UTF-8?q?=E9=97=B4=E6=97=A0=E6=95=88=E7=A9=BA=E6=A0=BC=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 3 ++- go.sum | 12 ++++++++++++ lexical.go | 37 ++++++++++++++++++++++++++++++++----- lexical_test.go | 4 ++-- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 54849f3..2c4fb69 100644 --- a/go.mod +++ b/go.mod @@ -3,15 +3,16 @@ module git.zhangdeman.cn/zhangdeman/filter go 1.17 require ( + git.zhangdeman.cn/zhangdeman/util v0.0.0-20220704053716-ec9271c0f350 github.com/Jeffail/gabs v1.4.0 github.com/pkg/errors v0.9.1 github.com/tidwall/gjson v1.14.1 ) require ( - git.zhangdeman.cn/zhangdeman/util v0.0.0-20220704053716-ec9271c0f350 // indirect github.com/go-ini/ini v1.66.6 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect + github.com/stretchr/testify v1.8.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 3144f52..cdd1798 100644 --- a/go.sum +++ b/go.sum @@ -2,18 +2,30 @@ git.zhangdeman.cn/zhangdeman/util v0.0.0-20220704053716-ec9271c0f350 h1:y54/TJ7S git.zhangdeman.cn/zhangdeman/util v0.0.0-20220704053716-ec9271c0f350/go.mod h1:aAIhnUdQewLipl4bddewAsAeSLST9SRvgTcPN5ITkAQ= github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo= github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-ini/ini v1.66.6 h1:h6k2Bb0HWS/BXXHCXj4QHjxPmlIU4NK+7MuLp9SD+4k= github.com/go-ini/ini v1.66.6/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/lexical.go b/lexical.go index 33b1332..5c1d8bc 100644 --- a/lexical.go +++ b/lexical.go @@ -15,12 +15,31 @@ import ( "github.com/pkg/errors" ) -// parseLexical 解析词法 +// NewLexical 获取实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:52 2022/7/4 +func NewLexical(jsonData string) *lexical { + return &lexical{jsonData: jsonData} +} + +// lexical 词法解析 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:42 2022/7/4 +type lexical struct { + jsonData string + keyLeftRightTokenCnt int +} + +// Parse 解析词法 // // Author : go_developer@163.com<白茶清欢> // // Date : 18:11 2022/7/4 -func parseLexical(jsonData string) ([]lexicalNode, error) { +func (l *lexical) Parse(jsonData string) ([]lexicalNode, error) { jsonData = strings.ReplaceAll(strings.ReplaceAll(jsonData, "\n", ""), "\t", "") // mt.Println(jsonData) if len(jsonData) < 2 { @@ -33,12 +52,20 @@ func parseLexical(jsonData string) ([]lexicalNode, error) { preChar := "-1" if len(lexicalList) > 0 { preChar = lexicalList[len(lexicalList)-1].Val - if preChar == objectLeftToken && currentChar == " " { + if len(tmpStr) == 0 && preChar == objectLeftToken && currentChar == " " { // 无意义的空格 continue } + if len(tmpStr) == 0 && currentChar == " " && preChar == keyLeftRightToken && l.keyLeftRightTokenCnt%2 == 0 { + // " : 之间的空格无意义 + continue + } } - if inputCharIsToken(currentChar, preChar) { + if l.inputCharIsToken(currentChar, preChar) { + if currentChar == keyLeftRightToken { + // 双引号计数 + l.keyLeftRightTokenCnt++ + } // 是关键词 if len(tmpStr) > 0 { lexicalList = append(lexicalList, lexicalNode{ @@ -65,7 +92,7 @@ func parseLexical(jsonData string) ([]lexicalNode, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 18:15 2022/7/4 -func inputCharIsToken(inputChar string, preChar string) bool { +func (l *lexical) inputCharIsToken(inputChar string, preChar string) bool { if preChar == escapeCharacterToken { // 前一个是转义符, 当前不是关键字 return false diff --git a/lexical_test.go b/lexical_test.go index d894b3b..6da71a2 100644 --- a/lexical_test.go +++ b/lexical_test.go @@ -13,7 +13,7 @@ import ( func Test_parseLexical(t *testing.T) { jsonData := `{ - "name":"zhangsan", + "name" :"zhangsan", "age":"18", "extension":{ "sex":"man", @@ -31,5 +31,5 @@ func Test_parseLexical(t *testing.T) { ] }` //jsonData = `{"name":"zhangsan","age":"18","extension":{"sex":"man","height":"180"},"teacher_list":[{"name":"t1","age":"11"},{"name":"t2","age":"12"}]}` - parseLexical(jsonData) + NewLexical(jsonData).Parse(jsonData) }