Skip to content

Commit c49f434

Browse files
authored
Auto merge of servo#332 - servo:parseerror, r=emilio
Make `ParseError` a `std::error::Error` Previously the `ParseError` struct did not implement `std::error::Error` or `std::fmt::Display`. This meant that `ParseError` was not boxable into an "any error" box like `anyhow::Error`. This commit adds Error and Display implementations for ParseError and friends. Rebase + MSRV fix of servo#298
2 parents 9ea6150 + c2ca656 commit c49f434

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/parser.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::cow_rc_str::CowRcStr;
66
use crate::tokenizer::{SourceLocation, SourcePosition, Token, Tokenizer};
77
use smallvec::SmallVec;
8+
use std::fmt;
89
use std::ops::BitOr;
910
use 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)]
5877
pub 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)]
128156
pub 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.
160196
pub 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

Comments
 (0)