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