@@ -117,8 +117,7 @@ impl From<FStringError> for LalrpopError<Location, Tok, LexicalError> {
117117}
118118
119119/// Represents an error during parsing
120- #[ derive( Debug , PartialEq ) ]
121- pub struct ParseError ( rustpython_compiler_core:: BaseError < ParseErrorType > ) ;
120+ pub type ParseError = rustpython_compiler_core:: BaseError < ParseErrorType > ;
122121
123122#[ derive( Debug , PartialEq ) ]
124123pub enum ParseErrorType {
@@ -134,66 +133,44 @@ pub enum ParseErrorType {
134133 Lexical ( LexicalErrorType ) ,
135134}
136135
137- impl From < ParseError > for rustpython_compiler_core:: BaseError < 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-
149136/// Convert `lalrpop_util::ParseError` to our internal type
150- impl ParseError {
151- fn new ( error : ParseErrorType , location : Location , source_path : String ) -> Self {
152- Self ( rustpython_compiler_core:: BaseError {
153- error,
137+ pub ( crate ) fn parse_error_from_lalrpop (
138+ err : LalrpopError < Location , Tok , LexicalError > ,
139+ source_path : & str ,
140+ ) -> ParseError {
141+ let source_path = source_path. to_owned ( ) ;
142+ match err {
143+ // TODO: Are there cases where this isn't an EOF?
144+ LalrpopError :: InvalidToken { location } => ParseError {
145+ error : ParseErrorType :: Eof ,
154146 location,
155147 source_path,
156- } )
157- }
158-
159- pub ( crate ) fn from_lalrpop (
160- err : LalrpopError < Location , Tok , LexicalError > ,
161- source_path : & str ,
162- ) -> Self {
163- let source_path = source_path. to_owned ( ) ;
164- match err {
165- // TODO: Are there cases where this isn't an EOF?
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 ,
148+ } ,
149+ LalrpopError :: ExtraToken { token } => ParseError {
150+ error : ParseErrorType :: ExtraToken ( token. 1 ) ,
151+ location : token. 0 ,
152+ source_path,
153+ } ,
154+ LalrpopError :: User { error } => ParseError {
155+ error : ParseErrorType :: Lexical ( error. error ) ,
156+ location : error. location ,
157+ source_path,
158+ } ,
159+ LalrpopError :: UnrecognizedToken { token, expected } => {
160+ // Hacky, but it's how CPython does it. See PyParser_AddToken,
161+ // in particular "Only one possible expected token" comment.
162+ let expected = ( expected. len ( ) == 1 ) . then ( || expected[ 0 ] . clone ( ) ) ;
163+ ParseError {
164+ error : ParseErrorType :: UnrecognizedToken ( token. 1 , expected) ,
165+ location : token. 0 ,
175166 source_path,
176- ) ,
177- LalrpopError :: UnrecognizedToken { token, expected } => {
178- // Hacky, but it's how CPython does it. See PyParser_AddToken,
179- // in particular "Only one possible expected token" comment.
180- let expected = ( expected. len ( ) == 1 ) . then ( || expected[ 0 ] . clone ( ) ) ;
181- ParseError :: new (
182- ParseErrorType :: UnrecognizedToken ( token. 1 , expected) ,
183- token. 0 ,
184- source_path,
185- )
186- }
187- LalrpopError :: UnrecognizedEOF { location, .. } => {
188- ParseError :: new ( ParseErrorType :: Eof , location, source_path)
189167 }
190168 }
191- }
192- }
193-
194- impl fmt:: Display for ParseError {
195- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
196- self . 0 . fmt ( f)
169+ LalrpopError :: UnrecognizedEOF { location, .. } => ParseError {
170+ error : ParseErrorType :: Eof ,
171+ location,
172+ source_path,
173+ } ,
197174 }
198175}
199176
@@ -237,16 +214,3 @@ impl ParseErrorType {
237214 )
238215 }
239216}
240-
241- impl std:: ops:: Deref for ParseError {
242- type Target = rustpython_compiler_core:: BaseError < ParseErrorType > ;
243- fn deref ( & self ) -> & Self :: Target {
244- & self . 0
245- }
246- }
247-
248- impl Error for ParseError {
249- fn source ( & self ) -> Option < & ( dyn Error + ' static ) > {
250- None
251- }
252- }
0 commit comments