author | wenzelm |
Fri, 23 May 2008 21:18:47 +0200 | |
changeset 26977 | e736139b553d |
parent 25999 | f8bcd311d501 |
child 27731 | a7444ded92cf |
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 |
|
24025 | 8 |
infix 5 -- :-- :|-- |-- --| ^^; |
25999 | 9 |
infixr 5 ::: @@@; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
10 |
infix 3 >>; |
23699 | 11 |
infixr 0 ||; |
6116
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 | 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 | 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 | 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 | 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 | 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 |
24025 | 25 |
(*projections*) |
26 |
val :|-- : ('a -> 'b * 'c) * ('b -> 'c -> 'd * 'e) -> 'a -> 'd * 'e |
|
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 -> 'd * 'e |
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 | 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 |
25999 | 31 |
val ::: : ('a -> 'b * 'c) * ('c -> 'b list * 'd) -> 'a -> 'b list * 'd |
32 |
val @@@ : ('a -> 'b list * 'c) * ('c -> 'b list * 'd) -> 'a -> 'b list * 'd |
|
14677 | 33 |
(*one element literal*) |
19291 | 34 |
val $$ : string -> string list -> string * string list |
19306 | 35 |
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
|
36 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
37 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
38 |
signature SCAN = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
39 |
sig |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
40 |
include BASIC_SCAN |
23699 | 41 |
val prompt: string -> ('a -> 'b) -> 'a -> 'b |
42 |
val error: ('a -> 'b) -> 'a -> 'b |
|
43 |
val catch: ('a -> 'b) -> 'a -> 'b (*exception Fail*) |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
44 |
val fail: 'a -> 'b |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
45 |
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
|
46 |
val succeed: 'a -> 'b -> 'a * 'b |
15664 | 47 |
val some: ('a -> 'b option) -> 'a list -> 'b * 'a list |
48 |
val one: ('a -> bool) -> 'a list -> 'a * 'a list |
|
19291 | 49 |
val this: string list -> string list -> string list * string list |
14927 | 50 |
val this_string: string -> string list -> string * string list |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19473
diff
changeset
|
51 |
val many: ('a -> bool) -> 'a list -> 'a list * 'a list |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19473
diff
changeset
|
52 |
val many1: ('a -> bool) -> 'a list -> 'a list * 'a list |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
23699 | 57 |
val single: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
58 |
val bulk: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
val first: ('a -> 'b) list -> 'a -> 'b |
14677 | 63 |
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
|
64 |
val depend: ('a -> 'b -> ('c * 'd) * 'e) -> 'a * 'b -> 'd * ('c * 'e) |
15664 | 65 |
val peek: ('a -> 'b -> 'c * 'd) -> 'a * 'b -> 'c * ('a * 'd) |
66 |
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
|
67 |
val lift: ('a -> 'b * 'c) -> 'd * 'a -> 'b * ('d * 'c) |
23699 | 68 |
val unlift: (unit * 'a -> 'b * ('c * 'd)) -> 'a -> 'b * 'd |
15664 | 69 |
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
|
70 |
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
|
71 |
-> '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
|
72 |
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
|
73 |
val read: 'a * ('a -> bool) -> ('a list -> 'b * 'a list) -> 'a list -> 'b option |
23699 | 74 |
val drain: string -> (string -> 'a -> 'b list * 'a) -> 'b * ('b -> bool) -> |
75 |
('c * 'b list -> 'd * ('e * 'b list)) -> ('c * 'b list) * 'a -> ('d * ('e * 'b list)) * 'a |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
76 |
type lexicon |
7025 | 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 |
22112 | 80 |
val extend_lexicon: string list list -> lexicon -> lexicon |
6116
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 | 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 |
|
23699 | 92 |
(* exceptions *) |
93 |
||
11523 | 94 |
exception MORE of string option; (*need more input (prompt)*) |
95 |
exception FAIL of string option; (*try alternatives (reason of failure)*) |
|
96 |
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
|
97 |
|
23699 | 98 |
fun !! err scan xs = scan xs handle FAIL msg => raise ABORT (err (xs, msg)); |
99 |
fun permissive scan xs = scan xs handle MORE _ => raise FAIL NONE | ABORT _ => raise FAIL NONE; |
|
100 |
fun strict scan xs = scan xs handle MORE _ => raise FAIL NONE; |
|
101 |
fun prompt str scan xs = scan xs handle MORE NONE => raise MORE (SOME str); |
|
102 |
fun error scan xs = scan xs handle ABORT msg => Library.error msg; |
|
103 |
||
104 |
fun catch scan xs = scan xs |
|
105 |
handle ABORT msg => raise Fail msg |
|
106 |
| FAIL msg => raise Fail (the_default "Syntax error." msg); |
|
107 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
108 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
109 |
(* scanner combinators *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
110 |
|
19306 | 111 |
fun (scan >> f) xs = scan xs |>> f; |
14078 | 112 |
|
19306 | 113 |
fun (scan1 || scan2) xs = scan1 xs handle FAIL _ => scan2 xs; |
14078 | 114 |
|
19306 | 115 |
fun (scan1 :-- scan2) xs = |
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
116 |
let |
19306 | 117 |
val (x, ys) = scan1 xs; |
118 |
val (y, zs) = scan2 x ys; |
|
119 |
in ((x, y), zs) end; |
|
14078 | 120 |
|
19306 | 121 |
fun (scan1 -- scan2) = scan1 :-- (fn _ => scan2); |
24025 | 122 |
fun (scan1 :|-- scan2) = scan1 :-- scan2 >> #2; |
19306 | 123 |
fun (scan1 |-- scan2) = scan1 -- scan2 >> #2; |
124 |
fun (scan1 --| scan2) = scan1 -- scan2 >> #1; |
|
125 |
fun (scan1 ^^ scan2) = scan1 -- scan2 >> op ^; |
|
25999 | 126 |
fun (scan1 ::: scan2) = scan1 -- scan2 >> op ::; |
127 |
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
|
128 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
129 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
130 |
(* generic scanners *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
131 |
|
15531 | 132 |
fun fail _ = raise FAIL NONE; |
133 |
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
|
134 |
fun succeed y xs = (y, xs); |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
135 |
|
15664 | 136 |
fun some _ [] = raise MORE NONE |
137 |
| some f (x :: xs) = |
|
138 |
(case f x of SOME y => (y, xs) | _ => raise FAIL NONE); |
|
139 |
||
15531 | 140 |
fun one _ [] = raise MORE NONE |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
141 |
| one pred (x :: xs) = |
15531 | 142 |
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
|
143 |
|
19306 | 144 |
fun $$ a = one (fn s: string => s = a); |
145 |
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
|
146 |
|
14833 | 147 |
fun this ys xs = |
14726 | 148 |
let |
149 |
fun drop_prefix [] xs = xs |
|
15531 | 150 |
| drop_prefix (_ :: _) [] = raise MORE NONE |
14726 | 151 |
| drop_prefix (y :: ys) (x :: xs) = |
19291 | 152 |
if (y: string) = x then drop_prefix ys xs else raise FAIL NONE; |
14726 | 153 |
in (ys, drop_prefix ys xs) end; |
154 |
||
15664 | 155 |
fun this_string s = this (explode s) >> K s; (*primitive string -- no symbols here!*) |
14907 | 156 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19473
diff
changeset
|
157 |
fun many _ [] = raise MORE NONE |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19473
diff
changeset
|
158 |
| many pred (lst as x :: xs) = |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19473
diff
changeset
|
159 |
if pred x then apfst (cons x) (many pred xs) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
160 |
else ([], lst); |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
161 |
|
25999 | 162 |
fun many1 pred = one pred ::: many pred; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
163 |
|
15664 | 164 |
fun optional scan def = scan || succeed def; |
165 |
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
|
166 |
|
13795 | 167 |
fun repeat scan = |
15664 | 168 |
let |
169 |
fun rep ys xs = |
|
170 |
(case (SOME (scan xs) handle FAIL _ => NONE) of |
|
171 |
NONE => (rev ys, xs) |
|
172 |
| SOME (y, xs') => rep (y :: ys) xs'); |
|
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
173 |
in rep [] end; |
13795 | 174 |
|
25999 | 175 |
fun repeat1 scan = scan ::: repeat scan; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
176 |
|
23699 | 177 |
fun single scan = scan >> (fn x => [x]); |
178 |
fun bulk scan = scan -- repeat (permissive scan) >> (op ::); |
|
179 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
180 |
fun max leq scan1 scan2 xs = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
181 |
(case (option scan1 xs, option scan2 xs) of |
15531 | 182 |
((NONE, _), (NONE, _)) => raise FAIL NONE (*looses FAIL msg!*) |
183 |
| ((SOME tok1, xs'), (NONE, _)) => (tok1, xs') |
|
184 |
| ((NONE, _), (SOME tok2, xs')) => (tok2, xs') |
|
185 |
| ((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
|
186 |
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
|
187 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
188 |
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
|
189 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
190 |
fun unless test scan = |
15531 | 191 |
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
|
192 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
193 |
fun first [] = fail |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
194 |
| first (scan :: scans) = scan || first scans; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
195 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
196 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
197 |
(* state based scanners *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
198 |
|
9122 | 199 |
fun state (st, xs) = (st, (st, xs)); |
200 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
201 |
fun depend scan (st, xs) = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
202 |
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
|
203 |
in (y, (st', xs')) end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
204 |
|
15664 | 205 |
fun peek scan = depend (fn st => scan st >> pair st); |
206 |
||
207 |
fun pass st scan xs = |
|
208 |
let val (y, (_, xs')) = scan (st, xs) |
|
209 |
in (y, xs') end; |
|
210 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
211 |
fun lift scan (st, xs) = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
212 |
let val (y, xs') = scan xs |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
213 |
in (y, (st, xs')) end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
214 |
|
15664 | 215 |
fun unlift scan = pass () scan; |
216 |
||
217 |
||
218 |
(* trace input *) |
|
219 |
||
23699 | 220 |
fun trace scan xs = |
221 |
let val (y, xs') = scan xs |
|
222 |
in ((y, Library.take (length xs - length xs', xs)), xs') end; |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
223 |
|
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 |
(* finite scans *) |
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 |
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
|
228 |
let |
24595 | 229 |
fun lost () = raise ABORT "Bad scanner: lost stopper of finite scan!"; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
230 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
231 |
fun stop [] = lost () |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
232 |
| stop lst = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
233 |
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
|
234 |
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
|
235 |
in |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
236 |
if exists is_stopper input then |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
237 |
raise ABORT "Stopper may not occur in input of finite scan!" |
23674 | 238 |
else (strict 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
|
239 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
240 |
|
15664 | 241 |
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
|
242 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
243 |
fun read stopper scan xs = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
244 |
(case error (finite stopper (option scan)) xs of |
15531 | 245 |
(y as SOME _, []) => y |
246 |
| _ => NONE); |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
247 |
|
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 |
(* infinite scans -- draining state-based source *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
250 |
|
23699 | 251 |
fun drain def_prompt get stopper scan ((state, xs), src) = |
252 |
(scan (state, xs), src) handle MORE prompt => |
|
253 |
(case get (the_default def_prompt prompt) src of |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
254 |
([], _) => (finite' stopper scan (state, xs), src) |
23699 | 255 |
| (xs', src') => drain def_prompt get stopper scan ((state, xs @ xs'), src')); |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
256 |
|
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 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
259 |
(** datatype lexicon **) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
260 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
261 |
datatype lexicon = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
262 |
Empty | |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
263 |
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
|
264 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
265 |
val no_literal = []; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
266 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
267 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
268 |
(* dest_lexicon *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
269 |
|
7025 | 270 |
fun dest_lex Empty = [] |
271 |
| dest_lex (Branch (_, [], lt, eq, gt)) = |
|
272 |
dest_lex lt @ dest_lex eq @ dest_lex gt |
|
273 |
| dest_lex (Branch (_, cs, lt, eq, gt)) = |
|
274 |
dest_lex lt @ [cs] @ dest_lex eq @ dest_lex gt; |
|
275 |
||
276 |
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
|
277 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
278 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
279 |
(* empty, extend, make, merge lexicons *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
280 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
281 |
val empty_lexicon = Empty; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
282 |
|
22112 | 283 |
fun extend_lexicon [] lexicon = lexicon |
284 |
| extend_lexicon chrss lexicon = |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
285 |
let |
19306 | 286 |
fun ext chrs lex = |
11523 | 287 |
let |
19306 | 288 |
fun add (chs as c :: cs) (Branch (d, a, lt, eq, gt)) = |
289 |
(case fast_string_ord (c, d) of |
|
290 |
LESS => Branch (d, a, add chs lt, eq, gt) |
|
291 |
| EQUAL => Branch (d, if null cs then chrs else a, lt, add cs eq, gt) |
|
292 |
| GREATER => Branch (d, a, lt, eq, add chs gt)) |
|
293 |
| add [c] Empty = Branch (c, chrs, Empty, Empty, Empty) |
|
294 |
| add (c :: cs) Empty = Branch (c, no_literal, Empty, add cs Empty, Empty) |
|
295 |
| add [] lex = lex; |
|
296 |
in add chrs lex end; |
|
297 |
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
|
298 |
|
22112 | 299 |
fun make_lexicon chrss = extend_lexicon chrss empty_lexicon; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
300 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
301 |
fun merge_lexicons lex1 lex2 = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
302 |
let |
7025 | 303 |
val chss1 = dest_lex lex1; |
304 |
val chss2 = dest_lex lex2; |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
305 |
in |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
306 |
if chss2 subset chss1 then lex1 |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
307 |
else if chss1 subset chss2 then lex2 |
22112 | 308 |
else extend_lexicon chss2 lex1 |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
309 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
310 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
311 |
|
14686 | 312 |
(* is_literal *) |
313 |
||
314 |
fun is_literal Empty _ = false |
|
315 |
| is_literal _ [] = false |
|
316 |
| is_literal (Branch (d, a, lt, eq, gt)) (chs as c :: cs) = |
|
19306 | 317 |
(case fast_string_ord (c, d) of |
14686 | 318 |
LESS => is_literal lt chs |
319 |
| EQUAL => a <> no_literal andalso null cs orelse is_literal eq cs |
|
320 |
| GREATER => is_literal gt chs); |
|
321 |
||
322 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
323 |
(* scan literal *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
324 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
325 |
fun literal lex chrs = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
326 |
let |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
327 |
fun lit Empty res _ = res |
15531 | 328 |
| lit (Branch _) _ [] = raise MORE NONE |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
329 |
| lit (Branch (d, a, lt, eq, gt)) res (chs as c :: cs) = |
19306 | 330 |
(case fast_string_ord (c, d) of |
14686 | 331 |
LESS => lit lt res chs |
15531 | 332 |
| EQUAL => lit eq (if a = no_literal then res else SOME (a, cs)) cs |
14686 | 333 |
| GREATER => lit gt res chs); |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
334 |
in |
15531 | 335 |
(case lit lex NONE chrs of |
336 |
NONE => raise FAIL NONE |
|
337 |
| SOME res => res) |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
338 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
339 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
340 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
341 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
342 |
structure BasicScan: BASIC_SCAN = Scan; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
343 |
open BasicScan; |