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)
22 import Text.Show (Show(..), showsPrec, showChar, showString)
23 import qualified Data.List as List
24 import qualified Data.Text as Text
26 import Language.TCT.Cell
29 type XML = Tree (Cell XmlName) (Cell XmlLeaf)
35 { xmlNamePrefix :: Text
36 , xmlNameSpace :: Text
37 , xmlNameLocal :: Text
39 instance Show XmlName where
40 showsPrec _p XmlName{xmlNameSpace="", ..} =
41 showString (Text.unpack xmlNameLocal)
42 showsPrec _p XmlName{..} =
43 if Text.null xmlNameSpace
44 then showString (Text.unpack xmlNameLocal)
47 showString (Text.unpack xmlNameSpace) .
49 showString (Text.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 "" (Text.pack ns) (Text.pack $ List.drop 1 local)
60 fromString local = XmlName "" "" (Text.pack local)
62 xmlLocalName :: Text -> XmlName
63 xmlLocalName = XmlName "" ""
67 = XmlAttr XmlName Text
70 deriving (Eq,Ord,Show)
75 { xmlPosAncestors :: [(XmlName,Rank)]
76 , xmlPosPrecedingsSiblings :: Map XmlName Rank
77 } deriving (Eq,Ord,Show)
78 instance Default XmlPos where
79 def = XmlPos mempty mempty
85 newtype Nat = Nat { unNat :: Int }
86 deriving (Eq, Ord, Show)
88 predNat :: Nat -> Maybe Nat
89 predNat (Nat n) | n <= 0 = Nothing
90 | otherwise = Just $ Nat $ pred n
93 newtype Nat1 = Nat1 { unNat1 :: Int }
94 deriving (Eq, Ord, Show)
96 predNat1 :: Nat1 -> Maybe Nat1
97 predNat1 (Nat1 n) | n <= 1 = Nothing
98 | otherwise = Just $ Nat1 $ pred n
101 newtype Ident = Ident { unIdent :: Text }
102 deriving (Eq,Show,Default,IsString)
103 instance Default Text where
107 newtype URL = URL Text
108 deriving (Eq,Show,Default)
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