use indexmap::{IndexMap, IndexSet};
use crate::ir::{diag_name, Element, EmitError, Kind, NodeKind};
use crate::leftrec::find_leading_ref_cycle_members;
use crate::shared::{origin_of, Grammar, Production};
#[derive(Debug, Default)]
pub(crate) struct ValuePlan {
pub plan: IndexMap<String, Vec<bool>>,
pub collect: IndexMap<String, Vec<bool>>,
}
const SRC_FIELD: &str = "src";
pub(crate) fn plan_value_annotations(grammar: &Grammar) -> Result<ValuePlan, EmitError> {
let by_name: IndexMap<&str, &Production> = grammar
.productions
.iter()
.map(|p| (p.name.as_str(), p))
.collect();
let mut out = ValuePlan::default();
if !grammar.productions.iter().any(|p| p.value.is_some()) {
return Ok(out);
}
let cyclic = find_leading_ref_cycle_members(&grammar.productions);
for prod in &grammar.productions {
if exempt_alias(prod, &cyclic) {
continue;
}
for alt in &prod.alts {
let Some(first) = alt.first() else { continue };
let Some(first_name) = first.ref_name() else {
continue;
};
let (_, annotated) = resolve_leading_fold(first_name, &by_name);
let Some(hit) = annotated else { continue };
return Err(EmitError::at(
format!(
"{}: rule '{}' begins with '{}', and '{}' is folded into '{}' by \
left-recursion elimination — which erases the value '{}' is \
annotated to build, so nothing would produce it. Put a literal \
before '{}', or remove the annotation on '{}'.",
diag_name(),
prod.name,
first_name,
hit,
prod.name,
hit,
first_name,
hit
),
&prod.name,
prod.sp,
));
}
}
for prod in &grammar.productions {
let Some(v) = &prod.value else { continue };
let at = |message: String| EmitError::at(message, &prod.name, prod.sp);
if v.kind != "object" && v.kind != "array" {
return Err(at(format!(
"{}: rule '{}' has a value annotation of unknown kind '{}'. A rule \
builds an 'object' or an 'array'.",
diag_name(),
prod.name,
v.kind
)));
}
if prod.alts.len() != 1 {
return Err(at(format!(
"{}: rule '{}' has a value annotation and {} alternatives. A value \
annotation names the parts of ONE alternative; with more than one it \
is ambiguous which alternative's parts are named. Split the rule, or \
annotate the alternatives' own rules.",
diag_name(),
prod.name,
prod.alts.len()
)));
}
let mut seen: IndexSet<&str> = IndexSet::new();
for m in &v.members {
if m.is_empty() {
return Err(at(format!(
"{}: rule '{}' has a value annotation naming a member that is not a \
name (\"\"). Every member of an object is named by a non-empty \
string.",
diag_name(),
prod.name
)));
}
if seen.contains(m.as_str()) {
return Err(at(format!(
"{}: rule '{}' names the member '{}' twice. Each member is a \
separate key, so the second part would overwrite the first. Give \
them different names.",
diag_name(),
prod.name,
m
)));
}
seen.insert(m);
if m == SRC_FIELD {
return Err(at(format!(
"{}: rule '{}' names a member '{}'. That name is how a value is \
told apart from a parse-tree node, so a value carrying it is \
mistaken for a node and dropped wherever an ordinary rule captures \
this one. Name the member something else.",
diag_name(),
prod.name,
SRC_FIELD
)));
}
}
let alt = &prod.alts[0];
if alt.len() == 1 {
if let Kind::Prose { text } = &alt[0].kind {
return Err(at(format!(
"{}: rule '{}' has a value annotation, but its body is prose \
('<{}>'), which describes a built-in token rather than defining a \
rule — the rule is dropped, so nothing would build the value. \
Remove the annotation.",
diag_name(),
prod.name,
text
)));
}
}
let parts: Vec<&Element> = alt
.iter()
.map(|el| &**el)
.filter(|el| pushes_value(el))
.collect();
if v.kind == "array" {
if !v.members.is_empty() {
return Err(at(format!(
"{}: rule '{}' builds an array and names {} member{}. An array's \
parts are positional and are not named; annotate it as an object \
to name them.",
diag_name(),
prod.name,
v.members.len(),
if v.members.len() == 1 { "" } else { "s" }
)));
}
} else {
let named = v.members.len();
if named != parts.len() {
return Err(at(format!(
"{}: rule '{}' names {} member{} but has {} part{} a value. A value \
annotation names one member per part that produces a value; a \
literal produces no value and is not a member.",
diag_name(),
prod.name,
named,
if named == 1 { "" } else { "s" },
parts.len(),
if parts.len() == 1 {
" that produces"
} else {
"s that produce"
}
)));
}
}
if let Some(first) = alt.first() {
if let Some(first_name) = first.ref_name() {
if !exempt_alias(prod, &cyclic) {
let (ok, _) = resolve_leading_fold(first_name, &by_name);
if !ok {
return Err(at(format!(
"{}: rule '{}' names '{}' as its first member, but '{}' is \
folded into this rule by left-recursion elimination and its \
body is not a single part, so the member would not cover \
what the author wrote. Give '{}' a body that is one part (a \
reference, a repetition or a group), or put a literal before \
it.",
diag_name(),
prod.name,
first_name,
first_name,
first_name
)));
}
}
}
}
let flags: Vec<bool> = parts
.iter()
.map(|el| {
el.ref_name()
.and_then(|n| by_name.get(n))
.is_some_and(|p| p.value.is_some())
})
.collect();
for (i, el) in parts.iter().enumerate() {
if flags[i] {
continue;
}
let mut hit = reaches_annotated(el, &by_name, &mut IndexSet::new());
let Some(mut hit_name) = hit.take() else {
continue;
};
if v.kind == "array" && collects_values(el) && hit_name != prod.name {
match hidden_annotated(el, &by_name) {
None => continue,
Some(h) => hit_name = h,
}
}
let which = if v.kind == "array" {
format!("element {}", i + 1)
} else {
format!("member '{}'", v.members[i])
};
if hit_name == prod.name {
return Err(at(format!(
"{}: rule '{}' takes {} as source text, but that part reaches '{}' \
itself, which builds a value — a rule that builds a value \
contributes no text to the part above it, so a recursive rule \
cannot take its own repetition as text. Annotate the rule the \
recursion pushes instead.",
diag_name(),
prod.name,
which,
prod.name
)));
}
return Err(at(format!(
"{}: rule '{}' takes {} as source text, but '{}' is reached from it and \
builds a value of its own — a rule that builds a value contributes no \
text to the part above it, so the member would be missing '{}'s match, \
or empty. Make that part '{}' itself so it nests, or remove the \
annotation on '{}'.",
diag_name(),
prod.name,
which,
hit_name,
hit_name,
hit_name,
hit_name
)));
}
out.plan.insert(prod.name.clone(), flags);
if v.kind == "array" {
out.collect.insert(
prod.name.clone(),
parts.iter().map(|el| collects_values(el)).collect(),
);
}
}
Ok(out)
}
fn hidden_annotated(el: &Element, by_name: &IndexMap<&str, &Production>) -> Option<String> {
match &el.kind {
Kind::Ref { name, .. } => {
let target = by_name.get(name.as_str())?;
if target.value.is_some() {
return None;
}
reaches_annotated(el, by_name, &mut IndexSet::new())
}
Kind::Opt { inner }
| Kind::Star { inner, .. }
| Kind::Plus { inner }
| Kind::Rep { inner, .. } => hidden_annotated(inner, by_name),
Kind::Group { alts } => alts
.iter()
.flatten()
.find_map(|inner| hidden_annotated(inner, by_name)),
_ => None,
}
}
fn reaches_annotated(
el: &Element,
by_name: &IndexMap<&str, &Production>,
seen: &mut IndexSet<String>,
) -> Option<String> {
let mut stack: Vec<&Element> = vec![el];
while let Some(el) = stack.pop() {
match &el.kind {
Kind::Ref { name, .. } => {
let Some(target) = by_name.get(name.as_str()) else {
continue;
};
if target.value.is_some() {
return Some(target.name.clone());
}
if !seen.insert(target.name.clone()) {
continue;
}
stack.extend(target.alts.iter().flatten().rev().map(|inner| &**inner));
}
Kind::Opt { inner }
| Kind::Star { inner, .. }
| Kind::Plus { inner }
| Kind::Rep { inner, .. } => stack.push(inner),
Kind::Group { alts } => stack.extend(alts.iter().flatten().rev()),
_ => {}
}
}
None
}
pub(crate) fn exempt_alias(p: &Production, cyclic: &IndexSet<String>) -> bool {
if p.alts.len() != 1 || p.alts[0].len() != 1 {
return false;
}
match p.alts[0][0].ref_name() {
Some(name) => !cyclic.contains(&p.name) && !cyclic.contains(name),
None => false,
}
}
pub(crate) fn pushes_value(el: &Element) -> bool {
!matches!(
el.kind,
Kind::Term { .. } | Kind::Regex { .. } | Kind::Token { .. } | Kind::Prose { .. }
)
}
fn collects_values(el: &Element) -> bool {
matches!(
el.kind,
Kind::Star { .. } | Kind::Plus { .. } | Kind::Opt { .. } | Kind::Rep { .. }
)
}
fn resolve_leading_fold(
name: &str,
by_name: &IndexMap<&str, &Production>,
) -> (bool, Option<String>) {
let mut seen: IndexSet<String> = IndexSet::new();
let mut prod = by_name.get(name).copied();
while let Some(p) = prod {
if seen.contains(&p.name) {
return (false, None);
}
seen.insert(p.name.clone());
if p.value.is_some() {
return (false, Some(p.name.clone()));
}
if p.alts.len() != 1 || p.alts[0].len() != 1 {
return (false, None);
}
let el = &p.alts[0][0];
if !pushes_value(el) {
return (false, None);
}
let Some(next) = el.ref_name() else {
return (true, None);
};
prod = by_name.get(next).copied();
}
(true, None)
}
pub(crate) fn plan_array_helpers(
grammar: &Grammar,
collect: &IndexMap<String, Vec<bool>>,
) -> IndexSet<String> {
let by_name: IndexMap<&str, &Production> = grammar
.productions
.iter()
.map(|p| (p.name.as_str(), p))
.collect();
let mut helpers: IndexSet<String> = IndexSet::new();
fn walk(name: &str, by_name: &IndexMap<&str, &Production>, helpers: &mut IndexSet<String>) {
let Some(prod) = by_name.get(name) else {
return;
};
if prod.node_kind != NodeKind::Helper || helpers.contains(name) {
return;
}
helpers.insert(name.to_string());
for alt in &prod.alts {
for el in alt {
if let Some(r) = el.ref_name() {
walk(r, by_name, helpers);
}
}
}
}
fn yields_elements(
name: &str,
by_name: &IndexMap<&str, &Production>,
seen: &mut IndexSet<String>,
) -> bool {
let Some(prod) = by_name.get(name) else {
return false;
};
if seen.contains(name) {
return false;
}
seen.insert(name.to_string());
for alt in &prod.alts {
for el in alt {
let Some(r) = el.ref_name() else { continue };
match by_name.get(r) {
Some(target) if target.node_kind == NodeKind::Helper => {
if yields_elements(r, by_name, seen) {
return true;
}
}
_ => return true,
}
}
}
false
}
for prod in &grammar.productions {
if prod.value.as_ref().map(|v| v.kind.as_str()) != Some("array") {
continue;
}
let Some(sugar) = collect.get(origin_of(prod)) else {
continue;
};
for alt in &prod.alts {
let mut k = 0;
for el in alt {
let Some(r) = el.ref_name() else { continue };
if sugar.get(k).copied().unwrap_or(false)
&& yields_elements(r, &by_name, &mut IndexSet::new())
{
walk(r, &by_name, &mut helpers);
}
k += 1;
}
}
}
helpers
}