src/Pure/Thy/thy_header.scala
author wenzelm
Wed Nov 05 22:17:05 2014 +0100 (2014-11-05)
changeset 58908 58bedbc18915
parent 58907 0ee3563803c9
child 58928 23d0ffd48006
permissions -rw-r--r--
tuned signature;
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@28495
    18
  val HEADER = "header"
wenzelm@58868
    19
  val CHAPTER = "chapter"
wenzelm@58868
    20
  val SECTION = "section"
wenzelm@58868
    21
  val SUBSECTION = "subsection"
wenzelm@58868
    22
  val SUBSUBSECTION = "subsubsection"
wenzelm@58868
    23
wenzelm@28495
    24
  val THEORY = "theory"
wenzelm@28495
    25
  val IMPORTS = "imports"
wenzelm@46938
    26
  val KEYWORDS = "keywords"
wenzelm@46938
    27
  val AND = "and"
wenzelm@28495
    28
  val BEGIN = "begin"
wenzelm@28495
    29
wenzelm@58903
    30
  private val header_keywords =
wenzelm@58907
    31
    Keyword.Keywords.empty +
wenzelm@58907
    32
      "%" + "(" + ")" + "," + "::" + "==" + AND + BEGIN + IMPORTS + KEYWORDS +
wenzelm@58907
    33
      (HEADER, Keyword.HEADING) +
wenzelm@58907
    34
      (CHAPTER, Keyword.HEADING) +
wenzelm@58907
    35
      (SECTION, Keyword.HEADING) +
wenzelm@58907
    36
      (SUBSECTION, Keyword.HEADING) +
wenzelm@58907
    37
      (SUBSUBSECTION, Keyword.HEADING) +
wenzelm@58907
    38
      (THEORY, Keyword.THY_BEGIN)
wenzelm@34190
    39
wenzelm@38149
    40
wenzelm@44160
    41
  /* theory file name */
wenzelm@44160
    42
wenzelm@44225
    43
  private val Base_Name = new Regex(""".*?([^/\\:]+)""")
wenzelm@44222
    44
  private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
wenzelm@44160
    45
wenzelm@44225
    46
  def base_name(s: String): String =
wenzelm@44225
    47
    s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
wenzelm@44225
    48
wenzelm@44222
    49
  def thy_name(s: String): Option[String] =
wenzelm@44222
    50
    s match { case Thy_Name(name) => Some(name) case _ => None }
wenzelm@38149
    51
wenzelm@34169
    52
wenzelm@34169
    53
  /* header */
wenzelm@34169
    54
wenzelm@44159
    55
  val header: Parser[Thy_Header] =
wenzelm@34169
    56
  {
wenzelm@43611
    57
    val file_name = atom("file name", _.is_name)
wenzelm@34169
    58
wenzelm@48864
    59
    val opt_files =
wenzelm@58908
    60
      $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
wenzelm@48864
    61
      success(Nil)
wenzelm@48864
    62
    val keyword_spec =
wenzelm@48864
    63
      atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
wenzelm@48864
    64
      { case x ~ y ~ z => ((x, y), z) }
wenzelm@48864
    65
wenzelm@46938
    66
    val keyword_decl =
wenzelm@50128
    67
      rep1(string) ~
wenzelm@58908
    68
      opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~
wenzelm@58908
    69
      opt($$$("==") ~! name ^^ { case _ ~ x => x }) ^^
wenzelm@50128
    70
      { case xs ~ y ~ z => xs.map((_, y, z)) }
wenzelm@46939
    71
    val keyword_decls =
wenzelm@58908
    72
      keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
wenzelm@46939
    73
      { case xs ~ yss => (xs :: yss).flatten }
wenzelm@46938
    74
wenzelm@34169
    75
    val file =
wenzelm@58908
    76
      $$$("(") ~! (file_name ~ $$$(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
wenzelm@44185
    77
      file_name ^^ (x => (x, true))
wenzelm@34169
    78
wenzelm@34169
    79
    val args =
wenzelm@46938
    80
      theory_name ~
wenzelm@58908
    81
      (opt($$$(IMPORTS) ~! (rep1(theory_xname))) ^^
wenzelm@56823
    82
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
    83
      (opt($$$(KEYWORDS) ~! keyword_decls) ^^
wenzelm@56823
    84
        { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@58908
    85
      $$$(BEGIN) ^^
wenzelm@51294
    86
      { case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) }
wenzelm@34169
    87
wenzelm@58868
    88
    val heading =
wenzelm@58907
    89
      (command(HEADER) |
wenzelm@58907
    90
        command(CHAPTER) |
wenzelm@58907
    91
        command(SECTION) |
wenzelm@58907
    92
        command(SUBSECTION) |
wenzelm@58907
    93
        command(SUBSUBSECTION)) ~
wenzelm@58868
    94
      tags ~! document_source
wenzelm@58868
    95
wenzelm@58907
    96
    (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    97
  }
wenzelm@34169
    98
wenzelm@34169
    99
wenzelm@34190
   100
  /* read -- lazy scanning */
wenzelm@34169
   101
wenzelm@44159
   102
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
   103
  {
wenzelm@58903
   104
    val token = Token.Parsers.token(header_keywords)
wenzelm@36956
   105
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
   106
wenzelm@43611
   107
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
   108
    {
wenzelm@34169
   109
      token(in) match {
wenzelm@55494
   110
        case Token.Parsers.Success(tok, rest) =>
wenzelm@34169
   111
          toks += tok
wenzelm@43611
   112
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
   113
        case _ =>
wenzelm@34169
   114
      }
wenzelm@34169
   115
    }
wenzelm@43611
   116
    scan_to_begin(reader)
wenzelm@34190
   117
wenzelm@36956
   118
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
   119
      case Success(result, _) => result
wenzelm@34190
   120
      case bad => error(bad.toString)
wenzelm@34190
   121
    }
wenzelm@34169
   122
  }
wenzelm@43611
   123
wenzelm@44159
   124
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
   125
    read(new CharSequenceReader(source))
wenzelm@43646
   126
wenzelm@48706
   127
wenzelm@48706
   128
  /* keywords */
wenzelm@48706
   129
wenzelm@58900
   130
  type Keywords = List[(String, Option[Keyword.Spec], Option[String])]
wenzelm@34169
   131
}
wenzelm@44159
   132
wenzelm@44159
   133
wenzelm@44185
   134
sealed case class Thy_Header(
wenzelm@50128
   135
  name: String,
wenzelm@50128
   136
  imports: List[String],
wenzelm@51294
   137
  keywords: Thy_Header.Keywords)
wenzelm@44159
   138
{
wenzelm@44159
   139
  def map(f: String => String): Thy_Header =
wenzelm@51294
   140
    Thy_Header(f(name), imports.map(f), keywords)
wenzelm@56823
   141
wenzelm@56823
   142
  def decode_symbols: Thy_Header =
wenzelm@56823
   143
  {
wenzelm@56823
   144
    val f = Symbol.decode _
wenzelm@56823
   145
    Thy_Header(f(name), imports.map(f),
wenzelm@56823
   146
      keywords.map({ case (a, b, c) =>
wenzelm@58899
   147
        (f(a), b.map({ case ((x, y), z) => ((f(x), y.map(f)), z.map(f)) }), c.map(f)) }))
wenzelm@56823
   148
  }
wenzelm@44159
   149
}
wenzelm@44159
   150