author | wenzelm |
Fri, 08 Jul 2011 17:04:38 +0200 | |
changeset 43710 | 7270ae921cf2 |
parent 42818 | 128cc195ced3 |
child 44200 | ce0112e26b3b |
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 |
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
10 |
val make: {line: int, offset: int, end_offset: int, props: Properties.T} -> T |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
11 |
val dest: T -> {line: int, offset: int, end_offset: int, props: Properties.T} |
23673 | 12 |
val line_of: T -> int option |
27795 | 13 |
val offset_of: T -> int option |
41484 | 14 |
val end_offset_of: T -> int option |
23673 | 15 |
val file_of: T -> string option |
27796 | 16 |
val advance: Symbol.symbol -> T -> T |
17 |
val distance_of: T -> T -> int |
|
5010 | 18 |
val none: T |
27777 | 19 |
val start: T |
31435 | 20 |
val file_name: string -> Properties.T |
27777 | 21 |
val file: string -> T |
5010 | 22 |
val line: int -> T |
27777 | 23 |
val line_file: int -> string -> T |
29307 | 24 |
val id: string -> T |
32573 | 25 |
val id_only: string -> T |
27426 | 26 |
val get_id: T -> string option |
27 |
val put_id: string -> T -> T |
|
28017 | 28 |
val of_properties: Properties.T -> T |
29 |
val properties_of: T -> Properties.T |
|
42327
7c7cc7590eb3
Name_Space.entry_markup: keep def position as separate properties;
wenzelm
parents:
42204
diff
changeset
|
30 |
val def_properties_of: T -> Properties.T |
28017 | 31 |
val default_properties: T -> Properties.T -> Properties.T |
39440
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
32 |
val markup: T -> Markup.T -> Markup.T |
41504 | 33 |
val is_reported: T -> bool |
39507
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
34 |
val reported_text: T -> Markup.T -> string -> string |
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
35 |
val report_text: T -> Markup.T -> string -> unit |
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
36 |
val report: T -> Markup.T -> unit |
42204 | 37 |
type reports = (T * Markup.T) list |
38 |
val reports: reports Unsynchronized.ref -> T list -> ('a -> Markup.T list) -> 'a -> unit |
|
26003 | 39 |
val str_of: T -> string |
27764 | 40 |
type range = T * T |
27796 | 41 |
val no_range: range |
41484 | 42 |
val set_range: range -> T |
27777 | 43 |
val reset_range: T -> T |
27764 | 44 |
val range: T -> T -> range |
25817 | 45 |
val thread_data: unit -> T |
46 |
val setmp_thread_data: T -> ('a -> 'b) -> 'a -> 'b |
|
33097
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
47 |
val default: T -> T |
5010 | 48 |
end; |
49 |
||
50 |
structure Position: POSITION = |
|
51 |
struct |
|
52 |
||
53 |
(* datatype position *) |
|
54 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
55 |
datatype T = Pos of (int * int * int) * Properties.T; |
41484 | 56 |
|
57 |
fun norm_props (props: Properties.T) = |
|
58 |
maps (fn a => the_list (find_first (fn (b, _) => a = b) props)) Markup.position_properties'; |
|
59 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
60 |
fun make {line = i, offset = j, end_offset = k, props} = Pos ((i, j, k), norm_props props); |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
61 |
fun dest (Pos ((i, j, k), props)) = {line = i, offset = j, end_offset = k, props = props}; |
27777 | 62 |
|
27795 | 63 |
fun valid (i: int) = i > 0; |
64 |
fun if_valid i i' = if valid i then i' else i; |
|
65 |
||
27777 | 66 |
|
27796 | 67 |
(* fields *) |
68 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
69 |
fun line_of (Pos ((i, _, _), _)) = if valid i then SOME i else NONE; |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
70 |
fun offset_of (Pos ((_, j, _), _)) = if valid j then SOME j else NONE; |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
71 |
fun end_offset_of (Pos ((_, _, k), _)) = if valid k then SOME k else NONE; |
27796 | 72 |
|
28017 | 73 |
fun file_of (Pos (_, props)) = Properties.get props Markup.fileN; |
27796 | 74 |
|
75 |
||
27777 | 76 |
(* advance *) |
26003 | 77 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
78 |
fun advance_count "\n" (i: int, j: int, k: int) = |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
79 |
(if_valid i (i + 1), if_valid j (j + 1), k) |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
80 |
| advance_count s (i, j, k) = |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
81 |
if Symbol.is_regular s then (i, if_valid j (j + 1), k) |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
82 |
else (i, j, k); |
5010 | 83 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
84 |
fun invalid_count (i, j, _: int) = |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
85 |
not (valid i orelse valid j); |
27795 | 86 |
|
87 |
fun advance sym (pos as (Pos (count, props))) = |
|
88 |
if invalid_count count then pos else Pos (advance_count sym count, props); |
|
27777 | 89 |
|
90 |
||
27796 | 91 |
(* distance of adjacent positions *) |
27777 | 92 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
93 |
fun distance_of (Pos ((_, j, _), _)) (Pos ((_, j', _), _)) = |
27796 | 94 |
if valid j andalso valid j' then j' - j |
95 |
else 0; |
|
26882 | 96 |
|
5010 | 97 |
|
27777 | 98 |
(* make position *) |
99 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
100 |
val none = Pos ((0, 0, 0), []); |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
101 |
val start = Pos ((1, 1, 0), []); |
27744 | 102 |
|
29307 | 103 |
|
27796 | 104 |
fun file_name "" = [] |
105 |
| file_name name = [(Markup.fileN, name)]; |
|
106 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
107 |
fun file name = Pos ((1, 1, 0), file_name name); |
27744 | 108 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
109 |
fun line_file i name = Pos ((i, 1, 0), file_name name); |
27795 | 110 |
fun line i = line_file i ""; |
5010 | 111 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
112 |
fun id id = Pos ((0, 1, 0), [(Markup.idN, id)]); |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
113 |
fun id_only id = Pos ((0, 0, 0), [(Markup.idN, id)]); |
22158 | 114 |
|
28017 | 115 |
fun get_id (Pos (_, props)) = Properties.get props Markup.idN; |
116 |
fun put_id id (Pos (count, props)) = Pos (count, Properties.put (Markup.idN, id) props); |
|
27426 | 117 |
|
29307 | 118 |
|
119 |
(* markup properties *) |
|
120 |
||
25817 | 121 |
fun of_properties props = |
23627 | 122 |
let |
27795 | 123 |
fun get name = |
28017 | 124 |
(case Properties.get props name of |
27795 | 125 |
NONE => 0 |
126 |
| SOME s => the_default 0 (Int.fromString s)); |
|
41484 | 127 |
in |
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
128 |
make {line = get Markup.lineN, offset = get Markup.offsetN, |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
129 |
end_offset = get Markup.end_offsetN, props = props} |
41484 | 130 |
end; |
131 |
||
26003 | 132 |
|
41484 | 133 |
fun value k i = if valid i then [(k, string_of_int i)] else []; |
134 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
135 |
fun properties_of (Pos ((i, j, k), props)) = |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
136 |
value Markup.lineN i @ value Markup.offsetN j @ value Markup.end_offsetN k @ props; |
26003 | 137 |
|
42327
7c7cc7590eb3
Name_Space.entry_markup: keep def position as separate properties;
wenzelm
parents:
42204
diff
changeset
|
138 |
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
|
139 |
|
26052 | 140 |
fun default_properties default props = |
141 |
if exists (member (op =) Markup.position_properties o #1) props then props |
|
142 |
else properties_of default @ props; |
|
143 |
||
39440
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
144 |
val markup = Markup.properties o properties_of; |
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
145 |
|
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
146 |
|
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
147 |
(* 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
|
148 |
|
41504 | 149 |
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
|
150 |
|
41504 | 151 |
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
|
152 |
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
|
153 |
fun report pos markup = report_text pos markup ""; |
27764 | 154 |
|
42204 | 155 |
type reports = (T * Markup.T) list; |
156 |
||
157 |
fun reports _ [] _ _ = () |
|
158 |
| reports (r: reports Unsynchronized.ref) ps markup x = |
|
159 |
let val ms = markup x |
|
160 |
in Unsynchronized.change r (fold (fn p => fold (fn m => cons (p, m)) ms) ps) end; |
|
161 |
||
25817 | 162 |
|
26003 | 163 |
(* str_of *) |
164 |
||
165 |
fun str_of pos = |
|
166 |
let |
|
167 |
val props = properties_of pos; |
|
168 |
val s = |
|
169 |
(case (line_of pos, file_of pos) of |
|
27795 | 170 |
(SOME i, NONE) => "(line " ^ string_of_int i ^ ")" |
171 |
| (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
|
172 |
| (NONE, SOME name) => "(" ^ quote name ^ ")" |
26003 | 173 |
| _ => ""); |
174 |
in |
|
175 |
if null props then "" |
|
176 |
else (if s = "" then "" else " ") ^ Markup.markup (Markup.properties props Markup.position) s |
|
177 |
end; |
|
25817 | 178 |
|
23627 | 179 |
|
27736 | 180 |
(* range *) |
181 |
||
182 |
type range = T * T; |
|
183 |
||
27796 | 184 |
val no_range = (none, none); |
185 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
186 |
fun set_range (Pos ((i, j, _), props), Pos ((_, j', _), _)) = Pos ((i, j, j'), props); |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
187 |
fun reset_range (Pos ((i, j, _), props)) = Pos ((i, j, 0), props); |
27777 | 188 |
|
41484 | 189 |
fun range pos pos' = (set_range (pos, pos'), pos'); |
27741 | 190 |
|
27764 | 191 |
|
192 |
(* thread data *) |
|
193 |
||
194 |
local val tag = Universal.tag () : T Universal.tag in |
|
27741 | 195 |
|
28122 | 196 |
fun thread_data () = the_default none (Thread.getLocal tag); |
27764 | 197 |
|
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
|
198 |
fun setmp_thread_data pos = Library.setmp_thread_data tag (thread_data ()) pos; |
27764 | 199 |
|
5010 | 200 |
end; |
27764 | 201 |
|
33097
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
202 |
fun default pos = |
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
203 |
if pos = none then thread_data () |
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
204 |
else pos; |
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
205 |
|
27764 | 206 |
end; |