progs/fun_parser.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 04 Jul 2020 16:58:12 +0100
changeset 732 c7bdd7eac4cb
parent 699 7dac350b20a1
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     1
// A parser for the Fun language
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     2
//================================
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     3
//
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     4
// call with 
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     5
//
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     6
//     scala fun_parser.scala fact.tks
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     7
//
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     8
//     scala fun_parser.scala defs.tks
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     9
//
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    10
// this will generate a .prs file that can be deserialised back
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    11
// into a list of declarations
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
object Fun_Parser {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
import scala.language.implicitConversions    
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
import scala.language.reflectiveCalls
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    17
import scala.util._ 
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
import java.io._
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
abstract class Token extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case object T_SEMI extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case object T_COMMA extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case object T_LPAREN extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case object T_RPAREN extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case class T_ID(s: String) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case class T_OP(s: String) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case class T_NUM(n: Int) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
case class T_KWD(s: String) extends Token
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
// Parser combinators
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
//    type parameter I needs to be of Seq-type
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
//
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
abstract class Parser[I, T](implicit ev: I => Seq[_]) {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  def parse(ts: I): Set[(T, I)]
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    37
  def parse_single(ts: I) : T = 
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    38
    parse(ts).partition(_._2.isEmpty) match {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    39
      case (good, _) if !good.isEmpty => good.head._1
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    40
      case (_, err) => { 
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    41
	println (s"Parse Error\n${err.minBy(_._2.length)}") ; sys.exit(-1) }
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 645
diff changeset
    42
    }
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// convenience for writing grammar rules
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
case class ~[+A, +B](_1: A, _2: B)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
class SeqParser[I, T, S](p: => Parser[I, T], 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
                         q: => Parser[I, S])(implicit ev: I => Seq[_]) extends Parser[I, ~[T, S]] {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  def parse(sb: I) = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    for ((head1, tail1) <- p.parse(sb); 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
class AltParser[I, T](p: => Parser[I, T], 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
                      q: => Parser[I, T])(implicit ev: I => Seq[_]) extends Parser[I, T] {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
class FunParser[I, T, S](p: => Parser[I, T], 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
                         f: T => S)(implicit ev: I => Seq[_]) extends Parser[I, S] {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  def parse(sb: I) = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
// convenient combinators
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
def ListParser[I, T, S](p: => Parser[I, T], 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
                        q: => Parser[I, S])(implicit ev: I => Seq[_]): Parser[I, List[T]] = {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  (p ~ q ~ ListParser(p, q)) ==> { case x ~ _ ~ z => x :: z : List[T] } ||
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  (p ==> ((s) => List(s)))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
case class TokParser(tok: Token) extends Parser[List[Token], Token] {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  def parse(ts: List[Token]) = ts match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
    case t::ts if (t == tok) => Set((t, ts)) 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
    case _ => Set ()
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
implicit def token2tparser(t: Token) = TokParser(t)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
implicit def TokOps(t: Token) = new {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  def || (q : => Parser[List[Token], Token]) = new AltParser[List[Token], Token](t, q)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  def ==>[S] (f: => Token => S) = new FunParser[List[Token], Token, S](t, f)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  def ~[S](q : => Parser[List[Token], S]) = new SeqParser[List[Token], Token, S](t, q)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
case object NumParser extends Parser[List[Token], Int] {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  def parse(ts: List[Token]) = ts match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
    case T_NUM(n)::ts => Set((n, ts)) 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
    case _ => Set ()
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
case object IdParser extends Parser[List[Token], String] {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
  def parse(ts: List[Token]) = ts match {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
    case T_ID(s)::ts => Set((s, ts)) 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
    case _ => Set ()
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
  }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
// Abstract syntax trees for the Fun language
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
abstract class Exp extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
abstract class BExp extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
abstract class Decl extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
case class Def(name: String, args: List[String], body: Exp) extends Decl
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
case class Main(e: Exp) extends Decl
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
case class Call(name: String, args: List[Exp]) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
case class Write(e: Exp) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
case class Var(s: String) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
case class Num(i: Int) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
case class Sequence(e1: Exp, e2: Exp) extends Exp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
// Grammar Rules for the Fun language
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
// arithmetic expressions
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
lazy val Exp: Parser[List[Token], Exp] = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Exp ~ T_KWD("else") ~ Exp) ==>
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
    { case _ ~ x ~ _ ~ y ~ _ ~ z => If(x, y, z): Exp } ||
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  (M ~ T_SEMI ~ Exp) ==> { case x ~ _ ~ y => Sequence(x, y): Exp } || M
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
lazy val M: Parser[List[Token], Exp] =
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  (T_KWD("write") ~ L) ==> { case _ ~ y => Write(y): Exp } || L
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
lazy val L: Parser[List[Token], Exp] = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  (T ~ T_OP("+") ~ Exp) ==> { case x ~ _ ~ z => Aop("+", x, z): Exp } ||
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
  (T ~ T_OP("-") ~ Exp) ==> { case x ~ _ ~ z => Aop("-", x, z): Exp } || T  
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
lazy val T: Parser[List[Token], Exp] = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  (F ~ T_OP("*") ~ T) ==> { case x ~ _ ~ z => Aop("*", x, z): Exp } || 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  (F ~ T_OP("/") ~ T) ==> { case x ~ _ ~ z => Aop("/", x, z): Exp } || 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
  (F ~ T_OP("%") ~ T) ==> { case x ~ _ ~ z => Aop("%", x, z): Exp } || F
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
lazy val F: Parser[List[Token], Exp] = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
  (IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN) ==> 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
    { case x ~ _ ~ z ~ _ => Call(x, z): Exp } ||
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
  (T_LPAREN ~ Exp ~ T_RPAREN) ==> { case _ ~ y ~ _ => y: Exp } || 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
  IdParser ==> { case x => Var(x): Exp } || 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
  NumParser ==> { case x => Num(x): Exp }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
// boolean expressions
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
lazy val BExp: Parser[List[Token], BExp] = 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
  (Exp ~ T_OP("==") ~ Exp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
  (Exp ~ T_OP("!=") ~ Exp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
699
7dac350b20a1 updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   156
  (Exp ~ T_OP("<") ~ Exp)  ==> { case x ~ _ ~ z => Bop("<",  x, z): BExp } || 
7dac350b20a1 updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   157
  (Exp ~ T_OP(">") ~ Exp)  ==> { case x ~ _ ~ z => Bop("<",  z, x): BExp } || 
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
  (Exp ~ T_OP("<=") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", x, z): BExp } || 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
  (Exp ~ T_OP("=>") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", z, x): BExp }  
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
lazy val Defn: Parser[List[Token], Decl] =
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
   (T_KWD("def") ~ IdParser ~ T_LPAREN ~ ListParser(IdParser, T_COMMA) ~ T_RPAREN ~ T_OP("=") ~ Exp) ==>
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
     { case _ ~ y ~ _ ~ w ~ _ ~ _ ~ r => Def(y, w, r): Decl }
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
lazy val Prog: Parser[List[Token], List[Decl]] =
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  (Defn ~ T_SEMI ~ Prog) ==> { case x ~ _ ~ z => x :: z : List[Decl] } ||
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
  (Exp ==> ((s) => List(Main(s)) : List[Decl]))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
// Reading tokens and Writing parse trees
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
def serialise[T](fname: String, data: T) = {
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   174
  import scala.util.Using
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   175
  Using(new ObjectOutputStream(new FileOutputStream(fname))) {
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   176
    out => out.writeObject(data)
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   177
  }
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   180
def deserialise[T](fname: String) : Try[T] = {
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   181
  import scala.util.Using
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   182
  Using(new ObjectInputStream(new FileInputStream(fname))) {
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   183
    in => in.readObject.asInstanceOf[T]
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   184
  }
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   188
def main(args: Array[String]) : Unit= {
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
  val fname = args(0)
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
  val pname = fname.stripSuffix(".tks") ++ ".prs"
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   191
  val tks = deserialise[List[Token]](fname).getOrElse(Nil)
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
  serialise(pname, Prog.parse_single(tks))
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
  // testing whether read-back is working
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   195
  //val ptree = deserialise[List[Decl]](pname).get
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
  //println(s"Reading back from ${pname}:\n${ptree.mkString("\n")}")  
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   199
}