src/Pure/Thy/thy_header.scala
author wenzelm
Sat Dec 16 14:40:21 2017 +0100 (18 months ago)
changeset 67212 f5d44a01030c
parent 67164 39f57f0757f1
child 67215 03d0c958d65a
permissions -rw-r--r--
tuned;
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@65384
    42
      ("%", Keyword.Spec.none),
wenzelm@65384
    43
      ("(", Keyword.Spec.none),
wenzelm@65384
    44
      (")", Keyword.Spec.none),
wenzelm@65384
    45
      (",", Keyword.Spec.none),
wenzelm@65384
    46
      ("::", Keyword.Spec.none),
wenzelm@65384
    47
      ("=", Keyword.Spec.none),
wenzelm@65384
    48
      (AND, Keyword.Spec.none),
wenzelm@65384
    49
      (BEGIN, Keyword.Spec(Keyword.QUASI_COMMAND)),
wenzelm@65384
    50
      (IMPORTS, Keyword.Spec(Keyword.QUASI_COMMAND)),
wenzelm@65384
    51
      (KEYWORDS, Keyword.Spec(Keyword.QUASI_COMMAND)),
wenzelm@65384
    52
      (ABBREVS, Keyword.Spec(Keyword.QUASI_COMMAND)),
wenzelm@65384
    53
      (CHAPTER, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
wenzelm@65384
    54
      (SECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
wenzelm@65384
    55
      (SUBSECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
wenzelm@65384
    56
      (SUBSUBSECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
wenzelm@65384
    57
      (PARAGRAPH, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
wenzelm@65384
    58
      (SUBPARAGRAPH, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
wenzelm@65384
    59
      (TEXT, Keyword.Spec(Keyword.DOCUMENT_BODY)),
wenzelm@65384
    60
      (TXT, Keyword.Spec(Keyword.DOCUMENT_BODY)),
wenzelm@65384
    61
      (TEXT_RAW, Keyword.Spec(Keyword.DOCUMENT_RAW)),
wenzelm@65384
    62
      (THEORY, Keyword.Spec(Keyword.THY_BEGIN, tags = List("theory"))),
wenzelm@65384
    63
      ("ML", Keyword.Spec(Keyword.THY_DECL, tags = 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@67004
    68
  val bootstrap_syntax: Outer_Syntax =
wenzelm@67004
    69
    Outer_Syntax.empty.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@65490
    76
  val ml_roots = List("ROOT0.ML" -> "ML_Root0", "ROOT.ML" -> "ML_Root")
wenzelm@63022
    77
  val bootstrap_thys = List(PURE, ML_BOOTSTRAP).map(a => a -> ("Bootstrap_" + a))
wenzelm@44160
    78
wenzelm@65490
    79
  val bootstrap_global_theories = (ml_roots ::: bootstrap_thys).map(p => (p._2 -> PURE))
wenzelm@65490
    80
wenzelm@65452
    81
  private val Thy_File_Name = new Regex(""".*?([^/\\:]+)\.thy""")
wenzelm@67212
    82
  private val File_Name = new Regex(""".*?([^/\\:]+)""")
wenzelm@44160
    83
wenzelm@65526
    84
  def is_base_name(s: String): Boolean =
wenzelm@65526
    85
    s != "" && !s.exists("/\\:".contains(_))
wenzelm@65526
    86
wenzelm@65452
    87
  def import_name(s: String): String =
wenzelm@67164
    88
    s match {
wenzelm@67212
    89
      case File_Name(name) if !name.endsWith(".thy") => name
wenzelm@67164
    90
      case _ => error("Malformed theory import: " + quote(s))
wenzelm@67164
    91
    }
wenzelm@44225
    92
wenzelm@65452
    93
  def theory_name(s: String): String =
wenzelm@62895
    94
    s match {
wenzelm@66195
    95
      case Thy_File_Name(name) => bootstrap_name(name)
wenzelm@67212
    96
      case File_Name(name) =>
wenzelm@65452
    97
        ml_roots.collectFirst({ case (a, b) if a == name => b }).getOrElse("")
wenzelm@65452
    98
      case _ => ""
wenzelm@62895
    99
    }
wenzelm@38149
   100
wenzelm@64673
   101
  def is_ml_root(theory: String): Boolean =
wenzelm@64673
   102
    ml_roots.exists({ case (_, b) => b == theory })
wenzelm@64673
   103
wenzelm@64673
   104
  def is_bootstrap(theory: String): Boolean =
wenzelm@64673
   105
    bootstrap_thys.exists({ case (_, b) => b == theory })
wenzelm@64673
   106
wenzelm@66195
   107
  def bootstrap_name(theory: String): String =
wenzelm@66195
   108
    bootstrap_thys.collectFirst({ case (a, b) if a == theory => b }).getOrElse(theory)
wenzelm@66195
   109
wenzelm@34169
   110
wenzelm@34169
   111
  /* header */
wenzelm@34169
   112
wenzelm@44159
   113
  val header: Parser[Thy_Header] =
wenzelm@34169
   114
  {
wenzelm@48864
   115
    val opt_files =
wenzelm@58908
   116
      $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
wenzelm@48864
   117
      success(Nil)
wenzelm@59694
   118
wenzelm@48864
   119
    val keyword_spec =
wenzelm@48864
   120
      atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
wenzelm@65384
   121
      { case x ~ y ~ z => Keyword.Spec(x, y, z) }
wenzelm@48864
   122
wenzelm@46938
   123
    val keyword_decl =
wenzelm@50128
   124
      rep1(string) ~
wenzelm@63579
   125
      opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^
wenzelm@65384
   126
      { case xs ~ y => xs.map((_, y.getOrElse(Keyword.Spec.none))) }
wenzelm@59694
   127
wenzelm@46939
   128
    val keyword_decls =
wenzelm@58908
   129
      keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
wenzelm@46939
   130
      { case xs ~ yss => (xs :: yss).flatten }
wenzelm@46938
   131
wenzelm@63579
   132
    val abbrevs =
wenzelm@67013
   133
      rep1sep(text ~ ($$$("=") ~! text) ^^ { case a ~ (_ ~ b) => (a, b) }, $$$("and"))
wenzelm@63579
   134
wenzelm@34169
   135
    val args =
wenzelm@59694
   136
      position(theory_name) ~
wenzelm@62969
   137
      (opt($$$(IMPORTS) ~! rep1(position(theory_name))) ^^
wenzelm@56823
   138
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   139
      (opt($$$(KEYWORDS) ~! keyword_decls) ^^
wenzelm@56823
   140
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@63579
   141
      (opt($$$(ABBREVS) ~! abbrevs) ^^
wenzelm@63579
   142
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   143
      $$$(BEGIN) ^^
wenzelm@66713
   144
      { case (name, pos) ~ imports ~ keywords ~ abbrevs ~ _ =>
wenzelm@66713
   145
          val f = Symbol.decode _
wenzelm@66713
   146
          Thy_Header((f(name), pos),
wenzelm@66713
   147
            imports.map({ case (a, b) => (f(a), b) }),
wenzelm@66713
   148
            keywords.map({ case (a, Keyword.Spec(b, c, d)) =>
wenzelm@66713
   149
              (f(a), Keyword.Spec(f(b), c.map(f), d.map(f))) }),
wenzelm@66713
   150
            abbrevs.map({ case (a, b) => (f(a), f(b)) }))
wenzelm@66713
   151
      }
wenzelm@34169
   152
wenzelm@58868
   153
    val heading =
wenzelm@62453
   154
      (command(CHAPTER) |
wenzelm@58907
   155
        command(SECTION) |
wenzelm@58907
   156
        command(SUBSECTION) |
wenzelm@58999
   157
        command(SUBSUBSECTION) |
wenzelm@61463
   158
        command(PARAGRAPH) |
wenzelm@61463
   159
        command(SUBPARAGRAPH) |
wenzelm@58999
   160
        command(TEXT) |
wenzelm@58999
   161
        command(TXT) |
wenzelm@58999
   162
        command(TEXT_RAW)) ~
wenzelm@58868
   163
      tags ~! document_source
wenzelm@58868
   164
wenzelm@58907
   165
    (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
   166
  }
wenzelm@34169
   167
wenzelm@34169
   168
wenzelm@34190
   169
  /* read -- lazy scanning */
wenzelm@34169
   170
wenzelm@65539
   171
  private def read_tokens(reader: Reader[Char], strict: Boolean): (List[Token], List[Token]) =
wenzelm@34169
   172
  {
wenzelm@58928
   173
    val token = Token.Parsers.token(bootstrap_keywords)
wenzelm@64825
   174
    def make_tokens(in: Reader[Char]): Stream[Token] =
wenzelm@34169
   175
      token(in) match {
wenzelm@64825
   176
        case Token.Parsers.Success(tok, rest) => tok #:: make_tokens(rest)
wenzelm@64825
   177
        case _ => Stream.empty
wenzelm@34169
   178
      }
wenzelm@64825
   179
wenzelm@65539
   180
    val all_tokens = make_tokens(reader)
wenzelm@65539
   181
    val drop_tokens =
wenzelm@65539
   182
      if (strict) Nil
wenzelm@65539
   183
      else all_tokens.takeWhile(tok => !tok.is_command(Thy_Header.THEORY)).toList
wenzelm@34190
   184
wenzelm@65539
   185
    val tokens = all_tokens.drop(drop_tokens.length)
wenzelm@64825
   186
    val tokens1 = tokens.takeWhile(tok => !tok.is_begin).toList
wenzelm@64825
   187
    val tokens2 = tokens.dropWhile(tok => !tok.is_begin).headOption.toList
wenzelm@64825
   188
wenzelm@65539
   189
    (drop_tokens, tokens1 ::: tokens2)
wenzelm@65539
   190
  }
wenzelm@65539
   191
wenzelm@65539
   192
  def read(reader: Reader[Char], start: Token.Pos, strict: Boolean = true): Thy_Header =
wenzelm@65539
   193
  {
wenzelm@65539
   194
    val (_, tokens0) = read_tokens(reader, true)
wenzelm@66918
   195
    val text = Scan.reader_decode_utf8(reader, Token.implode(tokens0))
wenzelm@65539
   196
wenzelm@65539
   197
    val (drop_tokens, tokens) = read_tokens(Scan.char_reader(text), strict)
wenzelm@65539
   198
    val pos = (start /: drop_tokens)(_.advance(_))
wenzelm@65539
   199
wenzelm@65539
   200
    parse(commit(header), Token.reader(tokens, pos)) match {
wenzelm@34190
   201
      case Success(result, _) => result
wenzelm@34190
   202
      case bad => error(bad.toString)
wenzelm@34190
   203
    }
wenzelm@34169
   204
  }
wenzelm@34169
   205
}
wenzelm@44159
   206
wenzelm@44185
   207
sealed case class Thy_Header(
wenzelm@59694
   208
  name: (String, Position.T),
wenzelm@59694
   209
  imports: List[(String, Position.T)],
wenzelm@63579
   210
  keywords: Thy_Header.Keywords,
wenzelm@63579
   211
  abbrevs: Thy_Header.Abbrevs)