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