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