src/Pure/Thy/thy_header.scala
author wenzelm
Thu Mar 15 10:16:21 2012 +0100 (2012-03-15)
changeset 46940 a40be2f10ca9
parent 46939 5b67ac48b384
child 46943 ac1c41ea856d
permissions -rw-r--r--
explicit Outer_Syntax.Decl;
     1 /*  Title:      Pure/Thy/thy_header.scala
     2     Author:     Makarius
     3 
     4 Static theory header information.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.annotation.tailrec
    11 import scala.collection.mutable
    12 import scala.util.parsing.input.{Reader, CharSequenceReader}
    13 import scala.util.matching.Regex
    14 
    15 import java.io.File
    16 
    17 
    18 object Thy_Header extends Parse.Parser
    19 {
    20   val HEADER = "header"
    21   val THEORY = "theory"
    22   val IMPORTS = "imports"
    23   val KEYWORDS = "keywords"
    24   val AND = "and"
    25   val USES = "uses"
    26   val BEGIN = "begin"
    27 
    28   private val lexicon =
    29     Scan.Lexicon("%", "(", ")", "::", ";", AND, BEGIN, HEADER, IMPORTS, KEYWORDS, THEORY, USES)
    30 
    31 
    32   /* theory file name */
    33 
    34   private val Base_Name = new Regex(""".*?([^/\\:]+)""")
    35   private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
    36 
    37   def base_name(s: String): String =
    38     s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
    39 
    40   def thy_name(s: String): Option[String] =
    41     s match { case Thy_Name(name) => Some(name) case _ => None }
    42 
    43 
    44   /* header */
    45 
    46   val header: Parser[Thy_Header] =
    47   {
    48     val file_name = atom("file name", _.is_name)
    49     val theory_name = atom("theory name", _.is_name)
    50 
    51     val keyword_kind =
    52       atom("outer syntax keyword kind", _.is_name) ~ tags ^^ { case x ~ y => (x, y) }
    53     val keyword_decl =
    54       rep1(name) ~ opt(keyword("::") ~! keyword_kind ^^ { case _ ~ x => x }) ^^
    55       { case xs ~ y => xs.map((_, y)) }
    56     val keyword_decls =
    57       keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
    58       { case xs ~ yss => (xs :: yss).flatten }
    59 
    60     val file =
    61       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
    62       file_name ^^ (x => (x, true))
    63 
    64     val args =
    65       theory_name ~
    66       (keyword(IMPORTS) ~! (rep1(theory_name)) ^^ { case _ ~ xs => xs }) ~
    67       (opt(keyword(KEYWORDS) ~! keyword_decls) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    68       (opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    69       keyword(BEGIN) ^^
    70       { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
    71 
    72     (keyword(HEADER) ~ tags) ~!
    73       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    74     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    75   }
    76 
    77 
    78   /* read -- lazy scanning */
    79 
    80   def read(reader: Reader[Char]): Thy_Header =
    81   {
    82     val token = lexicon.token(_ => false)
    83     val toks = new mutable.ListBuffer[Token]
    84 
    85     @tailrec def scan_to_begin(in: Reader[Char])
    86     {
    87       token(in) match {
    88         case lexicon.Success(tok, rest) =>
    89           toks += tok
    90           if (!tok.is_begin) scan_to_begin(rest)
    91         case _ =>
    92       }
    93     }
    94     scan_to_begin(reader)
    95 
    96     parse(commit(header), Token.reader(toks.toList)) match {
    97       case Success(result, _) => result
    98       case bad => error(bad.toString)
    99     }
   100   }
   101 
   102   def read(source: CharSequence): Thy_Header =
   103     read(new CharSequenceReader(source))
   104 
   105   def read(file: File): Thy_Header =
   106   {
   107     val reader = Scan.byte_reader(file)
   108     try { read(reader).map(Standard_System.decode_permissive_utf8) }
   109     finally { reader.close }
   110   }
   111 }
   112 
   113 
   114 sealed case class Thy_Header(
   115   name: String, imports: List[String],
   116   keywords: List[Outer_Syntax.Decl],
   117   uses: List[(String, Boolean)])
   118 {
   119   def map(f: String => String): Thy_Header =
   120     Thy_Header(f(name), imports.map(f), keywords, uses.map(p => (f(p._1), p._2)))
   121 }
   122