adam@8
|
1 (* This module implements imperative processing of XML feeds.
|
adam@8
|
2 *
|
adam@8
|
3 * Module author: Adam Chlipala
|
adam@8
|
4 *)
|
adam@8
|
5
|
adam@1
|
6 con pattern :: Type -> Type -> Type
|
adam@8
|
7 (* A pattern describes a set of XML subtrees, mapping each element of the set to
|
adam@8
|
8 * a data value. A value of type [pattern internal result] uses values of type
|
adam@8
|
9 * [internal] internally, but this API exposes no details of that usage. The
|
adam@8
|
10 * type [result] gives the type used in mappings of matched subtrees. *)
|
adam@8
|
11
|
adam@8
|
12 val null : pattern unit (variant [])
|
adam@8
|
13 (* A null pattern matches nothing, returning a value of the impossible empty
|
adam@8
|
14 * type if it ever does match. *)
|
adam@1
|
15
|
adam@1
|
16 con tagInternal :: {Unit} -> Type
|
adam@1
|
17
|
adam@1
|
18 val tag : attrs ::: {Unit} -> folder attrs -> string -> $(mapU string attrs)
|
adam@1
|
19 -> pattern (tagInternal attrs) {Attrs : $(mapU string attrs), Cdata : option string}
|
adam@8
|
20 (* A basic [tag] pattern matches a single tag with a number of required
|
adam@8
|
21 * attributes. A result value gives the attribute values and an optional
|
adam@8
|
22 * CDATA value for the text content of the tag. The [string] argument is the
|
adam@8
|
23 * tag name, and the following argument gives attribute names. *)
|
adam@1
|
24
|
adam@3
|
25 val tagA : attrs ::: {Unit} -> folder attrs -> string -> $(mapU string attrs)
|
adam@3
|
26 -> pattern (tagInternal attrs) $(mapU string attrs)
|
adam@8
|
27 (* A version of [tag] that ignores CDATA *)
|
adam@8
|
28
|
kkallio@9
|
29 val tagAV : attrs ::: {Unit} -> folder attrs -> string -> $(mapU (string * option string) attrs)
|
kkallio@9
|
30 -> pattern (tagInternal attrs) $(mapU string attrs)
|
kkallio@9
|
31 (* Extension of tagA with optional specification of values which attributes must
|
kkallio@9
|
32 * bear in order to count as a match. *)
|
kkallio@9
|
33
|
adam@6
|
34 val tagAO : attrs ::: {Unit} -> folder attrs -> string -> $(mapU string attrs)
|
adam@6
|
35 -> pattern (tagInternal attrs) $(mapU (option string) attrs)
|
adam@8
|
36 (* A version of [tagA] that makes each attribute optional *)
|
adam@6
|
37
|
adam@3
|
38 val tagC : string -> pattern (tagInternal []) string
|
adam@8
|
39 (* A version of [tag] that only matches tags with nonempty CDATA and returns
|
adam@8
|
40 * only that text *)
|
adam@3
|
41
|
adam@1
|
42 con childrenInternal :: Type -> {Type} -> Type
|
adam@1
|
43
|
adam@1
|
44 val children : parentI ::: Type -> parent ::: Type -> children ::: {(Type * Type)}
|
adam@1
|
45 -> pattern parentI parent -> $(map (fn (i, d) => pattern i d) children) -> folder children
|
adam@1
|
46 -> pattern (childrenInternal parentI (map fst children)) (parent * $(map snd children))
|
adam@8
|
47 (* A combinator that takes in a pattern for a parent node and a set of patterns
|
adam@8
|
48 * that must be matched against children of the parent. This combinator will
|
adam@8
|
49 * find at most one match per matching parent node. *)
|
adam@8
|
50
|
adam@6
|
51 val childrenO : parentI ::: Type -> parent ::: Type -> children ::: {(Type * Type)}
|
adam@6
|
52 -> pattern parentI parent -> $(map (fn (i, d) => pattern i d) children) -> folder children
|
adam@6
|
53 -> pattern (childrenInternal parentI (map fst children)) (parent * $(map (fn (i, d) => option d) children))
|
adam@8
|
54 (* A version of [children] where each child pattern need not be matched *)
|
adam@1
|
55
|
kkallio@11
|
56 datatype required t = Required of t | Optional of t
|
kkallio@11
|
57 (* Used for marking items as required or optional. *)
|
kkallio@11
|
58
|
kkallio@11
|
59 val childrenO' : parentI ::: Type -> parent ::: Type -> children ::: {(Type * Type)}
|
kkallio@11
|
60 -> pattern parentI parent -> $(map (fn (i, d) => required (pattern i d)) children) -> folder children
|
kkallio@11
|
61 -> pattern (childrenInternal parentI (map fst children)) (parent * $(map (fn (i, d) => option d) children))
|
kkallio@11
|
62 (* A version of [children] where the caller marks each child pattern
|
kkallio@11
|
63 * as either required or optional. *)
|
kkallio@11
|
64
|
adam@4
|
65 con treeInternal :: Type -> Type -> Type
|
adam@4
|
66
|
adam@4
|
67 val tree : parentI ::: Type -> parent ::: Type -> childI ::: Type -> child ::: Type
|
adam@4
|
68 -> pattern parentI parent -> pattern childI child
|
adam@4
|
69 -> pattern (treeInternal parentI childI) (parent * child)
|
adam@8
|
70 (* A combinator that takes in a pattern for a parent node and another pattern to
|
adam@8
|
71 * be matched at any depth within the parent's subtree. Unlike [children],
|
adam@8
|
72 * [tree] finds as many subtree matches per parent node as possible. *)
|
adam@4
|
73
|
kkallio@14
|
74 con gatherInternal :: Type -> Type -> Type -> Type
|
kkallio@14
|
75
|
kkallio@14
|
76 val gather : parentI ::: Type -> parent ::: Type -> childI ::: Type -> child ::: Type
|
kkallio@14
|
77 -> pattern parentI parent -> pattern childI child
|
kkallio@14
|
78 -> pattern (gatherInternal parentI childI child) (parent * list child)
|
kkallio@14
|
79 (* A combinator like tree that collects matching subtree patterns into a list rather
|
kkallio@14
|
80 * than handling them one at a time. *)
|
kkallio@14
|
81
|
adam@5
|
82 type document
|
adam@7
|
83 val show_document : show document
|
adam@8
|
84 (* Type of uninterpreted XML documents *)
|
adam@5
|
85
|
adam@8
|
86 val fetch : string -> transaction document
|
adam@8
|
87 (* Retrieve a document by URL. *)
|
adam@8
|
88
|
kkallio@10
|
89 val app' : internal ::: Type -> data ::: Type -> acc ::: Type -> pattern internal data
|
kkallio@10
|
90 -> (data -> acc -> transaction acc) -> document -> acc -> transaction acc
|
kkallio@10
|
91 (* Find all matches of a pattern in a document, running an imperative function
|
kkallio@10
|
92 * on the data returned by each match while threading through some state. *)
|
kkallio@10
|
93
|
adam@5
|
94 val app : internal ::: Type -> data ::: Type -> pattern internal data -> (data -> transaction {}) -> document -> transaction {}
|
adam@8
|
95 (* Find all matches of a pattern in a document, running an imperative function
|
adam@8
|
96 * on the data returned by each match. *)
|