diff options
| author | bringert <unknown> | 2004-08-23 07:51:36 +0000 |
|---|---|---|
| committer | bringert <unknown> | 2004-08-23 07:51:36 +0000 |
| commit | 65f012d15513814bd2cc4ad74f54edd35ade13fe (patch) | |
| tree | 089419071773038e8357a6b97a9ec0481df2a338 /src/GF/CFGM/PrintCFG.hs | |
| parent | 25ffe15333a881022047409a1c12a17dd41d1198 (diff) | |
Added CFGM format (pm -printer=cfgm) and utf8 conversion for pm.
Diffstat (limited to 'src/GF/CFGM/PrintCFG.hs')
| -rw-r--r-- | src/GF/CFGM/PrintCFG.hs | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/GF/CFGM/PrintCFG.hs b/src/GF/CFGM/PrintCFG.hs new file mode 100644 index 000000000..e7ecb1f6a --- /dev/null +++ b/src/GF/CFGM/PrintCFG.hs @@ -0,0 +1,164 @@ +module PrintCFG where + +-- pretty-printer generated by the BNF converter + +import 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 = (:) + +-- seriously hacked spacing +render :: Doc -> String +render d = rend 0 (map ($ "") $ d []) "" where + rend i ss = case ss of + ";" :ts -> showString ";" . new i . rend i ts + -- H removed a bunch of cases here + "]":".":ts -> showString "]" . space "." . rend i ts -- H + t:t' :ts | noSpace t' -> showString t . showString t' . rend i ts -- H + t :ts | noSpace t -> showString t . rend i ts -- H + 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)) + noSpace t = t `elem` ["[","]","{","}",",","/",":",".","!"] -- H + +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 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 id name profile category symbols -> prPrec i 0 (concatD [prt 0 id , doc (showString ":") , prt 0 name , prt 0 profile , 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 Profile where + prt i e = case e of + Profile intss -> prPrec i 0 (concatD [doc (showString "[") , prt 0 intss , doc (showString "]")]) + + +instance Print Ints where + prt i e = case e of + Ints ns -> prPrec i 0 (concatD [doc (showString "[") , prt 0 ns , doc (showString "]")]) + + 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 []) + x:xs -> (concatD [prt 0 x , prt 0 xs]) + +instance Print Name where + prt i e = case e of + Name identparams category -> prPrec i 0 (concatD [prt 0 identparams , prt 0 category]) + + +instance Print Category where + prt i e = case e of + Category identparam id params -> prPrec i 0 (concatD [prt 0 identparam , doc (showString ".") , prt 0 id , prt 0 params]) + + +instance Print IdentParam where + prt i e = case e of + IdentParam id params -> prPrec i 0 (concatD [prt 0 id , doc (showString "{") , prt 0 params , doc (showString "}")]) + + prtList es = case es of + [] -> (concatD []) + x:xs -> (concatD [prt 0 x , doc (showString "/") , prt 0 xs]) + +instance Print Param where + prt i e = case e of + Param id -> prPrec i 0 (concatD [doc (showString "!") , prt 0 id]) + + prtList es = case es of + [] -> (concatD []) + x:xs -> (concatD [prt 0 x , prt 0 xs]) + + |
