基础版词法分析
This commit is contained in:
		
							
								
								
									
										37
									
								
								lexical.go
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								lexical.go
									
									
									
									
									
								
							| @ -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, | ||||
| 	} | ||||
|  | ||||
		Reference in New Issue
	
	Block a user