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, Struct
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
|
# File 'lib/patm.rb', line 11
def self.build_from(plain)
case plain
when Pattern
plain
when ::Array
build_from_array(plain)
when ::Hash
build_from_hash(plain)
else
Obj.new(plain)
end
end
|
.build_from_array(plain) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/patm.rb', line 24
def self.build_from_array(plain)
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
end
|
.build_from_hash(plain) ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/patm.rb', line 37
def self.build_from_hash(plain)
self::Hash.new(
plain.each_with_object({}) do|(k, v), h|
h[k] = build_from(v) if k != Patm.exact
end,
plain[Patm.exact]
)
end
|
Instance Method Details
#[](name) ⇒ Object
83
84
85
|
# File 'lib/patm.rb', line 83
def [](name)
self & Named.new(name)
end
|
#compile ⇒ Object
87
88
89
90
91
|
# File 'lib/patm.rb', line 87
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)
95
96
97
98
99
100
101
|
# File 'lib/patm.rb', line 95
def compile_internal(free_index, target_name = "_obj")
[
"_ctx[#{free_index}].execute(_match, #{target_name})",
[self],
free_index + 1
]
end
|
#execute(match, obj) ⇒ Object
69
|
# File 'lib/patm.rb', line 69
def execute(match, obj); true; end
|
#opt ⇒ Object
65
66
67
|
# File 'lib/patm.rb', line 65
def opt
Opt.new(self)
end
|
#opt? ⇒ Boolean
71
72
73
|
# File 'lib/patm.rb', line 71
def opt?
false
end
|
#rest? ⇒ Boolean
75
76
77
|
# File 'lib/patm.rb', line 75
def rest?
false
end
|