src/Pure/PIDE/markup.ML
author wenzelm
Wed May 22 14:10:45 2013 +0200 (2013-05-22 ago)
changeset 52111 1fd184eaa310
parent 51990 cc66addbba6d
child 52563 f9a20c2c3b70
permissions -rw-r--r--
explicit management of Session.Protocol_Handlers, with protocol state and functions;
more self-contained ML/Scala module Invoke_Scala;
     1 (*  Title:      Pure/PIDE/markup.ML
     2     Author:     Makarius
     3 
     4 Isabelle-specific implementation of quasi-abstract markup elements.
     5 *)
     6 
     7 signature MARKUP =
     8 sig
     9   val parse_bool: string -> bool
    10   val print_bool: bool -> string
    11   val parse_int: string -> int
    12   val print_int: int -> string
    13   val parse_real: string -> real
    14   val print_real: real -> string
    15   type T = string * Properties.T
    16   val empty: T
    17   val is_empty: T -> bool
    18   val properties: Properties.T -> T -> T
    19   val nameN: string
    20   val name: string -> T -> T
    21   val kindN: string
    22   val bindingN: string val binding: T
    23   val entityN: string val entity: string -> string -> T
    24   val get_entity_kind: T -> string option
    25   val defN: string
    26   val refN: string
    27   val lineN: string
    28   val offsetN: string
    29   val end_offsetN: string
    30   val fileN: string
    31   val idN: string
    32   val position_properties': string list
    33   val position_properties: string list
    34   val positionN: string val position: T
    35   val pathN: string val path: string -> T
    36   val indentN: string
    37   val blockN: string val block: int -> T
    38   val widthN: string
    39   val breakN: string val break: int -> T
    40   val fbreakN: string val fbreak: T
    41   val itemN: string val item: T
    42   val hiddenN: string val hidden: T
    43   val theoryN: string
    44   val classN: string
    45   val type_nameN: string
    46   val constantN: string
    47   val fixedN: string val fixed: string -> T
    48   val dynamic_factN: string val dynamic_fact: string -> T
    49   val tfreeN: string val tfree: T
    50   val tvarN: string val tvar: T
    51   val freeN: string val free: T
    52   val skolemN: string val skolem: T
    53   val boundN: string val bound: T
    54   val varN: string val var: T
    55   val numeralN: string val numeral: T
    56   val literalN: string val literal: T
    57   val delimiterN: string val delimiter: T
    58   val inner_stringN: string val inner_string: T
    59   val inner_commentN: string val inner_comment: T
    60   val token_rangeN: string val token_range: T
    61   val sortN: string val sort: T
    62   val typN: string val typ: T
    63   val termN: string val term: T
    64   val propN: string val prop: T
    65   val sortingN: string val sorting: T
    66   val typingN: string val typing: T
    67   val ML_keywordN: string val ML_keyword: T
    68   val ML_delimiterN: string val ML_delimiter: T
    69   val ML_tvarN: string val ML_tvar: T
    70   val ML_numeralN: string val ML_numeral: T
    71   val ML_charN: string val ML_char: T
    72   val ML_stringN: string val ML_string: T
    73   val ML_commentN: string val ML_comment: T
    74   val ML_defN: string
    75   val ML_openN: string
    76   val ML_structN: string
    77   val ML_typingN: string val ML_typing: T
    78   val ML_sourceN: string val ML_source: T
    79   val document_sourceN: string val document_source: T
    80   val antiqN: string val antiq: T
    81   val ML_antiquotationN: string
    82   val document_antiquotationN: string
    83   val document_antiquotation_optionN: string
    84   val paragraphN: string val paragraph: T
    85   val text_foldN: string val text_fold: T
    86   val keywordN: string val keyword: T
    87   val operatorN: string val operator: T
    88   val commandN: string val command: T
    89   val stringN: string val string: T
    90   val altstringN: string val altstring: T
    91   val verbatimN: string val verbatim: T
    92   val commentN: string val comment: T
    93   val controlN: string val control: T
    94   val tokenN: string val token: Properties.T -> T
    95   val keyword1N: string val keyword1: T
    96   val keyword2N: string val keyword2: T
    97   val elapsedN: string
    98   val cpuN: string
    99   val gcN: string
   100   val timing_properties: {elapsed: Time.time, cpu: Time.time, gc: Time.time} -> Properties.T
   101   val parse_timing_properties: Properties.T -> {elapsed: Time.time, cpu: Time.time, gc: Time.time}
   102   val command_timingN: string
   103   val command_timing_properties:
   104     {file: string, offset: int, name: string} -> Time.time -> Properties.T
   105   val parse_command_timing_properties:
   106     Properties.T -> ({file: string, offset: int, name: string} * Time.time) option
   107   val timingN: string val timing: {elapsed: Time.time, cpu: Time.time, gc: Time.time} -> T
   108   val subgoalsN: string
   109   val proof_stateN: string val proof_state: int -> T
   110   val stateN: string val state: T
   111   val goalN: string val goal: T
   112   val subgoalN: string val subgoal: string -> T
   113   val taskN: string
   114   val acceptedN: string val accepted: T
   115   val forkedN: string val forked: T
   116   val joinedN: string val joined: T
   117   val runningN: string val running: T
   118   val finishedN: string val finished: T
   119   val failedN: string val failed: T
   120   val serialN: string
   121   val exec_idN: string
   122   val initN: string
   123   val statusN: string
   124   val resultN: string
   125   val writelnN: string
   126   val tracingN: string
   127   val warningN: string
   128   val errorN: string
   129   val protocolN: string
   130   val legacyN: string val legacy: T
   131   val promptN: string val prompt: T
   132   val reportN: string val report: T
   133   val no_reportN: string val no_report: T
   134   val badN: string val bad: T
   135   val intensifyN: string val intensify: T
   136   val browserN: string
   137   val graphviewN: string
   138   val sendbackN: string
   139   val paddingN: string
   140   val padding_line: Properties.entry
   141   val dialogN: string val dialog: serial -> string -> T
   142   val functionN: string
   143   val assign_execs: Properties.T
   144   val removed_versions: Properties.T
   145   val protocol_handler: string -> Properties.T
   146   val invoke_scala: string -> string -> Properties.T
   147   val cancel_scala: string -> Properties.T
   148   val ML_statistics: Properties.entry
   149   val task_statistics: Properties.entry
   150   val command_timing: Properties.entry
   151   val loading_theory: string -> Properties.T
   152   val dest_loading_theory: Properties.T -> string option
   153   val no_output: Output.output * Output.output
   154   val default_output: T -> Output.output * Output.output
   155   val add_mode: string -> (T -> Output.output * Output.output) -> unit
   156   val output: T -> Output.output * Output.output
   157   val enclose: T -> Output.output -> Output.output
   158   val markup: T -> string -> string
   159   val markup_only: T -> string
   160 end;
   161 
   162 structure Markup: MARKUP =
   163 struct
   164 
   165 (** markup elements **)
   166 
   167 (* misc values *)
   168 
   169 fun parse_bool "true" = true
   170   | parse_bool "false" = false
   171   | parse_bool s = raise Fail ("Bad boolean: " ^ quote s);
   172 
   173 val print_bool = Bool.toString;
   174 
   175 fun parse_int s =
   176   let val i = Int.fromString s in
   177     if is_none i orelse String.isPrefix "~" s
   178     then raise Fail ("Bad integer: " ^ quote s)
   179     else the i
   180   end;
   181 
   182 val print_int = signed_string_of_int;
   183 
   184 fun parse_real s =
   185   (case Real.fromString s of
   186     SOME x => x
   187   | NONE => raise Fail ("Bad real: " ^ quote s));
   188 
   189 fun print_real x =
   190   let val s = signed_string_of_real x in
   191     (case space_explode "." s of
   192       [a, b] => if forall_string (fn c => c = "0") b then a else s
   193     | _ => s)
   194   end;
   195 
   196 
   197 (* basic markup *)
   198 
   199 type T = string * Properties.T;
   200 
   201 val empty = ("", []);
   202 
   203 fun is_empty ("", _) = true
   204   | is_empty _ = false;
   205 
   206 
   207 fun properties more_props ((elem, props): T) =
   208   (elem, fold_rev Properties.put more_props props);
   209 
   210 fun markup_elem elem = (elem, (elem, []): T);
   211 fun markup_string elem prop = (elem, fn s => (elem, [(prop, s)]): T);
   212 fun markup_int elem prop = (elem, fn i => (elem, [(prop, print_int i)]): T);
   213 
   214 
   215 (* misc properties *)
   216 
   217 val nameN = "name";
   218 fun name a = properties [(nameN, a)];
   219 
   220 val kindN = "kind";
   221 
   222 
   223 (* formal entities *)
   224 
   225 val (bindingN, binding) = markup_elem "binding";
   226 
   227 val entityN = "entity";
   228 fun entity kind name = (entityN, [(nameN, name), (kindN, kind)]);
   229 
   230 fun get_entity_kind (name, props) =
   231   if name = entityN then AList.lookup (op =) props kindN
   232   else NONE;
   233 
   234 val defN = "def";
   235 val refN = "ref";
   236 
   237 
   238 (* position *)
   239 
   240 val lineN = "line";
   241 val offsetN = "offset";
   242 val end_offsetN = "end_offset";
   243 val fileN = "file";
   244 val idN = "id";
   245 
   246 val position_properties' = [fileN, idN];
   247 val position_properties = [lineN, offsetN, end_offsetN] @ position_properties';
   248 
   249 val (positionN, position) = markup_elem "position";
   250 
   251 
   252 (* path *)
   253 
   254 val (pathN, path) = markup_string "path" nameN;
   255 
   256 
   257 (* pretty printing *)
   258 
   259 val indentN = "indent";
   260 val (blockN, block) = markup_int "block" indentN;
   261 
   262 val widthN = "width";
   263 val (breakN, break) = markup_int "break" widthN;
   264 
   265 val (fbreakN, fbreak) = markup_elem "fbreak";
   266 
   267 val (itemN, item) = markup_elem "item";
   268 
   269 
   270 (* hidden text *)
   271 
   272 val (hiddenN, hidden) = markup_elem "hidden";
   273 
   274 
   275 (* logical entities *)
   276 
   277 val theoryN = "theory";
   278 val classN = "class";
   279 val type_nameN = "type_name";
   280 val constantN = "constant";
   281 
   282 val (fixedN, fixed) = markup_string "fixed" nameN;
   283 val (dynamic_factN, dynamic_fact) = markup_string "dynamic_fact" nameN;
   284 
   285 
   286 (* inner syntax *)
   287 
   288 val (tfreeN, tfree) = markup_elem "tfree";
   289 val (tvarN, tvar) = markup_elem "tvar";
   290 val (freeN, free) = markup_elem "free";
   291 val (skolemN, skolem) = markup_elem "skolem";
   292 val (boundN, bound) = markup_elem "bound";
   293 val (varN, var) = markup_elem "var";
   294 val (numeralN, numeral) = markup_elem "numeral";
   295 val (literalN, literal) = markup_elem "literal";
   296 val (delimiterN, delimiter) = markup_elem "delimiter";
   297 val (inner_stringN, inner_string) = markup_elem "inner_string";
   298 val (inner_commentN, inner_comment) = markup_elem "inner_comment";
   299 
   300 val (token_rangeN, token_range) = markup_elem "token_range";
   301 
   302 val (sortN, sort) = markup_elem "sort";
   303 val (typN, typ) = markup_elem "typ";
   304 val (termN, term) = markup_elem "term";
   305 val (propN, prop) = markup_elem "prop";
   306 
   307 val (sortingN, sorting) = markup_elem "sorting";
   308 val (typingN, typing) = markup_elem "typing";
   309 
   310 
   311 (* ML syntax *)
   312 
   313 val (ML_keywordN, ML_keyword) = markup_elem "ML_keyword";
   314 val (ML_delimiterN, ML_delimiter) = markup_elem "ML_delimiter";
   315 val (ML_tvarN, ML_tvar) = markup_elem "ML_tvar";
   316 val (ML_numeralN, ML_numeral) = markup_elem "ML_numeral";
   317 val (ML_charN, ML_char) = markup_elem "ML_char";
   318 val (ML_stringN, ML_string) = markup_elem "ML_string";
   319 val (ML_commentN, ML_comment) = markup_elem "ML_comment";
   320 
   321 val ML_defN = "ML_def";
   322 val ML_openN = "ML_open";
   323 val ML_structN = "ML_struct";
   324 val (ML_typingN, ML_typing) = markup_elem "ML_typing";
   325 
   326 
   327 (* embedded source text *)
   328 
   329 val (ML_sourceN, ML_source) = markup_elem "ML_source";
   330 val (document_sourceN, document_source) = markup_elem "document_source";
   331 
   332 val (antiqN, antiq) = markup_elem "antiq";
   333 val ML_antiquotationN = "ML_antiquotation";
   334 val document_antiquotationN = "document_antiquotation";
   335 val document_antiquotation_optionN = "document_antiquotation_option";
   336 
   337 
   338 (* text structure *)
   339 
   340 val (paragraphN, paragraph) = markup_elem "paragraph";
   341 val (text_foldN, text_fold) = markup_elem "text_fold";
   342 
   343 
   344 (* outer syntax *)
   345 
   346 val (keywordN, keyword) = markup_elem "keyword";
   347 val (operatorN, operator) = markup_elem "operator";
   348 val (commandN, command) = markup_elem "command";
   349 val (stringN, string) = markup_elem "string";
   350 val (altstringN, altstring) = markup_elem "altstring";
   351 val (verbatimN, verbatim) = markup_elem "verbatim";
   352 val (commentN, comment) = markup_elem "comment";
   353 val (controlN, control) = markup_elem "control";
   354 
   355 val tokenN = "token";
   356 fun token props = (tokenN, props);
   357 
   358 val (keyword1N, keyword1) = markup_elem "keyword1";
   359 val (keyword2N, keyword2) = markup_elem "keyword2";
   360 
   361 
   362 (* timing *)
   363 
   364 val elapsedN = "elapsed";
   365 val cpuN = "cpu";
   366 val gcN = "gc";
   367 
   368 fun timing_properties {elapsed, cpu, gc} =
   369  [(elapsedN, Time.toString elapsed),
   370   (cpuN, Time.toString cpu),
   371   (gcN, Time.toString gc)];
   372 
   373 fun parse_timing_properties props =
   374  {elapsed = Properties.seconds props elapsedN,
   375   cpu = Properties.seconds props cpuN,
   376   gc = Properties.seconds props gcN};
   377 
   378 val timingN = "timing";
   379 fun timing t = (timingN, timing_properties t);
   380 
   381 
   382 (* command timing *)
   383 
   384 val command_timingN = "command_timing";
   385 
   386 fun command_timing_properties {file, offset, name} elapsed =
   387  [(fileN, file), (offsetN, print_int offset),
   388   (nameN, name), (elapsedN, Time.toString elapsed)];
   389 
   390 fun parse_command_timing_properties props =
   391   (case (Properties.get props fileN, Properties.get props offsetN, Properties.get props nameN) of
   392     (SOME file, SOME offset, SOME name) =>
   393       SOME ({file = file, offset = parse_int offset, name = name},
   394         Properties.seconds props elapsedN)
   395   | _ => NONE);
   396 
   397 
   398 (* toplevel *)
   399 
   400 val subgoalsN = "subgoals";
   401 val (proof_stateN, proof_state) = markup_int "proof_state" subgoalsN;
   402 
   403 val (stateN, state) = markup_elem "state";
   404 val (goalN, goal) = markup_elem "goal";
   405 val (subgoalN, subgoal) = markup_string "subgoal" nameN;
   406 
   407 
   408 (* command status *)
   409 
   410 val taskN = "task";
   411 
   412 val (acceptedN, accepted) = markup_elem "accepted";
   413 val (forkedN, forked) = markup_elem "forked";
   414 val (joinedN, joined) = markup_elem "joined";
   415 val (runningN, running) = markup_elem "running";
   416 val (finishedN, finished) = markup_elem "finished";
   417 val (failedN, failed) = markup_elem "failed";
   418 
   419 
   420 (* messages *)
   421 
   422 val serialN = "serial";
   423 val exec_idN = "exec_id";
   424 
   425 val initN = "init";
   426 val statusN = "status";
   427 val resultN = "result";
   428 val writelnN = "writeln";
   429 val tracingN = "tracing";
   430 val warningN = "warning";
   431 val errorN = "error";
   432 val protocolN = "protocol";
   433 
   434 val (legacyN, legacy) = markup_elem "legacy";
   435 val (promptN, prompt) = markup_elem "prompt";
   436 
   437 val (reportN, report) = markup_elem "report";
   438 val (no_reportN, no_report) = markup_elem "no_report";
   439 
   440 val (badN, bad) = markup_elem "bad";
   441 
   442 val (intensifyN, intensify) = markup_elem "intensify";
   443 
   444 
   445 (* active areas *)
   446 
   447 val browserN = "browser"
   448 val graphviewN = "graphview";
   449 
   450 val sendbackN = "sendback";
   451 val paddingN = "padding";
   452 val padding_line = (paddingN, lineN);
   453 
   454 val dialogN = "dialog";
   455 fun dialog i result = (dialogN, [(serialN, print_int i), (resultN, result)]);
   456 
   457 
   458 (* protocol message functions *)
   459 
   460 val functionN = "function"
   461 
   462 val assign_execs = [(functionN, "assign_execs")];
   463 val removed_versions = [(functionN, "removed_versions")];
   464 
   465 fun protocol_handler name = [(functionN, "protocol_handler"), (nameN, name)];
   466 
   467 fun invoke_scala name id = [(functionN, "invoke_scala"), (nameN, name), (idN, id)];
   468 fun cancel_scala id = [(functionN, "cancel_scala"), (idN, id)];
   469 
   470 val ML_statistics = (functionN, "ML_statistics");
   471 
   472 val task_statistics = (functionN, "task_statistics");
   473 
   474 val command_timing = (functionN, "command_timing");
   475 
   476 fun loading_theory name = [("function", "loading_theory"), ("name", name)];
   477 
   478 fun dest_loading_theory [("function", "loading_theory"), ("name", name)] = SOME name
   479   | dest_loading_theory _ = NONE;
   480 
   481 
   482 
   483 (** print mode operations **)
   484 
   485 val no_output = ("", "");
   486 fun default_output (_: T) = no_output;
   487 
   488 local
   489   val default = {output = default_output};
   490   val modes = Synchronized.var "Markup.modes" (Symtab.make [("", default)]);
   491 in
   492   fun add_mode name output =
   493     Synchronized.change modes (fn tab =>
   494       (if not (Symtab.defined tab name) then ()
   495        else warning ("Redefining markup mode " ^ quote name);
   496        Symtab.update (name, {output = output}) tab));
   497   fun get_mode () =
   498     the_default default
   499       (Library.get_first (Symtab.lookup (Synchronized.value modes)) (print_mode_value ()));
   500 end;
   501 
   502 fun output m = if is_empty m then no_output else #output (get_mode ()) m;
   503 
   504 val enclose = output #-> Library.enclose;
   505 
   506 fun markup m =
   507   let val (bg, en) = output m
   508   in Library.enclose (Output.escape bg) (Output.escape en) end;
   509 
   510 fun markup_only m = markup m "";
   511 
   512 end;