src/Pure/Isar/outer_syntax.ML
author wenzelm
Tue Dec 05 22:14:42 2006 +0100 (2006-12-05)
changeset 21658 5e31241e1e3c
parent 21506 b2a673894ce5
child 21858 05f57309170c
permissions -rw-r--r--
Attrib.internal: morphism;
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@17071
    29
  val command: string -> string -> OuterKeyword.T ->
wenzelm@6373
    30
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@17071
    31
  val markup_command: IsarOutput.markup -> string -> string -> OuterKeyword.T ->
wenzelm@7789
    32
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@17071
    33
  val improper_command: string -> string -> OuterKeyword.T ->
wenzelm@6373
    34
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@14687
    35
  val is_keyword: string -> bool
wenzelm@7026
    36
  val dest_keywords: unit -> string list
wenzelm@7026
    37
  val dest_parsers: unit -> (string * string * string * bool) list
wenzelm@5883
    38
  val print_outer_syntax: unit -> unit
wenzelm@9223
    39
  val print_commands: Toplevel.transition -> Toplevel.transition
wenzelm@5829
    40
  val add_keywords: string list -> unit
wenzelm@5829
    41
  val add_parsers: parser list -> unit
wenzelm@19060
    42
  val check_text: string * Position.T -> Toplevel.node option -> unit
wenzelm@7940
    43
  val deps_thy: string -> bool -> Path.T -> string list * Path.T list
wenzelm@6199
    44
  val load_thy: string -> bool -> bool -> Path.T -> unit
nipkow@14091
    45
  val isar: bool -> 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@17071
    65
  Parser of string * (string * OuterKeyword.T * IsarOutput.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@17071
   107
  ref (Symtab.empty: (((string * OuterKeyword.T) * (bool * parser_fn)) * IsarOutput.markup option)
wenzelm@9132
   108
    Symtab.table);
wenzelm@9132
   109
val global_markups = ref ([]: (string * IsarOutput.markup) list);
wenzelm@5952
   110
wenzelm@7026
   111
fun change_lexicons f =
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@7026
   116
  end;
wenzelm@5829
   117
wenzelm@17071
   118
fun make_markups () = global_markups :=
wenzelm@17071
   119
  Symtab.fold (fn (name, (_, SOME m)) => cons (name, m) | _ => I) (! global_parsers) [];
wenzelm@7750
   120
wenzelm@9132
   121
fun change_parsers f = (Library.change global_parsers f; make_markups ());
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@17071
   135
fun command_tags name =
wenzelm@17412
   136
  (case Symtab.lookup (get_parsers ()) name of
wenzelm@17071
   137
    SOME (((_, k), _), _) => OuterKeyword.tags_of k
wenzelm@17071
   138
  | NONE => []);
wenzelm@17071
   139
wenzelm@17184
   140
fun is_markup kind name = (AList.lookup (op =) (! global_markups) name = SOME kind);
wenzelm@5829
   141
wenzelm@5829
   142
wenzelm@5829
   143
(* augment syntax *)
wenzelm@5829
   144
wenzelm@7026
   145
fun add_keywords keywords = change_lexicons (apfst (fn lex =>
wenzelm@7026
   146
  (Scan.extend_lexicon lex (map Symbol.explode keywords))));
wenzelm@5829
   147
wenzelm@17071
   148
fun add_parser (Parser (name, (comment, kind, markup), int_only, parse)) tab =
wenzelm@16894
   149
 (if not (Symtab.defined tab name) then ()
wenzelm@5829
   150
  else warning ("Redefined outer syntax command " ^ quote name);
wenzelm@17412
   151
  Symtab.update (name, (((comment, kind), (int_only, parse)), markup)) tab);
wenzelm@5829
   152
wenzelm@5829
   153
fun add_parsers parsers =
wenzelm@17071
   154
  (change_parsers (fold add_parser parsers);
wenzelm@7026
   155
    change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
wenzelm@7026
   156
      (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
wenzelm@7026
   157
wenzelm@7026
   158
end;
wenzelm@5829
   159
wenzelm@5829
   160
wenzelm@7026
   161
(* print syntax *)
wenzelm@7026
   162
wenzelm@14687
   163
fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
wenzelm@7026
   164
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
wenzelm@7026
   165
wenzelm@7026
   166
fun dest_parsers () =
wenzelm@16727
   167
  get_parsers () |> Symtab.dest |> sort_wrt #1
wenzelm@17071
   168
  |> map (fn (name, (((cmt, kind), (int_only, _)), _)) =>
wenzelm@17071
   169
    (name, cmt, OuterKeyword.kind_of kind, int_only));
wenzelm@5829
   170
wenzelm@9223
   171
fun print_outer_syntax () =
wenzelm@7026
   172
  let
wenzelm@7026
   173
    fun pretty_cmd (name, comment, _, _) =
wenzelm@7026
   174
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
skalberg@15570
   175
    val (int_cmds, cmds) = List.partition #4 (dest_parsers ());
wenzelm@7026
   176
  in
wenzelm@8720
   177
    [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
wenzelm@18326
   178
      Pretty.big_list "commands:" (map pretty_cmd cmds),
wenzelm@18326
   179
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
wenzelm@9223
   180
    |> Pretty.chunks |> Pretty.writeln
wenzelm@7026
   181
  end;
wenzelm@5829
   182
wenzelm@9223
   183
val print_commands = Toplevel.imperative print_outer_syntax;
wenzelm@7367
   184
wenzelm@5829
   185
wenzelm@5829
   186
wenzelm@9132
   187
(** toplevel parsing **)
wenzelm@5829
   188
wenzelm@9132
   189
(* basic sources *)
wenzelm@6860
   190
wenzelm@17071
   191
fun toplevel_source term do_trace do_recover cmd src =
wenzelm@9132
   192
  let
wenzelm@9132
   193
    val no_terminator =
wenzelm@9132
   194
      Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
skalberg@15531
   195
    fun recover x = (Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]) x;
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))
skalberg@15531
   201
      (if do_recover then SOME recover else NONE)
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))
skalberg@15531
   204
      (if do_recover then SOME recover else NONE)
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@9132
   211
fun isar term no_pos =
wenzelm@9132
   212
  Source.tty
wenzelm@9132
   213
  |> Symbol.source true
wenzelm@9132
   214
  |> T.source true get_lexicons
wenzelm@9132
   215
    (if no_pos then Position.none else Position.line_name 1 "stdin")
wenzelm@14925
   216
  |> toplevel_source term false true get_parser;
wenzelm@6199
   217
wenzelm@6199
   218
wenzelm@16195
   219
(* scan text *)
wenzelm@14925
   220
aspinall@15144
   221
fun scan str =
wenzelm@16195
   222
  Source.of_string str
wenzelm@16195
   223
  |> Symbol.source false
wenzelm@16195
   224
  |> T.source true get_lexicons Position.none
wenzelm@16195
   225
  |> Source.exhaust;
wenzelm@16195
   226
wenzelm@16195
   227
wenzelm@16195
   228
(* read tokens with trace *)
wenzelm@14925
   229
aspinall@15144
   230
fun read toks =
aspinall@15144
   231
  Source.of_list toks
wenzelm@14925
   232
  |> toplevel_source false true true get_parser
wenzelm@14925
   233
  |> Source.exhaust
wenzelm@15973
   234
  |> map (fn tr => (Toplevel.name_of tr, the (Toplevel.source_of tr), tr));
nipkow@14091
   235
nipkow@14091
   236
wenzelm@16195
   237
wenzelm@9132
   238
(** read theory **)
wenzelm@6247
   239
wenzelm@12943
   240
(* check_text *)
wenzelm@12943
   241
wenzelm@17265
   242
fun check_text s state = (IsarOutput.eval_antiquote (#1 (get_lexicons ())) state s; ());
wenzelm@12943
   243
wenzelm@12943
   244
wenzelm@9132
   245
(* deps_thy *)
wenzelm@6199
   246
wenzelm@7940
   247
fun deps_thy name ml path =
wenzelm@6199
   248
  let
wenzelm@7735
   249
    val src = Source.of_string (File.read path);
wenzelm@7735
   250
    val pos = Path.position path;
wenzelm@17932
   251
    val (name', parents, files) = ThyHeader.read (src, pos);
wenzelm@6199
   252
    val ml_path = ThyLoad.ml_path name;
wenzelm@15973
   253
    val ml_file = if ml andalso is_some (ThyLoad.check_file NONE ml_path) then [ml_path] else [];
wenzelm@6199
   254
  in
wenzelm@6199
   255
    if name <> name' then
wenzelm@7940
   256
      error ("Filename " ^ quote (Path.pack path) ^
wenzelm@8078
   257
        " does not agree with theory name " ^ quote name')
wenzelm@6247
   258
    else (parents, map (Path.unpack o #1) files @ ml_file)
wenzelm@6199
   259
  end;
wenzelm@6199
   260
wenzelm@7746
   261
wenzelm@9132
   262
(* load_thy *)
wenzelm@6199
   263
wenzelm@7746
   264
local
wenzelm@7746
   265
wenzelm@7940
   266
fun try_ml_file name time =
wenzelm@20323
   267
  let val path = ThyLoad.ml_path name in
skalberg@15531
   268
    if is_none (ThyLoad.check_file NONE path) then ()
wenzelm@20323
   269
    else
wenzelm@20323
   270
      let
wenzelm@20323
   271
        val _ = warning ("Loading legacy ML script " ^ quote (Path.pack path));
wenzelm@20323
   272
        val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
wenzelm@20323
   273
        val tr_name = if time then "time_use" else "use";
wenzelm@20323
   274
      in Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr] end
wenzelm@6199
   275
  end;
wenzelm@6199
   276
wenzelm@18717
   277
fun run_thy name path =
wenzelm@7683
   278
  let
wenzelm@17932
   279
    val text = Source.of_list (Library.untabify (explode (File.read path)));
wenzelm@17932
   280
    val _ = Present.init_theory name;
wenzelm@17932
   281
    val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text));
wenzelm@17932
   282
    val toks = text
wenzelm@17932
   283
      |> Symbol.source false
wenzelm@17932
   284
      |> T.source false (K (get_lexicons ())) (Path.position path)
wenzelm@17932
   285
      |> Source.exhausted;
wenzelm@17932
   286
    val trs = toks
wenzelm@17932
   287
      |> toplevel_source false false false (K (get_parser ()))
wenzelm@17932
   288
      |> Source.exhaust;
wenzelm@7683
   289
  in
wenzelm@17932
   290
    IsarOutput.present_thy (#1 (get_lexicons ())) command_tags is_markup trs toks
wenzelm@17932
   291
    |> Buffer.content
wenzelm@17932
   292
    |> Present.theory_output name
wenzelm@18717
   293
  end;
wenzelm@6199
   294
wenzelm@7746
   295
in
wenzelm@7746
   296
wenzelm@6199
   297
fun load_thy name ml time path =
wenzelm@6247
   298
 (if time then
wenzelm@6247
   299
    timeit (fn () =>
wenzelm@6247
   300
     (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
wenzelm@9036
   301
      run_thy name path;
wenzelm@6247
   302
      writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
wenzelm@6247
   303
  else run_thy name path;
wenzelm@6247
   304
  Context.context (ThyInfo.get_theory name);
wenzelm@7940
   305
  if ml then try_ml_file name time else ());
wenzelm@5829
   306
wenzelm@7746
   307
end;
wenzelm@7746
   308
wenzelm@5829
   309
wenzelm@5829
   310
wenzelm@5829
   311
(** the read-eval-print loop **)
wenzelm@5829
   312
wenzelm@5923
   313
(* main loop *)
wenzelm@5923
   314
wenzelm@7333
   315
fun gen_loop term no_pos =
wenzelm@7333
   316
 (Context.reset_context ();
wenzelm@15989
   317
  Toplevel.loop (isar term no_pos);
wenzelm@15989
   318
  ml_prompts "ML> " "ML# ");
wenzelm@5829
   319
wenzelm@7333
   320
fun gen_main term no_pos =
wenzelm@5829
   321
 (Toplevel.set_state Toplevel.toplevel;
wenzelm@6199
   322
  writeln (Session.welcome ());
wenzelm@7333
   323
  gen_loop term no_pos);
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@15830
   338
  fun main () = gen_main false false;
wenzelm@15830
   339
  fun loop () = gen_loop false false;
wenzelm@15830
   340
  fun sync_main () = gen_main true true;
wenzelm@15830
   341
  fun sync_loop () = gen_loop true 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;