词法分析树增加String()方法

This commit is contained in:
白茶清欢 2022-07-05 14:42:43 +08:00
parent 6a3548766d
commit 81085d9477
3 changed files with 33 additions and 10 deletions

View File

@ -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
}

View File

@ -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())
}

View File

@ -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 {
}