Previous chapter ⋅ Full source code ⋅ Next chapter
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:
compare closed opened = opened `hd'q` closed
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
type Imbalance = (Shape `P` Shape) `S` (Shape `S` Shape)
pattern Mismatch x = This x :: Imbalance
pattern Missing x = That x :: Imbalance
The check itself occurs when we stumble upon a closed bracket, comparing it to an opened one (topmost stack value):
`yok__` Try `ha__` None `hu_` Error `ha` Missing `ha` Opened `hv` bracket `la` Valid
`yok__` Try `ha__` Error `ha` Mismatch `la` Valid `ha_` compare bracket
It’s left to catch missing closed brackets after we finish traversing a sequence:
`la` Error `ha` Missing `ha` Closed `ha` this @Shape `ha` top @(Nonempty List)
And don’t forget to fix reporting errors:
`yi__` Error `hu` "[ERROR] We missed some bracket, oh my!" `ho` is @(List ASCII)
`la` Valid `hu` "[VALID] Everything is seem to be good."
Next time we will traverse a real piece of code.