Class: PryCoolline::ParenMatch::AST::Root

Inherits:
Struct
  • Object
show all
Defined in:
lib/pry-coolline/paren_match.rb

Overview

The root of the AST tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ Root

Returns a new instance of Root.

Parameters:

  • parser (Parser)

    Parser to get the tokens from



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pry-coolline/paren_match.rb', line 74

def initialize(parser)
  self.elements = []

  while tok = parser.next_token
    case tok
    when OpenToken  then elements << Node.new(parser, tok)
    when StrToken   then elements << Leaf.new(tok)
    when CloseToken then elements << DanglingClose.new(tok)
    end
  end
end

Instance Attribute Details

#elementsArray<Leaf, DanglingClose, Node>

All the top-level nodes of the tree.

Returns:



72
73
74
# File 'lib/pry-coolline/paren_match.rb', line 72

def elements
  @elements
end

Instance Method Details

#pair_at(pos) ⇒ Pair

Finds the opening and closing tokens that should be matched at a certain position in the string.

It is assumed you can be looking for the closing parenthesis when on the opening one, or for the opening one when selecting the character that immediately follows it.

Parameters:

  • pos (Integer)

Returns:

  • (Pair)

    An (open, close) pair. Notice both the opening and closing tokens coud be nil.



97
98
99
100
101
102
103
104
105
# File 'lib/pry-coolline/paren_match.rb', line 97

def pair_at(pos)
  elements.each do |el|
    if pair = el.pair_at(pos)
      return pair
    end
  end

  Pair.new
end