Minor fixups in docs plus some minor Refactoring

pull/35/head
Lennart Spitzner 2017-04-12 14:31:53 +02:00
parent 2dd6fe83f5
commit 14884be8df
2 changed files with 66 additions and 71 deletions

View File

@ -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): 1. For example, `Brittany.hs` contains the following code (shortened a bit):
~~~~.hs ~~~~.hs
ppDecl d@(L loc decl) = case decl of ppDecl d@(L loc decl) = case decl of
SigD sig -> [..] $ do SigD sig -> [..] $ do
briDoc <- briDocMToPPM $ layoutSig (L loc sig) briDoc <- briDocMToPPM $ layoutSig (L loc sig)
layoutBriDoc d briDoc layoutBriDoc d briDoc
ValD bind -> [..] $ do ValD bind -> [..] $ do
briDoc <- [..] layoutBind (L loc bind) briDoc <- [..] layoutBind (L loc bind)
layoutBriDoc d briDoc layoutBriDoc d briDoc
_ -> briDocMToPPM (briDocByExactNoComment d) >>= layoutBriDoc d _ -> briDocMToPPM (briDocByExactNoComment d) >>= layoutBriDoc d
~~~~ ~~~~
which matches on the type of module top-level syntax node and which matches on the type of module top-level syntax node and
dispatches to `layoutSig`/`layoutBind` to layout type signatures dispatches to `layoutSig`/`layoutBind` to layout type signatures
and equations. For all other constructs, it currently falls back to using and equations. For all other constructs, it currently falls back to using
ExactPrint to reproduce the exact original. ExactPrint to reproduce the exact original.
2. Lets look at a "lower" level fragment that actually produces BriDoc (from Type.hs): 2. Lets look at a "lower" level fragment that actually produces BriDoc (from Type.hs):
~~~~.hs ~~~~.hs
-- if our type is an application; think "HsAppTy Maybe Int" -- if our type is an application; think "HsAppTy Maybe Int"
HsAppTy typ1 typ2 -> do HsAppTy typ1 typ2 -> do
typeDoc1 <- docSharedWrapper layoutType typ1 -- layout `Maybe` typeDoc1 <- docSharedWrapper layoutType typ1 -- layout `Maybe`
typeDoc2 <- docSharedWrapper layoutType typ2 -- layout `Int` typeDoc2 <- docSharedWrapper layoutType typ2 -- layout `Int`
docAlt -- produce two possible layouts docAlt -- produce two possible layouts
[ docSeq -- a singular-line sequence, with a space in between [ docSeq -- a singular-line sequence, with a space in between
[ docForceSingleline typeDoc1 -- "Maybe Int" [ docForceSingleline typeDoc1 -- "Maybe Int"
, docLit $ Text.pack " " , docLit $ Text.pack " "
, docForceSingleline typeDoc2 , 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. here, all functions prefixed with "doc" produce new BriDoc(F) nodes.
I think this example can be understood already, even when many details I think this example can be understood already, even when many details
(what is `docSharedWrapper`? (what is `docSharedWrapper`?
What are the exact semantics of the different `doc..` functions? What are the exact semantics of the different `doc..` functions?
Why do we need to wrap the `BriDoc` constructors behind those smart-constructor thingies?) Why do we need to wrap the `BriDoc` constructors behind those smart-constructor thingies?)
are not explained yet. are not explained yet.
In [this example output](output-example-02.md) the BriDoc tree produced in 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 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 We will not go into detail about how this "alt-transformation" (the one doing
the "selection work" works and what other transformations follow here. 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 For this example not much happens; you can see so in the final `BriDoc` which
probably already noticed in the last example. you probably already noticed in the last example.
But for the "alt-transformation" itself, lets at least consider what it does: 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 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 and if the type `ToBriDocM` scared you, see how mundane it
is used here (`m` will be `ToBriDocM` mostly): is used here (`m` will be `ToBriDocM` mostly):
~~~~ ~~~~.hs
allocNodeIndex :: MonadMultiState NodeAllocIndex m => m Int allocNodeIndex :: MonadMultiState NodeAllocIndex m => m Int
allocNodeIndex = do allocNodeIndex = do
NodeAllocIndex i <- mGet NodeAllocIndex i <- mGet

View File

@ -188,11 +188,12 @@ ppModule lmod@(L loc m@(HsModule _name _exports _imports decls _ _)) = do
in (anns', post) in (anns', post)
MultiRWSS.withMultiReader anns' $ processDefault emptyModule MultiRWSS.withMultiReader anns' $ processDefault emptyModule
decls `forM_` ppDecl decls `forM_` ppDecl
let finalComments = filter ( fst .> \case let finalComments = filter
ExactPrint.Types.AnnComment{} -> True ( fst .> \case
_ -> False ExactPrint.Types.AnnComment{} -> True
) _ -> False
post )
post
post `forM_` \case post `forM_` \case
(ExactPrint.Types.AnnComment (ExactPrint.Types.Comment cmStr _ _), l) -> do (ExactPrint.Types.AnnComment (ExactPrint.Types.Comment cmStr _ _), l) -> do
ppmMoveToExactLoc l ppmMoveToExactLoc l
@ -257,39 +258,34 @@ layoutBriDoc :: Data.Data.Data ast => ast -> BriDocNumbered -> PPM ()
layoutBriDoc ast briDoc = do layoutBriDoc ast briDoc = do
-- first step: transform the briDoc. -- first step: transform the briDoc.
briDoc' <- MultiRWSS.withMultiStateS BDEmpty $ do 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 traceIfDumpConf "bridoc raw" _dconf_dump_bridoc_raw
$ briDocToDoc $ briDocToDoc
$ unwrapBriDocNumbered $ unwrapBriDocNumbered
$ briDoc $ briDoc
-- bridoc transformation: remove alts -- bridoc transformation: remove alts
transformAlts briDoc >>= mSet transformAlts briDoc >>= mSet
mGet mGet >>= briDocToDoc .> traceIfDumpConf "bridoc post-alt"
>>= traceIfDumpConf "bridoc post-alt" _dconf_dump_bridoc_simpl_alt _dconf_dump_bridoc_simpl_alt
. briDocToDoc
-- bridoc transformation: float stuff in -- bridoc transformation: float stuff in
mGet <&> transformSimplifyFloating >>= mSet mGet >>= transformSimplifyFloating .> mSet
mGet mGet >>= briDocToDoc .> traceIfDumpConf "bridoc post-floating"
>>= traceIfDumpConf "bridoc post-floating" _dconf_dump_bridoc_simpl_floating
_dconf_dump_bridoc_simpl_floating
. briDocToDoc
-- bridoc transformation: par removal -- bridoc transformation: par removal
mGet <&> transformSimplifyPar >>= mSet mGet >>= transformSimplifyPar .> mSet
mGet mGet >>= briDocToDoc .> traceIfDumpConf "bridoc post-par"
>>= traceIfDumpConf "bridoc post-par" _dconf_dump_bridoc_simpl_par _dconf_dump_bridoc_simpl_par
. briDocToDoc
-- bridoc transformation: float stuff in -- bridoc transformation: float stuff in
mGet <&> transformSimplifyColumns >>= mSet mGet >>= transformSimplifyColumns .> mSet
mGet mGet >>= briDocToDoc .> traceIfDumpConf "bridoc post-columns"
>>= traceIfDumpConf "bridoc post-columns" _dconf_dump_bridoc_simpl_columns _dconf_dump_bridoc_simpl_columns
. briDocToDoc -- bridoc transformation: indent
-- -- bridoc transformation: indent mGet >>= transformSimplifyIndent .> mSet
mGet <&> transformSimplifyIndent >>= mSet mGet >>= briDocToDoc .> traceIfDumpConf "bridoc post-indent"
mGet _dconf_dump_bridoc_simpl_indent
>>= traceIfDumpConf "bridoc post-indent" _dconf_dump_bridoc_simpl_indent mGet >>= briDocToDoc .> traceIfDumpConf "bridoc final"
. briDocToDoc _dconf_dump_bridoc_final
mGet
>>= traceIfDumpConf "bridoc final" _dconf_dump_bridoc_final
. briDocToDoc
-- -- convert to Simple type -- -- convert to Simple type
-- simpl <- mGet <&> transformToSimple -- simpl <- mGet <&> transformToSimple
-- return simpl -- return simpl
@ -317,10 +313,9 @@ layoutBriDoc ast briDoc = do
state' <- MultiRWSS.withMultiStateS state $ layoutBriDocM briDoc' state' <- MultiRWSS.withMultiStateS state $ layoutBriDocM briDoc'
let let remainingComments =
remainingComments = extractAllComments =<< Map.elems (_lstate_comments state')
extractAllComments =<< Map.elems (_lstate_comments state')
remainingComments remainingComments
`forM_` (mTell . (:[]) . LayoutErrorUnusedComment . show . fst) `forM_` (fst .> show .> LayoutErrorUnusedComment .> (:[]) .> mTell)
return $ () return $ ()