src/Pure/Tools/update_comments.scala
author nipkow
Tue, 17 Jun 2025 14:11:40 +0200
changeset 82733 8b537e1af2ec
parent 75906 2167b9e3157a
permissions -rw-r--r--
reinstated intersection of lists as inter_list_set
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74887
diff changeset
    13
object Update_Comments {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74887
diff changeset
    14
  def update_comments(path: Path): Unit = {
67442
f075640b8868 uniform treatment of old-style and new-style comments;
wenzelm
parents: 67436
diff changeset
    15
    def make_comment(tok: Token): String =
f075640b8868 uniform treatment of old-style and new-style comments;
wenzelm
parents: 67436
diff changeset
    16
      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
    17
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74887
diff changeset
    18
    @tailrec def update(toks: List[Token], result: List[String]): String = {
67433
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    19
      toks match {
67442
f075640b8868 uniform treatment of old-style and new-style comments;
wenzelm
parents: 67436
diff changeset
    20
        case tok :: rest
f075640b8868 uniform treatment of old-style and new-style comments;
wenzelm
parents: 67436
diff changeset
    21
        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
    22
          rest.dropWhile(_.is_space) match {
74887
56247fdb8bbb discontinued old-style {* verbatim *} tokens;
wenzelm
parents: 73340
diff changeset
    23
            case tok1 :: rest1 if tok1.is_embedded =>
67442
f075640b8868 uniform treatment of old-style and new-style comments;
wenzelm
parents: 67436
diff changeset
    24
              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
    25
            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
    26
          }
67442
f075640b8868 uniform treatment of old-style and new-style comments;
wenzelm
parents: 67436
diff changeset
    27
        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
    28
          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
    29
        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
    30
        case Nil => result.reverse.mkString
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    31
      }
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    32
    }
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    33
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    34
    val text0 = File.read(path)
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    35
    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
    36
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    37
    if (text0 != text1) {
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    38
      Output.writeln("changing " + path)
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    39
      File.write_backup2(path, text1)
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    40
    }
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    41
  }
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    42
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
  /* Isabelle tool wrapper */
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
  val isabelle_tool =
72763
3cc73d00553c added document antiquotation @{tool};
wenzelm
parents: 68994
diff changeset
    47
    Isabelle_Tool("update_comments", "update formal comments in outer syntax",
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74887
diff changeset
    48
      Scala_Project.here,
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    49
      { args =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    50
        val getopts = Getopts("""
67433
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    51
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
    52
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    53
  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
    54
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    55
  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
    56
""")
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    57
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    58
        val specs = getopts(args)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    59
        if (specs.isEmpty) getopts.usage()
67433
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    60
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    61
        for {
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    62
          spec <- specs
75906
2167b9e3157a clarified signature: support for adhoc file types;
wenzelm
parents: 75394
diff changeset
    63
          file <- File.find_files(Path.explode(spec).file, file => File.is_thy(file.getName))
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    64
        } update_comments(File.path(file))
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    65
      })
67433
e0c0c1f0e3e7 more specific tool "isabelle update_comments", for uniformity with formal comments in embedded languages;
wenzelm
parents:
diff changeset
    66
}