fix(parser): Error::expected for const init, clearer for-of error, op…#4973
Conversation
…erator unimplemented→syntax Made-with: Cursor
|
Hi @jedel1043 , I came across a few interesting cases while compiling. I think improving the error handling in this part could make the user experience better. Thanks! |
Test262 conformance changes
Tested main commit: |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4973 +/- ##
===========================================
+ Coverage 47.24% 58.42% +11.18%
===========================================
Files 476 556 +80
Lines 46892 61165 +14273
===========================================
+ Hits 22154 35737 +13583
- Misses 24738 25428 +690 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
core/parser/src/lexer/operator.rs
Outdated
| return Err(Error::syntax( | ||
| format!("unexpected operator '{}'", char::from(op)), | ||
| start_pos.position(), | ||
| )); |
There was a problem hiding this comment.
We have an Error::unexpected variant for cases like this.
There was a problem hiding this comment.
@jedel1043 Switched to Error::unexpected per your suggestion.
…iew) Made-with: Cursor
Made-with: Cursor
…:unexpected - Replace expect_expression() with try_into_expression()? (3 call sites) - Remove panicking expect_expression() method from FormalParameterListOrExpression - class_decl: use Error::unexpected instead of vague 'unexpected token' Made-with: Cursor
|
@jedel1043 i hope you can review my PR |
|
hey @jedel1043 if there are any fixes i need to do lemme know |
jedel1043
left a comment
There was a problem hiding this comment.
Thanks! This should also give much better error messages so that's good
Summary
Three parser improvements aligning with maintainer feedback: (1) Use
Error::expectedinstead ofError::generalfor const declaration missing initializer. (2) Replace vague "unexpected token" with a descriptive message for invalidletin for-of. (3) Replace operator lexerunimplemented!with proper syntax error.Motivation
Error::expected for const — "Expected initializer for const declaration" is an "expected X, got Y" pattern. Using
Error::expected(["="], found, span, context)matches the rest of the parser and gives a clearer message.For-of
letmessage — The previous "unexpected token" was vague. Replacing with "'let' cannot be used as the iterable in a for-of loop" explains the issue (mirrors the nearbyasyncerror).Operator lexer —
unimplemented!("operator {}", op)panics on unknown operator bytes. Replacing withError::syntaxreturns a recoverable parse error instead.Changes
lexical.rsError::general("Expected initializer...")→Error::expected(["="], next.to_string(interner), next.span(), "const declaration")for_statement.rsError::general("unexpected token")→Error::general("'let' cannot be used as the iterable in a for-of loop", position)operator.rsunimplemented!("operator {}", op)→Err(Error::syntax(format!("unexpected operator '{}'", char::from(op)), start_pos.position()))Testing
cargo test -p boa_parser— 296 tests passcargo clippy -p boa_parser— no warnings