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:

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 `LM` Shape) `ML` (Shape `ML` 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:

 `ha__` "[ERROR] Missing or mismatching bracket!"
   `lv` "[VALID] Everything is seem to be good!"

Full source code is available here.

Next time we will traverse a real piece of code.

Continue this tutorial