Class: Brandish::Parser::Node::Block

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

Overview

A "block" node. Basically, this is a node that encapsulates a block of text. This typically has a 1-to-1 relationship with the body of text, while a command may not.

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(name:, body:, arguments: nil, pairs: nil, location: nil) ⇒ Block

Returns a new instance of Block.

Parameters:

  • location (Location) (defaults to: nil)

    The location of the block.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/brandish/parser/node/block.rb', line 33

def initialize(name:, body:, arguments: nil, pairs: nil, location: nil)
  if !location && !arguments
    fail ArgumentError, "Expected either a location or " \
      "arguments, got neither"
  end

  @name = name.is_a?(Yoga::Token) ? name.value : name
  @body = body
  @location = location || arguments.map(&:location)
                                   .inject(name.location.union(body.location),
                                     :union)
  @pairs = pairs.freeze || derive_pairs(arguments).freeze
end

Instance Attribute Details

#bodyNode::Root (readonly)

The body of the block.

Returns:



19
20
21
# File 'lib/brandish/parser/node/block.rb', line 19

def body
  @body
end

#name::String (readonly)

The name of the block.

Returns:

  • (::String)


14
15
16
# File 'lib/brandish/parser/node/block.rb', line 14

def name
  @name
end

#pairs{::String => ::String} (readonly)

The "pairs" of arguments to be passed to the command.

Returns:

  • ({::String => ::String})


24
25
26
# File 'lib/brandish/parser/node/block.rb', line 24

def pairs
  @pairs
end

Instance Method Details

#==(other) ⇒ Boolean

Equates this object with another object. If the other object is this object, it returns true; otherwise, if it is a block, whose properties all equal this one, it returns true; otherwise, it returns false.

Parameters:

  • other (::Object)

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/brandish/parser/node/block.rb', line 62

def ==(other)
  equal?(other) || other.is_a?(Block) && @name == other.name &&
    @body == other.body && @location == other.location
end

#inspect::String

Pretty inspect.

Returns:

  • (::String)


50
51
52
53
# File 'lib/brandish/parser/node/block.rb', line 50

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