1
0
forked from GitHub/gf-core

Replace BNFC-generated PrintGFCCRaw with smaller hand-written one.

This commit is contained in:
bringert
2008-01-07 21:31:05 +00:00
parent c38f3da80b
commit ff9e6404b8

View File

@@ -1,104 +1,37 @@
{-# OPTIONS -fno-warn-incomplete-patterns #-}
module GF.GFCC.Raw.PrintGFCCRaw where
-- pretty-printer generated by the BNF converter
module GF.GFCC.Raw.PrintGFCCRaw (printTree) where
import GF.GFCC.Raw.AbsGFCCRaw
import Char
-- the top-level printing method
printTree :: Print a => a -> String
printTree = render . prt 0
import Data.List (intersperse)
type Doc = [ShowS] -> [ShowS]
printTree :: Grammar -> String
printTree g = prGrammar g ""
doc :: ShowS -> Doc
doc = (:)
prGrammar :: Grammar -> ShowS
prGrammar (Grm xs) = prRExpList xs
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))
prRExp :: RExp -> ShowS
prRExp (App x xs) = showChar '(' . prCId x . showChar ' '
. prRExpList xs . showChar ')'
prRExp (AId x) = prCId x
prRExp (AInt x) = shows x
prRExp (AStr x) = showChar '"' . concatS (map mkEsc x) . showChar '"'
prRExp (AFlt x) = shows x -- FIXME: simpler format
prRExp AMet = showChar '?'
parenth :: Doc -> Doc
parenth ss = doc (showChar '(') . ss . doc (showChar ')')
mkEsc :: Char -> ShowS
mkEsc s = case s of
'"' -> showString "\\\""
'\\' -> showString "\\\\"
'\n' -> showString "\\n"
'\t' -> showString "\\t"
_ -> showChar s
prRExpList :: [RExp] -> ShowS
prRExpList = concatS . intersperse (showChar ' ') . map prRExp
prCId :: CId -> ShowS
prCId (CId x) = showString x
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])