|
6118
|
1 |
(* Title: Pure/General/position.ML
|
|
5010
|
2 |
ID: $Id$
|
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
|
4 |
|
|
23673
|
5 |
Source positions.
|
|
5010
|
6 |
*)
|
|
|
7 |
|
|
|
8 |
signature POSITION =
|
|
|
9 |
sig
|
|
|
10 |
type T
|
|
23673
|
11 |
val line_of: T -> int option
|
|
26003
|
12 |
val column_of: T -> int option
|
|
23673
|
13 |
val file_of: T -> string option
|
|
5010
|
14 |
val none: T
|
|
|
15 |
val line: int -> T
|
|
23673
|
16 |
val file: string -> T
|
|
|
17 |
val path: Path.T -> T
|
|
26003
|
18 |
val advance: Symbol.symbol -> T -> T
|
|
25817
|
19 |
val of_properties: Markup.property list -> T
|
|
23627
|
20 |
val properties_of: T -> Markup.property list
|
|
26052
|
21 |
val default_properties: T -> Markup.property list -> Markup.property list
|
|
26003
|
22 |
val str_of: T -> string
|
|
25817
|
23 |
val thread_data: unit -> T
|
|
|
24 |
val setmp_thread_data: T -> ('a -> 'b) -> 'a -> 'b
|
|
25954
|
25 |
val setmp_thread_data_seq: T -> ('a -> 'b Seq.seq) -> 'a -> 'b Seq.seq
|
|
5010
|
26 |
end;
|
|
|
27 |
|
|
|
28 |
structure Position: POSITION =
|
|
|
29 |
struct
|
|
|
30 |
|
|
|
31 |
(* datatype position *)
|
|
|
32 |
|
|
26003
|
33 |
datatype T = Pos of (int * int) option * Markup.property list;
|
|
|
34 |
|
|
|
35 |
fun line_of (Pos (SOME (m, _), _)) = SOME m
|
|
|
36 |
| line_of _ = NONE;
|
|
5010
|
37 |
|
|
26003
|
38 |
fun column_of (Pos (SOME (_, n), _)) = SOME n
|
|
|
39 |
| column_of _ = NONE;
|
|
5010
|
40 |
|
|
26003
|
41 |
fun file_of (Pos (_, props)) = AList.lookup (op =) props Markup.fileN;
|
|
5010
|
42 |
|
|
25817
|
43 |
val none = Pos (NONE, []);
|
|
26003
|
44 |
fun line m = Pos (SOME (m, 1), []);
|
|
|
45 |
fun file name = Pos (SOME (1, 1), [(Markup.fileN, name)]);
|
|
|
46 |
val path = file o Path.implode o Path.expand;
|
|
5010
|
47 |
|
|
26003
|
48 |
fun advance "\n" (Pos (SOME (m, _), props)) = Pos (SOME (m + 1, 1), props)
|
|
|
49 |
| advance sym (pos as Pos (SOME (m, n), props)) =
|
|
|
50 |
if Symbol.is_regular sym then Pos (SOME (m, n + 1), props) else pos
|
|
|
51 |
| advance _ pos = pos;
|
|
5010
|
52 |
|
|
23627
|
53 |
|
|
|
54 |
(* markup properties *)
|
|
22158
|
55 |
|
|
26003
|
56 |
fun get_int props (name: string) =
|
|
|
57 |
(case AList.lookup (op =) props name of
|
|
|
58 |
NONE => NONE
|
|
|
59 |
| SOME s => Int.fromString s);
|
|
|
60 |
|
|
25817
|
61 |
fun of_properties props =
|
|
23627
|
62 |
let
|
|
26003
|
63 |
val count =
|
|
|
64 |
(case get_int props Markup.lineN of
|
|
|
65 |
NONE => NONE
|
|
|
66 |
| SOME m => SOME (m, the_default 1 (get_int props Markup.columnN)));
|
|
|
67 |
fun property name = the_list (find_first (fn (x: string, _) => x = name) props);
|
|
|
68 |
in (Pos (count, property Markup.fileN @ property Markup.idN)) end;
|
|
|
69 |
|
|
|
70 |
fun properties_of (Pos (count, props)) =
|
|
|
71 |
(case count of
|
|
|
72 |
NONE => []
|
|
|
73 |
| SOME (m, n) => [(Markup.lineN, string_of_int m), (Markup.columnN, string_of_int n)]) @ props;
|
|
|
74 |
|
|
26052
|
75 |
fun default_properties default props =
|
|
|
76 |
if exists (member (op =) Markup.position_properties o #1) props then props
|
|
|
77 |
else properties_of default @ props;
|
|
|
78 |
|
|
25817
|
79 |
|
|
26003
|
80 |
(* str_of *)
|
|
|
81 |
|
|
|
82 |
fun str_of pos =
|
|
|
83 |
let
|
|
|
84 |
val props = properties_of pos;
|
|
|
85 |
val s =
|
|
|
86 |
(case (line_of pos, file_of pos) of
|
|
|
87 |
(SOME m, NONE) => "(line " ^ string_of_int m ^ ")"
|
|
|
88 |
| (SOME m, SOME name) => "(line " ^ string_of_int m ^ " of " ^ quote name ^ ")"
|
|
|
89 |
| _ => "");
|
|
|
90 |
in
|
|
|
91 |
if null props then ""
|
|
|
92 |
else (if s = "" then "" else " ") ^ Markup.markup (Markup.properties props Markup.position) s
|
|
|
93 |
end;
|
|
25817
|
94 |
|
|
23627
|
95 |
|
|
25817
|
96 |
(* thread data *)
|
|
|
97 |
|
|
|
98 |
local val tag = Universal.tag () : T Universal.tag in
|
|
|
99 |
|
|
|
100 |
fun thread_data () = the_default none (Multithreading.get_data tag);
|
|
|
101 |
fun setmp_thread_data pos = Library.setmp_thread_data tag (thread_data ()) pos;
|
|
|
102 |
|
|
25954
|
103 |
fun setmp_thread_data_seq pos f seq =
|
|
|
104 |
setmp_thread_data pos f seq |> Seq.wrap (fn pull => setmp_thread_data pos pull ());
|
|
|
105 |
|
|
25817
|
106 |
end;
|
|
23673
|
107 |
|
|
5010
|
108 |
end;
|