src/Pure/Thy/thy_header.scala
author wenzelm
Mon Jul 04 22:11:32 2011 +0200 (2011-07-04)
changeset 43661 39fdbd814c7f
parent 43652 dcd0b667f73d
child 43672 e9f26e66692d
permissions -rw-r--r--
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
     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   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   /* header */
    53 
    54   val header: Parser[Header] =
    55   {
    56     val file_name = atom("file name", _.is_name)
    57     val theory_name = atom("theory name", _.is_name)
    58 
    59     val file =
    60       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
    61 
    62     val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
    63 
    64     val args =
    65       theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
    66         { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
    67 
    68     (keyword(HEADER) ~ tags) ~!
    69       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    70     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    71   }
    72 
    73 
    74   /* read -- lazy scanning */
    75 
    76   def read(reader: Reader[Char]): Header =
    77   {
    78     val token = lexicon.token(Isabelle_System.symbols, _ => false)
    79     val toks = new mutable.ListBuffer[Token]
    80 
    81     @tailrec def scan_to_begin(in: Reader[Char])
    82     {
    83       token(in) match {
    84         case lexicon.Success(tok, rest) =>
    85           toks += tok
    86           if (!tok.is_begin) scan_to_begin(rest)
    87         case _ =>
    88       }
    89     }
    90     scan_to_begin(reader)
    91 
    92     parse(commit(header), Token.reader(toks.toList)) match {
    93       case Success(result, _) => result
    94       case bad => error(bad.toString)
    95     }
    96   }
    97 
    98   def read(source: CharSequence): Header =
    99     read(new CharSequenceReader(source))
   100 
   101   def read(file: File): Header =
   102   {
   103     val reader = Scan.byte_reader(file)
   104     try { read(reader).decode_permissive_utf8 }
   105     finally { reader.close }
   106   }
   107 
   108 
   109   /* check */
   110 
   111   def check(name: String, source: CharSequence): Header =
   112   {
   113     val header = read(source)
   114     val name1 = header.name
   115     if (name == name1) header
   116     else error("Bad file name " + thy_path(name) + " for theory " + quote(name1))
   117   }
   118 }