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