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