pub fn is_whitespace(c: char) -> bool
Expand description

Checks if the given char c is a JSON whitespace.

Examples found in repository?
src/parse.rs (line 292)
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
	fn skip_whitespaces(&mut self) -> Result<(), Meta<Error<M, E>, M>> {
		while let Some(c) = self.peek_char()? {
			if is_whitespace(c) {
				self.next_char()?;
			} else {
				break;
			}
		}

		self.position.span.clear();
		Ok(())
	}

	fn skip_trailing_whitespaces(&mut self, context: Context) -> Result<(), Meta<Error<M, E>, M>> {
		self.skip_whitespaces()?;

		if let Some(c) = self.peek_char()? {
			if !context.follows(c) {
				// panic!("unexpected {:?} in {:?}", c, context);
				return Err(Meta(Error::unexpected(Some(c)), self.position.last()));
			}
		}

		Ok(())
	}
}

/// Parse error.
#[derive(Debug)]
pub enum Error<M, E = core::convert::Infallible> {
	/// Stream error.
	Stream(E),

	/// Unexpected character or end of stream.
	Unexpected(Option<char>),

	/// Invalid unicode codepoint.
	InvalidUnicodeCodePoint(u32),

	/// Missing low surrogate in a string.
	MissingLowSurrogate(Meta<u16, M>),

	/// Invalid low surrogate in a string.
	InvalidLowSurrogate(Meta<u16, M>, u32),
}

impl<M, E> Error<M, E> {
	/// Creates an `Unexpected` error.
	#[inline(always)]
	fn unexpected(c: Option<char>) -> Self {
		// panic!("unexpected {:?}", c);
		Self::Unexpected(c)
	}
}

impl<E: fmt::Display, M> fmt::Display for Error<M, E> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::Stream(e) => e.fmt(f),
			Self::Unexpected(Some(c)) => write!(f, "unexpected character `{}`", c),
			Self::Unexpected(None) => write!(f, "unexpected end of file"),
			Self::InvalidUnicodeCodePoint(c) => write!(f, "invalid Unicode code point {:x}", *c),
			Self::MissingLowSurrogate(_) => write!(f, "missing low surrogate"),
			Self::InvalidLowSurrogate(_, _) => write!(f, "invalid low surrogate"),
		}
	}
}

impl<E: 'static + std::error::Error, M: std::fmt::Debug> std::error::Error for Error<M, E> {
	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
		match self {
			Self::Stream(e) => Some(e),
			_ => None,
		}
	}
}

pub type MetaError<M, E = core::convert::Infallible> = Meta<Error<M, E>, M>;

/// Lexer position.
struct Position<F> {
	span: Span,
	last_span: Span,
	metadata_builder: F,
}

impl<F> Position<F> {
	fn new(metadata_builder: F) -> Self {
		Self {
			span: Span::default(),
			last_span: Span::default(),
			metadata_builder,
		}
	}

	fn current_span(&self) -> Span {
		self.span
	}
}

impl<F: FnMut(Span) -> M, M> Position<F> {
	fn metadata_at(&mut self, span: Span) -> M {
		(self.metadata_builder)(span)
	}

	fn current(&mut self) -> M {
		(self.metadata_builder)(self.span)
	}

	fn end(&mut self) -> M {
		(self.metadata_builder)(self.span.end().into())
	}

	fn last(&mut self) -> M {
		(self.metadata_builder)(self.last_span)
	}
}

/// Parsing context.
///
/// Defines what characters are allowed after a value.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Context {
	None,
	Array,
	ObjectKey,
	ObjectValue,
}

impl Context {
	/// Checks if the given character `c` can follow a value in this context.
	pub fn follows(&self, c: char) -> bool {
		match self {
			Self::None => is_whitespace(c),
			Self::Array => is_whitespace(c) || matches!(c, ',' | ']'),
			Self::ObjectKey => is_whitespace(c) || matches!(c, ':'),
			Self::ObjectValue => is_whitespace(c) || matches!(c, ',' | '}'),
		}
	}