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

Compute input type for Graphs #44

Merged
merged 2 commits into from
Nov 4, 2023
Merged
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
59 changes: 57 additions & 2 deletions automata/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//! Automaton loosely based on visibly pushdown automata.

use crate::{
try_merge, Check, Ctrl, CurryInput, CurryStack, IllFormed, Input, InputError, ParseError,
RangeMap, Stack, State, ToSrc, Transition,
try_merge, Check, Ctrl, CurryInput, CurryStack, IllFormed, Input, InputError, Merge,
ParseError, RangeMap, Stack, State, ToSrc, Transition,
};
use core::{iter, num::NonZeroUsize};
use std::{
Expand Down Expand Up @@ -322,6 +322,61 @@ impl<I: Input, S: Stack, C: Ctrl<I, S>> Graph<I, S, C> {
})
}

/// Compute the input type of any successful run.
/// # Errors
/// If multiple accepting states attempt to return different types.
#[inline]
#[allow(clippy::missing_panics_doc)]
pub fn input_type(&self) -> Result<Option<String>, IllFormed<I, S, C>> {
self.initial
.view()
.map(|r| {
r.map_or_else(
|tag| get!(self.states, *unwrap!(self.tags.get(tag))),
|i| get!(self.states, i),
)
})
.try_fold(None, |acc, state| {
let shit =
acc.merge(state.transitions.values().try_fold(None, |accc, curry| {
accc.merge({
curry.values().try_fold(None, |acccc, t| {
acccc.merge(Some(t.update.input_t.clone())).map_or_else(
|(a, b)| {
if a == b {
Ok(Some(a))
} else {
Err(IllFormed::TypeMismatch(a, b))
}
},
Ok,
)
})?
})
.map_or_else(
|(a, b)| {
if a == b {
Ok(Some(a))
} else {
Err(IllFormed::TypeMismatch(a, b))
}
},
Ok,
)
})?);
shit.map_or_else(
|(a, b)| {
if a == b {
Ok(Some(a))
} else {
Err(IllFormed::TypeMismatch(a, b))
}
},
Ok,
)
})
}

/// Change nothing about the semantics but sort the internal vector of states.
#[inline]
#[allow(clippy::panic)] // <-- TODO
Expand Down