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