基础版词法分析

This commit is contained in:
白茶清欢 2022-07-04 18:35:32 +08:00
parent f08f75266e
commit b3740ed1d4
2 changed files with 34 additions and 8 deletions

View File

@ -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,
}

View File

@ -17,12 +17,9 @@ const (
// 对象结束
objectRightToken = "}"
// key 值的起始
keyLeftToken = "\""
// key 值的结束
keyRightToken = "\""
keyLeftRightToken = "\""
// kvPairSplit kv 的分隔符
kvPairSplitToken = ":"
// 转义符
escapeCharacterToken = "\\"
)