src/Pure/Thy/thy_header.scala
author wenzelm
Sat Nov 01 15:01:41 2014 +0100 (2014-11-01)
changeset 58861 5ff61774df11
parent 56823 37be55461dbe
child 58868 c5e1cce7ace3
permissions -rw-r--r--
command-line terminator ";" is no longer accepted;
     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_xname))) ^^
    71         { case None => Nil case Some(_ ~ xs) => xs }) ~
    72       (opt(keyword(KEYWORDS) ~! keyword_decls) ^^
    73         { case None => Nil case Some(_ ~ xs) => xs }) ~
    74       keyword(BEGIN) ^^
    75       { case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) }
    76 
    77     (keyword(HEADER) ~ tags) ~!
    78       ((document_source ~ keyword(THEORY) ~ tags) ~> args) ^^ { case _ ~ x => x } |
    79     (keyword(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
    80   }
    81 
    82 
    83   /* read -- lazy scanning */
    84 
    85   def read(reader: Reader[Char]): Thy_Header =
    86   {
    87     val token = Token.Parsers.token(lexicon, _ => false)
    88     val toks = new mutable.ListBuffer[Token]
    89 
    90     @tailrec def scan_to_begin(in: Reader[Char])
    91     {
    92       token(in) match {
    93         case Token.Parsers.Success(tok, rest) =>
    94           toks += tok
    95           if (!tok.is_begin) scan_to_begin(rest)
    96         case _ =>
    97       }
    98     }
    99     scan_to_begin(reader)
   100 
   101     parse(commit(header), Token.reader(toks.toList)) match {
   102       case Success(result, _) => result
   103       case bad => error(bad.toString)
   104     }
   105   }
   106 
   107   def read(source: CharSequence): Thy_Header =
   108     read(new CharSequenceReader(source))
   109 
   110 
   111   /* keywords */
   112 
   113   type Keywords = List[(String, Option[((String, List[String]), List[String])], Option[String])]
   114 }
   115 
   116 
   117 sealed case class Thy_Header(
   118   name: String,
   119   imports: List[String],
   120   keywords: Thy_Header.Keywords)
   121 {
   122   def map(f: String => String): Thy_Header =
   123     Thy_Header(f(name), imports.map(f), keywords)
   124 
   125   def decode_symbols: Thy_Header =
   126   {
   127     val f = Symbol.decode _
   128     Thy_Header(f(name), imports.map(f),
   129       keywords.map({ case (a, b, c) =>
   130         (f(a), b.map({ case ((x, y), z) => ((f(x), y.map(f)), z.map(f))  }), c.map(f)) }))
   131   }
   132 }
   133