src/Pure/PIDE/markup.scala
author wenzelm
Sat Dec 19 14:47:52 2015 +0100 (2015-12-19 ago)
changeset 61866 3a5992c3410c
parent 61864 e2a9e46ac0fb
child 62806 de9bf8171626
permissions -rw-r--r--
support for blocks with consistent breaks;
tuned;
     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   val DOC = "doc"
   178   val Doc = new Markup_String(DOC, NAME)
   179 
   180 
   181   /* pretty printing */
   182 
   183   val Consistent = new Properties.Boolean("consistent")
   184   val Indent = new Properties.Int("indent")
   185   val Width = new Properties.Int("width")
   186 
   187   object Block
   188   {
   189     val name = "block"
   190     def apply(c: Boolean, i: Int): Markup =
   191       Markup(name,
   192         (if (c) Consistent(c) else Nil) :::
   193         (if (i != 0) Indent(i) else Nil))
   194     def unapply(markup: Markup): Option[(Boolean, Int)] =
   195       if (markup.name == name) {
   196         val c = Consistent.unapply(markup.properties).getOrElse(false)
   197         val i = Indent.unapply(markup.properties).getOrElse(0)
   198         Some((c, i))
   199       }
   200       else None
   201   }
   202 
   203   object Break
   204   {
   205     val name = "break"
   206     def apply(w: Int, i: Int): Markup =
   207       Markup(name,
   208         (if (w != 0) Width(w) else Nil) :::
   209         (if (i != 0) Indent(i) else Nil))
   210     def unapply(markup: Markup): Option[(Int, Int)] =
   211       if (markup.name == name) {
   212         val w = Width.unapply(markup.properties).getOrElse(0)
   213         val i = Indent.unapply(markup.properties).getOrElse(0)
   214         Some((w, i))
   215       }
   216       else None
   217   }
   218 
   219   val ITEM = "item"
   220   val BULLET = "bullet"
   221 
   222   val SEPARATOR = "separator"
   223 
   224 
   225   /* text properties */
   226 
   227   val WORDS = "words"
   228 
   229   val HIDDEN = "hidden"
   230 
   231 
   232   /* misc entities */
   233 
   234   val CLASS = "class"
   235   val TYPE_NAME = "type_name"
   236   val FIXED = "fixed"
   237   val CASE = "case"
   238   val CONSTANT = "constant"
   239   val DYNAMIC_FACT = "dynamic_fact"
   240 
   241 
   242   /* inner syntax */
   243 
   244   val TFREE = "tfree"
   245   val TVAR = "tvar"
   246   val FREE = "free"
   247   val SKOLEM = "skolem"
   248   val BOUND = "bound"
   249   val VAR = "var"
   250   val NUMERAL = "numeral"
   251   val LITERAL = "literal"
   252   val DELIMITER = "delimiter"
   253   val INNER_STRING = "inner_string"
   254   val INNER_CARTOUCHE = "inner_cartouche"
   255   val INNER_COMMENT = "inner_comment"
   256 
   257   val TOKEN_RANGE = "token_range"
   258 
   259   val SORTING = "sorting"
   260   val TYPING = "typing"
   261 
   262   val ATTRIBUTE = "attribute"
   263   val METHOD = "method"
   264 
   265 
   266   /* antiquotations */
   267 
   268   val ANTIQUOTED = "antiquoted"
   269   val ANTIQUOTE = "antiquote"
   270 
   271   val ML_ANTIQUOTATION = "ML_antiquotation"
   272   val DOCUMENT_ANTIQUOTATION = "document_antiquotation"
   273   val DOCUMENT_ANTIQUOTATION_OPTION = "document_antiquotation_option"
   274 
   275 
   276   /* text structure */
   277 
   278   val PARAGRAPH = "paragraph"
   279   val TEXT_FOLD = "text_fold"
   280 
   281 
   282   /* Markdown document structure */
   283 
   284   val MARKDOWN_PARAGRAPH = "markdown_paragraph"
   285   val Markdown_List = new Markup_String("markdown_list", "kind")
   286   val Markdown_Item = new Markup_Int("markdown_item", "depth")
   287 
   288 
   289   /* ML */
   290 
   291   val ML_KEYWORD1 = "ML_keyword1"
   292   val ML_KEYWORD2 = "ML_keyword2"
   293   val ML_KEYWORD3 = "ML_keyword3"
   294   val ML_DELIMITER = "ML_delimiter"
   295   val ML_TVAR = "ML_tvar"
   296   val ML_NUMERAL = "ML_numeral"
   297   val ML_CHAR = "ML_char"
   298   val ML_STRING = "ML_string"
   299   val ML_COMMENT = "ML_comment"
   300   val SML_STRING = "SML_string"
   301   val SML_COMMENT = "SML_comment"
   302 
   303   val ML_DEF = "ML_def"
   304   val ML_OPEN = "ML_open"
   305   val ML_STRUCTURE = "ML_structure"
   306   val ML_TYPING = "ML_typing"
   307 
   308   val ML_BREAKPOINT = "ML_breakpoint"
   309 
   310 
   311   /* outer syntax */
   312 
   313   val COMMAND = "command"
   314   val KEYWORD1 = "keyword1"
   315   val KEYWORD2 = "keyword2"
   316   val KEYWORD3 = "keyword3"
   317   val QUASI_KEYWORD = "quasi_keyword"
   318   val IMPROPER = "improper"
   319   val OPERATOR = "operator"
   320   val STRING = "string"
   321   val ALT_STRING = "alt_string"
   322   val VERBATIM = "verbatim"
   323   val CARTOUCHE = "cartouche"
   324   val COMMENT = "comment"
   325 
   326 
   327   /* timing */
   328 
   329   val Elapsed = new Properties.Double("elapsed")
   330   val CPU = new Properties.Double("cpu")
   331   val GC = new Properties.Double("gc")
   332 
   333   object Timing_Properties
   334   {
   335     def apply(timing: isabelle.Timing): Properties.T =
   336       Elapsed(timing.elapsed.seconds) ::: CPU(timing.cpu.seconds) ::: GC(timing.gc.seconds)
   337 
   338     def unapply(props: Properties.T): Option[isabelle.Timing] =
   339       (props, props, props) match {
   340         case (Elapsed(elapsed), CPU(cpu), GC(gc)) =>
   341           Some(new isabelle.Timing(Time.seconds(elapsed), Time.seconds(cpu), Time.seconds(gc)))
   342         case _ => None
   343       }
   344   }
   345 
   346   val TIMING = "timing"
   347 
   348   object Timing
   349   {
   350     def apply(timing: isabelle.Timing): Markup = Markup(TIMING, Timing_Properties(timing))
   351 
   352     def unapply(markup: Markup): Option[isabelle.Timing] =
   353       markup match {
   354         case Markup(TIMING, Timing_Properties(timing)) => Some(timing)
   355         case _ => None
   356       }
   357   }
   358 
   359 
   360   /* command timing */
   361 
   362   val COMMAND_TIMING = "command_timing"
   363 
   364 
   365   /* toplevel */
   366 
   367   val SUBGOALS = "subgoals"
   368   val PROOF_STATE = "proof_state"
   369 
   370   val GOAL = "goal"
   371   val SUBGOAL = "subgoal"
   372 
   373 
   374   /* command status */
   375 
   376   val TASK = "task"
   377 
   378   val ACCEPTED = "accepted"
   379   val FORKED = "forked"
   380   val JOINED = "joined"
   381   val RUNNING = "running"
   382   val FINISHED = "finished"
   383   val FAILED = "failed"
   384 
   385 
   386   /* interactive documents */
   387 
   388   val VERSION = "version"
   389   val ASSIGN = "assign"
   390 
   391 
   392   /* prover process */
   393 
   394   val PROVER_COMMAND = "prover_command"
   395   val PROVER_ARG = "prover_arg"
   396 
   397 
   398   /* messages */
   399 
   400   val INIT = "init"
   401   val STATUS = "status"
   402   val REPORT = "report"
   403   val RESULT = "result"
   404   val WRITELN = "writeln"
   405   val STATE = "state"
   406   val INFORMATION = "information"
   407   val TRACING = "tracing"
   408   val WARNING = "warning"
   409   val LEGACY = "legacy"
   410   val ERROR = "error"
   411   val PROTOCOL = "protocol"
   412   val SYSTEM = "system"
   413   val STDOUT = "stdout"
   414   val STDERR = "stderr"
   415   val EXIT = "exit"
   416 
   417   val WRITELN_MESSAGE = "writeln_message"
   418   val STATE_MESSAGE = "state_message"
   419   val INFORMATION_MESSAGE = "information_message"
   420   val TRACING_MESSAGE = "tracing_message"
   421   val WARNING_MESSAGE = "warning_message"
   422   val LEGACY_MESSAGE = "legacy_message"
   423   val ERROR_MESSAGE = "error_message"
   424 
   425   val messages = Map(
   426     WRITELN -> WRITELN_MESSAGE,
   427     STATE -> STATE_MESSAGE,
   428     INFORMATION -> INFORMATION_MESSAGE,
   429     TRACING -> TRACING_MESSAGE,
   430     WARNING -> WARNING_MESSAGE,
   431     LEGACY -> LEGACY_MESSAGE,
   432     ERROR -> ERROR_MESSAGE)
   433 
   434   val message: String => String = messages.withDefault((s: String) => s)
   435 
   436   val Return_Code = new Properties.Int("return_code")
   437 
   438   val NO_REPORT = "no_report"
   439 
   440   val BAD = "bad"
   441 
   442   val INTENSIFY = "intensify"
   443 
   444 
   445   /* active areas */
   446 
   447   val BROWSER = "browser"
   448   val GRAPHVIEW = "graphview"
   449 
   450   val SENDBACK = "sendback"
   451   val PADDING = "padding"
   452   val PADDING_LINE = (PADDING, "line")
   453   val PADDING_COMMAND = (PADDING, "command")
   454 
   455   val DIALOG = "dialog"
   456   val Result = new Properties.String(RESULT)
   457 
   458 
   459   /* protocol message functions */
   460 
   461   val FUNCTION = "function"
   462   val Function = new Properties.String(FUNCTION)
   463 
   464   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   465   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   466 
   467   object Protocol_Handler
   468   {
   469     def unapply(props: Properties.T): Option[(String)] =
   470       props match {
   471         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   472         case _ => None
   473       }
   474   }
   475 
   476   val INVOKE_SCALA = "invoke_scala"
   477   object Invoke_Scala
   478   {
   479     def unapply(props: Properties.T): Option[(String, String)] =
   480       props match {
   481         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   482         case _ => None
   483       }
   484   }
   485 
   486   val CANCEL_SCALA = "cancel_scala"
   487   object Cancel_Scala
   488   {
   489     def unapply(props: Properties.T): Option[String] =
   490       props match {
   491         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   492         case _ => None
   493       }
   494   }
   495 
   496   object ML_Statistics
   497   {
   498     def unapply(props: Properties.T): Option[Properties.T] =
   499       props match {
   500         case (FUNCTION, "ML_statistics") :: stats => Some(stats)
   501         case _ => None
   502       }
   503   }
   504 
   505   object Task_Statistics
   506   {
   507     def unapply(props: Properties.T): Option[Properties.T] =
   508       props match {
   509         case (FUNCTION, "task_statistics") :: stats => Some(stats)
   510         case _ => None
   511       }
   512   }
   513 
   514   val LOADING_THEORY = "loading_theory"
   515   object Loading_Theory
   516   {
   517     def unapply(props: Properties.T): Option[String] =
   518       props match {
   519         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   520         case _ => None
   521       }
   522   }
   523 
   524   val BUILD_THEORIES_RESULT = "build_theories_result"
   525   object Build_Theories_Result
   526   {
   527     def unapply(props: Properties.T): Option[String] =
   528       props match {
   529         case List((FUNCTION, BUILD_THEORIES_RESULT), (ID, id)) => Some(id)
   530         case _ => None
   531       }
   532   }
   533 
   534   val PRINT_OPERATIONS = "print_operations"
   535 
   536 
   537   /* debugger output */
   538 
   539   val DEBUGGER_STATE = "debugger_state"
   540   object Debugger_State
   541   {
   542     def unapply(props: Properties.T): Option[String] =
   543       props match {
   544         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   545         case _ => None
   546       }
   547   }
   548 
   549   val DEBUGGER_OUTPUT = "debugger_output"
   550   object Debugger_Output
   551   {
   552     def unapply(props: Properties.T): Option[String] =
   553       props match {
   554         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   555         case _ => None
   556       }
   557   }
   558 
   559 
   560   /* simplifier trace */
   561 
   562   val SIMP_TRACE_PANEL = "simp_trace_panel"
   563 
   564   val SIMP_TRACE_LOG = "simp_trace_log"
   565   val SIMP_TRACE_STEP = "simp_trace_step"
   566   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   567   val SIMP_TRACE_HINT = "simp_trace_hint"
   568   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   569 
   570   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   571   object Simp_Trace_Cancel
   572   {
   573     def unapply(props: Properties.T): Option[Long] =
   574       props match {
   575         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   576         case _ => None
   577       }
   578   }
   579 }
   580 
   581 
   582 sealed case class Markup(name: String, properties: Properties.T)
   583 {
   584   def markup(s: String): String =
   585     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   586 }