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