Class: Patm::Pattern
- Inherits:
-
Object
show all
- Defined in:
- lib/patm.rb
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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/patm.rb', line 11
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
71
72
73
|
# File 'lib/patm.rb', line 71
def &(rhs)
And.new([self, Pattern.build_from(rhs)])
end
|
#[](name) ⇒ Object
75
76
77
|
# File 'lib/patm.rb', line 75
def [](name)
self & Named.new(name)
end
|
#compile ⇒ Object
79
80
81
82
83
|
# File 'lib/patm.rb', line 79
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)
87
88
89
90
91
92
93
|
# File 'lib/patm.rb', line 87
def compile_internal(free_index, target_name = "_obj")
[
"_ctx[#{free_index}].execute(_match, #{target_name})",
[self],
free_index + 1
]
end
|
#execute(match, obj) ⇒ Object
61
|
# File 'lib/patm.rb', line 61
def execute(match, obj); true; end
|
#opt ⇒ Object
57
58
59
|
# File 'lib/patm.rb', line 57
def opt
Opt.new(self)
end
|
#opt? ⇒ Boolean
63
64
65
|
# File 'lib/patm.rb', line 63
def opt?
false
end
|
#rest? ⇒ Boolean
67
68
69
|
# File 'lib/patm.rb', line 67
def rest?
false
end
|