src/Pure/General/markup.ML
author wenzelm
Fri Nov 25 18:37:14 2011 +0100 (2011-11-25 ago)
changeset 45633 2cb7e34f6096
parent 45445 41e641a870de
child 45666 d83797ef0d2d
permissions -rw-r--r--
retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
     1 (*  Title:      Pure/General/markup.ML
     2     Author:     Makarius
     3 
     4 Common 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 classN: string
    39   val typeN: string
    40   val constantN: string
    41   val fixedN: string val fixed: string -> T
    42   val dynamic_factN: string val dynamic_fact: string -> T
    43   val tfreeN: string val tfree: T
    44   val tvarN: string val tvar: T
    45   val freeN: string val free: T
    46   val skolemN: string val skolem: T
    47   val boundN: string val bound: T
    48   val varN: string val var: T
    49   val numeralN: string val numeral: T
    50   val literalN: string val literal: T
    51   val delimiterN: string val delimiter: T
    52   val inner_stringN: string val inner_string: T
    53   val inner_commentN: string val inner_comment: T
    54   val token_rangeN: string val token_range: T
    55   val sortN: string val sort: T
    56   val typN: string val typ: T
    57   val termN: string val term: T
    58   val propN: string val prop: T
    59   val typingN: string val typing: T
    60   val ML_keywordN: string val ML_keyword: T
    61   val ML_delimiterN: string val ML_delimiter: T
    62   val ML_tvarN: string val ML_tvar: T
    63   val ML_numeralN: string val ML_numeral: T
    64   val ML_charN: string val ML_char: T
    65   val ML_stringN: string val ML_string: T
    66   val ML_commentN: string val ML_comment: T
    67   val ML_malformedN: string val ML_malformed: T
    68   val ML_defN: string
    69   val ML_openN: string
    70   val ML_structN: string
    71   val ML_typingN: string val ML_typing: T
    72   val ML_sourceN: string val ML_source: T
    73   val doc_sourceN: string val doc_source: T
    74   val antiqN: string val antiq: T
    75   val ML_antiquotationN: string
    76   val doc_antiquotationN: string
    77   val doc_antiquotation_optionN: string
    78   val keyword_declN: string val keyword_decl: string -> T
    79   val command_declN: string val command_decl: string -> string -> 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 malformedN: string val malformed: T
    89   val tokenN: string val token: Properties.T -> T
    90   val command_spanN: string val command_span: string -> T
    91   val ignored_spanN: string val ignored_span: T
    92   val malformed_spanN: string val malformed_span: T
    93   val loaded_theoryN: string val loaded_theory: string -> T
    94   val elapsedN: string
    95   val cpuN: string
    96   val gcN: string
    97   val timingN: string val timing: Timing.timing -> T
    98   val subgoalsN: string
    99   val proof_stateN: string val proof_state: int -> T
   100   val stateN: string val state: T
   101   val subgoalN: string val subgoal: T
   102   val sendbackN: string val sendback: T
   103   val hiliteN: string val hilite: T
   104   val taskN: string
   105   val forkedN: string val forked: T
   106   val joinedN: string val joined: T
   107   val failedN: string val failed: T
   108   val finishedN: string val finished: T
   109   val serialN: string
   110   val legacyN: string val legacy: T
   111   val promptN: string val prompt: T
   112   val readyN: string val ready: T
   113   val reportN: string val report: T
   114   val no_reportN: string val no_report: T
   115   val badN: string val bad: T
   116   val functionN: string
   117   val assign_execs: Properties.T
   118   val removed_versions: Properties.T
   119   val invoke_scala: string -> string -> Properties.T
   120   val cancel_scala: string -> Properties.T
   121   val no_output: Output.output * Output.output
   122   val default_output: T -> Output.output * Output.output
   123   val add_mode: string -> (T -> Output.output * Output.output) -> unit
   124   val output: T -> Output.output * Output.output
   125   val enclose: T -> Output.output -> Output.output
   126   val markup: T -> string -> string
   127   val markup_only: T -> string
   128 end;
   129 
   130 structure Markup: MARKUP =
   131 struct
   132 
   133 (** markup elements **)
   134 
   135 (* integers *)
   136 
   137 fun parse_int s =
   138   let val i = Int.fromString s in
   139     if is_none i orelse String.isPrefix "~" s
   140     then raise Fail ("Bad integer: " ^ quote s)
   141     else the i
   142   end;
   143 
   144 val print_int = signed_string_of_int;
   145 
   146 
   147 (* basic markup *)
   148 
   149 type T = string * Properties.T;
   150 
   151 val empty = ("", []);
   152 
   153 fun is_empty ("", _) = true
   154   | is_empty _ = false;
   155 
   156 
   157 fun properties more_props ((elem, props): T) =
   158   (elem, fold_rev Properties.put more_props props);
   159 
   160 fun markup_elem elem = (elem, (elem, []): T);
   161 fun markup_string elem prop = (elem, fn s => (elem, [(prop, s)]): T);
   162 fun markup_int elem prop = (elem, fn i => (elem, [(prop, print_int i)]): T);
   163 
   164 
   165 (* misc properties *)
   166 
   167 val nameN = "name";
   168 fun name a = properties [(nameN, a)];
   169 
   170 val kindN = "kind";
   171 
   172 
   173 (* formal entities *)
   174 
   175 val (bindingN, binding) = markup_elem "binding";
   176 
   177 val entityN = "entity";
   178 fun entity kind name = (entityN, [(nameN, name), (kindN, kind)]);
   179 
   180 fun get_entity_kind (name, props) =
   181   if name = entityN then AList.lookup (op =) props kindN
   182   else NONE;
   183 
   184 val defN = "def";
   185 val refN = "ref";
   186 
   187 
   188 (* position *)
   189 
   190 val lineN = "line";
   191 val offsetN = "offset";
   192 val end_offsetN = "end_offset";
   193 val fileN = "file";
   194 val idN = "id";
   195 
   196 val position_properties' = [fileN, idN];
   197 val position_properties = [lineN, offsetN, end_offsetN] @ position_properties';
   198 
   199 val (positionN, position) = markup_elem "position";
   200 
   201 
   202 (* path *)
   203 
   204 val (pathN, path) = markup_string "path" nameN;
   205 
   206 
   207 (* pretty printing *)
   208 
   209 val indentN = "indent";
   210 val (blockN, block) = markup_int "block" indentN;
   211 
   212 val widthN = "width";
   213 val (breakN, break) = markup_int "break" widthN;
   214 
   215 val (fbreakN, fbreak) = markup_elem "fbreak";
   216 
   217 
   218 (* hidden text *)
   219 
   220 val (hiddenN, hidden) = markup_elem "hidden";
   221 
   222 
   223 (* logical entities *)
   224 
   225 val classN = "class";
   226 val typeN = "type";
   227 val constantN = "constant";
   228 
   229 val (fixedN, fixed) = markup_string "fixed" nameN;
   230 val (dynamic_factN, dynamic_fact) = markup_string "dynamic_fact" nameN;
   231 
   232 
   233 (* inner syntax *)
   234 
   235 val (tfreeN, tfree) = markup_elem "tfree";
   236 val (tvarN, tvar) = markup_elem "tvar";
   237 val (freeN, free) = markup_elem "free";
   238 val (skolemN, skolem) = markup_elem "skolem";
   239 val (boundN, bound) = markup_elem "bound";
   240 val (varN, var) = markup_elem "var";
   241 val (numeralN, numeral) = markup_elem "numeral";
   242 val (literalN, literal) = markup_elem "literal";
   243 val (delimiterN, delimiter) = markup_elem "delimiter";
   244 val (inner_stringN, inner_string) = markup_elem "inner_string";
   245 val (inner_commentN, inner_comment) = markup_elem "inner_comment";
   246 
   247 val (token_rangeN, token_range) = markup_elem "token_range";
   248 
   249 val (sortN, sort) = markup_elem "sort";
   250 val (typN, typ) = markup_elem "typ";
   251 val (termN, term) = markup_elem "term";
   252 val (propN, prop) = markup_elem "prop";
   253 
   254 val (typingN, typing) = markup_elem "typing";
   255 
   256 
   257 (* ML syntax *)
   258 
   259 val (ML_keywordN, ML_keyword) = markup_elem "ML_keyword";
   260 val (ML_delimiterN, ML_delimiter) = markup_elem "ML_delimiter";
   261 val (ML_tvarN, ML_tvar) = markup_elem "ML_tvar";
   262 val (ML_numeralN, ML_numeral) = markup_elem "ML_numeral";
   263 val (ML_charN, ML_char) = markup_elem "ML_char";
   264 val (ML_stringN, ML_string) = markup_elem "ML_string";
   265 val (ML_commentN, ML_comment) = markup_elem "ML_comment";
   266 val (ML_malformedN, ML_malformed) = markup_elem "ML_malformed";
   267 
   268 val ML_defN = "ML_def";
   269 val ML_openN = "ML_open";
   270 val ML_structN = "ML_struct";
   271 val (ML_typingN, ML_typing) = markup_elem "ML_typing";
   272 
   273 
   274 (* embedded source text *)
   275 
   276 val (ML_sourceN, ML_source) = markup_elem "ML_source";
   277 val (doc_sourceN, doc_source) = markup_elem "doc_source";
   278 
   279 val (antiqN, antiq) = markup_elem "antiq";
   280 val ML_antiquotationN = "ML antiquotation";
   281 val doc_antiquotationN = "document antiquotation";
   282 val doc_antiquotation_optionN = "document antiquotation option";
   283 
   284 
   285 (* outer syntax *)
   286 
   287 val (keyword_declN, keyword_decl) = markup_string "keyword_decl" nameN;
   288 
   289 val command_declN = "command_decl";
   290 fun command_decl name kind : T = (command_declN, [(nameN, name), (kindN, kind)]);
   291 
   292 val (keywordN, keyword) = markup_elem "keyword";
   293 val (operatorN, operator) = markup_elem "operator";
   294 val (commandN, command) = markup_elem "command";
   295 val (stringN, string) = markup_elem "string";
   296 val (altstringN, altstring) = markup_elem "altstring";
   297 val (verbatimN, verbatim) = markup_elem "verbatim";
   298 val (commentN, comment) = markup_elem "comment";
   299 val (controlN, control) = markup_elem "control";
   300 val (malformedN, malformed) = markup_elem "malformed";
   301 
   302 val tokenN = "token";
   303 fun token props = (tokenN, props);
   304 
   305 val (command_spanN, command_span) = markup_string "command_span" nameN;
   306 val (ignored_spanN, ignored_span) = markup_elem "ignored_span";
   307 val (malformed_spanN, malformed_span) = markup_elem "malformed_span";
   308 
   309 
   310 (* theory loader *)
   311 
   312 val (loaded_theoryN, loaded_theory) = markup_string "loaded_theory" nameN;
   313 
   314 
   315 (* timing *)
   316 
   317 val timingN = "timing";
   318 val elapsedN = "elapsed";
   319 val cpuN = "cpu";
   320 val gcN = "gc";
   321 
   322 fun timing {elapsed, cpu, gc} =
   323   (timingN,
   324    [(elapsedN, Time.toString elapsed),
   325     (cpuN, Time.toString cpu),
   326     (gcN, Time.toString gc)]);
   327 
   328 
   329 (* toplevel *)
   330 
   331 val subgoalsN = "subgoals";
   332 val (proof_stateN, proof_state) = markup_int "proof_state" subgoalsN;
   333 
   334 val (stateN, state) = markup_elem "state";
   335 val (subgoalN, subgoal) = markup_elem "subgoal";
   336 val (sendbackN, sendback) = markup_elem "sendback";
   337 val (hiliteN, hilite) = markup_elem "hilite";
   338 
   339 
   340 (* command status *)
   341 
   342 val taskN = "task";
   343 
   344 val (forkedN, forked) = markup_elem "forked";
   345 val (joinedN, joined) = markup_elem "joined";
   346 
   347 val (failedN, failed) = markup_elem "failed";
   348 val (finishedN, finished) = markup_elem "finished";
   349 
   350 
   351 (* messages *)
   352 
   353 val serialN = "serial";
   354 
   355 val (legacyN, legacy) = markup_elem "legacy";
   356 val (promptN, prompt) = markup_elem "prompt";
   357 val (readyN, ready) = markup_elem "ready";
   358 
   359 val (reportN, report) = markup_elem "report";
   360 val (no_reportN, no_report) = markup_elem "no_report";
   361 
   362 val (badN, bad) = markup_elem "bad";
   363 
   364 
   365 (* raw message functions *)
   366 
   367 val functionN = "function"
   368 
   369 val assign_execs = [(functionN, "assign_execs")];
   370 val removed_versions = [(functionN, "removed_versions")];
   371 
   372 fun invoke_scala name id = [(functionN, "invoke_scala"), (nameN, name), (idN, id)];
   373 fun cancel_scala id = [(functionN, "cancel_scala"), (idN, id)];
   374 
   375 
   376 
   377 (** print mode operations **)
   378 
   379 val no_output = ("", "");
   380 fun default_output (_: T) = no_output;
   381 
   382 local
   383   val default = {output = default_output};
   384   val modes = Synchronized.var "Markup.modes" (Symtab.make [("", default)]);
   385 in
   386   fun add_mode name output =
   387     Synchronized.change modes (Symtab.update_new (name, {output = output}));
   388   fun get_mode () =
   389     the_default default
   390       (Library.get_first (Symtab.lookup (Synchronized.value modes)) (print_mode_value ()));
   391 end;
   392 
   393 fun output m = if is_empty m then no_output else #output (get_mode ()) m;
   394 
   395 val enclose = output #-> Library.enclose;
   396 
   397 fun markup m =
   398   let val (bg, en) = output m
   399   in Library.enclose (Output.escape bg) (Output.escape en) end;
   400 
   401 fun markup_only m = markup m "";
   402 
   403 end;