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