PetitParser: Parse X as long as it's not Y

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

PetitParser: Parse X as long as it's not Y

Sean P. DeNigris
Administrator
How do I tell Petit Parser to parse a string of certain allowable characters as long as the final result isn't a particular string? E.g. consume an identifier as long as it's not 'Baz' or 'Bar'

Thanks
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: PetitParser: Parse X as long as it's not Y

vonbecmann
did you try with negate?

comment
^ ($" asParser , $" asParser negate star , $" asParser) flatten



On Sat, Aug 5, 2017 at 5:26 PM, Sean P. DeNigris <[hidden email]> wrote:
How do I tell Petit Parser to parse a string of certain allowable characters
as long as the final result isn't a particular string? E.g. consume an
identifier as long as it's not 'Baz' or 'Bar'

Thanks



-----
Cheers,
Sean
--
View this message in context: http://forum.world.st/PetitParser-Parse-X-as-long-as-it-s-not-Y-tp4958895.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.




--
Bernardo E.C.

Sent from a cheap desktop computer in South America.
Reply | Threaded
Open this post in threaded view
|

Re: PetitParser: Parse X as long as it's not Y

Sean P. DeNigris
Administrator
vonbecmann wrote
did you try with negate?
That is the standard trick with PP, but the problem here is that I'm trying to say, not just "anything which is not Y", but "any string of these characters as long is it doesn't also match Y"
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: PetitParser: Parse X as long as it's not Y

Jan Kurš

Hi, not sure if I understand your requirement, but you can try:

Y not, chars star

Is this what you need?

Cheers Jan


On Sun, Aug 6, 2017, 04:47 Sean P. DeNigris <[hidden email]> wrote:
vonbecmann wrote
> did you try with negate?

That is the standard trick with PP, but the problem here is that I'm trying
to say, not just "anything which is not Y", but "any string of these
characters as long is it doesn't also match Y"



-----
Cheers,
Sean
--
View this message in context: http://forum.world.st/PetitParser-Parse-X-as-long-as-it-s-not-Y-tp4958895p4958912.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: PetitParser: Parse X as long as it's not Y

Peter Kenny
In reply to this post by Sean P. DeNigris
Sean

This seems to be what the PPAndParser is designed for. I tried the
following:

identifier := #letter asParser plus flatten.
exclusion := ('Bar' asParser / 'Baz' asParser) negate.
restricted := identifier and, exclusion.

restricted matches: 'abc'. "--> true"
restricted matches: 'Baz'. "--> false"

(Note the comma after 'and' is necessary - this wasted me a few minutes in
trying.)

Hope this helps

Peter Kenny

-----Original Message-----
From: Pharo-users [mailto:[hidden email]] On Behalf Of
Sean P. DeNigris
Sent: 05 August 2017 21:26
To: [hidden email]
Subject: [Pharo-users] PetitParser: Parse X as long as it's not Y

How do I tell Petit Parser to parse a string of certain allowable characters
as long as the final result isn't a particular string? E.g. consume an
identifier as long as it's not 'Baz' or 'Bar'

Thanks



-----
Cheers,
Sean
--
View this message in context:
http://forum.world.st/PetitParser-Parse-X-as-long-as-it-s-not-Y-tp4958895.ht
ml
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|

Re: PetitParser: Parse X as long as it's not Y

Peter Kenny
Sean

A supplementary point I have just realised. The basic 'and' idea is correct,
but my example is over-simplified. As written, it will reject any string
beginning with 'Bar' or 'Baz'. If you use this pattern, you will need to be
careful how you define the exclusion.

Peter


-----Original Message-----
From: Pharo-users [mailto:[hidden email]] On Behalf Of
PBKResearch
Sent: 06 August 2017 13:28
To: 'Any question about pharo is welcome' <[hidden email]>
Subject: Re: [Pharo-users] PetitParser: Parse X as long as it's not Y

Sean

This seems to be what the PPAndParser is designed for. I tried the
following:

identifier := #letter asParser plus flatten.
exclusion := ('Bar' asParser / 'Baz' asParser) negate.
restricted := identifier and, exclusion.

restricted matches: 'abc'. "--> true"
restricted matches: 'Baz'. "--> false"

(Note the comma after 'and' is necessary - this wasted me a few minutes in
trying.)

Hope this helps

Peter Kenny

-----Original Message-----
From: Pharo-users [mailto:[hidden email]] On Behalf Of
Sean P. DeNigris
Sent: 05 August 2017 21:26
To: [hidden email]
Subject: [Pharo-users] PetitParser: Parse X as long as it's not Y

How do I tell Petit Parser to parse a string of certain allowable characters
as long as the final result isn't a particular string? E.g. consume an
identifier as long as it's not 'Baz' or 'Bar'

Thanks



-----
Cheers,
Sean
--
View this message in context:
http://forum.world.st/PetitParser-Parse-X-as-long-as-it-s-not-Y-tp4958895.ht
ml
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.



Reply | Threaded
Open this post in threaded view
|

Re: PetitParser: Parse X as long as it's not Y

Sean P. DeNigris
Administrator
In reply to this post by Peter Kenny
Peter Kenny wrote
This seems to be what the PPAndParser is designed for.
Thanks for the pointer, Peter! I finally figured it out based on your example. To get more concrete, I'm trying to parse a name, where the end of the input might be an optional middle name, followed by an optional generational (e.g. 'Paul III'). What I think the restricted line says is: "peek to make sure we don't have a #gen, then try to parse an #identifier". Interestingly the order is important. If the peek and identifier are reversed, it tries to peek after the identifier, causing an error when there is only a middle name (i.e. no more input).

identifier := #letter asParser plus flatten.
gen := 'Jr' asParser / 'Sr' asParser.
restricted := gen negate and, identifier.
parser := ((#space asParser, restricted) optional, (#space asParser, gen) optional) end.
parser parse: ' Paul Jr'.
parser parse: ' Paul'.
parser parse: ' Jr'.

This really had me baffled. Thanks again!!
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: PetitParser: Parse X as long as it's not Y

Peter Kenny
Sean

One extra point. You may need to post-process the parse to get what you
want. In the 'restricted' line, if you don't have a #gen then 'gen negate'
will have succeeded and will generate its own node in the parse - in your
example of ' Paul' as input it will be $P. Assuming you don't want this (!),
you can add ==> [:nodes| nodes second] to the definition of restricted.

I had realized this after I posted on Sunday, as well as the fact that I had
put the anded elements in the wrong order, but I didn't want to generate a
stream of supplementaries.

HTH

Peter

-----Original Message-----
From: Pharo-users [mailto:[hidden email]] On Behalf Of
Sean P. DeNigris
Sent: 09 August 2017 03:41
To: [hidden email]
Subject: Re: [Pharo-users] PetitParser: Parse X as long as it's not Y

Peter Kenny wrote
> This seems to be what the PPAndParser is designed for.

Thanks for the pointer, Peter! I finally figured it out based on your
example. To get more concrete, I'm trying to parse a name, where the end of
the input might be an optional middle name, followed by an optional
generational (e.g. 'Paul III'). What I think the restricted line says is:
"peek to make sure we don't have a #gen, then try to parse an #identifier".
Interestingly the order is important. If the peek and identifier are
reversed, it tries to peek after the identifier, causing an error when there
is only a middle name (i.e. no more input).

identifier := #letter asParser plus flatten.
gen := 'Jr' asParser / 'Sr' asParser.
restricted := gen negate and, identifier.
parser := ((#space asParser, restricted) optional, (#space asParser, gen)
optional) end.
parser parse: ' Paul Jr'.
parser parse: ' Paul'.
parser parse: ' Jr'.

This really had me baffled. Thanks again!!



-----
Cheers,
Sean
--
View this message in context:
http://forum.world.st/PetitParser-Parse-X-as-long-as-it-s-not-Y-tp4958895p49
59268.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|

Re: PetitParser: Parse X as long as it's not Y

Sean P. DeNigris
Administrator
Peter Kenny wrote
You may need to post-process the parse to get what you
want.
Good point. I omitted several ` ==> #second` in several places for readability on the list, but we should mention that for posterity. Also, duh `gen negate and` should be `gen not`!
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: PetitParser: Parse X as long as it's not Y

Tudor Girba-2
Hi,

As Jan pointed out, you should use “not” instead of “negate and”.

Cheers,
Doru


> On Aug 10, 2017, at 5:22 AM, Sean P. DeNigris <[hidden email]> wrote:
>
> Peter Kenny wrote
>> You may need to post-process the parse to get what you
>> want.
>
> Good point. I omitted several ` ==> #second` in several places for
> readability on the list, but we should mention that for posterity. Also, duh
> `gen negate and` should be `gen not`!
>
>
>
> -----
> Cheers,
> Sean
> --
> View this message in context: http://forum.world.st/PetitParser-Parse-X-as-long-as-it-s-not-Y-tp4958895p4959533.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>

--
www.tudorgirba.com
www.feenk.com

"Speaking louder won't make the point worthier."