class Opal::Rewriters::PatternMatching::PatternConverter

Public Class Methods

new(pat) click to toggle source
# File lib/opal/rewriters/pattern_matching.rb, line 118
def initialize(pat)
  @pat = pat
  @variables = []
end

Public Instance Methods

on_array(node)
Alias for: on_literal
on_array_pattern(node, tail = false) click to toggle source
0, 1, 2

or [*, 0, 1] or [0, 1, *]

# File lib/opal/rewriters/pattern_matching.rb, line 197
def on_array_pattern(node, tail = false)
  children = *node
  children << s(:match_rest) if tail

  fixed_size = true
  array_size = 0

  children = children.each do |i|
    case i.type
    when :match_rest
      fixed_size = false
    else
      array_size += 1
    end
  end

  array(
    s(:sym, :array),
    to_ast(fixed_size),
    to_ast(array_size),
    to_ast(children.map(&method(:process)))
  )
end
on_array_pattern_with_tail(node) click to toggle source
0, 1, 2,
# File lib/opal/rewriters/pattern_matching.rb, line 222
def on_array_pattern_with_tail(node)
  on_array_pattern(node, true)
end
on_begin(node)
Alias for: on_literal
on_complex(node)
Alias for: on_literal
on_const(node)
Alias for: on_literal
on_const_pattern(node) click to toggle source

MyStructName

# File lib/opal/rewriters/pattern_matching.rb, line 192
def on_const_pattern(node)
  array(s(:sym, :all), *node.children.map(&method(:process)))
end
on_dstr(node)
Alias for: on_literal
on_erange(node)
Alias for: on_literal
on_find_pattern(node) click to toggle source
*, a, b, *
# File lib/opal/rewriters/pattern_matching.rb, line 256
def on_find_pattern(node)
  children = *node

  children = children.map(&method(:process))

  array(s(:sym, :find), array(*children))
end
on_float(node)
Alias for: on_literal
on_hash_pattern(node) click to toggle source

{a:, b:}

# File lib/opal/rewriters/pattern_matching.rb, line 227
def on_hash_pattern(node)
  children = *node

  any_size = children.empty? ? to_ast(false) : to_ast(true)

  children = children.map do |i|
    case i.type
    when :pair
      array(i.children[0], process(i.children[1]))
    when :match_var
      array(s(:sym, i.children[0]), process(i))
    when :match_nil_pattern
      any_size = to_ast(false)
      nil
    when :match_rest
      # Capturing rest?
      if i.children.first
        any_size = process(i.children.first)
      else
        any_size = to_ast(true)
      end
      nil
    end
  end.compact

  array(s(:sym, :hash), any_size, array(*children))
end
on_int(node)
Alias for: on_literal
on_irange(node)
Alias for: on_literal
on_lambda(node)
Alias for: on_literal
on_literal(node) click to toggle source
# File lib/opal/rewriters/pattern_matching.rb, line 152
def on_literal(node)
  array(s(:sym, :lit), node)
end
on_match_alt(node) click to toggle source

{} | []

# File lib/opal/rewriters/pattern_matching.rb, line 187
def on_match_alt(node)
  array(s(:sym, :any), *node.children.map(&method(:process)))
end
on_match_as(node) click to toggle source

> a

# File lib/opal/rewriters/pattern_matching.rb, line 145
def on_match_as(node)
  pat, save = *node

  process(save)
  array(s(:sym, :save), process(pat))
end
on_match_rest(node) click to toggle source

*

# File lib/opal/rewriters/pattern_matching.rb, line 178
def on_match_rest(node)
  if node.children.empty?
    array(s(:sym, :rest))
  else
    array(s(:sym, :rest), process(node.children.first))
  end
end
on_match_var(node) click to toggle source

a

# File lib/opal/rewriters/pattern_matching.rb, line 136
def on_match_var(node)
  var, = *node

  @variables << var

  s(:sym, :var)
end
on_pin(node) click to toggle source

^a

# File lib/opal/rewriters/pattern_matching.rb, line 173
def on_pin(node)
  on_literal(node.children.first)
end
on_rational(node)
Alias for: on_literal
on_regexp(node)
Alias for: on_literal
on_str(node)
Alias for: on_literal
on_sym(node)
Alias for: on_literal
on_xstr(node)
Alias for: on_literal
pattern() click to toggle source
# File lib/opal/rewriters/pattern_matching.rb, line 127
def pattern
  @outpat
end
run!() click to toggle source
# File lib/opal/rewriters/pattern_matching.rb, line 123
def run!
  @outpat = process(@pat)
end
variables() click to toggle source
# File lib/opal/rewriters/pattern_matching.rb, line 131
def variables
  @variables.map { |i| s(:lvasgn, i) }
end

Private Instance Methods

array(*args) click to toggle source
# File lib/opal/rewriters/pattern_matching.rb, line 266
def array(*args)
  to_ast(args)
end
to_ast(val) click to toggle source
# File lib/opal/rewriters/pattern_matching.rb, line 270
def to_ast(val)
  case val
  when Array
    s(:array, *val)
  when Integer
    s(:int, val)
  when true
    s(:true)
  when false
    s(:false)
  when nil
    s(:nil)
  end
end