43600
|
1 |
/* Title: Pure/General/path.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
43601
|
4 |
Algebra of file-system paths: basic POSIX notation, extended by named
|
|
5 |
roots (e.g. //foo) and variables (e.g. $BAR).
|
43600
|
6 |
*/
|
|
7 |
|
|
8 |
package isabelle
|
|
9 |
|
|
10 |
|
|
11 |
object Path
|
|
12 |
{
|
|
13 |
/* path elements */
|
|
14 |
|
|
15 |
private sealed abstract class Elem
|
|
16 |
private case class Root(val name: String) extends Elem
|
|
17 |
private case class Basic(val name: String) extends Elem
|
|
18 |
private case class Variable(val name: String) extends Elem
|
|
19 |
private case object Parent extends Elem
|
|
20 |
|
|
21 |
private def err_elem(msg: String, s: String): Nothing =
|
43652
|
22 |
error (msg + " path element specification: " + quote(s))
|
43600
|
23 |
|
|
24 |
private def check_elem(s: String): String =
|
|
25 |
if (s == "" || s == "~" || s == "~~") err_elem("Illegal", s)
|
|
26 |
else
|
|
27 |
s.iterator.filter(c => c == '/' || c == '\\' || c == '$' || c == ':').toList match {
|
|
28 |
case Nil => s
|
|
29 |
case bads =>
|
43652
|
30 |
err_elem ("Illegal character(s) " + commas_quote(bads.map(_.toString)) + " in", s)
|
43600
|
31 |
}
|
|
32 |
|
|
33 |
private def root_elem(s: String): Elem = Root(check_elem(s))
|
|
34 |
private def basic_elem(s: String): Elem = Basic(check_elem(s))
|
|
35 |
private def variable_elem(s: String): Elem = Variable(check_elem(s))
|
|
36 |
|
|
37 |
private def apply_elem(y: Elem, xs: List[Elem]): List[Elem] =
|
|
38 |
(y, xs) match {
|
|
39 |
case (Root(_), _) => List(y)
|
|
40 |
case (Parent, Root(_) :: _) => xs
|
|
41 |
case (Parent, Basic(_) :: rest) => rest
|
|
42 |
case _ => y :: xs
|
|
43 |
}
|
|
44 |
|
|
45 |
private def norm_elems(elems: List[Elem]): List[Elem] =
|
|
46 |
(elems :\ (Nil: List[Elem]))(apply_elem)
|
|
47 |
|
|
48 |
private def implode_elem(elem: Elem): String =
|
|
49 |
elem match {
|
|
50 |
case Root("") => ""
|
|
51 |
case Root(s) => "//" + s
|
|
52 |
case Basic(s) => s
|
|
53 |
case Variable(s) => "$" + s
|
|
54 |
case Parent => ".."
|
|
55 |
}
|
|
56 |
|
|
57 |
|
|
58 |
/* path constructors */
|
|
59 |
|
|
60 |
private def apply(xs: List[Elem]): Path =
|
|
61 |
new Path { override val elems = xs }
|
|
62 |
|
|
63 |
val current: Path = Path(Nil)
|
|
64 |
val root: Path = Path(List(Root("")))
|
|
65 |
def named_root(s: String): Path = Path(List(root_elem(s)))
|
|
66 |
def basic(s: String): Path = Path(List(basic_elem(s)))
|
|
67 |
def variable(s: String): Path = Path(List(variable_elem(s)))
|
|
68 |
val parent: Path = Path(List(Parent))
|
|
69 |
|
|
70 |
|
|
71 |
/* explode */
|
|
72 |
|
|
73 |
private def explode_elem(s: String): Elem =
|
|
74 |
if (s == "..") Parent
|
|
75 |
else if (s == "~") Variable("HOME")
|
|
76 |
else if (s == "~~") Variable("ISABELLE_HOME")
|
|
77 |
else if (s.startsWith("$")) variable_elem(s.substring(1))
|
|
78 |
else basic_elem(s)
|
|
79 |
|
|
80 |
private def explode_elems(ss: List[String]): List[Elem] =
|
|
81 |
ss.filterNot(s => s.isEmpty || s == ".").map(explode_elem).reverse
|
|
82 |
|
|
83 |
def explode(str: String): Path =
|
|
84 |
{
|
|
85 |
val ss = Library.space_explode('/', str)
|
|
86 |
val r = ss.takeWhile(_.isEmpty).length
|
|
87 |
val es = ss.dropWhile(_.isEmpty)
|
|
88 |
val (roots, raw_elems) =
|
|
89 |
if (r == 0) (Nil, es)
|
|
90 |
else if (r == 1) (List(Root("")), es)
|
|
91 |
else if (es.isEmpty) (List(Root("")), Nil)
|
|
92 |
else (List(root_elem(es.head)), es.tail)
|
|
93 |
Path(norm_elems(explode_elems(raw_elems) ++ roots))
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
class Path
|
|
98 |
{
|
|
99 |
protected val elems: List[Path.Elem] = Nil // reversed elements
|
|
100 |
|
|
101 |
def is_current: Boolean = elems.isEmpty
|
|
102 |
def is_absolute: Boolean = !elems.isEmpty && elems.last.isInstanceOf[Path.Root]
|
|
103 |
def is_basic: Boolean = elems match { case List(Path.Basic(_)) => true case _ => false }
|
|
104 |
|
43605
|
105 |
def +(other: Path): Path = Path((other.elems :\ elems)(Path.apply_elem))
|
43600
|
106 |
|
|
107 |
|
43604
|
108 |
/* implode */
|
43600
|
109 |
|
43604
|
110 |
def implode: String =
|
43600
|
111 |
elems match {
|
|
112 |
case Nil => "."
|
|
113 |
case List(Path.Root("")) => "/"
|
43604
|
114 |
case _ => elems.map(Path.implode_elem).reverse.mkString("/")
|
43600
|
115 |
}
|
|
116 |
|
43652
|
117 |
override def toString: String = quote(implode)
|
43600
|
118 |
|
|
119 |
|
|
120 |
/* base element */
|
|
121 |
|
|
122 |
private def split_path: (Path, String) =
|
|
123 |
elems match {
|
|
124 |
case Path.Basic(s) :: xs => (Path(xs), s)
|
43604
|
125 |
case _ => error("Cannot split path into dir/base: " + toString)
|
43600
|
126 |
}
|
|
127 |
|
|
128 |
def dir: Path = split_path._1
|
|
129 |
def base: Path = Path(List(Path.Basic(split_path._2)))
|
|
130 |
|
|
131 |
def ext(e: String): Path =
|
|
132 |
if (e == "") this
|
|
133 |
else {
|
|
134 |
val (prfx, s) = split_path
|
43604
|
135 |
prfx + Path.basic(s + "." + e)
|
43600
|
136 |
}
|
43604
|
137 |
|
|
138 |
|
|
139 |
/* expand */
|
|
140 |
|
43664
|
141 |
def expand: Path =
|
43604
|
142 |
{
|
|
143 |
def eval(elem: Path.Elem): List[Path.Elem] =
|
|
144 |
elem match {
|
43664
|
145 |
case Path.Variable(s) =>
|
|
146 |
Path.explode(Isabelle_System.getenv_strict(s)).elems
|
43604
|
147 |
case x => List(x)
|
|
148 |
}
|
|
149 |
|
|
150 |
Path(Path.norm_elems(elems.map(eval).flatten))
|
|
151 |
}
|
43600
|
152 |
}
|