Class: RSpock::AST::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/rspock/ast/block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, node) ⇒ Block

Constructs a new Block.

Parameters:

  • type (Symbol)

    The Block type.

  • node (Parser::AST::Node)

    The node associated to this 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

#nodeObject (readonly)

Returns the value of attribute node.



18
19
20
# File 'lib/rspock/ast/block.rb', line 18

def node
  @node
end

#typeObject (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.

Parameters:

  • child_node (Parser::AST::Node)

    The node to be added.

Raises:

  • (BlockError)

    if this Block cannot contain other nodes.



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

#childrenArray<Parser::AST::Node>

Retrieves the duped array of children AST nodes for this Block.

Returns:

  • (Array<Parser::AST::Node>)

    The children nodes.



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.

Parameters:

  • value (Boolean)

    True if this Block can contain other nodes, false otherwise.



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.

Returns:

  • (Boolean)

    True if this Block can contain other nodes, false otherwise.



45
46
47
# File 'lib/rspock/ast/block.rb', line 45

def node_container?
  @node_container
end

#rangeParser::Source::Range

Retrieves the Parser::Source::Range for this Block.

Returns:

  • (Parser::Source::Range)

    The range.



59
60
61
# File 'lib/rspock/ast/block.rb', line 59

def range
  node&.loc&.expression || "?"
end

#succession_error_msgString

Retrieves the error message for succession errors.

Returns:

  • (String)

    The error message.



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

#successorsArray<Symbol>

Retrieves the valid successors for this Block. Note: Defaults to [:End].

Returns:

  • (Array<Symbol>)

    This Block’s successors.



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.

Parameters:

  • child_node (Parser::AST::Node)

    The node to be added.

Raises:

  • (BlockError)

    if this Block cannot contain other nodes.



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.

Parameters:

  • block (Block)

    The candidate successor.

Returns:

  • (Boolean)

    True if the given block is a valid successor, false otherwise.



83
84
85
# File 'lib/rspock/ast/block.rb', line 83

def valid_successor?(block)
  successors.include?(block.type)
end