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