mod dfa;
mod parser;
pub use parser::AttachedParser;
pub use parser::Parser;
use anyhow::{Result, bail};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct StateId(u16);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct CaptureId(u16);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct MatchId(u16);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Action {
StartCapture(CaptureId),
EndCapture(CaptureId, MatchId),
Done,
StartCaptureAndDone(CaptureId),
EndCaptureAndDone(CaptureId, MatchId),
}
impl Action {
pub(crate) fn push(self, action: Action) -> Result<Action> {
let action = match (self, action) {
(action, other) if action == other => action,
(Action::Done, Action::StartCapture(cid))
| (Action::StartCapture(cid), Action::Done)
| (Action::Done, Action::StartCaptureAndDone(cid))
| (Action::StartCaptureAndDone(cid), Action::Done) => Action::StartCaptureAndDone(cid),
(Action::Done, Action::EndCapture(cid, mid))
| (Action::EndCapture(cid, mid), Action::Done)
| (Action::Done, Action::EndCaptureAndDone(cid, mid))
| (Action::EndCaptureAndDone(cid, mid), Action::Done) => {
Action::EndCaptureAndDone(cid, mid)
}
(Action::StartCapture(cid), Action::StartCaptureAndDone(other))
| (Action::StartCaptureAndDone(other), Action::StartCapture(cid))
if cid == other =>
{
Action::StartCaptureAndDone(cid)
}
(Action::EndCapture(cid, mid), Action::EndCaptureAndDone(other_cid, other_mid))
| (Action::EndCaptureAndDone(other_cid, other_mid), Action::EndCapture(cid, mid))
if (cid, mid) == (other_cid, other_mid) =>
{
Action::EndCaptureAndDone(cid, mid)
}
(action, other) => bail!("Cannot {action:?} and {other:?} with the same state"),
};
Ok(action)
}
}