src/Pure/Thy/thy_header.scala
author wenzelm
Thu Nov 02 10:16:22 2017 +0100 (19 months ago)
changeset 66987 352b23c97ac8
parent 66984 a1d3e5df0c95
child 67004 af72fa58f71b
permissions -rw-r--r--
support focus_session, for much faster startup of Isabelle/jEdit;
more options for "isabelle jedit";
     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_roots = List("ROOT0.ML" -> "ML_Root0", "ROOT.ML" -> "ML_Root")
    77   val bootstrap_thys = List(PURE, ML_BOOTSTRAP).map(a => a -> ("Bootstrap_" + a))
    78 
    79   val bootstrap_global_theories = (ml_roots ::: bootstrap_thys).map(p => (p._2 -> PURE))
    80 
    81   private val Thy_File_Name = new Regex(""".*?([^/\\:]+)\.thy""")
    82   private val Import_Name = new Regex(""".*?([^/\\:]+)""")
    83 
    84   def is_base_name(s: String): Boolean =
    85     s != "" && !s.exists("/\\:".contains(_))
    86 
    87   def import_name(s: String): String =
    88     s match { case Import_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
    89 
    90   def theory_name(s: String): String =
    91     s match {
    92       case Thy_File_Name(name) => bootstrap_name(name)
    93       case Import_Name(name) =>
    94         ml_roots.collectFirst({ case (a, b) if a == name => b }).getOrElse("")
    95       case _ => ""
    96     }
    97 
    98   def is_ml_root(theory: String): Boolean =
    99     ml_roots.exists({ case (_, b) => b == theory })
   100 
   101   def is_bootstrap(theory: String): Boolean =
   102     bootstrap_thys.exists({ case (_, b) => b == theory })
   103 
   104   def bootstrap_name(theory: String): String =
   105     bootstrap_thys.collectFirst({ case (a, b) if a == theory => b }).getOrElse(theory)
   106 
   107 
   108   /* header */
   109 
   110   val header: Parser[Thy_Header] =
   111   {
   112     val opt_files =
   113       $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
   114       success(Nil)
   115 
   116     val keyword_spec =
   117       atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
   118       { case x ~ y ~ z => Keyword.Spec(x, y, z) }
   119 
   120     val keyword_decl =
   121       rep1(string) ~
   122       opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^
   123       { case xs ~ y => xs.map((_, y.getOrElse(Keyword.Spec.none))) }
   124 
   125     val keyword_decls =
   126       keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
   127       { case xs ~ yss => (xs :: yss).flatten }
   128 
   129     val abbrevs =
   130       rep1(text ~ ($$$("=") ~! text) ^^ { case a ~ (_ ~ b) => (a, b) })
   131 
   132     val args =
   133       position(theory_name) ~
   134       (opt($$$(IMPORTS) ~! rep1(position(theory_name))) ^^
   135         { case None => Nil case Some(_ ~ xs) => xs }) ~
   136       (opt($$$(KEYWORDS) ~! keyword_decls) ^^
   137         { case None => Nil case Some(_ ~ xs) => xs }) ~
   138       (opt($$$(ABBREVS) ~! abbrevs) ^^
   139         { case None => Nil case Some(_ ~ xs) => xs }) ~
   140       $$$(BEGIN) ^^
   141       { case (name, pos) ~ imports ~ keywords ~ abbrevs ~ _ =>
   142           val f = Symbol.decode _
   143           Thy_Header((f(name), pos),
   144             imports.map({ case (a, b) => (f(a), b) }),
   145             keywords.map({ case (a, Keyword.Spec(b, c, d)) =>
   146               (f(a), Keyword.Spec(f(b), c.map(f), d.map(f))) }),
   147             abbrevs.map({ case (a, b) => (f(a), f(b)) }))
   148       }
   149 
   150     val heading =
   151       (command(CHAPTER) |
   152         command(SECTION) |
   153         command(SUBSECTION) |
   154         command(SUBSUBSECTION) |
   155         command(PARAGRAPH) |
   156         command(SUBPARAGRAPH) |
   157         command(TEXT) |
   158         command(TXT) |
   159         command(TEXT_RAW)) ~
   160       tags ~! document_source
   161 
   162     (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
   163   }
   164 
   165 
   166   /* read -- lazy scanning */
   167 
   168   private def read_tokens(reader: Reader[Char], strict: Boolean): (List[Token], List[Token]) =
   169   {
   170     val token = Token.Parsers.token(bootstrap_keywords)
   171     def make_tokens(in: Reader[Char]): Stream[Token] =
   172       token(in) match {
   173         case Token.Parsers.Success(tok, rest) => tok #:: make_tokens(rest)
   174         case _ => Stream.empty
   175       }
   176 
   177     val all_tokens = make_tokens(reader)
   178     val drop_tokens =
   179       if (strict) Nil
   180       else all_tokens.takeWhile(tok => !tok.is_command(Thy_Header.THEORY)).toList
   181 
   182     val tokens = all_tokens.drop(drop_tokens.length)
   183     val tokens1 = tokens.takeWhile(tok => !tok.is_begin).toList
   184     val tokens2 = tokens.dropWhile(tok => !tok.is_begin).headOption.toList
   185 
   186     (drop_tokens, tokens1 ::: tokens2)
   187   }
   188 
   189   def read(reader: Reader[Char], start: Token.Pos, strict: Boolean = true): Thy_Header =
   190   {
   191     val (_, tokens0) = read_tokens(reader, true)
   192     val text = Scan.reader_decode_utf8(reader, Token.implode(tokens0))
   193 
   194     val (drop_tokens, tokens) = read_tokens(Scan.char_reader(text), strict)
   195     val pos = (start /: drop_tokens)(_.advance(_))
   196 
   197     parse(commit(header), Token.reader(tokens, pos)) match {
   198       case Success(result, _) => result
   199       case bad => error(bad.toString)
   200     }
   201   }
   202 }
   203 
   204 sealed case class Thy_Header(
   205   name: (String, Position.T),
   206   imports: List[(String, Position.T)],
   207   keywords: Thy_Header.Keywords,
   208   abbrevs: Thy_Header.Abbrevs)