patx/relay-lang

Fix else token parsing in if blocks

Commit b0d32bc · Harrison Erd · 2026-02-10T16:40:03-05:00

Changeset
b0d32bc07787d1da1842b35ab581e849b857dd9d
Parents
3116853200ad4eaff78d256a7db6d41e82b2c508

View source at this commit

Comments

No comments yet.

Log in to comment

Diff

diff --git a/src/main.rs b/src/main.rs
index 40c47b4..be696d4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -451,10 +451,9 @@ impl<'a> Lexer<'a> {
                     if is_ident_start(c) {
                         let id = self.read_ident();
                         let k = match id.as_str() {
-                            "fn" | "if" | "for" | "while" | "return" | "try" | "except"
-                            | "import" | "True" | "False" | "None" | "str" | "int" | "float" => {
-                                Tok::Keyword(id)
-                            }
+                            "fn" | "if" | "else" | "for" | "while" | "return" | "try"
+                            | "except" | "import" | "True" | "False" | "None" | "str" | "int"
+                            | "float" => Tok::Keyword(id),
                             _ => Tok::Ident(id),
                         };
                         out.push(self.wrap(k));
@@ -804,7 +803,7 @@ impl Parser {
         let then_block = self.parse_block()?;
         let mut else_block = Vec::new();
         self.skip_newlines();
-        if self.peek_ident_string().as_deref() == Some("else") {
+        if self.peek_kw("else") || self.peek_ident_string().as_deref() == Some("else") {
             self.bump();
             self.expect_newline()?;
             else_block = self.parse_block()?;