Class: Opal::Nodes::Match3Node

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/nodes/call_special.rb

Overview

/regexp/ =~ rhs s(:match_with_lvasgn, lhs, rhs)

Instance Attribute Summary

Attributes inherited from Base

#compiler, #sexp, #type

Attributes included from Closure::NodeSupport

#closure

Instance Method Summary collapse

Methods inherited from Base

#add_gvar, #add_ivar, #add_local, #add_temp, #children, children, #class_variable_owner, #class_variable_owner_nesting_level, #comments, #compile_to_fragments, #error, #expr, #expr?, #expr_or_empty, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Closure::NodeSupport

#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower

Methods included from Helpers

#current_indent, #empty_line, #indent, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?

Constructor Details

This class inherits a constructor from Opal::Nodes::Base

Instance Method Details

#compileObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'opal/lib/opal/nodes/call_special.rb', line 70

def compile
  sexp = s(:send, lhs, :=~, rhs)
  # Handle named matches like: /(?<abc>b)/ =~ 'b'
  if lhs.type == :regexp && lhs.children.first.type == :str
    re = lhs.children.first.children.first
    names = re.scan(/\(\?<([^>]*)>/).flatten.map(&:to_sym)
    unless names.empty?
      # $m3names = $~ ? $~.named_captures : {}
      names_def = s(:lvasgn, :$m3names,
        s(:if,
          s(:gvar, :$~),
          s(:send, s(:gvar, :$~), :named_captures),
          s(:hash)
        )
      )

      names = names.map do |name|
        # abc = $m3names[:abc]
        s(:lvasgn, name,
          s(:send,
            s(:lvar, :$m3names),
            :[],
            s(:sym, name)
          )
        )
      end

      if stmt?
        # We don't care about a return value of this one, so we
        # ignore it and just assign the local variables.
        #
        # (/(?<abc>b)/ =~ 'f')
        # $m3names = $~ ? $~.named_captures : {}
        # abc = $m3names[:abc]
        sexp = s(:begin, sexp, names_def, *names)
      else
        # We actually do care about a return value, so we must
        # keep it saved.
        #
        # $m3tmp = (/(?<abc>b)/ =~ 'f')
        # $m3names = $~ ? $~.named_captures : {}
        # abc = $m3names[:abc]
        # $m3tmp
        sexp = s(:begin,
          s(:lvasgn, :$m3tmp, sexp),
          names_def,
          *names,
          s(:lvar, :$m3tmp)
        )
      end
    end
  end
  push process(sexp, @level)
end