Fawkes API  Fawkes Development Version
pddl_exception.h
1 /***************************************************************************
2  * pddl_exception.cpp exceptions thrown durning parsing
3  *
4  * Created: Thursday 15 October 2020
5  * Copyright 2020 Tarik Viehmann
6  ****************************************************************************/
7 
8 /* This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Library General Public License for more details.
17  *
18  * Read the full text in the LICENSE.GPL file in the doc directory.
19  */
20 
21 #ifndef PDDL_EXCEPTION_H
22 #define PDDL_EXCEPTION_H
23 
24 #include "pddl_ast.h"
25 
26 #include <core/exception.h>
27 #include <core/threading/mutex.h>
28 
29 #include <string>
30 
31 namespace pddl_parser {
32 
33 /** @class PddlParserException <pddl_parser/pddl_exception.h>
34  * Exception thrown by the parser if an error occurs during parsing.
35  */
36 
38 {
39 public:
40  /** Constructor.
41  * @param msg A message describing the error.
42  */
43  PddlParserException(const char *msg) : fawkes::Exception(msg)
44  {
45  }
46 
47  /** Merge all error messages to a single one.
48  * Useful after calling append() or prepend().
49  */
50  void
52  {
53  if (messages != NULL) {
54  std::string res(messages->msg);
55  auto curr_msg = messages->next;
56  auto old_msg = messages;
58  while (curr_msg != NULL) {
59  if (curr_msg->msg) {
60  res += curr_msg->msg;
61  free(curr_msg->msg);
62  }
63  old_msg = curr_msg;
64  curr_msg = curr_msg->next;
65  free(old_msg);
66  }
68  messages->msg = strdup(res.c_str());
69  messages->next = NULL;
70  messages_end = NULL;
71  }
72  }
73 
74  const char *
75  what() const throw() override
76  {
77  if (messages != NULL) {
78  return messages->msg;
79  } else {
80  return "Unknown Error";
81  }
82  }
83  /** Constructor with a string message.
84  * This wraps the constructor with a char* message for usage with std::string.
85  * @param msg A message describing the error.
86  */
87  PddlParserException(const std::string &msg) : fawkes::Exception(msg.c_str())
88  {
89  }
90 };
91 
92 /** @class PddlSemanticsException <pddl_parser/pddl_exception.h>
93  * Exception thrown by the parser if an error occurs during semantic checks
94  * during parsing.
95  */
97 {
98 public:
99  /** Position of the error to generate a helpful error message. */
100  const iterator_type pos;
101  /** Constructor.
102  * @param msg A message describing the error.
103  * @param pos The position in the parsed string where the error occurs.
104  */
105  PddlSemanticsException(const char *msg, const iterator_type &pos)
106  : PddlParserException(msg), pos(pos)
107  {
108  }
109  /** Constructor with a string message.
110  * This wraps the constructor with a char* message for usage with std::string.
111  * @param msg A message describing the error.
112  * @param pos The position in the parsed string where the error occurs.
113  */
114  PddlSemanticsException(const std::string &msg, const iterator_type &pos)
115  : PddlParserException(msg.c_str()), pos(pos)
116  {
117  }
118 };
119 
120 /** @class PddlTypeException <pddl_parser/pddl_exception.h>
121  * Exception thrown by the parser if declared type does not match the defined
122  * one.
123  */
125 {
127 };
128 /** @class PddlSyntaxException <pddl_parser/pddl_exception.h>
129  * Exception thrown by the parser if there is a syntax error.
130  */
132 {
134 };
135 /** @class PddlPredicateException <pddl_parser/pddl_exception.h>
136  * Exception thrown by the parser if a declared relation does not match the
137  * defined predicate.
138  */
140 {
142 };
143 /** @class PddlExpressionException <pddl_parser/pddl_exception.h>
144  * Exception thrown by the parser if an expression is invalid.
145  */
147 {
149 };
150 /** @class PddlConstantException <pddl_parser/pddl_exception.h>
151  * Exception thrown by the parser if a declared constant does not match a
152  * defined one.
153  */
155 {
157 };
158 /** @class PddlParameterException <pddl_parser/pddl_exception.h>
159  * Exception thrown by the parser if a parameter mismatch is encountered.
160  */
162 {
164 };
165 
166 } // end namespace pddl_parser
167 #endif /* !PDDL_EXCEPTION_H */
Base class for exceptions in Fawkes.
Definition: exception.h:36
Mutex * messages_mutex
Mutex to protect operations on messages list.
Definition: exception.h:111
message_list_t * messages_end
Pointer that points to the very last message.
Definition: exception.h:110
message_list_t * messages
List of messages.
Definition: exception.h:108
Exception() noexcept
Constructor for subclasses.
Definition: exception.cpp:253
void lock()
Lock this mutex.
Definition: mutex.cpp:87
void unlock()
Unlock the mutex.
Definition: mutex.cpp:131
Exception thrown by the parser if a declared constant does not match a defined one.
Exception thrown by the parser if an expression is invalid.
Exception thrown by the parser if a parameter mismatch is encountered.
Exception thrown by the parser if an error occurs during parsing.
PddlParserException(const std::string &msg)
Constructor with a string message.
void collapse_msg()
Merge all error messages to a single one.
const char * what() const override
Get primary string.
PddlParserException(const char *msg)
Constructor.
Exception thrown by the parser if a declared relation does not match the defined predicate.
Exception thrown by the parser if an error occurs during semantic checks during parsing.
PddlSemanticsException(const std::string &msg, const iterator_type &pos)
Constructor with a string message.
const iterator_type pos
Position of the error to generate a helpful error message.
PddlSemanticsException(const char *msg, const iterator_type &pos)
Constructor.
Exception thrown by the parser if there is a syntax error.
Exception thrown by the parser if declared type does not match the defined one.
Fawkes library namespace.
message_list_t * next
pointer to next element, NULL if last element
Definition: exception.h:66
char * msg
pointer to message, may not be NULL, will be freed in dtor
Definition: exception.h:67