Elements of HTML has an interesting property: the name of an opening tag has to match the name of a closing tag. Though natural for humans, this is, surprisingly, rather difficult task from the parsing theory point of view.
One of the standard solutions provided by parsing frameworks (including PetitParser) are parsing actions. We define an action to assert that the name of an open tag corresponds to the name of a close tag:
Unfortunately, this solution has a problem with unclosed elements like these:
elOpen consumes <b>,
content consumes bold and italics and
elClose consumes </b>.
The action checks if i = b and returns failure.
The failure will be the final result because such a code does not restore position to recover from the failure and actions do not offer a way to do so.
Other options based on custom parsers or wrapping parsers will sooner or later run into a hard to debug issues with backtracking as well.
The PetitParser2 offers more formal way of such definitions.
It can store a result of a rule (e.g.
elOpen) onto a stack using the push operator and assert that the result of a rule (e.g.
elClose) matches the top of the stack using the match operator and finally pop the result using the pop operator.
Here is the concrete example.
First we define an element name as a repetition of letters and digits:
Than we define element as a sequence of
elOpen, we push the element name as well as we consume water in case an element contains arguments:
elClose, we first match the element name against the top of a stack and we pop the stack in case of success: