| author | haftmann |
| Sat, 02 Jul 2011 10:37:35 +0200 | |
| changeset 43639 | 9cba66fb109a |
| parent 42818 | 128cc195ced3 |
| child 43710 | 7270ae921cf2 |
| permissions | -rw-r--r-- |
| 6118 | 1 |
(* Title: Pure/General/position.ML |
| 5010 | 2 |
Author: Markus Wenzel, TU Muenchen |
3 |
||
| 29307 | 4 |
Source positions: counting Isabelle symbols, starting from 1. |
| 5010 | 5 |
*) |
6 |
||
7 |
signature POSITION = |
|
8 |
sig |
|
| 41484 | 9 |
eqtype T |
10 |
val make: {line: int, column: int, offset: int, end_offset: int, props: Properties.T} -> T
|
|
11 |
val dest: T -> {line: int, column: int, offset: int, end_offset: int, props: Properties.T}
|
|
| 23673 | 12 |
val line_of: T -> int option |
| 26003 | 13 |
val column_of: T -> int option |
| 27795 | 14 |
val offset_of: T -> int option |
| 41484 | 15 |
val end_offset_of: T -> int option |
| 23673 | 16 |
val file_of: T -> string option |
| 27796 | 17 |
val advance: Symbol.symbol -> T -> T |
18 |
val distance_of: T -> T -> int |
|
| 5010 | 19 |
val none: T |
| 27777 | 20 |
val start: T |
| 31435 | 21 |
val file_name: string -> Properties.T |
| 27777 | 22 |
val file: string -> T |
| 5010 | 23 |
val line: int -> T |
| 27777 | 24 |
val line_file: int -> string -> T |
| 29307 | 25 |
val id: string -> T |
| 32573 | 26 |
val id_only: string -> T |
| 27426 | 27 |
val get_id: T -> string option |
28 |
val put_id: string -> T -> T |
|
| 28017 | 29 |
val of_properties: Properties.T -> T |
30 |
val properties_of: T -> Properties.T |
|
|
42327
7c7cc7590eb3
Name_Space.entry_markup: keep def position as separate properties;
wenzelm
parents:
42204
diff
changeset
|
31 |
val def_properties_of: T -> Properties.T |
| 28017 | 32 |
val default_properties: T -> Properties.T -> Properties.T |
|
39440
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
33 |
val markup: T -> Markup.T -> Markup.T |
| 41504 | 34 |
val is_reported: T -> bool |
|
39507
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
35 |
val reported_text: T -> Markup.T -> string -> string |
|
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
36 |
val report_text: T -> Markup.T -> string -> unit |
|
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
37 |
val report: T -> Markup.T -> unit |
| 42204 | 38 |
type reports = (T * Markup.T) list |
39 |
val reports: reports Unsynchronized.ref -> T list -> ('a -> Markup.T list) -> 'a -> unit
|
|
| 26003 | 40 |
val str_of: T -> string |
| 27764 | 41 |
type range = T * T |
| 27796 | 42 |
val no_range: range |
| 41484 | 43 |
val set_range: range -> T |
| 27777 | 44 |
val reset_range: T -> T |
| 27764 | 45 |
val range: T -> T -> range |
| 25817 | 46 |
val thread_data: unit -> T |
47 |
val setmp_thread_data: T -> ('a -> 'b) -> 'a -> 'b
|
|
|
33097
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
48 |
val default: T -> T |
| 5010 | 49 |
end; |
50 |
||
51 |
structure Position: POSITION = |
|
52 |
struct |
|
53 |
||
54 |
(* datatype position *) |
|
55 |
||
| 41484 | 56 |
datatype T = Pos of (int * int * int * int) * Properties.T; |
57 |
||
58 |
fun norm_props (props: Properties.T) = |
|
59 |
maps (fn a => the_list (find_first (fn (b, _) => a = b) props)) Markup.position_properties'; |
|
60 |
||
61 |
fun make {line = i, column = j, offset = k, end_offset = l, props} =
|
|
62 |
Pos ((i, j, k, l), norm_props props); |
|
63 |
||
64 |
fun dest (Pos ((i, j, k, l), props)) = |
|
65 |
{line = i, column = j, offset = k, end_offset = l, props = props};
|
|
66 |
||
| 27777 | 67 |
|
| 27795 | 68 |
fun valid (i: int) = i > 0; |
69 |
fun if_valid i i' = if valid i then i' else i; |
|
70 |
||
| 27777 | 71 |
|
| 27796 | 72 |
(* fields *) |
73 |
||
| 41484 | 74 |
fun line_of (Pos ((i, _, _, _), _)) = if valid i then SOME i else NONE; |
75 |
fun column_of (Pos ((_, j, _, _), _)) = if valid j then SOME j else NONE; |
|
76 |
fun offset_of (Pos ((_, _, k, _), _)) = if valid k then SOME k else NONE; |
|
77 |
fun end_offset_of (Pos ((_, _, _, l), _)) = if valid l then SOME l else NONE; |
|
| 27796 | 78 |
|
| 28017 | 79 |
fun file_of (Pos (_, props)) = Properties.get props Markup.fileN; |
| 27796 | 80 |
|
81 |
||
| 27777 | 82 |
(* advance *) |
| 26003 | 83 |
|
| 41484 | 84 |
fun advance_count "\n" (i: int, j: int, k: int, l: int) = |
85 |
(if_valid i (i + 1), if_valid j 1, if_valid k (k + 1), l) |
|
86 |
| advance_count s (i, j, k, l) = |
|
87 |
if Symbol.is_regular s then (i, if_valid j (j + 1), if_valid k (k + 1), l) |
|
88 |
else (i, j, k, l); |
|
| 5010 | 89 |
|
| 41484 | 90 |
fun invalid_count (i, j, k, _: int) = |
| 27795 | 91 |
not (valid i orelse valid j orelse valid k); |
92 |
||
93 |
fun advance sym (pos as (Pos (count, props))) = |
|
94 |
if invalid_count count then pos else Pos (advance_count sym count, props); |
|
| 27777 | 95 |
|
96 |
||
| 27796 | 97 |
(* distance of adjacent positions *) |
| 27777 | 98 |
|
| 41484 | 99 |
fun distance_of (Pos ((_, j, k, _), _)) (Pos ((_, j', k', _), _)) = |
| 27796 | 100 |
if valid j andalso valid j' then j' - j |
101 |
else if valid k andalso valid k' then k' - k |
|
102 |
else 0; |
|
| 26882 | 103 |
|
| 5010 | 104 |
|
| 27777 | 105 |
(* make position *) |
106 |
||
| 41484 | 107 |
val none = Pos ((0, 0, 0, 0), []); |
108 |
val start = Pos ((1, 1, 1, 0), []); |
|
| 27744 | 109 |
|
| 29307 | 110 |
|
| 27796 | 111 |
fun file_name "" = [] |
112 |
| file_name name = [(Markup.fileN, name)]; |
|
113 |
||
| 41484 | 114 |
fun file name = Pos ((1, 1, 1, 0), file_name name); |
| 27744 | 115 |
|
| 41484 | 116 |
fun line_file i name = Pos ((i, 0, 0, 0), file_name name); |
| 27795 | 117 |
fun line i = line_file i ""; |
| 5010 | 118 |
|
| 41484 | 119 |
fun id id = Pos ((0, 0, 1, 0), [(Markup.idN, id)]); |
120 |
fun id_only id = Pos ((0, 0, 0, 0), [(Markup.idN, id)]); |
|
| 22158 | 121 |
|
| 28017 | 122 |
fun get_id (Pos (_, props)) = Properties.get props Markup.idN; |
123 |
fun put_id id (Pos (count, props)) = Pos (count, Properties.put (Markup.idN, id) props); |
|
| 27426 | 124 |
|
| 29307 | 125 |
|
126 |
(* markup properties *) |
|
127 |
||
| 25817 | 128 |
fun of_properties props = |
| 23627 | 129 |
let |
| 27795 | 130 |
fun get name = |
| 28017 | 131 |
(case Properties.get props name of |
| 27795 | 132 |
NONE => 0 |
133 |
| SOME s => the_default 0 (Int.fromString s)); |
|
| 41484 | 134 |
in |
135 |
make {line = get Markup.lineN, column = get Markup.columnN,
|
|
136 |
offset = get Markup.offsetN, end_offset = get Markup.end_offsetN, props = props} |
|
137 |
end; |
|
138 |
||
| 26003 | 139 |
|
| 41484 | 140 |
fun value k i = if valid i then [(k, string_of_int i)] else []; |
141 |
||
142 |
fun properties_of (Pos ((i, j, k, l), props)) = |
|
143 |
value Markup.lineN i @ value Markup.columnN j @ |
|
144 |
value Markup.offsetN k @ value Markup.end_offsetN l @ props; |
|
| 26003 | 145 |
|
|
42327
7c7cc7590eb3
Name_Space.entry_markup: keep def position as separate properties;
wenzelm
parents:
42204
diff
changeset
|
146 |
val def_properties_of = properties_of #> (map (fn (x, y) => ("def_" ^ x, y)));
|
|
7c7cc7590eb3
Name_Space.entry_markup: keep def position as separate properties;
wenzelm
parents:
42204
diff
changeset
|
147 |
|
| 26052 | 148 |
fun default_properties default props = |
149 |
if exists (member (op =) Markup.position_properties o #1) props then props |
|
150 |
else properties_of default @ props; |
|
151 |
||
|
39440
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
152 |
val markup = Markup.properties o properties_of; |
|
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
153 |
|
|
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
154 |
|
|
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
155 |
(* reports *) |
|
38887
1261481ef5e5
Command.State: add reported positions to markup tree, according main message position or Markup.binding/entity/report occurrences in body;
wenzelm
parents:
38236
diff
changeset
|
156 |
|
| 41504 | 157 |
fun is_reported pos = is_some (offset_of pos) andalso is_some (get_id pos); |
|
30669
6de7ef888aa3
added report_text -- status messages with text body;
wenzelm
parents:
29307
diff
changeset
|
158 |
|
| 41504 | 159 |
fun reported_text pos m txt = if is_reported pos then Markup.markup (markup pos m) txt else ""; |
|
39507
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
160 |
fun report_text pos markup txt = Output.report (reported_text pos markup txt); |
|
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
161 |
fun report pos markup = report_text pos markup ""; |
| 27764 | 162 |
|
| 42204 | 163 |
type reports = (T * Markup.T) list; |
164 |
||
165 |
fun reports _ [] _ _ = () |
|
166 |
| reports (r: reports Unsynchronized.ref) ps markup x = |
|
167 |
let val ms = markup x |
|
168 |
in Unsynchronized.change r (fold (fn p => fold (fn m => cons (p, m)) ms) ps) end; |
|
169 |
||
| 25817 | 170 |
|
| 26003 | 171 |
(* str_of *) |
172 |
||
173 |
fun str_of pos = |
|
174 |
let |
|
175 |
val props = properties_of pos; |
|
176 |
val s = |
|
177 |
(case (line_of pos, file_of pos) of |
|
| 27795 | 178 |
(SOME i, NONE) => "(line " ^ string_of_int i ^ ")" |
179 |
| (SOME i, SOME name) => "(line " ^ string_of_int i ^ " of " ^ quote name ^ ")" |
|
|
42818
128cc195ced3
timing of Theory_Data operations, with implicit thread positions when functor is applied;
wenzelm
parents:
42327
diff
changeset
|
180 |
| (NONE, SOME name) => "(" ^ quote name ^ ")"
|
| 26003 | 181 |
| _ => ""); |
182 |
in |
|
183 |
if null props then "" |
|
184 |
else (if s = "" then "" else " ") ^ Markup.markup (Markup.properties props Markup.position) s |
|
185 |
end; |
|
| 25817 | 186 |
|
| 23627 | 187 |
|
| 27736 | 188 |
(* range *) |
189 |
||
190 |
type range = T * T; |
|
191 |
||
| 27796 | 192 |
val no_range = (none, none); |
193 |
||
| 41484 | 194 |
fun set_range (Pos ((i, j, k, _), props), Pos ((_, _, k', _), _)) = Pos ((i, j, k, k'), props); |
195 |
fun reset_range (Pos ((i, j, k, _), props)) = Pos ((i, j, k, 0), props); |
|
| 27777 | 196 |
|
| 41484 | 197 |
fun range pos pos' = (set_range (pos, pos'), pos'); |
| 27741 | 198 |
|
| 27764 | 199 |
|
200 |
(* thread data *) |
|
201 |
||
202 |
local val tag = Universal.tag () : T Universal.tag in |
|
| 27741 | 203 |
|
| 28122 | 204 |
fun thread_data () = the_default none (Thread.getLocal tag); |
| 27764 | 205 |
|
|
37043
f8e24980af05
more robust Position.setmp_thread_data, independently of Output.debugging (essentially reverts f9ec18f7c0f6, which was motivated by clean exception_trace, but without transaction positions the Isabelle_Process protocol breaks down);
wenzelm
parents:
33097
diff
changeset
|
206 |
fun setmp_thread_data pos = Library.setmp_thread_data tag (thread_data ()) pos; |
| 27764 | 207 |
|
| 5010 | 208 |
end; |
| 27764 | 209 |
|
|
33097
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
210 |
fun default pos = |
|
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
211 |
if pos = none then thread_data () |
|
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
212 |
else pos; |
|
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
213 |
|
| 27764 | 214 |
end; |