Class: Opal::Nodes::RegexpNode

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

Constant Summary collapse

SUPPORTED_FLAGS =
/[gimuy]/

Instance Attribute Summary collapse

Attributes inherited from Base

#compiler, #type

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_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_while?, #process, #push, #recv, #recv?, #s, #scope, #stmt, #stmt?, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Helpers

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

Constructor Details

#initializeRegexpNode

Returns a new instance of RegexpNode.



109
110
111
112
# File 'opal/lib/opal/nodes/literal.rb', line 109

def initialize(*)
  super
  extract_flags_and_value
end

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



104
105
106
# File 'opal/lib/opal/nodes/literal.rb', line 104

def flags
  @flags
end

#valueObject

Returns the value of attribute value.



104
105
106
# File 'opal/lib/opal/nodes/literal.rb', line 104

def value
  @value
end

Instance Method Details

#compileObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'opal/lib/opal/nodes/literal.rb', line 114

def compile
  flags.select! do |flag|
    if SUPPORTED_FLAGS =~ flag
      true
    else
      compiler.warning "Skipping the '#{flag}' Regexp flag as it's not widely supported by JavaScript vendors."
      false
    end
  end

  case value.type
  when :dstr, :begin
    compile_dynamic_regexp
  when :str
    compile_static_regexp
  end
end

#compile_dynamic_regexpObject



132
133
134
135
136
137
138
# File 'opal/lib/opal/nodes/literal.rb', line 132

def compile_dynamic_regexp
  if flags.any?
    push 'new RegExp(', expr(value), ", '#{flags.join}')"
  else
    push 'new RegExp(', expr(value), ')'
  end
end

#compile_static_regexpObject



140
141
142
143
144
145
146
147
148
149
150
151
# File 'opal/lib/opal/nodes/literal.rb', line 140

def compile_static_regexp
  value = self.value.children[0]
  case value
  when ''
    push('/(?:)/')
  when %r{\?<\w+\>}
    message = "named captures are not supported in javascript: #{value.inspect}"
    push "self.$raise(new SyntaxError('#{message}'))"
  else
    push "#{Regexp.new(value).inspect}#{flags.join}"
  end
end

#extract_flags_and_valueObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'opal/lib/opal/nodes/literal.rb', line 153

def extract_flags_and_value
  *values, flags_sexp = *children
  self.flags = flags_sexp.children.map(&:to_s)

  self.value = case values.length
               when 0
                 # empty regexp, we can process it inline
                 s(:str, '')
               when 1
                 # simple plain regexp, we can put it inline
                 values[0]
               else
                 s(:dstr, *values)
               end

  # trimming when //x provided
  # required by parser gem, but JS doesn't support 'x' flag
  if flags.include?('x')
    parts = value.children.map do |part|
      if part.is_a?(::Opal::AST::Node) && part.type == :str
        trimmed_value = part.children[0].gsub(/\A\s*\#.*/, '').gsub(/\s/, '')
        s(:str, trimmed_value)
      else
        part
      end
    end

    self.value = value.updated(nil, parts)
    flags.delete('x')
  end

  if value.type == :str
    # Replacing \A -> ^, \z -> $, required for the parser gem
    self.value = s(:str, value.children[0].gsub('\A', '^').gsub('\z', '$'))
  end
end

#raw_valueObject



190
191
192
# File 'opal/lib/opal/nodes/literal.rb', line 190

def raw_value
  self.value = @sexp.loc.expression.source
end