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
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
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
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
On 26 July 2010 17:48, Alberto Bacchelli alberto.bacchelli@usi.ch 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