Hi all,
I am trying to implement a small "island parser" in PetitParser, but I have a problem. The following is the most self contained example I can think of, it is able to extract the word 'island' from any context.
I have the class IslandSyntax, subclass of PPCompositeParser, with the following methods:
=== IslandSyntax>>start ^world end
IslandSytntax>>world world := PPUnresolvedParser new. world def: (island , world) / island / (water,world) / water. ^world.
IslandSyntax>>island ^'island' asParser
IslandSyntax>>water ^((island not), #any asParser) ==> #second ===
Then, I have IslandParser a subclass of IslandSyntax:
=== IslandParser>>island ^super island ==> [:result | result inspect] ===
If I open a workspace and do:
IslandParser new parse: 'blablablaislandblablabla'.
The inspect on island is called only once, while if I do:
IslandParser new parse: 'island'.
the inspector is called twice, probably because in the "world" production I first put (island , world) and then (island). Is there a way to avoid this double calling? Am I doing anything wrong here?
Thank you!
Alberto
IslandSytntax>>world world := PPUnresolvedParser new. world def: (island , world) / island / (water,world) / water. ^world.
Well --- this has nothing to do with your problem --- but you shouldn't use #def: in subclasses of PPCompositeParser. PPCompositeParser takes care and resolves recursive references for you. So better just write:
IslandSyntax>>world ^ (island , world) / island / (water , world) / water
the inspector is called twice, probably because in the "world" production I first put (island , world) and then (island). Is there a way to avoid this double calling? Am I doing anything wrong here?
I am not sure what you are trying to achieve with this grammar. So it is impossible for me to understand what the grammar is supposed to do.
Certainly both productions 'island' and 'water' will trigger the inspector. The #not does not prevent the side effects its receiver has.
I am sure that the PetitParser debugger can give you an explanation of why the two examples parse different to what you expect.
Likely you need to adapt the grammar, or trigger the events after parsing and resolving the duplicates.
If you are searching a random input stream for some part that satisfies a grammar use #matchesIn:, #matchesIn:do:, #matchingRangesIn:, or #matchingRangesIn:do:.
Lukas
First of all, thank you!
PPCompositeParser takes care and resolves recursive references for you.
Oh, yes, you are right. I had lost the big picture, shame on me :)
Likely you need to adapt the grammar, or trigger the events after parsing and resolving the duplicates.
I just realized that I could much more easily something like:
IslandSyntax>>world ^ (island / water ) plus
I don't know why I didn't see it before... probably my old implementation in asf-sdf needed it :|
Thank you again for the instance, I should be able to fix it now.
Cheers, Alberto