use crate::{
syntax::{layout_of, NodeKind},
tokens::TokenKind,
};
pub fn is_empty_capable(kind: NodeKind) -> bool {
is_empty_capable_seen(kind, &mut Vec::new())
}
fn is_empty_capable_seen(kind: NodeKind, seen: &mut Vec<NodeKind>) -> bool {
if seen.contains(&kind) {
return false;
}
seen.push(kind);
match layout_of(kind) {
Layout::Sequence(sequence) => sequence.items.iter().all(|item| item.optional),
Layout::List(_) => false,
Layout::Choice(choice) => choice
.options
.iter()
.any(|option| is_empty_capable_seen(*option, seen)),
}
}
#[derive(Debug, Copy, Clone)]
pub enum Layout {
Sequence(Sequence),
Choice(Choice),
List(List),
}
#[derive(Debug, Copy, Clone)]
pub struct Sequence {
pub kind: NodeKind,
pub items: &'static [LayoutItem],
}
#[derive(Debug, Copy, Clone)]
pub struct Choice {
pub options: &'static [NodeKind],
}
#[derive(Debug, Copy, Clone)]
pub struct List {
pub kind: NodeKind,
pub element: &'static LayoutItem,
pub separator: &'static LayoutItem,
}
#[derive(Debug, Copy, Clone)]
pub struct LayoutItem {
pub optional: bool,
pub repeated: bool,
pub name: &'static str,
pub kind: LayoutItemKind,
}
#[derive(Debug, Copy, Clone)]
pub enum LayoutItemKind {
Token(TokenKind),
Node(NodeKind),
NodeChoice(&'static [NodeKind]),
TokenChoice(&'static [TokenKind]),
}