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