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