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