src/Pure/Thy/thy_header.scala
author wenzelm
Sat Aug 13 20:20:36 2011 +0200 (2011-08-13)
changeset 44185 05641edb5d30
parent 44163 32e0c150c010
child 44222 9d5ef6cd4ee1
permissions -rw-r--r--
provide node header via Scala layer;
clarified node edit Clear: retain header information;
run_command: node info via document model, error handling within transaction;
node names without ".thy" suffix, to coincide with traditional theory loader treatment;
     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, String)] =
    34     s match { case Thy_Name(prefix, name) => Some((prefix, 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, false) } |
    48       file_name ^^ (x => (x, true))
    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(
   111   val name: String, val imports: List[String], val uses: List[(String, Boolean)])
   112 {
   113   def map(f: String => String): Thy_Header =
   114     Thy_Header(f(name), imports.map(f), uses.map(p => (f(p._1), p._2)))
   115 
   116   def norm_deps(f: String => String): Thy_Header =
   117     copy(imports = imports.map(name => f(name)), uses = uses.map(p => (f(p._1), p._2)))
   118 }
   119