src/Pure/Isar/outer_syntax.ML
author wenzelm
Sun Jun 25 23:54:13 2000 +0200 (2000-06-25)
changeset 9132 52286129faa5
parent 9056 8f78b2aea39e
child 9213 2651a4db8883
permissions -rw-r--r--
moved header stuff to thy_header.ML;
moved theory presentation to isar_output.ML;
major cleanup;
wenzelm@5829
     1
(*  Title:      Pure/Isar/outer_syntax.ML
wenzelm@5829
     2
    ID:         $Id$
wenzelm@5829
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8807
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5829
     5
wenzelm@5829
     6
The global Isabelle/Isar outer syntax.
wenzelm@5829
     7
*)
wenzelm@5829
     8
wenzelm@5829
     9
signature BASIC_OUTER_SYNTAX =
wenzelm@5829
    10
sig
wenzelm@5829
    11
  val main: unit -> unit
wenzelm@5883
    12
  val loop: unit -> unit
wenzelm@6860
    13
  val sync_main: unit -> unit
wenzelm@6860
    14
  val sync_loop: unit -> unit
wenzelm@5829
    15
  val help: unit -> unit
wenzelm@5829
    16
end;
wenzelm@5829
    17
wenzelm@5829
    18
signature OUTER_SYNTAX =
wenzelm@5829
    19
sig
wenzelm@5829
    20
  include BASIC_OUTER_SYNTAX
wenzelm@6722
    21
  structure Keyword:
wenzelm@6722
    22
    sig
wenzelm@6722
    23
      val control: string
wenzelm@6722
    24
      val diag: string
wenzelm@6722
    25
      val thy_begin: string
wenzelm@7104
    26
      val thy_switch: 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@8209
    33
      val qed_global: string
wenzelm@6722
    34
      val prf_goal: string
wenzelm@6722
    35
      val prf_block: string
wenzelm@9056
    36
      val prf_open: string
wenzelm@9056
    37
      val prf_close: string
wenzelm@6722
    38
      val prf_chain: string
wenzelm@6722
    39
      val prf_decl: string
wenzelm@6868
    40
      val prf_asm: string
wenzelm@7676
    41
      val prf_asm_goal: string
wenzelm@6722
    42
      val prf_script: string
wenzelm@6722
    43
      val kinds: string list
wenzelm@6722
    44
    end
wenzelm@5829
    45
  type token
wenzelm@5829
    46
  type parser
wenzelm@6722
    47
  val command: string -> string -> string ->
wenzelm@6373
    48
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@9132
    49
  val markup_command: IsarOutput.markup -> string -> string -> string ->
wenzelm@7789
    50
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@6722
    51
  val improper_command: string -> string -> string ->
wenzelm@6373
    52
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@7026
    53
  val dest_keywords: unit -> string list
wenzelm@7026
    54
  val dest_parsers: unit -> (string * string * string * bool) list
wenzelm@5883
    55
  val print_outer_syntax: unit -> unit
wenzelm@7367
    56
  val print_help: Toplevel.transition -> Toplevel.transition
wenzelm@5829
    57
  val add_keywords: string list -> unit
wenzelm@5829
    58
  val add_parsers: parser list -> unit
wenzelm@7940
    59
  val deps_thy: string -> bool -> Path.T -> string list * Path.T list
wenzelm@6199
    60
  val load_thy: string -> bool -> bool -> Path.T -> unit
wenzelm@7333
    61
  val isar: bool -> bool -> Toplevel.isar
wenzelm@5829
    62
end;
wenzelm@5829
    63
wenzelm@5829
    64
structure OuterSyntax: OUTER_SYNTAX =
wenzelm@5829
    65
struct
wenzelm@5829
    66
wenzelm@7750
    67
structure T = OuterLex;
wenzelm@6860
    68
structure P = OuterParse;
wenzelm@6860
    69
wenzelm@5829
    70
wenzelm@5829
    71
(** outer syntax **)
wenzelm@5829
    72
wenzelm@6722
    73
(* command keyword classification *)
wenzelm@6722
    74
wenzelm@6722
    75
structure Keyword =
wenzelm@6722
    76
struct
wenzelm@6722
    77
  val control = "control";
wenzelm@6722
    78
  val diag = "diag";
wenzelm@6722
    79
  val thy_begin = "theory-begin";
wenzelm@7104
    80
  val thy_switch = "theory-switch";
wenzelm@6722
    81
  val thy_end = "theory-end";
wenzelm@6722
    82
  val thy_heading = "theory-heading";
wenzelm@6722
    83
  val thy_decl = "theory-decl";
wenzelm@6722
    84
  val thy_goal = "theory-goal";
wenzelm@6722
    85
  val qed = "qed";
wenzelm@6733
    86
  val qed_block = "qed-block";
wenzelm@8209
    87
  val qed_global = "qed-global";
wenzelm@6722
    88
  val prf_goal = "proof-goal";
wenzelm@6722
    89
  val prf_block = "proof-block";
wenzelm@9056
    90
  val prf_open = "proof-open";
wenzelm@9056
    91
  val prf_close = "proof-close";
wenzelm@6722
    92
  val prf_chain = "proof-chain";
wenzelm@6722
    93
  val prf_decl = "proof-decl";
wenzelm@6868
    94
  val prf_asm = "proof-asm";
wenzelm@7676
    95
  val prf_asm_goal = "proof-asm-goal";
wenzelm@6722
    96
  val prf_script = "proof-script";
wenzelm@6722
    97
wenzelm@7104
    98
  val kinds = [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_goal,
wenzelm@9056
    99
    qed, qed_block, qed_global, prf_goal, prf_block, prf_open, prf_close, prf_chain, prf_decl,
wenzelm@9056
   100
    prf_asm, prf_asm_goal, prf_script];
wenzelm@6722
   101
end;
wenzelm@6722
   102
wenzelm@6722
   103
wenzelm@5829
   104
(* parsers *)
wenzelm@5829
   105
wenzelm@7750
   106
type token = T.token;
wenzelm@5829
   107
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
wenzelm@5829
   108
wenzelm@5829
   109
datatype parser =
wenzelm@9132
   110
  Parser of string * (string * string * IsarOutput.markup option) * bool * parser_fn;
wenzelm@5829
   111
wenzelm@7750
   112
fun parser int_only markup name comment kind parse =
wenzelm@7750
   113
  Parser (name, (comment, kind, markup), int_only, parse);
wenzelm@5829
   114
wenzelm@5829
   115
wenzelm@5829
   116
(* parse command *)
wenzelm@5829
   117
wenzelm@6860
   118
local
wenzelm@6199
   119
wenzelm@5829
   120
fun command_body cmd (name, _) =
wenzelm@7026
   121
  (case cmd name of
wenzelm@7026
   122
    Some (int_only, parse) => P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only))
wenzelm@7026
   123
  | None => sys_error ("no parser for outer syntax command " ^ quote name));
wenzelm@6860
   124
wenzelm@6860
   125
fun terminator false = Scan.succeed ()
wenzelm@9132
   126
  | terminator true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
wenzelm@6860
   127
wenzelm@6860
   128
in
wenzelm@5829
   129
wenzelm@6860
   130
fun command term cmd =
wenzelm@9132
   131
  P.semicolon >> K None ||
wenzelm@6860
   132
  P.sync >> K None ||
wenzelm@7026
   133
  (P.position P.command :-- command_body cmd) --| terminator term
wenzelm@6860
   134
    >> (fn ((name, pos), (int_only, f)) =>
wenzelm@6860
   135
      Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
   136
        Toplevel.interactive int_only |> f));
wenzelm@5829
   137
wenzelm@6199
   138
end;
wenzelm@6199
   139
wenzelm@5829
   140
wenzelm@5829
   141
wenzelm@9132
   142
(** global outer syntax **)
wenzelm@5829
   143
wenzelm@7026
   144
local
wenzelm@7026
   145
wenzelm@7026
   146
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
wenzelm@7750
   147
val global_parsers =
wenzelm@9132
   148
  ref (Symtab.empty: (((string * string) * (bool * parser_fn)) * IsarOutput.markup option)
wenzelm@9132
   149
    Symtab.table);
wenzelm@9132
   150
val global_markups = ref ([]: (string * IsarOutput.markup) list);
wenzelm@5952
   151
wenzelm@7026
   152
fun change_lexicons f =
wenzelm@7026
   153
  let val lexs = f (! global_lexicons) in
wenzelm@7026
   154
    (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
wenzelm@7026
   155
      [] => global_lexicons := lexs
wenzelm@7026
   156
    | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
wenzelm@7026
   157
  end;
wenzelm@5829
   158
wenzelm@7789
   159
fun get_markup (ms, (name, (_, Some m))) = (name, m) :: ms
wenzelm@7789
   160
  | get_markup (ms, _) = ms;
wenzelm@7750
   161
wenzelm@7750
   162
fun make_markups () = global_markups := Symtab.foldl get_markup ([], ! global_parsers);
wenzelm@9132
   163
fun change_parsers f = (Library.change global_parsers f; make_markups ());
wenzelm@6722
   164
wenzelm@7026
   165
in
wenzelm@7026
   166
wenzelm@7750
   167
wenzelm@9132
   168
(* access current syntax *)
wenzelm@7026
   169
wenzelm@7026
   170
(*Note: the syntax for files is statically determined at the very
wenzelm@7026
   171
  beginning; for interactive processing it may change dynamically.*)
wenzelm@7026
   172
wenzelm@7026
   173
fun get_lexicons () = ! global_lexicons;
wenzelm@7026
   174
fun get_parsers () = ! global_parsers;
wenzelm@7750
   175
fun get_parser () = apsome (#2 o #1) o curry Symtab.lookup (! global_parsers);
wenzelm@7789
   176
wenzelm@9132
   177
fun is_markup kind name =
wenzelm@9132
   178
  (case assoc (! global_markups, name) of Some k => k = kind | None => false);
wenzelm@9132
   179
fun markup kind = Scan.one (T.is_kind T.Command andf is_markup kind o T.val_of);
wenzelm@5829
   180
wenzelm@5829
   181
wenzelm@5829
   182
(* augment syntax *)
wenzelm@5829
   183
wenzelm@7026
   184
fun add_keywords keywords = change_lexicons (apfst (fn lex =>
wenzelm@7026
   185
  (Scan.extend_lexicon lex (map Symbol.explode keywords))));
wenzelm@5829
   186
wenzelm@7750
   187
fun add_parser (tab, Parser (name, (comment, kind, markup), int_only, parse)) =
wenzelm@5829
   188
 (if is_none (Symtab.lookup (tab, name)) then ()
wenzelm@5829
   189
  else warning ("Redefined outer syntax command " ^ quote name);
wenzelm@7750
   190
  Symtab.update ((name, (((comment, kind), (int_only, parse)), markup)), tab));
wenzelm@5829
   191
wenzelm@5829
   192
fun add_parsers parsers =
wenzelm@7026
   193
  (change_parsers (fn tab => foldl add_parser (tab, parsers));
wenzelm@7026
   194
    change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
wenzelm@7026
   195
      (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
wenzelm@7026
   196
wenzelm@7026
   197
end;
wenzelm@5829
   198
wenzelm@5829
   199
wenzelm@7026
   200
(* print syntax *)
wenzelm@7026
   201
wenzelm@7026
   202
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
wenzelm@7026
   203
wenzelm@7026
   204
fun dest_parsers () =
wenzelm@7750
   205
  map (fn (name, (((cmt, kind), (int_only, _)), _)) => (name, cmt, kind, int_only))
wenzelm@7026
   206
    (Symtab.dest (get_parsers ()));
wenzelm@5829
   207
wenzelm@8720
   208
fun help_outer_syntax () =
wenzelm@7026
   209
  let
wenzelm@7026
   210
    fun pretty_cmd (name, comment, _, _) =
wenzelm@7026
   211
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@7026
   212
    val (int_cmds, cmds) = partition #4 (dest_parsers ());
wenzelm@7026
   213
  in
wenzelm@8720
   214
    [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
wenzelm@8720
   215
      Pretty.big_list "proper commands:" (map pretty_cmd cmds),
wenzelm@8720
   216
      Pretty.big_list "improper commands (interactive-only):" (map pretty_cmd int_cmds)]
wenzelm@7026
   217
  end;
wenzelm@5829
   218
wenzelm@8720
   219
val print_outer_syntax = Pretty.writeln o Pretty.chunks o help_outer_syntax;
wenzelm@8720
   220
wenzelm@7367
   221
val print_help =
wenzelm@7367
   222
  Toplevel.keep (fn state =>
wenzelm@7613
   223
    let val opt_thy = try Toplevel.theory_of state in
wenzelm@8720
   224
      help_outer_syntax () @
wenzelm@8720
   225
      Method.help_methods opt_thy @
wenzelm@7613
   226
      Attrib.help_attributes opt_thy
wenzelm@8720
   227
      |> Pretty.chunks |> Pretty.writeln
wenzelm@7613
   228
    end);
wenzelm@7367
   229
wenzelm@5829
   230
wenzelm@5829
   231
wenzelm@9132
   232
(** toplevel parsing **)
wenzelm@5829
   233
wenzelm@9132
   234
(* basic sources *)
wenzelm@6860
   235
wenzelm@7683
   236
fun token_source (src, pos) =
wenzelm@7683
   237
  src
wenzelm@7683
   238
  |> Symbol.source false
wenzelm@7750
   239
  |> T.source false (K (get_lexicons ())) pos;
wenzelm@7683
   240
wenzelm@9132
   241
fun toplevel_source term do_recover cmd src =
wenzelm@9132
   242
  let
wenzelm@9132
   243
    val no_terminator =
wenzelm@9132
   244
      Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
wenzelm@9132
   245
    val recover = Scan.prompt "recover# " (Scan.repeat no_terminator);
wenzelm@9132
   246
  in
wenzelm@9132
   247
    src
wenzelm@9132
   248
    |> Source.source T.stopper
wenzelm@9132
   249
      (Scan.bulk (fn xs => P.!!! (command term (cmd ())) xs))
wenzelm@9132
   250
      (if do_recover then Some recover else None)
wenzelm@9132
   251
    |> Source.mapfilter I
wenzelm@9132
   252
  end;
wenzelm@5829
   253
wenzelm@7746
   254
wenzelm@9132
   255
(* interactive source of toplevel transformers *)
wenzelm@5829
   256
wenzelm@9132
   257
fun isar term no_pos =
wenzelm@9132
   258
  Source.tty
wenzelm@9132
   259
  |> Symbol.source true
wenzelm@9132
   260
  |> T.source true get_lexicons
wenzelm@9132
   261
    (if no_pos then Position.none else Position.line_name 1 "stdin")
wenzelm@9132
   262
  |> T.source_proper
wenzelm@9132
   263
  |> toplevel_source term true get_parser;
wenzelm@6199
   264
wenzelm@6199
   265
wenzelm@7746
   266
wenzelm@9132
   267
(** read theory **)
wenzelm@6247
   268
wenzelm@9132
   269
(* deps_thy *)
wenzelm@6199
   270
wenzelm@7940
   271
fun deps_thy name ml path =
wenzelm@6199
   272
  let
wenzelm@7735
   273
    val src = Source.of_string (File.read path);
wenzelm@7735
   274
    val pos = Path.position path;
wenzelm@9132
   275
    val (name', parents, files) = ThyHeader.scan (src, pos);
wenzelm@6199
   276
    val ml_path = ThyLoad.ml_path name;
wenzelm@7940
   277
    val ml_file = if ml andalso is_some (ThyLoad.check_file ml_path) then [ml_path] else [];
wenzelm@6199
   278
  in
wenzelm@6199
   279
    if name <> name' then
wenzelm@7940
   280
      error ("Filename " ^ quote (Path.pack path) ^
wenzelm@8078
   281
        " does not agree with theory name " ^ quote name')
wenzelm@6247
   282
    else (parents, map (Path.unpack o #1) files @ ml_file)
wenzelm@6199
   283
  end;
wenzelm@6199
   284
wenzelm@7746
   285
wenzelm@9132
   286
(* load_thy *)
wenzelm@6199
   287
wenzelm@7746
   288
local
wenzelm@7746
   289
wenzelm@7940
   290
fun try_ml_file name time =
wenzelm@6199
   291
  let
wenzelm@6199
   292
    val path = ThyLoad.ml_path name;
wenzelm@7940
   293
    val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
wenzelm@6247
   294
    val tr_name = if time then "time_use" else "use";
wenzelm@6199
   295
  in
wenzelm@7243
   296
    if is_none (ThyLoad.check_file path) then ()
wenzelm@9132
   297
    else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr]
wenzelm@6199
   298
  end;
wenzelm@6199
   299
wenzelm@9132
   300
fun parse_thy src =
wenzelm@9132
   301
  src
wenzelm@9132
   302
  |> T.source_proper
wenzelm@9132
   303
  |> toplevel_source false false (K (get_parser ()))
wenzelm@7746
   304
  |> Source.exhaust;
wenzelm@5829
   305
wenzelm@6247
   306
fun run_thy name path =
wenzelm@7683
   307
  let
wenzelm@7735
   308
    val pos = Path.position path;
wenzelm@9132
   309
    val text = Library.untabify (explode (File.read path));
wenzelm@9132
   310
    val text_src = Source.of_list text;
wenzelm@9132
   311
    fun present_text () = Source.exhaust (Symbol.source false text_src);
wenzelm@7683
   312
  in
wenzelm@7735
   313
    Present.init_theory name;
wenzelm@9132
   314
    Present.verbatim_source name present_text;
wenzelm@9132
   315
    if ThyHeader.is_old (text_src, pos) then (ThySyn.load_thy name text;
wenzelm@9132
   316
      Present.old_symbol_source name present_text)   (*note: text presented twice*)
wenzelm@9132
   317
    else
wenzelm@9132
   318
      let
wenzelm@9132
   319
        val tok_src = Source.exhausted (token_source (text_src, pos));
wenzelm@9132
   320
        val out = Toplevel.excursion_result
wenzelm@9132
   321
          (IsarOutput.parse_thy markup (#1 (get_lexicons ()))
wenzelm@9132
   322
            (parse_thy tok_src) tok_src);
wenzelm@9132
   323
      in Present.theory_output name (Buffer.content out) end
wenzelm@6247
   324
  end;
wenzelm@6199
   325
wenzelm@7746
   326
in
wenzelm@7746
   327
wenzelm@6199
   328
fun load_thy name ml time path =
wenzelm@6247
   329
 (if time then
wenzelm@6247
   330
    timeit (fn () =>
wenzelm@6247
   331
     (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
wenzelm@9036
   332
      run_thy name path;
wenzelm@6247
   333
      writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
wenzelm@6247
   334
  else run_thy name path;
wenzelm@6247
   335
  Context.context (ThyInfo.get_theory name);
wenzelm@7940
   336
  if ml then try_ml_file name time else ());
wenzelm@5829
   337
wenzelm@7746
   338
end;
wenzelm@7746
   339
wenzelm@5829
   340
wenzelm@5829
   341
wenzelm@5829
   342
(** the read-eval-print loop **)
wenzelm@5829
   343
wenzelm@5923
   344
(* main loop *)
wenzelm@5923
   345
wenzelm@7333
   346
fun gen_loop term no_pos =
wenzelm@7333
   347
 (Context.reset_context ();
wenzelm@7333
   348
  Toplevel.loop (isar term no_pos));
wenzelm@5829
   349
wenzelm@7333
   350
fun gen_main term no_pos =
wenzelm@5829
   351
 (Toplevel.set_state Toplevel.toplevel;
wenzelm@6199
   352
  writeln (Session.welcome ());
wenzelm@7333
   353
  gen_loop term no_pos);
wenzelm@6860
   354
wenzelm@7333
   355
fun main () = gen_main false false;
wenzelm@7333
   356
fun loop () = gen_loop false false;
wenzelm@7333
   357
fun sync_main () = gen_main true true;
wenzelm@7333
   358
fun sync_loop () = gen_loop true true;
wenzelm@5829
   359
wenzelm@5829
   360
wenzelm@5829
   361
(* help *)
wenzelm@5829
   362
wenzelm@5829
   363
fun help () =
wenzelm@5829
   364
  writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
wenzelm@5883
   365
    \invoke 'loop();' to enter the Isar loop.");
wenzelm@5829
   366
wenzelm@5829
   367
wenzelm@6373
   368
(*final declarations of this structure!*)
wenzelm@7789
   369
val command = parser false None;
wenzelm@9132
   370
val markup_command = parser false o Some;
wenzelm@7789
   371
val improper_command = parser true None;
wenzelm@6685
   372
wenzelm@6373
   373
wenzelm@5829
   374
end;
wenzelm@5829
   375
wenzelm@6199
   376
(*setup theory syntax dependent operations*)
wenzelm@6199
   377
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
wenzelm@6199
   378
ThyLoad.load_thy_fn := OuterSyntax.load_thy;
wenzelm@6199
   379
structure ThyLoad: THY_LOAD = ThyLoad;
wenzelm@6199
   380
wenzelm@5829
   381
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
wenzelm@5829
   382
open BasicOuterSyntax;