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