[Pharo-dev] petit parser longest match

Norbert Hartl norbert at hartl.name
Wed Nov 20 20:03:34 EST 2013


I'm not sure what you are trying to improve. It works quite well :)

Norbert

> Am 20.11.2013 um 22:59 schrieb Chris Cunningham <cunningham.cb at gmail.com>:
> 
> ehh, ignore this for now.  Neither is working well enough.  If I get something working well, I'll post back.
> 
> 
>> On Wed, Nov 20, 2013 at 1:45 PM, Chris Cunningham <cunningham.cb at gmail.com> wrote:
>> Actually, do this [the #( 0 nil ) meant it was shared with ALL choice parsers forever - not a good choice].
>> 
>> parseOn: aStream
>> 	"This is optimized code that avoids unnecessary block activations, do not change. When all choices fail, the last failure is answered."
>> 
>> 	| element longest |
>> 	longest := Array with: 0 with: nil.
>> 	1 to: parsers size do: [ :index |
>> 		element := (parsers at: index)
>> 			parseOn: aStream.
>> 		element isPetitFailure
>> 			ifTrue: [ aStream position > longest first ifTrue: [ longest at: 1 put: aStream position; at: 2 put: element ] ]
>> 			ifFalse: [ ^ element ] ].
>> 	aStream position: longest first.
>> 	^ element
>> 
>> 
>>> On Wed, Nov 20, 2013 at 1:35 PM, Chris Cunningham <cunningham.cb at gmail.com> wrote:
>>> Ho about this (replacement for yours, or just replace in PPChoiceParser - but it will result in different behavior for that method outside of your particular parser, so beware):
>>> 
>>> parseOn: aStream
>>> 	"This is optimized code that avoids unnecessary block activations, do not change. When all choices fail, the last failure is answered."
>>> 
>>> 	| element longest |
>>> 	longest := #( 0 nil ).
>>> 	1 to: parsers size do: [ :index |
>>> 		element := (parsers at: index)
>>> 			parseOn: aStream.
>>> 		element isPetitFailure
>>> 			ifTrue: [ aStream position > longest first ifTrue: [ longest at: 1 put: aStream position; at: 2 put: element ] ]
>>> 			ifFalse: [ ^ element ] ].
>>> 	aStream position: longest first.
>>> 	^ element
>>> 
>>> I had a need for it in the past (and, just now, in fact - so minimally tested just now).
>>> 
>>> -cbc
>>> 
>>> 
>>>> On Wed, Nov 20, 2013 at 2:15 AM, Norbert Hartl <norbert at hartl.name> wrote:
>>>> I talked to Lukas two years ago about parsing the longest match [1]. He committed it to PetitBeta back then. Now I need it and I had a look at the current parsers but didn’t find one that can do the same. Is there a parser that can do longest match?
>>>> 
>>>> If not I would propose adding that to the default petit parser package. Lukas’ proposal back then was
>>>> 
>>>> LongestChoiceParser>>parseOn: aStream
>>>> 	| start element longestEnd longestElement |
>>>> 	start := aStream position.
>>>> 	1 to: parsers size do: [ :index |
>>>> 		element := (parsers at: index)
>>>> 			parseOn: aStream.
>>>> 		(longestEnd isNil or: [ longestEnd < aStream position ]) ifTrue: [
>>>> 			longestEnd := aStream position.
>>>> 			longestElement := element ].
>>>> 		aStream position: start ].
>>>> 	aStream position: longestEnd.
>>>> 	^ longestElement
>>>> 
>>>> Norbert
>>>> 
>>>> [1] http://www.iam.unibe.ch/pipermail/moose-dev/2011-July/009365.html
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20131121/1c4c4ef1/attachment-0002.html>


More information about the Pharo-dev mailing list