-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from chrismwendt/ignore
Add the ability to ignore a parse result
- Loading branch information
Showing
7 changed files
with
57 additions
and
37 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Ignores the result of a match. | ||
# | ||
# Example: | ||
# | ||
# str('foo') # will return 'foo', | ||
# str('foo').ignore # will return nil | ||
# | ||
class Parslet::Atoms::Ignored < Parslet::Atoms::Base | ||
attr_reader :parslet | ||
def initialize(parslet) | ||
super() | ||
|
||
@parslet = parslet | ||
end | ||
|
||
def apply(source, context, consume_all) | ||
success, _ = result = parslet.apply(source, context, consume_all) | ||
|
||
return result unless success | ||
succ(nil) | ||
end | ||
|
||
def to_s_inner(prec) | ||
"ignored(#{parslet.to_s(prec)})" | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'spec_helper' | ||
|
||
describe Parslet::Atoms::Ignored do | ||
include Parslet | ||
|
||
describe "ignore" do | ||
it "ignores parts of the input" do | ||
str('a').ignore.parse('a').should == nil | ||
(str('a') >> str('b').ignore >> str('c')).parse('abc').should == 'ac' | ||
(str('a') >> str('b').as(:name).ignore >> str('c')).parse('abc').should == 'ac' | ||
(str('a') >> str('b').maybe.ignore >> str('c')).parse('abc').should == 'ac' | ||
(str('a') >> str('b').maybe.ignore >> str('c')).parse('ac').should == 'ac' | ||
end | ||
end | ||
end |
This file contains 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