schema_editor/comdel/parser/presult.h

64 lines
1.9 KiB
C
Raw Normal View History

2022-03-29 19:31:45 +00:00
#ifndef PRESULT_H
#define PRESULT_H
#include "expected.h"
2022-05-27 06:18:17 +00:00
#include "source_error.h"
2022-03-29 19:31:45 +00:00
#include <optional>
/// Returns from the CURRENT function, the one this macro is used in,
/// if PResult contains an error
#define RETURN_IF_ERR(presult) \
do { \
auto&& return_if_err_temp_ = (presult); \
if (!return_if_err_temp_) { \
return PError(return_if_err_temp_.error()); \
} \
} while (0)
/// Works the same as RETURN_IF_ERR but can also assign the unwrapped result
/// to a variable if successful
#define ASSIGN_OR_RETURN_IF_ERR(var, presult) \
do { \
auto&& assign_or_return_if_err_temp_ = (presult); \
if (!assign_or_return_if_err_temp_) { \
return PError(assign_or_return_if_err_temp_.error()); \
} else { \
var = *assign_or_return_if_err_temp_; \
} \
} while (0)
using PError = tl::unexpected<SourceError>;
/// Holds either an AST node or an error
/// The value MUST be checked before usage, e.g. using RETURN_IF_ERR
2022-05-27 06:18:17 +00:00
template<typename T>
2022-03-29 19:31:45 +00:00
struct PResult : tl::expected<T, SourceError> {
using tl::expected<T, SourceError>::expected;
/// Implicit conversion from PResult<U> to PResult<T>
/// if U is convertible to T
2022-05-27 06:18:17 +00:00
template<typename U, class V = std::enable_if_t<std::is_convertible<std::decay_t<U>, T>::value>>
2022-03-29 19:31:45 +00:00
PResult(PResult<U> presult) :
2022-05-27 06:18:17 +00:00
tl::expected<T, SourceError>(presult ? PResult<T>(*presult) : PError(presult.error())) {}
2022-03-29 19:31:45 +00:00
};
2022-05-27 06:18:17 +00:00
/// Alternative to PResult that is returned from parseLibrary* functions
2022-03-29 19:31:45 +00:00
/// that only consume one token so they can fail without affecting
/// the parser state
template<typename T>
struct POptResult : public PResult<T> {
using PResult<T>::PResult;
std::optional<T> optional() {
if (*this) {
return **this;
} else {
return std::nullopt;
}
}
};
#endif // PRESULT_H