brane_dsl/scanner/comments.rs
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
// COMMENTS.rs
// by Lut99
//
// Created:
// 25 Aug 2022, 11:08:56
// Last edited:
// 17 Jan 2023, 14:59:18
// Auto updated?
// Yes
//
// Description:
//! Defines a few scanning functions that parse comments.
//
use nom::bytes::complete as bc;
use nom::error::{ContextError, ParseError};
use nom::{IResult, Parser, branch, combinator as comb, sequence as seq};
use super::Span;
use super::tokens::Token;
/***** SCANNING FUNCTIONS *****/
/// Parses a single-line comment off the top of the given input.
///
/// # Arguments
/// - `input`: The input text to scan.
///
/// # Returns
/// The remaining tokens and a `Token::None`, representing that we did not really parse useful information.
///
/// # Errors
/// This function errors if we could not parse a comment.
pub fn single_line_comment<'a, E: ParseError<Span<'a>> + ContextError<Span<'a>>>(input: Span<'a>) -> IResult<Span<'a>, Token<'a>, E> {
comb::value(Token::None, seq::pair(bc::tag("//"), seq::terminated(comb::opt(bc::is_not("\n")), branch::alt((bc::tag("\n"), comb::eof)))))
.parse(input)
}
/// Parses a multi-line comment off the top of the given input.
///
/// # Arguments
/// - `input`: The input text to scan.
///
/// # Returns
/// The remaining tokens and a `Token::None`, representing that we did not really parse useful information.
///
/// # Errors
/// This function errors if we could not parse a comment.
pub fn multi_line_comment<'a, E: ParseError<Span<'a>> + ContextError<Span<'a>>>(input: Span<'a>) -> IResult<Span<'a>, Token<'a>, E> {
comb::value(Token::None, seq::tuple((bc::tag("/*"), comb::cut(seq::pair(bc::take_until("*/"), bc::tag("*/")))))).parse(input)
}
/***** LIBRARY *****/
/// Scans a comment from the top of the given input.
///
/// # Arguments
/// - `input`: The input text to scan.
///
/// # Returns
/// The remaining tokens and a `Token::None`, representing that we did not really parse useful information.
///
/// # Errors
/// This function errors if we could not parse a comment.
pub fn parse<'a, E: ParseError<Span<'a>> + ContextError<Span<'a>>>(input: Span<'a>) -> IResult<Span<'a>, Token<'a>, E> {
// println!("COMMENTS")
branch::alt((single_line_comment, multi_line_comment)).parse(input)
}