From a605af3bbe40c1051ca49cb9d829a160e0e7bb96 Mon Sep 17 00:00:00 2001 From: Jorge Luis Martinez Gomez Date: Thu, 24 Sep 2020 22:11:35 -0700 Subject: [PATCH] add more to examples of common combinators in readme... and fix splicing of < while we're at it. --- README.md | 19 +++++++++++++++---- lib/parsby.rb | 4 +++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 06e2a98..2cad70d 100644 --- a/README.md +++ b/README.md @@ -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" @@ -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" @@ -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"] @@ -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" diff --git a/lib/parsby.rb b/lib/parsby.rb index 3880a00..7e60615 100644 --- a/lib/parsby.rb +++ b/lib/parsby.rb @@ -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.