src/Pure/General/scan.ML
author wenzelm
Wed, 16 Jun 2004 20:37:00 +0200
changeset 14955 08ee855c1d94
parent 14927 66d797e1b950
child 14981 e73f8140af78
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11523
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
     1
(*  Title:      Pure/General/scan.ML
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
     2
    ID:         $Id$
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
     3
    Author:     Markus Wenzel and Tobias Nipkow, TU Muenchen
8806
wenzelm
parents: 8653
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
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
Generic scanners (for potentially infinite input).
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     7
*)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     8
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     9
infix 5 -- :-- |-- --| ^^;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    10
infix 3 >>;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    11
infix 0 ||;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    12
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    13
signature BASIC_SCAN =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    14
sig
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    15
  (*error msg handler*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    16
  val !! : ('a * string option -> string) -> ('a -> 'b) -> 'a -> 'b
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    17
  (*apply function*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    18
  val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    19
  (*alternative*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    20
  val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    21
  (*sequential pairing*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    22
  val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    23
  (*dependent pairing*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    24
  val :-- : ('a -> 'b * 'c) * ('b -> 'c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    25
  (*forget fst*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    26
  val |-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'd * 'e
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    27
  (*forget snd*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    28
  val --| : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'b * 'e
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    29
  (*concatenation*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    30
  val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    31
  (*one element literal*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    32
  val $$ : ''a -> ''a list -> ''a * ''a list
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    33
end;
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
signature SCAN =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    36
sig
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    37
  include BASIC_SCAN
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    38
  val fail: 'a -> 'b
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    39
  val fail_with: ('a -> string) -> 'a -> 'b
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    40
  val succeed: 'a -> 'b -> 'a * 'b
14927
66d797e1b950 added trace (inefficient for very long input);
wenzelm
parents: 14907
diff changeset
    41
  val this: ''a list -> ''a list -> ''a list * ''a list
66d797e1b950 added trace (inefficient for very long input);
wenzelm
parents: 14907
diff changeset
    42
  val this_string: string -> string list -> string * string list
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    43
  val one: ('a -> bool) -> 'a list -> 'a * 'a list
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    44
  val any: ('a -> bool) -> 'a list -> 'a list * 'a list
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    45
  val any1: ('a -> bool) -> 'a list -> 'a list * 'a list
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    46
  val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    47
  val option: ('a -> 'b * 'a) -> 'a -> 'b option * 'a
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    48
  val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    49
  val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    50
  val max: ('a * 'a -> bool) -> ('b -> 'a * 'b) -> ('b -> 'a * 'b) -> 'b -> 'a * 'b
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    51
  val ahead: ('a -> 'b * 'c) -> 'a -> 'b * 'a
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    52
  val unless: ('a -> 'b * 'a) -> ('a -> 'c * 'd) -> 'a -> 'c * 'd
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    53
  val first: ('a -> 'b) list -> 'a -> 'b
14927
66d797e1b950 added trace (inefficient for very long input);
wenzelm
parents: 14907
diff changeset
    54
  val trace: ('a list -> 'b * 'c list) -> 'a list -> ('b * 'a list) * 'c list
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    55
  val state: 'a * 'b -> 'a * ('a * 'b)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    56
  val depend: ('a -> 'b -> ('c * 'd) * 'e) -> 'a * 'b -> 'd * ('c * 'e)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    57
  val lift: ('a -> 'b * 'c) -> 'd * 'a -> 'b * ('d * 'c)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    58
  val pass: 'a -> ('a * 'b -> 'c * ('d * 'e)) -> 'b -> 'c * 'e
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    59
  val try: ('a -> 'b) -> 'a -> 'b
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    60
  val force: ('a -> 'b) -> 'a -> 'b
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    61
  val prompt: string -> ('a -> 'b) -> 'a -> 'b
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    62
  val finite': 'a * ('a -> bool) -> ('b * 'a list -> 'c * ('d * 'a list))
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    63
    -> 'b * 'a list -> 'c * ('d * 'a list)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    64
  val finite: 'a * ('a -> bool) -> ('a list -> 'b * 'a list) -> 'a list -> 'b * 'a list
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    65
  val read: 'a * ('a -> bool) -> ('a list -> 'b * 'a list) -> 'a list -> 'b option
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    66
  val catch: ('a -> 'b) -> 'a -> 'b
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    67
  val error: ('a -> 'b) -> 'a -> 'b
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    68
  val source': string -> (string -> 'a -> 'b list * 'a) -> ('b list * 'a -> 'c) ->
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    69
    'b * ('b -> bool) -> ('d * 'b list -> 'e list * ('d * 'b list)) ->
10746
01e2d857fb78 recover: result;
wenzelm
parents: 9122
diff changeset
    70
    ('d * 'b list -> 'e list * ('d * 'b list)) option -> 'd * 'a -> 'e list * ('d * 'c)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    71
  val source: string -> (string -> 'a -> 'b list * 'a) -> ('b list * 'a -> 'c) ->
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    72
    'b * ('b -> bool) -> ('b list -> 'd list * 'b list) ->
10746
01e2d857fb78 recover: result;
wenzelm
parents: 9122
diff changeset
    73
    ('b list -> 'd list * 'b list) option -> 'a -> 'd list * 'c
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    74
  val single: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    75
  val bulk: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    76
  type lexicon
7025
afbd8241797b tuned dest_lexicon;
wenzelm
parents: 6640
diff changeset
    77
  val dest_lexicon: lexicon -> string list
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    78
  val make_lexicon: string list list -> lexicon
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    79
  val empty_lexicon: lexicon
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    80
  val extend_lexicon: lexicon -> string list list -> lexicon
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    81
  val merge_lexicons: lexicon -> lexicon -> lexicon
14686
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
    82
  val is_literal: lexicon -> string list -> bool
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    83
  val literal: lexicon -> string list -> string list * string list
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    84
end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    85
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    86
structure Scan: SCAN =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    87
struct
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    88
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    89
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    90
(** scanners **)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    91
11523
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
    92
exception MORE of string option;        (*need more input (prompt)*)
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
    93
exception FAIL of string option;        (*try alternatives (reason of failure)*)
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
    94
exception ABORT of string;              (*dead end*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    95
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
(* scanner combinators *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    98
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
    99
(*dependent pairing*)
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   100
fun (sc1 :-- sc2) toks =
14108
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   101
  let
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   102
    val (x, toks2) = sc1 toks
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   103
    val (y, toks3) = sc2 x toks2
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   104
  in ((x, y), toks3) end;
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   105
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
   106
(*sequential pairing*)
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   107
fun (sc1 -- sc2) toks =
14108
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   108
  let
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   109
    val (x, toks2) = sc1 toks
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   110
    val (y, toks3) = sc2 toks2
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   111
  in ((x, y), toks3) end;
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   112
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
   113
(*application*)
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   114
fun (sc >> f) toks =
14108
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   115
  let val (x, toks2) = sc toks
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   116
  in (f x, toks2) end;
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   117
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
   118
(*forget snd*)
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   119
fun (sc1 --| sc2) toks =
14108
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   120
  let
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   121
    val (x, toks2) = sc1 toks
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   122
    val (_, toks3) = sc2 toks2
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   123
  in (x, toks3) end;
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   124
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
   125
(*forget fst*)
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   126
fun (sc1 |-- sc2) toks =
14108
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   127
  let val (_, toks2) = sc1 toks
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   128
  in sc2 toks2 end;
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   129
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
   130
(*concatenation*)
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   131
fun (sc1 ^^ sc2) toks =
14108
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   132
  let
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   133
    val (x, toks2) = sc1 toks
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   134
    val (y, toks3) = sc2 toks2
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   135
  in (x ^ y, toks3) end;
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   136
14677
33a37f091dc5 tuned presentation;
wenzelm
parents: 14108
diff changeset
   137
(*alternative*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   138
fun (scan1 || scan2) xs = scan1 xs handle FAIL _ => scan2 xs;
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
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   141
(* generic scanners *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   142
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   143
fun fail _ = raise FAIL None;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   144
fun fail_with msg_of xs = raise FAIL (Some (msg_of xs));
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   145
fun succeed y xs = (y, xs);
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   146
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   147
fun one _ [] = raise MORE None
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   148
  | one pred (x :: xs) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   149
      if pred x then (x, xs) else raise FAIL None;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   150
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   151
fun $$ _ [] = raise MORE None
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   152
  | $$ a (x :: xs) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   153
      if a = x then (x, xs) else raise FAIL None;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   154
14833
30556b84af7c Output.error;
wenzelm
parents: 14726
diff changeset
   155
fun this ys xs =
14726
9657c23cc3e7 added Scan.list;
wenzelm
parents: 14686
diff changeset
   156
  let
9657c23cc3e7 added Scan.list;
wenzelm
parents: 14686
diff changeset
   157
    fun drop_prefix [] xs = xs
9657c23cc3e7 added Scan.list;
wenzelm
parents: 14686
diff changeset
   158
      | drop_prefix (_ :: _) [] = raise MORE None
9657c23cc3e7 added Scan.list;
wenzelm
parents: 14686
diff changeset
   159
      | drop_prefix (y :: ys) (x :: xs) =
9657c23cc3e7 added Scan.list;
wenzelm
parents: 14686
diff changeset
   160
          if y = x then drop_prefix ys xs else raise FAIL None;
9657c23cc3e7 added Scan.list;
wenzelm
parents: 14686
diff changeset
   161
  in (ys, drop_prefix ys xs) end;
9657c23cc3e7 added Scan.list;
wenzelm
parents: 14686
diff changeset
   162
14955
wenzelm
parents: 14927
diff changeset
   163
fun this_string s = this (explode s) >> K s;  (*primitive string -- no symbols yet!*)
14907
c77fda9b6cf0 added this_string;
wenzelm
parents: 14833
diff changeset
   164
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   165
fun any _ [] = raise MORE None
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   166
  | any pred (lst as x :: xs) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   167
      if pred x then apfst (cons x) (any pred xs)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   168
      else ([], lst);
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   169
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   170
fun any1 p toks =
14108
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   171
  let
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   172
    val (x, toks2) = one p toks
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   173
    val (xs,toks3) = any p toks2
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   174
  in (x :: xs, toks3) end;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   175
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   176
fun optional scan def =  scan || succeed def
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   177
fun option scan = (scan >> Some) || succeed None
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   178
13795
cfa3441c5238 Some tuning:
berghofe
parents: 11523
diff changeset
   179
fun repeat scan =
14108
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   180
  let fun rep ys xs = (case (Some (scan xs) handle FAIL _ => None) of
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   181
    None => (rev ys, xs) | Some (y, xs') => rep (y :: ys) xs')
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   182
  in rep [] end;
13795
cfa3441c5238 Some tuning:
berghofe
parents: 11523
diff changeset
   183
14078
cddad2aa025b integrated optimizations by Sebastian Skalberg,
kleing
parents: 13795
diff changeset
   184
fun repeat1 scan toks =
14108
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   185
  let
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   186
    val (x, toks2) = scan toks
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   187
    val (xs, toks3) = repeat scan toks2
eaf3c75f2c8e Restored old (tail recursive!) version of repeat.
berghofe
parents: 14078
diff changeset
   188
  in (x :: xs, toks3) end;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   189
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   190
fun max leq scan1 scan2 xs =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   191
  (case (option scan1 xs, option scan2 xs) of
11523
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   192
    ((None, _), (None, _)) => raise FAIL None           (*looses FAIL msg!*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   193
  | ((Some tok1, xs'), (None, _)) => (tok1, xs')
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   194
  | ((None, _), (Some tok2, xs')) => (tok2, xs')
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   195
  | ((Some tok1, xs1'), (Some tok2, xs2')) =>
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   196
      if leq (tok2, tok1) then (tok1, xs1') else (tok2, xs2'));
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   197
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   198
fun ahead scan xs = (fst (scan xs), xs);
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   199
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   200
fun unless test scan =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   201
  ahead (option test) :-- (fn None => scan | _ => fail) >> #2;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   202
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   203
fun first [] = fail
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   204
  | first (scan :: scans) = scan || first scans;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   205
14927
66d797e1b950 added trace (inefficient for very long input);
wenzelm
parents: 14907
diff changeset
   206
fun trace scan toks =
66d797e1b950 added trace (inefficient for very long input);
wenzelm
parents: 14907
diff changeset
   207
  let val (x, toks') = scan toks
66d797e1b950 added trace (inefficient for very long input);
wenzelm
parents: 14907
diff changeset
   208
  in ((x, take (length toks - length toks', toks)), toks') end;
66d797e1b950 added trace (inefficient for very long input);
wenzelm
parents: 14907
diff changeset
   209
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   210
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   211
(* state based scanners *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   212
9122
addbea344673 added state: 'a * 'b -> 'a * ('a * 'b);
wenzelm
parents: 8806
diff changeset
   213
fun state (st, xs) = (st, (st, xs));
addbea344673 added state: 'a * 'b -> 'a * ('a * 'b);
wenzelm
parents: 8806
diff changeset
   214
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   215
fun depend scan (st, xs) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   216
  let val ((st', y), xs') = scan st xs
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   217
  in (y, (st', xs')) end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   218
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   219
fun lift scan (st, xs) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   220
  let val (y, xs') = scan xs
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   221
  in (y, (st, xs')) end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   222
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   223
fun pass st scan xs =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   224
  let val (y, (_, xs')) = scan (st, xs)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   225
  in (y, xs') end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   226
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   227
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   228
(* exception handling *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   229
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   230
fun !! err scan xs = scan xs handle FAIL msg => raise ABORT (err (xs, msg));
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   231
fun try scan xs = scan xs handle MORE _ => raise FAIL None | ABORT _ => raise FAIL None;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   232
fun force scan xs = scan xs handle MORE _ => raise FAIL None;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   233
fun prompt str scan xs = scan xs handle MORE None => raise MORE (Some str);
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   234
fun catch scan xs = scan xs handle ABORT msg => raise FAIL (Some msg);
14833
30556b84af7c Output.error;
wenzelm
parents: 14726
diff changeset
   235
fun error scan xs = scan xs handle ABORT msg => Output.error msg;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   236
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   237
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   238
(* finite scans *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   239
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   240
fun finite' (stopper, is_stopper) scan (state, input) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   241
  let
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   242
    fun lost () = raise ABORT "Scanner bug: lost stopper of finite scan!";
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   243
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   244
    fun stop [] = lost ()
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   245
      | stop lst =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   246
          let val (xs, x) = split_last lst
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   247
          in if is_stopper x then ((), xs) else lost () end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   248
  in
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   249
    if exists is_stopper input then
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   250
      raise ABORT "Stopper may not occur in input of finite scan!"
13795
cfa3441c5238 Some tuning:
berghofe
parents: 11523
diff changeset
   251
    else (force scan --| lift stop) (state, rev_append (rev input) [stopper])
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   252
  end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   253
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   254
fun finite stopper scan xs =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   255
  let val (y, ((), xs')) = finite' stopper (lift scan) ((), xs)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   256
  in (y, xs') end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   257
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   258
fun read stopper scan xs =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   259
  (case error (finite stopper (option scan)) xs of
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   260
    (y as Some _, []) => y
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   261
  | _ => None);
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   262
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   263
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   264
(* infinite scans -- draining state-based source *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   265
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   266
fun drain def_prmpt get stopper scan ((state, xs), src) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   267
  (scan (state, xs), src) handle MORE prmpt =>
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   268
    (case get (if_none prmpt def_prmpt) src of
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   269
      ([], _) => (finite' stopper scan (state, xs), src)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   270
    | (xs', src') => drain def_prmpt get stopper scan ((state, xs @ xs'), src'));
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   271
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   272
fun source' def_prmpt get unget stopper scanner opt_recover (state, src) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   273
  let
10746
01e2d857fb78 recover: result;
wenzelm
parents: 9122
diff changeset
   274
    val drain_with = drain def_prmpt get stopper;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   275
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   276
    fun drain_loop recover inp =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   277
      drain_with (catch scanner) inp handle FAIL msg =>
10746
01e2d857fb78 recover: result;
wenzelm
parents: 9122
diff changeset
   278
        (error_msg (if_none msg "Syntax error."); drain_with recover inp);
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   279
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   280
    val ((ys, (state', xs')), src') =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   281
      (case (get def_prmpt src, opt_recover) of
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   282
        (([], s), _) => (([], (state, [])), s)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   283
      | ((xs, s), None) => drain_with (error scanner) ((state, xs), s)
8653
a88e91792f0a recover: observe stopper;
wenzelm
parents: 7025
diff changeset
   284
      | ((xs, s), Some r) => drain_loop (unless (lift (one (#2 stopper))) r) ((state, xs), s));
a88e91792f0a recover: observe stopper;
wenzelm
parents: 7025
diff changeset
   285
  in (ys, (state', unget (xs', src'))) end;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   286
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   287
fun source def_prmpt get unget stopper scan opt_recover src =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   288
  let val (ys, ((), src')) =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   289
    source' def_prmpt get unget stopper (lift scan) (apsome lift opt_recover) ((), src)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   290
  in (ys, src') end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   291
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   292
fun single scan = scan >> (fn x => [x]);
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   293
fun bulk scan = scan -- repeat (try scan) >> (op ::);
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   294
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   295
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   296
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   297
(** datatype lexicon **)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   298
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   299
datatype lexicon =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   300
  Empty |
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   301
  Branch of string * string list * lexicon * lexicon * lexicon;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   302
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   303
val no_literal = [];
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   304
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   305
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   306
(* dest_lexicon *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   307
7025
afbd8241797b tuned dest_lexicon;
wenzelm
parents: 6640
diff changeset
   308
fun dest_lex Empty = []
afbd8241797b tuned dest_lexicon;
wenzelm
parents: 6640
diff changeset
   309
  | dest_lex (Branch (_, [], lt, eq, gt)) =
afbd8241797b tuned dest_lexicon;
wenzelm
parents: 6640
diff changeset
   310
      dest_lex lt @ dest_lex eq @ dest_lex gt
afbd8241797b tuned dest_lexicon;
wenzelm
parents: 6640
diff changeset
   311
  | dest_lex (Branch (_, cs, lt, eq, gt)) =
afbd8241797b tuned dest_lexicon;
wenzelm
parents: 6640
diff changeset
   312
      dest_lex lt @ [cs] @ dest_lex eq @ dest_lex gt;
afbd8241797b tuned dest_lexicon;
wenzelm
parents: 6640
diff changeset
   313
afbd8241797b tuned dest_lexicon;
wenzelm
parents: 6640
diff changeset
   314
val dest_lexicon = map implode o dest_lex;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   315
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   316
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   317
(* empty, extend, make, merge lexicons *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   318
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   319
val empty_lexicon = Empty;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   320
11523
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   321
fun extend_lexicon lexicon [] = lexicon
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   322
  | extend_lexicon lexicon chrss =
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   323
      let
11523
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   324
        fun ext (lex, chrs) =
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   325
          let
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   326
            fun add (Branch (d, a, lt, eq, gt)) (chs as c :: cs) =
14686
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   327
                  (case String.compare (c, d) of
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   328
                    LESS => Branch (d, a, add lt chs, eq, gt)
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   329
                  | EQUAL => Branch (d, if null cs then chrs else a, lt, add eq cs, gt)
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   330
                  | GREATER => Branch (d, a, lt, eq, add gt chs))
11523
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   331
              | add Empty [c] =
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   332
                  Branch (c, chrs, Empty, Empty, Empty)
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   333
              | add Empty (c :: cs) =
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   334
                  Branch (c, no_literal, Empty, add Empty cs, Empty)
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   335
              | add lex [] = lex;
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   336
          in add lex chrs end;
9a658fe20107 Tuned function extend_lexicon.
berghofe
parents: 10746
diff changeset
   337
      in foldl ext (lexicon, chrss \\ dest_lex lexicon) end;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   338
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   339
val make_lexicon = extend_lexicon empty_lexicon;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   340
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   341
fun merge_lexicons lex1 lex2 =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   342
  let
7025
afbd8241797b tuned dest_lexicon;
wenzelm
parents: 6640
diff changeset
   343
    val chss1 = dest_lex lex1;
afbd8241797b tuned dest_lexicon;
wenzelm
parents: 6640
diff changeset
   344
    val chss2 = dest_lex lex2;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   345
  in
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   346
    if chss2 subset chss1 then lex1
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   347
    else if chss1 subset chss2 then lex2
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   348
    else extend_lexicon lex1 chss2
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   349
  end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   350
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   351
14686
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   352
(* is_literal *)
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   353
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   354
fun is_literal Empty _ = false
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   355
  | is_literal _ [] = false
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   356
  | is_literal (Branch (d, a, lt, eq, gt)) (chs as c :: cs) =
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   357
      (case String.compare (c, d) of
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   358
        LESS => is_literal lt chs
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   359
      | EQUAL => a <> no_literal andalso null cs orelse is_literal eq cs
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   360
      | GREATER => is_literal gt chs);
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   361
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   362
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   363
(* scan literal *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   364
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   365
fun literal lex chrs =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   366
  let
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   367
    fun lit Empty res _ = res
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   368
      | lit (Branch _) _ [] = raise MORE None
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   369
      | lit (Branch (d, a, lt, eq, gt)) res (chs as c :: cs) =
14686
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   370
          (case String.compare (c, d) of
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   371
            LESS => lit lt res chs
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   372
          | EQUAL => lit eq (if a = no_literal then res else Some (a, cs)) cs
708c613370ab added is_literal;
wenzelm
parents: 14677
diff changeset
   373
          | GREATER => lit gt res chs);
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   374
  in
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   375
    (case lit lex None chrs of
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   376
      None => raise FAIL None
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   377
    | Some res => res)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   378
  end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   379
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   380
end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   381
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   382
structure BasicScan: BASIC_SCAN = Scan;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   383
open BasicScan;