src/Pure/PIDE/markup.scala
author wenzelm
Sun May 27 13:42:01 2018 +0200 (13 months ago)
changeset 68298 2c3ce27cf4a8
parent 68148 fb661e4c4717
child 68323 bf7336731981
permissions -rw-r--r--
markup for deleted fragments of token source (NB: quoted tokens transform "\123" implicitly);
     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   val DELETE = "delete"
   246 
   247 
   248   /* misc entities */
   249 
   250   val CLASS = "class"
   251   val TYPE_NAME = "type_name"
   252   val FIXED = "fixed"
   253   val CASE = "case"
   254   val CONSTANT = "constant"
   255   val DYNAMIC_FACT = "dynamic_fact"
   256 
   257 
   258   /* inner syntax */
   259 
   260   val TFREE = "tfree"
   261   val TVAR = "tvar"
   262   val FREE = "free"
   263   val SKOLEM = "skolem"
   264   val BOUND = "bound"
   265   val VAR = "var"
   266   val NUMERAL = "numeral"
   267   val LITERAL = "literal"
   268   val DELIMITER = "delimiter"
   269   val INNER_STRING = "inner_string"
   270   val INNER_CARTOUCHE = "inner_cartouche"
   271   val INNER_COMMENT = "inner_comment"
   272 
   273   val TOKEN_RANGE = "token_range"
   274 
   275   val SORTING = "sorting"
   276   val TYPING = "typing"
   277   val CLASS_PARAMETER = "class_parameter"
   278 
   279   val ATTRIBUTE = "attribute"
   280   val METHOD = "method"
   281 
   282 
   283   /* antiquotations */
   284 
   285   val ANTIQUOTED = "antiquoted"
   286   val ANTIQUOTE = "antiquote"
   287 
   288   val ML_ANTIQUOTATION = "ML_antiquotation"
   289   val DOCUMENT_ANTIQUOTATION = "document_antiquotation"
   290   val DOCUMENT_ANTIQUOTATION_OPTION = "document_antiquotation_option"
   291 
   292 
   293   /* text structure */
   294 
   295   val PARAGRAPH = "paragraph"
   296   val TEXT_FOLD = "text_fold"
   297 
   298 
   299   /* Markdown document structure */
   300 
   301   val MARKDOWN_PARAGRAPH = "markdown_paragraph"
   302   val MARKDOWN_ITEM = "markdown_item"
   303   val Markdown_Bullet = new Markup_Int("markdown_bullet", "depth")
   304   val Markdown_List = new Markup_String("markdown_list", "kind")
   305 
   306   val ITEMIZE = "itemize"
   307   val ENUMERATE = "enumerate"
   308   val DESCRIPTION = "description"
   309 
   310 
   311   /* ML */
   312 
   313   val ML_KEYWORD1 = "ML_keyword1"
   314   val ML_KEYWORD2 = "ML_keyword2"
   315   val ML_KEYWORD3 = "ML_keyword3"
   316   val ML_DELIMITER = "ML_delimiter"
   317   val ML_TVAR = "ML_tvar"
   318   val ML_NUMERAL = "ML_numeral"
   319   val ML_CHAR = "ML_char"
   320   val ML_STRING = "ML_string"
   321   val ML_COMMENT = "ML_comment"
   322   val SML_STRING = "SML_string"
   323   val SML_COMMENT = "SML_comment"
   324 
   325   val ML_DEF = "ML_def"
   326   val ML_OPEN = "ML_open"
   327   val ML_STRUCTURE = "ML_structure"
   328   val ML_TYPING = "ML_typing"
   329 
   330   val ML_BREAKPOINT = "ML_breakpoint"
   331 
   332 
   333   /* outer syntax */
   334 
   335   val COMMAND = "command"
   336   val KEYWORD = "keyword"
   337   val KEYWORD1 = "keyword1"
   338   val KEYWORD2 = "keyword2"
   339   val KEYWORD3 = "keyword3"
   340   val QUASI_KEYWORD = "quasi_keyword"
   341   val IMPROPER = "improper"
   342   val OPERATOR = "operator"
   343   val STRING = "string"
   344   val ALT_STRING = "alt_string"
   345   val VERBATIM = "verbatim"
   346   val CARTOUCHE = "cartouche"
   347   val COMMENT = "comment"
   348 
   349 
   350   /* timing */
   351 
   352   val Elapsed = new Properties.Double("elapsed")
   353   val CPU = new Properties.Double("cpu")
   354   val GC = new Properties.Double("gc")
   355 
   356   object Timing_Properties
   357   {
   358     def apply(timing: isabelle.Timing): Properties.T =
   359       Elapsed(timing.elapsed.seconds) ::: CPU(timing.cpu.seconds) ::: GC(timing.gc.seconds)
   360 
   361     def unapply(props: Properties.T): Option[isabelle.Timing] =
   362       (props, props, props) match {
   363         case (Elapsed(elapsed), CPU(cpu), GC(gc)) =>
   364           Some(new isabelle.Timing(Time.seconds(elapsed), Time.seconds(cpu), Time.seconds(gc)))
   365         case _ => None
   366       }
   367   }
   368 
   369   val TIMING = "timing"
   370 
   371   object Timing
   372   {
   373     def apply(timing: isabelle.Timing): Markup = Markup(TIMING, Timing_Properties(timing))
   374 
   375     def unapply(markup: Markup): Option[isabelle.Timing] =
   376       markup match {
   377         case Markup(TIMING, Timing_Properties(timing)) => Some(timing)
   378         case _ => None
   379       }
   380   }
   381 
   382 
   383   /* process result */
   384 
   385   val Return_Code = new Properties.Int("return_code")
   386 
   387   object Process_Result
   388   {
   389     def apply(result: Process_Result): Properties.T =
   390       Return_Code(result.rc) :::
   391         (if (result.timing.is_zero) Nil else Timing_Properties(result.timing))
   392 
   393     def unapply(props: Properties.T): Option[Process_Result] =
   394       props match {
   395         case Return_Code(rc) =>
   396           val timing = Timing_Properties.unapply(props).getOrElse(isabelle.Timing.zero)
   397           Some(isabelle.Process_Result(rc, timing = timing))
   398         case _ => None
   399       }
   400   }
   401 
   402 
   403   /* command indentation */
   404 
   405   object Command_Indent
   406   {
   407     val name = "command_indent"
   408     def unapply(markup: Markup): Option[Int] =
   409       if (markup.name == name) Indent.unapply(markup.properties) else None
   410   }
   411 
   412 
   413   /* goals */
   414 
   415   val GOAL = "goal"
   416   val SUBGOAL = "subgoal"
   417 
   418 
   419   /* command status */
   420 
   421   val TASK = "task"
   422 
   423   val ACCEPTED = "accepted"
   424   val FORKED = "forked"
   425   val JOINED = "joined"
   426   val RUNNING = "running"
   427   val FINISHED = "finished"
   428   val FAILED = "failed"
   429   val CONSOLIDATED = "consolidated"
   430 
   431 
   432   /* interactive documents */
   433 
   434   val VERSION = "version"
   435   val ASSIGN = "assign"
   436 
   437 
   438   /* prover process */
   439 
   440   val PROVER_COMMAND = "prover_command"
   441   val PROVER_ARG = "prover_arg"
   442 
   443 
   444   /* messages */
   445 
   446   val INIT = "init"
   447   val STATUS = "status"
   448   val REPORT = "report"
   449   val RESULT = "result"
   450   val WRITELN = "writeln"
   451   val STATE = "state"
   452   val INFORMATION = "information"
   453   val TRACING = "tracing"
   454   val WARNING = "warning"
   455   val LEGACY = "legacy"
   456   val ERROR = "error"
   457   val PROTOCOL = "protocol"
   458   val SYSTEM = "system"
   459   val STDOUT = "stdout"
   460   val STDERR = "stderr"
   461   val EXIT = "exit"
   462 
   463   val WRITELN_MESSAGE = "writeln_message"
   464   val STATE_MESSAGE = "state_message"
   465   val INFORMATION_MESSAGE = "information_message"
   466   val TRACING_MESSAGE = "tracing_message"
   467   val WARNING_MESSAGE = "warning_message"
   468   val LEGACY_MESSAGE = "legacy_message"
   469   val ERROR_MESSAGE = "error_message"
   470 
   471   val messages = Map(
   472     WRITELN -> WRITELN_MESSAGE,
   473     STATE -> STATE_MESSAGE,
   474     INFORMATION -> INFORMATION_MESSAGE,
   475     TRACING -> TRACING_MESSAGE,
   476     WARNING -> WARNING_MESSAGE,
   477     LEGACY -> LEGACY_MESSAGE,
   478     ERROR -> ERROR_MESSAGE)
   479 
   480   val message: String => String = messages.withDefault((s: String) => s)
   481 
   482   val NO_REPORT = "no_report"
   483 
   484   val BAD = "bad"
   485 
   486   val INTENSIFY = "intensify"
   487 
   488 
   489   /* active areas */
   490 
   491   val BROWSER = "browser"
   492   val GRAPHVIEW = "graphview"
   493 
   494   val SENDBACK = "sendback"
   495   val PADDING = "padding"
   496   val PADDING_LINE = (PADDING, "line")
   497   val PADDING_COMMAND = (PADDING, "command")
   498 
   499   val DIALOG = "dialog"
   500   val Result = new Properties.String(RESULT)
   501 
   502   val JEDIT_ACTION = "jedit_action"
   503 
   504 
   505   /* protocol message functions */
   506 
   507   val FUNCTION = "function"
   508   val Function = new Properties.String(FUNCTION)
   509 
   510   val COMMAND_TIMING: Properties.Entry = (FUNCTION, "command_timing")
   511   val THEORY_TIMING: Properties.Entry = (FUNCTION, "theory_timing")
   512 
   513   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   514   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   515 
   516   object Protocol_Handler
   517   {
   518     def unapply(props: Properties.T): Option[(String)] =
   519       props match {
   520         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   521         case _ => None
   522       }
   523   }
   524 
   525   val INVOKE_SCALA = "invoke_scala"
   526   object Invoke_Scala
   527   {
   528     def unapply(props: Properties.T): Option[(String, String)] =
   529       props match {
   530         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   531         case _ => None
   532       }
   533   }
   534 
   535   val CANCEL_SCALA = "cancel_scala"
   536   object Cancel_Scala
   537   {
   538     def unapply(props: Properties.T): Option[String] =
   539       props match {
   540         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   541         case _ => None
   542       }
   543   }
   544 
   545   object ML_Statistics
   546   {
   547     def unapply(props: Properties.T): Option[Properties.T] =
   548       props match {
   549         case (FUNCTION, "ML_statistics") :: props => Some(props)
   550         case _ => None
   551       }
   552   }
   553 
   554   object Task_Statistics
   555   {
   556     def unapply(props: Properties.T): Option[Properties.T] =
   557       props match {
   558         case (FUNCTION, "task_statistics") :: props => Some(props)
   559         case _ => None
   560       }
   561   }
   562 
   563   val LOADING_THEORY = "loading_theory"
   564   object Loading_Theory
   565   {
   566     def unapply(props: Properties.T): Option[String] =
   567       props match {
   568         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   569         case _ => None
   570       }
   571   }
   572 
   573   val EXPORT = "export"
   574   object Export
   575   {
   576     sealed case class Args(
   577       id: Option[String], serial: Long, theory_name: String, name: String, compress: Boolean)
   578 
   579     val THEORY_NAME = "theory_name"
   580     val COMPRESS = "compress"
   581 
   582     def dest_inline(props: Properties.T): Option[(Args, Path)] =
   583       props match {
   584         case
   585           List(
   586             (SERIAL, Value.Long(serial)),
   587             (THEORY_NAME, theory_name),
   588             (NAME, name),
   589             (COMPRESS, Value.Boolean(compress)),
   590             (FILE, file)) if isabelle.Path.is_valid(file) =>
   591           Some((Args(None, serial, theory_name, name, compress), isabelle.Path.explode(file)))
   592         case _ => None
   593       }
   594 
   595     def unapply(props: Properties.T): Option[Args] =
   596       props match {
   597         case List(
   598             (FUNCTION, EXPORT),
   599             (ID, id),
   600             (SERIAL, Value.Long(serial)),
   601             (THEORY_NAME, theory_name),
   602             (NAME, name),
   603             (COMPRESS, Value.Boolean(compress))) =>
   604           Some(Args(proper_string(id), serial, theory_name, name, compress))
   605         case _ => None
   606       }
   607   }
   608 
   609   val BUILD_SESSION_FINISHED = "build_session_finished"
   610   val Build_Session_Finished: Properties.T = List((FUNCTION, BUILD_SESSION_FINISHED))
   611 
   612   val PRINT_OPERATIONS = "print_operations"
   613 
   614 
   615   /* debugger output */
   616 
   617   val DEBUGGER_STATE = "debugger_state"
   618   object Debugger_State
   619   {
   620     def unapply(props: Properties.T): Option[String] =
   621       props match {
   622         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   623         case _ => None
   624       }
   625   }
   626 
   627   val DEBUGGER_OUTPUT = "debugger_output"
   628   object Debugger_Output
   629   {
   630     def unapply(props: Properties.T): Option[String] =
   631       props match {
   632         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   633         case _ => None
   634       }
   635   }
   636 
   637 
   638   /* simplifier trace */
   639 
   640   val SIMP_TRACE_PANEL = "simp_trace_panel"
   641 
   642   val SIMP_TRACE_LOG = "simp_trace_log"
   643   val SIMP_TRACE_STEP = "simp_trace_step"
   644   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   645   val SIMP_TRACE_HINT = "simp_trace_hint"
   646   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   647 
   648   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   649   object Simp_Trace_Cancel
   650   {
   651     def unapply(props: Properties.T): Option[Long] =
   652       props match {
   653         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   654         case _ => None
   655       }
   656   }
   657 
   658 
   659   /* XML data representation */
   660 
   661   def encode: XML.Encode.T[Markup] = (markup: Markup) =>
   662   {
   663     import XML.Encode._
   664     pair(string, properties)((markup.name, markup.properties))
   665   }
   666 
   667   def decode: XML.Decode.T[Markup] = (body: XML.Body) =>
   668   {
   669     import XML.Decode._
   670     val (name, props) = pair(string, properties)(body)
   671     Markup(name, props)
   672   }
   673 }
   674 
   675 
   676 sealed case class Markup(name: String, properties: Properties.T)
   677 {
   678   def markup(s: String): String =
   679     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   680 
   681   def update_properties(more_props: Properties.T): Markup =
   682     if (more_props.isEmpty) this
   683     else Markup(name, (more_props :\ properties) { case (p, ps) => Properties.put(ps, p) })
   684 
   685   def + (entry: Properties.Entry): Markup =
   686     Markup(name, Properties.put(properties, entry))
   687 }