annotate src/ur/feed.ur @ 9:f19beef42ceb

Add tag combinator with given attribute values.
author Karn Kallio <kkallio@eka>
date Tue, 31 May 2011 09:21:49 -0430
parents 05a28a77f6fe
children edc2b467f818
rev   line source
adam@0 1 task initialize = fn () => FeedFfi.init
adam@0 2
adam@4 3 con pattern internal output = {Initial : internal,
adam@4 4 EnterTag : {Tag : string, Attrs : list (string * string), Cdata : option string} -> internal -> option internal,
adam@4 5 ExitTag : internal -> option internal,
adam@4 6 Finished : internal -> option (output * bool)}
adam@4 7
adam@4 8 val null : pattern unit (variant []) =
adam@4 9 {Initial = (),
adam@4 10 EnterTag = fn _ () => Some (),
adam@4 11 ExitTag = fn () => Some (),
adam@4 12 Finished = fn () => None}
adam@1 13
adam@6 14 con tagInternal (attrs :: {Unit}) = option {Attrs : $(mapU (option string) attrs), Cdata : option string}
adam@1 15
adam@6 16 fun tagG [attrs ::: {Unit}] [t ::: Type] (fl : folder attrs) (accept : {Attrs : $(mapU (option string) attrs), Cdata : option string} -> option t)
adam@3 17 (name : string) (attrs : $(mapU string attrs))
adam@3 18 : pattern (tagInternal attrs) t =
adam@4 19 {Initial = None,
adam@4 20 EnterTag = fn tinfo _ =>
adam@4 21 if tinfo.Tag <> name then
adam@4 22 None
adam@4 23 else
adam@6 24 let
adam@6 25 val v = {Attrs = @mp [fn _ => string] [fn _ => option string]
adam@6 26 (fn [u] aname => List.assoc aname tinfo.Attrs)
adam@6 27 fl attrs,
adam@6 28 Cdata = tinfo.Cdata}
adam@6 29 in
adam@6 30 case accept v of
adam@6 31 None => None
adam@6 32 | Some _ => Some (Some v)
adam@6 33 end,
adam@4 34 ExitTag = fn _ => None,
adam@4 35 Finished = fn state => case state of
adam@4 36 None => None
adam@4 37 | Some state =>
adam@4 38 case accept state of
adam@4 39 None => None
adam@4 40 | Some v => Some (v, False)}
adam@3 41
adam@6 42 fun allPresent [attrs ::: {Unit}] (fl : folder attrs) (attrs : $(mapU (option string) attrs)) : option $(mapU string attrs) =
adam@6 43 @foldUR [option string] [fn attrs => option $(mapU string attrs)]
adam@6 44 (fn [nm ::_] [r ::_] [[nm] ~ r] os acc =>
adam@6 45 case (os, acc) of
adam@6 46 (Some s, Some acc) => Some ({nm = s} ++ acc)
adam@6 47 | _ => None)
adam@6 48 (Some {}) fl attrs
adam@6 49
kkallio@9 50 fun allPresentE [attrs ::: {Unit}] (fl : folder attrs) (vs : $(mapU (option string) attrs)) (attrs : $(mapU (option string) attrs))
kkallio@9 51 : option $(mapU string attrs) =
kkallio@9 52 @foldUR2 [option string] [option string] [fn attrs => option $(mapU string attrs)]
kkallio@9 53 (fn [nm ::_] [r ::_] [[nm] ~ r] os os' acc =>
kkallio@9 54 case (os, os', acc) of
kkallio@9 55 (Some s, Some s', Some acc) => if s = s' then Some ({nm = s'} ++ acc) else None
kkallio@9 56 | (None, Some s', Some acc) => Some ({nm = s'} ++ acc)
kkallio@9 57 | _ => None)
kkallio@9 58 (Some {}) fl vs attrs
kkallio@9 59
adam@3 60 fun tag [attrs ::: {Unit}] (fl : folder attrs) (name : string) (attrs : $(mapU string attrs))
adam@3 61 : pattern (tagInternal attrs) {Attrs : $(mapU string attrs), Cdata : option string} =
adam@6 62 @tagG fl (fn r =>
adam@6 63 case @allPresent fl r.Attrs of
adam@6 64 None => None
adam@6 65 | Some attrs => Some (r -- #Attrs ++ {Attrs = attrs}))
adam@6 66 name attrs
adam@3 67
adam@3 68 fun tagA [attrs ::: {Unit}] (fl : folder attrs) (name : string) (attrs : $(mapU string attrs))
adam@3 69 : pattern (tagInternal attrs) $(mapU string attrs) =
adam@6 70 @tagG fl (fn r => @allPresent fl r.Attrs) name attrs
kkallio@9 71
kkallio@9 72 fun tagAV [attrs ::: {Unit}] (fl : folder attrs) (name : string) (attrs : $(mapU (string * option string) attrs))
kkallio@9 73 : pattern (tagInternal attrs) $(mapU string attrs) =
kkallio@9 74 let
kkallio@9 75 val as = @mp [fn _ => (string * option string)] [fn _ => string] (fn [u] (x, _) => x) fl attrs
kkallio@9 76 val vs = @mp [fn _ => (string * option string)] [fn _ => option string] (fn [u] (_, x) => x) fl attrs
kkallio@9 77 in
kkallio@9 78 @tagG fl (fn r => @allPresentE fl vs r.Attrs) name as
kkallio@9 79 end
kkallio@9 80
adam@6 81 fun tagAO [attrs ::: {Unit}] (fl : folder attrs) (name : string) (attrs : $(mapU string attrs))
adam@6 82 : pattern (tagInternal attrs) $(mapU (option string) attrs) =
adam@6 83 @tagG fl (fn r => Some (r.Attrs)) name attrs
adam@3 84
adam@3 85 fun tagC (name : string) : pattern (tagInternal []) string =
adam@3 86 tagG (fn r => r.Cdata) name {}
adam@1 87
adam@4 88 datatype status a = Initial | Pending of a | Matched of a
adam@1 89
adam@1 90 con childrenInternal (parent :: Type) (children :: {Type}) = option (parent * int * $(map status children))
adam@1 91
adam@6 92 fun childrenG [parentI ::: Type] [parent ::: Type] [children ::: {(Type * Type)}] [t ::: Type]
adam@6 93 (ready : $(map (fn (i, d) => option d) children) -> option t)
adam@6 94 (parent : pattern parentI parent) (children : $(map (fn (i, d) => pattern i d) children)) (fl : folder children)
adam@6 95 : pattern (childrenInternal parentI (map fst children)) (parent * t) =
adam@4 96 {Initial = None,
adam@4 97 EnterTag = fn tinfo state =>
adam@4 98 case state of
adam@4 99 None =>
adam@4 100 (case parent.EnterTag tinfo parent.Initial of
adam@4 101 None => None
adam@4 102 | Some pstate => Some (Some (pstate, 1, @map0 [status] (fn [t ::_] => Initial)
adam@4 103 (@@Folder.mp [fst] [_] fl))))
adam@4 104 | Some (pstate, depth, cstates) =>
adam@6 105 if depth = 0 then
adam@6 106 case parent.EnterTag tinfo parent.Initial of
adam@6 107 None => None
adam@6 108 | Some pstate => Some (Some (pstate, 1, @map0 [status] (fn [t ::_] => Initial)
adam@6 109 (@@Folder.mp [fst] [_] fl)))
adam@6 110 else
adam@6 111 Some (Some (pstate,
adam@6 112 depth+1,
adam@6 113 @map2 [fn (i, d) => pattern i d] [fn (i, d) => status i] [fn (i, d) => status i]
adam@6 114 (fn [p] (ch : pattern p.1 p.2) (cstate : status p.1) =>
adam@6 115 case cstate of
adam@6 116 Initial =>
adam@6 117 (case ch.EnterTag tinfo ch.Initial of
adam@6 118 None => Initial
adam@6 119 | Some v =>
adam@6 120 case ch.Finished v of
adam@6 121 None => Pending v
adam@6 122 | _ => Matched v)
adam@6 123 | Pending cstate =>
adam@6 124 (case ch.EnterTag tinfo cstate of
adam@6 125 None => Initial
adam@6 126 | Some v =>
adam@6 127 case ch.Finished v of
adam@6 128 None => Pending v
adam@6 129 | _ => Matched v)
adam@6 130 | v => v)
adam@6 131 fl children cstates)),
adam@4 132 ExitTag = fn state =>
adam@4 133 case state of
adam@4 134 None => None
adam@6 135 | Some (pstate, 1, cstates) => Some (Some (pstate, 0, cstates))
adam@4 136 | Some (pstate, depth, cstates) =>
adam@4 137 Some (Some (pstate, depth-1,
adam@4 138 @map2 [fn (i, d) => pattern i d] [fn (i, d) => status i] [fn (i, d) => status i]
adam@4 139 (fn [p] (ch : pattern p.1 p.2) (cstate : status p.1) =>
adam@4 140 case cstate of
adam@4 141 Pending cstate =>
adam@4 142 (case ch.ExitTag cstate of
adam@4 143 None => Initial
adam@4 144 | Some cstate' =>
adam@4 145 case ch.Finished cstate' of
adam@4 146 None => Pending cstate'
adam@4 147 | _ => Matched cstate')
adam@4 148 | _ => cstate)
adam@4 149 fl children cstates)),
adam@4 150 Finished = fn state =>
adam@4 151 case state of
adam@6 152 Some (pstate, 0, cstates) =>
adam@4 153 (case parent.Finished pstate of
adam@4 154 None => None
adam@4 155 | Some (pdata, pcont) =>
adam@6 156 case ready (@map2 [fn (i, d) => status i] [fn (i, d) => pattern i d] [fn (i, d) => option d]
adam@6 157 (fn [p] (cstate : status p.1) (ch : pattern p.1 p.2) =>
adam@6 158 case cstate of
adam@6 159 Matched v => Option.mp (fn p => p.1) (ch.Finished v)
adam@6 160 | _ => None) fl cstates children) of
adam@4 161 None => None
adam@4 162 | Some cdata => Some ((pdata, cdata), pcont))
adam@4 163 | _ => None}
adam@1 164
adam@6 165 fun children [parentI ::: Type] [parent ::: Type] [children ::: {(Type * Type)}]
adam@6 166 (parent : pattern parentI parent) (children : $(map (fn (i, d) => pattern i d) children)) (fl : folder children)
adam@6 167 : pattern (childrenInternal parentI (map fst children)) (parent * $(map snd children)) =
adam@6 168 @childrenG (@foldR [fn (i, d) => option d] [fn cs => option $(map snd cs)]
adam@6 169 (fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] (cstate : option p.2) acc =>
adam@6 170 case (cstate, acc) of
adam@6 171 (Some cstate, Some acc) => Some ({nm = cstate} ++ acc)
adam@6 172 | _ => None)
adam@6 173 (Some {}) fl) parent children fl
adam@6 174
adam@6 175 fun childrenO [parentI ::: Type] [parent ::: Type] [children ::: {(Type * Type)}]
adam@6 176 (parent : pattern parentI parent) (children : $(map (fn (i, d) => pattern i d) children)) (fl : folder children)
adam@6 177 : pattern (childrenInternal parentI (map fst children)) (parent * $(map (fn (i, d) => option d) children)) =
adam@6 178 @childrenG Some parent children fl
adam@6 179
adam@4 180 con treeInternal (parent :: Type) (child :: Type) = option (parent * int * option child)
adam@4 181
adam@4 182 fun tree [parentI ::: Type] [parent ::: Type] [childI ::: Type] [child ::: Type]
adam@4 183 (parent : pattern parentI parent) (child : pattern childI child)
adam@4 184 : pattern (treeInternal parentI childI) (parent * child) =
adam@4 185 {Initial = None,
adam@4 186 EnterTag = fn tinfo state =>
adam@4 187 case state of
adam@4 188 None =>
adam@4 189 (case parent.EnterTag tinfo parent.Initial of
adam@4 190 None => None
adam@4 191 | Some pstate => Some (Some (pstate, 1, None)))
adam@4 192 | Some (pstate, depth, cstate) =>
adam@4 193 Some (Some (pstate,
adam@4 194 depth+1,
adam@4 195 child.EnterTag tinfo (Option.get child.Initial cstate))),
adam@4 196 ExitTag = fn state =>
adam@4 197 case state of
adam@4 198 None => None
adam@6 199 | Some (_, 1, _) => None
adam@4 200 | Some (pstate, depth, cstate) =>
adam@4 201 Some (Some (pstate, depth-1, Option.bind child.ExitTag cstate)),
adam@4 202 Finished = fn state =>
adam@4 203 case state of
adam@4 204 None => None
adam@4 205 | Some (pstate, _, cstate) =>
adam@4 206 case parent.Finished pstate of
adam@4 207 None => None
adam@4 208 | Some (pdata, _) =>
adam@4 209 case cstate of
adam@4 210 None => None
adam@4 211 | Some cstate =>
adam@4 212 case child.Finished cstate of
adam@4 213 None => None
adam@4 214 | Some (cdata, _) => Some ((pdata, cdata), True)}
adam@4 215
adam@5 216 type document = string
adam@7 217 val show_document = _
adam@5 218
adam@5 219 val fetch = FeedFfi.fetch
adam@5 220
adam@5 221 fun app [internal ::: Type] [data ::: Type] (p : pattern internal data) (f : data -> transaction {}) (doc : document) : transaction {} =
adam@1 222 let
adam@1 223 fun recur xml state =
adam@4 224 case String.seek xml #"<" of
adam@1 225 None => return ()
adam@4 226 | Some xml =>
adam@1 227 if xml <> "" && String.sub xml 0 = #"/" then
adam@4 228 case String.seek xml #"\x3E" of
adam@1 229 None => return ()
adam@4 230 | Some xml =>
adam@1 231 case p.ExitTag state of
adam@1 232 None => recur xml p.Initial
adam@1 233 | Some state =>
adam@1 234 case p.Finished state of
adam@1 235 None => recur xml state
adam@4 236 | Some (data, cont) =>
adam@1 237 f data;
adam@4 238 recur xml (if cont then state else p.Initial)
adam@1 239 else if xml <> "" && String.sub xml 0 = #"?" then
adam@4 240 case String.seek xml #"\x3E" of
adam@1 241 None => return ()
adam@4 242 | Some xml => recur xml state
adam@1 243 else if xml <> "" && String.sub xml 0 = #"!" then
adam@2 244 if String.lengthGe xml 3 && String.sub xml 1 = #"-" && String.sub xml 2 = #"-" then
adam@1 245 let
adam@1 246 fun skipper xml =
adam@4 247 case String.seek xml #"-" of
adam@1 248 None => xml
adam@4 249 | Some xml =>
adam@2 250 if String.lengthGe xml 2 && String.sub xml 0 = #"-" && String.sub xml 1 = #"\x3E" then
adam@1 251 String.suffix xml 2
adam@1 252 else
adam@1 253 skipper xml
adam@1 254 in
adam@1 255 recur (skipper (String.suffix xml 3)) state
adam@1 256 end
adam@1 257 else
adam@4 258 case String.seek xml #"]" of
adam@1 259 None => return ()
adam@4 260 | Some xml =>
adam@4 261 case String.seek xml #"\x3E" of
adam@1 262 None => return ()
adam@4 263 | Some xml => recur xml state
adam@1 264 else
adam@1 265 case String.msplit {Needle = " >/", Haystack = xml} of
adam@1 266 None => return ()
adam@1 267 | Some (tagName, ch, xml) =>
adam@1 268 let
adam@1 269 fun readAttrs ch xml acc =
adam@1 270 case ch of
adam@1 271 #"\x3E" => (xml, acc, False)
adam@1 272 | #"/" =>
adam@4 273 (case String.seek xml #"\x3E" of
adam@1 274 None => (xml, acc, True)
adam@4 275 | Some xml => (xml, acc, True))
adam@1 276 | _ =>
adam@2 277 if String.lengthGe xml 2 && Char.isSpace (String.sub xml 0) then
adam@1 278 readAttrs (String.sub xml 0) (String.suffix xml 1) acc
adam@1 279 else if xml <> "" && String.sub xml 0 = #"\x3E" then
adam@1 280 (String.suffix xml 1, acc, False)
adam@1 281 else if xml <> "" && String.sub xml 0 = #"/" then
adam@4 282 (case String.seek xml #"\x3E" of
adam@1 283 None => (xml, acc, True)
adam@4 284 | Some xml => (xml, acc, True))
adam@1 285 else
adam@1 286 case String.split xml #"=" of
adam@1 287 None => (xml, acc, False)
adam@1 288 | Some (aname, xml) =>
adam@6 289 if xml = "" || (String.sub xml 0 <> #"\"" && String.sub xml 0 <> #"'") then
adam@1 290 (xml, (aname, "") :: acc, False)
adam@1 291 else
adam@6 292 case String.split (String.suffix xml 1) (String.sub xml 0) of
adam@1 293 None => (xml, (aname, "") :: acc, False)
adam@1 294 | Some (value, xml) =>
adam@1 295 if xml = "" then
adam@1 296 (xml, (aname, value) :: acc, False)
adam@1 297 else
adam@1 298 readAttrs (String.sub xml 0) (String.suffix xml 1) ((aname, value) :: acc)
adam@1 299
adam@1 300 val (xml, attrs, ended) = readAttrs ch xml []
adam@1 301
adam@1 302 fun skipSpaces xml =
adam@1 303 if xml <> "" && Char.isSpace (String.sub xml 0) then
adam@1 304 skipSpaces (String.suffix xml 1)
adam@1 305 else
adam@1 306 xml
adam@1 307
adam@1 308 val xml = skipSpaces xml
adam@1 309
adam@1 310 val (xml, cdata) =
adam@1 311 if ended then
adam@1 312 (xml, None)
adam@1 313 else if String.isPrefix {Prefix = "<![CDATA[", Full = xml} then
adam@1 314 let
adam@1 315 fun skipper xml acc =
adam@1 316 case String.split xml #"]" of
adam@1 317 None => (acc ^ xml, None)
adam@1 318 | Some (pre, xml) =>
adam@2 319 if String.lengthGe xml 2 && String.sub xml 0 = #"]" && String.sub xml 1 = #"\x3E" then
adam@1 320 (String.suffix xml 2, Some (acc ^ pre))
adam@1 321 else
adam@1 322 skipper xml (acc ^ "]" ^ pre)
adam@1 323 in
adam@1 324 skipper (String.suffix xml 9) ""
adam@1 325 end
adam@1 326 else
adam@4 327 case String.split' xml #"<" of
adam@1 328 None => (xml, None)
adam@4 329 | Some (cdata, xml) => (xml, Some cdata)
adam@1 330 in
adam@1 331 case p.EnterTag {Tag = tagName, Attrs = attrs, Cdata = cdata} state of
adam@1 332 None => recur xml p.Initial
adam@1 333 | Some state =>
adam@4 334 case p.Finished state of
adam@4 335 None =>
adam@4 336 (case (if ended then p.ExitTag state else Some state) of
adam@4 337 None => recur xml p.Initial
adam@4 338 | Some state =>
adam@4 339 case p.Finished state of
adam@4 340 None => recur xml state
adam@4 341 | Some (data, cont) =>
adam@4 342 f data;
adam@4 343 recur xml (if cont then state else p.Initial))
adam@4 344 | Some (data, cont) =>
adam@4 345 f data;
adam@4 346 recur xml (if cont then state else p.Initial)
adam@1 347 end
adam@1 348 in
adam@5 349 recur doc p.Initial
adam@1 350 end