Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make a recoverable indentation block parser #561

Open
bingis-khan opened this issue May 19, 2024 · 1 comment
Open

How to make a recoverable indentation block parser #561

bingis-khan opened this issue May 19, 2024 · 1 comment
Labels

Comments

@bingis-khan
Copy link

bingis-khan commented May 19, 2024

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
Copy link
Author

Okay, to answer the question of whether it's possible to rewrite indentBlock to be recoverable is: yes. The final implementation (specialized to my parser), but can be easily generalized is this:

recoverableIndentBlock ::
  Parser (L.IndentOpt Parser a b) ->
  Parser a
recoverableIndentBlock r = do
  scn
  ref <- L.indentLevel
  a <- r
  case a of
    L.IndentNone x -> x <$ scn
    L.IndentMany indent f p -> do
      mlvl <- (optional . try) (C.eol *> L.indentGuard scn GT ref)
      done <- isJust <$> optional eof
      case (mlvl, done) of
        (Just lvl, False) ->
          indentedItems ref (fromMaybe lvl indent) p >>= f
        _ -> scn *> f []
    L.IndentSome indent f p -> do
      pos <- C.eol *> L.indentGuard scn GT ref
      let lvl = fromMaybe pos indent
      x <-
        if
          | pos <= ref -> L.incorrectIndent GT ref pos
          | pos == lvl -> p
          | otherwise -> L.incorrectIndent EQ lvl pos
      xs <- indentedItems ref lvl p
      f (x : xs)

indentedItems ::
  -- | Reference indentation level
  Pos ->
  -- | Level of the first indented item ('lookAhead'ed)
  Pos ->
  -- | How to parse indented tokens
  Parser b ->
  Parser [b]
indentedItems ref lvl p = go
  where
    go = do
      scn
      pos <- L.indentLevel
      done <- isJust <$> optional eof
      if done
        then return []
        else
          if
            | pos <= ref -> return []
            | pos == lvl -> (:) <$> p <*> go
            | otherwise -> do
              o <- getOffset
              registerParseError $ FancyError o $ Set.singleton $ ErrorIndentation EQ lvl pos
              (:) <$> p <*> go

I still have a question though: is it the idiomatic way to do it? Maybe it would be nice to add something like this to the library?

@mrkkrp mrkkrp added the question label Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants