src/Pure/Thy/thy_header.scala
author wenzelm
Fri Nov 07 16:36:55 2014 +0100 (2014-11-07)
changeset 58928 23d0ffd48006
parent 58908 58bedbc18915
child 58999 ed09ae4ea2d8
permissions -rw-r--r--
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
plain value Outer_Syntax within theory: parsing requires current theory context;
clarified name of Keyword.is_literal according to its semantics;
eliminated pointless type Keyword.T;
simplified @{command_spec};
clarified bootstrap keywords and syntax: take it as basis instead of side-branch;
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@58868
    27
wenzelm@28495
    28
  val THEORY = "theory"
wenzelm@28495
    29
  val IMPORTS = "imports"
wenzelm@46938
    30
  val KEYWORDS = "keywords"
wenzelm@46938
    31
  val AND = "and"
wenzelm@28495
    32
  val BEGIN = "begin"
wenzelm@28495
    33
wenzelm@58928
    34
  private val bootstrap_header: Keywords =
wenzelm@58928
    35
    List(
wenzelm@58928
    36
      ("%", None, None),
wenzelm@58928
    37
      ("(", None, None),
wenzelm@58928
    38
      (")", None, None),
wenzelm@58928
    39
      (",", None, None),
wenzelm@58928
    40
      ("::", None, None),
wenzelm@58928
    41
      ("==", None, None),
wenzelm@58928
    42
      (AND, None, None),
wenzelm@58928
    43
      (BEGIN, None, None),
wenzelm@58928
    44
      (IMPORTS, None, None),
wenzelm@58928
    45
      (KEYWORDS, None, None),
wenzelm@58928
    46
      (HEADER, Some(((Keyword.HEADING, Nil), Nil)), None),
wenzelm@58928
    47
      (CHAPTER, Some(((Keyword.HEADING, Nil), Nil)), None),
wenzelm@58928
    48
      (SECTION, Some(((Keyword.HEADING, Nil), Nil)), None),
wenzelm@58928
    49
      (SUBSECTION, Some(((Keyword.HEADING, Nil), Nil)), None),
wenzelm@58928
    50
      (SUBSUBSECTION, Some(((Keyword.HEADING, Nil), Nil)), None),
wenzelm@58928
    51
      (THEORY, Some((Keyword.THY_BEGIN, Nil), List("theory")), None),
wenzelm@58928
    52
      ("ML_file", Some((Keyword.THY_LOAD, Nil), List("ML")), None))
wenzelm@58928
    53
wenzelm@58928
    54
  private val bootstrap_keywords =
wenzelm@58928
    55
    Keyword.Keywords.empty.add_keywords(bootstrap_header)
wenzelm@58928
    56
wenzelm@58928
    57
  def bootstrap_syntax(): Outer_Syntax =
wenzelm@58928
    58
    Outer_Syntax.init().add_keywords(bootstrap_header)
wenzelm@34190
    59
wenzelm@38149
    60
wenzelm@44160
    61
  /* theory file name */
wenzelm@44160
    62
wenzelm@44225
    63
  private val Base_Name = new Regex(""".*?([^/\\:]+)""")
wenzelm@44222
    64
  private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
wenzelm@44160
    65
wenzelm@44225
    66
  def base_name(s: String): String =
wenzelm@44225
    67
    s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
wenzelm@44225
    68
wenzelm@44222
    69
  def thy_name(s: String): Option[String] =
wenzelm@44222
    70
    s match { case Thy_Name(name) => Some(name) case _ => None }
wenzelm@38149
    71
wenzelm@34169
    72
wenzelm@34169
    73
  /* header */
wenzelm@34169
    74
wenzelm@44159
    75
  val header: Parser[Thy_Header] =
wenzelm@34169
    76
  {
wenzelm@43611
    77
    val file_name = atom("file name", _.is_name)
wenzelm@34169
    78
wenzelm@48864
    79
    val opt_files =
wenzelm@58908
    80
      $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
wenzelm@48864
    81
      success(Nil)
wenzelm@48864
    82
    val keyword_spec =
wenzelm@48864
    83
      atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
wenzelm@48864
    84
      { case x ~ y ~ z => ((x, y), z) }
wenzelm@48864
    85
wenzelm@46938
    86
    val keyword_decl =
wenzelm@50128
    87
      rep1(string) ~
wenzelm@58908
    88
      opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~
wenzelm@58908
    89
      opt($$$("==") ~! name ^^ { case _ ~ x => x }) ^^
wenzelm@50128
    90
      { case xs ~ y ~ z => xs.map((_, y, z)) }
wenzelm@46939
    91
    val keyword_decls =
wenzelm@58908
    92
      keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
wenzelm@46939
    93
      { case xs ~ yss => (xs :: yss).flatten }
wenzelm@46938
    94
wenzelm@34169
    95
    val file =
wenzelm@58908
    96
      $$$("(") ~! (file_name ~ $$$(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
wenzelm@44185
    97
      file_name ^^ (x => (x, true))
wenzelm@34169
    98
wenzelm@34169
    99
    val args =
wenzelm@46938
   100
      theory_name ~
wenzelm@58908
   101
      (opt($$$(IMPORTS) ~! (rep1(theory_xname))) ^^
wenzelm@56823
   102
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   103
      (opt($$$(KEYWORDS) ~! keyword_decls) ^^
wenzelm@56823
   104
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
   105
      $$$(BEGIN) ^^
wenzelm@51294
   106
      { case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) }
wenzelm@34169
   107
wenzelm@58868
   108
    val heading =
wenzelm@58907
   109
      (command(HEADER) |
wenzelm@58907
   110
        command(CHAPTER) |
wenzelm@58907
   111
        command(SECTION) |
wenzelm@58907
   112
        command(SUBSECTION) |
wenzelm@58907
   113
        command(SUBSUBSECTION)) ~
wenzelm@58868
   114
      tags ~! document_source
wenzelm@58868
   115
wenzelm@58907
   116
    (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
   117
  }
wenzelm@34169
   118
wenzelm@34169
   119
wenzelm@34190
   120
  /* read -- lazy scanning */
wenzelm@34169
   121
wenzelm@44159
   122
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
   123
  {
wenzelm@58928
   124
    val token = Token.Parsers.token(bootstrap_keywords)
wenzelm@36956
   125
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
   126
wenzelm@43611
   127
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
   128
    {
wenzelm@34169
   129
      token(in) match {
wenzelm@55494
   130
        case Token.Parsers.Success(tok, rest) =>
wenzelm@34169
   131
          toks += tok
wenzelm@43611
   132
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
   133
        case _ =>
wenzelm@34169
   134
      }
wenzelm@34169
   135
    }
wenzelm@43611
   136
    scan_to_begin(reader)
wenzelm@34190
   137
wenzelm@36956
   138
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
   139
      case Success(result, _) => result
wenzelm@34190
   140
      case bad => error(bad.toString)
wenzelm@34190
   141
    }
wenzelm@34169
   142
  }
wenzelm@43611
   143
wenzelm@44159
   144
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
   145
    read(new CharSequenceReader(source))
wenzelm@34169
   146
}
wenzelm@44159
   147
wenzelm@44159
   148
wenzelm@44185
   149
sealed case class Thy_Header(
wenzelm@50128
   150
  name: String,
wenzelm@50128
   151
  imports: List[String],
wenzelm@51294
   152
  keywords: Thy_Header.Keywords)
wenzelm@44159
   153
{
wenzelm@44159
   154
  def map(f: String => String): Thy_Header =
wenzelm@51294
   155
    Thy_Header(f(name), imports.map(f), keywords)
wenzelm@56823
   156
wenzelm@56823
   157
  def decode_symbols: Thy_Header =
wenzelm@56823
   158
  {
wenzelm@56823
   159
    val f = Symbol.decode _
wenzelm@56823
   160
    Thy_Header(f(name), imports.map(f),
wenzelm@56823
   161
      keywords.map({ case (a, b, c) =>
wenzelm@58899
   162
        (f(a), b.map({ case ((x, y), z) => ((f(x), y.map(f)), z.map(f)) }), c.map(f)) }))
wenzelm@56823
   163
  }
wenzelm@44159
   164
}
wenzelm@44159
   165