author | wenzelm |
Sun, 22 Jul 2012 12:26:41 +0200 | |
changeset 48423 | 0ccf143a2a69 |
parent 43947 | 9b00f09f7721 |
child 48743 | a72f8ffecf31 |
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 |
|
43773 | 22 |
val quote_string_q: string -> string |
23 |
val quote_string_qq: string -> string |
|
24 |
val quote_string_bq: string -> string |
|
27763 | 25 |
val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) -> |
26 |
T list -> T list * T list |
|
27 |
val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) -> |
|
28 |
T list -> T list * T list |
|
29 |
val source: Position.T -> (Symbol.symbol, 'a) Source.source -> |
|
30 |
(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
|
31 |
type text = string |
27797 | 32 |
val implode: T list -> text |
33 |
val range: T list -> Position.range |
|
34 |
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
|
35 |
val explode: text * Position.T -> T list |
27763 | 36 |
end; |
37 |
||
30573 | 38 |
structure Symbol_Pos: SYMBOL_POS = |
27763 | 39 |
struct |
40 |
||
41 |
(* type T *) |
|
42 |
||
43 |
type T = Symbol.symbol * Position.T; |
|
44 |
||
45 |
fun symbol ((s, _): T) = s; |
|
27852 | 46 |
|
27797 | 47 |
val content = implode o map symbol; |
27763 | 48 |
|
49 |
||
50 |
(* stopper *) |
|
51 |
||
52 |
fun mk_eof pos = (Symbol.eof, pos); |
|
53 |
val eof = mk_eof Position.none; |
|
54 |
||
55 |
val is_eof = Symbol.is_eof o symbol; |
|
56 |
||
57 |
val stopper = |
|
58 |
Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof; |
|
59 |
||
60 |
||
61 |
(* basic scanners *) |
|
62 |
||
63 |
fun !!! text scan = |
|
64 |
let |
|
65 |
fun get_pos [] = " (past end-of-text!)" |
|
66 |
| get_pos ((_, pos) :: _) = Position.str_of pos; |
|
67 |
||
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
|
68 |
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
|
69 |
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
|
70 |
(case msg of NONE => "" | SOME m => "\n" ^ m ()); |
27763 | 71 |
in Scan.!! err scan end; |
72 |
||
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
73 |
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
|
74 |
|
27763 | 75 |
fun $$$ s = Scan.one (fn x => symbol x = s) >> single; |
76 |
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single; |
|
77 |
||
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
78 |
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos); |
27763 | 79 |
|
80 |
||
43773 | 81 |
(* 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
|
82 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
83 |
local |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
84 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
85 |
val char_code = |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
(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
|
90 |
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
|
91 |
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
|
92 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
93 |
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
|
94 |
$$$ "\\" |-- !!! (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
|
95 |
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
|
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_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
|
98 |
(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
|
99 |
(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
|
100 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
101 |
in |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
102 |
|
42503 | 103 |
val scan_string_q = scan_strs "'"; |
104 |
val scan_string_qq = scan_strs "\""; |
|
105 |
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
|
106 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
107 |
end; |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
108 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
109 |
|
43773 | 110 |
(* quote string literals *) |
111 |
||
112 |
local |
|
113 |
||
114 |
fun char_code i = |
|
115 |
(if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i; |
|
116 |
||
117 |
fun quote_str q s = |
|
118 |
if Symbol.is_ascii_control s then "\\" ^ char_code (ord s) |
|
119 |
else if s = q orelse s = "\\" then "\\" ^ s |
|
120 |
else s; |
|
121 |
||
122 |
fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode; |
|
123 |
||
124 |
in |
|
125 |
||
126 |
val quote_string_q = quote_string "'"; |
|
127 |
val quote_string_qq = quote_string "\""; |
|
128 |
val quote_string_bq = quote_string "`"; |
|
129 |
||
130 |
end; |
|
131 |
||
132 |
||
27763 | 133 |
(* ML-style comments *) |
134 |
||
135 |
local |
|
136 |
||
137 |
val scan_cmt = |
|
138 |
Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) || |
|
139 |
Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) || |
|
140 |
Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) || |
|
141 |
Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single; |
|
142 |
||
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
143 |
val scan_body = change_prompt (Scan.pass 0 (Scan.repeat scan_cmt >> flat)); |
27763 | 144 |
|
145 |
in |
|
146 |
||
147 |
fun scan_comment cut = |
|
148 |
$$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")"); |
|
149 |
||
150 |
fun scan_comment_body cut = |
|
151 |
$$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")"); |
|
152 |
||
153 |
end; |
|
154 |
||
155 |
||
156 |
(* source *) |
|
157 |
||
158 |
fun source pos = |
|
159 |
Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos => |
|
160 |
Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE; |
|
161 |
||
162 |
||
163 |
(* compact representation -- with Symbol.DEL padding *) |
|
164 |
||
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
165 |
type text = string; |
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
166 |
|
27763 | 167 |
fun pad [] = [] |
168 |
| pad [(s, _)] = [s] |
|
32784 | 169 |
| pad ((s1, pos1) :: (rest as (_, pos2) :: _)) = |
27763 | 170 |
let |
171 |
val end_pos1 = Position.advance s1 pos1; |
|
27797 | 172 |
val d = Int.max (0, Position.distance_of end_pos1 pos2); |
27763 | 173 |
in s1 :: replicate d Symbol.DEL @ pad rest end; |
174 |
||
27797 | 175 |
val implode = implode o pad; |
27763 | 176 |
|
27797 | 177 |
fun range (syms as (_, pos) :: _) = |
27763 | 178 |
let val pos' = List.last syms |-> Position.advance |
27797 | 179 |
in Position.range pos pos' end |
180 |
| range [] = Position.no_range; |
|
27763 | 181 |
|
27797 | 182 |
fun implode_range pos1 pos2 syms = |
183 |
let val syms' = (("", pos1) :: syms @ [("", pos2)]) |
|
184 |
in (implode syms', range syms') end; |
|
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
185 |
|
27763 | 186 |
fun explode (str, pos) = |
41416 | 187 |
let |
188 |
val (res, _) = |
|
189 |
fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p)) |
|
190 |
(Symbol.explode str) ([], Position.reset_range pos); |
|
191 |
in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end; |
|
27763 | 192 |
|
193 |
end; |
|
194 |
||
36957 | 195 |
structure Basic_Symbol_Pos = (*not open by default*) |
196 |
struct |
|
197 |
val $$$ = Symbol_Pos.$$$; |
|
198 |
val ~$$$ = Symbol_Pos.~$$$; |
|
199 |
end; |
|
27763 | 200 |