src/Pure/Tools/generated_files.ML
author wenzelm
Sat, 02 Nov 2019 12:02:27 +0100
changeset 70991 f9f7c34b7dd4
parent 70067 9b34dbeb1103
child 72046 c386d1b77762
permissions -rw-r--r--
more scalable protocol_message: use XML.body directly (Output.output hook is not required);
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
70054
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
    37
  val compile_generated_files: Proof.context ->
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
    38
    (Path.binding list * theory) list ->
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
    39
    (Path.T list * Path.T) list ->
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
    40
    (Path.binding list * bool option) list ->
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
    41
    Path.binding -> Input.source -> unit
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    42
  val compile_generated_files_cmd: Proof.context ->
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    43
    ((string * Position.T) list * (string * Position.T) option) list ->
70054
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
    44
    ((string * Position.T) list * (string * Position.T)) list ->
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
    45
    ((string * Position.T) list * bool option) list ->
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    46
    string * Position.T -> Input.source -> unit
70067
9b34dbeb1103 auxiliary operation for common uses of 'compile_generated_files';
wenzelm
parents: 70055
diff changeset
    47
  val execute: Path.T -> Input.source -> string -> string
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    48
end;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    49
69383
747f8b052e59 clarified modules;
wenzelm
parents: 69381
diff changeset
    50
structure Generated_Files: GENERATED_FILES =
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    51
struct
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
(** context data **)
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
type file_type =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    56
  {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
    57
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    58
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
    59
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    60
structure Data = Theory_Data
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    61
(
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    62
  type T =
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    63
    (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
    64
    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
    65
    antiquotation Name_Space.table;  (*antiquotations*)
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    66
  val empty =
69385
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.empty_table Markup.file_typeN,
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    69
     Name_Space.empty_table Markup.antiquotationN);
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    70
  val extend = @{apply 3(1)} (K []);
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    71
  fun merge ((_, types1, antiqs1), (_, types2, antiqs2)) =
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    72
    ([],
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    73
     Name_Space.merge_tables (types1, types2),
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    74
     Name_Space.merge_tables (antiqs1, antiqs2));
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    75
);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
    76
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    77
fun add_files (binding, content) =
70055
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70054
diff changeset
    78
  let
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70054
diff changeset
    79
    val _ = Path.proper_binding binding;
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70054
diff changeset
    80
    val (path, pos) = Path.dest_binding binding;
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70054
diff changeset
    81
  in
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    82
    (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
    83
      (case AList.lookup (op =) files path of
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    84
        SOME (pos', _) =>
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    85
          error ("Duplicate generated file: " ^ Path.print path ^ Position.here_list [pos, pos'])
70012
36aeb535a801 tuned signature;
wenzelm
parents: 69784
diff changeset
    86
      | NONE => (path, (pos, content)) :: files))
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    87
  end;
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
    88
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    89
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    90
(* get files *)
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    91
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    92
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
    93
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
    94
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
    95
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    96
fun file_markup (file: file) = (Markup.entityN, Position.def_properties_of (#pos file));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    97
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
    98
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
    99
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   100
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
   101
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
   102
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   103
fun get_files thy =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   104
  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
   105
    {path = path, pos = pos, content = content}: file);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   106
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   107
fun get_file thy binding =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   108
  let val (path, pos) = Path.dest_binding binding in
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   109
    (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
   110
      SOME file => file
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   111
    | NONE =>
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   112
        error ("Missing generated file " ^ Path.print path ^
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   113
          " 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
   114
  end;
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   115
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   116
fun get_files_in ([], thy) = map (rpair Position.none) (get_files thy)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   117
  | get_files_in (files, thy) =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   118
      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
   119
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   120
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   121
(* check and output files *)
69628
a2fbfdc5e62d export generated files;
wenzelm
parents: 69385
diff changeset
   122
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   123
fun check_files_in ctxt (files, opt_thy) =
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 thy =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   126
      (case opt_thy of
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   127
        SOME name => Theory.check {long = false} ctxt name
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   128
      | NONE => Proof_Context.theory_of ctxt);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   129
  in (map Path.explode_binding files, thy) end;
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   130
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   131
fun write_file dir (file: file) =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   132
  let
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   133
    val path = Path.expand (Path.append dir (#path file));
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   134
    val _ = Isabelle_System.mkdirs (Path.dir path);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   135
    val _ = File.generate path (#content file);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   136
  in () end;
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   137
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   138
fun export_file thy (file: file) =
70991
f9f7c34b7dd4 more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents: 70067
diff changeset
   139
  Export.export thy (file_binding file) [XML.Text (#content file)];
70013
6de8b7a5cd44 tuned signature -- more operations;
wenzelm
parents: 70012
diff changeset
   140
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   141
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   142
(* file types *)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   143
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   144
fun get_file_type thy ext =
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   145
  if ext = "" then error "Bad file extension"
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   146
  else
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
   147
    (#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
   148
      (fn (_, file_type) => fn res =>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   149
        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
   150
    |> (fn SOME file_type => file_type
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   151
         | 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
   152
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   153
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
   154
  let
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   155
    val name = Binding.name_of binding;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   156
    val pos = Binding.pos_of binding;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   157
    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
   158
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
   159
    val table = #2 (Data.get thy);
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   160
    val space = Name_Space.space_of_table table;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   161
    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
   162
    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
   163
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   164
    val _ =
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   165
      (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
   166
        NONE => ()
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   167
      | SOME name' =>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   168
          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
   169
            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
   170
  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
   171
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   172
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   173
(* antiquotations *)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   174
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
   175
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
   176
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   177
fun antiquotation name scan body thy =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   178
  let
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   179
    fun ant src ctxt file_type : string =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   180
      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
   181
      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
   182
  in
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   183
    thy
69385
be9f187dcd50 clarified signature: allow to add_files/get_files by other tools;
wenzelm
parents: 69383
diff changeset
   184
    |> (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
   185
  end;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   186
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   187
val scan_antiq =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   188
  Antiquote.scan_control >> Antiquote.Control ||
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   189
  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
   190
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   191
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
   192
  let
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   193
    val _ =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   194
      Context_Position.report ctxt (Input.pos_of source)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   195
        (Markup.language
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   196
          {name = #name file_type, symbols = false, antiquotes = true,
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   197
            delimited = Input.is_delimited source});
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   198
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   199
    val (input, _) =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   200
      Input.source_explode source
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   201
      |> 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
   202
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   203
    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
   204
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   205
    fun expand antiq =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   206
      (case antiq of
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   207
        Antiquote.Text s => s
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   208
      | Antiquote.Control {name, body, ...} =>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   209
          let
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   210
            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
   211
            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
   212
          in ant src' ctxt file_type end
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   213
      | 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
   214
  in implode (map expand input) end;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   215
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   216
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
(** Isar commands **)
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   219
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   220
(* generate_file *)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   221
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   222
fun generate_file (binding, source) lthy =
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   223
  let
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   224
    val thy = Proof_Context.theory_of lthy;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   225
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   226
    val (path, pos) = Path.dest_binding binding;
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   227
    val file_type =
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   228
      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
   229
        handle ERROR msg => error (msg ^ Position.here pos);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   230
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   231
    val header = #make_comment file_type " generated by Isabelle ";
70012
36aeb535a801 tuned signature;
wenzelm
parents: 69784
diff changeset
   232
    val content = header ^ "\n" ^ read_source lthy file_type source;
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   233
  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
   234
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   235
fun generate_file_cmd (file, source) lthy =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   236
  generate_file (Path.explode_binding file, source) lthy;
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   237
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   238
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   239
(* export_generated_files *)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   240
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   241
fun export_generated_files ctxt args =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   242
  let val thy = Proof_Context.theory_of ctxt in
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   243
    (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
   244
      [] => ()
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   245
    | files =>
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   246
       (List.app (export_file thy) files;
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   247
        writeln (Export.message thy Path.current);
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   248
        writeln (cat_lines (map (prefix "  " o print_file) files))))
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   249
  end;
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   250
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   251
fun export_generated_files_cmd ctxt args =
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   252
  export_generated_files ctxt (map (check_files_in ctxt) args);
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   253
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   254
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   255
(* compile_generated_files *)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   256
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   257
val compile_dir = Config.declare_string ("compile_dir", \<^here>) (K "");
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   258
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   259
fun with_compile_dir body : unit =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   260
  body (Path.explode (Config.get (Context.the_local_context ()) compile_dir));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   261
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   262
fun compile_generated_files ctxt args external export export_prefix source =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   263
  Isabelle_System.with_tmp_dir "compile" (fn dir =>
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   264
    let
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   265
      val thy = Proof_Context.theory_of ctxt;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   266
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   267
      val files = maps get_files_in args;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   268
      val _ = List.app (fn (file, pos) => report_file ctxt pos file) files;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   269
      val _ = List.app (write_file dir o #1) files;
70054
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
   270
      val _ =
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
   271
        external |> List.app (fn (files, base_dir) =>
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
   272
          files |> List.app (fn file => Isabelle_System.copy_file_base (base_dir, file) dir));
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   273
      val _ =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   274
        ML_Context.eval_in (SOME (Config.put compile_dir (Path.implode dir) ctxt))
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   275
          ML_Compiler.flags (Input.pos_of source)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   276
          (ML_Lex.read "Generated_Files.with_compile_dir (" @
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   277
            ML_Lex.read_source source @ ML_Lex.read ")");
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   278
      val _ =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   279
        export |> List.app (fn (bindings, executable) =>
70053
997f256c98e3 proper .exe path for export;
wenzelm
parents: 70051
diff changeset
   280
          bindings |> List.app (fn binding0 =>
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   281
            let
70053
997f256c98e3 proper .exe path for export;
wenzelm
parents: 70051
diff changeset
   282
              val binding = binding0 |> Path.map_binding (executable = SOME true ? Path.exe);
997f256c98e3 proper .exe path for export;
wenzelm
parents: 70051
diff changeset
   283
              val (path, pos) = Path.dest_binding binding;
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   284
              val content =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   285
                (case try File.read (Path.append dir path) of
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   286
                  SOME context => context
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   287
                | NONE => error ("Missing result file " ^ Path.print path ^ Position.here pos));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   288
              val _ = Export.report_export thy export_prefix;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   289
              val binding' =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   290
                Path.map_binding (Path.append (Path.path_of_binding export_prefix)) binding;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   291
            in
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   292
              (if is_some executable then Export.export_executable else Export.export)
70991
f9f7c34b7dd4 more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents: 70067
diff changeset
   293
                thy binding' [XML.Text content]
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   294
            end));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   295
      val _ =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   296
        if null export then ()
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   297
        else writeln (Export.message thy (Path.path_of_binding export_prefix));
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   298
    in () end);
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   299
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   300
fun compile_generated_files_cmd ctxt args external export export_prefix source =
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   301
  compile_generated_files ctxt
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   302
    (map (check_files_in ctxt) args)
70054
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
   303
    (external |> map (fn (raw_files, raw_base_dir) =>
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
   304
      let
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
   305
        val base_dir = Resources.check_dir ctxt NONE raw_base_dir;
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
   306
        fun check (s, pos) = (Resources.check_file ctxt (SOME base_dir) (s, pos); Path.explode s);
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
   307
        val files = map check raw_files;
a884b2967dd7 clarified export_files: Isabelle_System.copy_file_base preserves given directory sub-structure;
wenzelm
parents: 70053
diff changeset
   308
      in (files, base_dir) end))
70051
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   309
    ((map o apfst o map) Path.explode_binding export)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   310
    (Path.explode_binding export_prefix)
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   311
    source;
7a4dc1e60dc8 added command 'compile_generated_files';
wenzelm
parents: 70049
diff changeset
   312
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   313
70067
9b34dbeb1103 auxiliary operation for common uses of 'compile_generated_files';
wenzelm
parents: 70055
diff changeset
   314
(* execute compiler -- auxiliary *)
9b34dbeb1103 auxiliary operation for common uses of 'compile_generated_files';
wenzelm
parents: 70055
diff changeset
   315
9b34dbeb1103 auxiliary operation for common uses of 'compile_generated_files';
wenzelm
parents: 70055
diff changeset
   316
fun execute dir title compile =
9b34dbeb1103 auxiliary operation for common uses of 'compile_generated_files';
wenzelm
parents: 70055
diff changeset
   317
  Isabelle_System.bash_output_check ("cd " ^ File.bash_path dir ^ " && " ^ compile)
9b34dbeb1103 auxiliary operation for common uses of 'compile_generated_files';
wenzelm
parents: 70055
diff changeset
   318
    handle ERROR msg =>
9b34dbeb1103 auxiliary operation for common uses of 'compile_generated_files';
wenzelm
parents: 70055
diff changeset
   319
      let val (s, pos) = Input.source_content title
9b34dbeb1103 auxiliary operation for common uses of 'compile_generated_files';
wenzelm
parents: 70055
diff changeset
   320
      in error (s ^ " failed" ^ Position.here pos ^ ":\n" ^ msg) end;
9b34dbeb1103 auxiliary operation for common uses of 'compile_generated_files';
wenzelm
parents: 70055
diff changeset
   321
9b34dbeb1103 auxiliary operation for common uses of 'compile_generated_files';
wenzelm
parents: 70055
diff changeset
   322
70047
96fe857a7a6f clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents: 70015
diff changeset
   323
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   324
(** concrete file types **)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   325
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   326
val _ =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   327
  Theory.setup
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   328
    (file_type \<^binding>\<open>Haskell\<close>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   329
      {ext = "hs",
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   330
       make_comment = enclose "{-" "-}",
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   331
       make_string = GHC.print_string});
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   332
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   333
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   334
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   335
(** concrete antiquotations **)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   336
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   337
(* ML expression as string literal *)
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
structure Local_Data = Proof_Data
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
  type T = string option;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   342
  fun init _ = NONE;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   343
);
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   344
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   345
val set_string = Local_Data.put o SOME;
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
fun the_string ctxt =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   348
  (case Local_Data.get ctxt of
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   349
    SOME s => s
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   350
  | NONE => raise Fail "No result string");
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   351
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   352
val _ =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   353
  Theory.setup
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   354
    (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
   355
      (fn {context = ctxt, file_type, argument, ...} =>
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   356
        ctxt |> Context.proof_map
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   357
          (ML_Context.expression (Input.pos_of argument)
69383
747f8b052e59 clarified modules;
wenzelm
parents: 69381
diff changeset
   358
            (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
   359
             ML_Lex.read_source argument @ ML_Lex.read "))"))
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   360
        |> the_string |> #make_string file_type));
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   361
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   362
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   363
(* file-system paths *)
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   364
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   365
fun path_antiquotation binding check =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   366
  antiquotation binding
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   367
    (Args.context -- Scan.lift (Parse.position Parse.path) >>
70049
c1226e4c273e tuned signature;
wenzelm
parents: 70047
diff changeset
   368
      (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
   369
    (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
   370
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   371
val _ =
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   372
  Theory.setup
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   373
   (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
   374
    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
   375
    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
   376
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents:
diff changeset
   377
end;