src/Pure/Thy/thy_header.scala
author wenzelm
Mon Jan 09 20:26:59 2017 +0100 (2017-01-09)
changeset 64854 f5aa712e6250
parent 64825 e78b62c289bb
child 65362 908a27a4b9c9
permissions -rw-r--r--
tuned signature;
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@64824
    12
import scala.util.parsing.input.Reader
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@63579
    20
  type Keywords = List[(String, Keyword.Spec)]
wenzelm@63579
    21
  type Abbrevs = List[(String, String)]
wenzelm@58928
    22
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@61463
    27
  val PARAGRAPH = "paragraph"
wenzelm@61463
    28
  val SUBPARAGRAPH = "subparagraph"
wenzelm@58999
    29
  val TEXT = "text"
wenzelm@58999
    30
  val TXT = "txt"
wenzelm@58999
    31
  val TEXT_RAW = "text_raw"
wenzelm@58868
    32
wenzelm@28495
    33
  val THEORY = "theory"
wenzelm@28495
    34
  val IMPORTS = "imports"
wenzelm@46938
    35
  val KEYWORDS = "keywords"
wenzelm@63579
    36
  val ABBREVS = "abbrevs"
wenzelm@46938
    37
  val AND = "and"
wenzelm@28495
    38
  val BEGIN = "begin"
wenzelm@28495
    39
wenzelm@64854
    40
  val bootstrap_header: Keywords =
wenzelm@58928
    41
    List(
wenzelm@63579
    42
      ("%", Keyword.no_spec),
wenzelm@63579
    43
      ("(", Keyword.no_spec),
wenzelm@63579
    44
      (")", Keyword.no_spec),
wenzelm@63579
    45
      (",", Keyword.no_spec),
wenzelm@63579
    46
      ("::", Keyword.no_spec),
wenzelm@63579
    47
      ("=", Keyword.no_spec),
wenzelm@63579
    48
      (AND, Keyword.no_spec),
wenzelm@63579
    49
      (BEGIN, Keyword.quasi_command_spec),
wenzelm@63579
    50
      (IMPORTS, Keyword.quasi_command_spec),
wenzelm@63579
    51
      (KEYWORDS, Keyword.quasi_command_spec),
wenzelm@63579
    52
      (ABBREVS, Keyword.quasi_command_spec),
wenzelm@63579
    53
      (CHAPTER, (((Keyword.DOCUMENT_HEADING, Nil), Nil))),
wenzelm@63579
    54
      (SECTION, (((Keyword.DOCUMENT_HEADING, Nil), Nil))),
wenzelm@63579
    55
      (SUBSECTION, (((Keyword.DOCUMENT_HEADING, Nil), Nil))),
wenzelm@63579
    56
      (SUBSUBSECTION, (((Keyword.DOCUMENT_HEADING, Nil), Nil))),
wenzelm@63579
    57
      (PARAGRAPH, (((Keyword.DOCUMENT_HEADING, Nil), Nil))),
wenzelm@63579
    58
      (SUBPARAGRAPH, (((Keyword.DOCUMENT_HEADING, Nil), Nil))),
wenzelm@63579
    59
      (TEXT, (((Keyword.DOCUMENT_BODY, Nil), Nil))),
wenzelm@63579
    60
      (TXT, (((Keyword.DOCUMENT_BODY, Nil), Nil))),
wenzelm@63579
    61
      (TEXT_RAW, (((Keyword.DOCUMENT_RAW, Nil), Nil))),
wenzelm@63579
    62
      (THEORY, ((Keyword.THY_BEGIN, Nil), List("theory"))),
wenzelm@63579
    63
      ("ML", ((Keyword.THY_DECL, Nil), List("ML"))))
wenzelm@58928
    64
wenzelm@58928
    65
  private val bootstrap_keywords =
wenzelm@58928
    66
    Keyword.Keywords.empty.add_keywords(bootstrap_header)
wenzelm@58928
    67
wenzelm@59736
    68
  lazy val bootstrap_syntax: Outer_Syntax =
wenzelm@58928
    69
    Outer_Syntax.init().add_keywords(bootstrap_header)
wenzelm@34190
    70
wenzelm@38149
    71
wenzelm@64673
    72
  /* file name vs. theory name */
wenzelm@62895
    73
wenzelm@63022
    74
  val PURE = "Pure"
wenzelm@62895
    75
  val ML_BOOTSTRAP = "ML_Bootstrap"
wenzelm@63022
    76
  val ML_ROOT = "ML_Root"
wenzelm@63022
    77
  val ml_roots = List("ROOT0.ML" -> "ML_Root0", "ROOT.ML" -> ML_ROOT)
wenzelm@63022
    78
  val bootstrap_thys = List(PURE, ML_BOOTSTRAP).map(a => a -> ("Bootstrap_" + a))
wenzelm@44160
    79
wenzelm@44225
    80
  private val Base_Name = new Regex(""".*?([^/\\:]+)""")
wenzelm@44222
    81
  private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
wenzelm@44160
    82
wenzelm@44225
    83
  def base_name(s: String): String =
wenzelm@44225
    84
    s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
wenzelm@44225
    85
wenzelm@44222
    86
  def thy_name(s: String): Option[String] =
wenzelm@63022
    87
    s match { case Thy_Name(name) => Some(name) case _ => None }
wenzelm@63022
    88
wenzelm@63022
    89
  def thy_name_bootstrap(s: String): Option[String] =
wenzelm@62895
    90
    s match {
wenzelm@63022
    91
      case Thy_Name(name) =>
wenzelm@63022
    92
        Some(bootstrap_thys.collectFirst({ case (a, b) if a == name => b }).getOrElse(name))
wenzelm@62946
    93
      case Base_Name(name) => ml_roots.collectFirst({ case (a, b) if a == name => b })
wenzelm@62895
    94
      case _ => None
wenzelm@62895
    95
    }
wenzelm@38149
    96
wenzelm@64673
    97
  def is_ml_root(theory: String): Boolean =
wenzelm@64673
    98
    ml_roots.exists({ case (_, b) => b == theory })
wenzelm@64673
    99
wenzelm@64673
   100
  def is_bootstrap(theory: String): Boolean =
wenzelm@64673
   101
    bootstrap_thys.exists({ case (_, b) => b == theory })
wenzelm@64673
   102
wenzelm@34169
   103
wenzelm@34169
   104
  /* header */
wenzelm@34169
   105
wenzelm@44159
   106
  val header: Parser[Thy_Header] =
wenzelm@34169
   107
  {
wenzelm@48864
   108
    val opt_files =
wenzelm@58908
   109
      $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
wenzelm@48864
   110
      success(Nil)
wenzelm@59694
   111
wenzelm@48864
   112
    val keyword_spec =
wenzelm@48864
   113
      atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
wenzelm@48864
   114
      { case x ~ y ~ z => ((x, y), z) }
wenzelm@48864
   115
wenzelm@46938
   116
    val keyword_decl =
wenzelm@50128
   117
      rep1(string) ~
wenzelm@63579
   118
      opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^
wenzelm@63579
   119
      { case xs ~ y => xs.map((_, y.getOrElse(Keyword.no_spec))) }
wenzelm@59694
   120
wenzelm@46939
   121
    val keyword_decls =
wenzelm@58908
   122
      keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
wenzelm@46939
   123
      { case xs ~ yss => (xs :: yss).flatten }
wenzelm@46938
   124
wenzelm@63579
   125
    val abbrevs =
wenzelm@63579
   126
      rep1(text ~ ($$$("=") ~! text) ^^ { case a ~ (_ ~ b) => (a, b) })
wenzelm@63579
   127
wenzelm@34169
   128
    val args =
wenzelm@59694
   129
      position(theory_name) ~
wenzelm@62969
   130
      (opt($$$(IMPORTS) ~! rep1(position(theory_name))) ^^
wenzelm@56823
   131
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   132
      (opt($$$(KEYWORDS) ~! keyword_decls) ^^
wenzelm@56823
   133
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@63579
   134
      (opt($$$(ABBREVS) ~! abbrevs) ^^
wenzelm@63579
   135
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   136
      $$$(BEGIN) ^^
wenzelm@63579
   137
      { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
wenzelm@34169
   138
wenzelm@58868
   139
    val heading =
wenzelm@62453
   140
      (command(CHAPTER) |
wenzelm@58907
   141
        command(SECTION) |
wenzelm@58907
   142
        command(SUBSECTION) |
wenzelm@58999
   143
        command(SUBSUBSECTION) |
wenzelm@61463
   144
        command(PARAGRAPH) |
wenzelm@61463
   145
        command(SUBPARAGRAPH) |
wenzelm@58999
   146
        command(TEXT) |
wenzelm@58999
   147
        command(TXT) |
wenzelm@58999
   148
        command(TEXT_RAW)) ~
wenzelm@58868
   149
      tags ~! document_source
wenzelm@58868
   150
wenzelm@58907
   151
    (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
   152
  }
wenzelm@34169
   153
wenzelm@34169
   154
wenzelm@34190
   155
  /* read -- lazy scanning */
wenzelm@34169
   156
wenzelm@64825
   157
  def read(reader: Reader[Char], start: Token.Pos, strict: Boolean = true): Thy_Header =
wenzelm@34169
   158
  {
wenzelm@58928
   159
    val token = Token.Parsers.token(bootstrap_keywords)
wenzelm@64825
   160
    def make_tokens(in: Reader[Char]): Stream[Token] =
wenzelm@34169
   161
      token(in) match {
wenzelm@64825
   162
        case Token.Parsers.Success(tok, rest) => tok #:: make_tokens(rest)
wenzelm@64825
   163
        case _ => Stream.empty
wenzelm@34169
   164
      }
wenzelm@64825
   165
wenzelm@64825
   166
    val tokens =
wenzelm@64825
   167
      if (strict) make_tokens(reader)
wenzelm@64825
   168
      else make_tokens(reader).dropWhile(tok => !tok.is_command(Thy_Header.THEORY))
wenzelm@34190
   169
wenzelm@64825
   170
    val tokens1 = tokens.takeWhile(tok => !tok.is_begin).toList
wenzelm@64825
   171
    val tokens2 = tokens.dropWhile(tok => !tok.is_begin).headOption.toList
wenzelm@64825
   172
wenzelm@64825
   173
    parse(commit(header), Token.reader(tokens1 ::: tokens2, start)) match {
wenzelm@34190
   174
      case Success(result, _) => result
wenzelm@34190
   175
      case bad => error(bad.toString)
wenzelm@34190
   176
    }
wenzelm@34169
   177
  }
wenzelm@34169
   178
}
wenzelm@44159
   179
wenzelm@44159
   180
wenzelm@44185
   181
sealed case class Thy_Header(
wenzelm@59694
   182
  name: (String, Position.T),
wenzelm@59694
   183
  imports: List[(String, Position.T)],
wenzelm@63579
   184
  keywords: Thy_Header.Keywords,
wenzelm@63579
   185
  abbrevs: Thy_Header.Abbrevs)
wenzelm@44159
   186
{
wenzelm@56823
   187
  def decode_symbols: Thy_Header =
wenzelm@56823
   188
  {
wenzelm@56823
   189
    val f = Symbol.decode _
wenzelm@63579
   190
    Thy_Header((f(name._1), name._2),
wenzelm@63579
   191
      imports.map({ case (a, b) => (f(a), b) }),
wenzelm@63579
   192
      keywords.map({ case (a, ((b, c), d)) => (f(a), ((f(b), c.map(f)), d.map(f))) }),
wenzelm@63579
   193
      abbrevs.map({ case (a, b) => (f(a), f(b)) }))
wenzelm@56823
   194
  }
wenzelm@44159
   195
}