progs/detokenise.scala
changeset 645 30943d5491b6
parent 644 b4f5714485e1
child 686 05cfce0fdef7
equal deleted inserted replaced
644:b4f5714485e1 645:30943d5491b6
     8 //     scala detokenise.scala loops.tks
     8 //     scala detokenise.scala loops.tks
     9 
     9 
    10 object Detokenise {
    10 object Detokenise {
    11 
    11 
    12 import java.io._
    12 import java.io._
       
    13 import scala.util._ 
    13 
    14 
    14 abstract class Token extends Serializable 
    15 abstract class Token extends Serializable 
    15 case object T_SEMI extends Token
    16 case object T_SEMI extends Token
    16 case object T_LPAREN extends Token
    17 case object T_LPAREN extends Token
    17 case object T_RPAREN extends Token
    18 case object T_RPAREN extends Token
    19 case class T_OP(s: String) extends Token
    20 case class T_OP(s: String) extends Token
    20 case class T_NUM(n: Int) extends Token
    21 case class T_NUM(n: Int) extends Token
    21 case class T_KWD(s: String) extends Token
    22 case class T_KWD(s: String) extends Token
    22 case class T_STR(s: String) extends Token
    23 case class T_STR(s: String) extends Token
    23 
    24 
    24 def deserialise[T](fname: String) : T = {
    25 def deserialise[T](fname: String) : Try[T] = {
    25   val in = new ObjectInputStream(new FileInputStream(fname))
    26   import scala.util.Using
    26   val data = in.readObject.asInstanceOf[T]
    27   Using(new ObjectInputStream(new FileInputStream(fname))) {
    27   in.close
    28     in => in.readObject.asInstanceOf[T]
    28   data
    29   }
    29 }
    30 }
    30 
    31 
    31 def main(args: Array[String]) = {
    32 def main(args: Array[String]) = {
    32   val fname = args(0)
    33   val fname = args(0)
    33   val tks = deserialise[List[Token]](fname)
    34   val tks = deserialise[List[Token]](fname).getOrElse(Nil)
    34   println(s"Reading back from ${fname}:\n${tks.mkString("\n")}")  
    35   println(s"Reading back from ${fname}:\n${tks.mkString("\n")}")  
    35 }
    36 }
    36 
    37 
    37 
    38 
    38 }
    39 }