58610
|
1 |
/* Title: Pure/Tools/update_cartouches.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
61579
|
4 |
Update theory syntax to use cartouches etc.
|
58610
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
object Update_Cartouches
|
|
11 |
{
|
|
12 |
/* update cartouches */
|
|
13 |
|
|
14 |
private def cartouche(s: String): String =
|
|
15 |
Symbol.open + s + Symbol.close
|
|
16 |
|
|
17 |
private val Verbatim_Body = """(?s)[ \t]*(.*?)[ \t]*""".r
|
|
18 |
|
61492
|
19 |
val Text_Antiq = """(?s)@\{\s*text\b\s*(.+)\}""".r
|
|
20 |
|
|
21 |
def update_text(content: String): String =
|
|
22 |
{
|
|
23 |
(try { Some(Antiquote.read(content)) } catch { case ERROR(_) => None }) match {
|
|
24 |
case Some(ants) =>
|
|
25 |
val ants1: List[Antiquote.Antiquote] =
|
|
26 |
ants.map(ant =>
|
|
27 |
ant match {
|
|
28 |
case Antiquote.Antiq(Text_Antiq(body)) =>
|
|
29 |
Token.explode(Keyword.Keywords.empty, body).filterNot(_.is_space) match {
|
|
30 |
case List(tok) => Antiquote.Control(cartouche(tok.content))
|
|
31 |
case _ => ant
|
|
32 |
}
|
|
33 |
case _ => ant
|
|
34 |
})
|
|
35 |
if (ants == ants1) content else ants1.map(_.source).mkString
|
|
36 |
case None => content
|
|
37 |
}
|
|
38 |
}
|
|
39 |
|
61579
|
40 |
def update_cartouches(replace_comment: Boolean, replace_text: Boolean, path: Path)
|
58610
|
41 |
{
|
|
42 |
val text0 = File.read(path)
|
61492
|
43 |
|
61579
|
44 |
// outer syntax cartouches and comment markers
|
58610
|
45 |
val text1 =
|
59083
|
46 |
(for (tok <- Token.explode(Keyword.Keywords.empty, text0).iterator)
|
58610
|
47 |
yield {
|
|
48 |
if (tok.kind == Token.Kind.ALT_STRING) cartouche(tok.content)
|
|
49 |
else if (tok.kind == Token.Kind.VERBATIM) {
|
|
50 |
tok.content match {
|
|
51 |
case Verbatim_Body(s) => cartouche(s)
|
|
52 |
case s => tok.source
|
|
53 |
}
|
|
54 |
}
|
61579
|
55 |
else if (replace_comment && tok.source == "--") Symbol.comment
|
58610
|
56 |
else tok.source
|
|
57 |
}
|
|
58 |
).mkString
|
|
59 |
|
61492
|
60 |
// cartouches within presumed text tokens
|
|
61 |
val text2 =
|
|
62 |
if (replace_text) {
|
|
63 |
(for (tok <- Token.explode(Keyword.Keywords.empty, text1).iterator)
|
|
64 |
yield {
|
|
65 |
if (tok.kind == Token.Kind.STRING || tok.kind == Token.Kind.CARTOUCHE) {
|
|
66 |
val content = tok.content
|
|
67 |
val content1 = update_text(content)
|
|
68 |
|
|
69 |
if (content == content1) tok.source
|
|
70 |
else if (tok.kind == Token.Kind.STRING) Outer_Syntax.quote_string(content1)
|
|
71 |
else cartouche(content1)
|
|
72 |
}
|
|
73 |
else tok.source
|
|
74 |
}).mkString
|
|
75 |
}
|
|
76 |
else text1
|
|
77 |
|
|
78 |
if (text0 != text2) {
|
58610
|
79 |
Output.writeln("changing " + path)
|
61492
|
80 |
File.write_backup2(path, text2)
|
58610
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
|
62836
|
85 |
/* Isabelle tool wrapper */
|
58610
|
86 |
|
62836
|
87 |
val isabelle_tool =
|
|
88 |
Isabelle_Tool("update_cartouches", "update theory syntax to use cartouches", args =>
|
|
89 |
{
|
62445
|
90 |
var replace_comment = false
|
|
91 |
var replace_text = false
|
|
92 |
|
62454
|
93 |
val getopts = Getopts("""
|
62445
|
94 |
Usage: isabelle update_cartouches [FILES|DIRS...]
|
|
95 |
|
|
96 |
Options are:
|
|
97 |
-c replace comment marker "--" by symbol "\<comment>"
|
|
98 |
-t replace @{text} antiquotations within text tokens
|
|
99 |
|
|
100 |
Recursively find .thy files and update theory syntax to use cartouches
|
|
101 |
instead of old-style {* verbatim *} or `alt_string` tokens.
|
|
102 |
|
|
103 |
Old versions of files are preserved by appending "~~".
|
|
104 |
""",
|
|
105 |
"c" -> (_ => replace_comment = true),
|
|
106 |
"t" -> (_ => replace_text = true))
|
|
107 |
|
|
108 |
val specs = getopts(args)
|
|
109 |
if (specs.isEmpty) getopts.usage()
|
|
110 |
|
|
111 |
for {
|
|
112 |
spec <- specs
|
|
113 |
file <- File.find_files(Path.explode(spec).file, file => file.getName.endsWith(".thy"))
|
|
114 |
} update_cartouches(replace_comment, replace_text, Path.explode(File.standard_path(file)))
|
62836
|
115 |
})
|
58610
|
116 |
}
|