Class: Brandish::Parser::Node::Text

Inherits:
Brandish::Parser::Node show all
Defined in:
lib/brandish/parser/node/text.rb

Overview

A text node. This contains only text, and has no other node decendants. This node automatically merges the contents of the text into a single value, and places it in the value attribute.

Direct Known Subclasses

String

Constant Summary collapse

TOKENS =

A set of tokens kinds that are allowed to be in a text node.

Returns:

  • (::Set<::Symbol>)
::Set[:SPACE, :TEXT, :LINE, :NUMERIC, :ESCAPE, :/, :'"', :'='].freeze

Instance Attribute Summary collapse

Attributes inherited from Brandish::Parser::Node

#location

Instance Method Summary collapse

Methods inherited from Brandish::Parser::Node

#prevent_update, #update, #update_prevented?

Constructor Details

#initialize(tokens: , location: nil) ⇒ Text #initialize(value: , location: ) ⇒ Text

Initialize the node.

Overloads:

  • #initialize(tokens: , location: nil) ⇒ Text

    Initialize the text node with the given series of tokens. The location can be either provided, or assumed from the locations of the tokens. The tokens are concatenated into a single string, and used to provide the text.

    Parameters:

    • tokens (<Scanner::Token>) (defaults to: )

      The tokens that make up this text node.

    • location (Location) (defaults to: nil)

      The location of the text node. If none is provided, it is assumed from the tokens.

  • #initialize(value: , location: ) ⇒ Text

    Initialize the text node with the given value. The location must be provided.

    Parameters:

    • value (::String) (defaults to: )

      The value of the text node.

    • location (Location) (defaults to: )

      The location of the text node.



43
44
45
46
47
48
49
50
51
52
# File 'lib/brandish/parser/node/text.rb', line 43

def initialize(tokens: nil, value: nil, location: nil)
  unless tokens || (value && location)
    fail ::ArgumentError, "Expected either a set of tokens or a " \
      "value and a location, got neither"
  end

  @value = value || derive_value(tokens)
  @location = location || derive_location(tokens)
  freeze
end

Instance Attribute Details

#value::String (readonly)

The value of the text node. This is the string value of the source text that this node is based off of.

Returns:

  • (::String)


23
24
25
# File 'lib/brandish/parser/node/text.rb', line 23

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Determines if this object equals another object. If the other object is this object, it returns true; otherwise, if the other object is a Brandish::Parser::Node::Text, and all of the properties are equal, it returns true; otherwise, it returns false.

Parameters:

  • other (::Object)

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/brandish/parser/node/text.rb', line 69

def ==(other)
  equal?(other) || other.is_a?(self.class) && @value == other.value &&
    @location == other.location
end

#inspect::String

Pretty inspect.

Returns:

  • (::String)


57
58
59
60
# File 'lib/brandish/parser/node/text.rb', line 57

def inspect
  "#<#{self.class} value=#{@value.inspect} " \
    "location=#{@location.inspect}>"
end