newline.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_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  constexpr bool try_parse(const void*, Reader reader)
40  {
41  return reader.peek() == Reader::encoding::eof()
42  || lexy::try_match_token(newline, reader);
43  }
44 
45  template <typename Context>
46  constexpr void cancel(Context&)
47  {}
48 
49  template <typename NextParser, typename Context, typename... Args>
50  LEXY_PARSER_FUNC bool finish(Context& context, Reader& reader, Args&&... args)
51  {
52  if (reader.peek() == Reader::encoding::eof())
53  {
54  auto pos = reader.position();
55  context.on(_ev::token{}, lexy::eof_token_kind, pos, pos);
56  return NextParser::parse(context, reader, LEXY_FWD(args)...);
57  }
58  else
59  {
60  // Note that we're re-doing the parsing for newline,
61  // this looks at most at two characters, so it doesn't really matter.
62  return lexy::parser_for<_nl, NextParser>::parse(context, reader, LEXY_FWD(args)...);
63  }
64  }
65  };
66 
67  template <typename NextParser>
68  struct p
69  {
70  template <typename Context, typename Reader, typename... Args>
71  LEXY_PARSER_FUNC static bool parse(Context& context, Reader& reader, Args&&... args)
72  {
73  return bp<Reader>{}.template finish<NextParser>(context, reader, LEXY_FWD(args)...);
74  }
75  };
76 };
77 
79 constexpr auto eol = _eol{};
80 } // namespace lexyd
81 
82 #endif // LEXY_DSL_NEWLINE_HPP_INCLUDED
83 
lexy::eof_token_kind
@ eof_token_kind
Definition: grammar.hpp:83
LEXY_CONSTEVAL
#define LEXY_CONSTEVAL
Definition: config.hpp:90
lexyd::_eol::bp::try_parse
constexpr bool try_parse(const void *, Reader reader)
Definition: newline.hpp:39
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:22
lexy
Definition: any_ref.hpp:12
lexyd::eol
constexpr auto eol
Matches the end of line (EOF or newline).
Definition: newline.hpp:79
lexyd::_eol::bp
Definition: newline.hpp:37
lexyd::_eol::p
Definition: newline.hpp:68
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:46
lexy::try_match_token
constexpr LEXY_FORCE_INLINE auto try_match_token(TokenRule, Reader &reader)
Definition: dsl/base.hpp:232
LEXY_DECAY_DECLTYPE
#define LEXY_DECAY_DECLTYPE(...)
Definition: config.hpp:26
lexy::expected_newline
Definition: newline.hpp:13
LEXY_PARSER_FUNC
#define LEXY_PARSER_FUNC
Definition: dsl/base.hpp:95
base.hpp
lexy::parse_events::token
Definition: dsl/base.hpp:44
lexyd::_eol::p::parse
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
Definition: newline.hpp:71
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:50
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 Mon Sep 16 2024 02:19:23