Skip to content

Continuation PR for #256: Implement std::error::Error for errors #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Prev Previous commit
Next Next commit
improve the error implementation
  • Loading branch information
Martijn Groeneveldt committed Oct 19, 2019
commit a1f9e6d1c6a790d4663ce644aeee401fa8c1d106
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
/Cargo.lock
/.cargo/config
.idea
56 changes: 28 additions & 28 deletions procedural-masquerade/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,33 +244,33 @@ macro_rules! define_invoke_proc_macro {
#[doc(hidden)]
#[macro_export]
macro_rules! $macro_name {
($proc_macro_name: ident ! $paren: tt) => {
#[derive($proc_macro_name)]
#[allow(unused)]
enum ProceduralMasqueradeDummyType {
// The magic happens here.
//
// We use an `enum` with an explicit discriminant
// because that is the only case where a type definition
// can contain a (const) expression.
//
// `(0, "foo").0` evalutes to 0, with the `"foo"` part ignored.
//
// By the time the `#[proc_macro_derive]` function
// implementing `#[derive($proc_macro_name)]` is called,
// `$paren` has already been replaced with the input of this inner macro,
// but `stringify!` has not been expanded yet.
//
// This how arbitrary tokens can be inserted
// in the input to the `#[proc_macro_derive]` function.
//
// Later, `stringify!(...)` is expanded into a string literal
// which is then ignored.
// Using `stringify!` enables passing arbitrary tokens
// rather than only what can be parsed as a const expression.
Input = (0, stringify! $paren ).0
}
}
}
($proc_macro_name: ident ! $paren: tt) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a rustfmt bug that should be fixed and undone.

#[derive($proc_macro_name)]
#[allow(unused)]
enum ProceduralMasqueradeDummyType {
// The magic happens here.
//
// We use an `enum` with an explicit discriminant
// because that is the only case where a type definition
// can contain a (const) expression.
//
// `(0, "foo").0` evalutes to 0, with the `"foo"` part ignored.
//
// By the time the `#[proc_macro_derive]` function
// implementing `#[derive($proc_macro_name)]` is called,
// `$paren` has already been replaced with the input of this inner macro,
// but `stringify!` has not been expanded yet.
//
// This how arbitrary tokens can be inserted
// in the input to the `#[proc_macro_derive]` function.
//
// Later, `stringify!(...)` is expanded into a string literal
// which is then ignored.
// Using `stringify!` enables passing arbitrary tokens
// rather than only what can be parsed as a const expression.
Input = (0, stringify! $paren ).0
}
}
}
};
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ extern crate difference;
extern crate encoding_rs;
#[doc(hidden)]
pub extern crate phf as _internal__phf;
#[cfg(test)]
extern crate serde_json;
#[cfg(feature = "serde")]
extern crate serde;
#[cfg(test)]
extern crate serde_json;
#[cfg(feature = "heapsize")]
#[macro_use]
extern crate heapsize;
Expand Down
20 changes: 8 additions & 12 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,21 @@ macro_rules! cssparser_internal__to_lowercase {
#[macro_export]
#[doc(hidden)]
macro_rules! cssparser_internal__uninit {
($buffer: ident, $BUFFER_SIZE: expr) => {
{
$buffer = ::std::mem::MaybeUninit::<[u8; $BUFFER_SIZE]>::uninit();
&mut *($buffer.as_mut_ptr())
}
}
($buffer: ident, $BUFFER_SIZE: expr) => {{
$buffer = ::std::mem::MaybeUninit::<[u8; $BUFFER_SIZE]>::uninit();
&mut *($buffer.as_mut_ptr())
}};
}

// FIXME: remove this when we require Rust 1.36
#[cfg(not(has_std__mem__MaybeUninit))]
#[macro_export]
#[doc(hidden)]
macro_rules! cssparser_internal__uninit {
($buffer: ident, $BUFFER_SIZE: expr) => {
{
$buffer = ::std::mem::uninitialized::<[u8; $BUFFER_SIZE]>();
&mut $buffer
}
}
($buffer: ident, $BUFFER_SIZE: expr) => {{
$buffer = ::std::mem::uninitialized::<[u8; $BUFFER_SIZE]>();
&mut $buffer
}};
}

/// Implementation detail of match_ignore_ascii_case! and ascii_case_insensitive_phf_map! macros.
Expand Down
31 changes: 18 additions & 13 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ pub enum BasicParseErrorKind<'i> {
}

impl<'i> BasicParseErrorKind<'i> {
fn description(&self) -> &'static str {
fn description(&self) -> String {
match self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to Display::fmt?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in the last commit

BasicParseErrorKind::UnexpectedToken(_) => "Unexpected token",
BasicParseErrorKind::EndOfInput => "End of input",
BasicParseErrorKind::AtRuleInvalid(_) => "Invalid @ rule",
BasicParseErrorKind::AtRuleBodyInvalid => "Invalid @ rule body",
BasicParseErrorKind::QualifiedRuleInvalid => "Invalid qualified rule",
BasicParseErrorKind::UnexpectedToken(token) => {
format!("Unexpected token: '{}'", token.description())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just do: format!("Unexpected token: {:?}", token), and remove Token::description().

}
BasicParseErrorKind::EndOfInput => "End of input".to_owned(),
BasicParseErrorKind::AtRuleInvalid(message) => format!("Invalid @ rule: {}", message),
BasicParseErrorKind::AtRuleBodyInvalid => "Invalid @ rule body".to_owned(),
BasicParseErrorKind::QualifiedRuleInvalid => "Invalid qualified rule".to_owned(),
}
}
}
Expand All @@ -78,7 +80,7 @@ pub struct BasicParseError<'i> {

impl<'i> fmt::Display for BasicParseError<'i> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", self.kind.description())
formatter.write_str(self.kind.description().as_str())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That way this doesn't need the extra allocation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it may be nice to expose self.location, but that may be fine as a follow-up.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c962d4a

}
}

Expand Down Expand Up @@ -178,16 +180,19 @@ impl<'i, T> ParseError<'i, T> {
}
}

impl<'i, T> fmt::Display for ParseError<'i, T> {
impl<'i, T> fmt::Display for ParseError<'i, T>
where
T: fmt::Display,
{
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", match &self.kind {
ParseErrorKind::Basic(basic_kind) => basic_kind.description(),
ParseErrorKind::Custom(_) => "Custom error",
})
match &self.kind {
ParseErrorKind::Basic(basic_kind) => formatter.write_str(&basic_kind.description()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just use basic_kind.fmt(formatter)

ParseErrorKind::Custom(custom_type) => custom_type.fmt(formatter),
}
}
}

impl<'i, T> Error for ParseError<'i, T> where T: fmt::Debug {}
impl<'i, T> Error for ParseError<'i, T> where T: Error {}

/// The owned input for a parser.
pub struct ParserInput<'i> {
Expand Down
4 changes: 1 addition & 3 deletions src/rules_and_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ where
let start = self.input.state();
// FIXME: remove intermediate variable when lifetimes are non-lexical
let ident = match self.input.next_including_whitespace_and_comments() {
Ok(&Token::WhiteSpace(_)) | Ok(&Token::Comment(_)) | Ok(&Token::Semicolon) => {
continue
}
Ok(&Token::WhiteSpace(_)) | Ok(&Token::Comment(_)) | Ok(&Token::Semicolon) => continue,
Ok(&Token::Ident(ref name)) => Ok(Ok(name.clone())),
Ok(&Token::AtKeyword(ref name)) => Ok(Err(name.clone())),
Ok(token) => Err(token.clone()),
Expand Down
13 changes: 11 additions & 2 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,24 @@ impl TokenSerializationType {
),
Number => matches!(
other.0,
Ident | Function | UrlOrBadUrl | DelimMinus | Number | Percentage | DelimPercent | Dimension
Ident
| Function
| UrlOrBadUrl
| DelimMinus
| Number
| Percentage
| DelimPercent
| Dimension
),
DelimAt => matches!(other.0, Ident | Function | UrlOrBadUrl | DelimMinus),
DelimDotOrPlus => matches!(other.0, Number | Percentage | Dimension),
DelimAssorted | DelimAsterisk => matches!(other.0, DelimEquals),
DelimBar => matches!(other.0, DelimEquals | DelimBar | DashMatch),
DelimSlash => matches!(other.0, DelimAsterisk | SubstringMatch),
Nothing | WhiteSpace | Percentage | UrlOrBadUrl | Function | CDC | OpenParen
| DashMatch | SubstringMatch | DelimQuestion | DelimEquals | DelimPercent | Other => false,
| DashMatch | SubstringMatch | DelimQuestion | DelimEquals | DelimPercent | Other => {
false
}
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extern crate test;

use encoding_rs;
use serde_json::{self, Value, json, Map};
use serde_json::{self, json, Map, Value};

#[cfg(feature = "bench")]
use self::test::Bencher;
Expand All @@ -30,7 +30,7 @@ fn almost_equals(a: &Value, b: &Value) -> bool {
let a = a.as_f64().unwrap();
let b = b.as_f64().unwrap();
(a - b).abs() <= a.abs() * 1e-6
},
}

(&Value::Bool(a), &Value::Bool(b)) => a == b,
(&Value::String(ref a), &Value::String(ref b)) => a == b,
Expand Down Expand Up @@ -410,15 +410,16 @@ fn unicode_range() {
Ok(None)
}
});
result.unwrap()
result
.unwrap()
.iter()
.map(|v|
if let Some((v0, v1)) = v{
.map(|v| {
if let Some((v0, v1)) = v {
json!([v0, v1])
} else {
Value::Null
}
)
})
.collect::<Vec<_>>()
.to_json()
});
Expand Down Expand Up @@ -809,7 +810,11 @@ trait ToJson {
fn to_json(&self) -> Value;
}

impl<T> ToJson for T where T: Clone, Value: From<T> {
impl<T> ToJson for T
where
T: Clone,
Value: From<T>,
{
fn to_json(&self) -> Value {
Value::from(self.clone())
}
Expand Down
62 changes: 62 additions & 0 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,68 @@ impl<'a> Token<'a> {
BadUrl(_) | BadString(_) | CloseParenthesis | CloseSquareBracket | CloseCurlyBracket
)
}

pub(crate) fn description(&self) -> String {
match self {
Ident(name) => format!("A ident token '{}'", name),
AtKeyword(value) => format!("The value '{}' does not include the `@` marker", value),
Hash(value) => format!("The value '{}' does not include the `#` marker", value),
IDHash(value) => format!(
"The value '{}' does not include the `#` marker, but has a valid ID selector",
value
),
QuotedString(value) => format!("The value '{}' does not include the quotes", value),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this, I don't know what this is about?

UnquotedUrl(value) => format!(
"The value '{}' does not include the `url(` `)` markers.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right... UnquotedUrl is just url(string-without-quotes), not any kind of error.

value
),
Delim(character) => format!("'{}'", character),
Number {
has_sign, value, ..
} => {
let sign = if has_sign.clone() { '-' } else { '+' };
format!("{}{}", sign, value.to_string())
}
Percentage {
has_sign,
unit_value,
..
} => {
let sign = if has_sign.clone() { '-' } else { '+' };
format!("{}{}%", sign, unit_value.to_string())
}
Dimension {
has_sign,
value,
unit,
..
} => {
let sign = if has_sign.clone() { '-' } else { '+' };
format!("{}{} {}", sign, value.to_string(), unit)
}
WhiteSpace(whitespace) => format!("whitespace: '{}'", whitespace),
Comment(comment) => format!("The comment: '{}'", comment),
Colon => String::from(":"),
Semicolon => String::from(";"),
Comma => String::from(","),
IncludeMatch => String::from("~="),
DashMatch => String::from("|="),
PrefixMatch => String::from("^="),
SuffixMatch => String::from("$="),
SubstringMatch => String::from("*="),
CDO => String::from("<!--"),
CDC => String::from("-->"),
Function(name) => format!("The value ({}) does not include the `(` marker", name),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, these are legit token types. If the input is foo() and I do input.expect_ident(), I get an unexpected token error with the function, but there's nothing the "doesn't include the ( marker" or anything like that.

ParenthesisBlock => String::from("("),
SquareBracketBlock => String::from("["),
CurlyBracketBlock => String::from("{"),
BadUrl(url) => format!("Bad url: '{}'", url),
BadString(string) => format!("Bad string: '{}'", string),
CloseParenthesis => "Unclosed parenthesis".to_owned(),
CloseSquareBracket => "Unclosed square bracket".to_owned(),
CloseCurlyBracket => "Unclosed curly bracket".to_owned(),
}
}
}

#[derive(Clone)]
Expand Down