src/Pure/Isar/outer_syntax.ML
author wenzelm
Sun Aug 21 20:42:26 2011 +0200 (2011-08-21 ago)
changeset 44357 5f5649ac8235
parent 44187 88d770052bac
child 44478 4fdb1009a370
permissions -rw-r--r--
tuned Parse.group: delayed failure message;
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@36950
    15
  val command: string -> string -> Keyword.T ->
wenzelm@29311
    16
    (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@37216
    17
  val markup_command: Thy_Output.markup -> string -> string -> Keyword.T ->
wenzelm@29311
    18
    (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@36950
    19
  val improper_command: string -> string -> Keyword.T ->
wenzelm@29311
    20
    (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@43711
    21
  val internal_command: string ->
wenzelm@43711
    22
    (Toplevel.transition -> Toplevel.transition) parser -> unit
wenzelm@36950
    23
  val local_theory': string -> string -> Keyword.T ->
wenzelm@29380
    24
    (bool -> local_theory -> local_theory) parser -> unit
wenzelm@36950
    25
  val local_theory: string -> string -> Keyword.T ->
wenzelm@29311
    26
    (local_theory -> local_theory) parser -> unit
wenzelm@36950
    27
  val local_theory_to_proof': string -> string -> Keyword.T ->
wenzelm@29311
    28
    (bool -> local_theory -> Proof.state) parser -> unit
wenzelm@36950
    29
  val local_theory_to_proof: string -> string -> Keyword.T ->
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@43712
    37
  val prepare_span: outer_syntax -> Thy_Syntax.span -> Toplevel.transition * bool
wenzelm@44186
    38
  val prepare_element: outer_syntax -> (unit -> theory) -> Thy_Syntax.element ->
wenzelm@43712
    39
    (Toplevel.transition * Toplevel.transition list) list
wenzelm@27839
    40
  val prepare_command: Position.T -> string -> Toplevel.transition
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@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@44357
    65
  | terminate true =
wenzelm@44357
    66
      Parse.group (fn () => "end of input")
wenzelm@44357
    67
        (Scan.option Parse.sync -- Parse.semicolon >> K ());
wenzelm@14925
    68
wenzelm@26620
    69
fun body cmd (name, _) =
wenzelm@7026
    70
  (case cmd name of
wenzelm@29311
    71
    SOME (Command {int_only, parse, ...}) =>
wenzelm@36950
    72
      Parse.!!! (Scan.prompt (name ^ "# ") (Parse.tags |-- parse >> pair int_only))
wenzelm@37852
    73
  | NONE => raise Fail ("No parser for outer syntax command " ^ quote name));
wenzelm@6860
    74
wenzelm@6860
    75
in
wenzelm@5829
    76
wenzelm@26620
    77
fun parse_command do_terminate cmd =
wenzelm@36950
    78
  Parse.semicolon >> K NONE ||
wenzelm@36950
    79
  Parse.sync >> K NONE ||
wenzelm@36950
    80
  (Parse.position Parse.command :-- body cmd) --| terminate do_terminate
wenzelm@6860
    81
    >> (fn ((name, pos), (int_only, f)) =>
skalberg@15531
    82
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
    83
        Toplevel.interactive int_only |> f));
wenzelm@5829
    84
wenzelm@6199
    85
end;
wenzelm@6199
    86
wenzelm@5829
    87
wenzelm@43711
    88
(* type outer_syntax *)
wenzelm@43711
    89
wenzelm@43711
    90
datatype outer_syntax = Outer_Syntax of
wenzelm@43711
    91
 {commands: command Symtab.table,
wenzelm@43711
    92
  markups: (string * Thy_Output.markup) list};
wenzelm@43711
    93
wenzelm@43711
    94
fun make_outer_syntax commands markups =
wenzelm@43711
    95
  Outer_Syntax {commands = commands, markups = markups};
wenzelm@43711
    96
wenzelm@43711
    97
val empty_outer_syntax = make_outer_syntax Symtab.empty [];
wenzelm@43711
    98
wenzelm@43711
    99
wenzelm@43711
   100
fun map_commands f (Outer_Syntax {commands, ...}) =
wenzelm@43711
   101
  let
wenzelm@43711
   102
    val commands' = f commands;
wenzelm@43711
   103
    val markups' =
wenzelm@43711
   104
      Symtab.fold (fn (name, Command {markup = SOME m, ...}) => cons (name, m) | _ => I)
wenzelm@43711
   105
        commands' [];
wenzelm@43711
   106
  in make_outer_syntax commands' markups' end;
wenzelm@43711
   107
wenzelm@43711
   108
fun dest_commands (Outer_Syntax {commands, ...}) =
wenzelm@43711
   109
  commands |> Symtab.dest |> sort_wrt #1
wenzelm@43711
   110
  |> map (fn (name, Command {comment, int_only, ...}) => (name, comment, int_only));
wenzelm@43711
   111
wenzelm@43711
   112
fun lookup_commands (Outer_Syntax {commands, ...}) = Symtab.lookup commands;
wenzelm@43711
   113
wenzelm@43711
   114
fun is_markup (Outer_Syntax {markups, ...}) kind name =
wenzelm@43711
   115
  AList.lookup (op =) markups name = SOME kind;
wenzelm@43711
   116
wenzelm@43711
   117
wenzelm@5829
   118
wenzelm@9132
   119
(** global outer syntax **)
wenzelm@5829
   120
wenzelm@7026
   121
local
wenzelm@7026
   122
wenzelm@43711
   123
(*synchronized wrt. Keywords*)
wenzelm@43711
   124
val global_outer_syntax = Unsynchronized.ref empty_outer_syntax;
wenzelm@5952
   125
wenzelm@43711
   126
fun add_command name kind cmd = CRITICAL (fn () =>
wenzelm@43711
   127
 (Keyword.command name kind;
wenzelm@43711
   128
  Unsynchronized.change global_outer_syntax (map_commands (fn commands =>
wenzelm@43711
   129
   (if not (Symtab.defined commands name) then ()
wenzelm@43711
   130
    else warning ("Redefining outer syntax command " ^ quote name);
wenzelm@43711
   131
    Symtab.update (name, cmd) commands)))));
wenzelm@6722
   132
wenzelm@7026
   133
in
wenzelm@7026
   134
wenzelm@43711
   135
fun get_syntax () = CRITICAL (fn () => (Keyword.get_lexicons (), ! global_outer_syntax));
wenzelm@7789
   136
wenzelm@43711
   137
fun lookup_commands_dynamic () = lookup_commands (! global_outer_syntax);
wenzelm@5829
   138
wenzelm@24868
   139
fun command name comment kind parse =
wenzelm@29311
   140
  add_command name kind (make_command comment NONE false parse);
wenzelm@5829
   141
wenzelm@24868
   142
fun markup_command markup name comment kind parse =
wenzelm@29311
   143
  add_command name kind (make_command comment (SOME markup) false parse);
wenzelm@24868
   144
wenzelm@24868
   145
fun improper_command name comment kind parse =
wenzelm@29311
   146
  add_command name kind (make_command comment NONE true parse);
wenzelm@7026
   147
wenzelm@29311
   148
fun internal_command name parse =
wenzelm@36950
   149
  command name "(internal)" Keyword.control (parse >> (fn tr => Toplevel.no_timing o tr));
wenzelm@29311
   150
wenzelm@43711
   151
end;
wenzelm@43711
   152
wenzelm@5829
   153
wenzelm@26990
   154
(* local_theory commands *)
wenzelm@26990
   155
wenzelm@26990
   156
fun local_theory_command do_print trans name comment kind parse =
wenzelm@36950
   157
  command name comment kind (Parse.opt_target -- parse
wenzelm@26990
   158
    >> (fn (loc, f) => (if do_print then Toplevel.print else I) o trans loc f));
wenzelm@26990
   159
wenzelm@29380
   160
val local_theory' = local_theory_command false Toplevel.local_theory';
wenzelm@29311
   161
val local_theory = local_theory_command false Toplevel.local_theory;
wenzelm@26990
   162
val local_theory_to_proof' = local_theory_command true Toplevel.local_theory_to_proof';
wenzelm@29311
   163
val local_theory_to_proof = local_theory_command true Toplevel.local_theory_to_proof;
wenzelm@26990
   164
wenzelm@26990
   165
wenzelm@24872
   166
(* inspect syntax *)
wenzelm@7026
   167
wenzelm@9223
   168
fun print_outer_syntax () =
wenzelm@7026
   169
  let
wenzelm@43711
   170
    val (keywords, outer_syntax) =
wenzelm@43711
   171
      CRITICAL (fn () => (Keyword.dest_keywords (), #2 (get_syntax ())));
wenzelm@27353
   172
    fun pretty_cmd (name, comment, _) =
wenzelm@7026
   173
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@43711
   174
    val (int_cmds, cmds) = List.partition #3 (dest_commands outer_syntax);
wenzelm@7026
   175
  in
wenzelm@43711
   176
    [Pretty.strs ("syntax keywords:" :: map quote keywords),
wenzelm@18326
   177
      Pretty.big_list "commands:" (map pretty_cmd cmds),
wenzelm@18326
   178
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
wenzelm@9223
   179
    |> Pretty.chunks |> Pretty.writeln
wenzelm@7026
   180
  end;
wenzelm@5829
   181
wenzelm@5829
   182
wenzelm@5829
   183
wenzelm@9132
   184
(** toplevel parsing **)
wenzelm@5829
   185
wenzelm@9132
   186
(* basic sources *)
wenzelm@6860
   187
wenzelm@26620
   188
fun toplevel_source term do_recover cmd src =
wenzelm@9132
   189
  let
wenzelm@9132
   190
    val no_terminator =
wenzelm@36959
   191
      Scan.unless Parse.semicolon (Scan.one (Token.not_sync andf Token.not_eof));
wenzelm@23682
   192
    fun recover int =
wenzelm@23682
   193
      (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
wenzelm@9132
   194
  in
wenzelm@9132
   195
    src
wenzelm@36959
   196
    |> Token.source_proper
wenzelm@36959
   197
    |> Source.source Token.stopper
wenzelm@36950
   198
      (Scan.bulk (Parse.$$$ "--" -- Parse.!!! Parse.doc_source >> K NONE || Parse.not_eof >> SOME))
wenzelm@23682
   199
        (Option.map recover do_recover)
wenzelm@19482
   200
    |> Source.map_filter I
wenzelm@36959
   201
    |> Source.source Token.stopper
wenzelm@36950
   202
        (Scan.bulk (fn xs => Parse.!!! (parse_command term (cmd ())) xs))
wenzelm@23682
   203
        (Option.map recover do_recover)
wenzelm@19482
   204
    |> Source.map_filter I
wenzelm@9132
   205
  end;
wenzelm@5829
   206
wenzelm@7746
   207
wenzelm@25580
   208
(* off-line scanning/parsing *)
wenzelm@14925
   209
wenzelm@27839
   210
fun scan pos str =
wenzelm@16195
   211
  Source.of_string str
wenzelm@40523
   212
  |> Symbol.source
wenzelm@36959
   213
  |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
wenzelm@16195
   214
  |> Source.exhaust;
wenzelm@16195
   215
wenzelm@25580
   216
fun parse pos str =
wenzelm@25580
   217
  Source.of_string str
wenzelm@40523
   218
  |> Symbol.source
wenzelm@36959
   219
  |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
wenzelm@43711
   220
  |> toplevel_source false NONE lookup_commands_dynamic
wenzelm@25580
   221
  |> Source.exhaust;
wenzelm@25580
   222
nipkow@14091
   223
wenzelm@26431
   224
(* process file *)
wenzelm@26431
   225
wenzelm@26431
   226
fun process_file path thy =
wenzelm@26431
   227
  let
wenzelm@26881
   228
    val trs = parse (Path.position path) (File.read path);
wenzelm@44187
   229
    val init = Toplevel.init_theory (K thy) Toplevel.empty;
wenzelm@28424
   230
    val result = fold Toplevel.command (init :: trs) Toplevel.toplevel;
wenzelm@28424
   231
  in
wenzelm@28424
   232
    (case (Toplevel.is_theory result, Toplevel.generic_theory_of result) of
wenzelm@28424
   233
      (true, Context.Theory thy') => thy'
wenzelm@28424
   234
    | _ => error "Bad result state: global theory expected")
wenzelm@28424
   235
  end;
wenzelm@26431
   236
wenzelm@26431
   237
wenzelm@24868
   238
(* interactive source of toplevel transformers *)
wenzelm@24868
   239
wenzelm@26600
   240
type isar =
wenzelm@26600
   241
  (Toplevel.transition, (Toplevel.transition option,
wenzelm@36959
   242
    (Token.T, (Token.T option, (Token.T, (Token.T,
wenzelm@30573
   243
      (Symbol_Pos.T, Position.T * (Symbol.symbol, (string, unit) Source.source)
wenzelm@27770
   244
  Source.source) Source.source) Source.source) Source.source)
wenzelm@27770
   245
  Source.source) Source.source) Source.source) Source.source;
wenzelm@26600
   246
wenzelm@38253
   247
fun isar in_stream term : isar =
wenzelm@38253
   248
  Source.tty in_stream
wenzelm@40523
   249
  |> Symbol.source
wenzelm@36959
   250
  |> Token.source {do_recover = SOME true} Keyword.get_lexicons Position.none
wenzelm@43711
   251
  |> toplevel_source term (SOME true) lookup_commands_dynamic;
wenzelm@24868
   252
wenzelm@24868
   253
wenzelm@27839
   254
(* prepare toplevel commands -- fail-safe *)
wenzelm@27839
   255
wenzelm@27839
   256
val not_singleton = "Exactly one command expected";
wenzelm@27839
   257
wenzelm@43711
   258
fun prepare_span outer_syntax span =
wenzelm@27839
   259
  let
wenzelm@43711
   260
    val commands = lookup_commands outer_syntax;
wenzelm@41484
   261
    val range_pos = Position.set_range (Thy_Syntax.span_range span);
wenzelm@37216
   262
    val toks = Thy_Syntax.span_content span;
wenzelm@37216
   263
    val _ = List.app Thy_Syntax.report_token toks;
wenzelm@27839
   264
  in
wenzelm@29311
   265
    (case Source.exhaust (toplevel_source false NONE (K commands) (Source.of_list toks)) of
wenzelm@37713
   266
      [tr] =>
wenzelm@37713
   267
        if Keyword.is_control (Toplevel.name_of tr) then
wenzelm@37713
   268
          (Toplevel.malformed range_pos "Illegal control command", true)
wenzelm@37713
   269
        else (tr, true)
wenzelm@27839
   270
    | [] => (Toplevel.ignored range_pos, false)
wenzelm@27839
   271
    | _ => (Toplevel.malformed range_pos not_singleton, true))
wenzelm@27839
   272
    handle ERROR msg => (Toplevel.malformed range_pos msg, true)
wenzelm@27839
   273
  end;
wenzelm@27839
   274
wenzelm@43711
   275
fun prepare_element outer_syntax init {head, proof, proper_proof} =
wenzelm@28436
   276
  let
wenzelm@43711
   277
    val (tr, proper_head) = prepare_span outer_syntax head |>> Toplevel.modify_init init;
wenzelm@43711
   278
    val proof_trs = map (prepare_span outer_syntax) proof |> filter #2 |> map #1;
wenzelm@28436
   279
  in
wenzelm@43621
   280
    if proper_head andalso proper_proof then [(tr, proof_trs)]
wenzelm@43621
   281
    else map (rpair []) (if proper_head then tr :: proof_trs else proof_trs)
wenzelm@28436
   282
  end;
wenzelm@28432
   283
wenzelm@27839
   284
fun prepare_command pos str =
wenzelm@43711
   285
  let val (lexs, outer_syntax) = get_syntax () in
wenzelm@37216
   286
    (case Thy_Syntax.parse_spans lexs pos str of
wenzelm@43711
   287
      [span] => #1 (prepare_span outer_syntax span)
wenzelm@27839
   288
    | _ => Toplevel.malformed pos not_singleton)
wenzelm@27839
   289
  end;
wenzelm@27839
   290
wenzelm@5829
   291
end;
wenzelm@36953
   292