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