src/Pure/PIDE/markup.scala
author wenzelm
Fri Aug 12 20:58:05 2016 +0200 (2016-08-12 ago)
changeset 63681 d2448471ffba
parent 63475 31016a88197b
child 64358 15c90b744481
permissions -rw-r--r--
active jEdit actions;
     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 
    90   object Entity
    91   {
    92     val Def = new Properties.Long("def")
    93     val Ref = new Properties.Long("ref")
    94 
    95     def unapply(markup: Markup): Option[(String, String)] =
    96       markup match {
    97         case Markup(ENTITY, props) =>
    98           val kind = Kind.unapply(props).getOrElse("")
    99           val name = Name.unapply(props).getOrElse("")
   100           Some((kind, name))
   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   val CLASS_PARAMETER = "class_parameter"
   271 
   272   val ATTRIBUTE = "attribute"
   273   val METHOD = "method"
   274 
   275 
   276   /* antiquotations */
   277 
   278   val ANTIQUOTED = "antiquoted"
   279   val ANTIQUOTE = "antiquote"
   280 
   281   val ML_ANTIQUOTATION = "ML_antiquotation"
   282   val DOCUMENT_ANTIQUOTATION = "document_antiquotation"
   283   val DOCUMENT_ANTIQUOTATION_OPTION = "document_antiquotation_option"
   284 
   285 
   286   /* text structure */
   287 
   288   val PARAGRAPH = "paragraph"
   289   val TEXT_FOLD = "text_fold"
   290 
   291 
   292   /* Markdown document structure */
   293 
   294   val MARKDOWN_PARAGRAPH = "markdown_paragraph"
   295   val Markdown_List = new Markup_String("markdown_list", "kind")
   296   val Markdown_Item = new Markup_Int("markdown_item", "depth")
   297 
   298 
   299   /* ML */
   300 
   301   val ML_KEYWORD1 = "ML_keyword1"
   302   val ML_KEYWORD2 = "ML_keyword2"
   303   val ML_KEYWORD3 = "ML_keyword3"
   304   val ML_DELIMITER = "ML_delimiter"
   305   val ML_TVAR = "ML_tvar"
   306   val ML_NUMERAL = "ML_numeral"
   307   val ML_CHAR = "ML_char"
   308   val ML_STRING = "ML_string"
   309   val ML_COMMENT = "ML_comment"
   310   val SML_STRING = "SML_string"
   311   val SML_COMMENT = "SML_comment"
   312 
   313   val ML_DEF = "ML_def"
   314   val ML_OPEN = "ML_open"
   315   val ML_STRUCTURE = "ML_structure"
   316   val ML_TYPING = "ML_typing"
   317 
   318   val ML_BREAKPOINT = "ML_breakpoint"
   319 
   320 
   321   /* outer syntax */
   322 
   323   val COMMAND = "command"
   324   val KEYWORD1 = "keyword1"
   325   val KEYWORD2 = "keyword2"
   326   val KEYWORD3 = "keyword3"
   327   val QUASI_KEYWORD = "quasi_keyword"
   328   val IMPROPER = "improper"
   329   val OPERATOR = "operator"
   330   val STRING = "string"
   331   val ALT_STRING = "alt_string"
   332   val VERBATIM = "verbatim"
   333   val CARTOUCHE = "cartouche"
   334   val COMMENT = "comment"
   335 
   336 
   337   /* timing */
   338 
   339   val Elapsed = new Properties.Double("elapsed")
   340   val CPU = new Properties.Double("cpu")
   341   val GC = new Properties.Double("gc")
   342 
   343   object Timing_Properties
   344   {
   345     def apply(timing: isabelle.Timing): Properties.T =
   346       Elapsed(timing.elapsed.seconds) ::: CPU(timing.cpu.seconds) ::: GC(timing.gc.seconds)
   347 
   348     def unapply(props: Properties.T): Option[isabelle.Timing] =
   349       (props, props, props) match {
   350         case (Elapsed(elapsed), CPU(cpu), GC(gc)) =>
   351           Some(new isabelle.Timing(Time.seconds(elapsed), Time.seconds(cpu), Time.seconds(gc)))
   352         case _ => None
   353       }
   354   }
   355 
   356   val TIMING = "timing"
   357 
   358   object Timing
   359   {
   360     def apply(timing: isabelle.Timing): Markup = Markup(TIMING, Timing_Properties(timing))
   361 
   362     def unapply(markup: Markup): Option[isabelle.Timing] =
   363       markup match {
   364         case Markup(TIMING, Timing_Properties(timing)) => Some(timing)
   365         case _ => None
   366       }
   367   }
   368 
   369 
   370   /* command timing */
   371 
   372   val COMMAND_TIMING = "command_timing"
   373 
   374 
   375   /* command indentation */
   376 
   377   object Command_Indent
   378   {
   379     val name = "command_indent"
   380     def unapply(markup: Markup): Option[Int] =
   381       if (markup.name == name) Indent.unapply(markup.properties) else None
   382   }
   383 
   384 
   385   /* goals */
   386 
   387   val GOAL = "goal"
   388   val SUBGOAL = "subgoal"
   389 
   390 
   391   /* command status */
   392 
   393   val TASK = "task"
   394 
   395   val ACCEPTED = "accepted"
   396   val FORKED = "forked"
   397   val JOINED = "joined"
   398   val RUNNING = "running"
   399   val FINISHED = "finished"
   400   val FAILED = "failed"
   401 
   402 
   403   /* interactive documents */
   404 
   405   val VERSION = "version"
   406   val ASSIGN = "assign"
   407 
   408 
   409   /* prover process */
   410 
   411   val PROVER_COMMAND = "prover_command"
   412   val PROVER_ARG = "prover_arg"
   413 
   414 
   415   /* messages */
   416 
   417   val INIT = "init"
   418   val STATUS = "status"
   419   val REPORT = "report"
   420   val RESULT = "result"
   421   val WRITELN = "writeln"
   422   val STATE = "state"
   423   val INFORMATION = "information"
   424   val TRACING = "tracing"
   425   val WARNING = "warning"
   426   val LEGACY = "legacy"
   427   val ERROR = "error"
   428   val PROTOCOL = "protocol"
   429   val SYSTEM = "system"
   430   val STDOUT = "stdout"
   431   val STDERR = "stderr"
   432   val EXIT = "exit"
   433 
   434   val WRITELN_MESSAGE = "writeln_message"
   435   val STATE_MESSAGE = "state_message"
   436   val INFORMATION_MESSAGE = "information_message"
   437   val TRACING_MESSAGE = "tracing_message"
   438   val WARNING_MESSAGE = "warning_message"
   439   val LEGACY_MESSAGE = "legacy_message"
   440   val ERROR_MESSAGE = "error_message"
   441 
   442   val messages = Map(
   443     WRITELN -> WRITELN_MESSAGE,
   444     STATE -> STATE_MESSAGE,
   445     INFORMATION -> INFORMATION_MESSAGE,
   446     TRACING -> TRACING_MESSAGE,
   447     WARNING -> WARNING_MESSAGE,
   448     LEGACY -> LEGACY_MESSAGE,
   449     ERROR -> ERROR_MESSAGE)
   450 
   451   val message: String => String = messages.withDefault((s: String) => s)
   452 
   453   val Return_Code = new Properties.Int("return_code")
   454 
   455   val NO_REPORT = "no_report"
   456 
   457   val BAD = "bad"
   458 
   459   val INTENSIFY = "intensify"
   460 
   461 
   462   /* active areas */
   463 
   464   val BROWSER = "browser"
   465   val GRAPHVIEW = "graphview"
   466 
   467   val SENDBACK = "sendback"
   468   val PADDING = "padding"
   469   val PADDING_LINE = (PADDING, "line")
   470   val PADDING_COMMAND = (PADDING, "command")
   471 
   472   val DIALOG = "dialog"
   473   val Result = new Properties.String(RESULT)
   474 
   475   val JEDIT_ACTION = "jedit_action"
   476 
   477 
   478   /* protocol message functions */
   479 
   480   val FUNCTION = "function"
   481   val Function = new Properties.String(FUNCTION)
   482 
   483   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   484   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   485 
   486   object Protocol_Handler
   487   {
   488     def unapply(props: Properties.T): Option[(String)] =
   489       props match {
   490         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   491         case _ => None
   492       }
   493   }
   494 
   495   val INVOKE_SCALA = "invoke_scala"
   496   object Invoke_Scala
   497   {
   498     def unapply(props: Properties.T): Option[(String, String)] =
   499       props match {
   500         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   501         case _ => None
   502       }
   503   }
   504 
   505   val CANCEL_SCALA = "cancel_scala"
   506   object Cancel_Scala
   507   {
   508     def unapply(props: Properties.T): Option[String] =
   509       props match {
   510         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   511         case _ => None
   512       }
   513   }
   514 
   515   object ML_Statistics
   516   {
   517     def unapply(props: Properties.T): Option[Properties.T] =
   518       props match {
   519         case (FUNCTION, "ML_statistics") :: stats => Some(stats)
   520         case _ => None
   521       }
   522   }
   523 
   524   object Task_Statistics
   525   {
   526     def unapply(props: Properties.T): Option[Properties.T] =
   527       props match {
   528         case (FUNCTION, "task_statistics") :: stats => Some(stats)
   529         case _ => None
   530       }
   531   }
   532 
   533   val LOADING_THEORY = "loading_theory"
   534   object Loading_Theory
   535   {
   536     def unapply(props: Properties.T): Option[String] =
   537       props match {
   538         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   539         case _ => None
   540       }
   541   }
   542 
   543   val BUILD_THEORIES_RESULT = "build_theories_result"
   544   object Build_Theories_Result
   545   {
   546     def unapply(props: Properties.T): Option[String] =
   547       props match {
   548         case List((FUNCTION, BUILD_THEORIES_RESULT), (ID, id)) => Some(id)
   549         case _ => None
   550       }
   551   }
   552 
   553   val PRINT_OPERATIONS = "print_operations"
   554 
   555 
   556   /* debugger output */
   557 
   558   val DEBUGGER_STATE = "debugger_state"
   559   object Debugger_State
   560   {
   561     def unapply(props: Properties.T): Option[String] =
   562       props match {
   563         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   564         case _ => None
   565       }
   566   }
   567 
   568   val DEBUGGER_OUTPUT = "debugger_output"
   569   object Debugger_Output
   570   {
   571     def unapply(props: Properties.T): Option[String] =
   572       props match {
   573         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   574         case _ => None
   575       }
   576   }
   577 
   578 
   579   /* simplifier trace */
   580 
   581   val SIMP_TRACE_PANEL = "simp_trace_panel"
   582 
   583   val SIMP_TRACE_LOG = "simp_trace_log"
   584   val SIMP_TRACE_STEP = "simp_trace_step"
   585   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   586   val SIMP_TRACE_HINT = "simp_trace_hint"
   587   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   588 
   589   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   590   object Simp_Trace_Cancel
   591   {
   592     def unapply(props: Properties.T): Option[Long] =
   593       props match {
   594         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   595         case _ => None
   596       }
   597   }
   598 }
   599 
   600 
   601 sealed case class Markup(name: String, properties: Properties.T)
   602 {
   603   def markup(s: String): String =
   604     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   605 }