Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor parser and scanner for cleaner code Significant changes include: - Simplified the NewParser function in parser.go by removing unnecessary steps and adding a next() call. - Refactored the Scan function in scanner.go to return position, token, and literal directly instead of setting them as properties on the Scanner struct. - Updated tests in scanner_test.go to reflect these changes. * Enhanced error handling in parser and scanner The parser's error matching has been improved to check for substrings, providing more flexibility. The scanner now advances to the next token after identifying an identifier, ensuring proper sequence progression. Adjustments were also made to the scanner tests: literal comparison no longer trims spaces from input, and position calculation now accounts for an extra character. Lastly, a change was made in how scanning errors are handled during testing. * Added PositionFor function and corresponding tests A new function, PositionFor, has been added to the token package. This function returns the Position value for a given file position. If the provided position is out of bounds, it's adjusted to match File.Offset behavior. Alongside this addition, a test suite has also been created to ensure that calling PositionFor yields equivalent results as calling file.PositionFor(p, false). * Refactor scanner initialization and add position method The scanner's initialization has been refactored for better readability. The creation of the bufio.Scanner and setting its split function is now done before creating the Scanner struct. A new method, Position, has been added to the Scanner which returns a token's position in a file. In addition, changes have been made to the scanner tests. The test case for scanning an identifier followed by a token has been replaced with one for scanning an identifier followed by whitespace. Additional assertions have also been added to ensure that after scanning an identifier or special character, the next scan returns EOF (end of file). * Enhanced scanner test coverage Added new tests to the scanner_test.go file. These include a test for the Position function, ensuring it's equivalent to calling file.PositionFor(p, false). Also added checks for token position and newline tokens in existing tests. Corrected an issue with scanning newline followed by token. * Refactored scanner logic and expanded tests The scanning logic has been refactored for better readability and efficiency. The 'done' flag check is now performed after the offset update, ensuring accurate line addition in case of newline characters. In addition, the test coverage for the scanner functionality has been significantly expanded. New test cases have been added to verify correct token scanning following a newline character, with various input scenarios considered. This ensures robustness of the scanner across different use-cases. * Refactor scanner tests for readability Significant changes include: - Removed redundant test cases in the scanner_test.go file - Simplified token scanning tests by focusing on space-separated tokens only - This results in a cleaner, more maintainable test suite
- Loading branch information