pddl/parsers/
mod.rs

1//! Provides parsing functions, as well as the [`Parser`] trait.
2
3mod action_def;
4mod action_symbol;
5mod assign_op;
6mod assign_op_t;
7mod atomic_formula;
8mod atomic_formula_skeleton;
9mod atomic_function_skeleton;
10mod basic_function_term;
11mod binary_comp;
12mod binary_op;
13mod c_effect;
14mod comments;
15mod con_gd;
16mod cond_effect;
17mod constants_def;
18mod d_op;
19mod d_value;
20mod da_def;
21mod da_effect;
22mod da_gd;
23mod da_symbol;
24mod derived_predicate;
25mod domain;
26mod domain_constraints_def;
27mod duration_constraint;
28mod effects;
29mod empty_or;
30mod f_assign_da;
31mod f_comp;
32mod f_exp;
33mod f_exp_da;
34mod f_exp_t;
35mod f_head;
36mod function_symbol;
37mod function_term;
38mod function_type;
39mod function_typed_list;
40mod functions_def;
41mod gd;
42mod goal_def;
43mod init_def;
44mod init_el;
45mod interval;
46mod length_spec;
47mod literal;
48mod metric_f_exp;
49mod metric_spec;
50mod multi_op;
51mod name;
52pub(crate) mod number;
53mod objects_def;
54mod optimization;
55mod p_effect;
56mod pre_gd;
57mod predicate;
58mod predicates_def;
59mod pref_con_gd;
60mod pref_gd;
61mod pref_name;
62mod pref_timed_gd;
63mod primitive_type;
64mod problem;
65mod problem_constraints_def;
66mod requirements;
67mod simple_duration_constraint;
68mod structure_def;
69mod term;
70mod test_helpers;
71mod time_specifier;
72mod timed_effect;
73mod timed_gd;
74mod timeless_def;
75mod r#type;
76mod typed_list;
77mod types_def;
78mod utilities;
79mod variable;
80
81#[cfg(test)]
82pub(crate) use test_helpers::Match;
83pub use test_helpers::UnwrapValue;
84
85/// Provides the [`Parser::parse`] and [`Parser::from_str`] methods.
86pub trait Parser {
87    type Item;
88
89    /// Parses the `input` into the specified [`Item`](Self::Item) type.
90    fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item>;
91
92    /// Parses the `input` into the specified [`Item`](Self::Item) type.
93    fn parse_span(input: Span) -> ParseResult<Self::Item> {
94        Self::parse(input)
95    }
96
97    /// Uses the [`Parser::parse`] method to parse the input and, if successful,
98    /// discards the unparsed remaining input.
99    fn from_str(input: &str) -> Result<Self::Item, nom::Err<ParseError>> {
100        let (_, value) = Self::parse(input)?;
101        Ok(value)
102    }
103}
104
105/// Input type for parsers.
106pub type Span<'a> = nom_locate::LocatedSpan<&'a str>;
107
108/// A parsing error.
109pub type ParseError<'a> = nom_greedyerror::GreedyError<Span<'a>, nom::error::ErrorKind>;
110
111/// A result from a parser.
112pub type ParseResult<'a, T, E = ParseError<'a>> = nom::IResult<Span<'a>, T, E>;
113
114/// Re-exports commonly used types.
115pub mod preamble {
116    pub use crate::parsers::test_helpers::UnwrapValue;
117    pub use crate::parsers::Parser;
118    pub use crate::parsers::{ParseError, ParseResult, Span};
119}
120
121// Parsers.
122pub use action_def::parse_action_def;
123pub use action_symbol::parse_action_symbol;
124pub use assign_op::parse_assign_op;
125pub use assign_op_t::parse_assign_op_t;
126pub use atomic_formula_skeleton::parse_atomic_formula_skeleton;
127pub use atomic_function_skeleton::parse_atomic_function_skeleton;
128pub use basic_function_term::parse_basic_function_term;
129pub use binary_comp::parse_binary_comp;
130pub use binary_op::parse_binary_op;
131pub use c_effect::{parse_c_effect, parse_forall_c_effect, parse_when_c_effect};
132pub use comments::ignore_eol_comment;
133pub use con_gd::parse_con_gd;
134pub use cond_effect::parse_cond_effect;
135pub use constants_def::parse_constants_def;
136pub use d_op::parse_d_op;
137pub use d_value::parse_d_value;
138pub use da_def::parse_da_def;
139pub use da_effect::parse_da_effect;
140pub use da_gd::parse_da_gd;
141pub use da_symbol::parse_da_symbol;
142pub use derived_predicate::parse_derived_predicate;
143pub use domain::parse_domain;
144pub use domain_constraints_def::parse_domain_constraints_def;
145pub use duration_constraint::parse_duration_constraint;
146pub use effects::parse_effect;
147pub use f_assign_da::parse_f_assign_da;
148pub use f_comp::parse_f_comp;
149pub use f_exp::parse_f_exp;
150pub use f_exp_da::parse_f_exp_da;
151pub use f_exp_t::parse_f_exp_t;
152pub use f_head::parse_f_head;
153pub use function_symbol::parse_function_symbol;
154pub use function_term::parse_function_term;
155pub use function_type::parse_function_type;
156pub use functions_def::parse_functions_def;
157pub use gd::parse_gd;
158pub use goal_def::parse_problem_goal_def;
159pub use init_def::parse_problem_init_def;
160pub use init_el::parse_init_el;
161pub use interval::parse_interval;
162pub use length_spec::parse_problem_length_spec;
163pub use metric_f_exp::parse_metric_f_exp;
164pub use metric_spec::parse_problem_metric_spec;
165pub use multi_op::parse_multi_op;
166pub use name::parse_name;
167pub use number::parse_number;
168pub use objects_def::parse_problem_objects_declaration;
169pub use optimization::parse_optimization;
170pub use p_effect::parse_p_effect;
171pub use pre_gd::parse_pre_gd;
172pub use predicate::parse_predicate;
173pub use predicates_def::parse_predicates_def;
174pub use pref_con_gd::parse_pref_con_gd;
175pub use pref_gd::parse_pref_gd;
176pub use pref_name::parse_pref_name;
177pub use pref_timed_gd::parse_pref_timed_gd;
178pub use primitive_type::parse_primitive_type;
179pub use problem::parse_problem;
180pub use problem_constraints_def::parse_problem_constraints_def;
181pub use r#type::parse_type;
182pub use requirements::{parse_require_def, parse_require_key};
183pub use simple_duration_constraint::parse_simple_duration_constraint;
184pub use structure_def::parse_structure_def;
185pub use term::parse_term;
186pub use time_specifier::parse_time_specifier;
187pub use timed_effect::parse_timed_effect;
188pub use timed_gd::parse_timed_gd;
189pub use timeless_def::parse_timeless_def;
190pub use types_def::parse_types_def;
191pub use variable::parse_variable;
192
193// Parser combinators.
194pub use atomic_formula::atomic_formula;
195pub use empty_or::empty_or;
196pub use function_typed_list::function_typed_list;
197pub use literal::literal;
198pub use typed_list::typed_list;
199
200// Utility parser combinators.
201#[allow(unused_imports)]
202pub(crate) use utilities::{
203    parens, prefix_expr, space_separated_list0, space_separated_list1, ws, ws2,
204};
205
206#[cfg(test)]
207mod tests {
208    use crate::{BasicFunctionTerm, Parser};
209
210    #[test]
211    fn test_parse() {
212        let (_, value) = BasicFunctionTerm::parse("abcde").unwrap();
213        assert_eq!(value, BasicFunctionTerm::new("abcde".into(), []));
214    }
215
216    #[test]
217    fn test_parse_span() {
218        let (_, value) = BasicFunctionTerm::parse_span("abcde".into()).unwrap();
219        assert_eq!(value, BasicFunctionTerm::new("abcde".into(), []));
220    }
221
222    #[test]
223    fn test_from_str() {
224        let value = BasicFunctionTerm::from_str("abcde").unwrap();
225        assert_eq!(value, BasicFunctionTerm::new("abcde".into(), []));
226    }
227}