src/Pure/PIDE/markup.scala
author wenzelm
Fri May 11 19:57:49 2018 +0200 (13 months ago)
changeset 68148 fb661e4c4717
parent 68101 0699a0bacc50
child 68298 2c3ce27cf4a8
permissions -rw-r--r--
more scalable -- avoid huge lines within stdout;
     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 
   461   val WRITELN_MESSAGE = "writeln_message"
   462   val STATE_MESSAGE = "state_message"
   463   val INFORMATION_MESSAGE = "information_message"
   464   val TRACING_MESSAGE = "tracing_message"
   465   val WARNING_MESSAGE = "warning_message"
   466   val LEGACY_MESSAGE = "legacy_message"
   467   val ERROR_MESSAGE = "error_message"
   468 
   469   val messages = Map(
   470     WRITELN -> WRITELN_MESSAGE,
   471     STATE -> STATE_MESSAGE,
   472     INFORMATION -> INFORMATION_MESSAGE,
   473     TRACING -> TRACING_MESSAGE,
   474     WARNING -> WARNING_MESSAGE,
   475     LEGACY -> LEGACY_MESSAGE,
   476     ERROR -> ERROR_MESSAGE)
   477 
   478   val message: String => String = messages.withDefault((s: String) => s)
   479 
   480   val NO_REPORT = "no_report"
   481 
   482   val BAD = "bad"
   483 
   484   val INTENSIFY = "intensify"
   485 
   486 
   487   /* active areas */
   488 
   489   val BROWSER = "browser"
   490   val GRAPHVIEW = "graphview"
   491 
   492   val SENDBACK = "sendback"
   493   val PADDING = "padding"
   494   val PADDING_LINE = (PADDING, "line")
   495   val PADDING_COMMAND = (PADDING, "command")
   496 
   497   val DIALOG = "dialog"
   498   val Result = new Properties.String(RESULT)
   499 
   500   val JEDIT_ACTION = "jedit_action"
   501 
   502 
   503   /* protocol message functions */
   504 
   505   val FUNCTION = "function"
   506   val Function = new Properties.String(FUNCTION)
   507 
   508   val COMMAND_TIMING: Properties.Entry = (FUNCTION, "command_timing")
   509   val THEORY_TIMING: Properties.Entry = (FUNCTION, "theory_timing")
   510 
   511   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   512   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   513 
   514   object Protocol_Handler
   515   {
   516     def unapply(props: Properties.T): Option[(String)] =
   517       props match {
   518         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   519         case _ => None
   520       }
   521   }
   522 
   523   val INVOKE_SCALA = "invoke_scala"
   524   object Invoke_Scala
   525   {
   526     def unapply(props: Properties.T): Option[(String, String)] =
   527       props match {
   528         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   529         case _ => None
   530       }
   531   }
   532 
   533   val CANCEL_SCALA = "cancel_scala"
   534   object Cancel_Scala
   535   {
   536     def unapply(props: Properties.T): Option[String] =
   537       props match {
   538         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   539         case _ => None
   540       }
   541   }
   542 
   543   object ML_Statistics
   544   {
   545     def unapply(props: Properties.T): Option[Properties.T] =
   546       props match {
   547         case (FUNCTION, "ML_statistics") :: props => Some(props)
   548         case _ => None
   549       }
   550   }
   551 
   552   object Task_Statistics
   553   {
   554     def unapply(props: Properties.T): Option[Properties.T] =
   555       props match {
   556         case (FUNCTION, "task_statistics") :: props => Some(props)
   557         case _ => None
   558       }
   559   }
   560 
   561   val LOADING_THEORY = "loading_theory"
   562   object Loading_Theory
   563   {
   564     def unapply(props: Properties.T): Option[String] =
   565       props match {
   566         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   567         case _ => None
   568       }
   569   }
   570 
   571   val EXPORT = "export"
   572   object Export
   573   {
   574     sealed case class Args(
   575       id: Option[String], serial: Long, theory_name: String, name: String, compress: Boolean)
   576 
   577     val THEORY_NAME = "theory_name"
   578     val COMPRESS = "compress"
   579 
   580     def dest_inline(props: Properties.T): Option[(Args, Path)] =
   581       props match {
   582         case
   583           List(
   584             (SERIAL, Value.Long(serial)),
   585             (THEORY_NAME, theory_name),
   586             (NAME, name),
   587             (COMPRESS, Value.Boolean(compress)),
   588             (FILE, file)) if isabelle.Path.is_valid(file) =>
   589           Some((Args(None, serial, theory_name, name, compress), isabelle.Path.explode(file)))
   590         case _ => None
   591       }
   592 
   593     def unapply(props: Properties.T): Option[Args] =
   594       props match {
   595         case List(
   596             (FUNCTION, EXPORT),
   597             (ID, id),
   598             (SERIAL, Value.Long(serial)),
   599             (THEORY_NAME, theory_name),
   600             (NAME, name),
   601             (COMPRESS, Value.Boolean(compress))) =>
   602           Some(Args(proper_string(id), serial, theory_name, name, compress))
   603         case _ => None
   604       }
   605   }
   606 
   607   val BUILD_SESSION_FINISHED = "build_session_finished"
   608   val Build_Session_Finished: Properties.T = List((FUNCTION, BUILD_SESSION_FINISHED))
   609 
   610   val PRINT_OPERATIONS = "print_operations"
   611 
   612 
   613   /* debugger output */
   614 
   615   val DEBUGGER_STATE = "debugger_state"
   616   object Debugger_State
   617   {
   618     def unapply(props: Properties.T): Option[String] =
   619       props match {
   620         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   621         case _ => None
   622       }
   623   }
   624 
   625   val DEBUGGER_OUTPUT = "debugger_output"
   626   object Debugger_Output
   627   {
   628     def unapply(props: Properties.T): Option[String] =
   629       props match {
   630         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   631         case _ => None
   632       }
   633   }
   634 
   635 
   636   /* simplifier trace */
   637 
   638   val SIMP_TRACE_PANEL = "simp_trace_panel"
   639 
   640   val SIMP_TRACE_LOG = "simp_trace_log"
   641   val SIMP_TRACE_STEP = "simp_trace_step"
   642   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   643   val SIMP_TRACE_HINT = "simp_trace_hint"
   644   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   645 
   646   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   647   object Simp_Trace_Cancel
   648   {
   649     def unapply(props: Properties.T): Option[Long] =
   650       props match {
   651         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   652         case _ => None
   653       }
   654   }
   655 
   656 
   657   /* XML data representation */
   658 
   659   def encode: XML.Encode.T[Markup] = (markup: Markup) =>
   660   {
   661     import XML.Encode._
   662     pair(string, properties)((markup.name, markup.properties))
   663   }
   664 
   665   def decode: XML.Decode.T[Markup] = (body: XML.Body) =>
   666   {
   667     import XML.Decode._
   668     val (name, props) = pair(string, properties)(body)
   669     Markup(name, props)
   670   }
   671 }
   672 
   673 
   674 sealed case class Markup(name: String, properties: Properties.T)
   675 {
   676   def markup(s: String): String =
   677     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   678 
   679   def update_properties(more_props: Properties.T): Markup =
   680     if (more_props.isEmpty) this
   681     else Markup(name, (more_props :\ properties) { case (p, ps) => Properties.put(ps, p) })
   682 
   683   def + (entry: Properties.Entry): Markup =
   684     Markup(name, Properties.put(properties, entry))
   685 }