src/Pure/PIDE/markup.scala
author wenzelm
Sat Mar 18 20:51:42 2017 +0100 (2017-03-18)
changeset 65313 347ed6219dab
parent 65176 908d8be90533
child 65317 b9f5cd845616
permissions -rw-r--r--
more realistic PIDE build session;
     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   /* command timing */
   372 
   373   val COMMAND_TIMING = "command_timing"
   374 
   375 
   376   /* command indentation */
   377 
   378   object Command_Indent
   379   {
   380     val name = "command_indent"
   381     def unapply(markup: Markup): Option[Int] =
   382       if (markup.name == name) Indent.unapply(markup.properties) else None
   383   }
   384 
   385 
   386   /* goals */
   387 
   388   val GOAL = "goal"
   389   val SUBGOAL = "subgoal"
   390 
   391 
   392   /* command status */
   393 
   394   val TASK = "task"
   395 
   396   val ACCEPTED = "accepted"
   397   val FORKED = "forked"
   398   val JOINED = "joined"
   399   val RUNNING = "running"
   400   val FINISHED = "finished"
   401   val FAILED = "failed"
   402 
   403 
   404   /* interactive documents */
   405 
   406   val VERSION = "version"
   407   val ASSIGN = "assign"
   408 
   409 
   410   /* prover process */
   411 
   412   val PROVER_COMMAND = "prover_command"
   413   val PROVER_ARG = "prover_arg"
   414 
   415 
   416   /* messages */
   417 
   418   val INIT = "init"
   419   val STATUS = "status"
   420   val REPORT = "report"
   421   val RESULT = "result"
   422   val WRITELN = "writeln"
   423   val STATE = "state"
   424   val INFORMATION = "information"
   425   val TRACING = "tracing"
   426   val WARNING = "warning"
   427   val LEGACY = "legacy"
   428   val ERROR = "error"
   429   val PROTOCOL = "protocol"
   430   val SYSTEM = "system"
   431   val STDOUT = "stdout"
   432   val STDERR = "stderr"
   433   val EXIT = "exit"
   434 
   435   val WRITELN_MESSAGE = "writeln_message"
   436   val STATE_MESSAGE = "state_message"
   437   val INFORMATION_MESSAGE = "information_message"
   438   val TRACING_MESSAGE = "tracing_message"
   439   val WARNING_MESSAGE = "warning_message"
   440   val LEGACY_MESSAGE = "legacy_message"
   441   val ERROR_MESSAGE = "error_message"
   442 
   443   val messages = Map(
   444     WRITELN -> WRITELN_MESSAGE,
   445     STATE -> STATE_MESSAGE,
   446     INFORMATION -> INFORMATION_MESSAGE,
   447     TRACING -> TRACING_MESSAGE,
   448     WARNING -> WARNING_MESSAGE,
   449     LEGACY -> LEGACY_MESSAGE,
   450     ERROR -> ERROR_MESSAGE)
   451 
   452   val message: String => String = messages.withDefault((s: String) => s)
   453 
   454   val Return_Code = new Properties.Int("return_code")
   455 
   456   val NO_REPORT = "no_report"
   457 
   458   val BAD = "bad"
   459 
   460   val INTENSIFY = "intensify"
   461 
   462 
   463   /* active areas */
   464 
   465   val BROWSER = "browser"
   466   val GRAPHVIEW = "graphview"
   467 
   468   val SENDBACK = "sendback"
   469   val PADDING = "padding"
   470   val PADDING_LINE = (PADDING, "line")
   471   val PADDING_COMMAND = (PADDING, "command")
   472 
   473   val DIALOG = "dialog"
   474   val Result = new Properties.String(RESULT)
   475 
   476   val JEDIT_ACTION = "jedit_action"
   477 
   478 
   479   /* protocol message functions */
   480 
   481   val FUNCTION = "function"
   482   val Function = new Properties.String(FUNCTION)
   483 
   484   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   485   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   486 
   487   object Protocol_Handler
   488   {
   489     def unapply(props: Properties.T): Option[(String)] =
   490       props match {
   491         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   492         case _ => None
   493       }
   494   }
   495 
   496   val INVOKE_SCALA = "invoke_scala"
   497   object Invoke_Scala
   498   {
   499     def unapply(props: Properties.T): Option[(String, String)] =
   500       props match {
   501         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   502         case _ => None
   503       }
   504   }
   505 
   506   val CANCEL_SCALA = "cancel_scala"
   507   object Cancel_Scala
   508   {
   509     def unapply(props: Properties.T): Option[String] =
   510       props match {
   511         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   512         case _ => None
   513       }
   514   }
   515 
   516   object ML_Statistics
   517   {
   518     def unapply(props: Properties.T): Option[Properties.T] =
   519       props match {
   520         case (FUNCTION, "ML_statistics") :: stats => Some(stats)
   521         case _ => None
   522       }
   523   }
   524 
   525   object Task_Statistics
   526   {
   527     def unapply(props: Properties.T): Option[Properties.T] =
   528       props match {
   529         case (FUNCTION, "task_statistics") :: stats => Some(stats)
   530         case _ => None
   531       }
   532   }
   533 
   534   val LOADING_THEORY = "loading_theory"
   535   object Loading_Theory
   536   {
   537     def unapply(props: Properties.T): Option[String] =
   538       props match {
   539         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   540         case _ => None
   541       }
   542   }
   543 
   544   val BUILD_SESSION_FINISHED = "build_session_finished"
   545   val Build_Session_Finished: Properties.T = List((FUNCTION, BUILD_SESSION_FINISHED))
   546 
   547   val PRINT_OPERATIONS = "print_operations"
   548 
   549 
   550   /* debugger output */
   551 
   552   val DEBUGGER_STATE = "debugger_state"
   553   object Debugger_State
   554   {
   555     def unapply(props: Properties.T): Option[String] =
   556       props match {
   557         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   558         case _ => None
   559       }
   560   }
   561 
   562   val DEBUGGER_OUTPUT = "debugger_output"
   563   object Debugger_Output
   564   {
   565     def unapply(props: Properties.T): Option[String] =
   566       props match {
   567         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   568         case _ => None
   569       }
   570   }
   571 
   572 
   573   /* simplifier trace */
   574 
   575   val SIMP_TRACE_PANEL = "simp_trace_panel"
   576 
   577   val SIMP_TRACE_LOG = "simp_trace_log"
   578   val SIMP_TRACE_STEP = "simp_trace_step"
   579   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   580   val SIMP_TRACE_HINT = "simp_trace_hint"
   581   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   582 
   583   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   584   object Simp_Trace_Cancel
   585   {
   586     def unapply(props: Properties.T): Option[Long] =
   587       props match {
   588         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   589         case _ => None
   590       }
   591   }
   592 }
   593 
   594 
   595 sealed case class Markup(name: String, properties: Properties.T)
   596 {
   597   def markup(s: String): String =
   598     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   599 
   600   def update_properties(more_props: Properties.T): Markup =
   601     if (more_props.isEmpty) this
   602     else Markup(name, (more_props :\ properties) { case (p, ps) => Properties.put(ps, p) })
   603 }