class SyntaxTree::Parser::PinVisitor

Ugh… I really do not like this class. Basically, ripper doesn’t provide enough information about where pins are located in the tree. It only gives events for ^ ops and var_ref nodes. You have to piece it together yourself.

Note that there are edge cases here that we straight up do not address, because I honestly think it’s going to be faster to write a new parser than to address them. For example, this will not work properly:

foo in ^((bar = 0; bar; baz))

If someone actually does something like that, we’ll have to find another way to make this work.

Attributes

pins[R]
stack[R]

Public Class Methods

new(pins) click to toggle source
# File lib/syntax_tree/parser.rb, line 659
def initialize(pins)
  @pins = pins
  @stack = []
end
visit(node, tokens) click to toggle source
# File lib/syntax_tree/parser.rb, line 677
def self.visit(node, tokens)
  start_char = node.start_char
  allocated = []

  tokens.reverse_each do |token|
    char = token.location.start_char
    break if char <= start_char

    if token.is_a?(Op) && token.value == "^"
      allocated.unshift(tokens.delete(token))
    end
  end

  new(allocated).visit(node) if allocated.any?
end

Public Instance Methods

visit(node) click to toggle source
Calls superclass method
# File lib/syntax_tree/parser.rb, line 664
def visit(node)
  return if pins.empty?
  stack << node
  super
  stack.pop
end
visit_var_ref(node) click to toggle source
# File lib/syntax_tree/parser.rb, line 672
def visit_var_ref(node)
  node.pin(stack[-2], pins.shift)
end