Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanner refactor #8

Merged
merged 7 commits into from
Jan 18, 2025
Merged

Scanner refactor #8

merged 7 commits into from
Jan 18, 2025

Conversation

UnstoppableMango
Copy link
Contributor

Follow the pos, tok, lit := scanner.Scan() pattern from go/scanner
Pos should refer to the token's starting position

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.
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.
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).
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).
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.
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.
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
Copy link

codecov bot commented Jan 18, 2025

Codecov Report

Attention: Patch coverage is 96.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 95.72%. Comparing base (bda6241) to head (3665f69).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
scanner.go 95.12% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main       #8      +/-   ##
==========================================
- Coverage   95.76%   95.72%   -0.05%     
==========================================
  Files           6        7       +1     
  Lines         378      374       -4     
==========================================
- Hits          362      358       -4     
  Misses         12       12              
  Partials        4        4              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@UnstoppableMango UnstoppableMango merged commit b378b8d into main Jan 18, 2025
4 checks passed
@UnstoppableMango UnstoppableMango deleted the scan branch January 18, 2025 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant