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