Previous part: Balancing brackets - 1
This time we are going go use ya-ascii package.
Since we introduce different types of brackets, we additionally should check if the shape of an opened bracket mathes a closed one:
However, now we have one additional point of failure. So… maybe it’s good time to start define types of errors? There could be the following ones:
- Opened and closed brackets have a distinguish shape
- Either an opened or a closed bracket is missing
The check itself occurs when we stumble upon a closed bracket, comparing it to an opened one (topmost stack value):
It’s left to catch missing closed brackets after we finish traversing a sequence:
And don’t forget to fix reporting errors:
Full source code is available here.
Next time we will traverse a real piece of code.