src/Pure/General/path.ML
author wenzelm
Tue Oct 05 15:26:10 1999 +0200 (1999-10-05)
changeset 7714 e6aa4fca983e
parent 6460 cb8c85435228
child 7929 2010ae0393ca
permissions -rw-r--r--
added position;
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@7714
    30
  val position: T -> Position.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);
wenzelm@6270
    92
fun appends paths = 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@6183
    97
(* pack *)
wenzelm@5011
    98
wenzelm@6183
    99
fun pack_elem Root = ""
wenzelm@6183
   100
  | pack_elem Parent = ".."
wenzelm@6183
   101
  | pack_elem (Basic s) = s
wenzelm@6183
   102
  | pack_elem (Variable s) = "$" ^ s;
wenzelm@5011
   103
wenzelm@5011
   104
fun pack (Path []) = "."
wenzelm@6183
   105
  | pack (Path (Root :: xs)) = "/" ^ space_implode "/" (map pack_elem xs)
wenzelm@6183
   106
  | pack (Path xs) = space_implode "/" (map pack_elem xs);
wenzelm@5011
   107
wenzelm@5011
   108
wenzelm@6183
   109
(* unpack *)
wenzelm@6183
   110
wenzelm@6183
   111
fun unpack_elem "" = Root
wenzelm@6183
   112
  | unpack_elem ".." = Parent
wenzelm@6183
   113
  | unpack_elem "~" = Variable "HOME"
wenzelm@6183
   114
  | unpack_elem "~~" = Variable "ISABELLE_HOME"
wenzelm@6183
   115
  | unpack_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@6183
   120
val unpack_elems = map unpack_elem o filter_out (equal "" orf equal ".");
wenzelm@6183
   121
wenzelm@6183
   122
fun unpack str = Path (norm
wenzelm@6183
   123
  (case space_explode "/" str of
wenzelm@6183
   124
    "" :: ss => Root :: unpack_elems ss
wenzelm@6183
   125
  | ss => unpack_elems ss));
wenzelm@6183
   126
wenzelm@5011
   127
wenzelm@6183
   128
(* base element *)
wenzelm@6183
   129
wenzelm@6183
   130
fun err_no_base path =
wenzelm@6183
   131
  error ("No base path element in " ^ quote (pack path));
wenzelm@6183
   132
wenzelm@6183
   133
fun base (path as Path xs) =
wenzelm@6183
   134
  (case try split_last xs of
wenzelm@6183
   135
    Some (_, x as Basic _) => Path [x]
wenzelm@6183
   136
  | _ => err_no_base path);
wenzelm@6183
   137
wenzelm@6183
   138
fun ext e (path as Path xs) =
wenzelm@6183
   139
  (case try split_last xs of
wenzelm@6183
   140
    Some (prfx, Basic s) => if e = "" then path else Path (prfx @ [Basic (s ^ "." ^ e)])
wenzelm@6183
   141
  | _ => err_no_base path);
wenzelm@6183
   142
wenzelm@6319
   143
fun dir (path as Path xs) =
wenzelm@6319
   144
  (case try split_last xs of
wenzelm@6319
   145
    Some (prfx, _) => Path prfx
wenzelm@6319
   146
  | _ => error ("Cannot split path " ^ quote (pack path)));
wenzelm@6319
   147
wenzelm@6183
   148
wenzelm@6183
   149
(* evaluate variables *)
wenzelm@6183
   150
wenzelm@6183
   151
fun eval env (Variable s) = rep (env s)
wenzelm@6183
   152
  | eval _ x = [x];
wenzelm@5011
   153
wenzelm@5011
   154
fun evaluate env (Path xs) =
wenzelm@5011
   155
  Path (norm (flat (map (eval env) xs)));
wenzelm@5011
   156
wenzelm@6183
   157
val expand = evaluate (unpack o getenv);
wenzelm@5011
   158
wenzelm@7714
   159
val position = Position.line_name 1 o quote o pack o expand;
wenzelm@7714
   160
wenzelm@5011
   161
wenzelm@5011
   162
end;