Attic/detokenise.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 29 Sep 2022 20:54:02 +0100
changeset 876 771396fa6cc4
parent 742 b5b5583a3a08
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     1
// Detokenising the ouput of Tokeniser
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     2
//=====================================
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     3
//
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     4
// call with 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     5
//
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     6
//     scala detokenise.scala fib.tks
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     7
//
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
     8
//     scala detokenise.scala loops.tks
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
    10
object Detokenise {
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
import java.io._
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    13
import scala.util._ 
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
abstract class Token extends Serializable 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
case object T_SEMI extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
case object T_LPAREN extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
case object T_RPAREN extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
case class T_ID(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case class T_OP(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case class T_NUM(n: Int) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case class T_KWD(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case class T_STR(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    25
def deserialise[T](fname: String) : Try[T] = {
695
484b74bc057e deleted init function from boilerplate code
Christian Urban <urbanc@in.tum.de>
parents: 686
diff changeset
    26
  //import scala.util.Using
484b74bc057e deleted init function from boilerplate code
Christian Urban <urbanc@in.tum.de>
parents: 686
diff changeset
    27
  //Using(new ObjectInputStream(new FileInputStream(fname))) {
484b74bc057e deleted init function from boilerplate code
Christian Urban <urbanc@in.tum.de>
parents: 686
diff changeset
    28
  //  in => in.readObject.asInstanceOf[T]
484b74bc057e deleted init function from boilerplate code
Christian Urban <urbanc@in.tum.de>
parents: 686
diff changeset
    29
  //}
484b74bc057e deleted init function from boilerplate code
Christian Urban <urbanc@in.tum.de>
parents: 686
diff changeset
    30
  Try(new ObjectInputStream(new FileInputStream(fname))).get {
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    31
    in => in.readObject.asInstanceOf[T]
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    32
  }
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
def main(args: Array[String]) = {
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
    36
  val fname = args(0)
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    37
  val tks = deserialise[List[Token]](fname).getOrElse(Nil)
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    38
  println(s"Reading back from ${fname}:\n${tks.mkString(", ")}")  
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    42
}