brittany icon indicating copy to clipboard operation
brittany copied to clipboard

Pass unknown syntactical constructs through instead of crashing

Open mrkgnao opened this issue 8 years ago • 3 comments

With a file with pattern synonyms in it, for instance, Brittany throws errors. I'm fine with it supporting a subset of full GHC Haskell syntax, but could there be a switch or something to pass unknown syntactic forms through unmodified (like is done for data declarations at present)?

mrkgnao avatar Jan 16 '18 12:01 mrkgnao

I wouldn't mind implementing this myself if someone points me to a source location or two.

mrkgnao avatar Jan 26 '18 10:01 mrkgnao

@mrkgnao brittany does this in a number of places, but you'll generally be looking in src/Language/Haskell/Brittany/Internal/Layouters/.

eborden avatar Jan 28 '18 19:01 eborden

A quick fix, just for this extension, might be to modify ppDecl to just ignore the PatSynBind node, along the lines of

ppDecl :: LHsDecl RdrName -> PPMLocal ()
ppDecl d@(L loc decl) = case decl of
  SigD sig -> -- trace (_sigHead sig) $
              withTransformedAnns d $ do
    -- runLayouter $ Old.layoutSig (L loc sig)
    briDoc <- briDocMToPPM $ layoutSig (L loc sig)
    layoutBriDoc briDoc
  ValD PatSynBind{} ->
    -- fall-back solution for -XPatternSynonyms
    briDocMToPPM (briDocByExactNoComment d) >>= layoutBriDoc
  ValD bind -> -- trace (_bindHead bind) $
               withTransformedAnns d $ do
    -- Old.layoutBind (L loc bind)
    briDoc <- briDocMToPPM $ do
      eitherNode <- layoutBind (L loc bind)
      case eitherNode of
        Left  ns -> docLines $ return <$> ns
        Right n  -> return n
    layoutBriDoc briDoc
  _ -> briDocMToPPM (briDocByExactNoComment d) >>= layoutBriDoc

this is in Language.Haskell.Brittany.Internal.

Regarding a more proper/general solution: It is true that the code currently assumes that there are "termination-worthy" errors, where we could just fall back on exactprint. We could wrap the whole ppDecl, run the writer-of-warnings/errors part of the transformer stack, check for termination-worthy errors, revert to exactprint and continue without aborting. This might turn both ErrorUnusedComment and ErrorUnknownNode into warnings effectively.

If we go that route, i'd consider throwing out the PPM transformer entirely (but not PPMLocal) because it does not make sense to invent a specific transformer stack if we use the type for one function only (and then have to unwrap/switch to a different stack again anyways.) I.e. let ppModule have a transformer-free type.

pull-requests for either solution welcome. I should probably do the PPM-related refactoring, as i started overusing MultiRWS - well the requirements changed over time, but whatever.

lspitzner avatar Feb 03 '18 22:02 lspitzner