![]() |
|
|||
File indexing completed on 2025-09-17 08:40:35
0001 // Copyright (C) 2020 T. Zachary Laine 0002 // 0003 // Distributed under the Boost Software License, Version 1.0. (See 0004 // accompanying file LICENSE_1_0.txt or copy at 0005 // http://www.boost.org/LICENSE_1_0.txt) 0006 #ifndef BOOST_PARSER_PARSER_FWD_HPP 0007 #define BOOST_PARSER_PARSER_FWD_HPP 0008 0009 #include <boost/parser/config.hpp> 0010 #include <boost/parser/error_handling_fwd.hpp> 0011 0012 #include <any> 0013 #include <cstdint> 0014 #include <functional> 0015 #include <map> 0016 #include <memory> 0017 #include <optional> 0018 #include <variant> 0019 0020 0021 namespace boost::parser::detail { namespace text { 0022 struct null_sentinel_t; 0023 }} 0024 0025 namespace boost { namespace parser { 0026 0027 /** A sentinel type that compares equal to a pointer to a character value 0028 type, iff the pointer is null. */ 0029 using null_sentinel_t = boost::parser::detail::text::null_sentinel_t; 0030 0031 /** A variable template that indicates that type `T` is an optional-like 0032 type. */ 0033 template<typename T> 0034 constexpr bool enable_optional = false; 0035 0036 /** A variable template that indicates that type `T` is an variant-like 0037 type. */ 0038 template<typename T> 0039 constexpr bool enable_variant = false; 0040 0041 #ifndef BOOST_PARSER_DOXYGEN 0042 template<typename T> 0043 constexpr bool enable_optional<std::optional<T>> = true; 0044 template<typename... Ts> 0045 constexpr bool enable_variant<std::variant<Ts...>> = true; 0046 #endif 0047 0048 /** A type trait that evaluates to the attribute type for parser `Parser` 0049 used to parse range `R`, as if by calling `parse(r, parser)`, using 0050 some `R r` and `Parser parser`. Note that this implies that pointers 0051 to null-terminated strings are supported types for `R`. The result is 0052 not wrapped in a `std::optional` like the result of a call to 0053 `parse()` would be. If `Parser` produces no attribute, the result is 0054 the no-attribute sentinel type `none`. */ 0055 template<typename R, typename Parser> 0056 struct attribute; 0057 0058 /** An alias for `typename attribute<R, Parser>::type`. */ 0059 template<typename R, typename Parser> 0060 using attribute_t = typename attribute<R, Parser>::type; 0061 0062 /** Produces a `subrange` comprising the given pointer and 0063 `null_sentinel`. This should be used to make Null-Terminated Byte 0064 Strings ("NTBSs") compatible with ranges. */ 0065 template<typename CharT> 0066 constexpr auto null_term(CharT * ptr) 0067 { 0068 return BOOST_PARSER_SUBRANGE(ptr, detail::text::null_sentinel); 0069 } 0070 0071 namespace detail { 0072 template<typename T> 0073 constexpr bool is_optional_v = enable_optional<T>; 0074 0075 struct nope; 0076 0077 enum class flags : unsigned int { 0078 gen_attrs = 1 << 0, 0079 use_skip = 1 << 1, 0080 trace = 1 << 2, 0081 in_apply_parser = 1 << 3 0082 }; 0083 0084 struct symbol_table_trie_element 0085 { 0086 std::any trie_; 0087 bool has_case_folded_; 0088 }; 0089 0090 using symbol_table_tries_t = 0091 std::map<void *, symbol_table_trie_element, std::less<void *>>; 0092 0093 using pending_symtab_ops_visitor = std::function<void()>; 0094 struct pending_symtab_ops_entry 0095 { 0096 pending_symtab_ops_visitor visit_; 0097 // Contains std::vector<detail::symbol_table_operation<T>> (T is 0098 // known to visit_). 0099 std::any ops_; 0100 }; 0101 using pending_symbol_table_operations_t = std::map< 0102 void const *, 0103 pending_symtab_ops_entry, 0104 std::less<void const *>>; 0105 0106 template< 0107 bool DoTrace, 0108 bool UseCallbacks, 0109 typename Iter, 0110 typename Sentinel, 0111 typename ErrorHandler> 0112 inline auto make_context( 0113 Iter first, 0114 Sentinel last, 0115 bool & success, 0116 int & indent, 0117 ErrorHandler const & error_handler, 0118 nope &, 0119 symbol_table_tries_t & symbol_table_tries, 0120 pending_symbol_table_operations_t & 0121 pending_symbol_table_operations) noexcept; 0122 0123 struct skip_skipper; 0124 0125 struct char_subrange 0126 { 0127 char32_t lo_; 0128 char32_t hi_; 0129 }; 0130 0131 template<typename Tag> 0132 struct char_subranges 0133 {}; 0134 0135 struct hex_digit_subranges 0136 {}; 0137 struct control_subranges 0138 {}; 0139 0140 template<typename Tag> 0141 struct char_set 0142 {}; 0143 0144 struct punct_chars 0145 {}; 0146 struct symb_chars 0147 {}; 0148 struct lower_case_chars 0149 {}; 0150 struct upper_case_chars 0151 {}; 0152 } 0153 0154 /** Repeats the application of another parser `p` of type `Parser`, 0155 optionally applying another parser `d` of type `DelimiterParser` in 0156 between each pair of applications of `p`. The parse succeeds if `p` 0157 succeeds at least the minumum number of times, and `d` succeeds each 0158 time it is applied. The attribute produced is a sequence of the type 0159 of attribute produced by `Parser`. */ 0160 template< 0161 typename Parser, 0162 typename DelimiterParser = detail::nope, 0163 typename MinType = int64_t, 0164 typename MaxType = int64_t> 0165 struct repeat_parser; 0166 0167 /** Repeats the application of another parser `p` of type `Parser`, `[0, 0168 Inf)` times. The parse always succeeds. The attribute produced is a 0169 sequence of the type of attribute produced by `Parser`. */ 0170 template<typename Parser> 0171 struct zero_plus_parser; 0172 0173 /** Repeats the application of another parser `p` of type `Parser`, `[1, 0174 Inf)` times. The parse succeeds iff `p` succeeds at least once. The 0175 attribute produced is a sequence of the type of attribute produced by 0176 `Parser`. */ 0177 template<typename Parser> 0178 struct one_plus_parser; 0179 0180 /** Repeats the application of another parser `p` of type `Parser`, `[1, 0181 Inf)` times, applying a parser `d` of type `DelimiterParser` in 0182 between each pair of applications of `p`. The parse succeeds iff `p` 0183 succeeds at least once, and `d` succeeds each time it is applied. The 0184 attribute produced is a sequence of the type of attribute produced by 0185 `Parser`. */ 0186 template<typename Parser, typename DelimiterParser> 0187 struct delimited_seq_parser; 0188 0189 /** Repeats the application of another parser of type `Parser`, `[0, 1]` 0190 times. The parse always succeeds. The attribute produced is a 0191 `std::optional<T>`, where `T` is the type of attribute produced by 0192 `Parser`. */ 0193 template<typename Parser> 0194 struct opt_parser; 0195 0196 /** Applies each parser in `ParserTuple`, in order, stopping after the 0197 application of the first one that succeeds. The parse succeeds iff 0198 one of the sub-parsers succeeds. The attribute produced is a 0199 `std::variant` over the types of attribute produced by the parsers in 0200 `ParserTuple`. */ 0201 template<typename ParserTuple> 0202 struct or_parser; 0203 0204 /** Applies each parsers in `ParserTuple`, an any order, stopping after 0205 all of them have matched the input. The parse succeeds iff all the 0206 parsers match, regardless of the order in which they do. The 0207 attribute produced is a `parser::tuple` containing the attributes of 0208 the subparsers, in their order of the parsers' appearance in 0209 `ParserTuple`, not the order of the parsers' matches. It is an error 0210 to specialize `perm_parser` with a `ParserTuple` template parameter 0211 that includes an `eps_parser`. */ 0212 template<typename ParserTuple, typename DelimiterParser> 0213 struct perm_parser; 0214 0215 /** Applies each parser in `ParserTuple`, in order. The parse succeeds 0216 iff all of the sub-parsers succeed. The attribute produced is a 0217 `std::tuple` over the types of attribute produced by the parsers in 0218 `ParserTuple`. The BacktrackingTuple template parameter is a 0219 `parser::tuple` of `std::bool_constant` values. The `i`th such value 0220 indicates whether backtracking is allowed if the `i`th parser 0221 fails. */ 0222 template< 0223 typename ParserTuple, 0224 typename BacktrackingTuple, 0225 typename CombiningGroups> 0226 struct seq_parser; 0227 0228 /** Applies the given parser `p` of type `Parser` and an invocable `a` of 0229 type `Action`. `Action` shall model `semantic_action`, and `a` will 0230 only be invoked if `p` succeeds. The parse succeeds iff `p` succeeds. 0231 Produces no attribute. */ 0232 template<typename Parser, typename Action> 0233 struct action_parser; 0234 0235 /** Applies the given parser `p` of type `Parser`. The attribute produced 0236 by `p` is passed to the fiven invocable `f` of type `F`. `f` will 0237 only be invoked if `p` succeeds and sttributes are currently being 0238 generated. The parse succeeds iff `p` succeeds. The attribute 0239 produced is the the result of the call to `f`. */ 0240 template<typename Parser, typename F> 0241 struct transform_parser; 0242 0243 /** Applies the given parser `p` of type `Parser`. This parser produces 0244 no attribute, and suppresses the production of any attributes that 0245 would otherwise be produced by `p`. The parse succeeds iff `p` 0246 succeeds. */ 0247 template<typename Parser> 0248 struct omit_parser; 0249 0250 /** Applies the given parser `p` of type `Parser`; regardless of the 0251 attribute produced by `Parser`, this parser's attribute is equivalent 0252 to `_where(ctx)` within a semantic action on `p`. The parse succeeds 0253 iff `p` succeeds. */ 0254 template<typename Parser> 0255 struct raw_parser; 0256 0257 #if defined(BOOST_PARSER_DOXYGEN) || BOOST_PARSER_USE_CONCEPTS 0258 /** Applies the given parser `p` of type `Parser`. Regardless of the 0259 attribute produced by `Parser`, this parser's attribute is equivalent 0260 to `std::basic_string_view<char_type>` within a semantic action on 0261 `p`, where `char_type` is the type of character in the sequence being 0262 parsed. If the parsed range is transcoded, `char_type` will be the 0263 type being transcoded from. If the underlying range of `char_type` is 0264 non-contiguous, code using `string_view_parser` is ill-formed. The 0265 parse succeeds iff `p` succeeds. This parser is only available in 0266 C++20 and later. */ 0267 template<typename Parser> 0268 struct string_view_parser; 0269 #endif 0270 0271 /** Applies the given parser `p` of type `Parser`, disabling the current 0272 skipper in use, if any. The parse succeeds iff `p` succeeds. The 0273 attribute produced is the type of attribute produced by `Parser`. */ 0274 template<typename Parser> 0275 struct lexeme_parser; 0276 0277 /** Applies the given parser `p` of type `Parser`, enabling 0278 case-insensitive matching, based on Unicode case folding. The parse 0279 succeeds iff `p` succeeds. The attribute produced is the type of 0280 attribute produced by `Parser`. */ 0281 template<typename Parser> 0282 struct no_case_parser; 0283 0284 /** Applies the given parser `p` of type `Parser`, using a parser of type 0285 `SkipParser` as the skipper. The parse succeeds iff `p` succeeds. 0286 The attribute produced is the type of attribute produced by 0287 `Parser`. */ 0288 template<typename Parser, typename SkipParser = detail::nope> 0289 struct skip_parser; 0290 0291 /** Applies the given parser `p` of type `Parser`, producing no attributes 0292 and consuming no input. The parse succeeds iff `p`'s success is 0293 unequal to `FailOnMatch`. */ 0294 template<typename Parser, bool FailOnMatch> 0295 struct expect_parser; 0296 0297 /** Matches one of a set S of possible inputs, each of which is associated 0298 with an attribute value of type `T`, forming a symbol table. New 0299 elements and their associated attributes may be added to or removed 0300 from S dynamically, during parsing; any such changes are reverted at 0301 the end of parsing. The parse succeeds iff an element of S is 0302 matched. \see `symbols` */ 0303 template<typename T> 0304 struct symbol_parser; 0305 0306 /** Applies another parser `p`, associated with this parser via `TagType`. 0307 The attribute produced is `Attribute`. Both a default-constructed 0308 object of type `LocalState`, and a default-constructed object of type 0309 `ParamsTuple`, are added to the parse context before the associated 0310 parser is applied. The parse succeeds iff `p` succeeds. If 0311 `CanUseCallbacks` is `true`, and if this parser is used within a call 0312 to `callback_parse()`, the attribute is produced via callback; 0313 otherwise, the attribute is produced as normal (as a return value, or 0314 as an out-param). The rule may be constructed with user-friendly 0315 diagnostic text that will appear if the top-level parse is executed 0316 with `trace_mode == boost::parser::trace::on`. */ 0317 template< 0318 bool CanUseCallbacks, 0319 typename TagType, 0320 typename Attribute, 0321 typename LocalState, 0322 typename ParamsTuple> 0323 struct rule_parser; 0324 0325 /** Matches anything, and consumes no input. If `Predicate` is anything 0326 other than `detail::nope` (which it is by default), and `pred_(ctx)` 0327 evaluates to false, where `ctx` is the parser context, the parse 0328 fails. */ 0329 template<typename Predicate> 0330 struct eps_parser; 0331 0332 /** Matches only the end of input. Produces no attribute. */ 0333 struct eoi_parser; 0334 0335 /** Matches anything, consumes no input, and produces an attribute of type 0336 `RESOLVE(Attribute)`. */ 0337 template<typename Attribute> 0338 struct attr_parser; 0339 0340 /** A tag type that can be passed as the first parameter to `char_()` when 0341 the second parameter is a sorted, random access sequence that can be 0342 matched using a binary search.*/ 0343 struct sorted_t 0344 {}; 0345 0346 inline constexpr sorted_t sorted; 0347 0348 /** Matches a single code point. If `AttributeType` is not `void`, 0349 `AttributeType` is the attribute type produced; otherwise, the 0350 attribute type is the decayed type of the matched code point. The 0351 parse fails only if the parser is constructed with a specific set of 0352 expected code point values that does not include the matched code 0353 point. */ 0354 template<typename Expected, typename AttributeType = void> 0355 struct char_parser; 0356 0357 /** Matches a single code point that is equal to one of the code points 0358 associated with tag type `Tag`. This is used to create sets of 0359 characters for matching Unicode character classes like punctuation or 0360 lower case. Attribute type is the attribute type of the character 0361 being matched. */ 0362 template<typename Tag> 0363 struct char_set_parser; 0364 0365 /** Matches a single code point that falls into one of the subranges of 0366 code points associated with tag type `Tag`. This is used to create 0367 sets of characters for matching Unicode character classes like hex 0368 digits or control characters. Attribute type is the attribute type of 0369 the character being matched. */ 0370 template<typename Tag> 0371 struct char_subrange_parser; 0372 0373 /** Matches a single decimal digit code point, using the Unicode character 0374 class Hex_Digit. Attribute type is the attribute type of the 0375 character being matched. */ 0376 struct digit_parser; 0377 0378 /** Matches a particular string, delimited by an iterator sentinel pair; 0379 produces no attribute. */ 0380 template<typename StrIter, typename StrSentinel> 0381 struct string_parser; 0382 0383 /** Matches a string delimited by quotation marks; produces a 0384 `std::string` attribute. */ 0385 template< 0386 typename Quotes = detail::nope, 0387 typename Escapes = detail::nope, 0388 typename CharParser = char_parser<detail::nope>> 0389 struct quoted_string_parser; 0390 0391 /** Matches an end-of-line (`NewlinesOnly == true`), whitespace 0392 (`NewlinesOnly == false`), or (`NoNewlines == true`) blank (whitespace 0393 but not newline) code point, based on the Unicode definitions of each 0394 (also matches the two code points `"\r\n"`). Produces no 0395 attribute. */ 0396 template<bool NewlinesOnly, bool NoNewlines> 0397 struct ws_parser; 0398 0399 /** Matches the strings "true" and "false", producing an attribute of 0400 `true` or `false`, respectively, and fails on any other input. */ 0401 struct bool_parser; 0402 0403 /** Matches an unsigned number of radix `Radix`, of at least `MinDigits` 0404 and at most `MaxDigits`, producing an attribute of type `T`. Fails on 0405 any other input. The parse will also fail if `Expected` is anything 0406 but `detail::nope` (which it is by default), and the produced 0407 attribute is not equal to `expected_`. `Radix` must be in `[2, 0408 36]`. */ 0409 template< 0410 typename T, 0411 int Radix = 10, 0412 int MinDigits = 1, 0413 int MaxDigits = -1, 0414 typename Expected = detail::nope> 0415 struct uint_parser; 0416 0417 /** Matches a signed number of radix `Radix`, of at least `MinDigits` and 0418 at most `MaxDigits`, producing an attribute of type `T`. Fails on any 0419 other input. The parse will also fail if `Expected` is anything but 0420 `detail::nope` (which it is by default), and the produced 0421 attribute is not equal to `expected_`. `Radix` must be one of `2`, 0422 `8`, `10`, or `16`. */ 0423 template< 0424 typename T, 0425 int Radix = 10, 0426 int MinDigits = 1, 0427 int MaxDigits = -1, 0428 typename Expected = detail::nope> 0429 struct int_parser; 0430 0431 /** Matches a floating point number, producing an attribute of type 0432 `T`. */ 0433 template<typename T> 0434 struct float_parser; 0435 0436 /** Applies at most one of the parsers in `OrParser`. If `switch_value_` 0437 matches one or more of the values in the parsers in `OrParser`, the 0438 first such parser is applied, and the success or failure and attribute 0439 of the parse are those of the applied parser. Otherwise, the parse 0440 fails. */ 0441 template<typename SwitchValue, typename OrParser = detail::nope> 0442 struct switch_parser; 0443 0444 /** A wrapper for parsers that provides the operations that must be 0445 supported by all parsers (e.g. `operator>>()`). `GlobalState` is an 0446 optional state object that can be accessed within semantic actions via 0447 a call to `_globals()`. This global state object is ignored for all 0448 but the topmost parser; the topmost global state object is available 0449 in the semantic actions of all nested parsers. `ErrorHandler` is the 0450 type of the error handler to be used on parse failure. This handler 0451 is ignored on all but the topmost parser; the topmost parser's error 0452 handler is used for all errors encountered during parsing. */ 0453 template< 0454 typename Parser, 0455 typename GlobalState = detail::nope, 0456 typename ErrorHandler = default_error_handler> 0457 struct parser_interface; 0458 0459 using no_attribute = detail::nope; 0460 using no_local_state = detail::nope; 0461 using no_params = detail::nope; 0462 0463 /** A type used to declare named parsing rules. The `TagType` template 0464 parameter is used to associate a particular `rule` with the 0465 `rule_parser` used during parsing. */ 0466 template< 0467 typename TagType, 0468 typename Attribute = no_attribute, 0469 typename LocalState = no_local_state, 0470 typename ParamsTuple = no_params> 0471 struct rule; 0472 0473 /** A type used to declare named parsing rules that support reporting of 0474 attributes via callback. The `TagType` template parameter is used to 0475 associate a particular `rule` with the `rule_parser` used during 0476 parsing. */ 0477 template< 0478 typename TagType, 0479 typename Attribute = no_attribute, 0480 typename LocalState = no_local_state, 0481 typename ParamsTuple = no_params> 0482 struct callback_rule; 0483 0484 #ifdef BOOST_PARSER_DOXYGEN 0485 /** Returns a reference to the attribute(s) (i.e. return value) of the 0486 bottommost parser; multiple attributes will be stored within a 0487 `parser::tuple`. You may write to this value in a semantic action to 0488 control what attribute value(s) the associated parser produces. 0489 Returns `none` if the bottommost parser does produce an attribute. */ 0490 decltype(auto) _val(Context const & context); 0491 #endif 0492 0493 /** Returns a reference to the attribute or attributes already produced by 0494 the bottommost parser; multiple attributes will be stored within a 0495 `parser::tuple`. Returns `none` if the bottommost parser does produce 0496 an attribute. */ 0497 template<typename Context> 0498 decltype(auto) _attr(Context const & context); 0499 0500 /** Returns a `subrange` that describes the matched range of the 0501 bottommost parser. */ 0502 template<typename Context> 0503 decltype(auto) _where(Context const & context); 0504 0505 /** Returns an iterator to the beginning of the entire sequence being 0506 parsed. The effect of calling this within a semantic action 0507 associated with a skip-parser is undefined */ 0508 template<typename Context> 0509 decltype(auto) _begin(Context const & context); 0510 0511 /** Returns an iterator to the end of the entire sequence being parsed. */ 0512 template<typename Context> 0513 decltype(auto) _end(Context const & context); 0514 0515 /** Returns a reference to a `bool` that represents the success or failure 0516 of the bottommost parser. You can assign `false` to this within a 0517 semantic action to force a parser to fail its parse. */ 0518 template<typename Context> 0519 decltype(auto) _pass(Context const & context); 0520 0521 /** Returns a reference to one or more local values that the bottommost 0522 rule is declared to have; multiple values will be stored within a 0523 `parser::tuple`. Returns `none` if there is no bottommost rule, or if 0524 that rule has no locals. */ 0525 template<typename Context> 0526 decltype(auto) _locals(Context const & context); 0527 0528 /** Returns a reference to one or more parameters passed to the bottommost 0529 rule `r`, by using `r` as `r.with(param0, param1, ... paramN)`; 0530 multiple values will be stored within a `parser::tuple`. Returns 0531 `none` if there is no bottommost rule, or if that rule was not given 0532 any parameters. */ 0533 template<typename Context> 0534 decltype(auto) _params(Context const & context); 0535 0536 /** Returns a reference to the globals object associated with the 0537 top-level parser. Returns `none` if there is no associated globals 0538 object. */ 0539 template<typename Context> 0540 decltype(auto) _globals(Context const & context); 0541 0542 /** Returns a reference to the error handler object associated with the 0543 top-level parser. Returns `none` if there is no associated error 0544 handler. */ 0545 template<typename Context> 0546 decltype(auto) _error_handler(Context const & context); 0547 0548 /** Report that the error described in `message` occurred at `location`, 0549 using the context's error handler. */ 0550 #if BOOST_PARSER_USE_CONCEPTS 0551 template<std::forward_iterator I, typename Context> 0552 #else 0553 template<typename I, typename Context> 0554 #endif 0555 void _report_error( 0556 Context const & context, std::string_view message, I location); 0557 0558 /** Report that the error described in `message` occurred at 0559 `_where(context).begin()`, using the context's error handler. */ 0560 template<typename Context> 0561 void _report_error(Context const & context, std::string_view message); 0562 0563 /** Report that the warning described in `message` occurred at `location`, 0564 using the context's error handler. */ 0565 #if BOOST_PARSER_USE_CONCEPTS 0566 template<std::forward_iterator I, typename Context> 0567 #else 0568 template<typename I, typename Context> 0569 #endif 0570 void _report_warning( 0571 Context const & context, std::string_view message, I location); 0572 0573 /** Report that the warning described in `message` occurred at 0574 `_where(context).begin()`, using the context's error handler. */ 0575 template<typename Context> 0576 void _report_warning(Context const & context, std::string_view message); 0577 0578 }} 0579 0580 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |