Class: Brandish::Parser::Node::Command

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

Overview

A command node. This takes the form of {.a b=c}, and executes a given command with the given options.

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: , arguments: , location: nil) ⇒ Command #initialize(name: , pairs: , location: ) ⇒ Command

Initialize the command node.

Overloads:

  • #initialize(name: , arguments: , location: nil) ⇒ Command

    Creates a command node with the given name. The arguments are processed to turn into a hash that is a key-value store of the arguments; if there are duplicate key-values pairs, the earlier ones are overwritten. If no location is provided, it is assumed from the name and arguments.

    Parameters:

    • name (::String, Scanner::Token) (defaults to: )

      The name of the command. If no location is provided, this must respond to #location.

    • arguments (<Node::Pair>) (defaults to: )

      The pairs of arguments as an array of nodes.

  • #initialize(name: , pairs: , location: ) ⇒ Command

    Creates a command node with the given name and argument pairs. Since no location can be derived from either, the location is required.

    Parameters:

    • name (::String, Scanner::Token) (defaults to: )

      The name of the command.

    • pairs ({::String => ::String, ::Numeric}) (defaults to: )

      The argument pairs for the command.

    • location (Location) (defaults to: )

      The location of the node.



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

def initialize(name:, 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.freeze
  @location = location || arguments.map(&:location)
                                   .inject(name.location, :union)
  @pairs = pairs.freeze || derive_pairs(arguments).freeze
end

Instance Attribute Details

#name::String (readonly)

The name of the command node. This is the name of the command that is executed.

Returns:

  • (::String)


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

def name
  @name
end

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

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

Returns:

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


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

def pairs
  @pairs
end

Instance Method Details

#==(other) ⇒ Boolean

Determines if this object and the given object are equal. If the other object is this object, it returns true; otherwise, if the other object is a Brandish::Parser::Node::Command, and all of the properties of that object equal this one, then it returns true; otherwise, it returns false.

Parameters:

  • other (::Object)

    The object to equal.

Returns:

  • (Boolean)


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

def ==(other)
  equal?(other) || (other.is_a?(Command) && @name == other.name &&
    @location == other.location && @pairs == other.pairs)
end

#inspect::String

Pretty inspect.

Returns:

  • (::String)


70
71
72
73
# File 'lib/brandish/parser/node/command.rb', line 70

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