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