use std::rc::Rc;
use indexmap::IndexSet;
use crate::ir::{
self, AmbiguityReport, Element, Kind, NodeKind, ProbeDispatchSpec, ProbeHelperSpec, SrcSpan,
TailRepeatSpec, ValueAnnotation,
};
pub(crate) type Sequence = Vec<Rc<Element>>;
#[derive(Debug, Clone, Default)]
pub(crate) struct Production {
pub name: String,
pub alts: Vec<Sequence>,
pub incremental: bool,
pub node_kind: NodeKind,
pub origin: Option<String>,
pub sp: Option<SrcSpan>,
pub value: Option<ValueAnnotation>,
pub tail_repeat: Option<TailRepeatSpec>,
pub repeat_helper: bool,
pub repeat_loop: bool,
pub debt_guard: Option<String>,
pub debt_owed: Option<Vec<String>>,
pub probe_dispatch: Option<ProbeDispatchSpec>,
pub probe_helper: Option<ProbeHelperSpec>,
}
impl Production {
pub(crate) fn rebuilt(&self, alts: Vec<Sequence>) -> Self {
Self {
name: self.name.clone(),
alts,
node_kind: self.node_kind,
origin: self.origin.clone(),
sp: self.sp,
value: self.value.clone(),
..Default::default()
}
}
pub(crate) fn helper(name: impl Into<String>, alts: Vec<Sequence>, origin: &str) -> Self {
Self {
name: name.into(),
alts,
node_kind: NodeKind::Helper,
origin: Some(origin.to_string()),
..Default::default()
}
}
fn from_ir(p: &ir::Production) -> Self {
Self {
name: p.name.clone(),
alts: p
.alts
.iter()
.map(|alt| alt.iter().map(AsElement::share).collect())
.collect(),
incremental: p.incremental,
node_kind: p.node_kind,
origin: p.origin.clone(),
sp: p.sp,
value: p.value.clone(),
tail_repeat: p.tail_repeat.clone(),
repeat_helper: p.repeat_helper,
repeat_loop: p.repeat_loop,
debt_guard: p.debt_guard.clone(),
debt_owed: p.debt_owed.clone(),
probe_dispatch: p.probe_dispatch.clone(),
probe_helper: p.probe_helper.clone(),
}
}
fn into_ir(self) -> ir::Production {
ir::Production {
name: self.name,
alts: self
.alts
.into_iter()
.map(|alt| alt.into_iter().map(Rc::unwrap_or_clone).collect())
.collect(),
incremental: self.incremental,
node_kind: self.node_kind,
origin: self.origin,
sp: self.sp,
value: self.value,
tail_repeat: self.tail_repeat,
repeat_helper: self.repeat_helper,
repeat_loop: self.repeat_loop,
debt_guard: self.debt_guard,
debt_owed: self.debt_owed,
probe_dispatch: self.probe_dispatch,
probe_helper: self.probe_helper,
}
}
}
#[derive(Debug, Clone, Default)]
pub(crate) struct Grammar {
pub productions: Vec<Production>,
pub ambiguities: Vec<AmbiguityReport>,
pub remove: Vec<String>,
pub clear_all: bool,
}
impl Grammar {
pub(crate) fn from_ir(grammar: &ir::Grammar) -> Self {
Self {
productions: grammar
.productions
.iter()
.map(Production::from_ir)
.collect(),
ambiguities: grammar.ambiguities.clone(),
remove: grammar.remove.clone(),
clear_all: grammar.clear_all,
}
}
pub(crate) fn into_ir(self) -> ir::Grammar {
ir::Grammar {
productions: self
.productions
.into_iter()
.map(Production::into_ir)
.collect(),
ambiguities: self.ambiguities,
remove: self.remove,
clear_all: self.clear_all,
}
}
pub(crate) fn find(&self, name: &str) -> Option<&Production> {
self.productions.iter().find(|p| p.name == name)
}
}
pub(crate) fn origin_of(prod: &Production) -> &str {
prod.origin.as_deref().unwrap_or(&prod.name)
}
pub(crate) trait AsElement {
fn element(&self) -> ∈
fn share(&self) -> Rc<Element>;
}
impl AsElement for Element {
fn element(&self) -> &Element {
self
}
fn share(&self) -> Rc<Element> {
Rc::new(self.clone())
}
}
impl AsElement for Rc<Element> {
fn element(&self) -> &Element {
self
}
fn share(&self) -> Rc<Element> {
Rc::clone(self)
}
}
pub(crate) fn owned(seq: &[Rc<Element>]) -> ir::Sequence {
seq.iter().map(|el| Element::clone(el)).collect()
}
pub(crate) fn refs_in<E: AsElement>(alt: &[E], out: &mut IndexSet<String>) {
for el in alt {
match &el.element().kind {
Kind::Ref { name, .. } => {
out.insert(name.clone());
}
Kind::Opt { inner }
| Kind::Star { inner, .. }
| Kind::Plus { inner }
| Kind::Rep { inner, .. } => refs_in(std::slice::from_ref(inner.as_ref()), out),
Kind::Group { alts } => {
for a in alts {
refs_in(a, out);
}
}
_ => {}
}
}
}
pub(crate) fn tokens_in<E: AsElement>(alt: &[E], out: &mut IndexSet<String>) {
for el in alt {
match &el.element().kind {
Kind::Token { name, .. } => {
out.insert(name.clone());
}
Kind::Opt { inner }
| Kind::Star { inner, .. }
| Kind::Plus { inner }
| Kind::Rep { inner, .. } => tokens_in(std::slice::from_ref(inner.as_ref()), out),
Kind::Group { alts } => {
for a in alts {
tokens_in(a, out);
}
}
_ => {}
}
}
}