| author | wenzelm | 
| Fri, 26 Sep 2014 15:10:02 +0200 | |
| changeset 58465 | bd06c6479748 | 
| parent 57899 | 5867d1306712 | 
| child 58854 | b979c781c2db | 
| 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 | 
| 56437 | 23 | val line_file_only: int -> string -> T | 
| 24 | val line_file: int -> string -> T | |
| 5010 | 25 | val line: int -> T | 
| 29307 | 26 | val id: string -> T | 
| 32573 | 27 | val id_only: string -> T | 
| 27426 | 28 | val get_id: T -> string option | 
| 29 | val put_id: string -> T -> T | |
| 50911 
ee7fe4230642
more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
 wenzelm parents: 
50254diff
changeset | 30 | val parse_id: T -> int option | 
| 28017 | 31 | val of_properties: Properties.T -> T | 
| 32 | val properties_of: T -> Properties.T | |
| 42327 
7c7cc7590eb3
Name_Space.entry_markup: keep def position as separate properties;
 wenzelm parents: 
42204diff
changeset | 33 | val def_properties_of: T -> Properties.T | 
| 45412 | 34 | val entity_properties_of: bool -> serial -> T -> Properties.T | 
| 28017 | 35 | val default_properties: T -> Properties.T -> Properties.T | 
| 39440 
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
 wenzelm parents: 
38887diff
changeset | 36 | val markup: T -> Markup.T -> Markup.T | 
| 41504 | 37 | val is_reported: T -> bool | 
| 56459 
38d0b2099743
no report for position singularity, notably for aux. file, especially empty one;
 wenzelm parents: 
56437diff
changeset | 38 | val is_reported_range: T -> bool | 
| 39507 
839873937ddd
tuned signature of (Context_)Position.report variants;
 wenzelm parents: 
39440diff
changeset | 39 | val reported_text: T -> Markup.T -> string -> string | 
| 
839873937ddd
tuned signature of (Context_)Position.report variants;
 wenzelm parents: 
39440diff
changeset | 40 | val report_text: T -> Markup.T -> string -> unit | 
| 
839873937ddd
tuned signature of (Context_)Position.report variants;
 wenzelm parents: 
39440diff
changeset | 41 | val report: T -> Markup.T -> unit | 
| 44736 | 42 | type report = T * Markup.T | 
| 48767 | 43 | type report_text = report * string | 
| 44 | val reports_text: report_text list -> unit | |
| 44736 | 45 | val reports: report list -> unit | 
| 55922 
710bc66f432c
more markup for inner syntax class/type names (notably for completion);
 wenzelm parents: 
55624diff
changeset | 46 | val store_reports: report_text list Unsynchronized.ref -> | 
| 
710bc66f432c
more markup for inner syntax class/type names (notably for completion);
 wenzelm parents: 
55624diff
changeset | 47 |     T list -> ('a -> Markup.T list) -> 'a -> unit
 | 
| 55959 
c3b458435f4f
more decisive commitment to get_free vs. the_const;
 wenzelm parents: 
55922diff
changeset | 48 | val append_reports: report_text list Unsynchronized.ref -> report list -> unit | 
| 48992 | 49 | val here: T -> string | 
| 49691 | 50 | val here_list: T list -> string | 
| 27764 | 51 | type range = T * T | 
| 27796 | 52 | val no_range: range | 
| 41484 | 53 | val set_range: range -> T | 
| 27777 | 54 | val reset_range: T -> T | 
| 27764 | 55 | val range: T -> T -> range | 
| 25817 | 56 | val thread_data: unit -> T | 
| 57 |   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: 
48992diff
changeset | 58 | val default: T -> bool * T | 
| 5010 | 59 | end; | 
| 60 | ||
| 61 | structure Position: POSITION = | |
| 62 | struct | |
| 63 | ||
| 64 | (* datatype position *) | |
| 65 | ||
| 43710 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 66 | datatype T = Pos of (int * int * int) * Properties.T; | 
| 41484 | 67 | |
| 68 | fun norm_props (props: Properties.T) = | |
| 45666 | 69 | 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: 
49691diff
changeset | 70 | Markup.position_properties'; | 
| 41484 | 71 | |
| 43710 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 72 | 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 | 73 | fun dest (Pos ((i, j, k), props)) = {line = i, offset = j, end_offset = k, props = props};
 | 
| 27777 | 74 | |
| 27795 | 75 | fun valid (i: int) = i > 0; | 
| 76 | fun if_valid i i' = if valid i then i' else i; | |
| 77 | ||
| 27777 | 78 | |
| 27796 | 79 | (* fields *) | 
| 80 | ||
| 43710 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 81 | 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 | 82 | 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 | 83 | fun end_offset_of (Pos ((_, _, k), _)) = if valid k then SOME k else NONE; | 
| 27796 | 84 | |
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
49691diff
changeset | 85 | fun file_of (Pos (_, props)) = Properties.get props Markup.fileN; | 
| 27796 | 86 | |
| 87 | ||
| 27777 | 88 | (* advance *) | 
| 26003 | 89 | |
| 43710 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 90 | 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 | 91 | (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 | 92 | | advance_count s (i, j, k) = | 
| 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 93 | 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 | 94 | else (i, j, k); | 
| 5010 | 95 | |
| 43710 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 96 | fun invalid_count (i, j, _: int) = | 
| 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 97 | not (valid i orelse valid j); | 
| 27795 | 98 | |
| 99 | fun advance sym (pos as (Pos (count, props))) = | |
| 100 | if invalid_count count then pos else Pos (advance_count sym count, props); | |
| 27777 | 101 | |
| 102 | ||
| 27796 | 103 | (* distance of adjacent positions *) | 
| 27777 | 104 | |
| 43710 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 105 | fun distance_of (Pos ((_, j, _), _)) (Pos ((_, j', _), _)) = | 
| 27796 | 106 | if valid j andalso valid j' then j' - j | 
| 107 | else 0; | |
| 26882 | 108 | |
| 5010 | 109 | |
| 27777 | 110 | (* make position *) | 
| 111 | ||
| 43710 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 112 | val none = Pos ((0, 0, 0), []); | 
| 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 113 | val start = Pos ((1, 1, 0), []); | 
| 27744 | 114 | |
| 29307 | 115 | |
| 27796 | 116 | fun file_name "" = [] | 
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
49691diff
changeset | 117 | | file_name name = [(Markup.fileN, name)]; | 
| 27796 | 118 | |
| 44200 | 119 | 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 | 120 | fun file name = Pos ((1, 1, 0), file_name name); | 
| 27744 | 121 | |
| 56437 | 122 | fun line_file_only i name = Pos ((i, 0, 0), file_name name); | 
| 43710 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 123 | fun line_file i name = Pos ((i, 1, 0), file_name name); | 
| 27795 | 124 | fun line i = line_file i ""; | 
| 5010 | 125 | |
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
49691diff
changeset | 126 | 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: 
49691diff
changeset | 127 | fun id_only id = Pos ((0, 0, 0), [(Markup.idN, id)]); | 
| 22158 | 128 | |
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
49691diff
changeset | 129 | 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: 
49691diff
changeset | 130 | fun put_id id (Pos (count, props)) = Pos (count, Properties.put (Markup.idN, id) props); | 
| 27426 | 131 | |
| 50911 
ee7fe4230642
more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
 wenzelm parents: 
50254diff
changeset | 132 | fun parse_id pos = Option.map Markup.parse_int (get_id pos); | 
| 
ee7fe4230642
more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
 wenzelm parents: 
50254diff
changeset | 133 | |
| 29307 | 134 | |
| 135 | (* markup properties *) | |
| 136 | ||
| 25817 | 137 | fun of_properties props = | 
| 23627 | 138 | let | 
| 27795 | 139 | fun get name = | 
| 28017 | 140 | (case Properties.get props name of | 
| 27795 | 141 | NONE => 0 | 
| 50254 
935ac0ad7e83
prefer tight Markup.print_int/parse_int for property values;
 wenzelm parents: 
50201diff
changeset | 142 | | SOME s => Markup.parse_int s); | 
| 41484 | 143 | in | 
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
49691diff
changeset | 144 |     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: 
49691diff
changeset | 145 | end_offset = get Markup.end_offsetN, props = props} | 
| 41484 | 146 | end; | 
| 147 | ||
| 26003 | 148 | |
| 50254 
935ac0ad7e83
prefer tight Markup.print_int/parse_int for property values;
 wenzelm parents: 
50201diff
changeset | 149 | fun value k i = if valid i then [(k, Markup.print_int i)] else []; | 
| 41484 | 150 | |
| 43710 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 151 | 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: 
49691diff
changeset | 152 | value Markup.lineN i @ value Markup.offsetN j @ value Markup.end_offsetN k @ props; | 
| 26003 | 153 | |
| 54038 | 154 | val def_properties_of = properties_of #> map (fn (x, y) => ("def_" ^ x, y));
 | 
| 42327 
7c7cc7590eb3
Name_Space.entry_markup: keep def position as separate properties;
 wenzelm parents: 
42204diff
changeset | 155 | |
| 57899 
5867d1306712
clarified signature: entity serial number is not position id;
 wenzelm parents: 
56532diff
changeset | 156 | fun entity_properties_of def serial pos = | 
| 
5867d1306712
clarified signature: entity serial number is not position id;
 wenzelm parents: 
56532diff
changeset | 157 | if def then (Markup.defN, Markup.print_int serial) :: properties_of pos | 
| 
5867d1306712
clarified signature: entity serial number is not position id;
 wenzelm parents: 
56532diff
changeset | 158 | else (Markup.refN, Markup.print_int serial) :: def_properties_of pos; | 
| 45412 | 159 | |
| 26052 | 160 | fun default_properties default props = | 
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
49691diff
changeset | 161 | if exists (member (op =) Markup.position_properties o #1) props then props | 
| 26052 | 162 | else properties_of default @ props; | 
| 163 | ||
| 39440 
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
 wenzelm parents: 
38887diff
changeset | 164 | val markup = Markup.properties o properties_of; | 
| 
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
 wenzelm parents: 
38887diff
changeset | 165 | |
| 
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
 wenzelm parents: 
38887diff
changeset | 166 | |
| 
4c2547af5909
simplified/clarified (Context_)Position.markup/reported_text;
 wenzelm parents: 
38887diff
changeset | 167 | (* 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 | 168 | |
| 41504 | 169 | fun is_reported pos = is_some (offset_of pos) andalso is_some (get_id pos); | 
| 56459 
38d0b2099743
no report for position singularity, notably for aux. file, especially empty one;
 wenzelm parents: 
56437diff
changeset | 170 | fun is_reported_range pos = is_reported pos andalso is_some (end_offset_of pos); | 
| 30669 
6de7ef888aa3
added report_text -- status messages with text body;
 wenzelm parents: 
29307diff
changeset | 171 | |
| 41504 | 172 | fun reported_text pos m txt = if is_reported pos then Markup.markup (markup pos m) txt else ""; | 
| 56333 
38f1422ef473
support bulk messages consisting of small string segments, which are more healthy to the Poly/ML RTS and might prevent spurious GC crashes such as MTGCProcessMarkPointers::ScanAddressesInObject;
 wenzelm parents: 
55959diff
changeset | 173 | fun report_text pos markup txt = Output.report [reported_text pos markup txt]; | 
| 39507 
839873937ddd
tuned signature of (Context_)Position.report variants;
 wenzelm parents: 
39440diff
changeset | 174 | fun report pos markup = report_text pos markup ""; | 
| 27764 | 175 | |
| 44736 | 176 | type report = T * Markup.T; | 
| 48767 | 177 | type report_text = report * string; | 
| 44736 | 178 | |
| 48767 | 179 | val reports_text = | 
| 180 | map (fn ((pos, m), txt) => if is_reported pos then Markup.markup (markup pos m) txt else "") | |
| 56333 
38f1422ef473
support bulk messages consisting of small string segments, which are more healthy to the Poly/ML RTS and might prevent spurious GC crashes such as MTGCProcessMarkPointers::ScanAddressesInObject;
 wenzelm parents: 
55959diff
changeset | 181 | #> Output.report; | 
| 42204 | 182 | |
| 48767 | 183 | val reports = map (rpair "") #> reports_text; | 
| 184 | ||
| 44735 | 185 | fun store_reports _ [] _ _ = () | 
| 55922 
710bc66f432c
more markup for inner syntax class/type names (notably for completion);
 wenzelm parents: 
55624diff
changeset | 186 | | store_reports (r: report_text list Unsynchronized.ref) ps markup x = | 
| 42204 | 187 | let val ms = markup x | 
| 55922 
710bc66f432c
more markup for inner syntax class/type names (notably for completion);
 wenzelm parents: 
55624diff
changeset | 188 | in Unsynchronized.change r (fold (fn p => fold (fn m => cons ((p, m), "")) ms) ps) end; | 
| 42204 | 189 | |
| 55959 
c3b458435f4f
more decisive commitment to get_free vs. the_const;
 wenzelm parents: 
55922diff
changeset | 190 | fun append_reports (r: report_text list Unsynchronized.ref) reports = | 
| 
c3b458435f4f
more decisive commitment to get_free vs. the_const;
 wenzelm parents: 
55922diff
changeset | 191 | Unsynchronized.change r (append (map (rpair "") reports)); | 
| 
c3b458435f4f
more decisive commitment to get_free vs. the_const;
 wenzelm parents: 
55922diff
changeset | 192 | |
| 25817 | 193 | |
| 56532 
3da244bc02bd
tuned message, to accommodate extra brackets produced by Scala parsers;
 wenzelm parents: 
56459diff
changeset | 194 | (* here: user output *) | 
| 26003 | 195 | |
| 48992 | 196 | fun here pos = | 
| 26003 | 197 | let | 
| 198 | val props = properties_of pos; | |
| 55624 | 199 | val (s1, s2) = | 
| 26003 | 200 | (case (line_of pos, file_of pos) of | 
| 55624 | 201 |         (SOME i, NONE) => (" ", "(line " ^ Markup.print_int i ^ ")")
 | 
| 202 |       | (SOME i, SOME name) => (" ", "(line " ^ Markup.print_int i ^ " of " ^ quote name ^ ")")
 | |
| 203 |       | (NONE, SOME name) => (" ", "(file " ^ quote name ^ ")")
 | |
| 204 |       | _ => if is_reported pos then ("", "\\<here>") else ("", ""));
 | |
| 26003 | 205 | in | 
| 206 | if null props then "" | |
| 55624 | 207 | else s1 ^ Markup.markup (Markup.properties props Markup.position) s2 | 
| 26003 | 208 | end; | 
| 25817 | 209 | |
| 49691 | 210 | val here_list = space_implode " " o map here; | 
| 211 | ||
| 23627 | 212 | |
| 27736 | 213 | (* range *) | 
| 214 | ||
| 215 | type range = T * T; | |
| 216 | ||
| 27796 | 217 | val no_range = (none, none); | 
| 218 | ||
| 43710 
7270ae921cf2
discontinued odd Position.column -- left-over from attempts at PGIP implementation;
 wenzelm parents: 
42818diff
changeset | 219 | 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 | 220 | fun reset_range (Pos ((i, j, _), props)) = Pos ((i, j, 0), props); | 
| 27777 | 221 | |
| 41484 | 222 | fun range pos pos' = (set_range (pos, pos'), pos'); | 
| 27741 | 223 | |
| 27764 | 224 | |
| 225 | (* thread data *) | |
| 226 | ||
| 227 | local val tag = Universal.tag () : T Universal.tag in | |
| 27741 | 228 | |
| 28122 | 229 | fun thread_data () = the_default none (Thread.getLocal tag); | 
| 27764 | 230 | |
| 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 | 231 | fun setmp_thread_data pos = Library.setmp_thread_data tag (thread_data ()) pos; | 
| 27764 | 232 | |
| 5010 | 233 | end; | 
| 27764 | 234 | |
| 33097 
9d501e11084a
maintain position of formal entities via name space;
 wenzelm parents: 
32573diff
changeset | 235 | fun default pos = | 
| 49528 
789b73fcca72
report proper binding positions only -- avoid swamping document model with unspecific information;
 wenzelm parents: 
48992diff
changeset | 236 | if pos = none then (false, thread_data ()) | 
| 
789b73fcca72
report proper binding positions only -- avoid swamping document model with unspecific information;
 wenzelm parents: 
48992diff
changeset | 237 | else (true, pos); | 
| 33097 
9d501e11084a
maintain position of formal entities via name space;
 wenzelm parents: 
32573diff
changeset | 238 | |
| 27764 | 239 | end; |