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
use nom::IResult;

use crate::error::ParserError;
use crate::lexer::{Token, TokenStream};

/// Parse an identifier from the input stream. Identifiers are strings that do not start with a question mark.
///
/// # Errors
///
/// Returns an error if the next token is not an identifier.
pub fn id(i: TokenStream) -> IResult<TokenStream, String, ParserError> {
    match i.peek() {
        Some((Ok(Token::Id(s)), _)) => Ok((i.advance(), s)),
        _ => Err(nom::Err::Error(ParserError::ExpectedIdentifier)),
    }
}

/// Parse a variable from the input stream. Variables are identifiers that start with a question mark.
///
/// # Errors
///
/// Returns an error if the next token is not a variable.
pub fn var(i: TokenStream) -> IResult<TokenStream, String, ParserError> {
    match i.peek() {
        Some((Ok(Token::Var(s)), _)) => Ok((i.advance(), s)),
        _ => Err(nom::Err::Error(ParserError::ExpectedIdentifier)),
    }
}

/// Parse a floating point number from the input stream.
///
/// # Errors
///
/// Returns an error if the next token is not a floating point number.
pub fn float(i: TokenStream) -> IResult<TokenStream, f64, ParserError> {
    match i.peek() {
        Some((Ok(Token::Float(s)), _)) => Ok((i.advance(), s)),
        _ => Err(nom::Err::Error(ParserError::ExpectedFloat)),
    }
}

/// Parse an integer from the input stream.
///
/// # Errors
///
/// Returns an error if the next token is not an integer.
pub fn integer(i: TokenStream) -> IResult<TokenStream, i64, ParserError> {
    match i.peek() {
        Some((Ok(Token::Integer(s)), _)) => Ok((i.advance(), s)),
        _ => Err(nom::Err::Error(ParserError::ExpectedInteger)),
    }
}