|
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
|
|
|
12 |
val file_of: T -> string option
|
|
|
13 |
val inc: T -> T
|
|
5010
|
14 |
val none: T
|
|
|
15 |
val line: int -> T
|
|
23673
|
16 |
val file: string -> T
|
|
|
17 |
val path: Path.T -> T
|
|
25817
|
18 |
val of_properties: Markup.property list -> T
|
|
23627
|
19 |
val properties_of: T -> Markup.property list
|
|
25817
|
20 |
val thread_data: unit -> T
|
|
|
21 |
val setmp_thread_data: T -> ('a -> 'b) -> 'a -> 'b
|
|
25954
|
22 |
val setmp_thread_data_seq: T -> ('a -> 'b Seq.seq) -> 'a -> 'b Seq.seq
|
|
23673
|
23 |
val str_of: T -> string
|
|
5010
|
24 |
end;
|
|
|
25 |
|
|
|
26 |
structure Position: POSITION =
|
|
|
27 |
struct
|
|
|
28 |
|
|
|
29 |
(* datatype position *)
|
|
|
30 |
|
|
25817
|
31 |
datatype T = Pos of int option * Markup.property list;
|
|
5010
|
32 |
|
|
23627
|
33 |
fun line_of (Pos (opt_n, _)) = opt_n;
|
|
25817
|
34 |
fun file_of (Pos (_, props)) = AList.lookup (op =) props Markup.fileN ;
|
|
5010
|
35 |
|
|
15531
|
36 |
fun inc (pos as Pos (NONE, _)) = pos
|
|
25817
|
37 |
| inc (Pos (SOME n, props)) = Pos (SOME (n + 1), props);
|
|
5010
|
38 |
|
|
25817
|
39 |
val none = Pos (NONE, []);
|
|
|
40 |
fun line n = Pos (SOME n, []);
|
|
|
41 |
fun file s = Pos (SOME 1, [(Markup.fileN, s)]);
|
|
5010
|
42 |
|
|
23673
|
43 |
val path = file o Path.implode o Path.expand;
|
|
5010
|
44 |
|
|
23627
|
45 |
|
|
|
46 |
(* markup properties *)
|
|
22158
|
47 |
|
|
25817
|
48 |
fun of_properties props =
|
|
23627
|
49 |
let
|
|
|
50 |
val opt_n =
|
|
25817
|
51 |
(case AList.lookup (op =) props Markup.lineN of
|
|
23627
|
52 |
SOME s => Int.fromString s
|
|
|
53 |
| NONE => NONE);
|
|
25817
|
54 |
fun get name = the_list (find_first (fn (x: string, _) => x = name) props);
|
|
|
55 |
in (Pos (opt_n, get Markup.fileN @ get Markup.idN)) end;
|
|
|
56 |
|
|
|
57 |
fun properties_of (Pos (opt_n, props)) =
|
|
|
58 |
(case opt_n of SOME n => [(Markup.lineN, string_of_int n)] | NONE => []) @ props;
|
|
|
59 |
|
|
23627
|
60 |
|
|
25817
|
61 |
(* thread data *)
|
|
|
62 |
|
|
|
63 |
local val tag = Universal.tag () : T Universal.tag in
|
|
|
64 |
|
|
|
65 |
fun thread_data () = the_default none (Multithreading.get_data tag);
|
|
|
66 |
fun setmp_thread_data pos = Library.setmp_thread_data tag (thread_data ()) pos;
|
|
|
67 |
|
|
25954
|
68 |
fun setmp_thread_data_seq pos f seq =
|
|
|
69 |
setmp_thread_data pos f seq |> Seq.wrap (fn pull => setmp_thread_data pos pull ());
|
|
|
70 |
|
|
25817
|
71 |
end;
|
|
23673
|
72 |
|
|
|
73 |
|
|
|
74 |
(* str_of *)
|
|
|
75 |
|
|
25817
|
76 |
fun print (SOME n, NONE) = "(line " ^ string_of_int n ^ ")"
|
|
|
77 |
| print (NONE, SOME s) = "(" ^ s ^ ")"
|
|
|
78 |
| print (SOME n, SOME s) = "(line " ^ string_of_int n ^ " of " ^ quote s ^ ")";
|
|
23673
|
79 |
|
|
25817
|
80 |
fun str_of pos =
|
|
|
81 |
(case (line_of pos, file_of pos) of
|
|
|
82 |
(NONE, NONE) => ""
|
|
|
83 |
| res => " " ^ Markup.markup (Markup.properties (properties_of pos) Markup.position) (print res));
|
|
5010
|
84 |
|
|
|
85 |
end;
|