123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::AST::NodePattern::Compiler

Overview

The top-level compiler holding the global state Defers work to its subcompilers

Doc on how this fits in the compiling process: /docs/modules/ROOT/pages/node_pattern.adoc

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newCompiler

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 16

def initialize
  @temp_depth = 0 # avoid name clashes between temp variables
  @captures = 0 # number of captures seen
  @positional_parameters = 0 # highest % (param) number seen
  @named_parameters = Set[] # keyword parameters
  @binding = Binding.new # bound variables
  @atom_subcompiler = self.class::AtomSubcompiler.new(self)
end

Instance Attribute Details

#binding (readonly)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 14

attr_reader :captures, :named_parameters, :positional_parameters, :binding

#captures (readonly)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 14

attr_reader :captures, :named_parameters, :positional_parameters, :binding

#named_parameters (readonly)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 14

attr_reader :captures, :named_parameters, :positional_parameters, :binding

#positional_parameters (readonly)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 14

attr_reader :captures, :named_parameters, :positional_parameters, :binding

Instance Method Details

#compile_as_atom(node)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 43

def compile_as_atom(node)
  @atom_subcompiler.compile(node)
end

#compile_as_node_pattern(node, **options)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 47

def compile_as_node_pattern(node, **options)
  self.class::NodePatternSubcompiler.new(self, **options).compile(node)
end

#compile_sequence(sequence, var:)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 51

def compile_sequence(sequence, var:)
  self.class::SequenceSubcompiler.new(self, sequence: sequence, var: var).compile_sequence
end

#each_union(enum, &block)

Enumerates enum while keeping track of state across union branches (captures and unification).

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 39

def each_union(enum, &block)
  enforce_same_captures(binding.union_bind(enum), &block)
end

#enforce_same_captures(enum) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 82

def enforce_same_captures(enum)
  return to_enum __method__, enum unless block_given?

  captures_before = captures_after = nil
  enum.each do |node|
    captures_before ||= @captures
    @captures = captures_before
    yield node
    captures_after ||= @captures
    if captures_after != @captures
      raise Invalid, 'each branch must have same number of captures'
    end
  end
end

#freeze

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 75

def freeze
  @named_parameters.freeze
  super
end

#named_parameter(name)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 32

def named_parameter(name)
  @named_parameters << name
  name
end

#new_capture (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 97

def new_capture
  @captures
ensure
  @captures += 1
end

#next_capture

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 71

def next_capture
  "captures[#{new_capture}]"
end

#parser

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 55

def parser
  @parser ||= Parser.new
end

#positional_parameter(number)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 27

def positional_parameter(number)
  @positional_parameters = number if number > @positional_parameters
  "param#{number}"
end

#with_temp_variables(*names, &block)

Utilities

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 61

def with_temp_variables(*names, &block)
  @temp_depth += 1
  suffix = @temp_depth if @temp_depth > 1
  names = block.parameters.map(&:last) if names.empty?
  names.map! { |name| "#{name}#{suffix}" }
  yield(*names)
ensure
  @temp_depth -= 1
end