pddl_parser/
tokens.rs

1use nom::IResult;
2
3use crate::error::ParserError;
4use crate::lexer::{Token, TokenStream};
5
6/// Parse an identifier from the input stream. Identifiers are strings that do not start with a question mark.
7///
8/// # Errors
9///
10/// Returns an error if the next token is not an identifier.
11pub fn id(i: TokenStream) -> IResult<TokenStream, String, ParserError> {
12    match i.peek() {
13        Some((Ok(Token::Id(s)), _)) => Ok((i.advance(), s)),
14        _ => Err(nom::Err::Error(ParserError::ExpectedIdentifier)),
15    }
16}
17
18/// Parse a variable from the input stream. Variables are identifiers that start with a question mark.
19///
20/// # Errors
21///
22/// Returns an error if the next token is not a variable.
23pub fn var(i: TokenStream) -> IResult<TokenStream, String, ParserError> {
24    match i.peek() {
25        Some((Ok(Token::Var(s)), _)) => Ok((i.advance(), s)),
26        _ => Err(nom::Err::Error(ParserError::ExpectedIdentifier)),
27    }
28}
29
30/// Parse a floating point number from the input stream.
31///
32/// # Errors
33///
34/// Returns an error if the next token is not a floating point number.
35pub fn float(i: TokenStream) -> IResult<TokenStream, f64, ParserError> {
36    match i.peek() {
37        Some((Ok(Token::Float(s)), _)) => Ok((i.advance(), s)),
38        _ => Err(nom::Err::Error(ParserError::ExpectedFloat)),
39    }
40}
41
42/// Parse an integer from the input stream.
43///
44/// # Errors
45///
46/// Returns an error if the next token is not an integer.
47pub fn integer(i: TokenStream) -> IResult<TokenStream, i64, ParserError> {
48    match i.peek() {
49        Some((Ok(Token::Integer(s)), _)) => Ok((i.advance(), s)),
50        _ => Err(nom::Err::Error(ParserError::ExpectedInteger)),
51    }
52}