pec-0.1: Language/Pec/Print.hs
{-# OPTIONS -fno-warn-incomplete-patterns #-}
module Language.Pec.Print where
-- pretty-printer generated by the BNF converter
import Language.Pec.Abs
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 Frac where
prt _ (Frac i) = doc (showString ( i))
instance Print Uident where
prt _ (Uident i) = doc (showString ( i))
instance Print Lident where
prt _ (Lident i) = doc (showString ( i))
instance Print USym where
prt _ (USym i) = doc (showString ( i))
instance Print Number where
prt _ (Number i) = doc (showString ( i))
instance Print Count where
prt _ (Count i) = doc (showString ( i))
instance Print Module where
prt i e = case e of
Module modid exportdecl topdecls -> prPrec i 0 (concatD [doc (showString "module") , prt 0 modid , doc (showString "exports") , prt 0 exportdecl , doc (showString "where") , doc (showString "{") , prt 0 topdecls , doc (showString "}")])
instance Print ExportDecl where
prt i e = case e of
ExpAllD -> prPrec i 0 (concatD [doc (showString "all")])
ExpListD exports -> prPrec i 0 (concatD [doc (showString "(") , prt 0 exports , doc (showString ")")])
instance Print Export where
prt i e = case e of
TypeEx con spec -> prPrec i 0 (concatD [prt 0 con , prt 0 spec])
VarEx var -> prPrec i 0 (concatD [prt 0 var])
prtList es = case es of
[] -> (concatD [])
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
instance Print Spec where
prt i e = case e of
Neither -> prPrec i 0 (concatD [])
Decon -> prPrec i 0 (concatD [doc (showString "(") , doc (showString ".") , doc (showString ")")])
Both -> prPrec i 0 (concatD [doc (showString "(") , doc (showString "..") , doc (showString ")")])
instance Print TopDecl where
prt i e = case e of
ImportD modid asspec -> prPrec i 0 (concatD [doc (showString "import") , prt 0 modid , prt 0 asspec])
ExternD extnm var type' -> prPrec i 0 (concatD [doc (showString "extern") , prt 0 extnm , prt 0 var , doc (showString "::") , prt 0 type'])
TypeD con tyvars tydecl -> prPrec i 0 (concatD [doc (showString "type") , prt 0 con , prt 0 tyvars , doc (showString "=") , prt 0 tydecl])
AscribeD var type' -> prPrec i 0 (concatD [prt 0 var , doc (showString "::") , prt 0 type'])
VarD var exp -> prPrec i 0 (concatD [prt 0 var , doc (showString "=") , prt 0 exp])
ProcD var exps exp -> prPrec i 0 (concatD [prt 0 var , prt 0 exps , doc (showString "=") , prt 0 exp])
prtList es = case es of
[] -> (concatD [])
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString ";") , prt 0 xs])
instance Print AsSpec where
prt i e = case e of
AsAS con -> prPrec i 0 (concatD [doc (showString "as") , prt 0 con])
EmptyAS -> prPrec i 0 (concatD [])
instance Print ExtNm where
prt i e = case e of
SomeNm str -> prPrec i 0 (concatD [prt 0 str])
NoneNm -> prPrec i 0 (concatD [])
instance Print Exp where
prt i e = case e of
BlockE exps -> prPrec i 0 (concatD [doc (showString "do") , doc (showString "{") , prt 5 exps , doc (showString "}")])
LetS exp0 exp -> prPrec i 5 (concatD [prt 4 exp0 , doc (showString "=") , prt 0 exp])
LetE exp0 exp1 exp -> prPrec i 5 (concatD [doc (showString "let") , prt 4 exp0 , doc (showString "=") , prt 0 exp1 , doc (showString "in") , prt 0 exp])
StoreE exp0 exp -> prPrec i 5 (concatD [prt 4 exp0 , doc (showString "<-") , prt 0 exp])
CaseE exp casealts -> prPrec i 5 (concatD [doc (showString "case") , prt 0 exp , doc (showString "of") , doc (showString "{") , prt 0 casealts , doc (showString "}")])
BranchE branchalts -> prPrec i 5 (concatD [doc (showString "branch") , doc (showString "of") , doc (showString "{") , prt 0 branchalts , doc (showString "}")])
BinOpE exp0 usym exp -> prPrec i 4 (concatD [prt 3 exp0 , prt 0 usym , prt 3 exp])
AppE exp0 exp -> prPrec i 3 (concatD [prt 3 exp0 , prt 2 exp])
UnOpE unop exp -> prPrec i 2 (concatD [prt 0 unop , prt 1 exp])
IdxE exp0 exp -> prPrec i 1 (concatD [prt 1 exp0 , doc (showString "[") , prt 0 exp , doc (showString "]")])
FldE exp field -> prPrec i 1 (concatD [prt 1 exp , doc (showString ".") , prt 0 field])
ArrayE exps -> prPrec i 0 (concatD [doc (showString "Array") , doc (showString "[") , prt 0 exps , doc (showString "]")])
RecordE fieldds -> prPrec i 0 (concatD [doc (showString "{") , prt 0 fieldds , doc (showString "}")])
TupleE exps -> prPrec i 0 (concatD [doc (showString "(") , prt 0 exps , doc (showString ")")])
AscribeE exp type' -> prPrec i 0 (concatD [doc (showString "(") , prt 0 exp , doc (showString "::") , prt 0 type' , doc (showString ")")])
CountE count -> prPrec i 0 (concatD [prt 0 count])
VarE var -> prPrec i 0 (concatD [prt 0 var])
LitE lit -> prPrec i 0 (concatD [prt 0 lit])
prtList es = case es of
[] -> (concatD [])
[x] -> (concatD [prt 0 x])
[x] -> (concatD [prt 0 x])
[x] -> (concatD [prt 5 x])
x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
x:xs -> (concatD [prt 0 x , prt 0 xs])
x:xs -> (concatD [prt 5 x , doc (showString ";") , prt 5 xs])
instance Print UnOp where
prt i e = case e of
Load -> prPrec i 0 (concatD [doc (showString "@")])
instance Print CaseAlt where
prt i e = case e of
CaseAlt casepat exp -> prPrec i 0 (concatD [prt 0 casepat , doc (showString "->") , prt 0 exp])
prtList es = case es of
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString ";") , prt 0 xs])
instance Print CasePat where
prt i e = case e of
ConP con var -> prPrec i 0 (concatD [prt 0 con , prt 0 var])
LitP lit -> prPrec i 0 (concatD [prt 0 lit])
VarP var -> prPrec i 0 (concatD [prt 0 var])
instance Print BranchAlt where
prt i e = case e of
BranchAlt branchpat exp -> prPrec i 0 (concatD [doc (showString "|") , prt 0 branchpat , doc (showString "->") , prt 0 exp])
prtList es = case es of
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString ";") , prt 0 xs])
instance Print BranchPat where
prt i e = case e of
BoolBP exp -> prPrec i 0 (concatD [prt 4 exp])
DefaultBP -> prPrec i 0 (concatD [])
instance Print Type where
prt i e = case e of
TyFun type'0 type' -> prPrec i 0 (concatD [prt 2 type'0 , doc (showString "->") , prt 0 type'])
TyArray type'0 type' -> prPrec i 2 (concatD [doc (showString "Array") , prt 1 type'0 , prt 1 type'])
TyConstr con types -> prPrec i 2 (concatD [prt 0 con , prt 1 types])
TyTuple types -> prPrec i 0 (concatD [doc (showString "(") , prt 0 types , doc (showString ")")])
TyCount count -> prPrec i 0 (concatD [prt 0 count])
TyVarT tyvar -> prPrec i 0 (concatD [prt 0 tyvar])
TyConstr0 con -> prPrec i 0 (concatD [prt 0 con])
prtList es = case es of
[] -> (concatD [])
[] -> (concatD [])
[x] -> (concatD [prt 0 x])
[x] -> (concatD [prt 1 x])
x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
x:xs -> (concatD [prt 0 x , prt 0 xs])
x:xs -> (concatD [prt 1 x , prt 1 xs])
instance Print TyDecl where
prt i e = case e of
TyRecord fieldts -> prPrec i 0 (concatD [doc (showString "{") , prt 0 fieldts , doc (showString "}")])
TyTagged concs -> prPrec i 0 (concatD [doc (showString "|") , prt 0 concs])
TySyn type' -> prPrec i 0 (concatD [prt 0 type'])
instance Print ConC where
prt i e = case e of
ConC con types -> prPrec i 0 (concatD [prt 0 con , prt 0 types])
prtList es = case es of
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString "|") , prt 0 xs])
instance Print FieldT where
prt i e = case e of
FieldT field type' -> prPrec i 0 (concatD [prt 0 field , doc (showString "::") , prt 0 type'])
prtList es = case es of
[] -> (concatD [])
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
instance Print Lit where
prt i e = case e of
CharL c -> prPrec i 0 (concatD [prt 0 c])
StringL str -> prPrec i 0 (concatD [prt 0 str])
IntL number -> prPrec i 0 (concatD [prt 0 number])
FracL frac -> prPrec i 0 (concatD [prt 0 frac])
EnumL con -> prPrec i 0 (concatD [prt 0 con])
instance Print FieldD where
prt i e = case e of
FieldD field exp -> prPrec i 0 (concatD [prt 0 field , doc (showString "=") , prt 0 exp])
prtList es = case es of
[x] -> (concatD [prt 0 x])
x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
instance Print Var where
prt i e = case e of
Var lident -> prPrec i 0 (concatD [prt 0 lident])
instance Print Con where
prt i e = case e of
Con uident -> prPrec i 0 (concatD [prt 0 uident])
instance Print Modid where
prt i e = case e of
Modid uident -> prPrec i 0 (concatD [prt 0 uident])
instance Print Field where
prt i e = case e of
Field lident -> prPrec i 0 (concatD [prt 0 lident])
instance Print TyVar where
prt i e = case e of
VarTV lident -> prPrec i 0 (concatD [prt 0 lident])
CntTV lident -> prPrec i 0 (concatD [doc (showString "#") , prt 0 lident])
prtList es = case es of
[] -> (concatD [])
x:xs -> (concatD [prt 0 x , prt 0 xs])