src/Pure/Thy/thy_header.scala
author wenzelm
Wed Feb 29 23:09:06 2012 +0100 (2012-02-29)
changeset 46737 09ab89658a5d
parent 46676 b4bc54d4624b
child 46938 cda018294515
permissions -rw-r--r--
clarified module Thy_Load;
more precise graph based on Document.Node.Deps with actual Node.Name dependencies;
     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   private 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 
    41   /* header */
    42 
    43   val header: Parser[Thy_Header] =
    44   {
    45     val file_name = atom("file name", _.is_name)
    46     val theory_name = atom("theory name", _.is_name)
    47 
    48     val file =
    49       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
    50       file_name ^^ (x => (x, true))
    51 
    52     val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
    53 
    54     val args =
    55       theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
    56         { case x ~ (_ ~ (ys ~ zs ~ _)) => Thy_Header(x, ys, zs) }
    57 
    58     (keyword(HEADER) ~ tags) ~!
    59       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    60     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    61   }
    62 
    63 
    64   /* read -- lazy scanning */
    65 
    66   def read(reader: Reader[Char]): Thy_Header =
    67   {
    68     val token = lexicon.token(_ => false)
    69     val toks = new mutable.ListBuffer[Token]
    70 
    71     @tailrec def scan_to_begin(in: Reader[Char])
    72     {
    73       token(in) match {
    74         case lexicon.Success(tok, rest) =>
    75           toks += tok
    76           if (!tok.is_begin) scan_to_begin(rest)
    77         case _ =>
    78       }
    79     }
    80     scan_to_begin(reader)
    81 
    82     parse(commit(header), Token.reader(toks.toList)) match {
    83       case Success(result, _) => result
    84       case bad => error(bad.toString)
    85     }
    86   }
    87 
    88   def read(source: CharSequence): Thy_Header =
    89     read(new CharSequenceReader(source))
    90 
    91   def read(file: File): Thy_Header =
    92   {
    93     val reader = Scan.byte_reader(file)
    94     try { read(reader).map(Standard_System.decode_permissive_utf8) }
    95     finally { reader.close }
    96   }
    97 }
    98 
    99 
   100 sealed case class Thy_Header(
   101   val name: String, val imports: List[String], val uses: List[(String, Boolean)])
   102 {
   103   def map(f: String => String): Thy_Header =
   104     Thy_Header(f(name), imports.map(f), uses.map(p => (f(p._1), p._2)))
   105 }
   106