src/Pure/PIDE/markup.scala
author wenzelm
Tue Aug 08 22:13:05 2017 +0200 (22 months ago)
changeset 66379 6392766f3c25
parent 66044 bd7516709051
child 66872 69afe45a6062
permissions -rw-r--r--
maintain "consolidated" status of theory nodes, which means all evals are finished (but not necessarily prints nor imports);
     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 ML = "ML"
   165     val SML = "SML"
   166     val PATH = "path"
   167     val UNKNOWN = "unknown"
   168 
   169     def unapply(markup: Markup): Option[(String, Boolean, Boolean, Boolean)] =
   170       markup match {
   171         case Markup(LANGUAGE, props) =>
   172           (props, props, props, props) match {
   173             case (Name(name), Symbols(symbols), Antiquotes(antiquotes), Delimited(delimited)) =>
   174               Some((name, symbols, antiquotes, delimited))
   175             case _ => None
   176           }
   177         case _ => None
   178       }
   179   }
   180 
   181 
   182   /* external resources */
   183 
   184   val PATH = "path"
   185   val Path = new Markup_String(PATH, NAME)
   186 
   187   val URL = "url"
   188   val Url = new Markup_String(URL, NAME)
   189 
   190   val DOC = "doc"
   191   val Doc = new Markup_String(DOC, NAME)
   192 
   193 
   194   /* pretty printing */
   195 
   196   val Consistent = new Properties.Boolean("consistent")
   197   val Indent = new Properties.Int("indent")
   198   val Width = new Properties.Int("width")
   199 
   200   object Block
   201   {
   202     val name = "block"
   203     def apply(c: Boolean, i: Int): Markup =
   204       Markup(name,
   205         (if (c) Consistent(c) else Nil) :::
   206         (if (i != 0) Indent(i) else Nil))
   207     def unapply(markup: Markup): Option[(Boolean, Int)] =
   208       if (markup.name == name) {
   209         val c = Consistent.unapply(markup.properties).getOrElse(false)
   210         val i = Indent.unapply(markup.properties).getOrElse(0)
   211         Some((c, i))
   212       }
   213       else None
   214   }
   215 
   216   object Break
   217   {
   218     val name = "break"
   219     def apply(w: Int, i: Int): Markup =
   220       Markup(name,
   221         (if (w != 0) Width(w) else Nil) :::
   222         (if (i != 0) Indent(i) else Nil))
   223     def unapply(markup: Markup): Option[(Int, Int)] =
   224       if (markup.name == name) {
   225         val w = Width.unapply(markup.properties).getOrElse(0)
   226         val i = Indent.unapply(markup.properties).getOrElse(0)
   227         Some((w, i))
   228       }
   229       else None
   230   }
   231 
   232   val ITEM = "item"
   233   val BULLET = "bullet"
   234 
   235   val SEPARATOR = "separator"
   236 
   237 
   238   /* text properties */
   239 
   240   val WORDS = "words"
   241 
   242   val HIDDEN = "hidden"
   243 
   244 
   245   /* misc entities */
   246 
   247   val CLASS = "class"
   248   val TYPE_NAME = "type_name"
   249   val FIXED = "fixed"
   250   val CASE = "case"
   251   val CONSTANT = "constant"
   252   val DYNAMIC_FACT = "dynamic_fact"
   253 
   254 
   255   /* inner syntax */
   256 
   257   val TFREE = "tfree"
   258   val TVAR = "tvar"
   259   val FREE = "free"
   260   val SKOLEM = "skolem"
   261   val BOUND = "bound"
   262   val VAR = "var"
   263   val NUMERAL = "numeral"
   264   val LITERAL = "literal"
   265   val DELIMITER = "delimiter"
   266   val INNER_STRING = "inner_string"
   267   val INNER_CARTOUCHE = "inner_cartouche"
   268   val INNER_COMMENT = "inner_comment"
   269 
   270   val TOKEN_RANGE = "token_range"
   271 
   272   val SORTING = "sorting"
   273   val TYPING = "typing"
   274   val CLASS_PARAMETER = "class_parameter"
   275 
   276   val ATTRIBUTE = "attribute"
   277   val METHOD = "method"
   278 
   279 
   280   /* antiquotations */
   281 
   282   val ANTIQUOTED = "antiquoted"
   283   val ANTIQUOTE = "antiquote"
   284 
   285   val ML_ANTIQUOTATION = "ML_antiquotation"
   286   val DOCUMENT_ANTIQUOTATION = "document_antiquotation"
   287   val DOCUMENT_ANTIQUOTATION_OPTION = "document_antiquotation_option"
   288 
   289 
   290   /* text structure */
   291 
   292   val PARAGRAPH = "paragraph"
   293   val TEXT_FOLD = "text_fold"
   294 
   295 
   296   /* Markdown document structure */
   297 
   298   val MARKDOWN_PARAGRAPH = "markdown_paragraph"
   299   val Markdown_List = new Markup_String("markdown_list", "kind")
   300   val Markdown_Item = new Markup_Int("markdown_item", "depth")
   301 
   302 
   303   /* ML */
   304 
   305   val ML_KEYWORD1 = "ML_keyword1"
   306   val ML_KEYWORD2 = "ML_keyword2"
   307   val ML_KEYWORD3 = "ML_keyword3"
   308   val ML_DELIMITER = "ML_delimiter"
   309   val ML_TVAR = "ML_tvar"
   310   val ML_NUMERAL = "ML_numeral"
   311   val ML_CHAR = "ML_char"
   312   val ML_STRING = "ML_string"
   313   val ML_COMMENT = "ML_comment"
   314   val SML_STRING = "SML_string"
   315   val SML_COMMENT = "SML_comment"
   316 
   317   val ML_DEF = "ML_def"
   318   val ML_OPEN = "ML_open"
   319   val ML_STRUCTURE = "ML_structure"
   320   val ML_TYPING = "ML_typing"
   321 
   322   val ML_BREAKPOINT = "ML_breakpoint"
   323 
   324 
   325   /* outer syntax */
   326 
   327   val COMMAND = "command"
   328   val KEYWORD = "keyword"
   329   val KEYWORD1 = "keyword1"
   330   val KEYWORD2 = "keyword2"
   331   val KEYWORD3 = "keyword3"
   332   val QUASI_KEYWORD = "quasi_keyword"
   333   val IMPROPER = "improper"
   334   val OPERATOR = "operator"
   335   val STRING = "string"
   336   val ALT_STRING = "alt_string"
   337   val VERBATIM = "verbatim"
   338   val CARTOUCHE = "cartouche"
   339   val COMMENT = "comment"
   340 
   341 
   342   /* timing */
   343 
   344   val Elapsed = new Properties.Double("elapsed")
   345   val CPU = new Properties.Double("cpu")
   346   val GC = new Properties.Double("gc")
   347 
   348   object Timing_Properties
   349   {
   350     def apply(timing: isabelle.Timing): Properties.T =
   351       Elapsed(timing.elapsed.seconds) ::: CPU(timing.cpu.seconds) ::: GC(timing.gc.seconds)
   352 
   353     def unapply(props: Properties.T): Option[isabelle.Timing] =
   354       (props, props, props) match {
   355         case (Elapsed(elapsed), CPU(cpu), GC(gc)) =>
   356           Some(new isabelle.Timing(Time.seconds(elapsed), Time.seconds(cpu), Time.seconds(gc)))
   357         case _ => None
   358       }
   359   }
   360 
   361   val TIMING = "timing"
   362 
   363   object Timing
   364   {
   365     def apply(timing: isabelle.Timing): Markup = Markup(TIMING, Timing_Properties(timing))
   366 
   367     def unapply(markup: Markup): Option[isabelle.Timing] =
   368       markup match {
   369         case Markup(TIMING, Timing_Properties(timing)) => Some(timing)
   370         case _ => None
   371       }
   372   }
   373 
   374 
   375   /* process result */
   376 
   377   val Return_Code = new Properties.Int("return_code")
   378 
   379   object Process_Result
   380   {
   381     def apply(result: Process_Result): Properties.T =
   382       Return_Code(result.rc) :::
   383         (if (result.timing.is_zero) Nil else Timing_Properties(result.timing))
   384 
   385     def unapply(props: Properties.T): Option[Process_Result] =
   386       props match {
   387         case Return_Code(rc) =>
   388           val timing = Timing_Properties.unapply(props).getOrElse(isabelle.Timing.zero)
   389           Some(isabelle.Process_Result(rc, timing = timing))
   390         case _ => None
   391       }
   392   }
   393 
   394 
   395   /* command timing */
   396 
   397   val COMMAND_TIMING = "command_timing"
   398 
   399 
   400   /* command indentation */
   401 
   402   object Command_Indent
   403   {
   404     val name = "command_indent"
   405     def unapply(markup: Markup): Option[Int] =
   406       if (markup.name == name) Indent.unapply(markup.properties) else None
   407   }
   408 
   409 
   410   /* goals */
   411 
   412   val GOAL = "goal"
   413   val SUBGOAL = "subgoal"
   414 
   415 
   416   /* command status */
   417 
   418   val TASK = "task"
   419 
   420   val ACCEPTED = "accepted"
   421   val FORKED = "forked"
   422   val JOINED = "joined"
   423   val RUNNING = "running"
   424   val FINISHED = "finished"
   425   val FAILED = "failed"
   426   val CONSOLIDATED = "consolidated"
   427 
   428 
   429   /* interactive documents */
   430 
   431   val VERSION = "version"
   432   val ASSIGN = "assign"
   433 
   434 
   435   /* prover process */
   436 
   437   val PROVER_COMMAND = "prover_command"
   438   val PROVER_ARG = "prover_arg"
   439 
   440 
   441   /* messages */
   442 
   443   val INIT = "init"
   444   val STATUS = "status"
   445   val REPORT = "report"
   446   val RESULT = "result"
   447   val WRITELN = "writeln"
   448   val STATE = "state"
   449   val INFORMATION = "information"
   450   val TRACING = "tracing"
   451   val WARNING = "warning"
   452   val LEGACY = "legacy"
   453   val ERROR = "error"
   454   val PROTOCOL = "protocol"
   455   val SYSTEM = "system"
   456   val STDOUT = "stdout"
   457   val STDERR = "stderr"
   458   val EXIT = "exit"
   459 
   460   val WRITELN_MESSAGE = "writeln_message"
   461   val STATE_MESSAGE = "state_message"
   462   val INFORMATION_MESSAGE = "information_message"
   463   val TRACING_MESSAGE = "tracing_message"
   464   val WARNING_MESSAGE = "warning_message"
   465   val LEGACY_MESSAGE = "legacy_message"
   466   val ERROR_MESSAGE = "error_message"
   467 
   468   val messages = Map(
   469     WRITELN -> WRITELN_MESSAGE,
   470     STATE -> STATE_MESSAGE,
   471     INFORMATION -> INFORMATION_MESSAGE,
   472     TRACING -> TRACING_MESSAGE,
   473     WARNING -> WARNING_MESSAGE,
   474     LEGACY -> LEGACY_MESSAGE,
   475     ERROR -> ERROR_MESSAGE)
   476 
   477   val message: String => String = messages.withDefault((s: String) => s)
   478 
   479   val NO_REPORT = "no_report"
   480 
   481   val BAD = "bad"
   482 
   483   val INTENSIFY = "intensify"
   484 
   485 
   486   /* active areas */
   487 
   488   val BROWSER = "browser"
   489   val GRAPHVIEW = "graphview"
   490 
   491   val SENDBACK = "sendback"
   492   val PADDING = "padding"
   493   val PADDING_LINE = (PADDING, "line")
   494   val PADDING_COMMAND = (PADDING, "command")
   495 
   496   val DIALOG = "dialog"
   497   val Result = new Properties.String(RESULT)
   498 
   499   val JEDIT_ACTION = "jedit_action"
   500 
   501 
   502   /* protocol message functions */
   503 
   504   val FUNCTION = "function"
   505   val Function = new Properties.String(FUNCTION)
   506 
   507   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   508   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   509 
   510   object Protocol_Handler
   511   {
   512     def unapply(props: Properties.T): Option[(String)] =
   513       props match {
   514         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   515         case _ => None
   516       }
   517   }
   518 
   519   val INVOKE_SCALA = "invoke_scala"
   520   object Invoke_Scala
   521   {
   522     def unapply(props: Properties.T): Option[(String, String)] =
   523       props match {
   524         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   525         case _ => None
   526       }
   527   }
   528 
   529   val CANCEL_SCALA = "cancel_scala"
   530   object Cancel_Scala
   531   {
   532     def unapply(props: Properties.T): Option[String] =
   533       props match {
   534         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   535         case _ => None
   536       }
   537   }
   538 
   539   object ML_Statistics
   540   {
   541     def unapply(props: Properties.T): Option[Properties.T] =
   542       props match {
   543         case (FUNCTION, "ML_statistics") :: stats => Some(stats)
   544         case _ => None
   545       }
   546   }
   547 
   548   object Task_Statistics
   549   {
   550     def unapply(props: Properties.T): Option[Properties.T] =
   551       props match {
   552         case (FUNCTION, "task_statistics") :: stats => Some(stats)
   553         case _ => None
   554       }
   555   }
   556 
   557   val LOADING_THEORY = "loading_theory"
   558   object Loading_Theory
   559   {
   560     def unapply(props: Properties.T): Option[String] =
   561       props match {
   562         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   563         case _ => None
   564       }
   565   }
   566 
   567   val BUILD_SESSION_FINISHED = "build_session_finished"
   568   val Build_Session_Finished: Properties.T = List((FUNCTION, BUILD_SESSION_FINISHED))
   569 
   570   val PRINT_OPERATIONS = "print_operations"
   571 
   572 
   573   /* debugger output */
   574 
   575   val DEBUGGER_STATE = "debugger_state"
   576   object Debugger_State
   577   {
   578     def unapply(props: Properties.T): Option[String] =
   579       props match {
   580         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   581         case _ => None
   582       }
   583   }
   584 
   585   val DEBUGGER_OUTPUT = "debugger_output"
   586   object Debugger_Output
   587   {
   588     def unapply(props: Properties.T): Option[String] =
   589       props match {
   590         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   591         case _ => None
   592       }
   593   }
   594 
   595 
   596   /* simplifier trace */
   597 
   598   val SIMP_TRACE_PANEL = "simp_trace_panel"
   599 
   600   val SIMP_TRACE_LOG = "simp_trace_log"
   601   val SIMP_TRACE_STEP = "simp_trace_step"
   602   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   603   val SIMP_TRACE_HINT = "simp_trace_hint"
   604   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   605 
   606   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   607   object Simp_Trace_Cancel
   608   {
   609     def unapply(props: Properties.T): Option[Long] =
   610       props match {
   611         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   612         case _ => None
   613       }
   614   }
   615 
   616 
   617   /* XML data representation */
   618 
   619   def encode: XML.Encode.T[Markup] = (markup: Markup) =>
   620   {
   621     import XML.Encode._
   622     pair(string, properties)((markup.name, markup.properties))
   623   }
   624 
   625   def decode: XML.Decode.T[Markup] = (body: XML.Body) =>
   626   {
   627     import XML.Decode._
   628     val (name, props) = pair(string, properties)(body)
   629     Markup(name, props)
   630   }
   631 }
   632 
   633 
   634 sealed case class Markup(name: String, properties: Properties.T)
   635 {
   636   def markup(s: String): String =
   637     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   638 
   639   def update_properties(more_props: Properties.T): Markup =
   640     if (more_props.isEmpty) this
   641     else Markup(name, (more_props :\ properties) { case (p, ps) => Properties.put(ps, p) })
   642 
   643   def + (entry: Properties.Entry): Markup =
   644     Markup(name, Properties.put(properties, entry))
   645 }