src/Pure/System/scala.scala
changeset 71873 a7b81dd9954e
parent 71872 b5191ededb6c
child 71874 9d31fe4ecaea
equal deleted inserted replaced
71872:b5191ededb6c 71873:a7b81dd9954e
    98 
    98 
    99   /** invoke Scala functions from ML **/
    99   /** invoke Scala functions from ML **/
   100 
   100 
   101   /* registered functions */
   101   /* registered functions */
   102 
   102 
   103   object Fun
   103   sealed case class Fun(name: String, apply: String => String)
   104   {
       
   105     def apply(name: String, fn: String => String): Fun =
       
   106       new Fun(name, fn, false)
       
   107 
       
   108     def yxml(name: String, fn: XML.Body => XML.Body): Fun =
       
   109       new Fun(name, s => YXML.string_of_body(fn(YXML.parse_body(s))), true)
       
   110   }
       
   111   class Fun private(val name: String, val fn: String => String, val yxml: Boolean)
       
   112   {
       
   113     def decl: (String, Boolean) = (name, yxml)
       
   114   }
       
   115 
   104 
   116   lazy val functions: List[Fun] =
   105   lazy val functions: List[Fun] =
   117     Isabelle_System.services.collect { case c: Isabelle_Scala_Functions => c.functions.toList }.flatten
   106     Isabelle_System.services.collect { case c: Isabelle_Scala_Functions => c.functions.toList }.flatten
   118 
   107 
   119 
   108 
   123   {
   112   {
   124     val NULL, OK, ERROR, FAIL, INTERRUPT = Value
   113     val NULL, OK, ERROR, FAIL, INTERRUPT = Value
   125   }
   114   }
   126 
   115 
   127   def function(name: String, arg: String): (Tag.Value, String) =
   116   def function(name: String, arg: String): (Tag.Value, String) =
   128     functions.collectFirst({ case fun if fun.name == name => fun.fn }) match {
   117     functions.find(fun => fun.name == name) match {
   129       case Some(fn) =>
   118       case Some(fun) =>
   130         Exn.capture { fn(arg) } match {
   119         Exn.capture { fun.apply(arg) } match {
   131           case Exn.Res(null) => (Tag.NULL, "")
   120           case Exn.Res(null) => (Tag.NULL, "")
   132           case Exn.Res(res) => (Tag.OK, res)
   121           case Exn.Res(res) => (Tag.OK, res)
   133           case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, "")
   122           case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, "")
   134           case Exn.Exn(e) => (Tag.ERROR, Exn.message(e))
   123           case Exn.Exn(e) => (Tag.ERROR, Exn.message(e))
   135         }
   124         }
   207 
   196 
   208 class Isabelle_Scala_Functions(val functions: Scala.Fun*) extends Isabelle_System.Service
   197 class Isabelle_Scala_Functions(val functions: Scala.Fun*) extends Isabelle_System.Service
   209 
   198 
   210 class Functions extends Isabelle_Scala_Functions(
   199 class Functions extends Isabelle_Scala_Functions(
   211   Scala.Fun("echo", identity),
   200   Scala.Fun("echo", identity),
   212   Scala.Fun.yxml("echo_yxml", identity),
       
   213   Scala.Fun("scala_check", Scala.check_yxml),
   201   Scala.Fun("scala_check", Scala.check_yxml),
   214   Scala.Fun("check_bibtex_database", Bibtex.check_database_yxml))
   202   Scala.Fun("check_bibtex_database", Bibtex.check_database_yxml))