author | wenzelm |
Thu, 19 Nov 2015 22:35:10 +0100 | |
changeset 61707 | d5ddd022a451 |
parent 61705 | 546e6494049f |
child 62210 | e068ea693678 |
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 |
|
55103 | 11 |
val $$ : Symbol.symbol -> T list -> T * T list |
55107 | 12 |
val ~$$ : Symbol.symbol -> T list -> T * T list |
27763 | 13 |
val $$$ : Symbol.symbol -> T list -> T list * T list |
14 |
val ~$$$ : Symbol.symbol -> T list -> T list * T list |
|
27797 | 15 |
val content: T list -> string |
55033 | 16 |
val range: T list -> Position.range |
61705
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
17 |
val split_lines: T list -> T list list |
61456 | 18 |
val trim_blanks: T list -> T list |
61705
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
19 |
val trim_lines: T list -> T list |
27763 | 20 |
val is_eof: T -> bool |
21 |
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
|
22 |
val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a |
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
23 |
val scan_pos: T list -> Position.T * T list |
48764 | 24 |
val scan_string_q: string -> T list -> (Position.T * (T list * Position.T)) * T list |
25 |
val scan_string_qq: string -> T list -> (Position.T * (T list * Position.T)) * T list |
|
26 |
val scan_string_bq: string -> 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
|
27 |
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
|
28 |
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
|
29 |
val recover_string_bq: T list -> T list * T list |
43773 | 30 |
val quote_string_q: string -> string |
31 |
val quote_string_qq: string -> string |
|
32 |
val quote_string_bq: string -> string |
|
55105 | 33 |
val scan_cartouche: string -> T list -> T list * T list |
55033 | 34 |
val recover_cartouche: T list -> T list * T list |
35 |
val cartouche_content: T list -> T list |
|
55105 | 36 |
val scan_comment: string -> T list -> T list * T list |
37 |
val scan_comment_body: string -> 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
|
38 |
val recover_comment: T list -> T list * T list |
27763 | 39 |
val source: Position.T -> (Symbol.symbol, 'a) Source.source -> |
40 |
(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
|
41 |
type text = string |
27797 | 42 |
val implode: T list -> text |
59112 | 43 |
val implode_range: Position.range -> T list -> text * Position.range |
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
44 |
val explode: text * Position.T -> T list |
50239 | 45 |
val scan_ident: T list -> T list * T list |
46 |
val is_identifier: string -> bool |
|
27763 | 47 |
end; |
48 |
||
30573 | 49 |
structure Symbol_Pos: SYMBOL_POS = |
27763 | 50 |
struct |
51 |
||
52 |
(* type T *) |
|
53 |
||
54 |
type T = Symbol.symbol * Position.T; |
|
55 |
||
56 |
fun symbol ((s, _): T) = s; |
|
27852 | 57 |
|
27797 | 58 |
val content = implode o map symbol; |
27763 | 59 |
|
55033 | 60 |
fun range (syms as (_, pos) :: _) = |
61 |
let val pos' = List.last syms |-> Position.advance |
|
62 |
in Position.range pos pos' end |
|
63 |
| range [] = Position.no_range; |
|
64 |
||
61705
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
65 |
|
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
66 |
(* lines and blanks *) |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
67 |
|
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
68 |
fun split_lines [] = [] |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
69 |
| split_lines (list: T list) = |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
70 |
let |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
71 |
fun split syms = |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
72 |
(case take_prefix (fn (s, _) => s <> "\n") syms of |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
73 |
(line, []) => [line] |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
74 |
| (line, nl :: rest) => line :: split rest); |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
75 |
in split list end; |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
76 |
|
61707 | 77 |
val trim_blanks = trim (Symbol.is_blank o symbol); |
61456 | 78 |
|
61705
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
79 |
val trim_lines = |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
80 |
split_lines #> map trim_blanks #> separate [(Symbol.space, Position.none)] #> flat; |
546e6494049f
trim lines for @{theory_text} similarly to @{text};
wenzelm
parents:
61502
diff
changeset
|
81 |
|
27763 | 82 |
|
83 |
(* stopper *) |
|
84 |
||
85 |
fun mk_eof pos = (Symbol.eof, pos); |
|
86 |
val eof = mk_eof Position.none; |
|
87 |
||
88 |
val is_eof = Symbol.is_eof o symbol; |
|
89 |
||
90 |
val stopper = |
|
91 |
Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof; |
|
92 |
||
93 |
||
94 |
(* basic scanners *) |
|
95 |
||
96 |
fun !!! text scan = |
|
97 |
let |
|
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48770
diff
changeset
|
98 |
fun get_pos [] = " (end-of-input)" |
48992 | 99 |
| get_pos ((_, pos) :: _) = Position.here pos; |
27763 | 100 |
|
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
|
101 |
fun err (syms, msg) = fn () => |
48770 | 102 |
text () ^ get_pos syms ^ |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48992
diff
changeset
|
103 |
Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^ |
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
|
104 |
(case msg of NONE => "" | SOME m => "\n" ^ m ()); |
27763 | 105 |
in Scan.!! err scan end; |
106 |
||
55033 | 107 |
fun $$ s = Scan.one (fn x => symbol x = s); |
55107 | 108 |
fun ~$$ s = Scan.one (fn x => symbol x <> s); |
109 |
||
27763 | 110 |
fun $$$ s = Scan.one (fn x => symbol x = s) >> single; |
111 |
fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single; |
|
112 |
||
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
113 |
val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos); |
27763 | 114 |
|
115 |
||
43773 | 116 |
(* 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
|
117 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
118 |
local |
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 |
val char_code = |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
121 |
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
|
122 |
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
|
123 |
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
|
124 |
(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
|
125 |
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
|
126 |
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
|
127 |
|
48764 | 128 |
fun scan_str q err_prefix = |
129 |
$$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string") |
|
130 |
($$$ q || $$$ "\\" || char_code) || |
|
58854 | 131 |
Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.not_eof s) >> single; |
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
132 |
|
48764 | 133 |
fun scan_strs q err_prefix = |
55103 | 134 |
Scan.ahead ($$ q) |-- |
135 |
!!! (fn () => err_prefix ^ "unclosed string literal") |
|
61476 | 136 |
((scan_pos --| $$$ q) -- (Scan.repeats (scan_str q err_prefix) -- ($$$ q |-- scan_pos))); |
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
137 |
|
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
138 |
fun recover_strs q = |
61476 | 139 |
$$$ q @@@ Scan.repeats (Scan.permissive (scan_str q "")); |
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
140 |
|
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
141 |
in |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
142 |
|
42503 | 143 |
val scan_string_q = scan_strs "'"; |
144 |
val scan_string_qq = scan_strs "\""; |
|
145 |
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
|
146 |
|
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
|
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
151 |
end; |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
152 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
153 |
|
43773 | 154 |
(* quote string literals *) |
155 |
||
156 |
local |
|
157 |
||
158 |
fun char_code i = |
|
159 |
(if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i; |
|
160 |
||
161 |
fun quote_str q s = |
|
162 |
if Symbol.is_ascii_control s then "\\" ^ char_code (ord s) |
|
163 |
else if s = q orelse s = "\\" then "\\" ^ s |
|
164 |
else s; |
|
165 |
||
166 |
fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode; |
|
167 |
||
168 |
in |
|
169 |
||
170 |
val quote_string_q = quote_string "'"; |
|
171 |
val quote_string_qq = quote_string "\""; |
|
172 |
val quote_string_bq = quote_string "`"; |
|
173 |
||
174 |
end; |
|
175 |
||
176 |
||
55033 | 177 |
(* nested text cartouches *) |
178 |
||
55104
8284c0d5bf52
clarified scan_cartouche_depth, according to Scala version;
wenzelm
parents:
55103
diff
changeset
|
179 |
val scan_cartouche_depth = |
61502
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
180 |
Scan.repeat1 (Scan.depend (fn (depth: int option) => |
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
181 |
(case depth of |
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
182 |
SOME d => |
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
183 |
$$ "\\<open>" >> pair (SOME (d + 1)) || |
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
184 |
(if d > 0 then |
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
185 |
Scan.one (fn (s, _) => s <> "\\<close>" andalso Symbol.not_eof s) >> pair depth || |
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
186 |
$$ "\\<close>" >> pair (if d = 1 then NONE else SOME (d - 1)) |
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
187 |
else Scan.fail) |
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
188 |
| NONE => Scan.fail))); |
55033 | 189 |
|
55105 | 190 |
fun scan_cartouche err_prefix = |
55104
8284c0d5bf52
clarified scan_cartouche_depth, according to Scala version;
wenzelm
parents:
55103
diff
changeset
|
191 |
Scan.ahead ($$ "\\<open>") |-- |
55105 | 192 |
!!! (fn () => err_prefix ^ "unclosed text cartouche") |
61502
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
193 |
(Scan.provide is_none (SOME 0) scan_cartouche_depth); |
55033 | 194 |
|
61502
760e21900b01
clarified scan_cartouche_depth (amending 8284c0d5bf52): finish after outermost cartouche;
wenzelm
parents:
61476
diff
changeset
|
195 |
val recover_cartouche = Scan.pass (SOME 0) scan_cartouche_depth; |
55033 | 196 |
|
197 |
fun cartouche_content syms = |
|
198 |
let |
|
199 |
fun err () = |
|
55709
4e5a83a46ded
clarified Token.range_of in accordance to Symbol_Pos.range;
wenzelm
parents:
55107
diff
changeset
|
200 |
error ("Malformed text cartouche: " |
4e5a83a46ded
clarified Token.range_of in accordance to Symbol_Pos.range;
wenzelm
parents:
55107
diff
changeset
|
201 |
^ quote (content syms) ^ Position.here (#1 (range syms))); |
55033 | 202 |
in |
203 |
(case syms of |
|
204 |
("\\<open>", _) :: rest => |
|
205 |
(case rev rest of |
|
206 |
("\\<close>", _) :: rrest => rev rrest |
|
207 |
| _ => err ()) |
|
208 |
| _ => err ()) |
|
209 |
end; |
|
210 |
||
211 |
||
27763 | 212 |
(* ML-style comments *) |
213 |
||
214 |
local |
|
215 |
||
216 |
val scan_cmt = |
|
217 |
Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) || |
|
218 |
Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) || |
|
219 |
Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) || |
|
58854 | 220 |
Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.not_eof s)) >> single; |
27763 | 221 |
|
61476 | 222 |
val scan_cmts = Scan.pass 0 (Scan.repeats scan_cmt); |
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
223 |
|
27763 | 224 |
in |
225 |
||
55105 | 226 |
fun scan_comment err_prefix = |
55106 | 227 |
Scan.ahead ($$ "(" -- $$ "*") |-- |
228 |
!!! (fn () => err_prefix ^ "unclosed comment") |
|
58850 | 229 |
($$$ "(" @@@ $$$ "*" @@@ scan_cmts @@@ $$$ "*" @@@ $$$ ")"); |
27763 | 230 |
|
55105 | 231 |
fun scan_comment_body err_prefix = |
55106 | 232 |
Scan.ahead ($$ "(" -- $$ "*") |-- |
233 |
!!! (fn () => err_prefix ^ "unclosed comment") |
|
58850 | 234 |
($$ "(" |-- $$ "*" |-- scan_cmts --| $$ "*" --| $$ ")"); |
27763 | 235 |
|
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
236 |
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
|
237 |
$$$ "(" @@@ $$$ "*" @@@ scan_cmts; |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
43947
diff
changeset
|
238 |
|
27763 | 239 |
end; |
240 |
||
241 |
||
242 |
(* source *) |
|
243 |
||
244 |
fun source pos = |
|
245 |
Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos => |
|
58864 | 246 |
Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))); |
27763 | 247 |
|
248 |
||
249 |
(* compact representation -- with Symbol.DEL padding *) |
|
250 |
||
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
251 |
type text = string; |
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
252 |
|
27763 | 253 |
fun pad [] = [] |
254 |
| pad [(s, _)] = [s] |
|
32784 | 255 |
| pad ((s1, pos1) :: (rest as (_, pos2) :: _)) = |
27763 | 256 |
let |
257 |
val end_pos1 = Position.advance s1 pos1; |
|
27797 | 258 |
val d = Int.max (0, Position.distance_of end_pos1 pos2); |
27763 | 259 |
in s1 :: replicate d Symbol.DEL @ pad rest end; |
260 |
||
27797 | 261 |
val implode = implode o pad; |
27763 | 262 |
|
59112 | 263 |
fun implode_range (pos1, pos2) syms = |
27797 | 264 |
let val syms' = (("", pos1) :: syms @ [("", pos2)]) |
265 |
in (implode syms', range syms') end; |
|
27778
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
wenzelm
parents:
27763
diff
changeset
|
266 |
|
27763 | 267 |
fun explode (str, pos) = |
41416 | 268 |
let |
269 |
val (res, _) = |
|
270 |
fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p)) |
|
271 |
(Symbol.explode str) ([], Position.reset_range pos); |
|
272 |
in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end; |
|
27763 | 273 |
|
50239 | 274 |
|
275 |
(* identifiers *) |
|
276 |
||
50242
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
277 |
local |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
278 |
|
52616
3ac2878764f9
more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents:
50493
diff
changeset
|
279 |
val letter = Scan.one (symbol #> Symbol.is_letter); |
3ac2878764f9
more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents:
50493
diff
changeset
|
280 |
val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig); |
53016
fa9c38891cf2
disable old identifier syntax by default, legacy_isub_isup := true may be used temporarily as fall-back;
wenzelm
parents:
52920
diff
changeset
|
281 |
|
54732 | 282 |
val sub = Scan.one (symbol #> (fn s => s = "\\<^sub>")); |
50242
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
283 |
|
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
284 |
in |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
285 |
|
61476 | 286 |
val scan_ident = letter ::: Scan.repeats (letdigs1 || sub ::: letdigs1); |
50242
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
287 |
|
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
288 |
end; |
56b9c792a98b
support for sub-structured identifier syntax (inactive);
wenzelm
parents:
50239
diff
changeset
|
289 |
|
50239 | 290 |
fun is_identifier s = |
50295
3d6a4135a54f
eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents:
50253
diff
changeset
|
291 |
Symbol.is_ascii_identifier s orelse |
3d6a4135a54f
eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents:
50253
diff
changeset
|
292 |
(case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of |
3d6a4135a54f
eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents:
50253
diff
changeset
|
293 |
SOME (_, []) => true |
3d6a4135a54f
eliminated redundant is_ident -- more official is_identifier;
wenzelm
parents:
50253
diff
changeset
|
294 |
| _ => false); |
50239 | 295 |
|
27763 | 296 |
end; |
297 |
||
36957 | 298 |
structure Basic_Symbol_Pos = (*not open by default*) |
299 |
struct |
|
55103 | 300 |
val $$ = Symbol_Pos.$$; |
55107 | 301 |
val ~$$ = Symbol_Pos.~$$; |
36957 | 302 |
val $$$ = Symbol_Pos.$$$; |
303 |
val ~$$$ = Symbol_Pos.~$$$; |
|
304 |
end; |
|
27763 | 305 |