src/Pure/Thy/thy_header.scala
author wenzelm
Fri Feb 14 16:25:30 2014 +0100 (2014-02-14)
changeset 55494 009b71c1ed23
parent 55492 28d4db6c6e79
child 56801 8dd9df88f647
permissions -rw-r--r--
tuned signature (in accordance to ML version);
     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 
    16 object Thy_Header extends Parse.Parser
    17 {
    18   val HEADER = "header"
    19   val THEORY = "theory"
    20   val IMPORTS = "imports"
    21   val KEYWORDS = "keywords"
    22   val AND = "and"
    23   val BEGIN = "begin"
    24 
    25   private val lexicon =
    26     Scan.Lexicon("%", "(", ")", ",", "::", ";", "==",
    27       AND, BEGIN, HEADER, IMPORTS, KEYWORDS, THEORY)
    28 
    29 
    30   /* theory file name */
    31 
    32   private val Base_Name = new Regex(""".*?([^/\\:]+)""")
    33   private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
    34 
    35   def base_name(s: String): String =
    36     s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
    37 
    38   def thy_name(s: String): Option[String] =
    39     s match { case Thy_Name(name) => Some(name) case _ => None }
    40 
    41 
    42   /* header */
    43 
    44   val header: Parser[Thy_Header] =
    45   {
    46     val file_name = atom("file name", _.is_name)
    47 
    48     val opt_files =
    49       keyword("(") ~! (rep1sep(name, keyword(",")) <~ keyword(")")) ^^ { case _ ~ x => x } |
    50       success(Nil)
    51     val keyword_spec =
    52       atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
    53       { case x ~ y ~ z => ((x, y), z) }
    54 
    55     val keyword_decl =
    56       rep1(string) ~
    57       opt(keyword("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~
    58       opt(keyword("==") ~! name ^^ { case _ ~ x => x }) ^^
    59       { case xs ~ y ~ z => xs.map((_, y, z)) }
    60     val keyword_decls =
    61       keyword_decl ~ rep(keyword(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
    62       { case xs ~ yss => (xs :: yss).flatten }
    63 
    64     val file =
    65       keyword("(") ~! (file_name ~ keyword(")")) ^^ { case _ ~ (x ~ _) => (x, false) } |
    66       file_name ^^ (x => (x, true))
    67 
    68     val args =
    69       theory_name ~
    70       (opt(keyword(IMPORTS) ~! (rep1(theory_name))) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    71       (opt(keyword(KEYWORDS) ~! keyword_decls) ^^ { case None => Nil case Some(_ ~ xs) => xs }) ~
    72       keyword(BEGIN) ^^
    73       { case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) }
    74 
    75     (keyword(HEADER) ~ tags) ~!
    76       ((document_source ~ rep(keyword(";")) ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    77     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    78   }
    79 
    80 
    81   /* read -- lazy scanning */
    82 
    83   def read(reader: Reader[Char]): Thy_Header =
    84   {
    85     val token = Token.Parsers.token(lexicon, _ => false)
    86     val toks = new mutable.ListBuffer[Token]
    87 
    88     @tailrec def scan_to_begin(in: Reader[Char])
    89     {
    90       token(in) match {
    91         case Token.Parsers.Success(tok, rest) =>
    92           toks += tok
    93           if (!tok.is_begin) scan_to_begin(rest)
    94         case _ =>
    95       }
    96     }
    97     scan_to_begin(reader)
    98 
    99     parse(commit(header), Token.reader(toks.toList)) match {
   100       case Success(result, _) => result
   101       case bad => error(bad.toString)
   102     }
   103   }
   104 
   105   def read(source: CharSequence): Thy_Header =
   106     read(new CharSequenceReader(source))
   107 
   108 
   109   /* keywords */
   110 
   111   type Keywords = List[(String, Option[((String, List[String]), List[String])], Option[String])]
   112 }
   113 
   114 
   115 sealed case class Thy_Header(
   116   name: String,
   117   imports: List[String],
   118   keywords: Thy_Header.Keywords)
   119 {
   120   def map(f: String => String): Thy_Header =
   121     Thy_Header(f(name), imports.map(f), keywords)
   122 }
   123