author | wenzelm |
Mon, 11 Aug 2025 12:34:58 +0200 | |
changeset 82988 | 71ffc2c22348 |
parent 82980 | 839d86abfe86 |
child 82990 | 96010245b731 |
permissions | -rw-r--r-- |
82975 | 1 |
/* Title: Pure/Tools/process_theories.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Process theories within an adhoc session context. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
import java.io.{File => JFile} |
|
11 |
||
12 |
import scala.collection.mutable |
|
82977 | 13 |
import scala.util.matching.Regex |
82975 | 14 |
|
15 |
||
16 |
object Process_Theories { |
|
17 |
/** process theories **/ |
|
18 |
||
19 |
def read_files(path: Path): List[Path] = |
|
20 |
Library.trim_split_lines(File.read(path)).map(Path.explode) |
|
21 |
||
22 |
def process_theories( |
|
23 |
options: Options, |
|
24 |
logic: String, |
|
82980 | 25 |
theories: List[String] = Nil, |
82975 | 26 |
files: List[Path] = Nil, |
27 |
dirs: List[Path] = Nil, |
|
82977 | 28 |
output_messages: Boolean = false, |
82980 | 29 |
message_head: List[Regex] = Nil, |
30 |
message_body: List[Regex] = Nil, |
|
82977 | 31 |
margin: Double = Pretty.default_margin, |
32 |
breakgain: Double = Pretty.default_breakgain, |
|
33 |
metric: Pretty.Metric = Symbol.Metric, |
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
34 |
unicode_symbols: Boolean = false, |
82975 | 35 |
progress: Progress = new Progress |
36 |
): Build.Results = { |
|
37 |
Isabelle_System.with_tmp_dir("private") { private_dir => |
|
38 |
/* options */ |
|
39 |
||
40 |
val build_engine = Build.Engine(Build.engine_name(options)) |
|
41 |
val build_options = build_engine.build_options(options) |
|
42 |
||
43 |
||
44 |
/* session directory and files */ |
|
45 |
||
82977 | 46 |
val private_prefix = private_dir.implode + "/" |
47 |
||
48 |
val session_name = Sessions.DRAFT |
|
49 |
val session_dir = Isabelle_System.make_directory(private_dir + Path.basic(session_name)) |
|
82975 | 50 |
|
51 |
{ |
|
52 |
var seen = Set.empty[JFile] |
|
53 |
for (path0 <- files) { |
|
54 |
val path = path0.canonical |
|
55 |
val file = path.file |
|
56 |
if (!seen(file)) { |
|
57 |
seen += file |
|
58 |
val target = session_dir + path.base |
|
59 |
if (target.is_file) { |
|
60 |
error("Duplicate session source file " + path.base + " --- from " + path) |
|
61 |
} |
|
62 |
Isabelle_System.copy_file(path, target) |
|
63 |
} |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
/* session theories */ |
|
68 |
||
69 |
val more_theories = |
|
70 |
for (path <- files; name <- Thy_Header.get_thy_name(path.implode)) |
|
71 |
yield name |
|
72 |
||
73 |
val session_theories = theories ::: more_theories |
|
74 |
||
75 |
||
76 |
/* session imports */ |
|
77 |
||
78 |
val sessions_structure = Sessions.load_structure(build_options, dirs = dirs) |
|
79 |
||
80 |
val session_imports = |
|
81 |
Set.from( |
|
82 |
for { |
|
83 |
name <- session_theories.iterator |
|
84 |
session = sessions_structure.theory_qualifier(name) |
|
85 |
if session.nonEmpty |
|
86 |
} yield session).toList |
|
87 |
||
88 |
||
89 |
/* build adhoc session */ |
|
90 |
||
91 |
val session_entry = |
|
92 |
Sessions.Session_Entry( |
|
93 |
parent = Some(logic), |
|
94 |
theories = session_theories.map(a => (Nil, List(((a, Position.none), false)))), |
|
95 |
imports = session_imports) |
|
96 |
||
97 |
val session_info = |
|
98 |
Sessions.Info.make(session_entry, draft_session = true, |
|
99 |
dir = session_dir, options = options) |
|
100 |
||
82977 | 101 |
def session_setup(setup_session_name: String, session: Session): Unit = { |
102 |
if (output_messages && setup_session_name == session_name) { |
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
103 |
def recode(s: String): String = Symbol.output(unicode_symbols, s) |
82977 | 104 |
session.all_messages += Session.Consumer[Prover.Message]("process_theories") { |
105 |
case output: Prover.Output |
|
106 |
if Protocol.is_exported(output.message) || Protocol.is_state(output.message) => |
|
107 |
output.properties match { |
|
108 |
case Position.Line_File(line, file0) => |
|
109 |
val file = Library.perhaps_unprefix(private_prefix, file0) |
|
110 |
val pos = Position.Line_File(line, file) |
|
111 |
if (Build.print_log_check(pos, output.message, message_head, message_body)) { |
|
112 |
progress.echo(Protocol.message_text(output.message, heading = true, pos = pos, |
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
113 |
recode = recode, margin = margin, breakgain = breakgain, metric = metric)) |
82977 | 114 |
} |
115 |
case _ => |
|
116 |
} |
|
117 |
case _ => |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
||
82975 | 122 |
Build.build(options, private_dir = Some(private_dir), progress = progress, |
82977 | 123 |
infos = List(session_info), selection = Sessions.Selection.session(session_name), |
124 |
session_setup = session_setup) |
|
82975 | 125 |
} |
126 |
} |
|
127 |
||
128 |
||
129 |
||
130 |
/** Isabelle tool wrapper **/ |
|
131 |
||
132 |
val isabelle_tool = Isabelle_Tool("process_theories", |
|
133 |
"process theories within an adhoc session context", |
|
134 |
Scala_Project.here, |
|
135 |
{ args => |
|
82977 | 136 |
val message_head = new mutable.ListBuffer[Regex] |
137 |
val message_body = new mutable.ListBuffer[Regex] |
|
138 |
var output_messages = false |
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
139 |
var unicode_symbols = false |
82975 | 140 |
val dirs = new mutable.ListBuffer[Path] |
141 |
val files = new mutable.ListBuffer[Path] |
|
142 |
var logic = Isabelle_System.getenv("ISABELLE_LOGIC") |
|
82977 | 143 |
var margin = Pretty.default_margin |
82975 | 144 |
var options = Options.init() |
145 |
var verbose = false |
|
146 |
||
82977 | 147 |
|
82975 | 148 |
val getopts = Getopts(""" |
149 |
Usage: isabelle process_theories [OPTIONS] [THEORIES...] |
|
150 |
||
151 |
Options are: |
|
152 |
-F FILE include addition session files, listed in FILE |
|
82977 | 153 |
-H REGEX filter messages by matching against head |
154 |
-M REGEX filter messages by matching against body |
|
155 |
-O output messages |
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
156 |
-U output Unicode symbols |
82975 | 157 |
-d DIR include session directory |
158 |
-f FILE include addition session files |
|
159 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """) |
|
82977 | 160 |
-m MARGIN margin for pretty printing (default: """ + margin + """) |
82975 | 161 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
162 |
-v verbose |
|
163 |
||
164 |
Process theories within an adhoc session context. |
|
165 |
""", |
|
166 |
"F:" -> (arg => files ++= read_files(Path.explode(arg))), |
|
82977 | 167 |
"H:" -> (arg => message_head += arg.r), |
168 |
"M:" -> (arg => message_body += arg.r), |
|
169 |
"O" -> (_ => output_messages = true), |
|
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
170 |
"U" -> (_ => unicode_symbols = true), |
82975 | 171 |
"d:" -> (arg => dirs += Path.explode(arg)), |
172 |
"f:" -> (arg => files += Path.explode(arg)), |
|
173 |
"l:" -> (arg => logic = arg), |
|
82977 | 174 |
"m:" -> (arg => margin = Value.Double.parse(arg)), |
82975 | 175 |
"o:" -> (arg => options = options + arg), |
176 |
"v" -> (_ => verbose = true)) |
|
177 |
||
178 |
val theories = getopts(args) |
|
179 |
||
180 |
val progress = new Console_Progress(verbose = verbose) |
|
181 |
||
182 |
val results = |
|
183 |
progress.interrupt_handler { |
|
184 |
process_theories(options, logic, theories, files = files.toList, dirs = dirs.toList, |
|
82977 | 185 |
output_messages = output_messages, message_head = message_head.toList, |
82988
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
186 |
message_body = message_body.toList, margin = margin, unicode_symbols = unicode_symbols, |
71ffc2c22348
explicit "isabelle process_theories -U" as in "isabelle build_log";
wenzelm
parents:
82980
diff
changeset
|
187 |
progress = progress) |
82975 | 188 |
} |
189 |
||
190 |
sys.exit(results.rc) |
|
191 |
}) |
|
192 |
} |