Commits

james woodyatt committed bf5f33d

Throw an error when unfolding a LL(x) scanner does not completely
consume the input stream.

Comments (0)

Files changed (2)

     | Some _ as r -> r
 
 let rec unfold =
-    let rec loop p s =
+    let aux _ = Error in
+    let rec loop f p s =
         match p s with
-        | None -> Cf_seq.Z
-        | Some (r, s) -> Cf_seq.P (r, lazy (loop p s))
+        | None ->
+            if Lazy.force s <> Cf_seq.Z then raise (f s);
+            Cf_seq.Z
+        | Some (r, s) ->
+            Cf_seq.P (r, lazy (loop f p s))
     in
-    let start p s = lazy (loop p s) in
+    let start ?(f = aux) p s = lazy (loop f p s) in
     start
 
 module Op = struct
 *)
 val req: ?f:('s Cf_seq.t -> exn) -> ('s, 'r) t -> ('s, 'r) t
 
-(** Use [unfold p i] to create a sequence of output values recognized by
+(** Use [unfold ?f p i] to create a sequence of output values recognized by
     applying the input token sequence [i] to the parser [p] until no more
-    input is recognized.
+    input is recognized.  The remaining input stream is applied to [f] when
+    output terminates without finishing the input.  The default function raises
+    [Error].
 *)
-val unfold: ('s, 'r) t -> 's Cf_seq.t -> 'r Cf_seq.t
+val unfold:
+    ?f:('s Cf_seq.t -> exn) -> ('s, 'r) t -> 's Cf_seq.t -> 'r Cf_seq.t
 
 (** Open this module to take the parser operators into the current scope. *)
 module Op: sig