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