Skip to content
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

Implement fmt::Display for IllFormed<..> #39

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions automata/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ impl<S: Stack> Action<S> {
}
Some(())
}

/// Write this as a verb in natural language.
#[inline]
pub fn in_english(&self) -> String {
match *self {
Self::Local => "do nothing to".to_owned(),
Self::Push(ref s) => format!("push `{}` onto", s.to_src()),
Self::Pop => "pop from".to_owned(),
}
}
}
78 changes: 76 additions & 2 deletions automata/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
//! Check well-formedness.

use crate::{
Action, Ctrl, CurryInput, CurryStack, Input, Range, RangeMap, Stack, State, Transition, Update,
Action, Ctrl, CurryInput, CurryStack, Input, Range, RangeMap, Stack, State, ToSrc, Transition,
Update,
};
use core::{mem, num::NonZeroUsize};
use core::{fmt, mem, num::NonZeroUsize};
use std::collections::BTreeSet;

/// Maximum size we're willing to tolerate in an `Err` variant (for performance reasons).
Expand Down Expand Up @@ -92,6 +93,79 @@ impl<I: Input, S: Stack> IllFormed<I, S, usize> {
}
}

impl<I: Input, S: Stack, C: Ctrl<I, S>> fmt::Display for IllFormed<I, S, C> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::OutOfBounds(i) => write!(f, "State index out of bounds: {i}"),
Self::ProlongingDeath => write!(
f,
"Transition to a state that will never accept. \
Try removing the state along with any transitions to it.",
),
Self::InvertedRange(ref a, ref b) => {
write!(
f,
"Range with endpoints flipped: {}..={}",
a.to_src(),
b.to_src(),
)
}
Self::RangeMapOverlap(ref r) => {
write!(f, "Multiple ranges would accept {}", r.to_src())
}
Self::WildcardMask {
ref arg_stack,
ref arg_token,
ref possibility_1,
ref possibility_2,
} => {
write!(
f,
"When the stack top is {} and the token is {}, \
a wildcard match succeeds ({}), \
but so does a specific match ({}).",
arg_stack.to_src(),
arg_token.to_src(),
possibility_1.to_src(),
possibility_2.to_src(),
)
}
Self::Superposition(a, b) => write!(
f,
"Tried to visit two different deterministic states \
({a} and {b}) at the same time.",
),
Self::IncompatibleStackActions(ref a, ref b) => {
write!(
f,
"Can't {} and {} the stack at the same time.",
a.in_english(),
b.in_english(),
)
}
Self::IncompatibleCallbacks(ref a, ref b) => {
write!(
f,
"Tried to call both `{}` and `{}` at the same time.",
a.src, b.src,
)
}
Self::DuplicateState(ref s) => write!(f, "Duplicate state: {}", s.to_src()),
Self::TagDNE(ref tag) => write!(
f,
"Requested a transition to a tag that does not exist: \"{tag}\"",
),
Self::InitialNotUnit(ref s) => write!(
f,
"Initial state needs to take a unit-type input (`()`) but takes `{s}` instead.",
),
Self::TypeMismatch(ref a, ref b) => write!(f, "Type mismatch: `{a}` =/= `{b}`."),
Self::WrongReturnType(ref a, ref b) => write!(f, "Wrong output type: `{a}` =/= `{b}`"),
}
}
}

/// Check well-formedness.
pub trait Check<I: Input, S: Stack, C: Ctrl<I, S>> {
/// Check well-formedness.
Expand Down