src/Pure/PIDE/markup.scala
author wenzelm
Sat Nov 07 13:13:23 2015 +0100 (2015-11-07 ago)
changeset 61598 ed4dad8823a4
parent 61449 4f31f79cf2d1
child 61660 78b371644654
permissions -rw-r--r--
less confusing markup;
     1 /*  Title:      Pure/PIDE/markup.scala
     2     Module:     PIDE
     3     Author:     Makarius
     4 
     5 Quasi-abstract markup elements.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 object Markup
    12 {
    13   /* elements */
    14 
    15   object Elements
    16   {
    17     def apply(elems: Set[String]): Elements = new Elements(elems)
    18     def apply(elems: String*): Elements = apply(Set(elems: _*))
    19     val empty: Elements = apply()
    20     val full: Elements =
    21       new Elements(Set.empty)
    22       {
    23         override def apply(elem: String): Boolean = true
    24         override def toString: String = "Elements.full"
    25       }
    26   }
    27 
    28   sealed class Elements private[Markup](private val rep: Set[String])
    29   {
    30     def apply(elem: String): Boolean = rep.contains(elem)
    31     def + (elem: String): Elements = new Elements(rep + elem)
    32     def ++ (elems: Elements): Elements = new Elements(rep ++ elems.rep)
    33     override def toString: String = rep.mkString("Elements(", ",", ")")
    34   }
    35 
    36 
    37   /* properties */
    38 
    39   val NAME = "name"
    40   val Name = new Properties.String(NAME)
    41 
    42   val KIND = "kind"
    43   val Kind = new Properties.String(KIND)
    44 
    45   val SERIAL = "serial"
    46   val Serial = new Properties.Long(SERIAL)
    47 
    48   val INSTANCE = "instance"
    49   val Instance = new Properties.String(INSTANCE)
    50 
    51 
    52   /* basic markup */
    53 
    54   val Empty = Markup("", Nil)
    55   val Broken = Markup("broken", Nil)
    56 
    57   class Markup_String(val name: String, prop: String)
    58   {
    59     private val Prop = new Properties.String(prop)
    60 
    61     def apply(s: String): Markup = Markup(name, Prop(s))
    62     def unapply(markup: Markup): Option[String] =
    63       if (markup.name == name) Prop.unapply(markup.properties) else None
    64   }
    65 
    66   class Markup_Int(val name: String, prop: String)
    67   {
    68     private val Prop = new Properties.Int(prop)
    69 
    70     def apply(i: Int): Markup = Markup(name, Prop(i))
    71     def unapply(markup: Markup): Option[Int] =
    72       if (markup.name == name) Prop.unapply(markup.properties) else None
    73   }
    74 
    75   class Markup_Long(val name: String, prop: String)
    76   {
    77     private val Prop = new Properties.Long(prop)
    78 
    79     def apply(i: Long): Markup = Markup(name, Prop(i))
    80     def unapply(markup: Markup): Option[Long] =
    81       if (markup.name == name) Prop.unapply(markup.properties) else None
    82   }
    83 
    84 
    85   /* formal entities */
    86 
    87   val BINDING = "binding"
    88   val ENTITY = "entity"
    89   val DEF = "def"
    90   val REF = "ref"
    91 
    92   object Entity
    93   {
    94     def unapply(markup: Markup): Option[(String, String)] =
    95       markup match {
    96         case Markup(ENTITY, props) =>
    97           (props, props) match {
    98             case (Kind(kind), Name(name)) => Some((kind, name))
    99             case _ => None
   100           }
   101         case _ => None
   102       }
   103   }
   104 
   105 
   106   /* completion */
   107 
   108   val COMPLETION = "completion"
   109   val NO_COMPLETION = "no_completion"
   110 
   111 
   112   /* position */
   113 
   114   val LINE = "line"
   115   val END_LINE = "line"
   116   val OFFSET = "offset"
   117   val END_OFFSET = "end_offset"
   118   val FILE = "file"
   119   val ID = "id"
   120 
   121   val DEF_LINE = "def_line"
   122   val DEF_OFFSET = "def_offset"
   123   val DEF_END_OFFSET = "def_end_offset"
   124   val DEF_FILE = "def_file"
   125   val DEF_ID = "def_id"
   126 
   127   val POSITION_PROPERTIES = Set(LINE, OFFSET, END_OFFSET, FILE, ID)
   128   val POSITION = "position"
   129 
   130 
   131   /* expression */
   132 
   133   val EXPRESSION = "expression"
   134 
   135 
   136   /* citation */
   137 
   138   val CITATION = "citation"
   139   val Citation = new Markup_String(CITATION, NAME)
   140 
   141 
   142   /* embedded languages */
   143 
   144   val Symbols = new Properties.Boolean("symbols")
   145   val Antiquotes = new Properties.Boolean("antiquotes")
   146   val Delimited = new Properties.Boolean("delimited")
   147 
   148   val LANGUAGE = "language"
   149   object Language
   150   {
   151     val ML = "ML"
   152     val SML = "SML"
   153     val PATH = "path"
   154     val UNKNOWN = "unknown"
   155 
   156     def unapply(markup: Markup): Option[(String, Boolean, Boolean, Boolean)] =
   157       markup match {
   158         case Markup(LANGUAGE, props) =>
   159           (props, props, props, props) match {
   160             case (Name(name), Symbols(symbols), Antiquotes(antiquotes), Delimited(delimited)) =>
   161               Some((name, symbols, antiquotes, delimited))
   162             case _ => None
   163           }
   164         case _ => None
   165       }
   166   }
   167 
   168 
   169   /* external resources */
   170 
   171   val PATH = "path"
   172   val Path = new Markup_String(PATH, NAME)
   173 
   174   val URL = "url"
   175   val Url = new Markup_String(URL, NAME)
   176 
   177 
   178   /* pretty printing */
   179 
   180   val Block = new Markup_Int("block", "indent")
   181   val Break = new Markup_Int("break", "width")
   182 
   183   val ITEM = "item"
   184   val BULLET = "bullet"
   185 
   186   val SEPARATOR = "separator"
   187 
   188 
   189   /* text properties */
   190 
   191   val WORDS = "words"
   192 
   193   val HIDDEN = "hidden"
   194 
   195 
   196   /* misc entities */
   197 
   198   val CLASS = "class"
   199   val TYPE_NAME = "type_name"
   200   val FIXED = "fixed"
   201   val CASE = "case"
   202   val CONSTANT = "constant"
   203   val DYNAMIC_FACT = "dynamic_fact"
   204 
   205 
   206   /* inner syntax */
   207 
   208   val TFREE = "tfree"
   209   val TVAR = "tvar"
   210   val FREE = "free"
   211   val SKOLEM = "skolem"
   212   val BOUND = "bound"
   213   val VAR = "var"
   214   val NUMERAL = "numeral"
   215   val LITERAL = "literal"
   216   val DELIMITER = "delimiter"
   217   val INNER_STRING = "inner_string"
   218   val INNER_CARTOUCHE = "inner_cartouche"
   219   val INNER_COMMENT = "inner_comment"
   220 
   221   val TOKEN_RANGE = "token_range"
   222 
   223   val SORTING = "sorting"
   224   val TYPING = "typing"
   225 
   226   val ATTRIBUTE = "attribute"
   227   val METHOD = "method"
   228 
   229 
   230   /* antiquotations */
   231 
   232   val ANTIQUOTED = "antiquoted"
   233   val ANTIQUOTE = "antiquote"
   234 
   235   val ML_ANTIQUOTATION = "ML_antiquotation"
   236   val DOCUMENT_ANTIQUOTATION = "document_antiquotation"
   237   val DOCUMENT_ANTIQUOTATION_OPTION = "document_antiquotation_option"
   238 
   239 
   240   /* text structure */
   241 
   242   val PARAGRAPH = "paragraph"
   243   val TEXT_FOLD = "text_fold"
   244 
   245 
   246   /* Markdown document structure */
   247 
   248   val MARKDOWN_PARAGRAPH = "markdown_paragraph"
   249   val Markdown_List = new Markup_String("markdown_list", "kind")
   250   val Markdown_Item = new Markup_Int("markdown_item", "depth")
   251 
   252 
   253   /* ML */
   254 
   255   val ML_KEYWORD1 = "ML_keyword1"
   256   val ML_KEYWORD2 = "ML_keyword2"
   257   val ML_KEYWORD3 = "ML_keyword3"
   258   val ML_DELIMITER = "ML_delimiter"
   259   val ML_TVAR = "ML_tvar"
   260   val ML_NUMERAL = "ML_numeral"
   261   val ML_CHAR = "ML_char"
   262   val ML_STRING = "ML_string"
   263   val ML_COMMENT = "ML_comment"
   264   val SML_STRING = "SML_string"
   265   val SML_COMMENT = "SML_comment"
   266 
   267   val ML_DEF = "ML_def"
   268   val ML_OPEN = "ML_open"
   269   val ML_STRUCTURE = "ML_structure"
   270   val ML_TYPING = "ML_typing"
   271 
   272   val ML_BREAKPOINT = "ML_breakpoint"
   273 
   274 
   275   /* outer syntax */
   276 
   277   val COMMAND = "command"
   278   val KEYWORD1 = "keyword1"
   279   val KEYWORD2 = "keyword2"
   280   val KEYWORD3 = "keyword3"
   281   val QUASI_KEYWORD = "quasi_keyword"
   282   val IMPROPER = "improper"
   283   val OPERATOR = "operator"
   284   val STRING = "string"
   285   val ALT_STRING = "alt_string"
   286   val VERBATIM = "verbatim"
   287   val CARTOUCHE = "cartouche"
   288   val COMMENT = "comment"
   289 
   290 
   291   /* timing */
   292 
   293   val Elapsed = new Properties.Double("elapsed")
   294   val CPU = new Properties.Double("cpu")
   295   val GC = new Properties.Double("gc")
   296 
   297   object Timing_Properties
   298   {
   299     def apply(timing: isabelle.Timing): Properties.T =
   300       Elapsed(timing.elapsed.seconds) ::: CPU(timing.cpu.seconds) ::: GC(timing.gc.seconds)
   301 
   302     def unapply(props: Properties.T): Option[isabelle.Timing] =
   303       (props, props, props) match {
   304         case (Elapsed(elapsed), CPU(cpu), GC(gc)) =>
   305           Some(new isabelle.Timing(Time.seconds(elapsed), Time.seconds(cpu), Time.seconds(gc)))
   306         case _ => None
   307       }
   308   }
   309 
   310   val TIMING = "timing"
   311 
   312   object Timing
   313   {
   314     def apply(timing: isabelle.Timing): Markup = Markup(TIMING, Timing_Properties(timing))
   315 
   316     def unapply(markup: Markup): Option[isabelle.Timing] =
   317       markup match {
   318         case Markup(TIMING, Timing_Properties(timing)) => Some(timing)
   319         case _ => None
   320       }
   321   }
   322 
   323 
   324   /* command timing */
   325 
   326   val COMMAND_TIMING = "command_timing"
   327 
   328 
   329   /* toplevel */
   330 
   331   val SUBGOALS = "subgoals"
   332   val PROOF_STATE = "proof_state"
   333 
   334   val GOAL = "goal"
   335   val SUBGOAL = "subgoal"
   336 
   337 
   338   /* command status */
   339 
   340   val TASK = "task"
   341 
   342   val ACCEPTED = "accepted"
   343   val FORKED = "forked"
   344   val JOINED = "joined"
   345   val RUNNING = "running"
   346   val FINISHED = "finished"
   347   val FAILED = "failed"
   348 
   349 
   350   /* interactive documents */
   351 
   352   val VERSION = "version"
   353   val ASSIGN = "assign"
   354 
   355 
   356   /* prover process */
   357 
   358   val PROVER_COMMAND = "prover_command"
   359   val PROVER_ARG = "prover_arg"
   360 
   361 
   362   /* messages */
   363 
   364   val INIT = "init"
   365   val STATUS = "status"
   366   val REPORT = "report"
   367   val RESULT = "result"
   368   val WRITELN = "writeln"
   369   val STATE = "state"
   370   val INFORMATION = "information"
   371   val TRACING = "tracing"
   372   val WARNING = "warning"
   373   val LEGACY = "legacy"
   374   val ERROR = "error"
   375   val PROTOCOL = "protocol"
   376   val SYSTEM = "system"
   377   val STDOUT = "stdout"
   378   val STDERR = "stderr"
   379   val EXIT = "exit"
   380 
   381   val WRITELN_MESSAGE = "writeln_message"
   382   val STATE_MESSAGE = "state_message"
   383   val INFORMATION_MESSAGE = "information_message"
   384   val TRACING_MESSAGE = "tracing_message"
   385   val WARNING_MESSAGE = "warning_message"
   386   val LEGACY_MESSAGE = "legacy_message"
   387   val ERROR_MESSAGE = "error_message"
   388 
   389   val messages = Map(
   390     WRITELN -> WRITELN_MESSAGE,
   391     STATE -> STATE_MESSAGE,
   392     INFORMATION -> INFORMATION_MESSAGE,
   393     TRACING -> TRACING_MESSAGE,
   394     WARNING -> WARNING_MESSAGE,
   395     LEGACY -> LEGACY_MESSAGE,
   396     ERROR -> ERROR_MESSAGE)
   397 
   398   val message: String => String = messages.withDefault((s: String) => s)
   399 
   400   val Return_Code = new Properties.Int("return_code")
   401 
   402   val NO_REPORT = "no_report"
   403 
   404   val BAD = "bad"
   405 
   406   val INTENSIFY = "intensify"
   407 
   408 
   409   /* active areas */
   410 
   411   val BROWSER = "browser"
   412   val GRAPHVIEW = "graphview"
   413 
   414   val SENDBACK = "sendback"
   415   val PADDING = "padding"
   416   val PADDING_LINE = (PADDING, "line")
   417   val PADDING_COMMAND = (PADDING, "command")
   418 
   419   val DIALOG = "dialog"
   420   val Result = new Properties.String(RESULT)
   421 
   422 
   423   /* protocol message functions */
   424 
   425   val FUNCTION = "function"
   426   val Function = new Properties.String(FUNCTION)
   427 
   428   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   429   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   430 
   431   object Protocol_Handler
   432   {
   433     def unapply(props: Properties.T): Option[(String)] =
   434       props match {
   435         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   436         case _ => None
   437       }
   438   }
   439 
   440   val INVOKE_SCALA = "invoke_scala"
   441   object Invoke_Scala
   442   {
   443     def unapply(props: Properties.T): Option[(String, String)] =
   444       props match {
   445         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   446         case _ => None
   447       }
   448   }
   449 
   450   val CANCEL_SCALA = "cancel_scala"
   451   object Cancel_Scala
   452   {
   453     def unapply(props: Properties.T): Option[String] =
   454       props match {
   455         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   456         case _ => None
   457       }
   458   }
   459 
   460   object ML_Statistics
   461   {
   462     def unapply(props: Properties.T): Option[Properties.T] =
   463       props match {
   464         case (FUNCTION, "ML_statistics") :: stats => Some(stats)
   465         case _ => None
   466       }
   467   }
   468 
   469   object Task_Statistics
   470   {
   471     def unapply(props: Properties.T): Option[Properties.T] =
   472       props match {
   473         case (FUNCTION, "task_statistics") :: stats => Some(stats)
   474         case _ => None
   475       }
   476   }
   477 
   478   val LOADING_THEORY = "loading_theory"
   479   object Loading_Theory
   480   {
   481     def unapply(props: Properties.T): Option[String] =
   482       props match {
   483         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   484         case _ => None
   485       }
   486   }
   487 
   488   val BUILD_THEORIES_RESULT = "build_theories_result"
   489   object Build_Theories_Result
   490   {
   491     def unapply(props: Properties.T): Option[String] =
   492       props match {
   493         case List((FUNCTION, BUILD_THEORIES_RESULT), (ID, id)) => Some(id)
   494         case _ => None
   495       }
   496   }
   497 
   498   val PRINT_OPERATIONS = "print_operations"
   499 
   500 
   501   /* debugger output */
   502 
   503   val DEBUGGER_STATE = "debugger_state"
   504   object Debugger_State
   505   {
   506     def unapply(props: Properties.T): Option[String] =
   507       props match {
   508         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   509         case _ => None
   510       }
   511   }
   512 
   513   val DEBUGGER_OUTPUT = "debugger_output"
   514   object Debugger_Output
   515   {
   516     def unapply(props: Properties.T): Option[String] =
   517       props match {
   518         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   519         case _ => None
   520       }
   521   }
   522 
   523 
   524   /* simplifier trace */
   525 
   526   val SIMP_TRACE_PANEL = "simp_trace_panel"
   527 
   528   val SIMP_TRACE_LOG = "simp_trace_log"
   529   val SIMP_TRACE_STEP = "simp_trace_step"
   530   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   531   val SIMP_TRACE_HINT = "simp_trace_hint"
   532   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   533 
   534   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   535   object Simp_Trace_Cancel
   536   {
   537     def unapply(props: Properties.T): Option[Long] =
   538       props match {
   539         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   540         case _ => None
   541       }
   542   }
   543 }
   544 
   545 
   546 sealed case class Markup(name: String, properties: Properties.T)
   547 {
   548   def markup(s: String): String =
   549     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   550 }