src/Pure/Isar/outer_syntax.ML
author wenzelm
Fri, 05 Feb 1999 21:06:24 +0100
changeset 6247 e8bbe64861b8
parent 6229 f839b261b87f
child 6332 7cee353c7f2a
permissions -rw-r--r--
more robust handling of theory context; header-only theory files;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/outer_syntax.ML
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     4
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     5
The global Isabelle/Isar outer syntax.
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
     6
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
     7
TODO:
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
     8
  - cleanup;
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
     9
  - avoid string constants;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    10
*)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    11
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    12
signature BASIC_OUTER_SYNTAX =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    13
sig
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    14
  val main: unit -> unit
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
    15
  val loop: unit -> unit
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    16
  val help: unit -> unit
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    17
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    18
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    19
signature OUTER_SYNTAX =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    20
sig
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    21
  include BASIC_OUTER_SYNTAX
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    22
  type token
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    23
  type parser
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    24
  val parser: bool -> string -> string ->
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    25
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
    26
  val print_outer_syntax: unit -> unit
5952
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
    27
  val commands: unit -> string list
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    28
  val add_keywords: string list -> unit
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    29
  val add_parsers: parser list -> unit
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
    30
  val theory_header: token list -> (string * string list * (string * bool) list) * token list
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    31
  val deps_thy: string -> bool -> Path.T -> string list * Path.T list
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    32
  val load_thy: string -> bool -> bool -> Path.T -> unit
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    33
  val isar: Toplevel.isar
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    34
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    35
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    36
structure OuterSyntax: OUTER_SYNTAX =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    37
struct
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    38
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    39
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    40
(** outer syntax **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    41
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    42
(* parsers *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    43
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    44
type token = OuterLex.token;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    45
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    46
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    47
datatype parser =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    48
  Parser of string * string * bool * parser_fn;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    49
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    50
fun parser int_only name comment parse = Parser (name, comment, int_only, parse);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    51
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    52
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    53
(* parse command *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    54
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    55
local open OuterParse in
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    56
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    57
fun command_name cmd =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    58
  group "command"
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    59
    (position (Scan.one (OuterLex.keyword_pred (is_some o cmd)) >> OuterLex.val_of));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    60
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    61
fun command_body cmd (name, _) =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    62
  let val (int_only, parse) = the (cmd name)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    63
  in !!! (Scan.prompt (name ^ "# ") (parse >> pair int_only)) end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    64
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    65
fun command cmd =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    66
  $$$ ";" >> K None ||
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    67
  command_name cmd :-- command_body cmd >> (fn ((name, pos), (int_only, f)) =>
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    68
    Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    69
      Toplevel.interactive int_only |> f));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    70
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    71
end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    72
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    73
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    74
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    75
(** global syntax state **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    76
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    77
val global_lexicon = ref Scan.empty_lexicon;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    78
val global_parsers = ref (Symtab.empty: (string * (bool * parser_fn)) Symtab.table);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    79
5952
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
    80
fun commands () = Symtab.keys (! global_parsers);
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
    81
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    82
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    83
(* print syntax *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    84
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    85
fun print_outer_syntax () =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    86
  let
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    87
    val keywords = map implode (Scan.dest_lexicon (! global_lexicon));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    88
    fun pretty_cmd (name, (comment, _)) =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    89
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    90
    val (int_cmds, cmds) = partition (#1 o #2 o #2) (Symtab.dest (! global_parsers));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    91
  in
6107
1418bc571f23 tuned msg;
wenzelm
parents: 6095
diff changeset
    92
    Pretty.writeln (Pretty.strs ("syntax keywords:" :: map quote keywords));
6095
9f75a45384dd tuned msg;
wenzelm
parents: 5992
diff changeset
    93
    Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds));
9f75a45384dd tuned msg;
wenzelm
parents: 5992
diff changeset
    94
    Pretty.writeln (Pretty.big_list "improper commands (interactive-only):"
9f75a45384dd tuned msg;
wenzelm
parents: 5992
diff changeset
    95
      (map pretty_cmd int_cmds))
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    96
  end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    97
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    98
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    99
(* augment syntax *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   100
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   101
fun add_keywords keywords =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   102
  global_lexicon := Scan.extend_lexicon (! global_lexicon) (map Symbol.explode keywords);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   103
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   104
fun add_parser (tab, Parser (name, comment, int_only, parse)) =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   105
 (if is_none (Symtab.lookup (tab, name)) then ()
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   106
  else warning ("Redefined outer syntax command " ^ quote name);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   107
  Symtab.update ((name, (comment, (int_only, parse))), tab));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   108
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   109
fun add_parsers parsers =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   110
  (global_parsers := foldl add_parser (! global_parsers, parsers);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   111
    add_keywords (map (fn Parser (name, _, _, _) => name) parsers));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   112
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   113
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   114
(* get current lexer / parser *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   115
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   116
(*Note: the syntax for files is statically determined at the very
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   117
  beginning; for interactive processing it may change dynamically.*)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   118
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   119
fun get_lexicon () = ! global_lexicon;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   120
fun get_parser () = apsome snd o curry Symtab.lookup (! global_parsers);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   121
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   122
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   123
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   124
(** read theory **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   125
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   126
(* theory keyword *)
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   127
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   128
val theoryN = "theory";
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   129
val theory_keyword = OuterParse.$$$ theoryN;
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   130
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   131
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   132
(* source *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   133
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   134
fun no_command cmd =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   135
  Scan.one ((not o OuterLex.keyword_pred ((is_some o cmd) orf equal ";")) andf OuterLex.not_eof);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   136
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   137
fun recover cmd =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   138
  Scan.prompt "recover# " (Scan.one OuterLex.not_eof -- Scan.repeat (no_command cmd));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   139
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   140
fun source do_recover cmd src =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   141
  src
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   142
  |> Source.source OuterLex.stopper (Scan.bulk (fn xs => OuterParse.!!! (command (cmd ())) xs))
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   143
    (if do_recover then Some (fn xs => recover (cmd ()) xs) else None)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   144
  |> Source.mapfilter I;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   145
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   146
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   147
(* detect header *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   148
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   149
fun scan_header get_lexicon scan (src, pos) =
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   150
  src
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   151
  |> Symbol.source false
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   152
  |> OuterLex.source false get_lexicon pos
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   153
  |> Source.source OuterLex.stopper (Scan.single scan) None
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   154
  |> (fst o the o Source.get_single);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   155
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   156
val check_header_lexicon = Scan.make_lexicon [Symbol.explode theoryN];
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   157
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   158
fun is_old_theory src =
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   159
  is_none (scan_header (K check_header_lexicon) (Scan.option theory_keyword) src);
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   160
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   161
fun warn_theory_style path is_old =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   162
  let
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   163
    val style = if is_old then "old" else "new";
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   164
    val _ = warning ("Assuming " ^ style ^ "-style theory format for " ^ quote (Path.pack path));
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   165
  in is_old end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   166
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   167
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   168
(* deps_thy --- inspect theory header *)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   169
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   170
val header_lexicon =
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   171
  Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", "=", "files", theoryN]);
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   172
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   173
local open OuterParse in
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   174
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   175
val file_name = ($$$ "(" |-- !!! (name --| $$$ ")")) >> rpair false || name >> rpair true;
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   176
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   177
val theory_head =
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   178
  (name -- ($$$ "=" |-- enum1 "+" name) --
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   179
    Scan.optional ($$$ "files" |-- !!! (Scan.repeat1 file_name)) [])
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   180
  >> (fn ((A, Bs), files) => (A, Bs, files));
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   181
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   182
val theory_header = theory_head --| (Scan.ahead eof || $$$ ":");
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   183
val only_header = theory_keyword |-- theory_head --| Scan.ahead eof;
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   184
val new_header = theory_keyword |-- !!! theory_header;
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   185
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   186
val old_header =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   187
  name -- ($$$ "=" |-- name -- Scan.repeat ($$$ "+" |-- name))
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   188
  >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   189
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   190
end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   191
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   192
fun deps_thy name ml path =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   193
  let
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   194
    val src = Source.of_file path;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   195
    val is_old = warn_theory_style path (is_old_theory src);
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   196
    val (name', parents, files) =
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   197
      (*Note: old style headers dynamically depend on the current lexicon :-( *)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   198
      if is_old then scan_header ThySyn.get_lexicon (Scan.error old_header) src
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   199
      else scan_header (K header_lexicon) (Scan.error new_header) src;
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   200
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   201
    val ml_path = ThyLoad.ml_path name;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   202
    val ml_file = if not ml orelse is_none (ThyLoad.check_file ml_path) then [] else [ml_path];
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   203
  in
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   204
    if name <> name' then
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   205
      error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name)
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   206
    else (parents, map (Path.unpack o #1) files @ ml_file)
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   207
  end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   208
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   209
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   210
(* load_thy --- read text (including header) *)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   211
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   212
fun try_ml_file name ml time =
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   213
  let
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   214
    val path = ThyLoad.ml_path name;
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   215
    val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   216
    val tr_name = if time then "time_use" else "use";
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   217
  in
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   218
    if not ml orelse is_none (ThyLoad.check_file path) then ()
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   219
    else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr]
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   220
  end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   221
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   222
fun parse_thy (src, pos) =
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   223
  let
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   224
    val lex_src =
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   225
      src
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   226
      |> Symbol.source false
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   227
      |> OuterLex.source false (K (get_lexicon ())) pos;
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   228
    val only_head =
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   229
      lex_src
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   230
      |> Source.source OuterLex.stopper (Scan.single (Scan.option only_header)) None
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   231
      |> (fst o the o Source.get_single);
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   232
  in
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   233
    (case only_head of
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   234
      None =>
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   235
        lex_src
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   236
        |> source false (K (get_parser ()))
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   237
        |> Source.exhaust
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   238
    | Some spec =>
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   239
        [Toplevel.empty |> Toplevel.name theoryN |> IsarThy.theory spec,
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   240
          Toplevel.empty |> Toplevel.name "end" |> Toplevel.exit])
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   241
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   242
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   243
fun run_thy name path =
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   244
  let val (src, pos) = Source.of_file path in
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   245
    if is_old_theory (src, pos) then ThySyn.load_thy name (Source.exhaust src)
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   246
    else (Toplevel.excursion (parse_thy (src, pos))
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   247
      handle exn => error (Toplevel.exn_message exn))
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   248
  end;
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   249
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   250
fun load_thy name ml time path =
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   251
 (if time then
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   252
    timeit (fn () =>
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   253
     (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   254
      setmp Goals.proof_timing true (run_thy name) path;
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   255
      writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   256
  else run_thy name path;
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   257
  Context.context (ThyInfo.get_theory name);
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   258
  try_ml_file name ml time);
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   259
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   260
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   261
(* interactive source of state transformers *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   262
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   263
val isar =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   264
  Source.tty
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   265
  |> Symbol.source true
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   266
  |> OuterLex.source true get_lexicon (Position.line_name 1 "stdin")
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   267
  |> source true get_parser;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   268
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   269
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   270
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   271
(** the read-eval-print loop **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   272
5923
wenzelm
parents: 5883
diff changeset
   273
(* main loop *)
wenzelm
parents: 5883
diff changeset
   274
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
   275
fun loop () = (Context.reset_context (); Toplevel.loop isar);
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   276
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   277
fun main () =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   278
 (Toplevel.set_state Toplevel.toplevel;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   279
  ml_prompts "ML> " "ML# ";
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   280
  writeln (Session.welcome ());
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
   281
  loop ());
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   282
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   283
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   284
(* help *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   285
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   286
fun help () =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   287
  writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
   288
    \invoke 'loop();' to enter the Isar loop.");
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   289
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   290
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   291
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   292
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   293
(*setup theory syntax dependent operations*)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   294
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   295
ThyLoad.load_thy_fn := OuterSyntax.load_thy;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   296
structure ThyLoad: THY_LOAD = ThyLoad;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   297
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   298
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   299
open BasicOuterSyntax;