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