1. Pasberth Usbean
  2. RegParsec

Commits

Pasberth Usbean  committed 51a006c

between

  • Participants
  • Parent commits b244822
  • Branches master

Comments (0)

Files changed (15)

File lib/regparsec/parser/combinators.rb

View file
     ::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)
   end
   end
 end
 
+class BetweenParser < Base
+  
+  def __regparse__ state, open, close, body
+    case result = apply(open, body, close).regparse(state)
+    when Result::Success
+      Result::Success.new( :return_value => result.return_value[1], :matching_string => result.matching_string )
+    when Result::Accepted
+      Result::Accepted.new( :return_value => result.return_value[1], :matching_string => result.matching_string )
+    else
+      result
+    end
+  end
+end
+
 class OneOfParser < Base
   
   def __regparse__ state, *choices

File lib/regparsec/result.rb

View file
   end
   
   def == other
-    @information_keys.all? do |key|
+    self.class == other.class and @information_keys.all? do |key|
       send(key) == other.send(key)
     end
   rescue NoMethodError

File spec/regparsers/between_parser_spec.rb

View file
+require 'spec_helper'
+
+describe RegParsec::Regparsers::BetweenParser do
+  
+  subject { described_class.new.curry!('"', '"', "abc") }
+
+  example { subject.parse(%q_"abc"_).should == "abc" }
+  example { subject.regparse(%q_"abc"_).should == ::RegParsec::Result::Success.new( :return_value => "abc", :matching_string => %q_"abc"_ ) }
+  example { subject.parse(%q_"abc" "abc"_).should == "abc" }
+  example { subject.regparse(%q_"abc" "abc"_).should == ::RegParsec::Result::Success.new( :return_value => "abc", :matching_string => %q_"abc"_ ) }
+  example { subject.parse(%q_"a"_).should == nil }
+  example { subject.regparse(%q_"a"_).should == ::RegParsec::Result::Invalid.new }
+  example { subject.parse(%q_"abcd"_).should == nil }
+  example { subject.regparse(%q_"abcd"_).should == ::RegParsec::Result::Invalid.new }
+  example { subject.parse("abc").should == nil }
+  example { subject.regparse("abc").should == ::RegParsec::Result::Invalid.new }
+  example { subject.parse(%q_abc"_).should == nil }
+  example { subject.regparse(%q_abc"_).should == ::RegParsec::Result::Invalid.new }
+  example { subject.parse(%q_"abc_).should == nil }
+  example { subject.regparse(%q_"abc_).should == ::RegParsec::Result::Accepted.new( :return_value => "abc", :matching_string => %q_"abc_) }
+end

File spec/regparsers/many1_parser_spec.rb

View file
+require 'spec_helper'
+
+describe RegParsec::Regparsers::Many1Parser do
+  subject { described_class.new.curry!("abc") }
+
+  example { subject.parse("abc").should be_nil }
+  example { subject.regparse("abc").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", ""], :matching_string => "abc" ) }
+  example { subject.parse("a").should be_nil }
+  example { subject.regparse("a").should == ::RegParsec::Result::Accepted.new( :return_value => ["a"], :matching_string => "a" ) }
+  example { subject.parse("abca").should be_nil }
+  example { subject.regparse("abca").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", "a"], :matching_string => "abca" ) }
+  example { subject.parse("d").should be_nil }
+  example { subject.regparse("d").should == ::RegParsec::Result::Invalid.new }
+  example { subject.parse("abcd").should == ["abc"] }
+  example { subject.regparse("abcd").should == ::RegParsec::Result::Success.new( :return_value => ["abc"], :matching_string => "abc" ) }
+end

File spec/regparsers/many1_spec.rb

-require 'spec_helper'
-
-describe RegParsec::Regparsers::Many1Parser do
-  subject { described_class.new.curry!("abc") }
-
-  example { subject.parse("abc").should be_nil }
-  example { subject.regparse("abc").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", ""], :matching_string => "abc" ) }
-  example { subject.parse("a").should be_nil }
-  example { subject.regparse("a").should == ::RegParsec::Result::Accepted.new( :return_value => ["a"], :matching_string => "a" ) }
-  example { subject.parse("abca").should be_nil }
-  example { subject.regparse("abca").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", "a"], :matching_string => "abca" ) }
-  example { subject.parse("d").should be_nil }
-  example { subject.regparse("d").should == ::RegParsec::Result::Invalid.new }
-  example { subject.parse("abcd").should == ["abc"] }
-  example { subject.regparse("abcd").should == ::RegParsec::Result::Success.new( :return_value => ["abc"], :matching_string => "abc" ) }
-end

File spec/regparsers/many_parser_spec.rb

View file
+require 'spec_helper'
+
+describe RegParsec::Regparsers::ManyParser do
+  subject { described_class.new.curry!("abc") }
+
+  example { subject.parse("abc").should be_nil }
+  example { subject.regparse("abc").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", ""], :matching_string => "abc" ) }
+  example { subject.parse("a").should be_nil }
+  example { subject.regparse("a").should == ::RegParsec::Result::Accepted.new( :return_value => ["a"], :matching_string => "a" ) }
+  example { subject.parse("abca").should be_nil }
+  example { subject.regparse("abca").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", "a"], :matching_string => "abca" ) }
+  example { subject.parse("d").should == [] }
+  example { subject.regparse("d").should == ::RegParsec::Result::Success.new( :return_value => [], :matching_string => "" ) }
+  example { subject.parse("abcd").should == ["abc"] }
+  example { subject.regparse("abcd").should == ::RegParsec::Result::Success.new( :return_value => ["abc"], :matching_string => "abc" ) }
+end

File spec/regparsers/many_spec.rb

-require 'spec_helper'
-
-describe RegParsec::Regparsers::ManyParser do
-  subject { described_class.new.curry!("abc") }
-
-  example { subject.parse("abc").should be_nil }
-  example { subject.regparse("abc").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", ""], :matching_string => "abc" ) }
-  example { subject.parse("a").should be_nil }
-  example { subject.regparse("a").should == ::RegParsec::Result::Accepted.new( :return_value => ["a"], :matching_string => "a" ) }
-  example { subject.parse("abca").should be_nil }
-  example { subject.regparse("abca").should == ::RegParsec::Result::Accepted.new( :return_value => ["abc", "a"], :matching_string => "abca" ) }
-  example { subject.parse("d").should == [] }
-  example { subject.regparse("d").should == ::RegParsec::Result::Success.new( :return_value => [], :matching_string => "" ) }
-  example { subject.parse("abcd").should == ["abc"] }
-  example { subject.regparse("abcd").should == ::RegParsec::Result::Success.new( :return_value => ["abc"], :matching_string => "abc" ) }
-end

File spec/regparsers/one_of_parser_spec.rb

View file
+require 'spec_helper'
+
+describe RegParsec::Regparsers::OneOfParser do
+  subject { described_class.new.curry!("abc", "def") }
+
+  example { subject.parse("abc").should == "abc" }
+  example { subject.regparse("abc").should == ::RegParsec::Result::Success.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("def").should == "def" }
+  example { subject.regparse("def").should == ::RegParsec::Result::Success.new( :return_value => "def", :matching_string => "def" ) }
+  example { subject.parse("d").should == nil }
+  example { subject.regparse("d").should == ::RegParsec::Result::Accepted.new( :return_value => "d", :matching_string => "d" ) }
+  example { subject.regparse("abcdef").should == ::RegParsec::Result::Success.new( :return_value => "abc", :matching_string => "abc" ) }
+  example { subject.parse("abcdef").should == "abc" }
+  example { subject.regparse("defabc").should == ::RegParsec::Result::Success.new( :return_value => "def", :matching_string => "def" ) }
+  example { subject.parse("defabc").should == "def" }
+end

File spec/regparsers/one_of_spec.rb

-require 'spec_helper'
-
-describe RegParsec::Regparsers::OneOfParser do
-  subject { described_class.new.curry!("abc", "def") }
-
-  example { subject.parse("abc").should == "abc" }
-  example { subject.regparse("abc").should == ::RegParsec::Result::Success.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("def").should == "def" }
-  example { subject.regparse("def").should == ::RegParsec::Result::Success.new( :return_value => "def", :matching_string => "def" ) }
-  example { subject.parse("d").should == nil }
-  example { subject.regparse("d").should == ::RegParsec::Result::Accepted.new( :return_value => "d", :matching_string => "d" ) }
-  example { subject.regparse("abcdef").should == ::RegParsec::Result::Success.new( :return_value => "abc", :matching_string => "abc" ) }
-  example { subject.parse("abcdef").should == "abc" }
-  example { subject.regparse("defabc").should == ::RegParsec::Result::Success.new( :return_value => "def", :matching_string => "def" ) }
-  example { subject.parse("defabc").should == "def" }
-end

File spec/regparsers/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

File spec/regparsers/state_spec.rb

-require 'spec_helper'
-
-describe ::RegParsec::Regparsers::UpdateStateParser do
-  subject { described_class.new.curry(:x, "abc") }
-  let(:state) { ::RegParsec::StateAttributes.new( :x => '' ) }
-
-  example do
-    state.input = "abc"
-    subject.parse( state )
-    state.x.should == "abc"
-  end
-
-  example do
-    state.input = "def"
-    subject.parse( state )
-    state.x.should == ''
-  end
-end

File spec/regparsers/try_parser_spec.rb

Empty file added.

File spec/regparsers/try_spec.rb

Empty file removed.

File spec/regparsers/update_state_parser_spec.rb

View file
+require 'spec_helper'
+
+describe ::RegParsec::Regparsers::UpdateStateParser do
+  subject { described_class.new.curry(:x, "abc") }
+  let(:state) { ::RegParsec::StateAttributes.new( :x => '' ) }
+
+  example do
+    state.input = "abc"
+    subject.parse( state )
+    state.x.should == "abc"
+  end
+
+  example do
+    state.input = "def"
+    subject.parse( state )
+    state.x.should == ''
+  end
+end

File spec/result_proc_spec.rb

View file
+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