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