src/Pure/General/position.ML
author wenzelm
Wed, 12 Mar 2025 11:39:00 +0100
changeset 82265 4b875a4c83b0
parent 81558 b57996a0688c
permissions -rw-r--r--
update for release;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6118
caa439435666 fixed titles;
wenzelm
parents: 5010
diff changeset
     1
(*  Title:      Pure/General/position.ML
74165
86163ea20e77 tuned comments;
wenzelm
parents: 73864
diff changeset
     2
    Author:     Makarius
5010
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
     3
74165
86163ea20e77 tuned comments;
wenzelm
parents: 73864
diff changeset
     4
Source positions starting from 1; values <= 0 mean "absent". Count Isabelle
86163ea20e77 tuned comments;
wenzelm
parents: 73864
diff changeset
     5
symbols, not UTF8 bytes nor UTF16 characters. Position range specifies a
86163ea20e77 tuned comments;
wenzelm
parents: 73864
diff changeset
     6
right-open interval offset .. end_offset (exclusive).
5010
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
     7
*)
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
     8
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
     9
signature POSITION =
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
    10
sig
41484
51310e1ccd6f more direct treatment of Position.end_offset;
wenzelm
parents: 41483
diff changeset
    11
  eqtype T
71465
910a081cca74 more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents: 70498
diff changeset
    12
  val ord: T ord
78811
d3328c562307 more compact ML source;
wenzelm
parents: 78023
diff changeset
    13
  val make0: int -> int -> int -> string -> string -> string -> T
62940
a03592aafadf tuned -- avoid recoding properties;
wenzelm
parents: 62929
diff changeset
    14
  val make: Thread_Position.T -> T
a03592aafadf tuned -- avoid recoding properties;
wenzelm
parents: 62929
diff changeset
    15
  val dest: T -> Thread_Position.T
23673
67c748e5ae54 replaced name by file (unquoted);
wenzelm
parents: 23627
diff changeset
    16
  val line_of: T -> int option
27795
f67f4c677d81 count offset as well;
wenzelm
parents: 27791
diff changeset
    17
  val offset_of: T -> int option
41484
51310e1ccd6f more direct treatment of Position.end_offset;
wenzelm
parents: 41483
diff changeset
    18
  val end_offset_of: T -> int option
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
    19
  val label_of: T -> string option
23673
67c748e5ae54 replaced name by file (unquoted);
wenzelm
parents: 23627
diff changeset
    20
  val file_of: T -> string option
74166
ff3dbb2be924 tuned signature;
wenzelm
parents: 74165
diff changeset
    21
  val id_of: T -> string option
74174
a3b0fc510705 clarified signature;
wenzelm
parents: 74171
diff changeset
    22
  val symbol: Symbol.symbol -> T -> T
a3b0fc510705 clarified signature;
wenzelm
parents: 74171
diff changeset
    23
  val symbol_explode: string -> T -> T
68177
6e40f5d43936 clarified signature;
wenzelm
parents: 68172
diff changeset
    24
  val distance_of: T * T -> int option
5010
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
    25
  val none: T
27777
7a63d1b50b88 only increment column if valid;
wenzelm
parents: 27764
diff changeset
    26
  val start: T
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
    27
  val label: string -> T -> T
44200
ce0112e26b3b tuned error message;
wenzelm
parents: 43710
diff changeset
    28
  val file_only: string -> T
27777
7a63d1b50b88 only increment column if valid;
wenzelm
parents: 27764
diff changeset
    29
  val file: string -> T
56437
b14bd153a753 clarified position: no offset here;
wenzelm
parents: 56333
diff changeset
    30
  val line_file_only: int -> string -> T
b14bd153a753 clarified position: no offset here;
wenzelm
parents: 56333
diff changeset
    31
  val line_file: int -> string -> T
5010
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
    32
  val line: int -> T
29307
3a0b38b7fbb4 added id;
wenzelm
parents: 28122
diff changeset
    33
  val id: string -> T
32573
62b5b538408d Isar.define_command: identify transaction;
wenzelm
parents: 32195
diff changeset
    34
  val id_only: string -> T
27426
c0ef698c0904 added get_id/put_id;
wenzelm
parents: 26890
diff changeset
    35
  val put_id: string -> T -> T
68858
e1796b8ddbae more accurate position for auxiliary files;
wenzelm
parents: 68829
diff changeset
    36
  val copy_id: T -> T -> T
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents: 50254
diff changeset
    37
  val parse_id: T -> int option
77771
279b18bb4059 clarified signature;
wenzelm
parents: 77770
diff changeset
    38
  val shift_offsets: {remove_id: bool} -> int -> T -> T
68183
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    39
  val adjust_offsets: (int -> int option) -> T -> T
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
    40
  val of_props: {line: int, offset: int, end_offset: int, props: Properties.T} -> T
28017
4919bd124a58 type Properties.T;
wenzelm
parents: 27796
diff changeset
    41
  val of_properties: Properties.T -> T
4919bd124a58 type Properties.T;
wenzelm
parents: 27796
diff changeset
    42
  val properties_of: T -> Properties.T
42327
7c7cc7590eb3 Name_Space.entry_markup: keep def position as separate properties;
wenzelm
parents: 42204
diff changeset
    43
  val def_properties_of: T -> Properties.T
71910
f8b0271cc744 tuned signature;
wenzelm
parents: 71465
diff changeset
    44
  val entity_markup: string -> string * T -> Markup.T
74262
839a6e284545 tuned signature;
wenzelm
parents: 74183
diff changeset
    45
  val make_entity_markup: {def: bool} -> serial -> string -> string * T -> Markup.T
80875
2e33897071b6 clarified signature;
wenzelm
parents: 80873
diff changeset
    46
  val markup_properties: T -> Markup.T -> Markup.T
2e33897071b6 clarified signature;
wenzelm
parents: 80873
diff changeset
    47
  val markup: T -> Markup.T
41504
f0f20a5b54df clarified notion Position.is_reported;
wenzelm
parents: 41484
diff changeset
    48
  val is_reported: T -> bool
56459
38d0b2099743 no report for position singularity, notably for aux. file, especially empty one;
wenzelm
parents: 56437
diff changeset
    49
  val is_reported_range: T -> bool
39507
839873937ddd tuned signature of (Context_)Position.report variants;
wenzelm
parents: 39440
diff changeset
    50
  val reported_text: T -> Markup.T -> string -> string
839873937ddd tuned signature of (Context_)Position.report variants;
wenzelm
parents: 39440
diff changeset
    51
  val report_text: T -> Markup.T -> string -> unit
839873937ddd tuned signature of (Context_)Position.report variants;
wenzelm
parents: 39440
diff changeset
    52
  val report: T -> Markup.T -> unit
44736
c2a3f1c84179 bulk reports for improved message throughput;
wenzelm
parents: 44735
diff changeset
    53
  type report = T * Markup.T
48767
7f0c469cc796 reports with body text, not just markup;
wenzelm
parents: 45666
diff changeset
    54
  type report_text = report * string
7f0c469cc796 reports with body text, not just markup;
wenzelm
parents: 45666
diff changeset
    55
  val reports_text: report_text list -> unit
44736
c2a3f1c84179 bulk reports for improved message throughput;
wenzelm
parents: 44735
diff changeset
    56
  val reports: report list -> unit
55959
c3b458435f4f more decisive commitment to get_free vs. the_const;
wenzelm
parents: 55922
diff changeset
    57
  val append_reports: report_text list Unsynchronized.ref -> report list -> unit
71465
910a081cca74 more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents: 70498
diff changeset
    58
  val here_strs: T -> string * string
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48767
diff changeset
    59
  val here: T -> string
49691
74ad6ecf2af2 more error positions;
wenzelm
parents: 49528
diff changeset
    60
  val here_list: T list -> string
27764
e0ee3cc240fe advance: single argument (again);
wenzelm
parents: 27759
diff changeset
    61
  type range = T * T
80978
5e2b1588c5cb minor performance tuning: proper table for parsetree list;
wenzelm
parents: 80875
diff changeset
    62
  val range_ord: range ord
27796
a6da5f68e776 added distance_of (permissive version);
wenzelm
parents: 27795
diff changeset
    63
  val no_range: range
62800
7ac100f86863 tuned signature;
wenzelm
parents: 62798
diff changeset
    64
  val no_range_position: T -> T
62797
e08c44eed27f tuned signature;
wenzelm
parents: 62750
diff changeset
    65
  val range_position: range -> T
e08c44eed27f tuned signature;
wenzelm
parents: 62750
diff changeset
    66
  val range: T * T -> range
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
    67
  val range_of_properties: Properties.T -> range
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
    68
  val properties_of_range: range -> Properties.T
25817
d8e0190917a5 type T: based on properties, added id field;
wenzelm
parents: 25792
diff changeset
    69
  val thread_data: unit -> T
d8e0190917a5 type T: based on properties, added id field;
wenzelm
parents: 25792
diff changeset
    70
  val setmp_thread_data: T -> ('a -> 'b) -> 'a -> 'b
78023
76dece8cd8a7 more informative position information;
wenzelm
parents: 78021
diff changeset
    71
  val setmp_thread_data_label: string -> ('a -> 'b) -> 'a -> 'b
49528
789b73fcca72 report proper binding positions only -- avoid swamping document model with unspecific information;
wenzelm
parents: 48992
diff changeset
    72
  val default: T -> bool * T
5010
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
    73
end;
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
    74
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
    75
structure Position: POSITION =
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
    76
struct
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
    77
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
    78
(* datatype position *)
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
    79
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
    80
datatype T = Pos of Thread_Position.T;
71465
910a081cca74 more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents: 70498
diff changeset
    81
78811
d3328c562307 more compact ML source;
wenzelm
parents: 78023
diff changeset
    82
fun make0 line offset end_offset label file id =
d3328c562307 more compact ML source;
wenzelm
parents: 78023
diff changeset
    83
  Pos {line = line, offset = offset, end_offset = end_offset,
d3328c562307 more compact ML source;
wenzelm
parents: 78023
diff changeset
    84
    props = {label = label, file = file, id = id}};
d3328c562307 more compact ML source;
wenzelm
parents: 78023
diff changeset
    85
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
    86
val make = Pos;
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
    87
fun dest (Pos args) = args;
27777
7a63d1b50b88 only increment column if valid;
wenzelm
parents: 27764
diff changeset
    88
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
    89
val ord =
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
    90
  pointer_eq_ord
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
    91
   (int_ord o apply2 (#line o dest) |||
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
    92
    int_ord o apply2 (#offset o dest) |||
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
    93
    int_ord o apply2 (#end_offset o dest) |||
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
    94
    fast_string_ord o apply2 (#label o #props o dest) |||
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
    95
    fast_string_ord o apply2 (#file o #props o dest) |||
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
    96
    fast_string_ord o apply2 (#id o #props o dest));
27795
f67f4c677d81 count offset as well;
wenzelm
parents: 27791
diff changeset
    97
27777
7a63d1b50b88 only increment column if valid;
wenzelm
parents: 27764
diff changeset
    98
27796
a6da5f68e776 added distance_of (permissive version);
wenzelm
parents: 27795
diff changeset
    99
(* fields *)
a6da5f68e776 added distance_of (permissive version);
wenzelm
parents: 27795
diff changeset
   100
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   101
fun valid (i: int) = i > 0;
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   102
fun maybe_valid i = if valid i then SOME i else NONE;
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   103
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   104
val invalid = not o valid;
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   105
fun invalid_pos (Pos {line, offset, ...}) = invalid line andalso invalid offset;
27796
a6da5f68e776 added distance_of (permissive version);
wenzelm
parents: 27795
diff changeset
   106
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   107
val line_of = maybe_valid o #line o dest;
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   108
val offset_of = maybe_valid o #offset o dest;
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   109
val end_offset_of = maybe_valid o #end_offset o dest;
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   110
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   111
fun label_of (Pos {props = {label, ...}, ...}) = if label = "" then NONE else SOME label;
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   112
fun file_of (Pos {props = {file, ...}, ...}) = if file = "" then NONE else SOME file;
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   113
fun id_of (Pos {props = {id, ...}, ...}) = if id = "" then NONE else SOME id;
27796
a6da5f68e776 added distance_of (permissive version);
wenzelm
parents: 27795
diff changeset
   114
a6da5f68e776 added distance_of (permissive version);
wenzelm
parents: 27795
diff changeset
   115
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   116
(* symbol positions *)
5010
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
   117
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   118
fun symbols [] pos = pos
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   119
  | symbols ss (pos as Pos {line, offset, end_offset, props}) =
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   120
      let
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   121
        val line' = if valid line then fold (fn s => s = "\n" ? Integer.add 1) ss line else line;
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   122
        val offset' =
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   123
          if valid offset then fold (fn s => Symbol.not_eof s ? Integer.add 1) ss offset else offset;
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   124
      in
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   125
        if line = line' andalso offset = offset' then pos
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   126
        else Pos {line = line', offset = offset', end_offset = end_offset, props = props}
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   127
      end;
27795
f67f4c677d81 count offset as well;
wenzelm
parents: 27791
diff changeset
   128
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   129
val symbol = symbols o single;
27777
7a63d1b50b88 only increment column if valid;
wenzelm
parents: 27764
diff changeset
   130
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   131
fun symbol_explode str pos =
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   132
  if str = "" orelse invalid_pos pos then pos
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   133
  else fold symbol (Symbol.explode str) pos;
74171
a9e79c3645c4 clarified signature;
wenzelm
parents: 74166
diff changeset
   134
74176
wenzelm
parents: 74174
diff changeset
   135
27796
a6da5f68e776 added distance_of (permissive version);
wenzelm
parents: 27795
diff changeset
   136
(* distance of adjacent positions *)
27777
7a63d1b50b88 only increment column if valid;
wenzelm
parents: 27764
diff changeset
   137
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   138
fun distance_of (Pos {offset, ...}, Pos {offset = offset', ...}) =
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   139
  if valid offset andalso valid offset' then SOME (offset' - offset) else NONE;
26882
9e824d8f4512 renamed Position.path to Path.position;
wenzelm
parents: 26633
diff changeset
   140
5010
9101b70b696d moved Thy/position.ML to General/position.ML;
wenzelm
parents:
diff changeset
   141
27777
7a63d1b50b88 only increment column if valid;
wenzelm
parents: 27764
diff changeset
   142
(* make position *)
7a63d1b50b88 only increment column if valid;
wenzelm
parents: 27764
diff changeset
   143
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   144
type props = {label: string, file: string, id: string};
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   145
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   146
val no_props: props = {label = "", file = "", id = ""};
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   147
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   148
fun file_props name : props =
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   149
  if name = "" then no_props else {label = "", file = name, id = ""};
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   150
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   151
fun id_props id : props =
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   152
  if id = "" then no_props else {label = "", file = "", id = id};
27744
d4c5ddf98869 advance: operate on symbol list (less overhead);
wenzelm
parents: 27741
diff changeset
   153
29307
3a0b38b7fbb4 added id;
wenzelm
parents: 28122
diff changeset
   154
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   155
val none = Pos {line = 0, offset = 0, end_offset = 0, props = no_props};
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   156
val start = Pos {line = 1, offset = 1, end_offset = 0, props = no_props};
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   157
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   158
fun label label (Pos {line, offset, end_offset, props = {label = _, file, id}}) =
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   159
  Pos {line = line, offset = offset, end_offset = end_offset,
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   160
    props = {label = label, file = file, id = id}};
27796
a6da5f68e776 added distance_of (permissive version);
wenzelm
parents: 27795
diff changeset
   161
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   162
fun file_only name = Pos {line = 0, offset = 0, end_offset = 0, props = file_props name};
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   163
fun file name = Pos {line = 1, offset = 1, end_offset = 0, props = file_props name};
27744
d4c5ddf98869 advance: operate on symbol list (less overhead);
wenzelm
parents: 27741
diff changeset
   164
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   165
fun line_file_only line name = Pos {line = line, offset = 0, end_offset = 0, props = file_props name};
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   166
fun line_file line name = Pos {line = line, offset = 1, end_offset = 0, props = file_props name};
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   167
fun line line = line_file line "";
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   168
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   169
fun id id = Pos {line = 0, offset = 1, end_offset = 0, props = id_props id};
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   170
fun id_only id = Pos {line = 0, offset = 0, end_offset = 0, props = id_props id};
67280
dfc5a1503916 clarified default position for empty message pos;
wenzelm
parents: 64556
diff changeset
   171
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   172
fun put_id id' (pos as Pos {line, offset, end_offset, props = {label, file, id}}) =
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   173
  if id = id' then pos
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   174
  else Pos {line = line, offset = offset, end_offset = end_offset,
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   175
    props = {label = label, file = file, id = id'}};
22158
ff4fc4ee9eb0 Add line_of, name_of destructors.
aspinall
parents: 15531
diff changeset
   176
74166
ff3dbb2be924 tuned signature;
wenzelm
parents: 74165
diff changeset
   177
fun copy_id pos = (case id_of pos of NONE => I | SOME id => put_id id);
27426
c0ef698c0904 added get_id/put_id;
wenzelm
parents: 26890
diff changeset
   178
74166
ff3dbb2be924 tuned signature;
wenzelm
parents: 74165
diff changeset
   179
fun parse_id pos = Option.map Value.parse_int (id_of pos);
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents: 50254
diff changeset
   180
74180
wenzelm
parents: 74179
diff changeset
   181
wenzelm
parents: 74179
diff changeset
   182
(* adjust offsets *)
wenzelm
parents: 74179
diff changeset
   183
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   184
fun shift_offsets {remove_id} shift (pos as Pos {line, offset, end_offset, props}) =
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   185
  if shift < 0 then raise Fail "Illegal offset shift"
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   186
  else if shift > 0 andalso valid line then raise Fail "Illegal line position"
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   187
  else
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   188
    let
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   189
      val offset' = if valid offset then offset + shift else offset;
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   190
      val end_offset' = if valid end_offset then end_offset + shift else end_offset;
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   191
      val props' = if remove_id then file_props (#file props) else props;
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   192
    in
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   193
      if offset = offset' andalso end_offset = end_offset' andalso props = props' then pos
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   194
      else Pos {line = line, offset = offset', end_offset = end_offset', props = props'}
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   195
    end;
74180
wenzelm
parents: 74179
diff changeset
   196
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   197
fun adjust_offsets adjust pos =
68858
e1796b8ddbae more accurate position for auxiliary files;
wenzelm
parents: 68829
diff changeset
   198
  if is_none (file_of pos) then
e1796b8ddbae more accurate position for auxiliary files;
wenzelm
parents: 68829
diff changeset
   199
    (case parse_id pos of
e1796b8ddbae more accurate position for auxiliary files;
wenzelm
parents: 68829
diff changeset
   200
      SOME id =>
e1796b8ddbae more accurate position for auxiliary files;
wenzelm
parents: 68829
diff changeset
   201
        (case adjust id of
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   202
          SOME shift => shift_offsets {remove_id = true} shift pos
68858
e1796b8ddbae more accurate position for auxiliary files;
wenzelm
parents: 68829
diff changeset
   203
        | NONE => pos)
e1796b8ddbae more accurate position for auxiliary files;
wenzelm
parents: 68829
diff changeset
   204
    | NONE => pos)
e1796b8ddbae more accurate position for auxiliary files;
wenzelm
parents: 68829
diff changeset
   205
  else pos;
68183
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
   206
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
   207
29307
3a0b38b7fbb4 added id;
wenzelm
parents: 28122
diff changeset
   208
(* markup properties *)
3a0b38b7fbb4 added id;
wenzelm
parents: 28122
diff changeset
   209
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   210
fun of_props {line, offset, end_offset, props} =
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   211
  Pos {line = line, offset = offset, end_offset = end_offset,
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   212
    props =
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   213
     {label = Properties.get_string props Markup.labelN,
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   214
      file = Properties.get_string props Markup.fileN,
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   215
      id = Properties.get_string props Markup.idN}};
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   216
25817
d8e0190917a5 type T: based on properties, added id field;
wenzelm
parents: 25792
diff changeset
   217
fun of_properties props =
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   218
  of_props {
77772
7e0d920b4e6e tuned signature;
wenzelm
parents: 77771
diff changeset
   219
    line = Properties.get_int props Markup.lineN,
7e0d920b4e6e tuned signature;
wenzelm
parents: 77771
diff changeset
   220
    offset = Properties.get_int props Markup.offsetN,
7e0d920b4e6e tuned signature;
wenzelm
parents: 77771
diff changeset
   221
    end_offset = Properties.get_int props Markup.end_offsetN,
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   222
    props = props};
26003
7a8aee8353cf added column field;
wenzelm
parents: 25954
diff changeset
   223
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   224
fun properties_of (Pos {line, offset, end_offset, props = {label, file, id}}) =
77777
13cee8c2ed8d clarified signature;
wenzelm
parents: 77776
diff changeset
   225
  Properties.make_int Markup.lineN line @
13cee8c2ed8d clarified signature;
wenzelm
parents: 77776
diff changeset
   226
  Properties.make_int Markup.offsetN offset @
13cee8c2ed8d clarified signature;
wenzelm
parents: 77776
diff changeset
   227
  Properties.make_int Markup.end_offsetN end_offset @
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77778
diff changeset
   228
  Properties.make_string Markup.labelN label @
77777
13cee8c2ed8d clarified signature;
wenzelm
parents: 77776
diff changeset
   229
  Properties.make_string Markup.fileN file @
13cee8c2ed8d clarified signature;
wenzelm
parents: 77776
diff changeset
   230
  Properties.make_string Markup.idN id;
26003
7a8aee8353cf added column field;
wenzelm
parents: 25954
diff changeset
   231
74182
72bb7e9143f7 minor performance tuning;
wenzelm
parents: 74181
diff changeset
   232
val def_properties_of = properties_of #> map (apfst Markup.def_name);
42327
7c7cc7590eb3 Name_Space.entry_markup: keep def position as separate properties;
wenzelm
parents: 42204
diff changeset
   233
71910
f8b0271cc744 tuned signature;
wenzelm
parents: 71465
diff changeset
   234
fun entity_markup kind (name, pos) =
f8b0271cc744 tuned signature;
wenzelm
parents: 71465
diff changeset
   235
  Markup.entity kind name |> Markup.properties (def_properties_of pos);
f8b0271cc744 tuned signature;
wenzelm
parents: 71465
diff changeset
   236
74262
839a6e284545 tuned signature;
wenzelm
parents: 74183
diff changeset
   237
fun make_entity_markup {def} serial kind (name, pos) =
74183
af81e4a307be clarified signature;
wenzelm
parents: 74182
diff changeset
   238
  let
af81e4a307be clarified signature;
wenzelm
parents: 74182
diff changeset
   239
    val props =
af81e4a307be clarified signature;
wenzelm
parents: 74182
diff changeset
   240
      if def then (Markup.defN, Value.print_int serial) :: properties_of pos
af81e4a307be clarified signature;
wenzelm
parents: 74182
diff changeset
   241
      else (Markup.refN, Value.print_int serial) :: def_properties_of pos;
af81e4a307be clarified signature;
wenzelm
parents: 74182
diff changeset
   242
  in Markup.entity kind name |> Markup.properties props end;
45412
7797f5351ec4 entity markup for bound variables;
wenzelm
parents: 44736
diff changeset
   243
80875
2e33897071b6 clarified signature;
wenzelm
parents: 80873
diff changeset
   244
val markup_properties = Markup.properties o properties_of;
2e33897071b6 clarified signature;
wenzelm
parents: 80873
diff changeset
   245
fun markup pos = markup_properties pos Markup.position;
39440
4c2547af5909 simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents: 38887
diff changeset
   246
4c2547af5909 simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents: 38887
diff changeset
   247
4c2547af5909 simplified/clarified (Context_)Position.markup/reported_text;
wenzelm
parents: 38887
diff changeset
   248
(* 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
   249
74166
ff3dbb2be924 tuned signature;
wenzelm
parents: 74165
diff changeset
   250
fun is_reported pos = is_some (offset_of pos) andalso is_some (id_of pos);
56459
38d0b2099743 no report for position singularity, notably for aux. file, especially empty one;
wenzelm
parents: 56437
diff changeset
   251
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: 29307
diff changeset
   252
80868
0ed02f473cf9 more robust reports: ensure that markup is actually present;
wenzelm
parents: 78811
diff changeset
   253
fun reported_text pos m txt =
80875
2e33897071b6 clarified signature;
wenzelm
parents: 80873
diff changeset
   254
  if is_reported pos then Markup.markup (markup_properties pos m) txt else "";
80868
0ed02f473cf9 more robust reports: ensure that markup is actually present;
wenzelm
parents: 78811
diff changeset
   255
0ed02f473cf9 more robust reports: ensure that markup is actually present;
wenzelm
parents: 78811
diff changeset
   256
fun report_text pos markup txt =
0ed02f473cf9 more robust reports: ensure that markup is actually present;
wenzelm
parents: 78811
diff changeset
   257
  if Print_Mode.PIDE_enabled () then Output.report [reported_text pos markup txt] else ();
0ed02f473cf9 more robust reports: ensure that markup is actually present;
wenzelm
parents: 78811
diff changeset
   258
39507
839873937ddd tuned signature of (Context_)Position.report variants;
wenzelm
parents: 39440
diff changeset
   259
fun report pos markup = report_text pos markup "";
27764
e0ee3cc240fe advance: single argument (again);
wenzelm
parents: 27759
diff changeset
   260
44736
c2a3f1c84179 bulk reports for improved message throughput;
wenzelm
parents: 44735
diff changeset
   261
type report = T * Markup.T;
48767
7f0c469cc796 reports with body text, not just markup;
wenzelm
parents: 45666
diff changeset
   262
type report_text = report * string;
44736
c2a3f1c84179 bulk reports for improved message throughput;
wenzelm
parents: 44735
diff changeset
   263
80868
0ed02f473cf9 more robust reports: ensure that markup is actually present;
wenzelm
parents: 78811
diff changeset
   264
fun reports_text args =
0ed02f473cf9 more robust reports: ensure that markup is actually present;
wenzelm
parents: 78811
diff changeset
   265
  if Print_Mode.PIDE_enabled () then
0ed02f473cf9 more robust reports: ensure that markup is actually present;
wenzelm
parents: 78811
diff changeset
   266
    Output.report (args |> map (fn ((pos, m), txt) =>
80875
2e33897071b6 clarified signature;
wenzelm
parents: 80873
diff changeset
   267
      if is_reported pos then Markup.markup (markup_properties pos m) txt else ""))
80868
0ed02f473cf9 more robust reports: ensure that markup is actually present;
wenzelm
parents: 78811
diff changeset
   268
  else ();
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 41504
diff changeset
   269
48767
7f0c469cc796 reports with body text, not just markup;
wenzelm
parents: 45666
diff changeset
   270
val reports = map (rpair "") #> reports_text;
7f0c469cc796 reports with body text, not just markup;
wenzelm
parents: 45666
diff changeset
   271
55959
c3b458435f4f more decisive commitment to get_free vs. the_const;
wenzelm
parents: 55922
diff changeset
   272
fun append_reports (r: report_text list Unsynchronized.ref) reports =
c3b458435f4f more decisive commitment to get_free vs. the_const;
wenzelm
parents: 55922
diff changeset
   273
  Unsynchronized.change r (append (map (rpair "") reports));
c3b458435f4f more decisive commitment to get_free vs. the_const;
wenzelm
parents: 55922
diff changeset
   274
25817
d8e0190917a5 type T: based on properties, added id field;
wenzelm
parents: 25792
diff changeset
   275
56532
3da244bc02bd tuned message, to accommodate extra brackets produced by Scala parsers;
wenzelm
parents: 56459
diff changeset
   276
(* here: user output *)
26003
7a8aee8353cf added column field;
wenzelm
parents: 25954
diff changeset
   277
71465
910a081cca74 more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents: 70498
diff changeset
   278
fun here_strs pos =
910a081cca74 more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents: 70498
diff changeset
   279
  (case (line_of pos, file_of pos) of
910a081cca74 more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents: 70498
diff changeset
   280
    (SOME i, NONE) => (" ", "(line " ^ Value.print_int i ^ ")")
910a081cca74 more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents: 70498
diff changeset
   281
  | (SOME i, SOME name) => (" ", "(line " ^ Value.print_int i ^ " of " ^ quote name ^ ")")
910a081cca74 more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents: 70498
diff changeset
   282
  | (NONE, SOME name) => (" ", "(file " ^ quote name ^ ")")
910a081cca74 more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents: 70498
diff changeset
   283
  | _ => if is_reported pos then ("", "\092<^here>") else ("", ""));
910a081cca74 more position information for oracles (e.g. "skip_proof" for 'sorry'), requires Proofterm.proofs := 1;
wenzelm
parents: 70498
diff changeset
   284
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48767
diff changeset
   285
fun here pos =
80873
e71cb37c7395 clarified signature: more explicit operations;
wenzelm
parents: 80868
diff changeset
   286
  let val (s1, s2) = here_strs pos
80875
2e33897071b6 clarified signature;
wenzelm
parents: 80873
diff changeset
   287
  in if s2 = "" then "" else s1 ^ Markup.markup (markup pos) s2 end;
25817
d8e0190917a5 type T: based on properties, added id field;
wenzelm
parents: 25792
diff changeset
   288
70498
de75eea6ffc8 tuned message;
wenzelm
parents: 69348
diff changeset
   289
val here_list = map here #> distinct (op =) #> implode;
49691
74ad6ecf2af2 more error positions;
wenzelm
parents: 49528
diff changeset
   290
23627
f543538866a2 added property conversions;
wenzelm
parents: 22158
diff changeset
   291
27736
3703dbd0cdea added encode_range;
wenzelm
parents: 27661
diff changeset
   292
(* range *)
3703dbd0cdea added encode_range;
wenzelm
parents: 27661
diff changeset
   293
3703dbd0cdea added encode_range;
wenzelm
parents: 27661
diff changeset
   294
type range = T * T;
3703dbd0cdea added encode_range;
wenzelm
parents: 27661
diff changeset
   295
80978
5e2b1588c5cb minor performance tuning: proper table for parsetree list;
wenzelm
parents: 80875
diff changeset
   296
val range_ord = prod_ord ord ord;
5e2b1588c5cb minor performance tuning: proper table for parsetree list;
wenzelm
parents: 80875
diff changeset
   297
27796
a6da5f68e776 added distance_of (permissive version);
wenzelm
parents: 27795
diff changeset
   298
val no_range = (none, none);
a6da5f68e776 added distance_of (permissive version);
wenzelm
parents: 27795
diff changeset
   299
77769
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   300
fun no_range_position (Pos {line, offset, end_offset = _, props}) =
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   301
  Pos {line = line, offset = offset, end_offset = 0, props = props};
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   302
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   303
fun range_position (Pos {line, offset, end_offset = _, props}, Pos {offset = offset', ...}) =
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   304
  Pos {line = line, offset = offset, end_offset = offset', props = props};
17391f298cf5 misc tuning and clarification: more tight representation;
wenzelm
parents: 74262
diff changeset
   305
62800
7ac100f86863 tuned signature;
wenzelm
parents: 62798
diff changeset
   306
fun range (pos, pos') = (range_position (pos, pos'), no_range_position pos');
27777
7a63d1b50b88 only increment column if valid;
wenzelm
parents: 27764
diff changeset
   307
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   308
fun range_of_properties props =
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   309
  let
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   310
    val pos = of_properties props;
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   311
    val pos' =
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   312
      of_props {line = Properties.get_int props Markup.end_lineN,
77772
7e0d920b4e6e tuned signature;
wenzelm
parents: 77771
diff changeset
   313
        offset = Properties.get_int props Markup.end_offsetN,
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   314
        end_offset = 0,
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   315
        props = props};
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   316
  in (pos, pos') end;
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   317
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   318
fun properties_of_range (pos, pos') =
77777
13cee8c2ed8d clarified signature;
wenzelm
parents: 77776
diff changeset
   319
  properties_of pos @ Properties.make_int Markup.end_lineN (the_default 0 (line_of pos'));
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58854
diff changeset
   320
27764
e0ee3cc240fe advance: single argument (again);
wenzelm
parents: 27759
diff changeset
   321
e0ee3cc240fe advance: single argument (again);
wenzelm
parents: 27759
diff changeset
   322
(* thread data *)
e0ee3cc240fe advance: single argument (again);
wenzelm
parents: 27759
diff changeset
   323
77776
58e53c61f15f more compact data;
wenzelm
parents: 77774
diff changeset
   324
val thread_data = Pos o Thread_Position.get;
62940
a03592aafadf tuned -- avoid recoding properties;
wenzelm
parents: 62929
diff changeset
   325
fun setmp_thread_data pos = Thread_Position.setmp (dest pos);
27764
e0ee3cc240fe advance: single argument (again);
wenzelm
parents: 27759
diff changeset
   326
78023
76dece8cd8a7 more informative position information;
wenzelm
parents: 78021
diff changeset
   327
fun setmp_thread_data_label a f x =
76dece8cd8a7 more informative position information;
wenzelm
parents: 78021
diff changeset
   328
  if a = "" then f x
76dece8cd8a7 more informative position information;
wenzelm
parents: 78021
diff changeset
   329
  else setmp_thread_data (label a (thread_data ())) f x;
76dece8cd8a7 more informative position information;
wenzelm
parents: 78021
diff changeset
   330
33097
9d501e11084a maintain position of formal entities via name space;
wenzelm
parents: 32573
diff changeset
   331
fun default pos =
49528
789b73fcca72 report proper binding positions only -- avoid swamping document model with unspecific information;
wenzelm
parents: 48992
diff changeset
   332
  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
   333
  else (true, pos);
33097
9d501e11084a maintain position of formal entities via name space;
wenzelm
parents: 32573
diff changeset
   334
27764
e0ee3cc240fe advance: single argument (again);
wenzelm
parents: 27759
diff changeset
   335
end;