src/Pure/General/path.ML
author wenzelm
Wed Apr 21 16:30:35 1999 +0200 (1999-04-21)
changeset 6460 cb8c85435228
parent 6319 80173cb8691c
child 7714 e6aa4fca983e
permissions -rw-r--r--
added is_current;
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@5011
    24
  val pack: T -> string
wenzelm@5011
    25
  val unpack: string -> T
wenzelm@6183
    26
  val base: T -> T
wenzelm@6183
    27
  val ext: string -> T -> T
wenzelm@6319
    28
  val dir: T -> T
wenzelm@6183
    29
  val expand: T -> T
wenzelm@5011
    30
end;
wenzelm@5011
    31
wenzelm@6187
    32
structure Path: PATH =
wenzelm@5011
    33
struct
wenzelm@5011
    34
wenzelm@6183
    35
wenzelm@6183
    36
(* path elements *)
wenzelm@6183
    37
wenzelm@6183
    38
datatype elem = Root | Parent | Basic of string | Variable of string;
wenzelm@6183
    39
wenzelm@6223
    40
fun err_elem msg chs = error (msg ^ " path element specification: " ^ quote (implode chs));
wenzelm@6183
    41
wenzelm@6319
    42
fun check_elem (chs as []) = err_elem "Illegal" chs
wenzelm@6319
    43
  | check_elem (chs as ["~"]) = err_elem "Illegal" chs
wenzelm@6223
    44
  | check_elem (chs as ["~", "~"]) = err_elem "Illegal" chs
wenzelm@6223
    45
  | check_elem chs =
wenzelm@6223
    46
      (case ["/", "\\", "$", ":"] inter_string chs of
wenzelm@6223
    47
        [] => chs
wenzelm@6223
    48
      | bads => err_elem ("Illegal character(s) " ^ commas_quote bads ^ " in") chs);
wenzelm@6223
    49
wenzelm@6223
    50
val basic_elem = Basic o implode o check_elem;
wenzelm@6223
    51
val variable_elem = Variable o implode o check_elem;
wenzelm@6183
    52
wenzelm@6183
    53
fun is_var (Variable _) = true
wenzelm@6183
    54
  | is_var _ = false;
wenzelm@6183
    55
wenzelm@6183
    56
wenzelm@5011
    57
(* type path *)
wenzelm@5011
    58
wenzelm@6183
    59
datatype T = Path of elem list;
wenzelm@6183
    60
wenzelm@6183
    61
fun rep (Path xs) = xs;
wenzelm@5011
    62
wenzelm@6460
    63
fun is_current (Path []) = true
wenzelm@6460
    64
  | is_current _ = false;
wenzelm@6460
    65
wenzelm@5011
    66
val current = Path [];
wenzelm@6183
    67
val root = Path [Root];
wenzelm@6183
    68
val parent = Path [Parent];
wenzelm@6183
    69
fun basic s = Path [basic_elem (explode s)];
wenzelm@6183
    70
fun variable s = Path [variable_elem (explode s)];
wenzelm@5011
    71
wenzelm@6183
    72
fun is_absolute (Path (Root :: _)) = true
wenzelm@6183
    73
  | is_absolute _ = false;
wenzelm@5011
    74
wenzelm@6183
    75
fun is_basic (Path [Basic _]) = true
wenzelm@6183
    76
  | is_basic _ = false;
wenzelm@5011
    77
wenzelm@5011
    78
wenzelm@5011
    79
(* append and norm *)
wenzelm@5011
    80
wenzelm@5011
    81
(*append non-normal path (2n arg) to reversed normal one, result is normal*)
wenzelm@5011
    82
fun rev_app xs [] = rev xs
wenzelm@6183
    83
  | rev_app _ (Root :: ys) = rev_app [Root] ys
wenzelm@6183
    84
  | rev_app (x :: xs) (Parent :: ys) =
wenzelm@6183
    85
      if x = Parent orelse is_var x then rev_app (Parent :: x :: xs) ys
wenzelm@6183
    86
      else if x = Root then rev_app (x :: xs) ys
wenzelm@5011
    87
      else rev_app xs ys
wenzelm@5011
    88
  | rev_app xs (y :: ys) = rev_app (y :: xs) ys;
wenzelm@5011
    89
wenzelm@6183
    90
fun append (Path xs) (Path ys) = Path (rev_app (rev xs) ys);
wenzelm@6270
    91
fun appends paths = foldl (uncurry append) (current, paths);
wenzelm@6319
    92
val make = appends o map basic;
wenzelm@5011
    93
fun norm path = rev_app [] path;
wenzelm@5011
    94
wenzelm@6183
    95
wenzelm@6183
    96
(* pack *)
wenzelm@5011
    97
wenzelm@6183
    98
fun pack_elem Root = ""
wenzelm@6183
    99
  | pack_elem Parent = ".."
wenzelm@6183
   100
  | pack_elem (Basic s) = s
wenzelm@6183
   101
  | pack_elem (Variable s) = "$" ^ s;
wenzelm@5011
   102
wenzelm@5011
   103
fun pack (Path []) = "."
wenzelm@6183
   104
  | pack (Path (Root :: xs)) = "/" ^ space_implode "/" (map pack_elem xs)
wenzelm@6183
   105
  | pack (Path xs) = space_implode "/" (map pack_elem xs);
wenzelm@5011
   106
wenzelm@5011
   107
wenzelm@6183
   108
(* unpack *)
wenzelm@6183
   109
wenzelm@6183
   110
fun unpack_elem "" = Root
wenzelm@6183
   111
  | unpack_elem ".." = Parent
wenzelm@6183
   112
  | unpack_elem "~" = Variable "HOME"
wenzelm@6183
   113
  | unpack_elem "~~" = Variable "ISABELLE_HOME"
wenzelm@6183
   114
  | unpack_elem s =
wenzelm@6183
   115
      (case explode s of
wenzelm@6183
   116
        "$" :: cs => variable_elem cs
wenzelm@6183
   117
      | cs => basic_elem cs);
wenzelm@6183
   118
wenzelm@6183
   119
val unpack_elems = map unpack_elem o filter_out (equal "" orf equal ".");
wenzelm@6183
   120
wenzelm@6183
   121
fun unpack str = Path (norm
wenzelm@6183
   122
  (case space_explode "/" str of
wenzelm@6183
   123
    "" :: ss => Root :: unpack_elems ss
wenzelm@6183
   124
  | ss => unpack_elems ss));
wenzelm@6183
   125
wenzelm@5011
   126
wenzelm@6183
   127
(* base element *)
wenzelm@6183
   128
wenzelm@6183
   129
fun err_no_base path =
wenzelm@6183
   130
  error ("No base path element in " ^ quote (pack path));
wenzelm@6183
   131
wenzelm@6183
   132
fun base (path as Path xs) =
wenzelm@6183
   133
  (case try split_last xs of
wenzelm@6183
   134
    Some (_, x as Basic _) => Path [x]
wenzelm@6183
   135
  | _ => err_no_base path);
wenzelm@6183
   136
wenzelm@6183
   137
fun ext e (path as Path xs) =
wenzelm@6183
   138
  (case try split_last xs of
wenzelm@6183
   139
    Some (prfx, Basic s) => if e = "" then path else Path (prfx @ [Basic (s ^ "." ^ e)])
wenzelm@6183
   140
  | _ => err_no_base path);
wenzelm@6183
   141
wenzelm@6319
   142
fun dir (path as Path xs) =
wenzelm@6319
   143
  (case try split_last xs of
wenzelm@6319
   144
    Some (prfx, _) => Path prfx
wenzelm@6319
   145
  | _ => error ("Cannot split path " ^ quote (pack path)));
wenzelm@6319
   146
wenzelm@6183
   147
wenzelm@6183
   148
(* evaluate variables *)
wenzelm@6183
   149
wenzelm@6183
   150
fun eval env (Variable s) = rep (env s)
wenzelm@6183
   151
  | eval _ x = [x];
wenzelm@5011
   152
wenzelm@5011
   153
fun evaluate env (Path xs) =
wenzelm@5011
   154
  Path (norm (flat (map (eval env) xs)));
wenzelm@5011
   155
wenzelm@6183
   156
val expand = evaluate (unpack o getenv);
wenzelm@5011
   157
wenzelm@5011
   158
wenzelm@5011
   159
end;