regast-core 0.1.0

Parse-tree matching backends for regast
Documentation
use crate::{IrId, IrKind, IrPool, Value, deriv::nullable_at};

pub fn mkeps(pool: &mut IrPool, root: IrId) -> Value {
    mkeps_at(pool, root, 1, 2)
}

pub fn mkeps_at(pool: &mut IrPool, root: IrId, position: usize, input_len: usize) -> Value {
    debug_assert!(nullable_at(pool, root, position, input_len));
    match pool.kind(root).clone() {
        IrKind::One | IrKind::Anchor(..) => Value::Empty,
        IrKind::Alt(left, right, _, _) => {
            if nullable_at(pool, left, position, input_len) {
                Value::Left(Box::new(mkeps_at(pool, left, position, input_len)))
            } else {
                Value::Right(Box::new(mkeps_at(pool, right, position, input_len)))
            }
        }
        IrKind::Seq(left, right, _) => Value::Seq(
            Box::new(mkeps_at(pool, left, position, input_len)),
            Box::new(mkeps_at(pool, right, position, input_len)),
        ),
        IrKind::Star(..) => Value::Stars(Vec::new()),
        IrKind::Zero | IrKind::Class(..) => unreachable!("mkeps requires nullable IR"),
    }
}