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