src/Pure/Thy/thy_header.scala
author wenzelm
Sat Mar 14 18:18:40 2015 +0100 (2015-03-14)
changeset 59694 d2bb4b5ed862
parent 58999 ed09ae4ea2d8
child 59695 a03e0561bdbf
permissions -rw-r--r--
misc tuning -- more uniform ML vs. Scala;
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@48864
    83
    val opt_files =
wenzelm@58908
    84
      $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
wenzelm@48864
    85
      success(Nil)
wenzelm@59694
    86
wenzelm@48864
    87
    val keyword_spec =
wenzelm@48864
    88
      atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
wenzelm@48864
    89
      { case x ~ y ~ z => ((x, y), z) }
wenzelm@48864
    90
wenzelm@46938
    91
    val keyword_decl =
wenzelm@50128
    92
      rep1(string) ~
wenzelm@58908
    93
      opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~
wenzelm@58908
    94
      opt($$$("==") ~! name ^^ { case _ ~ x => x }) ^^
wenzelm@50128
    95
      { case xs ~ y ~ z => xs.map((_, y, z)) }
wenzelm@59694
    96
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 args =
wenzelm@59694
   102
      position(theory_name) ~
wenzelm@59694
   103
      (opt($$$(IMPORTS) ~! (rep1(position(theory_xname)))) ^^
wenzelm@56823
   104
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   105
      (opt($$$(KEYWORDS) ~! keyword_decls) ^^
wenzelm@56823
   106
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   107
      $$$(BEGIN) ^^
wenzelm@51294
   108
      { case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) }
wenzelm@34169
   109
wenzelm@58868
   110
    val heading =
wenzelm@58907
   111
      (command(HEADER) |
wenzelm@58907
   112
        command(CHAPTER) |
wenzelm@58907
   113
        command(SECTION) |
wenzelm@58907
   114
        command(SUBSECTION) |
wenzelm@58999
   115
        command(SUBSUBSECTION) |
wenzelm@58999
   116
        command(TEXT) |
wenzelm@58999
   117
        command(TXT) |
wenzelm@58999
   118
        command(TEXT_RAW)) ~
wenzelm@58868
   119
      tags ~! document_source
wenzelm@58868
   120
wenzelm@58907
   121
    (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
   122
  }
wenzelm@34169
   123
wenzelm@34169
   124
wenzelm@34190
   125
  /* read -- lazy scanning */
wenzelm@34169
   126
wenzelm@44159
   127
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
   128
  {
wenzelm@58928
   129
    val token = Token.Parsers.token(bootstrap_keywords)
wenzelm@36956
   130
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
   131
wenzelm@43611
   132
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
   133
    {
wenzelm@34169
   134
      token(in) match {
wenzelm@55494
   135
        case Token.Parsers.Success(tok, rest) =>
wenzelm@34169
   136
          toks += tok
wenzelm@43611
   137
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
   138
        case _ =>
wenzelm@34169
   139
      }
wenzelm@34169
   140
    }
wenzelm@43611
   141
    scan_to_begin(reader)
wenzelm@34190
   142
wenzelm@36956
   143
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
   144
      case Success(result, _) => result
wenzelm@34190
   145
      case bad => error(bad.toString)
wenzelm@34190
   146
    }
wenzelm@34169
   147
  }
wenzelm@43611
   148
wenzelm@44159
   149
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
   150
    read(new CharSequenceReader(source))
wenzelm@34169
   151
}
wenzelm@44159
   152
wenzelm@44159
   153
wenzelm@44185
   154
sealed case class Thy_Header(
wenzelm@59694
   155
  name: (String, Position.T),
wenzelm@59694
   156
  imports: List[(String, Position.T)],
wenzelm@51294
   157
  keywords: Thy_Header.Keywords)
wenzelm@44159
   158
{
wenzelm@56823
   159
  def decode_symbols: Thy_Header =
wenzelm@56823
   160
  {
wenzelm@56823
   161
    val f = Symbol.decode _
wenzelm@59694
   162
    Thy_Header((f(name._1), name._2), imports.map({ case (a, b) => (f(a), b) }),
wenzelm@56823
   163
      keywords.map({ case (a, b, c) =>
wenzelm@58899
   164
        (f(a), b.map({ case ((x, y), z) => ((f(x), y.map(f)), z.map(f)) }), c.map(f)) }))
wenzelm@56823
   165
  }
wenzelm@44159
   166
}