diff --git a/lexical.go b/lexical.go index aa77818..314d9c6 100644 --- a/lexical.go +++ b/lexical.go @@ -41,13 +41,13 @@ type lexical struct { // Author : go_developer@163.com<白茶清欢> // // Date : 18:11 2022/7/4 -func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) { +func (l *lexical) Parse() error { // mt.Println(jsonData) - if len(jsonData) < 2 { - return nil, errors.New("input data is not json") + if len(l.jsonData) < 2 { + return errors.New("input data is not json") } tmpStr := "" - for _, itemChar := range jsonData { + for _, itemChar := range l.jsonData { currentChar := string(itemChar) tmpRealVal, tmpStrType := l.getTmpStrType(tmpStr) if l.inputCharIsToken(currentChar, tmpStr, tmpStrType) { @@ -83,7 +83,7 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) { } } - return l.lexicalResult, nil + return nil } // inputCharIsToken 输入字符是否为关键字 @@ -272,3 +272,16 @@ func (l *lexical) getTmpStrType(tmpStr string) (interface{}, string) { } return tmpStr, "string" } + +// String 将解析后的词法, 转换为字符串 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:34 2022/7/5 +func (l *lexical) String() string { + str := "" + for _, item := range l.lexicalResult { + str = str + item.ValStr() + } + return str +} diff --git a/lexical_test.go b/lexical_test.go index 98a6dd2..e475662 100644 --- a/lexical_test.go +++ b/lexical_test.go @@ -34,9 +34,7 @@ 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"}]}` - r, _ := NewLexical(jsonData).Parse(jsonData) - for _, val := range r { - fmt.Print(val.Val) - } - fmt.Print("\n") + instance := NewLexical(jsonData) + _ = instance.Parse() + fmt.Println(instance.String()) } diff --git a/syntax.go b/syntax.go index c98c5bf..66b4d72 100644 --- a/syntax.go +++ b/syntax.go @@ -6,3 +6,15 @@ // // Date : 2022-07-04 17:53 package filter + +// NewSyntax 构建JSON语法树 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:33 2022/7/5 +func NewSyntax(lexical []*lexicalNode) *syntax { + return &syntax{} +} + +type syntax struct { +}