ShowHelp.hs 1.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{- sv2v
 - Author: Zachary Snow <zach@zachjs.com>
 - Initial Verilog AST Author: Tom Hawkins <tomahawkins@gmail.com>
 -
 - Helpers for printing AST items
 -}

module Language.SystemVerilog.AST.ShowHelp
    ( showPad
    , showPadBefore
    , indent
    , unlines'
    , commas
    , indentedParenList
15
    , showEither
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    ) where

import Data.List (intercalate)

showPad :: Show t => t -> String
showPad x =
    if str == ""
        then ""
        else str ++ " "
    where str = show x

showPadBefore :: Show t => t -> String
showPadBefore x =
    if str == ""
        then ""
        else " " ++ str
    where str = show x

indent :: String -> String
indent a = '\t' : f a
    where
        f [] = []
        f ('\n' : xs) = "\n\t" ++ f xs
        f (x : xs) = x : f xs

unlines' :: [String] -> String
unlines' = intercalate "\n"

commas :: [String] -> String
commas = intercalate ", "

indentedParenList :: [String] -> String
indentedParenList [] = "()"
indentedParenList [x] = "(" ++ x ++ ")"
indentedParenList l = "(\n" ++ (indent $ intercalate ",\n" l) ++ "\n)"

52 53 54
showEither :: (Show a, Show b) => Either a b -> String
showEither (Left  v) = show v
showEither (Right v) = show v