1
0
forked from GitHub/gf-core
Files
gf-core/src-3.0/GF/Command/PrintGFShell.hs
2008-05-23 13:12:11 +00:00

145 lines
4.1 KiB
Haskell

{-# OPTIONS -fno-warn-incomplete-patterns #-}
module GF.Command.PrintGFShell where
-- pretty-printer generated by the BNF converter
import GF.Command.AbsGFShell
import Data.Char
-- the top-level printing method
printTree :: Print a => a -> String
printTree = render . prt 0
type Doc = [ShowS] -> [ShowS]
doc :: ShowS -> Doc
doc = (:)
render :: Doc -> String
render d = rend 0 (map ($ "") $ d []) "" where
rend i ss = case ss of
"[" :ts -> showChar '[' . rend i ts
"(" :ts -> showChar '(' . rend i ts
"{" :ts -> showChar '{' . new (i+1) . rend (i+1) ts
"}" : ";":ts -> new (i-1) . space "}" . showChar ';' . new (i-1) . rend (i-1) ts
"}" :ts -> new (i-1) . showChar '}' . new (i-1) . rend (i-1) ts
";" :ts -> showChar ';' . new i . rend i ts
t : "," :ts -> showString t . space "," . rend i ts
t : ")" :ts -> showString t . showChar ')' . rend i ts
t : "]" :ts -> showString t . showChar ']' . rend i ts
t :ts -> space t . rend i ts
_ -> id
new i = showChar '\n' . replicateS (2*i) (showChar ' ') . dropWhile isSpace
space t = showString t . (\s -> if null s then "" else (' ':s))
parenth :: Doc -> Doc
parenth ss = doc (showChar '(') . ss . doc (showChar ')')
concatS :: [ShowS] -> ShowS
concatS = foldr (.) id
concatD :: [Doc] -> Doc
concatD = foldr (.) id
replicateS :: Int -> ShowS -> ShowS
replicateS n f = concatS (replicate n f)
-- the printer class does the job
class Print a where
prt :: Int -> a -> Doc
prtList :: [a] -> Doc
prtList = concatD . map (prt 0)
instance Print a => Print [a] where
prt _ = prtList
instance Print Char where
prt _ s = doc (showChar '\'' . mkEsc '\'' s . showChar '\'')
prtList s = doc (showChar '"' . concatS (map (mkEsc '"') s) . showChar '"')
mkEsc :: Char -> Char -> ShowS
mkEsc q s = case s of
_ | s == q -> showChar '\\' . showChar s
'\\'-> showString "\\\\"
'\n' -> showString "\\n"
'\t' -> showString "\\t"
_ -> showChar s
prPrec :: Int -> Int -> Doc -> Doc
prPrec i j = if j<i then parenth else id
instance Print Integer where
prt _ x = doc (shows x)
instance Print Double where
prt _ x = doc (shows x)
instance Print Ident where
prt _ (Ident i) = doc (showString i)
prtList es = case es of
[x] -> (concatD [prt 0 x , doc (showString ",")])
x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
instance Print CommandLine where
prt i e = case e of
CLine pipes -> prPrec i 0 (concatD [prt 0 pipes])
CEmpty -> prPrec i 0 (concatD [])
instance Print Pipe where
prt i e = case e of
PComm commands -> prPrec i 0 (concatD [prt 0 commands])
prtList es = case es of
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString ";") , prt 0 xs])
instance Print Command where
prt i e = case e of
Comm id options argument -> prPrec i 0 (concatD [prt 0 id , prt 0 options , prt 0 argument])
CNoarg id options -> prPrec i 0 (concatD [prt 0 id , prt 0 options])
prtList es = case es of
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString "|") , prt 0 xs])
instance Print Option where
prt i e = case e of
OOpt id -> prPrec i 0 (concatD [doc (showString "-") , prt 0 id])
OFlag id value -> prPrec i 0 (concatD [doc (showString "-") , prt 0 id , doc (showString "=") , prt 0 value])
prtList es = case es of
[] -> (concatD [])
x:xs -> (concatD [prt 0 x , prt 0 xs])
instance Print Value where
prt i e = case e of
VId id -> prPrec i 0 (concatD [prt 0 id])
VInt n -> prPrec i 0 (concatD [prt 0 n])
instance Print Argument where
prt i e = case e of
ATree tree -> prPrec i 0 (concatD [prt 0 tree])
instance Print Tree where
prt i e = case e of
TApp id trees -> prPrec i 1 (concatD [prt 0 id , prt 2 trees])
TAbs ids tree -> prPrec i 0 (concatD [doc (showString "\\") , prt 0 ids , doc (showString "->") , prt 0 tree])
TId id -> prPrec i 2 (concatD [prt 0 id])
TInt n -> prPrec i 2 (concatD [prt 0 n])
TStr str -> prPrec i 2 (concatD [prt 0 str])
TFloat d -> prPrec i 2 (concatD [prt 0 d])
prtList es = case es of
[x] -> (concatD [prt 2 x])
x:xs -> (concatD [prt 2 x , prt 2 xs])