src/Pure/Thy/thy_header.scala
author wenzelm
Sat Mar 14 19:51:36 2015 +0100 (2015-03-14)
changeset 59695 a03e0561bdbf
parent 59694 d2bb4b5ed862
child 59705 740a0ca7e09b
permissions -rw-r--r--
clarified positions of theory imports;
     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   /* bootstrap keywords */
    19 
    20   type Keywords = List[(String, Option[Keyword.Spec], Option[String])]
    21 
    22   val HEADER = "header"
    23   val CHAPTER = "chapter"
    24   val SECTION = "section"
    25   val SUBSECTION = "subsection"
    26   val SUBSUBSECTION = "subsubsection"
    27   val TEXT = "text"
    28   val TXT = "txt"
    29   val TEXT_RAW = "text_raw"
    30 
    31   val THEORY = "theory"
    32   val IMPORTS = "imports"
    33   val KEYWORDS = "keywords"
    34   val AND = "and"
    35   val BEGIN = "begin"
    36 
    37   private val bootstrap_header: Keywords =
    38     List(
    39       ("%", None, None),
    40       ("(", None, None),
    41       (")", None, None),
    42       (",", None, None),
    43       ("::", None, None),
    44       ("==", None, None),
    45       (AND, None, None),
    46       (BEGIN, None, None),
    47       (IMPORTS, None, None),
    48       (KEYWORDS, None, None),
    49       (HEADER, Some(((Keyword.DOCUMENT_HEADING, Nil), Nil)), None),
    50       (CHAPTER, Some(((Keyword.DOCUMENT_HEADING, Nil), Nil)), None),
    51       (SECTION, Some(((Keyword.DOCUMENT_HEADING, Nil), Nil)), None),
    52       (SUBSECTION, Some(((Keyword.DOCUMENT_HEADING, Nil), Nil)), None),
    53       (SUBSUBSECTION, Some(((Keyword.DOCUMENT_HEADING, Nil), Nil)), None),
    54       (TEXT, Some(((Keyword.DOCUMENT_BODY, Nil), Nil)), None),
    55       (TXT, Some(((Keyword.DOCUMENT_BODY, Nil), Nil)), None),
    56       (TEXT_RAW, Some(((Keyword.DOCUMENT_RAW, Nil), Nil)), None),
    57       (THEORY, Some((Keyword.THY_BEGIN, Nil), List("theory")), None),
    58       ("ML_file", Some((Keyword.THY_LOAD, Nil), List("ML")), None))
    59 
    60   private val bootstrap_keywords =
    61     Keyword.Keywords.empty.add_keywords(bootstrap_header)
    62 
    63   def bootstrap_syntax(): Outer_Syntax =
    64     Outer_Syntax.init().add_keywords(bootstrap_header)
    65 
    66 
    67   /* theory file name */
    68 
    69   private val Base_Name = new Regex(""".*?([^/\\:]+)""")
    70   private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
    71 
    72   def base_name(s: String): String =
    73     s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
    74 
    75   def thy_name(s: String): Option[String] =
    76     s match { case Thy_Name(name) => Some(name) case _ => None }
    77 
    78 
    79   /* header */
    80 
    81   val header: Parser[Thy_Header] =
    82   {
    83     val opt_files =
    84       $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
    85       success(Nil)
    86 
    87     val keyword_spec =
    88       atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
    89       { case x ~ y ~ z => ((x, y), z) }
    90 
    91     val keyword_decl =
    92       rep1(string) ~
    93       opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ~
    94       opt($$$("==") ~! name ^^ { case _ ~ x => x }) ^^
    95       { case xs ~ y ~ z => xs.map((_, y, z)) }
    96 
    97     val keyword_decls =
    98       keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
    99       { case xs ~ yss => (xs :: yss).flatten }
   100 
   101     val args =
   102       position(theory_name) ~
   103       (opt($$$(IMPORTS) ~! (rep1(position(theory_xname)))) ^^
   104         { case None => Nil case Some(_ ~ xs) => xs }) ~
   105       (opt($$$(KEYWORDS) ~! keyword_decls) ^^
   106         { case None => Nil case Some(_ ~ xs) => xs }) ~
   107       $$$(BEGIN) ^^
   108       { case x ~ ys ~ zs ~ _ => Thy_Header(x, ys, zs) }
   109 
   110     val heading =
   111       (command(HEADER) |
   112         command(CHAPTER) |
   113         command(SECTION) |
   114         command(SUBSECTION) |
   115         command(SUBSUBSECTION) |
   116         command(TEXT) |
   117         command(TXT) |
   118         command(TEXT_RAW)) ~
   119       tags ~! document_source
   120 
   121     (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
   122   }
   123 
   124 
   125   /* read -- lazy scanning */
   126 
   127   def read(reader: Reader[Char], file: String): Thy_Header =
   128   {
   129     val token = Token.Parsers.token(bootstrap_keywords)
   130     val toks = new mutable.ListBuffer[Token]
   131 
   132     @tailrec def scan_to_begin(in: Reader[Char])
   133     {
   134       token(in) match {
   135         case Token.Parsers.Success(tok, rest) =>
   136           toks += tok
   137           if (!tok.is_begin) scan_to_begin(rest)
   138         case _ =>
   139       }
   140     }
   141     scan_to_begin(reader)
   142 
   143     parse(commit(header), Token.reader(toks.toList, file)) match {
   144       case Success(result, _) => result
   145       case bad => error(bad.toString)
   146     }
   147   }
   148 
   149   def read(source: CharSequence, file: String): Thy_Header =
   150     read(new CharSequenceReader(source), file)
   151 }
   152 
   153 
   154 sealed case class Thy_Header(
   155   name: (String, Position.T),
   156   imports: List[(String, Position.T)],
   157   keywords: Thy_Header.Keywords)
   158 {
   159   def decode_symbols: Thy_Header =
   160   {
   161     val f = Symbol.decode _
   162     Thy_Header((f(name._1), name._2), imports.map({ case (a, b) => (f(a), b) }),
   163       keywords.map({ case (a, b, c) =>
   164         (f(a), b.map({ case ((x, y), z) => ((f(x), y.map(f)), z.map(f)) }), c.map(f)) }))
   165   }
   166 }