Skip to content

Commit

Permalink
Compute input type for Graphs (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrsturgeon authored Nov 4, 2023
2 parents 4a6f619 + 788a953 commit dcc95d9
Showing 1 changed file with 57 additions and 2 deletions.
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

0 comments on commit dcc95d9

Please sign in to comment.