From a3ab765efe831505179a5966b5546509cb862717 Mon Sep 17 00:00:00 2001 From: Greg Brown Date: Sat, 16 Jan 2021 12:35:13 +0000 Subject: Fix warnings in `chomp::ast`. --- src/chomp/ast/error.rs | 34 ++++++++++++++++++++++++++++++++-- src/chomp/ast/mod.rs | 6 +++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/chomp/ast/error.rs b/src/chomp/ast/error.rs index d2c49cd..7be5354 100644 --- a/src/chomp/ast/error.rs +++ b/src/chomp/ast/error.rs @@ -14,13 +14,43 @@ pub enum SubstituteError { impl From for syn::Error { fn from(e: SubstituteError) -> Self { - todo!() + match e { + SubstituteError::FreeParameter { span, .. } => { + Self::new(span.unwrap_or_else(Span::call_site), "unbound parameter") + } + SubstituteError::WrongArgCount { call, expected, span } => { + let msg = if call.args.len() == 1 { + format!("this function takes {} arguments but 1 was supplied", expected) + } else { + format!("this function takes {} arguments but {} were supplied", expected, call.args.len()) + }; + + Self::new(span.unwrap_or_else(Span::call_site), msg) + } + } } } impl Display for SubstituteError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!() + match self { + Self::FreeParameter { param, span, name } => { + let start = span.unwrap_or_else(Span::call_site).start(); + if let Some(name) = name { + write!(f, "{}:{}: unbound parameter `{}`", start.line, start.column, name) + } else { + write!(f, "{}:{}: unbound parameter '{}", start.line, start.column, param.index) + } + } + Self::WrongArgCount { call, expected, span } => { + let start = span.unwrap_or_else(Span::call_site).start(); + if call.args.len() == 1 { + write!(f, "{}:{}: this function takes {} arguments but 1 was supplied", start.line, start.column, expected) + } else { + write!(f, "{}:{}: this function takes {} arguments but {} were supplied", start.line, start.column, expected, call.args.len()) + } + } + } } } diff --git a/src/chomp/ast/mod.rs b/src/chomp/ast/mod.rs index 110e5c0..9a159e4 100644 --- a/src/chomp/ast/mod.rs +++ b/src/chomp/ast/mod.rs @@ -8,7 +8,7 @@ use super::Name; pub mod error; pub mod substitute; -#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)] pub struct Epsilon; pub type Literal = String; @@ -102,7 +102,7 @@ impl PartialEq for Fix { impl Eq for Fix {} -#[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub struct Variable { pub index: usize, } @@ -113,7 +113,7 @@ impl Display for Variable { } } -#[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub struct Parameter { pub index: usize, } -- cgit v1.2.3