src/Pure/Isar/outer_syntax.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 33223 d27956b4d3b4
child 36950 75b8f26f2f07
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm@5829
     1
(*  Title:      Pure/Isar/outer_syntax.ML
wenzelm@5829
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5829
     3
wenzelm@27353
     4
The global Isabelle/Isar outer syntax.
wenzelm@27353
     5
wenzelm@27353
     6
Note: the syntax for files is statically determined at the very
wenzelm@27353
     7
beginning; for interactive processing it may change dynamically.
wenzelm@5829
     8
*)
wenzelm@5829
     9
wenzelm@5829
    10
signature OUTER_SYNTAX =
wenzelm@5829
    11
sig
wenzelm@29311
    12
  val command: string -> string -> OuterKeyword.T ->
wenzelm@29311
    13
    (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@29311
    14
  val markup_command: ThyOutput.markup -> string -> string -> OuterKeyword.T ->
wenzelm@29311
    15
    (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@29311
    16
  val improper_command: string -> string -> OuterKeyword.T ->
wenzelm@29311
    17
    (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@29311
    18
  val internal_command: string -> (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@29380
    19
  val local_theory': string -> string -> OuterKeyword.T ->
wenzelm@29380
    20
    (bool -> local_theory -> local_theory) parser -> unit
wenzelm@26990
    21
  val local_theory: string -> string -> OuterKeyword.T ->
wenzelm@29311
    22
    (local_theory -> local_theory) parser -> unit
wenzelm@26990
    23
  val local_theory_to_proof': string -> string -> OuterKeyword.T ->
wenzelm@29311
    24
    (bool -> local_theory -> Proof.state) parser -> unit
wenzelm@26990
    25
  val local_theory_to_proof: string -> string -> OuterKeyword.T ->
wenzelm@29311
    26
    (local_theory -> Proof.state) parser -> unit
wenzelm@5883
    27
  val print_outer_syntax: unit -> unit
wenzelm@27839
    28
  val scan: Position.T -> string -> OuterLex.token list
wenzelm@25580
    29
  val parse: Position.T -> string -> Toplevel.transition list
wenzelm@26431
    30
  val process_file: Path.T -> theory -> theory
wenzelm@26600
    31
  type isar
wenzelm@26600
    32
  val isar: bool -> isar
wenzelm@27839
    33
  val prepare_command: Position.T -> string -> Toplevel.transition
wenzelm@29428
    34
  val load_thy: string -> Position.T -> string list -> bool -> unit -> unit
wenzelm@5829
    35
end;
wenzelm@5829
    36
wenzelm@26600
    37
structure OuterSyntax: OUTER_SYNTAX =
wenzelm@5829
    38
struct
wenzelm@5829
    39
wenzelm@7750
    40
structure T = OuterLex;
wenzelm@6860
    41
structure P = OuterParse;
wenzelm@29311
    42
type 'a parser = 'a P.parser;
wenzelm@29311
    43
wenzelm@6860
    44
wenzelm@5829
    45
wenzelm@5829
    46
(** outer syntax **)
wenzelm@5829
    47
wenzelm@29311
    48
(* command parsers *)
wenzelm@5829
    49
wenzelm@29311
    50
datatype command = Command of
wenzelm@24868
    51
 {comment: string,
wenzelm@24868
    52
  markup: ThyOutput.markup option,
wenzelm@24868
    53
  int_only: bool,
wenzelm@29311
    54
  parse: (Toplevel.transition -> Toplevel.transition) parser};
wenzelm@5829
    55
wenzelm@29311
    56
fun make_command comment markup int_only parse =
wenzelm@29311
    57
  Command {comment = comment, markup = markup, int_only = int_only, parse = parse};
wenzelm@5829
    58
wenzelm@5829
    59
wenzelm@5829
    60
(* parse command *)
wenzelm@5829
    61
wenzelm@6860
    62
local
wenzelm@6199
    63
wenzelm@14925
    64
fun terminate false = Scan.succeed ()
wenzelm@14925
    65
  | terminate true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
wenzelm@14925
    66
wenzelm@26620
    67
fun body cmd (name, _) =
wenzelm@7026
    68
  (case cmd name of
wenzelm@29311
    69
    SOME (Command {int_only, parse, ...}) =>
wenzelm@26620
    70
      P.!!! (Scan.prompt (name ^ "# ") (P.tags |-- parse >> pair int_only))
skalberg@15531
    71
  | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
wenzelm@6860
    72
wenzelm@6860
    73
in
wenzelm@5829
    74
wenzelm@26620
    75
fun parse_command do_terminate cmd =
skalberg@15531
    76
  P.semicolon >> K NONE ||
skalberg@15531
    77
  P.sync >> K NONE ||
wenzelm@26620
    78
  (P.position P.command :-- body cmd) --| terminate do_terminate
wenzelm@6860
    79
    >> (fn ((name, pos), (int_only, f)) =>
skalberg@15531
    80
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
    81
        Toplevel.interactive int_only |> f));
wenzelm@5829
    82
wenzelm@6199
    83
end;
wenzelm@6199
    84
wenzelm@5829
    85
wenzelm@5829
    86
wenzelm@9132
    87
(** global outer syntax **)
wenzelm@5829
    88
wenzelm@7026
    89
local
wenzelm@7026
    90
wenzelm@32738
    91
val global_commands = Unsynchronized.ref (Symtab.empty: command Symtab.table);
wenzelm@32738
    92
val global_markups = Unsynchronized.ref ([]: (string * ThyOutput.markup) list);
wenzelm@5952
    93
wenzelm@29311
    94
fun change_commands f = CRITICAL (fn () =>
wenzelm@32738
    95
 (Unsynchronized.change global_commands f;
wenzelm@23939
    96
  global_markups :=
wenzelm@29311
    97
    Symtab.fold (fn (name, Command {markup = SOME m, ...}) => cons (name, m) | _ => I)
wenzelm@29311
    98
      (! global_commands) []));
wenzelm@6722
    99
wenzelm@7026
   100
in
wenzelm@7026
   101
wenzelm@9132
   102
(* access current syntax *)
wenzelm@7026
   103
wenzelm@33223
   104
fun get_commands () = ! global_commands;
wenzelm@33223
   105
fun get_markups () = ! global_markups;
wenzelm@7026
   106
wenzelm@29311
   107
fun get_command () = Symtab.lookup (get_commands ());
wenzelm@29311
   108
fun get_syntax () = CRITICAL (fn () => (OuterKeyword.get_lexicons (), get_command ()));
wenzelm@7789
   109
wenzelm@24868
   110
fun is_markup kind name = AList.lookup (op =) (get_markups ()) name = SOME kind;
wenzelm@5829
   111
wenzelm@5829
   112
wenzelm@5829
   113
(* augment syntax *)
wenzelm@5829
   114
wenzelm@29311
   115
fun add_command name kind cmd = CRITICAL (fn () =>
wenzelm@27353
   116
 (OuterKeyword.command name kind;
wenzelm@29311
   117
  if not (Symtab.defined (get_commands ()) name) then ()
wenzelm@24868
   118
  else warning ("Redefining outer syntax command " ^ quote name);
wenzelm@29311
   119
  change_commands (Symtab.update (name, cmd))));
wenzelm@5829
   120
wenzelm@24868
   121
fun command name comment kind parse =
wenzelm@29311
   122
  add_command name kind (make_command comment NONE false parse);
wenzelm@5829
   123
wenzelm@24868
   124
fun markup_command markup name comment kind parse =
wenzelm@29311
   125
  add_command name kind (make_command comment (SOME markup) false parse);
wenzelm@24868
   126
wenzelm@24868
   127
fun improper_command name comment kind parse =
wenzelm@29311
   128
  add_command name kind (make_command comment NONE true parse);
wenzelm@7026
   129
wenzelm@7026
   130
end;
wenzelm@5829
   131
wenzelm@29311
   132
fun internal_command name parse =
wenzelm@29311
   133
  command name "(internal)" OuterKeyword.control (parse >> (fn tr => Toplevel.no_timing o tr));
wenzelm@29311
   134
wenzelm@5829
   135
wenzelm@26990
   136
(* local_theory commands *)
wenzelm@26990
   137
wenzelm@26990
   138
fun local_theory_command do_print trans name comment kind parse =
wenzelm@26990
   139
  command name comment kind (P.opt_target -- parse
wenzelm@26990
   140
    >> (fn (loc, f) => (if do_print then Toplevel.print else I) o trans loc f));
wenzelm@26990
   141
wenzelm@29380
   142
val local_theory' = local_theory_command false Toplevel.local_theory';
wenzelm@29311
   143
val local_theory = local_theory_command false Toplevel.local_theory;
wenzelm@26990
   144
val local_theory_to_proof' = local_theory_command true Toplevel.local_theory_to_proof';
wenzelm@29311
   145
val local_theory_to_proof = local_theory_command true Toplevel.local_theory_to_proof;
wenzelm@26990
   146
wenzelm@26990
   147
wenzelm@24872
   148
(* inspect syntax *)
wenzelm@7026
   149
wenzelm@29311
   150
fun dest_commands () =
wenzelm@29311
   151
  get_commands () |> Symtab.dest |> sort_wrt #1
wenzelm@29311
   152
  |> map (fn (name, Command {comment, int_only, ...}) => (name, comment, int_only));
wenzelm@5829
   153
wenzelm@9223
   154
fun print_outer_syntax () =
wenzelm@7026
   155
  let
wenzelm@27353
   156
    fun pretty_cmd (name, comment, _) =
wenzelm@7026
   157
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@29311
   158
    val (int_cmds, cmds) = List.partition #3 (dest_commands ());
wenzelm@7026
   159
  in
wenzelm@27353
   160
    [Pretty.strs ("syntax keywords:" :: map quote (OuterKeyword.dest_keywords ())),
wenzelm@18326
   161
      Pretty.big_list "commands:" (map pretty_cmd cmds),
wenzelm@18326
   162
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
wenzelm@9223
   163
    |> Pretty.chunks |> Pretty.writeln
wenzelm@7026
   164
  end;
wenzelm@5829
   165
wenzelm@5829
   166
wenzelm@5829
   167
wenzelm@9132
   168
(** toplevel parsing **)
wenzelm@5829
   169
wenzelm@9132
   170
(* basic sources *)
wenzelm@6860
   171
wenzelm@26620
   172
fun toplevel_source term do_recover cmd src =
wenzelm@9132
   173
  let
wenzelm@9132
   174
    val no_terminator =
wenzelm@9132
   175
      Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
wenzelm@23682
   176
    fun recover int =
wenzelm@23682
   177
      (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
wenzelm@9132
   178
  in
wenzelm@9132
   179
    src
wenzelm@12876
   180
    |> T.source_proper
wenzelm@9132
   181
    |> Source.source T.stopper
wenzelm@27872
   182
      (Scan.bulk (P.$$$ "--" -- P.!!! P.doc_source >> K NONE || P.not_eof >> SOME))
wenzelm@23682
   183
        (Option.map recover do_recover)
wenzelm@19482
   184
    |> Source.map_filter I
wenzelm@24868
   185
    |> Source.source T.stopper
wenzelm@26620
   186
        (Scan.bulk (fn xs => P.!!! (parse_command term (cmd ())) xs))
wenzelm@23682
   187
        (Option.map recover do_recover)
wenzelm@19482
   188
    |> Source.map_filter I
wenzelm@9132
   189
  end;
wenzelm@5829
   190
wenzelm@7746
   191
wenzelm@25580
   192
(* off-line scanning/parsing *)
wenzelm@14925
   193
wenzelm@27839
   194
fun scan pos str =
wenzelm@16195
   195
  Source.of_string str
wenzelm@27839
   196
  |> Symbol.source {do_recover = false}
wenzelm@27839
   197
  |> T.source {do_recover = SOME false} OuterKeyword.get_lexicons pos
wenzelm@16195
   198
  |> Source.exhaust;
wenzelm@16195
   199
wenzelm@25580
   200
fun parse pos str =
wenzelm@25580
   201
  Source.of_string str
wenzelm@27839
   202
  |> Symbol.source {do_recover = false}
wenzelm@27839
   203
  |> T.source {do_recover = SOME false} OuterKeyword.get_lexicons pos
wenzelm@29311
   204
  |> toplevel_source false NONE get_command
wenzelm@25580
   205
  |> Source.exhaust;
wenzelm@25580
   206
nipkow@14091
   207
wenzelm@26431
   208
(* process file *)
wenzelm@26431
   209
wenzelm@26431
   210
fun process_file path thy =
wenzelm@26431
   211
  let
wenzelm@26881
   212
    val trs = parse (Path.position path) (File.read path);
wenzelm@28424
   213
    val init = Toplevel.init_theory "" (K thy) (K ()) Toplevel.empty;
wenzelm@28424
   214
    val result = fold Toplevel.command (init :: trs) Toplevel.toplevel;
wenzelm@28424
   215
  in
wenzelm@28424
   216
    (case (Toplevel.is_theory result, Toplevel.generic_theory_of result) of
wenzelm@28424
   217
      (true, Context.Theory thy') => thy'
wenzelm@28424
   218
    | _ => error "Bad result state: global theory expected")
wenzelm@28424
   219
  end;
wenzelm@26431
   220
wenzelm@26431
   221
wenzelm@24868
   222
(* interactive source of toplevel transformers *)
wenzelm@24868
   223
wenzelm@26600
   224
type isar =
wenzelm@26600
   225
  (Toplevel.transition, (Toplevel.transition option,
wenzelm@26600
   226
    (OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token,
wenzelm@30573
   227
      (Symbol_Pos.T, Position.T * (Symbol.symbol, (string, unit) Source.source)
wenzelm@27770
   228
  Source.source) Source.source) Source.source) Source.source)
wenzelm@27770
   229
  Source.source) Source.source) Source.source) Source.source;
wenzelm@26600
   230
wenzelm@26600
   231
fun isar term : isar =
wenzelm@24868
   232
  Source.tty
wenzelm@27839
   233
  |> Symbol.source {do_recover = true}
wenzelm@27839
   234
  |> T.source {do_recover = SOME true} OuterKeyword.get_lexicons Position.none
wenzelm@29311
   235
  |> toplevel_source term (SOME true) get_command;
wenzelm@24868
   236
wenzelm@24868
   237
wenzelm@27839
   238
(* prepare toplevel commands -- fail-safe *)
wenzelm@27839
   239
wenzelm@27839
   240
val not_singleton = "Exactly one command expected";
wenzelm@27839
   241
wenzelm@29311
   242
fun prepare_span commands span =
wenzelm@27839
   243
  let
wenzelm@29315
   244
    val range_pos = Position.encode_range (ThySyntax.span_range span);
wenzelm@29315
   245
    val toks = ThySyntax.span_content span;
wenzelm@29315
   246
    val _ = List.app ThySyntax.report_token toks;
wenzelm@27839
   247
  in
wenzelm@29311
   248
    (case Source.exhaust (toplevel_source false NONE (K commands) (Source.of_list toks)) of
wenzelm@27839
   249
      [tr] => (tr, true)
wenzelm@27839
   250
    | [] => (Toplevel.ignored range_pos, false)
wenzelm@27839
   251
    | _ => (Toplevel.malformed range_pos not_singleton, true))
wenzelm@27839
   252
    handle ERROR msg => (Toplevel.malformed range_pos msg, true)
wenzelm@27839
   253
  end;
wenzelm@27839
   254
wenzelm@29311
   255
fun prepare_unit commands (cmd, proof, proper_proof) =
wenzelm@28436
   256
  let
wenzelm@29311
   257
    val (tr, proper_cmd) = prepare_span commands cmd;
wenzelm@29311
   258
    val proof_trs = map (prepare_span commands) proof |> filter #2 |> map #1;
wenzelm@28436
   259
  in
wenzelm@28436
   260
    if proper_cmd andalso proper_proof then [(tr, proof_trs)]
wenzelm@28436
   261
    else map (rpair []) (if proper_cmd then tr :: proof_trs else proof_trs)
wenzelm@28436
   262
  end;
wenzelm@28432
   263
wenzelm@27839
   264
fun prepare_command pos str =
wenzelm@29311
   265
  let val (lexs, commands) = get_syntax () in
wenzelm@29315
   266
    (case ThySyntax.parse_spans lexs pos str of
wenzelm@29311
   267
      [span] => #1 (prepare_span commands span)
wenzelm@27839
   268
    | _ => Toplevel.malformed pos not_singleton)
wenzelm@27839
   269
  end;
wenzelm@27839
   270
wenzelm@27839
   271
wenzelm@26611
   272
(* load_thy *)
wenzelm@7746
   273
wenzelm@27839
   274
fun load_thy name pos text time =
wenzelm@7683
   275
  let
wenzelm@29311
   276
    val (lexs, commands) = get_syntax ();
wenzelm@23866
   277
wenzelm@17932
   278
    val _ = Present.init_theory name;
wenzelm@27855
   279
wenzelm@29315
   280
    val toks = Source.exhausted (ThySyntax.token_source lexs pos (Source.of_list text));
wenzelm@29315
   281
    val spans = Source.exhaust (ThySyntax.span_source toks);
wenzelm@29315
   282
    val _ = List.app ThySyntax.report_span spans;
wenzelm@29315
   283
    val units = Source.exhaust (ThySyntax.unit_source (Source.of_list spans))
wenzelm@29311
   284
      |> maps (prepare_unit commands);
wenzelm@23866
   285
wenzelm@27855
   286
    val _ = Present.theory_source name
wenzelm@29315
   287
      (fn () => HTML.html_mode (implode o map ThySyntax.present_span) spans);
wenzelm@27855
   288
wenzelm@23866
   289
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@29428
   290
    val results = cond_timeit time "" (fn () => Toplevel.excursion units);
wenzelm@23866
   291
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@29428
   292
wenzelm@29428
   293
    fun after_load () =
wenzelm@29428
   294
      ThyOutput.present_thy (#1 lexs) OuterKeyword.command_tags is_markup (Lazy.force results) toks
wenzelm@29428
   295
      |> Buffer.content
wenzelm@29428
   296
      |> Present.theory_output name;
wenzelm@29428
   297
  in after_load end;
wenzelm@23866
   298
wenzelm@5829
   299
end;