src/Pure/Thy/thy_header.scala
author wenzelm
Sun Jul 10 00:21:19 2011 +0200 (2011-07-10)
changeset 43722 9b5dadb0c28d
parent 43699 fb3d99df4b1e
child 43723 8a63c95b1d5b
permissions -rw-r--r--
propagate header changes to prover process;
simplified Document case classes;
Document.State.assignments: indexed by Version_ID;
     1 /*  Title:      Pure/Thy/thy_header.scala
     2     Author:     Makarius
     3 
     4 Theory headers -- independent of outer syntax.
     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 USES = "uses"
    24   val BEGIN = "begin"
    25 
    26   val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES)
    27 
    28   sealed case class Header(val name: String, val imports: List[String], val uses: List[String])
    29   {
    30     def map(f: String => String): Header =
    31       Header(f(name), imports.map(f), uses.map(f))
    32   }
    33 
    34   def make_xml_data(header: Header): XML.Body =
    35     XML_Data.make_triple(XML_Data.make_string)(
    36       XML_Data.make_list(XML_Data.make_string))(
    37         XML_Data.make_list(XML_Data.make_string))(header.name, header.imports, header.uses)
    38 
    39 
    40   /* file name */
    41 
    42   def thy_path(name: String): Path = Path.basic(name).ext("thy")
    43 
    44   def split_thy_path(path: Path): (Path, String) =
    45     path.split_ext match {
    46       case (path1, "thy") => (path1.dir, path1.base.implode)
    47       case _ => error("Bad theory file specification: " + path)
    48     }
    49 
    50 
    51   /* header */
    52 
    53   val header: Parser[Header] =
    54   {
    55     val file_name = atom("file name", _.is_name)
    56     val theory_name = atom("theory name", _.is_name)
    57 
    58     val file =
    59       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
    60 
    61     val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
    62 
    63     val args =
    64       theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
    65         { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
    66 
    67     (keyword(HEADER) ~ tags) ~!
    68       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    69     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    70   }
    71 
    72 
    73   /* read -- lazy scanning */
    74 
    75   def read(reader: Reader[Char]): Header =
    76   {
    77     val token = lexicon.token(_ => false)
    78     val toks = new mutable.ListBuffer[Token]
    79 
    80     @tailrec def scan_to_begin(in: Reader[Char])
    81     {
    82       token(in) match {
    83         case lexicon.Success(tok, rest) =>
    84           toks += tok
    85           if (!tok.is_begin) scan_to_begin(rest)
    86         case _ =>
    87       }
    88     }
    89     scan_to_begin(reader)
    90 
    91     parse(commit(header), Token.reader(toks.toList)) match {
    92       case Success(result, _) => result
    93       case bad => error(bad.toString)
    94     }
    95   }
    96 
    97   def read(source: CharSequence): Header =
    98     read(new CharSequenceReader(source))
    99 
   100   def read(file: File): Header =
   101   {
   102     val reader = Scan.byte_reader(file)
   103     try { read(reader).map(Standard_System.decode_permissive_utf8) }
   104     finally { reader.close }
   105   }
   106 
   107 
   108   /* check */
   109 
   110   def check(name: String, source: CharSequence): Header =
   111   {
   112     val header = read(source)
   113     val name1 = header.name
   114     if (name != name1) error("Bad file name " + thy_path(name) + " for theory " + quote(name1))
   115     Path.explode(name)
   116     header
   117   }
   118 }