author | wenzelm |
Fri, 19 Jan 2018 19:09:25 +0100 | |
changeset 67474 | 90def2b06305 |
parent 67472 | 7ed8d4cdfb13 |
child 67571 | f858fe5531ac |
permissions | -rw-r--r-- |
67386 | 1 |
(* Title: Pure/Thy/document_antiquotation.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Document antiquotations. |
|
5 |
*) |
|
6 |
||
7 |
signature DOCUMENT_ANTIQUOTATION = |
|
8 |
sig |
|
9 |
val thy_output_display: bool Config.T |
|
10 |
val thy_output_quotes: bool Config.T |
|
11 |
val thy_output_margin: int Config.T |
|
12 |
val thy_output_indent: int Config.T |
|
13 |
val thy_output_source: bool Config.T |
|
14 |
val thy_output_break: bool Config.T |
|
15 |
val thy_output_modes: string Config.T |
|
67463 | 16 |
val trim_blanks: Proof.context -> string -> string |
17 |
val trim_lines: Proof.context -> string -> string |
|
18 |
val indent_lines: Proof.context -> string -> string |
|
67474
90def2b06305
more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents:
67472
diff
changeset
|
19 |
val prepare_lines: Proof.context -> string -> string |
67463 | 20 |
val quote: Proof.context -> Pretty.T -> Pretty.T |
21 |
val indent: Proof.context -> Pretty.T -> Pretty.T |
|
22 |
val format: Proof.context -> Pretty.T -> string |
|
23 |
val output: Proof.context -> Pretty.T -> Output.output |
|
67386 | 24 |
val print_antiquotations: bool -> Proof.context -> unit |
25 |
val check: Proof.context -> xstring * Position.T -> string |
|
26 |
val check_option: Proof.context -> xstring * Position.T -> string |
|
27 |
val setup: binding -> 'a context_parser -> |
|
67463 | 28 |
({context: Proof.context, source: Token.src, argument: 'a} -> Latex.text) -> theory -> theory |
67386 | 29 |
val setup_option: binding -> (string -> Proof.context -> Proof.context) -> theory -> theory |
30 |
val boolean: string -> bool |
|
31 |
val integer: string -> int |
|
67463 | 32 |
val evaluate: Proof.context -> Antiquote.text_antiquote -> Latex.text list |
67386 | 33 |
end; |
34 |
||
35 |
structure Document_Antiquotation: DOCUMENT_ANTIQUOTATION = |
|
36 |
struct |
|
37 |
||
38 |
(* options *) |
|
39 |
||
40 |
val thy_output_display = Attrib.setup_option_bool ("thy_output_display", \<^here>); |
|
41 |
val thy_output_break = Attrib.setup_option_bool ("thy_output_break", \<^here>); |
|
42 |
val thy_output_quotes = Attrib.setup_option_bool ("thy_output_quotes", \<^here>); |
|
43 |
val thy_output_margin = Attrib.setup_option_int ("thy_output_margin", \<^here>); |
|
44 |
val thy_output_indent = Attrib.setup_option_int ("thy_output_indent", \<^here>); |
|
45 |
val thy_output_source = Attrib.setup_option_bool ("thy_output_source", \<^here>); |
|
46 |
val thy_output_modes = Attrib.setup_option_string ("thy_output_modes", \<^here>); |
|
47 |
||
48 |
||
67463 | 49 |
(* blanks *) |
50 |
||
51 |
fun trim_blanks ctxt = |
|
52 |
not (Config.get ctxt thy_output_display) ? Symbol.trim_blanks; |
|
53 |
||
54 |
fun trim_lines ctxt = |
|
55 |
if not (Config.get ctxt thy_output_display) then |
|
56 |
split_lines #> map Symbol.trim_blanks #> space_implode " " |
|
57 |
else I; |
|
58 |
||
59 |
fun indent_lines ctxt = |
|
60 |
if Config.get ctxt thy_output_display then |
|
61 |
prefix_lines (Symbol.spaces (Config.get ctxt thy_output_indent)) |
|
62 |
else I; |
|
63 |
||
67474
90def2b06305
more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents:
67472
diff
changeset
|
64 |
fun prepare_lines ctxt = trim_lines ctxt #> indent_lines ctxt; |
90def2b06305
more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents:
67472
diff
changeset
|
65 |
|
67463 | 66 |
|
67 |
(* output *) |
|
68 |
||
69 |
fun quote ctxt = |
|
70 |
Config.get ctxt thy_output_quotes ? Pretty.quote; |
|
71 |
||
72 |
fun indent ctxt = |
|
73 |
Config.get ctxt thy_output_display ? Pretty.indent (Config.get ctxt thy_output_indent); |
|
74 |
||
75 |
fun format ctxt = |
|
76 |
if Config.get ctxt thy_output_display orelse Config.get ctxt thy_output_break |
|
77 |
then Pretty.string_of_margin (Config.get ctxt thy_output_margin) |
|
78 |
else Pretty.unformatted_string_of; |
|
79 |
||
80 |
fun output ctxt = quote ctxt #> indent ctxt #> format ctxt #> Output.output; |
|
81 |
||
82 |
||
67386 | 83 |
(* theory data *) |
84 |
||
85 |
structure Data = Theory_Data |
|
86 |
( |
|
87 |
type T = |
|
67463 | 88 |
(Token.src -> Proof.context -> Latex.text) Name_Space.table * |
67386 | 89 |
(string -> Proof.context -> Proof.context) Name_Space.table; |
90 |
val empty : T = |
|
91 |
(Name_Space.empty_table Markup.document_antiquotationN, |
|
92 |
Name_Space.empty_table Markup.document_antiquotation_optionN); |
|
93 |
val extend = I; |
|
94 |
fun merge ((commands1, options1), (commands2, options2)) : T = |
|
95 |
(Name_Space.merge_tables (commands1, commands2), |
|
96 |
Name_Space.merge_tables (options1, options2)); |
|
97 |
); |
|
98 |
||
99 |
val get_antiquotations = Data.get o Proof_Context.theory_of; |
|
100 |
||
101 |
fun print_antiquotations verbose ctxt = |
|
102 |
let |
|
103 |
val (commands, options) = get_antiquotations ctxt; |
|
104 |
val command_names = map #1 (Name_Space.markup_table verbose ctxt commands); |
|
105 |
val option_names = map #1 (Name_Space.markup_table verbose ctxt options); |
|
106 |
in |
|
107 |
[Pretty.big_list "document antiquotations:" (map Pretty.mark_str command_names), |
|
108 |
Pretty.big_list "document antiquotation options:" (map Pretty.mark_str option_names)] |
|
109 |
end |> Pretty.writeln_chunks; |
|
110 |
||
111 |
fun check ctxt = #1 o Name_Space.check (Context.Proof ctxt) (#1 (get_antiquotations ctxt)); |
|
112 |
fun check_option ctxt = #1 o Name_Space.check (Context.Proof ctxt) (#2 (get_antiquotations ctxt)); |
|
113 |
||
114 |
fun setup name scan body thy = |
|
115 |
let |
|
116 |
fun cmd src ctxt = |
|
117 |
let val (x, ctxt') = Token.syntax scan src ctxt |
|
67463 | 118 |
in body {context = ctxt', source = src, argument = x} end; |
67386 | 119 |
in thy |> Data.map (apfst (Name_Space.define (Context.Theory thy) true (name, cmd) #> #2)) end; |
120 |
||
121 |
fun setup_option name opt thy = thy |
|
122 |
|> Data.map (apsnd (Name_Space.define (Context.Theory thy) true (name, opt) #> #2)); |
|
123 |
||
124 |
||
125 |
(* syntax *) |
|
126 |
||
127 |
local |
|
128 |
||
129 |
val property = |
|
130 |
Parse.position Parse.name -- Scan.optional (Parse.$$$ "=" |-- Parse.!!! Parse.name) ""; |
|
131 |
||
132 |
val properties = |
|
133 |
Scan.optional (Parse.$$$ "[" |-- Parse.!!! (Parse.enum "," property --| Parse.$$$ "]")) []; |
|
134 |
||
135 |
in |
|
136 |
||
67466 | 137 |
val parse_antiq = |
67386 | 138 |
Parse.!!! |
139 |
(Parse.token Parse.liberal_name -- properties -- Parse.args --| Scan.ahead Parse.eof) |
|
140 |
>> (fn ((name, props), args) => (props, name :: args)); |
|
141 |
||
142 |
end; |
|
143 |
||
144 |
||
145 |
(* evaluate *) |
|
146 |
||
147 |
local |
|
148 |
||
149 |
fun command src ctxt = |
|
150 |
let val (src', f) = Token.check_src ctxt (#1 o get_antiquotations) src |
|
151 |
in f src' ctxt end; |
|
152 |
||
153 |
fun option ((xname, pos), s) ctxt = |
|
154 |
let |
|
155 |
val (_, opt) = |
|
156 |
Name_Space.check (Context.Proof ctxt) (#2 (get_antiquotations ctxt)) (xname, pos); |
|
157 |
in opt s ctxt end; |
|
158 |
||
159 |
fun eval ctxt (opts, src) = |
|
160 |
let |
|
161 |
val preview_ctxt = fold option opts ctxt; |
|
67472
7ed8d4cdfb13
recovered antiquotation check without latex mode (cf. dfc93f2b01ea);
wenzelm
parents:
67466
diff
changeset
|
162 |
val _ = command src preview_ctxt; |
7ed8d4cdfb13
recovered antiquotation check without latex mode (cf. dfc93f2b01ea);
wenzelm
parents:
67466
diff
changeset
|
163 |
|
67386 | 164 |
val print_ctxt = Context_Position.set_visible false preview_ctxt; |
165 |
val print_modes = space_explode "," (Config.get print_ctxt thy_output_modes) @ [Latex.latexN]; |
|
67463 | 166 |
in [Print_Mode.with_modes print_modes (fn () => command src print_ctxt) ()] end; |
67386 | 167 |
|
168 |
in |
|
169 |
||
67466 | 170 |
fun evaluate ctxt antiq = |
171 |
(case antiq of |
|
172 |
Antiquote.Text ss => [Latex.symbols ss] |
|
173 |
| Antiquote.Control {name, body, ...} => |
|
67386 | 174 |
eval ctxt ([], Token.make_src name (if null body then [] else [Token.read_cartouche body])) |
67466 | 175 |
| Antiquote.Antiq {range = (pos, _), body, ...} => |
67386 | 176 |
let val keywords = Thy_Header.get_keywords' ctxt; |
67466 | 177 |
in eval ctxt (Token.read_antiq keywords parse_antiq (body, pos)) end); |
67386 | 178 |
|
179 |
end; |
|
180 |
||
181 |
||
182 |
(* option syntax *) |
|
183 |
||
184 |
fun boolean "" = true |
|
185 |
| boolean "true" = true |
|
186 |
| boolean "false" = false |
|
67463 | 187 |
| boolean s = error ("Bad boolean value: " ^ Library.quote s); |
67386 | 188 |
|
189 |
fun integer s = |
|
190 |
let |
|
191 |
fun int ss = |
|
192 |
(case Library.read_int ss of (i, []) => i |
|
67463 | 193 |
| _ => error ("Bad integer value: " ^ Library.quote s)); |
67386 | 194 |
in (case Symbol.explode s of "-" :: ss => ~ (int ss) | ss => int ss) end; |
195 |
||
196 |
val _ = Theory.setup |
|
197 |
(setup_option \<^binding>\<open>show_types\<close> (Config.put show_types o boolean) #> |
|
198 |
setup_option \<^binding>\<open>show_sorts\<close> (Config.put show_sorts o boolean) #> |
|
199 |
setup_option \<^binding>\<open>show_structs\<close> (Config.put show_structs o boolean) #> |
|
200 |
setup_option \<^binding>\<open>show_question_marks\<close> (Config.put show_question_marks o boolean) #> |
|
201 |
setup_option \<^binding>\<open>show_abbrevs\<close> (Config.put show_abbrevs o boolean) #> |
|
202 |
setup_option \<^binding>\<open>names_long\<close> (Config.put Name_Space.names_long o boolean) #> |
|
203 |
setup_option \<^binding>\<open>names_short\<close> (Config.put Name_Space.names_short o boolean) #> |
|
204 |
setup_option \<^binding>\<open>names_unique\<close> (Config.put Name_Space.names_unique o boolean) #> |
|
205 |
setup_option \<^binding>\<open>eta_contract\<close> (Config.put Syntax_Trans.eta_contract o boolean) #> |
|
206 |
setup_option \<^binding>\<open>display\<close> (Config.put thy_output_display o boolean) #> |
|
207 |
setup_option \<^binding>\<open>break\<close> (Config.put thy_output_break o boolean) #> |
|
208 |
setup_option \<^binding>\<open>quotes\<close> (Config.put thy_output_quotes o boolean) #> |
|
67460
dfc93f2b01ea
discontinued unused wrapper: print_mode is provided directly;
wenzelm
parents:
67386
diff
changeset
|
209 |
setup_option \<^binding>\<open>mode\<close> (Config.put thy_output_modes) #> |
67386 | 210 |
setup_option \<^binding>\<open>margin\<close> (Config.put thy_output_margin o integer) #> |
211 |
setup_option \<^binding>\<open>indent\<close> (Config.put thy_output_indent o integer) #> |
|
212 |
setup_option \<^binding>\<open>source\<close> (Config.put thy_output_source o boolean) #> |
|
213 |
setup_option \<^binding>\<open>goals_limit\<close> (Config.put Goal_Display.goals_limit o integer)); |
|
214 |
||
215 |
end; |