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