context_identifier.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2024 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_DSL_CONTEXT_IDENTIFIER_HPP_INCLUDED
5 #define LEXY_DSL_CONTEXT_IDENTIFIER_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
8 #include <lexy/dsl/capture.hpp>
10 
11 namespace lexy
12 {
14 {
15  static LEXY_CONSTEVAL auto name()
16  {
17  return "different identifier";
18  }
19 };
20 } // namespace lexy
21 
22 namespace lexyd
23 {
24 template <typename Id, typename Reader>
26 
27 template <typename Id>
29 {
30  template <typename NextParser>
31  struct p
32  {
33  template <typename Context, typename Reader, typename... Args>
34  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
35  {
36  _ctx_id<Id, Reader> var({});
37  var.link(context);
38  auto result = NextParser::parse(context, reader, LEXY_FWD(args)...);
39  var.unlink(context);
40  return result;
41  }
42  };
43 };
44 
45 template <typename Id, typename Identifier>
47 {
48  template <typename NextParser>
49  struct _pc
50  {
51  template <typename Context, typename Reader, typename... Args>
52  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
53  {
54  // The last argument will be a lexeme.
55  _ctx_id<Id, Reader>::get(context.control_block) = (args, ...);
56  return NextParser::parse(context, reader, LEXY_FWD(args)...);
57  }
58  };
59  template <typename Reader>
61  template <typename NextParser>
63 };
64 
65 template <typename Id, typename Identifier, typename Tag>
67 {
68  // We only need the pattern:
69  // We don't want a value and don't need to check for reserved identifiers,
70  // as it needs to match a previously parsed identifier, which wasn't reserved.
71  using _pattern = decltype(Identifier{}.pattern());
72 
73  template <typename Reader>
74  struct bp
75  {
76  typename Reader::marker end;
77 
78  template <typename ControlBlock>
79  constexpr bool try_parse(const ControlBlock* cb, const Reader& reader)
80  {
81  // Parse the pattern.
83  if (!parser.try_parse(reader))
84  return false;
85  end = parser.end;
86 
87  // The two lexemes need to be equal.
88  auto lexeme = lexy::lexeme<Reader>(reader.position(), end.position());
90  }
91 
92  template <typename Context>
93  constexpr void cancel(Context&)
94  {}
95 
96  template <typename NextParser, typename Context, typename... Args>
97  LEXY_PARSER_FUNC bool finish(Context& context, Reader& reader, Args&&... args)
98  {
99  // Finish parsing the token.
100  context.on(_ev::token{}, lexy::identifier_token_kind, reader.position(),
101  end.position());
102  reader.reset(end);
104  LEXY_FWD(args)...);
105  }
106  };
107 
108  template <typename NextParser>
109  struct p
110  {
111  template <typename... PrevArgs>
112  struct _cont
113  {
114  template <typename Context, typename Reader>
115  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, PrevArgs&&... args,
117  {
118  if (!lexy::_detail::equal_lexemes(_ctx_id<Id, Reader>::get(context.control_block),
119  lexeme))
120  {
121  // The lexemes weren't equal.
123  auto err = lexy::error<Reader, tag>(lexeme.begin(), lexeme.end());
124  context.on(_ev::error{}, err);
125 
126  // But we can trivially recover.
127  }
128 
129  // Continue parsing with the symbol value.
130  return NextParser::parse(context, reader, LEXY_FWD(args)...);
131  }
132  };
133 
134  template <typename Context, typename Reader, typename... Args>
135  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
136  {
137  // Capture the pattern and continue with special continuation.
138  return lexy::parser_for<_cap<_pattern>, _cont<Args...>>::parse(context, reader,
139  LEXY_FWD(args)...);
140  }
141  };
142 
143  template <typename Error>
145 };
146 } // namespace lexyd
147 
148 namespace lexyd
149 {
150 template <typename Id, typename Identifier>
152 {
153  constexpr auto create() const
154  {
155  return _ctx_icreate<Id>{};
156  }
157 
158  constexpr auto capture() const
159  {
160  return _ctx_icap<Id, Identifier>{};
161  }
162 
163  constexpr auto rematch() const
164  {
166  }
167 };
168 
170 template <typename Id, typename Leading, typename Trailing, typename... Reserved>
172 {
173  return _ctx_id_dsl<Id, _id<Leading, Trailing, Reserved...>>{};
174 }
175 } // namespace lexyd
176 
177 #endif // LEXY_DSL_CONTEXT_IDENTIFIER_HPP_INCLUDED
178 
LEXY_CONSTEVAL
#define LEXY_CONSTEVAL
Definition: config.hpp:98
lexyd::branch_base
Definition: grammar.hpp:20
lexyd::_ctx_id_dsl::create
constexpr auto create() const
Definition: context_identifier.hpp:153
lexyd::_ctx_icap
Definition: context_identifier.hpp:46
lexyd::_ctx_irem::p
Definition: context_identifier.hpp:109
lexyd::_ctx_irem::bp
Definition: context_identifier.hpp:74
lexy::_detail::parse_context_var::get
static constexpr T & get(const ControlBlock *cb)
Definition: action/base.hpp:54
lexyd::_ctx_irem::error
static constexpr _ctx_irem< Id, Identifier, Error > error
Definition: context_identifier.hpp:144
lexyd::_id
Definition: identifier.hpp:142
lexyd::_ctx_icap::p
lexy::parser_for< Identifier, _pc< NextParser > > p
Definition: context_identifier.hpp:62
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexy::_detail::type_or
std::conditional_t< std::is_void_v< T >, Fallback, T > type_or
Definition: config.hpp:64
lexyd::_ctx_irem::bp::cancel
constexpr void cancel(Context &)
Definition: context_identifier.hpp:93
lexyd::_ctx_irem::p::_cont::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, PrevArgs &&... args, lexy::lexeme< Reader > lexeme)
Definition: context_identifier.hpp:115
lexy::different_identifier
Definition: context_identifier.hpp:13
lexyd::_ctx_irem::bp::end
Reader::marker end
Definition: context_identifier.hpp:76
lexy
Definition: any_ref.hpp:12
lexyd::_ctx_irem::bp::finish
LEXY_PARSER_FUNC bool finish(Context &context, Reader &reader, Args &&... args)
Definition: context_identifier.hpp:97
lexy::error
Generic failure.
Definition: error.hpp:14
lexyd::_ctx_irem
Definition: context_identifier.hpp:66
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
capture.hpp
lexy::parse_events::error
Definition: dsl/base.hpp:68
lexyd::_ctx_irem::bp::try_parse
constexpr bool try_parse(const ControlBlock *cb, const Reader &reader)
Definition: context_identifier.hpp:79
lexy::_detail::automatic_ws_parser::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: whitespace.hpp:178
lexy::different_identifier::name
static LEXY_CONSTEVAL auto name()
Definition: context_identifier.hpp:15
lexyd::_ctx_irem::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: context_identifier.hpp:135
lexy::_detail::parse_context_var_base::link
constexpr void link(Context &context)
Definition: action/base.hpp:27
lexyd::rule_base
Definition: grammar.hpp:17
lexyd::_ctx_irem::_pattern
decltype(Identifier{}.pattern()) _pattern
Definition: context_identifier.hpp:71
lexy::_detail::parse_context_var
Definition: action/base.hpp:43
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:108
base.hpp
lexyd::context_identifier
constexpr auto context_identifier(_id< Leading, Trailing, Reserved... >)
Declares a context variable that stores one instance of the given identifier.
Definition: context_identifier.hpp:171
lexyd::_ctx_icap::_pc::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: context_identifier.hpp:52
lexy::parse_events::token
Definition: dsl/base.hpp:57
lexyd::_ctx_icap::_pc
Definition: context_identifier.hpp:49
lexy::lexeme
Definition: lexeme.hpp:16
lexy::identifier_token_kind
@ identifier_token_kind
Definition: grammar.hpp:94
lexyd::_ctx_icreate::p
Definition: context_identifier.hpp:31
lexy::continuation_branch_parser
A branch parser that parses a branch rule but with a special continuation.
Definition: dsl/base.hpp:147
lexy::lexeme
lexeme(const Reader &, typename Reader::iterator) -> lexeme< typename Reader::canonical_reader >
lexyd::_ctx_irem::p::_cont
Definition: context_identifier.hpp:112
lexy::parser_for
typename Rule::template p< NextParser > parser_for
Definition: dsl/base.hpp:113
lexyd::_ctx_id_dsl::capture
constexpr auto capture() const
Definition: context_identifier.hpp:158
lexyd::_ctx_icreate
Definition: context_identifier.hpp:28
lexyd::_ctx_id_dsl::rematch
constexpr auto rematch() const
Definition: context_identifier.hpp:163
identifier.hpp
lexy::_detail::equal_lexemes
constexpr bool equal_lexemes(lexeme< Reader > lhs, lexeme< Reader > rhs)
Definition: lexeme.hpp:85
lexyd::_ctx_icreate::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: context_identifier.hpp:34
lexyd::_ctx_id_dsl
Definition: context_identifier.hpp:151
lexy::token_parser_for
typename TokenRule::template tp< Reader > token_parser_for
Definition: dsl/base.hpp:242
lexyd
Definition: trace.hpp:22


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16