use super::*;
pub(super) fn include_text(
name: String,
text: &str,
dialect: &Dialect,
inc: &mut Includes<'_>,
defs: &mut Defs,
diags: &mut Vec<Diagnostic>,
) {
inc.sources.push(name.clone());
let id = inc.sources.len().min(u16::MAX as usize) as u16;
inc.stack.push(name);
let (sub, mut d) = parse_impl(text, dialect, Mode::Included, inc, std::mem::take(defs));
inc.stack.pop();
let set = |sp: &mut Span| {
if sp.source == 0 {
sp.source = id;
}
};
for x in &mut d {
set(&mut x.span);
}
diags.extend(d);
let Ast {
mut macros,
mut envelopes,
mut vibratos,
mut detunes,
mut arps,
mut duties,
mut waves,
mut shots,
..
} = sub;
for m in &mut macros {
set(&mut m.span);
m.nodes.iter_mut().for_each(|n| n.set_source(id));
}
envelopes.iter_mut().for_each(|e| set(&mut e.span));
vibratos.iter_mut().for_each(|e| set(&mut e.span));
detunes.iter_mut().for_each(|e| set(&mut e.span));
arps.iter_mut().for_each(|e| set(&mut e.span));
duties.iter_mut().for_each(|e| set(&mut e.span));
waves.iter_mut().for_each(|e| set(&mut e.span));
for s in &mut shots {
set(&mut s.span);
set(&mut s.synth_span);
s.nodes.iter_mut().for_each(|n| n.set_source(id));
}
*defs = Defs {
macros,
envelopes,
vibratos,
detunes,
arps,
duties,
waves,
shots,
};
}
pub(super) fn include_directive(
chars: &[char],
head_end: usize,
head_span: Span,
dialect: &Dialect,
inc: &mut Includes<'_>,
defs: &mut Defs,
diags: &mut Vec<Diagnostic>,
) {
let rest: String = chars[head_end.min(chars.len())..].iter().collect();
let rest = rest.trim();
let path = rest
.strip_prefix('"')
.and_then(|r| r.strip_suffix('"'))
.filter(|p| !p.is_empty() && !p.contains('"'));
let Some(path) = path else {
diags.push(
Diagnostic::error(
codes::BAD_DIRECTIVE_VALUE,
head_span,
msg!("E0228-include-path"),
)
.with_suggestion("#include \"common.mml\""),
);
return;
};
let library = path.strip_prefix('@');
if inc.stack.len() >= crate::compiler::include::MAX_DEPTH {
diags.push(Diagnostic::error(
codes::INCLUDE_CYCLE,
head_span,
msg!("E0602-too-deep", max = crate::compiler::include::MAX_DEPTH),
));
return;
}
let loaded = match (library, inc.loader) {
(Some(name), loader) => match library_text(name, inc.synths) {
Ok(text) => Ok((path.to_string(), text.to_string(), Dialect::PIKOPIKO)),
Err(e) => match loader.and_then(|l| l.load_library(name)) {
Some(r) => r.map(|l| (l.name, l.text, Dialect::PIKOPIKO)),
None => Err(e),
},
},
(None, Some(loader)) => loader
.load(path, inc.stack.last().map(String::as_str))
.map(|l| (l.name, l.text, *dialect)),
(None, None) => Err(msg!("E0601-unavailable", path = path)),
};
let (name, text, dialect) = match loaded {
Ok(l) => l,
Err(e) => {
diags.push(Diagnostic::error(codes::INCLUDE_FAILED, head_span, e));
return;
}
};
if inc.stack.contains(&name) {
diags.push(Diagnostic::error(
codes::INCLUDE_CYCLE,
head_span,
msg!("E0602-self", path = path),
));
return;
}
if inc.sources.contains(&name) {
return;
}
include_text(name, &text, &dialect, inc, defs, diags);
}
pub(super) fn sample_directive(
chars: &[char],
head_end: usize,
head_span: Span,
inc: &Includes<'_>,
samples: &mut Vec<crate::synth::SampleAsset>,
diags: &mut Vec<Diagnostic>,
) {
let rest: String = chars[head_end.min(chars.len())..].iter().collect();
let rest = rest.trim();
let bad = |diags: &mut Vec<Diagnostic>| {
diags.push(
Diagnostic::error(codes::BAD_DIRECTIVE_VALUE, head_span, msg!("E0228-sample"))
.with_suggestion("#sample 0 \"kick.wav\" 60"),
);
};
let Some((slot, after)) = rest.split_once(char::is_whitespace) else {
return bad(diags);
};
let after = after.trim();
let Some(body) = after.strip_prefix('"') else {
return bad(diags);
};
let Some((path, tail)) = body.split_once('"') else {
return bad(diags);
};
let Ok(slot) = slot.parse::<u8>() else {
return bad(diags);
};
let tail = tail.trim();
let root_key = if tail.is_empty() {
None
} else {
match tail.parse::<u8>() {
Ok(k) if k <= 127 => Some(k),
_ => return bad(diags),
}
};
if path.is_empty() {
return bad(diags);
}
if slot >= crate::synth::SAMPLE_SLOTS {
diags.push(Diagnostic::error(
codes::BAD_DIRECTIVE_VALUE,
head_span,
msg!("E0228-sample-slot", max = crate::synth::SAMPLE_SLOTS - 1),
));
return;
}
let Some(loader) = inc.loader else {
diags.push(Diagnostic::error(
codes::INCLUDE_FAILED,
head_span,
msg!("E0601-sample-unavailable", path = path),
));
return;
};
match loader.load_bytes(path) {
Ok(data) => {
samples.retain(|s| s.slot != slot);
samples.push(crate::synth::SampleAsset {
slot,
path: path.to_string(),
root_key,
data,
span: head_span,
});
}
Err(e) => diags.push(Diagnostic::error(codes::INCLUDE_FAILED, head_span, e)),
}
}
fn library_text(name: &str, synths: &[&SynthSpec]) -> Result<&'static str, Message> {
let path = format!("@{name}");
let (synth, file) = name.split_once('/').unwrap_or((name, ""));
let with_files: Vec<&SynthSpec> = synths
.iter()
.copied()
.filter(|s| !s.tone_files.is_empty())
.collect();
let Some(spec) = with_files.iter().find(|s| s.matches(synth)) else {
if with_files.is_empty() {
return Err(msg!("E0601-library-none", path = path));
}
let names: Vec<String> = with_files
.iter()
.map(|s| format!("@{}", s.library_prefix()))
.collect();
return Err(msg!(
"E0601-library-synth",
path = path,
names = names.join(", ")
));
};
match spec.tone_file(file) {
Some(f) => Ok(f.text),
None => {
let names: Vec<String> = spec
.tone_files
.iter()
.map(|f| format!("@{}/{}", spec.library_prefix(), f.name))
.collect();
Err(msg!(
"E0601-library-file",
path = path,
names = names.join(", ")
))
}
}
}