Class: Herb::AST::LiteralNode

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

Overview

: type serialized_literal_node = { | content: 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, #self?.bold, #self?.bright_magenta, #self?.cyan, #self?.dimmed, #self?.enabled?, #self?.fg, #self?.fg_bg, #self?.green, #self?.magenta, #self?.red, #self?.white, #self?.yellow, white, yellow

Methods inherited from Node

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

Constructor Details

#initialize(type, location, errors, content) ⇒ LiteralNode

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

Parameters:



115
116
117
118
# File 'lib/herb/ast/nodes.rb', line 115

def initialize(type, location, errors, content)
  super(type, location, errors)
  @content = content&.force_encoding("utf-8")
end

Instance Attribute Details

#contentString? (readonly)

: String?

Returns:

  • (String, nil)


112
113
114
# File 'lib/herb/ast/nodes.rb', line 112

def content
  @content
end

Instance Method Details

#accept(visitor) ⇒ void

This method returns an undefined value.

: (Visitor) -> void

Parameters:



128
129
130
# File 'lib/herb/ast/nodes.rb', line 128

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

#child_nodesArray[Herb::AST::Node?]

: () -> Array

Returns:



133
134
135
# File 'lib/herb/ast/nodes.rb', line 133

def child_nodes
  []
end

#compact_child_nodesArray[Herb::AST::Node]

: () -> Array

Returns:



138
139
140
# File 'lib/herb/ast/nodes.rb', line 138

def compact_child_nodes
  child_nodes.compact
end

#inspectString

: () -> String

Returns:

  • (String)


143
144
145
# File 'lib/herb/ast/nodes.rb', line 143

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

#to_hashserialized_literal_node

: () -> serialized_literal_node

Returns:

  • (serialized_literal_node)


121
122
123
124
125
# File 'lib/herb/ast/nodes.rb', line 121

def to_hash
  super.merge({
    content: content,
  }) #: Herb::serialized_literal_node
end

#tree_inspect(indent: 0, depth: 0, depth_limit: 10) ⇒ 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: 10)

Returns:

  • (String)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/herb/ast/nodes.rb', line 148

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

  output += white("└── content: ") + green("#{content.inspect}\n")
  output += "\n"

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