summaryrefslogtreecommitdiff
path: root/src/GF/CFGM/PrintCFG.hs
diff options
context:
space:
mode:
authoraarne <aarne@cs.chalmers.se>2008-06-25 16:43:48 +0000
committeraarne <aarne@cs.chalmers.se>2008-06-25 16:43:48 +0000
commitb96b36f43de3e2f8b58d5f539daa6f6d47f25870 (patch)
tree0992334be13cec6538a1dea22fbbf26ad6bdf224 /src/GF/CFGM/PrintCFG.hs
parentfe367412e0aeb4ad5c02de68e6eca382e0f96984 (diff)
removed src for 2.9
Diffstat (limited to 'src/GF/CFGM/PrintCFG.hs')
-rw-r--r--src/GF/CFGM/PrintCFG.hs157
1 files changed, 0 insertions, 157 deletions
diff --git a/src/GF/CFGM/PrintCFG.hs b/src/GF/CFGM/PrintCFG.hs
deleted file mode 100644
index 0fd46239c..000000000
--- a/src/GF/CFGM/PrintCFG.hs
+++ /dev/null
@@ -1,157 +0,0 @@
-module GF.CFGM.PrintCFG where
-
--- pretty-printer generated by the BNF converter
-
-import GF.CFGM.AbsCFG
-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)
- prtList es = case es of
- [] -> (concatD [])
- [x] -> (concatD [prt 0 x])
- x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
-
-
-instance Print Double where
- prt _ x = doc (shows x)
-
-
-instance Print Ident where
- prt _ (Ident i) = doc (showString i)
-
-
-instance Print SingleQuoteString where
- prt _ (SingleQuoteString i) = doc (showString i)
-
-
-
-instance Print Grammars where
- prt i e = case e of
- Grammars grammars -> prPrec i 0 (concatD [prt 0 grammars])
-
-
-instance Print Grammar where
- prt i e = case e of
- Grammar id flags rules -> prPrec i 0 (concatD [doc (showString "grammar") , prt 0 id , prt 0 flags , prt 0 rules , doc (showString "end") , doc (showString "grammar")])
-
- prtList es = case es of
- [] -> (concatD [])
- x:xs -> (concatD [prt 0 x , prt 0 xs])
-
-instance Print Flag where
- prt i e = case e of
- StartCat category -> prPrec i 0 (concatD [doc (showString "startcat") , prt 0 category])
-
- prtList es = case es of
- [] -> (concatD [])
- x:xs -> (concatD [prt 0 x , doc (showString ";") , prt 0 xs])
-
-instance Print Rule where
- prt i e = case e of
- Rule fun profiles category symbols -> prPrec i 0 (concatD [prt 0 fun , doc (showString ":") , prt 0 profiles , doc (showString ".") , prt 0 category , doc (showString "->") , prt 0 symbols])
-
- prtList es = case es of
- [] -> (concatD [])
- x:xs -> (concatD [prt 0 x , doc (showString ";") , prt 0 xs])
-
-instance Print Fun where
- prt i e = case e of
- Cons id -> prPrec i 0 (concatD [prt 0 id])
- Coerce -> prPrec i 0 (concatD [doc (showString "_")])
-
-
-instance Print Profiles where
- prt i e = case e of
- Profiles profiles -> prPrec i 0 (concatD [doc (showString "[") , prt 0 profiles , doc (showString "]")])
-
-
-instance Print Profile where
- prt i e = case e of
- UnifyProfile ns -> prPrec i 0 (concatD [doc (showString "[") , prt 0 ns , doc (showString "]")])
- ConstProfile id -> prPrec i 0 (concatD [prt 0 id])
-
- prtList es = case es of
- [] -> (concatD [])
- [x] -> (concatD [prt 0 x])
- x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
-
-instance Print Symbol where
- prt i e = case e of
- CatS category -> prPrec i 0 (concatD [prt 0 category])
- TermS str -> prPrec i 0 (concatD [prt 0 str])
-
- prtList es = case es of
- [] -> (concatD [doc (showString ".")])
- [x] -> (concatD [prt 0 x])
- x:xs -> (concatD [prt 0 x , prt 0 xs])
-
-instance Print Category where
- prt i e = case e of
- Category singlequotestring -> prPrec i 0 (concatD [prt 0 singlequotestring])
-
-
-