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