src/Pure/PIDE/markup.ML
author wenzelm
Tue Aug 12 18:36:43 2014 +0200 (2014-08-12)
changeset 57916 2c2c24dbf0a4
parent 57594 037f3b251df5
child 57975 c657c68a60ab
permissions -rw-r--r--
generic process wrapping in Prover;
clarified module arrangement;
     1 (*  Title:      Pure/PIDE/markup.ML
     2     Author:     Makarius
     3 
     4 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 instanceN: string
    23   val languageN: string
    24   val symbolsN: string
    25   val delimitedN: string
    26   val is_delimited: Properties.T -> bool
    27   val language: {name: string, symbols: bool, antiquotes: bool, delimited: bool} -> T
    28   val language': {name: string, symbols: bool, antiquotes: bool} -> bool -> T
    29   val language_method: T
    30   val language_attribute: T
    31   val language_sort: bool -> T
    32   val language_type: bool -> T
    33   val language_term: bool -> T
    34   val language_prop: bool -> T
    35   val language_ML: bool -> T
    36   val language_SML: bool -> T
    37   val language_document: bool -> T
    38   val language_antiquotation: T
    39   val language_text: bool -> T
    40   val language_rail: T
    41   val language_path: T
    42   val bindingN: string val binding: T
    43   val entityN: string val entity: string -> string -> T
    44   val get_entity_kind: T -> string option
    45   val defN: string
    46   val refN: string
    47   val completionN: string val completion: T
    48   val no_completionN: string val no_completion: T
    49   val lineN: string
    50   val offsetN: string
    51   val end_offsetN: string
    52   val fileN: string
    53   val idN: string
    54   val position_properties': string list
    55   val position_properties: string list
    56   val positionN: string val position: T
    57   val pathN: string val path: string -> T
    58   val urlN: string val url: string -> T
    59   val indentN: string
    60   val blockN: string val block: int -> T
    61   val widthN: string
    62   val breakN: string val break: int -> T
    63   val fbreakN: string val fbreak: T
    64   val itemN: string val item: T
    65   val wordsN: string val words: T
    66   val hiddenN: string val hidden: T
    67   val system_optionN: string
    68   val theoryN: string
    69   val classN: string
    70   val type_nameN: string
    71   val constantN: string
    72   val fixedN: string val fixed: string -> T
    73   val caseN: string val case_: string -> T
    74   val dynamic_factN: string val dynamic_fact: string -> T
    75   val tfreeN: string val tfree: T
    76   val tvarN: string val tvar: T
    77   val freeN: string val free: T
    78   val skolemN: string val skolem: T
    79   val boundN: string val bound: T
    80   val varN: string val var: T
    81   val numeralN: string val numeral: T
    82   val literalN: string val literal: T
    83   val delimiterN: string val delimiter: T
    84   val inner_stringN: string val inner_string: T
    85   val inner_cartoucheN: string val inner_cartouche: T
    86   val inner_commentN: string val inner_comment: T
    87   val token_rangeN: string val token_range: T
    88   val sortingN: string val sorting: T
    89   val typingN: string val typing: T
    90   val ML_keyword1N: string val ML_keyword1: T
    91   val ML_keyword2N: string val ML_keyword2: T
    92   val ML_keyword3N: string val ML_keyword3: T
    93   val ML_delimiterN: string val ML_delimiter: T
    94   val ML_tvarN: string val ML_tvar: T
    95   val ML_numeralN: string val ML_numeral: T
    96   val ML_charN: string val ML_char: T
    97   val ML_stringN: string val ML_string: T
    98   val ML_commentN: string val ML_comment: T
    99   val SML_stringN: string val SML_string: T
   100   val SML_commentN: string val SML_comment: T
   101   val ML_defN: string
   102   val ML_openN: string
   103   val ML_structureN: string
   104   val ML_typingN: string val ML_typing: T
   105   val antiquotedN: string val antiquoted: T
   106   val antiquoteN: string val antiquote: T
   107   val ML_antiquotationN: string
   108   val document_antiquotationN: string
   109   val document_antiquotation_optionN: string
   110   val paragraphN: string val paragraph: T
   111   val text_foldN: string val text_fold: T
   112   val commandN: string val command: T
   113   val stringN: string val string: T
   114   val altstringN: string val altstring: T
   115   val verbatimN: string val verbatim: T
   116   val cartoucheN: string val cartouche: T
   117   val commentN: string val comment: T
   118   val controlN: string val control: T
   119   val tokenN: string val token: bool -> Properties.T -> T
   120   val keyword1N: string val keyword1: T
   121   val keyword2N: string val keyword2: T
   122   val keyword3N: string val keyword3: T
   123   val quasi_keywordN: string val quasi_keyword: T
   124   val improperN: string val improper: T
   125   val operatorN: string val operator: T
   126   val elapsedN: string
   127   val cpuN: string
   128   val gcN: string
   129   val timing_properties: {elapsed: Time.time, cpu: Time.time, gc: Time.time} -> Properties.T
   130   val parse_timing_properties: Properties.T -> {elapsed: Time.time, cpu: Time.time, gc: Time.time}
   131   val command_timingN: string
   132   val command_timing_properties:
   133     {file: string, offset: int, name: string} -> Time.time -> Properties.T
   134   val parse_command_timing_properties:
   135     Properties.T -> ({file: string, offset: int, name: string} * Time.time) option
   136   val timingN: string val timing: {elapsed: Time.time, cpu: Time.time, gc: Time.time} -> T
   137   val subgoalsN: string
   138   val proof_stateN: string val proof_state: int -> T
   139   val stateN: string val state: T
   140   val goalN: string val goal: T
   141   val subgoalN: string val subgoal: string -> T
   142   val taskN: string
   143   val acceptedN: string val accepted: T
   144   val forkedN: string val forked: T
   145   val joinedN: string val joined: T
   146   val runningN: string val running: T
   147   val finishedN: string val finished: T
   148   val failedN: string val failed: T
   149   val serialN: string
   150   val serial_properties: int -> Properties.T
   151   val exec_idN: string
   152   val initN: string
   153   val statusN: string
   154   val resultN: string
   155   val writelnN: string
   156   val tracingN: string
   157   val warningN: string
   158   val errorN: string
   159   val protocolN: string
   160   val legacyN: string val legacy: T
   161   val promptN: string val prompt: T
   162   val reportN: string val report: T
   163   val no_reportN: string val no_report: T
   164   val badN: string val bad: T
   165   val intensifyN: string val intensify: T
   166   val informationN: string val information: T
   167   val browserN: string
   168   val graphviewN: string
   169   val sendbackN: string
   170   val paddingN: string
   171   val padding_line: Properties.entry
   172   val padding_command: Properties.entry
   173   val dialogN: string val dialog: serial -> string -> T
   174   val functionN: string
   175   val assign_update: Properties.T
   176   val removed_versions: Properties.T
   177   val protocol_handler: string -> Properties.T
   178   val invoke_scala: string -> string -> Properties.T
   179   val cancel_scala: string -> Properties.T
   180   val ML_statistics: Properties.entry
   181   val task_statistics: Properties.entry
   182   val command_timing: Properties.entry
   183   val loading_theory: string -> Properties.T
   184   val dest_loading_theory: Properties.T -> string option
   185   val use_theories_result: string -> bool -> Properties.T
   186   val print_operationsN: string
   187   val print_operations: Properties.T
   188   val simp_trace_panelN: string
   189   val simp_trace_logN: string
   190   val simp_trace_stepN: string
   191   val simp_trace_recurseN: string
   192   val simp_trace_hintN: string
   193   val simp_trace_ignoreN: string
   194   val simp_trace_cancel: serial -> Properties.T
   195   val no_output: Output.output * Output.output
   196   val default_output: T -> Output.output * Output.output
   197   val add_mode: string -> (T -> Output.output * Output.output) -> unit
   198   val output: T -> Output.output * Output.output
   199   val enclose: T -> Output.output -> Output.output
   200   val markup: T -> string -> string
   201   val markup_only: T -> string
   202   val markup_report: string -> string
   203 end;
   204 
   205 structure Markup: MARKUP =
   206 struct
   207 
   208 (** markup elements **)
   209 
   210 (* misc values *)
   211 
   212 fun parse_bool "true" = true
   213   | parse_bool "false" = false
   214   | parse_bool s = raise Fail ("Bad boolean: " ^ quote s);
   215 
   216 val print_bool = Bool.toString;
   217 
   218 fun parse_int s =
   219   let val i = Int.fromString s in
   220     if is_none i orelse String.isPrefix "~" s
   221     then raise Fail ("Bad integer: " ^ quote s)
   222     else the i
   223   end;
   224 
   225 val print_int = signed_string_of_int;
   226 
   227 fun parse_real s =
   228   (case Real.fromString s of
   229     SOME x => x
   230   | NONE => raise Fail ("Bad real: " ^ quote s));
   231 
   232 fun print_real x =
   233   let val s = signed_string_of_real x in
   234     (case space_explode "." s of
   235       [a, b] => if forall_string (fn c => c = "0") b then a else s
   236     | _ => s)
   237   end;
   238 
   239 
   240 (* basic markup *)
   241 
   242 type T = string * Properties.T;
   243 
   244 val empty = ("", []);
   245 
   246 fun is_empty ("", _) = true
   247   | is_empty _ = false;
   248 
   249 
   250 fun properties more_props ((elem, props): T) =
   251   (elem, fold_rev Properties.put more_props props);
   252 
   253 fun markup_elem name = (name, (name, []): T);
   254 fun markup_string name prop = (name, fn s => (name, [(prop, s)]): T);
   255 fun markup_int name prop = (name, fn i => (name, [(prop, print_int i)]): T);
   256 
   257 
   258 (* misc properties *)
   259 
   260 val nameN = "name";
   261 fun name a = properties [(nameN, a)];
   262 
   263 val kindN = "kind";
   264 
   265 val instanceN = "instance";
   266 
   267 
   268 (* embedded languages *)
   269 
   270 val languageN = "language";
   271 val symbolsN = "symbols";
   272 val antiquotesN = "antiquotes";
   273 val delimitedN = "delimited"
   274 
   275 fun is_delimited props =
   276   Properties.get props delimitedN = SOME "true";
   277 
   278 fun language {name, symbols, antiquotes, delimited} =
   279   (languageN,
   280    [(nameN, name),
   281     (symbolsN, print_bool symbols),
   282     (antiquotesN, print_bool antiquotes),
   283     (delimitedN, print_bool delimited)]);
   284 
   285 fun language' {name, symbols, antiquotes} delimited =
   286   language {name = name, symbols = symbols, antiquotes = antiquotes, delimited = delimited};
   287 
   288 val language_method =
   289   language {name = "method", symbols = true, antiquotes = false, delimited = false};
   290 val language_attribute =
   291   language {name = "attribute", symbols = true, antiquotes = false, delimited = false};
   292 val language_sort = language' {name = "sort", symbols = true, antiquotes = false};
   293 val language_type = language' {name = "type", symbols = true, antiquotes = false};
   294 val language_term = language' {name = "term", symbols = true, antiquotes = false};
   295 val language_prop = language' {name = "prop", symbols = true, antiquotes = false};
   296 val language_ML = language' {name = "ML", symbols = false, antiquotes = true};
   297 val language_SML = language' {name = "SML", symbols = false, antiquotes = false};
   298 val language_document = language' {name = "document", symbols = false, antiquotes = true};
   299 val language_antiquotation =
   300   language {name = "antiquotation", symbols = true, antiquotes = false, delimited = true};
   301 val language_text = language' {name = "text", symbols = true, antiquotes = false};
   302 val language_rail = language {name = "rail", symbols = true, antiquotes = true, delimited = true};
   303 val language_path = language {name = "path", symbols = false, antiquotes = false, delimited = true};
   304 
   305 
   306 (* formal entities *)
   307 
   308 val (bindingN, binding) = markup_elem "binding";
   309 
   310 val entityN = "entity";
   311 fun entity kind name = (entityN, [(nameN, name), (kindN, kind)]);
   312 
   313 fun get_entity_kind (name, props) =
   314   if name = entityN then AList.lookup (op =) props kindN
   315   else NONE;
   316 
   317 val defN = "def";
   318 val refN = "ref";
   319 
   320 
   321 (* completion *)
   322 
   323 val (completionN, completion) = markup_elem "completion";
   324 val (no_completionN, no_completion) = markup_elem "no_completion";
   325 
   326 
   327 (* position *)
   328 
   329 val lineN = "line";
   330 val offsetN = "offset";
   331 val end_offsetN = "end_offset";
   332 val fileN = "file";
   333 val idN = "id";
   334 
   335 val position_properties' = [fileN, idN];
   336 val position_properties = [lineN, offsetN, end_offsetN] @ position_properties';
   337 
   338 val (positionN, position) = markup_elem "position";
   339 
   340 
   341 (* external resources *)
   342 
   343 val (pathN, path) = markup_string "path" nameN;
   344 val (urlN, url) = markup_string "url" nameN;
   345 
   346 
   347 (* pretty printing *)
   348 
   349 val indentN = "indent";
   350 val (blockN, block) = markup_int "block" indentN;
   351 
   352 val widthN = "width";
   353 val (breakN, break) = markup_int "break" widthN;
   354 
   355 val (fbreakN, fbreak) = markup_elem "fbreak";
   356 
   357 val (itemN, item) = markup_elem "item";
   358 
   359 
   360 (* text properties *)
   361 
   362 val (wordsN, words) = markup_elem "words";
   363 
   364 val (hiddenN, hidden) = markup_elem "hidden";
   365 
   366 
   367 (* formal entities *)
   368 
   369 val system_optionN = "system_option";
   370 
   371 val theoryN = "theory";
   372 val classN = "class";
   373 val type_nameN = "type_name";
   374 val constantN = "constant";
   375 
   376 val (fixedN, fixed) = markup_string "fixed" nameN;
   377 val (caseN, case_) = markup_string "case" nameN;
   378 val (dynamic_factN, dynamic_fact) = markup_string "dynamic_fact" nameN;
   379 
   380 
   381 (* inner syntax *)
   382 
   383 val (tfreeN, tfree) = markup_elem "tfree";
   384 val (tvarN, tvar) = markup_elem "tvar";
   385 val (freeN, free) = markup_elem "free";
   386 val (skolemN, skolem) = markup_elem "skolem";
   387 val (boundN, bound) = markup_elem "bound";
   388 val (varN, var) = markup_elem "var";
   389 val (numeralN, numeral) = markup_elem "numeral";
   390 val (literalN, literal) = markup_elem "literal";
   391 val (delimiterN, delimiter) = markup_elem "delimiter";
   392 val (inner_stringN, inner_string) = markup_elem "inner_string";
   393 val (inner_cartoucheN, inner_cartouche) = markup_elem "inner_cartouche";
   394 val (inner_commentN, inner_comment) = markup_elem "inner_comment";
   395 
   396 val (token_rangeN, token_range) = markup_elem "token_range";
   397 
   398 val (sortingN, sorting) = markup_elem "sorting";
   399 val (typingN, typing) = markup_elem "typing";
   400 
   401 
   402 (* ML syntax *)
   403 
   404 val (ML_keyword1N, ML_keyword1) = markup_elem "ML_keyword1";
   405 val (ML_keyword2N, ML_keyword2) = markup_elem "ML_keyword2";
   406 val (ML_keyword3N, ML_keyword3) = markup_elem "ML_keyword3";
   407 val (ML_delimiterN, ML_delimiter) = markup_elem "ML_delimiter";
   408 val (ML_tvarN, ML_tvar) = markup_elem "ML_tvar";
   409 val (ML_numeralN, ML_numeral) = markup_elem "ML_numeral";
   410 val (ML_charN, ML_char) = markup_elem "ML_char";
   411 val (ML_stringN, ML_string) = markup_elem "ML_string";
   412 val (ML_commentN, ML_comment) = markup_elem "ML_comment";
   413 val (SML_stringN, SML_string) = markup_elem "SML_string";
   414 val (SML_commentN, SML_comment) = markup_elem "SML_comment";
   415 
   416 val ML_defN = "ML_def";
   417 val ML_openN = "ML_open";
   418 val ML_structureN = "ML_structure";
   419 val (ML_typingN, ML_typing) = markup_elem "ML_typing";
   420 
   421 
   422 (* antiquotations *)
   423 
   424 val (antiquotedN, antiquoted) = markup_elem "antiquoted";
   425 val (antiquoteN, antiquote) = markup_elem "antiquote";
   426 
   427 val ML_antiquotationN = "ML_antiquotation";
   428 val document_antiquotationN = "document_antiquotation";
   429 val document_antiquotation_optionN = "document_antiquotation_option";
   430 
   431 
   432 (* text structure *)
   433 
   434 val (paragraphN, paragraph) = markup_elem "paragraph";
   435 val (text_foldN, text_fold) = markup_elem "text_fold";
   436 
   437 
   438 (* outer syntax *)
   439 
   440 val (commandN, command) = markup_elem "command";
   441 val (keyword1N, keyword1) = markup_elem "keyword1";
   442 val (keyword2N, keyword2) = markup_elem "keyword2";
   443 val (keyword3N, keyword3) = markup_elem "keyword3";
   444 val (quasi_keywordN, quasi_keyword) = markup_elem "quasi_keyword";
   445 val (improperN, improper) = markup_elem "improper";
   446 val (operatorN, operator) = markup_elem "operator";
   447 val (stringN, string) = markup_elem "string";
   448 val (altstringN, altstring) = markup_elem "altstring";
   449 val (verbatimN, verbatim) = markup_elem "verbatim";
   450 val (cartoucheN, cartouche) = markup_elem "cartouche";
   451 val (commentN, comment) = markup_elem "comment";
   452 val (controlN, control) = markup_elem "control";
   453 
   454 val tokenN = "token";
   455 fun token delimited props = (tokenN, (delimitedN, print_bool delimited) :: props);
   456 
   457 
   458 (* timing *)
   459 
   460 val elapsedN = "elapsed";
   461 val cpuN = "cpu";
   462 val gcN = "gc";
   463 
   464 fun timing_properties {elapsed, cpu, gc} =
   465  [(elapsedN, Time.toString elapsed),
   466   (cpuN, Time.toString cpu),
   467   (gcN, Time.toString gc)];
   468 
   469 fun parse_timing_properties props =
   470  {elapsed = Properties.seconds props elapsedN,
   471   cpu = Properties.seconds props cpuN,
   472   gc = Properties.seconds props gcN};
   473 
   474 val timingN = "timing";
   475 fun timing t = (timingN, timing_properties t);
   476 
   477 
   478 (* command timing *)
   479 
   480 val command_timingN = "command_timing";
   481 
   482 fun command_timing_properties {file, offset, name} elapsed =
   483  [(fileN, file), (offsetN, print_int offset),
   484   (nameN, name), (elapsedN, Time.toString elapsed)];
   485 
   486 fun parse_command_timing_properties props =
   487   (case (Properties.get props fileN, Properties.get props offsetN, Properties.get props nameN) of
   488     (SOME file, SOME offset, SOME name) =>
   489       SOME ({file = file, offset = parse_int offset, name = name},
   490         Properties.seconds props elapsedN)
   491   | _ => NONE);
   492 
   493 
   494 (* toplevel *)
   495 
   496 val subgoalsN = "subgoals";
   497 val (proof_stateN, proof_state) = markup_int "proof_state" subgoalsN;
   498 
   499 val (stateN, state) = markup_elem "state";
   500 val (goalN, goal) = markup_elem "goal";
   501 val (subgoalN, subgoal) = markup_string "subgoal" nameN;
   502 
   503 
   504 (* command status *)
   505 
   506 val taskN = "task";
   507 
   508 val (acceptedN, accepted) = markup_elem "accepted";
   509 val (forkedN, forked) = markup_elem "forked";
   510 val (joinedN, joined) = markup_elem "joined";
   511 val (runningN, running) = markup_elem "running";
   512 val (finishedN, finished) = markup_elem "finished";
   513 val (failedN, failed) = markup_elem "failed";
   514 
   515 
   516 (* messages *)
   517 
   518 val serialN = "serial";
   519 fun serial_properties i = [(serialN, print_int i)];
   520 
   521 val exec_idN = "exec_id";
   522 
   523 val initN = "init";
   524 val statusN = "status";
   525 val resultN = "result";
   526 val writelnN = "writeln";
   527 val tracingN = "tracing";
   528 val warningN = "warning";
   529 val errorN = "error";
   530 val protocolN = "protocol";
   531 
   532 val (legacyN, legacy) = markup_elem "legacy";
   533 val (promptN, prompt) = markup_elem "prompt";
   534 
   535 val (reportN, report) = markup_elem "report";
   536 val (no_reportN, no_report) = markup_elem "no_report";
   537 
   538 val (badN, bad) = markup_elem "bad";
   539 
   540 val (intensifyN, intensify) = markup_elem "intensify";
   541 val (informationN, information) = markup_elem "information";
   542 
   543 
   544 (* active areas *)
   545 
   546 val browserN = "browser"
   547 val graphviewN = "graphview";
   548 
   549 val sendbackN = "sendback";
   550 val paddingN = "padding";
   551 val padding_line = (paddingN, "line");
   552 val padding_command = (paddingN, "command");
   553 
   554 val dialogN = "dialog";
   555 fun dialog i result = (dialogN, [(serialN, print_int i), (resultN, result)]);
   556 
   557 
   558 (* protocol message functions *)
   559 
   560 val functionN = "function"
   561 
   562 val assign_update = [(functionN, "assign_update")];
   563 val removed_versions = [(functionN, "removed_versions")];
   564 
   565 fun protocol_handler name = [(functionN, "protocol_handler"), (nameN, name)];
   566 
   567 fun invoke_scala name id = [(functionN, "invoke_scala"), (nameN, name), (idN, id)];
   568 fun cancel_scala id = [(functionN, "cancel_scala"), (idN, id)];
   569 
   570 val ML_statistics = (functionN, "ML_statistics");
   571 
   572 val task_statistics = (functionN, "task_statistics");
   573 
   574 val command_timing = (functionN, "command_timing");
   575 
   576 fun loading_theory name = [("function", "loading_theory"), ("name", name)];
   577 
   578 fun dest_loading_theory [("function", "loading_theory"), ("name", name)] = SOME name
   579   | dest_loading_theory _ = NONE;
   580 
   581 fun use_theories_result id ok =
   582   [("function", "use_theories_result"), ("id", id), ("ok", print_bool ok)];
   583 
   584 val print_operationsN = "print_operations";
   585 val print_operations = [(functionN, print_operationsN)];
   586 
   587 
   588 (* simplifier trace *)
   589 
   590 val simp_trace_panelN = "simp_trace_panel";
   591 
   592 val simp_trace_logN = "simp_trace_log";
   593 val simp_trace_stepN = "simp_trace_step";
   594 val simp_trace_recurseN = "simp_trace_recurse";
   595 val simp_trace_hintN = "simp_trace_hint";
   596 val simp_trace_ignoreN = "simp_trace_ignore";
   597 
   598 fun simp_trace_cancel i = [(functionN, "simp_trace_cancel"), (serialN, print_int i)];
   599 
   600 
   601 
   602 (** print mode operations **)
   603 
   604 val no_output = ("", "");
   605 fun default_output (_: T) = no_output;
   606 
   607 local
   608   val default = {output = default_output};
   609   val modes = Synchronized.var "Markup.modes" (Symtab.make [("", default)]);
   610 in
   611   fun add_mode name output =
   612     Synchronized.change modes (fn tab =>
   613       (if not (Symtab.defined tab name) then ()
   614        else warning ("Redefining markup mode " ^ quote name);
   615        Symtab.update (name, {output = output}) tab));
   616   fun get_mode () =
   617     the_default default
   618       (Library.get_first (Symtab.lookup (Synchronized.value modes)) (print_mode_value ()));
   619 end;
   620 
   621 fun output m = if is_empty m then no_output else #output (get_mode ()) m;
   622 
   623 val enclose = output #-> Library.enclose;
   624 
   625 fun markup m =
   626   let val (bg, en) = output m
   627   in Library.enclose (Output.escape bg) (Output.escape en) end;
   628 
   629 fun markup_only m = markup m "";
   630 
   631 fun markup_report "" = ""
   632   | markup_report txt = markup report txt;
   633 
   634 end;