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