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