src/Pure/Thy/thy_output.ML
author haftmann
Mon Sep 13 15:22:40 2010 +0200 (2010-09-13)
changeset 39309 74469faa27ca
parent 39308 c2c9bb3c52c6
child 39507 839873937ddd
permissions -rw-r--r--
type antiquotation: allow arbitrary type abbreviations, but fail with user-space exception on bad input
wenzelm@22124
     1
(*  Title:      Pure/Thy/thy_output.ML
wenzelm@22124
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@22124
     3
wenzelm@30390
     4
Theory document output with antiquotations.
wenzelm@22124
     5
*)
wenzelm@22124
     6
wenzelm@22124
     7
signature THY_OUTPUT =
wenzelm@22124
     8
sig
wenzelm@38767
     9
  val display_default: bool Unsynchronized.ref
wenzelm@38767
    10
  val quotes_default: bool Unsynchronized.ref
wenzelm@38767
    11
  val indent_default: int Unsynchronized.ref
wenzelm@38767
    12
  val source_default: bool Unsynchronized.ref
wenzelm@38767
    13
  val break_default: bool Unsynchronized.ref
wenzelm@38767
    14
  val display: bool Config.T
wenzelm@38767
    15
  val quotes: bool Config.T
wenzelm@38767
    16
  val indent: int Config.T
wenzelm@38767
    17
  val source: bool Config.T
wenzelm@38767
    18
  val break: bool Config.T
wenzelm@38766
    19
  val add_wrapper: ((unit -> string) -> unit -> string) -> Proof.context -> Proof.context
wenzelm@38766
    20
  val add_option: string -> (string -> Proof.context -> Proof.context) -> unit
wenzelm@26893
    21
  val defined_command: string -> bool
wenzelm@26893
    22
  val defined_option: string -> bool
wenzelm@22124
    23
  val print_antiquotations: unit -> unit
wenzelm@22124
    24
  val boolean: string -> bool
wenzelm@22124
    25
  val integer: string -> int
wenzelm@30513
    26
  val antiquotation: string -> 'a context_parser ->
wenzelm@38766
    27
    ({source: Args.src, state: Toplevel.state, context: Proof.context} -> 'a -> string) -> unit
wenzelm@22124
    28
  datatype markup = Markup | MarkupEnv | Verbatim
wenzelm@32738
    29
  val modes: string list Unsynchronized.ref
wenzelm@30573
    30
  val eval_antiquote: Scan.lexicon -> Toplevel.state -> Symbol_Pos.text * Position.T -> string
wenzelm@28427
    31
  val present_thy: Scan.lexicon -> (string -> string list) -> (markup -> string -> bool) ->
wenzelm@36959
    32
    (Toplevel.transition * Toplevel.state) list -> (Token.T, 'a) Source.source -> Buffer.T
wenzelm@38767
    33
  val pretty_text: Proof.context -> string -> Pretty.T
wenzelm@28644
    34
  val pretty_term: Proof.context -> term -> Pretty.T
wenzelm@28644
    35
  val pretty_thm: Proof.context -> thm -> Pretty.T
wenzelm@30390
    36
  val str_of_source: Args.src -> string
wenzelm@38767
    37
  val maybe_pretty_source: (Proof.context -> 'a -> Pretty.T) -> Proof.context ->
wenzelm@38767
    38
    Args.src -> 'a list -> Pretty.T list
wenzelm@38767
    39
  val output: Proof.context -> Pretty.T list -> string
wenzelm@22124
    40
end;
wenzelm@22124
    41
wenzelm@37216
    42
structure Thy_Output: THY_OUTPUT =
wenzelm@22124
    43
struct
wenzelm@22124
    44
wenzelm@22124
    45
(** global options **)
wenzelm@22124
    46
wenzelm@38767
    47
val display_default = Unsynchronized.ref false;
wenzelm@38767
    48
val quotes_default = Unsynchronized.ref false;
wenzelm@38767
    49
val indent_default = Unsynchronized.ref 0;
wenzelm@38767
    50
val source_default = Unsynchronized.ref false;
wenzelm@38767
    51
val break_default = Unsynchronized.ref false;
wenzelm@38767
    52
wenzelm@38767
    53
val (display, setup_display) = Attrib.config_bool "thy_output_display" (fn _ => ! display_default);
wenzelm@38767
    54
val (quotes, setup_quotes) = Attrib.config_bool "thy_output_quotes" (fn _ => ! quotes_default);
wenzelm@38767
    55
val (indent, setup_indent) = Attrib.config_int "thy_output_indent" (fn _ => ! indent_default);
wenzelm@38767
    56
val (source, setup_source) = Attrib.config_bool "thy_output_source" (fn _ => ! source_default);
wenzelm@38767
    57
val (break, setup_break) = Attrib.config_bool "thy_output_break" (fn _ => ! break_default);
wenzelm@38767
    58
wenzelm@38767
    59
val _ = Context.>> (Context.map_theory
wenzelm@38767
    60
 (setup_display #> setup_quotes #> setup_indent #> setup_source #> setup_break));
wenzelm@22124
    61
wenzelm@22124
    62
wenzelm@38766
    63
structure Wrappers = Proof_Data
wenzelm@38766
    64
(
wenzelm@38766
    65
  type T = ((unit -> string) -> unit -> string) list;
wenzelm@38766
    66
  fun init _ = [];
wenzelm@38766
    67
);
wenzelm@38766
    68
wenzelm@38766
    69
fun add_wrapper wrapper = Wrappers.map (cons wrapper);
wenzelm@38766
    70
wenzelm@38766
    71
val wrap = Wrappers.get #> fold (fn wrapper => fn f => wrapper f);
wenzelm@38766
    72
wenzelm@38766
    73
wenzelm@22124
    74
wenzelm@30390
    75
(** maintain global antiquotations **)
wenzelm@22124
    76
wenzelm@22124
    77
local
wenzelm@22124
    78
wenzelm@22124
    79
val global_commands =
wenzelm@38766
    80
  Unsynchronized.ref
wenzelm@38766
    81
    (Symtab.empty: (Args.src -> Toplevel.state -> Proof.context -> string) Symtab.table);
wenzelm@22124
    82
wenzelm@22124
    83
val global_options =
wenzelm@38766
    84
  Unsynchronized.ref (Symtab.empty: (string -> Proof.context -> Proof.context) Symtab.table);
wenzelm@22124
    85
wenzelm@38766
    86
fun add_item kind name item tab =
wenzelm@22124
    87
 (if not (Symtab.defined tab name) then ()
wenzelm@27897
    88
  else warning ("Redefined document antiquotation " ^ kind ^ ": " ^ quote name);
wenzelm@38766
    89
  Symtab.update (name, item) tab);
wenzelm@22124
    90
wenzelm@22124
    91
in
wenzelm@22124
    92
wenzelm@38766
    93
fun add_command name cmd =
wenzelm@38766
    94
  CRITICAL (fn () => Unsynchronized.change global_commands (add_item "command" name cmd));
wenzelm@38766
    95
fun add_option name opt =
wenzelm@38766
    96
  CRITICAL (fn () => Unsynchronized.change global_options (add_item "option" name opt));
wenzelm@22124
    97
wenzelm@26893
    98
fun defined_command name = Symtab.defined (! global_commands) name;
wenzelm@26893
    99
fun defined_option name = Symtab.defined (! global_options) name;
wenzelm@26893
   100
wenzelm@22124
   101
fun command src =
wenzelm@22124
   102
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@22124
   103
    (case Symtab.lookup (! global_commands) name of
wenzelm@30317
   104
      NONE => error ("Unknown document antiquotation: " ^ quote name ^ Position.str_of pos)
wenzelm@30317
   105
    | SOME f =>
wenzelm@30317
   106
       (Position.report (Markup.doc_antiq name) pos;
wenzelm@38766
   107
        (fn state => fn ctxt => f src state ctxt handle ERROR msg =>
wenzelm@30317
   108
          cat_error msg ("The error(s) above occurred in document antiquotation: " ^
wenzelm@30317
   109
            quote name ^ Position.str_of pos))))
wenzelm@22124
   110
  end;
wenzelm@22124
   111
wenzelm@38766
   112
fun option (name, s) ctxt =
wenzelm@22124
   113
  (case Symtab.lookup (! global_options) name of
wenzelm@27897
   114
    NONE => error ("Unknown document antiquotation option: " ^ quote name)
wenzelm@38766
   115
  | SOME opt => opt s ctxt);
wenzelm@22124
   116
wenzelm@22124
   117
wenzelm@22124
   118
fun print_antiquotations () =
wenzelm@27897
   119
 [Pretty.big_list "document antiquotation commands:"
wenzelm@23272
   120
    (map Pretty.str (sort_strings (Symtab.keys (! global_commands)))),
wenzelm@27897
   121
  Pretty.big_list "document antiquotation options:"
wenzelm@23272
   122
    (map Pretty.str (sort_strings (Symtab.keys (! global_options))))]
wenzelm@22124
   123
 |> Pretty.chunks |> Pretty.writeln;
wenzelm@22124
   124
wenzelm@22124
   125
end;
wenzelm@22124
   126
wenzelm@38766
   127
fun antiquotation name scan out =
wenzelm@38766
   128
  add_command name
wenzelm@38766
   129
    (fn src => fn state => fn ctxt =>
wenzelm@38766
   130
      let val (x, ctxt') = Args.context_syntax "document antiquotation" scan src ctxt
wenzelm@38766
   131
      in out {source = src, state = state, context = ctxt'} x end);
wenzelm@30390
   132
wenzelm@22124
   133
wenzelm@22124
   134
wenzelm@22124
   135
(** syntax of antiquotations **)
wenzelm@22124
   136
wenzelm@22124
   137
(* option values *)
wenzelm@22124
   138
wenzelm@22124
   139
fun boolean "" = true
wenzelm@22124
   140
  | boolean "true" = true
wenzelm@22124
   141
  | boolean "false" = false
wenzelm@22124
   142
  | boolean s = error ("Bad boolean value: " ^ quote s);
wenzelm@22124
   143
wenzelm@22124
   144
fun integer s =
wenzelm@22124
   145
  let
wenzelm@22124
   146
    fun int ss =
wenzelm@22124
   147
      (case Library.read_int ss of (i, []) => i
wenzelm@22124
   148
      | _ => error ("Bad integer value: " ^ quote s));
wenzelm@22124
   149
  in (case Symbol.explode s of "-" :: ss => ~ (int ss) | ss => int ss) end;
wenzelm@22124
   150
wenzelm@22124
   151
wenzelm@22124
   152
(* outer syntax *)
wenzelm@22124
   153
wenzelm@22124
   154
local
wenzelm@22124
   155
wenzelm@36950
   156
val property =
wenzelm@36950
   157
  Parse.xname -- Scan.optional (Parse.$$$ "=" |-- Parse.!!! Parse.xname) "";
wenzelm@36950
   158
wenzelm@36950
   159
val properties =
wenzelm@36950
   160
  Scan.optional (Parse.$$$ "[" |-- Parse.!!! (Parse.enum "," property --| Parse.$$$ "]")) [];
wenzelm@22124
   161
wenzelm@22124
   162
in
wenzelm@22124
   163
wenzelm@36950
   164
val antiq =
wenzelm@36950
   165
  Parse.!!! (Parse.position Parse.xname -- properties -- Args.parse --| Scan.ahead Parse.eof)
wenzelm@22124
   166
  >> (fn (((x, pos), y), z) => (y, Args.src ((x, z), pos)));
wenzelm@22124
   167
wenzelm@22124
   168
end;
wenzelm@22124
   169
wenzelm@22124
   170
wenzelm@22124
   171
(* eval_antiquote *)
wenzelm@22124
   172
wenzelm@32738
   173
val modes = Unsynchronized.ref ([]: string list);
wenzelm@22124
   174
wenzelm@30367
   175
fun eval_antiquote lex state (txt, pos) =
wenzelm@22124
   176
  let
wenzelm@30589
   177
    fun expand (Antiquote.Text ss) = Symbol_Pos.content ss
wenzelm@30683
   178
      | expand (Antiquote.Antiq (ss, (pos, _))) =
wenzelm@38766
   179
          let
wenzelm@38766
   180
            val (opts, src) = Token.read_antiq lex antiq (ss, pos);
wenzelm@38832
   181
            fun cmd ctxt = wrap ctxt (fn () => command src state ctxt) ();
wenzelm@38832
   182
            val preview_ctxt = fold option opts (Toplevel.presentation_context_of state);
wenzelm@38832
   183
            val print_ctxt = Context_Position.set_visible false preview_ctxt;
wenzelm@38832
   184
            val _ = cmd preview_ctxt;
wenzelm@38832
   185
          in Print_Mode.with_modes (! modes @ Latex.modes) (fn () => cmd print_ctxt) () end
wenzelm@27344
   186
      | expand (Antiquote.Open _) = ""
wenzelm@27344
   187
      | expand (Antiquote.Close _) = "";
wenzelm@30642
   188
    val ants = Antiquote.read (Symbol_Pos.explode (txt, pos), pos);
wenzelm@22124
   189
  in
wenzelm@30635
   190
    if Toplevel.is_toplevel state andalso not (forall Antiquote.is_text ants) then
wenzelm@27897
   191
      error ("Unknown context -- cannot expand document antiquotations" ^ Position.str_of pos)
wenzelm@22124
   192
    else implode (map expand ants)
wenzelm@22124
   193
  end;
wenzelm@22124
   194
wenzelm@22124
   195
wenzelm@22124
   196
wenzelm@22124
   197
(** present theory source **)
wenzelm@22124
   198
wenzelm@22124
   199
(*NB: arranging white space around command spans is a black art.*)
wenzelm@22124
   200
wenzelm@22124
   201
(* presentation tokens *)
wenzelm@22124
   202
wenzelm@22124
   203
datatype token =
wenzelm@22124
   204
    NoToken
wenzelm@36959
   205
  | BasicToken of Token.T
wenzelm@22124
   206
  | MarkupToken of string * (string * Position.T)
wenzelm@22124
   207
  | MarkupEnvToken of string * (string * Position.T)
wenzelm@22124
   208
  | VerbatimToken of string * Position.T;
wenzelm@22124
   209
wenzelm@22124
   210
fun output_token lex state =
wenzelm@30367
   211
  let val eval = eval_antiquote lex state in
wenzelm@22124
   212
    fn NoToken => ""
wenzelm@22124
   213
     | BasicToken tok => Latex.output_basic tok
wenzelm@22124
   214
     | MarkupToken (cmd, txt) => Latex.output_markup cmd (eval txt)
wenzelm@22124
   215
     | MarkupEnvToken (cmd, txt) => Latex.output_markup_env cmd (eval txt)
wenzelm@22124
   216
     | VerbatimToken txt => Latex.output_verbatim (eval txt)
wenzelm@22124
   217
  end;
wenzelm@22124
   218
wenzelm@22124
   219
fun basic_token pred (BasicToken tok) = pred tok
wenzelm@22124
   220
  | basic_token _ _ = false;
wenzelm@22124
   221
wenzelm@36959
   222
val improper_token = basic_token (not o Token.is_proper);
wenzelm@36959
   223
val comment_token = basic_token Token.is_comment;
wenzelm@36959
   224
val blank_token = basic_token Token.is_blank;
wenzelm@36959
   225
val newline_token = basic_token Token.is_newline;
wenzelm@22124
   226
wenzelm@22124
   227
wenzelm@22124
   228
(* command spans *)
wenzelm@22124
   229
wenzelm@22124
   230
type command = string * Position.T * string list;   (*name, position, tags*)
wenzelm@22124
   231
type source = (token * (string * int)) list;        (*token, markup flag, meta-comment depth*)
wenzelm@22124
   232
wenzelm@22124
   233
datatype span = Span of command * (source * source * source * source) * bool;
wenzelm@22124
   234
wenzelm@22124
   235
fun make_span cmd src =
wenzelm@22124
   236
  let
wenzelm@22124
   237
    fun take_newline (tok :: toks) =
wenzelm@22124
   238
          if newline_token (fst tok) then ([tok], toks, true)
wenzelm@22124
   239
          else ([], tok :: toks, false)
wenzelm@22124
   240
      | take_newline [] = ([], [], false);
wenzelm@22124
   241
    val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) =
wenzelm@22124
   242
      src
wenzelm@22124
   243
      |> take_prefix (improper_token o fst)
wenzelm@22124
   244
      ||>> take_suffix (improper_token o fst)
wenzelm@22124
   245
      ||>> take_prefix (comment_token o fst)
wenzelm@22124
   246
      ||> take_newline;
wenzelm@22124
   247
  in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end;
wenzelm@22124
   248
wenzelm@22124
   249
wenzelm@22124
   250
(* present spans *)
wenzelm@22124
   251
wenzelm@22124
   252
local
wenzelm@22124
   253
wenzelm@22124
   254
fun err_bad_nesting pos =
wenzelm@22124
   255
  error ("Bad nesting of commands in presentation" ^ pos);
wenzelm@22124
   256
wenzelm@22124
   257
fun edge which f (x: string option, y) =
wenzelm@22124
   258
  if x = y then I
wenzelm@22124
   259
  else (case which (x, y) of NONE => I | SOME txt => Buffer.add (f txt));
wenzelm@22124
   260
wenzelm@22124
   261
val begin_tag = edge #2 Latex.begin_tag;
wenzelm@22124
   262
val end_tag = edge #1 Latex.end_tag;
wenzelm@22124
   263
fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e;
wenzelm@22124
   264
fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e;
wenzelm@22124
   265
wenzelm@22124
   266
in
wenzelm@22124
   267
wenzelm@22124
   268
fun present_span lex default_tags span state state'
wenzelm@22124
   269
    (tag_stack, active_tag, newline, buffer, present_cont) =
wenzelm@22124
   270
  let
wenzelm@22124
   271
    val present = fold (fn (tok, (flag, 0)) =>
wenzelm@22124
   272
        Buffer.add (output_token lex state' tok)
wenzelm@22124
   273
        #> Buffer.add flag
wenzelm@22124
   274
      | _ => I);
wenzelm@22124
   275
wenzelm@22124
   276
    val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span;
wenzelm@22124
   277
wenzelm@22124
   278
    val (tag, tags) = tag_stack;
wenzelm@36950
   279
    val tag' = try hd (fold Keyword.update_tags cmd_tags (the_list tag));
wenzelm@22124
   280
wenzelm@22124
   281
    val active_tag' =
wenzelm@22124
   282
      if is_some tag' then tag'
wenzelm@22124
   283
      else if cmd_name = "end" andalso not (Toplevel.is_toplevel state') then NONE
wenzelm@22124
   284
      else try hd (default_tags cmd_name);
wenzelm@22124
   285
    val edge = (active_tag, active_tag');
wenzelm@22124
   286
wenzelm@22124
   287
    val newline' =
wenzelm@22124
   288
      if is_none active_tag' then span_newline else newline;
wenzelm@22124
   289
wenzelm@22124
   290
    val nesting = Toplevel.level state' - Toplevel.level state;
wenzelm@22124
   291
    val tag_stack' =
wenzelm@22124
   292
      if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack
wenzelm@22124
   293
      else if nesting >= 0 then (tag', replicate nesting tag @ tags)
wenzelm@22124
   294
      else
haftmann@33957
   295
        (case drop (~ nesting - 1) tags of
wenzelm@22124
   296
          tgs :: tgss => (tgs, tgss)
wenzelm@22124
   297
        | [] => err_bad_nesting (Position.str_of cmd_pos));
wenzelm@22124
   298
wenzelm@22124
   299
    val buffer' =
wenzelm@22124
   300
      buffer
wenzelm@22124
   301
      |> end_tag edge
wenzelm@22124
   302
      |> close_delim (fst present_cont) edge
wenzelm@22124
   303
      |> snd present_cont
wenzelm@22124
   304
      |> open_delim (present (#1 srcs)) edge
wenzelm@22124
   305
      |> begin_tag edge
wenzelm@22124
   306
      |> present (#2 srcs);
wenzelm@22124
   307
    val present_cont' =
wenzelm@22124
   308
      if newline then (present (#3 srcs), present (#4 srcs))
wenzelm@22124
   309
      else (I, present (#3 srcs) #> present (#4 srcs));
wenzelm@22124
   310
  in (tag_stack', active_tag', newline', buffer', present_cont') end;
wenzelm@22124
   311
wenzelm@22124
   312
fun present_trailer ((_, tags), active_tag, _, buffer, present_cont) =
wenzelm@22124
   313
  if not (null tags) then err_bad_nesting " at end of theory"
wenzelm@22124
   314
  else
wenzelm@22124
   315
    buffer
wenzelm@22124
   316
    |> end_tag (active_tag, NONE)
wenzelm@22124
   317
    |> close_delim (fst present_cont) (active_tag, NONE)
wenzelm@22124
   318
    |> snd present_cont;
wenzelm@22124
   319
wenzelm@22124
   320
end;
wenzelm@22124
   321
wenzelm@22124
   322
wenzelm@28427
   323
(* present_thy *)
wenzelm@22124
   324
wenzelm@22124
   325
datatype markup = Markup | MarkupEnv | Verbatim;
wenzelm@22124
   326
wenzelm@22124
   327
local
wenzelm@22124
   328
wenzelm@22124
   329
val space_proper =
wenzelm@36959
   330
  Scan.one Token.is_blank -- Scan.many Token.is_comment -- Scan.one Token.is_proper;
wenzelm@22124
   331
wenzelm@36959
   332
val is_improper = not o (Token.is_proper orf Token.is_begin_ignore orf Token.is_end_ignore);
wenzelm@22124
   333
val improper = Scan.many is_improper;
wenzelm@22124
   334
val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper));
wenzelm@36959
   335
val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one Token.is_blank));
wenzelm@22124
   336
wenzelm@36959
   337
val opt_newline = Scan.option (Scan.one Token.is_newline);
wenzelm@22124
   338
wenzelm@22124
   339
val ignore =
wenzelm@36959
   340
  Scan.depend (fn d => opt_newline |-- Scan.one Token.is_begin_ignore
wenzelm@22124
   341
    >> pair (d + 1)) ||
wenzelm@36959
   342
  Scan.depend (fn d => Scan.one Token.is_end_ignore --|
wenzelm@22124
   343
    (if d = 0 then Scan.fail_with (K "Bad nesting of meta-comments") else opt_newline)
wenzelm@22124
   344
    >> pair (d - 1));
wenzelm@22124
   345
wenzelm@36950
   346
val tag = (improper -- Parse.$$$ "%" -- improper) |-- Parse.!!! (Parse.tag_name --| blank_end);
wenzelm@22124
   347
wenzelm@22124
   348
val locale =
wenzelm@36950
   349
  Scan.option ((Parse.$$$ "(" -- improper -- Parse.$$$ "in") |--
wenzelm@36950
   350
    Parse.!!! (improper |-- Parse.xname --| (improper -- Parse.$$$ ")")));
wenzelm@22124
   351
wenzelm@22124
   352
in
wenzelm@22124
   353
wenzelm@28427
   354
fun present_thy lex default_tags is_markup command_results src =
wenzelm@22124
   355
  let
wenzelm@22124
   356
    (* tokens *)
wenzelm@22124
   357
wenzelm@22124
   358
    val ignored = Scan.state --| ignore
wenzelm@22124
   359
      >> (fn d => (NONE, (NoToken, ("", d))));
wenzelm@22124
   360
wenzelm@22124
   361
    fun markup mark mk flag = Scan.peek (fn d =>
wenzelm@36950
   362
      improper |--
wenzelm@36959
   363
        Parse.position
wenzelm@36959
   364
          (Scan.one (Token.is_kind Token.Command andf is_markup mark o Token.content_of)) --
wenzelm@22124
   365
      Scan.repeat tag --
wenzelm@36950
   366
      Parse.!!!! ((improper -- locale -- improper) |-- Parse.doc_source --| improper_end)
wenzelm@22124
   367
      >> (fn (((tok, pos), tags), txt) =>
wenzelm@36959
   368
        let val name = Token.content_of tok
wenzelm@22124
   369
        in (SOME (name, pos, tags), (mk (name, txt), (flag, d))) end));
wenzelm@22124
   370
wenzelm@22124
   371
    val command = Scan.peek (fn d =>
wenzelm@36959
   372
      Parse.position (Scan.one (Token.is_kind Token.Command)) --
wenzelm@22124
   373
      Scan.repeat tag
wenzelm@22124
   374
      >> (fn ((tok, pos), tags) =>
wenzelm@36959
   375
        let val name = Token.content_of tok
wenzelm@22124
   376
        in (SOME (name, pos, tags), (BasicToken tok, (Latex.markup_false, d))) end));
wenzelm@22124
   377
wenzelm@22124
   378
    val cmt = Scan.peek (fn d =>
wenzelm@36950
   379
      Parse.$$$ "--" |-- Parse.!!!! (improper |-- Parse.doc_source)
wenzelm@22124
   380
      >> (fn txt => (NONE, (MarkupToken ("cmt", txt), ("", d)))));
wenzelm@22124
   381
wenzelm@22124
   382
    val other = Scan.peek (fn d =>
wenzelm@36950
   383
       Parse.not_eof >> (fn tok => (NONE, (BasicToken tok, ("", d)))));
wenzelm@22124
   384
wenzelm@22124
   385
    val token =
wenzelm@22124
   386
      ignored ||
wenzelm@22124
   387
      markup Markup MarkupToken Latex.markup_true ||
wenzelm@22124
   388
      markup MarkupEnv MarkupEnvToken Latex.markup_true ||
wenzelm@22124
   389
      markup Verbatim (VerbatimToken o #2) "" ||
wenzelm@22124
   390
      command || cmt || other;
wenzelm@22124
   391
wenzelm@22124
   392
wenzelm@22124
   393
    (* spans *)
wenzelm@22124
   394
wenzelm@36959
   395
    val is_eof = fn (_, (BasicToken x, _)) => Token.is_eof x | _ => false;
wenzelm@36959
   396
    val stopper = Scan.stopper (K (NONE, (BasicToken Token.eof, ("", 0)))) is_eof;
wenzelm@22124
   397
wenzelm@22124
   398
    val cmd = Scan.one (is_some o fst);
wenzelm@27732
   399
    val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2;
wenzelm@22124
   400
wenzelm@22124
   401
    val comments = Scan.many (comment_token o fst o snd);
wenzelm@22124
   402
    val blank = Scan.one (blank_token o fst o snd);
wenzelm@22124
   403
    val newline = Scan.one (newline_token o fst o snd);
wenzelm@22124
   404
    val before_cmd =
wenzelm@22124
   405
      Scan.option (newline -- comments) --
wenzelm@22124
   406
      Scan.option (newline -- comments) --
wenzelm@22124
   407
      Scan.option (blank -- comments) -- cmd;
wenzelm@22124
   408
wenzelm@22124
   409
    val span =
wenzelm@22124
   410
      Scan.repeat non_cmd -- cmd --
wenzelm@22124
   411
        Scan.repeat (Scan.unless before_cmd non_cmd) --
wenzelm@22124
   412
        Scan.option (newline >> (single o snd))
wenzelm@22124
   413
      >> (fn (((toks1, (cmd, tok2)), toks3), tok4) =>
wenzelm@22124
   414
          make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4))));
wenzelm@22124
   415
wenzelm@22124
   416
    val spans =
wenzelm@22124
   417
      src
wenzelm@36959
   418
      |> Source.filter (not o Token.is_semicolon)
wenzelm@36959
   419
      |> Source.source' 0 Token.stopper (Scan.error (Scan.bulk token)) NONE
wenzelm@22124
   420
      |> Source.source stopper (Scan.error (Scan.bulk span)) NONE
wenzelm@22124
   421
      |> Source.exhaust;
wenzelm@28427
   422
wenzelm@28427
   423
wenzelm@28427
   424
    (* present commands *)
wenzelm@28427
   425
wenzelm@28427
   426
    fun present_command tr span st st' =
wenzelm@28427
   427
      Toplevel.setmp_thread_position tr (present_span lex default_tags span st st');
wenzelm@28427
   428
wenzelm@28427
   429
    fun present _ [] = I
wenzelm@28427
   430
      | present st (((tr, st'), span) :: rest) = present_command tr span st st' #> present st' rest;
wenzelm@22124
   431
  in
wenzelm@28427
   432
    if length command_results = length spans then
wenzelm@22124
   433
      ((NONE, []), NONE, true, Buffer.empty, (I, I))
wenzelm@28427
   434
      |> present Toplevel.toplevel (command_results ~~ spans)
wenzelm@22124
   435
      |> present_trailer
wenzelm@22124
   436
    else error "Messed-up outer syntax for presentation"
wenzelm@22124
   437
  end;
wenzelm@22124
   438
wenzelm@22124
   439
end;
wenzelm@22124
   440
wenzelm@22124
   441
wenzelm@22124
   442
wenzelm@22124
   443
(** setup default output **)
wenzelm@22124
   444
wenzelm@22124
   445
(* options *)
wenzelm@22124
   446
wenzelm@39134
   447
val _ = add_option "show_types" (Config.put show_types o boolean);
wenzelm@39134
   448
val _ = add_option "show_sorts" (Config.put show_sorts o boolean);
wenzelm@39127
   449
val _ = add_option "show_structs" (Config.put show_structs o boolean);
wenzelm@38980
   450
val _ = add_option "show_question_marks" (Config.put show_question_marks o boolean);
wenzelm@38766
   451
val _ = add_option "long_names" (add_wrapper o setmp_CRITICAL Name_Space.long_names o boolean);
wenzelm@38766
   452
val _ = add_option "short_names" (add_wrapper o setmp_CRITICAL Name_Space.short_names o boolean);
wenzelm@38766
   453
val _ = add_option "unique_names" (add_wrapper o setmp_CRITICAL Name_Space.unique_names o boolean);
wenzelm@39128
   454
val _ = add_option "eta_contract" (Config.put eta_contract o boolean);
wenzelm@38767
   455
val _ = add_option "display" (Config.put display o boolean);
wenzelm@38767
   456
val _ = add_option "break" (Config.put break o boolean);
wenzelm@38767
   457
val _ = add_option "quotes" (Config.put quotes o boolean);
wenzelm@38766
   458
val _ = add_option "mode" (add_wrapper o Print_Mode.with_modes o single);
wenzelm@38766
   459
val _ = add_option "margin" (add_wrapper o setmp_CRITICAL Pretty.margin_default o integer);
wenzelm@38767
   460
val _ = add_option "indent" (Config.put indent o integer);
wenzelm@38767
   461
val _ = add_option "source" (Config.put source o boolean);
wenzelm@39125
   462
val _ = add_option "goals_limit" (Config.put Goal_Display.goals_limit o integer);
wenzelm@22124
   463
wenzelm@22124
   464
wenzelm@22124
   465
(* basic pretty printing *)
wenzelm@22124
   466
wenzelm@38767
   467
fun tweak_line ctxt s =
wenzelm@38767
   468
  if Config.get ctxt display then s else Symbol.strip_blanks s;
wenzelm@22124
   469
wenzelm@38767
   470
fun pretty_text ctxt =
wenzelm@38767
   471
  Pretty.chunks o map Pretty.str o map (tweak_line ctxt) o Library.split_lines;
wenzelm@22124
   472
wenzelm@26710
   473
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
wenzelm@24920
   474
haftmann@32898
   475
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
haftmann@32898
   476
haftmann@32898
   477
fun pretty_term_style ctxt (style, t) =
haftmann@32898
   478
  pretty_term ctxt (style t);
wenzelm@22124
   479
haftmann@32898
   480
fun pretty_thm_style ctxt (style, th) =
haftmann@32898
   481
  pretty_term ctxt (style (Thm.full_prop_of th));
wenzelm@22124
   482
haftmann@32898
   483
fun pretty_term_typ ctxt (style, t) =
haftmann@32898
   484
  let val t' = style t
wenzelm@39288
   485
  in pretty_term ctxt (Type.constraint (Term.fastype_of t') t') end;
haftmann@32898
   486
haftmann@32898
   487
fun pretty_term_typeof ctxt (style, t) =
haftmann@32898
   488
  Syntax.pretty_typ ctxt (Term.fastype_of (style t));
wenzelm@22124
   489
wenzelm@25373
   490
fun pretty_const ctxt c =
wenzelm@25373
   491
  let
wenzelm@25373
   492
    val t = Const (c, Consts.type_scheme (ProofContext.consts_of ctxt) c)
wenzelm@25373
   493
      handle TYPE (msg, _, _) => error msg;
wenzelm@25373
   494
    val ([t'], _) = Variable.import_terms true [t] ctxt;
wenzelm@25373
   495
  in pretty_term ctxt t' end;
wenzelm@22124
   496
wenzelm@25407
   497
fun pretty_abbrev ctxt s =
wenzelm@22124
   498
  let
wenzelm@25407
   499
    val t = Syntax.parse_term ctxt s |> singleton (ProofContext.standard_infer_types ctxt);
wenzelm@24920
   500
    fun err () = error ("Abbreviated constant expected: " ^ Syntax.string_of_term ctxt t);
wenzelm@22124
   501
    val (head, args) = Term.strip_comb t;
wenzelm@22124
   502
    val (c, T) = Term.dest_Const head handle TERM _ => err ();
wenzelm@25054
   503
    val (U, u) = Consts.the_abbreviation (ProofContext.consts_of ctxt) c
wenzelm@22124
   504
      handle TYPE _ => err ();
wenzelm@22124
   505
    val t' = Term.betapplys (Envir.expand_atom T (U, u), args);
haftmann@32898
   506
    val eq = Logic.mk_equals (t, t');
haftmann@32898
   507
    val ctxt' = Variable.auto_fixes eq ctxt;
haftmann@32898
   508
  in ProofContext.pretty_term_abbrev ctxt' eq end;
wenzelm@22124
   509
haftmann@39305
   510
fun pretty_class ctxt =
haftmann@39305
   511
  Pretty.str o Type.extern_class (ProofContext.tsig_of ctxt) o ProofContext.read_class ctxt;
haftmann@39305
   512
haftmann@39309
   513
fun pretty_type ctxt s =
haftmann@39309
   514
  let
haftmann@39309
   515
    val tsig = ProofContext.tsig_of ctxt;
haftmann@39309
   516
    val _ = ProofContext.read_type_name ctxt false s;
haftmann@39309
   517
  in (Pretty.str o Type.extern_type tsig o Type.intern_type tsig) s end;
haftmann@39305
   518
wenzelm@33388
   519
fun pretty_prf full ctxt = Proof_Syntax.pretty_proof_of ctxt full;
wenzelm@22124
   520
wenzelm@22124
   521
fun pretty_theory ctxt name =
wenzelm@22124
   522
  (Theory.requires (ProofContext.theory_of ctxt) name "presentation"; Pretty.str name);
wenzelm@22124
   523
wenzelm@22124
   524
wenzelm@30390
   525
(* default output *)
wenzelm@30390
   526
wenzelm@36959
   527
val str_of_source = space_implode " " o map Token.unparse o #2 o #1 o Args.dest_src;
wenzelm@22124
   528
wenzelm@38767
   529
fun maybe_pretty_source pretty ctxt src xs =
wenzelm@38767
   530
  map (pretty ctxt) xs  (*always pretty in order to exhibit errors!*)
wenzelm@38767
   531
  |> (if Config.get ctxt source then K [pretty_text ctxt (str_of_source src)] else I);
wenzelm@30390
   532
wenzelm@38767
   533
fun output ctxt prts =
wenzelm@30390
   534
  prts
wenzelm@38767
   535
  |> (if Config.get ctxt quotes then map Pretty.quote else I)
wenzelm@38767
   536
  |> (if Config.get ctxt display then
wenzelm@38767
   537
    map (Output.output o Pretty.string_of o Pretty.indent (Config.get ctxt indent))
wenzelm@22124
   538
    #> space_implode "\\isasep\\isanewline%\n"
wenzelm@22124
   539
    #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@22124
   540
  else
wenzelm@38767
   541
    map (Output.output o (if Config.get ctxt break then Pretty.string_of else Pretty.str_of))
wenzelm@22124
   542
    #> space_implode "\\isasep\\isanewline%\n"
wenzelm@22124
   543
    #> enclose "\\isa{" "}");
wenzelm@22124
   544
wenzelm@30390
   545
wenzelm@30390
   546
wenzelm@30390
   547
(** concrete antiquotations **)
wenzelm@30390
   548
wenzelm@30390
   549
(* basic entities *)
wenzelm@30390
   550
wenzelm@30390
   551
local
wenzelm@30390
   552
wenzelm@30390
   553
fun basic_entities name scan pretty = antiquotation name scan
wenzelm@38767
   554
  (fn {source, context, ...} => output context o maybe_pretty_source pretty context source);
wenzelm@30390
   555
haftmann@32890
   556
fun basic_entities_style name scan pretty = antiquotation name scan
haftmann@32890
   557
  (fn {source, context, ...} => fn (style, xs) =>
wenzelm@38767
   558
    output context
wenzelm@38767
   559
      (maybe_pretty_source (fn ctxt => fn x => pretty ctxt (style, x)) context source xs));
haftmann@32890
   560
wenzelm@30390
   561
fun basic_entity name scan = basic_entities name (scan >> single);
wenzelm@30390
   562
wenzelm@30390
   563
in
wenzelm@30390
   564
haftmann@32890
   565
val _ = basic_entities_style "thm" (Term_Style.parse -- Attrib.thms) pretty_thm_style;
haftmann@32890
   566
val _ = basic_entity "prop" (Term_Style.parse -- Args.prop) pretty_term_style;
haftmann@32890
   567
val _ = basic_entity "term" (Term_Style.parse -- Args.term) pretty_term_style;
haftmann@32898
   568
val _ = basic_entity "term_type" (Term_Style.parse -- Args.term) pretty_term_typ;
haftmann@32898
   569
val _ = basic_entity "typeof" (Term_Style.parse -- Args.term) pretty_term_typeof;
wenzelm@35399
   570
val _ = basic_entity "const" (Args.const_proper false) pretty_const;
wenzelm@30390
   571
val _ = basic_entity "abbrev" (Scan.lift Args.name_source) pretty_abbrev;
wenzelm@30390
   572
val _ = basic_entity "typ" Args.typ_abbrev Syntax.pretty_typ;
haftmann@39305
   573
val _ = basic_entity "class" (Scan.lift Args.name) pretty_class;
haftmann@39305
   574
val _ = basic_entity "type" (Scan.lift Args.name) pretty_type;
wenzelm@38767
   575
val _ = basic_entity "text" (Scan.lift Args.name) pretty_text;
wenzelm@30390
   576
val _ = basic_entities "prf" Attrib.thms (pretty_prf false);
wenzelm@30390
   577
val _ = basic_entities "full_prf" Attrib.thms (pretty_prf true);
wenzelm@30390
   578
val _ = basic_entity "theory" (Scan.lift Args.name) pretty_theory;
wenzelm@30390
   579
haftmann@32898
   580
val _ = basic_entities_style "thm_style" (Term_Style.parse_bare -- Attrib.thms) pretty_thm_style;
haftmann@32898
   581
val _ = basic_entity "term_style" (Term_Style.parse_bare -- Args.term) pretty_term_style;
haftmann@32898
   582
wenzelm@30390
   583
end;
wenzelm@30390
   584
wenzelm@30390
   585
wenzelm@30390
   586
(* goal state *)
wenzelm@30390
   587
wenzelm@30390
   588
local
wenzelm@22124
   589
wenzelm@30367
   590
fun proof_state state =
wenzelm@30367
   591
  (case try Toplevel.proof_of state of
wenzelm@30367
   592
    SOME prf => prf
wenzelm@22124
   593
  | _ => error "No proof state");
wenzelm@22124
   594
wenzelm@30390
   595
fun goal_state name main_goal = antiquotation name (Scan.succeed ())
wenzelm@39125
   596
  (fn {state, context = ctxt, ...} => fn () => output ctxt
wenzelm@39129
   597
    [Pretty.chunks
wenzelm@39129
   598
      (Proof.pretty_goals main_goal (Proof.map_context (K ctxt) (proof_state state)))]);
wenzelm@22124
   599
wenzelm@30390
   600
in
wenzelm@30390
   601
wenzelm@30390
   602
val _ = goal_state "goals" true;
wenzelm@30390
   603
val _ = goal_state "subgoals" false;
wenzelm@30390
   604
wenzelm@30390
   605
end;
wenzelm@22124
   606
wenzelm@22124
   607
wenzelm@30390
   608
(* embedded lemma *)
wenzelm@27522
   609
wenzelm@36950
   610
val _ = Keyword.keyword "by";
wenzelm@27381
   611
wenzelm@30390
   612
val _ = antiquotation "lemma"
wenzelm@30390
   613
  (Args.prop -- Scan.lift (Args.$$$ "by" |-- Method.parse -- Scan.option Method.parse))
wenzelm@30390
   614
  (fn {source, context, ...} => fn (prop, methods) =>
wenzelm@30390
   615
    let
wenzelm@30390
   616
      val prop_src =
wenzelm@30390
   617
        (case Args.dest_src source of ((a, arg :: _), pos) => Args.src ((a, [arg]), pos));
wenzelm@30390
   618
      val _ = context
wenzelm@36323
   619
        |> Proof.theorem NONE (K I) [[(prop, [])]]
wenzelm@30390
   620
        |> Proof.global_terminal_proof methods;
wenzelm@38767
   621
    in output context (maybe_pretty_source pretty_term context prop_src [prop]) end);
wenzelm@30390
   622
wenzelm@30390
   623
wenzelm@30390
   624
(* ML text *)
wenzelm@30390
   625
wenzelm@30390
   626
local
wenzelm@30390
   627
wenzelm@30390
   628
fun ml_text name ml = antiquotation name (Scan.lift Args.name_source_position)
wenzelm@30390
   629
  (fn {context, ...} => fn (txt, pos) =>
wenzelm@37198
   630
   (ML_Context.eval_in (SOME context) false pos (ml pos txt);
wenzelm@30573
   631
    Symbol_Pos.content (Symbol_Pos.explode (txt, pos))
wenzelm@38767
   632
    |> (if Config.get context quotes then quote else I)
wenzelm@38767
   633
    |> (if Config.get context display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@30390
   634
    else
wenzelm@30390
   635
      split_lines
wenzelm@30390
   636
      #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
wenzelm@30390
   637
      #> space_implode "\\isasep\\isanewline%\n")));
wenzelm@30390
   638
wenzelm@37198
   639
fun ml_enclose bg en pos txt =
wenzelm@37198
   640
  ML_Lex.read Position.none bg @ ML_Lex.read pos txt @ ML_Lex.read Position.none en;
wenzelm@37198
   641
wenzelm@30390
   642
in
wenzelm@30390
   643
wenzelm@37198
   644
val _ = ml_text "ML" (ml_enclose "fn _ => (" ");");
wenzelm@37198
   645
val _ = ml_text "ML_type" (ml_enclose "val _ = NONE : (" ") option;");
wenzelm@37198
   646
val _ = ml_text "ML_struct" (ml_enclose "functor XXX() = struct structure XX = " " end;");
wenzelm@37198
   647
wenzelm@37198
   648
val _ = ml_text "ML_functor"   (* FIXME formal treatment of functor name (!?) *)
wenzelm@37198
   649
  (fn pos => fn txt =>
wenzelm@37198
   650
    ML_Lex.read Position.none ("ML_Env.check_functor " ^
wenzelm@37198
   651
      ML_Syntax.print_string (Symbol_Pos.content (Symbol_Pos.explode (txt, pos)))));
wenzelm@37198
   652
wenzelm@37198
   653
val _ = ml_text "ML_text" (K (K []));
wenzelm@22124
   654
wenzelm@22124
   655
end;
wenzelm@30390
   656
wenzelm@30390
   657
end;