{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} module Extract.Print where -- pretty-printer generated by the BNF converter import Extract.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 prPrec i 0 (concatD [prt 0 defs]) instance Print Def where prt i e = case e of Paradigm id env head logic -> prPrec i 0 (concatD [doc (showString "paradigm") , prt 0 id , prt 0 env , doc (showString "=") , prt 0 head , doc (showString "{") , prt 0 logic , doc (showString "}")]) RParadigm id env head logic -> prPrec i 0 (concatD [doc (showString "rule") , prt 0 id , prt 0 env , doc (showString "=") , prt 0 head , doc (showString "{") , prt 0 logic , doc (showString "}")]) Regdef id reg -> prPrec i 0 (concatD [doc (showString "regexp") , prt 0 id , doc (showString "=") , prt 0 reg]) Cxtdef id clogic -> prPrec i 0 (concatD [doc (showString "context") , prt 0 id , doc (showString "=") , prt 0 clogic]) prtList es = case es of [] -> (concatD []) x:xs -> (concatD [prt 0 x , doc (showString ";") , prt 0 xs]) instance Print Env where prt i e = case e of Env bindings -> prPrec i 0 (concatD [doc (showString "[") , prt 0 bindings , doc (showString "]")]) Empty -> prPrec i 0 (concatD []) instance Print Binding where prt i e = case e of Assoc id reg -> prPrec i 0 (concatD [prt 0 id , doc (showString ":") , prt 0 reg]) prtList es = case es of [] -> (concatD []) [x] -> (concatD [prt 0 x]) x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs]) instance Print Pattern where prt i e = case e of CWord items constraint -> prPrec i 0 (concatD [prt 0 items , prt 0 constraint]) prtList es = case es of [x] -> (concatD [prt 0 x]) x:xs -> (concatD [prt 0 x , prt 0 xs]) instance Print Item where prt i e = case e of StrC str -> prPrec i 0 (concatD [prt 0 str]) Var id -> prPrec i 0 (concatD [prt 0 id]) prtList es = case es of [x] -> (concatD [prt 0 x]) x:xs -> (concatD [prt 0 x , doc (showString "+") , prt 0 xs]) instance Print Patt where prt i e = case e of PWild -> prPrec i 1 (concatD [doc (showString "_")]) PId id -> prPrec i 1 (concatD [prt 0 id]) PC id patts -> prPrec i 0 (concatD [prt 0 id , prt 1 patts]) prtList es = case es of [x] -> (concatD [prt 1 x]) x:xs -> (concatD [prt 1 x , prt 1 xs]) instance Print Head where prt i e = case e of Head patterns -> prPrec i 0 (concatD [prt 0 patterns]) instance Print Constraint where prt i e = case e of Cons clogic -> prPrec i 0 (concatD [doc (showString "[") , prt 0 clogic , doc (showString "]")]) ConsId id -> prPrec i 0 (concatD [doc (showString "[") , prt 0 id , doc (showString "]")]) NoCons -> prPrec i 0 (concatD []) instance Print CLogic where prt i e = case e of CConj clogic0 clogic -> prPrec i 0 (concatD [prt 0 clogic0 , doc (showString "&") , prt 1 clogic]) CDisj clogic0 clogic -> prPrec i 0 (concatD [prt 0 clogic0 , doc (showString "|") , prt 1 clogic]) CNeg clogic -> prPrec i 1 (concatD [doc (showString "~") , prt 1 clogic]) CLWild -> prPrec i 1 (concatD [doc (showString "_")]) CAtom position reg unique patt -> prPrec i 1 (concatD [doc (showString "(") , prt 0 position , doc (showString ",") , prt 0 reg , doc (showString ",") , prt 0 unique , prt 0 patt , doc (showString ")")]) instance Print Unique where prt i e = case e of U -> prPrec i 0 (concatD [doc (showString "!")]) NU -> prPrec i 0 (concatD []) instance Print Position where prt i e = case e of WPos -> prPrec i 0 (concatD [doc (showString "_")]) Pos pos -> prPrec i 0 (concatD [prt 0 pos]) VPos id pos -> prPrec i 0 (concatD [prt 0 id , doc (showString "@") , prt 0 pos]) RVPos id0 id pos -> prPrec i 0 (concatD [prt 0 id0 , doc (showString "@") , prt 0 id , prt 0 pos]) Rel id pos -> prPrec i 0 (concatD [prt 0 id , prt 0 pos]) instance Print Pos where prt i e = case e of P n -> prPrec i 0 (concatD [prt 0 n]) PP n -> prPrec i 0 (concatD [doc (showString "+") , prt 0 n]) NP n -> prPrec i 0 (concatD [doc (showString "-") , prt 0 n]) PStar n -> prPrec i 0 (concatD [doc (showString "+") , prt 0 n , doc (showString "*")]) NPStar n -> prPrec i 0 (concatD [doc (showString "-") , prt 0 n , doc (showString "*")]) Star -> prPrec i 0 (concatD [doc (showString "*")]) instance Print Logic where prt i e = case e of Conj logic0 logic -> prPrec i 0 (concatD [prt 0 logic0 , doc (showString "&") , prt 1 logic]) Disj logic0 logic -> prPrec i 0 (concatD [prt 0 logic0 , doc (showString "|") , prt 1 logic]) Neg logic -> prPrec i 1 (concatD [doc (showString "~") , prt 1 logic]) LWild -> prPrec i 1 (concatD [doc (showString "_")]) Atom pattern -> prPrec i 1 (concatD [prt 0 pattern]) instance Print Reg where prt i e = case e of RAlt reg0 reg -> prPrec i 0 (concatD [prt 0 reg0 , doc (showString "|") , prt 1 reg]) RMinus reg0 reg -> prPrec i 0 (concatD [prt 1 reg0 , doc (showString "-") , prt 1 reg]) RSeq reg0 reg -> prPrec i 1 (concatD [prt 1 reg0 , prt 2 reg]) RStar reg -> prPrec i 2 (concatD [prt 2 reg , doc (showString "*")]) RPlus reg -> prPrec i 2 (concatD [prt 2 reg , doc (showString "+")]) ROpt reg -> prPrec i 2 (concatD [prt 2 reg , doc (showString "?")]) REps -> prPrec i 2 (concatD [doc (showString "eps")]) RChar c -> prPrec i 2 (concatD [prt 0 c]) RAlts str -> prPrec i 2 (concatD [doc (showString "[") , prt 0 str , doc (showString "]")]) RDigit -> prPrec i 2 (concatD [doc (showString "digit")]) RLetter -> prPrec i 2 (concatD [doc (showString "letter")]) RUpper -> prPrec i 2 (concatD [doc (showString "upper")]) RLower -> prPrec i 2 (concatD [doc (showString "lower")]) RAny -> prPrec i 2 (concatD [doc (showString "char")]) Wild -> prPrec i 2 (concatD [doc (showString "_")]) RStr str -> prPrec i 2 (concatD [prt 0 str]) RegVar id -> prPrec i 2 (concatD [prt 0 id])