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.Map.Strict as Map
24 import qualified Data.Text.Lazy as TL
26 import Language.TCT.Cell
27 import Language.TCT.Debug
30 type XML = Tree (Cell XmlNode)
36 { xmlNamePrefix :: TL.Text
37 , xmlNameSpace :: TL.Text
38 , xmlNameLocal :: TL.Text
40 instance Show XmlName where
41 showsPrec _p XmlName{xmlNameSpace="", ..} =
42 showString (TL.unpack xmlNameLocal)
43 showsPrec _p XmlName{..} =
44 if TL.null xmlNameSpace
45 then showString (TL.unpack xmlNameLocal)
48 showString (TL.unpack xmlNameSpace) .
50 showString (TL.unpack xmlNameLocal)
51 instance Eq XmlName where
52 XmlName _ sx lx == XmlName _ sy ly = sx == sy && lx == ly
53 instance Ord XmlName where
54 XmlName _ sx lx `compare` XmlName _ sy ly = compare sx sy <> compare lx ly
55 instance IsString XmlName where
56 fromString "" = XmlName "" "" ""
57 fromString full@('{':rest) =
58 case List.break (== '}') rest of
59 (_, "") -> error ("Invalid Clark notation: " <> show full)
60 (ns, local) -> XmlName "" (TL.pack ns) (TL.pack $ List.drop 1 local)
61 fromString local = XmlName "" "" (TL.pack local)
62 instance Pretty XmlName
64 xmlLocalName :: TL.Text -> XmlName
65 xmlLocalName = XmlName "" ""
70 | XmlAttr XmlName TL.Text
73 deriving (Eq,Ord,Show)
74 instance Pretty XmlNode
77 type XmlAttrs = Map XmlName (Cell TL.Text)
84 newtype Nat = Nat { unNat :: Int }
85 deriving (Eq, Ord, Show)
86 instance Default Nat where
90 succNat (Nat n) = Nat $ succ n
92 predNat :: Nat -> Maybe Nat
93 predNat (Nat n) | n <= 0 = Nothing
94 | otherwise = Just $ Nat $ pred n
97 newtype Nat1 = Nat1 { unNat1 :: Int }
98 deriving (Eq, Ord, Show)
99 instance Default Nat1 where
102 succNat1 :: Nat1 -> Nat1
103 succNat1 (Nat1 n) = Nat1 $ succ n
104 predNat1 :: Nat1 -> Maybe Nat1
105 predNat1 (Nat1 n) | n <= 1 = Nothing
106 | otherwise = Just $ Nat1 $ pred n
109 newtype Ident = Ident { unIdent :: Text }
110 deriving (Eq,Ord,Show,Default,IsString)
111 instance Default Text where
115 newtype URL = URL { unURL :: Text }
116 deriving (Eq,Ord,Show,Default)
117 instance Semigroup URL where
121 newtype Path = Path Text
122 deriving (Eq,Show,Default)
126 = MayText { unMayText :: Text }
127 deriving (Eq,Show,Default)
128 instance Semigroup MayText where
133 whenMayText :: Applicative m => MayText -> (MayText -> m ()) -> m ()
134 whenMayText (MayText "") _f = pure ()
135 whenMayText t f = f t