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