megaparsec icon indicating copy to clipboard operation
megaparsec copied to clipboard

How to make a recoverable indentation block parser

Open bingis-khan opened this issue 9 months ago • 1 comments

I'm writing an indentation parser and I want to recover from one certain type of error: indentation that is larger than reference indentation, but unequal to indentation set by the block parser.

Ex:

f ()
  x = 1
  y = 2
    z = 5
 pass
      n = 1
  return

As you can see, it's obvious that the statements are correct and belong to the same block. It's just that the indentation is all weird.

I want to report the error and continue parsing "block items" after this, including the incorrectly indented statement. This would require me to catch the error some time during the execution of this function.

What would be the best approach to do that?

  • use indentedItems (which is not currently exported), get the indentation ref and call indentedItems after an error?
  • write a specific recoverableIndentBlock which handles this exact case? <-- I'll attempt this one

bingis-khan avatar May 19 '24 07:05 bingis-khan