src/Pure/PIDE/markup.scala
author wenzelm
Tue Jan 02 19:52:17 2018 +0100 (23 months ago ago)
changeset 67323 d02208cefbdb
parent 67322 734a4e44b159
child 67336 3ee6da378183
permissions -rw-r--r--
PIDE markup for Markdown items (which may consist of multiple paragraphs or lists);
     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_ITEM = "markdown_item"
   300   val Markdown_List = new Markup_String("markdown_list", "kind")
   301   val Markdown_Bullet = new Markup_Int("markdown_bullet", "depth")
   302 
   303 
   304   /* ML */
   305 
   306   val ML_KEYWORD1 = "ML_keyword1"
   307   val ML_KEYWORD2 = "ML_keyword2"
   308   val ML_KEYWORD3 = "ML_keyword3"
   309   val ML_DELIMITER = "ML_delimiter"
   310   val ML_TVAR = "ML_tvar"
   311   val ML_NUMERAL = "ML_numeral"
   312   val ML_CHAR = "ML_char"
   313   val ML_STRING = "ML_string"
   314   val ML_COMMENT = "ML_comment"
   315   val SML_STRING = "SML_string"
   316   val SML_COMMENT = "SML_comment"
   317 
   318   val ML_DEF = "ML_def"
   319   val ML_OPEN = "ML_open"
   320   val ML_STRUCTURE = "ML_structure"
   321   val ML_TYPING = "ML_typing"
   322 
   323   val ML_BREAKPOINT = "ML_breakpoint"
   324 
   325 
   326   /* outer syntax */
   327 
   328   val COMMAND = "command"
   329   val KEYWORD = "keyword"
   330   val KEYWORD1 = "keyword1"
   331   val KEYWORD2 = "keyword2"
   332   val KEYWORD3 = "keyword3"
   333   val QUASI_KEYWORD = "quasi_keyword"
   334   val IMPROPER = "improper"
   335   val OPERATOR = "operator"
   336   val STRING = "string"
   337   val ALT_STRING = "alt_string"
   338   val VERBATIM = "verbatim"
   339   val CARTOUCHE = "cartouche"
   340   val COMMENT = "comment"
   341 
   342 
   343   /* timing */
   344 
   345   val Elapsed = new Properties.Double("elapsed")
   346   val CPU = new Properties.Double("cpu")
   347   val GC = new Properties.Double("gc")
   348 
   349   object Timing_Properties
   350   {
   351     def apply(timing: isabelle.Timing): Properties.T =
   352       Elapsed(timing.elapsed.seconds) ::: CPU(timing.cpu.seconds) ::: GC(timing.gc.seconds)
   353 
   354     def unapply(props: Properties.T): Option[isabelle.Timing] =
   355       (props, props, props) match {
   356         case (Elapsed(elapsed), CPU(cpu), GC(gc)) =>
   357           Some(new isabelle.Timing(Time.seconds(elapsed), Time.seconds(cpu), Time.seconds(gc)))
   358         case _ => None
   359       }
   360   }
   361 
   362   val TIMING = "timing"
   363 
   364   object Timing
   365   {
   366     def apply(timing: isabelle.Timing): Markup = Markup(TIMING, Timing_Properties(timing))
   367 
   368     def unapply(markup: Markup): Option[isabelle.Timing] =
   369       markup match {
   370         case Markup(TIMING, Timing_Properties(timing)) => Some(timing)
   371         case _ => None
   372       }
   373   }
   374 
   375 
   376   /* process result */
   377 
   378   val Return_Code = new Properties.Int("return_code")
   379 
   380   object Process_Result
   381   {
   382     def apply(result: Process_Result): Properties.T =
   383       Return_Code(result.rc) :::
   384         (if (result.timing.is_zero) Nil else Timing_Properties(result.timing))
   385 
   386     def unapply(props: Properties.T): Option[Process_Result] =
   387       props match {
   388         case Return_Code(rc) =>
   389           val timing = Timing_Properties.unapply(props).getOrElse(isabelle.Timing.zero)
   390           Some(isabelle.Process_Result(rc, timing = timing))
   391         case _ => None
   392       }
   393   }
   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   val CONSOLIDATED = "consolidated"
   423 
   424 
   425   /* interactive documents */
   426 
   427   val VERSION = "version"
   428   val ASSIGN = "assign"
   429 
   430 
   431   /* prover process */
   432 
   433   val PROVER_COMMAND = "prover_command"
   434   val PROVER_ARG = "prover_arg"
   435 
   436 
   437   /* messages */
   438 
   439   val INIT = "init"
   440   val STATUS = "status"
   441   val REPORT = "report"
   442   val RESULT = "result"
   443   val WRITELN = "writeln"
   444   val STATE = "state"
   445   val INFORMATION = "information"
   446   val TRACING = "tracing"
   447   val WARNING = "warning"
   448   val LEGACY = "legacy"
   449   val ERROR = "error"
   450   val PROTOCOL = "protocol"
   451   val SYSTEM = "system"
   452   val STDOUT = "stdout"
   453   val STDERR = "stderr"
   454   val EXIT = "exit"
   455 
   456   val WRITELN_MESSAGE = "writeln_message"
   457   val STATE_MESSAGE = "state_message"
   458   val INFORMATION_MESSAGE = "information_message"
   459   val TRACING_MESSAGE = "tracing_message"
   460   val WARNING_MESSAGE = "warning_message"
   461   val LEGACY_MESSAGE = "legacy_message"
   462   val ERROR_MESSAGE = "error_message"
   463 
   464   val messages = Map(
   465     WRITELN -> WRITELN_MESSAGE,
   466     STATE -> STATE_MESSAGE,
   467     INFORMATION -> INFORMATION_MESSAGE,
   468     TRACING -> TRACING_MESSAGE,
   469     WARNING -> WARNING_MESSAGE,
   470     LEGACY -> LEGACY_MESSAGE,
   471     ERROR -> ERROR_MESSAGE)
   472 
   473   val message: String => String = messages.withDefault((s: String) => s)
   474 
   475   val NO_REPORT = "no_report"
   476 
   477   val BAD = "bad"
   478 
   479   val INTENSIFY = "intensify"
   480 
   481 
   482   /* active areas */
   483 
   484   val BROWSER = "browser"
   485   val GRAPHVIEW = "graphview"
   486 
   487   val SENDBACK = "sendback"
   488   val PADDING = "padding"
   489   val PADDING_LINE = (PADDING, "line")
   490   val PADDING_COMMAND = (PADDING, "command")
   491 
   492   val DIALOG = "dialog"
   493   val Result = new Properties.String(RESULT)
   494 
   495   val JEDIT_ACTION = "jedit_action"
   496 
   497 
   498   /* protocol message functions */
   499 
   500   val FUNCTION = "function"
   501   val Function = new Properties.String(FUNCTION)
   502 
   503   val COMMAND_TIMING: Properties.Entry = (FUNCTION, "command_timing")
   504   val THEORY_TIMING: Properties.Entry = (FUNCTION, "theory_timing")
   505 
   506   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   507   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   508 
   509   object Protocol_Handler
   510   {
   511     def unapply(props: Properties.T): Option[(String)] =
   512       props match {
   513         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   514         case _ => None
   515       }
   516   }
   517 
   518   val INVOKE_SCALA = "invoke_scala"
   519   object Invoke_Scala
   520   {
   521     def unapply(props: Properties.T): Option[(String, String)] =
   522       props match {
   523         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   524         case _ => None
   525       }
   526   }
   527 
   528   val CANCEL_SCALA = "cancel_scala"
   529   object Cancel_Scala
   530   {
   531     def unapply(props: Properties.T): Option[String] =
   532       props match {
   533         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   534         case _ => None
   535       }
   536   }
   537 
   538   object ML_Statistics
   539   {
   540     def unapply(props: Properties.T): Option[Properties.T] =
   541       props match {
   542         case (FUNCTION, "ML_statistics") :: props => Some(props)
   543         case _ => None
   544       }
   545   }
   546 
   547   object Task_Statistics
   548   {
   549     def unapply(props: Properties.T): Option[Properties.T] =
   550       props match {
   551         case (FUNCTION, "task_statistics") :: props => Some(props)
   552         case _ => None
   553       }
   554   }
   555 
   556   val LOADING_THEORY = "loading_theory"
   557   object Loading_Theory
   558   {
   559     def unapply(props: Properties.T): Option[String] =
   560       props match {
   561         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   562         case _ => None
   563       }
   564   }
   565 
   566   val BUILD_SESSION_FINISHED = "build_session_finished"
   567   val Build_Session_Finished: Properties.T = List((FUNCTION, BUILD_SESSION_FINISHED))
   568 
   569   val PRINT_OPERATIONS = "print_operations"
   570 
   571 
   572   /* debugger output */
   573 
   574   val DEBUGGER_STATE = "debugger_state"
   575   object Debugger_State
   576   {
   577     def unapply(props: Properties.T): Option[String] =
   578       props match {
   579         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   580         case _ => None
   581       }
   582   }
   583 
   584   val DEBUGGER_OUTPUT = "debugger_output"
   585   object Debugger_Output
   586   {
   587     def unapply(props: Properties.T): Option[String] =
   588       props match {
   589         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   590         case _ => None
   591       }
   592   }
   593 
   594 
   595   /* simplifier trace */
   596 
   597   val SIMP_TRACE_PANEL = "simp_trace_panel"
   598 
   599   val SIMP_TRACE_LOG = "simp_trace_log"
   600   val SIMP_TRACE_STEP = "simp_trace_step"
   601   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   602   val SIMP_TRACE_HINT = "simp_trace_hint"
   603   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   604 
   605   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   606   object Simp_Trace_Cancel
   607   {
   608     def unapply(props: Properties.T): Option[Long] =
   609       props match {
   610         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   611         case _ => None
   612       }
   613   }
   614 
   615 
   616   /* XML data representation */
   617 
   618   def encode: XML.Encode.T[Markup] = (markup: Markup) =>
   619   {
   620     import XML.Encode._
   621     pair(string, properties)((markup.name, markup.properties))
   622   }
   623 
   624   def decode: XML.Decode.T[Markup] = (body: XML.Body) =>
   625   {
   626     import XML.Decode._
   627     val (name, props) = pair(string, properties)(body)
   628     Markup(name, props)
   629   }
   630 }
   631 
   632 
   633 sealed case class Markup(name: String, properties: Properties.T)
   634 {
   635   def markup(s: String): String =
   636     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   637 
   638   def update_properties(more_props: Properties.T): Markup =
   639     if (more_props.isEmpty) this
   640     else Markup(name, (more_props :\ properties) { case (p, ps) => Properties.put(ps, p) })
   641 
   642   def + (entry: Properties.Entry): Markup =
   643     Markup(name, Properties.put(properties, entry))
   644 }