src/Pure/Thy/thy_header.scala
author wenzelm
Mon Nov 19 22:34:17 2012 +0100 (2012-11-19)
changeset 50128 599c935aac82
parent 48882 61dc7d5d150a
child 51293 05b1bbae748d
permissions -rw-r--r--
alternative completion for outer syntax keywords;
     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("%", "(", ")", ",", "::", ";", "==",
    30       AND, BEGIN, HEADER, IMPORTS, KEYWORDS, THEORY, USES)
    31 
    32 
    33   /* theory file name */
    34 
    35   private val Base_Name = new Regex(""".*?([^/\\:]+)""")
    36   private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
    37 
    38   def base_name(s: String): String =
    39     s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
    40 
    41   def thy_name(s: String): Option[String] =
    42     s match { case Thy_Name(name) => Some(name) case _ => None }
    43 
    44 
    45   /* header */
    46 
    47   val header: Parser[Thy_Header] =
    48   {
    49     val file_name = atom("file name", _.is_name)
    50 
    51     val opt_files =
    52       keyword("(") ~! (rep1sep(name, keyword(",")) <~ keyword(")")) ^^ { case _ ~ x => x } |
    53       success(Nil)
    54     val keyword_spec =
    55       atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
    56       { case x ~ y ~ z => ((x, y), z) }
    57 
    58     val keyword_decl =
    59       rep1(string) ~
    60       opt(keyword("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~
    61       opt(keyword("==") ~! name ^^ { case _ ~ x => x }) ^^
    62       { case xs ~ y ~ z => xs.map((_, y, z)) }
    63     val keyword_decls =
    64       keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
    65       { case xs ~ yss => (xs :: yss).flatten }
    66 
    67     val file =
    68       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
    69       file_name ^^ (x => (x, true))
    70 
    71     val args =
    72       theory_name ~
    73       (opt(keyword(IMPORTS) ~! (rep1(theory_name))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    74       (opt(keyword(KEYWORDS) ~! keyword_decls) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    75       (opt(keyword(USES) ~! (rep1(file))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    76       keyword(BEGIN) ^^
    77       { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
    78 
    79     (keyword(HEADER) ~ tags) ~!
    80       ((doc_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    81     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    82   }
    83 
    84 
    85   /* read -- lazy scanning */
    86 
    87   def read(reader: Reader[Char]): Thy_Header =
    88   {
    89     val token = lexicon.token(_ => false)
    90     val toks = new mutable.ListBuffer[Token]
    91 
    92     @tailrec def scan_to_begin(in: Reader[Char])
    93     {
    94       token(in) match {
    95         case lexicon.Success(tok, rest) =>
    96           toks += tok
    97           if (!tok.is_begin) scan_to_begin(rest)
    98         case _ =>
    99       }
   100     }
   101     scan_to_begin(reader)
   102 
   103     parse(commit(header), Token.reader(toks.toList)) match {
   104       case Success(result, _) => result
   105       case bad => error(bad.toString)
   106     }
   107   }
   108 
   109   def read(source: CharSequence): Thy_Header =
   110     read(new CharSequenceReader(source))
   111 
   112 
   113   /* keywords */
   114 
   115   type Keywords = List[(String, Option[((String, List[String]), List[String])], Option[String])]
   116 }
   117 
   118 
   119 sealed case class Thy_Header(
   120   name: String,
   121   imports: List[String],
   122   keywords: Thy_Header.Keywords,
   123   uses: List[(String, Boolean)])
   124 {
   125   def map(f: String => String): Thy_Header =
   126     Thy_Header(f(name), imports.map(f), keywords, uses.map(p => (f(p._1), p._2)))
   127 }
   128