Class: Wool::SexpAnalysis::Sexp

Inherits:
Array
  • Object
show all
Extended by:
ModuleExtensions
Defined in:
lib/wool/analysis/sexp_analysis.rb

Overview

Replaces the ParseTree Sexps by adding a few handy-dandy methods.

Instance Method Summary collapse

Methods included from ModuleExtensions

attr_accessor_with_default, cattr_accessor, cattr_accessor_with_default, cattr_get_and_setter, cattr_reader, cattr_writer

Constructor Details

#initialize(other) ⇒ Sexp

Initializes the Sexp with the contents of the array returned by Ripper.

Parameters:

  • other (Array<Object>)

    the other



21
22
23
24
25
# File 'lib/wool/analysis/sexp_analysis.rb', line 21

def initialize(other)
  replace other
  replace_children!
  self.class.annotations.each {|annotator| annotator.annotate!(self) }
end

Instance Method Details

#annotationsArray[Class < Annotation]] the activated annotations, in the order they will run in.

Returns a mutable reference to the list of annotations that will run upon initializing a new Sexp.

Returns:

  • (Array[Class < Annotation]] the activated annotations, in the order they will run in.)

    Array[Class < Annotation]] the activated annotations, in the order they will run in.



16
# File 'lib/wool/analysis/sexp_analysis.rb', line 16

cattr_accessor_with_default :annotations, []

#childrenArray<Object>

Returns the children of the node.

Returns:

  • (Array<Object>)

    the children of the node.



28
29
30
# File 'lib/wool/analysis/sexp_analysis.rb', line 28

def children
  (Array === self[0] ? self : self[1..-1]) || []
end

#eval_as_constant(scope) ⇒ Object

Evaluates the constant reference/path with the given scope as context.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wool/analysis/sexp_analysis.rb', line 51

def eval_as_constant(scope)
  case type
  # [:var_ref, [:@const, "B", [1, 17]]]
  when :var_ref then scope.constants[self[1][1]]
  # [:const_ref, [:@const, "M", [1, 17]]]
  when :const_ref then scope.constants[self[1][1]]
  # [:top_const_ref, [:@const, "M", [1, 2]]]
  when :top_const_ref then Scope::GlobalScope.constants[self[1][1]]
  # [:@const, "B", [1, 7]]
  when :@const then scope.constants[self[1]]
  # [:const_path_ref, [:var_ref, [:@const, "B", [1, 17]]], [:@const, "M", [1, 20]]]
  when :const_path_ref
    left, right = children
    scope = left.eval_as_constant(scope).scope
    right.eval_as_constant(scope)
  end
end

#typeSymbol

Returns the type of the node.

Returns:

  • (Symbol)

    the type of the node.



33
34
35
# File 'lib/wool/analysis/sexp_analysis.rb', line 33

def type
  self[0]
end