src/Pure/Thy/thy_header.scala
author wenzelm
Sun Jul 10 20:59:04 2011 +0200 (2011-07-10)
changeset 43731 70072780e095
parent 43725 bebcfcad12d4
child 43767 e0219ef7f84c
permissions -rw-r--r--
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
tuned signature;
     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   val encode_xml_data: XML_Data.Encode.T[Header] =
    35   {
    36     case Header(name, imports, uses) =>
    37       import XML_Data.Encode._
    38       triple(string, list(string), list(string))(name, imports, uses)
    39   }
    40 
    41 
    42   /* file name */
    43 
    44   def thy_path(name: String): Path = Path.basic(name).ext("thy")
    45 
    46   def split_thy_path(path: Path): (Path, String) =
    47     path.split_ext match {
    48       case (path1, "thy") => (path1.dir, path1.base.implode)
    49       case _ => error("Bad theory file specification: " + path)
    50     }
    51 
    52 
    53   /* header */
    54 
    55   val header: Parser[Header] =
    56   {
    57     val file_name = atom("file name", _.is_name)
    58     val theory_name = atom("theory name", _.is_name)
    59 
    60     val file =
    61       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
    62 
    63     val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
    64 
    65     val args =
    66       theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
    67         { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
    68 
    69     (keyword(HEADER) ~ tags) ~!
    70       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    71     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    72   }
    73 
    74 
    75   /* read -- lazy scanning */
    76 
    77   def read(reader: Reader[Char]): Header =
    78   {
    79     val token = lexicon.token(_ => false)
    80     val toks = new mutable.ListBuffer[Token]
    81 
    82     @tailrec def scan_to_begin(in: Reader[Char])
    83     {
    84       token(in) match {
    85         case lexicon.Success(tok, rest) =>
    86           toks += tok
    87           if (!tok.is_begin) scan_to_begin(rest)
    88         case _ =>
    89       }
    90     }
    91     scan_to_begin(reader)
    92 
    93     parse(commit(header), Token.reader(toks.toList)) match {
    94       case Success(result, _) => result
    95       case bad => error(bad.toString)
    96     }
    97   }
    98 
    99   def read(source: CharSequence): Header =
   100     read(new CharSequenceReader(source))
   101 
   102   def read(file: File): Header =
   103   {
   104     val reader = Scan.byte_reader(file)
   105     try { read(reader).map(Standard_System.decode_permissive_utf8) }
   106     finally { reader.close }
   107   }
   108 
   109 
   110   /* check */
   111 
   112   def check(name: String, source: CharSequence): Header =
   113   {
   114     val header = read(source)
   115     val name1 = header.name
   116     if (name != name1) error("Bad file name " + thy_path(name) + " for theory " + quote(name1))
   117     Path.explode(name)
   118     header
   119   }
   120 }