1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
use std::{
fmt::Display,
iter::Peekable,
str::{Chars, FromStr},
};
use super::Result;
use crate::audio::{channel::DutyCycle, program::ChanSpec};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Token {
Eof,
LineSeparator,
Pause(usize),
Jump(usize),
Tempo(u32),
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()?),
// tempO
'o' => T::Tempo(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)
}
|