Implement ${count()} metavariable expression

This commit is contained in:
Ryo Yoshida 2023-05-28 19:54:36 +09:00
parent 9ebaa85d37
commit 0d4d1d7e3b
No known key found for this signature in database
GPG key ID: E25698A930586171
7 changed files with 389 additions and 29 deletions

View file

@ -19,6 +19,7 @@ mod benchmark;
mod token_map;
use ::tt::token_id as tt;
use stdx::impl_from;
use std::fmt;
@ -77,8 +78,11 @@ pub enum ExpandError {
LimitExceeded,
NoMatchingRule,
UnexpectedToken,
CountError(CountError),
}
impl_from!(CountError for ExpandError);
impl ExpandError {
fn binding_error(e: impl Into<Box<str>>) -> ExpandError {
ExpandError::BindingError(Box::new(e.into()))
@ -94,6 +98,23 @@ impl fmt::Display for ExpandError {
ExpandError::ConversionError => f.write_str("could not convert tokens"),
ExpandError::LimitExceeded => f.write_str("Expand exceed limit"),
ExpandError::LeftoverTokens => f.write_str("leftover tokens"),
ExpandError::CountError(e) => e.fmt(f),
}
}
}
// FIXME: Showing these errors could be nicer.
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum CountError {
OutOfBounds,
Misplaced,
}
impl fmt::Display for CountError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CountError::OutOfBounds => f.write_str("${count} out of bounds"),
CountError::Misplaced => f.write_str("${count} misplaced"),
}
}
}