词法分析记录真实值 && 修复 , 关键词解析错误

This commit is contained in:
白茶清欢 2022-07-05 14:25:02 +08:00
parent db0f851aff
commit 6a3548766d
2 changed files with 11 additions and 8 deletions

View File

@ -49,7 +49,7 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) {
tmpStr := ""
for _, itemChar := range jsonData {
currentChar := string(itemChar)
tmpStrType := l.getTmpStrType(tmpStr)
tmpRealVal, tmpStrType := l.getTmpStrType(tmpStr)
if l.inputCharIsToken(currentChar, tmpStr, tmpStrType) {
if currentChar == keyLeftRightToken {
// 双引号计数
@ -58,13 +58,15 @@ func (l *lexical) Parse(jsonData string) ([]*lexicalNode, error) {
// 是关键词
if len(tmpStr) > 0 {
l.lexicalResult = append(l.lexicalResult, &lexicalNode{
Val: tmpStr,
Val: tmpRealVal,
IsToken: false,
Type: tmpStrType,
})
}
l.lexicalResult = append(l.lexicalResult, &lexicalNode{
Val: currentChar,
IsToken: true,
Type: "string",
})
tmpStr = ""
} else {
@ -161,7 +163,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string, tmpStrType s
// [[],[]]
// [1,2,3]
// [true,false,true]
if inputChar == commaToken && len(tmpStr) == 0 && (
if inputChar == commaToken && (
// 对应 {"name":"zhangsan", "age":"18"}
(nil != preNode && preNode.ValStr() == keyLeftRightToken) ||
// 对应[{"name":"zhangsan", "age":"18"}, {"name":"zhangsan", "age":"18"}]
@ -169,7 +171,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string, tmpStrType s
// 对应[[],[]]
(nil != preNode && preNode.ValStr() == listRightToken) ||
// 对应 [true,false,true] / [1,2,3] / [1,true,2,false]
(nil != preNode && (preNode.ValStr() == listLeftToken || preNode.ValStr() == commaToken) && (tmpStrType == "number" || tmpStr == "bool"))) { // 对应
(nil != preNode && (preNode.ValStr() == colonToken || preNode.ValStr() == listLeftToken || preNode.ValStr() == commaToken) && (tmpStrType == "number" || tmpStrType == "bool"))) { // 对应
return true
}
@ -244,7 +246,7 @@ func (l *lexical) inputCharIsToken(inputChar string, tmpStr string, tmpStrType s
return false
}
func (l *lexical) getTmpStrType(tmpStr string) string {
func (l *lexical) getTmpStrType(tmpStr string) (interface{}, string) {
var preNode *lexicalNode
if len(l.lexicalResult) > 0 {
preNode = l.lexicalResult[len(l.lexicalResult)-1]
@ -255,7 +257,7 @@ func (l *lexical) getTmpStrType(tmpStr string) string {
// 判断是否可转数字
var floatVal float64
if err := util.ConvertAssign(&floatVal, tmpStr); nil == err {
return "number"
return floatVal, "number"
}
}
@ -265,8 +267,8 @@ func (l *lexical) getTmpStrType(tmpStr string) string {
// 判断是否可转数字
var boolVal bool
if err := util.ConvertAssign(&boolVal, tmpStr); nil == err {
return "bool"
return boolVal, "bool"
}
}
return "string"
return tmpStr, "string"
}

View File

@ -14,6 +14,7 @@ import (
func Test_parseLexical(t *testing.T) {
jsonData := `{
"start" : 123456,
"name" : "zhangsan",
"age":"18",
"extension":{