src/Pure/General/source.ML
author wenzelm
Fri, 31 Oct 2014 21:10:11 +0100
changeset 58850 1bb0ad7827b4
parent 54387 890e983cb07b
child 58863 64e571275b36
permissions -rw-r--r--
discontinued obsolete tty and prompt;
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
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    10
  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
    11
  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
    12
  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
    13
  val exhaust: ('a, 'b) source -> 'a list
19485
5385c9d86c2a renamed Source.mapfilter to Source.map_filter;
wenzelm
parents: 15531
diff changeset
    14
  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
    15
  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
    16
  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
    17
  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
    18
  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
    19
  val of_string_limited: int -> string -> (string, substring) source
27732
8dbf5761a24a abstract type Scan.stopper;
wenzelm
parents: 25846
diff changeset
    20
  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
    21
    (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
    22
    ('b, 'e) source -> ('c, 'a * ('b, 'e) source) source
27732
8dbf5761a24a abstract type Scan.stopper;
wenzelm
parents: 25846
diff changeset
    23
  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
    24
    (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
    25
    ('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
    26
end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    27
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    28
structure Source: SOURCE =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    29
struct
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
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    32
(** datatype source **)
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
datatype ('a, 'b) source =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    35
  Source of
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    36
   {buffer: 'a list,
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    37
    info: 'b,
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    38
    drain: 'b -> 'a list * 'b};
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    39
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    40
fun make_source buffer info drain =
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    41
  Source {buffer = buffer, info = info, drain = drain};
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    42
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
(* get / unget *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    45
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    46
fun get (Source {buffer = [], info, drain}) =
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    47
      let val (xs, info') = drain info
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    48
      in (xs, make_source [] info' drain) end
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    49
  | get (Source {buffer, info, drain}) = (buffer, make_source [] info drain);
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    50
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    51
fun unget (xs, Source {buffer, info, drain}) = make_source (xs @ buffer) info drain;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    52
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    53
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    54
(* variations on get *)
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
fun get_single src =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    57
  (case get src of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    58
    ([], _) => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    59
  | (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
    60
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    61
fun exhaust src =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    62
  (case get src of
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
  | (xs, src') => xs @ exhaust src');
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    65
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
(* (map)filter *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    68
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    69
fun drain_map_filter f src =
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    70
  let
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    71
    val (xs, src') = get src;
19485
5385c9d86c2a renamed Source.mapfilter to Source.map_filter;
wenzelm
parents: 15531
diff changeset
    72
    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
    73
  in
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    74
    if null xs orelse not (null xs') then (xs', src')
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    75
    else drain_map_filter f src'
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    76
  end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    77
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    78
fun map_filter f src = make_source [] src (drain_map_filter f);
19485
5385c9d86c2a renamed Source.mapfilter to Source.map_filter;
wenzelm
parents: 15531
diff changeset
    79
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
    80
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
(** build sources **)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    84
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    85
(* list source *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    86
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    87
fun of_list xs = make_source [] xs (fn xs => (xs, []));
37903
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
    88
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
    89
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
    90
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
    91
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
    92
(* string source *)
6181
128646d4a975 of_file: Path.T, Position.T;
wenzelm
parents: 6118
diff changeset
    93
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 38253
diff changeset
    94
val of_string = of_list o raw_explode;
23875
e22705ccc07d added of_string_limited (more efficient for partial scans);
wenzelm
parents: 23700
diff changeset
    95
37903
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
    96
fun of_string_limited limit str =
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    97
  make_source [] (Substring.full str)
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
    98
    (fn s =>
37903
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
    99
      let
b7ae269c0d68 replaced Source.of_list_limited by slightly more economic Source.of_string_limited;
wenzelm
parents: 29606
diff changeset
   100
        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
   101
        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
   102
      in (cs, s2) end);
9123
f8f54877a18c added exhausted: ('a, 'b) source -> ('a, 'a list) source;
wenzelm
parents: 8806
diff changeset
   103
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   104
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   105
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   106
(** cascade sources **)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   107
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   108
(* state-based *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   109
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
   110
fun drain_source' stopper scan opt_recover (state, src) =
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   111
  let
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
   112
    val drain = Scan.drain get stopper;
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
   113
    val (xs, s) = get src;
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   114
    val inp = ((state, xs), s);
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   115
    val ((ys, (state', xs')), src') =
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   116
      if null xs then (([], (state, [])), s)
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   117
      else
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   118
        (case opt_recover of
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   119
          NONE => drain (Scan.error scan) inp
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   120
        | SOME (interactive, recover) =>
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   121
            (drain (Scan.catch scan) inp handle Fail msg =>
54387
890e983cb07b tuned signature;
wenzelm
parents: 40627
diff changeset
   122
              (if interactive then Output.error_message msg else ();
890e983cb07b tuned signature;
wenzelm
parents: 40627
diff changeset
   123
                drain (Scan.unless (Scan.lift (Scan.one (Scan.is_stopper stopper))) (recover msg))
890e983cb07b tuned signature;
wenzelm
parents: 40627
diff changeset
   124
                  inp)));
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   125
  in (ys, (state', unget (xs', src'))) end;
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   126
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   127
fun source' init_state stopper scan recover src =
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
   128
  make_source [] (init_state, src) (drain_source' stopper scan recover);
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   129
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   130
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   131
(* non state-based *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   132
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
   133
fun drain_source stopper scan opt_recover =
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   134
  Scan.unlift (drain_source' stopper (Scan.lift scan)
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
   135
    (Option.map (fn (int, r) => (int, Scan.lift o r)) opt_recover));
23700
fb1102e98cd4 moved source cascading from scan.ML to source.ML;
wenzelm
parents: 23682
diff changeset
   136
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   137
fun source stopper scan recover src =
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 54387
diff changeset
   138
  make_source [] src (drain_source stopper scan recover);
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   139
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   140
end;