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