From 14884be8dfd0e3343e180e38653f25b4e6a9671c Mon Sep 17 00:00:00 2001 From: Lennart Spitzner Date: Wed, 12 Apr 2017 14:31:53 +0200 Subject: [PATCH] Minor fixups in docs plus some minor Refactoring --- docs/implementation/bridoc-design.md | 76 ++++++++++++++-------------- src/Language/Haskell/Brittany.hs | 61 ++++++++++------------ 2 files changed, 66 insertions(+), 71 deletions(-) diff --git a/docs/implementation/bridoc-design.md b/docs/implementation/bridoc-design.md index 103d05c..c0f95e8 100644 --- a/docs/implementation/bridoc-design.md +++ b/docs/implementation/bridoc-design.md @@ -35,47 +35,47 @@ See [this example ast output](output-example-01.md) 1. For example, `Brittany.hs` contains the following code (shortened a bit): - ~~~~.hs - ppDecl d@(L loc decl) = case decl of - SigD sig -> [..] $ do - briDoc <- briDocMToPPM $ layoutSig (L loc sig) - layoutBriDoc d briDoc - ValD bind -> [..] $ do - briDoc <- [..] layoutBind (L loc bind) - layoutBriDoc d briDoc - _ -> briDocMToPPM (briDocByExactNoComment d) >>= layoutBriDoc d - ~~~~ + ~~~~.hs + ppDecl d@(L loc decl) = case decl of + SigD sig -> [..] $ do + briDoc <- briDocMToPPM $ layoutSig (L loc sig) + layoutBriDoc d briDoc + ValD bind -> [..] $ do + briDoc <- [..] layoutBind (L loc bind) + layoutBriDoc d briDoc + _ -> briDocMToPPM (briDocByExactNoComment d) >>= layoutBriDoc d + ~~~~ - which matches on the type of module top-level syntax node and - dispatches to `layoutSig`/`layoutBind` to layout type signatures - and equations. For all other constructs, it currently falls back to using - ExactPrint to reproduce the exact original. + which matches on the type of module top-level syntax node and + dispatches to `layoutSig`/`layoutBind` to layout type signatures + and equations. For all other constructs, it currently falls back to using + ExactPrint to reproduce the exact original. 2. Lets look at a "lower" level fragment that actually produces BriDoc (from Type.hs): - ~~~~.hs - -- if our type is an application; think "HsAppTy Maybe Int" - HsAppTy typ1 typ2 -> do - typeDoc1 <- docSharedWrapper layoutType typ1 -- layout `Maybe` - typeDoc2 <- docSharedWrapper layoutType typ2 -- layout `Int` - docAlt -- produce two possible layouts - [ docSeq -- a singular-line sequence, with a space in between - [ docForceSingleline typeDoc1 -- "Maybe Int" - , docLit $ Text.pack " " - , docForceSingleline typeDoc2 + ~~~~.hs + -- if our type is an application; think "HsAppTy Maybe Int" + HsAppTy typ1 typ2 -> do + typeDoc1 <- docSharedWrapper layoutType typ1 -- layout `Maybe` + typeDoc2 <- docSharedWrapper layoutType typ2 -- layout `Int` + docAlt -- produce two possible layouts + [ docSeq -- a singular-line sequence, with a space in between + [ docForceSingleline typeDoc1 -- "Maybe Int" + , docLit $ Text.pack " " + , docForceSingleline typeDoc2 + ] + , docPar -- a multi-line result, with the "child" indented. + typeDoc1 -- "Maybe\ + (docEnsureIndent BrIndentRegular typeDoc2) -- Int" ] - , docPar -- an multi-line result, with the "child" indented. - typeDoc1 -- "Maybe\ - (docEnsureIndent BrIndentRegular typeDoc2) -- Int" - ] - ~~~~ + ~~~~ - here, all functions prefixed with "doc" produces new BriDoc(F) nodes. - I think this example can be understood already, even when many details - (what is `docSharedWrapper`? - What are the exact semantics of the different `doc..` functions? - Why do we need to wrap the `BriDoc` constructors behind those smart-constructor thingies?) - are not explained yet. + here, all functions prefixed with "doc" produce new BriDoc(F) nodes. + I think this example can be understood already, even when many details + (what is `docSharedWrapper`? + What are the exact semantics of the different `doc..` functions? + Why do we need to wrap the `BriDoc` constructors behind those smart-constructor thingies?) + are not explained yet. In [this example output](output-example-02.md) the BriDoc tree produced in this fashion is shown for the trivial input `x :: Maybe Int`. Can you spot @@ -144,8 +144,8 @@ in a single line now. We will not go into detail about how this "alt-transformation" (the one doing the "selection work" works and what other transformations follow here. -For this example not much happens; you can see so in the output which you -probably already noticed in the last example. +For this example not much happens; you can see so in the final `BriDoc` which +you probably already noticed in the last example. But for the "alt-transformation" itself, lets at least consider what it does: We traverse the input BriDoc and whenever a `BDAlt` is encountered, one of the @@ -215,7 +215,7 @@ We don't use this directly, but the code below uses this, and if the type `ToBriDocM` scared you, see how mundane it is used here (`m` will be `ToBriDocM` mostly): -~~~~ +~~~~.hs allocNodeIndex :: MonadMultiState NodeAllocIndex m => m Int allocNodeIndex = do NodeAllocIndex i <- mGet diff --git a/src/Language/Haskell/Brittany.hs b/src/Language/Haskell/Brittany.hs index bd92e3b..6eeea5d 100644 --- a/src/Language/Haskell/Brittany.hs +++ b/src/Language/Haskell/Brittany.hs @@ -188,11 +188,12 @@ ppModule lmod@(L loc m@(HsModule _name _exports _imports decls _ _)) = do in (anns', post) MultiRWSS.withMultiReader anns' $ processDefault emptyModule decls `forM_` ppDecl - let finalComments = filter ( fst .> \case - ExactPrint.Types.AnnComment{} -> True - _ -> False - ) - post + let finalComments = filter + ( fst .> \case + ExactPrint.Types.AnnComment{} -> True + _ -> False + ) + post post `forM_` \case (ExactPrint.Types.AnnComment (ExactPrint.Types.Comment cmStr _ _), l) -> do ppmMoveToExactLoc l @@ -257,39 +258,34 @@ layoutBriDoc :: Data.Data.Data ast => ast -> BriDocNumbered -> PPM () layoutBriDoc ast briDoc = do -- first step: transform the briDoc. briDoc' <- MultiRWSS.withMultiStateS BDEmpty $ do + -- Note that briDoc is BriDocNumbered, but state type is BriDoc. + -- That's why the alt-transform looks a bit special here. traceIfDumpConf "bridoc raw" _dconf_dump_bridoc_raw $ briDocToDoc $ unwrapBriDocNumbered $ briDoc -- bridoc transformation: remove alts transformAlts briDoc >>= mSet - mGet - >>= traceIfDumpConf "bridoc post-alt" _dconf_dump_bridoc_simpl_alt - . briDocToDoc + mGet >>= briDocToDoc .> traceIfDumpConf "bridoc post-alt" + _dconf_dump_bridoc_simpl_alt -- bridoc transformation: float stuff in - mGet <&> transformSimplifyFloating >>= mSet - mGet - >>= traceIfDumpConf "bridoc post-floating" - _dconf_dump_bridoc_simpl_floating - . briDocToDoc + mGet >>= transformSimplifyFloating .> mSet + mGet >>= briDocToDoc .> traceIfDumpConf "bridoc post-floating" + _dconf_dump_bridoc_simpl_floating -- bridoc transformation: par removal - mGet <&> transformSimplifyPar >>= mSet - mGet - >>= traceIfDumpConf "bridoc post-par" _dconf_dump_bridoc_simpl_par - . briDocToDoc + mGet >>= transformSimplifyPar .> mSet + mGet >>= briDocToDoc .> traceIfDumpConf "bridoc post-par" + _dconf_dump_bridoc_simpl_par -- bridoc transformation: float stuff in - mGet <&> transformSimplifyColumns >>= mSet - mGet - >>= traceIfDumpConf "bridoc post-columns" _dconf_dump_bridoc_simpl_columns - . briDocToDoc - -- -- bridoc transformation: indent - mGet <&> transformSimplifyIndent >>= mSet - mGet - >>= traceIfDumpConf "bridoc post-indent" _dconf_dump_bridoc_simpl_indent - . briDocToDoc - mGet - >>= traceIfDumpConf "bridoc final" _dconf_dump_bridoc_final - . briDocToDoc + mGet >>= transformSimplifyColumns .> mSet + mGet >>= briDocToDoc .> traceIfDumpConf "bridoc post-columns" + _dconf_dump_bridoc_simpl_columns + -- bridoc transformation: indent + mGet >>= transformSimplifyIndent .> mSet + mGet >>= briDocToDoc .> traceIfDumpConf "bridoc post-indent" + _dconf_dump_bridoc_simpl_indent + mGet >>= briDocToDoc .> traceIfDumpConf "bridoc final" + _dconf_dump_bridoc_final -- -- convert to Simple type -- simpl <- mGet <&> transformToSimple -- return simpl @@ -317,10 +313,9 @@ layoutBriDoc ast briDoc = do state' <- MultiRWSS.withMultiStateS state $ layoutBriDocM briDoc' - let - remainingComments = - extractAllComments =<< Map.elems (_lstate_comments state') + let remainingComments = + extractAllComments =<< Map.elems (_lstate_comments state') remainingComments - `forM_` (mTell . (:[]) . LayoutErrorUnusedComment . show . fst) + `forM_` (fst .> show .> LayoutErrorUnusedComment .> (:[]) .> mTell) return $ ()