src/Pure/Thy/thy_header.scala
author wenzelm
Mon, 17 Apr 2017 13:14:01 +0200
changeset 65490 571a3ce3cc17
parent 65452 9e9750a7932c
child 65526 41dda3a292e6
permissions -rw-r--r--
special theories are always global;
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
43611
21a57a0c5f25 more general theory header parsing;
wenzelm
parents: 41535
diff changeset
    10
import scala.annotation.tailrec
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    11
import scala.collection.mutable
64824
330ec9bc4b75 tuned signature;
wenzelm
parents: 64777
diff changeset
    12
import scala.util.parsing.input.Reader
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    13
import scala.util.matching.Regex
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    14
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
    15
43661
39fdbd814c7f quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents: 43652
diff changeset
    16
object Thy_Header extends Parse.Parser
32450
375db037f4d2 misc tuning;
wenzelm
parents: 29140
diff changeset
    17
{
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    18
  /* bootstrap keywords */
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    19
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
    20
  type Keywords = List[(String, Keyword.Spec)]
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
    21
  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
    22
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
    23
  val CHAPTER = "chapter"
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
    24
  val SECTION = "section"
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
    25
  val SUBSECTION = "subsection"
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
    26
  val SUBSUBSECTION = "subsubsection"
61463
8e46cea6a45a added 'paragraph', 'subparagraph';
wenzelm
parents: 60957
diff changeset
    27
  val PARAGRAPH = "paragraph"
8e46cea6a45a added 'paragraph', 'subparagraph';
wenzelm
parents: 60957
diff changeset
    28
  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
    29
  val TEXT = "text"
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58928
diff changeset
    30
  val TXT = "txt"
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58928
diff changeset
    31
  val TEXT_RAW = "text_raw"
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
    32
28495
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    33
  val THEORY = "theory"
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    34
  val IMPORTS = "imports"
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    35
  val KEYWORDS = "keywords"
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
    36
  val ABBREVS = "abbrevs"
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    37
  val AND = "and"
28495
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    38
  val BEGIN = "begin"
c5f86d04743b Theory header keywords.
wenzelm
parents:
diff changeset
    39
64854
f5aa712e6250 tuned signature;
wenzelm
parents: 64825
diff changeset
    40
  val bootstrap_header: Keywords =
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    41
    List(
65384
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    42
      ("%", Keyword.Spec.none),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    43
      ("(", Keyword.Spec.none),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    44
      (")", Keyword.Spec.none),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    45
      (",", Keyword.Spec.none),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    46
      ("::", Keyword.Spec.none),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    47
      ("=", Keyword.Spec.none),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    48
      (AND, Keyword.Spec.none),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    49
      (BEGIN, Keyword.Spec(Keyword.QUASI_COMMAND)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    50
      (IMPORTS, Keyword.Spec(Keyword.QUASI_COMMAND)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    51
      (KEYWORDS, Keyword.Spec(Keyword.QUASI_COMMAND)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    52
      (ABBREVS, Keyword.Spec(Keyword.QUASI_COMMAND)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    53
      (CHAPTER, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    54
      (SECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    55
      (SUBSECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    56
      (SUBSUBSECTION, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    57
      (PARAGRAPH, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    58
      (SUBPARAGRAPH, Keyword.Spec(Keyword.DOCUMENT_HEADING)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    59
      (TEXT, Keyword.Spec(Keyword.DOCUMENT_BODY)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    60
      (TXT, Keyword.Spec(Keyword.DOCUMENT_BODY)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    61
      (TEXT_RAW, Keyword.Spec(Keyword.DOCUMENT_RAW)),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    62
      (THEORY, Keyword.Spec(Keyword.THY_BEGIN, tags = List("theory"))),
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
    63
      ("ML", Keyword.Spec(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
    64
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    65
  private val bootstrap_keywords =
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    66
    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
    67
59736
5c1a0069b9d3 tight span for theory header, which is relevant for error positions (including semantic completion);
wenzelm
parents: 59705
diff changeset
    68
  lazy val bootstrap_syntax: Outer_Syntax =
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
    69
    Outer_Syntax.init().add_keywords(bootstrap_header)
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
    70
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    71
64673
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
    72
  /* 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
    73
63022
785a59235a15 more IDE support for Isabelle/Pure bootstrap;
wenzelm
parents: 62969
diff changeset
    74
  val PURE = "Pure"
62895
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
    75
  val ML_BOOTSTRAP = "ML_Bootstrap"
65490
571a3ce3cc17 special theories are always global;
wenzelm
parents: 65452
diff changeset
    76
  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
    77
  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
    78
65490
571a3ce3cc17 special theories are always global;
wenzelm
parents: 65452
diff changeset
    79
  val bootstrap_global_theories = (ml_roots ::: bootstrap_thys).map(p => (p._2 -> PURE))
571a3ce3cc17 special theories are always global;
wenzelm
parents: 65452
diff changeset
    80
65452
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    81
  private val Thy_File_Name = new Regex(""".*?([^/\\:]+)\.thy""")
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    82
  private val Import_Name = new Regex(""".*?([^/\\:]+)""")
44160
8848867501fb clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents: 44159
diff changeset
    83
65452
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    84
  def import_name(s: String): String =
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    85
    s match { case Import_Name(name) => name case _ => error("Malformed import: " + quote(s)) }
44225
a8f921e6484f more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents: 44222
diff changeset
    86
65452
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    87
  def theory_name(s: String): String =
62895
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
    88
    s match {
65452
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    89
      case Thy_File_Name(name) =>
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    90
        bootstrap_thys.collectFirst({ case (a, b) if a == name => b }).getOrElse(name)
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    91
      case Import_Name(name) =>
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    92
        ml_roots.collectFirst({ case (a, b) if a == name => b }).getOrElse("")
9e9750a7932c clarified signature;
wenzelm
parents: 65392
diff changeset
    93
      case _ => ""
62895
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
    94
    }
38149
3c380380beac somewhat uniform Thy_Header.split_thy_path in ML and Scala;
wenzelm
parents: 36956
diff changeset
    95
64673
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
    96
  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
    97
    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
    98
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
    99
  def is_bootstrap(theory: String): Boolean =
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64671
diff changeset
   100
    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
   101
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   102
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   103
  /* header */
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   104
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   105
  val header: Parser[Thy_Header] =
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   106
  {
48864
3ee314ae1e0a added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents: 48706
diff changeset
   107
    val opt_files =
58908
58bedbc18915 tuned signature;
wenzelm
parents: 58907
diff changeset
   108
      $$$("(") ~! (rep1sep(name, $$$(",")) <~ $$$(")")) ^^ { case _ ~ x => x } |
48864
3ee314ae1e0a added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents: 48706
diff changeset
   109
      success(Nil)
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 58999
diff changeset
   110
48864
3ee314ae1e0a added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents: 48706
diff changeset
   111
    val keyword_spec =
3ee314ae1e0a added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents: 48706
diff changeset
   112
      atom("outer syntax keyword specification", _.is_name) ~ opt_files ~ tags ^^
65384
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
   113
      { case x ~ y ~ z => Keyword.Spec(x, y, z) }
48864
3ee314ae1e0a added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents: 48706
diff changeset
   114
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
   115
    val keyword_decl =
50128
599c935aac82 alternative completion for outer syntax keywords;
wenzelm
parents: 48882
diff changeset
   116
      rep1(string) ~
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   117
      opt($$$("::") ~! keyword_spec ^^ { case _ ~ x => x }) ^^
65384
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
   118
      { case xs ~ y => xs.map((_, y.getOrElse(Keyword.Spec.none))) }
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 58999
diff changeset
   119
46939
5b67ac48b384 allow multiple 'keywords' as in 'fixes';
wenzelm
parents: 46938
diff changeset
   120
    val keyword_decls =
58908
58bedbc18915 tuned signature;
wenzelm
parents: 58907
diff changeset
   121
      keyword_decl ~ rep($$$(AND) ~! keyword_decl ^^ { case _ ~ x => x }) ^^
46939
5b67ac48b384 allow multiple 'keywords' as in 'fixes';
wenzelm
parents: 46938
diff changeset
   122
      { case xs ~ yss => (xs :: yss).flatten }
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
   123
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   124
    val abbrevs =
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   125
      rep1(text ~ ($$$("=") ~! text) ^^ { case a ~ (_ ~ b) => (a, b) })
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   126
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   127
    val args =
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 58999
diff changeset
   128
      position(theory_name) ~
62969
9f394a16c557 eliminated "xname" and variants;
wenzelm
parents: 62946
diff changeset
   129
      (opt($$$(IMPORTS) ~! rep1(position(theory_name))) ^^
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
   130
        { case None => Nil case Some(_ ~ xs) => xs }) ~
58908
58bedbc18915 tuned signature;
wenzelm
parents: 58907
diff changeset
   131
      (opt($$$(KEYWORDS) ~! keyword_decls) ^^
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
   132
        { case None => Nil case Some(_ ~ xs) => xs }) ~
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   133
      (opt($$$(ABBREVS) ~! abbrevs) ^^
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   134
        { case None => Nil case Some(_ ~ xs) => xs }) ~
58908
58bedbc18915 tuned signature;
wenzelm
parents: 58907
diff changeset
   135
      $$$(BEGIN) ^^
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   136
      { case x ~ ys ~ zs ~ ws ~ _ => Thy_Header(x, ys, zs, ws) }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   137
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
   138
    val heading =
62453
b93cc7d73431 discontinued old 'header';
wenzelm
parents: 61463
diff changeset
   139
      (command(CHAPTER) |
58907
0ee3563803c9 more uniform header_keywords in ML/Scala;
wenzelm
parents: 58903
diff changeset
   140
        command(SECTION) |
0ee3563803c9 more uniform header_keywords in ML/Scala;
wenzelm
parents: 58903
diff changeset
   141
        command(SUBSECTION) |
58999
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58928
diff changeset
   142
        command(SUBSUBSECTION) |
61463
8e46cea6a45a added 'paragraph', 'subparagraph';
wenzelm
parents: 60957
diff changeset
   143
        command(PARAGRAPH) |
8e46cea6a45a added 'paragraph', 'subparagraph';
wenzelm
parents: 60957
diff changeset
   144
        command(SUBPARAGRAPH) |
58999
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58928
diff changeset
   145
        command(TEXT) |
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58928
diff changeset
   146
        command(TXT) |
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58928
diff changeset
   147
        command(TEXT_RAW)) ~
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
   148
      tags ~! document_source
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58861
diff changeset
   149
58907
0ee3563803c9 more uniform header_keywords in ML/Scala;
wenzelm
parents: 58903
diff changeset
   150
    (rep(heading) ~ command(THEORY) ~ tags) ~! args ^^ { case _ ~ x => x }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   151
  }
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   152
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   153
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
   154
  /* read -- lazy scanning */
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   155
64825
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   156
  def read(reader: Reader[Char], start: Token.Pos, strict: Boolean = true): Thy_Header =
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   157
  {
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58908
diff changeset
   158
    val token = Token.Parsers.token(bootstrap_keywords)
64825
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   159
    def make_tokens(in: Reader[Char]): Stream[Token] =
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   160
      token(in) match {
64825
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   161
        case Token.Parsers.Success(tok, rest) => tok #:: make_tokens(rest)
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   162
        case _ => Stream.empty
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   163
      }
64825
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   164
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   165
    val tokens =
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   166
      if (strict) make_tokens(reader)
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   167
      else make_tokens(reader).dropWhile(tok => !tok.is_command(Thy_Header.THEORY))
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
   168
64825
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   169
    val tokens1 = tokens.takeWhile(tok => !tok.is_begin).toList
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   170
    val tokens2 = tokens.dropWhile(tok => !tok.is_begin).headOption.toList
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   171
e78b62c289bb more uniform node_header (non-strict);
wenzelm
parents: 64824
diff changeset
   172
    parse(commit(header), Token.reader(tokens1 ::: tokens2, start)) match {
34190
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
   173
      case Success(result, _) => result
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
   174
      case bad => error(bad.toString)
dfcf667bbfed read header by scanning/parsing file;
wenzelm
parents: 34188
diff changeset
   175
    }
34169
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   176
  }
7501b2910900 basic setup for header scanning/parsing;
wenzelm
parents: 32466
diff changeset
   177
}
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   178
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   179
44185
05641edb5d30 provide node header via Scala layer;
wenzelm
parents: 44163
diff changeset
   180
sealed case class Thy_Header(
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 58999
diff changeset
   181
  name: (String, Position.T),
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 58999
diff changeset
   182
  imports: List[(String, Position.T)],
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   183
  keywords: Thy_Header.Keywords,
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   184
  abbrevs: Thy_Header.Abbrevs)
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   185
{
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
   186
  def decode_symbols: Thy_Header =
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
   187
  {
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
   188
    val f = Symbol.decode _
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   189
    Thy_Header((f(name._1), name._2),
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   190
      imports.map({ case (a, b) => (f(a), b) }),
65384
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
   191
      keywords.map({ case (a, Keyword.Spec(b, c, d)) =>
36255c43c64c more explicit types;
wenzelm
parents: 65362
diff changeset
   192
        (f(a), Keyword.Spec(f(b), c.map(f), d.map(f))) }),
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   193
      abbrevs.map({ case (a, b) => (f(a), f(b)) }))
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
   194
  }
44159
9a35e88d9dc9 simplified class Thy_Header;
wenzelm
parents: 44157
diff changeset
   195
}