src/Pure/General/path.ML
author wenzelm
Fri, 16 Aug 2013 22:57:16 +0200
changeset 53045 4c297ee47c28
parent 52106 090a519982e9
child 56134 4a7a07c01857
permissions -rw-r--r--
check_tool wrt. official ISABELLE_TOOLS; added Path.split (cf. Scala version);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6118
caa439435666 fixed titles;
wenzelm
parents: 5011
diff changeset
     1
(*  Title:      Pure/General/path.ML
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     3
43601
fd650d659275 tuned comments;
wenzelm
parents: 43599
diff changeset
     4
Algebra of file-system paths: basic POSIX notation, extended by named
fd650d659275 tuned comments;
wenzelm
parents: 43599
diff changeset
     5
roots (e.g. //foo) and variables (e.g. $BAR).
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     6
*)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     7
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     8
signature PATH =
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     9
sig
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    10
  eqtype T
6460
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    11
  val is_current: T -> bool
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    12
  val current: T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    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
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    15
  val parent: T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    16
  val basic: string -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    17
  val variable: string -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    18
  val is_absolute: T -> bool
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    19
  val is_basic: T -> bool
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    20
  val append: T -> T -> T
6270
c905fe5994a2 val appends: T list -> T;
wenzelm
parents: 6223
diff changeset
    21
  val appends: T list -> T
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    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
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 52106
diff changeset
    25
  val split: string -> T list
43593
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
    26
  val pretty: T -> Pretty.T
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
    27
  val print: T -> string
14912
88b9d9165452 added split_ext; removed drop_ext;
wenzelm
parents: 8806
diff changeset
    28
  val dir: T -> T
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    29
  val base: T -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    30
  val ext: string -> T -> T
14912
88b9d9165452 added split_ext; removed drop_ext;
wenzelm
parents: 8806
diff changeset
    31
  val split_ext: T -> T * string
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    32
  val expand: T -> T
26881
bb68f50644a9 renamed Position.path to Path.position;
wenzelm
parents: 23863
diff changeset
    33
  val position: T -> Position.T
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    34
end;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    35
6187
c6c4626ef693 enabled sig;
wenzelm
parents: 6183
diff changeset
    36
structure Path: PATH =
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    37
struct
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    38
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    39
(* path elements *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    40
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    41
datatype elem =
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    42
  Root of string |
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    43
  Basic of string |
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    44
  Variable of string |
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    45
  Parent;
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    46
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    47
local
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    48
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    49
fun err_elem msg s = error (msg ^ " path element specification " ^ quote s);
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    50
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    51
fun check_elem s =
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    52
  if s = "" orelse s = "~" orelse s = "~~" then err_elem "Illegal" s
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    53
  else
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    54
    let
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    55
      fun check c =
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    56
        if exists_string (fn c' => c = c') s then
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    57
          err_elem ("Illegal character " ^ quote c ^ " in") s
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    58
        else ();
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    59
      val _ = List.app check ["/", "\\", "$", ":", "\"", "'"];
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    60
    in s end;
6223
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    61
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    62
in
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    63
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    64
val root_elem = Root o check_elem;
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    65
val basic_elem = Basic o check_elem;
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    66
val variable_elem = Variable o check_elem;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    67
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    68
end;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    69
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    70
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    71
(* type path *)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    72
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    73
datatype T = Path of elem list;    (*reversed elements*)
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    74
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    75
fun rep (Path xs) = xs;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    76
6460
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    77
fun is_current (Path []) = true
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    78
  | is_current _ = false;
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    79
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    80
val current = Path [];
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    81
val root = Path [Root ""];
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    82
fun named_root s = Path [root_elem s];
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    83
fun basic s = Path [basic_elem s];
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    84
fun variable s = Path [variable_elem s];
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    85
val parent = Path [Parent];
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    86
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    87
fun is_absolute (Path xs) =
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    88
  (case try List.last xs of
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    89
    SOME (Root _) => true
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    90
  | _ => false);
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    91
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    92
fun is_basic (Path [Basic _]) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    93
  | is_basic _ = false;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    94
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    95
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    96
(* append and norm *)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    97
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    98
fun apply (y as Root _) _ = [y]
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    99
  | apply Parent (xs as (Root _ :: _)) = xs
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   100
  | apply Parent (Basic _ :: rest) = rest
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   101
  | apply y xs = y :: xs;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   102
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   103
fun append (Path xs) (Path ys) = Path (fold_rev apply ys xs);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   104
fun appends paths = Library.foldl (uncurry append) (current, paths);
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
   105
val make = appends o map basic;
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   106
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   107
fun norm elems = fold_rev apply elems [];
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   108
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   109
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   110
(* implode *)
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   111
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   112
local
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   113
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   114
fun implode_elem (Root "") = ""
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   115
  | 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
   116
  | implode_elem (Basic s) = s
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   117
  | implode_elem (Variable s) = "$" ^ s
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   118
  | implode_elem Parent = "..";
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   119
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   120
in
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   121
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   122
fun implode_path (Path []) = "."
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   123
  | implode_path (Path [Root ""]) = "/"
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   124
  | 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
   125
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   126
end;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   127
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   128
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   129
(* explode *)
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   130
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   131
fun explode_path str =
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   132
  let
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   133
    fun explode_elem s =
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   134
     (if s = ".." then Parent
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   135
      else if s = "~" then Variable "USER_HOME"
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   136
      else if s = "~~" then Variable "ISABELLE_HOME"
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   137
      else
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   138
        (case try (unprefix "$") s of
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   139
          SOME s' => variable_elem s'
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   140
        | NONE => basic_elem s))
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   141
      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
   142
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   143
    val (roots, raw_elems) =
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   144
      (case take_prefix (equal "") (space_explode "/" str) |>> length of
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   145
        (0, es) => ([], es)
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   146
      | (1, es) => ([Root ""], es)
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   147
      | (_, []) => ([Root ""], [])
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   148
      | (_, e :: es) => ([root_elem e], es));
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   149
    val elems = raw_elems |> filter_out (fn c => c = "" orelse c = ".") |> map explode_elem;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   150
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   151
  in Path (norm (rev elems @ roots)) end;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   152
53045
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 52106
diff changeset
   153
fun split str =
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 52106
diff changeset
   154
  space_explode ":" str
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 52106
diff changeset
   155
  |> map_filter (fn s => if s = "" then NONE else SOME (explode_path s));
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 52106
diff changeset
   156
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   157
43593
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   158
(* print *)
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   159
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   160
fun pretty path =
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   161
  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
   162
  in Pretty.mark (Markup.path s) (Pretty.str (quote s)) end;
43593
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   163
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   164
val print = Pretty.str_of o pretty;
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   165
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   166
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   167
(* base element *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   168
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   169
fun split_path f (Path (Basic s :: xs)) = f (Path xs, s)
43599
e0ee016fc4fd proper Path.print;
wenzelm
parents: 43593
diff changeset
   170
  | split_path _ path = error ("Cannot split path into dir/base: " ^ print path);
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   171
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   172
val dir = split_path #1;
7929
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   173
val base = split_path (fn (_, s) => Path [Basic s]);
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   174
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   175
fun ext "" = I
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   176
  | ext e = split_path (fn (prfx, s) => append prfx (basic (s ^ "." ^ e)));
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   177
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   178
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
   179
  (case take_suffix (fn c => c <> ".") (raw_explode s) of
14912
88b9d9165452 added split_ext; removed drop_ext;
wenzelm
parents: 8806
diff changeset
   180
    ([], _) => (Path [Basic s], "")
33957
e9afca2118d4 normalized uncurry take/drop
haftmann
parents: 33955
diff changeset
   181
  | (cs, e) => (Path [Basic (implode (take (length cs - 1) cs))], implode e)));
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
   182
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   183
17827
b32fad049413 expand: error on undefined/empty env variable;
wenzelm
parents: 15570
diff changeset
   184
(* expand variables *)
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   185
48658
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   186
fun eval (Variable s) =
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   187
      let val path = explode_path (getenv_strict s) in
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   188
        if exists (fn Variable _ => true | _ => false) (rep path) then
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   189
          error ("Illegal path variable nesting: " ^ s ^ "=" ^ print path)
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   190
        else rep path
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   191
      end
17827
b32fad049413 expand: error on undefined/empty env variable;
wenzelm
parents: 15570
diff changeset
   192
  | eval x = [x];
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   193
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19305
diff changeset
   194
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
   195
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   196
48866
034df7b05759 tuned comment;
wenzelm
parents: 48658
diff changeset
   197
(* source position -- with smart replacement of ISABELLE_HOME *)
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
   198
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
val isabelle_home = explode_path "~~";
26881
bb68f50644a9 renamed Position.path to Path.position;
wenzelm
parents: 23863
diff changeset
   200
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
fun position path =
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   202
  let
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   203
    val s = implode_path path;
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   204
    val prfx = implode_path (expand isabelle_home) ^ "/";
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   205
  in
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   206
    Position.file
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   207
      (case try (unprefix prfx) s of
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   208
        NONE => s
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   209
      | SOME s' => "~~/" ^ s')
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   210
  end;
26881
bb68f50644a9 renamed Position.path to Path.position;
wenzelm
parents: 23863
diff changeset
   211
bb68f50644a9 renamed Position.path to Path.position;
wenzelm
parents: 23863
diff changeset
   212
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   213
(*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
   214
val implode = implode_path;
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   215
val explode = explode_path;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   216
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   217
end;
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   218