mirror of
				https://github.com/taigrr/yq
				synced 2025-01-18 04:53:17 -08:00 
			
		
		
		
	tree wip
This commit is contained in:
		
							parent
							
								
									c321600afa
								
							
						
					
					
						commit
						ae59ad57f4
					
				| @ -23,9 +23,8 @@ func initTokens() { | ||||
| 	} | ||||
| 	Tokens = []string{ | ||||
| 		"OPERATION",   // ==, OR, AND | ||||
| 		"PATH",        // a.b.c | ||||
| 		"PATH_KEY",    // apples | ||||
| 		"ARRAY_INDEX", // 1234 | ||||
| 		"PATH_JOIN",   // "." | ||||
| 	} | ||||
| 	Tokens = append(Tokens, Literals...) | ||||
| 	TokenIds = make(map[string]int) | ||||
| @ -79,8 +78,8 @@ func initLexer() (*lex.Lexer, error) { | ||||
| 	lexer.Add([]byte(`\[-?[0-9]+\]`), numberToken("ARRAY_INDEX", true)) | ||||
| 	lexer.Add([]byte(`-?[0-9]+`), numberToken("ARRAY_INDEX", false)) | ||||
| 	lexer.Add([]byte("( |\t|\n|\r)+"), skip) | ||||
| 	lexer.Add([]byte(`"[^ "]+"`), wrappedToken("PATH")) | ||||
| 	lexer.Add([]byte(`[^ \.\[\(\)=]+`), token("PATH")) | ||||
| 	lexer.Add([]byte(`"[^ "]+"`), wrappedToken("PATH_KEY")) | ||||
| 	lexer.Add([]byte(`[^ \.\[\(\)=]+`), token("PATH_KEY")) | ||||
| 	lexer.Add([]byte(`\.`), skip) | ||||
| 	err := lexer.Compile() | ||||
| 	if err != nil { | ||||
|  | ||||
							
								
								
									
										51
									
								
								pkg/yqlib/path_tree.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								pkg/yqlib/path_tree.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,51 @@ | ||||
| package yqlib | ||||
| 
 | ||||
| import lex "github.com/timtadh/lexmachine" | ||||
| 
 | ||||
| type PathElementType uint32 | ||||
| 
 | ||||
| const ( | ||||
| 	PathKey PathElementType = 1 << iota | ||||
| 	ArrayIndex | ||||
| 	Operation | ||||
| ) | ||||
| 
 | ||||
| type OperationType uint32 | ||||
| 
 | ||||
| const ( | ||||
| 	None OperationType = 1 << iota | ||||
| 	Or | ||||
| 	And | ||||
| 	ChildEquals | ||||
| ) | ||||
| 
 | ||||
| type PathElement struct { | ||||
| 	PathElementType PathElementType | ||||
| 	OperationType   OperationType | ||||
| 	Value           interface{} | ||||
| 	ChildElements   [][]*PathElement | ||||
| } | ||||
| 
 | ||||
| func parseTree(tokens []*lex.Token, currentElement *PathElement, allElements []*PathElement) []*PathElement { | ||||
| 	currentToken, remainingTokens := tokens[0], tokens[1:] | ||||
| 
 | ||||
| 	switch currentToken.Type { | ||||
| 	case TokenIds["PATH_KEY"]: | ||||
| 		currentElement.PathElementType = PathKey | ||||
| 		currentElement.OperationType = None | ||||
| 		currentElement.Value = currentToken.Value | ||||
| 	} | ||||
| 
 | ||||
| 	if len(remainingTokens) == 0 { | ||||
| 		return append(allElements, currentElement) | ||||
| 	} | ||||
| 	return parseTree(remainingTokens, &PathElement{}, append(allElements, currentElement)) | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| func ParseTree(tokens []*lex.Token) []*PathElement { | ||||
| 	if len(tokens) == 0 { | ||||
| 		return make([]*PathElement, 0) | ||||
| 	} | ||||
| 	return parseTree(tokens, &PathElement{}, make([]*PathElement, 0)) | ||||
| } | ||||
							
								
								
									
										1
									
								
								pkg/yqlib/path_tree_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								pkg/yqlib/path_tree_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| package yqlib | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user