author | wenzelm |
Thu, 13 Mar 2014 12:09:43 +0100 | |
changeset 56136 | 81c66d9e274b |
parent 56134 | 4a7a07c01857 |
child 56533 | cd8b6d849b6a |
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 |
|
20 |
val append: T -> T -> T |
|
6270 | 21 |
val appends: T list -> T |
6319 | 22 |
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
|
23 |
val implode: T -> string |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
24 |
val explode: string -> T |
53045 | 25 |
val split: string -> T list |
43593 | 26 |
val pretty: T -> Pretty.T |
27 |
val print: T -> string |
|
14912 | 28 |
val dir: T -> T |
6183 | 29 |
val base: T -> T |
30 |
val ext: string -> T -> T |
|
14912 | 31 |
val split_ext: T -> T * string |
6183 | 32 |
val expand: T -> T |
56134 | 33 |
val smart_implode: T -> string |
26881 | 34 |
val position: T -> Position.T |
5011 | 35 |
end; |
36 |
||
6187 | 37 |
structure Path: PATH = |
5011 | 38 |
struct |
39 |
||
6183 | 40 |
(* path elements *) |
41 |
||
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
42 |
datatype elem = |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
43 |
Root of string | |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
44 |
Basic of string | |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
45 |
Variable of string | |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
46 |
Parent; |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
47 |
|
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
48 |
local |
6183 | 49 |
|
52106 | 50 |
fun err_elem msg s = error (msg ^ " path element specification " ^ quote s); |
6183 | 51 |
|
52106 | 52 |
fun check_elem s = |
53 |
if s = "" orelse s = "~" orelse s = "~~" then err_elem "Illegal" s |
|
54 |
else |
|
55 |
let |
|
56 |
fun check c = |
|
57 |
if exists_string (fn c' => c = c') s then |
|
58 |
err_elem ("Illegal character " ^ quote c ^ " in") s |
|
59 |
else (); |
|
60 |
val _ = List.app check ["/", "\\", "$", ":", "\"", "'"]; |
|
61 |
in s end; |
|
6223 | 62 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
63 |
in |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
64 |
|
52106 | 65 |
val root_elem = Root o check_elem; |
66 |
val basic_elem = Basic o check_elem; |
|
67 |
val variable_elem = Variable o check_elem; |
|
6183 | 68 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
69 |
end; |
6183 | 70 |
|
71 |
||
5011 | 72 |
(* type path *) |
73 |
||
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
74 |
datatype T = Path of elem list; (*reversed elements*) |
6183 | 75 |
|
76 |
fun rep (Path xs) = xs; |
|
5011 | 77 |
|
6460 | 78 |
fun is_current (Path []) = true |
79 |
| is_current _ = false; |
|
80 |
||
5011 | 81 |
val current = Path []; |
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
82 |
val root = Path [Root ""]; |
52106 | 83 |
fun named_root s = Path [root_elem s]; |
84 |
fun basic s = Path [basic_elem s]; |
|
85 |
fun variable s = Path [variable_elem s]; |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
86 |
val parent = Path [Parent]; |
5011 | 87 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
88 |
fun is_absolute (Path xs) = |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
89 |
(case try List.last xs of |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
90 |
SOME (Root _) => true |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
91 |
| _ => false); |
5011 | 92 |
|
6183 | 93 |
fun is_basic (Path [Basic _]) = true |
94 |
| is_basic _ = false; |
|
5011 | 95 |
|
96 |
||
97 |
(* append and norm *) |
|
98 |
||
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
99 |
fun apply (y as Root _) _ = [y] |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
100 |
| apply Parent (xs as (Root _ :: _)) = xs |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
101 |
| apply Parent (Basic _ :: rest) = rest |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
102 |
| apply y xs = y :: xs; |
5011 | 103 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
104 |
fun append (Path xs) (Path ys) = Path (fold_rev apply ys xs); |
15570 | 105 |
fun appends paths = Library.foldl (uncurry append) (current, paths); |
6319 | 106 |
val make = appends o map basic; |
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
107 |
|
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
108 |
fun norm elems = fold_rev apply elems []; |
5011 | 109 |
|
6183 | 110 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
111 |
(* implode *) |
5011 | 112 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
113 |
local |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
114 |
|
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
115 |
fun implode_elem (Root "") = "" |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
116 |
| 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
|
117 |
| implode_elem (Basic s) = s |
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
118 |
| implode_elem (Variable s) = "$" ^ s |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
119 |
| implode_elem Parent = ".."; |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
120 |
|
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
121 |
in |
5011 | 122 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
123 |
fun implode_path (Path []) = "." |
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
124 |
| implode_path (Path [Root ""]) = "/" |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
125 |
| 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
|
126 |
|
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
127 |
end; |
5011 | 128 |
|
129 |
||
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
130 |
(* explode *) |
6183 | 131 |
|
52106 | 132 |
fun explode_path str = |
133 |
let |
|
134 |
fun explode_elem s = |
|
135 |
(if s = ".." then Parent |
|
136 |
else if s = "~" then Variable "USER_HOME" |
|
137 |
else if s = "~~" then Variable "ISABELLE_HOME" |
|
138 |
else |
|
139 |
(case try (unprefix "$") s of |
|
140 |
SOME s' => variable_elem s' |
|
141 |
| NONE => basic_elem s)) |
|
142 |
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
|
143 |
|
52106 | 144 |
val (roots, raw_elems) = |
145 |
(case take_prefix (equal "") (space_explode "/" str) |>> length of |
|
146 |
(0, es) => ([], es) |
|
147 |
| (1, es) => ([Root ""], es) |
|
148 |
| (_, []) => ([Root ""], []) |
|
149 |
| (_, e :: es) => ([root_elem e], es)); |
|
150 |
val elems = raw_elems |> filter_out (fn c => c = "" orelse c = ".") |> map explode_elem; |
|
6183 | 151 |
|
52106 | 152 |
in Path (norm (rev elems @ roots)) end; |
6183 | 153 |
|
53045 | 154 |
fun split str = |
155 |
space_explode ":" str |
|
156 |
|> map_filter (fn s => if s = "" then NONE else SOME (explode_path s)); |
|
157 |
||
5011 | 158 |
|
43593 | 159 |
(* print *) |
160 |
||
161 |
fun pretty path = |
|
162 |
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
|
163 |
in Pretty.mark (Markup.path s) (Pretty.str (quote s)) end; |
43593 | 164 |
|
165 |
val print = Pretty.str_of o pretty; |
|
166 |
||
167 |
||
6183 | 168 |
(* base element *) |
169 |
||
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
170 |
fun split_path f (Path (Basic s :: xs)) = f (Path xs, s) |
43599 | 171 |
| split_path _ path = error ("Cannot split path into dir/base: " ^ print path); |
6183 | 172 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
173 |
val dir = split_path #1; |
7929 | 174 |
val base = split_path (fn (_, s) => Path [Basic s]); |
6183 | 175 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
176 |
fun ext "" = I |
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
177 |
| ext e = split_path (fn (prfx, s) => append prfx (basic (s ^ "." ^ e))); |
6183 | 178 |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
179 |
val split_ext = split_path (fn (prfx, s) => apfst (append prfx) |
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
36135
diff
changeset
|
180 |
(case take_suffix (fn c => c <> ".") (raw_explode s) of |
14912 | 181 |
([], _) => (Path [Basic s], "") |
33957 | 182 |
| (cs, e) => (Path [Basic (implode (take (length cs - 1) cs))], implode e))); |
6319 | 183 |
|
6183 | 184 |
|
17827 | 185 |
(* expand variables *) |
5011 | 186 |
|
48658 | 187 |
fun eval (Variable s) = |
188 |
let val path = explode_path (getenv_strict s) in |
|
189 |
if exists (fn Variable _ => true | _ => false) (rep path) then |
|
190 |
error ("Illegal path variable nesting: " ^ s ^ "=" ^ print path) |
|
191 |
else rep path |
|
192 |
end |
|
17827 | 193 |
| eval x = [x]; |
5011 | 194 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19305
diff
changeset
|
195 |
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
|
196 |
|
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
197 |
|
56136 | 198 |
(* 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
|
199 |
|
56134 | 200 |
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
|
201 |
let |
56134 | 202 |
val full_name = implode_path (expand path); |
56136 | 203 |
fun fold_path a = |
204 |
let val b = implode_path (expand (explode_path a)) in |
|
205 |
if full_name = b then SOME a |
|
206 |
else |
|
207 |
(case try (unprefix (b ^ "/")) full_name of |
|
208 |
SOME name => SOME (a ^ "/" ^ name) |
|
209 |
| NONE => NONE) |
|
210 |
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
|
211 |
in |
56136 | 212 |
(case get_first fold_path ["~~", "$ISABELLE_HOME_USER", "~"] of |
213 |
SOME name => name |
|
214 |
| 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
|
215 |
end; |
26881 | 216 |
|
56134 | 217 |
val position = Position.file o smart_implode; |
26881 | 218 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
219 |
(*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
|
220 |
val implode = implode_path; |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
221 |
val explode = explode_path; |
5011 | 222 |
|
223 |
end; |
|
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset
|
224 |