use super::*;
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct AttrDecl {
pub(crate) name: String,
pub(crate) ty: String,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ChildCardinality {
pub(crate) min: usize,
pub(crate) max: Option<usize>,
}
impl ChildCardinality {
pub(crate) const ZERO: Self = Self {
min: 0,
max: Some(0),
};
pub(crate) const ONE: Self = Self {
min: 1,
max: Some(1),
};
pub(crate) const fn is_required_single(self) -> bool {
self.min == 1 && matches!(self.max, Some(1))
}
pub(crate) const fn is_repeated(self) -> bool {
match self.max {
Some(max) => max > 1,
None => true,
}
}
}
pub(crate) const SIBLING_DECLARATION_SUFFIX: &str = " (sibling declaration)";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct GroupSpan {
pub(crate) start: usize,
pub(crate) end: usize,
pub(crate) optional: bool,
pub(crate) repeated: bool,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct ElementRef {
pub(crate) label: Option<String>,
pub(crate) target: String,
pub(crate) token_types: Vec<i32>,
pub(crate) is_block: bool,
pub(crate) is_list: bool,
pub(crate) cardinality: ChildCardinality,
pub(crate) stable_accessor: bool,
pub(crate) choice_branch: Vec<(usize, usize)>,
pub(crate) choice_arity: Vec<usize>,
pub(crate) choice_spans: Vec<(usize, usize)>,
pub(crate) group_local_cardinality: ChildCardinality,
pub(crate) group_spans: Vec<GroupSpan>,
pub(crate) branch_spans: Vec<(usize, usize)>,
pub(crate) leading_terminal: bool,
pub(crate) span: Option<(usize, usize)>,
pub(crate) branch_local_cardinality: ChildCardinality,
}
impl ElementRef {
pub(crate) fn can_coexist_with(&self, other: &Self) -> bool {
!self.choice_branch.iter().any(|(choice, branch)| {
other
.choice_branch
.iter()
.any(|(other_choice, other_branch)| {
choice == other_choice && branch != other_branch
})
})
}
pub(crate) fn retain_choices(&mut self, mut keep: impl FnMut(usize) -> bool) {
let mask = self
.choice_branch
.iter()
.map(|&(choice, _)| keep(choice))
.collect::<Vec<_>>();
fn retain_by_mask<T>(list: &mut Vec<T>, mask: &[bool]) {
let mut index = 0;
list.retain(|_| {
let kept = mask.get(index).copied().unwrap_or(true);
index += 1;
kept
});
}
retain_by_mask(&mut self.choice_branch, &mask);
retain_by_mask(&mut self.choice_arity, &mask);
retain_by_mask(&mut self.choice_spans, &mask);
retain_by_mask(&mut self.branch_spans, &mask);
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct AltModel {
pub(crate) label: Option<String>,
pub(crate) span: (usize, usize),
pub(crate) refs: Vec<ElementRef>,
pub(crate) children: BTreeMap<String, ChildCardinality>,
pub(crate) leading_target: Option<String>,
}
impl AltModel {
pub(crate) fn is_lr_operator(&self, rule_name: &str) -> bool {
self.leading_target.as_deref() == Some(rule_name)
}
}
#[derive(Clone, Debug, Default)]
pub(crate) struct RuleModel {
pub(crate) name: String,
pub(crate) attrs: Vec<AttrDecl>,
pub(crate) local_names: Vec<String>,
pub(crate) arg_names: Vec<String>,
pub(crate) init_body: Option<String>,
pub(crate) after_body: Option<String>,
pub(crate) alts: Vec<AltModel>,
}
impl RuleModel {
pub(crate) const fn has_attrs(&self) -> bool {
!self.attrs.is_empty()
}
pub(crate) fn attr(&self, name: &str) -> Option<&AttrDecl> {
self.attrs.iter().find(|attr| attr.name == name)
}
pub(crate) fn alt_at(&self, offset: usize) -> Option<&AltModel> {
self.alts
.iter()
.find(|alt| alt.span.0 <= offset && offset < alt.span.1)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct MemberField {
pub(crate) source: SourceId,
pub(crate) attributes: String,
pub(crate) name: String,
pub(crate) ty: String,
pub(crate) init: String,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct MemberItem {
pub(crate) source: SourceId,
pub(crate) body: String,
}
#[derive(Clone, Debug, Default)]
pub(crate) struct MembersModel {
pub(crate) fields: Vec<MemberField>,
pub(crate) impl_items: Vec<MemberItem>,
pub(crate) module_items: Vec<MemberItem>,
pub(crate) module_symbols: BTreeSet<String>,
pub(crate) module_symbol_cfgs: BTreeMap<String, Vec<Vec<String>>>,
pub(crate) module_import_cfgs: BTreeMap<String, Vec<Vec<String>>>,
}
#[derive(Clone, Debug, Default)]
pub(crate) struct EmbeddedModel {
pub(crate) rules: Vec<RuleModel>,
pub(crate) parser_members: MembersModel,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ActionSite {
Body,
After,
Init,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ParserBodyKind {
Action,
Predicate,
}
pub(crate) const ANTLR4RUST_TOKEN_ALIAS_MODULE: &str = "__antlr4rust_token_aliases";
pub(crate) const ANTLR4RUST_CONTEXT_WRAPPER: &str = "__Antlr4RustContext";
pub(crate) const ANTLR4RUST_INPUT_FACADE: &str = "__Antlr4RustInput";
pub(crate) const ANTLR4RUST_TOKEN_VIEW: &str = "__Antlr4RustTokenView";
#[derive(Clone, Copy)]
pub(crate) struct Antlr4RustNames<'a> {
pub(crate) token_alias_module: &'a str,
pub(crate) context_wrapper: &'a str,
pub(crate) input_facade: &'a str,
}
pub(crate) fn map_attr_type(raw: &str) -> String {
let raw = raw.trim();
if let Some(inner) = raw
.strip_prefix("List")
.map(str::trim_start)
.and_then(|rest| rest.strip_prefix('<'))
.and_then(|inner| inner.strip_suffix('>'))
.map(str::trim)
.filter(|inner| !inner.is_empty())
{
return format!("Vec<{}>", map_attr_type(inner));
}
match raw {
"int" | "Integer" => "i32".to_owned(),
"boolean" => "bool".to_owned(),
"float" | "double" => "f64".to_owned(),
other => other.to_owned(),
}
}
pub(crate) fn split_name_colon_type(part: &str) -> Option<(&str, &str)> {
let colon = part.find(':')?;
if part[colon..].starts_with("::") {
return None;
}
let name = part[..colon].trim();
let ty = part[colon + 1..].trim();
(is_identifier(name) && !ty.is_empty()).then_some((name, ty))
}
pub(crate) fn is_identifier(value: &str) -> bool {
let mut chars = value.chars();
chars
.next()
.is_some_and(|ch| ch == '_' || ch.is_ascii_alphabetic())
&& chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric())
}