src/Pure/PIDE/markup.scala
author wenzelm
Mon Mar 12 16:32:33 2018 +0100 (15 months ago)
changeset 67839 0c2ed45ece20
parent 67336 3ee6da378183
child 67870 586be47e00b3
permissions -rw-r--r--
explicit Server.Context with output channels (concurrent write);
support for Logger and Progress;
     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 
   246   /* misc entities */
   247 
   248   val CLASS = "class"
   249   val TYPE_NAME = "type_name"
   250   val FIXED = "fixed"
   251   val CASE = "case"
   252   val CONSTANT = "constant"
   253   val DYNAMIC_FACT = "dynamic_fact"
   254 
   255 
   256   /* inner syntax */
   257 
   258   val TFREE = "tfree"
   259   val TVAR = "tvar"
   260   val FREE = "free"
   261   val SKOLEM = "skolem"
   262   val BOUND = "bound"
   263   val VAR = "var"
   264   val NUMERAL = "numeral"
   265   val LITERAL = "literal"
   266   val DELIMITER = "delimiter"
   267   val INNER_STRING = "inner_string"
   268   val INNER_CARTOUCHE = "inner_cartouche"
   269   val INNER_COMMENT = "inner_comment"
   270 
   271   val TOKEN_RANGE = "token_range"
   272 
   273   val SORTING = "sorting"
   274   val TYPING = "typing"
   275   val CLASS_PARAMETER = "class_parameter"
   276 
   277   val ATTRIBUTE = "attribute"
   278   val METHOD = "method"
   279 
   280 
   281   /* antiquotations */
   282 
   283   val ANTIQUOTED = "antiquoted"
   284   val ANTIQUOTE = "antiquote"
   285 
   286   val ML_ANTIQUOTATION = "ML_antiquotation"
   287   val DOCUMENT_ANTIQUOTATION = "document_antiquotation"
   288   val DOCUMENT_ANTIQUOTATION_OPTION = "document_antiquotation_option"
   289 
   290 
   291   /* text structure */
   292 
   293   val PARAGRAPH = "paragraph"
   294   val TEXT_FOLD = "text_fold"
   295 
   296 
   297   /* Markdown document structure */
   298 
   299   val MARKDOWN_PARAGRAPH = "markdown_paragraph"
   300   val MARKDOWN_ITEM = "markdown_item"
   301   val Markdown_Bullet = new Markup_Int("markdown_bullet", "depth")
   302   val Markdown_List = new Markup_String("markdown_list", "kind")
   303 
   304   val ITEMIZE = "itemize"
   305   val ENUMERATE = "enumerate"
   306   val DESCRIPTION = "description"
   307 
   308 
   309   /* ML */
   310 
   311   val ML_KEYWORD1 = "ML_keyword1"
   312   val ML_KEYWORD2 = "ML_keyword2"
   313   val ML_KEYWORD3 = "ML_keyword3"
   314   val ML_DELIMITER = "ML_delimiter"
   315   val ML_TVAR = "ML_tvar"
   316   val ML_NUMERAL = "ML_numeral"
   317   val ML_CHAR = "ML_char"
   318   val ML_STRING = "ML_string"
   319   val ML_COMMENT = "ML_comment"
   320   val SML_STRING = "SML_string"
   321   val SML_COMMENT = "SML_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 CONSOLIDATED = "consolidated"
   428 
   429 
   430   /* interactive documents */
   431 
   432   val VERSION = "version"
   433   val ASSIGN = "assign"
   434 
   435 
   436   /* prover process */
   437 
   438   val PROVER_COMMAND = "prover_command"
   439   val PROVER_ARG = "prover_arg"
   440 
   441 
   442   /* messages */
   443 
   444   val INIT = "init"
   445   val STATUS = "status"
   446   val REPORT = "report"
   447   val RESULT = "result"
   448   val WRITELN = "writeln"
   449   val STATE = "state"
   450   val INFORMATION = "information"
   451   val TRACING = "tracing"
   452   val WARNING = "warning"
   453   val LEGACY = "legacy"
   454   val ERROR = "error"
   455   val PROTOCOL = "protocol"
   456   val SYSTEM = "system"
   457   val STDOUT = "stdout"
   458   val STDERR = "stderr"
   459   val EXIT = "exit"
   460   val LOGGER = "logger"
   461 
   462   val WRITELN_MESSAGE = "writeln_message"
   463   val STATE_MESSAGE = "state_message"
   464   val INFORMATION_MESSAGE = "information_message"
   465   val TRACING_MESSAGE = "tracing_message"
   466   val WARNING_MESSAGE = "warning_message"
   467   val LEGACY_MESSAGE = "legacy_message"
   468   val ERROR_MESSAGE = "error_message"
   469 
   470   val messages = Map(
   471     WRITELN -> WRITELN_MESSAGE,
   472     STATE -> STATE_MESSAGE,
   473     INFORMATION -> INFORMATION_MESSAGE,
   474     TRACING -> TRACING_MESSAGE,
   475     WARNING -> WARNING_MESSAGE,
   476     LEGACY -> LEGACY_MESSAGE,
   477     ERROR -> ERROR_MESSAGE)
   478 
   479   val message: String => String = messages.withDefault((s: String) => s)
   480 
   481   val NO_REPORT = "no_report"
   482 
   483   val BAD = "bad"
   484 
   485   val INTENSIFY = "intensify"
   486 
   487 
   488   /* active areas */
   489 
   490   val BROWSER = "browser"
   491   val GRAPHVIEW = "graphview"
   492 
   493   val SENDBACK = "sendback"
   494   val PADDING = "padding"
   495   val PADDING_LINE = (PADDING, "line")
   496   val PADDING_COMMAND = (PADDING, "command")
   497 
   498   val DIALOG = "dialog"
   499   val Result = new Properties.String(RESULT)
   500 
   501   val JEDIT_ACTION = "jedit_action"
   502 
   503 
   504   /* protocol message functions */
   505 
   506   val FUNCTION = "function"
   507   val Function = new Properties.String(FUNCTION)
   508 
   509   val COMMAND_TIMING: Properties.Entry = (FUNCTION, "command_timing")
   510   val THEORY_TIMING: Properties.Entry = (FUNCTION, "theory_timing")
   511 
   512   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   513   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   514 
   515   object Protocol_Handler
   516   {
   517     def unapply(props: Properties.T): Option[(String)] =
   518       props match {
   519         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   520         case _ => None
   521       }
   522   }
   523 
   524   val INVOKE_SCALA = "invoke_scala"
   525   object Invoke_Scala
   526   {
   527     def unapply(props: Properties.T): Option[(String, String)] =
   528       props match {
   529         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   530         case _ => None
   531       }
   532   }
   533 
   534   val CANCEL_SCALA = "cancel_scala"
   535   object Cancel_Scala
   536   {
   537     def unapply(props: Properties.T): Option[String] =
   538       props match {
   539         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   540         case _ => None
   541       }
   542   }
   543 
   544   object ML_Statistics
   545   {
   546     def unapply(props: Properties.T): Option[Properties.T] =
   547       props match {
   548         case (FUNCTION, "ML_statistics") :: props => Some(props)
   549         case _ => None
   550       }
   551   }
   552 
   553   object Task_Statistics
   554   {
   555     def unapply(props: Properties.T): Option[Properties.T] =
   556       props match {
   557         case (FUNCTION, "task_statistics") :: props => Some(props)
   558         case _ => None
   559       }
   560   }
   561 
   562   val LOADING_THEORY = "loading_theory"
   563   object Loading_Theory
   564   {
   565     def unapply(props: Properties.T): Option[String] =
   566       props match {
   567         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   568         case _ => None
   569       }
   570   }
   571 
   572   val BUILD_SESSION_FINISHED = "build_session_finished"
   573   val Build_Session_Finished: Properties.T = List((FUNCTION, BUILD_SESSION_FINISHED))
   574 
   575   val PRINT_OPERATIONS = "print_operations"
   576 
   577 
   578   /* debugger output */
   579 
   580   val DEBUGGER_STATE = "debugger_state"
   581   object Debugger_State
   582   {
   583     def unapply(props: Properties.T): Option[String] =
   584       props match {
   585         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   586         case _ => None
   587       }
   588   }
   589 
   590   val DEBUGGER_OUTPUT = "debugger_output"
   591   object Debugger_Output
   592   {
   593     def unapply(props: Properties.T): Option[String] =
   594       props match {
   595         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   596         case _ => None
   597       }
   598   }
   599 
   600 
   601   /* simplifier trace */
   602 
   603   val SIMP_TRACE_PANEL = "simp_trace_panel"
   604 
   605   val SIMP_TRACE_LOG = "simp_trace_log"
   606   val SIMP_TRACE_STEP = "simp_trace_step"
   607   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   608   val SIMP_TRACE_HINT = "simp_trace_hint"
   609   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   610 
   611   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   612   object Simp_Trace_Cancel
   613   {
   614     def unapply(props: Properties.T): Option[Long] =
   615       props match {
   616         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   617         case _ => None
   618       }
   619   }
   620 
   621 
   622   /* XML data representation */
   623 
   624   def encode: XML.Encode.T[Markup] = (markup: Markup) =>
   625   {
   626     import XML.Encode._
   627     pair(string, properties)((markup.name, markup.properties))
   628   }
   629 
   630   def decode: XML.Decode.T[Markup] = (body: XML.Body) =>
   631   {
   632     import XML.Decode._
   633     val (name, props) = pair(string, properties)(body)
   634     Markup(name, props)
   635   }
   636 }
   637 
   638 
   639 sealed case class Markup(name: String, properties: Properties.T)
   640 {
   641   def markup(s: String): String =
   642     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   643 
   644   def update_properties(more_props: Properties.T): Markup =
   645     if (more_props.isEmpty) this
   646     else Markup(name, (more_props :\ properties) { case (p, ps) => Properties.put(ps, p) })
   647 
   648   def + (entry: Properties.Entry): Markup =
   649     Markup(name, Properties.put(properties, entry))
   650 }