author | wenzelm |
Fri, 17 Sep 2010 17:09:31 +0200 | |
changeset 39440 | 4c2547af5909 |
parent 38887 | 1261481ef5e5 |
child 39507 | 839873937ddd |
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 |
|
9 |
type T |
|
31424 | 10 |
val value: string -> int -> Properties.T |
23673 | 11 |
val line_of: T -> int option |
26003 | 12 |
val column_of: T -> int option |
27795 | 13 |
val offset_of: T -> int option |
23673 | 14 |
val file_of: T -> string option |
27796 | 15 |
val advance: Symbol.symbol -> T -> T |
16 |
val distance_of: T -> T -> int |
|
5010 | 17 |
val none: T |
27777 | 18 |
val start: T |
31435 | 19 |
val file_name: string -> Properties.T |
27777 | 20 |
val file: string -> T |
5010 | 21 |
val line: int -> T |
27777 | 22 |
val line_file: int -> string -> T |
29307 | 23 |
val id: string -> T |
32573 | 24 |
val id_only: string -> T |
27426 | 25 |
val get_id: T -> string option |
26 |
val put_id: string -> T -> T |
|
28017 | 27 |
val of_properties: Properties.T -> T |
28 |
val properties_of: T -> Properties.T |
|
29 |
val default_properties: T -> Properties.T -> Properties.T |
|
39440
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
30 |
val markup: T -> Markup.T -> Markup.T |
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
31 |
val reported_text: Markup.T -> T -> string -> string |
30669
6de7ef888aa3
added report_text -- status messages with text body;
wenzelm
parents:
29307
diff
changeset
|
32 |
val report_text: Markup.T -> T -> string -> unit |
27764 | 33 |
val report: Markup.T -> T -> unit |
26003 | 34 |
val str_of: T -> string |
27764 | 35 |
type range = T * T |
27796 | 36 |
val no_range: range |
27764 | 37 |
val encode_range: range -> T |
27777 | 38 |
val reset_range: T -> T |
27764 | 39 |
val range: T -> T -> range |
25817 | 40 |
val thread_data: unit -> T |
41 |
val setmp_thread_data: T -> ('a -> 'b) -> 'a -> 'b |
|
33097
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
42 |
val default: T -> T |
5010 | 43 |
end; |
44 |
||
45 |
structure Position: POSITION = |
|
46 |
struct |
|
47 |
||
48 |
(* datatype position *) |
|
49 |
||
28017 | 50 |
datatype T = Pos of (int * int * int) * Properties.T; |
27777 | 51 |
|
27795 | 52 |
fun valid (i: int) = i > 0; |
53 |
fun if_valid i i' = if valid i then i' else i; |
|
54 |
||
55 |
fun value k i = if valid i then [(k, string_of_int i)] else []; |
|
27777 | 56 |
|
57 |
||
27796 | 58 |
(* fields *) |
59 |
||
60 |
fun line_of (Pos ((i, _, _), _)) = if valid i then SOME i else NONE; |
|
61 |
fun column_of (Pos ((_, j, _), _)) = if valid j then SOME j else NONE; |
|
62 |
fun offset_of (Pos ((_, _, k), _)) = if valid k then SOME k else NONE; |
|
63 |
||
28017 | 64 |
fun file_of (Pos (_, props)) = Properties.get props Markup.fileN; |
27796 | 65 |
|
66 |
||
27777 | 67 |
(* advance *) |
26003 | 68 |
|
27795 | 69 |
fun advance_count "\n" (i: int, j: int, k: int) = |
70 |
(if_valid i (i + 1), if_valid j 1, if_valid k (k + 1)) |
|
71 |
| advance_count s (i, j, k) = |
|
37533
d775bd70f571
explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents:
37043
diff
changeset
|
72 |
if Symbol.is_regular s then (i, if_valid j (j + 1), if_valid k (k + 1)) |
d775bd70f571
explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents:
37043
diff
changeset
|
73 |
else (i, j, k); |
5010 | 74 |
|
27795 | 75 |
fun invalid_count (i, j, k) = |
76 |
not (valid i orelse valid j orelse valid k); |
|
77 |
||
78 |
fun advance sym (pos as (Pos (count, props))) = |
|
79 |
if invalid_count count then pos else Pos (advance_count sym count, props); |
|
27777 | 80 |
|
81 |
||
27796 | 82 |
(* distance of adjacent positions *) |
27777 | 83 |
|
27796 | 84 |
fun distance_of (Pos ((_, j, k), _)) (Pos ((_, j', k'), _)) = |
85 |
if valid j andalso valid j' then j' - j |
|
86 |
else if valid k andalso valid k' then k' - k |
|
87 |
else 0; |
|
26882 | 88 |
|
5010 | 89 |
|
27777 | 90 |
(* make position *) |
91 |
||
27795 | 92 |
val none = Pos ((0, 0, 0), []); |
93 |
val start = Pos ((1, 1, 1), []); |
|
27744 | 94 |
|
29307 | 95 |
|
27796 | 96 |
fun file_name "" = [] |
97 |
| file_name name = [(Markup.fileN, name)]; |
|
98 |
||
27795 | 99 |
fun file name = Pos ((1, 1, 1), file_name name); |
27744 | 100 |
|
27795 | 101 |
fun line_file i name = Pos ((i, 0, 0), file_name name); |
102 |
fun line i = line_file i ""; |
|
5010 | 103 |
|
29307 | 104 |
fun id id = Pos ((0, 0, 1), [(Markup.idN, id)]); |
32573 | 105 |
fun id_only id = Pos ((0, 0, 0), [(Markup.idN, id)]); |
22158 | 106 |
|
28017 | 107 |
fun get_id (Pos (_, props)) = Properties.get props Markup.idN; |
108 |
fun put_id id (Pos (count, props)) = Pos (count, Properties.put (Markup.idN, id) props); |
|
27426 | 109 |
|
29307 | 110 |
|
111 |
(* markup properties *) |
|
112 |
||
25817 | 113 |
fun of_properties props = |
23627 | 114 |
let |
27795 | 115 |
fun get name = |
28017 | 116 |
(case Properties.get props name of |
27795 | 117 |
NONE => 0 |
118 |
| SOME s => the_default 0 (Int.fromString s)); |
|
119 |
val count = (get Markup.lineN, get Markup.columnN, get Markup.offsetN); |
|
26003 | 120 |
fun property name = the_list (find_first (fn (x: string, _) => x = name) props); |
27749
24f2b57a34ea
of_properties: observe Markup.position_properties';
wenzelm
parents:
27744
diff
changeset
|
121 |
in Pos (count, maps property Markup.position_properties') end; |
26003 | 122 |
|
27795 | 123 |
fun properties_of (Pos ((i, j, k), props)) = |
124 |
value Markup.lineN i @ value Markup.columnN j @ value Markup.offsetN k @ props; |
|
26003 | 125 |
|
26052 | 126 |
fun default_properties default props = |
127 |
if exists (member (op =) Markup.position_properties o #1) props then props |
|
128 |
else properties_of default @ props; |
|
129 |
||
39440
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
130 |
val markup = Markup.properties o properties_of; |
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
131 |
|
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
132 |
|
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
133 |
(* 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
|
134 |
|
39440
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
135 |
fun reported_text m (pos as Pos (count, _)) txt = |
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
136 |
if invalid_count count then "" |
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
137 |
else Markup.markup (markup pos m) txt; |
30669
6de7ef888aa3
added report_text -- status messages with text body;
wenzelm
parents:
29307
diff
changeset
|
138 |
|
39440
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents:
38887
diff
changeset
|
139 |
fun report_text markup pos txt = Output.report (reported_text markup pos txt); |
30669
6de7ef888aa3
added report_text -- status messages with text body;
wenzelm
parents:
29307
diff
changeset
|
140 |
fun report markup pos = report_text markup pos ""; |
27764 | 141 |
|
25817 | 142 |
|
26003 | 143 |
(* str_of *) |
144 |
||
145 |
fun str_of pos = |
|
146 |
let |
|
147 |
val props = properties_of pos; |
|
148 |
val s = |
|
149 |
(case (line_of pos, file_of pos) of |
|
27795 | 150 |
(SOME i, NONE) => "(line " ^ string_of_int i ^ ")" |
151 |
| (SOME i, SOME name) => "(line " ^ string_of_int i ^ " of " ^ quote name ^ ")" |
|
26003 | 152 |
| _ => ""); |
153 |
in |
|
154 |
if null props then "" |
|
155 |
else (if s = "" then "" else " ") ^ Markup.markup (Markup.properties props Markup.position) s |
|
156 |
end; |
|
25817 | 157 |
|
23627 | 158 |
|
27736 | 159 |
(* range *) |
160 |
||
161 |
type range = T * T; |
|
162 |
||
27796 | 163 |
val no_range = (none, none); |
164 |
||
27795 | 165 |
fun encode_range (Pos (count, props), Pos ((i, j, k), _)) = |
28017 | 166 |
let val props' = props |> fold_rev Properties.put |
27795 | 167 |
(value Markup.end_lineN i @ value Markup.end_columnN j @ value Markup.end_offsetN k) |
168 |
in Pos (count, props') end; |
|
27777 | 169 |
|
170 |
fun reset_range (Pos (count, props)) = |
|
28017 | 171 |
let val props' = props |> fold Properties.remove |
27795 | 172 |
[Markup.end_lineN, Markup.end_columnN, Markup.end_offsetN] |
173 |
in Pos (count, props') end; |
|
27736 | 174 |
|
27764 | 175 |
fun range pos pos' = (encode_range (pos, pos'), pos'); |
27741 | 176 |
|
27764 | 177 |
|
178 |
(* thread data *) |
|
179 |
||
180 |
local val tag = Universal.tag () : T Universal.tag in |
|
27741 | 181 |
|
28122 | 182 |
fun thread_data () = the_default none (Thread.getLocal tag); |
27764 | 183 |
|
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
|
184 |
fun setmp_thread_data pos = Library.setmp_thread_data tag (thread_data ()) pos; |
27764 | 185 |
|
5010 | 186 |
end; |
27764 | 187 |
|
33097
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
188 |
fun default pos = |
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
189 |
if pos = none then thread_data () |
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
190 |
else pos; |
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
32573
diff
changeset
|
191 |
|
27764 | 192 |
end; |