use std::borrow::Cow;
use typed_arena::Arena;
use crate::stt::SemanticValue;
use super::{RepeatRange, Rule};
pub struct RuleArena<'a> {
arena: Arena<Rule<'a>>,
}
impl<'a> RuleArena<'a> {
pub fn new() -> Self {
Self {
arena: Arena::new(),
}
}
pub fn alloc(&self, rule: Rule<'a>) -> &Rule<'a> {
self.arena.alloc(rule)
}
pub fn text<T: Into<Cow<'a, str>>>(&self, text: T) -> &Rule<'a> {
self.alloc(Rule::text(text))
}
pub fn choice<L: Into<Cow<'a, [&'a Rule<'a>]>>>(&self, options: L) -> &Rule<'a> {
self.alloc(Rule::choice(options))
}
pub fn sequence<L: Into<Cow<'a, [&'a Rule<'a>]>>>(&self, parts: L) -> &Rule<'a> {
self.alloc(Rule::sequence(parts))
}
pub fn repeat<R: Into<RepeatRange>>(&self, times: R, target: &'a Rule<'a>) -> &Rule<'a> {
self.alloc(Rule::repeat(times, target))
}
pub fn semantic<V: Into<SemanticValue<Cow<'a, str>>>>(
&self,
value: V,
target: &'a Rule<'a>,
) -> &Rule<'a> {
self.alloc(Rule::semantic(value, target))
}
}