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