src/Pure/PIDE/protocol.ML
author wenzelm
Sat Mar 01 22:46:31 2014 +0100 (2014-03-01)
changeset 55828 42ac3cfb89f6
parent 54717 42c209a6c225
child 56333 38f1422ef473
permissions -rw-r--r--
clarified language markup: added "delimited" property;
type Symbol_Pos.source preserves information about delimited outer tokens (e.g string, cartouche);
observe Completion.Language_Context only for delimited languages, which is important to complete keywords after undelimited inner tokens, e.g. "lemma A pro";
     1 (*  Title:      Pure/PIDE/protocol.ML
     2     Author:     Makarius
     3 
     4 Protocol message formats for interactive proof documents.
     5 *)
     6 
     7 structure Protocol: sig end =
     8 struct
     9 
    10 val _ =
    11   Isabelle_Process.protocol_command "Isabelle_Process.echo"
    12     (fn args => List.app writeln args);
    13 
    14 val _ =
    15   Isabelle_Process.protocol_command "Isabelle_Process.options"
    16     (fn [options_yxml] =>
    17       let val options = Options.decode (YXML.parse_body options_yxml) in
    18         Options.set_default options;
    19         Future.ML_statistics := true;
    20         Multithreading.trace := Options.int options "threads_trace";
    21         Multithreading.max_threads_update (Options.int options "threads");
    22         Goal.parallel_proofs := (if Options.int options "parallel_proofs" > 0 then 3 else 0)
    23       end);
    24 
    25 val _ =
    26   Isabelle_Process.protocol_command "Document.define_blob"
    27     (fn [digest, content] => Document.change_state (Document.define_blob digest content));
    28 
    29 val _ =
    30   Isabelle_Process.protocol_command "Document.define_command"
    31     (fn [id, name, blobs_yxml, text] =>
    32       let
    33         val blobs =
    34           YXML.parse_body blobs_yxml |>
    35             let open XML.Decode in
    36               list (variant
    37                [fn ([], a) => Exn.Res (pair string (option string) a),
    38                 fn ([], a) => Exn.Exn (ERROR (string a))])
    39             end;
    40       in
    41         Document.change_state (Document.define_command (Document_ID.parse id) name blobs text)
    42       end);
    43 
    44 val _ =
    45   Isabelle_Process.protocol_command "Document.discontinue_execution"
    46     (fn [] => Execution.discontinue ());
    47 
    48 val _ =
    49   Isabelle_Process.protocol_command "Document.cancel_exec"
    50     (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
    51 
    52 val _ =
    53   Isabelle_Process.protocol_command "Document.update"
    54     (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
    55       let
    56         val _ = Execution.discontinue ();
    57 
    58         val old_id = Document_ID.parse old_id_string;
    59         val new_id = Document_ID.parse new_id_string;
    60         val edits =
    61           YXML.parse_body edits_yxml |>
    62             let open XML.Decode in
    63               list (pair string
    64                 (variant
    65                  [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
    66                   fn ([], a) =>
    67                     let
    68                       val (master, (name, (imports, (keywords, errors)))) =
    69                         pair string (pair string (pair (list string)
    70                           (pair (list (pair string
    71                             (option (pair (pair string (list string)) (list string)))))
    72                             (list string)))) a;
    73                       val imports' = map (rpair Position.none) imports;
    74                       val header = Thy_Header.make (name, Position.none) imports' keywords;
    75                     in Document.Deps (master, header, errors) end,
    76                   fn (a :: b, c) =>
    77                     Document.Perspective (bool_atom a, map int_atom b,
    78                       list (pair int (pair string (list string))) c)]))
    79             end;
    80 
    81         val (removed, assign_update, state') = Document.update old_id new_id edits state;
    82         val _ = List.app Execution.terminate removed;
    83         val _ = Execution.purge removed;
    84         val _ = List.app Isabelle_Process.reset_tracing removed;
    85 
    86         val _ =
    87           Output.protocol_message Markup.assign_update
    88             ((new_id, assign_update) |>
    89               let open XML.Encode
    90               in pair int (list (pair int (list int))) end
    91               |> YXML.string_of_body);
    92       in Document.start_execution state' end));
    93 
    94 val _ =
    95   Isabelle_Process.protocol_command "Document.remove_versions"
    96     (fn [versions_yxml] => Document.change_state (fn state =>
    97       let
    98         val versions =
    99           YXML.parse_body versions_yxml |>
   100             let open XML.Decode in list int end;
   101         val state1 = Document.remove_versions versions state;
   102         val _ = Output.protocol_message Markup.removed_versions versions_yxml;
   103       in state1 end));
   104 
   105 val _ =
   106   Isabelle_Process.protocol_command "Document.dialog_result"
   107     (fn [serial, result] =>
   108       Active.dialog_result (Markup.parse_int serial) result
   109         handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn);
   110 
   111 end;
   112