From f23ece2f6f48b32d902616298c1eae22a65b9cd6 Mon Sep 17 00:00:00 2001 From: Elijah Miller Date: Wed, 25 Mar 2026 12:48:44 -0400 Subject: [PATCH] Initial stab at walk_nodes simplification. --- feel/lib/feel/literal_expression.rb | 46 ++--------------------------- feel/lib/feel/nodes.rb | 11 +++++++ 2 files changed, 13 insertions(+), 44 deletions(-) diff --git a/feel/lib/feel/literal_expression.rb b/feel/lib/feel/literal_expression.rb index a2430f6..6fa8624 100644 --- a/feel/lib/feel/literal_expression.rb +++ b/feel/lib/feel/literal_expression.rb @@ -34,54 +34,12 @@ def functions def named_functions return [] if text.blank? - - # Initialize a set to hold the qualified names - function_names = Set.new - - # Define a lambda for the recursive function - walk_tree = lambda do |node| - # If the node is a qualified name, add it to the set - if node.is_a?(FEEL::FunctionInvocation) - function_names << node.fn_name.text_value - end - - # Recursively walk the child nodes - node.elements&.each do |child| - walk_tree.call(child) - end - end - - # Start walking the tree from the root - walk_tree.call(tree) - - # Return the array of functions - function_names.to_a + FEEL::Node.walk_nodes(tree).select { |node| node.is_a?(FEEL::FunctionInvocation) }.map { |node| node.fn_name.text_value }.uniq end def named_variables return [] if text.blank? - - # Initialize a set to hold the qualified names - qualified_names = Set.new - - # Define a lambda for the recursive function - walk_tree = lambda do |node| - # If the node is a qualified name, add it to the set - if node.is_a?(FEEL::QualifiedName) - qualified_names << node.text_value - end - - # Recursively walk the child nodes - node.elements&.each do |child| - walk_tree.call(child) - end - end - - # Start walking the tree from the root - walk_tree.call(tree) - - # Return the array of qualified names - qualified_names.to_a + FEEL::Node.walk_nodes(tree).select { |node| node.is_a?(FEEL::QualifiedName) }.map(&:text_value).uniq end def self.builtin_functions diff --git a/feel/lib/feel/nodes.rb b/feel/lib/feel/nodes.rb index 8d5c70c..c6bc5f8 100644 --- a/feel/lib/feel/nodes.rb +++ b/feel/lib/feel/nodes.rb @@ -2,6 +2,17 @@ module FEEL class Node < Treetop::Runtime::SyntaxNode + def self.walk_nodes(node, &block) + return enum_for(:walk_nodes, node) unless block_given? + + yield node + node.elements&.each { |child| walk_nodes(child, &block) } + end + + def walk_nodes(&block) + self.class.walk_nodes(self, &block) + end + # # Takes a context hash and returns an array of qualified names # { "person": { "name": { "first": "Eric", "last": "Carlson" }, "age": 60 } } => ["person", "person.name.first", "person.name.last", "person.age"]