-
Notifications
You must be signed in to change notification settings - Fork 1
/
timed_gd.rs
104 lines (94 loc) · 2.91 KB
/
timed_gd.rs
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
//! Provides parsers for timed goal definitions.
use crate::parsers::{parse_gd, parse_interval, parse_time_specifier};
use crate::parsers::{prefix_expr, ParseResult, Span};
use crate::types::TimedGD;
use nom::branch::alt;
use nom::character::complete::multispace1;
use nom::combinator::map;
use nom::sequence::{preceded, tuple};
/// Parser for timed goal definitions.
///
/// ## Examples
/// ```
/// # use pddl::parsers::{parse_timed_gd, preamble::*};
/// # use pddl::{AtomicFormula, GoalDefinition, Interval, Term, TimedGD, TimeSpecifier};
/// assert!(parse_timed_gd("(at start (= x y))").is_value(
/// TimedGD::new_at(
/// TimeSpecifier::Start,
/// GoalDefinition::AtomicFormula(
/// AtomicFormula::new_equality(
/// Term::Name("x".into()),
/// Term::Name("y".into())
/// )
/// )
/// )
/// ));
///
/// assert!(parse_timed_gd("(over all (= x y))").is_value(
/// TimedGD::new_over(
/// Interval::All,
/// GoalDefinition::AtomicFormula(
/// AtomicFormula::new_equality(
/// Term::Name("x".into()),
/// Term::Name("y".into())
/// )
/// )
/// )
/// ));
/// ```
pub fn parse_timed_gd<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, TimedGD> {
let at = map(
prefix_expr(
"at",
tuple((parse_time_specifier, preceded(multispace1, parse_gd))),
),
TimedGD::from,
);
let over = map(
prefix_expr(
"over",
tuple((parse_interval, preceded(multispace1, parse_gd))),
),
TimedGD::from,
);
alt((at, over))(input.into())
}
impl crate::parsers::Parser for TimedGD {
type Item = TimedGD;
/// See [`parse_timed_gd`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_timed_gd(input)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parsers::UnwrapValue;
use crate::{AtomicFormula, GoalDefinition, Interval, Parser, Term, TimeSpecifier};
#[test]
fn it_works() {
let input = "(over all (can-move ?from-waypoint ?to-waypoint))";
let (_, _gd) = parse_timed_gd(Span::new(input)).unwrap();
}
#[test]
fn test_parse() {
assert!(
TimedGD::parse("(at start (= x y))").is_value(TimedGD::new_at(
TimeSpecifier::Start,
GoalDefinition::AtomicFormula(AtomicFormula::new_equality(
Term::Name("x".into()),
Term::Name("y".into())
))
))
);
assert!(
parse_timed_gd("(over all (= x y))").is_value(TimedGD::new_over(
Interval::All,
GoalDefinition::AtomicFormula(AtomicFormula::new_equality(
Term::Name("x".into()),
Term::Name("y".into())
))
))
);
}
}