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.Maybe (Maybe(..))
13 import Data.Ord (Ord(..))
14 import Data.Semigroup (Semigroup(..))
15 import Data.Sequence (Seq)
16 import Data.String (IsString(..))
17 import Data.Text (Text)
18 import Data.TreeSeq.Strict (Tree)
19 import Prelude (error, pred, succ)
20 import Text.Show (Show(..), showsPrec, showChar, showString)
21 import qualified Data.List as List
22 import qualified Data.Text as Text
24 import Language.TCT.Cell
27 type XML = Tree (Cell XmlName) (Cell XmlLeaf)
33 { xmlNamePrefix :: Text
34 , xmlNameSpace :: Text
35 , xmlNameLocal :: Text
37 instance Show XmlName where
38 showsPrec _p XmlName{xmlNameSpace="", ..} =
39 showString (Text.unpack xmlNameLocal)
40 showsPrec _p XmlName{..} =
41 if Text.null xmlNameSpace
42 then showString (Text.unpack xmlNameLocal)
45 showString (Text.unpack xmlNameSpace) .
47 showString (Text.unpack xmlNameLocal)
48 instance Eq XmlName where
49 XmlName _ sx lx == XmlName _ sy ly = sx == sy && lx == ly
50 instance Ord XmlName where
51 XmlName _ sx lx `compare` XmlName _ sy ly = compare sx sy <> compare lx ly
52 instance IsString XmlName where
53 fromString "" = XmlName "" "" ""
54 fromString full@('{':rest) =
55 case List.break (== '}') rest of
56 (_, "") -> error ("Invalid Clark notation: " <> show full)
57 (ns, local) -> XmlName "" (Text.pack ns) (Text.pack $ List.drop 1 local)
58 fromString local = XmlName "" "" (Text.pack local)
60 xmlLocalName :: Text -> XmlName
61 xmlLocalName = XmlName "" ""
65 = XmlAttr XmlName Text
68 deriving (Eq,Ord,Show)
74 newtype Nat = Nat { unNat :: Int }
75 deriving (Eq, Ord, Show)
76 instance Default Nat where
80 succNat (Nat n) = Nat $ succ n
82 predNat :: Nat -> Maybe Nat
83 predNat (Nat n) | n <= 0 = Nothing
84 | otherwise = Just $ Nat $ pred n
87 newtype Nat1 = Nat1 { unNat1 :: Int }
88 deriving (Eq, Ord, Show)
89 instance Default Nat1 where
92 succNat1 :: Nat1 -> Nat1
93 succNat1 (Nat1 n) = Nat1 $ succ n
94 predNat1 :: Nat1 -> Maybe Nat1
95 predNat1 (Nat1 n) | n <= 1 = Nothing
96 | otherwise = Just $ Nat1 $ pred n
99 newtype Ident = Ident { unIdent :: Text }
100 deriving (Eq,Ord,Show,Default,IsString)
101 instance Default Text where
105 newtype URL = URL { unURL :: Text }
106 deriving (Eq,Ord,Show,Default)
107 instance Semigroup URL where
111 newtype Path = Path Text
112 deriving (Eq,Show,Default)
116 = MayText { unMayText :: Text }
117 deriving (Eq,Show,Default)
118 instance Semigroup MayText where
123 whenMayText :: Applicative m => MayText -> (MayText -> m ()) -> m ()
124 whenMayText (MayText "") _f = pure ()
125 whenMayText t f = f t