src/Pure/Isar/outer_syntax.ML
author wenzelm
Thu Jul 05 15:40:57 2012 +0200 (2012-07-05 ago)
changeset 48191 c1def7433a72
parent 47416 df8fc0567a3d
child 48638 22d65e375c01
permissions -rw-r--r--
internalize error into command transaction -- relevant for commands that are declared via 'keywords', but not defined yet;
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@43711
    12
  type outer_syntax
wenzelm@43712
    13
  val is_markup: outer_syntax -> Thy_Output.markup -> string -> bool
wenzelm@43711
    14
  val get_syntax: unit -> (Scan.lexicon * Scan.lexicon) * outer_syntax
wenzelm@46970
    15
  val check_syntax: unit -> unit
wenzelm@46961
    16
  type command_spec = string * Keyword.T
wenzelm@46961
    17
  val command: command_spec -> string ->
wenzelm@29311
    18
    (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@46961
    19
  val markup_command: Thy_Output.markup -> command_spec -> string ->
wenzelm@29311
    20
    (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@46961
    21
  val improper_command: command_spec -> string ->
wenzelm@43711
    22
    (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@46961
    23
  val local_theory': command_spec -> string ->
wenzelm@29380
    24
    (bool -> local_theory -> local_theory) parser -> unit
wenzelm@46961
    25
  val local_theory: command_spec -> string ->
wenzelm@29311
    26
    (local_theory -> local_theory) parser -> unit
wenzelm@46961
    27
  val local_theory_to_proof': command_spec -> string ->
wenzelm@29311
    28
    (bool -> local_theory -> Proof.state) parser -> unit
wenzelm@46961
    29
  val local_theory_to_proof: command_spec -> string ->
wenzelm@29311
    30
    (local_theory -> Proof.state) parser -> unit
wenzelm@5883
    31
  val print_outer_syntax: unit -> unit
wenzelm@36959
    32
  val scan: Position.T -> string -> Token.T list
wenzelm@25580
    33
  val parse: Position.T -> string -> Toplevel.transition list
wenzelm@26431
    34
  val process_file: Path.T -> theory -> theory
wenzelm@26600
    35
  type isar
wenzelm@38253
    36
  val isar: TextIO.instream -> bool -> isar
wenzelm@46876
    37
  val read_span: outer_syntax -> Token.T list -> Toplevel.transition * bool
wenzelm@44478
    38
  val read_element: outer_syntax -> (unit -> theory) -> Thy_Syntax.element ->
wenzelm@43712
    39
    (Toplevel.transition * Toplevel.transition list) list
wenzelm@5829
    40
end;
wenzelm@5829
    41
wenzelm@36953
    42
structure Outer_Syntax: OUTER_SYNTAX =
wenzelm@5829
    43
struct
wenzelm@5829
    44
wenzelm@5829
    45
(** outer syntax **)
wenzelm@5829
    46
wenzelm@29311
    47
(* command parsers *)
wenzelm@5829
    48
wenzelm@29311
    49
datatype command = Command of
wenzelm@24868
    50
 {comment: string,
wenzelm@37216
    51
  markup: Thy_Output.markup option,
wenzelm@24868
    52
  int_only: bool,
wenzelm@29311
    53
  parse: (Toplevel.transition -> Toplevel.transition) parser};
wenzelm@5829
    54
wenzelm@29311
    55
fun make_command comment markup int_only parse =
wenzelm@29311
    56
  Command {comment = comment, markup = markup, int_only = int_only, parse = parse};
wenzelm@5829
    57
wenzelm@5829
    58
wenzelm@5829
    59
(* parse command *)
wenzelm@5829
    60
wenzelm@6860
    61
local
wenzelm@6199
    62
wenzelm@14925
    63
fun terminate false = Scan.succeed ()
wenzelm@44357
    64
  | terminate true =
wenzelm@44357
    65
      Parse.group (fn () => "end of input")
wenzelm@44357
    66
        (Scan.option Parse.sync -- Parse.semicolon >> K ());
wenzelm@14925
    67
wenzelm@26620
    68
fun body cmd (name, _) =
wenzelm@7026
    69
  (case cmd name of
wenzelm@29311
    70
    SOME (Command {int_only, parse, ...}) =>
wenzelm@36950
    71
      Parse.!!! (Scan.prompt (name ^ "# ") (Parse.tags |-- parse >> pair int_only))
wenzelm@48191
    72
  | NONE =>
wenzelm@48191
    73
      Scan.succeed (false, Toplevel.imperative (fn () =>
wenzelm@48191
    74
        error ("Bad parser for outer syntax command " ^ quote name))));
wenzelm@6860
    75
wenzelm@6860
    76
in
wenzelm@5829
    77
wenzelm@26620
    78
fun parse_command do_terminate cmd =
wenzelm@36950
    79
  Parse.semicolon >> K NONE ||
wenzelm@36950
    80
  Parse.sync >> K NONE ||
wenzelm@36950
    81
  (Parse.position Parse.command :-- body cmd) --| terminate do_terminate
wenzelm@6860
    82
    >> (fn ((name, pos), (int_only, f)) =>
skalberg@15531
    83
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
    84
        Toplevel.interactive int_only |> f));
wenzelm@5829
    85
wenzelm@6199
    86
end;
wenzelm@6199
    87
wenzelm@5829
    88
wenzelm@43711
    89
(* type outer_syntax *)
wenzelm@43711
    90
wenzelm@43711
    91
datatype outer_syntax = Outer_Syntax of
wenzelm@43711
    92
 {commands: command Symtab.table,
wenzelm@43711
    93
  markups: (string * Thy_Output.markup) list};
wenzelm@43711
    94
wenzelm@43711
    95
fun make_outer_syntax commands markups =
wenzelm@43711
    96
  Outer_Syntax {commands = commands, markups = markups};
wenzelm@43711
    97
wenzelm@43711
    98
val empty_outer_syntax = make_outer_syntax Symtab.empty [];
wenzelm@43711
    99
wenzelm@43711
   100
wenzelm@43711
   101
fun map_commands f (Outer_Syntax {commands, ...}) =
wenzelm@43711
   102
  let
wenzelm@43711
   103
    val commands' = f commands;
wenzelm@43711
   104
    val markups' =
wenzelm@43711
   105
      Symtab.fold (fn (name, Command {markup = SOME m, ...}) => cons (name, m) | _ => I)
wenzelm@43711
   106
        commands' [];
wenzelm@43711
   107
  in make_outer_syntax commands' markups' end;
wenzelm@43711
   108
wenzelm@43711
   109
fun dest_commands (Outer_Syntax {commands, ...}) =
wenzelm@43711
   110
  commands |> Symtab.dest |> sort_wrt #1
wenzelm@43711
   111
  |> map (fn (name, Command {comment, int_only, ...}) => (name, comment, int_only));
wenzelm@43711
   112
wenzelm@43711
   113
fun lookup_commands (Outer_Syntax {commands, ...}) = Symtab.lookup commands;
wenzelm@43711
   114
wenzelm@43711
   115
fun is_markup (Outer_Syntax {markups, ...}) kind name =
wenzelm@43711
   116
  AList.lookup (op =) markups name = SOME kind;
wenzelm@43711
   117
wenzelm@43711
   118
wenzelm@5829
   119
wenzelm@9132
   120
(** global outer syntax **)
wenzelm@5829
   121
wenzelm@46961
   122
type command_spec = string * Keyword.T;
wenzelm@46961
   123
wenzelm@7026
   124
local
wenzelm@7026
   125
wenzelm@43711
   126
(*synchronized wrt. Keywords*)
wenzelm@43711
   127
val global_outer_syntax = Unsynchronized.ref empty_outer_syntax;
wenzelm@5952
   128
wenzelm@46961
   129
fun add_command (name, kind) cmd = CRITICAL (fn () =>
wenzelm@46950
   130
  let
wenzelm@46950
   131
    val thy = ML_Context.the_global_context ();
wenzelm@46950
   132
    val _ =
wenzelm@46950
   133
      (case try (Thy_Header.the_keyword thy) name of
wenzelm@46961
   134
        SOME spec =>
wenzelm@47014
   135
          if Option.map #1 spec = SOME (Keyword.kind_of kind) then ()
wenzelm@46950
   136
          else error ("Inconsistent outer syntax keyword declaration " ^ quote name)
wenzelm@46950
   137
      | NONE =>
wenzelm@46961
   138
          if Context.theory_name thy = Context.PureN
wenzelm@46961
   139
          then Keyword.define (name, SOME kind)
wenzelm@46957
   140
          else error ("Undeclared outer syntax command " ^ quote name));
wenzelm@46950
   141
  in
wenzelm@46950
   142
    Unsynchronized.change global_outer_syntax (map_commands (fn commands =>
wenzelm@46950
   143
     (if not (Symtab.defined commands name) then ()
wenzelm@46950
   144
      else warning ("Redefining outer syntax command " ^ quote name);
wenzelm@46950
   145
      Symtab.update (name, cmd) commands)))
wenzelm@46950
   146
  end);
wenzelm@6722
   147
wenzelm@7026
   148
in
wenzelm@7026
   149
wenzelm@43711
   150
fun get_syntax () = CRITICAL (fn () => (Keyword.get_lexicons (), ! global_outer_syntax));
wenzelm@7789
   151
wenzelm@46970
   152
fun check_syntax () =
wenzelm@46970
   153
  let
wenzelm@46970
   154
    val ((_, major), syntax) = CRITICAL (fn () => (Keyword.dest (), ! global_outer_syntax));
wenzelm@46970
   155
  in
wenzelm@46970
   156
    (case subtract (op =) (map #1 (dest_commands syntax)) major of
wenzelm@46970
   157
      [] => ()
wenzelm@46970
   158
    | missing => error ("Missing outer syntax command(s) " ^ commas_quote missing))
wenzelm@46970
   159
  end;
wenzelm@46970
   160
wenzelm@43711
   161
fun lookup_commands_dynamic () = lookup_commands (! global_outer_syntax);
wenzelm@5829
   162
wenzelm@46961
   163
fun command command_spec comment parse =
wenzelm@46961
   164
  add_command command_spec (make_command comment NONE false parse);
wenzelm@24868
   165
wenzelm@46961
   166
fun markup_command markup command_spec comment parse =
wenzelm@46961
   167
  add_command command_spec (make_command comment (SOME markup) false parse);
wenzelm@7026
   168
wenzelm@46961
   169
fun improper_command command_spec comment parse =
wenzelm@46961
   170
  add_command command_spec (make_command comment NONE true parse);
wenzelm@29311
   171
wenzelm@43711
   172
end;
wenzelm@43711
   173
wenzelm@5829
   174
wenzelm@26990
   175
(* local_theory commands *)
wenzelm@26990
   176
wenzelm@46961
   177
fun local_theory_command do_print trans command_spec comment parse =
wenzelm@46961
   178
  command command_spec comment (Parse.opt_target -- parse
wenzelm@26990
   179
    >> (fn (loc, f) => (if do_print then Toplevel.print else I) o trans loc f));
wenzelm@26990
   180
wenzelm@29380
   181
val local_theory' = local_theory_command false Toplevel.local_theory';
wenzelm@29311
   182
val local_theory = local_theory_command false Toplevel.local_theory;
wenzelm@26990
   183
val local_theory_to_proof' = local_theory_command true Toplevel.local_theory_to_proof';
wenzelm@29311
   184
val local_theory_to_proof = local_theory_command true Toplevel.local_theory_to_proof;
wenzelm@26990
   185
wenzelm@26990
   186
wenzelm@24872
   187
(* inspect syntax *)
wenzelm@7026
   188
wenzelm@9223
   189
fun print_outer_syntax () =
wenzelm@7026
   190
  let
wenzelm@46957
   191
    val ((keywords, _), outer_syntax) =
wenzelm@46957
   192
      CRITICAL (fn () => (Keyword.dest (), #2 (get_syntax ())));
wenzelm@27353
   193
    fun pretty_cmd (name, comment, _) =
wenzelm@7026
   194
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@43711
   195
    val (int_cmds, cmds) = List.partition #3 (dest_commands outer_syntax);
wenzelm@7026
   196
  in
wenzelm@43711
   197
    [Pretty.strs ("syntax keywords:" :: map quote keywords),
wenzelm@18326
   198
      Pretty.big_list "commands:" (map pretty_cmd cmds),
wenzelm@18326
   199
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
wenzelm@9223
   200
    |> Pretty.chunks |> Pretty.writeln
wenzelm@7026
   201
  end;
wenzelm@5829
   202
wenzelm@5829
   203
wenzelm@5829
   204
wenzelm@9132
   205
(** toplevel parsing **)
wenzelm@5829
   206
wenzelm@9132
   207
(* basic sources *)
wenzelm@6860
   208
wenzelm@26620
   209
fun toplevel_source term do_recover cmd src =
wenzelm@9132
   210
  let
wenzelm@9132
   211
    val no_terminator =
wenzelm@36959
   212
      Scan.unless Parse.semicolon (Scan.one (Token.not_sync andf Token.not_eof));
wenzelm@23682
   213
    fun recover int =
wenzelm@23682
   214
      (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
wenzelm@9132
   215
  in
wenzelm@9132
   216
    src
wenzelm@36959
   217
    |> Token.source_proper
wenzelm@36959
   218
    |> Source.source Token.stopper
wenzelm@36950
   219
      (Scan.bulk (Parse.$$$ "--" -- Parse.!!! Parse.doc_source >> K NONE || Parse.not_eof >> SOME))
wenzelm@23682
   220
        (Option.map recover do_recover)
wenzelm@19482
   221
    |> Source.map_filter I
wenzelm@36959
   222
    |> Source.source Token.stopper
wenzelm@36950
   223
        (Scan.bulk (fn xs => Parse.!!! (parse_command term (cmd ())) xs))
wenzelm@23682
   224
        (Option.map recover do_recover)
wenzelm@19482
   225
    |> Source.map_filter I
wenzelm@9132
   226
  end;
wenzelm@5829
   227
wenzelm@7746
   228
wenzelm@25580
   229
(* off-line scanning/parsing *)
wenzelm@14925
   230
wenzelm@27839
   231
fun scan pos str =
wenzelm@16195
   232
  Source.of_string str
wenzelm@40523
   233
  |> Symbol.source
wenzelm@36959
   234
  |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
wenzelm@16195
   235
  |> Source.exhaust;
wenzelm@16195
   236
wenzelm@25580
   237
fun parse pos str =
wenzelm@25580
   238
  Source.of_string str
wenzelm@40523
   239
  |> Symbol.source
wenzelm@36959
   240
  |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
wenzelm@43711
   241
  |> toplevel_source false NONE lookup_commands_dynamic
wenzelm@25580
   242
  |> Source.exhaust;
wenzelm@25580
   243
nipkow@14091
   244
wenzelm@26431
   245
(* process file *)
wenzelm@26431
   246
wenzelm@26431
   247
fun process_file path thy =
wenzelm@26431
   248
  let
wenzelm@26881
   249
    val trs = parse (Path.position path) (File.read path);
wenzelm@44187
   250
    val init = Toplevel.init_theory (K thy) Toplevel.empty;
wenzelm@28424
   251
    val result = fold Toplevel.command (init :: trs) Toplevel.toplevel;
wenzelm@28424
   252
  in
wenzelm@28424
   253
    (case (Toplevel.is_theory result, Toplevel.generic_theory_of result) of
wenzelm@28424
   254
      (true, Context.Theory thy') => thy'
wenzelm@28424
   255
    | _ => error "Bad result state: global theory expected")
wenzelm@28424
   256
  end;
wenzelm@26431
   257
wenzelm@26431
   258
wenzelm@24868
   259
(* interactive source of toplevel transformers *)
wenzelm@24868
   260
wenzelm@26600
   261
type isar =
wenzelm@26600
   262
  (Toplevel.transition, (Toplevel.transition option,
wenzelm@36959
   263
    (Token.T, (Token.T option, (Token.T, (Token.T,
wenzelm@30573
   264
      (Symbol_Pos.T, Position.T * (Symbol.symbol, (string, unit) Source.source)
wenzelm@27770
   265
  Source.source) Source.source) Source.source) Source.source)
wenzelm@27770
   266
  Source.source) Source.source) Source.source) Source.source;
wenzelm@26600
   267
wenzelm@38253
   268
fun isar in_stream term : isar =
wenzelm@38253
   269
  Source.tty in_stream
wenzelm@40523
   270
  |> Symbol.source
wenzelm@36959
   271
  |> Token.source {do_recover = SOME true} Keyword.get_lexicons Position.none
wenzelm@43711
   272
  |> toplevel_source term (SOME true) lookup_commands_dynamic;
wenzelm@24868
   273
wenzelm@24868
   274
wenzelm@44478
   275
(* read toplevel commands -- fail-safe *)
wenzelm@27839
   276
wenzelm@27839
   277
val not_singleton = "Exactly one command expected";
wenzelm@27839
   278
wenzelm@44658
   279
fun read_span outer_syntax toks =
wenzelm@27839
   280
  let
wenzelm@43711
   281
    val commands = lookup_commands outer_syntax;
wenzelm@44658
   282
    val range_pos = Position.set_range (Token.range toks);
wenzelm@44736
   283
    val _ = Position.reports (maps Thy_Syntax.reports_of_token toks);
wenzelm@27839
   284
  in
wenzelm@29311
   285
    (case Source.exhaust (toplevel_source false NONE (K commands) (Source.of_list toks)) of
wenzelm@37713
   286
      [tr] =>
wenzelm@37713
   287
        if Keyword.is_control (Toplevel.name_of tr) then
wenzelm@44658
   288
          (Toplevel.malformed (Toplevel.pos_of tr) "Illegal control command", true)
wenzelm@37713
   289
        else (tr, true)
wenzelm@27839
   290
    | [] => (Toplevel.ignored range_pos, false)
wenzelm@27839
   291
    | _ => (Toplevel.malformed range_pos not_singleton, true))
wenzelm@27839
   292
    handle ERROR msg => (Toplevel.malformed range_pos msg, true)
wenzelm@27839
   293
  end;
wenzelm@27839
   294
wenzelm@44478
   295
fun read_element outer_syntax init {head, proof, proper_proof} =
wenzelm@28436
   296
  let
wenzelm@44658
   297
    val read = read_span outer_syntax o Thy_Syntax.span_content;
wenzelm@44658
   298
    val (tr, proper_head) = read head |>> Toplevel.modify_init init;
wenzelm@44658
   299
    val proof_trs = map read proof |> filter #2 |> map #1;
wenzelm@28436
   300
  in
wenzelm@47416
   301
    if proper_head andalso proper_proof andalso
wenzelm@47416
   302
      not (Keyword.is_schematic_goal (Toplevel.name_of tr)) then [(tr, proof_trs)]
wenzelm@43621
   303
    else map (rpair []) (if proper_head then tr :: proof_trs else proof_trs)
wenzelm@28436
   304
  end;
wenzelm@28432
   305
wenzelm@5829
   306
end;
wenzelm@36953
   307