1 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module Language.XML where
6 import Control.Applicative (Applicative(..))
8 import Data.Default.Class (Default(..))
9 import Data.Eq (Eq(..))
10 import Data.Function (($), (.))
12 import Data.Map.Strict (Map)
13 import Data.Maybe (Maybe(..))
14 import Data.Ord (Ord(..))
15 import Data.Semigroup (Semigroup(..))
16 import Data.Sequence (Seq)
17 import Data.String (IsString(..))
18 import Data.Text (Text)
19 import Data.TreeSeq.Strict (Tree)
20 import Prelude (error, pred, succ)
21 import Text.Show (Show(..), showsPrec, showChar, showString)
22 import qualified Data.List as List
23 import qualified Data.Text.Lazy as TL
25 import Language.TCT.Cell
26 import Language.TCT.Debug
29 type XML = Tree (Cell XmlNode)
35 { xmlNamePrefix :: TL.Text
36 , xmlNameSpace :: TL.Text
37 , xmlNameLocal :: TL.Text
39 instance Show XmlName where
40 showsPrec _p XmlName{xmlNameSpace="", ..} =
41 showString (TL.unpack xmlNameLocal)
42 showsPrec _p XmlName{..} =
43 if TL.null xmlNameSpace
44 then showString (TL.unpack xmlNameLocal)
47 showString (TL.unpack xmlNameSpace) .
49 showString (TL.unpack xmlNameLocal)
50 instance Eq XmlName where
51 XmlName _ sx lx == XmlName _ sy ly = sx == sy && lx == ly
52 instance Ord XmlName where
53 XmlName _ sx lx `compare` XmlName _ sy ly = compare sx sy <> compare lx ly
54 instance IsString XmlName where
55 fromString "" = XmlName "" "" ""
56 fromString full@('{':rest) =
57 case List.break (== '}') rest of
58 (_, "") -> error ("Invalid Clark notation: " <> show full)
59 (ns, local) -> XmlName "" (TL.pack ns) (TL.pack $ List.drop 1 local)
60 fromString local = XmlName "" "" (TL.pack local)
61 instance Pretty XmlName
63 xmlLocalName :: TL.Text -> XmlName
64 xmlLocalName = XmlName "" ""
69 | XmlAttr XmlName TL.Text
72 deriving (Eq,Ord,Show)
73 instance Pretty XmlNode
76 type XmlAttrs = Map XmlName (Cell TL.Text)
83 newtype Nat = Nat { unNat :: Int }
85 instance Show Nat where
86 showsPrec p = showsPrec p . unNat
87 instance Default Nat where
91 succNat (Nat n) = Nat $ succ n
93 predNat :: Nat -> Maybe Nat
94 predNat (Nat n) | n <= 0 = Nothing
95 | otherwise = Just $ Nat $ pred n
98 newtype Nat1 = Nat1 { unNat1 :: Int }
100 instance Show Nat1 where
101 showsPrec p = showsPrec p . unNat1
102 instance Default Nat1 where
105 succNat1 :: Nat1 -> Nat1
106 succNat1 (Nat1 n) = Nat1 $ succ n
107 predNat1 :: Nat1 -> Maybe Nat1
108 predNat1 (Nat1 n) | n <= 1 = Nothing
109 | otherwise = Just $ Nat1 $ pred n
112 newtype Ident = Ident { unIdent :: TL.Text }
113 deriving (Eq,Ord,Show,Default,IsString)
116 newtype URL = URL { unURL :: TL.Text }
117 deriving (Eq,Ord,Default)
118 instance Show URL where
119 showsPrec p = showsPrec p . unURL
120 instance Semigroup URL where
124 newtype Path = Path TL.Text
125 deriving (Eq,Show,Default)
129 = MayText { unMayText :: TL.Text }
130 deriving (Eq,Ord,Default)
131 instance Show MayText where
132 showsPrec p = showsPrec p . unMayText
133 instance Semigroup MayText where
138 whenMayText :: Applicative m => MayText -> (MayText -> m ()) -> m ()
139 whenMayText (MayText "") _f = pure ()
140 whenMayText t f = f t
142 instance Default Text where
144 instance Default TL.Text where