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