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] = {
|
|
26 |
import scala.util.Using
|
|
27 |
Using(new ObjectInputStream(new FileInputStream(fname))) {
|
|
28 |
in => in.readObject.asInstanceOf[T]
|
|
29 |
}
|
642
|
30 |
}
|
|
31 |
|
|
32 |
def main(args: Array[String]) = {
|
644
|
33 |
val fname = args(0)
|
645
|
34 |
val tks = deserialise[List[Token]](fname).getOrElse(Nil)
|
644
|
35 |
println(s"Reading back from ${fname}:\n${tks.mkString("\n")}")
|
642
|
36 |
}
|
|
37 |
|
|
38 |
|
|
39 |
} |