Class: Foxtail::Syntax::Parser::AST::BaseNode

Inherits:
Object
  • Object
show all
Defined in:
lib/foxtail/syntax/parser/ast/base_node.rb

Overview

Base class for all AST nodes in the Fluent parser Provides common functionality for type management and node equality

Direct Known Subclasses

Span, SyntaxNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseNode

Returns a new instance of BaseNode.



17
# File 'lib/foxtail/syntax/parser/ast/base_node.rb', line 17

def initialize = @type = self.class.name.split("::").last

Instance Attribute Details

#typeObject

Returns the value of attribute type.



15
16
17
# File 'lib/foxtail/syntax/parser/ast/base_node.rb', line 15

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object

Compare nodes for equality, ignoring span information



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/foxtail/syntax/parser/ast/base_node.rb', line 20

def ==(other)
  return false unless other.is_a?(BaseNode)

  this_keys = instance_variables.to_set {|v| v.to_s.delete("@") }
  other_keys = other.instance_variables.to_set {|v| v.to_s.delete("@") }

  # Always ignore span information in equality comparison
  this_keys.delete("span")
  other_keys.delete("span")

  return false if this_keys.size != other_keys.size

  this_keys.each do |field_name|
    return false unless other_keys.include?(field_name)

    this_val = instance_variable_get("@#{field_name}")
    other_val = other.instance_variable_get("@#{field_name}")

    return false unless this_val.class == other_val.class

    if this_val.is_a?(Array) && other_val.is_a?(Array)
      return false if this_val.length != other_val.length

      this_val.each_with_index do |item, i|
        return false unless item == other_val[i]
      end
    elsif this_val != other_val
      return false
    end
  end

  true
end

#accept(visitor) ⇒ Object

Accept a visitor and dispatch to the appropriate visit method



55
56
57
58
# File 'lib/foxtail/syntax/parser/ast/base_node.rb', line 55

def accept(visitor)
  method_name = "visit_#{INFLECTOR.underscore(self.class.name.split("::").last)}"
  visitor.public_send(method_name, self)
end

#childrenObject

Returns an array of child nodes for traversal Subclasses with children should override this method



62
# File 'lib/foxtail/syntax/parser/ast/base_node.rb', line 62

def children = []

#to_hObject

Convert node to hash representation for JSON serialization



65
66
67
68
69
70
71
72
73
# File 'lib/foxtail/syntax/parser/ast/base_node.rb', line 65

def to_h
  result = {}
  instance_variables.each do |var|
    key = var.to_s.delete("@")
    value = instance_variable_get(var)
    result[key] = serialize_value(value)
  end
  result
end