annotate src/ur/feed.ur @ 10:edc2b467f818

Add a version of Feed.app which threads state.
author Karn Kallio <kkallio@eka>
date Thu, 09 Jun 2011 12:59:27 -0430
parents f19beef42ceb
children 43c3fbd8527a
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
kkallio@10 221 fun app' [internal ::: Type] [data ::: Type] [acc ::: Type] (p : pattern internal data) (f : data -> acc -> transaction acc)
kkallio@10 222 (doc : document) (acc : acc) : transaction acc =
adam@1 223 let
kkallio@10 224 fun recur xml acc state =
adam@4 225 case String.seek xml #"<" of
kkallio@10 226 None => return acc
adam@4 227 | Some xml =>
adam@1 228 if xml <> "" && String.sub xml 0 = #"/" then
adam@4 229 case String.seek xml #"\x3E" of
kkallio@10 230 None => return acc
adam@4 231 | Some xml =>
adam@1 232 case p.ExitTag state of
kkallio@10 233 None => recur xml acc p.Initial
adam@1 234 | Some state =>
adam@1 235 case p.Finished state of
kkallio@10 236 None => recur xml acc state
adam@4 237 | Some (data, cont) =>
kkallio@10 238 acc <- f data acc;
kkallio@10 239 recur xml acc (if cont then state else p.Initial)
adam@1 240 else if xml <> "" && String.sub xml 0 = #"?" then
adam@4 241 case String.seek xml #"\x3E" of
kkallio@10 242 None => return acc
kkallio@10 243 | Some xml => recur xml acc state
adam@1 244 else if xml <> "" && String.sub xml 0 = #"!" then
adam@2 245 if String.lengthGe xml 3 && String.sub xml 1 = #"-" && String.sub xml 2 = #"-" then
adam@1 246 let
adam@1 247 fun skipper xml =
adam@4 248 case String.seek xml #"-" of
adam@1 249 None => xml
adam@4 250 | Some xml =>
adam@2 251 if String.lengthGe xml 2 && String.sub xml 0 = #"-" && String.sub xml 1 = #"\x3E" then
adam@1 252 String.suffix xml 2
adam@1 253 else
adam@1 254 skipper xml
adam@1 255 in
kkallio@10 256 recur (skipper (String.suffix xml 3)) acc state
adam@1 257 end
adam@1 258 else
adam@4 259 case String.seek xml #"]" of
kkallio@10 260 None => return acc
adam@4 261 | Some xml =>
adam@4 262 case String.seek xml #"\x3E" of
kkallio@10 263 None => return acc
kkallio@10 264 | Some xml => recur xml acc state
adam@1 265 else
adam@1 266 case String.msplit {Needle = " >/", Haystack = xml} of
kkallio@10 267 None => return acc
adam@1 268 | Some (tagName, ch, xml) =>
adam@1 269 let
adam@1 270 fun readAttrs ch xml acc =
adam@1 271 case ch of
adam@1 272 #"\x3E" => (xml, acc, False)
adam@1 273 | #"/" =>
adam@4 274 (case String.seek xml #"\x3E" of
adam@1 275 None => (xml, acc, True)
adam@4 276 | Some xml => (xml, acc, True))
adam@1 277 | _ =>
adam@2 278 if String.lengthGe xml 2 && Char.isSpace (String.sub xml 0) then
adam@1 279 readAttrs (String.sub xml 0) (String.suffix xml 1) acc
adam@1 280 else if xml <> "" && String.sub xml 0 = #"\x3E" then
adam@1 281 (String.suffix xml 1, acc, False)
adam@1 282 else if xml <> "" && String.sub xml 0 = #"/" then
adam@4 283 (case String.seek xml #"\x3E" of
adam@1 284 None => (xml, acc, True)
adam@4 285 | Some xml => (xml, acc, True))
adam@1 286 else
adam@1 287 case String.split xml #"=" of
adam@1 288 None => (xml, acc, False)
adam@1 289 | Some (aname, xml) =>
adam@6 290 if xml = "" || (String.sub xml 0 <> #"\"" && String.sub xml 0 <> #"'") then
adam@1 291 (xml, (aname, "") :: acc, False)
adam@1 292 else
adam@6 293 case String.split (String.suffix xml 1) (String.sub xml 0) of
adam@1 294 None => (xml, (aname, "") :: acc, False)
adam@1 295 | Some (value, xml) =>
adam@1 296 if xml = "" then
adam@1 297 (xml, (aname, value) :: acc, False)
adam@1 298 else
adam@1 299 readAttrs (String.sub xml 0) (String.suffix xml 1) ((aname, value) :: acc)
adam@1 300
adam@1 301 val (xml, attrs, ended) = readAttrs ch xml []
adam@1 302
adam@1 303 fun skipSpaces xml =
adam@1 304 if xml <> "" && Char.isSpace (String.sub xml 0) then
adam@1 305 skipSpaces (String.suffix xml 1)
adam@1 306 else
adam@1 307 xml
adam@1 308
adam@1 309 val xml = skipSpaces xml
adam@1 310
adam@1 311 val (xml, cdata) =
adam@1 312 if ended then
adam@1 313 (xml, None)
adam@1 314 else if String.isPrefix {Prefix = "<![CDATA[", Full = xml} then
adam@1 315 let
adam@1 316 fun skipper xml acc =
adam@1 317 case String.split xml #"]" of
adam@1 318 None => (acc ^ xml, None)
adam@1 319 | Some (pre, xml) =>
adam@2 320 if String.lengthGe xml 2 && String.sub xml 0 = #"]" && String.sub xml 1 = #"\x3E" then
adam@1 321 (String.suffix xml 2, Some (acc ^ pre))
adam@1 322 else
adam@1 323 skipper xml (acc ^ "]" ^ pre)
adam@1 324 in
adam@1 325 skipper (String.suffix xml 9) ""
adam@1 326 end
adam@1 327 else
adam@4 328 case String.split' xml #"<" of
adam@1 329 None => (xml, None)
adam@4 330 | Some (cdata, xml) => (xml, Some cdata)
adam@1 331 in
adam@1 332 case p.EnterTag {Tag = tagName, Attrs = attrs, Cdata = cdata} state of
kkallio@10 333 None => recur xml acc p.Initial
adam@1 334 | Some state =>
adam@4 335 case p.Finished state of
adam@4 336 None =>
adam@4 337 (case (if ended then p.ExitTag state else Some state) of
kkallio@10 338 None => recur xml acc p.Initial
adam@4 339 | Some state =>
adam@4 340 case p.Finished state of
kkallio@10 341 None => recur xml acc state
adam@4 342 | Some (data, cont) =>
kkallio@10 343 acc <- f data acc;
kkallio@10 344 recur xml acc (if cont then state else p.Initial))
adam@4 345 | Some (data, cont) =>
kkallio@10 346 acc <- f data acc;
kkallio@10 347 recur xml acc (if cont then state else p.Initial)
adam@1 348 end
adam@1 349 in
kkallio@10 350 recur doc acc p.Initial
adam@1 351 end
kkallio@10 352
kkallio@10 353 fun app [internal ::: Type] [data ::: Type] (p : pattern internal data) (f : data -> transaction {}) (doc : document) : transaction {} =
kkallio@10 354 app' p (fn data acc => f data) doc ()