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.Monoid (Monoid(..))
15 import Data.Ord (Ord(..))
16 import Data.Semigroup (Semigroup(..))
17 import Data.Sequence (Seq)
18 import Data.String (IsString(..))
19 import Data.Text (Text)
20 import Data.TreeSeq.Strict (Tree)
21 import Prelude (error, pred, succ)
22 import Text.Show (Show(..), showsPrec, showChar, showString)
23 import qualified Data.List as List
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 }
86 instance Show Nat where
87 showsPrec p = showsPrec p . unNat
88 instance Default Nat where
92 succNat (Nat n) = Nat $ succ n
94 predNat :: Nat -> Maybe Nat
95 predNat (Nat n) | n <= 0 = Nothing
96 | otherwise = Just $ Nat $ pred n
99 newtype Nat1 = Nat1 { unNat1 :: Int }
101 instance Show Nat1 where
102 showsPrec p = showsPrec p . unNat1
103 instance Default Nat1 where
106 succNat1 :: Nat1 -> Nat1
107 succNat1 (Nat1 n) = Nat1 $ succ n
108 predNat1 :: Nat1 -> Maybe Nat1
109 predNat1 (Nat1 n) | n <= 1 = Nothing
110 | otherwise = Just $ Nat1 $ pred n
113 newtype Ident = Ident { unIdent :: TL.Text }
114 deriving (Eq,Ord,Show,Default,IsString,Semigroup,Monoid)
117 newtype URL = URL { unURL :: TL.Text }
118 deriving (Eq,Ord,Default)
119 instance Show URL where
120 showsPrec p = showsPrec p . unURL
121 instance Semigroup URL where
125 newtype Path = Path TL.Text
126 deriving (Eq,Show,Default)
130 = MayText { unMayText :: TL.Text }
131 deriving (Eq,Ord,Default)
132 instance Show MayText where
133 showsPrec p = showsPrec p . unMayText
134 instance Semigroup MayText where
139 whenMayText :: Applicative m => MayText -> (MayText -> m ()) -> m ()
140 whenMayText (MayText "") _f = pure ()
141 whenMayText t f = f t
143 instance Default Text where
145 instance Default TL.Text where