Class: Herb::AST::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

Parameters:



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

#errorsArray[Herb::Errors::Error] (readonly)

: Array

Returns:



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

def errors
  @errors
end

#locationLocation (readonly)

: Location

Returns:



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

def location
  @location
end

#sourceString?

: String?

Returns:

  • (String, nil)


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

def source
  @source
end

#typeString (readonly)

: String

Returns:

  • (String)


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

def type
  @type
end

Instance Method Details

#accept(_visitor) ⇒ void

This method returns an undefined value.

: (Visitor) -> void

Parameters:



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

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

#child_nodesArray[Herb::AST::Node?] Also known as: deconstruct

: () -> Array

Returns:



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

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

#class_nameString

: () -> String

Returns:

  • (String)


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

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

#compact_child_nodesArray[Herb::AST::Node]

: () -> Array

Returns:



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

Parameters:

  • array (Object)
  • item_name: (Object) (defaults to: "item")
  • prefix: (Object) (defaults to: " ")
  • indent: (Object) (defaults to: 0)
  • depth: (Object) (defaults to: 0)
  • depth_limit: (Object) (defaults to: 25)

Returns:

  • (Object)


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: " ") ⇒ String

: (?prefix: String) -> String

Parameters:

  • prefix: (String) (defaults to: " ")

Returns:

  • (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_nameString

: () -> String

Returns:

  • (String)


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

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

#recursive_errorsArray[Herb::Errors::Error]

: () -> Array

Returns:



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

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

#to_hashserialized_node

: () -> serialized_node

Returns:

  • (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) ⇒ String

: (?untyped) -> String

Parameters:

  • (Object)

Returns:

  • (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) ⇒ String

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

Parameters:

  • indent: (Integer) (defaults to: 0)
  • depth: (Integer) (defaults to: 0)
  • depth_limit: (Integer) (defaults to: 25)

Returns:

  • (String)


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

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