src/Pure/General/path.ML
author skalberg
Sun Feb 13 17:15:14 2005 +0100 (2005-02-13)
changeset 15531 08c8dad8e399
parent 14981 e73f8140af78
child 15570 8d8c70b41bab
permissions -rw-r--r--
Deleted Library.option type.
     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 pack: T -> string
    25   val unpack: 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   val position: T -> Position.T
    32 end;
    33 
    34 structure Path: PATH =
    35 struct
    36 
    37 
    38 (* path elements *)
    39 
    40 datatype elem = Root | Parent | Basic of string | Variable of string;
    41 
    42 fun err_elem msg chs = error (msg ^ " path element specification: " ^ quote (implode chs));
    43 
    44 fun check_elem (chs as []) = err_elem "Illegal" chs
    45   | check_elem (chs as ["~"]) = err_elem "Illegal" chs
    46   | check_elem (chs as ["~", "~"]) = err_elem "Illegal" chs
    47   | check_elem chs =
    48       (case ["/", "\\", "$", ":"] inter_string chs of
    49         [] => chs
    50       | bads => err_elem ("Illegal character(s) " ^ commas_quote bads ^ " in") chs);
    51 
    52 val basic_elem = Basic o implode o check_elem;
    53 val variable_elem = Variable o implode o check_elem;
    54 
    55 fun is_var (Variable _) = true
    56   | is_var _ = false;
    57 
    58 
    59 (* type path *)
    60 
    61 datatype T = Path of elem list;
    62 
    63 fun rep (Path xs) = xs;
    64 
    65 fun is_current (Path []) = true
    66   | is_current _ = false;
    67 
    68 val current = Path [];
    69 val root = Path [Root];
    70 val parent = Path [Parent];
    71 fun basic s = Path [basic_elem (explode s)];
    72 fun variable s = Path [variable_elem (explode s)];
    73 
    74 fun is_absolute (Path (Root :: _)) = true
    75   | is_absolute _ = false;
    76 
    77 fun is_basic (Path [Basic _]) = true
    78   | is_basic _ = false;
    79 
    80 
    81 (* append and norm *)
    82 
    83 (*append non-normal path (2n arg) to reversed normal one, result is normal*)
    84 fun rev_app xs [] = rev xs
    85   | rev_app _ (Root :: ys) = rev_app [Root] ys
    86   | rev_app (x :: xs) (Parent :: ys) =
    87       if x = Parent orelse is_var x then rev_app (Parent :: x :: xs) ys
    88       else if x = Root then rev_app (x :: xs) ys
    89       else rev_app xs ys
    90   | rev_app xs (y :: ys) = rev_app (y :: xs) ys;
    91 
    92 fun append (Path xs) (Path ys) = Path (rev_app (rev xs) ys);
    93 fun appends paths = foldl (uncurry append) (current, paths);
    94 val make = appends o map basic;
    95 fun norm path = rev_app [] path;
    96 
    97 
    98 (* pack *)
    99 
   100 fun pack_elem Root = ""
   101   | pack_elem Parent = ".."
   102   | pack_elem (Basic s) = s
   103   | pack_elem (Variable s) = "$" ^ s;
   104 
   105 fun pack (Path []) = "."
   106   | pack (Path (Root :: xs)) = "/" ^ space_implode "/" (map pack_elem xs)
   107   | pack (Path xs) = space_implode "/" (map pack_elem xs);
   108 
   109 
   110 (* unpack *)
   111 
   112 fun unpack_elem "" = Root
   113   | unpack_elem ".." = Parent
   114   | unpack_elem "~" = Variable "HOME"
   115   | unpack_elem "~~" = Variable "ISABELLE_HOME"
   116   | unpack_elem s =
   117       (case explode s of
   118         "$" :: cs => variable_elem cs
   119       | cs => basic_elem cs);
   120 
   121 val unpack_elems = map unpack_elem o filter_out (equal "" orf equal ".");
   122 
   123 fun unpack str = Path (norm
   124   (case space_explode "/" str of
   125     "" :: ss => Root :: unpack_elems ss
   126   | ss => unpack_elems ss));
   127 
   128 
   129 (* base element *)
   130 
   131 fun split_path f (path as Path xs) =
   132   (case try split_last xs of
   133     SOME (prfx, Basic s) => f (prfx, s)
   134   | _ => error ("Cannot split path into dir/base: " ^ quote (pack path)));
   135 
   136 val dir = split_path (fn (prfx, _) => Path prfx);
   137 val base = split_path (fn (_, s) => Path [Basic s]);
   138 
   139 fun ext "" path = path
   140   | ext e path = split_path (fn (prfx, s) => append (Path prfx) (basic (s ^ "." ^ e))) path;
   141 
   142 val split_ext = split_path (fn (prfx, s) => apfst (append (Path prfx))
   143   (case take_suffix (not_equal ".") (explode s) of
   144     ([], _) => (Path [Basic s], "")
   145   | (cs, e) => (Path [Basic (implode (take (length cs - 1, cs)))], implode e)));
   146 
   147 
   148 (* evaluate variables *)
   149 
   150 fun eval env (Variable s) = rep (env s)
   151   | eval _ x = [x];
   152 
   153 fun evaluate env (Path xs) =
   154   Path (norm (flat (map (eval env) xs)));
   155 
   156 val expand = evaluate (unpack o getenv);
   157 
   158 val position = Position.line_name 1 o quote o pack o expand;
   159 
   160 
   161 end;