src/Pure/General/path.ML
author wenzelm
Mon Jul 09 23:12:36 2007 +0200 (2007-07-09)
changeset 23672 3fd7770f6795
parent 21858 05f57309170c
child 23863 8f3099589cfa
permissions -rw-r--r--
moved Path.position to Position.path;
wenzelm@6118
     1
(*  Title:      Pure/General/path.ML
wenzelm@5011
     2
    ID:         $Id$
wenzelm@5011
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5011
     4
wenzelm@6183
     5
Abstract algebra of file paths (external encoding Unix-style).
wenzelm@5011
     6
*)
wenzelm@5011
     7
wenzelm@5011
     8
signature PATH =
wenzelm@5011
     9
sig
wenzelm@6183
    10
  datatype elem = Root | Parent | Basic of string | Variable of string
wenzelm@6183
    11
  eqtype T
wenzelm@6183
    12
  val rep: T -> elem list
wenzelm@6460
    13
  val is_current: T -> bool
wenzelm@6183
    14
  val current: T
wenzelm@6183
    15
  val root: T
wenzelm@6183
    16
  val parent: T
wenzelm@6183
    17
  val basic: string -> T
wenzelm@6183
    18
  val variable: string -> T
wenzelm@6183
    19
  val is_absolute: T -> bool
wenzelm@6183
    20
  val is_basic: T -> bool
wenzelm@6183
    21
  val append: T -> T -> T
wenzelm@6270
    22
  val appends: T list -> T
wenzelm@6319
    23
  val make: string list -> T
wenzelm@21858
    24
  val implode: T -> string
wenzelm@21858
    25
  val explode: string -> T
wenzelm@14912
    26
  val dir: T -> T
wenzelm@6183
    27
  val base: T -> T
wenzelm@6183
    28
  val ext: string -> T -> T
wenzelm@14912
    29
  val split_ext: T -> T * string
wenzelm@6183
    30
  val expand: T -> T
wenzelm@5011
    31
end;
wenzelm@5011
    32
wenzelm@6187
    33
structure Path: PATH =
wenzelm@5011
    34
struct
wenzelm@5011
    35
wenzelm@6183
    36
wenzelm@6183
    37
(* path elements *)
wenzelm@6183
    38
wenzelm@6183
    39
datatype elem = Root | Parent | Basic of string | Variable of string;
wenzelm@6183
    40
wenzelm@6223
    41
fun err_elem msg chs = error (msg ^ " path element specification: " ^ quote (implode chs));
wenzelm@6183
    42
wenzelm@6319
    43
fun check_elem (chs as []) = err_elem "Illegal" chs
wenzelm@6319
    44
  | check_elem (chs as ["~"]) = err_elem "Illegal" chs
wenzelm@6223
    45
  | check_elem (chs as ["~", "~"]) = err_elem "Illegal" chs
wenzelm@6223
    46
  | check_elem chs =
wenzelm@6223
    47
      (case ["/", "\\", "$", ":"] inter_string chs of
wenzelm@6223
    48
        [] => chs
wenzelm@6223
    49
      | bads => err_elem ("Illegal character(s) " ^ commas_quote bads ^ " in") chs);
wenzelm@6223
    50
wenzelm@6223
    51
val basic_elem = Basic o implode o check_elem;
wenzelm@6223
    52
val variable_elem = Variable o implode o check_elem;
wenzelm@6183
    53
wenzelm@6183
    54
fun is_var (Variable _) = true
wenzelm@6183
    55
  | is_var _ = false;
wenzelm@6183
    56
wenzelm@6183
    57
wenzelm@5011
    58
(* type path *)
wenzelm@5011
    59
wenzelm@6183
    60
datatype T = Path of elem list;
wenzelm@6183
    61
wenzelm@6183
    62
fun rep (Path xs) = xs;
wenzelm@5011
    63
wenzelm@6460
    64
fun is_current (Path []) = true
wenzelm@6460
    65
  | is_current _ = false;
wenzelm@6460
    66
wenzelm@5011
    67
val current = Path [];
wenzelm@6183
    68
val root = Path [Root];
wenzelm@6183
    69
val parent = Path [Parent];
wenzelm@6183
    70
fun basic s = Path [basic_elem (explode s)];
wenzelm@6183
    71
fun variable s = Path [variable_elem (explode s)];
wenzelm@5011
    72
wenzelm@6183
    73
fun is_absolute (Path (Root :: _)) = true
wenzelm@6183
    74
  | is_absolute _ = false;
wenzelm@5011
    75
wenzelm@6183
    76
fun is_basic (Path [Basic _]) = true
wenzelm@6183
    77
  | is_basic _ = false;
wenzelm@5011
    78
wenzelm@5011
    79
wenzelm@5011
    80
(* append and norm *)
wenzelm@5011
    81
wenzelm@5011
    82
(*append non-normal path (2n arg) to reversed normal one, result is normal*)
wenzelm@5011
    83
fun rev_app xs [] = rev xs
wenzelm@6183
    84
  | rev_app _ (Root :: ys) = rev_app [Root] ys
wenzelm@6183
    85
  | rev_app (x :: xs) (Parent :: ys) =
wenzelm@6183
    86
      if x = Parent orelse is_var x then rev_app (Parent :: x :: xs) ys
wenzelm@6183
    87
      else if x = Root then rev_app (x :: xs) ys
wenzelm@5011
    88
      else rev_app xs ys
wenzelm@5011
    89
  | rev_app xs (y :: ys) = rev_app (y :: xs) ys;
wenzelm@5011
    90
wenzelm@6183
    91
fun append (Path xs) (Path ys) = Path (rev_app (rev xs) ys);
skalberg@15570
    92
fun appends paths = Library.foldl (uncurry append) (current, paths);
wenzelm@6319
    93
val make = appends o map basic;
wenzelm@5011
    94
fun norm path = rev_app [] path;
wenzelm@5011
    95
wenzelm@6183
    96
wenzelm@21858
    97
(* implode *)
wenzelm@5011
    98
wenzelm@21858
    99
fun implode_elem Root = ""
wenzelm@21858
   100
  | implode_elem Parent = ".."
wenzelm@21858
   101
  | implode_elem (Basic s) = s
wenzelm@21858
   102
  | implode_elem (Variable s) = "$" ^ s;
wenzelm@5011
   103
wenzelm@21858
   104
fun implode_path (Path []) = "."
wenzelm@21858
   105
  | implode_path (Path (Root :: xs)) = "/" ^ space_implode "/" (map implode_elem xs)
wenzelm@21858
   106
  | implode_path (Path xs) = space_implode "/" (map implode_elem xs);
wenzelm@5011
   107
wenzelm@5011
   108
wenzelm@21858
   109
(* explode *)
wenzelm@6183
   110
wenzelm@21858
   111
fun explode_elem "" = Root
wenzelm@21858
   112
  | explode_elem ".." = Parent
wenzelm@21858
   113
  | explode_elem "~" = Variable "HOME"
wenzelm@21858
   114
  | explode_elem "~~" = Variable "ISABELLE_HOME"
wenzelm@21858
   115
  | explode_elem s =
wenzelm@6183
   116
      (case explode s of
wenzelm@6183
   117
        "$" :: cs => variable_elem cs
wenzelm@6183
   118
      | cs => basic_elem cs);
wenzelm@6183
   119
wenzelm@21858
   120
val explode_elems = map explode_elem o filter_out (fn c => c = "" orelse c = ".");
wenzelm@6183
   121
wenzelm@21858
   122
fun explode_path str = Path (norm
wenzelm@6183
   123
  (case space_explode "/" str of
wenzelm@21858
   124
    "" :: ss => Root :: explode_elems ss
wenzelm@21858
   125
  | ss => explode_elems ss));
wenzelm@6183
   126
wenzelm@5011
   127
wenzelm@6183
   128
(* base element *)
wenzelm@6183
   129
wenzelm@7929
   130
fun split_path f (path as Path xs) =
wenzelm@7929
   131
  (case try split_last xs of
skalberg@15531
   132
    SOME (prfx, Basic s) => f (prfx, s)
wenzelm@21858
   133
  | _ => error ("Cannot split path into dir/base: " ^ quote (implode_path path)));
wenzelm@6183
   134
wenzelm@14912
   135
val dir = split_path (fn (prfx, _) => Path prfx);
wenzelm@7929
   136
val base = split_path (fn (_, s) => Path [Basic s]);
wenzelm@6183
   137
wenzelm@7929
   138
fun ext "" path = path
wenzelm@7929
   139
  | ext e path = split_path (fn (prfx, s) => append (Path prfx) (basic (s ^ "." ^ e))) path;
wenzelm@6183
   140
wenzelm@14912
   141
val split_ext = split_path (fn (prfx, s) => apfst (append (Path prfx))
wenzelm@19305
   142
  (case take_suffix (fn c => c <> ".") (explode s) of
wenzelm@14912
   143
    ([], _) => (Path [Basic s], "")
skalberg@15570
   144
  | (cs, e) => (Path [Basic (implode (Library.take (length cs - 1, cs)))], implode e)));
wenzelm@6319
   145
wenzelm@6183
   146
wenzelm@17827
   147
(* expand variables *)
wenzelm@5011
   148
wenzelm@17827
   149
fun eval (Variable s) =
wenzelm@17827
   150
    (case getenv s of
wenzelm@17827
   151
      "" => error ("Undefined Isabelle environment variable: " ^ quote s)
wenzelm@21858
   152
    | path => rep (explode_path path))
wenzelm@17827
   153
  | eval x = [x];
wenzelm@5011
   154
wenzelm@19482
   155
val expand = rep #> maps eval #> norm #> Path;
wenzelm@21858
   156
wenzelm@21858
   157
wenzelm@21858
   158
(*final declarations of this structure!*)
wenzelm@21858
   159
val implode = implode_path;
wenzelm@21858
   160
val explode = explode_path;
wenzelm@5011
   161
wenzelm@5011
   162
end;