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