lib/funkr/adt/matcher.rb in funkr-0.0.23 vs lib/funkr/adt/matcher.rb in funkr-0.0.24
- old
+ new
@@ -1,34 +1,45 @@
module Funkr
+
# Should not be used directly
- class Matcher
-
- def initialize(match)
- @const, *@data = match
- @runner = nil
- @undefined = self.class.constructs.clone
- end
-
- def self.build_matchers(constructs)
- @constructs = constructs
- constructs.each do |c|
- name, *data = c
- define_method(name) do |&b|
- @undefined.delete(name)
- if @const == name then
- @runner = b
+ module Matchers
+
+ class SafeMatcher
+
+ def initialize(match)
+ @const, *@data = match
+ @runner = nil
+ @undefined = self.class.constructs.clone
+ end
+
+ def self.build_matchers(constructs)
+ @constructs = constructs
+ constructs.each do |c|
+ name, *data = c
+ define_method(name) do |&b|
+ @undefined.delete(name)
+ if @const == name then
+ @runner = b
+ end
end
end
end
- end
-
- def self.constructs; @constructs; end
-
- def run_match
- if @undefined.any? then
- raise "Incomplete match, missing : #{@undefined.join(" ")}"
+
+ def self.constructs; @constructs; end
+
+ def self.match_with(normal_form) # &block
+ m = self.new(normal_form)
+ yield m
+ m.run_match
end
- @runner.call(*@data)
+
+ def run_match
+ if @undefined.any? then
+ raise "Incomplete match, missing : #{@undefined.join(" ")}"
+ end
+ @runner.call(*@data)
+ end
+
end
-
+
end
end