PetitParser, longest match

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

PetitParser, longest match

Alberto Bacchelli
Hi,

 I am playing with PetitParser (which looks super-cool!).
Following Lukas' blogpost [1], I defined my identifier as:

<snip>
identifier := #letter asParser , #word asParser star.
</snip>

then, I am using it to find the matches in a given string:

<snip>
identifier matchesIn: 'foo 123 bar12'.
</snip>

which returns:

<snip>
 an OrderedCollection(
#($f #($o $o))
#($o #($o))
#($o #())
#($b #($a $r $1 $2))
#($a #($r $1 $2))
#($r #($1 $2))
)
</snip>

Even though the result is correct and makes much sense, would it
be possible to simply have the longest matches?
In that case I would expect something like:

<snip>
 an OrderedCollection(
#($f #($o $o))
#($b #($a $r $1 $2))
)
</snip>

Thank you!

Cheers,
 Alberto

[1] http://www.lukas-renggli.ch/blog/petitparser-1
_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev
Reply | Threaded
Open this post in threaded view
|

Re: PetitParser, longest match

Lukas Renggli
>  I am playing with PetitParser (which looks super-cool!).
> Following Lukas' blogpost [1], I defined my identifier as:
>
> <snip>
> identifier := #letter asParser , #word asParser star.
> </snip>
>
> then, I am using it to find the matches in a given string:
>
> <snip>
> identifier matchesIn: 'foo 123 bar12'.
> </snip>
>
> which returns:
>
> <snip>
>  an OrderedCollection(
> #($f #($o $o))
> #($o #($o))
> #($o #())
> #($b #($a $r $1 $2))
> #($a #($r $1 $2))
> #($r #($1 $2))
> )
> </snip>
>
> Even though the result is correct and makes much sense, would it
> be possible to simply have the longest matches?

Sure.

As you can see in the implementation and in the comment of
#matchesIn:do: which is called by #matchesIn: the implementation tries
the parser at each input position. You can skip over matches by using
the following code instead:

PPParser>>matchesSkipIn: anObject do: aBlock
        "Search anObject repeatedly for the matches of the receiver. Evaluate
aBlock for each match with the matched parse-tree as the argument.
Skip over the matched part."

        (self ==> aBlock / #any asParser) star parse: anObject

Maybe that should be added? Maybe somebody has a better name :-)

Lukas

--
Lukas Renggli
www.lukas-renggli.ch

_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev
Reply | Threaded
Open this post in threaded view
|

Re: PetitParser, longest match

Alberto Bacchelli
>> Even though the result is correct and makes much sense, would it
>> be possible to simply have the longest matches?
>
> Sure.
>
> As you can see in the implementation and in the comment of
> #matchesIn:do: which is called by #matchesIn: the implementation tries
> the parser at each input position. You can skip over matches by using
> the following code instead:

Thanks.

[...]
> Maybe that should be added? Maybe somebody has a better name :-)

What about matchesLongestIn: , or longestMatchesIn: ?

Alberto
_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev
Reply | Threaded
Open this post in threaded view
|

Re: PetitParser, longest match

Alberto Bacchelli
Another fast question :) :

since the "not" method parses p and succeeds when it fails, but does
not consume its input.
What is the best way to define something negated? For example to define:

"everything but a word or a space"

Thanks,
 Alberto
_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev
Reply | Threaded
Open this post in threaded view
|

Re: PetitParser, longest match

Lukas Renggli
On 26 July 2010 17:48, Alberto Bacchelli <[hidden email]> wrote:
> Another fast question :) :
>
> since the "not" method parses p and succeeds when it fails, but does
> not consume its input.
> What is the best way to define something negated? For example to define:
>
> "everything but a word or a space"

If

   wordOrSpace := #word asParser / #space asParser.

is given you can write the negation using the helper #negate like

   wordOrSpace negate

which simply consumes one character if the receiver does not parse:

   wordOrSpace not , #any asParser ==> #second

Lukas

--
Lukas Renggli
www.lukas-renggli.ch
_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev