author | wenzelm |
Sat, 26 May 2018 21:23:51 +0200 | |
changeset 68293 | 2bc4e5d9cca6 |
parent 67442 | f075640b8868 |
child 68994 | d961e11e0e87 |
permissions | -rw-r--r-- |
67433
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/Tools/update_comments.scala |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
3 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
4 |
Update formal comments in outer syntax: \<comment> \<open>...\<close> |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
6 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
8 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
9 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
10 |
import scala.annotation.tailrec |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
11 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
12 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
13 |
object Update_Comments |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
14 |
{ |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
15 |
def update_comments(path: Path) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
16 |
{ |
67442
f075640b8868
uniform treatment of old-style and new-style comments;
wenzelm
parents:
67436
diff
changeset
|
17 |
def make_comment(tok: Token): String = |
f075640b8868
uniform treatment of old-style and new-style comments;
wenzelm
parents:
67436
diff
changeset
|
18 |
Symbol.comment + Symbol.space + Symbol.cartouche(Symbol.trim_blanks(tok.content)) |
f075640b8868
uniform treatment of old-style and new-style comments;
wenzelm
parents:
67436
diff
changeset
|
19 |
|
67433
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
20 |
@tailrec def update(toks: List[Token], result: List[String]): String = |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
21 |
{ |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
22 |
toks match { |
67442
f075640b8868
uniform treatment of old-style and new-style comments;
wenzelm
parents:
67436
diff
changeset
|
23 |
case tok :: rest |
f075640b8868
uniform treatment of old-style and new-style comments;
wenzelm
parents:
67436
diff
changeset
|
24 |
if tok.source == "--" || tok.source == Symbol.comment => |
67433
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
25 |
rest.dropWhile(_.is_space) match { |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
26 |
case tok1 :: rest1 if tok1.is_text => |
67442
f075640b8868
uniform treatment of old-style and new-style comments;
wenzelm
parents:
67436
diff
changeset
|
27 |
update(rest1, make_comment(tok1) :: result) |
67433
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
28 |
case _ => update(rest, tok.source :: result) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
29 |
} |
67442
f075640b8868
uniform treatment of old-style and new-style comments;
wenzelm
parents:
67436
diff
changeset
|
30 |
case tok :: rest if tok.is_formal_comment && tok.source.startsWith(Symbol.comment) => |
f075640b8868
uniform treatment of old-style and new-style comments;
wenzelm
parents:
67436
diff
changeset
|
31 |
update(rest, make_comment(tok) :: result) |
67433
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
32 |
case tok :: rest => update(rest, tok.source :: result) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
33 |
case Nil => result.reverse.mkString |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
34 |
} |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
35 |
} |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
36 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
37 |
val text0 = File.read(path) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
38 |
val text1 = update(Token.explode(Keyword.Keywords.empty, text0), Nil) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
39 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
40 |
if (text0 != text1) { |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
41 |
Output.writeln("changing " + path) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
42 |
File.write_backup2(path, text1) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
43 |
} |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
44 |
} |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
45 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
46 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
47 |
/* Isabelle tool wrapper */ |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
48 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
49 |
val isabelle_tool = |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
50 |
Isabelle_Tool("update_comments", "update formal comments in outer syntax", args => |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
51 |
{ |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
52 |
val getopts = Getopts(""" |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
53 |
Usage: isabelle update_comments [FILES|DIRS...] |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
54 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
55 |
Recursively find .thy files and update formal comments in outer syntax. |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
56 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
57 |
Old versions of files are preserved by appending "~~". |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
58 |
""") |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
59 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
60 |
val specs = getopts(args) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
61 |
if (specs.isEmpty) getopts.usage() |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
62 |
|
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
63 |
for { |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
64 |
spec <- specs |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
65 |
file <- File.find_files(Path.explode(spec).file, file => file.getName.endsWith(".thy")) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
66 |
} update_comments(Path.explode(File.standard_path(file))) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
67 |
}) |
e0c0c1f0e3e7
more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff
changeset
|
68 |
} |