src/Pure/PIDE/markup.scala
author wenzelm
Mon Oct 30 17:06:02 2017 +0100 (19 months ago)
changeset 66942 91a21a5631ae
parent 66873 9953ae603a23
child 67322 734a4e44b159
permissions -rw-r--r--
proper order of initialization (amending 9953ae603a23);
     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 KIND = "kind"
    44   val Kind = new Properties.String(KIND)
    45 
    46   val CONTENT = "content"
    47   val Content = new Properties.String(CONTENT)
    48 
    49   val SERIAL = "serial"
    50   val Serial = new Properties.Long(SERIAL)
    51 
    52   val INSTANCE = "instance"
    53   val Instance = new Properties.String(INSTANCE)
    54 
    55 
    56   /* basic markup */
    57 
    58   val Empty = Markup("", Nil)
    59   val Broken = Markup("broken", Nil)
    60 
    61   class Markup_String(val name: String, prop: String)
    62   {
    63     private val Prop = new Properties.String(prop)
    64 
    65     def apply(s: String): Markup = Markup(name, Prop(s))
    66     def unapply(markup: Markup): Option[String] =
    67       if (markup.name == name) Prop.unapply(markup.properties) else None
    68   }
    69 
    70   class Markup_Int(val name: String, prop: String)
    71   {
    72     private val Prop = new Properties.Int(prop)
    73 
    74     def apply(i: Int): Markup = Markup(name, Prop(i))
    75     def unapply(markup: Markup): Option[Int] =
    76       if (markup.name == name) Prop.unapply(markup.properties) else None
    77   }
    78 
    79   class Markup_Long(val name: String, prop: String)
    80   {
    81     private val Prop = new Properties.Long(prop)
    82 
    83     def apply(i: Long): Markup = Markup(name, Prop(i))
    84     def unapply(markup: Markup): Option[Long] =
    85       if (markup.name == name) Prop.unapply(markup.properties) else None
    86   }
    87 
    88 
    89   /* formal entities */
    90 
    91   val BINDING = "binding"
    92   val ENTITY = "entity"
    93 
    94   object Entity
    95   {
    96     val Def = new Properties.Long("def")
    97     val Ref = new Properties.Long("ref")
    98 
    99     def unapply(markup: Markup): Option[(String, String)] =
   100       markup match {
   101         case Markup(ENTITY, props) =>
   102           val kind = Kind.unapply(props).getOrElse("")
   103           val name = Name.unapply(props).getOrElse("")
   104           Some((kind, name))
   105         case _ => None
   106       }
   107   }
   108 
   109 
   110   /* completion */
   111 
   112   val COMPLETION = "completion"
   113   val NO_COMPLETION = "no_completion"
   114 
   115 
   116   /* position */
   117 
   118   val LINE = "line"
   119   val END_LINE = "line"
   120   val OFFSET = "offset"
   121   val END_OFFSET = "end_offset"
   122   val FILE = "file"
   123   val ID = "id"
   124 
   125   val DEF_LINE = "def_line"
   126   val DEF_OFFSET = "def_offset"
   127   val DEF_END_OFFSET = "def_end_offset"
   128   val DEF_FILE = "def_file"
   129   val DEF_ID = "def_id"
   130 
   131   val POSITION_PROPERTIES = Set(LINE, OFFSET, END_OFFSET, FILE, ID)
   132   val POSITION = "position"
   133 
   134 
   135   /* expression */
   136 
   137   val EXPRESSION = "expression"
   138   object Expression
   139   {
   140     def unapply(markup: Markup): Option[String] =
   141       markup match {
   142         case Markup(EXPRESSION, Kind(kind)) => Some(kind)
   143         case Markup(EXPRESSION, _) => Some("")
   144         case _ => None
   145       }
   146   }
   147 
   148 
   149   /* citation */
   150 
   151   val CITATION = "citation"
   152   val Citation = new Markup_String(CITATION, NAME)
   153 
   154 
   155   /* embedded languages */
   156 
   157   val Symbols = new Properties.Boolean("symbols")
   158   val Antiquotes = new Properties.Boolean("antiquotes")
   159   val Delimited = new Properties.Boolean("delimited")
   160 
   161   val LANGUAGE = "language"
   162   object Language
   163   {
   164     val ML = "ML"
   165     val SML = "SML"
   166     val PATH = "path"
   167     val UNKNOWN = "unknown"
   168 
   169     def unapply(markup: Markup): Option[(String, Boolean, Boolean, Boolean)] =
   170       markup match {
   171         case Markup(LANGUAGE, props) =>
   172           (props, props, props, props) match {
   173             case (Name(name), Symbols(symbols), Antiquotes(antiquotes), Delimited(delimited)) =>
   174               Some((name, symbols, antiquotes, delimited))
   175             case _ => None
   176           }
   177         case _ => None
   178       }
   179   }
   180 
   181 
   182   /* external resources */
   183 
   184   val PATH = "path"
   185   val Path = new Markup_String(PATH, NAME)
   186 
   187   val URL = "url"
   188   val Url = new Markup_String(URL, NAME)
   189 
   190   val DOC = "doc"
   191   val Doc = new Markup_String(DOC, NAME)
   192 
   193 
   194   /* pretty printing */
   195 
   196   val Consistent = new Properties.Boolean("consistent")
   197   val Indent = new Properties.Int("indent")
   198   val Width = new Properties.Int("width")
   199 
   200   object Block
   201   {
   202     val name = "block"
   203     def apply(c: Boolean, i: Int): Markup =
   204       Markup(name,
   205         (if (c) Consistent(c) else Nil) :::
   206         (if (i != 0) Indent(i) else Nil))
   207     def unapply(markup: Markup): Option[(Boolean, Int)] =
   208       if (markup.name == name) {
   209         val c = Consistent.unapply(markup.properties).getOrElse(false)
   210         val i = Indent.unapply(markup.properties).getOrElse(0)
   211         Some((c, i))
   212       }
   213       else None
   214   }
   215 
   216   object Break
   217   {
   218     val name = "break"
   219     def apply(w: Int, i: Int): Markup =
   220       Markup(name,
   221         (if (w != 0) Width(w) else Nil) :::
   222         (if (i != 0) Indent(i) else Nil))
   223     def unapply(markup: Markup): Option[(Int, Int)] =
   224       if (markup.name == name) {
   225         val w = Width.unapply(markup.properties).getOrElse(0)
   226         val i = Indent.unapply(markup.properties).getOrElse(0)
   227         Some((w, i))
   228       }
   229       else None
   230   }
   231 
   232   val ITEM = "item"
   233   val BULLET = "bullet"
   234 
   235   val SEPARATOR = "separator"
   236 
   237 
   238   /* text properties */
   239 
   240   val WORDS = "words"
   241 
   242   val HIDDEN = "hidden"
   243 
   244 
   245   /* misc entities */
   246 
   247   val CLASS = "class"
   248   val TYPE_NAME = "type_name"
   249   val FIXED = "fixed"
   250   val CASE = "case"
   251   val CONSTANT = "constant"
   252   val DYNAMIC_FACT = "dynamic_fact"
   253 
   254 
   255   /* inner syntax */
   256 
   257   val TFREE = "tfree"
   258   val TVAR = "tvar"
   259   val FREE = "free"
   260   val SKOLEM = "skolem"
   261   val BOUND = "bound"
   262   val VAR = "var"
   263   val NUMERAL = "numeral"
   264   val LITERAL = "literal"
   265   val DELIMITER = "delimiter"
   266   val INNER_STRING = "inner_string"
   267   val INNER_CARTOUCHE = "inner_cartouche"
   268   val INNER_COMMENT = "inner_comment"
   269 
   270   val TOKEN_RANGE = "token_range"
   271 
   272   val SORTING = "sorting"
   273   val TYPING = "typing"
   274   val CLASS_PARAMETER = "class_parameter"
   275 
   276   val ATTRIBUTE = "attribute"
   277   val METHOD = "method"
   278 
   279 
   280   /* antiquotations */
   281 
   282   val ANTIQUOTED = "antiquoted"
   283   val ANTIQUOTE = "antiquote"
   284 
   285   val ML_ANTIQUOTATION = "ML_antiquotation"
   286   val DOCUMENT_ANTIQUOTATION = "document_antiquotation"
   287   val DOCUMENT_ANTIQUOTATION_OPTION = "document_antiquotation_option"
   288 
   289 
   290   /* text structure */
   291 
   292   val PARAGRAPH = "paragraph"
   293   val TEXT_FOLD = "text_fold"
   294 
   295 
   296   /* Markdown document structure */
   297 
   298   val MARKDOWN_PARAGRAPH = "markdown_paragraph"
   299   val Markdown_List = new Markup_String("markdown_list", "kind")
   300   val Markdown_Item = new Markup_Int("markdown_item", "depth")
   301 
   302 
   303   /* ML */
   304 
   305   val ML_KEYWORD1 = "ML_keyword1"
   306   val ML_KEYWORD2 = "ML_keyword2"
   307   val ML_KEYWORD3 = "ML_keyword3"
   308   val ML_DELIMITER = "ML_delimiter"
   309   val ML_TVAR = "ML_tvar"
   310   val ML_NUMERAL = "ML_numeral"
   311   val ML_CHAR = "ML_char"
   312   val ML_STRING = "ML_string"
   313   val ML_COMMENT = "ML_comment"
   314   val SML_STRING = "SML_string"
   315   val SML_COMMENT = "SML_comment"
   316 
   317   val ML_DEF = "ML_def"
   318   val ML_OPEN = "ML_open"
   319   val ML_STRUCTURE = "ML_structure"
   320   val ML_TYPING = "ML_typing"
   321 
   322   val ML_BREAKPOINT = "ML_breakpoint"
   323 
   324 
   325   /* outer syntax */
   326 
   327   val COMMAND = "command"
   328   val KEYWORD = "keyword"
   329   val KEYWORD1 = "keyword1"
   330   val KEYWORD2 = "keyword2"
   331   val KEYWORD3 = "keyword3"
   332   val QUASI_KEYWORD = "quasi_keyword"
   333   val IMPROPER = "improper"
   334   val OPERATOR = "operator"
   335   val STRING = "string"
   336   val ALT_STRING = "alt_string"
   337   val VERBATIM = "verbatim"
   338   val CARTOUCHE = "cartouche"
   339   val COMMENT = "comment"
   340 
   341 
   342   /* timing */
   343 
   344   val Elapsed = new Properties.Double("elapsed")
   345   val CPU = new Properties.Double("cpu")
   346   val GC = new Properties.Double("gc")
   347 
   348   object Timing_Properties
   349   {
   350     def apply(timing: isabelle.Timing): Properties.T =
   351       Elapsed(timing.elapsed.seconds) ::: CPU(timing.cpu.seconds) ::: GC(timing.gc.seconds)
   352 
   353     def unapply(props: Properties.T): Option[isabelle.Timing] =
   354       (props, props, props) match {
   355         case (Elapsed(elapsed), CPU(cpu), GC(gc)) =>
   356           Some(new isabelle.Timing(Time.seconds(elapsed), Time.seconds(cpu), Time.seconds(gc)))
   357         case _ => None
   358       }
   359   }
   360 
   361   val TIMING = "timing"
   362 
   363   object Timing
   364   {
   365     def apply(timing: isabelle.Timing): Markup = Markup(TIMING, Timing_Properties(timing))
   366 
   367     def unapply(markup: Markup): Option[isabelle.Timing] =
   368       markup match {
   369         case Markup(TIMING, Timing_Properties(timing)) => Some(timing)
   370         case _ => None
   371       }
   372   }
   373 
   374 
   375   /* process result */
   376 
   377   val Return_Code = new Properties.Int("return_code")
   378 
   379   object Process_Result
   380   {
   381     def apply(result: Process_Result): Properties.T =
   382       Return_Code(result.rc) :::
   383         (if (result.timing.is_zero) Nil else Timing_Properties(result.timing))
   384 
   385     def unapply(props: Properties.T): Option[Process_Result] =
   386       props match {
   387         case Return_Code(rc) =>
   388           val timing = Timing_Properties.unapply(props).getOrElse(isabelle.Timing.zero)
   389           Some(isabelle.Process_Result(rc, timing = timing))
   390         case _ => None
   391       }
   392   }
   393 
   394 
   395   /* command indentation */
   396 
   397   object Command_Indent
   398   {
   399     val name = "command_indent"
   400     def unapply(markup: Markup): Option[Int] =
   401       if (markup.name == name) Indent.unapply(markup.properties) else None
   402   }
   403 
   404 
   405   /* goals */
   406 
   407   val GOAL = "goal"
   408   val SUBGOAL = "subgoal"
   409 
   410 
   411   /* command status */
   412 
   413   val TASK = "task"
   414 
   415   val ACCEPTED = "accepted"
   416   val FORKED = "forked"
   417   val JOINED = "joined"
   418   val RUNNING = "running"
   419   val FINISHED = "finished"
   420   val FAILED = "failed"
   421   val CONSOLIDATED = "consolidated"
   422 
   423 
   424   /* interactive documents */
   425 
   426   val VERSION = "version"
   427   val ASSIGN = "assign"
   428 
   429 
   430   /* prover process */
   431 
   432   val PROVER_COMMAND = "prover_command"
   433   val PROVER_ARG = "prover_arg"
   434 
   435 
   436   /* messages */
   437 
   438   val INIT = "init"
   439   val STATUS = "status"
   440   val REPORT = "report"
   441   val RESULT = "result"
   442   val WRITELN = "writeln"
   443   val STATE = "state"
   444   val INFORMATION = "information"
   445   val TRACING = "tracing"
   446   val WARNING = "warning"
   447   val LEGACY = "legacy"
   448   val ERROR = "error"
   449   val PROTOCOL = "protocol"
   450   val SYSTEM = "system"
   451   val STDOUT = "stdout"
   452   val STDERR = "stderr"
   453   val EXIT = "exit"
   454 
   455   val WRITELN_MESSAGE = "writeln_message"
   456   val STATE_MESSAGE = "state_message"
   457   val INFORMATION_MESSAGE = "information_message"
   458   val TRACING_MESSAGE = "tracing_message"
   459   val WARNING_MESSAGE = "warning_message"
   460   val LEGACY_MESSAGE = "legacy_message"
   461   val ERROR_MESSAGE = "error_message"
   462 
   463   val messages = Map(
   464     WRITELN -> WRITELN_MESSAGE,
   465     STATE -> STATE_MESSAGE,
   466     INFORMATION -> INFORMATION_MESSAGE,
   467     TRACING -> TRACING_MESSAGE,
   468     WARNING -> WARNING_MESSAGE,
   469     LEGACY -> LEGACY_MESSAGE,
   470     ERROR -> ERROR_MESSAGE)
   471 
   472   val message: String => String = messages.withDefault((s: String) => s)
   473 
   474   val NO_REPORT = "no_report"
   475 
   476   val BAD = "bad"
   477 
   478   val INTENSIFY = "intensify"
   479 
   480 
   481   /* active areas */
   482 
   483   val BROWSER = "browser"
   484   val GRAPHVIEW = "graphview"
   485 
   486   val SENDBACK = "sendback"
   487   val PADDING = "padding"
   488   val PADDING_LINE = (PADDING, "line")
   489   val PADDING_COMMAND = (PADDING, "command")
   490 
   491   val DIALOG = "dialog"
   492   val Result = new Properties.String(RESULT)
   493 
   494   val JEDIT_ACTION = "jedit_action"
   495 
   496 
   497   /* protocol message functions */
   498 
   499   val FUNCTION = "function"
   500   val Function = new Properties.String(FUNCTION)
   501 
   502   val COMMAND_TIMING: Properties.Entry = (FUNCTION, "command_timing")
   503   val THEORY_TIMING: Properties.Entry = (FUNCTION, "theory_timing")
   504 
   505   val Assign_Update: Properties.T = List((FUNCTION, "assign_update"))
   506   val Removed_Versions: Properties.T = List((FUNCTION, "removed_versions"))
   507 
   508   object Protocol_Handler
   509   {
   510     def unapply(props: Properties.T): Option[(String)] =
   511       props match {
   512         case List((FUNCTION, "protocol_handler"), (NAME, name)) => Some(name)
   513         case _ => None
   514       }
   515   }
   516 
   517   val INVOKE_SCALA = "invoke_scala"
   518   object Invoke_Scala
   519   {
   520     def unapply(props: Properties.T): Option[(String, String)] =
   521       props match {
   522         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   523         case _ => None
   524       }
   525   }
   526 
   527   val CANCEL_SCALA = "cancel_scala"
   528   object Cancel_Scala
   529   {
   530     def unapply(props: Properties.T): Option[String] =
   531       props match {
   532         case List((FUNCTION, CANCEL_SCALA), (ID, id)) => Some(id)
   533         case _ => None
   534       }
   535   }
   536 
   537   object ML_Statistics
   538   {
   539     def unapply(props: Properties.T): Option[Properties.T] =
   540       props match {
   541         case (FUNCTION, "ML_statistics") :: props => Some(props)
   542         case _ => None
   543       }
   544   }
   545 
   546   object Task_Statistics
   547   {
   548     def unapply(props: Properties.T): Option[Properties.T] =
   549       props match {
   550         case (FUNCTION, "task_statistics") :: props => Some(props)
   551         case _ => None
   552       }
   553   }
   554 
   555   val LOADING_THEORY = "loading_theory"
   556   object Loading_Theory
   557   {
   558     def unapply(props: Properties.T): Option[String] =
   559       props match {
   560         case List((FUNCTION, LOADING_THEORY), (NAME, name)) => Some(name)
   561         case _ => None
   562       }
   563   }
   564 
   565   val BUILD_SESSION_FINISHED = "build_session_finished"
   566   val Build_Session_Finished: Properties.T = List((FUNCTION, BUILD_SESSION_FINISHED))
   567 
   568   val PRINT_OPERATIONS = "print_operations"
   569 
   570 
   571   /* debugger output */
   572 
   573   val DEBUGGER_STATE = "debugger_state"
   574   object Debugger_State
   575   {
   576     def unapply(props: Properties.T): Option[String] =
   577       props match {
   578         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   579         case _ => None
   580       }
   581   }
   582 
   583   val DEBUGGER_OUTPUT = "debugger_output"
   584   object Debugger_Output
   585   {
   586     def unapply(props: Properties.T): Option[String] =
   587       props match {
   588         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   589         case _ => None
   590       }
   591   }
   592 
   593 
   594   /* simplifier trace */
   595 
   596   val SIMP_TRACE_PANEL = "simp_trace_panel"
   597 
   598   val SIMP_TRACE_LOG = "simp_trace_log"
   599   val SIMP_TRACE_STEP = "simp_trace_step"
   600   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   601   val SIMP_TRACE_HINT = "simp_trace_hint"
   602   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   603 
   604   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   605   object Simp_Trace_Cancel
   606   {
   607     def unapply(props: Properties.T): Option[Long] =
   608       props match {
   609         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   610         case _ => None
   611       }
   612   }
   613 
   614 
   615   /* XML data representation */
   616 
   617   def encode: XML.Encode.T[Markup] = (markup: Markup) =>
   618   {
   619     import XML.Encode._
   620     pair(string, properties)((markup.name, markup.properties))
   621   }
   622 
   623   def decode: XML.Decode.T[Markup] = (body: XML.Body) =>
   624   {
   625     import XML.Decode._
   626     val (name, props) = pair(string, properties)(body)
   627     Markup(name, props)
   628   }
   629 }
   630 
   631 
   632 sealed case class Markup(name: String, properties: Properties.T)
   633 {
   634   def markup(s: String): String =
   635     YXML.string_of_tree(XML.Elem(this, List(XML.Text(s))))
   636 
   637   def update_properties(more_props: Properties.T): Markup =
   638     if (more_props.isEmpty) this
   639     else Markup(name, (more_props :\ properties) { case (p, ps) => Properties.put(ps, p) })
   640 
   641   def + (entry: Properties.Entry): Markup =
   642     Markup(name, Properties.put(properties, entry))
   643 }