summaryrefslogtreecommitdiff
path: root/src/GF
diff options
context:
space:
mode:
authorbringert <bringert@cs.chalmers.se>2008-01-07 21:31:05 +0000
committerbringert <bringert@cs.chalmers.se>2008-01-07 21:31:05 +0000
commitff9e6404b80bda8d52a57454f55ca60c6d9ab723 (patch)
tree6663616c4317d5c70b4cd6f8d049498530b16b4b /src/GF
parentc38f3da80b0ea5ccfca25444bb82b75be4a4eeb5 (diff)
Replace BNFC-generated PrintGFCCRaw with smaller hand-written one.
Diffstat (limited to 'src/GF')
-rw-r--r--src/GF/GFCC/Raw/PrintGFCCRaw.hs117
1 files changed, 25 insertions, 92 deletions
diff --git a/src/GF/GFCC/Raw/PrintGFCCRaw.hs b/src/GF/GFCC/Raw/PrintGFCCRaw.hs
index c13908fe1..1b937e429 100644
--- a/src/GF/GFCC/Raw/PrintGFCCRaw.hs
+++ b/src/GF/GFCC/Raw/PrintGFCCRaw.hs
@@ -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
-
-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))
+import Data.List (intersperse)
-parenth :: Doc -> Doc
-parenth ss = doc (showChar '(') . ss . doc (showChar ')')
-
-concatS :: [ShowS] -> ShowS
-concatS = foldr (.) id
+printTree :: Grammar -> String
+printTree g = prGrammar g ""
-concatD :: [Doc] -> Doc
-concatD = foldr (.) id
+prGrammar :: Grammar -> ShowS
+prGrammar (Grm xs) = prRExpList xs
-replicateS :: Int -> ShowS -> ShowS
-replicateS n f = concatS (replicate n f)
+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 '?'
--- 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 "\\\\"
+mkEsc :: Char -> ShowS
+mkEsc s = case s of
+ '"' -> showString "\\\""
+ '\\' -> 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)
+ _ -> showChar s
+prRExpList :: [RExp] -> ShowS
+prRExpList = concatS . intersperse (showChar ' ') . map prRExp
-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])
-
+prCId :: CId -> ShowS
+prCId (CId x) = showString x
+concatS :: [ShowS] -> ShowS
+concatS = foldr (.) id