src/Pure/PIDE/protocol_command.ML
author wenzelm
Thu, 28 Sep 2023 14:43:07 +0200
changeset 78725 3c02ad5a1586
parent 75577 c51e1cef1eae
child 78757 a094bf81a496
permissions -rw-r--r--
clarified treatment of exceptions: avoid catch-all handlers;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/PIDE/protocol_command.ML
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     3
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     4
Protocol commands.
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     5
*)
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     6
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     7
signature PROTOCOL_COMMAND =
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     8
sig
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
     9
  exception STOP of int
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    10
  val is_protocol_exn: exn -> bool
75577
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    11
  val define_bytes: string -> (Bytes.T list -> unit) -> unit
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    12
  val define: string -> (string list -> unit) -> unit
75577
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    13
  val run: string -> Bytes.T list -> unit
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    14
end;
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    15
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    16
structure Protocol_Command: PROTOCOL_COMMAND =
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    17
struct
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    18
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    19
exception STOP of int;
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    20
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    21
val is_protocol_exn = fn STOP _ => true | _ => false;
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    22
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    23
local
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    24
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    25
val commands =
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    26
  Synchronized.var "Protocol_Command.commands"
75577
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    27
    (Symtab.empty: (Bytes.T list -> unit) Symtab.table);
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    28
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    29
in
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    30
75577
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    31
fun define_bytes name cmd =
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    32
  Synchronized.change commands (fn cmds =>
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    33
   (if not (Symtab.defined cmds name) then ()
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    34
    else warning ("Redefining Isabelle protocol command " ^ quote name);
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    35
    Symtab.update (name, cmd) cmds));
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    36
75577
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    37
fun define name cmd = define_bytes name (map Bytes.content #> cmd);
c51e1cef1eae more scalable byte messages, notably for Scala functions in ML;
wenzelm
parents: 73225
diff changeset
    38
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    39
fun run name args =
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    40
  (case Symtab.lookup (Synchronized.value commands) name of
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    41
    NONE => error ("Undefined Isabelle protocol command " ^ quote name)
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    42
  | SOME cmd =>
78725
3c02ad5a1586 clarified treatment of exceptions: avoid catch-all handlers;
wenzelm
parents: 75577
diff changeset
    43
      (case Exn.capture (fn () => Runtime.exn_trace_system (fn () => cmd args)) () of
3c02ad5a1586 clarified treatment of exceptions: avoid catch-all handlers;
wenzelm
parents: 75577
diff changeset
    44
        Exn.Res res => res
3c02ad5a1586 clarified treatment of exceptions: avoid catch-all handlers;
wenzelm
parents: 75577
diff changeset
    45
      | Exn.Exn exn =>
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    46
          if is_protocol_exn exn then Exn.reraise exn
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    47
          else error ("Isabelle protocol command failure: " ^ quote name)));
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    48
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    49
end;
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    50
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents:
diff changeset
    51
end;