author | Christian Urban <christian.urban@kcl.ac.uk> |
Thu, 30 Jul 2020 13:50:54 +0100 | |
changeset 742 | b5b5583a3a08 |
parent 695 | progs/detokenise.scala@484b74bc057e |
permissions | -rw-r--r-- |
644 | 1 |
// Detokenising the ouput of Tokeniser |
2 |
//===================================== |
|
3 |
// |
|
4 |
// call with |
|
5 |
// |
|
6 |
// scala detokenise.scala fib.tks |
|
7 |
// |
|
8 |
// scala detokenise.scala loops.tks |
|
642 | 9 |
|
644 | 10 |
object Detokenise { |
642 | 11 |
|
12 |
import java.io._ |
|
645 | 13 |
import scala.util._ |
642 | 14 |
|
15 |
abstract class Token extends Serializable |
|
16 |
case object T_SEMI extends Token |
|
17 |
case object T_LPAREN extends Token |
|
18 |
case object T_RPAREN extends Token |
|
19 |
case class T_ID(s: String) extends Token |
|
20 |
case class T_OP(s: String) extends Token |
|
21 |
case class T_NUM(n: Int) extends Token |
|
22 |
case class T_KWD(s: String) extends Token |
|
23 |
case class T_STR(s: String) extends Token |
|
24 |
||
645 | 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 | 31 |
in => in.readObject.asInstanceOf[T] |
32 |
} |
|
642 | 33 |
} |
34 |
||
35 |
def main(args: Array[String]) = { |
|
644 | 36 |
val fname = args(0) |
645 | 37 |
val tks = deserialise[List[Token]](fname).getOrElse(Nil) |
686 | 38 |
println(s"Reading back from ${fname}:\n${tks.mkString(", ")}") |
642 | 39 |
} |
40 |
||
41 |
||
686 | 42 |
} |