src/Pure/Isar/outer_syntax.ML
author wenzelm
Mon Dec 10 13:52:33 2012 +0100 (2012-12-10 ago)
changeset 50450 358b6020f8b6
parent 50215 97959912840a
child 51225 3fe0d8d55975
permissions -rw-r--r--
generalized notion of active area, where sendback is just one application;
some support for graphview via active area;
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@48646
    16
  type command_spec = (string * Keyword.T) * Position.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@50213
    31
  val help_outer_syntax: string list -> unit
wenzelm@5883
    32
  val print_outer_syntax: unit -> unit
wenzelm@36959
    33
  val scan: Position.T -> string -> Token.T list
wenzelm@25580
    34
  val parse: Position.T -> string -> Toplevel.transition list
wenzelm@26600
    35
  type isar
wenzelm@38253
    36
  val isar: TextIO.instream -> bool -> isar
wenzelm@48918
    37
  val span_cmts: Token.T list -> Token.T list
wenzelm@46876
    38
  val read_span: outer_syntax -> Token.T list -> Toplevel.transition * bool
wenzelm@44478
    39
  val read_element: outer_syntax -> (unit -> theory) -> Thy_Syntax.element ->
wenzelm@43712
    40
    (Toplevel.transition * Toplevel.transition list) list
wenzelm@5829
    41
end;
wenzelm@5829
    42
wenzelm@36953
    43
structure Outer_Syntax: OUTER_SYNTAX =
wenzelm@5829
    44
struct
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@37216
    52
  markup: Thy_Output.markup option,
wenzelm@24868
    53
  int_only: bool,
wenzelm@48647
    54
  parse: (Toplevel.transition -> Toplevel.transition) parser,
wenzelm@48647
    55
  pos: Position.T,
wenzelm@48647
    56
  id: serial};
wenzelm@5829
    57
wenzelm@48647
    58
fun new_command comment markup int_only parse pos =
wenzelm@48647
    59
  Command {comment = comment, markup = markup, int_only = int_only, parse = parse,
wenzelm@48647
    60
    pos = pos, id = serial ()};
wenzelm@48647
    61
wenzelm@48647
    62
fun command_markup def (name, Command {pos, id, ...}) =
wenzelm@48647
    63
  Markup.properties (Position.entity_properties_of def id pos)
wenzelm@50201
    64
    (Markup.entity Markup.commandN name);
wenzelm@5829
    65
wenzelm@50213
    66
fun pretty_command (cmd as (name, Command {comment, ...})) =
wenzelm@50213
    67
  Pretty.block
wenzelm@50213
    68
    (Pretty.marks_str
wenzelm@50450
    69
      ([Active.make_markup Markup.sendbackN {implicit = true, properties = [Markup.padding_line]},
wenzelm@50215
    70
        command_markup false cmd], name) :: Pretty.str ":" :: Pretty.brk 2 :: Pretty.text comment);
wenzelm@50213
    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@44357
    78
  | terminate true =
wenzelm@44357
    79
      Parse.group (fn () => "end of input")
wenzelm@44357
    80
        (Scan.option Parse.sync -- Parse.semicolon >> K ());
wenzelm@14925
    81
wenzelm@26620
    82
fun body cmd (name, _) =
wenzelm@7026
    83
  (case cmd name of
wenzelm@29311
    84
    SOME (Command {int_only, parse, ...}) =>
wenzelm@36950
    85
      Parse.!!! (Scan.prompt (name ^ "# ") (Parse.tags |-- parse >> pair int_only))
wenzelm@48191
    86
  | NONE =>
wenzelm@48191
    87
      Scan.succeed (false, Toplevel.imperative (fn () =>
wenzelm@48191
    88
        error ("Bad parser for outer syntax command " ^ quote name))));
wenzelm@6860
    89
wenzelm@6860
    90
in
wenzelm@5829
    91
wenzelm@26620
    92
fun parse_command do_terminate cmd =
wenzelm@36950
    93
  Parse.semicolon >> K NONE ||
wenzelm@36950
    94
  Parse.sync >> K NONE ||
wenzelm@36950
    95
  (Parse.position Parse.command :-- body cmd) --| terminate do_terminate
wenzelm@6860
    96
    >> (fn ((name, pos), (int_only, f)) =>
skalberg@15531
    97
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
    98
        Toplevel.interactive int_only |> f));
wenzelm@5829
    99
wenzelm@6199
   100
end;
wenzelm@6199
   101
wenzelm@5829
   102
wenzelm@43711
   103
(* type outer_syntax *)
wenzelm@43711
   104
wenzelm@43711
   105
datatype outer_syntax = Outer_Syntax of
wenzelm@43711
   106
 {commands: command Symtab.table,
wenzelm@43711
   107
  markups: (string * Thy_Output.markup) list};
wenzelm@43711
   108
wenzelm@43711
   109
fun make_outer_syntax commands markups =
wenzelm@43711
   110
  Outer_Syntax {commands = commands, markups = markups};
wenzelm@43711
   111
wenzelm@43711
   112
val empty_outer_syntax = make_outer_syntax Symtab.empty [];
wenzelm@43711
   113
wenzelm@43711
   114
wenzelm@43711
   115
fun map_commands f (Outer_Syntax {commands, ...}) =
wenzelm@43711
   116
  let
wenzelm@43711
   117
    val commands' = f commands;
wenzelm@43711
   118
    val markups' =
wenzelm@43711
   119
      Symtab.fold (fn (name, Command {markup = SOME m, ...}) => cons (name, m) | _ => I)
wenzelm@43711
   120
        commands' [];
wenzelm@43711
   121
  in make_outer_syntax commands' markups' end;
wenzelm@43711
   122
wenzelm@43711
   123
fun dest_commands (Outer_Syntax {commands, ...}) =
wenzelm@50213
   124
  commands |> Symtab.dest |> sort_wrt #1;
wenzelm@43711
   125
wenzelm@43711
   126
fun lookup_commands (Outer_Syntax {commands, ...}) = Symtab.lookup commands;
wenzelm@43711
   127
wenzelm@43711
   128
fun is_markup (Outer_Syntax {markups, ...}) kind name =
wenzelm@43711
   129
  AList.lookup (op =) markups name = SOME kind;
wenzelm@43711
   130
wenzelm@43711
   131
wenzelm@5829
   132
wenzelm@9132
   133
(** global outer syntax **)
wenzelm@5829
   134
wenzelm@48646
   135
type command_spec = (string * Keyword.T) * Position.T;
wenzelm@46961
   136
wenzelm@7026
   137
local
wenzelm@7026
   138
wenzelm@43711
   139
(*synchronized wrt. Keywords*)
wenzelm@43711
   140
val global_outer_syntax = Unsynchronized.ref empty_outer_syntax;
wenzelm@5952
   141
wenzelm@48647
   142
fun add_command (name, kind) cmd = CRITICAL (fn () =>
wenzelm@46950
   143
  let
wenzelm@46950
   144
    val thy = ML_Context.the_global_context ();
wenzelm@48647
   145
    val Command {pos, ...} = cmd;
wenzelm@46950
   146
    val _ =
wenzelm@46950
   147
      (case try (Thy_Header.the_keyword thy) name of
wenzelm@46961
   148
        SOME spec =>
wenzelm@48864
   149
          if Option.map #1 spec = SOME (Keyword.kind_files_of kind) then ()
wenzelm@48646
   150
          else error ("Inconsistent outer syntax keyword declaration " ^
wenzelm@48992
   151
            quote name ^ Position.here pos)
wenzelm@46950
   152
      | NONE =>
wenzelm@46961
   153
          if Context.theory_name thy = Context.PureN
wenzelm@46961
   154
          then Keyword.define (name, SOME kind)
wenzelm@48992
   155
          else error ("Undeclared outer syntax command " ^ quote name ^ Position.here pos));
wenzelm@48647
   156
    val _ = Position.report pos (command_markup true (name, cmd));
wenzelm@46950
   157
  in
wenzelm@46950
   158
    Unsynchronized.change global_outer_syntax (map_commands (fn commands =>
wenzelm@46950
   159
     (if not (Symtab.defined commands name) then ()
wenzelm@46950
   160
      else warning ("Redefining outer syntax command " ^ quote name);
wenzelm@46950
   161
      Symtab.update (name, cmd) commands)))
wenzelm@46950
   162
  end);
wenzelm@6722
   163
wenzelm@7026
   164
in
wenzelm@7026
   165
wenzelm@43711
   166
fun get_syntax () = CRITICAL (fn () => (Keyword.get_lexicons (), ! global_outer_syntax));
wenzelm@7789
   167
wenzelm@46970
   168
fun check_syntax () =
wenzelm@46970
   169
  let
wenzelm@46970
   170
    val ((_, major), syntax) = CRITICAL (fn () => (Keyword.dest (), ! global_outer_syntax));
wenzelm@46970
   171
  in
wenzelm@46970
   172
    (case subtract (op =) (map #1 (dest_commands syntax)) major of
wenzelm@46970
   173
      [] => ()
wenzelm@46970
   174
    | missing => error ("Missing outer syntax command(s) " ^ commas_quote missing))
wenzelm@46970
   175
  end;
wenzelm@46970
   176
wenzelm@43711
   177
fun lookup_commands_dynamic () = lookup_commands (! global_outer_syntax);
wenzelm@5829
   178
wenzelm@48647
   179
fun command (spec, pos) comment parse =
wenzelm@48647
   180
  add_command spec (new_command comment NONE false parse pos);
wenzelm@24868
   181
wenzelm@48647
   182
fun markup_command markup (spec, pos) comment parse =
wenzelm@48647
   183
  add_command spec (new_command comment (SOME markup) false parse pos);
wenzelm@7026
   184
wenzelm@48647
   185
fun improper_command (spec, pos) comment parse =
wenzelm@48647
   186
  add_command spec (new_command comment NONE true parse pos);
wenzelm@29311
   187
wenzelm@43711
   188
end;
wenzelm@43711
   189
wenzelm@5829
   190
wenzelm@26990
   191
(* local_theory commands *)
wenzelm@26990
   192
wenzelm@46961
   193
fun local_theory_command do_print trans command_spec comment parse =
wenzelm@46961
   194
  command command_spec comment (Parse.opt_target -- parse
wenzelm@26990
   195
    >> (fn (loc, f) => (if do_print then Toplevel.print else I) o trans loc f));
wenzelm@26990
   196
wenzelm@29380
   197
val local_theory' = local_theory_command false Toplevel.local_theory';
wenzelm@29311
   198
val local_theory = local_theory_command false Toplevel.local_theory;
wenzelm@26990
   199
val local_theory_to_proof' = local_theory_command true Toplevel.local_theory_to_proof';
wenzelm@29311
   200
val local_theory_to_proof = local_theory_command true Toplevel.local_theory_to_proof;
wenzelm@26990
   201
wenzelm@26990
   202
wenzelm@24872
   203
(* inspect syntax *)
wenzelm@7026
   204
wenzelm@50213
   205
fun help_outer_syntax pats =
wenzelm@50213
   206
  dest_commands (#2 (get_syntax ()))
wenzelm@50213
   207
  |> filter (fn (name, _) => forall (fn pat => match_string pat name) pats)
wenzelm@50213
   208
  |> map pretty_command
wenzelm@50213
   209
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@50213
   210
wenzelm@9223
   211
fun print_outer_syntax () =
wenzelm@7026
   212
  let
wenzelm@46957
   213
    val ((keywords, _), outer_syntax) =
wenzelm@46957
   214
      CRITICAL (fn () => (Keyword.dest (), #2 (get_syntax ())));
wenzelm@50213
   215
    val (int_cmds, cmds) =
wenzelm@50213
   216
      List.partition (fn (_, Command {int_only, ...}) => int_only) (dest_commands outer_syntax);
wenzelm@7026
   217
  in
wenzelm@43711
   218
    [Pretty.strs ("syntax keywords:" :: map quote keywords),
wenzelm@50213
   219
      Pretty.big_list "commands:" (map pretty_command cmds),
wenzelm@50213
   220
      Pretty.big_list "interactive-only commands:" (map pretty_command int_cmds)]
wenzelm@9223
   221
    |> Pretty.chunks |> Pretty.writeln
wenzelm@7026
   222
  end;
wenzelm@5829
   223
wenzelm@5829
   224
wenzelm@5829
   225
wenzelm@9132
   226
(** toplevel parsing **)
wenzelm@5829
   227
wenzelm@9132
   228
(* basic sources *)
wenzelm@6860
   229
wenzelm@26620
   230
fun toplevel_source term do_recover cmd src =
wenzelm@9132
   231
  let
wenzelm@9132
   232
    val no_terminator =
wenzelm@36959
   233
      Scan.unless Parse.semicolon (Scan.one (Token.not_sync andf Token.not_eof));
wenzelm@23682
   234
    fun recover int =
wenzelm@23682
   235
      (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
wenzelm@9132
   236
  in
wenzelm@9132
   237
    src
wenzelm@36959
   238
    |> Token.source_proper
wenzelm@36959
   239
    |> Source.source Token.stopper
wenzelm@36950
   240
      (Scan.bulk (Parse.$$$ "--" -- Parse.!!! Parse.doc_source >> K NONE || Parse.not_eof >> SOME))
wenzelm@23682
   241
        (Option.map recover do_recover)
wenzelm@19482
   242
    |> Source.map_filter I
wenzelm@36959
   243
    |> Source.source Token.stopper
wenzelm@36950
   244
        (Scan.bulk (fn xs => Parse.!!! (parse_command term (cmd ())) xs))
wenzelm@23682
   245
        (Option.map recover do_recover)
wenzelm@19482
   246
    |> Source.map_filter I
wenzelm@9132
   247
  end;
wenzelm@5829
   248
wenzelm@7746
   249
wenzelm@25580
   250
(* off-line scanning/parsing *)
wenzelm@14925
   251
wenzelm@27839
   252
fun scan pos str =
wenzelm@16195
   253
  Source.of_string str
wenzelm@40523
   254
  |> Symbol.source
wenzelm@36959
   255
  |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
wenzelm@16195
   256
  |> Source.exhaust;
wenzelm@16195
   257
wenzelm@25580
   258
fun parse pos str =
wenzelm@25580
   259
  Source.of_string str
wenzelm@40523
   260
  |> Symbol.source
wenzelm@36959
   261
  |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
wenzelm@43711
   262
  |> toplevel_source false NONE lookup_commands_dynamic
wenzelm@25580
   263
  |> Source.exhaust;
wenzelm@25580
   264
nipkow@14091
   265
wenzelm@24868
   266
(* interactive source of toplevel transformers *)
wenzelm@24868
   267
wenzelm@26600
   268
type isar =
wenzelm@26600
   269
  (Toplevel.transition, (Toplevel.transition option,
wenzelm@36959
   270
    (Token.T, (Token.T option, (Token.T, (Token.T,
wenzelm@30573
   271
      (Symbol_Pos.T, Position.T * (Symbol.symbol, (string, unit) Source.source)
wenzelm@27770
   272
  Source.source) Source.source) Source.source) Source.source)
wenzelm@27770
   273
  Source.source) Source.source) Source.source) Source.source;
wenzelm@26600
   274
wenzelm@38253
   275
fun isar in_stream term : isar =
wenzelm@38253
   276
  Source.tty in_stream
wenzelm@40523
   277
  |> Symbol.source
wenzelm@36959
   278
  |> Token.source {do_recover = SOME true} Keyword.get_lexicons Position.none
wenzelm@43711
   279
  |> toplevel_source term (SOME true) lookup_commands_dynamic;
wenzelm@24868
   280
wenzelm@24868
   281
wenzelm@48918
   282
(* side-comments *)
wenzelm@48918
   283
wenzelm@48918
   284
local
wenzelm@48918
   285
wenzelm@48918
   286
fun cmts (t1 :: t2 :: toks) =
wenzelm@48918
   287
      if Token.keyword_with (fn s => s = "--") t1 then t2 :: cmts toks
wenzelm@48918
   288
      else cmts (t2 :: toks)
wenzelm@49564
   289
  | cmts _ = [];
wenzelm@48918
   290
wenzelm@48918
   291
in
wenzelm@48918
   292
wenzelm@48918
   293
val span_cmts = filter Token.is_proper #> cmts;
wenzelm@48918
   294
wenzelm@48918
   295
end;
wenzelm@48918
   296
wenzelm@48918
   297
wenzelm@44478
   298
(* read toplevel commands -- fail-safe *)
wenzelm@27839
   299
wenzelm@44658
   300
fun read_span outer_syntax toks =
wenzelm@27839
   301
  let
wenzelm@43711
   302
    val commands = lookup_commands outer_syntax;
wenzelm@48749
   303
wenzelm@48771
   304
    val proper_range = Position.set_range (Command.proper_range toks);
wenzelm@48749
   305
    val pos =
wenzelm@48749
   306
      (case find_first Token.is_command toks of
wenzelm@48749
   307
        SOME tok => Token.position_of tok
wenzelm@48771
   308
      | NONE => proper_range);
wenzelm@48647
   309
wenzelm@48647
   310
    fun command_reports tok =
wenzelm@48749
   311
      if Token.is_command tok then
wenzelm@48647
   312
        let val name = Token.content_of tok in
wenzelm@48647
   313
          (case commands name of
wenzelm@48647
   314
            NONE => []
wenzelm@48768
   315
          | SOME cmd => [((Token.position_of tok, command_markup false (name, cmd)), "")])
wenzelm@48647
   316
        end
wenzelm@48647
   317
      else [];
wenzelm@48749
   318
wenzelm@48749
   319
    val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens toks;
wenzelm@48768
   320
    val _ = Position.reports_text (token_reports @ maps command_reports toks);
wenzelm@27839
   321
  in
wenzelm@48749
   322
    if is_malformed then (Toplevel.malformed pos "Malformed command syntax", true)
wenzelm@48749
   323
    else
wenzelm@48749
   324
      (case Source.exhaust (toplevel_source false NONE (K commands) (Source.of_list toks)) of
wenzelm@48749
   325
        [tr] =>
wenzelm@48749
   326
          if Keyword.is_control (Toplevel.name_of tr) then
wenzelm@48749
   327
            (Toplevel.malformed pos "Illegal control command", true)
wenzelm@48749
   328
          else (tr, true)
wenzelm@48771
   329
      | [] => (Toplevel.ignored (Position.set_range (Command.range toks)), false)
wenzelm@48771
   330
      | _ => (Toplevel.malformed proper_range "Exactly one command expected", true))
wenzelm@48771
   331
      handle ERROR msg => (Toplevel.malformed proper_range msg, true)
wenzelm@27839
   332
  end;
wenzelm@27839
   333
wenzelm@44478
   334
fun read_element outer_syntax init {head, proof, proper_proof} =
wenzelm@28436
   335
  let
wenzelm@44658
   336
    val read = read_span outer_syntax o Thy_Syntax.span_content;
wenzelm@44658
   337
    val (tr, proper_head) = read head |>> Toplevel.modify_init init;
wenzelm@44658
   338
    val proof_trs = map read proof |> filter #2 |> map #1;
wenzelm@28436
   339
  in
wenzelm@47416
   340
    if proper_head andalso proper_proof andalso
wenzelm@47416
   341
      not (Keyword.is_schematic_goal (Toplevel.name_of tr)) then [(tr, proof_trs)]
wenzelm@43621
   342
    else map (rpair []) (if proper_head then tr :: proof_trs else proof_trs)
wenzelm@28436
   343
  end;
wenzelm@28432
   344
wenzelm@5829
   345
end;
wenzelm@36953
   346