Class: RSpock::AST::Block
- Inherits:
-
Object
- Object
- RSpock::AST::Block
- Defined in:
- lib/rspock/ast/block.rb
Direct Known Subclasses
CleanupBlock, EndBlock, ExpectBlock, GivenBlock, StartBlock, ThenBlock, WhenBlock, WhereBlock
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#<<(child_node) ⇒ Object
Adds the given
child_node
to this Block. -
#children ⇒ Array<Parser::AST::Node>
Retrieves the duped array of children AST nodes for this Block.
-
#initialize(type, node) ⇒ Block
constructor
Constructs a new Block.
-
#node_container=(value) ⇒ Object
Sets whether this Block can contain other nodes.
-
#node_container? ⇒ Boolean
Checks whether this Block can contain other nodes.
-
#range ⇒ Parser::Source::Range
Retrieves the Parser::Source::Range for this Block.
-
#succession_error_msg ⇒ String
Retrieves the error message for succession errors.
-
#successors ⇒ Array<Symbol>
Retrieves the valid successors for this Block.
-
#unshift(child_node) ⇒ Object
Adds the given
child_node
to the beginning of this Block. -
#valid_successor?(block) ⇒ Boolean
Checks whether or not the given
block
is a valid successor for this Block.
Constructor Details
#initialize(type, node) ⇒ Block
Constructs a new Block.
11 12 13 14 15 16 |
# File 'lib/rspock/ast/block.rb', line 11 def initialize(type, node) @type = type @node = node @children = [] @node_container = true end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
18 19 20 |
# File 'lib/rspock/ast/block.rb', line 18 def node @node end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
18 19 20 |
# File 'lib/rspock/ast/block.rb', line 18 def type @type end |
Instance Method Details
#<<(child_node) ⇒ Object
Adds the given child_node
to this Block.
25 26 27 28 29 |
# File 'lib/rspock/ast/block.rb', line 25 def <<(child_node) raise BlockError, succession_error_msg unless node_container? @children << child_node end |
#children ⇒ Array<Parser::AST::Node>
Retrieves the duped array of children AST nodes for this Block.
74 75 76 |
# File 'lib/rspock/ast/block.rb', line 74 def children @children.dup end |
#node_container=(value) ⇒ Object
Sets whether this Block can contain other nodes.
52 53 54 |
# File 'lib/rspock/ast/block.rb', line 52 def node_container=(value) @node_container = value end |
#node_container? ⇒ Boolean
Checks whether this Block can contain other nodes.
45 46 47 |
# File 'lib/rspock/ast/block.rb', line 45 def node_container? @node_container end |
#range ⇒ Parser::Source::Range
Retrieves the Parser::Source::Range for this Block.
59 60 61 |
# File 'lib/rspock/ast/block.rb', line 59 def range node&.loc&.expression || "?" end |
#succession_error_msg ⇒ String
Retrieves the error message for succession errors.
90 91 92 |
# File 'lib/rspock/ast/block.rb', line 90 def succession_error_msg "Block #{type} @ #{range} must be followed by one of these Blocks: #{successors}" end |
#successors ⇒ Array<Symbol>
Retrieves the valid successors for this Block. Note: Defaults to [:End].
67 68 69 |
# File 'lib/rspock/ast/block.rb', line 67 def successors @successors ||= [:End].freeze end |
#unshift(child_node) ⇒ Object
Adds the given child_node
to the beginning of this Block.
36 37 38 39 40 |
# File 'lib/rspock/ast/block.rb', line 36 def unshift(child_node) raise BlockError, succession_error_msg unless node_container? @children.unshift(child_node) end |
#valid_successor?(block) ⇒ Boolean
Checks whether or not the given block
is a valid successor for this Block.
83 84 85 |
# File 'lib/rspock/ast/block.rb', line 83 def valid_successor?(block) successors.include?(block.type) end |