Created
January 26, 2011 05:11
-
-
Save matthewd/796261 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| From a93dc9af9c6ebba6d375aed61ca0ee67f75d14b2 Mon Sep 17 00:00:00 2001 | |
| From: Matthew Draper <[email protected]> | |
| Date: Wed, 26 Jan 2011 15:39:24 +1030 | |
| Subject: [PATCH] Remove Success/Fail structs | |
| --- | |
| lib/parslet/atoms/alternative.rb | 2 +- | |
| lib/parslet/atoms/base.rb | 30 +++++------------------------- | |
| lib/parslet/atoms/lookahead.rb | 2 +- | |
| lib/parslet/atoms/named.rb | 9 +++++---- | |
| lib/parslet/atoms/re.rb | 2 +- | |
| lib/parslet/atoms/repetition.rb | 8 ++++---- | |
| lib/parslet/atoms/sequence.rb | 8 ++++---- | |
| lib/parslet/atoms/str.rb | 2 +- | |
| spec/parslet/atoms_spec.rb | 8 ++++---- | |
| 9 files changed, 26 insertions(+), 45 deletions(-) | |
| diff --git a/lib/parslet/atoms/alternative.rb b/lib/parslet/atoms/alternative.rb | |
| index e81345f..560eb50 100644 | |
| --- a/lib/parslet/atoms/alternative.rb | |
| +++ b/lib/parslet/atoms/alternative.rb | |
| @@ -34,7 +34,7 @@ class Parslet::Atoms::Alternative < Parslet::Atoms::Base | |
| def try(source, context) # :nodoc: | |
| alternatives.each { |a| | |
| value = a.apply(source, context) | |
| - return value unless value.error? | |
| + return value unless value == :fail | |
| } | |
| # If we reach this point, all alternatives have failed. | |
| error(source, @error_msg) | |
| diff --git a/lib/parslet/atoms/base.rb b/lib/parslet/atoms/base.rb | |
| index 10089ac..aa515b8 100644 | |
| --- a/lib/parslet/atoms/base.rb | |
| +++ b/lib/parslet/atoms/base.rb | |
| @@ -4,20 +4,6 @@ | |
| class Parslet::Atoms::Base | |
| include Parslet::Atoms::Precedence | |
| - # Internally, all parsing functions return either an instance of Fail | |
| - # or an instance of Success. | |
| - # | |
| - class Fail < Struct.new(:message) | |
| - def error?; true end | |
| - end | |
| - | |
| - # Internally, all parsing functions return either an instance of Fail | |
| - # or an instance of Success. | |
| - # | |
| - class Success < Struct.new(:result) | |
| - def error?; false end | |
| - end | |
| - | |
| # Given a string or an IO object, this will attempt a parse of its contents | |
| # and return a result. If the parse fails, a Parslet::ParseFailed exception | |
| # will be thrown. | |
| @@ -32,8 +18,8 @@ class Parslet::Atoms::Base | |
| # If we didn't succeed the parse, raise an exception for the user. | |
| # Stack trace will be off, but the error tree should explain the reason | |
| # it failed. | |
| - if value.error? | |
| - raise Parslet::ParseFailed, value.message | |
| + if value == :fail | |
| + raise Parslet::ParseFailed, cause | |
| end | |
| # assert: value is a success answer | |
| @@ -56,7 +42,7 @@ class Parslet::Atoms::Base | |
| end | |
| end | |
| - return flatten(value.result) | |
| + return flatten(value) | |
| end | |
| #--- | |
| @@ -71,7 +57,7 @@ class Parslet::Atoms::Base | |
| } | |
| # This has just succeeded, so last_cause must be empty | |
| - unless result.error? | |
| + unless result == :fail | |
| @last_cause = nil | |
| return result | |
| end | |
| @@ -320,17 +306,11 @@ private | |
| end | |
| end | |
| - # Produces an instance of Success and returns it. | |
| - # | |
| - def success(result) | |
| - Success.new(result) | |
| - end | |
| - | |
| # Produces an instance of Fail and returns it. | |
| # | |
| def error(source, str, pos=nil) | |
| @last_cause = format_cause(source, str, pos) | |
| - Fail.new(@last_cause) | |
| + :fail | |
| end | |
| # Signals to the outside that the parse has failed. Use this in conjunction | |
| diff --git a/lib/parslet/atoms/lookahead.rb b/lib/parslet/atoms/lookahead.rb | |
| index 29a102b..3495721 100644 | |
| --- a/lib/parslet/atoms/lookahead.rb | |
| +++ b/lib/parslet/atoms/lookahead.rb | |
| @@ -24,7 +24,7 @@ class Parslet::Atoms::Lookahead < Parslet::Atoms::Base | |
| pos = source.pos | |
| value = bound_parslet.apply(source, context) | |
| - return success(nil) if positive ^ value.error? | |
| + return nil if positive ^ (value == :fail) | |
| return error(source, @error_msgs[:positive]) if positive | |
| return error(source, @error_msgs[:negative]) | |
| diff --git a/lib/parslet/atoms/named.rb b/lib/parslet/atoms/named.rb | |
| index e647e7b..c6474b1 100644 | |
| --- a/lib/parslet/atoms/named.rb | |
| +++ b/lib/parslet/atoms/named.rb | |
| @@ -16,10 +16,8 @@ class Parslet::Atoms::Named < Parslet::Atoms::Base | |
| def apply(source, context) # :nodoc: | |
| value = parslet.apply(source, context) | |
| - return value if value.error? | |
| - success( | |
| - produce_return_value( | |
| - value.result)) | |
| + return value if value == :fail | |
| + produce_return_value(value) | |
| end | |
| def to_s_inner(prec) # :nodoc: | |
| @@ -29,6 +27,9 @@ class Parslet::Atoms::Named < Parslet::Atoms::Base | |
| def error_tree # :nodoc: | |
| parslet.error_tree | |
| end | |
| + | |
| + def cause; parslet.cause; end | |
| + def cause?; parslet.cause?; end | |
| private | |
| def produce_return_value(val) # :nodoc: | |
| { name => flatten(val, true) } | |
| diff --git a/lib/parslet/atoms/re.rb b/lib/parslet/atoms/re.rb | |
| index ebabec0..a345438 100644 | |
| --- a/lib/parslet/atoms/re.rb | |
| +++ b/lib/parslet/atoms/re.rb | |
| @@ -27,7 +27,7 @@ class Parslet::Atoms::Re < Parslet::Atoms::Base | |
| return error(source, @error_msgs[:premature], error_pos) unless s | |
| return error(source, @error_msgs[:failed], error_pos) unless s.match(re) | |
| - return success(s) | |
| + return s | |
| end | |
| def to_s_inner(prec) # :nodoc: | |
| diff --git a/lib/parslet/atoms/repetition.rb b/lib/parslet/atoms/repetition.rb | |
| index 198897f..abb242b 100644 | |
| --- a/lib/parslet/atoms/repetition.rb | |
| +++ b/lib/parslet/atoms/repetition.rb | |
| @@ -25,20 +25,20 @@ class Parslet::Atoms::Repetition < Parslet::Atoms::Base | |
| start_pos = source.pos | |
| loop do | |
| value = parslet.apply(source, context) | |
| - break if value.error? | |
| + break if value == :fail | |
| occ += 1 | |
| - result << value.result | |
| + result << value | |
| # If we're not greedy (max is defined), check if that has been | |
| # reached. | |
| - return success(result) if max && occ>=max | |
| + return result if max && occ>=max | |
| end | |
| # Greedy matcher has produced a failure. Check if occ (which will | |
| # contain the number of sucesses) is in {min, max}. | |
| return error(source, @error_msgs[:minrep], start_pos) if occ < min | |
| - return success(result) | |
| + return result | |
| end | |
| precedence REPETITION | |
| diff --git a/lib/parslet/atoms/sequence.rb b/lib/parslet/atoms/sequence.rb | |
| index e7868d2..cb0f0a3 100644 | |
| --- a/lib/parslet/atoms/sequence.rb | |
| +++ b/lib/parslet/atoms/sequence.rb | |
| @@ -21,16 +21,16 @@ class Parslet::Atoms::Sequence < Parslet::Atoms::Base | |
| end | |
| def try(source, context) # :nodoc: | |
| - success([:sequence]+parslets.map { |p| | |
| + [:sequence]+parslets.map { |p| | |
| # Save each parslet as potentially offending (raising an error). | |
| @offending_parslet = p | |
| value = p.apply(source, context) | |
| - return error(source, @error_msgs[:failed]) if value.error? | |
| + return error(source, @error_msgs[:failed]) if value == :fail | |
| - value.result | |
| - }) | |
| + value | |
| + } | |
| end | |
| precedence SEQUENCE | |
| diff --git a/lib/parslet/atoms/str.rb b/lib/parslet/atoms/str.rb | |
| index b57da6d..5c629de 100644 | |
| --- a/lib/parslet/atoms/str.rb | |
| +++ b/lib/parslet/atoms/str.rb | |
| @@ -20,7 +20,7 @@ class Parslet::Atoms::Str < Parslet::Atoms::Base | |
| error_pos = source.pos | |
| s = source.read(str.size) | |
| - return success(s) if s == str | |
| + return s if s == str | |
| # assert: s != str | |
| diff --git a/spec/parslet/atoms_spec.rb b/spec/parslet/atoms_spec.rb | |
| index 7fe80da..08598bd 100644 | |
| --- a/spec/parslet/atoms_spec.rb | |
| +++ b/spec/parslet/atoms_spec.rb | |
| @@ -184,7 +184,7 @@ describe Parslet do | |
| end | |
| context "when fed 'foo'" do | |
| it "should parse" do | |
| - parslet.apply(src('foo'), context).should_not be_error | |
| + parslet.apply(src('foo'), context).should_not == :fail | |
| end | |
| it "should not change input position" do | |
| source = src('foo') | |
| @@ -199,7 +199,7 @@ describe Parslet do | |
| end | |
| describe "<- #parse" do | |
| it "should return nil" do | |
| - parslet.apply(src('foo'), context).result.should == nil | |
| + parslet.apply(src('foo'), context).should == nil | |
| end | |
| end | |
| end | |
| @@ -214,7 +214,7 @@ describe Parslet do | |
| end | |
| context "when fed 'bar'" do | |
| it "should parse" do | |
| - parslet.apply(src('bar'), context).should_not be_error | |
| + parslet.apply(src('bar'), context).should_not == :fail | |
| end | |
| it "should not change input position" do | |
| source = src('bar') | |
| @@ -439,4 +439,4 @@ describe Parslet do | |
| end | |
| end | |
| end | |
| -end | |
| \ No newline at end of file | |
| +end | |
| -- | |
| 1.7.2.3 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment