use core::fmt;
use intern_lang::Symbol;
use token_lang::Span;
use crate::Limit;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MacroError {
NoRules,
DuplicateBinding {
rule: usize,
name: Symbol,
},
UnboundVariable {
rule: usize,
name: Symbol,
},
StillRepeating {
rule: usize,
name: Symbol,
},
NoRepeatingVariable {
rule: usize,
},
EmptyRepetition {
rule: usize,
},
OptionalSeparator {
rule: usize,
},
}
impl fmt::Display for MacroError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoRules => f.write_str("macro has no rules"),
Self::DuplicateBinding { rule, name } => {
write!(
f,
"rule {rule}: metavariable {name:?} is bound more than once"
)
}
Self::UnboundVariable { rule, name } => {
write!(
f,
"rule {rule}: metavariable {name:?} is not bound by the pattern"
)
}
Self::StillRepeating { rule, name } => write!(
f,
"rule {rule}: metavariable {name:?} is still repeating at this depth"
),
Self::NoRepeatingVariable { rule } => write!(
f,
"rule {rule}: template repetition contains no metavariable that repeats at its depth"
),
Self::EmptyRepetition { rule } => write!(
f,
"rule {rule}: pattern repetition can match without consuming input"
),
Self::OptionalSeparator { rule } => write!(
f,
"rule {rule}: an at-most-once repetition cannot have a separator"
),
}
}
}
impl core::error::Error for MacroError {}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ExpandError {
NoMatch {
span: Span,
},
Ambiguous {
rule: usize,
},
RepetitionMismatch {
rule: usize,
},
RecursionLimit {
limit: u32,
},
Budget(Limit),
ContextOverflow,
}
impl fmt::Display for ExpandError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoMatch { span } => write!(f, "no rule matched the invocation (stuck at {span})"),
Self::Ambiguous { rule } => {
write!(f, "rule {rule} matches the invocation in more than one way")
}
Self::RepetitionMismatch { rule } => write!(
f,
"rule {rule}: metavariables repeat a different number of times"
),
Self::RecursionLimit { limit } => {
write!(f, "macro expansion exceeded the recursion limit of {limit}")
}
Self::Budget(limit) => {
write!(f, "macro expansion exceeded its budget of {limit}")
}
Self::ContextOverflow => f.write_str("hygiene context space exhausted"),
}
}
}
impl core::error::Error for ExpandError {}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use alloc::string::ToString;
use super::*;
#[test]
fn test_macro_error_messages_name_the_rule() {
let name = Symbol::from_u32(3).unwrap();
let cases = [
(MacroError::NoRules, "macro has no rules"),
(
MacroError::DuplicateBinding { rule: 1, name },
"rule 1: metavariable Symbol(3) is bound more than once",
),
(
MacroError::UnboundVariable { rule: 0, name },
"rule 0: metavariable Symbol(3) is not bound by the pattern",
),
(
MacroError::StillRepeating { rule: 2, name },
"rule 2: metavariable Symbol(3) is still repeating at this depth",
),
(
MacroError::EmptyRepetition { rule: 4 },
"rule 4: pattern repetition can match without consuming input",
),
];
for (err, text) in cases {
assert_eq!(err.to_string(), text);
}
assert!(
MacroError::NoRepeatingVariable { rule: 0 }
.to_string()
.contains("no metavariable")
);
assert!(
MacroError::OptionalSeparator { rule: 0 }
.to_string()
.contains("separator")
);
}
#[test]
fn test_expand_error_messages() {
assert_eq!(
ExpandError::NoMatch {
span: Span::new(2, 5)
}
.to_string(),
"no rule matched the invocation (stuck at 2..5)"
);
assert!(
ExpandError::Ambiguous { rule: 1 }
.to_string()
.contains("rule 1")
);
assert!(
ExpandError::RepetitionMismatch { rule: 0 }
.to_string()
.contains("repeat")
);
assert!(
ExpandError::RecursionLimit { limit: 8 }
.to_string()
.contains('8')
);
assert!(
ExpandError::ContextOverflow
.to_string()
.contains("exhausted")
);
assert_eq!(
ExpandError::Budget(Limit::Tokens { max: 64 }).to_string(),
"macro expansion exceeded its budget of 64 output tokens"
);
assert_eq!(
ExpandError::Budget(Limit::Expansions { max: 3 }).to_string(),
"macro expansion exceeded its budget of 3 expansions"
);
}
#[test]
fn test_errors_are_std_errors() {
fn assert_error<E: core::error::Error>() {}
assert_error::<MacroError>();
assert_error::<ExpandError>();
}
}