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