5
5
use crate :: cow_rc_str:: CowRcStr ;
6
6
use crate :: tokenizer:: { SourceLocation , SourcePosition , Token , Tokenizer } ;
7
7
use smallvec:: SmallVec ;
8
+ use std:: fmt;
8
9
use std:: ops:: BitOr ;
9
10
use std:: ops:: Range ;
10
11
@@ -53,6 +54,24 @@ pub enum BasicParseErrorKind<'i> {
53
54
QualifiedRuleInvalid ,
54
55
}
55
56
57
+ impl < ' i > fmt:: Display for BasicParseErrorKind < ' i > {
58
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
59
+ match self {
60
+ BasicParseErrorKind :: UnexpectedToken ( token) => {
61
+ write ! ( f, "unexpected token: {:?}" , token)
62
+ }
63
+ BasicParseErrorKind :: EndOfInput => write ! ( f, "unexpected end of input" ) ,
64
+ BasicParseErrorKind :: AtRuleInvalid ( rule) => {
65
+ write ! ( f, "invalid @ rule encountered: '@{}'" , rule)
66
+ }
67
+ BasicParseErrorKind :: AtRuleBodyInvalid => write ! ( f, "invalid @ rule body encountered" ) ,
68
+ BasicParseErrorKind :: QualifiedRuleInvalid => {
69
+ write ! ( f, "invalid qualified rule encountered" )
70
+ }
71
+ }
72
+ }
73
+ }
74
+
56
75
/// The fundamental parsing errors that can be triggered by built-in parsing routines.
57
76
#[ derive( Clone , Debug , PartialEq ) ]
58
77
pub struct BasicParseError < ' i > {
@@ -123,6 +142,15 @@ impl<'i, T> ParseErrorKind<'i, T> {
123
142
}
124
143
}
125
144
145
+ impl < ' i , E : fmt:: Display > fmt:: Display for ParseErrorKind < ' i , E > {
146
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
147
+ match self {
148
+ ParseErrorKind :: Basic ( ref basic) => basic. fmt ( f) ,
149
+ ParseErrorKind :: Custom ( ref custom) => custom. fmt ( f) ,
150
+ }
151
+ }
152
+ }
153
+
126
154
/// Extensible parse errors that can be encountered by client parsing implementations.
127
155
#[ derive( Clone , Debug , PartialEq ) ]
128
156
pub struct ParseError < ' i , E > {
@@ -137,7 +165,7 @@ impl<'i, T> ParseError<'i, T> {
137
165
pub fn basic ( self ) -> BasicParseError < ' i > {
138
166
match self . kind {
139
167
ParseErrorKind :: Basic ( kind) => BasicParseError {
140
- kind : kind ,
168
+ kind,
141
169
location : self . location ,
142
170
} ,
143
171
ParseErrorKind :: Custom ( _) => panic ! ( "Not a basic parse error" ) ,
@@ -156,6 +184,14 @@ impl<'i, T> ParseError<'i, T> {
156
184
}
157
185
}
158
186
187
+ impl < ' i , E : fmt:: Display > fmt:: Display for ParseError < ' i , E > {
188
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
189
+ self . kind . fmt ( f)
190
+ }
191
+ }
192
+
193
+ impl < ' i , E : fmt:: Display + fmt:: Debug > std:: error:: Error for ParseError < ' i , E > { }
194
+
159
195
/// The owned input for a parser.
160
196
pub struct ParserInput < ' i > {
161
197
tokenizer : Tokenizer < ' i > ,
@@ -396,7 +432,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
396
432
#[ inline]
397
433
pub fn new_basic_error ( & self , kind : BasicParseErrorKind < ' i > ) -> BasicParseError < ' i > {
398
434
BasicParseError {
399
- kind : kind ,
435
+ kind,
400
436
location : self . current_source_location ( ) ,
401
437
}
402
438
}
0 commit comments