Advertisement
tudzic

Untitled

Oct 12th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.09 KB | None | 0 0
  1. def stmt: Parser[Any] = assignstmt|blockstmt|ifstmt|"break" ~ ";"|"continue" ~ ";"|whilestmt|repeatstmt|forstmt|invokestmt| "return" ~ exp
  2.             def assignstmt: Parser[Any] = ((ident ~ "[" ~ exp ~ "]")| ident ~ rep("." ~ ident))|ident ~ ":=" ~ exp ~ ";"
  3.             def blockstmt: Parser[Any] = "{" ~ rep(constdecl|vardecl)~ rep(stmt) ~ "}"
  4.             def ifstmt: Parser[Any] = "if" ~ exp ~ "then" ~ stmt ~ opt("else" ~ stmt)
  5.             def whilestmt: Parser[Any] = "while" ~ exp ~ "do" ~ stmt
  6.             def repeatstmt: Parser[Any] = "repeat" ~ rep1(stmt) ~ "until" ~ exp  ~ ";"
  7.             def forstmt: Parser[Any] = "for" ~ ident ~ ":=" ~ exp ~ ("to"|"downto") ~ exp ~ "do" ~ stmt
  8.             def invokestmt: Parser[Any] = ident ~ "." ~ ident ~ "(" ~ explist ~ ")" ~ ";"
  9.             // declare
  10.             def classdecl: Parser[Any] = "class" ~ ident ~ opt("extends" ~ ident) ~ "{" ~ rep(constdecl|abstractmethoddecl|vardecl|method) ~ "}"
  11.             def vardecl: Parser[Any] = rep1sep(ident, ",") ~ ":" ~ (arraytypes|types) ~ ";"
  12.             def constdecl: Parser[Any] = "final" ~ arraytypes|types ~ ident ~ "=" ~ exp ~ ";"
  13.             def abstractmethoddecl : Parser[Any] = "abstract" ~ returntype ~ ident ~ "(" ~ parameterlist ~ ")" ~ ";"
  14.             def method:Parser[Any] = methoddecl|constructordecl
  15.             def methoddecl : Parser[Any] = returntype ~ ident ~ "(" ~ parameterlist ~ ")" ~ "{" ~ rep(constdecl| vardecl) ~ rep(stmt) ~ "}"
  16.             def constructordecl : Parser[Any] = ident ~ "(" ~ parameterlist ~ ")" ~ "{"  ~ rep(constdecl|vardecl)~ rep(stmt) ~ "}"
  17.  
  18.             //exp
  19.             def exp : Parser[Any] = term ~ opt("<"|">"|"<="|">=" ~ term)
  20.             def term : Parser[Any] = term1 ~ opt("=="|"<>" ~ term1)
  21.             def term1: Parser[Any] = term2 ~ rep("&&"|"||" ~ term2)
  22.             def term2: Parser[Any] = term3 ~ rep("+"|"-" ~ term3)
  23.             def term3: Parser[Any] = term4 ~ rep(("*"|"/"|"\\"|"%") ~ term4)
  24.             def term4: Parser[Any] = term5 ~ rep("^" ~ term5)
  25.             def term5: Parser[Any] = opt("!") ~ term6
  26.             def term6: Parser[Any] = opt("-"|"+") ~ term7
  27.             def term7: Parser[Any] = term8 ~ opt("[" ~ exp ~ "]")
  28.             def term8: Parser[Any] = term9 ~ rep("." ~ ident ~ opt("(" ~ explist ~ ")"))
  29.             def term9: Parser[Any] = ("new" ~ ident ~ "(" ~ explist ~ ")")|term10
  30.             def term10: Parser[Any] = ident| lit| "(" ~ exp ~ ")" | "self"
  31.             // list
  32.             def explist : Parser[Any] = repsep(exp, ",")
  33.             def identifierlist : Parser[Any] = rep1sep(ident,",")
  34.             def parameter : Parser[Any] = ident ~ ":" ~ arraytypes|types
  35.             def parameterlist : Parser[Any] = repsep( parameter, ";")
  36.  
  37.             // type
  38.             def classtype: Parser[Any] = ident
  39.             def primitivetype: Parser[Any] = "integer"|"float"|"bool"|"string"
  40.             def types: Parser[Any] = classtype|primitivetype
  41.             def arraytypes: Parser[Any] = types ~ "[" ~ lit ~ "]"
  42.             def returntype: Parser[Any] = arraytypes|types|"void"
  43.             // literal
  44.             def lit : Parser[Any] = stringlit|boollit|floatlit|intlit
  45.             def stringlit: Parser[Any] = elem("string", _.isInstanceOf[lexical.StringLit2])
  46.             def boollit: Parser[Any] = elem("boolean", _.isInstanceOf[lexical.BooleanLit])  
  47.             def floatlit: Parser[Any] = elem("real", _.isInstanceOf[lexical.FloatLit])
  48.             def intlit: Parser[Any] = elem("integer", _.isInstanceOf[lexical.IntLit])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement