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