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