ParseExpression parses a string expression into a ConditionNode tree Supports && (AND), || (OR), ! (NOT), and parentheses for grouping Example: "condition1 && (condition2 || !condition3)"
(expression string)
| 52 | // Supports && (AND), || (OR), ! (NOT), and parentheses for grouping |
| 53 | // Example: "condition1 && (condition2 || !condition3)" |
| 54 | func ParseExpression(expression string) (ConditionNode, error) { |
| 55 | expressionsLog.Printf("Parsing expression: %s", expression) |
| 56 | |
| 57 | if strings.TrimSpace(expression) == "" { |
| 58 | return nil, errors.New("empty expression") |
| 59 | } |
| 60 | |
| 61 | parser := &ExpressionParser{} |
| 62 | tokens, err := parser.tokenize(expression) |
| 63 | if err != nil { |
| 64 | expressionsLog.Printf("Failed to tokenize expression: %v", err) |
| 65 | return nil, err |
| 66 | } |
| 67 | parser.tokens = tokens |
| 68 | parser.pos = 0 |
| 69 | |
| 70 | result, err := parser.parseOrExpression() |
| 71 | if err != nil { |
| 72 | expressionsLog.Printf("Failed to parse expression: %v", err) |
| 73 | return nil, err |
| 74 | } |
| 75 | |
| 76 | // Check that all tokens were consumed |
| 77 | if parser.current().kind != tokenEOF { |
| 78 | return nil, fmt.Errorf("unexpected token '%s' at position %d", parser.current().value, parser.current().pos) |
| 79 | } |
| 80 | |
| 81 | expressionsLog.Printf("Successfully parsed expression with %d tokens", len(tokens)) |
| 82 | return result, nil |
| 83 | } |
| 84 | |
| 85 | // tokenize breaks the expression string into tokens |
| 86 | func (p *ExpressionParser) tokenize(expression string) ([]token, error) { |