src/Pure/Tools/generated_files.ML
author wenzelm
Thu, 04 Apr 2019 15:26:18 +0200
changeset 70053 997f256c98e3
parent 70051 7a4dc1e60dc8
child 70054 a884b2967dd7
permissions -rw-r--r--
proper .exe path for export;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
69383
747f8b052e59 clarified modules;
wenzelm
parents: 69381
diff changeset
     1
(*  Title:      Pure/Tools/generated_files.ML
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
     3
69383
747f8b052e59 clarified modules;
wenzelm
parents: 69381
diff changeset
     4
Generated source files for other languages: with antiquotations, without Isabelle symbols.
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
     5
*)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
     6
69383
747f8b052e59 clarified modules;
wenzelm
parents: 69381
diff changeset
     7
signature GENERATED_FILES =
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
     8
sig
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
     9
  val add_files: Path.binding * string -> theory -> theory
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    10
  type file = {path: Path.T, pos: Position.T, content: string}
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    11
  val file_binding: file -> Path.binding
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    12
  val file_markup: file -> Markup.T
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    13
  val print_file: file -> string
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    14
  val report_file: Proof.context -> Position.T -> file -> unit
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    15
  val get_files: theory -> file list
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    16
  val get_file: theory -> Path.binding -> file
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    17
  val get_files_in: Path.binding list * theory -> (file * Position.T) list
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    18
  val check_files_in: Proof.context ->
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    19
    (string * Position.T) list * (string * Position.T) option -> Path.binding list * theory
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    20
  val write_file: Path.T -> file -> unit
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    21
  val export_file: theory -> file -> unit
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    22
  type file_type =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    23
    {name: string, ext: string, make_comment: string -> string, make_string: string -> string}
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    24
  val file_type:
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    25
    binding -> {ext: string, make_comment: string -> string, make_string: string -> string} ->
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    26
    theory -> theory
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    27
  val antiquotation: binding -> 'a Token.context_parser ->
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    28
    ({context: Proof.context, source: Token.src, file_type: file_type, argument: 'a} -> string) ->
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    29
    theory -> theory
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    30
  val set_string: string -> Proof.context -> Proof.context
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    31
  val generate_file: Path.binding * Input.source -> Proof.context -> local_theory
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    32
  val generate_file_cmd: (string * Position.T) * Input.source -> local_theory -> local_theory
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    33
  val export_generated_files: Proof.context -> (Path.binding list * theory) list -> unit
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    34
  val export_generated_files_cmd: Proof.context ->
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    35
    ((string * Position.T) list * (string * Position.T) option) list -> unit
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    36
  val with_compile_dir: (Path.T -> unit) -> unit
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    37
  val compile_generated_files: Proof.context -> (Path.binding list * theory) list ->
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    38
    Path.T list -> (Path.binding list * bool option) list -> Path.binding -> Input.source -> unit
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    39
  val compile_generated_files_cmd: Proof.context ->
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    40
    ((string * Position.T) list * (string * Position.T) option) list ->
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    41
    (string * Position.T) list -> ((string * Position.T) list * bool option) list ->
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    42
    string * Position.T -> Input.source -> unit
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    43
end;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    44
69383
747f8b052e59 clarified modules;
wenzelm
parents: 69381
diff changeset
    45
structure Generated_Files: GENERATED_FILES =
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    46
struct
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    47
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    48
(** context data **)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    49
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    50
type file_type =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    51
  {name: string, ext: string, make_comment: string -> string, make_string: string -> string};
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    52
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    53
type antiquotation = Token.src -> Proof.context -> file_type -> string;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    54
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    55
structure Data = Theory_Data
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    56
(
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    57
  type T =
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    58
    (Path.T * (Position.T * string)) list *  (*files for current theory*)
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    59
    file_type Name_Space.table *  (*file types*)
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    60
    antiquotation Name_Space.table;  (*antiquotations*)
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    61
  val empty =
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    62
    ([],
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    63
     Name_Space.empty_table Markup.file_typeN,
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    64
     Name_Space.empty_table Markup.antiquotationN);
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    65
  val extend = @{apply 3(1)} (K []);
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    66
  fun merge ((_, types1, antiqs1), (_, types2, antiqs2)) =
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    67
    ([],
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    68
     Name_Space.merge_tables (types1, types2),
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    69
     Name_Space.merge_tables (antiqs1, antiqs2));
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    70
);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    71
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    72
fun add_files (binding, content) =
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    73
  let val (path, pos) = Path.dest_binding binding in
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    74
    (Data.map o @{apply 3(1)}) (fn files =>
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    75
      (case AList.lookup (op =) files path of
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    76
        SOME (pos', _) =>
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    77
          error ("Duplicate generated file: " ^ Path.print path ^ Position.here_list [pos, pos'])
70012
36aeb535a801 tuned signature;
wenzelm
parents: 69784
diff changeset
    78
      | NONE => (path, (pos, content)) :: files))
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    79
  end;
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    80
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    81
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    82
(* get files *)
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    83
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    84
type file = {path: Path.T, pos: Position.T, content: string};
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    85
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    86
fun file_binding (file: file) = Path.binding (#path file, #pos file);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    87
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    88
fun file_markup (file: file) = (Markup.entityN, Position.def_properties_of (#pos file));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    89
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    90
fun print_file (file: file) = Markup.markup (file_markup file) (quote (Path.implode (#path file)));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    91
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    92
fun report_file ctxt pos file = Context_Position.report ctxt pos (file_markup file);
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    93
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    94
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    95
fun get_files thy =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    96
  Data.get thy |> #1 |> rev |> map (fn (path, (pos, content)) =>
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    97
    {path = path, pos = pos, content = content}: file);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    98
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    99
fun get_file thy binding =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   100
  let val (path, pos) = Path.dest_binding binding in
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   101
    (case find_first (fn file => #path file = path) (get_files thy) of
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   102
      SOME file => file
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   103
    | NONE =>
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   104
        error ("Missing generated file " ^ Path.print path ^
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   105
          " in theory " ^ quote (Context.theory_long_name thy) ^ Position.here pos))
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   106
  end;
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   107
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   108
fun get_files_in ([], thy) = map (rpair Position.none) (get_files thy)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   109
  | get_files_in (files, thy) =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   110
      map (fn binding => (get_file thy binding, Path.pos_of_binding binding)) files;
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   111
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   112
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   113
(* check and output files *)
69628
a2fbfdc5e62d export generated files;
wenzelm
parents: 69385
diff changeset
   114
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   115
fun check_files_in ctxt (files, opt_thy) =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   116
  let
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   117
    val thy =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   118
      (case opt_thy of
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   119
        SOME name => Theory.check {long = false} ctxt name
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   120
      | NONE => Proof_Context.theory_of ctxt);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   121
  in (map Path.explode_binding files, thy) end;
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   122
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   123
fun write_file dir (file: file) =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   124
  let
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   125
    val path = Path.expand (Path.append dir (#path file));
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   126
    val _ = Isabelle_System.mkdirs (Path.dir path);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   127
    val _ = File.generate path (#content file);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   128
  in () end;
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   129
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   130
fun export_file thy (file: file) =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   131
  Export.export thy (file_binding file) [#content file];
70013
6de8b7a5cd44 tuned signature -- more operations;
wenzelm
parents: 70012
diff changeset
   132
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   133
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   134
(* file types *)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   135
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   136
fun get_file_type thy ext =
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   137
  if ext = "" then error "Bad file extension"
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   138
  else
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
   139
    (#2 (Data.get thy), NONE) |-> Name_Space.fold_table
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   140
      (fn (_, file_type) => fn res =>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   141
        if #ext file_type = ext then SOME file_type else res)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   142
    |> (fn SOME file_type => file_type
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   143
         | NONE => error ("Unknown file type for extension " ^ quote ext));
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   144
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   145
fun file_type binding {ext, make_comment, make_string} thy =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   146
  let
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   147
    val name = Binding.name_of binding;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   148
    val pos = Binding.pos_of binding;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   149
    val file_type = {name = name, ext = ext, make_comment = make_comment, make_string = make_string};
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   150
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
   151
    val table = #2 (Data.get thy);
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   152
    val space = Name_Space.space_of_table table;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   153
    val context = Context.Theory thy |> Name_Space.map_naming (K Name_Space.global_naming);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   154
    val (_, data') = table |> Name_Space.define context true (Binding.make (name, pos), file_type);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   155
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   156
    val _ =
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   157
      (case try (#name o get_file_type thy) ext of
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   158
        NONE => ()
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   159
      | SOME name' =>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   160
          error ("Extension " ^ quote ext ^ " already registered for file type " ^
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   161
            quote (Markup.markup (Name_Space.markup space name') name') ^ Position.here pos));
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
   162
  in thy |> (Data.map o @{apply 3(2)}) (K data') end;
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   163
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   164
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   165
(* antiquotations *)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   166
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
   167
val get_antiquotations = #3 o Data.get o Proof_Context.theory_of;
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   168
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   169
fun antiquotation name scan body thy =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   170
  let
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   171
    fun ant src ctxt file_type : string =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   172
      let val (x, ctxt') = Token.syntax scan src ctxt
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   173
      in body {context = ctxt', source = src, file_type = file_type, argument = x} end;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   174
  in
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   175
    thy
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
   176
    |> (Data.map o @{apply 3(3)}) (Name_Space.define (Context.Theory thy) true (name, ant) #> #2)
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   177
  end;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   178
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   179
val scan_antiq =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   180
  Antiquote.scan_control >> Antiquote.Control ||
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   181
  Scan.one (Symbol.not_eof o Symbol_Pos.symbol) >> (Antiquote.Text o Symbol_Pos.symbol);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   182
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   183
fun read_source ctxt (file_type: file_type) source =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   184
  let
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   185
    val _ =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   186
      Context_Position.report ctxt (Input.pos_of source)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   187
        (Markup.language
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   188
          {name = #name file_type, symbols = false, antiquotes = true,
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   189
            delimited = Input.is_delimited source});
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   190
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   191
    val (input, _) =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   192
      Input.source_explode source
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   193
      |> Scan.error (Scan.finite Symbol_Pos.stopper (Scan.repeat scan_antiq));
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   194
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   195
    val _ = Context_Position.reports ctxt (Antiquote.antiq_reports input);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   196
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   197
    fun expand antiq =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   198
      (case antiq of
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   199
        Antiquote.Text s => s
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   200
      | Antiquote.Control {name, body, ...} =>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   201
          let
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   202
            val src = Token.make_src name (if null body then [] else [Token.read_cartouche body]);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   203
            val (src', ant) = Token.check_src ctxt get_antiquotations src;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   204
          in ant src' ctxt file_type end
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   205
      | Antiquote.Antiq antiq => error ("Bad antiquotation " ^ Position.here (#1 (#range antiq))));
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   206
  in implode (map expand input) end;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   207
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   208
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   209
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   210
(** Isar commands **)
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   211
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   212
(* generate_file *)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   213
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   214
fun generate_file (binding, source) lthy =
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   215
  let
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   216
    val thy = Proof_Context.theory_of lthy;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   217
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   218
    val (path, pos) = Path.dest_binding binding;
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   219
    val file_type =
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   220
      get_file_type thy (#2 (Path.split_ext path))
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   221
        handle ERROR msg => error (msg ^ Position.here pos);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   222
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   223
    val header = #make_comment file_type " generated by Isabelle ";
70012
36aeb535a801 tuned signature;
wenzelm
parents: 69784
diff changeset
   224
    val content = header ^ "\n" ^ read_source lthy file_type source;
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   225
  in lthy |> (Local_Theory.background_theory o add_files) (binding, content) end;
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   226
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   227
fun generate_file_cmd (file, source) lthy =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   228
  generate_file (Path.explode_binding file, source) lthy;
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   229
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   230
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   231
(* export_generated_files *)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   232
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   233
fun export_generated_files ctxt args =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   234
  let val thy = Proof_Context.theory_of ctxt in
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   235
    (case map #1 (maps get_files_in args) of
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   236
      [] => ()
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   237
    | files =>
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   238
       (List.app (export_file thy) files;
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   239
        writeln (Export.message thy Path.current);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   240
        writeln (cat_lines (map (prefix "  " o print_file) files))))
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   241
  end;
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   242
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   243
fun export_generated_files_cmd ctxt args =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   244
  export_generated_files ctxt (map (check_files_in ctxt) args);
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   245
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   246
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   247
(* compile_generated_files *)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   248
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   249
val compile_dir = Config.declare_string ("compile_dir", \<^here>) (K "");
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   250
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   251
fun with_compile_dir body : unit =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   252
  body (Path.explode (Config.get (Context.the_local_context ()) compile_dir));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   253
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   254
fun compile_generated_files ctxt args external export export_prefix source =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   255
  Isabelle_System.with_tmp_dir "compile" (fn dir =>
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   256
    let
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   257
      val thy = Proof_Context.theory_of ctxt;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   258
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   259
      val files = maps get_files_in args;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   260
      val _ = List.app (fn (file, pos) => report_file ctxt pos file) files;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   261
      val _ = List.app (write_file dir o #1) files;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   262
      val _ = external |> List.app (fn file => Isabelle_System.copy_file file dir);
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   263
      val _ =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   264
        ML_Context.eval_in (SOME (Config.put compile_dir (Path.implode dir) ctxt))
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   265
          ML_Compiler.flags (Input.pos_of source)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   266
          (ML_Lex.read "Generated_Files.with_compile_dir (" @
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   267
            ML_Lex.read_source source @ ML_Lex.read ")");
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   268
      val _ =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   269
        export |> List.app (fn (bindings, executable) =>
70053
997f256c98e3 proper .exe path for export;
wenzelm
parents: 70051
diff changeset
   270
          bindings |> List.app (fn binding0 =>
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   271
            let
70053
997f256c98e3 proper .exe path for export;
wenzelm
parents: 70051
diff changeset
   272
              val binding = binding0 |> Path.map_binding (executable = SOME true ? Path.exe);
997f256c98e3 proper .exe path for export;
wenzelm
parents: 70051
diff changeset
   273
              val (path, pos) = Path.dest_binding binding;
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   274
              val content =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   275
                (case try File.read (Path.append dir path) of
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   276
                  SOME context => context
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   277
                | NONE => error ("Missing result file " ^ Path.print path ^ Position.here pos));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   278
              val _ = Export.report_export thy export_prefix;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   279
              val binding' =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   280
                Path.map_binding (Path.append (Path.path_of_binding export_prefix)) binding;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   281
            in
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   282
              (if is_some executable then Export.export_executable else Export.export)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   283
                thy binding' [content]
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   284
            end));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   285
      val _ =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   286
        if null export then ()
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   287
        else writeln (Export.message thy (Path.path_of_binding export_prefix));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   288
    in () end);
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   289
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   290
fun compile_generated_files_cmd ctxt args external export export_prefix source =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   291
  compile_generated_files ctxt
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   292
    (map (check_files_in ctxt) args)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   293
    (map (Resources.check_file ctxt NONE) external)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   294
    ((map o apfst o map) Path.explode_binding export)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   295
    (Path.explode_binding export_prefix)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   296
    source;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   297
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   298
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   299
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   300
(** concrete file types **)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   301
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   302
val _ =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   303
  Theory.setup
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   304
    (file_type \<^binding>\<open>Haskell\<close>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   305
      {ext = "hs",
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   306
       make_comment = enclose "{-" "-}",
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   307
       make_string = GHC.print_string});
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   308
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   309
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   310
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   311
(** concrete antiquotations **)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   312
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   313
(* ML expression as string literal *)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   314
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   315
structure Local_Data = Proof_Data
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   316
(
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   317
  type T = string option;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   318
  fun init _ = NONE;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   319
);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   320
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   321
val set_string = Local_Data.put o SOME;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   322
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   323
fun the_string ctxt =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   324
  (case Local_Data.get ctxt of
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   325
    SOME s => s
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   326
  | NONE => raise Fail "No result string");
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   327
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   328
val _ =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   329
  Theory.setup
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   330
    (antiquotation \<^binding>\<open>cartouche\<close> (Scan.lift Args.cartouche_input)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   331
      (fn {context = ctxt, file_type, argument, ...} =>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   332
        ctxt |> Context.proof_map
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   333
          (ML_Context.expression (Input.pos_of argument)
69383
747f8b052e59 clarified modules;
wenzelm
parents: 69381
diff changeset
   334
            (ML_Lex.read "Theory.local_setup (Generated_Files.set_string (" @
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   335
             ML_Lex.read_source argument @ ML_Lex.read "))"))
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   336
        |> the_string |> #make_string file_type));
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   337
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   338
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   339
(* file-system paths *)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   340
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   341
fun path_antiquotation binding check =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   342
  antiquotation binding
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   343
    (Args.context -- Scan.lift (Parse.position Parse.path) >>
70049
c1226e4c273e tuned signature;
wenzelm
parents: 70047
diff changeset
   344
      (fn (ctxt, (name, pos)) => (check ctxt (SOME Path.current) (name, pos) |> Path.implode)))
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   345
    (fn {file_type, argument, ...} => #make_string file_type argument);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   346
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   347
val _ =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   348
  Theory.setup
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   349
   (path_antiquotation \<^binding>\<open>path\<close> Resources.check_path #>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   350
    path_antiquotation \<^binding>\<open>file\<close> Resources.check_file #>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   351
    path_antiquotation \<^binding>\<open>dir\<close> Resources.check_dir);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   352
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   353
end;