diff options
Diffstat (limited to 'audio/src/parse')
| -rw-r--r-- | audio/src/parse/lex.rs | 218 | ||||
| -rw-r--r-- | audio/src/parse/lexer.rs | 264 | ||||
| -rw-r--r-- | audio/src/parse/macros.rs | 214 | ||||
| -rw-r--r-- | audio/src/parse/mod.rs | 32 | ||||
| -rw-r--r-- | audio/src/parse/parser.rs | 232 | ||||
| -rw-r--r-- | audio/src/parse/pos.rs | 83 | ||||
| -rw-r--r-- | audio/src/parse/util.rs | 42 |
7 files changed, 724 insertions, 361 deletions
diff --git a/audio/src/parse/lex.rs b/audio/src/parse/lex.rs deleted file mode 100644 index 0ef8d47..0000000 --- a/audio/src/parse/lex.rs +++ /dev/null @@ -1,218 +0,0 @@ -use std::{ - fmt::Display, - iter::Peekable, - str::{Chars, FromStr}, -}; - -use super::Result; -use crate::{channel::DutyCycle, program::ChanSpec}; - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Token { - Eof, - LineSeparator, - Pause(usize), - PauseLen(u32), - Jump(usize), - ChanSpec(ChanSpec), - SetVolume(u8), - SetPitch(u8), - SetNoiseMode(bool), - SetPulseDuty(DutyCycle), -} -impl Token { - pub const fn is_eol(self) -> bool { - matches!(self, Self::Eof | Self::LineSeparator) - } -} - -pub struct Lexer<'s> { - src: &'s str, - chars: Peekable<Chars<'s>>, - pos: usize, -} -impl<'s> Lexer<'s> { - pub fn new(src: &'s str) -> Self { - let chars = src.chars().peekable(); - Self { src, chars, pos: 0 } - } - - fn filter_char(&mut self, ch: Option<char>, advance: bool) -> Result<char> { - match ch { - Some(c) if c.is_control() && !matches!(c, '\n' | '\r' | '\t') => { - Err(invalid_char(c)) - } - Some(c) => { - if advance { - self.pos += c.len_utf8(); - } - Ok(c) - } - None => Ok('\0'), - } - } - - fn peek(&mut self) -> Result<char> { - let c = self.chars.peek().copied(); - self.filter_char(c, false) - } - - fn next(&mut self) -> Result<char> { - let c = self.chars.next(); - self.filter_char(c, true) - } - - fn next_int<T>(&mut self) -> Result<T> - where - T: FromStr, - <T as FromStr>::Err: Display, - { - let start = self.pos; - while self.peek()?.is_ascii_digit() { - self.next()?; - } - let str = &self.src[start..self.pos]; - str.parse::<T>().map_err(|e| e.to_string()) - } - - fn next_note(&mut self) -> Result<u8> { - if self.peek()?.is_ascii_digit() { - return self.next_int(); - } - - let note = match self.next()? { - 'a' => 80, - 'b' => 82, - 'c' => 83, - 'd' => 85, - 'e' => 87, - 'f' => 88, - 'g' => 90, - c => unexpected(c)?, - }; - - let octave = { - let c = self.next()?; - c.to_digit(10).ok_or_else(|| format!("invalid octave: {c}")) - }?; - - let off = match self.peek()? { - '#' => { - self.next()?; - 1 - } - 'b' => { - self.next()?; - -1 - } - _ => 0, - }; - - let pitch_u32 = (note + octave * 12).wrapping_add_signed(off); - let pitch = u8::try_from(pitch_u32).map_err(|e| format!("{e}"))?; - - Ok(pitch) - } - - fn next_bool(&mut self) -> Result<bool> { - match self.next_int()? { - 0 => Ok(false), - _ => Ok(true), - } - } - - fn next_duty_cycle(&mut self) -> Result<DutyCycle> { - match self.next_int()? { - 12 => Ok(DutyCycle::Percent12), - 25 => Ok(DutyCycle::Percent25), - 50 => Ok(DutyCycle::Percent50), - 75 => Ok(DutyCycle::Percent25Neg), - n => Err(format!("invalid duty cycle: {n}")), - } - } - - fn skip_comment(&mut self) -> Result<()> { - loop { - let next = self.next()?; - if next == '\n' || next == '\0' { - break; - } - } - Ok(()) - } - - fn next_pause(&mut self) -> Result<Token> { - let mut count = 1; - if self.peek()?.is_ascii_digit() { - count = self.next_int()?; - } - Ok(Token::Pause(count)) - } - - pub fn next_token(&mut self) -> Result<Token> { - use Token as T; - loop { - let peek = self.peek()?; - if peek == ';' { - self.skip_comment()?; - } else if matches!(peek, ' ' | '\t' | '\r') { - self.next()?; - } else { - break; - } - } - let token = match self.next()? { - // chan spec - 'a' => Token::ChanSpec(ChanSpec::PulseA), - 'b' => Token::ChanSpec(ChanSpec::PulseB), - 't' => Token::ChanSpec(ChanSpec::Triangle), - 'n' => Token::ChanSpec(ChanSpec::Noise), - // volume - 'v' => T::SetVolume(self.next_int()?), - // pitch - 'p' => T::SetPitch(self.next_note()?), - // duty cycle - 'd' => T::SetPulseDuty(self.next_duty_cycle()?), - // noise mode - 'm' => T::SetNoiseMode(self.next_bool()?), - // pause - '-' => self.next_pause()?, - // jump - 'j' => T::Jump(self.next_int()?), - // pause len - 'P' => T::PauseLen(self.next_int()?), - // eof - '\0' => T::Eof, - // new line - '\n' => T::LineSeparator, - // unexpected - c => unexpected(c)?, - }; - Ok(token) - } -} -impl Iterator for Lexer<'_> { - type Item = Result<Token>; - - fn next(&mut self) -> Option<Self::Item> { - Some(self.next_token()) - } -} - -fn invalid_char(ch: char) -> String { - match ch as u32 { - c @ 0x00..=0x7f => format!("invalid character (codepoint 0x{c:2x})"), - c => format!("invalid character (codepoint U+{c:04x})"), - } -} - -fn unexpected<T>(c: char) -> Result<T> { - let msg = match c { - '\0' => "unexpected end of file".to_owned(), - '\n' => "unexpected newline character".to_owned(), - '\t' => "unexpected tab character".to_owned(), - '\r' => "unexpected return character".to_owned(), - c => format!("unexpected character {c}"), - }; - Err(msg) -} diff --git a/audio/src/parse/lexer.rs b/audio/src/parse/lexer.rs new file mode 100644 index 0000000..59bd264 --- /dev/null +++ b/audio/src/parse/lexer.rs @@ -0,0 +1,264 @@ +use std::{fmt, iter::Peekable, str::Chars}; + +use super::{ + ParserError, Result, + pos::{Pos, Span}, +}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum TokenKind { + Eof, + LineSeparator, + + MacroDefine, + MacroEnd, + Argument, + + PulseA, + PulseB, + Triangle, + Noise, + + Volume, + Pitch, + DutyCycle, + Mode, + PauseLen, + + Identifier, + Integer, + Dash, +} +impl TokenKind { + pub const fn name(self) -> &'static str { + match self { + Self::Eof => "end of file", + Self::LineSeparator => "line seperator", + + Self::MacroDefine => "%define", + Self::MacroEnd => "%end", + Self::Argument => "$", + + Self::PulseA => "pulsea", + Self::PulseB => "pulseb", + Self::Triangle => "triangle", + Self::Noise => "noise", + + Self::Volume => "volume", + Self::Pitch => "pitch", + Self::DutyCycle => "duty cycle", + Self::Mode => "mode", + Self::PauseLen => "pause len", + + Self::Identifier => "identifier", + Self::Integer => "integer", + Self::Dash => "dash", + } + } +} +impl fmt::Display for TokenKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.name()) + } +} +use TokenKind as K; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Token<'s> { + pub span: Span, + pub content: &'s str, + pub kind: TokenKind, +} + +pub struct Lexer<'s> { + src: &'s str, + chars: Peekable<Chars<'s>>, + start_pos: Pos, + pos: Pos, +} +impl<'s> Lexer<'s> { + pub fn new(src: &'s str) -> Self { + Self::new_at(src, 1) + } + + pub fn new_at(src: &'s str, line: u32) -> Self { + let pos = Pos { + line, + col: 1, + idx: 0, + }; + Self { + src, + chars: src.chars().peekable(), + start_pos: pos, + pos, + } + } + + fn invalid_char(&self, ch: char) -> ParserError { + let span = Span::new(self.pos, self.pos.advance(ch)); + let msg = match ch as u32 { + c @ 0x00..=0x7f => format!("invalid character (codepoint 0x{c:2x})"), + c => format!("invalid character (codepoint U+{c:04x})"), + }; + ParserError { + span, + msg, + file: None, + } + } + + fn filter_char(&mut self, ch: Option<char>, advance: bool) -> Result<char> { + match ch { + Some(c) if c.is_control() && !matches!(c, '\n' | '\r' | '\t') => { + Err(self.invalid_char(c)) + } + Some(c) => { + if advance { + self.pos = self.pos.advance(c); + } + Ok(c) + } + None => Ok('\0'), + } + } + + fn peek(&mut self) -> Result<char> { + let c = self.chars.peek().copied(); + self.filter_char(c, false) + } + + fn next(&mut self) -> Result<char> { + let c = self.chars.next(); + self.filter_char(c, true) + } + + fn emit(&self, kind: TokenKind) -> Result<Token<'s>> { + let span = Span::new(self.start_pos, self.pos); + Ok(Token { + span, + content: span.of(self.src), + kind, + }) + } + + fn and_emit(&mut self, kind: TokenKind) -> Result<Token<'s>> { + self.next()?; + self.emit(kind) + } + + fn unexpected(&mut self) -> Result<Token<'s>> { + let c = self.peek()?; + let span = Span::new(self.pos, self.pos.advance(c)); + let msg = match c { + '\0' => "unexpected end of file".to_owned(), + '\n' => "unexpected newline character".to_owned(), + '\t' => "unexpected tab character".to_owned(), + '\r' => "unexpected return character".to_owned(), + c => format!("unexpected character {c}"), + }; + Err(ParserError { + span, + msg, + file: None, + }) + } + + fn err<T>(&self, msg: &str) -> Result<T> { + Err(ParserError { + span: Span::new(self.start_pos, self.pos), + msg: msg.to_owned(), + file: None, + }) + } + + fn next_ident(&mut self) -> Result<Token<'s>> { + let first = self.next()?; + loop { + let c = self.peek()?; + let cond = if ('a'..='g').contains(&first) { + c.is_ascii_alphanumeric() || c == '#' + } else { + c.is_ascii_alphabetic() + }; + if !cond { + break; + } + self.next()?; + } + let kind = match Span::new(self.start_pos, self.pos).of(self.src) { + "pulsea" | "a" => K::PulseA, + "pulseb" | "b" => K::PulseB, + "triangle" | "t" => K::Triangle, + "noise" | "n" => K::Noise, + "volume" | "v" => K::Volume, + "pitch" | "p" => K::Pitch, + "dutycycle" | "dc" | "w" => K::DutyCycle, + "mode" | "m" => K::Mode, + "puselen" | "P" => K::PauseLen, + _ => K::Identifier, + }; + self.emit(kind) + } + + fn next_macro_ident(&mut self) -> Result<Token<'s>> { + self.next()?; + let ident = self.next_ident()?; + let kind = match ident.content { + "%macro" => K::MacroDefine, + "%endmacro" => K::MacroEnd, + _ => self.err("expected %macro or %endmacro")?, + }; + self.start_pos = ident.span.start; + self.emit(kind) + } + + fn next_int(&mut self) -> Result<Token<'s>> { + loop { + let c = self.peek()?; + if c.is_ascii_digit() { + self.next()?; + } else { + return self.emit(K::Integer); + } + } + } + + fn next_comment(&mut self) -> Result<Token<'s>> { + while !matches!(self.peek()?, '\0' | '\n') { + self.next()?; + } + self.next_token() + } + + pub fn next_token(&mut self) -> Result<Token<'s>> { + while matches!(self.peek()?, ' ' | '\t' | '\r') { + self.next()?; + } + self.start_pos = self.pos; + match self.peek()? { + // misc + '\0' => self.emit(K::Eof), + '\n' => self.and_emit(K::LineSeparator), + ';' => self.next_comment(), + // macros + '%' => self.next_macro_ident(), + '$' => self.and_emit(K::Argument), + // pause + '-' => self.and_emit(K::Dash), + // integer + c if c.is_ascii_digit() => self.next_int(), + // ident + c if c.is_ascii_alphabetic() => self.next_ident(), + // rest + _ => self.unexpected(), + } + } +} +impl<'s> Iterator for Lexer<'s> { + type Item = Result<Token<'s>>; + + fn next(&mut self) -> Option<Self::Item> { + Some(self.next_token()) + } +} diff --git a/audio/src/parse/macros.rs b/audio/src/parse/macros.rs index d33208a..a42b6d5 100644 --- a/audio/src/parse/macros.rs +++ b/audio/src/parse/macros.rs @@ -1,105 +1,175 @@ -use std::{borrow::Cow, collections::HashMap, str::Lines}; +use std::collections::HashMap; + +use super::{ + Result, + lexer::{Token, TokenKind}, + util::{SpanParserError, TokenError}, +}; + +use TokenKind as K; + +#[derive(Clone, Debug)] +struct Macro<'s> { + contents: Vec<Token<'s>>, + args: Vec<(usize, usize)>, + num_args: usize, +} struct PreProcessor<'s> { - src: &'s str, - pos: usize, - lines: Lines<'s>, - macros: HashMap<&'s str, &'s str>, + idx: usize, + tokens: Vec<Token<'s>>, + macros: HashMap<&'s str, Macro<'s>>, } impl<'s> PreProcessor<'s> { - fn new(src: &'s str) -> Self { - let lines = src.lines(); + fn new(tokens: Vec<Token<'s>>) -> Self { let macros = HashMap::new(); Self { - src, - pos: 0, - lines, + idx: 0, + tokens, macros, } } - fn next(&mut self) -> Option<&'s str> { - self.lines.next().map(|line| { - self.pos += line.len() + 1; - line.trim() - }) + fn next(&mut self) -> Token<'s> { + if self.idx >= self.tokens.len() { + Token { + span: self.tokens[self.idx - 1].span, + content: "", + kind: K::Eof, + } + } else { + self.idx += 1; + self.tokens[self.idx - 1] + } } - fn read_macro(&mut self, full_name: &'s str) { - let name = &full_name[8..]; - let start = self.pos; - let mut end = start; - while let Some(line) = self.next() - && !matches!(line, "%end") - { - end = self.pos; + fn expect(&mut self, kind: TokenKind) -> Result<Token<'s>> { + let token = self.next(); + if token.kind == kind { + Ok(token) + } else { + token.token_err(format!("expected {kind}, got {}", token.kind)) } - let str = &self.src[start..end]; - self.macros.insert(name, str); } - fn read_macros(&mut self) -> String { - let mut buf = String::new(); - while let Some(line) = self.next() { - if line.starts_with("%define ") { - self.read_macro(line); - } else { - buf.push_str(line); - buf.push('\n'); + fn parse_int(&mut self) -> Result<usize> { + let token = self.expect(K::Integer)?; + token.content.parse().span_err(token.span) + } + + fn read_macro(&mut self) -> Result<Macro<'s>> { + let mut nest_counter = 0; + let mut contents = vec![]; + let mut args = vec![]; + let mut num_args = 0; + loop { + let token = self.next(); + match token.kind { + K::Argument => { + let num = self.parse_int()?; + args.push((num, contents.len())); + num_args = num_args.max(num); + } + K::MacroDefine => { + nest_counter += 1; + contents.push(token); + } + K::MacroEnd => { + if nest_counter > 0 { + nest_counter -= 1; + contents.push(token); + } else { + break; + } + } + K::Eof => return token.token_err("macro definition was not closed"), + _ => contents.push(token), } } - buf + + args.sort_by(|(_, l), (_, r)| l.cmp(r).reverse()); + + Ok(Macro { + contents, + args, + num_args, + }) } - fn process(&mut self) -> String { - let rest = self.read_macros(); - let mut lines = rest.lines().map(Cow::Borrowed).collect::<Vec<_>>(); + fn read_macros(&mut self) -> Result<Vec<Token<'s>>> { + let mut buffer = vec![]; + self.idx = 0; loop { - let mut count = 0; - for (name, body) in &self.macros { - count += fill_macro(&mut lines, name, body); - } - if count == 0 { + let token = self.next(); + if token.kind == K::Eof { break; } + if token.kind != K::MacroDefine { + buffer.push(token); + continue; + } + let name = self.expect(K::Identifier)?.content; + let content = self.read_macro()?; + self.macros.insert(name, content); } - lines.join("\n") + Ok(buffer) } -} -fn fill_macro(contents: &mut Vec<Cow<'_, str>>, name: &str, body: &str) -> usize { - let mut count = 0; - let mut idx = 0; - loop { - if idx >= contents.len() { - break; - } - let line = &contents[idx]; - if line.starts_with(name) - && matches!(line.chars().nth(name.len()), None | Some(' ')) - { - fill_macro_once(contents, idx, body); - count += 1; + fn pass(&mut self) -> Result<Vec<Token<'s>>> { + let mut buffer = self.read_macros()?; + let mut idx = 0; + loop { + if idx >= buffer.len() { + break; + } + + let token = buffer[idx]; + + if token.kind != K::Identifier { + idx += 1; + continue; + } + + // TODO: remove clone + let Some(mac) = self.macros.get(token.content).cloned() else { + idx += 1; + continue; + }; + + let mut args = vec![]; + for n in 1..=mac.num_args { + let arg = buffer[idx + n]; + if matches!(arg.kind, K::Eof | K::LineSeparator) { + return arg.token_err("missing macro argument"); + } + args.push(arg); + } + + let mut content = mac.contents; + for (n, idx) in mac.args { + // this works since mac.args is stored by idx descending + content.insert(idx, args[n - 1]); + } + + let len = content.len(); + buffer.splice(idx..=(idx + mac.num_args), content); + idx += len + 1; } - idx += 1; - } - count -} -fn fill_macro_once(contents: &mut Vec<Cow<'_, str>>, idx: usize, body: &str) { - let invoke_line = contents.remove(idx); - let args = invoke_line.split_whitespace().skip(1).collect::<Vec<_>>(); + Ok(buffer) + } - for line in body.lines().rev() { - let mut buf = String::from(line); - for (idx, arg) in args.iter().enumerate() { - let key = format!("${}", idx + 1); - buf = buf.replace(&key, arg); + fn process(&mut self) -> Result<Vec<Token<'s>>> { + loop { + let result = self.pass()?; + if self.tokens == result { + return Ok(result); + } + self.tokens = result; } - contents.insert(idx, Cow::Owned(buf)); } } -pub fn process(src: &str) -> String { - PreProcessor::new(src).process() +pub fn process(tokens: Vec<Token<'_>>) -> Result<Vec<Token<'_>>> { + PreProcessor::new(tokens).process() } diff --git a/audio/src/parse/mod.rs b/audio/src/parse/mod.rs index 726aaa2..895ddbd 100644 --- a/audio/src/parse/mod.rs +++ b/audio/src/parse/mod.rs @@ -1,13 +1,33 @@ use crate::program::Instruction; +use lexer::{Lexer, TokenKind}; use parser::Parser; +use pos::Span; -pub type Result<T> = std::result::Result<T, String>; - -mod lex; +mod lexer; mod macros; mod parser; +mod pos; +mod util; + +pub type Result<T> = std::result::Result<T, ParserError>; + +#[derive(Clone, Debug)] +pub struct ParserError { + pub span: Span, + pub msg: String, + pub file: Option<String>, +} -pub fn parse(raw_src: &str) -> Result<Vec<Instruction>> { - let src = macros::process(raw_src); - Parser::new(&src).parse() +pub fn parse(src: &str) -> Result<Vec<Instruction>> { + let mut tokens = vec![]; + let mut lexer = Lexer::new(src); + loop { + let token = lexer.next_token()?; + tokens.push(token); + if token.kind == TokenKind::Eof { + break; + } + } + tokens = macros::process(tokens)?; + Parser::new(tokens).parse() } diff --git a/audio/src/parse/parser.rs b/audio/src/parse/parser.rs index b46e707..989a6f1 100644 --- a/audio/src/parse/parser.rs +++ b/audio/src/parse/parser.rs @@ -1,104 +1,206 @@ -use std::iter::Peekable; - -use crate::program::{ChanSpec, Instruction}; +use std::{iter::Peekable, str::FromStr, vec::IntoIter}; use super::{ Result, - lex::{Lexer, Token}, + lexer::{Token, TokenKind}, + util::{SpanParserError, TokenError}, }; +use crate::program::{ChanSpec, Instruction}; +use TokenKind as K; pub struct Parser<'s> { - lexer: Peekable<Lexer<'s>>, + tokens: Peekable<IntoIter<Token<'s>>>, + spec: Option<ChanSpec>, + eof: Token<'s>, } impl<'s> Parser<'s> { - pub fn new(src: &'s str) -> Self { + pub fn new(tokens: Vec<Token<'s>>) -> Self { + let eof = Token { + span: tokens[tokens.len() - 1].span, + content: "", + kind: K::Eof, + }; Self { - lexer: Lexer::new(src).peekable(), + tokens: tokens.into_iter().peekable(), + spec: None, + eof, } } - fn next(&mut self) -> Result<Token> { - self.lexer - .next() - .unwrap_or_else(|| Err("should not happen".to_owned())) + fn next(&mut self) -> Token<'s> { + self.tokens.next().unwrap_or(self.eof) } - fn peek(&mut self) -> Result<Token> { - self.lexer - .peek() - .map_or_else(|| Err("should not happen".to_owned()), Result::clone) + fn peek(&mut self) -> Token<'s> { + self.tokens.peek().cloned().unwrap_or(self.eof) } - fn parse_chan_spec(&mut self) -> Result<ChanSpec> { - match self.next()? { - Token::ChanSpec(spec) => Ok(spec), - t => Err(format!("expected channel specifier, got {t:?}")), + fn expect(&mut self, kind: TokenKind) -> Result<Token<'s>> { + let token = self.next(); + if token.kind == kind { + Ok(token) + } else { + token.token_err(format!("expected {kind}, got {}", token.kind)) } } - fn parse_ins(&mut self, spec: ChanSpec) -> Result<Instruction> { - use Token as T; - let t = self.next()?; - let ins = match t { - T::SetPitch(pitch) => Instruction::SetPitch(spec, pitch), - T::SetVolume(volume) => Instruction::SetVolume(spec, volume), - T::SetNoiseMode(mode) if spec == ChanSpec::Noise => { - Instruction::SetNoiseMode(mode) + fn parse_int<T>(&mut self) -> Result<T> + where + T: FromStr, + <T as FromStr>::Err: std::error::Error, + { + let token = self.expect(K::Integer)?; + token.content.parse().span_err(token.span) + } + + fn parse_note(&mut self) -> Result<u8> { + if self.peek().kind == K::Integer { + return self.parse_int(); + } + + let ident = self.expect(K::Identifier)?; + let str = ident.content; + let str_len = str.len(); + let last = str.chars().last().unwrap_or_default(); + + let note_str = &str[..1]; + let note = match note_str { + "a" => 80, + "b" => 82, + "c" => 83, + "d" => 85, + "e" => 87, + "f" => 88, + "g" => 90, + _ => return ident.token_err("invalid note"), + }; + + let octave_str = if last.is_ascii_digit() { + &str[1..str_len] + } else { + &str[1..str_len - 1] + }; + let octave = if !octave_str.is_empty() { + octave_str.parse().span_err(ident.span)? + } else { + 4 + }; + + let off = match last { + 'b' | 'f' => -1, + '#' | 's' => 1, + _ => 0, + }; + + let pitch_u32 = (note + octave * 12) + off; + let pitch = u8::try_from(pitch_u32).span_err(ident.span)?; + + Ok(pitch) + } + + fn parse_bool(&mut self) -> Result<bool> { + let n = self.parse_int::<u8>()?; + Ok(n > 0) + } + + fn parse_chan_ins(&mut self) -> Result<Instruction> { + let token = self.next(); + let Some(spec) = self.spec else { + return token.token_err("missing channel specifier"); + }; + let ins = match token.kind { + K::Volume => { + let volume = self.parse_int()?; + Instruction::SetVolume(spec, volume) + } + K::Pitch => { + let pitch = self.parse_note()?; + Instruction::SetPitch(spec, pitch) + } + K::DutyCycle if spec == ChanSpec::PulseA => { + let cycle = self.parse_int()?; + Instruction::SetPulseDutyA(cycle) } - T::SetPulseDuty(duty_cycle) if spec == ChanSpec::PulseA => { - Instruction::SetPulseDutyA(duty_cycle) + K::DutyCycle if spec == ChanSpec::PulseB => { + let cycle = self.parse_int()?; + Instruction::SetPulseDutyB(cycle) } - T::SetPulseDuty(duty_cycle) if spec == ChanSpec::PulseB => { - Instruction::SetPulseDutyB(duty_cycle) + K::DutyCycle => { + return token.token_err("cannot set duty cycle for this channel"); } - _ => unexpected(t)?, + K::Mode if spec == ChanSpec::Noise => { + let mode = self.parse_bool()?; + Instruction::SetNoiseMode(mode) + } + K::Mode => return token.token_err("cannot set mode for this channel"), + _ => return token.token_err("invalid channel argument"), }; Ok(ins) } - fn parse_line(&mut self, prog: &mut Vec<Instruction>) -> Result<()> { - let spec = self.parse_chan_spec()?; - loop { - prog.push(self.parse_ins(spec)?); - let peek = self.peek()?; - if peek.is_eol() || matches!(peek, Token::Pause(_)) { - break; + fn parse_pause_len(&mut self) -> Result<Instruction> { + self.next(); + let num = self.parse_int()?; + Ok(Instruction::PauseLen(num)) + } + + fn parse_pause(&mut self) -> Result<Instruction> { + self.next(); + let next = self.peek(); + let ins = if next.kind == K::Integer { + let num = self.parse_int()?; + Instruction::Pause(num) + } else { + Instruction::Pause(1) + }; + Ok(ins) + } + + fn parse_ins(&mut self) -> Result<Instruction> { + let token = self.peek(); + let spec = self.spec.take(); + match token.kind { + K::PulseA => { + self.spec = Some(ChanSpec::PulseA); + self.next(); + self.parse_chan_ins() + } + K::PulseB => { + self.spec = Some(ChanSpec::PulseB); + self.next(); + self.parse_chan_ins() + } + K::Triangle => { + self.spec = Some(ChanSpec::Triangle); + self.next(); + self.parse_chan_ins() + } + K::Noise => { + self.spec = Some(ChanSpec::Noise); + self.next(); + self.parse_chan_ins() + } + K::PauseLen => self.parse_pause_len(), + K::Dash => self.parse_pause(), + _ => { + self.spec = spec; + self.parse_chan_ins() } } - Ok(()) } pub fn parse(&mut self) -> Result<Vec<Instruction>> { let mut prog = vec![]; loop { - let t = self.peek()?; - match t { - Token::Eof => break, - Token::LineSeparator => { - self.next()?; + let token = self.peek(); + match token.kind { + K::Eof => break, + K::LineSeparator => { + self.next(); } - Token::Pause(count) => { - self.next()?; - for _ in 0..count { - prog.push(Instruction::Pause); - } - } - Token::Jump(pc) => { - self.next()?; - prog.push(Instruction::Jump(pc)); - } - Token::PauseLen(pause_len) => { - self.next()?; - prog.push(Instruction::PauseLen(pause_len)); - } - _ => self.parse_line(&mut prog)?, + _ => prog.push(self.parse_ins()?), } } Ok(prog) } } - -fn unexpected<T>(t: Token) -> Result<T> { - let msg = format!("unexpected token: {t:?}"); - Err(msg) -} diff --git a/audio/src/parse/pos.rs b/audio/src/parse/pos.rs new file mode 100644 index 0000000..f56a96b --- /dev/null +++ b/audio/src/parse/pos.rs @@ -0,0 +1,83 @@ +use std::fmt; + +#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)] +pub struct Pos { + pub idx: u32, + pub line: u32, + pub col: u32, +} + +impl std::cmp::PartialOrd for Pos { + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { + Some(self.cmp(other)) + } +} + +impl std::cmp::Ord for Pos { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.idx.cmp(&other.idx) + } +} + +impl Pos { + pub const fn new() -> Self { + Self { + idx: 0, + line: 1, + col: 1, + } + } + + #[must_use] + pub fn advance(self, c: char) -> Self { + let idx = self.idx + u32::try_from(c.len_utf8()).unwrap_or_default(); + if c == '\n' { + Self { + idx, + line: self.line + 1, + col: 1, + } + } else { + Self { + idx, + line: self.line, + col: self.col + 1, + } + } + } +} + +impl fmt::Display for Pos { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}:{}", self.line, self.col) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Span { + pub start: Pos, + pub end: Pos, +} + +impl Span { + pub const fn new(start: Pos, end: Pos) -> Self { + if end.idx < start.idx { + Self { + start: end, + end: start, + } + } else { + Self { start, end } + } + } + + pub fn of<'a>(&self, s: &'a str) -> &'a str { + &s[(self.start.idx as usize)..(self.end.idx as usize)] + } +} + +impl fmt::Display for Span { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}-{}", self.start, self.end) + } +} diff --git a/audio/src/parse/util.rs b/audio/src/parse/util.rs new file mode 100644 index 0000000..47779c2 --- /dev/null +++ b/audio/src/parse/util.rs @@ -0,0 +1,42 @@ +use super::{ParserError, lexer::Token, pos::Span}; +use std::fmt; + +impl std::error::Error for ParserError {} + +impl fmt::Display for ParserError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(file) = &self.file { + write!(f, "{file} | ")?; + } + write!(f, "{} | {}", self.span, self.msg) + } +} + +pub trait SpanParserError { + type Output; + fn span_err(self, span: Span) -> Self::Output; +} + +impl<T, E: std::error::Error> SpanParserError for Result<T, E> { + type Output = Result<T, ParserError>; + fn span_err(self, span: Span) -> Self::Output { + self.map_err(|e| ParserError { + span, + msg: e.to_string(), + file: None, + }) + } +} + +pub trait TokenError { + fn token_err<T>(self, msg: impl Into<String>) -> Result<T, ParserError>; +} +impl TokenError for Token<'_> { + fn token_err<T>(self, msg: impl Into<String>) -> Result<T, ParserError> { + Err(ParserError { + span: self.span, + msg: msg.into(), + file: None, + }) + } +} |