author | wenzelm |
Thu, 09 Aug 2012 14:37:43 +0200 | |
changeset 48743 | a72f8ffecf31 |
parent 43947 | 9b00f09f7721 |
child 48764 | 4fe0920d5049 |
permissions | -rw-r--r-- |
27763 | 1 |
(* Title: Pure/General/symbol_pos.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Symbols with explicit position information. |
|
5 |
*) |
|
6 |
||
36957 | 7 |
signature SYMBOL_POS = |
27763 | 8 |
sig |
9 |
type T = Symbol.symbol * Position.T |
|
10 |
val symbol: T -> Symbol.symbol |
|
11 |
val $$$ : Symbol.symbol -> T list -> T list * T list |
|
12 |
val ~$$$ : Symbol.symbol -> T list -> T list * T list |
|
27797 | 13 |
val content: T list -> string |
27763 | 14 |
val is_eof: T -> bool |
15 |
val stopper: T Scan.stopper |
|
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43773
diff
changeset
|
16 |
val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a |
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
17 |
val change_prompt: ('a -> 'b) -> 'a -> 'b |
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
18 |
val scan_pos: T list -> Position.T * T list |
42503 | 19 |
val scan_string_q: T list -> (Position.T * (T list * Position.T)) * T list |
20 |
val scan_string_qq: T list -> (Position.T * (T list * Position.T)) * T list |
|
21 |
val scan_string_bq: T list -> (Position.T * (T list * Position.T)) * T list |
|
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
22 |
val recover_string_q: T list -> T list * T list |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
23 |
val recover_string_qq: T list -> T list * T list |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
24 |
val recover_string_bq: T list -> T list * T list |
43773 | 25 |
val quote_string_q: string -> string |
26 |
val quote_string_qq: string -> string |
|
27 |
val quote_string_bq: string -> string |
|
27763 | 28 |
val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) -> |
29 |
T list -> T list * T list |
|
30 |
val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) -> |
|
31 |
T list -> T list * T list |
|
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
32 |
val recover_comment: T list -> T list * T list |
27763 | 33 |
val source: Position.T -> (Symbol.symbol, 'a) Source.source -> |
34 |
(T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source |
|
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
35 |
type text = string |
27797 | 36 |
val implode: T list -> text |
37 |
val range: T list -> Position.range |
|
38 |
val implode_range: Position.T -> Position.T -> T list -> text * Position.range |
|
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
39 |
val explode: text * Position.T -> T list |
27763 | 40 |
end; |
41 |
||
30573 | 42 |
structure Symbol_Pos: SYMBOL_POS = |
27763 | 43 |
struct |
44 |
||
45 |
(* type T *) |
|
46 |
||
47 |
type T = Symbol.symbol * Position.T; |
|
48 |
||
49 |
fun symbol ((s, _): T) = s; |
|
27852 | 50 |
|
27797 | 51 |
val content = implode o map symbol; |
27763 | 52 |
|
53 |
||
54 |
(* stopper *) |
|
55 |
||
56 |
fun mk_eof pos = (Symbol.eof, pos); |
|
57 |
val eof = mk_eof Position.none; |
|
58 |
||
59 |
val is_eof = Symbol.is_eof o symbol; |
|
60 |
||
61 |
val stopper = |
|
62 |
Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof; |
|
63 |
||
64 |
||
65 |
(* basic scanners *) |
|
66 |
||
67 |
fun !!! text scan = |
|
68 |
let |
|
69 |
fun get_pos [] = " (past end-of-text!)" |
|
70 |
| get_pos ((_, pos) :: _) = Position.str_of pos; |
|
71 |
||
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43773
diff
changeset
|
72 |
fun err (syms, msg) = fn () => |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43773
diff
changeset
|
73 |
text () ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^ |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43773
diff
changeset
|
74 |
(case msg of NONE => "" | SOME m => "\n" ^ m ()); |
27763 | 75 |
in Scan.!! err scan end; |
76 |
||
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
77 |
fun change_prompt scan = Scan.prompt "# " scan; |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
78 |
|
27763 | 79 |
fun $$$ s = Scan.one (fn x => symbol x = s) >> single; |
80 |
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single; |
|
81 |
||
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
82 |
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos); |
27763 | 83 |
|
84 |
||
43773 | 85 |
(* scan string literals *) |
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
86 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
87 |
local |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
88 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
89 |
val char_code = |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
90 |
Scan.one (Symbol.is_ascii_digit o symbol) -- |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
91 |
Scan.one (Symbol.is_ascii_digit o symbol) -- |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
92 |
Scan.one (Symbol.is_ascii_digit o symbol) :|-- |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
93 |
(fn (((a, pos), (b, _)), (c, _)) => |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
94 |
let val (n, _) = Library.read_int [a, b, c] |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
95 |
in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end); |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
96 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
97 |
fun scan_str q = |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43773
diff
changeset
|
98 |
$$$ "\\" |-- !!! (fn () => "bad escape character in string") ($$$ q || $$$ "\\" || char_code) || |
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
99 |
Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single; |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
100 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
101 |
fun scan_strs q = |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43773
diff
changeset
|
102 |
(scan_pos --| $$$ q) -- !!! (fn () => "missing quote at end of string") |
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
103 |
(change_prompt ((Scan.repeat (scan_str q) >> flat) -- ($$$ q |-- scan_pos))); |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
104 |
|
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
105 |
fun recover_strs q = |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
106 |
$$$ q @@@ (Scan.repeat (Scan.permissive (scan_str q)) >> flat); |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
107 |
|
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
108 |
in |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
109 |
|
42503 | 110 |
val scan_string_q = scan_strs "'"; |
111 |
val scan_string_qq = scan_strs "\""; |
|
112 |
val scan_string_bq = scan_strs "`"; |
|
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
113 |
|
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
114 |
val recover_string_q = recover_strs "'"; |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
115 |
val recover_string_qq = recover_strs "\""; |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
116 |
val recover_string_bq = recover_strs "`"; |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
117 |
|
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
118 |
end; |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
119 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
120 |
|
43773 | 121 |
(* quote string literals *) |
122 |
||
123 |
local |
|
124 |
||
125 |
fun char_code i = |
|
126 |
(if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i; |
|
127 |
||
128 |
fun quote_str q s = |
|
129 |
if Symbol.is_ascii_control s then "\\" ^ char_code (ord s) |
|
130 |
else if s = q orelse s = "\\" then "\\" ^ s |
|
131 |
else s; |
|
132 |
||
133 |
fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode; |
|
134 |
||
135 |
in |
|
136 |
||
137 |
val quote_string_q = quote_string "'"; |
|
138 |
val quote_string_qq = quote_string "\""; |
|
139 |
val quote_string_bq = quote_string "`"; |
|
140 |
||
141 |
end; |
|
142 |
||
143 |
||
27763 | 144 |
(* ML-style comments *) |
145 |
||
146 |
local |
|
147 |
||
148 |
val scan_cmt = |
|
149 |
Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) || |
|
150 |
Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) || |
|
151 |
Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) || |
|
152 |
Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single; |
|
153 |
||
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
154 |
val scan_cmts = Scan.pass 0 (Scan.repeat scan_cmt >> flat); |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
155 |
|
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
156 |
val scan_body = change_prompt scan_cmts; |
27763 | 157 |
|
158 |
in |
|
159 |
||
160 |
fun scan_comment cut = |
|
161 |
$$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")"); |
|
162 |
||
163 |
fun scan_comment_body cut = |
|
164 |
$$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")"); |
|
165 |
||
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
166 |
val recover_comment = |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
167 |
$$$ "(" @@@ $$$ "*" @@@ scan_cmts; |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
168 |
|
27763 | 169 |
end; |
170 |
||
171 |
||
172 |
(* source *) |
|
173 |
||
174 |
fun source pos = |
|
175 |
Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos => |
|
176 |
Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE; |
|
177 |
||
178 |
||
179 |
(* compact representation -- with Symbol.DEL padding *) |
|
180 |
||
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
181 |
type text = string; |
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
182 |
|
27763 | 183 |
fun pad [] = [] |
184 |
| pad [(s, _)] = [s] |
|
32784 | 185 |
| pad ((s1, pos1) :: (rest as (_, pos2) :: _)) = |
27763 | 186 |
let |
187 |
val end_pos1 = Position.advance s1 pos1; |
|
27797 | 188 |
val d = Int.max (0, Position.distance_of end_pos1 pos2); |
27763 | 189 |
in s1 :: replicate d Symbol.DEL @ pad rest end; |
190 |
||
27797 | 191 |
val implode = implode o pad; |
27763 | 192 |
|
27797 | 193 |
fun range (syms as (_, pos) :: _) = |
27763 | 194 |
let val pos' = List.last syms |-> Position.advance |
27797 | 195 |
in Position.range pos pos' end |
196 |
| range [] = Position.no_range; |
|
27763 | 197 |
|
27797 | 198 |
fun implode_range pos1 pos2 syms = |
199 |
let val syms' = (("", pos1) :: syms @ [("", pos2)]) |
|
200 |
in (implode syms', range syms') end; |
|
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
201 |
|
27763 | 202 |
fun explode (str, pos) = |
41416 | 203 |
let |
204 |
val (res, _) = |
|
205 |
fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p)) |
|
206 |
(Symbol.explode str) ([], Position.reset_range pos); |
|
207 |
in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end; |
|
27763 | 208 |
|
209 |
end; |
|
210 |
||
36957 | 211 |
structure Basic_Symbol_Pos = (*not open by default*) |
212 |
struct |
|
213 |
val $$$ = Symbol_Pos.$$$; |
|
214 |
val ~$$$ = Symbol_Pos.~$$$; |
|
215 |
end; |
|
27763 | 216 |