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