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