src/Pure/Thy/thy_header.scala
author wenzelm
Tue Apr 04 22:56:28 2017 +0200 (2017-04-04)
changeset 65384 36255c43c64c
parent 65362 908a27a4b9c9
child 65392 f365f61f2081
permissions -rw-r--r--
more explicit types;
     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
    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, Keyword.Spec)]
    21   type Abbrevs = List[(String, String)]
    22 
    23   val CHAPTER = "chapter"
    24   val SECTION = "section"
    25   val SUBSECTION = "subsection"
    26   val SUBSUBSECTION = "subsubsection"
    27   val PARAGRAPH = "paragraph"
    28   val SUBPARAGRAPH = "subparagraph"
    29   val TEXT = "text"
    30   val TXT = "txt"
    31   val TEXT_RAW = "text_raw"
    32 
    33   val THEORY = "theory"
    34   val IMPORTS = "imports"
    35   val KEYWORDS = "keywords"
    36   val ABBREVS = "abbrevs"
    37   val AND = "and"
    38   val BEGIN = "begin"
    39 
    40   val bootstrap_header: Keywords =
    41     List(
    42       ("%", Keyword.Spec.none),
    43       ("(", Keyword.Spec.none),
    44       (")", Keyword.Spec.none),
    45       (",", Keyword.Spec.none),
    46       ("::", Keyword.Spec.none),
    47       ("=", Keyword.Spec.none),
    48       (AND, Keyword.Spec.none),
    49       (BEGIN, Keyword.Spec(Keyword.QUASI_COMMAND)),
    50       (IMPORTS, Keyword.Spec(Keyword.QUASI_COMMAND)),
    51       (KEYWORDS, Keyword.Spec(Keyword.QUASI_COMMAND)),
    52       (ABBREVS, Keyword.Spec(Keyword.QUASI_COMMAND)),
    53       (CHAPTER, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
    54       (SECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
    55       (SUBSECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
    56       (SUBSUBSECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
    57       (PARAGRAPH, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
    58       (SUBPARAGRAPH, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
    59       (TEXT, Keyword.Spec(Keyword.DOCUMENT_BODY)),
    60       (TXT, Keyword.Spec(Keyword.DOCUMENT_BODY)),
    61       (TEXT_RAW, Keyword.Spec(Keyword.DOCUMENT_RAW)),
    62       (THEORY, Keyword.Spec(Keyword.THY_BEGIN, tags = List("theory"))),
    63       ("ML", Keyword.Spec(Keyword.THY_DECL, tags = List("ML"))))
    64 
    65   private val bootstrap_keywords =
    66     Keyword.Keywords.empty.add_keywords(bootstrap_header)
    67 
    68   lazy val bootstrap_syntax: Outer_Syntax =
    69     Outer_Syntax.init().add_keywords(bootstrap_header)
    70 
    71 
    72   /* file name vs. theory name */
    73 
    74   val PURE = "Pure"
    75   val ML_BOOTSTRAP = "ML_Bootstrap"
    76   val ML_ROOT = "ML_Root"
    77   val ml_roots = List("ROOT0.ML" -> "ML_Root0", "ROOT.ML" -> ML_ROOT)
    78   val bootstrap_thys = List(PURE, ML_BOOTSTRAP).map(a => a -> ("Bootstrap_" + a))
    79 
    80   private val Base_Name = new Regex(""".*?([^/\\:]+)""")
    81   private val Thy_Name = new Regex(""".*?([^/\\:]+)\.thy""")
    82 
    83   def is_base_name(s: String): Boolean =
    84     s != "" && !s.exists("/\\:".contains(_))
    85 
    86   def base_name(s: String): String =
    87     s match { case Base_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
    88 
    89   def thy_name(s: String): Option[String] =
    90     s match { case Thy_Name(name) => Some(name) case _ => None }
    91 
    92   def thy_name_bootstrap(s: String): Option[String] =
    93     s match {
    94       case Thy_Name(name) =>
    95         Some(bootstrap_thys.collectFirst({ case (a, b) if a == name => b }).getOrElse(name))
    96       case Base_Name(name) => ml_roots.collectFirst({ case (a, b) if a == name => b })
    97       case _ => None
    98     }
    99 
   100   def is_ml_root(theory: String): Boolean =
   101     ml_roots.exists({ case (_, b) => b == theory })
   102 
   103   def is_bootstrap(theory: String): Boolean =
   104     bootstrap_thys.exists({ case (_, b) => b == theory })
   105 
   106 
   107   /* header */
   108 
   109   val header: Parser[Thy_Header] =
   110   {
   111     val opt_files =
   112       $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
   113       success(Nil)
   114 
   115     val keyword_spec =
   116       atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
   117       { case x ~ y ~ z => Keyword.Spec(x, y, z) }
   118 
   119     val keyword_decl =
   120       rep1(string) ~
   121       opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^
   122       { case xs ~ y => xs.map((_, y.getOrElse(Keyword.Spec.none))) }
   123 
   124     val keyword_decls =
   125       keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
   126       { case xs ~ yss => (xs :: yss).flatten }
   127 
   128     val abbrevs =
   129       rep1(text ~ ($$$("=") ~! text) ^^ { case a ~ (_ ~ b) => (a, b) })
   130 
   131     val args =
   132       position(theory_name) ~
   133       (opt($$$(IMPORTS) ~! rep1(position(theory_name))) ^^
   134         { case None => Nil case Some(_ ~ xs) => xs }) ~
   135       (opt($$$(KEYWORDS) ~! keyword_decls) ^^
   136         { case None => Nil case Some(_ ~ xs) => xs }) ~
   137       (opt($$$(ABBREVS) ~! abbrevs) ^^
   138         { case None => Nil case Some(_ ~ xs) => xs }) ~
   139       $$$(BEGIN) ^^
   140       { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
   141 
   142     val heading =
   143       (command(CHAPTER) |
   144         command(SECTION) |
   145         command(SUBSECTION) |
   146         command(SUBSUBSECTION) |
   147         command(PARAGRAPH) |
   148         command(SUBPARAGRAPH) |
   149         command(TEXT) |
   150         command(TXT) |
   151         command(TEXT_RAW)) ~
   152       tags ~! document_source
   153 
   154     (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
   155   }
   156 
   157 
   158   /* read -- lazy scanning */
   159 
   160   def read(reader: Reader[Char], start: Token.Pos, strict: Boolean = true): Thy_Header =
   161   {
   162     val token = Token.Parsers.token(bootstrap_keywords)
   163     def make_tokens(in: Reader[Char]): Stream[Token] =
   164       token(in) match {
   165         case Token.Parsers.Success(tok, rest) => tok #:: make_tokens(rest)
   166         case _ => Stream.empty
   167       }
   168 
   169     val tokens =
   170       if (strict) make_tokens(reader)
   171       else make_tokens(reader).dropWhile(tok => !tok.is_command(Thy_Header.THEORY))
   172 
   173     val tokens1 = tokens.takeWhile(tok => !tok.is_begin).toList
   174     val tokens2 = tokens.dropWhile(tok => !tok.is_begin).headOption.toList
   175 
   176     parse(commit(header), Token.reader(tokens1 ::: tokens2, start)) match {
   177       case Success(result, _) => result
   178       case bad => error(bad.toString)
   179     }
   180   }
   181 }
   182 
   183 
   184 sealed case class Thy_Header(
   185   name: (String, Position.T),
   186   imports: List[(String, Position.T)],
   187   keywords: Thy_Header.Keywords,
   188   abbrevs: Thy_Header.Abbrevs)
   189 {
   190   def decode_symbols: Thy_Header =
   191   {
   192     val f = Symbol.decode _
   193     Thy_Header((f(name._1), name._2),
   194       imports.map({ case (a, b) => (f(a), b) }),
   195       keywords.map({ case (a, Keyword.Spec(b, c, d)) =>
   196         (f(a), Keyword.Spec(f(b), c.map(f), d.map(f))) }),
   197       abbrevs.map({ case (a, b) => (f(a), f(b)) }))
   198   }
   199 }