src/Pure/PIDE/markup.scala
changeset 75393 87ebf5a50283
parent 74887 56247fdb8bbb
child 75394 42267c650205
equal deleted inserted replaced
75388:b3ca4a6ed74b 75393:87ebf5a50283
     5 */
     5 */
     6 
     6 
     7 package isabelle
     7 package isabelle
     8 
     8 
     9 
     9 
    10 object Markup
    10 object Markup {
    11 {
       
    12   /* elements */
    11   /* elements */
    13 
    12 
    14   object Elements
    13   object Elements {
    15   {
       
    16     def apply(elems: Set[String]): Elements = new Elements(elems)
    14     def apply(elems: Set[String]): Elements = new Elements(elems)
    17     def apply(elems: String*): Elements = apply(Set(elems: _*))
    15     def apply(elems: String*): Elements = apply(Set(elems: _*))
    18     val empty: Elements = apply()
    16     val empty: Elements = apply()
    19     val full: Elements =
    17     val full: Elements =
    20       new Elements(Set.empty)
    18       new Elements(Set.empty) {
    21       {
       
    22         override def apply(elem: String): Boolean = true
    19         override def apply(elem: String): Boolean = true
    23         override def toString: String = "Elements.full"
    20         override def toString: String = "Elements.full"
    24       }
    21       }
    25   }
    22   }
    26 
    23 
    27   sealed class Elements private[Markup](private val rep: Set[String])
    24   sealed class Elements private[Markup](private val rep: Set[String]) {
    28   {
       
    29     def apply(elem: String): Boolean = rep.contains(elem)
    25     def apply(elem: String): Boolean = rep.contains(elem)
    30     def + (elem: String): Elements = new Elements(rep + elem)
    26     def + (elem: String): Elements = new Elements(rep + elem)
    31     def ++ (elems: Elements): Elements = new Elements(rep ++ elems.rep)
    27     def ++ (elems: Elements): Elements = new Elements(rep ++ elems.rep)
    32     def - (elem: String): Elements = new Elements(rep - elem)
    28     def - (elem: String): Elements = new Elements(rep - elem)
    33     def -- (elems: Elements): Elements = new Elements(rep -- elems.rep)
    29     def -- (elems: Elements): Elements = new Elements(rep -- elems.rep)
    59   /* basic markup */
    55   /* basic markup */
    60 
    56 
    61   val Empty: Markup = Markup("", Nil)
    57   val Empty: Markup = Markup("", Nil)
    62   val Broken: Markup = Markup("broken", Nil)
    58   val Broken: Markup = Markup("broken", Nil)
    63 
    59 
    64   class Markup_Elem(val name: String)
    60   class Markup_Elem(val name: String) {
    65   {
       
    66     def apply(props: Properties.T = Nil): Markup = Markup(name, props)
    61     def apply(props: Properties.T = Nil): Markup = Markup(name, props)
    67     def unapply(markup: Markup): Option[Properties.T] =
    62     def unapply(markup: Markup): Option[Properties.T] =
    68       if (markup.name == name) Some(markup.properties) else None
    63       if (markup.name == name) Some(markup.properties) else None
    69   }
    64   }
    70 
    65 
    71   class Markup_String(val name: String, prop: String)
    66   class Markup_String(val name: String, prop: String) {
    72   {
       
    73     val Prop: Properties.String = new Properties.String(prop)
    67     val Prop: Properties.String = new Properties.String(prop)
    74 
    68 
    75     def apply(s: String): Markup = Markup(name, Prop(s))
    69     def apply(s: String): Markup = Markup(name, Prop(s))
    76     def unapply(markup: Markup): Option[String] =
    70     def unapply(markup: Markup): Option[String] =
    77       if (markup.name == name) Prop.unapply(markup.properties) else None
    71       if (markup.name == name) Prop.unapply(markup.properties) else None
    78     def get(markup: Markup): String = unapply(markup).getOrElse("")
    72     def get(markup: Markup): String = unapply(markup).getOrElse("")
    79   }
    73   }
    80 
    74 
    81   class Markup_Int(val name: String, prop: String)
    75   class Markup_Int(val name: String, prop: String) {
    82   {
       
    83     val Prop: Properties.Int = new Properties.Int(prop)
    76     val Prop: Properties.Int = new Properties.Int(prop)
    84 
    77 
    85     def apply(i: Int): Markup = Markup(name, Prop(i))
    78     def apply(i: Int): Markup = Markup(name, Prop(i))
    86     def unapply(markup: Markup): Option[Int] =
    79     def unapply(markup: Markup): Option[Int] =
    87       if (markup.name == name) Prop.unapply(markup.properties) else None
    80       if (markup.name == name) Prop.unapply(markup.properties) else None
    88     def get(markup: Markup): Int = unapply(markup).getOrElse(0)
    81     def get(markup: Markup): Int = unapply(markup).getOrElse(0)
    89   }
    82   }
    90 
    83 
    91   class Markup_Long(val name: String, prop: String)
    84   class Markup_Long(val name: String, prop: String) {
    92   {
       
    93     val Prop: Properties.Long = new Properties.Long(prop)
    85     val Prop: Properties.Long = new Properties.Long(prop)
    94 
    86 
    95     def apply(i: Long): Markup = Markup(name, Prop(i))
    87     def apply(i: Long): Markup = Markup(name, Prop(i))
    96     def unapply(markup: Markup): Option[Long] =
    88     def unapply(markup: Markup): Option[Long] =
    97       if (markup.name == name) Prop.unapply(markup.properties) else None
    89       if (markup.name == name) Prop.unapply(markup.properties) else None
   112   /* formal entities */
   104   /* formal entities */
   113 
   105 
   114   val BINDING = "binding"
   106   val BINDING = "binding"
   115   val ENTITY = "entity"
   107   val ENTITY = "entity"
   116 
   108 
   117   object Entity
   109   object Entity {
   118   {
       
   119     val Def = new Markup_Long(ENTITY, "def")
   110     val Def = new Markup_Long(ENTITY, "def")
   120     val Ref = new Markup_Long(ENTITY, "ref")
   111     val Ref = new Markup_Long(ENTITY, "ref")
   121 
   112 
   122     object Occ
   113     object Occ {
   123     {
       
   124       def unapply(markup: Markup): Option[Long] =
   114       def unapply(markup: Markup): Option[Long] =
   125         Def.unapply(markup) orElse Ref.unapply(markup)
   115         Def.unapply(markup) orElse Ref.unapply(markup)
   126     }
   116     }
   127 
   117 
   128     def unapply(markup: Markup): Option[(String, String)] =
   118     def unapply(markup: Markup): Option[(String, String)] =
   174 
   164 
   175 
   165 
   176   /* expression */
   166   /* expression */
   177 
   167 
   178   val EXPRESSION = "expression"
   168   val EXPRESSION = "expression"
   179   object Expression
   169   object Expression {
   180   {
       
   181     def unapply(markup: Markup): Option[String] =
   170     def unapply(markup: Markup): Option[String] =
   182       markup match {
   171       markup match {
   183         case Markup(EXPRESSION, props) => Some(Kind.get(props))
   172         case Markup(EXPRESSION, props) => Some(Kind.get(props))
   184         case _ => None
   173         case _ => None
   185       }
   174       }
   197   val Symbols = new Properties.Boolean("symbols")
   186   val Symbols = new Properties.Boolean("symbols")
   198   val Antiquotes = new Properties.Boolean("antiquotes")
   187   val Antiquotes = new Properties.Boolean("antiquotes")
   199   val Delimited = new Properties.Boolean("delimited")
   188   val Delimited = new Properties.Boolean("delimited")
   200 
   189 
   201   val LANGUAGE = "language"
   190   val LANGUAGE = "language"
   202   object Language
   191   object Language {
   203   {
       
   204     val DOCUMENT = "document"
   192     val DOCUMENT = "document"
   205     val ML = "ML"
   193     val ML = "ML"
   206     val SML = "SML"
   194     val SML = "SML"
   207     val PATH = "path"
   195     val PATH = "path"
   208     val UNKNOWN = "unknown"
   196     val UNKNOWN = "unknown"
   216             case _ => None
   204             case _ => None
   217           }
   205           }
   218         case _ => None
   206         case _ => None
   219       }
   207       }
   220 
   208 
   221     object Path
   209     object Path {
   222     {
       
   223       def unapply(markup: Markup): Option[Boolean] =
   210       def unapply(markup: Markup): Option[Boolean] =
   224         markup match {
   211         markup match {
   225           case Language(PATH, _, _, delimited) => Some(delimited)
   212           case Language(PATH, _, _, delimited) => Some(delimited)
   226           case _ => None
   213           case _ => None
   227         }
   214         }
   248 
   235 
   249   val Consistent = new Properties.Boolean("consistent")
   236   val Consistent = new Properties.Boolean("consistent")
   250   val Indent = new Properties.Int("indent")
   237   val Indent = new Properties.Int("indent")
   251   val Width = new Properties.Int("width")
   238   val Width = new Properties.Int("width")
   252 
   239 
   253   object Block
   240   object Block {
   254   {
       
   255     val name = "block"
   241     val name = "block"
   256     def apply(c: Boolean, i: Int): Markup =
   242     def apply(c: Boolean, i: Int): Markup =
   257       Markup(name,
   243       Markup(name,
   258         (if (c) Consistent(c) else Nil) :::
   244         (if (c) Consistent(c) else Nil) :::
   259         (if (i != 0) Indent(i) else Nil))
   245         (if (i != 0) Indent(i) else Nil))
   264         Some((c, i))
   250         Some((c, i))
   265       }
   251       }
   266       else None
   252       else None
   267   }
   253   }
   268 
   254 
   269   object Break
   255   object Break {
   270   {
       
   271     val name = "break"
   256     val name = "break"
   272     def apply(w: Int, i: Int): Markup =
   257     def apply(w: Int, i: Int): Markup =
   273       Markup(name,
   258       Markup(name,
   274         (if (w != 0) Width(w) else Nil) :::
   259         (if (w != 0) Width(w) else Nil) :::
   275         (if (i != 0) Indent(i) else Nil))
   260         (if (i != 0) Indent(i) else Nil))
   358   val PLAIN_TEXT = "plain_text"
   343   val PLAIN_TEXT = "plain_text"
   359 
   344 
   360   val PARAGRAPH = "paragraph"
   345   val PARAGRAPH = "paragraph"
   361   val TEXT_FOLD = "text_fold"
   346   val TEXT_FOLD = "text_fold"
   362 
   347 
   363   object Document_Tag extends Markup_String("document_tag", NAME)
   348   object Document_Tag extends Markup_String("document_tag", NAME) {
   364   {
       
   365     val IMPORTANT = "important"
   349     val IMPORTANT = "important"
   366     val UNIMPORTANT = "unimportant"
   350     val UNIMPORTANT = "unimportant"
   367   }
   351   }
   368 
   352 
   369 
   353 
   450 
   434 
   451   val Elapsed = new Properties.Double("elapsed")
   435   val Elapsed = new Properties.Double("elapsed")
   452   val CPU = new Properties.Double("cpu")
   436   val CPU = new Properties.Double("cpu")
   453   val GC = new Properties.Double("gc")
   437   val GC = new Properties.Double("gc")
   454 
   438 
   455   object Timing_Properties
   439   object Timing_Properties {
   456   {
       
   457     def apply(timing: isabelle.Timing): Properties.T =
   440     def apply(timing: isabelle.Timing): Properties.T =
   458       Elapsed(timing.elapsed.seconds) ::: CPU(timing.cpu.seconds) ::: GC(timing.gc.seconds)
   441       Elapsed(timing.elapsed.seconds) ::: CPU(timing.cpu.seconds) ::: GC(timing.gc.seconds)
   459 
   442 
   460     def unapply(props: Properties.T): Option[isabelle.Timing] =
   443     def unapply(props: Properties.T): Option[isabelle.Timing] =
   461       (props, props, props) match {
   444       (props, props, props) match {
   468       unapply(props).getOrElse(isabelle.Timing.zero)
   451       unapply(props).getOrElse(isabelle.Timing.zero)
   469   }
   452   }
   470 
   453 
   471   val TIMING = "timing"
   454   val TIMING = "timing"
   472 
   455 
   473   object Timing
   456   object Timing {
   474   {
       
   475     def apply(timing: isabelle.Timing): Markup = Markup(TIMING, Timing_Properties(timing))
   457     def apply(timing: isabelle.Timing): Markup = Markup(TIMING, Timing_Properties(timing))
   476 
   458 
   477     def unapply(markup: Markup): Option[isabelle.Timing] =
   459     def unapply(markup: Markup): Option[isabelle.Timing] =
   478       markup match {
   460       markup match {
   479         case Markup(TIMING, Timing_Properties(timing)) => Some(timing)
   461         case Markup(TIMING, Timing_Properties(timing)) => Some(timing)
   484 
   466 
   485   /* process result */
   467   /* process result */
   486 
   468 
   487   val Return_Code = new Properties.Int("return_code")
   469   val Return_Code = new Properties.Int("return_code")
   488 
   470 
   489   object Process_Result
   471   object Process_Result {
   490   {
       
   491     def apply(result: Process_Result): Properties.T =
   472     def apply(result: Process_Result): Properties.T =
   492       Return_Code(result.rc) :::
   473       Return_Code(result.rc) :::
   493         (if (result.timing.is_zero) Nil else Timing_Properties(result.timing))
   474         (if (result.timing.is_zero) Nil else Timing_Properties(result.timing))
   494 
   475 
   495     def unapply(props: Properties.T): Option[Process_Result] =
   476     def unapply(props: Properties.T): Option[Process_Result] =
   592 
   573 
   593   val COUNT = "count"
   574   val COUNT = "count"
   594   val ML_PROFILING_ENTRY = "ML_profiling_entry"
   575   val ML_PROFILING_ENTRY = "ML_profiling_entry"
   595   val ML_PROFILING = "ML_profiling"
   576   val ML_PROFILING = "ML_profiling"
   596 
   577 
   597   object ML_Profiling
   578   object ML_Profiling {
   598   {
       
   599     def unapply_entry(tree: XML.Tree): Option[isabelle.ML_Profiling.Entry] =
   579     def unapply_entry(tree: XML.Tree): Option[isabelle.ML_Profiling.Entry] =
   600       tree match {
   580       tree match {
   601         case XML.Elem(Markup(ML_PROFILING_ENTRY, List((NAME, name), (COUNT, Value.Long(count)))), _) =>
   581         case XML.Elem(Markup(ML_PROFILING_ENTRY, List((NAME, name), (COUNT, Value.Long(count)))), _) =>
   602           Some(isabelle.ML_Profiling.Entry(name, count))
   582           Some(isabelle.ML_Profiling.Entry(name, count))
   603         case _ => None
   583         case _ => None
   631 
   611 
   632   /* protocol message functions */
   612   /* protocol message functions */
   633 
   613 
   634   val FUNCTION = "function"
   614   val FUNCTION = "function"
   635 
   615 
   636   class Function(val name: String)
   616   class Function(val name: String) {
   637   {
       
   638     val PROPERTY: Properties.Entry = (FUNCTION, name)
   617     val PROPERTY: Properties.Entry = (FUNCTION, name)
   639   }
   618   }
   640 
   619 
   641   class Properties_Function(name: String) extends Function(name)
   620   class Properties_Function(name: String) extends Function(name) {
   642   {
       
   643     def unapply(props: Properties.T): Option[Properties.T] =
   621     def unapply(props: Properties.T): Option[Properties.T] =
   644       props match {
   622       props match {
   645         case PROPERTY :: args => Some(args)
   623         case PROPERTY :: args => Some(args)
   646         case _ => None
   624         case _ => None
   647       }
   625       }
   648   }
   626   }
   649 
   627 
   650   class Name_Function(name: String) extends Function(name)
   628   class Name_Function(name: String) extends Function(name) {
   651   {
       
   652     def unapply(props: Properties.T): Option[String] =
   629     def unapply(props: Properties.T): Option[String] =
   653       props match {
   630       props match {
   654         case List(PROPERTY, (NAME, a)) => Some(a)
   631         case List(PROPERTY, (NAME, a)) => Some(a)
   655         case _ => None
   632         case _ => None
   656       }
   633       }
   657   }
   634   }
   658 
   635 
   659   object ML_Statistics extends Function("ML_statistics")
   636   object ML_Statistics extends Function("ML_statistics") {
   660   {
       
   661     def unapply(props: Properties.T): Option[(Long, String)] =
   637     def unapply(props: Properties.T): Option[(Long, String)] =
   662       props match {
   638       props match {
   663         case List(PROPERTY, ("pid", Value.Long(pid)), ("stats_dir", stats_dir)) =>
   639         case List(PROPERTY, ("pid", Value.Long(pid)), ("stats_dir", stats_dir)) =>
   664           Some((pid, stats_dir))
   640           Some((pid, stats_dir))
   665         case _ => None
   641         case _ => None
   669   val command_timing_properties: Set[String] = Set(FILE, OFFSET, NAME, Elapsed.name)
   645   val command_timing_properties: Set[String] = Set(FILE, OFFSET, NAME, Elapsed.name)
   670   def command_timing_property(entry: Properties.Entry): Boolean = command_timing_properties(entry._1)
   646   def command_timing_property(entry: Properties.Entry): Boolean = command_timing_properties(entry._1)
   671 
   647 
   672   object Command_Timing extends Properties_Function("command_timing")
   648   object Command_Timing extends Properties_Function("command_timing")
   673   object Theory_Timing extends Properties_Function("theory_timing")
   649   object Theory_Timing extends Properties_Function("theory_timing")
   674   object Session_Timing extends Properties_Function("session_timing")
   650   object Session_Timing extends Properties_Function("session_timing") {
   675   {
       
   676     val Threads = new Properties.Int("threads")
   651     val Threads = new Properties.Int("threads")
   677   }
   652   }
   678   object Task_Statistics extends Properties_Function("task_statistics")
   653   object Task_Statistics extends Properties_Function("task_statistics")
   679 
   654 
   680   object Loading_Theory extends Properties_Function("loading_theory")
   655   object Loading_Theory extends Properties_Function("loading_theory")
   682 
   657 
   683   object Commands_Accepted extends Function("commands_accepted")
   658   object Commands_Accepted extends Function("commands_accepted")
   684   object Assign_Update extends Function("assign_update")
   659   object Assign_Update extends Function("assign_update")
   685   object Removed_Versions extends Function("removed_versions")
   660   object Removed_Versions extends Function("removed_versions")
   686 
   661 
   687   object Invoke_Scala extends Function("invoke_scala")
   662   object Invoke_Scala extends Function("invoke_scala") {
   688   {
       
   689     def unapply(props: Properties.T): Option[(String, String)] =
   663     def unapply(props: Properties.T): Option[(String, String)] =
   690       props match {
   664       props match {
   691         case List(PROPERTY, (NAME, name), (ID, id)) => Some((name, id))
   665         case List(PROPERTY, (NAME, name), (ID, id)) => Some((name, id))
   692         case _ => None
   666         case _ => None
   693       }
   667       }
   694   }
   668   }
   695 
   669 
   696   object Cancel_Scala extends Function("cancel_scala")
   670   object Cancel_Scala extends Function("cancel_scala") {
   697   {
       
   698     def unapply(props: Properties.T): Option[String] =
   671     def unapply(props: Properties.T): Option[String] =
   699       props match {
   672       props match {
   700         case List(PROPERTY, (ID, id)) => Some(id)
   673         case List(PROPERTY, (ID, id)) => Some(id)
   701         case _ => None
   674         case _ => None
   702       }
   675       }
   715 
   688 
   716 
   689 
   717   /* debugger output */
   690   /* debugger output */
   718 
   691 
   719   val DEBUGGER_STATE = "debugger_state"
   692   val DEBUGGER_STATE = "debugger_state"
   720   object Debugger_State
   693   object Debugger_State {
   721   {
       
   722     def unapply(props: Properties.T): Option[String] =
   694     def unapply(props: Properties.T): Option[String] =
   723       props match {
   695       props match {
   724         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   696         case List((FUNCTION, DEBUGGER_STATE), (NAME, name)) => Some(name)
   725         case _ => None
   697         case _ => None
   726       }
   698       }
   727   }
   699   }
   728 
   700 
   729   val DEBUGGER_OUTPUT = "debugger_output"
   701   val DEBUGGER_OUTPUT = "debugger_output"
   730   object Debugger_Output
   702   object Debugger_Output {
   731   {
       
   732     def unapply(props: Properties.T): Option[String] =
   703     def unapply(props: Properties.T): Option[String] =
   733       props match {
   704       props match {
   734         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   705         case List((FUNCTION, DEBUGGER_OUTPUT), (NAME, name)) => Some(name)
   735         case _ => None
   706         case _ => None
   736       }
   707       }
   746   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   717   val SIMP_TRACE_RECURSE = "simp_trace_recurse"
   747   val SIMP_TRACE_HINT = "simp_trace_hint"
   718   val SIMP_TRACE_HINT = "simp_trace_hint"
   748   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   719   val SIMP_TRACE_IGNORE = "simp_trace_ignore"
   749 
   720 
   750   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   721   val SIMP_TRACE_CANCEL = "simp_trace_cancel"
   751   object Simp_Trace_Cancel
   722   object Simp_Trace_Cancel {
   752   {
       
   753     def unapply(props: Properties.T): Option[Long] =
   723     def unapply(props: Properties.T): Option[Long] =
   754       props match {
   724       props match {
   755         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   725         case (FUNCTION, SIMP_TRACE_CANCEL) :: Serial(i) => Some(i)
   756         case _ => None
   726         case _ => None
   757       }
   727       }
   758   }
   728   }
   759 
   729 
   760 
   730 
   761   /* XML data representation */
   731   /* XML data representation */
   762 
   732 
   763   def encode: XML.Encode.T[Markup] = (markup: Markup) =>
   733   def encode: XML.Encode.T[Markup] = (markup: Markup) => {
   764   {
       
   765     import XML.Encode._
   734     import XML.Encode._
   766     pair(string, properties)((markup.name, markup.properties))
   735     pair(string, properties)((markup.name, markup.properties))
   767   }
   736   }
   768 
   737 
   769   def decode: XML.Decode.T[Markup] = (body: XML.Body) =>
   738   def decode: XML.Decode.T[Markup] = (body: XML.Body) => {
   770   {
       
   771     import XML.Decode._
   739     import XML.Decode._
   772     val (name, props) = pair(string, properties)(body)
   740     val (name, props) = pair(string, properties)(body)
   773     Markup(name, props)
   741     Markup(name, props)
   774   }
   742   }
   775 }
   743 }
   776 
   744 
   777 
   745 
   778 sealed case class Markup(name: String, properties: Properties.T)
   746 sealed case class Markup(name: String, properties: Properties.T) {
   779 {
       
   780   def is_empty: Boolean = name.isEmpty
   747   def is_empty: Boolean = name.isEmpty
   781 
   748 
   782   def position_properties: Position.T = properties.filter(Markup.position_property)
   749   def position_properties: Position.T = properties.filter(Markup.position_property)
   783 
   750 
   784   def markup(s: String): String =
   751   def markup(s: String): String =