src/Pure/General/source.ML
changeset 58863 64e571275b36
parent 58850 1bb0ad7827b4
child 58864 505a8150368a
equal deleted inserted replaced
58862:63a16e98cc14 58863:64e571275b36
    16   val of_list: 'a list -> ('a, 'a list) source
    16   val of_list: 'a list -> ('a, 'a list) source
    17   val exhausted: ('a, 'b) source -> ('a, 'a list) source
    17   val exhausted: ('a, 'b) source -> ('a, 'a list) source
    18   val of_string: string -> (string, string list) source
    18   val of_string: string -> (string, string list) source
    19   val of_string_limited: int -> string -> (string, substring) source
    19   val of_string_limited: int -> string -> (string, substring) source
    20   val source': 'a -> 'b Scan.stopper -> ('a * 'b list -> 'c list * ('a * 'b list)) ->
    20   val source': 'a -> 'b Scan.stopper -> ('a * 'b list -> 'c list * ('a * 'b list)) ->
    21     (bool * (string -> 'a * 'b list -> 'c list * ('a * 'b list))) option ->
    21     (string -> 'a * 'b list -> 'c list * ('a * 'b list)) option ->
    22     ('b, 'e) source -> ('c, 'a * ('b, 'e) source) source
    22     ('b, 'e) source -> ('c, 'a * ('b, 'e) source) source
    23   val source: 'a Scan.stopper -> ('a list -> 'b list * 'a list) ->
    23   val source: 'a Scan.stopper -> ('a list -> 'b list * 'a list) ->
    24     (bool * (string -> 'a list -> 'b list * 'a list)) option ->
    24     (string -> 'a list -> 'b list * 'a list) option ->
    25     ('a, 'd) source -> ('b, ('a, 'd) source) source
    25     ('a, 'd) source -> ('b, ('a, 'd) source) source
    26 end;
    26 end;
    27 
    27 
    28 structure Source: SOURCE =
    28 structure Source: SOURCE =
    29 struct
    29 struct
   115     val ((ys, (state', xs')), src') =
   115     val ((ys, (state', xs')), src') =
   116       if null xs then (([], (state, [])), s)
   116       if null xs then (([], (state, [])), s)
   117       else
   117       else
   118         (case opt_recover of
   118         (case opt_recover of
   119           NONE => drain (Scan.error scan) inp
   119           NONE => drain (Scan.error scan) inp
   120         | SOME (interactive, recover) =>
   120         | SOME recover =>
   121             (drain (Scan.catch scan) inp handle Fail msg =>
   121             (drain (Scan.catch scan) inp handle Fail msg =>
   122               (if interactive then Output.error_message msg else ();
   122               (drain (Scan.unless (Scan.lift (Scan.one (Scan.is_stopper stopper))) (recover msg))
   123                 drain (Scan.unless (Scan.lift (Scan.one (Scan.is_stopper stopper))) (recover msg))
   123                 inp)));
   124                   inp)));
       
   125   in (ys, (state', unget (xs', src'))) end;
   124   in (ys, (state', unget (xs', src'))) end;
   126 
   125 
   127 fun source' init_state stopper scan recover src =
   126 fun source' init_state stopper scan recover src =
   128   make_source [] (init_state, src) (drain_source' stopper scan recover);
   127   make_source [] (init_state, src) (drain_source' stopper scan recover);
   129 
   128 
   130 
   129 
   131 (* non state-based *)
   130 (* non state-based *)
   132 
   131 
   133 fun drain_source stopper scan opt_recover =
   132 fun drain_source stopper scan opt_recover =
   134   Scan.unlift (drain_source' stopper (Scan.lift scan)
   133   Scan.unlift (drain_source' stopper (Scan.lift scan)
   135     (Option.map (fn (int, r) => (int, Scan.lift o r)) opt_recover));
   134     (Option.map (fn r => Scan.lift o r) opt_recover));
   136 
   135 
   137 fun source stopper scan recover src =
   136 fun source stopper scan recover src =
   138   make_source [] src (drain_source stopper scan recover);
   137   make_source [] src (drain_source stopper scan recover);
   139 
   138 
   140 end;
   139 end;