src/Pure/Thy/thy_output.ML
author wenzelm
Sat Mar 09 23:57:07 2019 +0100 (2 months ago ago)
changeset 70067 0cb8753bdb50
parent 70064 f8293bf510a0
child 70068 b9985133805d
permissions -rw-r--r--
clarified signature;
wenzelm@22124
     1
(*  Title:      Pure/Thy/thy_output.ML
wenzelm@67386
     2
    Author:     Makarius
wenzelm@22124
     3
wenzelm@67386
     4
Theory document output.
wenzelm@22124
     5
*)
wenzelm@22124
     6
wenzelm@22124
     7
signature THY_OUTPUT =
wenzelm@22124
     8
sig
wenzelm@67569
     9
  val output_document: Proof.context -> {markdown: bool} -> Input.source -> Latex.text list
wenzelm@67381
    10
  val check_comments: Proof.context -> Symbol_Pos.T list -> unit
wenzelm@67381
    11
  val output_token: Proof.context -> Token.T -> Latex.text list
wenzelm@67475
    12
  val output_source: Proof.context -> string -> Latex.text list
wenzelm@68183
    13
  type segment = {span: Command_Span.span, command: Toplevel.transition, state: Toplevel.state}
wenzelm@68506
    14
  val present_thy: Options.T -> theory -> segment list -> Latex.text list
wenzelm@28644
    15
  val pretty_term: Proof.context -> term -> Pretty.T
wenzelm@28644
    16
  val pretty_thm: Proof.context -> thm -> Pretty.T
wenzelm@67508
    17
  val lines: Latex.text list -> Latex.text list
wenzelm@67505
    18
  val items: Latex.text list -> Latex.text list
wenzelm@67464
    19
  val isabelle: Proof.context -> Latex.text list -> Latex.text
wenzelm@67464
    20
  val isabelle_typewriter: Proof.context -> Latex.text list -> Latex.text
wenzelm@67464
    21
  val typewriter: Proof.context -> string -> Latex.text
wenzelm@67464
    22
  val verbatim: Proof.context -> string -> Latex.text
wenzelm@67475
    23
  val source: Proof.context -> Token.src -> Latex.text
wenzelm@67505
    24
  val pretty: Proof.context -> Pretty.T -> Latex.text
wenzelm@67505
    25
  val pretty_source: Proof.context -> Token.src -> Pretty.T -> Latex.text
wenzelm@67505
    26
  val pretty_items: Proof.context -> Pretty.T list -> Latex.text
wenzelm@67505
    27
  val pretty_items_source: Proof.context -> Token.src -> Pretty.T list -> Latex.text
wenzelm@67464
    28
  val antiquotation_pretty:
wenzelm@67505
    29
    binding -> 'a context_parser -> (Proof.context -> 'a -> Pretty.T) -> theory -> theory
wenzelm@69604
    30
  val antiquotation_pretty_embedded:
wenzelm@69604
    31
    binding -> 'a context_parser -> (Proof.context -> 'a -> Pretty.T) -> theory -> theory
wenzelm@67464
    32
  val antiquotation_pretty_source:
wenzelm@67505
    33
    binding -> 'a context_parser -> (Proof.context -> 'a -> Pretty.T) -> theory -> theory
wenzelm@69604
    34
  val antiquotation_pretty_source_embedded:
wenzelm@69604
    35
    binding -> 'a context_parser -> (Proof.context -> 'a -> Pretty.T) -> theory -> theory
wenzelm@67464
    36
  val antiquotation_raw:
wenzelm@67464
    37
    binding -> 'a context_parser -> (Proof.context -> 'a -> Latex.text) -> theory -> theory
wenzelm@69604
    38
  val antiquotation_raw_embedded:
wenzelm@69604
    39
    binding -> 'a context_parser -> (Proof.context -> 'a -> Latex.text) -> theory -> theory
wenzelm@67464
    40
  val antiquotation_verbatim:
wenzelm@67464
    41
    binding -> 'a context_parser -> (Proof.context -> 'a -> string) -> theory -> theory
wenzelm@69604
    42
  val antiquotation_verbatim_embedded:
wenzelm@69604
    43
    binding -> 'a context_parser -> (Proof.context -> 'a -> string) -> theory -> theory
wenzelm@22124
    44
end;
wenzelm@22124
    45
wenzelm@37216
    46
structure Thy_Output: THY_OUTPUT =
wenzelm@22124
    47
struct
wenzelm@22124
    48
wenzelm@67569
    49
(* output document source *)
wenzelm@56548
    50
wenzelm@67571
    51
val output_symbols = single o Latex.symbols_output;
wenzelm@67571
    52
wenzelm@67571
    53
fun output_comment ctxt (kind, syms) =
wenzelm@67571
    54
  (case kind of
wenzelm@67571
    55
    Comment.Comment =>
wenzelm@67571
    56
      Input.cartouche_content syms
wenzelm@67571
    57
      |> output_document (ctxt |> Config.put Document_Antiquotation.thy_output_display false)
wenzelm@67571
    58
          {markdown = false}
wenzelm@67571
    59
      |> Latex.enclose_body "%\n\\isamarkupcmt{" "%\n}"
wenzelm@67571
    60
  | Comment.Cancel =>
wenzelm@67571
    61
      Symbol_Pos.cartouche_content syms
wenzelm@67571
    62
      |> output_symbols
wenzelm@67571
    63
      |> Latex.enclose_body "%\n\\isamarkupcancel{" "}"
wenzelm@67571
    64
  | Comment.Latex =>
wenzelm@67571
    65
      [Latex.symbols (Symbol_Pos.cartouche_content syms)])
wenzelm@67571
    66
and output_comment_document ctxt (comment, syms) =
wenzelm@67571
    67
  (case comment of
wenzelm@67571
    68
    SOME kind => output_comment ctxt (kind, syms)
wenzelm@67571
    69
  | NONE => [Latex.symbols syms])
wenzelm@67571
    70
and output_document_text ctxt syms =
wenzelm@67572
    71
  Comment.read_body syms |> maps (output_comment_document ctxt)
wenzelm@67571
    72
and output_document ctxt {markdown} source =
wenzelm@61457
    73
  let
wenzelm@61457
    74
    val pos = Input.pos_of source;
wenzelm@61457
    75
    val syms = Input.source_explode source;
wenzelm@61457
    76
wenzelm@67571
    77
    val output_antiquotes =
wenzelm@67571
    78
      maps (Document_Antiquotation.evaluate (output_document_text ctxt) ctxt);
wenzelm@61036
    79
wenzelm@61461
    80
    fun output_line line =
wenzelm@67194
    81
      (if Markdown.line_is_item line then [Latex.string "\\item "] else []) @
wenzelm@61461
    82
        output_antiquotes (Markdown.line_content line);
wenzelm@61461
    83
wenzelm@67194
    84
    fun output_block (Markdown.Par lines) =
wenzelm@67194
    85
          Latex.block (separate (Latex.string "\n") (map (Latex.block o output_line) lines))
wenzelm@61459
    86
      | output_block (Markdown.List {kind, body, ...}) =
wenzelm@67194
    87
          Latex.environment_block (Markdown.print_kind kind) (output_blocks body)
wenzelm@67194
    88
    and output_blocks blocks = separate (Latex.string "\n\n") (map output_block blocks);
wenzelm@67184
    89
  in
wenzelm@67381
    90
    if Toplevel.is_skipped_proof (Toplevel.presentation_state ctxt) then []
wenzelm@67184
    91
    else if markdown andalso exists (Markdown.is_control o Symbol_Pos.symbol) syms
wenzelm@67184
    92
    then
wenzelm@67184
    93
      let
wenzelm@67571
    94
        val ants = Antiquote.parse_comments pos syms;
wenzelm@67184
    95
        val reports = Antiquote.antiq_reports ants;
wenzelm@67184
    96
        val blocks = Markdown.read_antiquotes ants;
wenzelm@67381
    97
        val _ = Context_Position.reports ctxt (reports @ Markdown.reports blocks);
wenzelm@67186
    98
      in output_blocks blocks end
wenzelm@67184
    99
    else
wenzelm@67184
   100
      let
wenzelm@67571
   101
        val ants = Antiquote.parse_comments pos (trim (Symbol.is_blank o Symbol_Pos.symbol) syms);
wenzelm@67184
   102
        val reports = Antiquote.antiq_reports ants;
wenzelm@67381
   103
        val _ = Context_Position.reports ctxt (reports @ Markdown.text_reports ants);
wenzelm@67186
   104
      in output_antiquotes ants end
wenzelm@67184
   105
  end;
wenzelm@48918
   106
wenzelm@48918
   107
wenzelm@67425
   108
(* output tokens with formal comments *)
wenzelm@22124
   109
wenzelm@67353
   110
local
wenzelm@67353
   111
wenzelm@67358
   112
val output_symbols_antiq =
wenzelm@67358
   113
  (fn Antiquote.Text syms => output_symbols syms
wenzelm@67353
   114
    | Antiquote.Control {name = (name, _), body, ...} =>
wenzelm@67374
   115
        Latex.string (Latex.output_symbols [Symbol.encode (Symbol.Control name)]) ::
wenzelm@67358
   116
          output_symbols body
wenzelm@67353
   117
    | Antiquote.Antiq {body, ...} =>
wenzelm@67358
   118
        Latex.enclose_body "%\n\\isaantiq\n" "{}%\n\\endisaantiq\n" (output_symbols body));
wenzelm@67353
   119
wenzelm@67571
   120
fun output_comment_symbols ctxt {antiq} (comment, syms) =
wenzelm@67425
   121
  (case (comment, antiq) of
wenzelm@67425
   122
    (NONE, false) => output_symbols syms
wenzelm@67425
   123
  | (NONE, true) =>
wenzelm@67571
   124
      Antiquote.parse_comments (#1 (Symbol_Pos.range syms)) syms
wenzelm@67413
   125
      |> maps output_symbols_antiq
wenzelm@67571
   126
  | (SOME comment, _) => output_comment ctxt (comment, syms));
wenzelm@67372
   127
wenzelm@67381
   128
fun output_body ctxt antiq bg en syms =
wenzelm@67572
   129
  Comment.read_body syms
wenzelm@67572
   130
  |> maps (output_comment_symbols ctxt {antiq = antiq})
wenzelm@68194
   131
  |> Latex.enclose_body bg en;
wenzelm@68194
   132
wenzelm@68194
   133
in
wenzelm@68194
   134
wenzelm@68194
   135
fun output_token ctxt tok =
wenzelm@67353
   136
  let
wenzelm@67378
   137
    fun output antiq bg en =
wenzelm@67381
   138
      output_body ctxt antiq bg en (Input.source_explode (Token.input_of tok));
wenzelm@67378
   139
  in
wenzelm@67378
   140
    (case Token.kind_of tok of
wenzelm@67439
   141
      Token.Comment NONE => []
wenzelm@67378
   142
    | Token.Command => output false "\\isacommand{" "}"
wenzelm@67378
   143
    | Token.Keyword =>
wenzelm@67378
   144
        if Symbol.is_ascii_identifier (Token.content_of tok)
wenzelm@67378
   145
        then output false "\\isakeyword{" "}"
wenzelm@67378
   146
        else output false "" ""
wenzelm@67378
   147
    | Token.String => output false "{\\isachardoublequoteopen}" "{\\isachardoublequoteclose}"
wenzelm@67378
   148
    | Token.Alt_String => output false "{\\isacharbackquoteopen}" "{\\isacharbackquoteclose}"
wenzelm@67378
   149
    | Token.Verbatim => output true "{\\isacharverbatimopen}" "{\\isacharverbatimclose}"
wenzelm@67378
   150
    | Token.Cartouche => output false "{\\isacartoucheopen}" "{\\isacartoucheclose}"
wenzelm@67378
   151
    | _ => output false "" "")
wenzelm@67378
   152
  end handle ERROR msg => error (msg ^ Position.here (Token.pos_of tok));
wenzelm@67353
   153
wenzelm@67475
   154
fun output_source ctxt s =
wenzelm@67475
   155
  output_body ctxt false "" "" (Symbol_Pos.explode (s, Position.none));
wenzelm@67475
   156
wenzelm@67381
   157
fun check_comments ctxt =
wenzelm@67572
   158
  Comment.read_body #> List.app (fn (comment, syms) =>
wenzelm@67388
   159
    let
wenzelm@67388
   160
      val pos = #1 (Symbol_Pos.range syms);
wenzelm@67388
   161
      val _ =
wenzelm@67429
   162
        comment |> Option.app (fn kind =>
wenzelm@67506
   163
          Context_Position.reports ctxt (map (pair pos) (Markup.cartouche :: Comment.markups kind)));
wenzelm@67571
   164
      val _ = output_comment_symbols ctxt {antiq = false} (comment, syms);
wenzelm@67425
   165
    in if comment = SOME Comment.Comment then check_comments ctxt syms else () end);
wenzelm@67377
   166
wenzelm@67353
   167
end;
wenzelm@67353
   168
wenzelm@67353
   169
wenzelm@67356
   170
wenzelm@67356
   171
(** present theory source **)
wenzelm@67356
   172
wenzelm@22124
   173
(* presentation tokens *)
wenzelm@22124
   174
wenzelm@22124
   175
datatype token =
wenzelm@67360
   176
    Ignore_Token
wenzelm@59065
   177
  | Basic_Token of Token.T
wenzelm@59065
   178
  | Markup_Token of string * Input.source
wenzelm@59065
   179
  | Markup_Env_Token of string * Input.source
wenzelm@61456
   180
  | Raw_Token of Input.source;
wenzelm@22124
   181
wenzelm@59065
   182
fun basic_token pred (Basic_Token tok) = pred tok
wenzelm@22124
   183
  | basic_token _ _ = false;
wenzelm@22124
   184
wenzelm@70057
   185
val white_token = basic_token Document_Source.is_white;
wenzelm@70057
   186
val white_comment_token = basic_token Document_Source.is_white_comment;
wenzelm@36959
   187
val blank_token = basic_token Token.is_blank;
wenzelm@36959
   188
val newline_token = basic_token Token.is_newline;
wenzelm@22124
   189
wenzelm@67381
   190
fun present_token ctxt tok =
wenzelm@61457
   191
  (case tok of
wenzelm@67360
   192
    Ignore_Token => []
wenzelm@67381
   193
  | Basic_Token tok => output_token ctxt tok
wenzelm@67357
   194
  | Markup_Token (cmd, source) =>
wenzelm@67357
   195
      Latex.enclose_body ("%\n\\isamarkup" ^ cmd ^ "{") "%\n}\n"
wenzelm@67569
   196
        (output_document ctxt {markdown = false} source)
wenzelm@61457
   197
  | Markup_Env_Token (cmd, source) =>
wenzelm@67569
   198
      [Latex.environment_block ("isamarkup" ^ cmd) (output_document ctxt {markdown = true} source)]
wenzelm@67186
   199
  | Raw_Token source =>
wenzelm@67569
   200
      Latex.string "%\n" :: output_document ctxt {markdown = true} source @ [Latex.string "\n"]);
wenzelm@61455
   201
wenzelm@61455
   202
wenzelm@22124
   203
(* command spans *)
wenzelm@22124
   204
wenzelm@22124
   205
type command = string * Position.T * string list;   (*name, position, tags*)
wenzelm@22124
   206
type source = (token * (string * int)) list;        (*token, markup flag, meta-comment depth*)
wenzelm@22124
   207
wenzelm@22124
   208
datatype span = Span of command * (source * source * source * source) * bool;
wenzelm@22124
   209
wenzelm@22124
   210
fun make_span cmd src =
wenzelm@22124
   211
  let
wenzelm@67522
   212
    fun chop_newline (tok :: toks) =
wenzelm@22124
   213
          if newline_token (fst tok) then ([tok], toks, true)
wenzelm@22124
   214
          else ([], tok :: toks, false)
wenzelm@67522
   215
      | chop_newline [] = ([], [], false);
wenzelm@22124
   216
    val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) =
wenzelm@22124
   217
      src
wenzelm@67522
   218
      |> chop_prefix (white_token o fst)
wenzelm@67522
   219
      ||>> chop_suffix (white_token o fst)
wenzelm@67522
   220
      ||>> chop_prefix (white_comment_token o fst)
wenzelm@67522
   221
      ||> chop_newline;
wenzelm@22124
   222
  in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end;
wenzelm@22124
   223
wenzelm@22124
   224
wenzelm@22124
   225
(* present spans *)
wenzelm@22124
   226
wenzelm@22124
   227
local
wenzelm@22124
   228
wenzelm@22124
   229
fun err_bad_nesting pos =
wenzelm@22124
   230
  error ("Bad nesting of commands in presentation" ^ pos);
wenzelm@22124
   231
wenzelm@22124
   232
fun edge which f (x: string option, y) =
wenzelm@22124
   233
  if x = y then I
wenzelm@67194
   234
  else (case which (x, y) of NONE => I | SOME txt => cons (Latex.string (f txt)));
wenzelm@22124
   235
wenzelm@22124
   236
val begin_tag = edge #2 Latex.begin_tag;
wenzelm@22124
   237
val end_tag = edge #1 Latex.end_tag;
wenzelm@22124
   238
fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e;
wenzelm@22124
   239
fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e;
wenzelm@22124
   240
wenzelm@68507
   241
fun read_tag s =
wenzelm@68512
   242
  (case space_explode "%" s of
wenzelm@68512
   243
    ["", b] => (SOME b, NONE)
wenzelm@68510
   244
  | [a, b] => (NONE, SOME (a, b))
wenzelm@68507
   245
  | _ => error ("Bad document_tags specification: " ^ quote s));
wenzelm@68507
   246
wenzelm@22124
   247
in
wenzelm@22124
   248
wenzelm@68506
   249
fun make_command_tag options keywords =
wenzelm@68506
   250
  let
wenzelm@68507
   251
    val document_tags =
wenzelm@68507
   252
      map read_tag (space_explode "," (Options.string options \<^system_option>\<open>document_tags\<close>));
wenzelm@68507
   253
    val document_tags_default = map_filter #1 document_tags;
wenzelm@68510
   254
    val document_tags_command = map_filter #2 document_tags;
wenzelm@68506
   255
  in
wenzelm@68506
   256
    fn {cmd_name, cmd_tags, tag, active_tag} => fn state => fn state' =>
wenzelm@68506
   257
      let
wenzelm@68506
   258
        val tag' = try hd (fold (update (op =)) cmd_tags (the_list tag));
wenzelm@68508
   259
wenzelm@68509
   260
        val keyword_tags =
wenzelm@68509
   261
          if cmd_name = "end" andalso Toplevel.is_end_theory state' then ["theory"]
wenzelm@68509
   262
          else Keyword.command_tags keywords cmd_name;
wenzelm@68508
   263
        val command_tags =
wenzelm@68508
   264
          the_list (AList.lookup (op =) document_tags_command cmd_name) @
wenzelm@68508
   265
          keyword_tags @ document_tags_default;
wenzelm@68508
   266
wenzelm@68506
   267
        val active_tag' =
wenzelm@68506
   268
          if is_some tag' then tag'
wenzelm@68506
   269
          else
wenzelm@68508
   270
            (case command_tags of
wenzelm@68506
   271
              default_tag :: _ => SOME default_tag
wenzelm@68506
   272
            | [] =>
wenzelm@68506
   273
                if Keyword.is_vacuous keywords cmd_name andalso Toplevel.is_proof state
wenzelm@68506
   274
                then active_tag
wenzelm@68506
   275
                else NONE);
wenzelm@68506
   276
      in {tag' = tag', active_tag' = active_tag'} end
wenzelm@68506
   277
  end;
wenzelm@68506
   278
wenzelm@68506
   279
fun present_span thy command_tag span state state'
wenzelm@67194
   280
    (tag_stack, active_tag, newline, latex, present_cont) =
wenzelm@22124
   281
  let
wenzelm@67381
   282
    val ctxt' =
wenzelm@67381
   283
      Toplevel.presentation_context state'
wenzelm@68482
   284
        handle Toplevel.UNDEF => Proof_Context.get_global thy Context.PureN;
wenzelm@22124
   285
    val present = fold (fn (tok, (flag, 0)) =>
wenzelm@67381
   286
        fold cons (present_token ctxt' tok)
wenzelm@67194
   287
        #> cons (Latex.string flag)
wenzelm@22124
   288
      | _ => I);
wenzelm@22124
   289
wenzelm@22124
   290
    val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span;
wenzelm@22124
   291
wenzelm@22124
   292
    val (tag, tags) = tag_stack;
wenzelm@68506
   293
    val {tag', active_tag'} =
wenzelm@68506
   294
      command_tag {cmd_name = cmd_name, cmd_tags = cmd_tags, tag = tag, active_tag = active_tag}
wenzelm@68506
   295
        state state';
wenzelm@68506
   296
    val edge = (active_tag, active_tag');
wenzelm@22124
   297
wenzelm@59000
   298
    val nesting = Toplevel.level state' - Toplevel.level state;
wenzelm@59000
   299
wenzelm@22124
   300
    val newline' =
wenzelm@22124
   301
      if is_none active_tag' then span_newline else newline;
wenzelm@22124
   302
wenzelm@22124
   303
    val tag_stack' =
wenzelm@22124
   304
      if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack
wenzelm@22124
   305
      else if nesting >= 0 then (tag', replicate nesting tag @ tags)
wenzelm@22124
   306
      else
haftmann@33957
   307
        (case drop (~ nesting - 1) tags of
wenzelm@59000
   308
          tg :: tgs => (tg, tgs)
wenzelm@48992
   309
        | [] => err_bad_nesting (Position.here cmd_pos));
wenzelm@22124
   310
wenzelm@67194
   311
    val latex' =
wenzelm@67194
   312
      latex
wenzelm@22124
   313
      |> end_tag edge
wenzelm@22124
   314
      |> close_delim (fst present_cont) edge
wenzelm@22124
   315
      |> snd present_cont
wenzelm@22124
   316
      |> open_delim (present (#1 srcs)) edge
wenzelm@22124
   317
      |> begin_tag edge
wenzelm@22124
   318
      |> present (#2 srcs);
wenzelm@22124
   319
    val present_cont' =
wenzelm@22124
   320
      if newline then (present (#3 srcs), present (#4 srcs))
wenzelm@22124
   321
      else (I, present (#3 srcs) #> present (#4 srcs));
wenzelm@67194
   322
  in (tag_stack', active_tag', newline', latex', present_cont') end;
wenzelm@22124
   323
wenzelm@67194
   324
fun present_trailer ((_, tags), active_tag, _, latex, present_cont) =
wenzelm@22124
   325
  if not (null tags) then err_bad_nesting " at end of theory"
wenzelm@22124
   326
  else
wenzelm@67194
   327
    latex
wenzelm@22124
   328
    |> end_tag (active_tag, NONE)
wenzelm@22124
   329
    |> close_delim (fst present_cont) (active_tag, NONE)
wenzelm@22124
   330
    |> snd present_cont;
wenzelm@22124
   331
wenzelm@22124
   332
end;
wenzelm@22124
   333
wenzelm@22124
   334
wenzelm@28427
   335
(* present_thy *)
wenzelm@22124
   336
wenzelm@22124
   337
local
wenzelm@22124
   338
wenzelm@61455
   339
val markup_true = "\\isamarkuptrue%\n";
wenzelm@61455
   340
val markup_false = "\\isamarkupfalse%\n";
wenzelm@61455
   341
wenzelm@36959
   342
val opt_newline = Scan.option (Scan.one Token.is_newline);
wenzelm@22124
   343
wenzelm@22124
   344
val ignore =
wenzelm@36959
   345
  Scan.depend (fn d => opt_newline |-- Scan.one Token.is_begin_ignore
wenzelm@22124
   346
    >> pair (d + 1)) ||
wenzelm@36959
   347
  Scan.depend (fn d => Scan.one Token.is_end_ignore --|
wenzelm@43947
   348
    (if d = 0 then Scan.fail_with (K (fn () => "Bad nesting of meta-comments")) else opt_newline)
wenzelm@22124
   349
    >> pair (d - 1));
wenzelm@22124
   350
wenzelm@22124
   351
val locale =
wenzelm@70057
   352
  Scan.option ((Parse.$$$ "(" -- Document_Source.improper -- Parse.$$$ "in") |--
wenzelm@70057
   353
    Parse.!!!
wenzelm@70057
   354
      (Document_Source.improper |-- Parse.name --| (Document_Source.improper -- Parse.$$$ ")")));
wenzelm@22124
   355
wenzelm@22124
   356
in
wenzelm@22124
   357
wenzelm@68183
   358
type segment = {span: Command_Span.span, command: Toplevel.transition, state: Toplevel.state};
wenzelm@68179
   359
wenzelm@68506
   360
fun present_thy options thy (segments: segment list) =
wenzelm@22124
   361
  let
wenzelm@58928
   362
    val keywords = Thy_Header.get_keywords thy;
wenzelm@58928
   363
wenzelm@58928
   364
wenzelm@22124
   365
    (* tokens *)
wenzelm@22124
   366
wenzelm@22124
   367
    val ignored = Scan.state --| ignore
wenzelm@67360
   368
      >> (fn d => (NONE, (Ignore_Token, ("", d))));
wenzelm@22124
   369
wenzelm@59000
   370
    fun markup pred mk flag = Scan.peek (fn d =>
wenzelm@70057
   371
      Document_Source.improper |--
wenzelm@59000
   372
        Parse.position (Scan.one (fn tok =>
wenzelm@59000
   373
          Token.is_command tok andalso pred keywords (Token.content_of tok))) --
wenzelm@70057
   374
      Scan.repeat Document_Source.tag --
wenzelm@70057
   375
      Parse.!!!! ((Document_Source.improper -- locale -- Document_Source.improper) |--
wenzelm@70057
   376
        Parse.document_source --| Document_Source.improper_end)
wenzelm@70057
   377
          >> (fn (((tok, pos'), tags), source) =>
wenzelm@70057
   378
            let val name = Token.content_of tok
wenzelm@70057
   379
            in (SOME (name, pos', tags), (mk (name, source), (flag, d))) end));
wenzelm@22124
   380
wenzelm@22124
   381
    val command = Scan.peek (fn d =>
wenzelm@70057
   382
      Scan.optional (Scan.one Token.is_command_modifier ::: Document_Source.improper) [] --
wenzelm@70057
   383
      Scan.one Token.is_command -- Document_Source.tags
wenzelm@59939
   384
      >> (fn ((cmd_mod, cmd), tags) =>
wenzelm@59939
   385
        map (fn tok => (NONE, (Basic_Token tok, ("", d)))) cmd_mod @
wenzelm@59924
   386
          [(SOME (Token.content_of cmd, Token.pos_of cmd, tags),
wenzelm@61455
   387
            (Basic_Token cmd, (markup_false, d)))]));
wenzelm@22124
   388
wenzelm@22124
   389
    val cmt = Scan.peek (fn d =>
wenzelm@70057
   390
      Scan.one Document_Source.is_black_comment >> (fn tok => (NONE, (Basic_Token tok, ("", d)))));
wenzelm@22124
   391
wenzelm@22124
   392
    val other = Scan.peek (fn d =>
wenzelm@59065
   393
       Parse.not_eof >> (fn tok => (NONE, (Basic_Token tok, ("", d)))));
wenzelm@22124
   394
wenzelm@59924
   395
    val tokens =
wenzelm@59924
   396
      (ignored ||
wenzelm@61455
   397
        markup Keyword.is_document_heading Markup_Token markup_true ||
wenzelm@61455
   398
        markup Keyword.is_document_body Markup_Env_Token markup_true ||
wenzelm@61456
   399
        markup Keyword.is_document_raw (Raw_Token o #2) "") >> single ||
wenzelm@59924
   400
      command ||
wenzelm@59924
   401
      (cmt || other) >> single;
wenzelm@22124
   402
wenzelm@22124
   403
wenzelm@22124
   404
    (* spans *)
wenzelm@22124
   405
wenzelm@59065
   406
    val is_eof = fn (_, (Basic_Token x, _)) => Token.is_eof x | _ => false;
wenzelm@59065
   407
    val stopper = Scan.stopper (K (NONE, (Basic_Token Token.eof, ("", 0)))) is_eof;
wenzelm@22124
   408
wenzelm@22124
   409
    val cmd = Scan.one (is_some o fst);
wenzelm@27732
   410
    val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2;
wenzelm@22124
   411
wenzelm@67439
   412
    val white_comments = Scan.many (white_comment_token o fst o snd);
wenzelm@22124
   413
    val blank = Scan.one (blank_token o fst o snd);
wenzelm@22124
   414
    val newline = Scan.one (newline_token o fst o snd);
wenzelm@22124
   415
    val before_cmd =
wenzelm@67439
   416
      Scan.option (newline -- white_comments) --
wenzelm@67439
   417
      Scan.option (newline -- white_comments) --
wenzelm@67439
   418
      Scan.option (blank -- white_comments) -- cmd;
wenzelm@22124
   419
wenzelm@22124
   420
    val span =
wenzelm@22124
   421
      Scan.repeat non_cmd -- cmd --
wenzelm@22124
   422
        Scan.repeat (Scan.unless before_cmd non_cmd) --
wenzelm@22124
   423
        Scan.option (newline >> (single o snd))
wenzelm@22124
   424
      >> (fn (((toks1, (cmd, tok2)), toks3), tok4) =>
wenzelm@22124
   425
          make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4))));
wenzelm@22124
   426
wenzelm@68179
   427
    val spans = segments
wenzelm@68179
   428
      |> maps (Command_Span.content o #span)
wenzelm@67522
   429
      |> drop_suffix Token.is_space
wenzelm@57080
   430
      |> Source.of_list
wenzelm@59924
   431
      |> Source.source' 0 Token.stopper (Scan.error (Scan.bulk tokens >> flat))
wenzelm@58864
   432
      |> Source.source stopper (Scan.error (Scan.bulk span))
wenzelm@22124
   433
      |> Source.exhaust;
wenzelm@28427
   434
wenzelm@68179
   435
    val command_results =
wenzelm@68183
   436
      segments |> map_filter (fn {command, state, ...} =>
wenzelm@68183
   437
        if Toplevel.is_ignored command then NONE else SOME (command, state));
wenzelm@68179
   438
wenzelm@28427
   439
wenzelm@28427
   440
    (* present commands *)
wenzelm@28427
   441
wenzelm@68506
   442
    val command_tag = make_command_tag options keywords;
wenzelm@67138
   443
wenzelm@28427
   444
    fun present_command tr span st st' =
wenzelm@68506
   445
      Toplevel.setmp_thread_position tr (present_span thy command_tag span st st');
wenzelm@28427
   446
wenzelm@28427
   447
    fun present _ [] = I
wenzelm@68179
   448
      | present st ((span, (tr, st')) :: rest) = present_command tr span st st' #> present st' rest;
wenzelm@22124
   449
  in
wenzelm@28427
   450
    if length command_results = length spans then
wenzelm@67194
   451
      ((NONE, []), NONE, true, [], (I, I))
wenzelm@70067
   452
      |> present (Toplevel.init_toplevel ()) (spans ~~ command_results)
wenzelm@22124
   453
      |> present_trailer
wenzelm@67194
   454
      |> rev
wenzelm@22124
   455
    else error "Messed-up outer syntax for presentation"
wenzelm@22124
   456
  end;
wenzelm@22124
   457
wenzelm@22124
   458
end;
wenzelm@22124
   459
wenzelm@22124
   460
wenzelm@22124
   461
wenzelm@67386
   462
(** standard output operations **)
wenzelm@22124
   463
wenzelm@67464
   464
(* pretty printing *)
wenzelm@22124
   465
wenzelm@67464
   466
fun pretty_term ctxt t =
wenzelm@67464
   467
  Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
wenzelm@24920
   468
haftmann@32898
   469
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
haftmann@32898
   470
wenzelm@22124
   471
wenzelm@30390
   472
(* default output *)
wenzelm@30390
   473
wenzelm@67508
   474
val lines = separate (Latex.string "\\isanewline%\n");
wenzelm@67505
   475
val items = separate (Latex.string "\\isasep\\isanewline%\n");
wenzelm@22124
   476
wenzelm@67464
   477
fun isabelle ctxt body =
wenzelm@67464
   478
  if Config.get ctxt Document_Antiquotation.thy_output_display
wenzelm@67464
   479
  then Latex.environment_block "isabelle" body
wenzelm@67464
   480
  else Latex.block (Latex.enclose_body "\\isa{" "}" body);
wenzelm@59175
   481
wenzelm@67464
   482
fun isabelle_typewriter ctxt body =
wenzelm@67464
   483
  if Config.get ctxt Document_Antiquotation.thy_output_display
wenzelm@67464
   484
  then Latex.environment_block "isabellett" body
wenzelm@67464
   485
  else Latex.block (Latex.enclose_body "\\isatt{" "}" body);
wenzelm@67464
   486
wenzelm@67464
   487
fun typewriter ctxt s =
wenzelm@67464
   488
  isabelle_typewriter ctxt [Latex.string (Latex.output_ascii s)];
wenzelm@67464
   489
wenzelm@67464
   490
fun verbatim ctxt =
wenzelm@67464
   491
  if Config.get ctxt Document_Antiquotation.thy_output_display
wenzelm@67464
   492
  then Document_Antiquotation.indent_lines ctxt #> typewriter ctxt
wenzelm@67508
   493
  else split_lines #> map (typewriter ctxt) #> lines #> Latex.block;
wenzelm@22124
   494
wenzelm@67475
   495
fun source ctxt =
wenzelm@67475
   496
  Token.args_of_src
wenzelm@67475
   497
  #> map (Token.unparse #> Document_Antiquotation.prepare_lines ctxt)
wenzelm@67475
   498
  #> space_implode " "
wenzelm@67475
   499
  #> output_source ctxt
wenzelm@67475
   500
  #> isabelle ctxt;
wenzelm@67475
   501
wenzelm@67464
   502
fun pretty ctxt =
wenzelm@67505
   503
  Document_Antiquotation.output ctxt #> Latex.string #> single #> isabelle ctxt;
wenzelm@67505
   504
wenzelm@67505
   505
fun pretty_source ctxt src prt =
wenzelm@67505
   506
  if Config.get ctxt Document_Antiquotation.thy_output_source
wenzelm@67505
   507
  then source ctxt src else pretty ctxt prt;
wenzelm@67464
   508
wenzelm@67505
   509
fun pretty_items ctxt =
wenzelm@67505
   510
  map (Document_Antiquotation.output ctxt #> Latex.string) #> items #> isabelle ctxt;
wenzelm@67505
   511
wenzelm@67505
   512
fun pretty_items_source ctxt src prts =
wenzelm@67475
   513
  if Config.get ctxt Document_Antiquotation.thy_output_source
wenzelm@67505
   514
  then source ctxt src else pretty_items ctxt prts;
wenzelm@61473
   515
wenzelm@67466
   516
wenzelm@67466
   517
(* antiquotation variants *)
wenzelm@67466
   518
wenzelm@69604
   519
local
wenzelm@67464
   520
wenzelm@69604
   521
fun gen_setup name embedded =
wenzelm@69604
   522
  if embedded
wenzelm@69604
   523
  then Document_Antiquotation.setup_embedded name
wenzelm@69604
   524
  else Document_Antiquotation.setup name;
wenzelm@69604
   525
wenzelm@69604
   526
fun gen_antiquotation_pretty name embedded scan f =
wenzelm@69604
   527
  gen_setup name embedded scan (fn {context = ctxt, argument = x, ...} => pretty ctxt (f ctxt x));
wenzelm@69604
   528
wenzelm@69604
   529
fun gen_antiquotation_pretty_source name embedded scan f =
wenzelm@69604
   530
  gen_setup name embedded scan
wenzelm@67464
   531
    (fn {context = ctxt, source = src, argument = x} => pretty_source ctxt src (f ctxt x));
wenzelm@67464
   532
wenzelm@69604
   533
fun gen_antiquotation_raw name embedded scan f =
wenzelm@69604
   534
  gen_setup name embedded scan (fn {context = ctxt, argument = x, ...} => f ctxt x);
wenzelm@69604
   535
wenzelm@69604
   536
fun gen_antiquotation_verbatim name embedded scan f =
wenzelm@69604
   537
  gen_antiquotation_raw name embedded scan (fn ctxt => verbatim ctxt o f ctxt);
wenzelm@69604
   538
wenzelm@69604
   539
in
wenzelm@67464
   540
wenzelm@69604
   541
fun antiquotation_pretty name = gen_antiquotation_pretty name false;
wenzelm@69604
   542
fun antiquotation_pretty_embedded name = gen_antiquotation_pretty name true;
wenzelm@69604
   543
wenzelm@69604
   544
fun antiquotation_pretty_source name = gen_antiquotation_pretty_source name false;
wenzelm@69604
   545
fun antiquotation_pretty_source_embedded name = gen_antiquotation_pretty_source name true;
wenzelm@69604
   546
wenzelm@69604
   547
fun antiquotation_raw name = gen_antiquotation_raw name false;
wenzelm@69604
   548
fun antiquotation_raw_embedded name = gen_antiquotation_raw name true;
wenzelm@69604
   549
wenzelm@69604
   550
fun antiquotation_verbatim name = gen_antiquotation_verbatim name false;
wenzelm@69604
   551
fun antiquotation_verbatim_embedded name = gen_antiquotation_verbatim name true;
wenzelm@61491
   552
wenzelm@30390
   553
end;
wenzelm@69604
   554
wenzelm@69604
   555
end;