author | Norbert Schirmer <norbert.schirmer@web.de> |
Wed, 22 May 2019 13:24:30 +0200 | |
changeset 575 | c3dbc04471a9 |
parent 256 | 1fb8d62c88a0 |
permissions | -rw-r--r-- |
90
b071a0b88298
made chunks aware of the gray-option
Christian Urban <urbanc@in.tum.de>
parents:
57
diff
changeset
|
1 |
|
24 | 2 |
structure Chunks = |
3 |
struct |
|
4 |
||
5 |
(** theory data **) |
|
6 |
||
7 |
structure ChunkData = TheoryDataFun |
|
8 |
( |
|
9 |
type T = (ML_Lex.token list * stamp) NameSpace.table |
|
10 |
val empty = NameSpace.empty_table; |
|
11 |
val copy = I; |
|
12 |
val extend = I; |
|
13 |
fun merge _ tables : T = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUP dup => |
|
14 |
error ("Attempt to merge different versions of ML chunk " ^ quote dup); |
|
15 |
); |
|
16 |
||
17 |
fun declare_chunk name thy = |
|
55 | 18 |
(Sign.full_bname thy name, |
176
3da5f3f07d8b
updated to new read_specification
Christian Urban <urbanc@in.tum.de>
parents:
171
diff
changeset
|
19 |
ChunkData.map (snd o NameSpace.define (Sign.naming_of thy) |
55 | 20 |
(Binding.name name, ([], stamp ()))) thy |
24 | 21 |
handle Symtab.DUP _ => error ("Duplicate definition of ML chunk " ^ quote name)); |
22 |
||
23 |
fun augment_chunk tok name = |
|
24 |
ChunkData.map (apsnd (Symtab.map_entry name (apfst (cons tok)))); |
|
25 |
||
26 |
fun the_chunk thy name = |
|
27 |
let val (space, tab) = ChunkData.get thy |
|
28 |
in case Symtab.lookup tab (NameSpace.intern space name) of |
|
29 |
NONE => error ("Unknown ML chunk " ^ quote name) |
|
30 |
| SOME (toks, _) => rev toks |
|
31 |
end; |
|
32 |
||
33 |
||
34 |
(** parsers **) |
|
35 |
||
36 |
val not_eof = ML_Lex.stopper |> Scan.is_stopper #> not; |
|
37 |
||
38 |
val scan_space = Scan.many Symbol.is_blank; |
|
39 |
||
40 |
fun scan_cmt scan = |
|
41 |
Scan.one (ML_Lex.kind_of #> equal ML_Lex.Comment) >> |
|
42 |
(ML_Lex.content_of #> Symbol.explode #> Scan.finite Symbol.stopper scan #> fst); |
|
43 |
||
44 |
val scan_chunks = Scan.depend (fn (open_chunks, thy) => |
|
45 |
scan_cmt ((Scan.this_string "(*" -- scan_space -- Scan.this_string "@chunk" -- |
|
46 |
scan_space) |-- Symbol.scan_id --| |
|
47 |
(scan_space -- Scan.this_string "*)") >> (fn name => |
|
48 |
let val (name', thy') = declare_chunk name thy |
|
49 |
in ((name' :: open_chunks, thy'), "") end)) |
|
50 |
|| |
|
51 |
scan_cmt (Scan.this_string "(*" -- scan_space -- Scan.this_string "@end" -- |
|
52 |
scan_space -- Scan.this_string "*)" >> (fn _ => |
|
53 |
(case open_chunks of |
|
54 |
[] => error "end without matching chunk encountered" |
|
55 |
| _ :: open_chunks' => ((open_chunks', thy), "")))) |
|
56 |
|| |
|
57 |
Scan.one not_eof >> (fn tok => |
|
58 |
((open_chunks, fold (augment_chunk tok) open_chunks thy), ML_Lex.content_of tok))); |
|
59 |
||
60 |
fun chunks_of s thy = |
|
61 |
let val toks = Source.exhaust (ML_Lex.source (Source.of_string s)) |
|
62 |
in case Scan.finite' ML_Lex.stopper (Scan.repeat (Scan.error scan_chunks)) |
|
63 |
(([], thy), toks) of |
|
64 |
(toks', (([], thy'), _)) => (implode toks', thy') |
|
65 |
| (_, ((open_chunks, _), _)) => |
|
66 |
error ("Open chunks at end of text: " ^ commas_quote open_chunks) |
|
67 |
end; |
|
68 |
||
69 |
||
70 |
(** use command **) |
|
71 |
||
72 |
fun load_ml dir raw_path thy = |
|
73 |
(case ThyLoad.check_ml dir raw_path of |
|
74 |
NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path)) |
|
75 |
| SOME (path, _) => |
|
76 |
let val (s, thy') = chunks_of (File.read path) thy |
|
77 |
in |
|
78 |
Context.theory_map (ThyInfo.exec_file false raw_path) thy' |
|
79 |
end); |
|
80 |
||
81 |
fun use_chunks path thy = |
|
82 |
load_ml (ThyInfo.master_directory (Context.theory_name thy)) path thy; |
|
83 |
||
84 |
val _ = |
|
85 |
OuterSyntax.command "use_chunks" "eval ML text from file" |
|
86 |
(OuterKeyword.tag_ml OuterKeyword.thy_decl) |
|
87 |
(OuterParse.path >> (Toplevel.theory o use_chunks)); |
|
88 |
||
89 |
||
90 |
(** antiquotation **) |
|
91 |
||
92 |
val str_of_source = space_implode " " o map OuterLex.unparse o #2 o #1 o Args.dest_src; |
|
93 |
||
94 |
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|"; |
|
95 |
||
96 |
val transform_cmts = |
|
97 |
Scan.finite ML_Lex.stopper (Scan.repeat |
|
98 |
( scan_cmt |
|
99 |
(Scan.this_string "(*{" |-- |
|
100 |
Scan.repeat (Scan.unless (Scan.this_string "}*)") |
|
101 |
(Scan.one (not o Symbol.is_eof))) --| |
|
102 |
Scan.this_string "}*)" >> |
|
57
065f472c09ab
Repaired output of marginal comments in ML antiquotation.
berghofe
parents:
55
diff
changeset
|
103 |
(Symbol.encode_raw o enclose "\\hfill\\textrm{\\textit{" "}}" o implode)) |
24 | 104 |
|| Scan.one not_eof >> ML_Lex.content_of)) #> |
105 |
fst; |
|
106 |
||
165
890fbfef6d6b
partially adapted to new antiquotation infrastructure
Christian Urban <urbanc@in.tum.de>
parents:
118
diff
changeset
|
107 |
fun output_chunk {state: Toplevel.state, source = src, context = ctxt} name = |
24 | 108 |
let |
109 |
val toks = the_chunk (ProofContext.theory_of ctxt) name; |
|
110 |
val (spc, toks') = (case toks of |
|
111 |
[] => ("", []) |
|
112 |
| [tok] => ("", [tok]) |
|
113 |
| tok :: toks' => |
|
114 |
let |
|
115 |
val (toks'', tok') = split_last toks' |
|
116 |
val toks''' = |
|
117 |
if ML_Lex.kind_of tok' = ML_Lex.Space then toks'' |
|
118 |
else toks' |
|
119 |
in |
|
120 |
if ML_Lex.kind_of tok = ML_Lex.Space then |
|
121 |
(tok |> ML_Lex.content_of |> Symbol.explode |> |
|
122 |
take_suffix (equal " ") |> snd |> implode, |
|
123 |
toks''') |
|
124 |
else ("", tok :: toks''') |
|
125 |
end) |
|
126 |
val txt = spc ^ implode (transform_cmts toks') |
|
127 |
in |
|
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
255
diff
changeset
|
128 |
OutputTutorial.output (split_lines txt) |
24 | 129 |
end; |
130 |
||
165
890fbfef6d6b
partially adapted to new antiquotation infrastructure
Christian Urban <urbanc@in.tum.de>
parents:
118
diff
changeset
|
131 |
val _ = ThyOutput.antiquotation "ML_chunk" (Scan.lift Args.name) output_chunk; |
24 | 132 |
|
133 |
end; |