src/Pure/Thy/thy_header.ML
author wenzelm
Wed, 12 Mar 2025 11:39:00 +0100
changeset 82265 4b875a4c83b0
parent 80307 718daea1cf99
permissions -rw-r--r--
update for release;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/thy_header.ML
46939
5b67ac48b384 allow multiple 'keywords' as in 'fixes';
wenzelm
parents: 46938
diff changeset
     2
    Author:     Makarius
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     3
46939
5b67ac48b384 allow multiple 'keywords' as in 'fixes';
wenzelm
parents: 46938
diff changeset
     4
Static theory header information.
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     5
*)
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     6
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     7
signature THY_HEADER =
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
     8
sig
63429
baedd4724f08 tuned signature: more uniform Keyword.spec;
wenzelm
parents: 63022
diff changeset
     9
  type keywords = ((string * Position.T) * Keyword.spec) list
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    10
  type header =
48927
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48881
diff changeset
    11
   {name: string * Position.T,
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48881
diff changeset
    12
    imports: (string * Position.T) list,
51294
0850d43cb355 discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents: 51293
diff changeset
    13
    keywords: keywords}
0850d43cb355 discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents: 51293
diff changeset
    14
  val make: string * Position.T -> (string * Position.T) list -> keywords -> header
59735
24bee1b11fce misc tuning and simplification;
wenzelm
parents: 59694
diff changeset
    15
  val theoryN: string
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    16
  val bootstrap_keywords: Keyword.keywords
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    17
  val add_keywords: keywords -> theory -> theory
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    18
  val get_keywords: theory -> Keyword.keywords
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    19
  val get_keywords': Proof.context -> Keyword.keywords
80307
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
    20
  val print_name: theory -> string -> string
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
    21
  val print_name': Proof.context -> string -> string
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
    22
  val pretty_name: theory -> string -> Pretty.T
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
    23
  val pretty_name': Proof.context -> string -> Pretty.T
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
    24
62895
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
    25
  val ml_bootstrapN: string
62932
db12de2367ca support ROOT0.ML as well -- independently of ROOT.ML;
wenzelm
parents: 62895
diff changeset
    26
  val ml_roots: string list
63022
785a59235a15 more IDE support for Isabelle/Pure bootstrap;
wenzelm
parents: 62969
diff changeset
    27
  val bootstrap_thys: string list
66771
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 65999
diff changeset
    28
  val is_base_name: string -> bool
65452
9e9750a7932c clarified signature;
wenzelm
parents: 64556
diff changeset
    29
  val import_name: string -> string
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    30
  val args: header parser
67500
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
    31
  val read_tokens: Position.T -> Token.T list -> header
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    32
  val read: Position.T -> string -> header
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    33
end;
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    34
32466
a393b7e2a2f8 modernized Thy_Header;
wenzelm
parents: 29606
diff changeset
    35
structure Thy_Header: THY_HEADER =
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    36
struct
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    37
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    38
(** keyword declarations **)
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    39
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    40
(* header *)
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    41
63429
baedd4724f08 tuned signature: more uniform Keyword.spec;
wenzelm
parents: 63022
diff changeset
    42
type keywords = ((string * Position.T) * Keyword.spec) list;
48874
ff9cd47be39b refined Thy_Load.check_thy: find more uses in body text, based on keywords;
wenzelm
parents: 48864
diff changeset
    43
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    44
type header =
48927
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48881
diff changeset
    45
 {name: string * Position.T,
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48881
diff changeset
    46
  imports: (string * Position.T) list,
51294
0850d43cb355 discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents: 51293
diff changeset
    47
  keywords: keywords};
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    48
51294
0850d43cb355 discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents: 51293
diff changeset
    49
fun make name imports keywords : header =
0850d43cb355 discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents: 51293
diff changeset
    50
  {name = name, imports = imports, keywords = keywords};
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    51
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    52
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    53
(* bootstrap keywords *)
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    54
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58864
diff changeset
    55
val chapterN = "chapter";
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58864
diff changeset
    56
val sectionN = "section";
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58864
diff changeset
    57
val subsectionN = "subsection";
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58864
diff changeset
    58
val subsubsectionN = "subsubsection";
61463
8e46cea6a45a added 'paragraph', 'subparagraph';
wenzelm
parents: 60957
diff changeset
    59
val paragraphN = "paragraph";
8e46cea6a45a added 'paragraph', 'subparagraph';
wenzelm
parents: 60957
diff changeset
    60
val subparagraphN = "subparagraph";
58999
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58932
diff changeset
    61
val textN = "text";
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58932
diff changeset
    62
val txtN = "txt";
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58932
diff changeset
    63
val text_rawN = "text_raw";
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58864
diff changeset
    64
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    65
val theoryN = "theory";
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    66
val importsN = "imports";
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
    67
val keywordsN = "keywords";
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
    68
val abbrevsN = "abbrevs";
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    69
val beginN = "begin";
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    70
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    71
val bootstrap_keywords =
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58868
diff changeset
    72
  Keyword.empty_keywords
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    73
  |> Keyword.add_keywords
64556
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    74
    [(("%", \<^here>), Keyword.no_spec),
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    75
     (("(", \<^here>), Keyword.no_spec),
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    76
     ((")", \<^here>), Keyword.no_spec),
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    77
     ((",", \<^here>), Keyword.no_spec),
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    78
     (("::", \<^here>), Keyword.no_spec),
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    79
     (("=", \<^here>), Keyword.no_spec),
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    80
     (("and", \<^here>), Keyword.no_spec),
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    81
     ((beginN, \<^here>), Keyword.quasi_command_spec),
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    82
     ((importsN, \<^here>), Keyword.quasi_command_spec),
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    83
     ((keywordsN, \<^here>), Keyword.quasi_command_spec),
851ae0e7b09c more symbols;
wenzelm
parents: 63936
diff changeset
    84
     ((abbrevsN, \<^here>), Keyword.quasi_command_spec),
67139
8fe0aba577af explicit tag for document commands: avoid implicit use of document_tags;
wenzelm
parents: 67136
diff changeset
    85
     ((chapterN, \<^here>), Keyword.document_heading_spec),
8fe0aba577af explicit tag for document commands: avoid implicit use of document_tags;
wenzelm
parents: 67136
diff changeset
    86
     ((sectionN, \<^here>), Keyword.document_heading_spec),
8fe0aba577af explicit tag for document commands: avoid implicit use of document_tags;
wenzelm
parents: 67136
diff changeset
    87
     ((subsectionN, \<^here>), Keyword.document_heading_spec),
8fe0aba577af explicit tag for document commands: avoid implicit use of document_tags;
wenzelm
parents: 67136
diff changeset
    88
     ((subsubsectionN, \<^here>), Keyword.document_heading_spec),
8fe0aba577af explicit tag for document commands: avoid implicit use of document_tags;
wenzelm
parents: 67136
diff changeset
    89
     ((paragraphN, \<^here>), Keyword.document_heading_spec),
8fe0aba577af explicit tag for document commands: avoid implicit use of document_tags;
wenzelm
parents: 67136
diff changeset
    90
     ((subparagraphN, \<^here>), Keyword.document_heading_spec),
8fe0aba577af explicit tag for document commands: avoid implicit use of document_tags;
wenzelm
parents: 67136
diff changeset
    91
     ((textN, \<^here>), Keyword.document_body_spec),
8fe0aba577af explicit tag for document commands: avoid implicit use of document_tags;
wenzelm
parents: 67136
diff changeset
    92
     ((txtN, \<^here>), Keyword.document_body_spec),
74671
df12779c3ce8 more PIDE markup;
wenzelm
parents: 74561
diff changeset
    93
     ((text_rawN, \<^here>), Keyword.command_spec (Keyword.document_raw, ["document"])),
df12779c3ce8 more PIDE markup;
wenzelm
parents: 74561
diff changeset
    94
     ((theoryN, \<^here>), Keyword.command_spec (Keyword.thy_begin, ["theory"])),
df12779c3ce8 more PIDE markup;
wenzelm
parents: 74561
diff changeset
    95
     (("ML", \<^here>), Keyword.command_spec (Keyword.thy_decl, ["ML"]))];
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    96
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
    97
80307
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
    98
(* keywords *)
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
    99
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   100
structure Data = Theory_Data
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   101
(
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   102
  type T = Keyword.keywords;
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   103
  val empty = bootstrap_keywords;
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   104
  val merge = Keyword.merge_keywords;
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   105
);
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   106
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   107
val add_keywords = Data.map o Keyword.add_keywords;
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   108
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   109
val get_keywords = Data.get;
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   110
val get_keywords' = get_keywords o Proof_Context.theory_of;
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   111
80307
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
   112
val print_name = Token.print_name o get_keywords;
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
   113
val print_name' = Token.print_name o get_keywords';
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
   114
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
   115
val pretty_name = Pretty.str oo print_name;
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
   116
val pretty_name' = Pretty.str oo print_name';
718daea1cf99 clarified modules;
wenzelm
parents: 74887
diff changeset
   117
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   118
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   119
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58924
diff changeset
   120
(** concrete syntax **)
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   121
62895
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
   122
(* names *)
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
   123
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
   124
val ml_bootstrapN = "ML_Bootstrap";
62932
db12de2367ca support ROOT0.ML as well -- independently of ROOT.ML;
wenzelm
parents: 62895
diff changeset
   125
val ml_roots = ["ML_Root0", "ML_Root"];
63022
785a59235a15 more IDE support for Isabelle/Pure bootstrap;
wenzelm
parents: 62969
diff changeset
   126
val bootstrap_thys = ["Bootstrap_Pure", "Bootstrap_ML_Bootstrap"];
62932
db12de2367ca support ROOT0.ML as well -- independently of ROOT.ML;
wenzelm
parents: 62895
diff changeset
   127
66771
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 65999
diff changeset
   128
fun is_base_name s =
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 65999
diff changeset
   129
  s <> "" andalso not (exists_string (member (op =) ["/", "\\", ":"]) s)
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 65999
diff changeset
   130
67164
39f57f0757f1 clarified error;
wenzelm
parents: 67139
diff changeset
   131
fun import_name s =
39f57f0757f1 clarified error;
wenzelm
parents: 67139
diff changeset
   132
  if String.isSuffix ".thy" s then
39f57f0757f1 clarified error;
wenzelm
parents: 67139
diff changeset
   133
    error ("Malformed theory import: " ^ quote s)
69366
b6dacf6eabe3 clarified signature;
wenzelm
parents: 69349
diff changeset
   134
  else Path.file_name (Path.explode s);
62895
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
   135
54c2abe7e9a4 treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents: 62849
diff changeset
   136
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   137
(* header args *)
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   138
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
   139
local
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
   140
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59693
diff changeset
   141
fun imports name =
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59693
diff changeset
   142
  if name = Context.PureN then Scan.succeed []
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 67722
diff changeset
   143
  else Parse.$$$ importsN |-- Parse.!!! (Scan.repeat1 Parse.theory_name);
53962
65bca53daf70 more robust parser: 'imports' are mandatory except for bootstrapping Pure;
wenzelm
parents: 51627
diff changeset
   144
72765
f34f5c057c9e clarified parsing vs. semantic errors;
wenzelm
parents: 72747
diff changeset
   145
val load_command =
74671
df12779c3ce8 more PIDE markup;
wenzelm
parents: 74561
diff changeset
   146
  Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Parse.position Parse.name) --| Parse.$$$ ")")
df12779c3ce8 more PIDE markup;
wenzelm
parents: 74561
diff changeset
   147
    ("", Position.none);
50128
599c935aac82 alternative completion for outer syntax keywords;
wenzelm
parents: 48992
diff changeset
   148
48864
3ee314ae1e0a added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents: 48707
diff changeset
   149
val keyword_spec =
3ee314ae1e0a added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents: 48707
diff changeset
   150
  Parse.group (fn () => "outer syntax keyword specification")
74671
df12779c3ce8 more PIDE markup;
wenzelm
parents: 74561
diff changeset
   151
    ((Parse.name -- load_command) -- Document_Source.old_tags) >>
df12779c3ce8 more PIDE markup;
wenzelm
parents: 74561
diff changeset
   152
      (fn ((a, b), c) => {kind = a, load_command = b, tags = c});
48864
3ee314ae1e0a added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents: 48707
diff changeset
   153
46939
5b67ac48b384 allow multiple 'keywords' as in 'fixes';
wenzelm
parents: 46938
diff changeset
   154
val keyword_decl =
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 67722
diff changeset
   155
  Scan.repeat1 Parse.string_position --
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   156
  Scan.optional (Parse.$$$ "::" |-- Parse.!!! keyword_spec) Keyword.no_spec
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   157
  >> (fn (names, spec) => map (rpair spec) names);
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   158
67722
012f1e8a1209 allow multiple entries of and_list (on both sides);
wenzelm
parents: 67500
diff changeset
   159
val abbrevs =
012f1e8a1209 allow multiple entries of and_list (on both sides);
wenzelm
parents: 67500
diff changeset
   160
  Parse.and_list1
74887
56247fdb8bbb discontinued old-style {* verbatim *} tokens;
wenzelm
parents: 74671
diff changeset
   161
    (Scan.repeat1 Parse.embedded -- (Parse.$$$ "=" |-- Parse.!!! (Scan.repeat1 Parse.embedded))
67722
012f1e8a1209 allow multiple entries of and_list (on both sides);
wenzelm
parents: 67500
diff changeset
   162
      >> uncurry (map_product pair)) >> flat;
50128
599c935aac82 alternative completion for outer syntax keywords;
wenzelm
parents: 48992
diff changeset
   163
46939
5b67ac48b384 allow multiple 'keywords' as in 'fixes';
wenzelm
parents: 46938
diff changeset
   164
val keyword_decls = Parse.and_list1 keyword_decl >> flat;
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
   165
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
   166
in
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   167
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   168
val args =
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 67722
diff changeset
   169
  Parse.theory_name :|-- (fn (name, pos) =>
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59693
diff changeset
   170
    imports name --
53962
65bca53daf70 more robust parser: 'imports' are mandatory except for bootstrapping Pure;
wenzelm
parents: 51627
diff changeset
   171
    Scan.optional (Parse.$$$ keywordsN |-- Parse.!!! keyword_decls) [] --|
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   172
    (Scan.optional (Parse.$$$ abbrevsN |-- Parse.!!! abbrevs) [] -- Parse.$$$ beginN)
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63449
diff changeset
   173
    >> (fn (imports, keywords) => make (name, pos) imports keywords));
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
   174
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46737
diff changeset
   175
end;
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   176
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   177
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   178
(* read header *)
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   179
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58864
diff changeset
   180
val heading =
67136
1368cfa92b7a tuned signature;
wenzelm
parents: 67013
diff changeset
   181
  (Parse.command_name chapterN ||
1368cfa92b7a tuned signature;
wenzelm
parents: 67013
diff changeset
   182
    Parse.command_name sectionN ||
1368cfa92b7a tuned signature;
wenzelm
parents: 67013
diff changeset
   183
    Parse.command_name subsectionN ||
1368cfa92b7a tuned signature;
wenzelm
parents: 67013
diff changeset
   184
    Parse.command_name subsubsectionN ||
1368cfa92b7a tuned signature;
wenzelm
parents: 67013
diff changeset
   185
    Parse.command_name paragraphN ||
1368cfa92b7a tuned signature;
wenzelm
parents: 67013
diff changeset
   186
    Parse.command_name subparagraphN ||
1368cfa92b7a tuned signature;
wenzelm
parents: 67013
diff changeset
   187
    Parse.command_name textN ||
1368cfa92b7a tuned signature;
wenzelm
parents: 67013
diff changeset
   188
    Parse.command_name txtN ||
1368cfa92b7a tuned signature;
wenzelm
parents: 67013
diff changeset
   189
    Parse.command_name text_rawN) --
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69887
diff changeset
   190
  (Document_Source.annotation |-- Parse.!!! Parse.document_source);
58868
c5e1cce7ace3 uniform heading commands work in any context, even in theory header;
wenzelm
parents: 58864
diff changeset
   191
67500
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   192
val parse_header =
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69887
diff changeset
   193
  (Scan.repeat heading -- Parse.command_name theoryN --| Document_Source.annotation)
69887
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
   194
    |-- Parse.!!! args;
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   195
67500
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   196
fun read_tokens pos toks =
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   197
  filter Token.is_proper toks
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   198
  |> Source.of_list
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   199
  |> Source.source Token.stopper (Scan.single (Scan.error (Parse.!!! parse_header)))
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   200
  |> Source.get_single
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   201
  |> (fn SOME (header, _) => header | NONE => error ("Unexpected end of input" ^ Position.here pos));
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   202
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   203
local
48927
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48881
diff changeset
   204
67500
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   205
fun read_header pos text =
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   206
  Symbol_Pos.explode (text, pos)
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   207
  |> Token.tokenize bootstrap_keywords {strict = false}
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   208
  |> read_tokens pos;
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   209
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   210
val approx_length = 1024;
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   211
67500
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   212
in
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   213
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   214
fun read pos text =
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   215
  if size text <= approx_length then read_header pos text
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   216
  else
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   217
    let val approx_text = String.substring (text, 0, approx_length) in
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   218
      if String.isSuffix "begin" approx_text then read_header pos text
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   219
      else (read_header pos approx_text handle ERROR _ => read_header pos text)
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   220
    end;
48927
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48881
diff changeset
   221
22106
0886ec05f951 renamed Isar/thy_header.ML to Thy/thy_header.ML;
wenzelm
parents:
diff changeset
   222
end;
67500
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   223
dfde99d59f6e tuned: prefer list operations over Source.source;
wenzelm
parents: 67164
diff changeset
   224
end;