src/Pure/Thy/thy_header.scala
author wenzelm
Tue Aug 07 13:21:29 2012 +0200 (2012-08-07)
changeset 48706 e2b512024eab
parent 48638 22d65e375c01
child 48864 3ee314ae1e0a
permissions -rw-r--r--
tuned signature;
     1 /*  Title:      Pure/Thy/thy_header.scala
     2     Author:     Makarius
     3 
     4 Static theory header information.
     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 => JFile}
    16 
    17 
    18 object Thy_Header extends Parse.Parser
    19 {
    20   val HEADER = "header"
    21   val THEORY = "theory"
    22   val IMPORTS = "imports"
    23   val KEYWORDS = "keywords"
    24   val AND = "and"
    25   val USES = "uses"
    26   val BEGIN = "begin"
    27 
    28   private val lexicon =
    29     Scan.Lexicon("%", "(", ")", "::", ";", AND, BEGIN, HEADER, IMPORTS, KEYWORDS, THEORY, USES)
    30 
    31 
    32   /* theory file name */
    33 
    34   private val Base_Name = new Regex(""".*?([^/\\:]+)""")
    35   private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
    36 
    37   def base_name(s: String): String =
    38     s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
    39 
    40   def thy_name(s: String): Option[String] =
    41     s match { case Thy_Name(name) => Some(name) case _ => None }
    42 
    43 
    44   /* header */
    45 
    46   val header: Parser[Thy_Header] =
    47   {
    48     val file_name = atom("file name", _.is_name)
    49 
    50     val keyword_kind =
    51       atom("outer syntax keyword kind", _.is_name) ~ tags ^^ { case x ~ y => (x, y) }
    52     val keyword_decl =
    53       rep1(string) ~ opt(keyword("::") ~! keyword_kind ^^ { case _ ~ x => x }) ^^
    54       { case xs ~ y => xs.map((_, y)) }
    55     val keyword_decls =
    56       keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
    57       { case xs ~ yss => (xs :: yss).flatten }
    58 
    59     val file =
    60       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
    61       file_name ^^ (x => (x, true))
    62 
    63     val args =
    64       theory_name ~
    65       (opt(keyword(IMPORTS) ~! (rep1(theory_name))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    66       (opt(keyword(KEYWORDS) ~! keyword_decls) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    67       (opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    68       keyword(BEGIN) ^^
    69       { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
    70 
    71     (keyword(HEADER) ~ tags) ~!
    72       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    73     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    74   }
    75 
    76 
    77   /* read -- lazy scanning */
    78 
    79   def read(reader: Reader[Char]): Thy_Header =
    80   {
    81     val token = lexicon.token(_ => false)
    82     val toks = new mutable.ListBuffer[Token]
    83 
    84     @tailrec def scan_to_begin(in: Reader[Char])
    85     {
    86       token(in) match {
    87         case lexicon.Success(tok, rest) =>
    88           toks += tok
    89           if (!tok.is_begin) scan_to_begin(rest)
    90         case _ =>
    91       }
    92     }
    93     scan_to_begin(reader)
    94 
    95     parse(commit(header), Token.reader(toks.toList)) match {
    96       case Success(result, _) => result
    97       case bad => error(bad.toString)
    98     }
    99   }
   100 
   101   def read(source: CharSequence): Thy_Header =
   102     read(new CharSequenceReader(source))
   103 
   104   def read(file: JFile): Thy_Header =
   105   {
   106     val reader = Scan.byte_reader(file)
   107     try { read(reader).map(Standard_System.decode_permissive_utf8) }
   108     finally { reader.close }
   109   }
   110 
   111 
   112   /* keywords */
   113 
   114   type Keywords = List[(String, Option[(String, List[String])])]
   115 }
   116 
   117 
   118 sealed case class Thy_Header(
   119   name: String, imports: List[String],
   120   keywords: Thy_Header.Keywords,
   121   uses: List[(String, Boolean)])
   122 {
   123   def map(f: String => String): Thy_Header =
   124     Thy_Header(f(name), imports.map(f), keywords, uses.map(p => (f(p._1), p._2)))
   125 }
   126