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