author | wenzelm |
Sat, 26 May 2018 21:23:51 +0200 | |
changeset 68293 | 2bc4e5d9cca6 |
parent 67522 | 9e712280cc37 |
child 68519 | e1c24b628ca5 |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/path.ML |
5011 | 2 |
Author: Markus Wenzel, TU Muenchen |
3 |
||
43601 | 4 |
Algebra of file-system paths: basic POSIX notation, extended by named |
5 |
roots (e.g. //foo) and variables (e.g. $BAR). |
|
5011 | 6 |
*) |
7 |
||
8 |
signature PATH = |
|
9 |
sig |
|
6183 | 10 |
eqtype T |
6460 | 11 |
val is_current: T -> bool |
6183 | 12 |
val current: T |
13 |
val root: T |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
14 |
val named_root: string -> T |
6183 | 15 |
val parent: T |
16 |
val basic: string -> T |
|
17 |
val variable: string -> T |
|
18 |
val is_absolute: T -> bool |
|
19 |
val is_basic: T -> bool |
|
56533
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset
|
20 |
val starts_basic: T -> bool |
6183 | 21 |
val append: T -> T -> T |
6270 | 22 |
val appends: T list -> T |
6319 | 23 |
val make: string list -> T |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
24 |
val implode: T -> string |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
25 |
val explode: string -> T |
59363
4660b0409096
added Path.decode in ML, in correspondence to Path.encode in Scala;
wenzelm
parents:
56533
diff
changeset
|
26 |
val decode: T XML.Decode.T |
53045 | 27 |
val split: string -> T list |
43593 | 28 |
val pretty: T -> Pretty.T |
29 |
val print: T -> string |
|
14912 | 30 |
val dir: T -> T |
6183 | 31 |
val base: T -> T |
65999 | 32 |
val base_name: T -> string |
6183 | 33 |
val ext: string -> T -> T |
14912 | 34 |
val split_ext: T -> T * string |
6183 | 35 |
val expand: T -> T |
56134 | 36 |
val smart_implode: T -> string |
26881 | 37 |
val position: T -> Position.T |
5011 | 38 |
end; |
39 |
||
6187 | 40 |
structure Path: PATH = |
5011 | 41 |
struct |
42 |
||
6183 | 43 |
(* path elements *) |
44 |
||
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
45 |
datatype elem = |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
46 |
Root of string | |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
47 |
Basic of string | |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
48 |
Variable of string | |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
49 |
Parent; |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
50 |
|
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
51 |
local |
6183 | 52 |
|
52106 | 53 |
fun err_elem msg s = error (msg ^ " path element specification " ^ quote s); |
6183 | 54 |
|
52106 | 55 |
fun check_elem s = |
56 |
if s = "" orelse s = "~" orelse s = "~~" then err_elem "Illegal" s |
|
57 |
else |
|
58 |
let |
|
59 |
fun check c = |
|
60 |
if exists_string (fn c' => c = c') s then |
|
61 |
err_elem ("Illegal character " ^ quote c ^ " in") s |
|
62 |
else (); |
|
63 |
val _ = List.app check ["/", "\\", "$", ":", "\"", "'"]; |
|
64 |
in s end; |
|
6223 | 65 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
66 |
in |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
67 |
|
52106 | 68 |
val root_elem = Root o check_elem; |
69 |
val basic_elem = Basic o check_elem; |
|
70 |
val variable_elem = Variable o check_elem; |
|
6183 | 71 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
72 |
end; |
6183 | 73 |
|
74 |
||
5011 | 75 |
(* type path *) |
76 |
||
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
77 |
datatype T = Path of elem list; (*reversed elements*) |
6183 | 78 |
|
79 |
fun rep (Path xs) = xs; |
|
5011 | 80 |
|
6460 | 81 |
fun is_current (Path []) = true |
82 |
| is_current _ = false; |
|
83 |
||
5011 | 84 |
val current = Path []; |
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
85 |
val root = Path [Root ""]; |
52106 | 86 |
fun named_root s = Path [root_elem s]; |
87 |
fun basic s = Path [basic_elem s]; |
|
88 |
fun variable s = Path [variable_elem s]; |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
89 |
val parent = Path [Parent]; |
5011 | 90 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
91 |
fun is_absolute (Path xs) = |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
92 |
(case try List.last xs of |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
93 |
SOME (Root _) => true |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
94 |
| _ => false); |
5011 | 95 |
|
6183 | 96 |
fun is_basic (Path [Basic _]) = true |
97 |
| is_basic _ = false; |
|
5011 | 98 |
|
56533
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset
|
99 |
fun starts_basic (Path xs) = |
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset
|
100 |
(case try List.last xs of |
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset
|
101 |
SOME (Basic _) => true |
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset
|
102 |
| _ => false); |
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset
|
103 |
|
5011 | 104 |
|
105 |
(* append and norm *) |
|
106 |
||
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
107 |
fun apply (y as Root _) _ = [y] |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
108 |
| apply Parent (xs as (Root _ :: _)) = xs |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
109 |
| apply Parent (Basic _ :: rest) = rest |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
110 |
| apply y xs = y :: xs; |
5011 | 111 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
112 |
fun append (Path xs) (Path ys) = Path (fold_rev apply ys xs); |
15570 | 113 |
fun appends paths = Library.foldl (uncurry append) (current, paths); |
6319 | 114 |
val make = appends o map basic; |
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
115 |
|
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
116 |
fun norm elems = fold_rev apply elems []; |
5011 | 117 |
|
6183 | 118 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
119 |
(* implode *) |
5011 | 120 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
121 |
local |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
122 |
|
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
123 |
fun implode_elem (Root "") = "" |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
124 |
| implode_elem (Root s) = "//" ^ s |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
125 |
| implode_elem (Basic s) = s |
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
126 |
| implode_elem (Variable s) = "$" ^ s |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
127 |
| implode_elem Parent = ".."; |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
128 |
|
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
129 |
in |
5011 | 130 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
131 |
fun implode_path (Path []) = "." |
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
132 |
| implode_path (Path [Root ""]) = "/" |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
133 |
| implode_path (Path xs) = space_implode "/" (rev (map implode_elem xs)); |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
134 |
|
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
135 |
end; |
5011 | 136 |
|
137 |
||
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
138 |
(* explode *) |
6183 | 139 |
|
52106 | 140 |
fun explode_path str = |
141 |
let |
|
142 |
fun explode_elem s = |
|
143 |
(if s = ".." then Parent |
|
144 |
else if s = "~" then Variable "USER_HOME" |
|
145 |
else if s = "~~" then Variable "ISABELLE_HOME" |
|
146 |
else |
|
147 |
(case try (unprefix "$") s of |
|
148 |
SOME s' => variable_elem s' |
|
149 |
| NONE => basic_elem s)) |
|
150 |
handle ERROR msg => cat_error msg ("The error(s) above occurred in " ^ quote str); |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
151 |
|
52106 | 152 |
val (roots, raw_elems) = |
67522 | 153 |
(case chop_prefix (equal "") (space_explode "/" str) |>> length of |
52106 | 154 |
(0, es) => ([], es) |
155 |
| (1, es) => ([Root ""], es) |
|
156 |
| (_, []) => ([Root ""], []) |
|
157 |
| (_, e :: es) => ([root_elem e], es)); |
|
158 |
val elems = raw_elems |> filter_out (fn c => c = "" orelse c = ".") |> map explode_elem; |
|
6183 | 159 |
|
52106 | 160 |
in Path (norm (rev elems @ roots)) end; |
6183 | 161 |
|
53045 | 162 |
fun split str = |
163 |
space_explode ":" str |
|
164 |
|> map_filter (fn s => if s = "" then NONE else SOME (explode_path s)); |
|
165 |
||
59363
4660b0409096
added Path.decode in ML, in correspondence to Path.encode in Scala;
wenzelm
parents:
56533
diff
changeset
|
166 |
val decode = XML.Decode.string #> explode_path; |
4660b0409096
added Path.decode in ML, in correspondence to Path.encode in Scala;
wenzelm
parents:
56533
diff
changeset
|
167 |
|
5011 | 168 |
|
43593 | 169 |
(* print *) |
170 |
||
171 |
fun pretty path = |
|
172 |
let val s = implode_path path |
|
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48866
diff
changeset
|
173 |
in Pretty.mark (Markup.path s) (Pretty.str (quote s)) end; |
43593 | 174 |
|
61877
276ad4354069
renamed Pretty.str_of to Pretty.unformatted_string_of to emphasize its meaning;
wenzelm
parents:
59363
diff
changeset
|
175 |
val print = Pretty.unformatted_string_of o pretty; |
43593 | 176 |
|
62819
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
wenzelm
parents:
62663
diff
changeset
|
177 |
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty); |
62663 | 178 |
|
43593 | 179 |
|
6183 | 180 |
(* base element *) |
181 |
||
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
182 |
fun split_path f (Path (Basic s :: xs)) = f (Path xs, s) |
43599 | 183 |
| split_path _ path = error ("Cannot split path into dir/base: " ^ print path); |
6183 | 184 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
185 |
val dir = split_path #1; |
7929 | 186 |
val base = split_path (fn (_, s) => Path [Basic s]); |
65999 | 187 |
val base_name = implode_path o base; |
6183 | 188 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
189 |
fun ext "" = I |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
190 |
| ext e = split_path (fn (prfx, s) => append prfx (basic (s ^ "." ^ e))); |
6183 | 191 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
192 |
val split_ext = split_path (fn (prfx, s) => apfst (append prfx) |
67522 | 193 |
(case chop_suffix (fn c => c <> ".") (raw_explode s) of |
14912 | 194 |
([], _) => (Path [Basic s], "") |
33957 | 195 |
| (cs, e) => (Path [Basic (implode (take (length cs - 1) cs))], implode e))); |
6319 | 196 |
|
6183 | 197 |
|
17827 | 198 |
(* expand variables *) |
5011 | 199 |
|
48658 | 200 |
fun eval (Variable s) = |
201 |
let val path = explode_path (getenv_strict s) in |
|
202 |
if exists (fn Variable _ => true | _ => false) (rep path) then |
|
203 |
error ("Illegal path variable nesting: " ^ s ^ "=" ^ print path) |
|
204 |
else rep path |
|
205 |
end |
|
17827 | 206 |
| eval x = [x]; |
5011 | 207 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19305
diff
changeset
|
208 |
val expand = rep #> maps eval #> norm #> Path; |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
209 |
|
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
210 |
|
56136 | 211 |
(* smart implode *) |
44863
49ea566cb3b4
more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents:
44161
diff
changeset
|
212 |
|
56134 | 213 |
fun smart_implode path = |
44863
49ea566cb3b4
more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents:
44161
diff
changeset
|
214 |
let |
56134 | 215 |
val full_name = implode_path (expand path); |
56136 | 216 |
fun fold_path a = |
217 |
let val b = implode_path (expand (explode_path a)) in |
|
218 |
if full_name = b then SOME a |
|
219 |
else |
|
220 |
(case try (unprefix (b ^ "/")) full_name of |
|
221 |
SOME name => SOME (a ^ "/" ^ name) |
|
222 |
| NONE => NONE) |
|
223 |
end; |
|
44863
49ea566cb3b4
more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents:
44161
diff
changeset
|
224 |
in |
56136 | 225 |
(case get_first fold_path ["~~", "$ISABELLE_HOME_USER", "~"] of |
226 |
SOME name => name |
|
227 |
| NONE => implode_path path) |
|
44863
49ea566cb3b4
more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents:
44161
diff
changeset
|
228 |
end; |
26881 | 229 |
|
56134 | 230 |
val position = Position.file o smart_implode; |
26881 | 231 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
232 |
(*final declarations of this structure!*) |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
233 |
val implode = implode_path; |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
234 |
val explode = explode_path; |
5011 | 235 |
|
236 |
end; |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
237 |