src/Pure/Thy/thy_header.scala
author wenzelm
Sun Jul 03 19:42:32 2011 +0200 (2011-07-03)
changeset 43648 e32de528b5ef
parent 43646 598b2c6ce13f
child 43652 dcd0b667f73d
permissions -rw-r--r--
more explicit edit_node vs. init_node;
some support for master_dir and header;
     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
    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   final case class Header(val name: String, val imports: List[String], val uses: List[String])
    29   {
    30     def decode_permissive_utf8: Header =
    31       Header(Standard_System.decode_permissive_utf8(name),
    32         imports.map(Standard_System.decode_permissive_utf8),
    33         uses.map(Standard_System.decode_permissive_utf8))
    34   }
    35 
    36 
    37   /* file name */
    38 
    39   def thy_path(name: String): Path = Path.basic(name).ext("thy")
    40 
    41   private val Thy_Path1 = new Regex("([^/]*)\\.thy")
    42   private val Thy_Path2 = new Regex("(.*)/([^/]*)\\.thy")
    43 
    44   def split_thy_path(path: String): Option[(String, String)] =
    45     path match {
    46       case Thy_Path1(name) => Some(("", name))
    47       case Thy_Path2(dir, name) => Some((dir, name))
    48       case _ => None
    49     }
    50 }
    51 
    52 
    53 class Thy_Header(symbols: Symbol.Interpretation) extends Parse.Parser
    54 {
    55   import Thy_Header._
    56 
    57 
    58   /* header */
    59 
    60   val header: Parser[Header] =
    61   {
    62     val file_name = atom("file name", _.is_name)
    63     val theory_name = atom("theory name", _.is_name)
    64 
    65     val file =
    66       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
    67 
    68     val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
    69 
    70     val args =
    71       theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
    72         { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
    73 
    74     (keyword(HEADER) ~ tags) ~!
    75       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    76     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    77   }
    78 
    79 
    80   /* read -- lazy scanning */
    81 
    82   def read(reader: Reader[Char]): Header =
    83   {
    84     val token = lexicon.token(symbols, _ => false)
    85     val toks = new mutable.ListBuffer[Token]
    86 
    87     @tailrec def scan_to_begin(in: Reader[Char])
    88     {
    89       token(in) match {
    90         case lexicon.Success(tok, rest) =>
    91           toks += tok
    92           if (!tok.is_begin) scan_to_begin(rest)
    93         case _ =>
    94       }
    95     }
    96     scan_to_begin(reader)
    97 
    98     parse(commit(header), Token.reader(toks.toList)) match {
    99       case Success(result, _) => result
   100       case bad => error(bad.toString)
   101     }
   102   }
   103 
   104   def read(source: CharSequence): Header =
   105     read(new CharSequenceReader(source))
   106 
   107   def read(file: File): Header =
   108   {
   109     val reader = Scan.byte_reader(file)
   110     try { read(reader).decode_permissive_utf8 }
   111     finally { reader.close }
   112   }
   113 
   114 
   115   /* check */
   116 
   117   def check(name: String, source: CharSequence): Header =
   118   {
   119     val header = read(source)
   120     val name1 = header.name
   121     if (name == name1) header
   122     else error("Bad file name " + Thy_Header.thy_path(name) + " for theory " + Library.quote(name1))
   123   }
   124 }