修复关键字判断BUG
This commit is contained in:
parent
7e48e92648
commit
43f4d51146
54
lexical.go
54
lexical.go
@ -8,8 +8,6 @@
|
||||
package filter
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/util"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@ -70,14 +68,15 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) {
|
||||
tmpStr = ""
|
||||
} else {
|
||||
// 不是关键词, 继续向后走
|
||||
if currentChar == " " {
|
||||
// 当前字符是空格, 只有在字符串内方才有效
|
||||
if l.keyLeftRightTokenCnt%2 == 0 {
|
||||
// 关键字之间的空格, 忽略即可
|
||||
continue
|
||||
}
|
||||
if currentChar == " " && l.keyLeftRightTokenCnt%2 == 0 {
|
||||
// 当前字符是空格, 只有在 "" 之间方才有效 , 关键字之间的空格, 忽略即可
|
||||
continue
|
||||
}
|
||||
tmpStr = tmpStr + currentChar
|
||||
if (tmpStr == "\n" || tmpStr == "\t") && l.keyLeftRightTokenCnt%2 == 0 {
|
||||
// 不在 "" 之间的 \n \t 无意义, 过滤掉
|
||||
tmpStr = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(tmpStr) > 0 {
|
||||
@ -87,28 +86,7 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) {
|
||||
})
|
||||
tmpStr = ""
|
||||
}
|
||||
// 格式化, 去掉 \n \t 等换行符
|
||||
format := make([]*lexicalNode, 0)
|
||||
for idx, val := range l.lexicalResult {
|
||||
formatVal := strings.ReplaceAll(strings.ReplaceAll(val.Val, "\n", ""), "\t", "")
|
||||
// 说明是 \n\t 组成的
|
||||
if len(formatVal) == 0 {
|
||||
if idx == 0 {
|
||||
continue
|
||||
}
|
||||
if idx == len(l.lexicalResult)-1 {
|
||||
break
|
||||
}
|
||||
if (!l.lexicalResult[idx-1].IsToken || l.lexicalResult[idx-1].Val != keyLeftRightToken) ||
|
||||
(!l.lexicalResult[idx+1].IsToken || l.lexicalResult[idx+1].Val != keyLeftRightToken) {
|
||||
// 不是 "" 之间的 \n \t 没有实际意义
|
||||
continue
|
||||
}
|
||||
}
|
||||
format = append(format, val)
|
||||
}
|
||||
l.lexicalResult = format
|
||||
// util.JSON.ConsoleOutput(l.lexicalResult)
|
||||
|
||||
return l.lexicalResult, nil
|
||||
}
|
||||
|
||||
@ -137,7 +115,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool {
|
||||
break
|
||||
}
|
||||
// 0 个 或者 偶数个转义符, " 是关键字
|
||||
if escapeCharacterTokenCnt%2 == 0 && inputChar == keyLeftRightToken {
|
||||
if inputChar == keyLeftRightToken && escapeCharacterTokenCnt%2 == 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@ -159,15 +137,15 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool {
|
||||
// [[],[]]
|
||||
// [1,2,3]
|
||||
// [true,false,true]
|
||||
if inputChar == commaToken && len(tmpStr) == 0 &&
|
||||
// 对应 {"name":"zhangsan", "age":"18"}
|
||||
(nil != preNode && preNode.Val == keyLeftRightToken) ||
|
||||
if inputChar == commaToken && len(tmpStr) == 0 && (
|
||||
// 对应 {"name":"zhangsan", "age":"18"}
|
||||
(nil != preNode && preNode.Val == keyLeftRightToken) ||
|
||||
// 对应[{"name":"zhangsan", "age":"18"}, {"name":"zhangsan", "age":"18"}]
|
||||
(nil != preNode && preNode.Val == objectRightToken) ||
|
||||
// 对应[[],[]]
|
||||
(nil != preNode && preNode.Val == listRightToken) ||
|
||||
// 对应 [true,false,true] / [1,2,3] / [1,true,2,false]
|
||||
(nil != preNode && (preNode.Val == listLeftToken || preNode.Val == commaToken) && (tmpStrType == "number" || tmpStr == "bool")) { // 对应
|
||||
(nil != preNode && (preNode.Val == listLeftToken || preNode.Val == commaToken) && (tmpStrType == "number" || tmpStr == "bool"))) { // 对应
|
||||
return true
|
||||
}
|
||||
|
||||
@ -212,7 +190,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool {
|
||||
// []
|
||||
// [[],[]]
|
||||
// "a": []
|
||||
if inputChar == listLeftToken && (nil == preNode || // 对应 []
|
||||
if inputChar == listLeftToken && len(tmpStr) == 0 && (nil == preNode || // 对应 []
|
||||
(nil != preNode && preNode.Val == listLeftToken) || // 对应 [[],[]]
|
||||
(nil != preNode && preNode.Val == colonToken)) { // 对应 "a": []
|
||||
// [ 是关键字
|
||||
@ -226,7 +204,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool {
|
||||
// [1,2,3]
|
||||
// [true, false]
|
||||
// ["", "" ]
|
||||
if inputChar == listLeftToken && (
|
||||
if inputChar == listRightToken && len(tmpStr) == 0 && (
|
||||
//对应 []
|
||||
(nil != preNode && preNode.Val == listLeftToken) ||
|
||||
// 对应 [[],[]]
|
||||
@ -237,7 +215,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string) bool {
|
||||
(nil != preNode && (tmpStrType == "number" || tmpStrType == "bool")) ||
|
||||
// 对应 ["", "" ]
|
||||
(nil != preNode && preNode.Val == keyLeftRightToken)) {
|
||||
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user