1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
use asciifile::Spanned; use diagnostics::MessageLevel; use lexer::IntLit; use strtab::Symbol; use strum_macros::EnumDiscriminants; #[strum_discriminants(derive(Display))] #[derive(EnumDiscriminants, Debug, PartialEq, Eq)] pub enum AST<'t> { Empty, Program(Spanned<'t, Program<'t>>), } /// This is the top-level AST node. It stores all class declarations of the /// `MiniJava` program. #[derive(Debug, PartialEq, Eq, Clone)] pub struct Program<'t> { pub classes: Vec<Spanned<'t, ClassDeclaration<'t>>>, pub attrs: Vec<Spanned<'t, Attribute<'t>>>, } /// This AST node stores the Class declaration, which consists of a name and /// the members of the class. #[derive(Debug, PartialEq, Eq, Clone)] pub struct ClassDeclaration<'t> { pub name: Spanned<'t, Symbol<'t>>, pub members: Vec<Spanned<'t, ClassMember<'t>>>, pub attrs: Vec<Spanned<'t, Attribute<'t>>>, } /// This AST node describes a class member. Variants of class members are /// defined in `ClassMemberKind`. Every class member has a name. #[derive(Debug, PartialEq, Eq, Clone)] pub struct ClassMember<'t> { pub kind: ClassMemberKind<'t>, pub name: Symbol<'t>, pub attrs: Vec<Spanned<'t, Attribute<'t>>>, } pub type ParameterList<'t> = Vec<Spanned<'t, Parameter<'t>>>; /// A class member is either one of /// * `Field(type)`: a declaration of a field of a class /// * `Method(type, params, body)`: a method of a class /// * `MainMethod(params, body)`: a main method, which is a special method that /// is only allowed once in a `MiniJava` Program. `params` is guaranteed to /// only contain the `String[] IDENT` parameter. #[strum_discriminants(derive(Display, Hash, PartialOrd, Ord))] #[derive(EnumDiscriminants, Debug, PartialEq, Eq, Clone)] pub enum ClassMemberKind<'t> { Field(Spanned<'t, Type<'t>>), Method( Spanned<'t, Type<'t>>, Spanned<'t, ParameterList<'t>>, Spanned<'t, Block<'t>>, ), MainMethod(Spanned<'t, ParameterList<'t>>, Spanned<'t, Block<'t>>), } /// This AST node represents a method parameter. A parameter consists of a /// `Type<'t>` and a name. #[derive(Debug, PartialEq, Eq, Clone)] pub struct Parameter<'t> { pub ty: Spanned<'t, Type<'t>>, pub name: Symbol<'t>, } /// A `Type<'t>` is basically a `BasicType<'t>`. Optional it can be an /// (n-dimensional) array type. #[derive(Debug, PartialEq, Eq, Clone)] pub struct Type<'t> { pub basic: Spanned<'t, BasicType<'t>>, /// Depth of the array type (number of `[]`) i.e. this means means `self.ty /// []^(self.array)` pub array_depth: u64, } /// A `BasicType<'t>` is either one of /// * `Int`: a 32-bit integer /// * `Boolean`: a boolean /// * `Void`: a void type /// * `Custom`: a custom defined type #[strum_discriminants(derive(Display))] #[derive(EnumDiscriminants, Debug, PartialEq, Eq, Clone)] pub enum BasicType<'t> { Int, Boolean, Void, Custom(Symbol<'t>), MainParam, } /// A `Block` in the AST is basically just a vector of statements. #[derive(Debug, PartialEq, Eq, Clone)] pub struct Block<'t> { pub statements: Vec<Spanned<'t, Stmt<'t>>>, } /// A statement can have one of the kinds: /// * `Block`: A block defined in `Block` /// * `Empty`: An empty statement: `;` /// * `If`: a if expression consisting of the condition, its body and /// optionally an else statement /// * `Expression`: an expression defined in `Expr` /// * `While`: a while loop consisting of the condition and its body /// * `Return`: a return which can optionally return an expression /// * `LocalVariableDeclaration`: a declaration and optional initialization of /// a local variable #[strum_discriminants(derive(Display))] #[derive(EnumDiscriminants, Debug, PartialEq, Eq, Clone)] // TODO: should all be named, especially variants with 3 arguments. // `LocalVariableDeclaration` pub enum Stmt<'t> { Block(Spanned<'t, Block<'t>>), Empty, If( Box<Spanned<'t, Expr<'t>>>, Box<Spanned<'t, Stmt<'t>>>, Option<Box<Spanned<'t, Stmt<'t>>>>, ), While(Box<Spanned<'t, Expr<'t>>>, Box<Spanned<'t, Stmt<'t>>>), Expression(Box<Spanned<'t, Expr<'t>>>), Return(Option<Box<Spanned<'t, Expr<'t>>>>), LocalVariableDeclaration( Spanned<'t, Type<'t>>, Spanned<'t, Symbol<'t>>, Option<Box<Spanned<'t, Expr<'t>>>>, ), } /// An expression is either one of /// * `Assignment`: an assignment expression /// * `Binary`: one of the binary operations defined in `BinaryOp` /// * `Unary`: one of the unary operations defined in `UnaryOp` /// * `MethodInvocation`: a method invocation on a primary expression: /// `foo.method()` /// * `FieldAccess`: a field access on a primary expression: /// `foo.bar` /// * `ArrayAccess`: an array access on a primary expression: /// `foo[42]` /// The primary expression from the original grammar are also part of this, /// since the distinction is only required for correct postfix-op parsing. These /// are: /// * `Null`: the `null` keyword /// * `Boolean`: a boolean literal /// * `Int`: an integer literal /// * `Var`: use of a variable /// * `MethodInvocation`: a method invocation /// * `This`: the `this` keyword /// * `NewObject`: generating a new object, e.g. `new Foo()` /// * `NewArray`: generating a new array, e.g. `new int[]` #[strum_discriminants(derive(Display))] #[derive(EnumDiscriminants, Debug, PartialEq, Eq, Clone)] pub enum Expr<'t> { Binary( BinaryOp, Box<Spanned<'t, Expr<'t>>>, Box<Spanned<'t, Expr<'t>>>, ), Unary(UnaryOp, Box<Spanned<'t, Expr<'t>>>), // Postfix ops MethodInvocation( Box<Spanned<'t, Expr<'t>>>, Spanned<'t, Symbol<'t>>, Spanned<'t, ArgumentList<'t>>, ), FieldAccess(Box<Spanned<'t, Expr<'t>>>, Spanned<'t, Symbol<'t>>), ArrayAccess(Box<Spanned<'t, Expr<'t>>>, Box<Spanned<'t, Expr<'t>>>), // The old primary expressions Null, Boolean(bool), Int(Spanned<'t, IntLit<'t>>), NegInt(Spanned<'t, IntLit<'t>>), Var(Spanned<'t, Symbol<'t>>), ThisMethodInvocation(Spanned<'t, Symbol<'t>>, Spanned<'t, ArgumentList<'t>>), This, NewObject(Spanned<'t, Symbol<'t>>), NewArray(Spanned<'t, BasicType<'t>>, Box<Spanned<'t, Expr<'t>>>, u64), } /// Binary operations like comparisons (`==`, `!=`, `<=`, ...), logical /// operations (`||`, `&&`) or algebraic operation (`+`, `-`, `*`, `/`, `%`). #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum BinaryOp { Assign, Equals, NotEquals, LessThan, GreaterThan, LessEquals, GreaterEquals, LogicalOr, LogicalAnd, Add, Sub, Mul, Div, Mod, } /// One of the unary operations `!` and `-` #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum UnaryOp { Not, Neg, } pub type ArgumentList<'t> = Vec<Spanned<'t, Expr<'t>>>; impl<'f> ClassMemberKind<'f> { pub fn is_method(&self) -> bool { use self::ClassMemberKind::*; match self { Method(_, _, _) | MainMethod(_, _) => true, Field(_) => false, } } pub fn method_params(&self) -> Option<&Spanned<'_, ParameterList<'_>>> { use self::ClassMemberKind::*; match &self { Method(_t, pl, _block) => Some(pl), MainMethod(pl, _block) => Some(pl), Field(_) => None, } } pub fn method_body(&self) -> Option<&Spanned<'_, Block<'_>>> { use self::ClassMemberKind::*; match &self { Method(_t, _pl, block) => Some(block), MainMethod(_pl, block) => Some(block), Field(_) => None, } } } /// Attributes can be given to a program with /// ```ignore /// #!attribute_name /// ``` /// at the start of the file. /// /// For classes, methods and fields use the syntax /// ```ignore /// #attribute_name /// public void f() {} /// ``` /// /// Available attributes are: /// - Lint level: `?!level:lint_name` level can be one of `allow`, `info`, /// `warning` or `error` e.g `?allow:unused_argument` /// - Inline: `?!inline`, `?!noinline` #[derive(Debug, PartialEq, Eq, Clone)] pub enum Attribute<'t> { LintLevel(MessageLevel, Spanned<'t, Symbol<'t>>), Inline(bool), }