Class: Herb::AST::DocumentNode

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

Overview

: type serialized_document_node = { | children: Array, | prism_context: nil, | prism_node: String?, | }

Constant Summary

Constants included from Colors

Colors::CLEAR_SCREEN, Colors::HIDE_CURSOR, Colors::SHOW_CURSOR

Instance Attribute Summary collapse

Attributes inherited from Node

#errors, #location, #source, #type

Instance Method Summary collapse

Methods included from Colors

bold, bright_magenta, cyan, dimmed, enabled?, fg, fg_bg, green, magenta, red, white, yellow

Methods inherited from Node

#class_name, #inspect_array, #inspect_errors, #node_name, #recursive_errors, #to_json

Constructor Details

#initialize(type, location, errors, children, prism_context, prism_node) ⇒ DocumentNode

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



24
25
26
27
28
29
# File 'lib/herb/ast/nodes.rb', line 24

def initialize(type, location, errors, children, prism_context, prism_node)
  super(type, location, errors)
  @children = children
  @prism_context = prism_context
  @prism_node = prism_node
end

Instance Attribute Details

#childrenObject (readonly)

: Array



19
20
21
# File 'lib/herb/ast/nodes.rb', line 19

def children
  @children
end

#prism_contextObject (readonly)

: nil



20
21
22
# File 'lib/herb/ast/nodes.rb', line 20

def prism_context
  @prism_context
end

#prism_nodeObject (readonly)

: String?



21
22
23
# File 'lib/herb/ast/nodes.rb', line 21

def prism_node
  @prism_node
end

Instance Method Details

#accept(visitor) ⇒ Object

: (Visitor) -> void



57
58
59
# File 'lib/herb/ast/nodes.rb', line 57

def accept(visitor)
  visitor.visit_document_node(self)
end

#child_nodesObject

: () -> Array



62
63
64
# File 'lib/herb/ast/nodes.rb', line 62

def child_nodes
  [*(children || [])]
end

#compact_child_nodesObject

: () -> Array



67
68
69
# File 'lib/herb/ast/nodes.rb', line 67

def compact_child_nodes
  child_nodes.compact
end

#deserialized_prism_nodeObject

: () -> Prism::node?



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/herb/ast/nodes.rb', line 32

def deserialized_prism_node
  prism_node = @prism_node
  return nil unless prism_node
  return nil unless source

  begin
    require "prism"
  rescue LoadError
    warn "The 'prism' gem is required to deserialize Prism nodes. Add it to your Gemfile or install it with: gem install prism"
    return nil
  end

  Prism.load(source, prism_node).value
end

#inspectObject

: () -> String



72
73
74
# File 'lib/herb/ast/nodes.rb', line 72

def inspect
  tree_inspect.rstrip.gsub(/\s+$/, "")
end

#to_hashObject

: () -> serialized_document_node



48
49
50
51
52
53
54
# File 'lib/herb/ast/nodes.rb', line 48

def to_hash
  super.merge({
    children: children,
    prism_context: prism_context,
    prism_node: prism_node,
  }) #: Herb::serialized_document_node
end

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

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/herb/ast/nodes.rb', line 77

def tree_inspect(indent: 0, depth: 0, depth_limit: 10)
  output = +""

  output += white("@ #{bold(yellow(node_name.to_s))} #{dimmed("(location: #{location.tree_inspect})")}")
  output += "\n"

  if depth >= depth_limit
    output += dimmed("└── [depth limit reached ...]\n\n")

    return output.gsub(/^/, "    " * indent)
  end

  output += inspect_errors(prefix: "")

  dynamic_symbol = prism_node ? "├──" : "└──"
  dynamic_prefix = prism_node ? "" : "    "
  output += white("#{dynamic_symbol} children: ")
  output += inspect_array(children, prefix: dynamic_prefix, indent: indent, depth: depth + 1, depth_limit: depth_limit)
  if prism_node && source
    output += white("└── prism_node: ")
    output += Herb::PrismInspect.inspect_prism_serialized(prism_node, source, "    ")
    output += "\n"
  end
  output += "\n"

  output.gsub(/^/, "    " * indent)
end