summaryrefslogtreecommitdiff
path: root/src/GF/GFCC/Raw/PrintGFCCRaw.hs
diff options
context:
space:
mode:
authoraarne <aarne@cs.chalmers.se>2007-12-13 22:15:19 +0000
committeraarne <aarne@cs.chalmers.se>2007-12-13 22:15:19 +0000
commit9e0dd0a41a9d386d4cc9ae6bab6fa2e1862e829f (patch)
tree80b8db48188b041e128c45f077157474b5223680 /src/GF/GFCC/Raw/PrintGFCCRaw.hs
parented5a85ce1d48e8a8c4c151c19b5dc3adf55ce4cb (diff)
some generated GFCCRaw files added to darcs; make gf3langs for alltenses
Diffstat (limited to 'src/GF/GFCC/Raw/PrintGFCCRaw.hs')
-rw-r--r--src/GF/GFCC/Raw/PrintGFCCRaw.hs104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/GF/GFCC/Raw/PrintGFCCRaw.hs b/src/GF/GFCC/Raw/PrintGFCCRaw.hs
new file mode 100644
index 000000000..c13908fe1
--- /dev/null
+++ b/src/GF/GFCC/Raw/PrintGFCCRaw.hs
@@ -0,0 +1,104 @@
+{-# OPTIONS -fno-warn-incomplete-patterns #-}
+module GF.GFCC.Raw.PrintGFCCRaw where
+
+-- pretty-printer generated by the BNF converter
+
+import GF.GFCC.Raw.AbsGFCCRaw
+import 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 CId where
+ prt _ (CId i) = doc (showString i)
+
+
+
+instance Print Grammar where
+ prt i e = case e of
+ Grm rexps -> prPrec i 0 (concatD [prt 0 rexps])
+
+
+instance Print RExp where
+ prt i e = case e of
+ App cid rexps -> prPrec i 0 (concatD [doc (showString "(") , prt 0 cid , prt 0 rexps , doc (showString ")")])
+ AId cid -> prPrec i 0 (concatD [prt 0 cid])
+ AInt n -> prPrec i 0 (concatD [prt 0 n])
+ AStr str -> prPrec i 0 (concatD [prt 0 str])
+ AFlt d -> prPrec i 0 (concatD [prt 0 d])
+ AMet -> prPrec i 0 (concatD [doc (showString "?")])
+
+ prtList es = case es of
+ [] -> (concatD [])
+ x:xs -> (concatD [prt 0 x , prt 0 xs])
+
+