Commits

Pasberth Usbean  committed b927756

result hook

  • Participants
  • Parent commits a7923b1

Comments (0)

Files changed (4)

File lib/regparsec/parser/combinators.rb

 module RegParsec::Regparsers
 
-  def try *regparsers, &result_proc
-    ::RegParsec::Regparsers::TryParser.new.curry!(*regparsers, &result_proc)
-  end
-  
-  def apply *regparsers, &result_proc
-    ::RegParsec::Regparsers::ApplyParser.new.curry!(*regparsers, &result_proc)
-  end
-
-  def many *regparsers, &result_proc
-    ::RegParsec::Regparsers::ManyParser.new.curry!(*regparsers, &result_proc)
-  end
-  
-  def many1 *regparsers, &result_proc
-    ::RegParsec::Regparsers::Many1Parser.new.curry!(*regparsers, &result_proc)
-  end
-  
-  def between *regparsers, &result_proc
-    ::RegParsec::Regparsers::BetweenParser.new.curry!(*regparsers, &result_proc)
-  end
-  
-  def one_of *regparsers, &result_proc
-    ::RegParsec::Regparsers::OneOfParser.new.curry!(*regparsers, &result_proc)
+  [ [:try, :TryParser],
+    [:apply, :ApplyParser],
+    [:many, :ManyParser],
+    [:many1, :Many1Parser],
+    [:between, :BetweenParser],
+    [:one_of, :OneOfParser]
+  ].each do |method, klass|
+    module_eval(<<-DEF)
+      def #{method}(*args, &result_hook)
+        regparser = ::RegParsec::Regparsers::#{klass}.new.curry!(*args)
+        regparser.result_hook(&result_hook) if result_hook
+        regparser
+      end
+    DEF
   end
 end
 

File lib/regparsec/regparseable.rb

   end
 
   def regparse state
-    __regparse__ ::RegParsec::RegparserHelpers.build_state_attributes(state), *format_args(*curried_args)
+    result = __regparse__ ::RegParsec::RegparserHelpers.build_state_attributes(state), *format_args(*curried_args)
+    case result
+    when ::RegParsec::Result::Success
+      result.return_value = result_hooks.inject(result.return_value) { |r, hook| hook.call(state, r) }
+    end
+    result
   end
   
   def parse state
   def __parse__ state, *args
     case result = regparse(state)
     when ::RegParsec::Result::Success
-      if result_procs.empty?
-        result.return_value
-      else
-        result_procs[0].call result.return_value
-      end
+      result.return_value
+      #result_hooks.inject(result.return_value) { |r, hook| hook.call(state, r) }
     else
       nil
     end
     @_curried_args ||= []
   end
   
-  def result_procs
-    @_result_proc ||= []
+  def result_hook &hook
+    result_hooks << hook || raise(ArgumentError, "tried to put a result hook without a block.")
+  end
+
+  def result_hooks
+    @_result_hooks ||= []
   end
   
   def curry *args, &block
     clone.curry! *args, &block
   end
   
-  def curry! *args, &result_proc
+  def curry! *args, &block
     args.each &:push.to(curried_args)
-    result_procs << result_proc if result_procs.empty? and result_proc
+    # TODO: How will using to the block
+    # result_procs << result_proc if result_procs.empty? and result_proc
     self
   end
   

File spec/result_hook_spec.rb

+require 'spec_helper'
+
+describe "result hook" do
+  subject { ::RegParsec::Regparsers.apply("abc", "def") { |state, list| list.join ', ' } }
+
+  example { subject.parse("abc").should == nil }
+  example { subject.regparse("abc").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", ""], :matching_string => "abc" ) }
+  example { subject.parse("a").should == nil }
+  example { subject.regparse("a").should == ::RegParsec::Result::Accepted.new( :return_value => ["a"], :matching_string => "a" ) }
+  example { subject.parse("abcd").should == nil }
+  example { subject.regparse("abcd").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", "d"], :matching_string => "abcd" ) }
+  example { subject.regparse("abcdef").should == ::RegParsec::Result::Success.new( :return_value => "abc, def", :matching_string => "abcdef" ) }
+  example { subject.parse("abcdef").should == "abc, def" }
+  example { subject.regparse("def").should == ::RegParsec::Result::Invalid.new }
+  example { subject.parse("def").should == nil }
+end

File spec/result_proc_spec.rb

-require 'spec_helper'
-
-describe "result proc" do
-  subject { ::RegParsec::Regparsers::ApplyParser.new.curry!("abc", "def") { |list| list.join ', ' } }
-
-  example { subject.parse("abc").should == nil }
-  example { subject.regparse("abc").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", ""], :matching_string => "abc" ) }
-  example { subject.parse("a").should == nil }
-  example { subject.regparse("a").should == ::RegParsec::Result::Accepted.new( :return_value => ["a"], :matching_string => "a" ) }
-  example { subject.parse("abcd").should == nil }
-  example { subject.regparse("abcd").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", "d"], :matching_string => "abcd" ) }
-  example { subject.regparse("abcdef").should == ::RegParsec::Result::Success.new( :return_value => ["abc", "def"], :matching_string => "abcdef" ) }
-  example { subject.parse("abcdef").should == "abc, def" }
-  example { subject.regparse("def").should == ::RegParsec::Result::Invalid.new }
-  example { subject.parse("def").should == nil }
-end