src/Pure/Isar/outer_syntax.ML
author wenzelm
Thu Aug 26 19:01:58 1999 +0200 (1999-08-26 ago)
changeset 7367 a79d4683fadf
parent 7333 6cb15c6f1d9f
child 7604 55566b9ec7d7
permissions -rw-r--r--
print_help;
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@6860
    12
  val sync_main: unit -> unit
wenzelm@6860
    13
  val sync_loop: unit -> unit
wenzelm@5829
    14
  val help: unit -> unit
wenzelm@5829
    15
end;
wenzelm@5829
    16
wenzelm@5829
    17
signature OUTER_SYNTAX =
wenzelm@5829
    18
sig
wenzelm@5829
    19
  include BASIC_OUTER_SYNTAX
wenzelm@6722
    20
  structure Keyword:
wenzelm@6722
    21
    sig
wenzelm@6722
    22
      val control: string
wenzelm@6722
    23
      val diag: string
wenzelm@6722
    24
      val thy_begin: string
wenzelm@7104
    25
      val thy_switch: string
wenzelm@6722
    26
      val thy_end: string
wenzelm@6722
    27
      val thy_heading: string
wenzelm@6722
    28
      val thy_decl: string
wenzelm@6722
    29
      val thy_goal: string
wenzelm@6722
    30
      val qed: string
wenzelm@6733
    31
      val qed_block: string
wenzelm@6722
    32
      val prf_goal: string
wenzelm@6722
    33
      val prf_block: string
wenzelm@6722
    34
      val prf_chain: string
wenzelm@6722
    35
      val prf_decl: string
wenzelm@6868
    36
      val prf_asm: 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@7026
    46
  val dest_keywords: unit -> string list
wenzelm@7026
    47
  val dest_parsers: unit -> (string * string * string * bool) list
wenzelm@5883
    48
  val print_outer_syntax: unit -> unit
wenzelm@7367
    49
  val print_help: Toplevel.transition -> Toplevel.transition
wenzelm@5829
    50
  val add_keywords: string list -> unit
wenzelm@5829
    51
  val add_parsers: parser list -> unit
wenzelm@6247
    52
  val theory_header: token list -> (string * string list * (string * bool) list) * token list
wenzelm@7192
    53
  val deps_thy: string -> Path.T -> string list * Path.T list
wenzelm@6199
    54
  val load_thy: string -> bool -> bool -> Path.T -> unit
wenzelm@7333
    55
  val isar: bool -> bool -> Toplevel.isar
wenzelm@5829
    56
end;
wenzelm@5829
    57
wenzelm@5829
    58
structure OuterSyntax: OUTER_SYNTAX =
wenzelm@5829
    59
struct
wenzelm@5829
    60
wenzelm@6860
    61
structure P = OuterParse;
wenzelm@6860
    62
wenzelm@5829
    63
wenzelm@5829
    64
(** outer syntax **)
wenzelm@5829
    65
wenzelm@6722
    66
(* command keyword classification *)
wenzelm@6722
    67
wenzelm@6722
    68
structure Keyword =
wenzelm@6722
    69
struct
wenzelm@6722
    70
  val control = "control";
wenzelm@6722
    71
  val diag = "diag";
wenzelm@6722
    72
  val thy_begin = "theory-begin";
wenzelm@7104
    73
  val thy_switch = "theory-switch";
wenzelm@6722
    74
  val thy_end = "theory-end";
wenzelm@6722
    75
  val thy_heading = "theory-heading";
wenzelm@6722
    76
  val thy_decl = "theory-decl";
wenzelm@6722
    77
  val thy_goal = "theory-goal";
wenzelm@6722
    78
  val qed = "qed";
wenzelm@6733
    79
  val qed_block = "qed-block";
wenzelm@6722
    80
  val prf_goal = "proof-goal";
wenzelm@6722
    81
  val prf_block = "proof-block";
wenzelm@6722
    82
  val prf_chain = "proof-chain";
wenzelm@6722
    83
  val prf_decl = "proof-decl";
wenzelm@6868
    84
  val prf_asm = "proof-asm";
wenzelm@6722
    85
  val prf_script = "proof-script";
wenzelm@6722
    86
wenzelm@7104
    87
  val kinds = [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_goal,
wenzelm@7104
    88
    qed, qed_block, prf_goal, prf_block, prf_chain, prf_decl, prf_asm, prf_script];
wenzelm@6722
    89
end;
wenzelm@6722
    90
wenzelm@6722
    91
wenzelm@5829
    92
(* parsers *)
wenzelm@5829
    93
wenzelm@5829
    94
type token = OuterLex.token;
wenzelm@5829
    95
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
wenzelm@5829
    96
wenzelm@5829
    97
datatype parser =
wenzelm@6722
    98
  Parser of string * (string * string) * bool * parser_fn;
wenzelm@5829
    99
wenzelm@6722
   100
fun parser int_only name comment kind parse = Parser (name, (comment, kind), int_only, parse);
wenzelm@5829
   101
wenzelm@5829
   102
wenzelm@5829
   103
(* parse command *)
wenzelm@5829
   104
wenzelm@6860
   105
local
wenzelm@6199
   106
wenzelm@5829
   107
fun command_body cmd (name, _) =
wenzelm@7026
   108
  (case cmd name of
wenzelm@7026
   109
    Some (int_only, parse) => P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only))
wenzelm@7026
   110
  | None => sys_error ("no parser for outer syntax command " ^ quote name));
wenzelm@6860
   111
wenzelm@6860
   112
fun terminator false = Scan.succeed ()
wenzelm@6860
   113
  | terminator true = P.group "terminator" (Scan.option P.sync -- P.$$$ ";" >> K ());
wenzelm@6860
   114
wenzelm@6860
   115
in
wenzelm@5829
   116
wenzelm@6860
   117
fun command term cmd =
wenzelm@6860
   118
  P.$$$ ";" >> K None ||
wenzelm@6860
   119
  P.sync >> K None ||
wenzelm@7026
   120
  (P.position P.command :-- command_body cmd) --| terminator term
wenzelm@6860
   121
    >> (fn ((name, pos), (int_only, f)) =>
wenzelm@6860
   122
      Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
   123
        Toplevel.interactive int_only |> f));
wenzelm@5829
   124
wenzelm@6199
   125
end;
wenzelm@6199
   126
wenzelm@5829
   127
wenzelm@5829
   128
wenzelm@5829
   129
(** global syntax state **)
wenzelm@5829
   130
wenzelm@7026
   131
local
wenzelm@7026
   132
wenzelm@7026
   133
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
wenzelm@6722
   134
val global_parsers = ref (Symtab.empty: ((string * string) * (bool * parser_fn)) Symtab.table);
wenzelm@5952
   135
wenzelm@7026
   136
fun change_lexicons f =
wenzelm@7026
   137
  let val lexs = f (! global_lexicons) in
wenzelm@7026
   138
    (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
wenzelm@7026
   139
      [] => global_lexicons := lexs
wenzelm@7026
   140
    | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
wenzelm@7026
   141
  end;
wenzelm@5829
   142
wenzelm@7026
   143
fun change_parsers f = global_parsers := f (! global_parsers);
wenzelm@6722
   144
wenzelm@7026
   145
in
wenzelm@7026
   146
wenzelm@7026
   147
(* get current lexers / parsers *)
wenzelm@7026
   148
wenzelm@7026
   149
(*Note: the syntax for files is statically determined at the very
wenzelm@7026
   150
  beginning; for interactive processing it may change dynamically.*)
wenzelm@7026
   151
wenzelm@7026
   152
fun get_lexicons () = ! global_lexicons;
wenzelm@7026
   153
fun get_parsers () = ! global_parsers;
wenzelm@7026
   154
fun get_parser () = apsome snd o curry Symtab.lookup (! global_parsers);
wenzelm@5829
   155
wenzelm@5829
   156
wenzelm@5829
   157
(* augment syntax *)
wenzelm@5829
   158
wenzelm@7026
   159
fun add_keywords keywords = change_lexicons (apfst (fn lex =>
wenzelm@7026
   160
  (Scan.extend_lexicon lex (map Symbol.explode keywords))));
wenzelm@5829
   161
wenzelm@5829
   162
fun add_parser (tab, Parser (name, comment, int_only, parse)) =
wenzelm@5829
   163
 (if is_none (Symtab.lookup (tab, name)) then ()
wenzelm@5829
   164
  else warning ("Redefined outer syntax command " ^ quote name);
wenzelm@5829
   165
  Symtab.update ((name, (comment, (int_only, parse))), tab));
wenzelm@5829
   166
wenzelm@5829
   167
fun add_parsers parsers =
wenzelm@7026
   168
  (change_parsers (fn tab => foldl add_parser (tab, parsers));
wenzelm@7026
   169
    change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
wenzelm@7026
   170
      (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
wenzelm@7026
   171
wenzelm@7026
   172
end;
wenzelm@5829
   173
wenzelm@5829
   174
wenzelm@7026
   175
(* print syntax *)
wenzelm@7026
   176
wenzelm@7026
   177
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
wenzelm@7026
   178
wenzelm@7026
   179
fun dest_parsers () =
wenzelm@7026
   180
  map (fn (name, ((cmt, kind), (int_only, _))) => (name, cmt, kind, int_only))
wenzelm@7026
   181
    (Symtab.dest (get_parsers ()));
wenzelm@5829
   182
wenzelm@7026
   183
fun print_outer_syntax () =
wenzelm@7026
   184
  let
wenzelm@7026
   185
    fun pretty_cmd (name, comment, _, _) =
wenzelm@7026
   186
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@7026
   187
    val (int_cmds, cmds) = partition #4 (dest_parsers ());
wenzelm@7026
   188
  in
wenzelm@7026
   189
    Pretty.writeln (Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())));
wenzelm@7026
   190
    Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds));
wenzelm@7026
   191
    Pretty.writeln (Pretty.big_list "improper commands (interactive-only):"
wenzelm@7026
   192
      (map pretty_cmd int_cmds))
wenzelm@7026
   193
  end;
wenzelm@5829
   194
wenzelm@7367
   195
val print_help =
wenzelm@7367
   196
  Toplevel.imperative print_outer_syntax o
wenzelm@7367
   197
  Toplevel.keep (fn state =>
wenzelm@7367
   198
    (print_outer_syntax ();
wenzelm@7367
   199
      Method.help_methods (Toplevel.theory_of state);
wenzelm@7367
   200
      Attrib.help_attributes (Toplevel.theory_of state)));
wenzelm@7367
   201
wenzelm@5829
   202
wenzelm@5829
   203
wenzelm@5829
   204
(** read theory **)
wenzelm@5829
   205
wenzelm@6247
   206
(* theory keyword *)
wenzelm@6247
   207
wenzelm@6247
   208
val theoryN = "theory";
wenzelm@6247
   209
val theory_keyword = OuterParse.$$$ theoryN;
wenzelm@6247
   210
wenzelm@6247
   211
wenzelm@5829
   212
(* source *)
wenzelm@5829
   213
wenzelm@6860
   214
local
wenzelm@5829
   215
wenzelm@6860
   216
val no_terminator =
wenzelm@6860
   217
  Scan.unless (P.$$$ ";") (Scan.one (OuterLex.not_sync andf OuterLex.not_eof));
wenzelm@6860
   218
wenzelm@6860
   219
val recover = Scan.prompt "recover# " (Scan.repeat no_terminator);
wenzelm@6860
   220
wenzelm@6860
   221
in
wenzelm@5829
   222
wenzelm@6860
   223
fun source term do_recover cmd src =
wenzelm@5829
   224
  src
wenzelm@6860
   225
  |> Source.source OuterLex.stopper
wenzelm@6860
   226
    (Scan.bulk (fn xs => OuterParse.!!! (command term (cmd ())) xs))
wenzelm@6860
   227
    (if do_recover then Some recover else None)
wenzelm@5829
   228
  |> Source.mapfilter I;
wenzelm@5829
   229
wenzelm@6860
   230
end;
wenzelm@6860
   231
wenzelm@5829
   232
wenzelm@5829
   233
(* detect header *)
wenzelm@5829
   234
wenzelm@7026
   235
fun scan_header get_lex scan (src, pos) =
wenzelm@5829
   236
  src
wenzelm@5829
   237
  |> Symbol.source false
wenzelm@7026
   238
  |> OuterLex.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos
wenzelm@6199
   239
  |> Source.source OuterLex.stopper (Scan.single scan) None
wenzelm@5829
   240
  |> (fst o the o Source.get_single);
wenzelm@5829
   241
wenzelm@6247
   242
val check_header_lexicon = Scan.make_lexicon [Symbol.explode theoryN];
wenzelm@5829
   243
wenzelm@6199
   244
fun is_old_theory src =
wenzelm@6247
   245
  is_none (scan_header (K check_header_lexicon) (Scan.option theory_keyword) src);
wenzelm@6199
   246
wenzelm@6199
   247
wenzelm@6199
   248
(* deps_thy --- inspect theory header *)
wenzelm@6199
   249
wenzelm@6247
   250
val header_lexicon =
wenzelm@6247
   251
  Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", "=", "files", theoryN]);
wenzelm@6199
   252
wenzelm@6860
   253
local
wenzelm@6199
   254
wenzelm@7026
   255
val file_name =
wenzelm@7026
   256
  (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
wenzelm@6247
   257
wenzelm@6247
   258
val theory_head =
wenzelm@6860
   259
  (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
wenzelm@6860
   260
    Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [])
wenzelm@6247
   261
  >> (fn ((A, Bs), files) => (A, Bs, files));
wenzelm@6247
   262
wenzelm@6860
   263
in
wenzelm@6860
   264
wenzelm@6860
   265
val theory_header = theory_head --| (Scan.ahead P.eof || P.$$$ ":");
wenzelm@6860
   266
val only_header = theory_keyword |-- theory_head --| Scan.ahead P.eof;
wenzelm@6860
   267
val new_header = theory_keyword |-- P.!!! theory_header;
wenzelm@6199
   268
wenzelm@6199
   269
val old_header =
wenzelm@6860
   270
  P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))
wenzelm@6247
   271
  >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
wenzelm@6199
   272
wenzelm@6199
   273
end;
wenzelm@5829
   274
wenzelm@7192
   275
fun deps_thy name path =
wenzelm@6199
   276
  let
wenzelm@6641
   277
    val src = File.source path;
wenzelm@6247
   278
    val (name', parents, files) =
wenzelm@6199
   279
      (*Note: old style headers dynamically depend on the current lexicon :-( *)
wenzelm@7212
   280
      if is_old_theory src then scan_header ThySyn.get_lexicon (Scan.error old_header) src
wenzelm@6247
   281
      else scan_header (K header_lexicon) (Scan.error new_header) src;
wenzelm@6199
   282
wenzelm@6199
   283
    val ml_path = ThyLoad.ml_path name;
wenzelm@7192
   284
    val ml_file = if is_none (ThyLoad.check_file ml_path) then [] else [ml_path];
wenzelm@6199
   285
  in
wenzelm@6199
   286
    if name <> name' then
wenzelm@6199
   287
      error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name)
wenzelm@6247
   288
    else (parents, map (Path.unpack o #1) files @ ml_file)
wenzelm@6199
   289
  end;
wenzelm@6199
   290
wenzelm@6199
   291
wenzelm@6199
   292
(* load_thy --- read text (including header) *)
wenzelm@6199
   293
wenzelm@6247
   294
fun try_ml_file name ml time =
wenzelm@6199
   295
  let
wenzelm@6199
   296
    val path = ThyLoad.ml_path name;
wenzelm@7243
   297
    val tr = Toplevel.imperative (fn () => ThyInfo.may_load_file ml time path);
wenzelm@6247
   298
    val tr_name = if time then "time_use" else "use";
wenzelm@6199
   299
  in
wenzelm@7243
   300
    if is_none (ThyLoad.check_file path) then ()
wenzelm@7062
   301
    else Toplevel.excursion_error [Toplevel.empty |> Toplevel.name tr_name |> tr]
wenzelm@6199
   302
  end;
wenzelm@6199
   303
wenzelm@6199
   304
fun parse_thy (src, pos) =
wenzelm@6247
   305
  let
wenzelm@6247
   306
    val lex_src =
wenzelm@6247
   307
      src
wenzelm@6247
   308
      |> Symbol.source false
wenzelm@7026
   309
      |> OuterLex.source false (K (get_lexicons ())) pos;
wenzelm@6247
   310
    val only_head =
wenzelm@6247
   311
      lex_src
wenzelm@6247
   312
      |> Source.source OuterLex.stopper (Scan.single (Scan.option only_header)) None
wenzelm@6247
   313
      |> (fst o the o Source.get_single);
wenzelm@6247
   314
  in
wenzelm@6247
   315
    (case only_head of
wenzelm@6247
   316
      None =>
wenzelm@6247
   317
        lex_src
wenzelm@6860
   318
        |> source false false (K (get_parser ()))
wenzelm@6247
   319
        |> Source.exhaust
wenzelm@6247
   320
    | Some spec =>
wenzelm@6247
   321
        [Toplevel.empty |> Toplevel.name theoryN |> IsarThy.theory spec,
wenzelm@6247
   322
          Toplevel.empty |> Toplevel.name "end" |> Toplevel.exit])
wenzelm@6247
   323
  end;
wenzelm@5829
   324
wenzelm@6247
   325
fun run_thy name path =
wenzelm@6641
   326
  let val (src, pos) = File.source path in
wenzelm@6332
   327
    Present.theory_source name src;
wenzelm@6247
   328
    if is_old_theory (src, pos) then ThySyn.load_thy name (Source.exhaust src)
wenzelm@7062
   329
    else Toplevel.excursion_error (parse_thy (src, pos))
wenzelm@6247
   330
  end;
wenzelm@6199
   331
wenzelm@6199
   332
fun load_thy name ml time path =
wenzelm@6247
   333
 (if time then
wenzelm@6247
   334
    timeit (fn () =>
wenzelm@6247
   335
     (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
wenzelm@6247
   336
      setmp Goals.proof_timing true (run_thy name) path;
wenzelm@6247
   337
      writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
wenzelm@6247
   338
  else run_thy name path;
wenzelm@6247
   339
  Context.context (ThyInfo.get_theory name);
wenzelm@6247
   340
  try_ml_file name ml time);
wenzelm@5829
   341
wenzelm@5829
   342
wenzelm@5829
   343
(* interactive source of state transformers *)
wenzelm@5829
   344
wenzelm@7333
   345
fun isar term no_pos =
wenzelm@5829
   346
  Source.tty
wenzelm@5829
   347
  |> Symbol.source true
wenzelm@7333
   348
  |> OuterLex.source true get_lexicons (if no_pos then Position.none else Position.line_name 1 "stdin")
wenzelm@6860
   349
  |> source term true get_parser;
wenzelm@5829
   350
wenzelm@5829
   351
wenzelm@5829
   352
wenzelm@5829
   353
(** the read-eval-print loop **)
wenzelm@5829
   354
wenzelm@5923
   355
(* main loop *)
wenzelm@5923
   356
wenzelm@7333
   357
fun gen_loop term no_pos =
wenzelm@7333
   358
 (Context.reset_context ();
wenzelm@7333
   359
  Toplevel.loop (isar term no_pos));
wenzelm@5829
   360
wenzelm@7333
   361
fun gen_main term no_pos =
wenzelm@5829
   362
 (Toplevel.set_state Toplevel.toplevel;
wenzelm@5829
   363
  ml_prompts "ML> " "ML# ";
wenzelm@6199
   364
  writeln (Session.welcome ());
wenzelm@7333
   365
  gen_loop term no_pos);
wenzelm@6860
   366
wenzelm@7333
   367
fun main () = gen_main false false;
wenzelm@7333
   368
fun loop () = gen_loop false false;
wenzelm@7333
   369
fun sync_main () = gen_main true true;
wenzelm@7333
   370
fun sync_loop () = gen_loop true true;
wenzelm@5829
   371
wenzelm@5829
   372
wenzelm@5829
   373
(* help *)
wenzelm@5829
   374
wenzelm@5829
   375
fun help () =
wenzelm@5829
   376
  writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
wenzelm@5883
   377
    \invoke 'loop();' to enter the Isar loop.");
wenzelm@5829
   378
wenzelm@5829
   379
wenzelm@6373
   380
(*final declarations of this structure!*)
wenzelm@6685
   381
val command = parser false;
wenzelm@6685
   382
val improper_command = parser true;
wenzelm@6685
   383
wenzelm@6373
   384
wenzelm@5829
   385
end;
wenzelm@5829
   386
wenzelm@6199
   387
(*setup theory syntax dependent operations*)
wenzelm@6199
   388
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
wenzelm@6199
   389
ThyLoad.load_thy_fn := OuterSyntax.load_thy;
wenzelm@6199
   390
structure ThyLoad: THY_LOAD = ThyLoad;
wenzelm@6199
   391
wenzelm@5829
   392
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
wenzelm@5829
   393
open BasicOuterSyntax;