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