| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
type ('e,'a) t = { |
| 14 |
strm : ('e,'a) Stream.t; |
| 15 |
|
| 16 |
fun_loc : int -> Loc.t; |
| 17 |
|
| 18 |
mutable max_peek : int; |
| 19 |
} |
| 20 |
|
| 21 |
let from ?(loc=Loc.(initial ToplevelInput)) f = |
| 22 |
let loct = Hashtbl.create 207 in |
| 23 |
let loct_func loct i = Hashtbl.find loct i in |
| 24 |
let loct_add loct i loc = Hashtbl.add loct i loc in |
| 25 |
let strm = |
| 26 |
let i = ref 0 in |
| 27 |
Stream.from |
| 28 |
(fun e -> |
| 29 |
match f e with |
| 30 |
| None -> None |
| 31 |
| Some (a,loc) -> |
| 32 |
loct_add loct !i loc; incr i; Some a) in |
| 33 |
let fun_loc i = if i = 0 then loc else loct_func loct (i - 1) in |
| 34 |
{ strm; max_peek = 0; fun_loc } |
| 35 |
|
| 36 |
let count strm = Stream.count strm.strm |
| 37 |
|
| 38 |
let current_loc strm = |
| 39 |
strm.fun_loc (Stream.count strm.strm) |
| 40 |
|
| 41 |
let max_peek_loc strm = |
| 42 |
strm.fun_loc strm.max_peek |
| 43 |
|
| 44 |
let interval_loc bp ep strm = |
| 45 |
assert (bp <= ep); |
| 46 |
if ep > strm.max_peek then failwith "Not peeked position"; |
| 47 |
if bp == ep then |
| 48 |
Loc.after (strm.fun_loc bp) 0 0 |
| 49 |
else |
| 50 |
let loc1 = strm.fun_loc (bp + 1) in |
| 51 |
let loc2 = strm.fun_loc ep in |
| 52 |
Loc.merge loc1 loc2 |
| 53 |
|
| 54 |
let get_loc n strm = |
| 55 |
strm.fun_loc (n + 1) |
| 56 |
|
| 57 |
let peek e strm = |
| 58 |
let a = Stream.peek e strm.strm in |
| 59 |
if Option.has_some a then strm.max_peek <- max (Stream.count strm.strm + 1) strm.max_peek; |
| 60 |
a |
| 61 |
|
| 62 |
let npeek e n strm = |
| 63 |
let l = Stream.npeek e n strm.strm in |
| 64 |
strm.max_peek <- max (Stream.count strm.strm + List.length l) strm.max_peek; |
| 65 |
l |
| 66 |
|
| 67 |
let peek_nth e n strm = |
| 68 |
let list = npeek e (n + 1) strm in |
| 69 |
List.nth_opt list n |
| 70 |
|
| 71 |
let junk e strm = Stream.junk e strm.strm |
| 72 |
let njunk e len strm = Stream.njunk e len strm.strm |
| 73 |
|
| 74 |
let next e strm = Stream.next e strm.strm |