Class: Brandish::Parser::Node::Root

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

Overview

The root node. This is the parent of the full document.

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(children:, location: nil) ⇒ Root

Initialize the root node with the given children and location.

Parameters:

  • children (<Node>)

    The children of the root node. See #children.

  • location (Location) (defaults to: nil)

    The location of the node. If no location is provided, it assumed from the children.



21
22
23
24
25
26
# File 'lib/brandish/parser/node/root.rb', line 21

def initialize(children:, location: nil)
  @children = children.freeze
  @location = location || derive_location(children)
  fail unless children.any?
  freeze
end

Instance Attribute Details

#children<Node> (readonly)

The children of the root node. This should be a series of nodes that are in the body.

Returns:



13
14
15
# File 'lib/brandish/parser/node/root.rb', line 13

def children
  @children
end

Instance Method Details

#==(other) ⇒ Boolean

Checks for equivalence between this and another object. If the other object is this object, it returns true; otherwise, if the other object is a root, and its properties are equal to this one, it returns true; otherwise, it returns false.

Parameters:

  • other (::Object)

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/brandish/parser/node/root.rb', line 43

def ==(other)
  equal?(other) || other.is_a?(Root) && @children == other.children &&
    @location == other.location
end

#flatten::String

This flattens out the root node into a single string value. This is used for outputing the contents.

Returns:

  • (::String)

Raises:

  • (NodeError)

    if the node contains a non-root or non-text node.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/brandish/parser/node/root.rb', line 53

def flatten
  children.map do |child|
    case child
    when Node::Root then child.flatten
    when Node::Text then child.value
    else
      fail NodeError.new("Unexpected node `#{child.class}",
        node.location)
    end
  end.join
end

#inspect::String

Pretty inspect.

Returns:

  • (::String)


31
32
33
34
# File 'lib/brandish/parser/node/root.rb', line 31

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