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
77
78
79
80
//! Provides parsers for f-comps.
use nom::character::complete::multispace1;
use nom::combinator::map;
use nom::sequence::preceded;
use nom::Parser;
use crate::parsers::{parens, ParseResult, Span};
use crate::parsers::{parse_binary_comp, parse_f_exp};
use crate::types::FluentComparison;
/// Parses an f-comp.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_f_comp, preamble::*};
/// # use pddl::{FunctionTerm, Variable, FunctionSymbol, Term, FluentComparison, BinaryComparison, FluentExpression, BinaryOp};
/// assert!(parse_f_comp("(= (+ 1.23 2.34) (+ 1.23 2.34))").is_value(
/// FluentComparison::new(
/// BinaryComparison::Equal,
/// FluentExpression::new_binary_op(
/// BinaryOp::Addition,
/// FluentExpression::new_number(1.23),
/// FluentExpression::new_number(2.34),
/// ),
/// FluentExpression::new_binary_op(
/// BinaryOp::Addition,
/// FluentExpression::new_number(1.23),
/// FluentExpression::new_number(2.34),
/// )
/// )
/// ));
///```
pub fn parse_f_comp<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, FluentComparison> {
map(
parens((
parse_binary_comp,
preceded(multispace1, parse_f_exp),
preceded(multispace1, parse_f_exp),
)),
FluentComparison::from,
)
.parse(input.into())
}
impl crate::parsers::Parser for FluentComparison {
type Item = FluentComparison;
/// 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::{BinaryComparison, BinaryOp, FluentComparison, FluentExpression, Parser};
#[test]
fn test_parse() {
assert!(
FluentComparison::parse("(= (+ 1.23 2.34) (+ 1.23 2.34))").is_value(
FluentComparison::new(
BinaryComparison::Equal,
FluentExpression::new_binary_op(
BinaryOp::Addition,
FluentExpression::new_number(1.23),
FluentExpression::new_number(2.34),
),
FluentExpression::new_binary_op(
BinaryOp::Addition,
FluentExpression::new_number(1.23),
FluentExpression::new_number(2.34),
)
)
)
);
}
}