Skip to content

Commit

Permalink
lexpr-macros: Provide a Display implementation for ParseError
Browse files Browse the repository at this point in the history
This improvement was prompted by `clippy`, which now warns about
unused values contained in enum variants of `ParseError`, even if they
are used by a derived `Debug` implementation.

As a bonus to fixing the clippy warning, the error messages are now a
bit nicer.
  • Loading branch information
Andreas Rottmann authored and rotty committed Jun 15, 2024
1 parent 5089e0b commit 35c5d2e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lexpr-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use quote::quote;
pub fn sexp(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let output = match expand(TokenStream::from(input)) {
Err(e) => {
let msg = format!("could not parse s-expression: {:?}", e);
let msg = format!("could not parse s-expression: {}", e);
quote! { compile_error!(#msg) }
}
Ok(output) => output,
Expand Down
24 changes: 24 additions & 0 deletions lexpr-macros/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

use crate::value::Value;

use proc_macro2::{Delimiter, Literal, Spacing, TokenStream, TokenTree};
Expand All @@ -17,6 +19,28 @@ pub enum ParseError {
UnexpectedEnd,
}

impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ExpectedStringLiteral(literal) => {
write!(f, "expected string literal `{}`", literal)
}
Self::UnexpectedToken(tt) => write!(f, "unexpected token `{}`", tt),
Self::UnexpectedChar(c) => write!(f, "unexpected character `{}`", c),
Self::UnexpectedDelimiter(delim) => {
let delim = match delim {
Delimiter::Brace => "brace",
Delimiter::Bracket => "bracket",
Delimiter::Parenthesis => "parenthesis",
Delimiter::None => "non",
};
write!(f, "unexpected {}-delimited expression", delim)
}
Self::UnexpectedEnd => write!(f, "unexpected end of input"),
}
}
}

impl Parser {
fn new(tokens: Vec<TokenTree>) -> Self {
Parser { tokens, index: 0 }
Expand Down
3 changes: 3 additions & 0 deletions lexpr/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Changes:
octothorpe, as in Emacs Lisp and Common Lisp, allowing for a
less-noisy spelling; e.g.: `:foo` instead of `#:foo`. Feature
request (#99) and initial implementation (#96) by @samuel-jimenez.
- The errors produced by the `sexp!` macro now are a bit more
human-oriented, as the `lexpr-macros` `ParseError` type gained a
`Display` implementation.

Maintenance-related changes:

Expand Down

0 comments on commit 35c5d2e

Please sign in to comment.