PetitParser trim bug?

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

PetitParser trim bug?

Sean P. DeNigris
Administrator
According to http://www.lukas-renggli.ch/blog/petitparser-1, "p trim Trims whitespaces before and after p".

However, the following snippet seems not to trim the whitespace at the end of the second parser:

stringToParse := 'class=" aClass "'.
parser := ('class="' asParser, $" asParser negate plus trim flatten, '"' asParser) ==> [ :nodes | nodes second ].
result := parser parse: stringToParse.
self assert: result = 'aClass'. "actually returns ' aClass '"

Should it be stripping the trailing space?

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

Re: PetitParser trim bug?

Tudor Girba
Hi,

Actually, it is not a bug in trim.

There are two problems in your code.
- The first problem is that "$" asParser negate plus" will consume whitespaces, too, and thus, there will be no whitespace left to trim afterwards.
- The second problem was the order of "trim" and "flatten". It should be "flatten trim". If you say "trim flatten", trim will indeed return the collection without whitespaces, but the position of the token still includes the whitespaces, and thus, because flatten simply flattens from the start to the end of the current position, you get the spaces back.

Try this:
stringToParse := 'class=" aClass "'.
valueParser := ($" asParser /  #blank asParser ) negate plus.
parser := ('class="' asParser,  valueParser flatten trim , $" asParser) ==> [:nodes | nodes second].
result := parser parse: stringToParse.

Cheers,
Doru


On 26 Nov 2010, at 07:20, Sean P. DeNigris wrote:

>
> According to http://www.lukas-renggli.ch/blog/petitparser-1, "p trim Trims
> whitespaces before and after p".
>
> However, the following snippet seems not to trim the whitespace at the end
> of the second parser:
>
> stringToParse := 'class=" aClass "'.
> parser := ('class="' asParser, $" asParser negate plus trim flatten, '"'
> asParser) ==> [ :nodes | nodes second ].
> result := parser parse: stringToParse.
> self assert: result = 'aClass'. "actually returns ' aClass '"
>
> Should it be stripping the trailing space?
>
> Thanks.
> Sean
> --
> View this message in context: http://forum.world.st/PetitParser-trim-bug-tp3059805p3059805.html
> Sent from the Moose mailing list archive at Nabble.com.
> _______________________________________________
> Moose-dev mailing list
> [hidden email]
> https://www.iam.unibe.ch/mailman/listinfo/moose-dev

--
www.tudorgirba.com

"Every now and then stop and ask yourself if the war you're fighting is the right one."




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