src/Pure/Thy/thy_header.scala
author wenzelm
Mon Aug 20 14:09:09 2012 +0200 (2012-08-20)
changeset 48864 3ee314ae1e0a
parent 48706 e2b512024eab
child 48882 61dc7d5d150a
permissions -rw-r--r--
added keyword kind "thy_load" (with optional list of file extensions);
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@48409
    15
import java.io.{File => JFile}
wenzelm@34188
    16
wenzelm@34169
    17
wenzelm@43661
    18
object Thy_Header extends Parse.Parser
wenzelm@32450
    19
{
wenzelm@28495
    20
  val HEADER = "header"
wenzelm@28495
    21
  val THEORY = "theory"
wenzelm@28495
    22
  val IMPORTS = "imports"
wenzelm@46938
    23
  val KEYWORDS = "keywords"
wenzelm@46938
    24
  val AND = "and"
wenzelm@28495
    25
  val USES = "uses"
wenzelm@28495
    26
  val BEGIN = "begin"
wenzelm@28495
    27
wenzelm@46938
    28
  private val lexicon =
wenzelm@48864
    29
    Scan.Lexicon("%", "(", ")", ",", "::", ";", AND, BEGIN, HEADER, IMPORTS, KEYWORDS, THEORY, USES)
wenzelm@34190
    30
wenzelm@38149
    31
wenzelm@44160
    32
  /* theory file name */
wenzelm@44160
    33
wenzelm@44225
    34
  private val Base_Name = new Regex(""".*?([^/\\:]+)""")
wenzelm@44222
    35
  private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
wenzelm@44160
    36
wenzelm@44225
    37
  def base_name(s: String): String =
wenzelm@44225
    38
    s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
wenzelm@44225
    39
wenzelm@44222
    40
  def thy_name(s: String): Option[String] =
wenzelm@44222
    41
    s match { case Thy_Name(name) => Some(name) case _ => None }
wenzelm@38149
    42
wenzelm@34169
    43
wenzelm@34169
    44
  /* header */
wenzelm@34169
    45
wenzelm@44159
    46
  val header: Parser[Thy_Header] =
wenzelm@34169
    47
  {
wenzelm@43611
    48
    val file_name = atom("file name", _.is_name)
wenzelm@34169
    49
wenzelm@48864
    50
    val opt_files =
wenzelm@48864
    51
      keyword("(") ~! (rep1sep(name, keyword(",")) <~ keyword(")")) ^^ { case _ ~ x => x } |
wenzelm@48864
    52
      success(Nil)
wenzelm@48864
    53
    val keyword_spec =
wenzelm@48864
    54
      atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
wenzelm@48864
    55
      { case x ~ y ~ z => ((x, y), z) }
wenzelm@48864
    56
wenzelm@46938
    57
    val keyword_decl =
wenzelm@48864
    58
      rep1(string) ~ opt(keyword("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^
wenzelm@46939
    59
      { case xs ~ y => xs.map((_, y)) }
wenzelm@46939
    60
    val keyword_decls =
wenzelm@46938
    61
      keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
wenzelm@46939
    62
      { case xs ~ yss => (xs :: yss).flatten }
wenzelm@46938
    63
wenzelm@34169
    64
    val file =
wenzelm@44185
    65
      keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
wenzelm@44185
    66
      file_name ^^ (x => (x, true))
wenzelm@34169
    67
wenzelm@34169
    68
    val args =
wenzelm@46938
    69
      theory_name ~
wenzelm@48638
    70
      (opt(keyword(IMPORTS) ~! (rep1(theory_name))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@46939
    71
      (opt(keyword(KEYWORDS) ~! keyword_decls) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@46938
    72
      (opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
wenzelm@46938
    73
      keyword(BEGIN) ^^
wenzelm@46938
    74
      { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
wenzelm@34169
    75
wenzelm@34169
    76
    (keyword(HEADER) ~ tags) ~!
wenzelm@34169
    77
      ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
wenzelm@34169
    78
    (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
wenzelm@34169
    79
  }
wenzelm@34169
    80
wenzelm@34169
    81
wenzelm@34190
    82
  /* read -- lazy scanning */
wenzelm@34169
    83
wenzelm@44159
    84
  def read(reader: Reader[Char]): Thy_Header =
wenzelm@34169
    85
  {
wenzelm@43695
    86
    val token = lexicon.token(_ => false)
wenzelm@36956
    87
    val toks = new mutable.ListBuffer[Token]
wenzelm@34188
    88
wenzelm@43611
    89
    @tailrec def scan_to_begin(in: Reader[Char])
wenzelm@34169
    90
    {
wenzelm@34169
    91
      token(in) match {
wenzelm@34169
    92
        case lexicon.Success(tok, rest) =>
wenzelm@34169
    93
          toks += tok
wenzelm@43611
    94
          if (!tok.is_begin) scan_to_begin(rest)
wenzelm@34169
    95
        case _ =>
wenzelm@34169
    96
      }
wenzelm@34169
    97
    }
wenzelm@43611
    98
    scan_to_begin(reader)
wenzelm@34190
    99
wenzelm@36956
   100
    parse(commit(header), Token.reader(toks.toList)) match {
wenzelm@34190
   101
      case Success(result, _) => result
wenzelm@34190
   102
      case bad => error(bad.toString)
wenzelm@34190
   103
    }
wenzelm@34169
   104
  }
wenzelm@43611
   105
wenzelm@44159
   106
  def read(source: CharSequence): Thy_Header =
wenzelm@43646
   107
    read(new CharSequenceReader(source))
wenzelm@43646
   108
wenzelm@48409
   109
  def read(file: JFile): Thy_Header =
wenzelm@43611
   110
  {
wenzelm@43611
   111
    val reader = Scan.byte_reader(file)
wenzelm@43699
   112
    try { read(reader).map(Standard_System.decode_permissive_utf8) }
wenzelm@43611
   113
    finally { reader.close }
wenzelm@43611
   114
  }
wenzelm@48706
   115
wenzelm@48706
   116
wenzelm@48706
   117
  /* keywords */
wenzelm@48706
   118
wenzelm@48864
   119
  type Keywords = List[(String, Option[((String, List[String]), List[String])])]
wenzelm@34169
   120
}
wenzelm@44159
   121
wenzelm@44159
   122
wenzelm@44185
   123
sealed case class Thy_Header(
wenzelm@46938
   124
  name: String, imports: List[String],
wenzelm@48706
   125
  keywords: Thy_Header.Keywords,
wenzelm@46938
   126
  uses: List[(String, Boolean)])
wenzelm@44159
   127
{
wenzelm@44159
   128
  def map(f: String => String): Thy_Header =
wenzelm@46938
   129
    Thy_Header(f(name), imports.map(f), keywords, uses.map(p => (f(p._1), p._2)))
wenzelm@44159
   130
}
wenzelm@44159
   131