{-# OPTIONS_GHC -Wno-orphans #-}
module Morley.Michelson.Printer.Util
( RenderDoc(..)
, printRenderDoc
, renderOpsList
, renderOpsListNoBraces
, wrapInParens
, renderDocList
, RenderContext
, needsParens
, doesntNeedParens
, addParens
, addParensMultiline
, assertParensNotNeeded
) where
import Prelude hiding (group)
import Control.Exception (assert)
import Data.Foldable qualified as Foldable
import Fmt (Buildable(build), FromSimpleDoc(..))
import Fmt.Operators ((<+>), (<//>))
import Fmt.Utils (Doc, SimpleDoc, isEmpty, mkLayoutOptions, renderOneLine)
import Prettyprinter
(align, braces, enclose, encloseSep, group, hsep, layoutPretty, lbracket, parens, punctuate,
rbracket, semi, sep, space)
newtype RenderContext = RenderContext
{ RenderContext -> Bool
_rcWillNeedParens :: Bool
}
class RenderDoc a where
renderDoc :: RenderContext -> a -> Doc
isRenderable :: a -> Bool
isRenderable a
_ = Bool
True
instance RenderDoc Text where
renderDoc :: RenderContext -> Text -> Doc
renderDoc RenderContext
_ = Text -> Doc
forall a. Buildable a => a -> Doc
build
instance RenderDoc Doc where
renderDoc :: RenderContext -> Doc -> Doc
renderDoc = RenderContext -> Doc -> Doc
addParens
renderDocList :: RenderDoc a => RenderContext -> [a] -> Doc
renderDocList :: forall a. RenderDoc a => RenderContext -> [a] -> Doc
renderDocList RenderContext
context = Doc -> Doc
forall ann. Doc ann -> Doc ann
align (Doc -> Doc) -> ([a] -> Doc) -> [a] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Doc -> Doc -> [Doc] -> Doc
forall ann. Doc ann -> Doc ann -> Doc ann -> [Doc ann] -> Doc ann
encloseSep Doc
forall ann. Doc ann
lbracket Doc
forall ann. Doc ann
rbracket Doc
", " ([Doc] -> Doc) -> ([a] -> [Doc]) -> [a] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Doc) -> [a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (RenderContext -> a -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc RenderContext
context)
printRenderDoc :: (RenderDoc a, FromSimpleDoc b) => Bool -> a -> b
printRenderDoc :: forall a b. (RenderDoc a, FromSimpleDoc b) => Bool -> a -> b
printRenderDoc Bool
oneLine = SimpleDocStream () -> b
forall ann. SimpleDocStream ann -> b
forall a ann. FromSimpleDoc a => SimpleDocStream ann -> a
fmtSimple (SimpleDocStream () -> b) -> (a -> SimpleDocStream ()) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Doc -> SimpleDocStream ()
doRender Bool
oneLine (Doc -> SimpleDocStream ())
-> (a -> Doc) -> a -> SimpleDocStream ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenderContext -> a -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc RenderContext
doesntNeedParens
renderOpsList :: (RenderDoc op, Foldable f) => Bool -> f op -> Doc
renderOpsList :: forall op (f :: * -> *).
(RenderDoc op, Foldable f) =>
Bool -> f op -> Doc
renderOpsList Bool
oneLine f op
ops =
Doc -> Doc
forall ann. Doc ann -> Doc ann
braces (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc -> Doc -> Doc
forall ann. Doc ann -> Doc ann -> Doc ann -> Doc ann
enclose Doc
forall ann. Doc ann
space Doc
forall ann. Doc ann
space (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Bool -> f op -> Doc
forall op (f :: * -> *).
(RenderDoc op, Foldable f) =>
Bool -> f op -> Doc
renderOpsListNoBraces Bool
oneLine f op
ops
renderOpsListNoBraces :: (RenderDoc op, Foldable f) => Bool -> f op -> Doc
renderOpsListNoBraces :: forall op (f :: * -> *).
(RenderDoc op, Foldable f) =>
Bool -> f op -> Doc
renderOpsListNoBraces Bool
oneLine =
Doc -> Doc
forall ann. Doc ann -> Doc ann
align (Doc -> Doc) -> (f op -> Doc) -> f op -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (if Bool
oneLine then [Doc] -> Doc
forall ann. [Doc ann] -> Doc ann
hsep else [Doc] -> Doc
forall ann. [Doc ann] -> Doc ann
sep) ([Doc] -> Doc) -> (f op -> [Doc]) -> f op -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> [Doc] -> [Doc]
forall ann. Doc ann -> [Doc ann] -> [Doc ann]
punctuate Doc
forall ann. Doc ann
semi ([Doc] -> [Doc]) -> (f op -> [Doc]) -> f op -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(op -> Doc) -> [op] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (RenderContext -> op -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc RenderContext
doesntNeedParens) ([op] -> [Doc]) -> (f op -> [op]) -> f op -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (op -> Bool) -> [op] -> [op]
forall a. (a -> Bool) -> [a] -> [a]
filter op -> Bool
forall a. RenderDoc a => a -> Bool
isRenderable ([op] -> [op]) -> (f op -> [op]) -> f op -> [op]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f op -> [op]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
wrapInParens :: RenderContext -> NonEmpty Doc -> Doc
wrapInParens :: RenderContext -> NonEmpty Doc -> Doc
wrapInParens (RenderContext Bool
pn) NonEmpty Doc
ds =
let moreThanOne :: Bool
moreThanOne = [Doc] -> Int
forall i a.
(Integral i, Container a,
DefaultToInt (IsIntSubType Length i) i) =>
a -> i
length ((Doc -> Bool) -> [Doc] -> [Doc]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
forall a. Boolean a => a -> a
not (Bool -> Bool) -> (Doc -> Bool) -> Doc -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Bool
isEmpty) (NonEmpty Doc -> [Element (NonEmpty Doc)]
forall t. Container t => t -> [Element t]
toList NonEmpty Doc
ds)) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1
in RenderContext -> Doc -> Doc
addParens (Bool -> RenderContext
RenderContext (Bool
pn Bool -> Bool -> Bool
forall a. Boolean a => a -> a -> a
&& Bool
moreThanOne)) (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
(Element (NonEmpty Doc) -> Doc -> Doc)
-> Doc -> NonEmpty Doc -> Doc
forall t b. Container t => (Element t -> b -> b) -> b -> t -> b
forall b.
(Element (NonEmpty Doc) -> b -> b) -> b -> NonEmpty Doc -> b
foldr Doc -> Doc -> Doc
Element (NonEmpty Doc) -> Doc -> Doc
(<+>) Doc
forall a. Monoid a => a
mempty NonEmpty Doc
ds
instance {-# overlappable #-} RenderDoc a => Buildable a where
build :: a -> Doc
build = Doc -> Doc
forall ann. Doc ann -> Doc ann
align (Doc -> Doc) -> (a -> Doc) -> a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Doc
forall ann. Doc ann -> Doc ann
group (Doc -> Doc) -> (a -> Doc) -> a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenderContext -> a -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc RenderContext
doesntNeedParens
doRender :: Bool -> Doc -> SimpleDoc
doRender :: Bool -> Doc -> SimpleDocStream ()
doRender Bool
oneLine = if Bool
oneLine then Doc -> SimpleDocStream ()
renderOneLine else LayoutOptions -> Doc -> SimpleDocStream ()
forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutPretty (LayoutOptions -> Doc -> SimpleDocStream ())
-> LayoutOptions -> Doc -> SimpleDocStream ()
forall a b. (a -> b) -> a -> b
$ Double -> Int -> LayoutOptions
mkLayoutOptions Double
1.0 Int
80
needsParens, doesntNeedParens :: RenderContext
needsParens :: RenderContext
needsParens = Bool -> RenderContext
RenderContext Bool
True
doesntNeedParens :: RenderContext
doesntNeedParens = Bool -> RenderContext
RenderContext Bool
False
addParens :: RenderContext -> Doc -> Doc
addParens :: RenderContext -> Doc -> Doc
addParens = \case
RenderContext Bool
True -> Doc -> Doc
forall ann. Doc ann -> Doc ann
parens
RenderContext Bool
False -> Doc -> Doc
forall a. a -> a
id
addParensMultiline :: RenderContext -> Doc -> Doc
addParensMultiline :: RenderContext -> Doc -> Doc
addParensMultiline RenderContext
pn Doc
doc = case RenderContext
pn of
RenderContext Bool
True -> Doc
"(" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
doc Doc -> Doc -> Doc
<//> Doc
")"
RenderContext Bool
False -> Doc
doc
assertParensNotNeeded :: RenderContext -> a -> a
assertParensNotNeeded :: forall a. RenderContext -> a -> a
assertParensNotNeeded (RenderContext Bool
pn) = Bool -> a -> a
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Bool -> Bool
forall a. Boolean a => a -> a
not Bool
pn)