@@ -118,11 +118,7 @@ impl From<FStringError> for LalrpopError<Location, Tok, LexicalError> {
118118
119119/// Represents an error during parsing
120120#[derive(Debug, PartialEq)]
121- pub struct ParseError {
122- pub error: ParseErrorType,
123- pub location: Location,
124- pub source_path: String,
125- }
121+ pub struct ParseError(rustpython_compiler_core::Error<ParseErrorType>);
126122
127123#[derive(Debug, PartialEq)]
128124pub enum ParseErrorType {
@@ -138,45 +134,59 @@ pub enum ParseErrorType {
138134 Lexical(LexicalErrorType),
139135}
140136
137+ impl From<ParseError> for rustpython_compiler_core::Error<ParseErrorType> {
138+ fn from(err: ParseError) -> Self {
139+ err.0
140+ }
141+ }
142+
143+ impl From<ParseError> for ParseErrorType {
144+ fn from(err: ParseError) -> Self {
145+ err.0.error
146+ }
147+ }
148+
141149/// Convert `lalrpop_util::ParseError` to our internal type
142150impl ParseError {
151+ fn new(error: ParseErrorType, location: Location, source_path: String) -> Self {
152+ Self(rustpython_compiler_core::Error {
153+ error,
154+ location,
155+ source_path,
156+ })
157+ }
158+
143159 pub(crate) fn from_lalrpop(
144160 err: LalrpopError<Location, Tok, LexicalError>,
145161 source_path: &str,
146162 ) -> Self {
147163 let source_path = source_path.to_owned();
148164 match err {
149165 // TODO: Are there cases where this isn't an EOF?
150- LalrpopError::InvalidToken { location } => ParseError {
151- error: ParseErrorType::Eof,
152- location,
153- source_path,
154- },
155- LalrpopError::ExtraToken { token } => ParseError {
156- error: ParseErrorType::ExtraToken(token.1),
157- location: token.0,
158- source_path,
159- },
160- LalrpopError::User { error } => ParseError {
161- error: ParseErrorType::Lexical(error.error),
162- location: error.location,
166+ LalrpopError::InvalidToken { location } => {
167+ ParseError::new(ParseErrorType::Eof, location, source_path)
168+ }
169+ LalrpopError::ExtraToken { token } => {
170+ ParseError::new(ParseErrorType::ExtraToken(token.1), token.0, source_path)
171+ }
172+ LalrpopError::User { error } => ParseError::new(
173+ ParseErrorType::Lexical(error.error),
174+ error.location,
163175 source_path,
164- } ,
176+ ) ,
165177 LalrpopError::UnrecognizedToken { token, expected } => {
166178 // Hacky, but it's how CPython does it. See PyParser_AddToken,
167179 // in particular "Only one possible expected token" comment.
168180 let expected = (expected.len() == 1).then(|| expected[0].clone());
169- ParseError {
170- error: ParseErrorType::UnrecognizedToken(token.1, expected),
171- location: token.0,
181+ ParseError::new(
182+ ParseErrorType::UnrecognizedToken(token.1, expected),
183+ token.0,
172184 source_path,
173- }
185+ )
186+ }
187+ LalrpopError::UnrecognizedEOF { location, .. } => {
188+ ParseError::new(ParseErrorType::Eof, location, source_path)
174189 }
175- LalrpopError::UnrecognizedEOF { location, .. } => ParseError {
176- error: ParseErrorType::Eof,
177- location,
178- source_path,
179- },
180190 }
181191 }
182192}
@@ -229,9 +239,9 @@ impl ParseErrorType {
229239}
230240
231241impl std::ops::Deref for ParseError {
232- type Target = ParseErrorType;
242+ type Target = rustpython_compiler_core::Error< ParseErrorType> ;
233243 fn deref(&self) -> &Self::Target {
234- &self.error
244+ &self.0
235245 }
236246}
237247
0 commit comments