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
72
73
74
75
76
//! Provides parsers for f-comps.
use nom::character::complete::multispace1;
use nom::combinator::map;
use nom::sequence::{preceded, tuple};
use crate::parsers::{parens, ParseResult, Span};
use crate::parsers::{parse_binary_comp, parse_f_exp};
use crate::types::FComp;
/// Parses an f-comp.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_f_comp, preamble::*};
/// # use pddl::{FunctionTerm, Variable, FunctionSymbol, Term, FComp, BinaryComp, FExp, BinaryOp};
/// assert!(parse_f_comp("(= (+ 1.23 2.34) (+ 1.23 2.34))").is_value(
/// FComp::new(
/// BinaryComp::Equal,
/// FExp::new_binary_op(
/// BinaryOp::Addition,
/// FExp::new_number(1.23),
/// FExp::new_number(2.34),
/// ),
/// FExp::new_binary_op(
/// BinaryOp::Addition,
/// FExp::new_number(1.23),
/// FExp::new_number(2.34),
/// )
/// )
/// ));
///```
pub fn parse_f_comp<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, FComp> {
map(
parens(tuple((
parse_binary_comp,
preceded(multispace1, parse_f_exp),
preceded(multispace1, parse_f_exp),
))),
FComp::from,
)(input.into())
}
impl crate::parsers::Parser for FComp {
type Item = FComp;
/// See [`parse_f_comp`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_f_comp(input)
}
}
#[cfg(test)]
mod tests {
use crate::parsers::UnwrapValue;
use crate::{BinaryComp, BinaryOp, FComp, FExp, Parser};
#[test]
fn test_parse() {
assert!(
FComp::parse("(= (+ 1.23 2.34) (+ 1.23 2.34))").is_value(FComp::new(
BinaryComp::Equal,
FExp::new_binary_op(
BinaryOp::Addition,
FExp::new_number(1.23),
FExp::new_number(2.34),
),
FExp::new_binary_op(
BinaryOp::Addition,
FExp::new_number(1.23),
FExp::new_number(2.34),
)
))
);
}
}