src/Pure/Thy/thy_header.scala
author wenzelm
Wed Aug 22 15:53:17 2012 +0200 (2012-08-22)
changeset 48882 61dc7d5d150a
parent 48864 3ee314ae1e0a
child 50128 599c935aac82
permissions -rw-r--r--
use Thy_Header.read on explicit text only -- potentially via File.read, not Scan.byte_reader;
tuned signatures;
     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 => JFile}
    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 
    50     val opt_files =
    51       keyword("(") ~! (rep1sep(name, keyword(",")) <~ keyword(")")) ^^ { case _ ~ x => x } |
    52       success(Nil)
    53     val keyword_spec =
    54       atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
    55       { case x ~ y ~ z => ((x, y), z) }
    56 
    57     val keyword_decl =
    58       rep1(string) ~ opt(keyword("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^
    59       { case xs ~ y => xs.map((_, y)) }
    60     val keyword_decls =
    61       keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
    62       { case xs ~ yss => (xs :: yss).flatten }
    63 
    64     val file =
    65       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
    66       file_name ^^ (x => (x, true))
    67 
    68     val args =
    69       theory_name ~
    70       (opt(keyword(IMPORTS) ~! (rep1(theory_name))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    71       (opt(keyword(KEYWORDS) ~! keyword_decls) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    72       (opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    73       keyword(BEGIN) ^^
    74       { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
    75 
    76     (keyword(HEADER) ~ tags) ~!
    77       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    78     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    79   }
    80 
    81 
    82   /* read -- lazy scanning */
    83 
    84   def read(reader: Reader[Char]): Thy_Header =
    85   {
    86     val token = lexicon.token(_ => false)
    87     val toks = new mutable.ListBuffer[Token]
    88 
    89     @tailrec def scan_to_begin(in: Reader[Char])
    90     {
    91       token(in) match {
    92         case lexicon.Success(tok, rest) =>
    93           toks += tok
    94           if (!tok.is_begin) scan_to_begin(rest)
    95         case _ =>
    96       }
    97     }
    98     scan_to_begin(reader)
    99 
   100     parse(commit(header), Token.reader(toks.toList)) match {
   101       case Success(result, _) => result
   102       case bad => error(bad.toString)
   103     }
   104   }
   105 
   106   def read(source: CharSequence): Thy_Header =
   107     read(new CharSequenceReader(source))
   108 
   109 
   110   /* keywords */
   111 
   112   type Keywords = List[(String, Option[((String, List[String]), List[String])])]
   113 }
   114 
   115 
   116 sealed case class Thy_Header(
   117   name: String, imports: List[String],
   118   keywords: Thy_Header.Keywords,
   119   uses: List[(String, Boolean)])
   120 {
   121   def map(f: String => String): Thy_Header =
   122     Thy_Header(f(name), imports.map(f), keywords, uses.map(p => (f(p._1), p._2)))
   123 }
   124