src/Pure/Thy/thy_header.scala
author wenzelm
Wed Feb 27 12:45:19 2013 +0100 (2013-02-27)
changeset 51293 05b1bbae748d
parent 50128 599c935aac82
child 51294 0850d43cb355
permissions -rw-r--r--
discontinued obsolete 'uses' within theory header;
     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 BEGIN = "begin"
    26 
    27   private val lexicon =
    28     Scan.Lexicon("%", "(", ")", ",", "::", ";", "==",
    29       AND, BEGIN, HEADER, IMPORTS, KEYWORDS, THEORY)
    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) ~
    59       opt(keyword("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~
    60       opt(keyword("==") ~! name ^^ { case _ ~ x => x }) ^^
    61       { case xs ~ y ~ z => xs.map((_, y, z)) }
    62     val keyword_decls =
    63       keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
    64       { case xs ~ yss => (xs :: yss).flatten }
    65 
    66     val file =
    67       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
    68       file_name ^^ (x => (x, true))
    69 
    70     val args =
    71       theory_name ~
    72       (opt(keyword(IMPORTS) ~! (rep1(theory_name))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    73       (opt(keyword(KEYWORDS) ~! keyword_decls) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    74       keyword(BEGIN) ^^
    75       { case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs, Nil) }
    76 
    77     (keyword(HEADER) ~ tags) ~!
    78       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    79     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    80   }
    81 
    82 
    83   /* read -- lazy scanning */
    84 
    85   def read(reader: Reader[Char]): Thy_Header =
    86   {
    87     val token = lexicon.token(_ => false)
    88     val toks = new mutable.ListBuffer[Token]
    89 
    90     @tailrec def scan_to_begin(in: Reader[Char])
    91     {
    92       token(in) match {
    93         case lexicon.Success(tok, rest) =>
    94           toks += tok
    95           if (!tok.is_begin) scan_to_begin(rest)
    96         case _ =>
    97       }
    98     }
    99     scan_to_begin(reader)
   100 
   101     parse(commit(header), Token.reader(toks.toList)) match {
   102       case Success(result, _) => result
   103       case bad => error(bad.toString)
   104     }
   105   }
   106 
   107   def read(source: CharSequence): Thy_Header =
   108     read(new CharSequenceReader(source))
   109 
   110 
   111   /* keywords */
   112 
   113   type Keywords = List[(String, Option[((String, List[String]), List[String])], Option[String])]
   114 }
   115 
   116 
   117 sealed case class Thy_Header(
   118   name: String,
   119   imports: List[String],
   120   keywords: Thy_Header.Keywords,
   121   files: List[String])
   122 {
   123   def map(f: String => String): Thy_Header =
   124     Thy_Header(f(name), imports.map(f), keywords, files.map(f))
   125 }
   126