author | haftmann |
Tue, 20 Sep 2005 08:23:59 +0200 | |
changeset 17497 | 539319bd6162 |
parent 16803 | 014090d1e64b |
child 18683 | a8f9c192f6d1 |
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*) |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
31 |
val $$ : ''a -> ''a list -> ''a * ''a list |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
32 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
33 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
34 |
signature SCAN = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
35 |
sig |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
36 |
include BASIC_SCAN |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
37 |
val fail: 'a -> 'b |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
38 |
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
|
39 |
val succeed: 'a -> 'b -> 'a * 'b |
15664 | 40 |
val some: ('a -> 'b option) -> 'a list -> 'b * 'a list |
41 |
val one: ('a -> bool) -> 'a list -> 'a * 'a list |
|
14927 | 42 |
val this: ''a list -> ''a list -> ''a list * ''a list |
43 |
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
|
44 |
val any: ('a -> bool) -> 'a list -> 'a list * 'a list |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
45 |
val any1: ('a -> bool) -> 'a list -> 'a list * 'a list |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
46 |
val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
47 |
val option: ('a -> 'b * 'a) -> 'a -> 'b option * 'a |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
48 |
val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
49 |
val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
50 |
val max: ('a * 'a -> bool) -> ('b -> 'a * 'b) -> ('b -> 'a * 'b) -> 'b -> 'a * 'b |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
51 |
val ahead: ('a -> 'b * 'c) -> 'a -> 'b * 'a |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
52 |
val unless: ('a -> 'b * 'a) -> ('a -> 'c * 'd) -> 'a -> 'c * 'd |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
53 |
val first: ('a -> 'b) list -> 'a -> 'b |
14677 | 54 |
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
|
55 |
val depend: ('a -> 'b -> ('c * 'd) * 'e) -> 'a * 'b -> 'd * ('c * 'e) |
15664 | 56 |
val peek: ('a -> 'b -> 'c * 'd) -> 'a * 'b -> 'c * ('a * 'd) |
57 |
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
|
58 |
val lift: ('a -> 'b * 'c) -> 'd * 'a -> 'b * ('d * 'c) |
15664 | 59 |
val trace': ('a * 'b list -> 'c * ('d * 'e list)) -> 'a * 'b list -> |
60 |
('c * 'b list) * ('d * 'e list) |
|
61 |
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
|
62 |
val try: ('a -> 'b) -> 'a -> 'b |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
63 |
val force: ('a -> 'b) -> 'a -> 'b |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
-> '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 |
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
|
68 |
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
|
69 |
val catch: ('a -> 'b) -> 'a -> 'b |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
70 |
val error: ('a -> 'b) -> 'a -> 'b |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
71 |
val source': string -> (string -> 'a -> 'b list * 'a) -> ('b list * 'a -> 'c) -> |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
72 |
'b * ('b -> bool) -> ('d * 'b list -> 'e list * ('d * 'b list)) -> |
10746 | 73 |
('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
|
74 |
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
|
75 |
'b * ('b -> bool) -> ('b list -> 'd list * 'b list) -> |
10746 | 76 |
('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
|
77 |
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
|
78 |
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
|
79 |
type lexicon |
7025 | 80 |
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
|
81 |
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
|
82 |
val empty_lexicon: lexicon |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
83 |
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
|
84 |
val merge_lexicons: lexicon -> lexicon -> lexicon |
14686 | 85 |
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
|
86 |
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
|
87 |
end; |
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 |
structure Scan: SCAN = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
90 |
struct |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
91 |
|
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 |
(** scanners **) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
94 |
|
11523 | 95 |
exception MORE of string option; (*need more input (prompt)*) |
96 |
exception FAIL of string option; (*try alternatives (reason of failure)*) |
|
97 |
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
|
98 |
|
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 |
(* scanner combinators *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
101 |
|
14677 | 102 |
(*dependent pairing*) |
15664 | 103 |
fun (scan1 :-- scan2) toks = |
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
104 |
let |
15664 | 105 |
val (x, toks2) = scan1 toks; |
106 |
val (y, toks3) = scan2 x toks2; |
|
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
107 |
in ((x, y), toks3) end; |
14078 | 108 |
|
14677 | 109 |
(*sequential pairing*) |
15664 | 110 |
fun (scan1 -- scan2) toks = |
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
111 |
let |
15664 | 112 |
val (x, toks2) = scan1 toks; |
113 |
val (y, toks3) = scan2 toks2; |
|
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
114 |
in ((x, y), toks3) end; |
14078 | 115 |
|
14677 | 116 |
(*application*) |
15664 | 117 |
fun (scan >> f) toks = |
118 |
let val (x, toks2) = scan toks; |
|
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
119 |
in (f x, toks2) end; |
14078 | 120 |
|
14677 | 121 |
(*forget snd*) |
15664 | 122 |
fun (scan1 --| scan2) toks = |
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
123 |
let |
15664 | 124 |
val (x, toks2) = scan1 toks; |
125 |
val (_, toks3) = scan2 toks2; |
|
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
126 |
in (x, toks3) end; |
14078 | 127 |
|
14677 | 128 |
(*forget fst*) |
15664 | 129 |
fun (scan1 |-- scan2) toks = |
130 |
let val (_, toks2) = scan1 toks; |
|
131 |
in scan2 toks2 end; |
|
14078 | 132 |
|
14677 | 133 |
(*concatenation*) |
15664 | 134 |
fun (scan1 ^^ scan2) toks = |
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
135 |
let |
15664 | 136 |
val (x, toks2) = scan1 toks; |
137 |
val (y, toks3) = scan2 toks2; |
|
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
138 |
in (x ^ y, toks3) end; |
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
139 |
|
14677 | 140 |
(*alternative*) |
15664 | 141 |
fun (scan1 || scan2) toks = scan1 toks handle FAIL _ => scan2 toks; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
142 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
143 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
144 |
(* generic scanners *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
145 |
|
15531 | 146 |
fun fail _ = raise FAIL NONE; |
147 |
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
|
148 |
fun succeed y xs = (y, xs); |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
149 |
|
15664 | 150 |
fun some _ [] = raise MORE NONE |
151 |
| some f (x :: xs) = |
|
152 |
(case f x of SOME y => (y, xs) | _ => raise FAIL NONE); |
|
153 |
||
15531 | 154 |
fun one _ [] = raise MORE NONE |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
155 |
| one pred (x :: xs) = |
15531 | 156 |
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
|
157 |
|
15531 | 158 |
fun $$ _ [] = raise MORE NONE |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
159 |
| $$ a (x :: xs) = |
15531 | 160 |
if a = 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
|
161 |
|
14833 | 162 |
fun this ys xs = |
14726 | 163 |
let |
164 |
fun drop_prefix [] xs = xs |
|
15531 | 165 |
| drop_prefix (_ :: _) [] = raise MORE NONE |
14726 | 166 |
| drop_prefix (y :: ys) (x :: xs) = |
15531 | 167 |
if y = x then drop_prefix ys xs else raise FAIL NONE; |
14726 | 168 |
in (ys, drop_prefix ys xs) end; |
169 |
||
15664 | 170 |
fun this_string s = this (explode s) >> K s; (*primitive string -- no symbols here!*) |
14907 | 171 |
|
15531 | 172 |
fun any _ [] = raise MORE NONE |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
173 |
| any pred (lst as x :: xs) = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
174 |
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
|
175 |
else ([], lst); |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
176 |
|
15664 | 177 |
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
|
178 |
|
15664 | 179 |
fun optional scan def = scan || succeed def; |
180 |
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
|
181 |
|
13795 | 182 |
fun repeat scan = |
15664 | 183 |
let |
184 |
fun rep ys xs = |
|
185 |
(case (SOME (scan xs) handle FAIL _ => NONE) of |
|
186 |
NONE => (rev ys, xs) |
|
187 |
| SOME (y, xs') => rep (y :: ys) xs'); |
|
14108
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
berghofe
parents:
14078
diff
changeset
|
188 |
in rep [] end; |
13795 | 189 |
|
15664 | 190 |
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
|
191 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
192 |
fun max leq scan1 scan2 xs = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
193 |
(case (option scan1 xs, option scan2 xs) of |
15531 | 194 |
((NONE, _), (NONE, _)) => raise FAIL NONE (*looses FAIL msg!*) |
195 |
| ((SOME tok1, xs'), (NONE, _)) => (tok1, xs') |
|
196 |
| ((NONE, _), (SOME tok2, xs')) => (tok2, xs') |
|
197 |
| ((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
|
198 |
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
|
199 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
200 |
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
|
201 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
202 |
fun unless test scan = |
15531 | 203 |
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
|
204 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
205 |
fun first [] = fail |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
206 |
| first (scan :: scans) = scan || first scans; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
207 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
208 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
209 |
(* state based scanners *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
210 |
|
9122 | 211 |
fun state (st, xs) = (st, (st, xs)); |
212 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
213 |
fun depend scan (st, xs) = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
214 |
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
|
215 |
in (y, (st', xs')) end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
216 |
|
15664 | 217 |
fun peek scan = depend (fn st => scan st >> pair st); |
218 |
||
219 |
fun pass st scan xs = |
|
220 |
let val (y, (_, xs')) = scan (st, xs) |
|
221 |
in (y, xs') end; |
|
222 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
223 |
fun lift scan (st, xs) = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
224 |
let val (y, xs') = scan xs |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
225 |
in (y, (st, xs')) end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
226 |
|
15664 | 227 |
fun unlift scan = pass () scan; |
228 |
||
229 |
||
230 |
(* trace input *) |
|
231 |
||
232 |
fun trace' scan (st, xs) = |
|
233 |
let val (y, (st', xs')) = scan (st, xs) |
|
234 |
in ((y, Library.take (length xs - length xs', xs)), (st', xs')) end; |
|
235 |
||
236 |
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
|
237 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
238 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
239 |
(* exception handling *) |
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 !! err scan xs = scan xs handle FAIL msg => raise ABORT (err (xs, msg)); |
15531 | 242 |
fun try scan xs = scan xs handle MORE _ => raise FAIL NONE | ABORT _ => raise FAIL NONE; |
243 |
fun force scan xs = scan xs handle MORE _ => raise FAIL NONE; |
|
244 |
fun prompt str scan xs = scan xs handle MORE NONE => raise MORE (SOME str); |
|
245 |
fun catch scan xs = scan xs handle ABORT msg => raise FAIL (SOME msg); |
|
14833 | 246 |
fun error scan xs = scan xs handle ABORT msg => Output.error msg; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
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 |
(* finite scans *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
250 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
251 |
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
|
252 |
let |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
253 |
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
|
254 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
255 |
fun stop [] = lost () |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
256 |
| stop lst = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
257 |
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
|
258 |
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
|
259 |
in |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
260 |
if exists is_stopper input then |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
261 |
raise ABORT "Stopper may not occur in input of finite scan!" |
16803 | 262 |
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
|
263 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
264 |
|
15664 | 265 |
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
|
266 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
267 |
fun read stopper scan xs = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
268 |
(case error (finite stopper (option scan)) xs of |
15531 | 269 |
(y as SOME _, []) => y |
270 |
| _ => NONE); |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
271 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
272 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
273 |
(* infinite scans -- draining state-based source *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
274 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
275 |
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
|
276 |
(scan (state, xs), src) handle MORE prmpt => |
16002 | 277 |
(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
|
278 |
([], _) => (finite' stopper scan (state, xs), src) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
279 |
| (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
|
280 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
281 |
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
|
282 |
let |
10746 | 283 |
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
|
284 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
285 |
fun drain_loop recover inp = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
286 |
drain_with (catch scanner) inp handle FAIL msg => |
16002 | 287 |
(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
|
288 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
289 |
val ((ys, (state', xs')), src') = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
290 |
(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
|
291 |
(([], s), _) => (([], (state, [])), s) |
15531 | 292 |
| ((xs, s), NONE) => drain_with (error scanner) ((state, xs), s) |
293 |
| ((xs, s), SOME r) => drain_loop (unless (lift (one (#2 stopper))) r) ((state, xs), s)); |
|
8653 | 294 |
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
|
295 |
|
15664 | 296 |
fun source def_prmpt get unget stopper scan opt_recover = |
297 |
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
|
298 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
299 |
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
|
300 |
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
|
301 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
302 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
303 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
304 |
(** datatype lexicon **) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
305 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
306 |
datatype lexicon = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
307 |
Empty | |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
308 |
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
|
309 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
310 |
val no_literal = []; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
311 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
312 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
313 |
(* dest_lexicon *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
314 |
|
7025 | 315 |
fun dest_lex Empty = [] |
316 |
| dest_lex (Branch (_, [], lt, eq, gt)) = |
|
317 |
dest_lex lt @ dest_lex eq @ dest_lex gt |
|
318 |
| dest_lex (Branch (_, cs, lt, eq, gt)) = |
|
319 |
dest_lex lt @ [cs] @ dest_lex eq @ dest_lex gt; |
|
320 |
||
321 |
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
|
322 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
323 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
324 |
(* empty, extend, make, merge lexicons *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
325 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
326 |
val empty_lexicon = Empty; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
327 |
|
11523 | 328 |
fun extend_lexicon lexicon [] = lexicon |
329 |
| extend_lexicon lexicon chrss = |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
330 |
let |
11523 | 331 |
fun ext (lex, chrs) = |
332 |
let |
|
333 |
fun add (Branch (d, a, lt, eq, gt)) (chs as c :: cs) = |
|
16512 | 334 |
(case string_ord (c, d) of |
14686 | 335 |
LESS => Branch (d, a, add lt chs, eq, gt) |
336 |
| EQUAL => Branch (d, if null cs then chrs else a, lt, add eq cs, gt) |
|
337 |
| GREATER => Branch (d, a, lt, eq, add gt chs)) |
|
11523 | 338 |
| add Empty [c] = |
339 |
Branch (c, chrs, Empty, Empty, Empty) |
|
340 |
| add Empty (c :: cs) = |
|
341 |
Branch (c, no_literal, Empty, add Empty cs, Empty) |
|
342 |
| add lex [] = lex; |
|
343 |
in add lex chrs end; |
|
15570 | 344 |
in Library.foldl ext (lexicon, chrss \\ dest_lex lexicon) end; |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
345 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
346 |
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
|
347 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
348 |
fun merge_lexicons lex1 lex2 = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
349 |
let |
7025 | 350 |
val chss1 = dest_lex lex1; |
351 |
val chss2 = dest_lex lex2; |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
352 |
in |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
353 |
if chss2 subset chss1 then lex1 |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
354 |
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
|
355 |
else extend_lexicon lex1 chss2 |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
356 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
357 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
358 |
|
14686 | 359 |
(* is_literal *) |
360 |
||
361 |
fun is_literal Empty _ = false |
|
362 |
| is_literal _ [] = false |
|
363 |
| is_literal (Branch (d, a, lt, eq, gt)) (chs as c :: cs) = |
|
16512 | 364 |
(case string_ord (c, d) of |
14686 | 365 |
LESS => is_literal lt chs |
366 |
| EQUAL => a <> no_literal andalso null cs orelse is_literal eq cs |
|
367 |
| GREATER => is_literal gt chs); |
|
368 |
||
369 |
||
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
370 |
(* scan literal *) |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
371 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
372 |
fun literal lex chrs = |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
373 |
let |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
374 |
fun lit Empty res _ = res |
15531 | 375 |
| lit (Branch _) _ [] = raise MORE NONE |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
376 |
| lit (Branch (d, a, lt, eq, gt)) res (chs as c :: cs) = |
16512 | 377 |
(case string_ord (c, d) of |
14686 | 378 |
LESS => lit lt res chs |
15531 | 379 |
| EQUAL => lit eq (if a = no_literal then res else SOME (a, cs)) cs |
14686 | 380 |
| GREATER => lit gt res chs); |
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
381 |
in |
15531 | 382 |
(case lit lex NONE chrs of |
383 |
NONE => raise FAIL NONE |
|
384 |
| SOME res => res) |
|
6116
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
385 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
386 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
387 |
end; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
388 |
|
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
389 |
structure BasicScan: BASIC_SCAN = Scan; |
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff
changeset
|
390 |
open BasicScan; |