annotate src/ur/feed.urs @ 11:43c3fbd8527a

Add variant of children allowing specification of optional matches.
author Karn Kallio <kkallio@eka>
date Thu, 23 Jun 2011 23:40:29 -0430
parents edc2b467f818
children f641cfcd5153
rev   line source
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
adam@5 74 type document
adam@7 75 val show_document : show document
adam@8 76 (* Type of uninterpreted XML documents *)
adam@5 77
adam@8 78 val fetch : string -> transaction document
adam@8 79 (* Retrieve a document by URL. *)
adam@8 80
kkallio@10 81 val app' : internal ::: Type -> data ::: Type -> acc ::: Type -> pattern internal data
kkallio@10 82 -> (data -> acc -> transaction acc) -> document -> acc -> transaction acc
kkallio@10 83 (* Find all matches of a pattern in a document, running an imperative function
kkallio@10 84 * on the data returned by each match while threading through some state. *)
kkallio@10 85
adam@5 86 val app : internal ::: Type -> data ::: Type -> pattern internal data -> (data -> transaction {}) -> document -> transaction {}
adam@8 87 (* Find all matches of a pattern in a document, running an imperative function
adam@8 88 * on the data returned by each match. *)