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