brane_dsl/parser/
identifier.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
//  IDENTIFIER.rs
//    by Lut99
//
//  Created:
//    10 Aug 2022, 17:13:42
//  Last edited:
//    31 Oct 2023, 10:45:02
//  Auto updated?
//    Yes
//
//  Description:
//!   Defines the function(s) that parse identifiers.
//

use std::num::NonZeroUsize;

use log::trace;
use nom::error::{ContextError, ParseError};
use nom::{IResult, Parser, combinator as comb};

use super::ast::Identifier;
use crate::scanner::{Token, Tokens};
use crate::tag_token;


/***** LIBRARY *****/
/// Parses an iodentifier Token to an Identifier node in the AST.
///
/// # Arguments
/// - `input`: The list of tokens to parse from.
///
/// # Returns
/// The remaining list of tokens and the parsed Identifier if there was anything to parse. Otherwise, a `nom::Error` is returned (which may be a real error or simply 'could not parse').
pub fn parse<'a, E: ParseError<Tokens<'a>> + ContextError<Tokens<'a>>>(input: Tokens<'a>) -> IResult<Tokens<'a>, Identifier, E> {
    trace!("Attempting to parse identifier");
    comb::map(tag_token!(Token::Ident), |t| Identifier::new(t.tok[0].as_string(), t.tok[0].inner().into())).parse(input)
}