Advertisement
tudzic

Untitled

Oct 29th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.60 KB | None | 0 0
  1.  def vardecl: Parser[List[Decl]] = (identifiers <~ ":") ~ types <~ ";" ^^
  2.     {
  3.       case il ~ typ => il.map((x) => VarDecl(x, typ))
  4.     }
  5.  
  6.   def constdecl: Parser[List[Decl]] = ("final" ~> types) ~ (ident <~ "=") ~ (literal <~ ";") ^^
  7.     {
  8.       case typ ~ id ~ value => List(ConstDecl(Id(id), typ, value))
  9.     }
  10.   def abstractmethoddecl: Parser[List[Decl]] = ("abstract" ~> returnType) ~ ident ~ ("(" ~> paramslist <~ ")" ~ ";") ^^
  11.     {
  12.       case a ~ b ~ c => List(MethodAbstractDecl(a, Id(b), c))
  13.     }
  14.   def classdecl: Parser[ClassDecl] = "class" ~> ident ~ opt("extends" ~> ident) ~ ("{" ~> rep(listAttribute) <~ "}") ^^
  15.     {
  16.       case id ~ Some(sup) ~ d => d match {
  17.         case Nil => ClassDecl(Id(id), Id(sup), List[ClassDecl]())
  18.         case head :: tail => ClassDecl(Id(id), Id(sup), tail.foldRight(head)((b, x) => b ::: x))
  19.       }
  20.       case id ~ None ~ d => d match {
  21.         case Nil => ClassDecl(Id(id), null, List[ClassDecl]())
  22.         case head :: tail => ClassDecl(Id(id), null, tail.foldRight(head)((b, x) => b ::: x))
  23.       }
  24.     }
  25.   def listAttribute:Parser[List[Decl]] = constdecl | vardecl | abstractmethoddecl | method
  26.   def method: Parser[List[Decl]] = methoddecl | constructordecl
  27.   def methoddecl: Parser[List[Decl]] = (returnType) ~ ident ~ ("(" ~> paramslist <~ ")") ~ blockstmt ^^
  28.     {
  29.       case typ ~ id ~ pramlist ~ stm => List(MethodImpDecl(typ, Id(id), pramlist, stm))
  30.     }
  31.   def constructordecl: Parser[List[Decl]] = ident ~ ("(" ~> paramslist <~ ")") ~ blockstmt ^^
  32.     {
  33.       case id ~ pramlist ~ stm => List(MethodImpDecl(null, Id(id), pramlist, stm))
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement