src/Pure/Thy/thy_header.scala
author wenzelm
Sat Jul 02 23:31:07 2011 +0200 (2011-07-02)
changeset 43646 598b2c6ce13f
parent 43611 21a57a0c5f25
child 43648 e32de528b5ef
permissions -rw-r--r--
Thy_Header.read convenience;
     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   val Thy_Path1 = new Regex("([^/]*)\\.thy")
    40   val Thy_Path2 = new Regex("(.*)/([^/]*)\\.thy")
    41 
    42   def split_thy_path(path: String): Option[(String, String)] =
    43     path match {
    44       case Thy_Path1(name) => Some(("", name))
    45       case Thy_Path2(dir, name) => Some((dir, name))
    46       case _ => None
    47     }
    48 }
    49 
    50 
    51 class Thy_Header(symbols: Symbol.Interpretation) extends Parse.Parser
    52 {
    53   import Thy_Header._
    54 
    55 
    56   /* header */
    57 
    58   val header: Parser[Header] =
    59   {
    60     val file_name = atom("file name", _.is_name)
    61     val theory_name = atom("theory name", _.is_name)
    62 
    63     val file =
    64       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
    65 
    66     val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
    67 
    68     val args =
    69       theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
    70         { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
    71 
    72     (keyword(HEADER) ~ tags) ~!
    73       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    74     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    75   }
    76 
    77 
    78   /* read -- lazy scanning */
    79 
    80   def read(reader: Reader[Char]): Header =
    81   {
    82     val token = lexicon.token(symbols, _ => false)
    83     val toks = new mutable.ListBuffer[Token]
    84 
    85     @tailrec def scan_to_begin(in: Reader[Char])
    86     {
    87       token(in) match {
    88         case lexicon.Success(tok, rest) =>
    89           toks += tok
    90           if (!tok.is_begin) scan_to_begin(rest)
    91         case _ =>
    92       }
    93     }
    94     scan_to_begin(reader)
    95 
    96     parse(commit(header), Token.reader(toks.toList)) match {
    97       case Success(result, _) => result
    98       case bad => error(bad.toString)
    99     }
   100   }
   101 
   102   def read(source: CharSequence): Header =
   103     read(new CharSequenceReader(source))
   104 
   105   def read(file: File): Header =
   106   {
   107     val reader = Scan.byte_reader(file)
   108     try { read(reader).decode_permissive_utf8 }
   109     finally { reader.close }
   110   }
   111 }