src/Pure/Thy/thy_header.scala
author wenzelm
Thu Nov 02 10:16:22 2017 +0100 (19 months ago)
changeset 66987 352b23c97ac8
parent 66984 a1d3e5df0c95
child 67004 af72fa58f71b
permissions -rw-r--r--
support focus_session, for much faster startup of Isabelle/jEdit;
more options for "isabelle jedit";
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@59736
    68
  lazy val bootstrap_syntax: Outer_Syntax =
wenzelm@66984
    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@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@65452
    82
  private val Import_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@65452
    88
    s match { case Import_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
wenzelm@44225
    89
wenzelm@65452
    90
  def theory_name(s: String): String =
wenzelm@62895
    91
    s match {
wenzelm@66195
    92
      case Thy_File_Name(name) => bootstrap_name(name)
wenzelm@65452
    93
      case Import_Name(name) =>
wenzelm@65452
    94
        ml_roots.collectFirst({ case (a, b) if a == name => b }).getOrElse("")
wenzelm@65452
    95
      case _ => ""
wenzelm@62895
    96
    }
wenzelm@38149
    97
wenzelm@64673
    98
  def is_ml_root(theory: String): Boolean =
wenzelm@64673
    99
    ml_roots.exists({ case (_, b) => b == theory })
wenzelm@64673
   100
wenzelm@64673
   101
  def is_bootstrap(theory: String): Boolean =
wenzelm@64673
   102
    bootstrap_thys.exists({ case (_, b) => b == theory })
wenzelm@64673
   103
wenzelm@66195
   104
  def bootstrap_name(theory: String): String =
wenzelm@66195
   105
    bootstrap_thys.collectFirst({ case (a, b) if a == theory => b }).getOrElse(theory)
wenzelm@66195
   106
wenzelm@34169
   107
wenzelm@34169
   108
  /* header */
wenzelm@34169
   109
wenzelm@44159
   110
  val header: Parser[Thy_Header] =
wenzelm@34169
   111
  {
wenzelm@48864
   112
    val opt_files =
wenzelm@58908
   113
      $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
wenzelm@48864
   114
      success(Nil)
wenzelm@59694
   115
wenzelm@48864
   116
    val keyword_spec =
wenzelm@48864
   117
      atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
wenzelm@65384
   118
      { case x ~ y ~ z => Keyword.Spec(x, y, z) }
wenzelm@48864
   119
wenzelm@46938
   120
    val keyword_decl =
wenzelm@50128
   121
      rep1(string) ~
wenzelm@63579
   122
      opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^
wenzelm@65384
   123
      { case xs ~ y => xs.map((_, y.getOrElse(Keyword.Spec.none))) }
wenzelm@59694
   124
wenzelm@46939
   125
    val keyword_decls =
wenzelm@58908
   126
      keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
wenzelm@46939
   127
      { case xs ~ yss => (xs :: yss).flatten }
wenzelm@46938
   128
wenzelm@63579
   129
    val abbrevs =
wenzelm@63579
   130
      rep1(text ~ ($$$("=") ~! text) ^^ { case a ~ (_ ~ b) => (a, b) })
wenzelm@63579
   131
wenzelm@34169
   132
    val args =
wenzelm@59694
   133
      position(theory_name) ~
wenzelm@62969
   134
      (opt($$$(IMPORTS) ~! rep1(position(theory_name))) ^^
wenzelm@56823
   135
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   136
      (opt($$$(KEYWORDS) ~! keyword_decls) ^^
wenzelm@56823
   137
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@63579
   138
      (opt($$$(ABBREVS) ~! abbrevs) ^^
wenzelm@63579
   139
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   140
      $$$(BEGIN) ^^
wenzelm@66713
   141
      { case (name, pos) ~ imports ~ keywords ~ abbrevs ~ _ =>
wenzelm@66713
   142
          val f = Symbol.decode _
wenzelm@66713
   143
          Thy_Header((f(name), pos),
wenzelm@66713
   144
            imports.map({ case (a, b) => (f(a), b) }),
wenzelm@66713
   145
            keywords.map({ case (a, Keyword.Spec(b, c, d)) =>
wenzelm@66713
   146
              (f(a), Keyword.Spec(f(b), c.map(f), d.map(f))) }),
wenzelm@66713
   147
            abbrevs.map({ case (a, b) => (f(a), f(b)) }))
wenzelm@66713
   148
      }
wenzelm@34169
   149
wenzelm@58868
   150
    val heading =
wenzelm@62453
   151
      (command(CHAPTER) |
wenzelm@58907
   152
        command(SECTION) |
wenzelm@58907
   153
        command(SUBSECTION) |
wenzelm@58999
   154
        command(SUBSUBSECTION) |
wenzelm@61463
   155
        command(PARAGRAPH) |
wenzelm@61463
   156
        command(SUBPARAGRAPH) |
wenzelm@58999
   157
        command(TEXT) |
wenzelm@58999
   158
        command(TXT) |
wenzelm@58999
   159
        command(TEXT_RAW)) ~
wenzelm@58868
   160
      tags ~! document_source
wenzelm@58868
   161
wenzelm@58907
   162
    (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
   163
  }
wenzelm@34169
   164
wenzelm@34169
   165
wenzelm@34190
   166
  /* read -- lazy scanning */
wenzelm@34169
   167
wenzelm@65539
   168
  private def read_tokens(reader: Reader[Char], strict: Boolean): (List[Token], List[Token]) =
wenzelm@34169
   169
  {
wenzelm@58928
   170
    val token = Token.Parsers.token(bootstrap_keywords)
wenzelm@64825
   171
    def make_tokens(in: Reader[Char]): Stream[Token] =
wenzelm@34169
   172
      token(in) match {
wenzelm@64825
   173
        case Token.Parsers.Success(tok, rest) => tok #:: make_tokens(rest)
wenzelm@64825
   174
        case _ => Stream.empty
wenzelm@34169
   175
      }
wenzelm@64825
   176
wenzelm@65539
   177
    val all_tokens = make_tokens(reader)
wenzelm@65539
   178
    val drop_tokens =
wenzelm@65539
   179
      if (strict) Nil
wenzelm@65539
   180
      else all_tokens.takeWhile(tok => !tok.is_command(Thy_Header.THEORY)).toList
wenzelm@34190
   181
wenzelm@65539
   182
    val tokens = all_tokens.drop(drop_tokens.length)
wenzelm@64825
   183
    val tokens1 = tokens.takeWhile(tok => !tok.is_begin).toList
wenzelm@64825
   184
    val tokens2 = tokens.dropWhile(tok => !tok.is_begin).headOption.toList
wenzelm@64825
   185
wenzelm@65539
   186
    (drop_tokens, tokens1 ::: tokens2)
wenzelm@65539
   187
  }
wenzelm@65539
   188
wenzelm@65539
   189
  def read(reader: Reader[Char], start: Token.Pos, strict: Boolean = true): Thy_Header =
wenzelm@65539
   190
  {
wenzelm@65539
   191
    val (_, tokens0) = read_tokens(reader, true)
wenzelm@66918
   192
    val text = Scan.reader_decode_utf8(reader, Token.implode(tokens0))
wenzelm@65539
   193
wenzelm@65539
   194
    val (drop_tokens, tokens) = read_tokens(Scan.char_reader(text), strict)
wenzelm@65539
   195
    val pos = (start /: drop_tokens)(_.advance(_))
wenzelm@65539
   196
wenzelm@65539
   197
    parse(commit(header), Token.reader(tokens, pos)) match {
wenzelm@34190
   198
      case Success(result, _) => result
wenzelm@34190
   199
      case bad => error(bad.toString)
wenzelm@34190
   200
    }
wenzelm@34169
   201
  }
wenzelm@34169
   202
}
wenzelm@44159
   203
wenzelm@44185
   204
sealed case class Thy_Header(
wenzelm@59694
   205
  name: (String, Position.T),
wenzelm@59694
   206
  imports: List[(String, Position.T)],
wenzelm@63579
   207
  keywords: Thy_Header.Keywords,
wenzelm@63579
   208
  abbrevs: Thy_Header.Abbrevs)