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 { TokenType::Number(_) | &TokenType::String(_) => Ok(ExprNode::Litteral {
value: self.advance().clone(), value: self.advance().clone(),
}), }),
TokenType::Identifier(_) => Ok(ExprNode::Variable(VariableExpr { TokenType::Identifier(_) => Ok(ExprNode::Variable(self.make_var_expr())),
token: self.advance().clone(), TokenType::This => Ok(ExprNode::This(self.make_var_expr())),
id: self.make_id(),
})),
_ => self.error("expected expression"), _ => self.error("expected expression"),
} }
} }
@ -766,6 +764,14 @@ impl Parser {
* HELPER METHODS * * 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 /// 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. /// pointer is moved and a clone of the token is returned.
fn expect(&mut self, accepts: &[TokenType]) -> Option<Token> { fn expect(&mut self, accepts: &[TokenType]) -> Option<Token> {