词法分析树增加String()方法
This commit is contained in:
parent
6a3548766d
commit
81085d9477
23
lexical.go
23
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
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user