-
Notifications
You must be signed in to change notification settings - Fork 325
[wit-parser] Migrate to structured errors in the AST/package parser #2465
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
Merged
alexcrichton
merged 15 commits into
bytecodealliance:main
from
PhoebeSzmucer:structured-parsing-errors
Mar 31, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f3c9fe2
[wit-parser] Migrate to structured errors in the AST/package parser
PhoebeSzmucer 0a58d5d
Update snapshots
PhoebeSzmucer 5164020
Derive Eq and PartialEq
PhoebeSzmucer 1d20ddc
Remove toposort::Error
PhoebeSzmucer e2c6c84
Remove useless comments
PhoebeSzmucer 0c713ea
Make highlight_span pub(crate) again
PhoebeSzmucer 4cdf617
Expose ast/parsing errors
PhoebeSzmucer 61619f6
Introduce new_syntax helper
PhoebeSzmucer 430f706
PackageParseErrorKind -> PackageParseErrors
PhoebeSzmucer c685bad
Introduce ParseResult
PhoebeSzmucer 3ab3261
Rename PackageParse -> Parse
PhoebeSzmucer 7211ee3
Comments
PhoebeSzmucer 09c1a90
Use singular ParseError
PhoebeSzmucer ec5d885
Remove some methods
PhoebeSzmucer f00e850
Merge branch 'main' into structured-parsing-errors
PhoebeSzmucer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| use alloc::boxed::Box; | ||
| use alloc::string::{String, ToString}; | ||
| use core::fmt; | ||
|
|
||
| use crate::{SourceMap, Span, ast::lex}; | ||
|
|
||
| pub type ParseResult<T, E = ParseError> = Result<T, E>; | ||
|
|
||
| #[non_exhaustive] | ||
| #[derive(Debug, PartialEq, Eq)] | ||
| pub enum ParseErrorKind { | ||
| /// Lexer error (invalid character, unterminated comment, etc.) | ||
| Lex(lex::Error), | ||
| /// Syntactic or semantic error within a single package (duplicate name, | ||
| /// invalid attribute, etc.) | ||
| Syntax { span: Span, message: String }, | ||
| /// A type/interface/world references a name that does not exist within | ||
| /// the same package. | ||
| ItemNotFound { | ||
| span: Span, | ||
| name: String, | ||
| kind: String, | ||
| hint: Option<String>, | ||
| }, | ||
| /// A type/interface/world depends on itself. | ||
| TypeCycle { | ||
| span: Span, | ||
| name: String, | ||
| kind: String, | ||
| }, | ||
| } | ||
|
|
||
| impl ParseErrorKind { | ||
| pub fn span(&self) -> Span { | ||
| match self { | ||
| ParseErrorKind::Lex(e) => Span::new(e.position(), e.position() + 1), | ||
| ParseErrorKind::Syntax { span, .. } | ||
| | ParseErrorKind::ItemNotFound { span, .. } | ||
| | ParseErrorKind::TypeCycle { span, .. } => *span, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for ParseErrorKind { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| ParseErrorKind::Lex(e) => fmt::Display::fmt(e, f), | ||
| ParseErrorKind::Syntax { message, .. } => message.fmt(f), | ||
| ParseErrorKind::ItemNotFound { | ||
| kind, name, hint, .. | ||
| } => { | ||
| write!(f, "{kind} `{name}` does not exist")?; | ||
| if let Some(hint) = hint { | ||
| write!(f, "\n{hint}")?; | ||
| } | ||
| Ok(()) | ||
| } | ||
| ParseErrorKind::TypeCycle { kind, name, .. } => { | ||
| write!(f, "{kind} `{name}` depends on itself") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Eq)] | ||
| pub struct ParseError(Box<ParseErrorKind>); | ||
|
|
||
| impl ParseError { | ||
| pub fn new_syntax(span: Span, message: impl Into<String>) -> Self { | ||
| ParseErrorKind::Syntax { | ||
| span, | ||
| message: message.into(), | ||
| } | ||
| .into() | ||
| } | ||
|
|
||
| pub fn kind(&self) -> &ParseErrorKind { | ||
| &self.0 | ||
| } | ||
|
|
||
| pub fn kind_mut(&mut self) -> &mut ParseErrorKind { | ||
| &mut self.0 | ||
| } | ||
|
|
||
| /// Format this error with source context (file:line:col + snippet) | ||
| pub fn highlight(&self, source_map: &SourceMap) -> String { | ||
| let e = self.kind(); | ||
| source_map | ||
| .highlight_span(e.span(), e) | ||
| .unwrap_or_else(|| e.to_string()) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for ParseError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Display::fmt(self.kind(), f) | ||
| } | ||
| } | ||
|
|
||
| impl core::error::Error for ParseError {} | ||
|
|
||
| impl From<ParseErrorKind> for ParseError { | ||
| fn from(kind: ParseErrorKind) -> Self { | ||
| ParseError(Box::new(kind)) | ||
| } | ||
| } | ||
|
|
||
| impl From<lex::Error> for ParseError { | ||
| fn from(e: lex::Error) -> Self { | ||
| ParseErrorKind::Lex(e).into() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.