openusd-rs 0.1.0

Work in progress, opinionated implementation of OpenUSD in pure Rust
Documentation
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use std::ops::Range;

pub struct Input<'a, C> {
	pub input: &'a str,
	pub pos: usize,
	pub ctx: &'a mut C,
}

impl<'a, C> Input<'a, C> {
	pub fn new(input: &'a str, ctx: &'a mut C) -> Self {
		Input { input, pos: 0, ctx }
	}

	pub fn char_at_byte(&self, byte_pos: usize) -> Option<char> {
		self.input[byte_pos..].chars().next()
	}

	pub fn current_char(&self) -> Option<char> {
		self.char_at_byte(self.pos)
	}

	pub fn advance(&mut self) {
		if let Some(c) = self.current_char() {
			self.pos += c.len_utf8();
		}
	}

	pub fn slice(&self, range: Range<usize>) -> &'a str {
		&self.input[range.start..range.end]
	}

	pub fn get_error_context(&self) -> ErrorContext<'a> {
		ErrorContext::new(self.input, self.pos)
	}

	pub fn format_error(&self, error: &str) -> String {
		self.get_error_context().format_error_display(error)
	}

	pub fn remaining(&self) -> &'a str {
		&self.input[self.pos..]
	}
}

#[derive(Debug, Clone, Copy)]
pub struct Location {
	line: usize,
	column: usize,
}

pub struct ErrorContext<'a> {
	input: &'a str,
	position: usize,
	location: Location,
}

impl<'a> ErrorContext<'a> {
	pub fn new(input: &'a str, position: usize) -> Self {
		let location = ErrorContext::get_location(input, position);
		Self {
			input,
			position,
			location,
		}
	}

	pub fn get_location(input: &str, position: usize) -> Location {
		let position = std::cmp::min(position, input.len());
		let mut line = 1;
		let mut column = 1;

		for (_, c) in input.char_indices().take_while(|(i, _)| *i < position) {
			if c == '\n' {
				line += 1;
				column = 1;
			} else {
				column += 1;
			}
		}

		Location { line, column }
	}

	pub fn format_error_display(&self, error: &str) -> String {
		let line_start = self.input[..self.position]
			.rfind('\n')
			.map_or(0, |pos| pos + 1);

		let line_end = self.input[self.position..]
			.find('\n')
			.map_or(self.input.len(), |pos| self.position + pos);

		let line_content = &self.input[line_start..line_end];
		let caret_position = self.position - line_start;

		let mut result = format!(
			"Error at line {}, column {}: {}\n",
			self.location.line, self.location.column, error
		);

		result.push_str(&format!("{}\n", line_content));
		result.push_str(&format!("{}^\n", " ".repeat(caret_position)));

		result
	}
}

#[derive(Debug, Clone)]
pub struct Error {
	global: bool,
	message: String,
}

impl Error {
	pub fn from_msg(msg: &str) -> Self {
		Error {
			global: false,
			message: msg.to_string(),
		}
	}

	pub fn fatal(mut self) -> Self {
		self.global = true;
		self
	}
}

fn err() -> Error {
	Error {
		global: false,
		message: String::new(),
	}
}

pub fn one<'a, C>(i: &mut Input<'a, C>, expected: char) -> Result<(), Error> {
	match i.current_char() {
		Some(c) if c == expected => {
			i.advance();
			Ok(())
		}
		Some(_) | None => Err(err()),
	}
}

// Recognizes a specific string tag
pub fn tag<'a, C>(i: &mut Input<'a, C>, expected_tag: &'static str) -> Result<&'a str, Error> {
	let start_pos = i.pos;
	for expected_char in expected_tag.chars() {
		match i.current_char() {
			Some(c) if c == expected_char => {
				i.advance();
			}
			_ => {
				i.pos = start_pos; // Backtrack on mismatch
				return Err(err());
			}
		}
	}
	Ok(i.slice(start_pos..i.pos))
}

// Makes a parser optional
pub fn opt<'a, T, P, C>(i: &mut Input<'a, C>, parser: P) -> Result<Option<T>, Error>
where
	P: FnOnce(&mut Input<'a, C>) -> Result<T, Error>,
{
	match parser(i) {
		Ok(result) => Ok(Some(result)),
		Err(_) => Ok(None),
	}
}

// Runs parser P and consumes output of parser S afterwards
pub fn terminated<'a, T, P, S, C>(
	i: &mut Input<'a, C>,
	mut parser: P,
	mut terminator: S,
) -> Result<T, Error>
where
	P: FnMut(&mut Input<'a, C>) -> Result<T, Error>,
	S: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
{
	let result = parser(i)?;
	terminator(i)?;
	Ok(result)
}

// Runs parser S, then parser P, returns result of P
pub fn preceded<'a, T, P, S, C>(
	i: &mut Input<'a, C>,
	mut prefix: S,
	mut parser: P,
) -> Result<T, Error>
where
	P: FnMut(&mut Input<'a, C>) -> Result<T, Error>,
	S: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
{
	prefix(i)?;
	parser(i)
}

// Runs parser O, then P, then C. Returns result of P.
pub fn delimited<'a, T, O, P, CL, C>(
	i: &mut Input<'a, C>,
	mut opener: O,
	mut parser: P,
	mut closer: CL,
) -> Result<T, Error>
where
	O: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
	P: FnMut(&mut Input<'a, C>) -> Result<T, Error>,
	CL: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
{
	opener(i)?;
	let result = parser(i)?;
	closer(i)?;
	Ok(result)
}

/// Matches a non-empty list of P separated by S.
/// Equivalent to seq(P, zero_or_more(S, P)).
pub fn list<'a, T, P, S, C>(
	i: &mut Input<'a, C>,
	mut parser: P,
	mut separator: S,
) -> Result<&'a str, Error>
where
	P: FnMut(&mut Input<'a, C>) -> Result<T, Error>,
	S: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
{
	let start_pos = i.pos;
	parser(i)?;
	zero_or_more(i, |i| {
		separator(i)?;
		parser(i)?;
		Ok(())
	})?;

	Ok(i.slice(start_pos..i.pos))
}

/// Matches a non-empty list of P separated by S and returns the results in a Vec.
/// Equivalent to seq(P, zero_or_more(S, P)).
pub fn list_collect<'a, T, P, S, C>(
	i: &mut Input<'a, C>,
	mut parser: P,
	mut separator: S,
) -> Result<Vec<T>, Error>
where
	P: FnMut(&mut Input<'a, C>) -> Result<T, Error>,
	S: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
{
	let mut results = Vec::new();

	results.push(parser(i)?);
	zero_or_more(i, |i| {
		separator(i)?;
		results.push(parser(i)?);
		Ok(())
	})?;

	Ok(results)
}

pub fn must<'a, T, P, C>(i: &mut Input<'a, C>, mut parser: P) -> Result<T, Error>
where
	P: FnMut(&mut Input<'a, C>) -> Result<T, Error>,
{
	match parser(i) {
		Ok(result) => Ok(result),
		Err(e) => Err(e.fatal()),
	}
}

pub fn zero_or_more<'a, P, C>(i: &mut Input<'a, C>, mut parser: P) -> Result<(), Error>
where
	P: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
{
	loop {
		let start_pos = i.pos;
		match parser(i) {
			Ok(_) => {
				if i.pos == start_pos {
					// Zero-width loop detected
					// TODO: Is it correct to return Ok here?
					return Ok(());
				}
			}
			Err(e) if e.global => {
				return Err(e);
			}
			Err(_) => {
				i.pos = start_pos; // Backtrack on non-fatal error
				return Ok(());
			}
		}
	}
}

pub fn one_or_more<'a, P, C>(i: &mut Input<'a, C>, mut parser: P) -> Result<(), Error>
where
	P: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
{
	let initial_pos = i.pos;
	parser(i)?;

	if i.pos == initial_pos {
		// First parse succeeded but consumed nothing. This is tricky.
		// If zero_or_more is called next, it needs to handle this.
		// Let's rely on zero_or_more's ZeroWidthLoop check.
	}

	zero_or_more(i, parser)?;

	Ok(())
}

pub fn blank<'a, C>(i: &mut Input<'a, C>) -> Result<(), Error> {
	loop {
		let start_pos = i.pos;
		match i.current_char() {
			Some(c) if c.is_whitespace() => {
				i.advance();
				if i.pos == start_pos {
					return Err(err());
				}
			}
			_ => break,
		}
	}
	Ok(())
}

/// Matches an R that can be padded by arbitrary many P on the left and on the right.
/// Equivalent to seq(zero_or_more(P), R, zero_or_more(P)).
pub fn pad<'a, R, P, T, C>(i: &mut Input<'a, C>, mut parser: R, mut padding: P) -> Result<T, Error>
where
	R: FnMut(&mut Input<'a, C>) -> Result<T, Error>,
	P: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
{
	zero_or_more(i, |i| padding(i))?;
	let result = parser(i)?;
	zero_or_more(i, |i| padding(i))?;
	Ok(result)
}

/// Matches an optional R that can be padded by arbitrary many P or matches just arbitrary many P.
/// Equivalent to seq(zero_or_more(P), opt(R, zero_or_more(P))).
pub fn pad_opt<'a, R, P, T, C>(
	i: &mut Input<'a, C>,
	mut parser: R,
	mut padding: P,
) -> Result<Option<T>, Error>
where
	R: FnMut(&mut Input<'a, C>) -> Result<T, Error>,
	P: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
{
	zero_or_more(i, |i| padding(i))?;
	opt(i, |i| {
		terminated(i, |i| parser(i), |i| zero_or_more(i, |i| padding(i)))
	})
}

// Parses content until a delimiter is reached, allowing escapes
// Returns the parsed content exluding the delimiter and consumes the input past the delimiter
pub fn until<'a, D, E, C>(
	i: &mut Input<'a, C>,
	mut delimiter: D,
	mut element: E,
) -> Result<&'a str, Error>
where
	D: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
	E: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
{
	let start_pos = i.pos;

	loop {
		let delimiter_start = i.pos;

		if delimiter(i).is_ok() {
			return Ok(i.slice(start_pos..delimiter_start));
		}

		i.pos = delimiter_start;

		match element(i) {
			Ok(_) => {
				if i.pos == delimiter_start {
					return Err(Error::from_msg("Zero-width match in until parser"));
				}
			}
			Err(e) if e.global => return Err(e),
			Err(_) => {
				if i.current_char().is_none() {
					return Err(Error::from_msg("Unterminated sequence in until parser"));
				}
				i.advance();
			}
		}
	}
}

pub fn if_must<'a, F, S, C, T>(i: &mut Input<'a, C>, mut first: F, second: S) -> Result<T, Error>
where
	F: FnMut(&mut Input<'a, C>) -> Result<(), Error>,
	S: FnMut(&mut Input<'a, C>) -> Result<T, Error>,
{
	first(i)?;
	must(i, second)
}

/// Succeeds at "end-of-input", i.e. when the input is empty or all input has been consumed.
pub fn eoi<'a, C>(i: &mut Input<'a, C>) -> Result<(), Error> {
	if i.current_char().is_none() {
		Ok(())
	} else {
		Err(Error::from_msg("Expected end of input"))
	}
}

macro_rules! sor {
	// Base case with single parser
	($input:expr, $parser:expr $(,)?) => {
		$parser($input)
	};

	// Recursive case with two or more parsers
	($input:expr, $first:expr, $($rest:expr),+ $(,)?) => {{
		let start_pos = $input.pos;
		match $first($input) {
			Ok(result) => Ok(result),
			Err(_) => {
				$input.pos = start_pos;
				match sor!($input, $($rest),+) {
					Ok(result) => Ok(result),
					Err(e) => {
						$input.pos = start_pos;
						Err(e)
					}
				}
			}
		}
	}};
}

pub(crate) use sor;