src/Pure/Isar/isar_output.ML
author wenzelm
Tue Dec 05 22:14:42 2006 +0100 (2006-12-05)
changeset 21658 5e31241e1e3c
parent 21360 c63755004033
child 21717 410ca6910f6f
permissions -rw-r--r--
Attrib.internal: morphism;
wenzelm@9140
     1
(*  Title:      Pure/Isar/isar_output.ML
wenzelm@9140
     2
    ID:         $Id$
wenzelm@16002
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@9140
     4
wenzelm@9140
     5
Isar theory output.
wenzelm@9140
     6
*)
wenzelm@9140
     7
wenzelm@9140
     8
signature ISAR_OUTPUT =
wenzelm@9140
     9
sig
wenzelm@14899
    10
  val display: bool ref
wenzelm@14899
    11
  val quotes: bool ref
wenzelm@14899
    12
  val indent: int ref
wenzelm@14899
    13
  val source: bool ref
wenzelm@19058
    14
  val add_commands: (string * (Args.src -> Toplevel.node option -> string)) list -> unit
wenzelm@9140
    15
  val add_options: (string * (string -> (unit -> string) -> unit -> string)) list -> unit
wenzelm@9220
    16
  val print_antiquotations: unit -> unit
wenzelm@9140
    17
  val boolean: string -> bool
wenzelm@9140
    18
  val integer: string -> int
wenzelm@18988
    19
  val args: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
wenzelm@19058
    20
    (Args.src -> Proof.context -> 'a -> string) -> Args.src -> Toplevel.node option -> string
wenzelm@9140
    21
  datatype markup = Markup | MarkupEnv | Verbatim
wenzelm@11714
    22
  val modes: string list ref
wenzelm@19058
    23
  val eval_antiquote: Scan.lexicon -> Toplevel.node option -> string * Position.T -> string
wenzelm@17067
    24
  val present_thy: Scan.lexicon -> (string -> string list) -> (markup -> string -> bool) ->
wenzelm@17067
    25
    Toplevel.transition list -> (OuterLex.token, 'a) Source.source -> Buffer.T
wenzelm@17067
    26
  val output_list: (Proof.context -> 'a -> Pretty.T) -> Args.src ->
wenzelm@17067
    27
    Proof.context -> 'a list -> string
wenzelm@17067
    28
  val output: (Proof.context -> 'a -> Pretty.T) -> Args.src -> Proof.context -> 'a -> string
wenzelm@9140
    29
end;
wenzelm@9140
    30
wenzelm@9140
    31
structure IsarOutput: ISAR_OUTPUT =
wenzelm@9140
    32
struct
wenzelm@9140
    33
wenzelm@9140
    34
structure T = OuterLex;
wenzelm@9140
    35
structure P = OuterParse;
wenzelm@9140
    36
wenzelm@17067
    37
wenzelm@17067
    38
(** global options **)
wenzelm@14899
    39
wenzelm@14899
    40
val locale = ref "";
wenzelm@14899
    41
val display = ref false;
wenzelm@14899
    42
val quotes = ref false;
wenzelm@14899
    43
val indent = ref 0;
wenzelm@14899
    44
val source = ref false;
wenzelm@14899
    45
val break = ref false;
wenzelm@14899
    46
wenzelm@14899
    47
wenzelm@14899
    48
wenzelm@9140
    49
(** maintain global commands **)
wenzelm@9140
    50
wenzelm@9140
    51
local
wenzelm@9140
    52
wenzelm@9140
    53
val global_commands =
wenzelm@19058
    54
  ref (Symtab.empty: (Args.src -> Toplevel.node option -> string) Symtab.table);
wenzelm@9140
    55
wenzelm@9140
    56
val global_options =
wenzelm@9140
    57
  ref (Symtab.empty: (string -> (unit -> string) -> unit -> string) Symtab.table);
wenzelm@9140
    58
wenzelm@17067
    59
fun add_item kind (name, x) tab =
wenzelm@16894
    60
 (if not (Symtab.defined tab name) then ()
wenzelm@17067
    61
  else warning ("Redefined antiquotation " ^ kind ^ ": " ^ quote name);
wenzelm@17412
    62
  Symtab.update (name, x) tab);
wenzelm@9140
    63
wenzelm@9140
    64
in
wenzelm@9140
    65
wenzelm@17067
    66
val add_commands = Library.change global_commands o fold (add_item "command");
wenzelm@17067
    67
val add_options = Library.change global_options o fold (add_item "option");
wenzelm@9140
    68
wenzelm@9140
    69
fun command src =
wenzelm@9140
    70
  let val ((name, _), pos) = Args.dest_src src in
wenzelm@17412
    71
    (case Symtab.lookup (! global_commands) name of
skalberg@15531
    72
      NONE => error ("Unknown antiquotation command: " ^ quote name ^ Position.str_of pos)
skalberg@15531
    73
    | SOME f => transform_failure (curry Antiquote.ANTIQUOTE_FAIL (name, pos)) (f src))
wenzelm@9140
    74
  end;
wenzelm@9140
    75
wenzelm@9140
    76
fun option (name, s) f () =
wenzelm@17412
    77
  (case Symtab.lookup (! global_options) name of
skalberg@15531
    78
    NONE => error ("Unknown antiquotation option: " ^ quote name)
skalberg@15531
    79
  | SOME opt => opt s f ());
wenzelm@9140
    80
wenzelm@9140
    81
fun options [] f = f
wenzelm@9140
    82
  | options (opt :: opts) f = option opt (options opts f);
wenzelm@9140
    83
wenzelm@9213
    84
wenzelm@9220
    85
fun print_antiquotations () =
wenzelm@9220
    86
 [Pretty.big_list "antiquotation commands:" (map Pretty.str (Symtab.keys (! global_commands))),
wenzelm@9220
    87
  Pretty.big_list "antiquotation options:" (map Pretty.str (Symtab.keys (! global_options)))]
wenzelm@9220
    88
 |> Pretty.chunks |> Pretty.writeln;
wenzelm@9213
    89
wenzelm@9140
    90
end;
wenzelm@9140
    91
wenzelm@9140
    92
wenzelm@9140
    93
wenzelm@9140
    94
(** syntax of antiquotations **)
wenzelm@9140
    95
wenzelm@9140
    96
(* option values *)
wenzelm@9140
    97
wenzelm@9140
    98
fun boolean "" = true
wenzelm@9140
    99
  | boolean "true" = true
wenzelm@9140
   100
  | boolean "false" = false
wenzelm@9140
   101
  | boolean s = error ("Bad boolean value: " ^ quote s);
wenzelm@9140
   102
wenzelm@9140
   103
fun integer s =
wenzelm@9140
   104
  let
wenzelm@9140
   105
    fun int ss =
wenzelm@14899
   106
      (case Library.read_int ss of (i, []) => i
wenzelm@14899
   107
      | _ => error ("Bad integer value: " ^ quote s));
wenzelm@9140
   108
  in (case Symbol.explode s of "-" :: ss => ~ (int ss) | ss => int ss) end;
wenzelm@9140
   109
wenzelm@9140
   110
wenzelm@9140
   111
(* args syntax *)
wenzelm@9140
   112
wenzelm@18988
   113
fun syntax scan = Args.context_syntax "antiquotation" scan;
wenzelm@9140
   114
wenzelm@19058
   115
fun args scan f src node : string =
wenzelm@14899
   116
  let
wenzelm@19058
   117
    val loc = if ! locale = "" then NONE else SOME (! locale);
wenzelm@20966
   118
    val (ctxt, x) = syntax scan src (Toplevel.presentation_context node loc);
wenzelm@10321
   119
  in f src ctxt x end;
wenzelm@9140
   120
wenzelm@9140
   121
wenzelm@9140
   122
(* outer syntax *)
wenzelm@9140
   123
wenzelm@9140
   124
local
wenzelm@9140
   125
wenzelm@9140
   126
val property = P.xname -- Scan.optional (P.$$$ "=" |-- P.!!! P.xname) "";
wenzelm@9140
   127
val properties = Scan.optional (P.$$$ "[" |-- P.!!! (P.enum "," property --| P.$$$ "]")) [];
wenzelm@9140
   128
wenzelm@9140
   129
val antiq = P.position P.xname -- properties -- P.arguments --| Scan.ahead P.eof
wenzelm@9140
   130
  >> (fn (((x, pos), y), z) => (y, Args.src ((x, z), pos)));
wenzelm@9140
   131
wenzelm@9140
   132
fun antiq_args_aux keyword_lexicon (str, pos) =
wenzelm@9140
   133
  Source.of_string str
wenzelm@9140
   134
  |> Symbol.source false
wenzelm@9140
   135
  |> T.source false (K (keyword_lexicon, Scan.empty_lexicon)) pos
wenzelm@9140
   136
  |> T.source_proper
skalberg@15531
   137
  |> Source.source T.stopper (Scan.error (Scan.bulk (P.!!! antiq))) NONE
wenzelm@9140
   138
  |> Source.exhaust;
wenzelm@9140
   139
wenzelm@9140
   140
in
wenzelm@9140
   141
wenzelm@9140
   142
fun antiq_args lex (s, pos) =
wenzelm@18678
   143
  let
wenzelm@18678
   144
    fun err msg = cat_error msg
wenzelm@18678
   145
      ("Malformed antiquotation: " ^ quote ("@{" ^ s ^ "}") ^ Position.str_of pos);
wenzelm@18678
   146
  in (case antiq_args_aux lex (s, pos) of [x] => x | _ => err "") handle ERROR msg => err msg end;
wenzelm@9140
   147
wenzelm@9140
   148
end;
wenzelm@9140
   149
wenzelm@9140
   150
wenzelm@9140
   151
(* eval_antiquote *)
wenzelm@9140
   152
wenzelm@11714
   153
val modes = ref ([]: string list);
wenzelm@11714
   154
wenzelm@19058
   155
fun eval_antiquote lex node (str, pos) =
wenzelm@9140
   156
  let
wenzelm@9140
   157
    fun expand (Antiquote.Text s) = s
wenzelm@9140
   158
      | expand (Antiquote.Antiq x) =
wenzelm@9140
   159
          let val (opts, src) = antiq_args lex x in
wenzelm@19058
   160
            options opts (fn () => command src node) ();  (*preview errors!*)
wenzelm@11714
   161
            Library.setmp print_mode (! modes @ Latex.modes @ ! print_mode)
wenzelm@19058
   162
              (Output.no_warnings (options opts (fn () => command src node))) ()
wenzelm@10321
   163
          end;
wenzelm@9140
   164
    val ants = Antiquote.antiquotes_of (str, pos);
wenzelm@9140
   165
  in
wenzelm@19058
   166
    if is_none node andalso exists Antiquote.is_antiq ants then
wenzelm@9140
   167
      error ("Cannot expand antiquotations at top-level" ^ Position.str_of pos)
wenzelm@9140
   168
    else implode (map expand ants)
wenzelm@9140
   169
  end;
wenzelm@9140
   170
wenzelm@9140
   171
wenzelm@9140
   172
wenzelm@9140
   173
(** present theory source **)
wenzelm@9140
   174
wenzelm@21309
   175
(*NB: arranging white space around command spans is a black art.*)
wenzelm@21309
   176
wenzelm@17067
   177
(* presentation tokens *)
wenzelm@9140
   178
wenzelm@17067
   179
datatype token =
wenzelm@17067
   180
    NoToken
wenzelm@17067
   181
  | BasicToken of T.token
wenzelm@17067
   182
  | MarkupToken of string * (string * Position.T)
wenzelm@17067
   183
  | MarkupEnvToken of string * (string * Position.T)
wenzelm@17067
   184
  | VerbatimToken of string * Position.T;
berghofe@15430
   185
wenzelm@17067
   186
fun output_token lex state =
wenzelm@17067
   187
  let
wenzelm@19058
   188
    val eval = eval_antiquote lex (try Toplevel.node_of state)
wenzelm@17067
   189
  in
wenzelm@17067
   190
    fn NoToken => ""
wenzelm@17067
   191
     | BasicToken tok => Latex.output_basic tok
wenzelm@17067
   192
     | MarkupToken (cmd, txt) => Latex.output_markup cmd (eval txt)
wenzelm@17067
   193
     | MarkupEnvToken (cmd, txt) => Latex.output_markup_env cmd (eval txt)
wenzelm@17067
   194
     | VerbatimToken txt => Latex.output_verbatim (eval txt)
wenzelm@17067
   195
  end;
berghofe@15430
   196
wenzelm@17067
   197
fun basic_token pred (BasicToken tok) = pred tok
wenzelm@17067
   198
  | basic_token _ _ = false;
berghofe@15430
   199
wenzelm@17067
   200
val improper_token = basic_token (not o T.is_proper);
wenzelm@17067
   201
val comment_token = basic_token T.is_comment;
wenzelm@17067
   202
val blank_token = basic_token T.is_blank;
wenzelm@17067
   203
val newline_token = basic_token T.is_newline;
wenzelm@17067
   204
berghofe@15430
   205
wenzelm@17067
   206
(* command spans *)
wenzelm@17067
   207
wenzelm@17067
   208
type command = string * Position.T * string list;   (*name, position, tags*)
wenzelm@17756
   209
type source = (token * (string * int)) list;        (*token, markup flag, meta-comment depth*)
wenzelm@17067
   210
wenzelm@17067
   211
datatype span = Span of command * (source * source * source * source) * bool;
berghofe@15529
   212
wenzelm@17067
   213
fun make_span cmd src =
wenzelm@17067
   214
  let
wenzelm@17756
   215
    fun take_newline (tok :: toks) =
wenzelm@17756
   216
          if newline_token (fst tok) then ([tok], toks, true)
wenzelm@17067
   217
          else ([], tok :: toks, false)
wenzelm@17067
   218
      | take_newline [] = ([], [], false);
wenzelm@17067
   219
    val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) =
wenzelm@17067
   220
      src
wenzelm@17756
   221
      |> take_prefix (improper_token o fst)
wenzelm@17756
   222
      ||>> take_suffix (improper_token o fst)
wenzelm@17756
   223
      ||>> take_prefix (comment_token o fst)
wenzelm@17185
   224
      ||> take_newline;
wenzelm@17067
   225
  in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end;
wenzelm@9140
   226
wenzelm@9140
   227
wenzelm@17067
   228
(* present spans *)
wenzelm@17067
   229
wenzelm@17067
   230
local
wenzelm@17067
   231
wenzelm@17067
   232
fun err_bad_nesting pos =
wenzelm@17067
   233
  error ("Bad nesting of commands in presentation" ^ pos);
wenzelm@17067
   234
haftmann@21337
   235
fun edge1 f (x : string option, y) = the_default I (Option.map (Buffer.add o f) (if x = y then NONE else x));
haftmann@21337
   236
fun edge2 f (x : string option, y) = the_default I (Option.map (Buffer.add o f) (if x = y then NONE else y));
wenzelm@17067
   237
wenzelm@17067
   238
val begin_tag = edge2 Latex.begin_tag;
wenzelm@17067
   239
val end_tag = edge1 Latex.end_tag;
wenzelm@17067
   240
fun open_delim delim e = edge2 Latex.begin_delim e #> delim #> edge2 Latex.end_delim e;
wenzelm@17067
   241
fun close_delim delim e = edge1 Latex.begin_delim e #> delim #> edge1 Latex.end_delim e;
wenzelm@17067
   242
wenzelm@17067
   243
in
wenzelm@17067
   244
wenzelm@17067
   245
fun present_span lex default_tags span state state'
wenzelm@17067
   246
    (tag_stack, active_tag, newline, buffer, present_cont) =
wenzelm@17067
   247
  let
wenzelm@17756
   248
    val present = fold (fn (tok, (flag, 0)) =>
wenzelm@17263
   249
        Buffer.add (output_token lex state' tok)
wenzelm@17263
   250
        #> Buffer.add flag
wenzelm@17263
   251
      | _ => I);
wenzelm@17067
   252
wenzelm@17067
   253
    val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span;
wenzelm@17067
   254
wenzelm@17067
   255
    val (tag, tags) = tag_stack;
wenzelm@17557
   256
    val tag' = try hd (fold OuterKeyword.update_tags cmd_tags (the_list tag));
wenzelm@17067
   257
wenzelm@17067
   258
    val active_tag' =
wenzelm@17067
   259
      if is_some tag' then tag'
wenzelm@21309
   260
      else if cmd_name = "end" andalso not (Toplevel.is_toplevel state') then NONE
wenzelm@17067
   261
      else try hd (default_tags cmd_name);
wenzelm@17067
   262
    val edge = (active_tag, active_tag');
wenzelm@17067
   263
wenzelm@17067
   264
    val newline' =
wenzelm@17067
   265
      if is_none active_tag' then span_newline else newline;
wenzelm@17067
   266
wenzelm@17067
   267
    val nesting = Toplevel.level state' - Toplevel.level state;
wenzelm@17067
   268
    val tag_stack' =
wenzelm@17067
   269
      if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack
wenzelm@17067
   270
      else if nesting >= 0 then (tag', replicate nesting tag @ tags)
wenzelm@17067
   271
      else
wenzelm@17067
   272
        (case Library.drop (~ nesting - 1, tags) of
wenzelm@17067
   273
          tgs :: tgss => (tgs, tgss)
wenzelm@17067
   274
        | [] => err_bad_nesting (Position.str_of cmd_pos));
wenzelm@17067
   275
wenzelm@17067
   276
    val buffer' =
wenzelm@17067
   277
      buffer
wenzelm@17067
   278
      |> end_tag edge
wenzelm@17067
   279
      |> close_delim (fst present_cont) edge
wenzelm@17067
   280
      |> snd present_cont
wenzelm@17067
   281
      |> open_delim (present (#1 srcs)) edge
wenzelm@17067
   282
      |> begin_tag edge
wenzelm@17067
   283
      |> present (#2 srcs);
wenzelm@17067
   284
    val present_cont' =
wenzelm@17067
   285
      if newline then (present (#3 srcs), present (#4 srcs))
wenzelm@17067
   286
      else (I, present (#3 srcs) #> present (#4 srcs));
wenzelm@17067
   287
  in (tag_stack', active_tag', newline', buffer', present_cont') end;
wenzelm@17067
   288
wenzelm@17067
   289
fun present_trailer ((_, tags), active_tag, _, buffer, present_cont) =
wenzelm@17067
   290
  if not (null tags) then err_bad_nesting " at end of theory"
wenzelm@17067
   291
  else
wenzelm@17067
   292
    buffer
wenzelm@17067
   293
    |> end_tag (active_tag, NONE)
wenzelm@17067
   294
    |> close_delim (fst present_cont) (active_tag, NONE)
wenzelm@17067
   295
    |> snd present_cont;
wenzelm@17067
   296
wenzelm@17067
   297
end;
wenzelm@17067
   298
wenzelm@17067
   299
wenzelm@17067
   300
(* present_thy *)
wenzelm@9140
   301
wenzelm@9140
   302
datatype markup = Markup | MarkupEnv | Verbatim;
wenzelm@9140
   303
wenzelm@9140
   304
local
wenzelm@9140
   305
wenzelm@17067
   306
val space_proper =
wenzelm@17067
   307
  Scan.one T.is_blank -- Scan.any T.is_comment -- Scan.one T.is_proper;
wenzelm@9140
   308
wenzelm@9140
   309
val is_improper = not o (T.is_proper orf T.is_begin_ignore orf T.is_end_ignore);
wenzelm@9140
   310
val improper = Scan.any is_improper;
wenzelm@17067
   311
val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper));
wenzelm@21309
   312
val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one T.is_blank));
wenzelm@9140
   313
wenzelm@17067
   314
val opt_newline = Scan.option (Scan.one T.is_newline);
wenzelm@9140
   315
wenzelm@17067
   316
val ignore =
wenzelm@17067
   317
  Scan.depend (fn d => opt_newline |-- Scan.one T.is_begin_ignore
wenzelm@17067
   318
    >> pair (d + 1)) ||
wenzelm@17067
   319
  Scan.depend (fn d => Scan.one T.is_end_ignore --|
wenzelm@17067
   320
    (if d = 0 then Scan.fail_with (K "Bad nesting of meta-comments") else opt_newline)
wenzelm@17067
   321
    >> pair (d - 1));
wenzelm@9140
   322
wenzelm@21309
   323
val tag = (improper -- P.$$$ "%" -- improper) |-- P.!!! (P.tag_name --| blank_end);
wenzelm@9140
   324
wenzelm@17263
   325
val locale =
wenzelm@17263
   326
  Scan.option ((P.$$$ "(" -- improper -- P.$$$ "in") |--
wenzelm@17263
   327
    P.!!! (improper |-- P.xname --| (improper -- P.$$$ ")")));
wenzelm@17263
   328
wenzelm@9140
   329
in
wenzelm@9140
   330
wenzelm@17067
   331
fun present_thy lex default_tags is_markup trs src =
wenzelm@9140
   332
  let
wenzelm@17067
   333
    (* tokens *)
wenzelm@17067
   334
wenzelm@17067
   335
    val ignored = Scan.state --| ignore
wenzelm@17756
   336
      >> (fn d => (NONE, (NoToken, ("", d))));
wenzelm@17067
   337
wenzelm@17185
   338
    fun markup mark mk flag = Scan.peek (fn d =>
wenzelm@17263
   339
      improper |-- P.position (Scan.one (T.is_kind T.Command andf is_markup mark o T.val_of)) --
wenzelm@17067
   340
      Scan.repeat tag --
wenzelm@17263
   341
      P.!!!! ((improper -- locale -- improper) |-- P.position P.text --| improper_end)
wenzelm@17067
   342
      >> (fn (((tok, pos), tags), txt) =>
wenzelm@17067
   343
        let val name = T.val_of tok
wenzelm@17756
   344
        in (SOME (name, pos, tags), (mk (name, txt), (flag, d))) end));
wenzelm@17067
   345
wenzelm@17067
   346
    val command = Scan.peek (fn d =>
wenzelm@17067
   347
      P.position (Scan.one (T.is_kind T.Command)) --
wenzelm@17067
   348
      Scan.repeat tag
wenzelm@17067
   349
      >> (fn ((tok, pos), tags) =>
wenzelm@17067
   350
        let val name = T.val_of tok
wenzelm@17756
   351
        in (SOME (name, pos, tags), (BasicToken tok, (Latex.markup_false, d))) end));
wenzelm@17067
   352
wenzelm@17067
   353
    val cmt = Scan.peek (fn d =>
wenzelm@17067
   354
      P.$$$ "--" |-- P.!!!! (improper |-- P.position P.text)
wenzelm@17756
   355
      >> (fn txt => (NONE, (MarkupToken ("cmt", txt), ("", d)))));
wenzelm@17067
   356
wenzelm@17067
   357
    val other = Scan.peek (fn d =>
wenzelm@17756
   358
       Scan.one T.not_eof >> (fn tok => (NONE, (BasicToken tok, ("", d)))));
wenzelm@9140
   359
wenzelm@17067
   360
    val token =
wenzelm@17067
   361
      ignored ||
wenzelm@17185
   362
      markup Markup MarkupToken Latex.markup_true ||
wenzelm@17185
   363
      markup MarkupEnv MarkupEnvToken Latex.markup_true ||
wenzelm@17185
   364
      markup Verbatim (VerbatimToken o #2) "" ||
wenzelm@17067
   365
      command || cmt || other;
wenzelm@17067
   366
wenzelm@17067
   367
wenzelm@17067
   368
    (* spans *)
wenzelm@17067
   369
wenzelm@17067
   370
    val stopper =
wenzelm@17756
   371
      ((NONE, (BasicToken (#1 T.stopper), ("", 0))),
wenzelm@17756
   372
        fn (_, (BasicToken x, _)) => #2 T.stopper x | _ => false);
wenzelm@17067
   373
wenzelm@17756
   374
    val cmd = Scan.one (is_some o fst);
wenzelm@17756
   375
    val non_cmd = Scan.one (is_none o fst andf not o #2 stopper) >> #2;
wenzelm@9140
   376
wenzelm@17756
   377
    val comments = Scan.any (comment_token o fst o snd);
wenzelm@17756
   378
    val blank = Scan.one (blank_token o fst o snd);
wenzelm@17756
   379
    val newline = Scan.one (newline_token o fst o snd);
wenzelm@17067
   380
    val before_cmd =
wenzelm@17067
   381
      Scan.option (newline -- comments) --
wenzelm@17067
   382
      Scan.option (newline -- comments) --
wenzelm@17067
   383
      Scan.option (blank -- comments) -- cmd;
wenzelm@17067
   384
wenzelm@17067
   385
    val span =
wenzelm@17067
   386
      Scan.repeat non_cmd -- cmd --
wenzelm@17067
   387
        Scan.repeat (Scan.unless before_cmd non_cmd) --
wenzelm@17756
   388
        Scan.option (newline >> (single o snd))
wenzelm@17067
   389
      >> (fn (((toks1, (cmd, tok2)), toks3), tok4) =>
wenzelm@17756
   390
          make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4))));
wenzelm@17067
   391
wenzelm@17067
   392
    val spans =
wenzelm@9140
   393
      src
wenzelm@9140
   394
      |> Source.filter (not o T.is_semicolon)
wenzelm@17067
   395
      |> Source.source' 0 T.stopper (Scan.error (Scan.bulk token)) NONE
wenzelm@17067
   396
      |> Source.source stopper (Scan.error (Scan.bulk span)) NONE
wenzelm@9140
   397
      |> Source.exhaust;
wenzelm@9140
   398
  in
wenzelm@17067
   399
    if length trs = length spans then
wenzelm@17067
   400
      ((NONE, []), NONE, true, Buffer.empty, (I, I))
wenzelm@17067
   401
      |> Toplevel.present_excursion (trs ~~ map (present_span lex default_tags) spans)
wenzelm@17067
   402
      |> present_trailer
wenzelm@11861
   403
    else error "Messed-up outer syntax for presentation"
wenzelm@9140
   404
  end;
wenzelm@9140
   405
wenzelm@9140
   406
end;
wenzelm@9140
   407
wenzelm@9140
   408
wenzelm@9140
   409
wenzelm@9140
   410
(** setup default output **)
wenzelm@9140
   411
wenzelm@9140
   412
(* options *)
wenzelm@9140
   413
wenzelm@9140
   414
val _ = add_options
wenzelm@9220
   415
 [("show_types", Library.setmp Syntax.show_types o boolean),
wenzelm@9220
   416
  ("show_sorts", Library.setmp Syntax.show_sorts o boolean),
wenzelm@14707
   417
  ("show_structs", Library.setmp show_structs o boolean),
wenzelm@15988
   418
  ("show_question_marks", Library.setmp show_question_marks o boolean),
wenzelm@14696
   419
  ("long_names", Library.setmp NameSpace.long_names o boolean),
wenzelm@16142
   420
  ("short_names", Library.setmp NameSpace.short_names o boolean),
wenzelm@16142
   421
  ("unique_names", Library.setmp NameSpace.unique_names o boolean),
wenzelm@9220
   422
  ("eta_contract", Library.setmp Syntax.eta_contract o boolean),
wenzelm@14899
   423
  ("locale", Library.setmp locale),
wenzelm@9140
   424
  ("display", Library.setmp display o boolean),
berghofe@13929
   425
  ("break", Library.setmp break o boolean),
wenzelm@9140
   426
  ("quotes", Library.setmp quotes o boolean),
wenzelm@9140
   427
  ("mode", fn s => fn f => fn () => Library.setmp print_mode (s :: ! print_mode) f ()),
wenzelm@9732
   428
  ("margin", Pretty.setmp_margin o integer),
wenzelm@9750
   429
  ("indent", Library.setmp indent o integer),
wenzelm@10321
   430
  ("source", Library.setmp source o boolean),
wenzelm@10321
   431
  ("goals_limit", Library.setmp goals_limit o integer)];
wenzelm@9140
   432
wenzelm@16002
   433
wenzelm@17067
   434
(* basic pretty printing *)
wenzelm@9140
   435
wenzelm@17067
   436
val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
kleing@14345
   437
wenzelm@11011
   438
fun tweak_line s =
wenzelm@11011
   439
  if ! display then s else Symbol.strip_blanks s;
wenzelm@9750
   440
wenzelm@11011
   441
val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
wenzelm@9140
   442
wenzelm@20242
   443
fun pretty_term ctxt = ProofContext.pretty_term ctxt o ProofContext.revert_skolems ctxt;
wenzelm@9140
   444
wenzelm@16002
   445
fun pretty_term_typ ctxt t =
wenzelm@19581
   446
  ProofContext.pretty_term ctxt (TypeInfer.constrain t (Term.fastype_of t));
haftmann@15880
   447
wenzelm@16002
   448
fun pretty_term_typeof ctxt = ProofContext.pretty_typ ctxt o Term.fastype_of;
haftmann@15880
   449
wenzelm@16002
   450
fun pretty_term_const ctxt t =
wenzelm@16002
   451
  if Term.is_Const t then pretty_term ctxt t
wenzelm@16002
   452
  else error ("Logical constant expected: " ^ ProofContext.string_of_term ctxt t);
haftmann@15880
   453
wenzelm@17067
   454
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
wenzelm@17067
   455
wenzelm@17067
   456
fun pretty_term_style ctxt (name, t) =
wenzelm@17067
   457
  pretty_term ctxt (TermStyle.the_style (ProofContext.theory_of ctxt) name ctxt t);
wenzelm@9140
   458
wenzelm@17067
   459
fun pretty_thm_style ctxt (name, th) =
wenzelm@17067
   460
  pretty_term_style ctxt (name, Thm.full_prop_of th);
haftmann@15880
   461
wenzelm@17067
   462
fun pretty_prf full ctxt thms =
wenzelm@17067
   463
  Pretty.chunks (map (ProofContext.pretty_proof_of ctxt full) thms);
wenzelm@17067
   464
wenzelm@21360
   465
fun pretty_theory ctxt name =
wenzelm@21360
   466
  (Theory.requires (ProofContext.theory_of ctxt) name "presentation"; Pretty.str name);
wenzelm@21345
   467
wenzelm@17067
   468
wenzelm@17067
   469
(* Isar output *)
haftmann@15880
   470
wenzelm@17067
   471
fun output_list pretty src ctxt xs =
wenzelm@17067
   472
  map (pretty ctxt) xs        (*always pretty in order to exhibit errors!*)
wenzelm@17067
   473
  |> (if ! source then K [pretty_text (str_of_source src)] else I)
wenzelm@17067
   474
  |> (if ! quotes then map Pretty.quote else I)
wenzelm@17067
   475
  |> (if ! display then
wenzelm@17067
   476
    map (Output.output o Pretty.string_of o Pretty.indent (! indent))
wenzelm@17067
   477
    #> space_implode "\\isasep\\isanewline%\n"
wenzelm@17067
   478
    #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@17067
   479
  else
wenzelm@17067
   480
    map (Output.output o (if ! break then Pretty.string_of else Pretty.str_of))
wenzelm@17067
   481
    #> space_implode "\\isasep\\isanewline%\n"
wenzelm@17067
   482
    #> enclose "\\isa{" "}");
berghofe@11524
   483
wenzelm@17067
   484
fun output pretty src ctxt = output_list pretty src ctxt o single;
wenzelm@9732
   485
wenzelm@19058
   486
fun proof_state node =
wenzelm@19058
   487
  (case Option.map Toplevel.proof_node node of
wenzelm@19058
   488
    SOME (SOME prf) => ProofHistory.current prf
wenzelm@19058
   489
  | _ => error "No proof state");
wenzelm@19058
   490
wenzelm@19058
   491
fun output_goals main_goal src node = args (Scan.succeed ()) (output (fn _ => fn _ =>
wenzelm@19058
   492
  Pretty.chunks (Proof.pretty_goals main_goal (proof_state node)))) src node;
kleing@14345
   493
wenzelm@17863
   494
fun ml_val txt = "fn _ => (" ^ txt ^ ");";
wenzelm@17863
   495
fun ml_type txt = "val _ = NONE : (" ^ txt ^ ") option;";
wenzelm@17863
   496
fun ml_struct txt = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
wenzelm@17863
   497
wenzelm@17863
   498
fun output_ml ml src ctxt txt =
wenzelm@17863
   499
 (Context.use_mltext (ml txt) false (SOME (ProofContext.theory_of ctxt));
wenzelm@17067
   500
  (if ! source then str_of_source src else txt)
wenzelm@17067
   501
  |> (if ! quotes then quote else I)
wenzelm@17067
   502
  |> (if ! display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@17067
   503
  else
wenzelm@17067
   504
    split_lines
wenzelm@17067
   505
    #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
wenzelm@17067
   506
    #> space_implode "\\isasep\\isanewline%\n"));
wenzelm@10360
   507
wenzelm@17067
   508
wenzelm@17067
   509
(* commands *)
wenzelm@17067
   510
wenzelm@17067
   511
val _ = add_commands
wenzelm@18988
   512
 [("thm", args Attrib.thms (output_list pretty_thm)),
wenzelm@18988
   513
  ("thm_style", args (Scan.lift Args.liberal_name -- Attrib.thm) (output pretty_thm_style)),
wenzelm@18988
   514
  ("prop", args Args.prop (output pretty_term)),
wenzelm@18988
   515
  ("term", args Args.term (output pretty_term)),
wenzelm@18988
   516
  ("term_style", args (Scan.lift Args.liberal_name -- Args.term) (output pretty_term_style)),
wenzelm@18988
   517
  ("term_type", args Args.term (output pretty_term_typ)),
wenzelm@18988
   518
  ("typeof", args Args.term (output pretty_term_typeof)),
wenzelm@18988
   519
  ("const", args Args.term (output pretty_term_const)),
wenzelm@18988
   520
  ("typ", args Args.typ_abbrev (output ProofContext.pretty_typ)),
wenzelm@17067
   521
  ("text", args (Scan.lift Args.name) (output (K pretty_text))),
wenzelm@10360
   522
  ("goals", output_goals true),
wenzelm@14696
   523
  ("subgoals", output_goals false),
wenzelm@18988
   524
  ("prf", args Attrib.thms (output (pretty_prf false))),
wenzelm@18988
   525
  ("full_prf", args Attrib.thms (output (pretty_prf true))),
wenzelm@21360
   526
  ("theory", args (Scan.lift Args.name) (output pretty_theory)),
wenzelm@17863
   527
  ("ML", args (Scan.lift Args.name) (output_ml ml_val)),
wenzelm@17863
   528
  ("ML_type", args (Scan.lift Args.name) (output_ml ml_type)),
wenzelm@17863
   529
  ("ML_struct", args (Scan.lift Args.name) (output_ml ml_struct))];
wenzelm@9140
   530
wenzelm@9140
   531
end;