Open your playground and let's start coding. First of all, we define what we want to parse:
The source very simplified (and slightly modified) version of Wikipedia and contains the following text:
starLazy operator is a new feature of PetitParser2.
It repetitively invokes the given parser (any character in this case) until a string recognized by the following parser (</script> in this case) appears.
starLazy unique and useful is that you don’t need to specify what is the following parser, it is inferred automatically based on the grammar specification.
Of course, with any change in the grammar, the
starLazy updates itself.
If you want to define the same rule in previous version of PetitParser, it would be:
The result is failure.
If we inspect the failure object and switch to Debug View we notice that <script> is expected at the beginning of a file, yet our input starts with <!DOCTYPE html>.
The fix to this is simple with PetitParser2: we create a new rule
sea is doing a lot of magic and the result is not failure!
But it is not exactly what we want.
Sea returns an array of three elements:
Looks better, but we are missing some results!
This is because we never specified that there could be multiple occurrences of
Therefore the sea rule finds only one --- the first one.
We can easily add more
jsSea rules by defining a
The whole script looks like:
Now by calling
js as we defined it does not know about strings.
The result looks better now:
Great, everything works as expected!
In the new version of PetitParser, we have a
starLazy operators at hand to help us skip the uninteresting input.
More details are provided in this chapter.
If you think that the task from this chapter could be done using a simple regular expression, you are right absolutely right. Nevertheless, contrary to regular expressions, this is just a first step for PetitParser. PetitParser will boldly go where no regular expression has gone before.
This text is part of the Parsing With PetitParser2 series.
Part I, Developer's Workflow:
Go to top.