use std::fmt::Debug;
use std::marker::PhantomData;
use crate::ctx::Context;
use crate::ctx::CtxGuard;
use crate::ctx::Match;
use crate::ctx::Span;
use crate::err::Error;
use crate::re::def_not;
use crate::re::trace;
use crate::re::Ctor;
use crate::re::Extract;
use crate::re::Handler;
use crate::re::Regex;
#[derive(Default, Copy)]
pub struct Or<C, L, R> {
left: L,
right: R,
marker: PhantomData<C>,
}
def_not!(Or<C, L, R>);
impl<C, L, R> Debug for Or<C, L, R>
where
L: Debug,
R: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Or")
.field("left", &self.left)
.field("right", &self.right)
.finish()
}
}
impl<C, L, R> Clone for Or<C, L, R>
where
L: Clone,
R: Clone,
{
fn clone(&self) -> Self {
Self {
left: self.left.clone(),
right: self.right.clone(),
marker: self.marker,
}
}
}
impl<C, L, R> Or<C, L, R> {
pub fn new(pat1: L, pat2: R) -> Self {
Self {
left: pat1,
right: pat2,
marker: PhantomData,
}
}
pub fn left(&self) -> &L {
&self.left
}
pub fn left_mut(&mut self) -> &mut L {
&mut self.left
}
pub fn right(&self) -> &R {
&self.right
}
pub fn right_mut(&mut self) -> &mut R {
&mut self.right
}
pub fn set_left(&mut self, left: L) -> &mut Self {
self.left = left;
self
}
pub fn set_right(&mut self, right: R) -> &mut Self {
self.right = right;
self
}
}
impl<'a, C, L, R, M, O, H, A> Ctor<'a, C, M, O, H, A> for Or<C, L, R>
where
L: Ctor<'a, C, M, O, H, A>,
R: Ctor<'a, C, M, O, H, A>,
C: Context<'a> + Match<C>,
H: Handler<A, Out = M, Error = Error>,
A: Extract<'a, C, Span, Out<'a> = A, Error = Error>,
{
#[inline(always)]
fn constrct(&self, ctx: &mut C, func: &mut H) -> Result<O, Error> {
let mut g = CtxGuard::new(ctx);
let beg = g.beg();
let mut ret = trace!("or", beg @ "left", self.left.constrct(g.ctx(), func));
if ret.is_err() {
ret = trace!("or", beg @ "right", self.right.constrct(g.reset().ctx(), func));
}
trace!("or", beg -> g.end(), ret.is_ok());
g.process_ret(ret)
}
}
impl<'a, C, L, R> Regex<C> for Or<C, L, R>
where
L: Regex<C, Ret = Span>,
R: Regex<C, Ret = Span>,
C: Context<'a> + Match<C>,
{
type Ret = L::Ret;
#[inline(always)]
fn try_parse(&self, ctx: &mut C) -> Result<Self::Ret, Error> {
let mut g = CtxGuard::new(ctx);
let beg = g.beg();
let ret = trace!("or", beg @ "left", g.try_mat(&self.left).or_else(|_| {
trace!("or", beg @ "right", g.reset().try_mat(&self.right))
}));
trace!("or", beg => g.end(), ret)
}
}