src/Pure/Thy/thy_header.scala
author wenzelm
Thu Sep 23 18:44:26 2010 +0200 (2010-09-23)
changeset 39630 44181423183a
parent 38149 3c380380beac
child 41535 0112f14d75ec
permissions -rw-r--r--
explicit Session.Phase indication with associated event bus;
asynchronous Session.start();
synchronous Session.stop();
added Plugin.session_manager on top of basic Session;
eliminated separate isabelle.activate action;
misc tuning;
     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.collection.mutable
    11 import scala.util.parsing.input.{Reader, CharSequenceReader}
    12 import scala.util.matching.Regex
    13 
    14 import java.io.File
    15 
    16 
    17 object Thy_Header
    18 {
    19   val HEADER = "header"
    20   val THEORY = "theory"
    21   val IMPORTS = "imports"
    22   val USES = "uses"
    23   val BEGIN = "begin"
    24 
    25   val lexicon = Scan.Lexicon("%", "(", ")", ";", BEGIN, HEADER, IMPORTS, THEORY, USES)
    26 
    27   final case class Header(val name: String, val imports: List[String], val uses: List[String])
    28 
    29 
    30   /* file name */
    31 
    32   val Thy_Path1 = new Regex("([^/]*)\\.thy")
    33   val Thy_Path2 = new Regex("(.*/)([^/]*)\\.thy")
    34 
    35   def split_thy_path(path: String): Option[(String, String)] =
    36     path match {
    37       case Thy_Path1(name) => Some(("", name))
    38       case Thy_Path2(dir, name) => Some((dir, name))
    39       case _ => None
    40     }
    41 }
    42 
    43 
    44 class Thy_Header(symbols: Symbol.Interpretation) extends Parse.Parser
    45 {
    46   import Thy_Header._
    47 
    48 
    49   /* header */
    50 
    51   val header: Parser[Header] =
    52   {
    53     val file_name = atom("file name", _.is_name) ^^ Standard_System.decode_permissive_utf8
    54     val theory_name = atom("theory name", _.is_name) ^^ Standard_System.decode_permissive_utf8
    55 
    56     val file =
    57       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => x } | file_name
    58 
    59     val uses = opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }
    60 
    61     val args =
    62       theory_name ~ (keyword(IMPORTS) ~! (rep1(theory_name) ~ uses ~ keyword(BEGIN))) ^^
    63         { case x ~ (_ ~ (ys ~ zs ~ _)) => Header(x, ys, zs) }
    64 
    65     (keyword(HEADER) ~ tags) ~!
    66       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    67     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    68   }
    69 
    70 
    71   /* read -- lazy scanning */
    72 
    73   def read(file: File): Header =
    74   {
    75     val token = lexicon.token(symbols, _ => false)
    76     val toks = new mutable.ListBuffer[Token]
    77 
    78     def scan_to_begin(in: Reader[Char])
    79     {
    80       token(in) match {
    81         case lexicon.Success(tok, rest) =>
    82           toks += tok
    83           if (!(tok.kind == Token.Kind.KEYWORD && tok.content == BEGIN))
    84             scan_to_begin(rest)
    85         case _ =>
    86       }
    87     }
    88     val reader = Scan.byte_reader(file)
    89     try { scan_to_begin(reader) } finally { reader.close }
    90 
    91     parse(commit(header), Token.reader(toks.toList)) match {
    92       case Success(result, _) => result
    93       case bad => error(bad.toString)
    94     }
    95   }
    96 }