Parser - Handle the "this" keyword

This commit is contained in:
Emmanuel BENOîT 2023-01-09 07:56:58 +01:00
parent c9f74cdc55
commit 7e7c8ffc37

View file

@ -725,10 +725,8 @@ impl Parser {
TokenType::Number(_) | &TokenType::String(_) => Ok(ExprNode::Litteral {
value: self.advance().clone(),
}),
TokenType::Identifier(_) => Ok(ExprNode::Variable(VariableExpr {
token: self.advance().clone(),
id: self.make_id(),
})),
TokenType::Identifier(_) => Ok(ExprNode::Variable(self.make_var_expr())),
TokenType::This => Ok(ExprNode::This(self.make_var_expr())),
_ => self.error("expected expression"),
}
}
@ -766,6 +764,14 @@ impl Parser {
* HELPER METHODS *
* -------------- */
/// Generate a variable reference record based on the current token.
fn make_var_expr(&mut self) -> VariableExpr {
VariableExpr {
token: self.advance().clone(),
id: self.make_id(),
}
}
/// Expect a token of some types. If a matching token is found, the read
/// pointer is moved and a clone of the token is returned.
fn expect(&mut self, accepts: &[TokenType]) -> Option<Token> {