src/Pure/Thy/thy_header.scala
author wenzelm
Thu Nov 13 23:45:15 2014 +0100 (2014-11-13)
changeset 58999 ed09ae4ea2d8
parent 58928 23d0ffd48006
child 59694 d2bb4b5ed862
permissions -rw-r--r--
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
discontinued obsolete 'txt_raw' (superseded by 'text_raw');
eliminated obsolete Outer_Syntax.markup (superseded by keyword kinds);
'text' and 'txt' no longer appear in Sidekick tree due to change of keyword kind;
changed tagging of diagnostic commands within proof;
wenzelm@28495
     1
/*  Title:      Pure/Thy/thy_header.scala
wenzelm@28495
     2
    Author:     Makarius
wenzelm@28495
     3
wenzelm@46939
     4
Static theory header information.
wenzelm@28495
     5
*/
wenzelm@28495
     6
wenzelm@28495
     7
package isabelle
wenzelm@28495
     8
wenzelm@28495
     9
wenzelm@43611
    10
import scala.annotation.tailrec
wenzelm@34169
    11
import scala.collection.mutable
wenzelm@43646
    12
import scala.util.parsing.input.{Reader, CharSequenceReader}
wenzelm@38149
    13
import scala.util.matching.Regex
wenzelm@34169
    14
wenzelm@34169
    15
wenzelm@43661
    16
object Thy_Header extends Parse.Parser
wenzelm@32450
    17
{
wenzelm@58928
    18
  /* bootstrap keywords */
wenzelm@58928
    19
wenzelm@58928
    20
  type Keywords = List[(String, Option[Keyword.Spec], Option[String])]
wenzelm@58928
    21
wenzelm@28495
    22
  val HEADER = "header"
wenzelm@58868
    23
  val CHAPTER = "chapter"
wenzelm@58868
    24
  val SECTION = "section"
wenzelm@58868
    25
  val SUBSECTION = "subsection"
wenzelm@58868
    26
  val SUBSUBSECTION = "subsubsection"
wenzelm@58999
    27
  val TEXT = "text"
wenzelm@58999
    28
  val TXT = "txt"
wenzelm@58999
    29
  val TEXT_RAW = "text_raw"
wenzelm@58868
    30
wenzelm@28495
    31
  val THEORY = "theory"
wenzelm@28495
    32
  val IMPORTS = "imports"
wenzelm@46938
    33
  val KEYWORDS = "keywords"
wenzelm@46938
    34
  val AND = "and"
wenzelm@28495
    35
  val BEGIN = "begin"
wenzelm@28495
    36
wenzelm@58928
    37
  private val bootstrap_header: Keywords =
wenzelm@58928
    38
    List(
wenzelm@58928
    39
      ("%", None, None),
wenzelm@58928
    40
      ("(", None, None),
wenzelm@58928
    41
      (")", None, None),
wenzelm@58928
    42
      (",", None, None),
wenzelm@58928
    43
      ("::", None, None),
wenzelm@58928
    44
      ("==", None, None),
wenzelm@58928
    45
      (AND, None, None),
wenzelm@58928
    46
      (BEGIN, None, None),
wenzelm@58928
    47
      (IMPORTS, None, None),
wenzelm@58928
    48
      (KEYWORDS, None, None),
wenzelm@58999
    49
      (HEADER, Some(((Keyword.DOCUMENT_HEADING, Nil), Nil)), None),
wenzelm@58999
    50
      (CHAPTER, Some(((Keyword.DOCUMENT_HEADING, Nil), Nil)), None),
wenzelm@58999
    51
      (SECTION, Some(((Keyword.DOCUMENT_HEADING, Nil), Nil)), None),
wenzelm@58999
    52
      (SUBSECTION, Some(((Keyword.DOCUMENT_HEADING, Nil), Nil)), None),
wenzelm@58999
    53
      (SUBSUBSECTION, Some(((Keyword.DOCUMENT_HEADING, Nil), Nil)), None),
wenzelm@58999
    54
      (TEXT, Some(((Keyword.DOCUMENT_BODY, Nil), Nil)), None),
wenzelm@58999
    55
      (TXT, Some(((Keyword.DOCUMENT_BODY, Nil), Nil)), None),
wenzelm@58999
    56
      (TEXT_RAW, Some(((Keyword.DOCUMENT_RAW, Nil), Nil)), None),
wenzelm@58928
    57
      (THEORY, Some((Keyword.THY_BEGIN, Nil), List("theory")), None),
wenzelm@58928
    58
      ("ML_file", Some((Keyword.THY_LOAD, Nil), List("ML")), None))
wenzelm@58928
    59
wenzelm@58928
    60
  private val bootstrap_keywords =
wenzelm@58928
    61
    Keyword.Keywords.empty.add_keywords(bootstrap_header)
wenzelm@58928
    62
wenzelm@58928
    63
  def bootstrap_syntax(): Outer_Syntax =
wenzelm@58928
    64
    Outer_Syntax.init().add_keywords(bootstrap_header)
wenzelm@34190
    65
wenzelm@38149
    66
wenzelm@44160
    67
  /* theory file name */
wenzelm@44160
    68
wenzelm@44225
    69
  private val Base_Name = new Regex(""".*?([^/\\:]+)""")
wenzelm@44222
    70
  private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
wenzelm@44160
    71
wenzelm@44225
    72
  def base_name(s: String): String =
wenzelm@44225
    73
    s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
wenzelm@44225
    74
wenzelm@44222
    75
  def thy_name(s: String): Option[String] =
wenzelm@44222
    76
    s match { case Thy_Name(name) => Some(name) case _ => None }
wenzelm@38149
    77
wenzelm@34169
    78
wenzelm@34169
    79
  /* header */
wenzelm@34169
    80
wenzelm@44159
    81
  val header: Parser[Thy_Header] =
wenzelm@34169
    82
  {
wenzelm@43611
    83
    val file_name = atom("file name", _.is_name)
wenzelm@34169
    84
wenzelm@48864
    85
    val opt_files =
wenzelm@58908
    86
      $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
wenzelm@48864
    87
      success(Nil)
wenzelm@48864
    88
    val keyword_spec =
wenzelm@48864
    89
      atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
wenzelm@48864
    90
      { case x ~ y ~ z => ((x, y), z) }
wenzelm@48864
    91
wenzelm@46938
    92
    val keyword_decl =
wenzelm@50128
    93
      rep1(string) ~
wenzelm@58908
    94
      opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~
wenzelm@58908
    95
      opt($$$("==") ~! name ^^ { case _ ~ x => x }) ^^
wenzelm@50128
    96
      { case xs ~ y ~ z => xs.map((_, y, z)) }
wenzelm@46939
    97
    val keyword_decls =
wenzelm@58908
    98
      keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
wenzelm@46939
    99
      { case xs ~ yss => (xs :: yss).flatten }
wenzelm@46938
   100
wenzelm@34169
   101
    val file =
wenzelm@58908
   102
      $$$("(") ~! (file_name ~ $$$(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
wenzelm@44185
   103
      file_name ^^ (x => (x, true))
wenzelm@34169
   104
wenzelm@34169
   105
    val args =
wenzelm@46938
   106
      theory_name ~
wenzelm@58908
   107
      (opt($$$(IMPORTS) ~! (rep1(theory_xname))) ^^
wenzelm@56823
   108
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   109
      (opt($$$(KEYWORDS) ~! keyword_decls) ^^
wenzelm@56823
   110
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   111
      $$$(BEGIN) ^^
wenzelm@51294
   112
      { case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) }
wenzelm@34169
   113
wenzelm@58868
   114
    val heading =
wenzelm@58907
   115
      (command(HEADER) |
wenzelm@58907
   116
        command(CHAPTER) |
wenzelm@58907
   117
        command(SECTION) |
wenzelm@58907
   118
        command(SUBSECTION) |
wenzelm@58999
   119
        command(SUBSUBSECTION) |
wenzelm@58999
   120
        command(TEXT) |
wenzelm@58999
   121
        command(TXT) |
wenzelm@58999
   122
        command(TEXT_RAW)) ~
wenzelm@58868
   123
      tags ~! document_source
wenzelm@58868
   124
wenzelm@58907
   125
    (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
   126
  }
wenzelm@34169
   127
wenzelm@34169
   128
wenzelm@34190
   129
  /* read -- lazy scanning */
wenzelm@34169
   130
wenzelm@44159
   131
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
   132
  {
wenzelm@58928
   133
    val token = Token.Parsers.token(bootstrap_keywords)
wenzelm@36956
   134
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
   135
wenzelm@43611
   136
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
   137
    {
wenzelm@34169
   138
      token(in) match {
wenzelm@55494
   139
        case Token.Parsers.Success(tok, rest) =>
wenzelm@34169
   140
          toks += tok
wenzelm@43611
   141
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
   142
        case _ =>
wenzelm@34169
   143
      }
wenzelm@34169
   144
    }
wenzelm@43611
   145
    scan_to_begin(reader)
wenzelm@34190
   146
wenzelm@36956
   147
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
   148
      case Success(result, _) => result
wenzelm@34190
   149
      case bad => error(bad.toString)
wenzelm@34190
   150
    }
wenzelm@34169
   151
  }
wenzelm@43611
   152
wenzelm@44159
   153
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
   154
    read(new CharSequenceReader(source))
wenzelm@34169
   155
}
wenzelm@44159
   156
wenzelm@44159
   157
wenzelm@44185
   158
sealed case class Thy_Header(
wenzelm@50128
   159
  name: String,
wenzelm@50128
   160
  imports: List[String],
wenzelm@51294
   161
  keywords: Thy_Header.Keywords)
wenzelm@44159
   162
{
wenzelm@44159
   163
  def map(f: String => String): Thy_Header =
wenzelm@51294
   164
    Thy_Header(f(name), imports.map(f), keywords)
wenzelm@56823
   165
wenzelm@56823
   166
  def decode_symbols: Thy_Header =
wenzelm@56823
   167
  {
wenzelm@56823
   168
    val f = Symbol.decode _
wenzelm@56823
   169
    Thy_Header(f(name), imports.map(f),
wenzelm@56823
   170
      keywords.map({ case (a, b, c) =>
wenzelm@58899
   171
        (f(a), b.map({ case ((x, y), z) => ((f(x), y.map(f)), z.map(f)) }), c.map(f)) }))
wenzelm@56823
   172
  }
wenzelm@44159
   173
}
wenzelm@44159
   174