pretty-simple-3.2.3.0: pretty printer for data types with a 'Show' instance.

Copyright | (c) Dennis Gosnell 2016
License | BSD-style (see LICENSE file)
Maintainer | cdep.illabout@gmail.com
Stability | experimental
Portability | POSIX
Safe Haskell | None
Language | Haskell2010

Text.Pretty.Simple.Internal.ExprToOutput

Description

Synopsis

- newtype LineNum = LineNum {
  - unLineNum :: Int
  }
- data PrinterState = PrinterState {
  - currLine :: !LineNum
  - nestLevel :: !NestLevel
  }
- printerState :: LineNum -> NestLevel -> PrinterState
- addOutput :: MonadState PrinterState m => OutputType -> m Output
- addOutputs :: MonadState PrinterState m => [OutputType] -> m [Output]
- initPrinterState :: PrinterState
- putSurroundExpr :: MonadState PrinterState m => OutputType -> OutputType -> CommaSeparated [Expr] -> m [Output]
- putCommaSep :: forall m. MonadState PrinterState m => CommaSeparated [Expr] -> m [Output]
- putComma :: MonadState PrinterState m => m [Output]
- doIndent :: MonadState PrinterState m => m [Output]
- newLine :: MonadState PrinterState m => m Output
- newLineAndDoIndent :: MonadState PrinterState m => m [Output]
- addToNestLevel :: MonadState PrinterState m => NestLevel -> m ()
- addToCurrentLine :: MonadState PrinterState m => LineNum -> m ()
- putExpression :: MonadState PrinterState m => Expr -> m [Output]
- runPrinterState :: PrinterState -> [Expr] -> [Output]
- runInitPrinterState :: [Expr] -> [Output]
- expressionsToOutputs :: [Expr] -> [Output]
- modificationsExprList :: [Expr] -> [Expr]
- removeEmptyInnerCommaSeparatedExprList :: [Expr] -> [Expr]
- removeEmptyInnerCommaSeparatedExpr :: Expr -> Expr
- removeEmptyInnerCommaSeparated :: CommaSeparated [Expr] -> CommaSeparated [Expr]
- removeEmptyList :: forall a. [[a]] -> [[a]]

# Documentation

    >>> :set -XOverloadedStrings
    >>> import Control.Monad.State (State)
    >>> :{
    let test :: PrinterState -> State PrinterState [Output] -> [Output]
        test initState state = evalState state initState
        testInit :: State PrinterState [Output] -> [Output]
        testInit = test initPrinterState
    :}

newtype LineNum Source #

Newtype around `Int` to represent a line number. After a newline, the `LineNum` will increase by 1.

Constructors

LineNum |

Fields

- unLineNum :: Int

#### Instances

Instances details

Eq LineNum Source # |
Instance details Defined in Text.Pretty.Simple.Internal.ExprToOutput Methods (==) :: LineNum -> LineNum -> Bool (/=) :: LineNum -> LineNum -> Bool
Data LineNum Source # |

Instance details

Defined in Text.Pretty.Simple.Internal.ExprToOutput

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> LineNum -> c LineNum

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c LineNum

toConstr :: LineNum -> Constr

dataTypeOf :: LineNum -> DataType

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c LineNum)

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c LineNum)

gmapT :: (forall b. Data b => b -> b) -> LineNum -> LineNum

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> LineNum -> r

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> LineNum -> r

gmapQ :: (forall d. Data d => d -> u) -> LineNum -> [u]

gmapQi :: Int -> (forall d. Data d => d -> u) -> LineNum -> u

gmapM :: Monad m => (forall d. Data d => d -> m d) -> LineNum -> m LineNum

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> LineNum -> m LineNum

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> LineNum -> m LineNum

Num LineNum Source # |
Instance details Defined in Text.Pretty.Simple.Internal.ExprToOutput Methods (+) :: LineNum -> LineNum -> LineNum (-) :: LineNum -> LineNum -> LineNum (*) :: LineNum -> LineNum -> LineNum negate :: LineNum -> LineNum abs :: LineNum -> LineNum signum :: LineNum -> LineNum fromInteger :: Integer -> LineNum
Ord LineNum Source # |
Instance details Defined in Text.Pretty.Simple.Internal.ExprToOutput Methods compare :: LineNum -> LineNum -> Ordering (<) :: LineNum -> LineNum -> Bool (<=) :: LineNum -> LineNum -> Bool (>) :: LineNum -> LineNum -> Bool (>=) :: LineNum -> LineNum -> Bool max :: LineNum -> LineNum -> LineNum min :: LineNum -> LineNum -> LineNum
Read LineNum Source # |
Instance details Defined in Text.Pretty.Simple.Internal.ExprToOutput Methods readsPrec :: Int -> ReadS LineNum readList :: ReadS [LineNum] readPrec :: ReadPrec LineNum readListPrec :: ReadPrec [LineNum]
Show LineNum Source # |
Instance details Defined in Text.Pretty.Simple.Internal.ExprToOutput Methods showsPrec :: Int -> LineNum -> ShowS show :: LineNum -> String showList :: [LineNum] -> ShowS
Generic LineNum Source # |
Instance details Defined in Text.Pretty.Simple.Internal.ExprToOutput Associated Types type Rep LineNum :: Type -> Type Methods from :: LineNum -> Rep LineNum x to :: Rep LineNum x -> LineNum
type Rep LineNum Source # |
Instance details Defined in Text.Pretty.Simple.Internal.ExprToOutput type Rep LineNum = D1 ('MetaData "LineNum" "Text.Pretty.Simple.Internal.ExprToOutput" "pretty-simple-3.2.3.0-AL2TRmsgZiFGT8HSl1bmPq" 'True) (C1 ('MetaCons "LineNum" 'PrefixI 'True) (S1 ('MetaSel ('Just "unLineNum") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int)))

data PrinterState Source #

Constructors

PrinterState |

Fields

- currLine :: !LineNum
- nestLevel :: !NestLevel

#### Instances

Instances details

Eq PrinterState Source # |
Instance details Defined in Text.Pretty.Simple.Internal.ExprToOutput Methods (==) :: PrinterState -> PrinterState -> Bool (/=) :: PrinterState -> PrinterState -> Bool
Data PrinterState Source # |

Instance details

Defined in Text.Pretty.Simple.Internal.ExprToOutput

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> PrinterState -> c PrinterState

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c PrinterState

toConstr :: PrinterState -> Constr

dataTypeOf :: PrinterState -> DataType

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c PrinterState)

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c PrinterState)

gmapT :: (forall b. Data b => b -> b) -> PrinterState -> PrinterState

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> PrinterState -> r

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> PrinterState -> r

gmapQ :: (forall d. Data d => d -> u) -> PrinterState -> [u]

gmapQi :: Int -> (forall d. Data d => d -> u) -> PrinterState -> u

gmapM :: Monad m => (forall d. Data d => d -> m d) -> PrinterState -> m PrinterState

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> PrinterState -> m PrinterState

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> PrinterState -> m PrinterState

Show PrinterState Source # |
Instance details Defined in Text.Pretty.Simple.Internal.ExprToOutput Methods showsPrec :: Int -> PrinterState -> ShowS show :: PrinterState -> String showList :: [PrinterState] -> ShowS
Generic PrinterState Source # |
Instance details Defined in Text.Pretty.Simple.Internal.ExprToOutput Associated Types type Rep PrinterState :: Type -> Type Methods from :: PrinterState -> Rep PrinterState x to :: Rep PrinterState x -> PrinterState
type Rep PrinterState Source # |

Instance details

Defined in Text.Pretty.Simple.Internal.ExprToOutput

type Rep PrinterState = D1 ('MetaData "PrinterState" "Text.Pretty.Simple.Internal.ExprToOutput" "pretty-simple-3.2.3.0-AL2TRmsgZiFGT8HSl1bmPq" 'False) (C1 ('MetaCons "PrinterState" 'PrefixI 'True) (S1 ('MetaSel ('Just "currLine") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 LineNum) :*: S1 ('MetaSel ('Just "nestLevel") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 NestLevel)))

printerState :: LineNum -> NestLevel -> PrinterState Source #

Smart-constructor for `PrinterState`.

addOutput :: MonadState PrinterState m => OutputType -> m Output Source #

addOutputs :: MonadState PrinterState m => [OutputType] -> m [Output] Source #

initPrinterState :: PrinterState Source #

putSurroundExpr Source #

Arguments

:: MonadState PrinterState m |
=> OutputType |
-> OutputType |
-> CommaSeparated [Expr] | comma separated inner expression.
-> m [Output] |

Print a surrounding expression (like `[]` or `{}` or `()`).

If the `CommaSeparated` expressions are empty, just print the start and end markers.

    >>> testInit $ putSurroundExpr "[" "]" (CommaSeparated [])
    [Output {outputNestLevel = NestLevel {unNestLevel = 0}, outputOutputType = OutputOpenBracket},Output {outputNestLevel = NestLevel {unNestLevel = 0}, outputOutputType = OutputCloseBracket}]

If there is only one expression, and it will print out on one line, then just print everything all on one line, with spaces around the expressions.

    >>> testInit $ putSurroundExpr "{" "}" (CommaSeparated [[Other "hello"]])
    [Output {outputNestLevel = NestLevel {unNestLevel = 0}, outputOutputType = OutputOpenBrace},Output {outputNestLevel = NestLevel {unNestLevel = 0}, outputOutputType = OutputOther " "},Output {outputNestLevel = NestLevel {unNestLevel = 0}, outputOutputType = OutputOther "hello"},Output {outputNestLevel = NestLevel {unNestLevel = 0}, outputOutputType = OutputOther " "},Output {outputNestLevel = NestLevel {unNestLevel = 0}, outputOutputType = OutputCloseBrace}]

If there is only one expression, but it will print out on multiple lines, then go to newline and print out on multiple lines.

    >>> 1 + 1  -- TODO: Example here.
    2

If there are multiple expressions, then first go to a newline. Print out on multiple lines.

    >>> 1 + 1  -- TODO: Example here.
    2

putCommaSep :: forall m. MonadState PrinterState m => CommaSeparated [Expr] -> m [Output] Source #

putComma :: MonadState PrinterState m => m [Output] Source #

doIndent :: MonadState PrinterState m => m [Output] Source #

newLine :: MonadState PrinterState m => m Output Source #

newLineAndDoIndent :: MonadState PrinterState m => m [Output] Source #

addToNestLevel :: MonadState PrinterState m => NestLevel -> m () Source #

addToCurrentLine :: MonadState PrinterState m => LineNum -> m () Source #

putExpression :: MonadState PrinterState m => Expr -> m [Output] Source #

runPrinterState :: PrinterState -> [Expr] -> [Output] Source #

runInitPrinterState :: [Expr] -> [Output] Source #

expressionsToOutputs :: [Expr] -> [Output] Source #

modificationsExprList :: [Expr] -> [Expr] Source #

A function that performs optimizations and modifications to a list of input `Expr`s.

An sample of an optimization is `removeEmptyInnerCommaSeparatedExprList` which removes empty inner lists in a `CommaSeparated` value.

removeEmptyInnerCommaSeparatedExprList :: [Expr] -> [Expr] Source #

removeEmptyInnerCommaSeparatedExpr :: Expr -> Expr Source #

removeEmptyInnerCommaSeparated :: CommaSeparated [Expr] -> CommaSeparated [Expr] Source #

removeEmptyList :: forall a. [[a]] -> [[a]] Source #

Remove empty lists from a list of lists.

    >>> removeEmptyList [[1,2,3], [], [4,5]]
    [[1,2,3],[4,5]]

    >>> removeEmptyList [[]]
    []

    >>> removeEmptyList [[1]]
    [[1]]

    >>> removeEmptyList [[1,2], [10,20], [100,200]]
    [[1,2],[10,20],[100,200]]