Skip to content

Commit

Permalink
add more to examples of common combinators in readme...
Browse files Browse the repository at this point in the history
and fix splicing of < while we're at it.
  • Loading branch information
jolmg committed Sep 25, 2020
1 parent da4a42e commit a605af3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ between(lit("<"), lit(">"), decimal).parse "<100>"
ilit("Foo").parse "fOo"
#=> "fOo"

# Make any value into a parser that results in that value without
# consuming input.
pure("foo").parse ""
#=> "foo"

# Parse foo or bar
(lit("foo") | lit("bar")).parse "bar"
#=> "bar"
Expand All @@ -94,7 +99,7 @@ between(lit("<"), lit(">"), decimal).parse "<100>"
(lit("foo") < lit("bar")).parse "foobar"
#=> "foo"

# Parse foo and transform result according to block.
# Parse transform result according to block.
lit("foo").fmap {|x| x.upcase }.parse "foo"
#=> "FOO"

Expand All @@ -111,7 +116,8 @@ between(lit("<"), lit(">"), decimal).parse "<100>"
many(lit("foo")).parse "foofoo"
#=> ["foo", "foo"]

# Parse many, but each separated by something.
# Parse many, but each separated by something. sep_by_1 requires at least
# one element.
sep_by(lit(","), lit("foo")).parse "foo,foo"
#=> ["foo", "foo"]

Expand All @@ -129,12 +135,17 @@ between(lit("<"), lit(">"), decimal).parse "<100>"
#=> "foo"

# Parse any one character
any_char.parse "f"
any_char.parse "foo"
#=> "f"

# Require eof at end of parse
(lit("foo") < eof).parse "foobar"
#=> Parsby::ExpectationFailed
#=> Parsby::ExpectationFailed: line 1:
foobar
| * failure: eof
\-/ *| success: lit("foo")
\|
| * failure: (lit("foo") < eof)

# join(p) is the same as p.fmap {|xs| xs.join}
join(sep_by(lit(","), lit("foo") | lit("bar"))).parse "foo,bar"
Expand Down
4 changes: 3 additions & 1 deletion lib/parsby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,9 @@ def |(p)

# x < y runs parser x then y and returns x.
def <(p)
self.then {|r| p.then { pure r } } % "(#{label} < #{p.label})"
~splicer.start do |m|
m.end(self).then {|r| m.end(p).then { pure r } }
end % "(#{label} < #{p.label})"
end

# x > y runs parser x then y and returns y.
Expand Down

0 comments on commit a605af3

Please sign in to comment.