capture.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2023 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_DSL_CAPTURE_HPP_INCLUDED
5 #define LEXY_DSL_CAPTURE_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
9 #include <lexy/lexeme.hpp>
10 
11 namespace lexyd
12 {
13 template <typename Token>
14 struct _cap : _copy_base<Token>
15 {
16  template <typename Reader>
17  struct bp
18  {
19  typename Reader::iterator end;
20 
21  constexpr auto try_parse(const void*, const Reader& reader)
22  {
24  auto result = parser.try_parse(reader);
25  end = parser.end;
26  return result;
27  }
28 
29  template <typename Context>
30  constexpr void cancel(Context&)
31  {}
32 
33  template <typename NextParser, typename Context, typename... Args>
34  LEXY_PARSER_FUNC auto finish(Context& context, Reader& reader, Args&&... args)
35  {
36  auto begin = reader.position();
37 
38  context.on(_ev::token{}, Token{}, begin, end);
39  reader.set_position(end);
40 
42  return continuation::parse(context, reader, LEXY_FWD(args)...,
44  }
45  };
46 
47  template <typename NextParser>
48  struct p
49  {
50  template <typename Context, typename Reader, typename... Args>
51  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
52  {
53  auto begin = reader.position();
54  if (!Token::token_parse(context, reader))
55  return false;
56  auto end = reader.position();
57 
59  return continuation::parse(context, reader, LEXY_FWD(args)...,
61  }
62  };
63 };
64 
65 template <typename Rule>
66 struct _capr : _copy_base<Rule>
67 {
68  template <typename NextParser, typename... PrevArgs>
70  {
71  template <typename Context, typename Reader, typename... Args>
72  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader,
73  PrevArgs&&... prev_args, typename Reader::iterator begin,
74  Args&&... args)
75  {
77  return continuation::parse(context, reader, LEXY_FWD(prev_args)...,
78  lexy::lexeme(reader, begin), LEXY_FWD(args)...);
79  }
80  };
81 
82  template <typename Reader>
83  struct bp
84  {
86 
87  template <typename ControlBlock>
88  constexpr auto try_parse(const ControlBlock* cb, const Reader& reader)
89  {
90  return rule.try_parse(cb, reader);
91  }
92 
93  template <typename Context>
94  constexpr void cancel(Context& context)
95  {
96  rule.cancel(context);
97  }
98 
99  template <typename NextParser, typename Context, typename... Args>
100  LEXY_PARSER_FUNC auto finish(Context& context, Reader& reader, Args&&... args)
101  {
102  // Forward to the rule, but remember the current reader position.
103  using continuation = _pc<NextParser, Args...>;
104  return rule.template finish<continuation>(context, reader, LEXY_FWD(args)...,
105  reader.position());
106  }
107  };
108 
109  template <typename NextParser>
110  struct p
111  {
112  template <typename Context, typename Reader, typename... Args>
113  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
114  {
115  // Forward to the rule, but remember the current reader position.
116  using parser = lexy::parser_for<Rule, _pc<NextParser, Args...>>;
117  return parser::parse(context, reader, LEXY_FWD(args)..., reader.position());
118  }
119  };
120 };
121 
122 template <typename Production>
123 struct _prd;
124 
126 template <typename Token>
127 constexpr auto capture(Token)
128 {
129  static_assert(lexy::is_token_rule<Token>);
130  return _cap<Token>{};
131 }
132 
134 template <typename Production>
135 constexpr auto capture(_prd<Production>)
136 {
137  static_assert(lexy::is_token_production<Production>);
138  return _capr<_prd<Production>>{};
139 }
140 } // namespace lexyd
141 
142 #endif // LEXY_DSL_CAPTURE_HPP_INCLUDED
143 
lexyd::_cap::bp
Definition: capture.hpp:17
lexyd::_capr::bp::rule
lexy::branch_parser_for< Rule, Reader > rule
Definition: capture.hpp:85
lexyd::_cap::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: capture.hpp:51
lexyd::_capr::bp::cancel
constexpr void cancel(Context &context)
Definition: capture.hpp:94
lexy::branch_parser_for
typename BranchRule::template bp< Reader > branch_parser_for
Definition: dsl/base.hpp:103
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:22
lexyd::_prd
Definition: capture.hpp:123
lexyd::_cap::bp::try_parse
constexpr auto try_parse(const void *, const Reader &reader)
Definition: capture.hpp:21
lexyd::_cap::bp::cancel
constexpr void cancel(Context &)
Definition: capture.hpp:30
cx::end
constexpr auto end(const C &c) -> decltype(c.end())
Definition: wildcards.hpp:686
lexyd::_capr::bp
Definition: capture.hpp:83
lexyd::_capr::_pc
Definition: capture.hpp:69
lexy::parse
constexpr auto parse(const Input &input, const ErrorCallback &callback)
Parses the production into a value, invoking the callback on error.
Definition: parse.hpp:171
lexyd::_capr::bp::try_parse
constexpr auto try_parse(const ControlBlock *cb, const Reader &reader)
Definition: capture.hpp:88
lexyd::_cap::bp::end
Reader::iterator end
Definition: capture.hpp:19
lexeme.hpp
lexyd::_capr::_pc::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, PrevArgs &&... prev_args, typename Reader::iterator begin, Args &&... args)
Definition: capture.hpp:72
lexyd::_cap::p
Definition: capture.hpp:48
lexyd::_capr::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: capture.hpp:113
lexyd::_cap::bp::finish
LEXY_PARSER_FUNC auto finish(Context &context, Reader &reader, Args &&... args)
Definition: capture.hpp:34
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:95
cx::begin
constexpr auto begin(const C &c) -> decltype(c.begin())
Definition: wildcards.hpp:661
lexy::whitespace_parser
Definition: dsl/base.hpp:216
whitespace.hpp
base.hpp
lexy::parse_events::token
Definition: dsl/base.hpp:44
lexy::lexeme
Definition: lexeme.hpp:16
lexyd::_capr
Definition: capture.hpp:66
lexy::parser_for
typename Rule::template p< NextParser > parser_for
Definition: dsl/base.hpp:100
lexyd::_capr::bp::finish
LEXY_PARSER_FUNC auto finish(Context &context, Reader &reader, Args &&... args)
Definition: capture.hpp:100
lexyd::_cap
Definition: capture.hpp:14
lexyd::_capr::p
Definition: capture.hpp:110
lexy::token_parser_for
typename TokenRule::template tp< Reader > token_parser_for
Definition: dsl/base.hpp:229
lexyd
Definition: trace.hpp:22
lexyd::capture
constexpr auto capture(Token)
Captures whatever the token matches as a lexeme; does not include trailing whitespace.
Definition: capture.hpp:127
lexy::_detail::disable_whitespace_skipping
Definition: whitespace.hpp:170
lexyd::_copy_base
decltype(_copy_base_impl< Rule >()) _copy_base
Definition: dsl/base.hpp:91


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07