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
|
adam@4
|
56 con treeInternal :: Type -> Type -> Type
|
adam@4
|
57
|
adam@4
|
58 val tree : parentI ::: Type -> parent ::: Type -> childI ::: Type -> child ::: Type
|
adam@4
|
59 -> pattern parentI parent -> pattern childI child
|
adam@4
|
60 -> pattern (treeInternal parentI childI) (parent * child)
|
adam@8
|
61 (* A combinator that takes in a pattern for a parent node and another pattern to
|
adam@8
|
62 * be matched at any depth within the parent's subtree. Unlike [children],
|
adam@8
|
63 * [tree] finds as many subtree matches per parent node as possible. *)
|
adam@4
|
64
|
adam@5
|
65 type document
|
adam@7
|
66 val show_document : show document
|
adam@8
|
67 (* Type of uninterpreted XML documents *)
|
adam@5
|
68
|
adam@8
|
69 val fetch : string -> transaction document
|
adam@8
|
70 (* Retrieve a document by URL. *)
|
adam@8
|
71
|
adam@5
|
72 val app : internal ::: Type -> data ::: Type -> pattern internal data -> (data -> transaction {}) -> document -> transaction {}
|
adam@8
|
73 (* Find all matches of a pattern in a document, running an imperative function
|
adam@8
|
74 * on the data returned by each match. *)
|