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