Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Definitions to write renderers based on looking at a SimpleDocStream
as
an instruction tape for a stack machine: text is written, annotations are
added (pushed) and later removed (popped).
Synopsis
- renderSimplyDecorated :: Monoid out => (Text -> out) -> (ann -> out) -> (ann -> out) -> SimpleDocStream ann -> out
- renderSimplyDecoratedA :: (Applicative f, Monoid out) => (Text -> f out) -> (ann -> f out) -> (ann -> f out) -> SimpleDocStream ann -> f out
- data StackMachine output style a
- execStackMachine :: [styles] -> StackMachine output styles a -> (output, [styles])
- pushStyle :: Monoid output => style -> StackMachine output style ()
- unsafePopStyle :: Monoid output => StackMachine output style style
- unsafePeekStyle :: Monoid output => StackMachine output style style
- writeOutput :: output -> StackMachine output style ()
Simple, pre-defined stack machines
These cover most basic use cases where there is not too much special logic, and all that’s important is how to render text, and how to add/remove an annotation.
renderSimplyDecorated Source #
:: Monoid out | |
=> (Text -> out) | Render plain |
-> (ann -> out) | How to render an annotation |
-> (ann -> out) | How to render the removed annotation |
-> SimpleDocStream ann | |
-> out |
Simplest possible stack-based renderer.
For example, here is a document annotated with ()
, and the behaviour is to
write »>>>« at the beginning, and »<<<« at the end of the annotated region:
>>>
let doc = "hello" <+> annotate () "world" <> "!"
>>>
let sdoc = layoutPretty defaultLayoutOptions doc
>>>
T.putStrLn (renderSimplyDecorated id (\() -> ">>>") (\() -> "<<<") sdoc)
hello >>>world<<<!
The monoid will be concatenated in a right associative fashion.
renderSimplyDecoratedA Source #
:: (Applicative f, Monoid out) | |
=> (Text -> f out) | Render plain |
-> (ann -> f out) | How to render an annotation |
-> (ann -> f out) | How to render the removed annotation |
-> SimpleDocStream ann | |
-> f out |
Version of renderSimplyDecoratedA
that allows for Applicative
effects.
General stack machine
These definitions allow defining a full-blown stack machine renderer, allowing for arbitrary peeking, popping and what not.
data StackMachine output style a Source #
Deprecated: Writing your own stack machine is probably more efficient and customizable; also consider using »renderSimplyDecorated(A)« instead
WriterT output StateT [style] a
, but with a strict Writer value.
The output
type is used to append data chunks to, the style
is the member
of a stack of styles to model nested styles with.
Instances
Monoid output => Monad (StackMachine output style) Source # | |
Defined in Prettyprinter.Render.Util.StackMachine (>>=) :: StackMachine output style a -> (a -> StackMachine output style b) -> StackMachine output style b # (>>) :: StackMachine output style a -> StackMachine output style b -> StackMachine output style b # return :: a -> StackMachine output style a # fail :: String -> StackMachine output style a # | |
Functor (StackMachine output style) Source # | |
Defined in Prettyprinter.Render.Util.StackMachine fmap :: (a -> b) -> StackMachine output style a -> StackMachine output style b # (<$) :: a -> StackMachine output style b -> StackMachine output style a # | |
Monoid output => Applicative (StackMachine output style) Source # | |
Defined in Prettyprinter.Render.Util.StackMachine pure :: a -> StackMachine output style a # (<*>) :: StackMachine output style (a -> b) -> StackMachine output style a -> StackMachine output style b # liftA2 :: (a -> b -> c) -> StackMachine output style a -> StackMachine output style b -> StackMachine output style c # (*>) :: StackMachine output style a -> StackMachine output style b -> StackMachine output style b # (<*) :: StackMachine output style a -> StackMachine output style b -> StackMachine output style a # |
execStackMachine :: [styles] -> StackMachine output styles a -> (output, [styles]) Source #
Run the renderer and retrive the writing end
pushStyle :: Monoid output => style -> StackMachine output style () Source #
Add a new style to the style stack.
unsafePopStyle :: Monoid output => StackMachine output style style Source #
Get the topmost style.
If the stack is empty, this raises an error
.
unsafePeekStyle :: Monoid output => StackMachine output style style Source #
View the topmost style, but do not modify the stack.
If the stack is empty, this raises an error
.
writeOutput :: output -> StackMachine output style () Source #
Append a value to the output end.