File indexing completed on 2025-01-31 10:01:57
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_SPIRIT_EXCEPTIONS_HPP
0009 #define BOOST_SPIRIT_EXCEPTIONS_HPP
0010
0011 #include <boost/config.hpp>
0012 #include <boost/throw_exception.hpp>
0013 #include <boost/spirit/home/classic/namespace.hpp>
0014 #include <boost/spirit/home/classic/core/parser.hpp>
0015 #include <boost/spirit/home/classic/core/composite/composite.hpp>
0016 #include <exception>
0017
0018 #include <boost/spirit/home/classic/error_handling/exceptions_fwd.hpp>
0019
0020 namespace boost { namespace spirit {
0021
0022 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 class BOOST_SYMBOL_VISIBLE parser_error_base : public std::exception
0038 {
0039 protected:
0040
0041 parser_error_base() {}
0042 virtual ~parser_error_base() BOOST_NOEXCEPT_OR_NOTHROW {}
0043
0044 public:
0045
0046 parser_error_base(parser_error_base const& rhs)
0047 : std::exception(rhs) {}
0048 parser_error_base& operator=(parser_error_base const&)
0049 {
0050 return *this;
0051 }
0052 };
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 template <typename ErrorDescrT, typename IteratorT>
0069 struct parser_error : public parser_error_base
0070 {
0071 typedef ErrorDescrT error_descr_t;
0072 typedef IteratorT iterator_t;
0073
0074 parser_error(IteratorT where_, ErrorDescrT descriptor_)
0075 : where(where_), descriptor(descriptor_) {}
0076
0077 parser_error(parser_error const& rhs)
0078 : parser_error_base(rhs)
0079 , where(rhs.where), descriptor(rhs.descriptor) {}
0080
0081 parser_error&
0082 operator=(parser_error const& rhs)
0083 {
0084 where = rhs.where;
0085 descriptor = rhs.descriptor;
0086 return *this;
0087 }
0088
0089 virtual
0090 ~parser_error() BOOST_NOEXCEPT_OR_NOTHROW {}
0091
0092 virtual const char*
0093 what() const BOOST_NOEXCEPT_OR_NOTHROW
0094 {
0095 return "BOOST_SPIRIT_CLASSIC_NS::parser_error";
0096 }
0097
0098 IteratorT where;
0099 ErrorDescrT descriptor;
0100 };
0101
0102
0103 template <typename ErrorDescrT, typename IteratorT>
0104 inline void
0105 throw_(IteratorT where, ErrorDescrT descriptor)
0106 {
0107 boost::throw_exception(
0108 parser_error<ErrorDescrT, IteratorT>(where, descriptor));
0109 }
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121 template <typename ErrorDescrT, typename ParserT>
0122 struct assertive_parser
0123 : public unary<ParserT, parser<assertive_parser<ErrorDescrT, ParserT> > >
0124 {
0125 typedef assertive_parser<ErrorDescrT, ParserT> self_t;
0126 typedef unary<ParserT, parser<self_t> > base_t;
0127 typedef unary_parser_category parser_category_t;
0128
0129 assertive_parser(ParserT const& parser, ErrorDescrT descriptor_)
0130 : base_t(parser), descriptor(descriptor_) {}
0131
0132 template <typename ScannerT>
0133 struct result
0134 {
0135 typedef typename parser_result<ParserT, ScannerT>::type type;
0136 };
0137
0138 template <typename ScannerT>
0139 typename parser_result<self_t, ScannerT>::type
0140 parse(ScannerT const& scan) const
0141 {
0142 typedef typename parser_result<ParserT, ScannerT>::type result_t;
0143
0144 result_t hit = this->subject().parse(scan);
0145 if (!hit)
0146 {
0147 throw_(scan.first, descriptor);
0148 }
0149 return hit;
0150 }
0151
0152 ErrorDescrT descriptor;
0153 };
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184 template <typename ErrorDescrT>
0185 struct assertion
0186 {
0187 assertion(ErrorDescrT descriptor_)
0188 : descriptor(descriptor_) {}
0189
0190 template <typename ParserT>
0191 assertive_parser<ErrorDescrT, ParserT>
0192 operator()(ParserT const& parser) const
0193 {
0194 return assertive_parser<ErrorDescrT, ParserT>(parser, descriptor);
0195 }
0196
0197 ErrorDescrT descriptor;
0198 };
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217 template <typename T>
0218 struct error_status
0219 {
0220 enum result_t { fail, retry, accept, rethrow };
0221
0222 error_status(
0223 result_t result_ = fail,
0224 std::ptrdiff_t length_ = -1,
0225 T const& value_ = T())
0226 : result(result_), length(length_), value(value_) {}
0227
0228 result_t result;
0229 std::ptrdiff_t length;
0230 T value;
0231 };
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252 namespace impl
0253 {
0254 template <typename RT, typename ParserT, typename ScannerT>
0255 RT fallback_parser_parse(ParserT const& p, ScannerT const& scan);
0256 }
0257
0258 template <typename ErrorDescrT, typename ParserT, typename HandlerT>
0259 struct fallback_parser
0260 : public unary<ParserT,
0261 parser<fallback_parser<ErrorDescrT, ParserT, HandlerT> > >
0262 {
0263 typedef fallback_parser<ErrorDescrT, ParserT, HandlerT>
0264 self_t;
0265 typedef ErrorDescrT
0266 error_descr_t;
0267 typedef unary<ParserT, parser<self_t> >
0268 base_t;
0269 typedef unary_parser_category
0270 parser_category_t;
0271
0272 fallback_parser(ParserT const& parser, HandlerT const& handler_)
0273 : base_t(parser), handler(handler_) {}
0274
0275 template <typename ScannerT>
0276 struct result
0277 {
0278 typedef typename parser_result<ParserT, ScannerT>::type type;
0279 };
0280
0281 template <typename ScannerT>
0282 typename parser_result<self_t, ScannerT>::type
0283 parse(ScannerT const& scan) const
0284 {
0285 typedef typename parser_result<self_t, ScannerT>::type result_t;
0286 return impl::fallback_parser_parse<result_t>(*this, scan);
0287 }
0288
0289 HandlerT handler;
0290 };
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316 template <typename ErrorDescrT, typename ParserT>
0317 struct guard_gen : public unary<ParserT, nil_t>
0318 {
0319 typedef guard<ErrorDescrT> parser_generator_t;
0320 typedef unary_parser_category parser_category_t;
0321
0322 guard_gen(ParserT const& p)
0323 : unary<ParserT, nil_t>(p) {}
0324
0325 template <typename HandlerT>
0326 fallback_parser<ErrorDescrT, ParserT, HandlerT>
0327 operator[](HandlerT const& handler) const
0328 {
0329 return fallback_parser<ErrorDescrT, ParserT, HandlerT>
0330 (this->subject(), handler);
0331 }
0332 };
0333
0334 template <typename ErrorDescrT>
0335 struct guard
0336 {
0337 template <typename ParserT>
0338 struct result
0339 {
0340 typedef guard_gen<ErrorDescrT, ParserT> type;
0341 };
0342
0343 template <typename ParserT>
0344 static guard_gen<ErrorDescrT, ParserT>
0345 generate(ParserT const& parser)
0346 {
0347 return guard_gen<ErrorDescrT, ParserT>(parser);
0348 }
0349
0350 template <typename ParserT>
0351 guard_gen<ErrorDescrT, ParserT>
0352 operator()(ParserT const& parser) const
0353 {
0354 return guard_gen<ErrorDescrT, ParserT>(parser);
0355 }
0356 };
0357
0358 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
0359
0360 }}
0361
0362 #include <boost/spirit/home/classic/error_handling/impl/exceptions.ipp>
0363 #endif
0364