src/Pure/PIDE/markup.scala
author wenzelm
Sat Sep 01 20:20:50 2018 +0200 (9 months ago)
changeset 68871 f5c76072db55
parent 68822 253f04c1e814
child 68884 9b97d0b20d95
permissions -rw-r--r--
more explicit status for "canceled" command within theory node;
     1 /*  Title:      Pure/PIDE/markup.scala
     2     Author:     Makarius
     3 
     4 Quasi-abstract markup elements.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 object Markup
    11 {
    12   /* elements */
    13 
    14   object Elements
    15   {
    16     def apply(elems: Set[String]): Elements = new Elements(elems)
    17     def apply(elems: String*): Elements = apply(Set(elems: _*))
    18     val empty: Elements = apply()
    19     val full: Elements =
    20       new Elements(Set.empty)
    21       {
    22         override def apply(elem: String): Boolean = true
    23         override def toString: String = "Elements.full"
    24       }
    25   }
    26 
    27   sealed class Elements private[Markup](private val rep: Set[String])
    28   {
    29     def apply(elem: String): Boolean = rep.contains(elem)
    30     def + (elem: String): Elements = new Elements(rep + elem)
    31     def ++ (elems: Elements): Elements = new Elements(rep ++ elems.rep)
    32     def - (elem: String): Elements = new Elements(rep - elem)
    33     def -- (elems: Elements): Elements = new Elements(rep -- elems.rep)
    34     override def toString: String = rep.mkString("Elements(", ",", ")")
    35   }
    36 
    37 
    38   /* properties */
    39 
    40   val NAME = "name"
    41   val Name = new Properties.String(NAME)
    42 
    43   val KIND = "kind"
    44   val Kind = new Properties.String(KIND)
    45 
    46   val CONTENT = "content"
    47   val Content = new Properties.String(CONTENT)
    48 
    49   val SERIAL = "serial"
    50   val Serial = new Properties.Long(SERIAL)
    51 
    52   val INSTANCE = "instance"
    53   val Instance = new Properties.String(INSTANCE)
    54 
    55 
    56   /* basic markup */
    57 
    58   val Empty = Markup("", Nil)
    59   val Broken = Markup("broken", Nil)
    60 
    61   class Markup_String(val name: String, prop: String)
    62   {
    63     private val Prop = new Properties.String(prop)
    64 
    65     def apply(s: String): Markup = Markup(name, Prop(s))
    66     def unapply(markup: Markup): Option[String] =
    67       if (markup.name == name) Prop.unapply(markup.properties) else None
    68   }
    69 
    70   class Markup_Int(val name: String, prop: String)
    71   {
    72     private val Prop = new Properties.Int(prop)
    73 
    74     def apply(i: Int): Markup = Markup(name, Prop(i))
    75     def unapply(markup: Markup): Option[Int] =
    76       if (markup.name == name) Prop.unapply(markup.properties) else None
    77   }
    78 
    79   class Markup_Long(val name: String, prop: String)
    80   {
    81     private val Prop = new Properties.Long(prop)
    82 
    83     def apply(i: Long): Markup = Markup(name, Prop(i))
    84     def unapply(markup: Markup): Option[Long] =
    85       if (markup.name == name) Prop.unapply(markup.properties) else None
    86   }
    87 
    88 
    89   /* formal entities */
    90 
    91   val BINDING = "binding"
    92   val ENTITY = "entity"
    93 
    94   object Entity
    95   {
    96     val Def = new Properties.Long("def")
    97     val Ref = new Properties.Long("ref")
    98 
    99     def unapply(markup: Markup): Option[(String, String)] =
   100       markup match {
   101         case Markup(ENTITY, props) =>
   102           val kind = Kind.unapply(props).getOrElse("")
   103           val name = Name.unapply(props).getOrElse("")
   104           Some((kind, name))
   105         case _ => None
   106       }
   107   }
   108 
   109 
   110   /* completion */
   111 
   112   val COMPLETION = "completion"
   113   val NO_COMPLETION = "no_completion"
   114 
   115 
   116   /* position */
   117 
   118   val LINE = "line"
   119   val END_LINE = "line"
   120   val OFFSET = "offset"
   121   val END_OFFSET = "end_offset"
   122   val FILE = "file"
   123   val ID = "id"
   124 
   125   val DEF_LINE = "def_line"
   126   val DEF_OFFSET = "def_offset"
   127   val DEF_END_OFFSET = "def_end_offset"
   128   val DEF_FILE = "def_file"
   129   val DEF_ID = "def_id"
   130 
   131   val POSITION_PROPERTIES = Set(LINE, OFFSET, END_OFFSET, FILE, ID)
   132   val POSITION = "position"
   133 
   134 
   135   /* expression */
   136 
   137   val EXPRESSION = "expression"
   138   object Expression
   139   {
   140     def unapply(markup: Markup): Option[String] =
   141       markup match {
   142         case Markup(EXPRESSION, Kind(kind)) => Some(kind)
   143         case Markup(EXPRESSION, _) => Some("")
   144         case _ => None
   145       }
   146   }
   147 
   148 
   149   /* citation */
   150 
   151   val CITATION = "citation"
   152   val Citation = new Markup_String(CITATION, NAME)
   153 
   154 
   155   /* embedded languages */
   156 
   157   val Symbols = new Properties.Boolean("symbols")
   158   val Antiquotes = new Properties.Boolean("antiquotes")
   159   val Delimited = new Properties.Boolean("delimited")
   160 
   161   val LANGUAGE = "language"
   162   object Language
   163   {
   164     val DOCUMENT = "document"
   165     val ML = "ML"
   166     val SML = "SML"
   167     val PATH = "path"
   168     val UNKNOWN = "unknown"
   169 
   170     def unapply(markup: Markup): Option[(String, Boolean, Boolean, Boolean)] =
   171       markup match {
   172         case Markup(LANGUAGE, props) =>
   173           (props, props, props, props) match {
   174             case (Name(name), Symbols(symbols), Antiquotes(antiquotes), Delimited(delimited)) =>
   175               Some((name, symbols, antiquotes, delimited))
   176             case _ => None
   177           }
   178         case _ => None
   179       }
   180   }
   181 
   182 
   183   /* external resources */
   184 
   185   val PATH = "path"
   186   val Path = new Markup_String(PATH, NAME)
   187 
   188   val URL = "url"
   189   val Url = new Markup_String(URL, NAME)
   190 
   191   val DOC = "doc"
   192   val Doc = new Markup_String(DOC, NAME)
   193 
   194 
   195   /* pretty printing */
   196 
   197   val Consistent = new Properties.Boolean("consistent")
   198   val Indent = new Properties.Int("indent")
   199   val Width = new Properties.Int("width")
   200 
   201   object Block
   202   {
   203     val name = "block"
   204     def apply(c: Boolean, i: Int): Markup =
   205       Markup(name,
   206         (if (c) Consistent(c) else Nil) :::
   207         (if (i != 0) Indent(i) else Nil))
   208     def unapply(markup: Markup): Option[(Boolean, Int)] =
   209       if (markup.name == name) {
   210         val c = Consistent.unapply(markup.properties).getOrElse(false)
   211         val i = Indent.unapply(markup.properties).getOrElse(0)
   212         Some((c, i))
   213       }
   214       else None
   215   }
   216 
   217   object Break
   218   {
   219     val name = "break"
   220     def apply(w: Int, i: Int): Markup =
   221       Markup(name,
   222         (if (w != 0) Width(w) else Nil) :::
   223         (if (i != 0) Indent(i) else Nil))
   224     def unapply(markup: Markup): Option[(Int, Int)] =
   225       if (markup.name == name) {
   226         val w = Width.unapply(markup.properties).getOrElse(0)
   227         val i = Indent.unapply(markup.properties).getOrElse(0)
   228         Some((w, i))
   229       }
   230       else None
   231   }
   232 
   233   val ITEM = "item"
   234   val BULLET = "bullet"
   235 
   236   val SEPARATOR = "separator"
   237 
   238 
   239   /* text properties */
   240 
   241   val WORDS = "words"
   242 
   243   val HIDDEN = "hidden"
   244 
   245   val DELETE = "delete"
   246 
   247 
   248   /* misc entities */
   249 
   250   val CLASS = "class"
   251   val TYPE_NAME = "type_name"
   252   val FIXED = "fixed"
   253   val CASE = "case"
   254   val CONSTANT = "constant"
   255   val DYNAMIC_FACT = "dynamic_fact"
   256 
   257 
   258   /* inner syntax */
   259 
   260   val TFREE = "tfree"
   261   val TVAR = "tvar"
   262   val FREE = "free"
   263   val SKOLEM = "skolem"
   264   val BOUND = "bound"
   265   val VAR = "var"
   266   val NUMERAL = "numeral"
   267   val LITERAL = "literal"
   268   val DELIMITER = "delimiter"
   269   val INNER_STRING = "inner_string"
   270   val INNER_CARTOUCHE = "inner_cartouche"
   271   val INNER_COMMENT = "inner_comment"
   272 
   273   val TOKEN_RANGE = "token_range"
   274 
   275   val SORTING = "sorting"
   276   val TYPING = "typing"
   277   val CLASS_PARAMETER = "class_parameter"
   278 
   279   val ATTRIBUTE = "attribute"
   280   val METHOD = "method"
   281 
   282 
   283   /* antiquotations */
   284 
   285   val ANTIQUOTED = "antiquoted"
   286   val ANTIQUOTE = "antiquote"
   287 
   288   val ML_ANTIQUOTATION = "ML_antiquotation"
   289   val DOCUMENT_ANTIQUOTATION = "document_antiquotation"
   290   val DOCUMENT_ANTIQUOTATION_OPTION = "document_antiquotation_option"
   291 
   292 
   293   /* text structure */
   294 
   295   val PARAGRAPH = "paragraph"
   296   val TEXT_FOLD = "text_fold"
   297 
   298 
   299   /* Markdown document structure */
   300 
   301   val MARKDOWN_PARAGRAPH = "markdown_paragraph"
   302   val MARKDOWN_ITEM = "markdown_item"
   303   val Markdown_Bullet = new Markup_Int("markdown_bullet", "depth")
   304   val Markdown_List = new Markup_String("markdown_list", "kind")
   305 
   306   val ITEMIZE = "itemize"
   307   val ENUMERATE = "enumerate"
   308   val DESCRIPTION = "description"
   309 
   310 
   311   /* ML */
   312 
   313   val ML_KEYWORD1 = "ML_keyword1"
   314   val ML_KEYWORD2 = "ML_keyword2"
   315   val ML_KEYWORD3 = "ML_keyword3"
   316   val ML_DELIMITER = "ML_delimiter"
   317   val ML_TVAR = "ML_tvar"
   318   val ML_NUMERAL = "ML_numeral"
   319   val ML_CHAR = "ML_char"
   320   val ML_STRING = "ML_string"
   321   val ML_COMMENT = "ML_comment"
   322 
   323   val ML_DEF = "ML_def"
   324   val ML_OPEN = "ML_open"
   325   val ML_STRUCTURE = "ML_structure"
   326   val ML_TYPING = "ML_typing"
   327 
   328   val ML_BREAKPOINT = "ML_breakpoint"
   329 
   330 
   331   /* outer syntax */
   332 
   333   val COMMAND = "command"
   334   val KEYWORD = "keyword"
   335   val KEYWORD1 = "keyword1"
   336   val KEYWORD2 = "keyword2"
   337   val KEYWORD3 = "keyword3"
   338   val QUASI_KEYWORD = "quasi_keyword"
   339   val IMPROPER = "improper"
   340   val OPERATOR = "operator"
   341   val STRING = "string"
   342   val ALT_STRING = "alt_string"
   343   val VERBATIM = "verbatim"
   344   val CARTOUCHE = "cartouche"
   345   val COMMENT = "comment"
   346 
   347 
   348   /* timing */
   349 
   350   val Elapsed = new Properties.Double("elapsed")
   351   val CPU = new Properties.Double("cpu")
   352   val GC = new Properties.Double("gc")
   353 
   354   object Timing_Properties
   355   {
   356     def apply(timing: isabelle.Timing): Properties.T =
   357       Elapsed(timing.elapsed.seconds) ::: CPU(timing.cpu.seconds) ::: GC(timing.gc.seconds)
   358 
   359     def unapply(props: Properties.T): Option[isabelle.Timing] =
   360       (props, props, props) match {
   361         case (Elapsed(elapsed), CPU(cpu), GC(gc)) =>
   362           Some(new isabelle.Timing(Time.seconds(elapsed), Time.seconds(cpu), Time.seconds(gc)))
   363         case _ => None
   364       }
   365   }
   366 
   367   val TIMING = "timing"
   368 
   369   object Timing
   370   {
   371     def apply(timing: isabelle.Timing): Markup = Markup(TIMING, Timing_Properties(timing))
   372 
   373     def unapply(markup: Markup): Option[isabelle.Timing] =
   374       markup match {
   375         case Markup(TIMING, Timing_Properties(timing)) => Some(timing)
   376         case _ => None
   377       }
   378   }
   379 
   380 
   381   /* process result */
   382 
   383   val Return_Code = new Properties.Int("return_code")
   384 
   385   object Process_Result
   386   {
   387     def apply(result: Process_Result): Properties.T =
   388       Return_Code(result.rc) :::
   389         (if (result.timing.is_zero) Nil else Timing_Properties(result.timing))
   390 
   391     def unapply(props: Properties.T): Option[Process_Result] =
   392       props match {
   393         case Return_Code(rc) =>
   394           val timing = Timing_Properties.unapply(props).getOrElse(isabelle.Timing.zero)
   395           Some(isabelle.Process_Result(rc, timing = timing))
   396         case _ => None
   397       }
   398   }
   399 
   400 
   401   /* command indentation */
   402 
   403   object Command_Indent
   404   {
   405     val name = "command_indent"
   406     def unapply(markup: Markup): Option[Int] =
   407       if (markup.name == name) Indent.unapply(markup.properties) else None
   408   }
   409 
   410 
   411   /* goals */
   412 
   413   val GOAL = "goal"
   414   val SUBGOAL = "subgoal"
   415 
   416 
   417   /* command status */
   418 
   419   val TASK = "task"
   420 
   421   val ACCEPTED = "accepted"
   422   val FORKED = "forked"
   423   val JOINED = "joined"
   424   val RUNNING = "running"
   425   val FINISHED = "finished"
   426   val FAILED = "failed"
   427   val CANCELED = "canceled"
   428   val INITIALIZED = "initialized"
   429   val CONSOLIDATED = "consolidated"
   430 
   431 
   432   /* interactive documents */
   433 
   434   val VERSION = "version"
   435   val ASSIGN = "assign"
   436 
   437 
   438   /* prover process */
   439 
   440   val PROVER_COMMAND = "prover_command"
   441   val PROVER_ARG = "prover_arg"
   442 
   443 
   444   /* messages */
   445 
   446   val INIT = "init"
   447   val STATUS = "status"
   448   val REPORT = "report"
   449   val RESULT = "result"
   450   val WRITELN = "writeln"
   451   val STATE = "state"
   452   val INFORMATION = "information"
   453   val TRACING = "tracing"
   454   val WARNING = "warning"
   455   val LEGACY = "legacy"
   456   val ERROR = "error"
   457   val NODES_STATUS = "nodes_status"
   458   val PROTOCOL = "protocol"
   459   val SYSTEM = "system"
   460   val STDOUT = "stdout"
   461   val STDERR = "stderr"
   462   val EXIT = "exit"
   463 
   464   val WRITELN_MESSAGE = "writeln_message"
   465   val STATE_MESSAGE = "state_message"
   466   val INFORMATION_MESSAGE = "information_message"
   467   val TRACING_MESSAGE = "tracing_message"
   468   val WARNING_MESSAGE = "warning_message"
   469   val LEGACY_MESSAGE = "legacy_message"
   470   val ERROR_MESSAGE = "error_message"
   471 
   472   val messages = Map(
   473     WRITELN -> WRITELN_MESSAGE,
   474     STATE -> STATE_MESSAGE,
   475     INFORMATION -> INFORMATION_MESSAGE,
   476     TRACING -> TRACING_MESSAGE,
   477     WARNING -> WARNING_MESSAGE,
   478     LEGACY -> LEGACY_MESSAGE,
   479     ERROR -> ERROR_MESSAGE)
   480 
   481   val message: String => String = messages.withDefault((s: String) => s)
   482 
   483   val NO_REPORT = "no_report"
   484 
   485   val BAD = "bad"
   486 
   487   val INTENSIFY = "intensify"
   488 
   489 
   490   /* active areas */
   491 
   492   val BROWSER = "browser"
   493   val GRAPHVIEW = "graphview"
   494 
   495   val SENDBACK = "sendback"
   496   val PADDING = "padding"
   497   val PADDING_LINE = (PADDING, "line")
   498   val PADDING_COMMAND = (PADDING, "command")
   499 
   500   val DIALOG = "dialog"
   501   val Result = new Properties.String(RESULT)
   502 
   503   val JEDIT_ACTION = "jedit_action"
   504 
   505 
   506   /* protocol message functions */
   507 
   508   val FUNCTION = "function"
   509   val Function = new Properties.String(FUNCTION)
   510 
   511   val COMMAND_TIMING: Properties.Entry = (FUNCTION, "command_timing")
   512   val THEORY_TIMING: Properties.Entry = (FUNCTION, "theory_timing")
   513 
   514   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   515   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   516 
   517   object Protocol_Handler
   518   {
   519     def unapply(props: Properties.T): Option[(String)] =
   520       props match {
   521         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   522         case _ => None
   523       }
   524   }
   525 
   526   val INVOKE_SCALA = "invoke_scala"
   527   object Invoke_Scala
   528   {
   529     def unapply(props: Properties.T): Option[(String, String)] =
   530       props match {
   531         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   532         case _ => None
   533       }
   534   }
   535 
   536   val CANCEL_SCALA = "cancel_scala"
   537   object Cancel_Scala
   538   {
   539     def unapply(props: Properties.T): Option[String] =
   540       props match {
   541         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   542         case _ => None
   543       }
   544   }
   545 
   546   object ML_Statistics
   547   {
   548     def unapply(props: Properties.T): Option[Properties.T] =
   549       props match {
   550         case (FUNCTION, "ML_statistics") :: props => Some(props)
   551         case _ => None
   552       }
   553   }
   554 
   555   object Task_Statistics
   556   {
   557     def unapply(props: Properties.T): Option[Properties.T] =
   558       props match {
   559         case (FUNCTION, "task_statistics") :: props => Some(props)
   560         case _ => None
   561       }
   562   }
   563 
   564   val LOADING_THEORY = "loading_theory"
   565   object Loading_Theory
   566   {
   567     def unapply(props: Properties.T): Option[String] =
   568       props match {
   569         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   570         case _ => None
   571       }
   572   }
   573 
   574   val EXPORT = "export"
   575   object Export
   576   {
   577     sealed case class Args(
   578       id: Option[String], serial: Long, theory_name: String, name: String, compress: Boolean)
   579 
   580     val THEORY_NAME = "theory_name"
   581     val COMPRESS = "compress"
   582 
   583     def dest_inline(props: Properties.T): Option[(Args, Path)] =
   584       props match {
   585         case
   586           List(
   587             (SERIAL, Value.Long(serial)),
   588             (THEORY_NAME, theory_name),
   589             (NAME, name),
   590             (COMPRESS, Value.Boolean(compress)),
   591             (FILE, file)) if isabelle.Path.is_valid(file) =>
   592           Some((Args(None, serial, theory_name, name, compress), isabelle.Path.explode(file)))
   593         case _ => None
   594       }
   595 
   596     def unapply(props: Properties.T): Option[Args] =
   597       props match {
   598         case List(
   599             (FUNCTION, EXPORT),
   600             (ID, id),
   601             (SERIAL, Value.Long(serial)),
   602             (THEORY_NAME, theory_name),
   603             (NAME, name),
   604             (COMPRESS, Value.Boolean(compress))) =>
   605           Some(Args(proper_string(id), serial, theory_name, name, compress))
   606         case _ => None
   607       }
   608   }
   609 
   610   val BUILD_SESSION_FINISHED = "build_session_finished"
   611   val Build_Session_Finished: Properties.T = List((FUNCTION, BUILD_SESSION_FINISHED))
   612 
   613   val PRINT_OPERATIONS = "print_operations"
   614 
   615 
   616   /* debugger output */
   617 
   618   val DEBUGGER_STATE = "debugger_state"
   619   object Debugger_State
   620   {
   621     def unapply(props: Properties.T): Option[String] =
   622       props match {
   623         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   624         case _ => None
   625       }
   626   }
   627 
   628   val DEBUGGER_OUTPUT = "debugger_output"
   629   object Debugger_Output
   630   {
   631     def unapply(props: Properties.T): Option[String] =
   632       props match {
   633         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   634         case _ => None
   635       }
   636   }
   637 
   638 
   639   /* simplifier trace */
   640 
   641   val SIMP_TRACE_PANEL = "simp_trace_panel"
   642 
   643   val SIMP_TRACE_LOG = "simp_trace_log"
   644   val SIMP_TRACE_STEP = "simp_trace_step"
   645   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   646   val SIMP_TRACE_HINT = "simp_trace_hint"
   647   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   648 
   649   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   650   object Simp_Trace_Cancel
   651   {
   652     def unapply(props: Properties.T): Option[Long] =
   653       props match {
   654         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   655         case _ => None
   656       }
   657   }
   658 
   659 
   660   /* XML data representation */
   661 
   662   def encode: XML.Encode.T[Markup] = (markup: Markup) =>
   663   {
   664     import XML.Encode._
   665     pair(string, properties)((markup.name, markup.properties))
   666   }
   667 
   668   def decode: XML.Decode.T[Markup] = (body: XML.Body) =>
   669   {
   670     import XML.Decode._
   671     val (name, props) = pair(string, properties)(body)
   672     Markup(name, props)
   673   }
   674 }
   675 
   676 
   677 sealed case class Markup(name: String, properties: Properties.T)
   678 {
   679   def markup(s: String): String =
   680     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   681 
   682   def update_properties(more_props: Properties.T): Markup =
   683     if (more_props.isEmpty) this
   684     else Markup(name, (more_props :\ properties) { case (p, ps) => Properties.put(ps, p) })
   685 
   686   def + (entry: Properties.Entry): Markup =
   687     Markup(name, Properties.put(properties, entry))
   688 }