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
//! Provides parsers for f-exps.
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::multispace1;
use nom::combinator::map;
use nom::sequence::{preceded, terminated, tuple};
use crate::parsers::prefix_expr;
use crate::parsers::{parse_f_exp, ParseResult, Span};
use crate::types::FExpT;
/// Parses an f-exp-t.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_f_exp, parse_f_exp_t, preamble::*};
/// # use pddl::{BinaryOp, FExp, FExpT, FHead, FunctionSymbol, MultiOp, Term, Variable};
/// assert!(parse_f_exp_t("#t").is_value(FExpT::Now));
///
/// assert!(parse_f_exp_t("(* (fuel ?tank) #t)").is_value(
/// FExpT::new_scaled(
/// FExp::new_function(
/// FHead::new_with_terms(
/// FunctionSymbol::from_str("fuel"),
/// [Term::Variable(Variable::from_str("tank"))]
/// )
/// )
/// )
/// ));
///
/// assert!(parse_f_exp_t("(* #t (fuel ?tank))").is_value(
/// FExpT::new_scaled(
/// FExp::new_function(
/// FHead::new_with_terms(
/// FunctionSymbol::from_str("fuel"),
/// [Term::Variable(Variable::from_str("tank"))]
/// )
/// )
/// )
/// ));
///```
pub fn parse_f_exp_t<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, FExpT> {
let now = map(tag("#t"), |_| FExpT::new());
let scaled = map(
prefix_expr(
"*",
alt((
preceded(tuple((tag("#t"), multispace1)), parse_f_exp),
terminated(parse_f_exp, tuple((multispace1, tag("#t")))),
)),
),
FExpT::new_scaled,
);
alt((scaled, now))(input.into())
}
impl crate::parsers::Parser for FExpT {
type Item = FExpT;
/// See [`parse_f_exp_t`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_f_exp_t(input)
}
}
#[cfg(test)]
mod tests {
use crate::parsers::UnwrapValue;
use crate::{FExp, FExpT, FHead, FunctionSymbol, Parser, Term, Variable};
#[test]
fn test_parse() {
assert!(FExpT::parse("#t").is_value(FExpT::Now));
assert!(
FExpT::parse("(* (fuel ?tank) #t)").is_value(FExpT::new_scaled(FExp::new_function(
FHead::new_with_terms(
FunctionSymbol::from_str("fuel"),
[Term::Variable(Variable::from_str("tank"))]
)
)))
);
assert!(
FExpT::parse("(* #t (fuel ?tank))").is_value(FExpT::new_scaled(FExp::new_function(
FHead::new_with_terms(
FunctionSymbol::from_str("fuel"),
[Term::Variable(Variable::from_str("tank"))]
)
)))
);
}
}