src/Pure/Thy/thy_header.scala
author wenzelm
Fri, 30 Dec 2022 20:26:28 +0100
changeset 76828 a5ff9cf61551
parent 75906 2167b9e3157a
child 76839 2121fde115b2
permissions -rw-r--r--
clarified generic path operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28495
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/thy_header.scala
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     3
46939
5b67ac48b384 allow multiple 'keywords' as in 'fixes';
wenzelm
parents: 46938
diff changeset
     4
Static theory header information.
28495
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     5
*/
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     6
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     7
package isabelle
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     8
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
     9
64824
330ec9bc4b75 tuned signature;
wenzelm
parents: 64777
diff changeset
    10
import scala.util.parsing.input.Reader
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    11
import scala.util.matching.Regex
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    12
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    13
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73368
diff changeset
    14
object Thy_Header {
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    15
  /* bootstrap keywords */
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    16
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
    17
  type Keywords = List[(String, Keyword.Spec)]
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
    18
  type Abbrevs = List[(String, String)]
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    19
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
    20
  val CHAPTER = "chapter"
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
    21
  val SECTION = "section"
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
    22
  val SUBSECTION = "subsection"
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
    23
  val SUBSUBSECTION = "subsubsection"
61463
8e46cea6a45a added 'paragraph', 'subparagraph';
wenzelm
parents: 60957
diff changeset
    24
  val PARAGRAPH = "paragraph"
8e46cea6a45a added 'paragraph', 'subparagraph';
wenzelm
parents: 60957
diff changeset
    25
  val SUBPARAGRAPH = "subparagraph"
58999
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58928
diff changeset
    26
  val TEXT = "text"
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58928
diff changeset
    27
  val TXT = "txt"
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58928
diff changeset
    28
  val TEXT_RAW = "text_raw"
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
    29
28495
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    30
  val THEORY = "theory"
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    31
  val IMPORTS = "imports"
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    32
  val KEYWORDS = "keywords"
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
    33
  val ABBREVS = "abbrevs"
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    34
  val AND = "and"
28495
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    35
  val BEGIN = "begin"
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    36
64854
f5aa712e6250 tuned signature;
wenzelm
parents: 64825
diff changeset
    37
  val bootstrap_header: Keywords =
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    38
    List(
72764
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    39
      ("%", Keyword.Spec()),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    40
      ("(", Keyword.Spec()),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    41
      (")", Keyword.Spec()),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    42
      (",", Keyword.Spec()),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    43
      ("::", Keyword.Spec()),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    44
      ("=", Keyword.Spec()),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    45
      (AND, Keyword.Spec()),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    46
      (BEGIN, Keyword.Spec(kind = Keyword.QUASI_COMMAND)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    47
      (IMPORTS, Keyword.Spec(kind = Keyword.QUASI_COMMAND)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    48
      (KEYWORDS, Keyword.Spec(kind = Keyword.QUASI_COMMAND)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    49
      (ABBREVS, Keyword.Spec(kind = Keyword.QUASI_COMMAND)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    50
      (CHAPTER, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    51
      (SECTION, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    52
      (SUBSECTION, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    53
      (SUBSUBSECTION, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    54
      (PARAGRAPH, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    55
      (SUBPARAGRAPH, Keyword.Spec(kind = Keyword.DOCUMENT_HEADING)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    56
      (TEXT, Keyword.Spec(kind = Keyword.DOCUMENT_BODY)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    57
      (TXT, Keyword.Spec(kind = Keyword.DOCUMENT_BODY)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    58
      (TEXT_RAW, Keyword.Spec(kind = Keyword.DOCUMENT_RAW)),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    59
      (THEORY, Keyword.Spec(kind = Keyword.THY_BEGIN, tags = List("theory"))),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
    60
      ("ML", Keyword.Spec(kind = Keyword.THY_DECL, tags = List("ML"))))
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    61
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    62
  private val bootstrap_keywords =
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    63
    Keyword.Keywords.empty.add_keywords(bootstrap_header)
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    64
67004
af72fa58f71b clarified lazy Completion within Outer_Syntax: measurable speedup of Sessions.deps;
wenzelm
parents: 66984
diff changeset
    65
  val bootstrap_syntax: Outer_Syntax =
af72fa58f71b clarified lazy Completion within Outer_Syntax: measurable speedup of Sessions.deps;
wenzelm
parents: 66984
diff changeset
    66
    Outer_Syntax.empty.add_keywords(bootstrap_header)
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    67
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    68
64673
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
    69
  /* file name vs. theory name */
62895
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
    70
63022
785a59235a15 more IDE support for Isabelle/Pure bootstrap;
wenzelm
parents: 62969
diff changeset
    71
  val PURE = "Pure"
62895
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
    72
  val ML_BOOTSTRAP = "ML_Bootstrap"
65490
571a3ce3cc17 special theories are always global;
wenzelm
parents: 65452
diff changeset
    73
  val ml_roots = List("ROOT0.ML" -> "ML_Root0", "ROOT.ML" -> "ML_Root")
63022
785a59235a15 more IDE support for Isabelle/Pure bootstrap;
wenzelm
parents: 62969
diff changeset
    74
  val bootstrap_thys = List(PURE, ML_BOOTSTRAP).map(a => a -> ("Bootstrap_" + a))
44160
8848867501fb clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents: 44159
diff changeset
    75
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents: 67212
diff changeset
    76
  val bootstrap_global_theories =
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents: 67212
diff changeset
    77
    (Sessions.root_name :: (ml_roots ::: bootstrap_thys).map(_._2)).map(_ -> PURE)
65490
571a3ce3cc17 special theories are always global;
wenzelm
parents: 65452
diff changeset
    78
65452
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    79
  def import_name(s: String): String =
76828
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    80
    Url.get_base_name(s) match {
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    81
      case Some(name) if !File.is_thy(name) => name
67164
39f57f0757f1 clarified error;
wenzelm
parents: 67013
diff changeset
    82
      case _ => error("Malformed theory import: " + quote(s))
39f57f0757f1 clarified error;
wenzelm
parents: 67013
diff changeset
    83
    }
44225
a8f921e6484f more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents: 44222
diff changeset
    84
76828
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    85
  def get_thy_name(s: String): Option[String] = Url.get_base_name(s, suffix = ".thy")
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    86
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    87
  def list_thy_names(dir: Path): List[String] = {
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    88
    val files =
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    89
      try { File.read_dir(dir) }
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    90
      catch { case ERROR(_) => Nil }
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    91
    files.flatMap(get_thy_name)
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    92
  }
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    93
65452
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    94
  def theory_name(s: String): String =
76828
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    95
    get_thy_name(s) match {
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    96
      case Some(name) => bootstrap_name(name)
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    97
      case None =>
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    98
        Url.get_base_name(s) match {
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
    99
          case Some(name) =>
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
   100
            if (name == Sessions.root_name) name
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
   101
            else ml_roots.collectFirst({ case (a, b) if a == name => b }).getOrElse("")
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
   102
          case None => ""
a5ff9cf61551 clarified generic path operations;
wenzelm
parents: 75906
diff changeset
   103
        }
62895
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
   104
    }
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
   105
64673
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
   106
  def is_ml_root(theory: String): Boolean =
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
   107
    ml_roots.exists({ case (_, b) => b == theory })
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
   108
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
   109
  def is_bootstrap(theory: String): Boolean =
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
   110
    bootstrap_thys.exists({ case (_, b) => b == theory })
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
   111
66195
bb886f13623a proper bootstrap_name (amending b42743f5b595);
wenzelm
parents: 65539
diff changeset
   112
  def bootstrap_name(theory: String): String =
bb886f13623a proper bootstrap_name (amending b42743f5b595);
wenzelm
parents: 65539
diff changeset
   113
    bootstrap_thys.collectFirst({ case (a, b) if a == theory => b }).getOrElse(theory)
bb886f13623a proper bootstrap_name (amending b42743f5b595);
wenzelm
parents: 65539
diff changeset
   114
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   115
68841
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   116
  /* parser */
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   117
75405
b13ab7d11b90 clarified signature: avoid ambiguity in scala3;
wenzelm
parents: 75393
diff changeset
   118
  trait Parsers extends Parse.Parsers {
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73368
diff changeset
   119
    val header: Parser[Thy_Header] = {
72765
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   120
      val load_command =
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   121
        ($$$("(") ~! (position(name) <~ $$$(")")) ^^ { case _ ~ x => x }) |
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   122
          success(("", Position.none))
72748
04d5f6d769a7 more flexible syntax for theory load commands via Isabelle/Scala;
wenzelm
parents: 72747
diff changeset
   123
72765
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   124
      val keyword_kind = atom("outer syntax keyword specification", _.is_name)
68841
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   125
      val keyword_spec =
72765
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   126
        position(keyword_kind) ~ load_command ~ tags ^^
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   127
          { case (a, b) ~ c ~ d =>
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   128
              Keyword.Spec(kind = a, kind_pos = b,
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   129
                load_command = c._1, load_command_pos = c._2, tags = d)
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   130
          }
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 58999
diff changeset
   131
68841
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   132
      val keyword_decl =
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   133
        rep1(string) ~
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   134
        opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^
72764
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
   135
        { case xs ~ y => xs.map((_, y.getOrElse(Keyword.Spec()))) }
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
   136
68841
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   137
      val keyword_decls =
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   138
        keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   139
        { case xs ~ yss => (xs :: yss).flatten }
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   140
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   141
      val abbrevs =
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   142
        rep1sep(rep1(text) ~ ($$$("=") ~! rep1(text)), $$$("and")) ^^
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   143
          { case res => for ((as ~ (_ ~ bs)) <- res; a <- as; b <- bs) yield (a, b) }
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   144
68841
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   145
      val args =
75407
c7051638a38c tuned: avoid ambiguity in scala3;
wenzelm
parents: 75405
diff changeset
   146
        position(this.theory_name) ~
c7051638a38c tuned: avoid ambiguity in scala3;
wenzelm
parents: 75405
diff changeset
   147
        (opt($$$(IMPORTS) ~! rep1(position(this.theory_name))) ^^
68841
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   148
          { case None => Nil case Some(_ ~ xs) => xs }) ~
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   149
        (opt($$$(KEYWORDS) ~! keyword_decls) ^^
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   150
          { case None => Nil case Some(_ ~ xs) => xs }) ~
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   151
        (opt($$$(ABBREVS) ~! abbrevs) ^^
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   152
          { case None => Nil case Some(_ ~ xs) => xs }) ~
72779
1cc74982d038 clarified parser;
wenzelm
parents: 72778
diff changeset
   153
        $$$(BEGIN) ^^ { case a ~ b ~ c ~ d ~ _ => Thy_Header(a._1, a._2, b, c, d) }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   154
68841
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   155
      val heading =
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   156
        (command(CHAPTER) |
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   157
          command(SECTION) |
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   158
          command(SUBSECTION) |
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   159
          command(SUBSUBSECTION) |
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   160
          command(PARAGRAPH) |
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   161
          command(SUBPARAGRAPH) |
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   162
          command(TEXT) |
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   163
          command(TXT) |
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   164
          command(TEXT_RAW)) ~
69887
b9985133805d added semantic document markers;
wenzelm
parents: 69255
diff changeset
   165
        annotation ~! document_source
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
   166
69887
b9985133805d added semantic document markers;
wenzelm
parents: 69255
diff changeset
   167
      (rep(heading) ~ command(THEORY) ~ annotation) ~! args ^^ { case _ ~ x => x }
68841
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   168
    }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   169
  }
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   170
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   171
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
   172
  /* read -- lazy scanning */
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   173
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73368
diff changeset
   174
  private def read_tokens(reader: Reader[Char], strict: Boolean): (List[Token], List[Token]) = {
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
   175
    val token = Token.Parsers.token(bootstrap_keywords)
73368
894f29abe5fc clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
   176
    def make_tokens(in: Reader[Char]): LazyList[Token] =
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   177
      token(in) match {
64825
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   178
        case Token.Parsers.Success(tok, rest) => tok #:: make_tokens(rest)
73368
894f29abe5fc clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
   179
        case _ => LazyList.empty
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   180
      }
64825
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   181
65539
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   182
    val all_tokens = make_tokens(reader)
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   183
    val drop_tokens =
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   184
      if (strict) Nil
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   185
      else all_tokens.takeWhile(tok => !tok.is_command(Thy_Header.THEORY)).toList
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
   186
65539
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   187
    val tokens = all_tokens.drop(drop_tokens.length)
64825
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   188
    val tokens1 = tokens.takeWhile(tok => !tok.is_begin).toList
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   189
    val tokens2 = tokens.dropWhile(tok => !tok.is_begin).headOption.toList
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   190
65539
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   191
    (drop_tokens, tokens1 ::: tokens2)
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   192
  }
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   193
75405
b13ab7d11b90 clarified signature: avoid ambiguity in scala3;
wenzelm
parents: 75393
diff changeset
   194
  private object Parsers extends Parsers {
68841
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   195
    def parse_header(tokens: List[Token], pos: Token.Pos): Thy_Header =
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   196
      parse(commit(header), Token.reader(tokens, pos)) match {
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   197
        case Success(result, _) => result
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   198
        case bad => error(bad.toString)
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   199
      }
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   200
  }
252b43600737 clarified signature;
wenzelm
parents: 67722
diff changeset
   201
72775
0a94eb91190d clarified modules;
wenzelm
parents: 72774
diff changeset
   202
  def read(node_name: Document.Node.Name, reader: Reader[Char],
72777
164cb0806d0a proper positions for inlined command messages, e.g. for completion within theory header;
wenzelm
parents: 72776
diff changeset
   203
    command: Boolean = true,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73368
diff changeset
   204
    strict: Boolean = true
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73368
diff changeset
   205
  ): Thy_Header = {
65539
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   206
    val (_, tokens0) = read_tokens(reader, true)
66918
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 66713
diff changeset
   207
    val text = Scan.reader_decode_utf8(reader, Token.implode(tokens0))
65539
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   208
72777
164cb0806d0a proper positions for inlined command messages, e.g. for completion within theory header;
wenzelm
parents: 72776
diff changeset
   209
    val (skip_tokens, tokens) = read_tokens(Scan.char_reader(text), strict)
164cb0806d0a proper positions for inlined command messages, e.g. for completion within theory header;
wenzelm
parents: 72776
diff changeset
   210
    val pos =
164cb0806d0a proper positions for inlined command messages, e.g. for completion within theory header;
wenzelm
parents: 72776
diff changeset
   211
      if (command) Token.Pos.command
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 72857
diff changeset
   212
      else skip_tokens.foldLeft(Token.Pos.file(node_name.node))(_ advance _)
65539
dbcd9b3e1b49 more precise position information;
wenzelm
parents: 65526
diff changeset
   213
75405
b13ab7d11b90 clarified signature: avoid ambiguity in scala3;
wenzelm
parents: 75393
diff changeset
   214
    Parsers.parse_header(tokens, pos).map(Symbol.decode).check(node_name)
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   215
  }
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   216
}
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   217
44185
05641edb5d30 provide node header via Scala layer;
wenzelm
parents: 44163
diff changeset
   218
sealed case class Thy_Header(
72778
83e581c9a5f1 tuned signature;
wenzelm
parents: 72777
diff changeset
   219
  name: String,
83e581c9a5f1 tuned signature;
wenzelm
parents: 72777
diff changeset
   220
  pos: Position.T,
83e581c9a5f1 tuned signature;
wenzelm
parents: 72777
diff changeset
   221
  imports: List[(String, Position.T)],
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   222
  keywords: Thy_Header.Keywords,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73368
diff changeset
   223
  abbrevs: Thy_Header.Abbrevs
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73368
diff changeset
   224
) {
72764
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
   225
  def map(f: String => String): Thy_Header =
72778
83e581c9a5f1 tuned signature;
wenzelm
parents: 72777
diff changeset
   226
    Thy_Header(f(name), pos,
83e581c9a5f1 tuned signature;
wenzelm
parents: 72777
diff changeset
   227
      imports.map({ case (a, b) => (f(a), b) }),
72764
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
   228
      keywords.map({ case (a, spec) => (f(a), spec.map(f)) }),
722c0d02ffab clarified signature;
wenzelm
parents: 72748
diff changeset
   229
      abbrevs.map({ case (a, b) => (f(a), f(b)) }))
72765
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   230
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73368
diff changeset
   231
  def check(node_name: Document.Node.Name): Thy_Header = {
72775
0a94eb91190d clarified modules;
wenzelm
parents: 72774
diff changeset
   232
    val base_name = node_name.theory_base_name
0a94eb91190d clarified modules;
wenzelm
parents: 72774
diff changeset
   233
    if (Long_Name.is_qualified(name)) {
0a94eb91190d clarified modules;
wenzelm
parents: 72774
diff changeset
   234
      error("Bad theory name " + quote(name) + " with qualification" + Position.here(pos))
0a94eb91190d clarified modules;
wenzelm
parents: 72774
diff changeset
   235
    }
0a94eb91190d clarified modules;
wenzelm
parents: 72774
diff changeset
   236
    if (base_name != name) {
72776
27a464537fb0 tuned signature;
wenzelm
parents: 72775
diff changeset
   237
      error("Bad theory name " + quote(name) + " for file " + Path.basic(base_name).thy +
27a464537fb0 tuned signature;
wenzelm
parents: 72775
diff changeset
   238
        Position.here(pos) + Completion.report_theories(pos, List(base_name)))
72775
0a94eb91190d clarified modules;
wenzelm
parents: 72774
diff changeset
   239
    }
0a94eb91190d clarified modules;
wenzelm
parents: 72774
diff changeset
   240
72765
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   241
    for ((_, spec) <- keywords) {
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   242
      if (spec.kind != Keyword.THY_LOAD && spec.load_command.nonEmpty) {
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   243
        error("Illegal load command specification for kind: " + quote(spec.kind) +
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   244
          Position.here(spec.kind_pos))
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   245
      }
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   246
      if (!Command_Span.load_commands.exists(_.name == spec.load_command)) {
72782
98ecb951d911 more completion;
wenzelm
parents: 72779
diff changeset
   247
        val completion =
98ecb951d911 more completion;
wenzelm
parents: 72779
diff changeset
   248
          for {
98ecb951d911 more completion;
wenzelm
parents: 72779
diff changeset
   249
            load_command <- Command_Span.load_commands
98ecb951d911 more completion;
wenzelm
parents: 72779
diff changeset
   250
            name = load_command.name
98ecb951d911 more completion;
wenzelm
parents: 72779
diff changeset
   251
            if name.startsWith(spec.load_command)
98ecb951d911 more completion;
wenzelm
parents: 72779
diff changeset
   252
          } yield (name, (Markup.LOAD_COMMAND, name))
72765
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   253
        error("Unknown load command specification: " + quote(spec.load_command) +
72782
98ecb951d911 more completion;
wenzelm
parents: 72779
diff changeset
   254
          Position.here(spec.load_command_pos) +
98ecb951d911 more completion;
wenzelm
parents: 72779
diff changeset
   255
          Completion.report_names(spec.load_command_pos, completion))
72765
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   256
      }
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   257
    }
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   258
    this
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72764
diff changeset
   259
  }
70638
f164cec7ac22 clarified signature: prefer operations without position;
wenzelm
parents: 69887
diff changeset
   260
}