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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Provides parsers for f-exps.
use nom::branch::alt;
use nom::character::complete::{char, multispace0, multispace1};
use nom::combinator::map;
use nom::sequence::{preceded, tuple};
use crate::parsers::{parens, parse_number, space_separated_list1, ParseResult, Span};
use crate::parsers::{parse_binary_op, parse_f_head, parse_multi_op};
use crate::types::FExp;
/// Parses an f-exp.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_f_exp, preamble::*};
/// # use pddl::{BinaryOp, FExp, FHead, FunctionSymbol, MultiOp};
/// assert!(parse_f_exp("1.23").is_value(
/// FExp::new_number(1.23)
/// ));
///
/// assert!(parse_f_exp("(+ 1.23 2.34)").is_value(
/// FExp::new_binary_op(
/// BinaryOp::Addition,
/// FExp::new_number(1.23),
/// FExp::new_number(2.34)
/// )
/// ));
///
/// assert!(parse_f_exp("(+ 1.23 2.34 3.45)").is_value(
/// FExp::new_multi_op(
/// MultiOp::Addition,
/// FExp::new_number(1.23),
/// [FExp::new_number(2.34), FExp::new_number(3.45)]
/// )
/// ));
///
/// assert!(parse_f_exp("(- 1.23)").is_value(
/// FExp::new_negative(FExp::new_number(1.23))
/// ));
///
/// assert!(parse_f_exp("fun-sym").is_value(
/// FExp::new_function(
/// FHead::new(FunctionSymbol::from_str("fun-sym"))
/// )
/// ));
///```
pub fn parse_f_exp<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, FExp> {
// :numeric-fluents
let number = map(parse_number, FExp::new_number);
// :numeric-fluents
let binary_op = map(
parens(tuple((
parse_binary_op,
preceded(multispace1, parse_f_exp),
preceded(multispace1, parse_f_exp),
))),
|(op, lhs, rhs)| FExp::new_binary_op(op, lhs, rhs),
);
// :numeric-fluents
let multi_op = map(
parens(tuple((
parse_multi_op,
preceded(multispace1, parse_f_exp),
preceded(multispace1, space_separated_list1(parse_f_exp)),
))),
|(op, lhs, rhs)| FExp::new_multi_op(op, lhs, rhs),
);
// :numeric-fluents
let negated = map(
parens(preceded(tuple((char('-'), multispace0)), parse_f_exp)),
FExp::new_negative,
);
// :numeric-fluents
let f_head = map(parse_f_head, FExp::new_function);
alt((number, binary_op, multi_op, negated, f_head))(input.into())
}
impl crate::parsers::Parser for FExp {
type Item = FExp;
/// See [`parse_f_exp`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_f_exp(input)
}
}
#[cfg(test)]
mod tests {
use crate::parsers::UnwrapValue;
use crate::{BinaryOp, FExp, FHead, FunctionSymbol, MultiOp, Parser};
#[test]
fn test_parse() {
assert!(FExp::parse("1.23").is_value(FExp::new_number(1.23)));
assert!(FExp::parse("(+ 1.23 2.34)").is_value(FExp::new_binary_op(
BinaryOp::Addition,
FExp::new_number(1.23),
FExp::new_number(2.34)
)));
assert!(
FExp::parse("(+ 1.23 2.34 3.45)").is_value(FExp::new_multi_op(
MultiOp::Addition,
FExp::new_number(1.23),
[FExp::new_number(2.34), FExp::new_number(3.45)]
))
);
assert!(FExp::parse("(- 1.23)").is_value(FExp::new_negative(FExp::new_number(1.23))));
assert!(
FExp::parse("fun-sym").is_value(FExp::new_function(FHead::new(
FunctionSymbol::from_str("fun-sym")
)))
);
}
}