55use crate :: cow_rc_str:: CowRcStr ;
66use crate :: tokenizer:: { SourceLocation , SourcePosition , Token , Tokenizer } ;
77use smallvec:: SmallVec ;
8+ use std:: fmt;
89use std:: ops:: BitOr ;
910use std:: ops:: Range ;
1011
@@ -53,6 +54,24 @@ pub enum BasicParseErrorKind<'i> {
5354 QualifiedRuleInvalid ,
5455}
5556
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+
5675/// The fundamental parsing errors that can be triggered by built-in parsing routines.
5776#[ derive( Clone , Debug , PartialEq ) ]
5877pub struct BasicParseError < ' i > {
@@ -123,6 +142,15 @@ impl<'i, T> ParseErrorKind<'i, T> {
123142 }
124143}
125144
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+
126154/// Extensible parse errors that can be encountered by client parsing implementations.
127155#[ derive( Clone , Debug , PartialEq ) ]
128156pub struct ParseError < ' i , E > {
@@ -137,7 +165,7 @@ impl<'i, T> ParseError<'i, T> {
137165 pub fn basic ( self ) -> BasicParseError < ' i > {
138166 match self . kind {
139167 ParseErrorKind :: Basic ( kind) => BasicParseError {
140- kind : kind ,
168+ kind,
141169 location : self . location ,
142170 } ,
143171 ParseErrorKind :: Custom ( _) => panic ! ( "Not a basic parse error" ) ,
@@ -156,6 +184,14 @@ impl<'i, T> ParseError<'i, T> {
156184 }
157185}
158186
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+
159195/// The owned input for a parser.
160196pub struct ParserInput < ' i > {
161197 tokenizer : Tokenizer < ' i > ,
@@ -396,7 +432,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
396432 #[ inline]
397433 pub fn new_basic_error ( & self , kind : BasicParseErrorKind < ' i > ) -> BasicParseError < ' i > {
398434 BasicParseError {
399- kind : kind ,
435+ kind,
400436 location : self . current_source_location ( ) ,
401437 }
402438 }
0 commit comments