wright 0.10.1

The wright programming language compiler and tooling.
Documentation
//! Utilities for parsing through whitespace.

use super::{
    error::{ParserError, ParserErrorKind},
    Parser,
};
use crate::lexer::token::TokenTy;

/// Consume and ignore a [TokenTy::Whitespace] from the front of the [Parser].
/// If there is not one, do nothing.
pub fn optional_whitespace(parser: &mut Parser) {
    while parser.peek().is_some_and(|token| token.variant == TokenTy::Whitespace) {
        parser.advance(1);
    }
}

/// Require a whitespace from the [Parser]. Do not advance if the next [Token] is not a whitespace.
///
/// [Token]: crate::lexer::token::Token
pub fn require_whitespace(parser: &mut Parser) -> Result<(), ParserError> {
    match parser.next_if_is(TokenTy::Whitespace) {
        Some(_) => {
            // Remove any other non-contiguous whitespaces that may have followed.
            optional_whitespace(parser);
            Ok(())
        },

        None => Err(ParserError {
            kind: ParserErrorKind::ExpectedWhitespace,
            location: parser.peek_fragment_or_rest_cloned(),
            help: None,
        }),
    }
}