use super::*;
type ShotToks = (String, bool, String, Span, Span, Vec<Tok>);
pub(super) struct LineReader {
mode: Mode,
dialect: Dialect,
dialect_set: Option<Span>,
synth: Option<SynthDirective>,
meta: crate::compiler::song::SongMeta,
meta_set: Vec<(&'static str, Span)>,
frame: Option<(u8, Span)>,
meter: Option<((u8, u8), Span)>,
swing: Option<(crate::compiler::ast::Swing, Span)>,
samples: Vec<crate::synth::SampleAsset>,
body_started: bool,
defs: Defs,
track_toks: Vec<(char, Span, Vec<Tok>)>,
macro_toks: Vec<(String, Span, Vec<Tok>)>,
shot_toks: Vec<ShotToks>,
diags: Vec<Diagnostic>,
}
impl LineReader {
pub(super) fn new(dialect: Dialect, mode: Mode, seed: Defs) -> Self {
LineReader {
mode,
dialect,
dialect_set: None,
synth: None,
meta: crate::compiler::song::SongMeta::default(),
meta_set: Vec::new(),
frame: None,
meter: None,
swing: None,
samples: Vec::new(),
body_started: false,
defs: seed,
track_toks: Vec::new(),
macro_toks: Vec::new(),
shot_toks: Vec::new(),
diags: Vec::new(),
}
}
pub(super) fn line(&mut self, line: u32, raw: &str, inc: &mut Includes<'_>) {
let mut chars: Vec<char> = raw.chars().collect();
if let Some(p) = chars.iter().position(|&c| c == ';') {
chars.truncate(p);
}
let Some(start) = chars.iter().position(|c| !c.is_whitespace()) else {
return;
};
let first = chars[start];
if first == '#'
&& chars
.get(start + 1)
.is_some_and(|c| c.is_ascii_alphabetic())
{
self.directive_line(&chars, start, line, inc);
return;
}
if first == '$' && self.macro_line(&chars, start, line) {
return;
}
let at_command =
first == '@' && chars.get(start + 1).is_some_and(|c| c.is_ascii_lowercase());
let mut name_at = start + 1;
if at_command {
match self.at_command_line(&chars, start, line) {
Some(at) => name_at = at,
None => return,
}
}
if first == '~' || at_command {
self.envelope_line(&chars, start, name_at, line);
return;
}
if first.is_ascii_uppercase() {
self.track_line(&chars, start, line);
return;
}
let rest: String = chars[start..].iter().collect();
self.diags.push(
Diagnostic::error(
codes::NO_TRACK,
Span::new(line, start as u32 + 1, (chars.len() - start) as u32),
msg!("E0201-no-track"),
)
.with_suggestion(format!("A {}", rest)),
);
}
fn directive_line(&mut self, chars: &[char], start: usize, line: u32, inc: &mut Includes<'_>) {
let mut head_end = start + 1;
while head_end < chars.len() && !chars[head_end].is_whitespace() {
head_end += 1;
}
let head: String = chars[start + 1..head_end].iter().collect();
let head_span = Span::new(line, start as u32 + 1, (head_end - start) as u32);
if head == "include" {
if self.body_started {
self.diags.push(Diagnostic::error(
codes::DIRECTIVE_NOT_AT_TOP,
head_span,
msg!("E0217-include-position"),
));
return;
}
include_directive(
chars,
head_end,
head_span,
&self.dialect,
inc,
&mut self.defs,
&mut self.diags,
);
return;
}
if self.mode == Mode::Included {
self.diags.push(Diagnostic::error(
codes::BAD_INCLUDED_CONTENT,
head_span,
msg!("E0603-directive", head = head),
));
return;
}
if head == "sample" {
if self.body_started {
self.diags.push(Diagnostic::error(
codes::DIRECTIVE_NOT_AT_TOP,
head_span,
msg!("E0217-sample-position"),
));
return;
}
sample_directive(
chars,
head_end,
head_span,
inc,
&mut self.samples,
&mut self.diags,
);
return;
}
let mut st = DirectiveState {
body_started: self.body_started,
dialect: &mut self.dialect,
dialect_set: &mut self.dialect_set,
synth: &mut self.synth,
meta: &mut self.meta,
meta_set: &mut self.meta_set,
frame: &mut self.frame,
meter: &mut self.meter,
swing: &mut self.swing,
diags: &mut self.diags,
};
directive(chars, start, line, &mut st);
}
fn macro_line(&mut self, chars: &[char], start: usize, line: u32) -> bool {
let mut j = start + 1;
while j < chars.len() && is_name_char(chars[j]) {
j += 1;
}
let name: String = chars[start + 1..j].iter().collect();
let mut k = j;
while k < chars.len() && chars[k].is_whitespace() {
k += 1;
}
if k >= chars.len() || chars[k] != '=' {
return false;
}
let name_span = Span::new(line, start as u32 + 1, (j - start) as u32);
if name.is_empty() {
self.diags.push(Diagnostic::error(
codes::BAD_MACRO_NAME,
name_span,
msg!("E0209-macro-name-missing"),
));
return true;
}
let mut toks = Vec::new();
lex(
chars,
k + 1,
line,
&self.dialect,
&mut toks,
&mut self.diags,
);
self.macro_toks.push((name, name_span, toks));
self.body_started = true;
true
}
fn at_command_line(&mut self, chars: &[char], start: usize, line: u32) -> Option<usize> {
let mut j = start + 1;
while j < chars.len() && chars[j].is_ascii_lowercase() {
j += 1;
}
let word: String = chars[start + 1..j].iter().collect();
if word == "shot" {
self.body_started = true;
if let Some(def) = shot_head(chars, start, j, line, &mut self.diags) {
let (name, duck, synth, synth_span, span, body_at) = def;
let mut toks = Vec::new();
lex(
chars,
body_at,
line,
&self.dialect,
&mut toks,
&mut self.diags,
);
self.shot_toks
.push((name, duck, synth, synth_span, span, toks));
}
return None;
}
if !is_at_word(&word) {
self.body_started = true;
self.diags.push(Diagnostic::error(
codes::UNKNOWN_CHAR,
Span::new(line, start as u32 + 1, (j - start) as u32),
msg!("E0101-unknown-at-command", word = word),
));
return None;
}
while j < chars.len() && chars[j].is_whitespace() {
j += 1;
}
let name_at = j;
if word == "env" {
return Some(name_at);
}
self.body_started = true;
let diags = &mut self.diags;
if word == "duty" {
if let Some(def) = envelope_def(chars, start, name_at, line, diags) {
if let Some(prev) = self.defs.duties.iter().find(|e| e.name == def.name) {
diags.push(dup_effect("@duty", &def.name, def.span, prev.span.line));
} else {
self.defs.duties.push(def);
}
}
return None;
}
if word == "wave" {
if let Some(def) = wave_def(chars, start, name_at, line, diags) {
if let Some(prev) = self.defs.waves.iter().find(|e| e.name == def.name) {
diags.push(dup_effect("@wave", &def.name, def.span, prev.span.line));
} else {
self.defs.waves.push(def);
}
}
return None;
}
if word == "vib" {
if let Some(def) = vibrato_def(chars, start, name_at, line, diags) {
if let Some(prev) = self.defs.vibratos.iter().find(|e| e.name == def.name) {
diags.push(dup_effect("@vib", &def.name, def.span, prev.span.line));
} else {
self.defs.vibratos.push(def);
}
}
} else {
let kind = if word == "arp" {
StepKind::Arp
} else {
StepKind::Detune
};
let list = if kind == StepKind::Arp {
&mut self.defs.arps
} else {
&mut self.defs.detunes
};
if let Some(def) = steps_def(kind, chars, start, name_at, line, diags) {
if let Some(prev) = list.iter().find(|e| e.name == def.name) {
diags.push(dup_effect(kind.cmd(), &def.name, def.span, prev.span.line));
} else {
list.push(def);
}
}
}
None
}
fn envelope_line(&mut self, chars: &[char], start: usize, mut name_at: usize, line: u32) {
self.body_started = true;
if chars[start] == '~' {
name_at = start + 1;
let mut e = name_at;
while e < chars.len() && is_name_char(chars[e]) {
e += 1;
}
if e > name_at {
let rest: String = chars[name_at..].iter().collect();
self.diags.push(
Diagnostic::error(
codes::OLD_ENVELOPE_SYNTAX,
Span::new(line, start as u32 + 1, (e - start) as u32),
msg!("E0222-tilde-removed"),
)
.with_suggestion(format!("@env {}", rest.trim_end())),
);
}
}
if let Some(def) = envelope_def(chars, start, name_at, line, &mut self.diags) {
if let Some(prev) = self.defs.envelopes.iter().find(|e| e.name == def.name) {
self.diags.push(Diagnostic::error(
codes::DUPLICATE_ENVELOPE,
def.span,
msg!("E0408-duplicate", name = def.name, line = prev.span.line),
));
} else {
self.defs.envelopes.push(def);
}
}
}
fn track_line(&mut self, chars: &[char], start: usize, line: u32) {
let mut j = start;
while j < chars.len() && chars[j].is_ascii_uppercase() {
j += 1;
}
if j < chars.len() && !chars[j].is_whitespace() {
self.diags.push(
Diagnostic::error(
codes::TRACK_NAME_SPACE,
Span::new(line, start as u32 + 1, (j - start) as u32),
msg!("E0214-space"),
)
.with_suggestion(format!(
"{} {}",
chars[start..j].iter().collect::<String>(),
chars[j..].iter().collect::<String>()
)),
);
return;
}
self.body_started = true;
if self.mode == Mode::Included {
self.diags.push(Diagnostic::error(
codes::BAD_INCLUDED_CONTENT,
Span::new(line, start as u32 + 1, (j - start) as u32),
msg!("E0603-track"),
));
return;
}
let mut toks = Vec::new();
lex(chars, j, line, &self.dialect, &mut toks, &mut self.diags);
for (p, &name) in chars[start..j].iter().enumerate() {
if chars[start..start + p].contains(&name) {
continue;
}
let idx = match self.track_toks.iter().position(|(n, _, _)| *n == name) {
Some(i) => i,
None => {
let sp = Span::new(line, (start + p) as u32 + 1, 1);
self.track_toks.push((name, sp, Vec::new()));
self.track_toks.len() - 1
}
};
self.track_toks[idx].2.extend(toks.iter().cloned());
}
}
pub(super) fn finish(self) -> (Ast, Vec<Diagnostic>) {
let LineReader {
dialect,
synth,
meta,
frame,
meter,
swing,
samples,
defs,
track_toks,
macro_toks,
shot_toks,
mut diags,
..
} = self;
let mut ast = Ast {
dialect: dialect.name.to_string(),
synth,
envelopes: defs.envelopes,
vibratos: defs.vibratos,
detunes: defs.detunes,
arps: defs.arps,
duties: defs.duties,
waves: defs.waves,
meta,
frame: frame.map(|(f, _)| f),
meter: meter.map(|(m, _)| m),
swing: swing.map(|(s, _)| s),
macros: defs.macros,
shots: defs.shots,
samples,
..Ast::default()
};
for (name, duck, synth, synth_span, span, toks) in shot_toks {
let nodes = Parser::new(&toks, &dialect, &mut diags).parse_all();
if let Some(prev) = ast.shots.iter().find(|s| s.name == name) {
diags.push(Diagnostic::error(
codes::DUPLICATE_SHOT,
span,
msg!("E0416-duplicate", name = name, line = prev.span.line),
));
continue;
}
ast.shots.push(ShotDef {
name,
duck,
synth,
synth_span,
nodes,
span,
});
}
for (name, span, toks) in macro_toks {
let nodes = Parser::new(&toks, &dialect, &mut diags).parse_all();
if ast.macros.iter().any(|m| m.name == name) {
diags.push(Diagnostic::error(
codes::DUPLICATE_MACRO,
span,
msg!("E0403-duplicate", name = name),
));
continue;
}
ast.macros.push(MacroDef { name, nodes, span });
}
for (name, span, toks) in track_toks {
let nodes = Parser::new(&toks, &dialect, &mut diags).parse_all();
ast.tracks.push(Track {
name: name.to_string(),
nodes,
span,
});
}
diags.sort_by_key(|d| (d.span.source, d.span.line, d.span.col));
(ast, diags)
}
}