1 {-# LANGUAGE AllowAmbiguousTypes #-}
2 {-# LANGUAGE InstanceSigs #-}
3 {-# LANGUAGE PatternSynonyms #-}
4 {-# LANGUAGE StrictData #-}
5 {-# LANGUAGE ViewPatterns #-}
6 {-# OPTIONS_GHC -fno-warn-orphans #-}
7 module Symantic.XML.Tree.Data
8 ( module Symantic.XML.Tree.Data
13 import Control.Applicative (Applicative(..))
15 import Data.Either (Either(..))
16 import Data.Eq (Eq(..))
17 import Data.Foldable (Foldable(..))
18 import Data.Function (($), id)
19 import Data.Functor ((<$>))
20 import Data.Functor.Identity (Identity(..))
21 import Data.Maybe (Maybe(..))
22 import Data.Monoid (Monoid(..))
23 import Data.Ord (Ord(..))
24 import Data.Semigroup (Semigroup(..))
25 import Prelude (error)
26 import Text.Show (Show(..))
27 import qualified Data.HashMap.Strict as HM
28 import qualified Data.Sequence as Seq
29 import qualified Data.Text.Lazy as TL
30 import qualified Data.TreeSeq.Strict as TS
33 import Symantic.XML.Language
34 import Symantic.XML.RelaxNG.Language
35 import Symantic.XML.Write
36 import Symantic.XML.Tree.Source
39 type Tree src = TS.Tree (src (Node (src EscapedAttr)))
42 type Trees src = TS.Trees (src (Node (src EscapedAttr)))
44 pattern Tree0 :: a -> TS.Tree a
45 pattern Tree0 a <- TS.Tree a (null -> True)
46 where Tree0 a = TS.Tree a Seq.empty
50 = NodeElem QName (HM.HashMap QName attr) -- ^ Node.
51 | NodePI PName TL.Text -- ^ Leaf (except for @<?xml?>@ which has 'NodePI' children.
52 | NodeText EscapedText -- ^ Leaf.
53 | NodeComment TL.Text -- ^ Leaf.
54 | NodeCDATA TL.Text -- ^ Leaf.
55 deriving (Eq, Ord, Show)
58 newtype TreeData params k
60 { unTreeData :: ( HM.HashMap QName (Identity EscapedAttr) ->
65 tree :: TreeData callers (Trees Identity) -> callers
66 tree (TreeData callers) = callers (\_as _txt ts -> ts)
68 type SourcedTree src = Tree (Sourced src)
69 type SourcedTrees src = Trees (Sourced src)
70 type FileSourcedTree = SourcedTree (FileSource Offset)
71 type FileSourcedTrees = SourcedTrees (FileSource Offset)
73 -- | Unify two 'Trees', merging border 'NodeText's if any.
75 Semigroup (Sourced src EscapedText) =>
76 SourcedTrees src -> SourcedTrees src -> SourcedTrees src
78 case (Seq.viewr x, Seq.viewl y) of
79 (xs Seq.:> x0, y0 Seq.:< ys) ->
81 ( Tree0 (Sourced sx (NodeText tx))
82 , Tree0 (Sourced sy (NodeText ty)) ) ->
84 Seq.singleton (Tree0 $ (NodeText <$>) $
85 Sourced sx tx <> Sourced sy ty) `union`
92 Semigroup (Sourced src EscapedText) =>
93 Foldable f => f (SourcedTrees src) -> SourcedTrees src
94 unions = foldl' union mempty
96 instance Emptyable TreeData where
97 empty = TreeData (\k -> k mempty mempty mempty)
98 instance Unitable TreeData where
99 unit = TreeData (\k () -> k mempty mempty mempty)
100 instance Voidable TreeData where
101 void a (TreeData x) = TreeData (`x` a)
102 instance Dimapable TreeData where
103 dimap _a2b b2a (TreeData x) = TreeData $ \k b ->
105 instance Dicurryable TreeData where
106 dicurry (_::proxy args) _construct destruct (TreeData x) = TreeData $ \k r ->
107 uncurryN @args (x k) (destruct r)
108 instance Composable TreeData where
109 TreeData x <.> TreeData y = TreeData $ \k ->
110 x (\ax vx tx -> y (\ay vy ty -> k (ax<>ay) (vx<>vy) (tx<>ty)))
111 instance Tupable TreeData where
112 TreeData x <:> TreeData y = TreeData $ \k (a,b) ->
113 x (\ax vx tx -> y (\ay vy ty -> k (ax<>ay) (vx<>vy) (tx<>ty)) b) a
114 instance Eitherable TreeData where
115 TreeData x <+> TreeData y = TreeData $ \k -> \case
118 instance Constant TreeData where
119 constant _a = TreeData $ \k _a -> k mempty mempty mempty
120 instance Optionable TreeData where
122 optional (TreeData x) = TreeData $ \k ->
124 Nothing -> k mempty mempty mempty
127 instance Routable TreeData where
128 TreeData x <!> TreeData y = TreeData $ \k ->
131 instance Repeatable TreeData where
132 many0 (TreeData x) = TreeData $ \k -> \case
133 [] -> k mempty mempty mempty
134 a:as -> x (\ax vx tx ->
135 unTreeData (many0 (TreeData x))
136 (\aas vas tas -> k (ax<>aas) (vx<>vas) (tx<>tas)) as) a
137 many1 (TreeData x) = TreeData $ \k -> \case
138 [] -> k mempty mempty mempty
139 a:as -> x (\ax vx tx ->
140 unTreeData (many1 (TreeData x))
141 (\aas vas tas -> k (ax<>aas) (vx<>vas) (tx<>tas)) as) a
142 instance Textable TreeData where
143 type TextConstraint TreeData a = EncodeText a
144 text = TreeData $ \k v ->
145 let t = encodeText v in
147 TS.Tree (Identity (NodeText (escapeText t))) mempty
148 instance XML TreeData where
149 namespace _nm _ns = empty
150 element n (TreeData x) = TreeData $ \k ->
152 k mempty mempty $ pure $
153 TS.Tree (Identity (NodeElem n as)) ts
154 attribute n (TreeData x) = TreeData $ \k ->
156 k (HM.insert n (Identity (escapeAttr txt)) as) mempty mempty
157 literal lit = TreeData $ \k ->
158 k mempty lit $ pure $
159 TS.Tree (Identity (NodeText (escapeText lit))) mempty
160 pi n = TreeData $ \k v ->
161 k mempty mempty $ pure $
162 TS.Tree (Identity (NodePI n v)) mempty
163 comment = TreeData $ \k v ->
164 k mempty mempty $ pure $
165 TS.Tree (Identity (NodeComment v)) mempty
166 cdata = TreeData $ \k v ->
167 k mempty mempty $ pure $
168 TS.Tree (Identity (NodeCDATA v)) mempty
169 instance Permutable TreeData where
170 type Permutation TreeData = TreeDataPerm TreeData
171 permutable = unTreeDataPerm
173 noPerm = TreeDataPerm empty
174 permWithDefault _a = TreeDataPerm
175 instance Definable TreeData where
177 instance RelaxNG TreeData where
178 elementMatch nc x = TreeData $ \k n ->
179 if matchNameClass nc n
180 then error "elementMatch: given QName does not match expected NameClass"
181 else unTreeData (element n x) k
182 attributeMatch nc x = TreeData $ \k n ->
183 if matchNameClass nc n
184 then error "attributeMatch: given QName does not match expected NameClass"
185 else unTreeData (attribute n x) k
187 -- ** Type 'TreeDataPerm'
188 newtype TreeDataPerm repr xml k
190 { unTreeDataPerm :: repr xml k }
191 instance Transformable (TreeDataPerm repr) where
192 type UnTrans (TreeDataPerm repr) = repr
193 noTrans = TreeDataPerm
194 unTrans = unTreeDataPerm
195 instance Dimapable (TreeDataPerm TreeData)
196 instance Composable (TreeDataPerm TreeData)
197 instance Tupable (TreeDataPerm TreeData)