From 7e7c8ffc37dc3c0c8cb25e389d653c7503faef93 Mon Sep 17 00:00:00 2001
From: Emmanuel Benoit <tseeker@nocternity.net>
Date: Mon, 9 Jan 2023 07:56:58 +0100
Subject: [PATCH] Parser - Handle the "this" keyword

---
 src/parser.rs | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/parser.rs b/src/parser.rs
index 0845efe..923bb78 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -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> {