Class: Patm::Pattern
- Inherits:
-
Object
show all
- Defined in:
- lib/patm.rb
Defined Under Namespace
Classes: And, Any, Arr, ArrRest, Compiled, Group, LogicalOp, Obj, 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
|
# 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
else
Obj.new(plain)
end
end
|
Instance Method Details
#&(rhs) ⇒ Object
30
31
32
|
# File 'lib/patm.rb', line 30
def &(rhs)
And.new([self, rhs])
end
|
#compile ⇒ Object
34
35
36
37
38
|
# File 'lib/patm.rb', line 34
def compile
src, context, _ = self.compile_internal(0)
Compiled.new(self.inspect, src, context)
end
|
#compile_internal(free_index, target_name = "_obj") ⇒ Object
free_index:Numeric -> [src, context, free_index] variables: _ctx, _match, _obj
42
43
44
45
46
47
48
|
# File 'lib/patm.rb', line 42
def compile_internal(free_index, target_name = "_obj")
[
"_ctx[#{free_index}].execute(_match, #{target_name})",
[self],
free_index + 1
]
end
|
#execute(match, obj) ⇒ Object
24
|
# File 'lib/patm.rb', line 24
def execute(match, obj); true; end
|
#rest? ⇒ Boolean
26
27
28
|
# File 'lib/patm.rb', line 26
def rest?
false
end
|