Skip to content

Commit

Permalink
[ #289 ] XML: import Prelude qualified
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasabel committed Feb 1, 2021
1 parent cf27e92 commit 082e136
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions source/src/BNFC/Backend/XML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,23 @@ endtagDef b = if b then endtagDefConstr else endtagDefNotyp
-- lengthy, but validation guarantees type correctness
-- flag -xmlt
elemDataConstrs cf (cat,fcs) = elemc cat [(f,rhsCat cf f cs) | (f,cs) <- fcs]
efunDefConstrs = "elemFun i t x = [replicate (i+i) ' ' ++ tag t ++ \" \" ++ etag x]"
efunDefConstrs = "elemFun i t x = [P.replicate (i+i) ' ' ++ tag t ++ \" \" ++ etag x]"
endtagDefConstrs = "endtag _ c = tag (\"/\" ++ c)"

-- coding 1:
-- to show constructors as empty tags;
-- shorter than 0, but validation still guarantees type correctness
-- flag -xmlt
elemDataConstr cf (cat,fcs) = elemc cat [(f,rhsCat cf f cs) | (f,cs) <- fcs]
efunDefConstr = "elemFun i t x = [replicate (i+i) ' ' ++ tag t ++ \" \" ++ etag x]"
efunDefConstr = "elemFun i t x = [P.replicate (i+i) ' ' ++ tag t ++ \" \" ++ etag x]"
endtagDefConstr = "endtag _ c = tag (\"/\" ++ c)"

-- coding 2:
-- constructors as tags, no types.
-- clumsy DTD, but nice trees. Validation guarantees type correctness
-- flag -xml
elemDataNotyp cf (_,fcs) = unlines [element f [rhsCatNot cf cs] | (f,cs) <- fcs]
efunDefNotyp = "elemFun i _ x = [replicate (i+i) ' ' ++ tag x]"
efunDefNotyp = "elemFun i _ x = [P.replicate (i+i) ' ' ++ tag x]"
endtagDefNotyp = "endtag f _ = tag (\"/\" ++ f)"


Expand Down Expand Up @@ -123,6 +123,7 @@ parenth s = "(" ++ s ++ ")"
-- derive an XML printer from a BNF grammar
cf2XMLPrinter :: Bool -> SharedOptions -> String -> CF -> String
cf2XMLPrinter typ opts absMod cf = unlines [
"{-# LANGUAGE LambdaCase #-}",
pragmas opts,
prologue typ opts absMod,
integerRule cf,
Expand All @@ -141,23 +142,33 @@ pragmas opts =

prologue :: Bool -> SharedOptions -> String -> String
prologue b opts _ = unlines [
"-- pretty-printer generated by the BNF converter\n",
"module " ++ xmlFileM opts +++ "where\n",
"import Prelude\n",
"-- pretty-printer generated by the BNF converter",
"",
"module " ++ xmlFileM opts +++ "where",
"",
"import Prelude",
" ( Char, Double, Integer, String",
" , (.), ($), (+), (++)",
" )",
"import qualified Prelude as P",
" ( Show(..), Int",
" , concat, map, replicate, unlines",
" )",
"",
"import " ++ absFileM opts,
"",
"-- the top-level printing method",
"printXML :: XPrint a => a -> String",
"printXML = render . prt 0",
"",
"render :: [String] -> String",
"render = unlines",
"render = P.unlines",
"",
"-- the printer class does the job",
"class XPrint a where",
" prt :: Int -> a -> [String]",
" prtList :: Int -> [a] -> [String]",
" prtList i = concat . map (prt i)",
" prt :: P.Int -> a -> [String]",
" prtList :: P.Int -> [a] -> [String]",
" prtList i = P.concat . P.map (prt i)",
"",
"instance XPrint a => XPrint [a] where",
" prt = prtList",
Expand All @@ -166,11 +177,11 @@ prologue b opts _ = unlines [
"tag t = \"<\" ++ t ++ \">\"",
"etag t = \"<\" ++ t ++ \"/>\"",
"",
"elemTok, elemTokS :: Show a => Int -> String -> a -> [String]",
"elemTok i t x = [replicate (i+i) ' ' ++ tag (t ++ \" value = \" ++ show x ++ \" /\")]",
"elemTokS i t x = elemTok i t (show x)",
"elemTok, elemTokS :: P.Show a => P.Int -> String -> a -> [String]",
"elemTok i t x = [P.replicate (i+i) ' ' ++ tag (t ++ \" value = \" ++ P.show x ++ \" /\")]",
"elemTokS i t x = elemTok i t (P.show x)",
"",
"elemFun :: Int -> String -> String -> [String]",
"elemFun :: P.Int -> String -> String -> [String]",
efunDef b,
"",
"endtag :: String -> String -> String",
Expand Down Expand Up @@ -210,11 +221,11 @@ rules cf = unlines $
case_fun :: Cat -> [(String, [String])] -> String
case_fun cat xs = unlines $ concat
[ [ "instance XPrint" +++ show cat +++ "where"
, " prt i" +++ "e = case e of"
, " prt i'" +++ "= \\case"
]
, (`map` xs) $ \ (c, xx) ->
" " ++ c +++ unwords xx +++ "-> concat $ " +++
"elemFun i \"" ++ show cat ++ "\" \"" ++ c ++ "\"" +++
unwords [": prt (i+1)" +++ x | x <- xx] +++ ":" +++
"[[replicate (i+i) ' ' ++ endtag \"" ++ c ++ "\" \"" ++ show cat ++ "\"]]"
" " ++ c +++ unwords xx +++ "-> P.concat $ " +++
"elemFun i' \"" ++ show cat ++ "\" \"" ++ c ++ "\"" +++
unwords [": prt (i'+1)" +++ x | x <- xx] +++ ":" +++
"[[P.replicate (i'+i') ' ' ++ endtag \"" ++ c ++ "\" \"" ++ show cat ++ "\"]]"
]

0 comments on commit 082e136

Please sign in to comment.