2022-07-04 18:00:22 +08:00
|
|
|
// Package filter ...
|
|
|
|
//
|
|
|
|
// Description : JSON 词法分析
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2022-07-04 17:52
|
|
|
|
package filter
|
2022-07-04 18:08:10 +08:00
|
|
|
|
2022-07-04 18:19:04 +08:00
|
|
|
import (
|
2022-07-04 18:39:29 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.zhangdeman.cn/zhangdeman/util"
|
2022-07-04 18:19:04 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// parseLexical 解析词法
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:11 2022/7/4
|
2022-07-04 18:35:32 +08:00
|
|
|
func parseLexical(jsonData string) ([]lexicalNode, error) {
|
2022-07-04 18:39:29 +08:00
|
|
|
jsonData = strings.ReplaceAll(strings.ReplaceAll(jsonData, "\n", ""), "\t", "")
|
2022-07-04 18:19:04 +08:00
|
|
|
if len(jsonData) < 2 {
|
|
|
|
return nil, errors.New("input data is not json")
|
|
|
|
}
|
2022-07-04 18:35:32 +08:00
|
|
|
lexicalList := make([]lexicalNode, 0)
|
|
|
|
tmpStr := ""
|
2022-07-04 18:19:04 +08:00
|
|
|
for _, itemChar := range jsonData {
|
2022-07-04 18:35:32 +08:00
|
|
|
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
|
|
|
|
}
|
2022-07-04 18:19:04 +08:00
|
|
|
}
|
2022-07-04 18:39:29 +08:00
|
|
|
util.JSON.ConsoleOutput(lexicalList)
|
2022-07-04 18:19:04 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputCharIsToken 输入字符是否为关键字
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:15 2022/7/4
|
2022-07-04 18:35:32 +08:00
|
|
|
func inputCharIsToken(inputChar string, preChar string) bool {
|
|
|
|
if preChar == escapeCharacterToken {
|
|
|
|
// 前一个是转义符, 当前不是关键字
|
|
|
|
return false
|
|
|
|
}
|
2022-07-04 18:19:04 +08:00
|
|
|
tokenCharList := []string{
|
|
|
|
listLeftToken,
|
|
|
|
listRightToken,
|
|
|
|
objectLeftToken,
|
|
|
|
objectRightToken,
|
2022-07-04 18:35:32 +08:00
|
|
|
keyLeftRightToken,
|
2022-07-04 18:19:04 +08:00
|
|
|
kvPairSplitToken,
|
|
|
|
escapeCharacterToken,
|
|
|
|
}
|
|
|
|
for _, itemChar := range tokenCharList {
|
|
|
|
if itemChar == inputChar {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|