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