src/Pure/General/source.ML
author wenzelm
Sat, 20 Nov 2010 00:53:26 +0100
changeset 40627 becf5d5187cc
parent 38253 3d4e521014f7
child 54387 890e983cb07b
permissions -rw-r--r--
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6118
caa439435666 fixed titles;
wenzelm
parents: 6116
diff changeset
     1
(*  Title:      Pure/General/source.ML
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     3
8806
wenzelm
parents: 8120
diff changeset
     4
Coalgebraic data sources -- efficient purely functional input streams.
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     5
*)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     6
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     7
signature SOURCE =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     8
sig
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     9
  type ('a, 'b) source
6681
08a084c79d8b added default_prompt;
wenzelm
parents: 6640
diff changeset
    10
  val default_prompt: string
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    11
  val set_prompt: string -> ('a, 'b) source -> ('a, 'b) source
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    12
  val get: ('a, 'b) source -> 'a list * ('a, 'b) source
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    13
  val unget: 'a list * ('a, 'b) source -> ('a, 'b) source
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    14
  val get_single: ('a, 'b) source -> ('a * ('a, 'b) source) option
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    15
  val exhaust: ('a, 'b) source -> 'a list
19485
5385c9d86c2a renamed Source.mapfilter to Source.map_filter;
wenzelm
parents: 15531
diff changeset
    16
  val map_filter: ('a -> 'b option) -> ('a, 'c) source -> ('b, ('a, 'c) source) source
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    17
  val filter: ('a -> bool) -> ('a, 'b) source -> ('a, ('a, 'b) source) source
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    18
  val of_list: 'a list -> ('a, 'a list) source
37903
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
    19
  val exhausted: ('a, 'b) source -> ('a, 'a list) source
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    20
  val of_string: string -> (string, string list) source
37903
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
    21
  val of_string_limited: int -> string -> (string, substring) source
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 37903
diff changeset
    22
  val tty: TextIO.instream -> (string, unit) source
27732
8dbf5761a24a abstract type Scan.stopper;
wenzelm
parents: 25846
diff changeset
    23
  val source': 'a -> 'b Scan.stopper -> ('a * 'b list -> 'c list * ('a * 'b list)) ->
23682
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23675
diff changeset
    24
    (bool * (string -> 'a * 'b list -> 'c list * ('a * 'b list))) option ->
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    25
    ('b, 'e) source -> ('c, 'a * ('b, 'e) source) source
27732
8dbf5761a24a abstract type Scan.stopper;
wenzelm
parents: 25846
diff changeset
    26
  val source: 'a Scan.stopper -> ('a list -> 'b list * 'a list) ->
23682
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23675
diff changeset
    27
    (bool * (string -> 'a list -> 'b list * 'a list)) option ->
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    28
    ('a, 'd) source -> ('b, ('a, 'd) source) source
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    29
end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    30
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    31
structure Source: SOURCE =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    32
struct
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    33
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    34
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    35
(** datatype source **)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    36
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    37
datatype ('a, 'b) source =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    38
  Source of
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    39
   {buffer: 'a list,
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    40
    info: 'b,
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    41
    prompt: string,
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    42
    drain: string -> 'b -> 'a list * 'b};
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    43
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    44
fun make_source buffer info prompt drain =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    45
  Source {buffer = buffer, info = info, prompt = prompt, drain = drain};
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    46
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    47
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    48
(* prompt *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    49
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    50
val default_prompt = "> ";
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    51
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    52
fun set_prompt prompt (Source {buffer, info, prompt = _, drain}) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    53
  make_source buffer info prompt drain;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    54
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    55
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    56
(* get / unget *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    57
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    58
fun get (Source {buffer = [], info, prompt, drain}) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    59
      let val (xs, info') = drain prompt info
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    60
      in (xs, make_source [] info' prompt drain) end
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    61
  | get (Source {buffer, info, prompt, drain}) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    62
      (buffer, make_source [] info prompt drain);
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    63
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    64
fun unget (xs, Source {buffer, info, prompt, drain}) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    65
  make_source (xs @ buffer) info prompt drain;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    66
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    67
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    68
(* variations on get *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    69
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    70
fun get_prompt prompt src = get (set_prompt prompt src);
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    71
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    72
fun get_single src =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    73
  (case get src of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    74
    ([], _) => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    75
  | (x :: xs, src') => SOME (x, unget (xs, src')));
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    76
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    77
fun exhaust src =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    78
  (case get src of
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    79
    ([], _) => []
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    80
  | (xs, src') => xs @ exhaust src');
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    81
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    82
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    83
(* (map)filter *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    84
19485
5385c9d86c2a renamed Source.mapfilter to Source.map_filter;
wenzelm
parents: 15531
diff changeset
    85
fun drain_map_filter f prompt src =
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    86
  let
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    87
    val (xs, src') = get_prompt prompt src;
19485
5385c9d86c2a renamed Source.mapfilter to Source.map_filter;
wenzelm
parents: 15531
diff changeset
    88
    val xs' = map_filter f xs;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    89
  in
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    90
    if null xs orelse not (null xs') then (xs', src')
19485
5385c9d86c2a renamed Source.mapfilter to Source.map_filter;
wenzelm
parents: 15531
diff changeset
    91
    else drain_map_filter f prompt src'
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    92
  end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    93
19485
5385c9d86c2a renamed Source.mapfilter to Source.map_filter;
wenzelm
parents: 15531
diff changeset
    94
fun map_filter f src = make_source [] src default_prompt (drain_map_filter f);
5385c9d86c2a renamed Source.mapfilter to Source.map_filter;
wenzelm
parents: 15531
diff changeset
    95
fun filter pred = map_filter (fn x => if pred x then SOME x else NONE);
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    96
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    97
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    98
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    99
(** build sources **)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   100
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   101
(* list source *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   102
14727
ab06e87e5c83 Source.of_list: no buffer limitation (now pointless due to tail-recursive Scan.repeat);
wenzelm
parents: 10746
diff changeset
   103
fun of_list xs = make_source [] xs default_prompt (fn _ => fn xs => (xs, []));
37903
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   104
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   105
fun exhausted src = of_list (exhaust src);
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   106
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   107
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   108
(* string source *)
6181
128646d4a975 of_file: Path.T, Position.T;
wenzelm
parents: 6118
diff changeset
   109
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 38253
diff changeset
   110
val of_string = of_list o raw_explode;
23875
e22705ccc07d added of_string_limited (more efficient for partial scans);
wenzelm
parents: 23700
diff changeset
   111
37903
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   112
fun of_string_limited limit str =
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   113
  make_source [] (Substring.full str) default_prompt
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   114
    (fn _ => fn s =>
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   115
      let
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   116
        val (s1, s2) = Substring.splitAt (s, Int.min (Substring.size s, limit));
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   117
        val cs = map String.str (Substring.explode s1);
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   118
      in (cs, s2) end);
9123
f8f54877a18c added exhausted: ('a, 'b) source -> ('a, 'a list) source;
wenzelm
parents: 8806
diff changeset
   119
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   120
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   121
(* stream source *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   122
24232
a70360a54e5c stream source: non-critical, assuming exclusive ownership;
wenzelm
parents: 24064
diff changeset
   123
fun slurp_input instream =
20737
de54cafdf3ef of_stream/tty: slurp input eagerly;
wenzelm
parents: 20642
diff changeset
   124
  let
de54cafdf3ef of_stream/tty: slurp input eagerly;
wenzelm
parents: 20642
diff changeset
   125
    fun slurp () =
de54cafdf3ef of_stream/tty: slurp input eagerly;
wenzelm
parents: 20642
diff changeset
   126
      (case TextIO.canInput (instream, 1) handle IO.Io _ => NONE of
de54cafdf3ef of_stream/tty: slurp input eagerly;
wenzelm
parents: 20642
diff changeset
   127
        NONE => []
de54cafdf3ef of_stream/tty: slurp input eagerly;
wenzelm
parents: 20642
diff changeset
   128
      | SOME 0 => []
de54cafdf3ef of_stream/tty: slurp input eagerly;
wenzelm
parents: 20642
diff changeset
   129
      | SOME _ => TextIO.input instream :: slurp ());
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 38253
diff changeset
   130
  in maps raw_explode (slurp ()) end;
20737
de54cafdf3ef of_stream/tty: slurp input eagerly;
wenzelm
parents: 20642
diff changeset
   131
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 37903
diff changeset
   132
fun tty in_stream = make_source [] () default_prompt (fn prompt => fn () =>
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 37903
diff changeset
   133
  let val input = slurp_input in_stream in
20737
de54cafdf3ef of_stream/tty: slurp input eagerly;
wenzelm
parents: 20642
diff changeset
   134
    if exists (fn c => c = "\n") input then (input, ())
de54cafdf3ef of_stream/tty: slurp input eagerly;
wenzelm
parents: 20642
diff changeset
   135
    else
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 37903
diff changeset
   136
      (case (Output.prompt prompt; TextIO.inputLine in_stream) of
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 38253
diff changeset
   137
        SOME line => (input @ raw_explode line, ())
25575
fee953b45015 output_prompt: CRITICAL;
wenzelm
parents: 24232
diff changeset
   138
      | NONE => (input, ()))
25846
f5fb187ae10d removed unused of_stream;
wenzelm
parents: 25575
diff changeset
   139
  end);
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   140
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   141
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   142
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   143
(** cascade sources **)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   144
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   145
(* state-based *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   146
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   147
fun drain_source' stopper scan opt_recover prompt (state, src) =
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   148
  let
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   149
    val drain = Scan.drain prompt get_prompt stopper;
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   150
    val (xs, s) = get_prompt prompt src;
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   151
    val inp = ((state, xs), s);
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   152
    val ((ys, (state', xs')), src') =
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   153
      if null xs then (([], (state, [])), s)
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   154
      else
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   155
        (case opt_recover of
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   156
          NONE => drain (Scan.error scan) inp
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   157
        | SOME (interactive, recover) =>
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   158
            (drain (Scan.catch scan) inp handle Fail msg =>
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   159
              (if interactive then Output.error_msg msg else ();
27732
8dbf5761a24a abstract type Scan.stopper;
wenzelm
parents: 25846
diff changeset
   160
                drain (Scan.unless (Scan.lift (Scan.one (Scan.is_stopper stopper))) (recover msg)) inp)));
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   161
  in (ys, (state', unget (xs', src'))) end;
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   162
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   163
fun source' init_state stopper scan recover src =
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   164
  make_source [] (init_state, src) default_prompt (drain_source' stopper scan recover);
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   165
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   166
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   167
(* non state-based *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   168
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   169
fun drain_source stopper scan opt_recover prompt =
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   170
  Scan.unlift (drain_source' stopper (Scan.lift scan)
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   171
    (Option.map (fn (int, r) => (int, Scan.lift o r)) opt_recover) prompt);
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   172
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   173
fun source stopper scan recover src =
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   174
  make_source [] src default_prompt (drain_source stopper scan recover);
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   175
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   176
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   177
end;