58610
|
1 |
/* Title: Pure/Tools/update_cartouches.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Update theory syntax to use cartouches.
|
|
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 |
|
|
40 |
def update_cartouches(replace_text: Boolean, path: Path)
|
58610
|
41 |
{
|
|
42 |
val text0 = File.read(path)
|
61492
|
43 |
|
|
44 |
// outer syntax cartouches
|
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 |
}
|
|
55 |
else tok.source
|
|
56 |
}
|
|
57 |
).mkString
|
|
58 |
|
61492
|
59 |
// cartouches within presumed text tokens
|
|
60 |
val text2 =
|
|
61 |
if (replace_text) {
|
|
62 |
(for (tok <- Token.explode(Keyword.Keywords.empty, text1).iterator)
|
|
63 |
yield {
|
|
64 |
if (tok.kind == Token.Kind.STRING || tok.kind == Token.Kind.CARTOUCHE) {
|
|
65 |
val content = tok.content
|
|
66 |
val content1 = update_text(content)
|
|
67 |
|
|
68 |
if (content == content1) tok.source
|
|
69 |
else if (tok.kind == Token.Kind.STRING) Outer_Syntax.quote_string(content1)
|
|
70 |
else cartouche(content1)
|
|
71 |
}
|
|
72 |
else tok.source
|
|
73 |
}).mkString
|
|
74 |
}
|
|
75 |
else text1
|
|
76 |
|
|
77 |
if (text0 != text2) {
|
58610
|
78 |
Output.writeln("changing " + path)
|
61492
|
79 |
File.write_backup2(path, text2)
|
58610
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
|
|
84 |
/* command line entry point */
|
|
85 |
|
|
86 |
def main(args: Array[String])
|
|
87 |
{
|
|
88 |
Command_Line.tool0 {
|
61492
|
89 |
args.toList match {
|
|
90 |
case Properties.Value.Boolean(replace_text) :: files =>
|
|
91 |
files.foreach(file => update_cartouches(replace_text, Path.explode(file)))
|
|
92 |
case _ => error("Bad arguments:\n" + cat_lines(args))
|
|
93 |
}
|
58610
|
94 |
}
|
|
95 |
}
|
|
96 |
}
|