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