progs/detokenise.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 02 Oct 2019 14:05:36 +0100
changeset 645 30943d5491b6
parent 644 b4f5714485e1
child 686 05cfce0fdef7
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] = {
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    26
  import scala.util.Using
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    27
  Using(new ObjectInputStream(new FileInputStream(fname))) {
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    28
    in => in.readObject.asInstanceOf[T]
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    29
  }
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
def main(args: Array[String]) = {
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
    33
  val fname = args(0)
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    34
  val tks = deserialise[List[Token]](fname).getOrElse(Nil)
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 642
diff changeset
    35
  println(s"Reading back from ${fname}:\n${tks.mkString("\n")}")  
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
}