Class: Herb::AST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/herb/ast/node.rb,
ext/herb/nodes.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, location, errors = []) ⇒ Node

: (String, Location, ?Array) -> void



18
19
20
21
22
23
# File 'lib/herb/ast/node.rb', line 18

def initialize(type, location, errors = [])
  @type = type
  @location = location
  @errors = errors
  @source = nil
end

Instance Attribute Details

#errorsObject (readonly)

: Array



14
15
16
# File 'lib/herb/ast/node.rb', line 14

def errors
  @errors
end

#locationObject (readonly)

: Location



13
14
15
# File 'lib/herb/ast/node.rb', line 13

def location
  @location
end

#sourceObject

: String?



15
16
17
# File 'lib/herb/ast/node.rb', line 15

def source
  @source
end

#typeObject (readonly)

: String



12
13
14
# File 'lib/herb/ast/node.rb', line 12

def type
  @type
end

Instance Method Details

#accept(_visitor) ⇒ Object

: (Visitor) -> void

Raises:

  • (NoMethodError)


102
103
104
# File 'lib/herb/ast/node.rb', line 102

def accept(_visitor)
  raise NoMethodError, "undefined method `accept' for #{inspect}"
end

#child_nodesObject Also known as: deconstruct

: () -> Array

Raises:

  • (NoMethodError)


107
108
109
# File 'lib/herb/ast/node.rb', line 107

def child_nodes
  raise NoMethodError, "undefined method `child_nodes' for #{inspect}"
end

#class_nameObject

: () -> String



41
42
43
# File 'lib/herb/ast/node.rb', line 41

def class_name
  self.class.name || "Node"
end

#compact_child_nodesObject

: () -> Array



114
115
116
# File 'lib/herb/ast/node.rb', line 114

def compact_child_nodes
  child_nodes.compact
end

#inspect_array(array, item_name: "item", prefix: " ", indent: 0, depth: 0, depth_limit: 25) ⇒ Object

: ( | Array, | ?item_name: String, | ?prefix: String, | ?indent: Integer, | ?depth: Integer, | ?depth_limit: Integer | ) -> String



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/herb/ast/node.rb', line 70

def inspect_array(array, item_name: "item", prefix: "    ", indent: 0, depth: 0, depth_limit: 25)
  output = +""

  if array.any?
    output += "(#{array.count} #{array.one? ? item_name : "#{item_name}s"})"
    output += "\n"

    items = array.map { |item|
      kwargs = { indent: indent, depth: depth, depth_limit: depth_limit }

      if array.last == item
        "└── #{item.tree_inspect(**kwargs).gsub(/^/, "    ").lstrip}"
      else
        "├── #{item.tree_inspect(**kwargs).gsub(/^/, "")}".gsub("├── │  ", "├──")
      end
    }

    output += items.join.gsub(/^/, prefix)
  else
    output += "[]"
    output += "\n"
  end

  output
end

#inspect_errors(prefix: " ") ⇒ Object

: (?prefix: String) -> String



56
57
58
59
60
# File 'lib/herb/ast/node.rb', line 56

def inspect_errors(prefix: "    ")
  return "" if errors.empty?

  "├── errors: #{inspect_array(errors, item_name: "error", prefix: prefix)}"
end

#node_nameObject

: () -> String



46
47
48
# File 'lib/herb/ast/node.rb', line 46

def node_name
  class_name.split("::").last || "Node"
end

#recursive_errorsObject

: () -> Array



119
120
121
# File 'lib/herb/ast/node.rb', line 119

def recursive_errors
  errors + compact_child_nodes.flat_map(&:recursive_errors)
end

#to_hashObject

: () -> serialized_node



32
33
34
35
36
37
38
# File 'lib/herb/ast/node.rb', line 32

def to_hash
  {
    type: type,
    location: location.to_hash,
    errors: errors.map(&:to_hash),
  }
end

#to_json(state = nil) ⇒ Object

: (?untyped) -> String



51
52
53
# File 'lib/herb/ast/node.rb', line 51

def to_json(state = nil)
  to_hash.to_json(state)
end

#tree_inspect(indent: 0, depth: 0, depth_limit: 25) ⇒ Object

: (?indent: Integer, ?depth: Integer, ?depth_limit: Integer) -> String

Raises:

  • (NotImplementedError)


97
98
99
# File 'lib/herb/ast/node.rb', line 97

def tree_inspect(indent: 0, depth: 0, depth_limit: 25)
  raise NotImplementedError
end