forked from GitHub/gf-core
Replace BNFC-generated PrintGFCCRaw with smaller hand-written one.
This commit is contained in:
@@ -1,104 +1,37 @@
|
|||||||
{-# OPTIONS -fno-warn-incomplete-patterns #-}
|
module GF.GFCC.Raw.PrintGFCCRaw (printTree) where
|
||||||
module GF.GFCC.Raw.PrintGFCCRaw where
|
|
||||||
|
|
||||||
-- pretty-printer generated by the BNF converter
|
|
||||||
|
|
||||||
import GF.GFCC.Raw.AbsGFCCRaw
|
import GF.GFCC.Raw.AbsGFCCRaw
|
||||||
import Char
|
|
||||||
|
|
||||||
-- the top-level printing method
|
import Data.List (intersperse)
|
||||||
printTree :: Print a => a -> String
|
|
||||||
printTree = render . prt 0
|
|
||||||
|
|
||||||
type Doc = [ShowS] -> [ShowS]
|
printTree :: Grammar -> String
|
||||||
|
printTree g = prGrammar g ""
|
||||||
|
|
||||||
doc :: ShowS -> Doc
|
prGrammar :: Grammar -> ShowS
|
||||||
doc = (:)
|
prGrammar (Grm xs) = prRExpList xs
|
||||||
|
|
||||||
render :: Doc -> String
|
prRExp :: RExp -> ShowS
|
||||||
render d = rend 0 (map ($ "") $ d []) "" where
|
prRExp (App x xs) = showChar '(' . prCId x . showChar ' '
|
||||||
rend i ss = case ss of
|
. prRExpList xs . showChar ')'
|
||||||
"[" :ts -> showChar '[' . rend i ts
|
prRExp (AId x) = prCId x
|
||||||
"(" :ts -> showChar '(' . rend i ts
|
prRExp (AInt x) = shows x
|
||||||
"{" :ts -> showChar '{' . new (i+1) . rend (i+1) ts
|
prRExp (AStr x) = showChar '"' . concatS (map mkEsc x) . showChar '"'
|
||||||
"}" : ";":ts -> new (i-1) . space "}" . showChar ';' . new (i-1) . rend (i-1) ts
|
prRExp (AFlt x) = shows x -- FIXME: simpler format
|
||||||
"}" :ts -> new (i-1) . showChar '}' . new (i-1) . rend (i-1) ts
|
prRExp AMet = showChar '?'
|
||||||
";" :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
|
mkEsc :: Char -> ShowS
|
||||||
parenth ss = doc (showChar '(') . ss . doc (showChar ')')
|
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 :: [ShowS] -> ShowS
|
||||||
concatS = foldr (.) id
|
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])
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user