{-# LANGUAGE CPP #-} #if __GLASGOW_HASKELL__ <= 708 {-# LANGUAGE OverlappingInstances #-} #endif {-# LANGUAGE FlexibleInstances #-} {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} -- | Pretty-printer for MiniJS. -- Generated by the BNF converter. module MiniJS.Print where import qualified MiniJS.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 [";"] -> showChar ';' ";" :ts -> showChar ';' . new i . rend i ts t : ts@(p:_) | closingOrPunctuation p -> showString t . 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) closingOrPunctuation :: String -> Bool closingOrPunctuation [c] = c `elem` closerOrPunct closingOrPunctuation _ = False closerOrPunct :: String closerOrPunct = ")],;" 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 :: Int -> [a] -> Doc prtList i = concatD . map (prt i) instance {-# OVERLAPPABLE #-} 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 MiniJS.Abs.Ident where prt _ (MiniJS.Abs.Ident i) = doc $ showString $ i instance Print MiniJS.Abs.Program where prt i e = case e of MiniJS.Abs.Prg stms -> prPrec i 0 (concatD [prt 0 stms]) instance Print MiniJS.Abs.Stm where prt i e = case e of MiniJS.Abs.SAssign id exp -> prPrec i 0 (concatD [prt 0 id, doc (showString "="), prt 0 exp]) MiniJS.Abs.SPrint exp -> prPrec i 0 (concatD [doc (showString "console.log"), doc (showString "("), prt 0 exp, doc (showString ")")]) prtList _ [] = concatD [] prtList _ (x:xs) = concatD [prt 0 x, doc (showString ";"), prt 0 xs] instance Print [MiniJS.Abs.Stm] where prt = prtList instance Print MiniJS.Abs.Exp where prt i e = case e of MiniJS.Abs.EVar id -> prPrec i 2 (concatD [prt 0 id]) MiniJS.Abs.EInt n -> prPrec i 2 (concatD [prt 0 n]) MiniJS.Abs.EDouble d -> prPrec i 2 (concatD [prt 0 d]) MiniJS.Abs.ETimes exp1 exp2 -> prPrec i 1 (concatD [prt 1 exp1, doc (showString "*"), prt 2 exp2]) MiniJS.Abs.EDiv exp1 exp2 -> prPrec i 1 (concatD [prt 1 exp1, doc (showString "/"), prt 2 exp2]) MiniJS.Abs.EPlus exp1 exp2 -> prPrec i 0 (concatD [prt 0 exp1, doc (showString "+"), prt 1 exp2]) MiniJS.Abs.EMinus exp1 exp2 -> prPrec i 0 (concatD [prt 0 exp1, doc (showString "-"), prt 1 exp2])