newline.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_NEWLINE_HPP_INCLUDED
5 #define LEXY_DSL_NEWLINE_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
8 #include <lexy/dsl/literal.hpp>
9 #include <lexy/dsl/token.hpp>
10 
11 namespace lexy
12 {
14 {
15  static LEXY_CONSTEVAL auto name()
16  {
17  return "expected newline";
18  }
19 };
20 } // namespace lexy
21 
22 namespace lexyd
23 {
24 struct _nl
25 : LEXY_DECAY_DECLTYPE(literal_set(LEXY_LIT("\n"), LEXY_LIT("\r\n")).error<lexy::expected_newline>)
26 {};
27 
29 constexpr auto newline = _nl{};
30 } // namespace lexyd
31 
32 namespace lexyd
33 {
34 struct _eol : branch_base
35 {
36  template <typename Reader>
37  struct bp
38  {
39  static_assert(lexy::is_char_encoding<typename Reader::encoding>);
40 
41  constexpr bool try_parse(const void*, Reader reader)
42  {
43  return reader.peek() == Reader::encoding::eof()
44  || lexy::try_match_token(newline, reader);
45  }
46 
47  template <typename Context>
48  constexpr void cancel(Context&)
49  {}
50 
51  template <typename NextParser, typename Context, typename... Args>
52  LEXY_PARSER_FUNC bool finish(Context& context, Reader& reader, Args&&... args)
53  {
54  if (reader.peek() == Reader::encoding::eof())
55  {
56  auto pos = reader.position();
57  context.on(_ev::token{}, lexy::eof_token_kind, pos, pos);
58  return NextParser::parse(context, reader, LEXY_FWD(args)...);
59  }
60  else
61  {
62  // Note that we're re-doing the parsing for newline,
63  // this looks at most at two characters, so it doesn't really matter.
64  return lexy::parser_for<_nl, NextParser>::parse(context, reader, LEXY_FWD(args)...);
65  }
66  }
67  };
68 
69  template <typename NextParser>
70  struct p
71  {
72  template <typename Context, typename Reader, typename... Args>
73  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
74  {
75  static_assert(lexy::is_char_encoding<typename Reader::encoding>);
76  return bp<Reader>{}.template finish<NextParser>(context, reader, LEXY_FWD(args)...);
77  }
78  };
79 };
80 
82 constexpr auto eol = _eol{};
83 } // namespace lexyd
84 
85 #endif // LEXY_DSL_NEWLINE_HPP_INCLUDED
86 
lexy::eof_token_kind
@ eof_token_kind
Definition: grammar.hpp:92
LEXY_CONSTEVAL
#define LEXY_CONSTEVAL
Definition: config.hpp:98
lexyd::_eol::bp::try_parse
constexpr bool try_parse(const void *, Reader reader)
Definition: newline.hpp:41
lexyd::branch_base
Definition: grammar.hpp:20
token.hpp
literal.hpp
lexyd::_nl
Definition: newline.hpp:24
lexyd::_eol
Definition: newline.hpp:34
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexy
Definition: any_ref.hpp:12
lexyd::eol
constexpr auto eol
Matches the end of line (EOF or newline).
Definition: newline.hpp:82
lexyd::_eol::bp
Definition: newline.hpp:37
lexyd::_eol::p
Definition: newline.hpp:70
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::_eol::bp::cancel
constexpr void cancel(Context &)
Definition: newline.hpp:48
lexy::try_match_token
constexpr LEXY_FORCE_INLINE auto try_match_token(TokenRule, Reader &reader)
Definition: dsl/base.hpp:245
LEXY_DECAY_DECLTYPE
#define LEXY_DECAY_DECLTYPE(...)
Definition: config.hpp:34
lexy::expected_newline
Definition: newline.hpp:13
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:108
base.hpp
lexy::parse_events::token
Definition: dsl/base.hpp:57
lexyd::_eol::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: newline.hpp:73
lexyd::newline
constexpr auto newline
Matches a newline character.
Definition: newline.hpp:29
lexyd::_eol::bp::finish
LEXY_PARSER_FUNC bool finish(Context &context, Reader &reader, Args &&... args)
Definition: newline.hpp:52
lexy::expected_newline::name
static LEXY_CONSTEVAL auto name()
Definition: newline.hpp:15
lexyd
Definition: trace.hpp:22
lexyd::eof
constexpr auto eof
Matches EOF.
Definition: eof.hpp:72


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