Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def stmt: Parser[Any] = assignstmt|blockstmt|ifstmt|"break" ~ ";"|"continue" ~ ";"|whilestmt|repeatstmt|forstmt|invokestmt| "return" ~ exp
- def assignstmt: Parser[Any] = ((ident ~ "[" ~ exp ~ "]")| ident ~ rep("." ~ ident))|ident ~ ":=" ~ exp ~ ";"
- def blockstmt: Parser[Any] = "{" ~ rep(constdecl|vardecl)~ rep(stmt) ~ "}"
- def ifstmt: Parser[Any] = "if" ~ exp ~ "then" ~ stmt ~ opt("else" ~ stmt)
- def whilestmt: Parser[Any] = "while" ~ exp ~ "do" ~ stmt
- def repeatstmt: Parser[Any] = "repeat" ~ rep1(stmt) ~ "until" ~ exp ~ ";"
- def forstmt: Parser[Any] = "for" ~ ident ~ ":=" ~ exp ~ ("to"|"downto") ~ exp ~ "do" ~ stmt
- def invokestmt: Parser[Any] = ident ~ "." ~ ident ~ "(" ~ explist ~ ")" ~ ";"
- // declare
- def classdecl: Parser[Any] = "class" ~ ident ~ opt("extends" ~ ident) ~ "{" ~ rep(constdecl|abstractmethoddecl|vardecl|method) ~ "}"
- def vardecl: Parser[Any] = rep1sep(ident, ",") ~ ":" ~ (arraytypes|types) ~ ";"
- def constdecl: Parser[Any] = "final" ~ arraytypes|types ~ ident ~ "=" ~ exp ~ ";"
- def abstractmethoddecl : Parser[Any] = "abstract" ~ returntype ~ ident ~ "(" ~ parameterlist ~ ")" ~ ";"
- def method:Parser[Any] = methoddecl|constructordecl
- def methoddecl : Parser[Any] = returntype ~ ident ~ "(" ~ parameterlist ~ ")" ~ "{" ~ rep(constdecl| vardecl) ~ rep(stmt) ~ "}"
- def constructordecl : Parser[Any] = ident ~ "(" ~ parameterlist ~ ")" ~ "{" ~ rep(constdecl|vardecl)~ rep(stmt) ~ "}"
- //exp
- def exp : Parser[Any] = term ~ opt("<"|">"|"<="|">=" ~ term)
- def term : Parser[Any] = term1 ~ opt("=="|"<>" ~ term1)
- def term1: Parser[Any] = term2 ~ rep("&&"|"||" ~ term2)
- def term2: Parser[Any] = term3 ~ rep("+"|"-" ~ term3)
- def term3: Parser[Any] = term4 ~ rep(("*"|"/"|"\\"|"%") ~ term4)
- def term4: Parser[Any] = term5 ~ rep("^" ~ term5)
- def term5: Parser[Any] = opt("!") ~ term6
- def term6: Parser[Any] = opt("-"|"+") ~ term7
- def term7: Parser[Any] = term8 ~ opt("[" ~ exp ~ "]")
- def term8: Parser[Any] = term9 ~ rep("." ~ ident ~ opt("(" ~ explist ~ ")"))
- def term9: Parser[Any] = ("new" ~ ident ~ "(" ~ explist ~ ")")|term10
- def term10: Parser[Any] = ident| lit| "(" ~ exp ~ ")" | "self"
- // list
- def explist : Parser[Any] = repsep(exp, ",")
- def identifierlist : Parser[Any] = rep1sep(ident,",")
- def parameter : Parser[Any] = ident ~ ":" ~ arraytypes|types
- def parameterlist : Parser[Any] = repsep( parameter, ";")
- // type
- def classtype: Parser[Any] = ident
- def primitivetype: Parser[Any] = "integer"|"float"|"bool"|"string"
- def types: Parser[Any] = classtype|primitivetype
- def arraytypes: Parser[Any] = types ~ "[" ~ lit ~ "]"
- def returntype: Parser[Any] = arraytypes|types|"void"
- // literal
- def lit : Parser[Any] = stringlit|boollit|floatlit|intlit
- def stringlit: Parser[Any] = elem("string", _.isInstanceOf[lexical.StringLit2])
- def boollit: Parser[Any] = elem("boolean", _.isInstanceOf[lexical.BooleanLit])
- def floatlit: Parser[Any] = elem("real", _.isInstanceOf[lexical.FloatLit])
- def intlit: Parser[Any] = elem("integer", _.isInstanceOf[lexical.IntLit])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement