author | Christian Urban <urbanc@in.tum.de> |
Fri, 17 Oct 2008 05:02:04 -0400 | |
changeset 39 | 631d12c25bde |
parent 26 | 2311f81d7a22 |
child 41 | b11653b11bd3 |
permissions | -rw-r--r-- |
15
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
1 |
(* |
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
2 |
Auxiliary antiquotations for the Cookbook. |
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
3 |
|
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
4 |
*) |
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
5 |
|
26 | 6 |
structure AntiquoteSetup: sig end = |
7 |
struct |
|
8 |
||
9 |
val str_of_source = space_implode " " o map OuterLex.unparse o #2 o #1 o Args.dest_src; |
|
10 |
||
11 |
fun ml_val ys txt = "fn " ^ |
|
12 |
(case ys of [] => "_" | _ => enclose "(" ")" (commas ys)) ^ |
|
13 |
" => (" ^ txt ^ ");"; |
|
14 |
||
15 |
fun ml_val_open (ys, []) txt = ml_val ys txt |
|
16 |
| ml_val_open (ys, xs) txt = |
|
17 |
ml_val ys ("let open " ^ space_implode " " xs ^ " in " ^ txt ^ " end"); |
|
18 |
||
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
19 |
fun ml_pat (rhs, pat) = |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
20 |
let val pat' = implode (map (fn "\\<dots>" => "_" | s => s) |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
21 |
(Symbol.explode pat)) |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
22 |
in |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
23 |
"val " ^ pat' ^ " = " ^ rhs |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
24 |
end; |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
25 |
|
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
26 |
(* modified from original *) |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
27 |
fun ml_val txt = "fn _ => (" ^ txt ^ ");"; |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
28 |
fun ml_struct txt = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"; |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
29 |
fun ml_type txt = "val _ = NONE : (" ^ txt ^ ") option;"; |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
30 |
|
26 | 31 |
fun output_verbatim f src ctxt x = |
32 |
let val txt = f ctxt x |
|
33 |
in |
|
34 |
(if ! ThyOutput.source then str_of_source src else txt) |
|
35 |
|> (if ! ThyOutput.quotes then quote else I) |
|
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
36 |
|> ThyOutput.output (fn _ => fn s => Pretty.str s) src ctxt |
26 | 37 |
end; |
38 |
||
39 |
fun output_ml ml = output_verbatim |
|
40 |
(fn ctxt => fn ((txt, p), pos) => |
|
41 |
(ML_Context.eval_in (SOME ctxt) false pos (ml p txt); |
|
42 |
txt |> Source.of_string |> ML_Lex.source |> Source.exhaust |> |
|
43 |
Chunks.transform_cmts |> implode)); |
|
15
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
44 |
|
26 | 45 |
fun output_ml_response ml = output_verbatim |
46 |
(fn ctxt => fn ((x as (rhs, pat), p), pos) => |
|
47 |
(ML_Context.eval_in (SOME ctxt) false pos (ml p x); |
|
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
48 |
rhs ^ "\n" ^ |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
49 |
space_implode "\n" (map (fn s => "> " ^ s) |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
50 |
(space_explode "\n" pat)))); |
26 | 51 |
|
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
52 |
fun output_ml_checked ml src ctxt (txt, pos) = |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
53 |
(ML_Context.eval_in (SOME ctxt) false pos (ml txt); |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
54 |
(if ! ThyOutput.source then str_of_source src else SymbolPos.content (SymbolPos.explode (txt, pos))) |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
55 |
|> (if ! ThyOutput.quotes then quote else I) |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
56 |
|> ThyOutput.output (fn _ => fn s => Pretty.str s) src ctxt) |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
57 |
|
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
58 |
fun check_file_exists ctxt name = |
26 | 59 |
if File.exists (Path.append (Path.explode ("~~/src")) (Path.explode name)) then () |
60 |
else error ("Source file " ^ quote name ^ " does not exist.") |
|
61 |
||
62 |
val _ = ThyOutput.add_commands |
|
63 |
[("ML_open", ThyOutput.args (Scan.lift (OuterParse.position (Args.name -- |
|
64 |
(Scan.optional (Args.$$$ "for" |-- OuterParse.!!! (Scan.repeat1 Args.name)) [] -- |
|
65 |
Scan.optional (Args.parens (OuterParse.list1 Args.name)) [])))) (output_ml ml_val_open)), |
|
66 |
("ML_response", ThyOutput.args (Scan.lift (OuterParse.position |
|
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
67 |
((Args.name -- Args.name) >> rpair ()))) (output_ml_response (K ml_pat))), |
26 | 68 |
("ML_file", ThyOutput.args (Scan.lift Args.name) |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
69 |
(ThyOutput.output (fn _ => fn name => (check_file_exists name; Pretty.str name)))), |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
70 |
("ML_text", ThyOutput.args (Scan.lift Args.name) |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
71 |
(ThyOutput.output (fn _ => fn s => Pretty.str s))), |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
72 |
("ML", ThyOutput.args (Scan.lift Args.name_source_position) (output_ml_checked ml_val)), |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
73 |
("ML_struct", ThyOutput.args (Scan.lift Args.name_source_position) (output_ml_checked ml_struct)), |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
74 |
("ML_type", ThyOutput.args (Scan.lift Args.name_source_position) (output_ml_checked ml_type))]; |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
26
diff
changeset
|
75 |
|
26 | 76 |
end; |