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