CookBook/antiquote_setup_plus.ML
author berghofe
Mon, 13 Oct 2008 17:51:59 +0200
changeset 35 d5c090b9a2b1
parent 26 2311f81d7a22
child 39 631d12c25bde
permissions -rw-r--r--
Tuned.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
     6
structure AntiquoteSetup: sig end =
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
     7
struct
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
     8
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
     9
val str_of_source = space_implode " " o map OuterLex.unparse o #2 o #1 o Args.dest_src;
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    10
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    11
fun ml_val ys txt = "fn " ^
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    12
  (case ys of [] => "_" | _ => enclose "(" ")" (commas ys)) ^
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    13
  " => (" ^ txt ^ ");";
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    14
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    15
fun ml_val_open (ys, []) txt = ml_val ys txt
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    16
  | ml_val_open (ys, xs) txt =
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    17
      ml_val ys ("let open " ^ space_implode " " xs ^ " in " ^ txt ^ " end");
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    18
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    19
fun output_verbatim f src ctxt x =
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    20
  let val txt = f ctxt x
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    21
  in
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    22
    (if ! ThyOutput.source then str_of_source src else txt)
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    23
    |> (if ! ThyOutput.quotes then quote else I)
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    24
    |> (if ! ThyOutput.display then enclose "\\begin{alltt}\n" "\n\\end{alltt}"
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    25
    else
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    26
      split_lines
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    27
      #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    28
      #> space_implode "\\isasep\\\\%\n")
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    29
  end;
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    30
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    31
fun output_ml ml = output_verbatim
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    32
  (fn ctxt => fn ((txt, p), pos) =>
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    33
     (ML_Context.eval_in (SOME ctxt) false pos (ml p txt);
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    34
      txt |> Source.of_string |> ML_Lex.source |> Source.exhaust |>
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    35
      Chunks.transform_cmts |> implode));
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
26
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    37
fun transform_char "\\" = "{\\isacharbackslash}"
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    38
  | transform_char "$" = "{\\$}"
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    39
  | transform_char "{" = "{\\isacharbraceleft}"
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    40
  | transform_char "}" = "{\\isacharbraceright}"
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    41
  | transform_char x = x;
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    42
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    43
fun transform_symbol x =
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    44
  if Symbol.is_char x then transform_char x else Latex.output_symbols [x];
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    45
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    46
fun transform_str s = implode (map transform_symbol (Symbol.explode s));
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    47
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    48
fun ml_pat (rhs, pat) =
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    49
  let val pat' = implode (map (fn "\\<dots>" => "_" | s => s)
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    50
    (Symbol.explode pat))
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    51
  in
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    52
    "val " ^ pat' ^ " = " ^ rhs
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    53
  end;
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    54
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    55
fun output_ml_response ml = output_verbatim
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    56
  (fn ctxt => fn ((x as (rhs, pat), p), pos) =>
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    57
     (ML_Context.eval_in (SOME ctxt) false pos (ml p x);
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    58
      transform_str rhs ^ "\n\n" ^
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    59
      space_implode "\n" (map (enclose "\\textit{" "}")
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    60
        (space_explode "\n" (transform_str pat)))));
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    61
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    62
fun check_exists ctxt name =
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    63
  if File.exists (Path.append (Path.explode ("~~/src")) (Path.explode name)) then ()
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    64
  else error ("Source file " ^ quote name ^ " does not exist.")
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    65
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    66
val _ = ThyOutput.add_commands
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    67
  [("ML_open", ThyOutput.args (Scan.lift (OuterParse.position (Args.name --
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    68
      (Scan.optional (Args.$$$ "for" |-- OuterParse.!!! (Scan.repeat1 Args.name)) [] --
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    69
       Scan.optional (Args.parens (OuterParse.list1 Args.name)) [])))) (output_ml ml_val_open)),
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    70
   ("ML_response", ThyOutput.args (Scan.lift (OuterParse.position
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    71
        ((Args.name -- Args.name) >> rpair ())))
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    72
      (output_ml_response (K ml_pat))),
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    73
   ("ML_file", ThyOutput.args (Scan.lift Args.name)
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    74
      (ThyOutput.output (fn _ => fn name => (check_exists name; Pretty.str name))))];
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    75
2311f81d7a22 Added some more antiquotations.
berghofe
parents: 15
diff changeset
    76
end;