PPrinter 0.0.4 → 0.1.0
raw patch · 3 files changed
+27/−74 lines, 3 files
Files
- LICENSE +2/−2
- PPrinter.cabal +1/−1
- Text/PPrinter.hs +24/−71
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2016, Yi ZHen+Copyright (c) 2016, Yi Zhen All rights reserved. @@ -13,7 +13,7 @@ disclaimer in the documentation and/or other materials provided with the distribution. - * Neither the name of Yi ZHen nor the names of other+ * Neither the name of Yi Zhen nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission.
PPrinter.cabal view
@@ -10,7 +10,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.0.4+version: 0.1.0 -- A short (one-line) description of the package. synopsis: A generic derivable Haskell pretty printer
Text/PPrinter.hs view
@@ -1,37 +1,15 @@-{-# LANGUAGE TypeOperators, FlexibleInstances, FlexibleContexts, DefaultSignatures #-}+-- This library is also available at https://hackage.haskell.org/package/PPrinter --------------------------------------------------------------------------------- |--- Module : Text.PPrinter--- Copyright : (c) The University of Edinburgh 2016--- License : BSD-style (see the file LICENSE)------ Maintainer : Yi Zhen <s1563190@sms.ed.ac.uk>--- Stability : Unknown--- Portability : portable------ Provides a collection of pretty printer combinators, a set of API's that--- provides a way to easily print out text in a consistent format.------ Originally designed by Philip Wadler's.------ For more information you can refer to the--- <http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf original paper>--- that serves as the basis for this libraries design: A prettier printer,--- by Philip Wadler, 2003.---------------------------------------------------------------------------------+{-# LANGUAGE DeriveGeneric, TypeOperators, FlexibleInstances, FlexibleContexts, DefaultSignatures #-} module Text.PPrinter ( Pretty(..),+ Style(..), -- Instances for Pretty: (), Bool, Ordering, Int, Integer, Char, String, Float, Double -- Pretty support code- printer, printLen, fullPrinter,- pprint, pshow, pretty,- (<>), nil, nest, text, line, group, parens, layout,- char, rep,+ pprint, pprintLen, pprintStyle, Generic ) where @@ -169,14 +147,13 @@ -- add whitespace addWhitespace :: [DOC] -> [DOC] addWhitespace [] = []- addWhitespace s@(x:xs)- | flag = if flag then map (nest 1) (line : s) else line : s- | otherwise = map (nest $ white + 1 + (if flag then 1 else 0)) (line : s)+ addWhitespace m@(x:xs)+ | paren == 0 = if flag then map (nest 1) (line : m) else line : m+ | otherwise = map (nest $ white + 1 + (if flag then 1 else 0)) (line : m) where- len x = length (pretty oneLayout 1 x)- sa = pretty oneLayout (len x) x- sb = pretty oneLayout (len x) (head gppa)- parens = length $ takeWhile (== '(') sa+ sa = Prelude.filter (\x -> x /= '\n') $ pretty layout 1 x+ sb = Prelude.filter (\x -> x /= '\n') $ pretty layout 1 (head gppa)+ paren = length $ takeWhile (== '(') sa white = length $ takeWhile( /= ' ') (dropWhile(== '(') sb) nullary _ = False@@ -205,7 +182,7 @@ case conFixity c of Prefix -> wrapParens checkIfWrap $ text (conName c) <> whiteSpace- : addWhitespace checkIfWrap (wrapRecord (gpp t 11 b a)) -- always wrap parens+ : addWhitespace checkIfWrap (wrapRecord (gpp t 11 b a)) Infix _ l -> wrapParens (d > l) $ gpp t (l + 1) (d > l) a where@@ -245,30 +222,6 @@ nullary (M1 x) = nullary x --- | Conversion of values to pretty printable 'String's------ Derived instances of 'Pretty' have the following properties------ * The result of 'ppPrec' is a syntactically correct Haskell--- expression containing only constants, given the fixity--- declarations in force at the point where the type is declared.--- It contains only the constructor names defined in the data type,--- parentheses, and spaces. When labelled constructor fields are--- used, braces, commas, field names, and equal signs are also used.------ * the representation will be enclosed in parentheses if the--- precedence of the top-level constructor in @x@ is less than @d@--- (associativity is ignored). Thus, if @d@ is @0@ then the result--- is never surrounded in parentheses; if @d@ is @11@ it is always--- surrounded in parentheses, unless it is an atomic expression.------ * If the constructor is defined to be an infix operator, then--- 'ppPrec' will produce infix applications of the constructor.------ * If the constructor is defined using record syntax, then 'ppPrec'--- will produce the record-syntax form, with the fields given in the--- same order as the original declaration.--- class Pretty a where @@ -289,7 +242,7 @@ -- helper function for generating a DOC list genList :: [a] -> DOC genList [] = nil- genList (x:xs) = text "," <>+ genList (x:xs) = comma <> line <> whiteSpace <> nest indent (pp x) <> genList xs@@ -306,7 +259,7 @@ instance Pretty () where- pp () = group $ text "()"+ pp () = text "()" ppPrec _ = pp instance Pretty Bool where@@ -487,14 +440,14 @@ pshow :: Pretty a => (Doc -> String) -> Int -> a -> String pshow f w x = pretty f w (pp x <> line) -pprint :: Pretty a => Int -> a -> IO()-pprint w x = putStr (pshow layout w x)+pprinter :: Pretty a => Int -> a -> IO()+pprinter w x = putStr (pshow layout w x) ------------------------------------------------------------- -- Pretty Printer ------------------------------------------------------------- -data Mode = DefaultMode | OneLineMode+data Mode = ManyLineMode | OneLineMode -- | A rendering style data Style = Style { mode :: Mode, -- ^ The redering mode@@ -509,7 +462,7 @@ -- | The default 'Style' style :: Style-style = Style {mode = DefaultMode, lineLen = 40}+style = Style {mode = ManyLineMode, lineLen = 40} render :: Show a => Pretty a => a -> String fullRender :: Show a => Pretty a =>@@ -517,18 +470,18 @@ -> Int -> a -> String-fullRender DefaultMode w x = pshow layout w x+fullRender ManyLineMode w x = pshow layout w x fullRender OneLineMode w x = pshow oneLayout w x -- use default style render = fullRender (styleMode style) (styleLen style) -printer :: Show a => Pretty a => a -> IO()-printer x = putStr (render x)+pprint :: Show a => Pretty a => a -> IO()+pprint x = putStr (render x) -printLen :: Show a => Pretty a => Int -> a -> IO()-printLen = pprint+pprintLen :: Show a => Pretty a => Int -> a -> IO()+pprintLen = pprinter -- | The default Pretty Printer-fullPrinter :: Show a => Pretty a => Style -> a -> IO()-fullPrinter s x = putStr $ fullRender (styleMode s) (styleLen s) x+pprintStyle :: Show a => Pretty a => Style -> a -> IO()+pprintStyle s x = putStr $ fullRender (styleMode s) (styleLen s) x