feat: Add error handling and tests for unknown keywords#16
Merged
Conversation
Owner
|
@ArjunDixit1 I will look into it today |
Contributor
Author
|
@Pallavrai Okay Thanks |
Contributor
Author
|
@Pallavrai Did u check? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
### 📝 Problem
The DogLang interpreter did not properly handle unknown keywords or typos (e.g., wagdog instead of wagtail). This would cause the parser to either crash or misinterpret the code, making it difficult for users to find simple mistakes.
### 💡 Solution
This PR implements robust syntax error handling to catch these typos during the parsing stage. If the interpreter encounters a word that is not a valid keyword in a context where one is expected, it will now halt immediately and provide a clear, user-friendly error message that includes the line number.
This successfully meets all acceptance criteria:
Invalid keywords are detected during parsing.
A descriptive error message is shown: Syntax Error: Unknown keyword 'XYZ' at line N.
A new unit test has been added to verify this functionality.
### 🛠️ Technical Implementation
Custom Exceptions: Created a new DogLangSyntaxError in doglang/error.py for more specific error handling.
Line Number Tracking: The Tokenizer was enhanced to track and store the line number for every token it produces.
Parser Logic: The SyntaxAnalyser now uses a lookahead (peek()) to differentiate between a valid variable assignment (e.g., myvar = 5) and a standalone identifier, which is now correctly treated as an unknown keyword.
Unit Test: A new test file, tests/test_syntax_analyser.py, was added with a test case that confirms the parser fails correctly and with the exact error message when given a keyword typo.
Closes #6