Source

planck / lib / stream.ml

open Stream_intf

(** Import the constructors *)
type 'a desc = 'a Spotlib.Spot.Stream.desc = 
  | Cons of 'a * 'a desc lazy_t
  | Null

module Extend(P : Base) = struct

  type elem = P.Elem.t
  type pos = P.Pos.t
  type attr = P.Attr.t

  open Spotlib.Spot.Stream
  open P

  type desc = (Elem.t option * Attr.t) Spotlib.Spot.Stream.desc
  type t = (Elem.t option * Attr.t) Spotlib.Spot.Stream.t (* None means EOS *)

  let cons_desc e attr v = Cons ((Some e,attr), v)
  let null_desc attr = Cons ((None, attr), Spotlib.Spot.Stream.null)
  let null attr = Lazy.lazy_from_val (null_desc attr)

  let peek = function
    | lazy Cons ((Some elem, _), t') -> Some (elem, t')
    | _ -> None

  let is_null = is_null
    
  let to_list t = 
    let rec to_list st = function
      | lazy Cons ((Some elem, _), t) -> to_list (elem :: st) t
      | _ -> List.rev st
    in
    to_list [] t

  let rec iter f = function
    | lazy Cons ((Some elem, _), t) -> f elem; iter f t
    | _ -> ()

  let rec fold_right f lst st = match lst with
    | lazy Cons ((Some v, _), lst') -> f v (fold_right f lst' st)
    | _ -> st

  let rec map f lst = lazy (match lst with
    | lazy Cons ((Some v, _pos), lst') -> Cons (f v, map f lst')
    | _ -> Null)

  (* [t2] must be a postfix of [t1] otherwise, it loops forever *)
  let rev_between t1 t2 =
    let rec loop st t =
      if t == t2 then st (* CR jfuruse: we cannot always use pointer eq *)
      else 
        match t with
        | lazy Cons ((Some elem, _), t') -> loop (elem::st) t'
        | _ -> st
    in
    loop [] t1

  let between t1 t2 = List.rev (rev_between t1 t2)

  let attr = function
    | lazy Cons ((_, attr), _) -> attr
    | _ -> assert false

  let position s = Attr.position (attr s)

end

module Make(P : Base) = struct
  include P
  include Extend(P)
end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.