progs/detokenise.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 01 Oct 2019 23:40:25 +0100
changeset 642 064afa8fc1d9
child 644 b4f5714485e1
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// A simple lexer inspired by work of Sulzmann & Lu
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==================================================
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
object Delexer {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
import java.io._
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
abstract class Token extends Serializable 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
case object T_SEMI extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
case object T_LPAREN extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
case object T_RPAREN extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
case class T_ID(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
case class T_OP(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
case class T_NUM(n: Int) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
case class T_KWD(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
case class T_STR(s: String) extends Token
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def deserialise[T](fname: String) : T = {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  val in = new ObjectInputStream(new FileInputStream(fname))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  val data = in.readObject.asInstanceOf[T]
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  in.close
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  data
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
def main(args: Array[String]) = {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  println("TEST\n" ++ deserialise[List[Token]]("/tmp/nflx").mkString)  
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
}