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

Inherits:
Object
  • Object
show all
Extended by:
SimpleForwardable
Defined in:
lib/rubocop/ast/node_pattern/compiler.rb,
lib/rubocop/ast/node_pattern/compiler/debug.rb,
lib/rubocop/ast/node_pattern/compiler/binding.rb,
lib/rubocop/ast/node_pattern/compiler/subcompiler.rb,
lib/rubocop/ast/node_pattern/compiler/atom_subcompiler.rb,
lib/rubocop/ast/node_pattern/compiler/sequence_subcompiler.rb,
lib/rubocop/ast/node_pattern/compiler/node_pattern_subcompiler.rb

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

Direct Known Subclasses

Debug

Defined Under Namespace

Classes: AtomSubcompiler, Binding, Debug, NodePatternSubcompiler, SequenceSubcompiler, Subcompiler

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SimpleForwardable

def_delegators

Constructor Details

#initializeCompiler

Returns a new instance of Compiler.



15
16
17
18
19
20
21
22
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 15

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

#bindingObject (readonly)

Returns the value of attribute binding.



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

def binding
  @binding
end

#capturesObject (readonly)

Returns the value of attribute captures.



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

def captures
  @captures
end

#named_parametersObject (readonly)

Returns the value of attribute named_parameters.



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

def named_parameters
  @named_parameters
end

#positional_parametersObject (readonly)

Returns the value of attribute positional_parameters.



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

def positional_parameters
  @positional_parameters
end

Instance Method Details

#compile_as_atom(node) ⇒ Object



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

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

#compile_as_node_pattern(node, **options) ⇒ Object



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

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

#compile_sequence(sequence, var:) ⇒ Object



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

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

#each_union(enum, &block) ⇒ Object

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



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

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

#freezeObject



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

def freeze
  @named_parameters.freeze
  super
end

#named_parameter(name) ⇒ Object



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

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

#next_captureObject



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

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

#parserObject



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

def parser
  @parser ||= Parser.new
end

#positional_parameter(number) ⇒ Object



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

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

#with_temp_variables(*names, &block) ⇒ Object

Utilities



60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/ast/node_pattern/compiler.rb', line 60

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