author | wenzelm |
Wed, 28 Nov 2012 16:09:05 +0100 | |
changeset 50254 | 935ac0ad7e83 |
parent 50201 | c26369c9eda6 |
child 50911 | ee7fe4230642 |
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 |
44200 | 21 |
val file_only: string -> 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 |
45412 | 32 |
val entity_properties_of: bool -> serial -> T -> Properties.T |
28017 | 33 |
val default_properties: T -> Properties.T -> Properties.T |
39440
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
34 |
val markup: T -> Markup.T -> Markup.T |
41504 | 35 |
val is_reported: T -> bool |
39507
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
36 |
val reported_text: T -> Markup.T -> string -> string |
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
37 |
val report_text: T -> Markup.T -> string -> unit |
839873937ddd
tuned signature of (Context_)Position.report variants;
wenzelm
parents:
39440
diff
changeset
|
38 |
val report: T -> Markup.T -> unit |
44736 | 39 |
type report = T * Markup.T |
48767 | 40 |
type report_text = report * string |
41 |
val reports_text: report_text list -> unit |
|
44736 | 42 |
val reports: report list -> unit |
43 |
val store_reports: report list Unsynchronized.ref -> T list -> ('a -> Markup.T list) -> 'a -> unit |
|
48992 | 44 |
val here: T -> string |
49691 | 45 |
val here_list: T list -> string |
27764 | 46 |
type range = T * T |
27796 | 47 |
val no_range: range |
41484 | 48 |
val set_range: range -> T |
27777 | 49 |
val reset_range: T -> T |
27764 | 50 |
val range: T -> T -> range |
25817 | 51 |
val thread_data: unit -> T |
52 |
val setmp_thread_data: T -> ('a -> 'b) -> 'a -> 'b |
|
49528
789b73fcca72
report proper binding positions only -- avoid swamping document model with unspecific information;
wenzelm
parents:
48992
diff
changeset
|
53 |
val default: T -> bool * T |
5010 | 54 |
end; |
55 |
||
56 |
structure Position: POSITION = |
|
57 |
struct |
|
58 |
||
59 |
(* datatype position *) |
|
60 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
61 |
datatype T = Pos of (int * int * int) * Properties.T; |
41484 | 62 |
|
63 |
fun norm_props (props: Properties.T) = |
|
45666 | 64 |
maps (fn a => the_list (find_first (fn (b, _) => a = b) props)) |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
65 |
Markup.position_properties'; |
41484 | 66 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
67 |
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
|
68 |
fun dest (Pos ((i, j, k), props)) = {line = i, offset = j, end_offset = k, props = props}; |
27777 | 69 |
|
27795 | 70 |
fun valid (i: int) = i > 0; |
71 |
fun if_valid i i' = if valid i then i' else i; |
|
72 |
||
27777 | 73 |
|
27796 | 74 |
(* fields *) |
75 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
76 |
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
|
77 |
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
|
78 |
fun end_offset_of (Pos ((_, _, k), _)) = if valid k then SOME k else NONE; |
27796 | 79 |
|
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
80 |
fun file_of (Pos (_, props)) = Properties.get props Markup.fileN; |
27796 | 81 |
|
82 |
||
27777 | 83 |
(* advance *) |
26003 | 84 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
85 |
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
|
86 |
(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
|
87 |
| advance_count s (i, j, k) = |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
88 |
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
|
89 |
else (i, j, k); |
5010 | 90 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
91 |
fun invalid_count (i, j, _: int) = |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
92 |
not (valid i orelse valid j); |
27795 | 93 |
|
94 |
fun advance sym (pos as (Pos (count, props))) = |
|
95 |
if invalid_count count then pos else Pos (advance_count sym count, props); |
|
27777 | 96 |
|
97 |
||
27796 | 98 |
(* distance of adjacent positions *) |
27777 | 99 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
100 |
fun distance_of (Pos ((_, j, _), _)) (Pos ((_, j', _), _)) = |
27796 | 101 |
if valid j andalso valid j' then j' - j |
102 |
else 0; |
|
26882 | 103 |
|
5010 | 104 |
|
27777 | 105 |
(* make position *) |
106 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
107 |
val none = Pos ((0, 0, 0), []); |
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
108 |
val start = Pos ((1, 1, 0), []); |
27744 | 109 |
|
29307 | 110 |
|
27796 | 111 |
fun file_name "" = [] |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
112 |
| file_name name = [(Markup.fileN, name)]; |
27796 | 113 |
|
44200 | 114 |
fun file_only name = Pos ((0, 0, 0), file_name name); |
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
115 |
fun file name = Pos ((1, 1, 0), file_name name); |
27744 | 116 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
117 |
fun line_file i name = Pos ((i, 1, 0), file_name name); |
27795 | 118 |
fun line i = line_file i ""; |
5010 | 119 |
|
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
120 |
fun id id = Pos ((0, 1, 0), [(Markup.idN, id)]); |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
121 |
fun id_only id = Pos ((0, 0, 0), [(Markup.idN, id)]); |
22158 | 122 |
|
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
123 |
fun get_id (Pos (_, props)) = Properties.get props Markup.idN; |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
124 |
fun put_id id (Pos (count, props)) = Pos (count, Properties.put (Markup.idN, id) props); |
27426 | 125 |
|
29307 | 126 |
|
127 |
(* markup properties *) |
|
128 |
||
25817 | 129 |
fun of_properties props = |
23627 | 130 |
let |
27795 | 131 |
fun get name = |
28017 | 132 |
(case Properties.get props name of |
27795 | 133 |
NONE => 0 |
50254
935ac0ad7e83
prefer tight Markup.print_int/parse_int for property values;
wenzelm
parents:
50201
diff
changeset
|
134 |
| SOME s => Markup.parse_int s); |
41484 | 135 |
in |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
136 |
make {line = get Markup.lineN, offset = get Markup.offsetN, |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
137 |
end_offset = get Markup.end_offsetN, props = props} |
41484 | 138 |
end; |
139 |
||
26003 | 140 |
|
50254
935ac0ad7e83
prefer tight Markup.print_int/parse_int for property values;
wenzelm
parents:
50201
diff
changeset
|
141 |
fun value k i = if valid i then [(k, Markup.print_int i)] else []; |
41484 | 142 |
|
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
143 |
fun properties_of (Pos ((i, j, k), props)) = |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
144 |
value Markup.lineN i @ value Markup.offsetN j @ value Markup.end_offsetN k @ 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 |
|
45412 | 148 |
fun entity_properties_of def id pos = |
50254
935ac0ad7e83
prefer tight Markup.print_int/parse_int for property values;
wenzelm
parents:
50201
diff
changeset
|
149 |
if def then (Markup.defN, Markup.print_int id) :: properties_of pos |
935ac0ad7e83
prefer tight Markup.print_int/parse_int for property values;
wenzelm
parents:
50201
diff
changeset
|
150 |
else (Markup.refN, Markup.print_int id) :: def_properties_of pos; |
45412 | 151 |
|
26052 | 152 |
fun default_properties default props = |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
153 |
if exists (member (op =) Markup.position_properties o #1) props then props |
26052 | 154 |
else properties_of default @ props; |
155 |
||
39440
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
156 |
val markup = Markup.properties o properties_of; |
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
157 |
|
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
158 |
|
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
159 |
(* 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
|
160 |
|
41504 | 161 |
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
|
162 |
|
41504 | 163 |
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
|
164 |
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
|
165 |
fun report pos markup = report_text pos markup ""; |
27764 | 166 |
|
44736 | 167 |
type report = T * Markup.T; |
48767 | 168 |
type report_text = report * string; |
44736 | 169 |
|
48767 | 170 |
val reports_text = |
171 |
map (fn ((pos, m), txt) => if is_reported pos then Markup.markup (markup pos m) txt else "") |
|
44736 | 172 |
#> implode #> Output.report; |
42204 | 173 |
|
48767 | 174 |
val reports = map (rpair "") #> reports_text; |
175 |
||
44735 | 176 |
fun store_reports _ [] _ _ = () |
44736 | 177 |
| store_reports (r: report list Unsynchronized.ref) ps markup x = |
42204 | 178 |
let val ms = markup x |
179 |
in Unsynchronized.change r (fold (fn p => fold (fn m => cons (p, m)) ms) ps) end; |
|
180 |
||
25817 | 181 |
|
48992 | 182 |
(* here: inlined formal markup *) |
26003 | 183 |
|
48992 | 184 |
fun here pos = |
26003 | 185 |
let |
186 |
val props = properties_of pos; |
|
187 |
val s = |
|
188 |
(case (line_of pos, file_of pos) of |
|
50254
935ac0ad7e83
prefer tight Markup.print_int/parse_int for property values;
wenzelm
parents:
50201
diff
changeset
|
189 |
(SOME i, NONE) => "(line " ^ Markup.print_int i ^ ")" |
935ac0ad7e83
prefer tight Markup.print_int/parse_int for property values;
wenzelm
parents:
50201
diff
changeset
|
190 |
| (SOME i, SOME name) => "(line " ^ Markup.print_int i ^ " of " ^ quote name ^ ")" |
44224 | 191 |
| (NONE, SOME name) => "(file " ^ quote name ^ ")" |
26003 | 192 |
| _ => ""); |
193 |
in |
|
194 |
if null props then "" |
|
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49691
diff
changeset
|
195 |
else (if s = "" then "" else " ") ^ Markup.markup (Markup.properties props Markup.position) s |
26003 | 196 |
end; |
25817 | 197 |
|
49691 | 198 |
val here_list = space_implode " " o map here; |
199 |
||
23627 | 200 |
|
27736 | 201 |
(* range *) |
202 |
||
203 |
type range = T * T; |
|
204 |
||
27796 | 205 |
val no_range = (none, none); |
206 |
||
43710
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
wenzelm
parents:
42818
diff
changeset
|
207 |
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
|
208 |
fun reset_range (Pos ((i, j, _), props)) = Pos ((i, j, 0), props); |
27777 | 209 |
|
41484 | 210 |
fun range pos pos' = (set_range (pos, pos'), pos'); |
27741 | 211 |
|
27764 | 212 |
|
213 |
(* thread data *) |
|
214 |
||
215 |
local val tag = Universal.tag () : T Universal.tag in |
|
27741 | 216 |
|
28122 | 217 |
fun thread_data () = the_default none (Thread.getLocal tag); |
27764 | 218 |
|
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
|
219 |
fun setmp_thread_data pos = Library.setmp_thread_data tag (thread_data ()) pos; |
27764 | 220 |
|
5010 | 221 |
end; |
27764 | 222 |
|
33097
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
223 |
fun default pos = |
49528
789b73fcca72
report proper binding positions only -- avoid swamping document model with unspecific information;
wenzelm
parents:
48992
diff
changeset
|
224 |
if pos = none then (false, thread_data ()) |
789b73fcca72
report proper binding positions only -- avoid swamping document model with unspecific information;
wenzelm
parents:
48992
diff
changeset
|
225 |
else (true, pos); |
33097
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
226 |
|
27764 | 227 |
end; |