Class: Opal::AST::Matcher::Node

Inherits:
Struct
  • Object
show all
Defined in:
lib/opal/ast/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children

Returns:

  • (Object)

    the current value of children



33
34
35
# File 'lib/opal/ast/matcher.rb', line 33

def children
  @children
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



33
34
35
# File 'lib/opal/ast/matcher.rb', line 33

def type
  @type
end

Instance Method Details

#inspectObject



67
68
69
70
71
72
73
# File 'lib/opal/ast/matcher.rb', line 67

def inspect
  if type == :capture
    "{#{children.first.inspect}}"
  else
    "s(#{type.inspect}, #{children.inspect[1..-2]})"
  end
end

#match(ast, matcher) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/opal/ast/matcher.rb', line 34

def match(ast, matcher)
  return false if ast.nil?

  ast_parts = [ast.type] + ast.children
  self_parts = [type] + children

  return false if ast_parts.length != self_parts.length

  ast_parts.length.times.all? do |i|
    ast_elem = ast_parts[i]
    self_elem = self_parts[i]

    if self_elem.is_a?(Node) && self_elem.type == :capture
      capture = true
      self_elem = self_elem.children.first
    end

    res = case self_elem
          when Node
            self_elem.match(ast_elem, matcher)
          when Array
            self_elem.include?(ast_elem)
          when :*
            true
          else
            self_elem == ast_elem
          end

    matcher.captures << ast_elem if capture
    res
  end
end