Class: Patm::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/patm.rb

Direct Known Subclasses

Any, Arr, ArrRest, Compiled, Hash, LogicalOp, Named, Obj, Opt

Defined Under Namespace

Modules: Util Classes: And, Any, Arr, ArrRest, Compiled, Hash, LogicalOp, Named, Obj, Opt, Or

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_from(plain) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/patm.rb', line 4

def self.build_from(plain)
  case plain
  when Pattern
    plain
  when ::Array
    array = plain.map{|a| build_from(a)}
    rest_index = array.index(&:rest?)
    if rest_index
      head = array[0...rest_index]
      rest = array[rest_index]
      tail = array[(rest_index+1)..-1]
      Arr.new(head, rest, tail)
    else
      Arr.new(array)
    end
  when ::Hash
    Hash.new(
      plain.each_with_object({}) do|(k, v), h|
        h[k] = build_from(v) if k != Patm.exact
      end,
      plain[Patm.exact]
    )
  else
    Obj.new(plain)
  end
end

Instance Method Details

#&(rhs) ⇒ Object



64
65
66
# File 'lib/patm.rb', line 64

def &(rhs)
  And.new([self, rhs])
end

#[](name) ⇒ Object



68
69
70
# File 'lib/patm.rb', line 68

def [](name)
  self & Named.new(name)
end

#compileObject



72
73
74
75
76
# File 'lib/patm.rb', line 72

def compile
  src, context, _ = self.compile_internal(0)

  Compiled.new(self.inspect, src || "true", context)
end

#compile_internal(free_index, target_name = "_obj") ⇒ Object

free_index:Numeric -> target_name:String -> [src:String|nil, context:Array, free_index:Numeric] variables: _ctx, _match, (target_name)



80
81
82
83
84
85
86
# File 'lib/patm.rb', line 80

def compile_internal(free_index, target_name = "_obj")
  [
    "_ctx[#{free_index}].execute(_match, #{target_name})",
    [self],
    free_index + 1
  ]
end

#execute(match, obj) ⇒ Object



54
# File 'lib/patm.rb', line 54

def execute(match, obj); true; end

#optObject

Use in Hash pattern.



50
51
52
# File 'lib/patm.rb', line 50

def opt
  Opt.new(self)
end

#opt?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/patm.rb', line 56

def opt?
  false
end

#rest?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/patm.rb', line 60

def rest?
  false
end