author | wenzelm |
Tue, 16 Aug 2016 15:51:44 +0200 | |
changeset 63706 | 76c2f833abf4 |
parent 63688 | cc57255bf6ae |
child 64143 | 578e71c2c976 |
permissions | -rw-r--r-- |
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/System/isabelle_tool.scala |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
63226 | 3 |
Author: Lars Hupel |
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
4 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
5 |
Isabelle system tools: external executables or internal Scala functions. |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
6 |
*/ |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
7 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
8 |
package isabelle |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
9 |
|
63226 | 10 |
import java.net.URLClassLoader |
11 |
import scala.reflect.runtime.universe |
|
12 |
import scala.tools.reflect.{ToolBox,ToolBoxError} |
|
13 |
||
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
14 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
15 |
object Isabelle_Tool |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
16 |
{ |
63226 | 17 |
/* Scala source tools */ |
18 |
||
19 |
abstract class Body extends Function[List[String], Unit] |
|
20 |
||
21 |
private def compile(path: Path): Body = |
|
22 |
{ |
|
23 |
def err(msg: String): Nothing = |
|
24 |
cat_error(msg, "The error(s) above occurred in Isabelle/Scala tool " + path) |
|
25 |
||
26 |
val source = File.read(path) |
|
27 |
||
28 |
val class_loader = new URLClassLoader(Array(), getClass.getClassLoader) |
|
29 |
val tool_box = universe.runtimeMirror(class_loader).mkToolBox() |
|
30 |
||
31 |
try { |
|
32 |
val symbol = tool_box.parse(source) match { |
|
33 |
case tree: universe.ModuleDef => tool_box.define(tree) |
|
34 |
case _ => err("Source does not describe a module (Scala object)") |
|
35 |
} |
|
36 |
tool_box.compile(universe.Ident(symbol))() match { |
|
37 |
case body: Body => body |
|
38 |
case _ => err("Ill-typed source: Isabelle_Tool.Body expected") |
|
39 |
} |
|
40 |
} |
|
63519
78401d628718
more precise error information for dynamic Scala tools
Lars Hupel <lars.hupel@mytum.de>
parents:
63226
diff
changeset
|
41 |
catch { |
78401d628718
more precise error information for dynamic Scala tools
Lars Hupel <lars.hupel@mytum.de>
parents:
63226
diff
changeset
|
42 |
case e: ToolBoxError => |
78401d628718
more precise error information for dynamic Scala tools
Lars Hupel <lars.hupel@mytum.de>
parents:
63226
diff
changeset
|
43 |
if (tool_box.frontEnd.hasErrors) { |
78401d628718
more precise error information for dynamic Scala tools
Lars Hupel <lars.hupel@mytum.de>
parents:
63226
diff
changeset
|
44 |
val infos = tool_box.frontEnd.infos.toList |
78401d628718
more precise error information for dynamic Scala tools
Lars Hupel <lars.hupel@mytum.de>
parents:
63226
diff
changeset
|
45 |
val msgs = infos.map(info => "Error in line " + info.pos.line + ":\n" + info.msg) |
78401d628718
more precise error information for dynamic Scala tools
Lars Hupel <lars.hupel@mytum.de>
parents:
63226
diff
changeset
|
46 |
err(msgs.mkString("\n")) |
78401d628718
more precise error information for dynamic Scala tools
Lars Hupel <lars.hupel@mytum.de>
parents:
63226
diff
changeset
|
47 |
} |
78401d628718
more precise error information for dynamic Scala tools
Lars Hupel <lars.hupel@mytum.de>
parents:
63226
diff
changeset
|
48 |
else |
78401d628718
more precise error information for dynamic Scala tools
Lars Hupel <lars.hupel@mytum.de>
parents:
63226
diff
changeset
|
49 |
err(e.toString) |
78401d628718
more precise error information for dynamic Scala tools
Lars Hupel <lars.hupel@mytum.de>
parents:
63226
diff
changeset
|
50 |
} |
63226 | 51 |
} |
52 |
||
53 |
||
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
54 |
/* external tools */ |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
55 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
56 |
private def dirs(): List[Path] = Path.split(Isabelle_System.getenv_strict("ISABELLE_TOOLS")) |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
57 |
|
63226 | 58 |
private def is_external(dir: Path, file_name: String): Boolean = |
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
59 |
{ |
63226 | 60 |
val file = (dir + Path.basic(file_name)).file |
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
61 |
try { |
63226 | 62 |
file.isFile && file.canRead && |
63 |
(file_name.endsWith(".scala") || file.canExecute) && |
|
64 |
!file_name.endsWith("~") && !file_name.endsWith(".orig") |
|
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
65 |
} |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
66 |
catch { case _: SecurityException => false } |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
67 |
} |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
68 |
|
62830 | 69 |
private def list_external(): List[(String, String)] = |
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
70 |
{ |
62830 | 71 |
val Description = """.*\bDESCRIPTION: *(.*)""".r |
72 |
for { |
|
73 |
dir <- dirs() if dir.is_dir |
|
63226 | 74 |
file_name <- File.read_dir(dir) if is_external(dir, file_name) |
62830 | 75 |
} yield { |
63226 | 76 |
val source = File.read(dir + Path.basic(file_name)) |
77 |
val name = Library.try_unsuffix(".scala", file_name) getOrElse file_name |
|
62830 | 78 |
val description = |
79 |
split_lines(source).collectFirst({ case Description(s) => s }) getOrElse "" |
|
80 |
(name, description) |
|
81 |
} |
|
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
82 |
} |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
83 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
84 |
private def find_external(name: String): Option[List[String] => Unit] = |
63226 | 85 |
dirs.collectFirst({ |
86 |
case dir if is_external(dir, name + ".scala") => |
|
87 |
compile(dir + Path.basic(name + ".scala")) |
|
88 |
case dir if is_external(dir, name) => |
|
89 |
(args: List[String]) => |
|
90 |
{ |
|
91 |
val tool = dir + Path.basic(name) |
|
92 |
val result = Isabelle_System.bash(File.bash_path(tool) + " " + File.bash_args(args)) |
|
93 |
sys.exit(result.print_stdout.rc) |
|
94 |
} |
|
62830 | 95 |
}) |
96 |
||
97 |
||
98 |
/* internal tools */ |
|
99 |
||
62960
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
100 |
private val internal_tools: List[Isabelle_Tool] = |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
101 |
List( |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
102 |
Build.isabelle_tool, |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
103 |
Build_Doc.isabelle_tool, |
63688
cc57255bf6ae
gnuplot presentation similar to former isatest-statistics;
wenzelm
parents:
63519
diff
changeset
|
104 |
Build_Stats.isabelle_tool, |
62960
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
105 |
Check_Sources.isabelle_tool, |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
106 |
Doc.isabelle_tool, |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
107 |
ML_Process.isabelle_tool, |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
108 |
Options.isabelle_tool, |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
109 |
Update_Cartouches.isabelle_tool, |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
110 |
Update_Header.isabelle_tool, |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
111 |
Update_Then.isabelle_tool, |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
112 |
Update_Theorems.isabelle_tool) |
62830 | 113 |
|
114 |
private def list_internal(): List[(String, String)] = |
|
62960
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
115 |
for (tool <- internal_tools.toList) yield (tool.name, tool.description) |
62830 | 116 |
|
117 |
private def find_internal(name: String): Option[List[String] => Unit] = |
|
62960
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
118 |
internal_tools.collectFirst({ |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
119 |
case tool if tool.name == name => |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
120 |
args => Command_Line.tool0 { tool.body(args) } |
cfbb6a5b427c
simplified -- avoid odd mutable state, which potentially causes problems with module initialization;
wenzelm
parents:
62838
diff
changeset
|
121 |
}) |
62831 | 122 |
|
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
123 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
124 |
/* command line entry point */ |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
125 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
126 |
def main(args: Array[String]) |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
127 |
{ |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
128 |
Command_Line.tool0 { |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
129 |
args.toList match { |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
130 |
case Nil | List("-?") => |
62830 | 131 |
val tool_descriptions = |
132 |
(list_external() ::: list_internal()).sortBy(_._1). |
|
133 |
map({ case (a, "") => a case (a, b) => a + " - " + b }) |
|
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
134 |
Getopts(""" |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
135 |
Usage: isabelle TOOL [ARGS ...] |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
136 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
137 |
Start Isabelle TOOL with ARGS; pass "-?" for tool-specific help. |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
138 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
139 |
Available tools:""" + tool_descriptions.mkString("\n ", "\n ", "\n")).usage |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
140 |
case tool_name :: tool_args => |
62830 | 141 |
find_external(tool_name) orElse find_internal(tool_name) match { |
142 |
case Some(tool) => tool(tool_args) |
|
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
143 |
case None => error("Unknown Isabelle tool: " + quote(tool_name)) |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
144 |
} |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
145 |
} |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
146 |
} |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
147 |
} |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
diff
changeset
|
148 |
} |
62830 | 149 |
|
150 |
sealed case class Isabelle_Tool(name: String, description: String, body: List[String] => Unit) |