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