src/Pure/Isar/isar_output.ML
author wenzelm
Mon, 05 Nov 2001 20:59:35 +0100
changeset 12055 a9c44895cc8c
parent 11861 38d8075ebff6
child 12805 3be853cf19cf
permissions -rw-r--r--
pretty/print functions with context;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/isar_output.ML
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
     5
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
     6
Isar theory output.
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
     7
*)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
     8
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
     9
signature ISAR_OUTPUT =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    10
sig
10321
bbaad3045e37 let commands access Toplevel.state;
wenzelm
parents: 9863
diff changeset
    11
  val add_commands: (string * (Args.src -> Toplevel.state -> string)) list -> unit
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    12
  val add_options: (string * (string -> (unit -> string) -> unit -> string)) list -> unit
9220
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
    13
  val print_antiquotations: unit -> unit
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    14
  val boolean: string -> bool
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    15
  val integer: string -> int
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    16
  val args: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
10321
bbaad3045e37 let commands access Toplevel.state;
wenzelm
parents: 9863
diff changeset
    17
    (Args.src -> Proof.context -> 'a -> string) -> Args.src -> Toplevel.state -> string
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    18
  datatype markup = Markup | MarkupEnv | Verbatim
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    19
  val interest_level: int ref
11714
bc0a84063a9c added global modes ref;
wenzelm
parents: 11524
diff changeset
    20
  val modes: string list ref
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    21
  val parse_thy: (markup -> OuterLex.token list -> OuterLex.token * OuterLex.token list) ->
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    22
    Scan.lexicon -> Toplevel.transition list -> (OuterLex.token, 'a) Source.source ->
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    23
      (Toplevel.transition * (Toplevel.state -> Buffer.T -> Buffer.T)) list * Buffer.T
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    24
  val display: bool ref
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    25
  val quotes: bool ref
9732
c32c7ef228c6 added "name" antiq and "indent" option;
wenzelm
parents: 9504
diff changeset
    26
  val indent: int ref
9750
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
    27
  val source: bool ref
11716
064833efb479 Exported output_with.
berghofe
parents: 11714
diff changeset
    28
  val output_with: (Proof.context -> 'a -> Pretty.T) -> Args.src ->
064833efb479 Exported output_with.
berghofe
parents: 11714
diff changeset
    29
    Proof.context -> 'a -> string
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    30
end;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    31
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    32
structure IsarOutput: ISAR_OUTPUT =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    33
struct
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    34
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    35
structure T = OuterLex;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    36
structure P = OuterParse;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    37
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    38
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    39
(** maintain global commands **)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    40
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    41
local
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    42
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    43
val global_commands =
10321
bbaad3045e37 let commands access Toplevel.state;
wenzelm
parents: 9863
diff changeset
    44
  ref (Symtab.empty: (Args.src -> Toplevel.state -> string) Symtab.table);
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    45
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    46
val global_options =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    47
  ref (Symtab.empty: (string -> (unit -> string) -> unit -> string) Symtab.table);
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    48
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    49
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    50
fun add_item kind (tab, (name, x)) =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    51
 (if is_none (Symtab.lookup (tab, name)) then ()
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    52
  else warning ("Redefined antiquotation output " ^ kind ^ ": " ^ quote name);
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    53
  Symtab.update ((name, x), tab));
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    54
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    55
fun add_items kind xs tab = foldl (add_item kind) (tab, xs);
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    56
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    57
in
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    58
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    59
val add_commands = Library.change global_commands o (add_items "command");
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    60
val add_options = Library.change global_options o (add_items "option");
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    61
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    62
fun command src =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    63
  let val ((name, _), pos) = Args.dest_src src in
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    64
    (case Symtab.lookup (! global_commands, name) of
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    65
      None => error ("Unknown antiquotation command: " ^ quote name ^ Position.str_of pos)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    66
    | Some f => transform_failure (curry Comment.OUTPUT_FAIL (name, pos)) (f src))
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    67
  end;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    68
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    69
fun option (name, s) f () =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    70
  (case Symtab.lookup (! global_options, name) of
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    71
    None => error ("Unknown antiquotation option: " ^ quote name)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    72
  | Some opt => opt s f ());
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    73
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    74
fun options [] f = f
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    75
  | options (opt :: opts) f = option opt (options opts f);
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    76
9213
2651a4db8883 help_antiquotations;
wenzelm
parents: 9140
diff changeset
    77
9220
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
    78
fun print_antiquotations () =
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
    79
 [Pretty.big_list "antiquotation commands:" (map Pretty.str (Symtab.keys (! global_commands))),
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
    80
  Pretty.big_list "antiquotation options:" (map Pretty.str (Symtab.keys (! global_options)))]
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
    81
 |> Pretty.chunks |> Pretty.writeln;
9213
2651a4db8883 help_antiquotations;
wenzelm
parents: 9140
diff changeset
    82
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    83
end;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    84
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    85
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    86
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    87
(** syntax of antiquotations **)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    88
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    89
(* option values *)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    90
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    91
fun boolean "" = true
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    92
  | boolean "true" = true
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    93
  | boolean "false" = false
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    94
  | boolean s = error ("Bad boolean value: " ^ quote s);
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    95
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    96
fun integer s =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    97
  let
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    98
    fun int ss =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
    99
      (case Term.read_int ss of (i, []) => i | _ => error ("Bad integer value: " ^ quote s));
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   100
  in (case Symbol.explode s of "-" :: ss => ~ (int ss) | ss => int ss) end;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   101
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   102
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   103
(* args syntax *)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   104
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   105
fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   106
  Args.syntax "antiquotation" scan;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   107
10321
bbaad3045e37 let commands access Toplevel.state;
wenzelm
parents: 9863
diff changeset
   108
fun args scan f src state : string =
bbaad3045e37 let commands access Toplevel.state;
wenzelm
parents: 9863
diff changeset
   109
  let val (ctxt, x) = syntax scan src (Toplevel.context_of state)
bbaad3045e37 let commands access Toplevel.state;
wenzelm
parents: 9863
diff changeset
   110
  in f src ctxt x end;
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   111
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   112
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   113
(* outer syntax *)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   114
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   115
local
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   116
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   117
val property = P.xname -- Scan.optional (P.$$$ "=" |-- P.!!! P.xname) "";
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   118
val properties = Scan.optional (P.$$$ "[" |-- P.!!! (P.enum "," property --| P.$$$ "]")) [];
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   119
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   120
val antiq = P.position P.xname -- properties -- P.arguments --| Scan.ahead P.eof
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   121
  >> (fn (((x, pos), y), z) => (y, Args.src ((x, z), pos)));
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   122
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   123
fun antiq_args_aux keyword_lexicon (str, pos) =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   124
  Source.of_string str
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   125
  |> Symbol.source false
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   126
  |> T.source false (K (keyword_lexicon, Scan.empty_lexicon)) pos
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   127
  |> T.source_proper
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   128
  |> Source.source T.stopper (Scan.error (Scan.bulk (P.!!! antiq))) None
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   129
  |> Source.exhaust;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   130
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   131
in
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   132
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   133
fun antiq_args lex (s, pos) =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   134
  (case antiq_args_aux lex (s, pos) of [x] => x | _ => raise ERROR) handle ERROR =>
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   135
    error ("Malformed antiquotation: " ^ quote ("@{" ^ s ^ "}") ^ Position.str_of pos);
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   136
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   137
end;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   138
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   139
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   140
(* eval_antiquote *)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   141
11714
bc0a84063a9c added global modes ref;
wenzelm
parents: 11524
diff changeset
   142
val modes = ref ([]: string list);
bc0a84063a9c added global modes ref;
wenzelm
parents: 11524
diff changeset
   143
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   144
fun eval_antiquote lex state (str, pos) =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   145
  let
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   146
    fun expand (Antiquote.Text s) = s
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   147
      | expand (Antiquote.Antiq x) =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   148
          let val (opts, src) = antiq_args lex x in
10739
ec19f5902ef5 antiq: preview errors;
wenzelm
parents: 10570
diff changeset
   149
            options opts (fn () => command src state) ();  (*preview errors!*)
11714
bc0a84063a9c added global modes ref;
wenzelm
parents: 11524
diff changeset
   150
            Library.setmp print_mode (! modes @ Latex.modes @ ! print_mode)
10570
fc7afc98a329 append print_modes;
wenzelm
parents: 10360
diff changeset
   151
              (options opts (fn () => command src state)) ()
10321
bbaad3045e37 let commands access Toplevel.state;
wenzelm
parents: 9863
diff changeset
   152
          end;
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   153
    val ants = Antiquote.antiquotes_of (str, pos);
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   154
  in
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   155
    if Toplevel.is_toplevel state andalso exists Antiquote.is_antiq ants then
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   156
      error ("Cannot expand antiquotations at top-level" ^ Position.str_of pos)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   157
    else implode (map expand ants)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   158
  end;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   159
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   160
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   161
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   162
(** present theory source **)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   163
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   164
(* present_tokens *)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   165
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   166
val interest_level = ref 0;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   167
11861
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   168
fun present_tokens lex (flag, toks) state =
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   169
  Buffer.add (case flag of None => "" | Some b => Latex.flag_markup b) o
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   170
   (toks
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   171
    |> mapfilter (fn (tk, i) => if i > ! interest_level then None else Some tk)
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   172
    |> map (apsnd (eval_antiquote lex state))
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   173
    |> Latex.output_tokens
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   174
    |> Buffer.add);
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   175
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   176
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   177
(* parse_thy *)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   178
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   179
datatype markup = Markup | MarkupEnv | Verbatim;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   180
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   181
local
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   182
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   183
val opt_newline = Scan.option (Scan.one T.is_newline);
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   184
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   185
fun check_level i =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   186
  if i > 0 then Scan.succeed ()
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   187
  else Scan.fail_with (K "Bad nesting of meta-comments");
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   188
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   189
val ignore =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   190
  Scan.depend (fn i => opt_newline |-- P.position (Scan.one T.is_begin_ignore) >> pair (i + 1)) ||
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   191
  Scan.depend (fn i => P.position (Scan.one T.is_end_ignore) --|
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   192
    (opt_newline -- check_level i) >> pair (i - 1));
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   193
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   194
val ignore_cmd = Scan.state -- ignore
11861
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   195
  >> (fn (i, (x, pos)) => (false, (None, ((Latex.Basic x, ("", pos)), i))));
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   196
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   197
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   198
val is_improper = not o (T.is_proper orf T.is_begin_ignore orf T.is_end_ignore);
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   199
val improper = Scan.any is_improper;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   200
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   201
val improper_keep_indent = Scan.repeat
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   202
  (Scan.unless (Scan.one T.is_indent -- Scan.one T.is_proper) (Scan.one is_improper));
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   203
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   204
val improper_end =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   205
  (improper -- P.semicolon) |-- improper_keep_indent ||
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   206
  improper_keep_indent;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   207
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   208
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   209
val stopper =
11861
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   210
  ((false, (None, ((Latex.Basic (#1 T.stopper), ("", Position.none)), 0))),
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   211
    fn (_, (_, ((Latex.Basic x, _), _))) => (#2 T.stopper x) | _ => false);
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   212
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   213
in
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   214
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   215
fun parse_thy markup lex trs src =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   216
  let
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   217
    val text = P.position P.text;
9220
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
   218
    val token = Scan.depend (fn i =>
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   219
     (improper |-- markup Markup -- P.!!!! (improper |-- text --| improper_end)
11861
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   220
        >> (fn (x, y) => (true, (Some true, ((Latex.Markup (T.val_of x), y), i)))) ||
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   221
      improper |-- markup MarkupEnv -- P.!!!! (improper |-- text --| improper_end)
11861
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   222
        >> (fn (x, y) => (true, (Some true, ((Latex.MarkupEnv (T.val_of x), y), i)))) ||
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   223
      (P.$$$ "--" |-- P.!!!! (improper |-- text))
11861
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   224
        >> (fn y => (false, (None, ((Latex.Markup "cmt", y), i)))) ||
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   225
      (improper -- markup Verbatim) |-- P.!!!! (improper |-- text --| improper_end)
11861
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   226
        >> (fn y => (true, (None, ((Latex.Verbatim, y), i)))) ||
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   227
      P.position (Scan.one T.not_eof)
11861
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   228
        >> (fn (x, pos) => (T.is_kind T.Command x, (Some false, ((Latex.Basic x, ("", pos)), i)))))
9220
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
   229
      >> pair i);
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   230
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   231
    val body = Scan.any (not o fst andf not o #2 stopper);
11861
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   232
    val section = body -- Scan.one fst -- body
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   233
      >> (fn ((b1, c), b2) => (#1 (#2 c), map (snd o snd) (b1 @ (c :: b2))));
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   234
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   235
    val cmds =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   236
      src
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   237
      |> Source.filter (not o T.is_semicolon)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   238
      |> Source.source' 0 T.stopper (Scan.error (Scan.bulk (ignore_cmd || token))) None
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   239
      |> Source.source stopper (Scan.error (Scan.bulk section)) None
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   240
      |> Source.exhaust;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   241
  in
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   242
    if length cmds = length trs then
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   243
      (trs ~~ map (present_tokens lex) cmds, Buffer.empty)
11861
38d8075ebff6 maintain Latex.flag_markup;
wenzelm
parents: 11716
diff changeset
   244
    else error "Messed-up outer syntax for presentation"
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   245
  end;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   246
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   247
end;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   248
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   249
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   250
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   251
(** setup default output **)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   252
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   253
(* options *)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   254
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   255
val display = ref false;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   256
val quotes = ref false;
9732
c32c7ef228c6 added "name" antiq and "indent" option;
wenzelm
parents: 9504
diff changeset
   257
val indent = ref 0;
9750
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   258
val source = ref false;
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   259
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   260
val _ = add_options
9220
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
   261
 [("show_types", Library.setmp Syntax.show_types o boolean),
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
   262
  ("show_sorts", Library.setmp Syntax.show_sorts o boolean),
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
   263
  ("eta_contract", Library.setmp Syntax.eta_contract o boolean),
d0506ced27c8 added options "eta_contract", "long_names";
wenzelm
parents: 9213
diff changeset
   264
  ("long_names", Library.setmp NameSpace.long_names o boolean),
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   265
  ("display", Library.setmp display o boolean),
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   266
  ("quotes", Library.setmp quotes o boolean),
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   267
  ("mode", fn s => fn f => fn () => Library.setmp print_mode (s :: ! print_mode) f ()),
9732
c32c7ef228c6 added "name" antiq and "indent" option;
wenzelm
parents: 9504
diff changeset
   268
  ("margin", Pretty.setmp_margin o integer),
9750
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   269
  ("indent", Library.setmp indent o integer),
10321
bbaad3045e37 let commands access Toplevel.state;
wenzelm
parents: 9863
diff changeset
   270
  ("source", Library.setmp source o boolean),
bbaad3045e37 let commands access Toplevel.state;
wenzelm
parents: 9863
diff changeset
   271
  ("goals_limit", Library.setmp goals_limit o integer)];
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   272
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   273
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   274
(* commands *)
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   275
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   276
fun cond_quote prt =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   277
  if ! quotes then Pretty.quote prt else prt;
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   278
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   279
fun cond_display prt =
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   280
  if ! display then
9732
c32c7ef228c6 added "name" antiq and "indent" option;
wenzelm
parents: 9504
diff changeset
   281
    Pretty.string_of (Pretty.indent (! indent) prt)
9863
67cdb658e422 tuned output of isabelle env;
wenzelm
parents: 9828
diff changeset
   282
    |> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   283
  else
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   284
    Pretty.str_of prt
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   285
    |> enclose "\\isa{" "}";
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   286
11011
14b78c0c9f05 pretty_text: tweak_lines handles linebreaks gracefully;
wenzelm
parents: 10739
diff changeset
   287
fun tweak_line s =
14b78c0c9f05 pretty_text: tweak_lines handles linebreaks gracefully;
wenzelm
parents: 10739
diff changeset
   288
  if ! display then s else Symbol.strip_blanks s;
9750
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   289
11011
14b78c0c9f05 pretty_text: tweak_lines handles linebreaks gracefully;
wenzelm
parents: 10739
diff changeset
   290
val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   291
9750
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   292
val pretty_source =
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   293
  pretty_text o space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   294
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 11861
diff changeset
   295
fun pretty_term ctxt = ProofContext.pretty_term ctxt o ProofContext.extern_skolem ctxt;
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   296
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 11861
diff changeset
   297
fun pretty_thms ctxt thms =
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   298
  Pretty.chunks (map (pretty_term ctxt o #prop o Thm.rep_thm) thms);
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   299
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 11861
diff changeset
   300
fun pretty_prf full ctxt thms =    (* FIXME context syntax!? *)
11524
197f2e14a714 Added functions for printing primitive proof terms.
berghofe
parents: 11240
diff changeset
   301
  Pretty.chunks (map (ProofSyntax.pretty_proof_of full) thms);
197f2e14a714 Added functions for printing primitive proof terms.
berghofe
parents: 11240
diff changeset
   302
9750
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   303
fun output_with pretty src ctxt x =
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   304
  let
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 11861
diff changeset
   305
    val prt = pretty ctxt x;      (*always pretty in order to catch errors!*)
9750
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   306
    val prt' = if ! source then pretty_source src else prt;
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   307
  in cond_display (cond_quote prt') end;
9732
c32c7ef228c6 added "name" antiq and "indent" option;
wenzelm
parents: 9504
diff changeset
   308
10360
807992b67edd tuned goals output;
wenzelm
parents: 10350
diff changeset
   309
fun output_goals main_goal src state = args (Scan.succeed ()) (output_with (fn _ => fn _ =>
807992b67edd tuned goals output;
wenzelm
parents: 10350
diff changeset
   310
  Pretty.chunks (Proof.pretty_goals main_goal (Toplevel.proof_of state
807992b67edd tuned goals output;
wenzelm
parents: 10350
diff changeset
   311
      handle Toplevel.UNDEF => error "No proof state")))) src state;
807992b67edd tuned goals output;
wenzelm
parents: 10350
diff changeset
   312
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   313
val _ = add_commands
9750
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   314
 [("text", args (Scan.lift Args.name) (output_with (K pretty_text))),
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 11861
diff changeset
   315
  ("thm", args Attrib.local_thmss (output_with pretty_thms)),
11524
197f2e14a714 Added functions for printing primitive proof terms.
berghofe
parents: 11240
diff changeset
   316
  ("prf", args Attrib.local_thmss (output_with (pretty_prf false))),
197f2e14a714 Added functions for printing primitive proof terms.
berghofe
parents: 11240
diff changeset
   317
  ("full_prf", args Attrib.local_thmss (output_with (pretty_prf true))),
9750
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   318
  ("prop", args Args.local_prop (output_with pretty_term)),
270cd9831e7b added "source" option;
wenzelm
parents: 9732
diff changeset
   319
  ("term", args Args.local_term (output_with pretty_term)),
12055
a9c44895cc8c pretty/print functions with context;
wenzelm
parents: 11861
diff changeset
   320
  ("typ", args Args.local_typ_no_norm (output_with ProofContext.pretty_typ)),
10360
807992b67edd tuned goals output;
wenzelm
parents: 10350
diff changeset
   321
  ("goals", output_goals true),
807992b67edd tuned goals output;
wenzelm
parents: 10350
diff changeset
   322
  ("subgoals", output_goals false)];
9140
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   323
69e8938c2409 Isar theory output.
wenzelm
parents:
diff changeset
   324
end;