6639
|
1 |
(* Title: Pure/General/url.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
Basic URLs.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature URL =
|
|
9 |
sig
|
|
10 |
type T
|
|
11 |
val file: Path.T -> T
|
|
12 |
val http: string * Path.T -> T
|
|
13 |
val ftp: string * Path.T -> T
|
|
14 |
val append: T -> T -> T
|
|
15 |
val pack: T -> string
|
|
16 |
val unpack: string -> T
|
|
17 |
end;
|
|
18 |
|
|
19 |
structure Url: URL =
|
|
20 |
struct
|
|
21 |
|
|
22 |
|
|
23 |
(* type url *)
|
|
24 |
|
|
25 |
datatype scheme = File | Http of string | Ftp of string;
|
|
26 |
|
|
27 |
datatype T = Url of scheme * Path.T;
|
|
28 |
|
|
29 |
fun file p = Url (File, p);
|
|
30 |
fun http (h, p) = Url (Http h, p);
|
|
31 |
fun ftp (h, p) = Url (Ftp h, p);
|
|
32 |
|
|
33 |
|
|
34 |
(* append *)
|
|
35 |
|
|
36 |
fun append (Url (s, p)) (Url (File, p')) = Url (s, Path.append p p')
|
|
37 |
| append _ url = url;
|
|
38 |
|
|
39 |
|
|
40 |
(* pack *)
|
|
41 |
|
|
42 |
fun pack_scheme File = ""
|
|
43 |
| pack_scheme (Http host) = "http://" ^ host
|
|
44 |
| pack_scheme (Ftp host) = "ftp://" ^ host;
|
|
45 |
|
|
46 |
fun pack (Url (s, p)) = pack_scheme s ^ (if Path.is_current p then "" else Path.pack p);
|
|
47 |
|
|
48 |
|
|
49 |
(* unpack *)
|
|
50 |
|
|
51 |
fun scan_lits [] x = ("", x)
|
|
52 |
| scan_lits (c :: cs) x = (($$ c -- scan_lits cs) >> op ^) x;
|
|
53 |
|
|
54 |
val scan_literal = scan_lits o Symbol.explode;
|
|
55 |
|
|
56 |
val scan_path = Scan.any Symbol.not_eof >> (Path.unpack o implode);
|
|
57 |
val scan_host = Scan.any1 (not_equal "/" andf Symbol.not_eof) >> implode;
|
|
58 |
|
|
59 |
val scan_url =
|
|
60 |
scan_literal "http://" |-- scan_host -- scan_path >> http ||
|
|
61 |
scan_literal "ftp://" |-- scan_host -- scan_path >> ftp ||
|
|
62 |
Scan.unless (scan_literal "http:" || scan_literal "ftp:") scan_path >> file;
|
|
63 |
|
|
64 |
fun unpack s = Symbol.scanner "Malformed URL" scan_url (Symbol.explode s);
|
|
65 |
|
|
66 |
|
|
67 |
end;
|