Class: Herb::AST::DocumentNode
- 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
-
#children ⇒ Object
readonly
: Array.
-
#prism_context ⇒ Object
readonly
: nil.
-
#prism_node ⇒ Object
readonly
: String?.
Attributes inherited from Node
#errors, #location, #source, #type
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
: (Visitor) -> void.
-
#child_nodes ⇒ Object
: () -> Array.
-
#compact_child_nodes ⇒ Object
: () -> Array.
-
#deserialized_prism_node ⇒ Object
: () -> Prism::node?.
- #initialize(type, location, errors, children, prism_context, prism_node) ⇒ DocumentNode constructor
-
#inspect ⇒ Object
: () -> String.
-
#to_hash ⇒ Object
: () -> serialized_document_node.
-
#tree_inspect(indent: 0, depth: 0, depth_limit: 10) ⇒ Object
: (?indent: Integer, ?depth: Integer, ?depth_limit: Integer) -> String.
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
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
#children ⇒ Object (readonly)
: Array
19 20 21 |
# File 'lib/herb/ast/nodes.rb', line 19 def children @children end |
#prism_context ⇒ Object (readonly)
: nil
20 21 22 |
# File 'lib/herb/ast/nodes.rb', line 20 def prism_context @prism_context end |
#prism_node ⇒ Object (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_nodes ⇒ Object
: () -> Array
62 63 64 |
# File 'lib/herb/ast/nodes.rb', line 62 def child_nodes [*(children || [])] end |
#compact_child_nodes ⇒ Object
: () -> Array
67 68 69 |
# File 'lib/herb/ast/nodes.rb', line 67 def compact_child_nodes child_nodes.compact end |
#deserialized_prism_node ⇒ Object
: () -> 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 |
#inspect ⇒ Object
: () -> String
72 73 74 |
# File 'lib/herb/ast/nodes.rb', line 72 def inspect tree_inspect.rstrip.gsub(/\s+$/, "") end |
#to_hash ⇒ Object
: () -> 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 |