author | wenzelm |
Tue, 09 Jan 2018 17:09:34 +0100 | |
changeset 67388 | 5fc0b0c9a735 |
parent 67386 | 998e01d6f8fd |
child 67393 | be88c2bc8a45 |
permissions | -rw-r--r-- |
22124 | 1 |
(* Title: Pure/Thy/thy_output.ML |
67386 | 2 |
Author: Makarius |
22124 | 3 |
|
67386 | 4 |
Theory document output. |
22124 | 5 |
*) |
6 |
||
7 |
signature THY_OUTPUT = |
|
8 |
sig |
|
67381 | 9 |
val output_text: Proof.context -> {markdown: bool} -> Input.source -> Latex.text list |
10 |
val check_comments: Proof.context -> Symbol_Pos.T list -> unit |
|
11 |
val check_token_comments: Proof.context -> Token.T -> unit |
|
12 |
val output_token: Proof.context -> Token.T -> Latex.text list |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
13 |
val present_thy: theory -> (Toplevel.transition * Toplevel.state) list -> |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
14 |
Token.T list -> Latex.text list |
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
15 |
val pretty_text: Proof.context -> string -> Pretty.T |
28644 | 16 |
val pretty_term: Proof.context -> term -> Pretty.T |
17 |
val pretty_thm: Proof.context -> thm -> Pretty.T |
|
58011
bc6bced136e5
tuned signature -- moved type src to Token, without aliases;
wenzelm
parents:
57080
diff
changeset
|
18 |
val str_of_source: Token.src -> string |
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
19 |
val maybe_pretty_source: (Proof.context -> 'a -> Pretty.T) -> Proof.context -> |
58011
bc6bced136e5
tuned signature -- moved type src to Token, without aliases;
wenzelm
parents:
57080
diff
changeset
|
20 |
Token.src -> 'a list -> Pretty.T list |
59175
bf465f335e85
system option "pretty_margin" is superseded by "thy_output_margin";
wenzelm
parents:
59067
diff
changeset
|
21 |
val string_of_margin: Proof.context -> Pretty.T -> string |
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
22 |
val output: Proof.context -> Pretty.T list -> string |
58716
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents:
58069
diff
changeset
|
23 |
val verbatim_text: Proof.context -> string -> string |
22124 | 24 |
end; |
25 |
||
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37198
diff
changeset
|
26 |
structure Thy_Output: THY_OUTPUT = |
22124 | 27 |
struct |
28 |
||
61457 | 29 |
(* output text *) |
56548 | 30 |
|
67381 | 31 |
fun output_text ctxt {markdown} source = |
61457 | 32 |
let |
33 |
val pos = Input.pos_of source; |
|
34 |
val syms = Input.source_explode source; |
|
35 |
||
67381 | 36 |
val _ = Context_Position.report ctxt pos (Markup.language_document (Input.is_delimited source)); |
62749 | 37 |
|
67184 | 38 |
val output_antiquotes = |
67386 | 39 |
map (fn ant => |
40 |
Latex.text (Document_Antiquotation.evaluate ctxt ant, #1 (Antiquote.range [ant]))); |
|
61036
f6f2959bed67
clarified language context, e.g. relevant for symbols;
wenzelm
parents:
60100
diff
changeset
|
41 |
|
61461 | 42 |
fun output_line line = |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
43 |
(if Markdown.line_is_item line then [Latex.string "\\item "] else []) @ |
61461 | 44 |
output_antiquotes (Markdown.line_content line); |
45 |
||
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
46 |
fun output_block (Markdown.Par lines) = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
47 |
Latex.block (separate (Latex.string "\n") (map (Latex.block o output_line) lines)) |
61459
5f2ddeb15c06
clarified nesting of paragraphs: indentation is taken into account more uniformly;
wenzelm
parents:
61458
diff
changeset
|
48 |
| output_block (Markdown.List {kind, body, ...}) = |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
49 |
Latex.environment_block (Markdown.print_kind kind) (output_blocks body) |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
50 |
and output_blocks blocks = separate (Latex.string "\n\n") (map output_block blocks); |
67184 | 51 |
in |
67381 | 52 |
if Toplevel.is_skipped_proof (Toplevel.presentation_state ctxt) then [] |
67184 | 53 |
else if markdown andalso exists (Markdown.is_control o Symbol_Pos.symbol) syms |
54 |
then |
|
55 |
let |
|
56 |
val ants = Antiquote.parse pos syms; |
|
57 |
val reports = Antiquote.antiq_reports ants; |
|
58 |
val blocks = Markdown.read_antiquotes ants; |
|
67381 | 59 |
val _ = Context_Position.reports ctxt (reports @ Markdown.reports blocks); |
67186
a58bbe66ac81
avoid excessive whitespace between antiquotations and text;
wenzelm
parents:
67184
diff
changeset
|
60 |
in output_blocks blocks end |
67184 | 61 |
else |
62 |
let |
|
63 |
val ants = Antiquote.parse pos (Symbol_Pos.trim_blanks syms); |
|
64 |
val reports = Antiquote.antiq_reports ants; |
|
67381 | 65 |
val _ = Context_Position.reports ctxt (reports @ Markdown.text_reports ants); |
67186
a58bbe66ac81
avoid excessive whitespace between antiquotations and text;
wenzelm
parents:
67184
diff
changeset
|
66 |
in output_antiquotes ants end |
67184 | 67 |
end; |
48918
6e5fd4585512
check side-comments of command spans (normally filtered out in Outer_Syntax.toplevel_source);
wenzelm
parents:
47005
diff
changeset
|
68 |
|
6e5fd4585512
check side-comments of command spans (normally filtered out in Outer_Syntax.toplevel_source);
wenzelm
parents:
47005
diff
changeset
|
69 |
|
67356
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
70 |
(* output tokens with comments *) |
22124 | 71 |
|
67353 | 72 |
local |
73 |
||
67356
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
74 |
fun output_symbols syms = |
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
75 |
[Latex.text (Latex.output_symbols (map Symbol_Pos.symbol syms), #1 (Symbol_Pos.range syms))]; |
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
76 |
|
67358 | 77 |
val output_symbols_antiq = |
78 |
(fn Antiquote.Text syms => output_symbols syms |
|
67353 | 79 |
| Antiquote.Control {name = (name, _), body, ...} => |
67374
5a049cf98438
clarified output (see also 909dcdec2122, 34d1913f0b20);
wenzelm
parents:
67372
diff
changeset
|
80 |
Latex.string (Latex.output_symbols [Symbol.encode (Symbol.Control name)]) :: |
67358 | 81 |
output_symbols body |
67353 | 82 |
| Antiquote.Antiq {body, ...} => |
67358 | 83 |
Latex.enclose_body "%\n\\isaantiq\n" "{}%\n\\endisaantiq\n" (output_symbols body)); |
67353 | 84 |
|
67381 | 85 |
fun output_symbols_comment ctxt {antiq} (is_comment, syms) = |
67359
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
86 |
if is_comment then |
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
87 |
Latex.enclose_body ("%\n\\isamarkupcmt{") "}" |
67381 | 88 |
(output_text ctxt {markdown = false} |
67359
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
89 |
(Input.source true (Symbol_Pos.implode syms) (Symbol_Pos.range syms))) |
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
90 |
else if antiq then maps output_symbols_antiq (Antiquote.parse (#1 (Symbol_Pos.range syms)) syms) |
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
91 |
else output_symbols syms; |
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
92 |
|
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
93 |
val scan_symbols_comment = |
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
94 |
Scan.many1 (fn (s, _) => s <> Symbol.comment andalso Symbol.not_eof s) >> pair false || |
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
95 |
(Symbol_Pos.$$ Symbol.comment ::: Scan.many (Symbol.is_blank o Symbol_Pos.symbol)) -- |
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
96 |
Scan.option (Symbol_Pos.scan_cartouche_content "Document token error: ") |
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
97 |
>> (fn (syms, NONE) => (false, syms) | (_, SOME syms) => (true, syms)); |
fed0e220be45
more uniform output: formal comments within {* ... *};
wenzelm
parents:
67358
diff
changeset
|
98 |
|
67375 | 99 |
fun read_symbols_comment syms = |
100 |
if exists (fn (s, _) => s = Symbol.comment) syms then |
|
101 |
Scan.read Symbol_Pos.stopper (Scan.repeat scan_symbols_comment) syms |
|
102 |
else NONE; |
|
67372 | 103 |
|
67353 | 104 |
in |
105 |
||
67381 | 106 |
fun output_body ctxt antiq bg en syms = |
67375 | 107 |
(case read_symbols_comment syms of |
67381 | 108 |
SOME res => maps (output_symbols_comment ctxt {antiq = antiq}) res |
67375 | 109 |
| NONE => output_symbols syms) |> Latex.enclose_body bg en |
67381 | 110 |
and output_token ctxt tok = |
67353 | 111 |
let |
67378 | 112 |
fun output antiq bg en = |
67381 | 113 |
output_body ctxt antiq bg en (Input.source_explode (Token.input_of tok)); |
67378 | 114 |
in |
115 |
(case Token.kind_of tok of |
|
116 |
Token.Comment => [] |
|
117 |
| Token.Command => output false "\\isacommand{" "}" |
|
118 |
| Token.Keyword => |
|
119 |
if Symbol.is_ascii_identifier (Token.content_of tok) |
|
120 |
then output false "\\isakeyword{" "}" |
|
121 |
else output false "" "" |
|
122 |
| Token.String => output false "{\\isachardoublequoteopen}" "{\\isachardoublequoteclose}" |
|
123 |
| Token.Alt_String => output false "{\\isacharbackquoteopen}" "{\\isacharbackquoteclose}" |
|
124 |
| Token.Verbatim => output true "{\\isacharverbatimopen}" "{\\isacharverbatimclose}" |
|
125 |
| Token.Cartouche => output false "{\\isacartoucheopen}" "{\\isacartoucheclose}" |
|
126 |
| _ => output false "" "") |
|
127 |
end handle ERROR msg => error (msg ^ Position.here (Token.pos_of tok)); |
|
67353 | 128 |
|
67381 | 129 |
fun check_comments ctxt = |
67377
143665524d8e
check formal comments recursively, within arbitrary cartouches (unknown sublanguages);
wenzelm
parents:
67375
diff
changeset
|
130 |
read_symbols_comment #> (Option.app o List.app) (fn (is_comment, syms) => |
67388
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
131 |
let |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
132 |
val pos = #1 (Symbol_Pos.range syms); |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
133 |
val _ = |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
134 |
if is_comment then |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
135 |
Context_Position.reports ctxt |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
136 |
[(pos, Markup.language_document true), |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
137 |
(pos, Markup.cartouche)] |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
138 |
else (); |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
139 |
val _ = output_symbols_comment ctxt {antiq = false} (is_comment, syms); |
5fc0b0c9a735
clarified markup: more like outer syntax side-comment;
wenzelm
parents:
67386
diff
changeset
|
140 |
in if is_comment then check_comments ctxt syms else () end); |
67377
143665524d8e
check formal comments recursively, within arbitrary cartouches (unknown sublanguages);
wenzelm
parents:
67375
diff
changeset
|
141 |
|
67381 | 142 |
fun check_token_comments ctxt tok = |
143 |
check_comments ctxt (Input.source_explode (Token.input_of tok)); |
|
67377
143665524d8e
check formal comments recursively, within arbitrary cartouches (unknown sublanguages);
wenzelm
parents:
67375
diff
changeset
|
144 |
|
67353 | 145 |
end; |
146 |
||
147 |
||
67356
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
148 |
|
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
149 |
(** present theory source **) |
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
150 |
|
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
151 |
(*NB: arranging white space around command spans is a black art*) |
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
152 |
|
ba226b87c69e
output token content with formal comments and antiquotations;
wenzelm
parents:
67354
diff
changeset
|
153 |
|
22124 | 154 |
(* presentation tokens *) |
155 |
||
156 |
datatype token = |
|
67360 | 157 |
Ignore_Token |
59065 | 158 |
| Basic_Token of Token.T |
159 |
| Markup_Token of string * Input.source |
|
160 |
| Markup_Env_Token of string * Input.source |
|
61456 | 161 |
| Raw_Token of Input.source; |
22124 | 162 |
|
59065 | 163 |
fun basic_token pred (Basic_Token tok) = pred tok |
22124 | 164 |
| basic_token _ _ = false; |
165 |
||
51267 | 166 |
val improper_token = basic_token Token.is_improper; |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
167 |
val comment_token = basic_token Token.is_comment; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
168 |
val blank_token = basic_token Token.is_blank; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
169 |
val newline_token = basic_token Token.is_newline; |
22124 | 170 |
|
67381 | 171 |
fun present_token ctxt tok = |
61457 | 172 |
(case tok of |
67360 | 173 |
Ignore_Token => [] |
67381 | 174 |
| Basic_Token tok => output_token ctxt tok |
67357 | 175 |
| Markup_Token (cmd, source) => |
176 |
Latex.enclose_body ("%\n\\isamarkup" ^ cmd ^ "{") "%\n}\n" |
|
67381 | 177 |
(output_text ctxt {markdown = false} source) |
61457 | 178 |
| Markup_Env_Token (cmd, source) => |
67381 | 179 |
[Latex.environment_block ("isamarkup" ^ cmd) (output_text ctxt {markdown = true} source)] |
67186
a58bbe66ac81
avoid excessive whitespace between antiquotations and text;
wenzelm
parents:
67184
diff
changeset
|
180 |
| Raw_Token source => |
67381 | 181 |
Latex.string "%\n" :: output_text ctxt {markdown = true} source @ [Latex.string "\n"]); |
61455 | 182 |
|
183 |
||
22124 | 184 |
(* command spans *) |
185 |
||
186 |
type command = string * Position.T * string list; (*name, position, tags*) |
|
187 |
type source = (token * (string * int)) list; (*token, markup flag, meta-comment depth*) |
|
188 |
||
189 |
datatype span = Span of command * (source * source * source * source) * bool; |
|
190 |
||
191 |
fun make_span cmd src = |
|
192 |
let |
|
193 |
fun take_newline (tok :: toks) = |
|
194 |
if newline_token (fst tok) then ([tok], toks, true) |
|
195 |
else ([], tok :: toks, false) |
|
196 |
| take_newline [] = ([], [], false); |
|
197 |
val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) = |
|
198 |
src |
|
199 |
|> take_prefix (improper_token o fst) |
|
200 |
||>> take_suffix (improper_token o fst) |
|
201 |
||>> take_prefix (comment_token o fst) |
|
202 |
||> take_newline; |
|
203 |
in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end; |
|
204 |
||
205 |
||
206 |
(* present spans *) |
|
207 |
||
208 |
local |
|
209 |
||
210 |
fun err_bad_nesting pos = |
|
211 |
error ("Bad nesting of commands in presentation" ^ pos); |
|
212 |
||
213 |
fun edge which f (x: string option, y) = |
|
214 |
if x = y then I |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
215 |
else (case which (x, y) of NONE => I | SOME txt => cons (Latex.string (f txt))); |
22124 | 216 |
|
217 |
val begin_tag = edge #2 Latex.begin_tag; |
|
218 |
val end_tag = edge #1 Latex.end_tag; |
|
219 |
fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e; |
|
220 |
fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e; |
|
221 |
||
222 |
in |
|
223 |
||
67381 | 224 |
fun present_span thy keywords document_tags span state state' |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
225 |
(tag_stack, active_tag, newline, latex, present_cont) = |
22124 | 226 |
let |
67381 | 227 |
val ctxt' = |
228 |
Toplevel.presentation_context state' |
|
229 |
handle Toplevel.UNDEF => Proof_Context.init_global (Context.get_theory thy Context.PureN); |
|
22124 | 230 |
val present = fold (fn (tok, (flag, 0)) => |
67381 | 231 |
fold cons (present_token ctxt' tok) |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
232 |
#> cons (Latex.string flag) |
22124 | 233 |
| _ => I); |
234 |
||
235 |
val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span; |
|
236 |
||
237 |
val (tag, tags) = tag_stack; |
|
46924 | 238 |
val tag' = try hd (fold (update (op =)) cmd_tags (the_list tag)); |
22124 | 239 |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
240 |
val nesting = Toplevel.level state' - Toplevel.level state; |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
241 |
|
22124 | 242 |
val active_tag' = |
243 |
if is_some tag' then tag' |
|
244 |
else if cmd_name = "end" andalso not (Toplevel.is_toplevel state') then NONE |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
245 |
else |
67138 | 246 |
(case Keyword.command_tags keywords cmd_name @ document_tags of |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
247 |
default_tag :: _ => SOME default_tag |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
248 |
| [] => |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
249 |
if Keyword.is_vacuous keywords cmd_name andalso Toplevel.is_proof state |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
250 |
then active_tag |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
251 |
else NONE); |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
252 |
|
22124 | 253 |
val edge = (active_tag, active_tag'); |
254 |
||
255 |
val newline' = |
|
256 |
if is_none active_tag' then span_newline else newline; |
|
257 |
||
258 |
val tag_stack' = |
|
259 |
if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack |
|
260 |
else if nesting >= 0 then (tag', replicate nesting tag @ tags) |
|
261 |
else |
|
33957 | 262 |
(case drop (~ nesting - 1) tags of |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
263 |
tg :: tgs => (tg, tgs) |
48992 | 264 |
| [] => err_bad_nesting (Position.here cmd_pos)); |
22124 | 265 |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
266 |
val latex' = |
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
267 |
latex |
22124 | 268 |
|> end_tag edge |
269 |
|> close_delim (fst present_cont) edge |
|
270 |
|> snd present_cont |
|
271 |
|> open_delim (present (#1 srcs)) edge |
|
272 |
|> begin_tag edge |
|
273 |
|> present (#2 srcs); |
|
274 |
val present_cont' = |
|
275 |
if newline then (present (#3 srcs), present (#4 srcs)) |
|
276 |
else (I, present (#3 srcs) #> present (#4 srcs)); |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
277 |
in (tag_stack', active_tag', newline', latex', present_cont') end; |
22124 | 278 |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
279 |
fun present_trailer ((_, tags), active_tag, _, latex, present_cont) = |
22124 | 280 |
if not (null tags) then err_bad_nesting " at end of theory" |
281 |
else |
|
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
282 |
latex |
22124 | 283 |
|> end_tag (active_tag, NONE) |
284 |
|> close_delim (fst present_cont) (active_tag, NONE) |
|
285 |
|> snd present_cont; |
|
286 |
||
287 |
end; |
|
288 |
||
289 |
||
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
290 |
(* present_thy *) |
22124 | 291 |
|
292 |
local |
|
293 |
||
61455 | 294 |
val markup_true = "\\isamarkuptrue%\n"; |
295 |
val markup_false = "\\isamarkupfalse%\n"; |
|
296 |
||
22124 | 297 |
val space_proper = |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
298 |
Scan.one Token.is_blank -- Scan.many Token.is_comment -- Scan.one Token.is_proper; |
22124 | 299 |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
300 |
val is_improper = not o (Token.is_proper orf Token.is_begin_ignore orf Token.is_end_ignore); |
22124 | 301 |
val improper = Scan.many is_improper; |
302 |
val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper)); |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
303 |
val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one Token.is_blank)); |
22124 | 304 |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
305 |
val opt_newline = Scan.option (Scan.one Token.is_newline); |
22124 | 306 |
|
307 |
val ignore = |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
308 |
Scan.depend (fn d => opt_newline |-- Scan.one Token.is_begin_ignore |
22124 | 309 |
>> pair (d + 1)) || |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36950
diff
changeset
|
310 |
Scan.depend (fn d => Scan.one Token.is_end_ignore --| |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43564
diff
changeset
|
311 |
(if d = 0 then Scan.fail_with (K (fn () => "Bad nesting of meta-comments")) else opt_newline) |
22124 | 312 |
>> pair (d - 1)); |
313 |
||
36950 | 314 |
val tag = (improper -- Parse.$$$ "%" -- improper) |-- Parse.!!! (Parse.tag_name --| blank_end); |
22124 | 315 |
|
316 |
val locale = |
|
36950 | 317 |
Scan.option ((Parse.$$$ "(" -- improper -- Parse.$$$ "in") |-- |
62969 | 318 |
Parse.!!! (improper |-- Parse.name --| (improper -- Parse.$$$ ")"))); |
22124 | 319 |
|
320 |
in |
|
321 |
||
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58923
diff
changeset
|
322 |
fun present_thy thy command_results toks = |
22124 | 323 |
let |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58923
diff
changeset
|
324 |
val keywords = Thy_Header.get_keywords thy; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58923
diff
changeset
|
325 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58923
diff
changeset
|
326 |
|
22124 | 327 |
(* tokens *) |
328 |
||
329 |
val ignored = Scan.state --| ignore |
|
67360 | 330 |
>> (fn d => (NONE, (Ignore_Token, ("", d)))); |
22124 | 331 |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
332 |
fun markup pred mk flag = Scan.peek (fn d => |
36950 | 333 |
improper |-- |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
334 |
Parse.position (Scan.one (fn tok => |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58992
diff
changeset
|
335 |
Token.is_command tok andalso pred keywords (Token.content_of tok))) -- |
22124 | 336 |
Scan.repeat tag -- |
51627
589daaf48dba
tuned signature -- agree with markup terminology;
wenzelm
parents:
51626
diff
changeset
|
337 |
Parse.!!!! ((improper -- locale -- improper) |-- Parse.document_source --| improper_end) |
59064 | 338 |
>> (fn (((tok, pos'), tags), source) => |
59065 | 339 |
let val name = Token.content_of tok |
340 |
in (SOME (name, pos', tags), (mk (name, source), (flag, d))) end)); |
|
22124 | 341 |
|
342 |
val command = Scan.peek (fn d => |
|
61814
1ca1142e1711
clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents:
61748
diff
changeset
|
343 |
Scan.optional (Scan.one Token.is_command_modifier ::: improper) [] -- |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
344 |
Scan.one Token.is_command -- Scan.repeat tag |
59939
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents:
59924
diff
changeset
|
345 |
>> (fn ((cmd_mod, cmd), tags) => |
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents:
59924
diff
changeset
|
346 |
map (fn tok => (NONE, (Basic_Token tok, ("", d)))) cmd_mod @ |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
347 |
[(SOME (Token.content_of cmd, Token.pos_of cmd, tags), |
61455 | 348 |
(Basic_Token cmd, (markup_false, d)))])); |
22124 | 349 |
|
350 |
val cmt = Scan.peek (fn d => |
|
61579 | 351 |
(Parse.$$$ "--" || Parse.$$$ Symbol.comment) |-- |
352 |
Parse.!!!! (improper |-- Parse.document_source) >> |
|
59065 | 353 |
(fn source => (NONE, (Markup_Token ("cmt", source), ("", d))))); |
22124 | 354 |
|
355 |
val other = Scan.peek (fn d => |
|
59065 | 356 |
Parse.not_eof >> (fn tok => (NONE, (Basic_Token tok, ("", d))))); |
22124 | 357 |
|
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
358 |
val tokens = |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
359 |
(ignored || |
61455 | 360 |
markup Keyword.is_document_heading Markup_Token markup_true || |
361 |
markup Keyword.is_document_body Markup_Env_Token markup_true || |
|
61456 | 362 |
markup Keyword.is_document_raw (Raw_Token o #2) "") >> single || |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
363 |
command || |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
364 |
(cmt || other) >> single; |
22124 | 365 |
|
366 |
||
367 |
(* spans *) |
|
368 |
||
59065 | 369 |
val is_eof = fn (_, (Basic_Token x, _)) => Token.is_eof x | _ => false; |
370 |
val stopper = Scan.stopper (K (NONE, (Basic_Token Token.eof, ("", 0)))) is_eof; |
|
22124 | 371 |
|
372 |
val cmd = Scan.one (is_some o fst); |
|
27732 | 373 |
val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2; |
22124 | 374 |
|
375 |
val comments = Scan.many (comment_token o fst o snd); |
|
376 |
val blank = Scan.one (blank_token o fst o snd); |
|
377 |
val newline = Scan.one (newline_token o fst o snd); |
|
378 |
val before_cmd = |
|
379 |
Scan.option (newline -- comments) -- |
|
380 |
Scan.option (newline -- comments) -- |
|
381 |
Scan.option (blank -- comments) -- cmd; |
|
382 |
||
383 |
val span = |
|
384 |
Scan.repeat non_cmd -- cmd -- |
|
385 |
Scan.repeat (Scan.unless before_cmd non_cmd) -- |
|
386 |
Scan.option (newline >> (single o snd)) |
|
387 |
>> (fn (((toks1, (cmd, tok2)), toks3), tok4) => |
|
388 |
make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4)))); |
|
389 |
||
57080
0e5fa27d3293
strip trailing white space, to avoid notorious problems of jEdit with last line;
wenzelm
parents:
56548
diff
changeset
|
390 |
val spans = toks |
0e5fa27d3293
strip trailing white space, to avoid notorious problems of jEdit with last line;
wenzelm
parents:
56548
diff
changeset
|
391 |
|> take_suffix Token.is_space |> #1 |
0e5fa27d3293
strip trailing white space, to avoid notorious problems of jEdit with last line;
wenzelm
parents:
56548
diff
changeset
|
392 |
|> Source.of_list |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59917
diff
changeset
|
393 |
|> Source.source' 0 Token.stopper (Scan.error (Scan.bulk tokens >> flat)) |
58864 | 394 |
|> Source.source stopper (Scan.error (Scan.bulk span)) |
22124 | 395 |
|> Source.exhaust; |
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
396 |
|
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
397 |
|
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
398 |
(* present commands *) |
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
399 |
|
67147 | 400 |
val document_tags = space_explode "," (Options.default_string \<^system_option>\<open>document_tags\<close>); |
67138 | 401 |
|
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
402 |
fun present_command tr span st st' = |
67191 | 403 |
Toplevel.setmp_thread_position tr |
67381 | 404 |
(present_span thy keywords document_tags span st st'); |
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
405 |
|
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
406 |
fun present _ [] = I |
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
407 |
| present st (((tr, st'), span) :: rest) = present_command tr span st st' #> present st' rest; |
22124 | 408 |
in |
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
409 |
if length command_results = length spans then |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
410 |
((NONE, []), NONE, true, [], (I, I)) |
28427
cc9f7d99fb73
turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents:
28273
diff
changeset
|
411 |
|> present Toplevel.toplevel (command_results ~~ spans) |
22124 | 412 |
|> present_trailer |
67194
1c0a6a957114
positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents:
67191
diff
changeset
|
413 |
|> rev |
22124 | 414 |
else error "Messed-up outer syntax for presentation" |
415 |
end; |
|
416 |
||
417 |
end; |
|
418 |
||
419 |
||
420 |
||
67386 | 421 |
(** standard output operations **) |
22124 | 422 |
|
423 |
(* basic pretty printing *) |
|
424 |
||
61435 | 425 |
fun perhaps_trim ctxt = |
67386 | 426 |
not (Config.get ctxt Document_Antiquotation.thy_output_display) ? Symbol.trim_blanks; |
22124 | 427 |
|
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
428 |
fun pretty_text ctxt = |
61435 | 429 |
Pretty.chunks o map Pretty.str o map (perhaps_trim ctxt) o split_lines; |
22124 | 430 |
|
26710
f79aa228c582
pretty_term: no revert_skolems here, but auto_fixes (token translations will do the rest);
wenzelm
parents:
26455
diff
changeset
|
431 |
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t; |
24920 | 432 |
|
32898
e871d897969c
term styles also cover antiquotations term_type and typeof
haftmann
parents:
32890
diff
changeset
|
433 |
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of; |
e871d897969c
term styles also cover antiquotations term_type and typeof
haftmann
parents:
32890
diff
changeset
|
434 |
|
22124 | 435 |
|
30390
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
436 |
(* default output *) |
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
437 |
|
61814
1ca1142e1711
clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents:
61748
diff
changeset
|
438 |
val str_of_source = space_implode " " o map Token.unparse o Token.args_of_src; |
22124 | 439 |
|
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
440 |
fun maybe_pretty_source pretty ctxt src xs = |
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
441 |
map (pretty ctxt) xs (*always pretty in order to exhibit errors!*) |
67386 | 442 |
|> (if Config.get ctxt Document_Antiquotation.thy_output_source |
443 |
then K [pretty_text ctxt (str_of_source src)] else I); |
|
30390
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
444 |
|
67386 | 445 |
fun string_of_margin ctxt = |
446 |
Pretty.string_of_margin (Config.get ctxt Document_Antiquotation.thy_output_margin); |
|
59175
bf465f335e85
system option "pretty_margin" is superseded by "thy_output_margin";
wenzelm
parents:
59067
diff
changeset
|
447 |
|
38767
d8da44a8dd25
proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents:
38766
diff
changeset
|
448 |
fun output ctxt prts = |
30390
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
449 |
prts |
67386 | 450 |
|> Config.get ctxt Document_Antiquotation.thy_output_quotes ? map Pretty.quote |
451 |
|> (if Config.get ctxt Document_Antiquotation.thy_output_display then |
|
452 |
map (Pretty.indent (Config.get ctxt Document_Antiquotation.thy_output_indent) |
|
453 |
#> string_of_margin ctxt #> Output.output) |
|
58716
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents:
58069
diff
changeset
|
454 |
#> space_implode "\\isasep\\isanewline%\n" |
61462 | 455 |
#> Latex.environment "isabelle" |
58716
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents:
58069
diff
changeset
|
456 |
else |
61877
276ad4354069
renamed Pretty.str_of to Pretty.unformatted_string_of to emphasize its meaning;
wenzelm
parents:
61865
diff
changeset
|
457 |
map |
67386 | 458 |
((if Config.get ctxt Document_Antiquotation.thy_output_break |
459 |
then string_of_margin ctxt else Pretty.unformatted_string_of) #> Output.output) |
|
58716
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents:
58069
diff
changeset
|
460 |
#> space_implode "\\isasep\\isanewline%\n" |
23a380cc45f4
official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents:
58069
diff
changeset
|
461 |
#> enclose "\\isa{" "}"); |
22124 | 462 |
|
30390
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
463 |
|
61619 | 464 |
(* verbatim text *) |
61473
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
wenzelm
parents:
61471
diff
changeset
|
465 |
|
61619 | 466 |
fun verbatim_text ctxt = |
67386 | 467 |
if Config.get ctxt Document_Antiquotation.thy_output_display then |
468 |
split_lines #> |
|
469 |
map (prefix (Symbol.spaces (Config.get ctxt Document_Antiquotation.thy_output_indent))) #> |
|
470 |
cat_lines #> |
|
61619 | 471 |
Latex.output_ascii #> Latex.environment "isabellett" |
472 |
else |
|
473 |
split_lines #> |
|
474 |
map (Latex.output_ascii #> enclose "\\isatt{" "}") #> |
|
475 |
space_implode "\\isasep\\isanewline%\n"; |
|
61491
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents:
61473
diff
changeset
|
476 |
|
30390
ad591ee76c78
simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents:
30381
diff
changeset
|
477 |
end; |