diff --git a/lexical.go b/lexical.go index 81790dc..32624e6 100644 --- a/lexical.go +++ b/lexical.go @@ -8,6 +8,7 @@ package filter import ( + "encoding/json" "fmt" "github.com/pkg/errors" @@ -18,13 +19,37 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 18:11 2022/7/4 -func parseLexical(jsonData string) ([]*lexicalNode, error) { +func parseLexical(jsonData string) ([]lexicalNode, error) { if len(jsonData) < 2 { return nil, errors.New("input data is not json") } + lexicalList := make([]lexicalNode, 0) + tmpStr := "" for _, itemChar := range jsonData { - fmt.Println("============ : ", string(itemChar)) + currentChar := string(itemChar) + preChar := "-1" + if len(lexicalList) > 0 { + preChar = lexicalList[len(lexicalList)-1].Val + } + if inputCharIsToken(currentChar, preChar) { + // 是关键词 + if len(tmpStr) > 0 { + lexicalList = append(lexicalList, lexicalNode{ + Val: tmpStr, + IsToken: false, + }) + } + lexicalList = append(lexicalList, lexicalNode{ + Val: currentChar, + IsToken: true, + }) + } else { + // 不是关键词, 继续向后走 + tmpStr = tmpStr + currentChar + } } + byteData, _ := json.Marshal(lexicalList) + fmt.Println("============ : ", string(byteData)) return nil, nil } @@ -33,13 +58,17 @@ func parseLexical(jsonData string) ([]*lexicalNode, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 18:15 2022/7/4 -func inputCharIsToken(inputChar string) bool { +func inputCharIsToken(inputChar string, preChar string) bool { + if preChar == escapeCharacterToken { + // 前一个是转义符, 当前不是关键字 + return false + } tokenCharList := []string{ listLeftToken, listRightToken, objectLeftToken, objectRightToken, - keyRightToken, + keyLeftRightToken, kvPairSplitToken, escapeCharacterToken, } diff --git a/token.go b/token.go index 154b807..d1dc91f 100644 --- a/token.go +++ b/token.go @@ -17,12 +17,9 @@ const ( // 对象结束 objectRightToken = "}" // key 值的起始 - keyLeftToken = "\"" - // key 值的结束 - keyRightToken = "\"" + keyLeftRightToken = "\"" // kvPairSplit kv 的分隔符 kvPairSplitToken = ":" // 转义符 escapeCharacterToken = "\\" ) -