src/Pure/General/markup.scala
author wenzelm
Mon Jul 11 16:48:02 2011 +0200 (2011-07-11)
changeset 43748 c70bd78ec83c
parent 43746 a41f618c641d
child 43780 2cb2310d68b6
permissions -rw-r--r--
JVM method invocation service via Scala layer;
     1 /*  Title:      Pure/General/markup.scala
     2     Author:     Makarius
     3 
     4 Common markup elements.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 object Markup
    11 {
    12   /* plain values */
    13 
    14   object Int
    15   {
    16     def apply(x: scala.Int): String = x.toString
    17     def unapply(s: String): Option[scala.Int] =
    18       try { Some(Integer.parseInt(s)) }
    19       catch { case _: NumberFormatException => None }
    20   }
    21 
    22   object Long
    23   {
    24     def apply(x: scala.Long): String = x.toString
    25     def unapply(s: String): Option[scala.Long] =
    26       try { Some(java.lang.Long.parseLong(s)) }
    27       catch { case _: NumberFormatException => None }
    28   }
    29 
    30   object Double
    31   {
    32     def apply(x: scala.Double): String = x.toString
    33     def unapply(s: String): Option[scala.Double] =
    34       try { Some(java.lang.Double.parseDouble(s)) }
    35       catch { case _: NumberFormatException => None }
    36   }
    37 
    38 
    39   /* named properties */
    40 
    41   class Property(val name: String)
    42   {
    43     def apply(value: String): List[(String, String)] = List((name, value))
    44     def unapply(props: List[(String, String)]): Option[String] =
    45       props.find(_._1 == name).map(_._2)
    46   }
    47 
    48   class Int_Property(name: String)
    49   {
    50     def apply(value: scala.Int): List[(String, String)] = List((name, Int(value)))
    51     def unapply(props: List[(String, String)]): Option[Int] =
    52       props.find(_._1 == name) match {
    53         case None => None
    54         case Some((_, value)) => Int.unapply(value)
    55       }
    56   }
    57 
    58   class Long_Property(name: String)
    59   {
    60     def apply(value: scala.Long): List[(String, String)] = List((name, Long(value)))
    61     def unapply(props: List[(String, String)]): Option[Long] =
    62       props.find(_._1 == name) match {
    63         case None => None
    64         case Some((_, value)) => Long.unapply(value)
    65       }
    66   }
    67 
    68   class Double_Property(name: String)
    69   {
    70     def apply(value: scala.Double): List[(String, String)] = List((name, Double(value)))
    71     def unapply(props: List[(String, String)]): Option[Double] =
    72       props.find(_._1 == name) match {
    73         case None => None
    74         case Some((_, value)) => Double.unapply(value)
    75       }
    76   }
    77 
    78 
    79   /* empty */
    80 
    81   val Empty = Markup("", Nil)
    82 
    83 
    84   /* misc properties */
    85 
    86   val NAME = "name"
    87   val Name = new Property(NAME)
    88 
    89   val KIND = "kind"
    90   val Kind = new Property(KIND)
    91 
    92 
    93   /* formal entities */
    94 
    95   val BINDING = "binding"
    96   val ENTITY = "entity"
    97   val DEF = "def"
    98   val REF = "ref"
    99 
   100   object Entity
   101   {
   102     def unapply(markup: Markup): Option[(String, String)] =
   103       markup match {
   104         case Markup(ENTITY, props @ Kind(kind)) =>
   105           props match {
   106             case Name(name) => Some(kind, name)
   107             case _ => None
   108           }
   109         case _ => None
   110       }
   111   }
   112 
   113 
   114   /* position */
   115 
   116   val LINE = "line"
   117   val OFFSET = "offset"
   118   val END_OFFSET = "end_offset"
   119   val FILE = "file"
   120   val ID = "id"
   121 
   122   val DEF_LINE = "def_line"
   123   val DEF_OFFSET = "def_offset"
   124   val DEF_END_OFFSET = "def_end_offset"
   125   val DEF_FILE = "def_file"
   126   val DEF_ID = "def_id"
   127 
   128   val POSITION_PROPERTIES = Set(LINE, OFFSET, END_OFFSET, FILE, ID)
   129   val POSITION = "position"
   130 
   131 
   132   /* path */
   133 
   134   val PATH = "path"
   135 
   136   object Path
   137   {
   138     def unapply(markup: Markup): Option[String] =
   139       markup match {
   140         case Markup(PATH, Name(name)) => Some(name)
   141         case _ => None
   142       }
   143   }
   144 
   145 
   146   /* pretty printing */
   147 
   148   val Indent = new Int_Property("indent")
   149   val BLOCK = "block"
   150   val Width = new Int_Property("width")
   151   val BREAK = "break"
   152 
   153 
   154   /* hidden text */
   155 
   156   val HIDDEN = "hidden"
   157 
   158 
   159   /* logical entities */
   160 
   161   val CLASS = "class"
   162   val TYPE = "type"
   163   val FIXED = "fixed"
   164   val CONSTANT = "constant"
   165 
   166   val DYNAMIC_FACT = "dynamic_fact"
   167 
   168 
   169   /* inner syntax */
   170 
   171   val TFREE = "tfree"
   172   val TVAR = "tvar"
   173   val FREE = "free"
   174   val SKOLEM = "skolem"
   175   val BOUND = "bound"
   176   val VAR = "var"
   177   val NUM = "num"
   178   val FLOAT = "float"
   179   val XNUM = "xnum"
   180   val XSTR = "xstr"
   181   val LITERAL = "literal"
   182   val DELIMITER = "delimiter"
   183   val INNER_STRING = "inner_string"
   184   val INNER_COMMENT = "inner_comment"
   185 
   186   val TOKEN_RANGE = "token_range"
   187 
   188   val SORT = "sort"
   189   val TYP = "typ"
   190   val TERM = "term"
   191   val PROP = "prop"
   192 
   193   val ATTRIBUTE = "attribute"
   194   val METHOD = "method"
   195 
   196 
   197   /* embedded source text */
   198 
   199   val ML_SOURCE = "ML_source"
   200   val DOC_SOURCE = "doc_source"
   201 
   202   val ANTIQ = "antiq"
   203   val ML_ANTIQUOTATION = "ML antiquotation"
   204   val DOCUMENT_ANTIQUOTATION = "document antiquotation"
   205   val DOCUMENT_ANTIQUOTATION_OPTION = "document antiquotation option"
   206 
   207 
   208   /* ML syntax */
   209 
   210   val ML_KEYWORD = "ML_keyword"
   211   val ML_DELIMITER = "ML_delimiter"
   212   val ML_IDENT = "ML_ident"
   213   val ML_TVAR = "ML_tvar"
   214   val ML_NUMERAL = "ML_numeral"
   215   val ML_CHAR = "ML_char"
   216   val ML_STRING = "ML_string"
   217   val ML_COMMENT = "ML_comment"
   218   val ML_MALFORMED = "ML_malformed"
   219 
   220   val ML_DEF = "ML_def"
   221   val ML_OPEN = "ML_open"
   222   val ML_STRUCT = "ML_struct"
   223   val ML_TYPING = "ML_typing"
   224 
   225 
   226   /* outer syntax */
   227 
   228   val KEYWORD_DECL = "keyword_decl"
   229   val COMMAND_DECL = "command_decl"
   230 
   231   val KEYWORD = "keyword"
   232   val OPERATOR = "operator"
   233   val COMMAND = "command"
   234   val IDENT = "ident"
   235   val STRING = "string"
   236   val ALTSTRING = "altstring"
   237   val VERBATIM = "verbatim"
   238   val COMMENT = "comment"
   239   val CONTROL = "control"
   240   val MALFORMED = "malformed"
   241 
   242   val COMMAND_SPAN = "command_span"
   243   val IGNORED_SPAN = "ignored_span"
   244   val MALFORMED_SPAN = "malformed_span"
   245 
   246 
   247   /* theory loader */
   248 
   249   val LOADED_THEORY = "loaded_theory"
   250 
   251 
   252   /* timing */
   253 
   254   val TIMING = "timing"
   255   val ELAPSED = "elapsed"
   256   val CPU = "cpu"
   257   val GC = "gc"
   258 
   259   object Timing
   260   {
   261     def apply(timing: isabelle.Timing): Markup =
   262       Markup(TIMING, List(
   263         (ELAPSED, Double(timing.elapsed.seconds)),
   264         (CPU, Double(timing.cpu.seconds)),
   265         (GC, Double(timing.gc.seconds))))
   266     def unapply(markup: Markup): Option[isabelle.Timing] =
   267       markup match {
   268         case Markup(TIMING, List(
   269           (ELAPSED, Double(elapsed)), (CPU, Double(cpu)), (GC, Double(gc)))) =>
   270             Some(new isabelle.Timing(Time.seconds(elapsed), Time.seconds(cpu), Time.seconds(gc)))
   271         case _ => None
   272       }
   273   }
   274 
   275 
   276   /* toplevel */
   277 
   278   val SUBGOALS = "subgoals"
   279   val PROOF_STATE = "proof_state"
   280 
   281   val STATE = "state"
   282   val SUBGOAL = "subgoal"
   283   val SENDBACK = "sendback"
   284   val HILITE = "hilite"
   285 
   286 
   287   /* command status */
   288 
   289   val TASK = "task"
   290 
   291   val FORKED = "forked"
   292   val JOINED = "joined"
   293   val FAILED = "failed"
   294   val FINISHED = "finished"
   295 
   296 
   297   /* interactive documents */
   298 
   299   val VERSION = "version"
   300   val EXEC = "exec"
   301   val ASSIGN = "assign"
   302   val EDIT = "edit"
   303 
   304 
   305   /* prover process */
   306 
   307   val PROVER_COMMAND = "prover_command"
   308   val PROVER_ARG = "prover_arg"
   309 
   310 
   311   /* messages */
   312 
   313   val Serial = new Long_Property("serial")
   314 
   315   val MESSAGE = "message"
   316 
   317   val INIT = "init"
   318   val STATUS = "status"
   319   val REPORT = "report"
   320   val WRITELN = "writeln"
   321   val TRACING = "tracing"
   322   val WARNING = "warning"
   323   val ERROR = "error"
   324   val RAW = "raw"
   325   val SYSTEM = "system"
   326   val STDOUT = "stdout"
   327   val EXIT = "exit"
   328 
   329   val NO_REPORT = "no_report"
   330 
   331   val BAD = "bad"
   332 
   333   val READY = "ready"
   334 
   335 
   336   /* raw message functions */
   337 
   338   val FUNCTION = "function"
   339   val Function = new Property(FUNCTION)
   340 
   341   val INVOKE_SCALA = "invoke_scala"
   342   object Invoke_Scala
   343   {
   344     def unapply(props: List[(String, String)]): Option[(String, String)] =
   345       props match {
   346         case List((FUNCTION, INVOKE_SCALA), (NAME, name), (ID, id)) => Some((name, id))
   347         case _ => None
   348       }
   349   }
   350 
   351 
   352   /* system data */
   353 
   354   val Data = Markup("data", Nil)
   355 }
   356 
   357 sealed case class Markup(name: String, properties: List[(String, String)])