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