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