summaryrefslogtreecommitdiff
path: root/audio/src/parse/lexer.rs
blob: 59bd264bb9dfad08686af18da9654bd363724443 (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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())
	}
}