Fawkes API  Fawkes Development Version
pddl_parser.cpp
1 /***************************************************************************
2  * pddl_parser.cpp
3  *
4  * Created: Fri 19 May 2017 11:10:01 CEST
5  * Copyright 2017 Matthias Loebach
6  * Till Hofmann
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include "pddl_parser.h"
23 
24 #include <fstream>
25 #include <streambuf>
26 
27 namespace pddl_parser {
28 
29 /** @class PddlParser <pddl_parser/pddl_parser.h>
30  * Parse a PDDL domain file or problem.
31  * This class parses a domain/problem into a structured representation of
32  * the domain, which can then be used by other components.
33  * @see pddl_ast.h
34  */
35 
36 /** @class PddlParserException <pddl_parser/pddl_parser.h>
37  * Exception thrown by the parser if an error occurs during parsing.
38  */
39 
40 /** provide a description of the position in a file, where an error occurred.
41  * @param start_it begin of parsing context
42  * @param end_it end of parsing context
43  * @param current_it position in the parsing context
44  * \return a string containing the line at \a current_it and an indication
45  * of the current position
46  */
47 std::string
48 PddlParser::getErrorContext(const iterator_type &start_it,
49  const iterator_type &end_it,
50  const iterator_type &current_it)
51 {
52  auto line = get_current_line(start_it, current_it, end_it);
53  std::string line_str = std::string(line.begin(), line.end());
54  std::replace(begin(line_str), end(line_str), '\t', ' ');
55  std::ostringstream error_msg;
56  error_msg << " line:" << get_line(current_it) << ", col:" << get_column(line.begin(), current_it)
57  << "\n";
58  while (!line.empty() && std::strchr("\r\n", *line.begin()))
59  line.advance_begin(1);
60  error_msg << line_str << "\n";
61  error_msg << std::string(std::distance(line.begin(), current_it), ' ')
62  << "^ --- parsing halted here\n";
63  return error_msg.str();
64 }
65 
66 /** Parse the PDDL domain.
67  * @param pddl_domain The PDDL domain as string (not a path)
68  * @return A Domain object that contains the parsed domain.
69  * @see Domain
70  */
71 Domain
72 PddlParser::parseDomain(const std::string &pddl_domain)
73 {
76 
77  skipper s;
78  Domain dom;
79  bool r = false;
80 
81  iterator_type iter(pddl_domain.begin());
82  iterator_type end(pddl_domain.end());
83  grammar g;
84 
85  try {
86  r = phrase_parse(iter, end, g, s, dom);
87  } catch (qi::expectation_failure<iterator_type> const &e) {
88  using boost::spirit::basic_info_walker;
89  std::stringstream expectation;
90  boost::spirit::simple_printer<std::stringstream> pr(expectation);
91  basic_info_walker<boost::spirit::simple_printer<std::stringstream>> walker(pr, e.what_.tag, 0);
92  boost::apply_visitor(walker, e.what_.value);
93  throw PddlSyntaxException(std::string("Syntax Error: ") + e.what() + " expected "
94  + expectation.str() + " at " + getErrorContext(iter, end, e.first),
95  iter);
96  } catch (PddlSemanticsException &e) {
97  e.prepend("Semantic Error: ");
98  e.append(getErrorContext(iter, end, e.pos).c_str());
99  e.collapse_msg();
100  throw;
101  }
102 
103  if (!r) {
104  throw PddlParserException("Parsing PDDL domain string failed!");
105  }
106  return dom;
107 }
108 
109 /** Parse the PDDL problem.
110  * @param pddl_problem The problem as string (not a path)
111  * @return A Problem object that contains the parsed problem.
112  * @see Problem
113  */
114 Problem
115 PddlParser::parseProblem(const std::string &pddl_problem)
116 {
119 
120  grammar g;
121  skipper s;
122  Problem prob;
123  bool r = false;
124 
125  iterator_type iter(pddl_problem.begin());
126  iterator_type end(pddl_problem.end());
127 
128  try {
129  r = phrase_parse(iter, end, g, s, prob);
130  } catch (qi::expectation_failure<iterator_type> const &e) {
131  throw PddlSyntaxException(std::string("Expectation failed: ") + e.what()
132  + getErrorContext(iter, end, e.first),
133  iter);
134  }
135 
136  if (!r) {
137  throw PddlParserException("Parsing PDDL problem string failed!");
138  }
139  return prob;
140 }
141 
142 } // namespace pddl_parser
Exception thrown by the parser if an error occurs during parsing.
static Domain parseDomain(const std::string &pddl_domain)
Parse the PDDL domain.
Definition: pddl_parser.cpp:72
static Problem parseProblem(const std::string &pddl_problem)
Parse the PDDL problem.
Exception thrown by the parser if an error occurs during semantic checks during parsing.
Exception thrown by the parser if there is a syntax error.
A structured representation of a PDDL domain.
Definition: pddl_ast.h:157
A structured representation of a PDDL problem.
Definition: pddl_ast.h:180
A Boost QI parser for a PDDL domain.
Definition: pddl_grammar.h:59
A skipper for PDDL files.
Definition: pddl_grammar.h:45
A Boost QI parser for a PDDL problem.
Definition: pddl_grammar.h:283