MCPcopy Create free account
hub / github.com/github/gh-aw / ParseExpression

Function ParseExpression

pkg/workflow/expression_parser.go:54–83  ·  view source on GitHub ↗

ParseExpression parses a string expression into a ConditionNode tree Supports && (AND), || (OR), ! (NOT), and parentheses for grouping Example: "condition1 && (condition2 || !condition3)"

(expression string)

Source from the content-addressed store, hash-verified

52// Supports && (AND), || (OR), ! (NOT), and parentheses for grouping
53// Example: "condition1 && (condition2 || !condition3)"
54func 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
86func (p *ExpressionParser) tokenize(expression string) ([]token, error) {

Calls 5

tokenizeMethod · 0.95
parseOrExpressionMethod · 0.95
currentMethod · 0.95
PrintfMethod · 0.45
ErrorfMethod · 0.45