src/Pure/Isar/outer_syntax.ML
author wenzelm
Sun Jul 29 22:41:58 2007 +0200 (2007-07-29 ago)
changeset 24065 21483400c2ca
parent 23939 e543359fe8b6
child 24071 82873bc360c2
permissions -rw-r--r--
load_thy: avoid reloading of text;
tuned;
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@15830
    10
  structure Isar:
wenzelm@15830
    11
    sig
wenzelm@18064
    12
      val state: unit -> Toplevel.state
wenzelm@21207
    13
      val exn: unit -> (exn * string) option
wenzelm@20023
    14
      val context: unit -> Proof.context
wenzelm@21401
    15
      val goal: unit -> thm list * thm
wenzelm@15830
    16
      val main: unit -> unit
wenzelm@15830
    17
      val loop: unit -> unit
wenzelm@15830
    18
      val sync_main: unit -> unit
wenzelm@15830
    19
      val sync_loop: unit -> unit
wenzelm@18684
    20
      val toplevel: (unit -> 'a) -> 'a
wenzelm@15830
    21
    end;
wenzelm@5829
    22
end;
wenzelm@5829
    23
wenzelm@5829
    24
signature OUTER_SYNTAX =
wenzelm@5829
    25
sig
wenzelm@5829
    26
  include BASIC_OUTER_SYNTAX
wenzelm@5829
    27
  type token
wenzelm@5829
    28
  type parser
wenzelm@23722
    29
  val get_lexicons: unit -> Scan.lexicon * Scan.lexicon
wenzelm@23796
    30
  val command_keyword: string -> OuterKeyword.T option
wenzelm@17071
    31
  val command: string -> string -> OuterKeyword.T ->
wenzelm@6373
    32
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@22120
    33
  val markup_command: ThyOutput.markup -> string -> string -> OuterKeyword.T ->
wenzelm@7789
    34
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@17071
    35
  val improper_command: string -> string -> OuterKeyword.T ->
wenzelm@6373
    36
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@14687
    37
  val is_keyword: string -> bool
wenzelm@7026
    38
  val dest_keywords: unit -> string list
wenzelm@7026
    39
  val dest_parsers: unit -> (string * string * string * bool) list
wenzelm@5883
    40
  val print_outer_syntax: unit -> unit
wenzelm@9223
    41
  val print_commands: Toplevel.transition -> Toplevel.transition
wenzelm@5829
    42
  val add_keywords: string list -> unit
wenzelm@5829
    43
  val add_parsers: parser list -> unit
wenzelm@19060
    44
  val check_text: string * Position.T -> Toplevel.node option -> unit
wenzelm@23679
    45
  val isar: bool -> unit Toplevel.isar
wenzelm@16195
    46
  val scan: string -> OuterLex.token list
wenzelm@16195
    47
  val read: OuterLex.token list -> (string * OuterLex.token list * Toplevel.transition) list
wenzelm@5829
    48
end;
wenzelm@5829
    49
aspinall@15224
    50
structure OuterSyntax : OUTER_SYNTAX  =
wenzelm@5829
    51
struct
wenzelm@5829
    52
wenzelm@7750
    53
structure T = OuterLex;
wenzelm@6860
    54
structure P = OuterParse;
wenzelm@6860
    55
wenzelm@5829
    56
wenzelm@5829
    57
(** outer syntax **)
wenzelm@5829
    58
wenzelm@5829
    59
(* parsers *)
wenzelm@5829
    60
wenzelm@7750
    61
type token = T.token;
wenzelm@5829
    62
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
wenzelm@5829
    63
wenzelm@5829
    64
datatype parser =
wenzelm@22120
    65
  Parser of string * (string * OuterKeyword.T * ThyOutput.markup option) * bool * parser_fn;
wenzelm@5829
    66
wenzelm@7750
    67
fun parser int_only markup name comment kind parse =
wenzelm@7750
    68
  Parser (name, (comment, kind, markup), int_only, parse);
wenzelm@5829
    69
wenzelm@5829
    70
wenzelm@5829
    71
(* parse command *)
wenzelm@5829
    72
wenzelm@6860
    73
local
wenzelm@6199
    74
wenzelm@14925
    75
fun terminate false = Scan.succeed ()
wenzelm@14925
    76
  | terminate true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
wenzelm@14925
    77
wenzelm@14925
    78
fun trace false parse = parse
wenzelm@14925
    79
  | trace true parse = Scan.trace parse >> (fn (f, toks) => f o Toplevel.source toks);
wenzelm@14925
    80
wenzelm@17071
    81
fun body cmd do_trace (name, _) =
wenzelm@7026
    82
  (case cmd name of
skalberg@15531
    83
    SOME (int_only, parse) =>
wenzelm@17118
    84
      P.!!! (Scan.prompt (name ^ "# ") (trace do_trace (P.tags |-- parse) >> pair int_only))
skalberg@15531
    85
  | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
wenzelm@6860
    86
wenzelm@6860
    87
in
wenzelm@5829
    88
wenzelm@14925
    89
fun command do_terminate do_trace cmd =
skalberg@15531
    90
  P.semicolon >> K NONE ||
skalberg@15531
    91
  P.sync >> K NONE ||
wenzelm@17118
    92
  (P.position P.command :-- body cmd do_trace) --| terminate do_terminate
wenzelm@6860
    93
    >> (fn ((name, pos), (int_only, f)) =>
skalberg@15531
    94
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
    95
        Toplevel.interactive int_only |> f));
wenzelm@5829
    96
wenzelm@6199
    97
end;
wenzelm@6199
    98
wenzelm@5829
    99
wenzelm@5829
   100
wenzelm@9132
   101
(** global outer syntax **)
wenzelm@5829
   102
wenzelm@7026
   103
local
wenzelm@7026
   104
wenzelm@7026
   105
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
wenzelm@7750
   106
val global_parsers =
wenzelm@22120
   107
  ref (Symtab.empty: (((string * OuterKeyword.T) * (bool * parser_fn)) * ThyOutput.markup option)
wenzelm@9132
   108
    Symtab.table);
wenzelm@22120
   109
val global_markups = ref ([]: (string * ThyOutput.markup) list);
wenzelm@5952
   110
wenzelm@23939
   111
fun change_lexicons f = CRITICAL (fn () =>
wenzelm@7026
   112
  let val lexs = f (! global_lexicons) in
wenzelm@7026
   113
    (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
wenzelm@7026
   114
      [] => global_lexicons := lexs
wenzelm@7026
   115
    | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
wenzelm@23939
   116
  end);
wenzelm@5829
   117
wenzelm@23939
   118
fun change_parsers f = CRITICAL (fn () =>
wenzelm@23939
   119
 (change global_parsers f;
wenzelm@23939
   120
  global_markups :=
wenzelm@23939
   121
    Symtab.fold (fn (name, (_, SOME m)) => cons (name, m) | _ => I) (! global_parsers) []));
wenzelm@6722
   122
wenzelm@7026
   123
in
wenzelm@7026
   124
wenzelm@7750
   125
wenzelm@9132
   126
(* access current syntax *)
wenzelm@7026
   127
wenzelm@7026
   128
(*Note: the syntax for files is statically determined at the very
wenzelm@7026
   129
  beginning; for interactive processing it may change dynamically.*)
wenzelm@7026
   130
wenzelm@7026
   131
fun get_lexicons () = ! global_lexicons;
wenzelm@7026
   132
fun get_parsers () = ! global_parsers;
wenzelm@17412
   133
fun get_parser () = Option.map (#2 o #1) o Symtab.lookup (get_parsers ());
wenzelm@7789
   134
wenzelm@23796
   135
fun command_keyword name =
wenzelm@23796
   136
  Option.map (fn (((_, k), _), _) => k) (Symtab.lookup (get_parsers ()) name);
wenzelm@23796
   137
fun command_tags name = these ((Option.map OuterKeyword.tags_of) (command_keyword name));
wenzelm@17071
   138
wenzelm@17184
   139
fun is_markup kind name = (AList.lookup (op =) (! global_markups) name = SOME kind);
wenzelm@5829
   140
wenzelm@5829
   141
wenzelm@5829
   142
(* augment syntax *)
wenzelm@5829
   143
wenzelm@22120
   144
fun add_keywords keywords =
wenzelm@22120
   145
  change_lexicons (apfst (Scan.extend_lexicon (map Symbol.explode keywords)));
wenzelm@5829
   146
wenzelm@17071
   147
fun add_parser (Parser (name, (comment, kind, markup), int_only, parse)) tab =
wenzelm@16894
   148
 (if not (Symtab.defined tab name) then ()
wenzelm@5829
   149
  else warning ("Redefined outer syntax command " ^ quote name);
wenzelm@17412
   150
  Symtab.update (name, (((comment, kind), (int_only, parse)), markup)) tab);
wenzelm@5829
   151
wenzelm@23939
   152
fun add_parsers parsers = CRITICAL (fn () =>
wenzelm@17071
   153
  (change_parsers (fold add_parser parsers);
wenzelm@22120
   154
    change_lexicons (apsnd (Scan.extend_lexicon
wenzelm@23939
   155
      (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers)))));
wenzelm@7026
   156
wenzelm@7026
   157
end;
wenzelm@5829
   158
wenzelm@5829
   159
wenzelm@7026
   160
(* print syntax *)
wenzelm@7026
   161
wenzelm@14687
   162
fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
wenzelm@7026
   163
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
wenzelm@7026
   164
wenzelm@7026
   165
fun dest_parsers () =
wenzelm@16727
   166
  get_parsers () |> Symtab.dest |> sort_wrt #1
wenzelm@17071
   167
  |> map (fn (name, (((cmt, kind), (int_only, _)), _)) =>
wenzelm@17071
   168
    (name, cmt, OuterKeyword.kind_of kind, int_only));
wenzelm@5829
   169
wenzelm@9223
   170
fun print_outer_syntax () =
wenzelm@7026
   171
  let
wenzelm@7026
   172
    fun pretty_cmd (name, comment, _, _) =
wenzelm@7026
   173
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
skalberg@15570
   174
    val (int_cmds, cmds) = List.partition #4 (dest_parsers ());
wenzelm@7026
   175
  in
wenzelm@8720
   176
    [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
wenzelm@18326
   177
      Pretty.big_list "commands:" (map pretty_cmd cmds),
wenzelm@18326
   178
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
wenzelm@9223
   179
    |> Pretty.chunks |> Pretty.writeln
wenzelm@7026
   180
  end;
wenzelm@5829
   181
wenzelm@9223
   182
val print_commands = Toplevel.imperative print_outer_syntax;
wenzelm@7367
   183
wenzelm@5829
   184
wenzelm@5829
   185
wenzelm@9132
   186
(** toplevel parsing **)
wenzelm@5829
   187
wenzelm@9132
   188
(* basic sources *)
wenzelm@6860
   189
wenzelm@17071
   190
fun toplevel_source term do_trace do_recover cmd src =
wenzelm@9132
   191
  let
wenzelm@9132
   192
    val no_terminator =
wenzelm@9132
   193
      Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
wenzelm@23682
   194
    fun recover int =
wenzelm@23682
   195
      (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
wenzelm@9132
   196
  in
wenzelm@9132
   197
    src
wenzelm@12876
   198
    |> T.source_proper
wenzelm@9132
   199
    |> Source.source T.stopper
skalberg@15531
   200
      (Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K NONE || P.not_eof >> SOME))
wenzelm@23682
   201
        (Option.map recover do_recover)
wenzelm@19482
   202
    |> Source.map_filter I
wenzelm@17071
   203
    |> Source.source T.stopper (Scan.bulk (fn xs => P.!!! (command term do_trace (cmd ())) xs))
wenzelm@23682
   204
        (Option.map recover do_recover)
wenzelm@19482
   205
    |> Source.map_filter I
wenzelm@9132
   206
  end;
wenzelm@5829
   207
wenzelm@7746
   208
wenzelm@9132
   209
(* interactive source of toplevel transformers *)
wenzelm@5829
   210
wenzelm@23679
   211
fun isar term =
wenzelm@9132
   212
  Source.tty
wenzelm@9132
   213
  |> Symbol.source true
wenzelm@23679
   214
  |> T.source (SOME true) get_lexicons Position.none
wenzelm@23679
   215
  |> toplevel_source term false (SOME true) get_parser;
wenzelm@6199
   216
wenzelm@6199
   217
wenzelm@16195
   218
(* scan text *)
wenzelm@14925
   219
aspinall@15144
   220
fun scan str =
wenzelm@16195
   221
  Source.of_string str
wenzelm@16195
   222
  |> Symbol.source false
wenzelm@23679
   223
  |> T.source (SOME false) get_lexicons Position.none
wenzelm@16195
   224
  |> Source.exhaust;
wenzelm@16195
   225
wenzelm@16195
   226
wenzelm@16195
   227
(* read tokens with trace *)
wenzelm@14925
   228
aspinall@15144
   229
fun read toks =
aspinall@15144
   230
  Source.of_list toks
wenzelm@23679
   231
  |> toplevel_source false true (SOME false) get_parser
wenzelm@14925
   232
  |> Source.exhaust
wenzelm@15973
   233
  |> map (fn tr => (Toplevel.name_of tr, the (Toplevel.source_of tr), tr));
nipkow@14091
   234
nipkow@14091
   235
wenzelm@16195
   236
wenzelm@9132
   237
(** read theory **)
wenzelm@6247
   238
wenzelm@12943
   239
(* check_text *)
wenzelm@12943
   240
wenzelm@22120
   241
fun check_text s state = (ThyOutput.eval_antiquote (#1 (get_lexicons ())) state s; ());
wenzelm@12943
   242
wenzelm@12943
   243
wenzelm@9132
   244
(* load_thy *)
wenzelm@6199
   245
wenzelm@7746
   246
local
wenzelm@7746
   247
wenzelm@23884
   248
fun try_ml_file dir name time =
wenzelm@20323
   249
  let val path = ThyLoad.ml_path name in
wenzelm@23884
   250
    if is_none (ThyLoad.check_file dir path) then ()
wenzelm@20323
   251
    else
wenzelm@20323
   252
      let
wenzelm@22826
   253
        val _ = legacy_feature ("loading attached ML script " ^ quote (Path.implode path));
wenzelm@20323
   254
        val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
wenzelm@20323
   255
        val tr_name = if time then "time_use" else "use";
wenzelm@20323
   256
      in Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr] end
wenzelm@6199
   257
  end;
wenzelm@6199
   258
wenzelm@24065
   259
fun run_thy dir name pos text time =
wenzelm@7683
   260
  let
wenzelm@24065
   261
    val text_src = Source.of_list (Library.untabify text);
wenzelm@23866
   262
wenzelm@17932
   263
    val _ = Present.init_theory name;
wenzelm@24065
   264
    val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text_src));
wenzelm@24065
   265
    val toks = text_src
wenzelm@17932
   266
      |> Symbol.source false
wenzelm@24065
   267
      |> T.source NONE (K (get_lexicons ())) pos
wenzelm@17932
   268
      |> Source.exhausted;
wenzelm@17932
   269
    val trs = toks
wenzelm@23679
   270
      |> toplevel_source false false NONE (K (get_parser ()))
wenzelm@17932
   271
      |> Source.exhaust;
wenzelm@23866
   272
wenzelm@23866
   273
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@23866
   274
    val _ = cond_timeit time (fn () =>
wenzelm@23866
   275
      ThyOutput.process_thy (#1 (get_lexicons ())) command_tags is_markup trs toks
wenzelm@23866
   276
      |> Buffer.content
wenzelm@23866
   277
      |> Present.theory_output name);
wenzelm@23866
   278
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@24065
   279
  in () end;
wenzelm@23866
   280
wenzelm@24065
   281
fun load_thy dir name pos text ml time =
wenzelm@24065
   282
 (run_thy dir name pos text time;
wenzelm@24065
   283
  ML_Context.set_context (SOME (Context.Theory (ThyInfo.get_theory name)));
wenzelm@24065
   284
  if ml then try_ml_file dir name time else ());
wenzelm@6199
   285
wenzelm@7746
   286
in
wenzelm@7746
   287
wenzelm@23866
   288
val _ = ThyLoad.load_thy_fn := load_thy;
wenzelm@5829
   289
wenzelm@7746
   290
end;
wenzelm@7746
   291
wenzelm@5829
   292
wenzelm@5829
   293
wenzelm@5829
   294
(** the read-eval-print loop **)
wenzelm@5829
   295
wenzelm@5923
   296
(* main loop *)
wenzelm@5923
   297
wenzelm@23679
   298
fun gen_loop term =
wenzelm@22120
   299
 (ML_Context.set_context NONE;
wenzelm@23679
   300
  Toplevel.loop (isar term));
wenzelm@5829
   301
wenzelm@23679
   302
fun gen_main term =
wenzelm@21957
   303
 (Toplevel.init_state ();
wenzelm@6199
   304
  writeln (Session.welcome ());
wenzelm@23679
   305
  gen_loop term);
wenzelm@6860
   306
wenzelm@15830
   307
structure Isar =
wenzelm@15830
   308
struct
wenzelm@18064
   309
  val state = Toplevel.get_state;
wenzelm@18064
   310
  val exn = Toplevel.exn;
wenzelm@21401
   311
wenzelm@21207
   312
  fun context () =
wenzelm@21506
   313
    Toplevel.context_of (state ())
wenzelm@21207
   314
      handle Toplevel.UNDEF => error "Unknown context";
wenzelm@21401
   315
wenzelm@21401
   316
  fun goal () =
wenzelm@21401
   317
    #2 (Proof.get_goal (Toplevel.proof_of (state ())))
wenzelm@21401
   318
      handle Toplevel.UNDEF => error "No goal present";
wenzelm@21401
   319
wenzelm@23679
   320
  fun main () = gen_main false;
wenzelm@23679
   321
  fun loop () = gen_loop false;
wenzelm@23679
   322
  fun sync_main () = gen_main true;
wenzelm@23679
   323
  fun sync_loop () = gen_loop true;
wenzelm@18684
   324
  val toplevel = Toplevel.program;
wenzelm@15830
   325
end;
wenzelm@5829
   326
wenzelm@5829
   327
wenzelm@6373
   328
(*final declarations of this structure!*)
skalberg@15531
   329
val command = parser false NONE;
skalberg@15531
   330
val markup_command = parser false o SOME;
skalberg@15531
   331
val improper_command = parser true NONE;
wenzelm@6685
   332
wenzelm@5829
   333
end;
wenzelm@5829
   334
wenzelm@6199
   335
structure ThyLoad: THY_LOAD = ThyLoad;
wenzelm@5829
   336
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
wenzelm@5829
   337
open BasicOuterSyntax;