增减 "与:之间无效空格过滤

This commit is contained in:
2022-07-04 20:59:57 +08:00
parent 89f595c48a
commit d933ff5f03
4 changed files with 48 additions and 8 deletions

View File

@ -15,12 +15,31 @@ import (
"github.com/pkg/errors"
)
// parseLexical 解析词法
// NewLexical 获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:52 2022/7/4
func NewLexical(jsonData string) *lexical {
return &lexical{jsonData: jsonData}
}
// lexical 词法解析
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:42 2022/7/4
type lexical struct {
jsonData string
keyLeftRightTokenCnt int
}
// Parse 解析词法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:11 2022/7/4
func parseLexical(jsonData string) ([]lexicalNode, error) {
func (l *lexical) Parse(jsonData string) ([]lexicalNode, error) {
jsonData = strings.ReplaceAll(strings.ReplaceAll(jsonData, "\n", ""), "\t", "")
// mt.Println(jsonData)
if len(jsonData) < 2 {
@ -33,12 +52,20 @@ func parseLexical(jsonData string) ([]lexicalNode, error) {
preChar := "-1"
if len(lexicalList) > 0 {
preChar = lexicalList[len(lexicalList)-1].Val
if preChar == objectLeftToken && currentChar == " " {
if len(tmpStr) == 0 && preChar == objectLeftToken && currentChar == " " {
// 无意义的空格
continue
}
if len(tmpStr) == 0 && currentChar == " " && preChar == keyLeftRightToken && l.keyLeftRightTokenCnt%2 == 0 {
// " : 之间的空格无意义
continue
}
}
if inputCharIsToken(currentChar, preChar) {
if l.inputCharIsToken(currentChar, preChar) {
if currentChar == keyLeftRightToken {
// 双引号计数
l.keyLeftRightTokenCnt++
}
// 是关键词
if len(tmpStr) > 0 {
lexicalList = append(lexicalList, lexicalNode{
@ -65,7 +92,7 @@ func parseLexical(jsonData string) ([]lexicalNode, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:15 2022/7/4
func inputCharIsToken(inputChar string, preChar string) bool {
func (l *lexical) inputCharIsToken(inputChar string, preChar string) bool {
if preChar == escapeCharacterToken {
// 前一个是转义符, 当前不是关键字
return false