any_types.hpp
Go to the documentation of this file.
1 /* Copyright (C) 2022-24 Davide Faconti - All Rights Reserved
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #pragma once
14 
15 #include "lexy/action/parse.hpp"
16 #include "lexy/callback.hpp"
17 #include "lexy/dsl.hpp"
19 
21 
22 namespace BT::Grammar
23 {
24 namespace dsl = lexy::dsl;
25 
26 struct _xid_start_character : lexyd::char_class_base<_xid_start_character>
27 {
29  {
30  return "code-point.BT-start-character";
31  }
32 
34  {
36  result.insert('a', 'z');
37  result.insert('A', 'Z');
38  result.insert('_');
39  result.insert('@');
40  return result;
41  }
42 
44  {
45  // underscore handled as part of ASCII.
46  return lexy::_detail::code_point_has_properties<LEXY_UNICODE_PROPERTY(xid_start)>(cp);
47  }
48 
49  template <typename Encoding>
51  {
52  return lexyd::ascii::_alphau::template char_class_match_swar<Encoding>(c);
53  }
54 };
55 inline constexpr auto xid_start_character = _xid_start_character{};
56 
57 // A Unicode-aware identifier.
58 struct Name
59 {
60  static constexpr auto rule =
62 
63  static constexpr auto value = lexy::as_string<std::string>;
64 };
65 
66 //----------
68 {
69  struct integer
70  {
71  static constexpr auto rule = dsl::sign + dsl::integer<int64_t>;
72  static constexpr auto value = lexy::as_integer<int64_t>;
73  };
74 
76  {
77  static constexpr auto name = "invalid suffix on integer literal";
78  };
79 
80  static constexpr auto rule = [] {
81  auto hex_integer =
82  (LEXY_LIT("0x") | LEXY_LIT("0X")) >> dsl::integer<int64_t, dsl::hex>;
83  auto regular_integer =
84  dsl::peek(dsl::lit_c<'-'> / dsl::lit_c<'+'> / dsl::digit<>) >> dsl::p<integer>;
85  auto suffix_error =
87  .error<invalid_suffix>;
88  return (hex_integer | regular_integer) >> suffix_error;
89  }();
90 
91  static constexpr auto value = lexy::construct<int64_t>;
92 };
93 
94 //----------
96 {
98  {
99  static constexpr auto name = "invalid suffix on double literal";
100  };
101 
102  static constexpr auto rule = [] {
103  auto integer_part = dsl::sign + dsl::digits<>;
104  auto fraction = dsl::period >> dsl::digits<>;
105  auto exponent = (dsl::lit_c<'e'> / dsl::lit_c<'E'>) >> (dsl::sign + dsl::digits<>);
106 
107  auto suffix_error =
109  .error<invalid_suffix>;
110 
111  auto real_part = (fraction >> dsl::if_(exponent) | exponent) >> suffix_error;
112  auto real_number = dsl::token(integer_part + real_part);
113  return dsl::capture(real_number);
114  }();
115 
116  static constexpr auto value =
117  lexy::as_string<std::string> |
118  lexy::callback<BT::Any>([](std::string&& str) { return BT::Any(std::stod(str)); });
119 };
120 
121 //----------
122 //struct Variable : lexy::token_production
123 //{
124 // static constexpr auto rule = dsl::identifier(dsl::unicode::xid_start_underscore, dsl::unicode::xid_continue);
125 // static constexpr auto value = lexy::as_string<std::string>;
126 //};
127 
128 //----------
130 {
131  static constexpr auto rule =
133  static constexpr auto value = lexy::as_string<std::string>;
134 };
135 
136 //----------
138 {
139  struct True
140  {
141  static constexpr auto rule = LEXY_LIT("true");
142  static constexpr auto value = lexy::constant(1);
143  };
144  struct False
145  {
146  static constexpr auto rule = LEXY_LIT("false");
147  static constexpr auto value = lexy::constant(0);
148  };
149 
150  static constexpr auto rule = dsl::p<True> | dsl::p<False>;
151  static constexpr auto value = lexy::construct<BT::Any>;
152 };
153 
154 //----------
156 {
157  static constexpr auto rule =
158  dsl::p<BooleanLiteral> | dsl::p<StringLiteral> | dsl::p<Real> | dsl::p<Integer>;
159  static constexpr auto value = lexy::construct<BT::Any>;
160 };
161 
162 } // namespace BT::Grammar
lexyd::peek_not
constexpr auto peek_not(Rule)
Checks if at this reader position, the rule would not match.
Definition: peek.hpp:173
BT::Grammar::Real::invalid_suffix
Definition: any_types.hpp:97
LEXY_CONSTEVAL
#define LEXY_CONSTEVAL
Definition: config.hpp:90
BT::Grammar::Real::invalid_suffix::name
static constexpr auto name
Definition: any_types.hpp:99
lexyd::token
constexpr auto token(Rule)
Turns the arbitrary rule into a token by matching it without producing any values.
Definition: dsl/token.hpp:214
lexyd::peek
constexpr auto peek(Rule)
Definition: peek.hpp:166
BT::Grammar::AnyValue
Definition: any_types.hpp:155
BT::Grammar::_xid_start_character
Definition: any_types.hpp:26
BT::Any
Definition: safe_any.hpp:36
lexyd::quoted
constexpr auto quoted
Definition: delimited.hpp:340
LEXY_LIT
#define LEXY_LIT(Str)
Definition: literal.hpp:390
BT::Grammar::Integer
Definition: any_types.hpp:67
BT::Grammar::Integer::integer::rule
static constexpr auto rule
Definition: any_types.hpp:71
BT::Grammar::Real::rule
static constexpr auto rule
Definition: any_types.hpp:102
lexyd::ascii::alpha_underscore
constexpr auto alpha_underscore
Definition: ascii.hpp:226
BT::Grammar::BooleanLiteral::True
Definition: any_types.hpp:139
BT::Grammar::Real
Definition: any_types.hpp:95
BT::Grammar::AnyValue::rule
static constexpr auto rule
Definition: any_types.hpp:157
lexy::_detail::swar_int
std::uintmax_t swar_int
Definition: swar.hpp:20
BT::Grammar::Integer::invalid_suffix
Definition: any_types.hpp:75
callback.hpp
lexy::constant
LEXY_CONSTEVAL auto constant(Arg &&value)
Creates a callback that produces the given value without accepting arguments.
Definition: constant.hpp:26
BT::Grammar::StringLiteral::rule
static constexpr auto rule
Definition: any_types.hpp:131
lexy::_detail::ascii_set::insert
constexpr void insert(int c)
Definition: char_class.hpp:54
LEXY_UNICODE_CONSTEXPR
#define LEXY_UNICODE_CONSTEXPR
Definition: code_point.hpp:18
BT::Grammar::BooleanLiteral::False
Definition: any_types.hpp:144
BT::Grammar::StringLiteral::value
static constexpr auto value
Definition: any_types.hpp:133
lexyd::ascii::alpha
constexpr auto alpha
Definition: ascii.hpp:201
BT::Grammar::Integer::rule
static constexpr auto rule
Definition: any_types.hpp:80
dsl.hpp
parse.hpp
BT::Grammar::Real::value
static constexpr auto value
Definition: any_types.hpp:116
lexyd::single_quoted
constexpr auto single_quoted
Definition: delimited.hpp:343
BT::Grammar::_xid_start_character::char_class_match_cp
static LEXY_UNICODE_CONSTEXPR bool char_class_match_cp(char32_t cp)
Definition: any_types.hpp:43
BT::Grammar::Integer::invalid_suffix::name
static constexpr auto name
Definition: any_types.hpp:77
BT::Grammar::Name
Definition: any_types.hpp:58
BT::Grammar::BooleanLiteral::rule
static constexpr auto rule
Definition: any_types.hpp:150
BT::Grammar::_xid_start_character::char_class_name
static LEXY_CONSTEVAL auto char_class_name()
Definition: any_types.hpp:28
BT::Grammar::Name::rule
static constexpr auto rule
Definition: any_types.hpp:60
BT::Grammar::_xid_start_character::char_class_ascii
static LEXY_CONSTEVAL auto char_class_ascii()
Definition: any_types.hpp:33
lexy::token_production
Definition: grammar.hpp:142
report_error.hpp
BT::Grammar::BooleanLiteral::value
static constexpr auto value
Definition: any_types.hpp:151
BT::Grammar::Integer::value
static constexpr auto value
Definition: any_types.hpp:91
BT::Grammar::BooleanLiteral::True::value
static constexpr auto value
Definition: any_types.hpp:142
lexyd::ascii::character
constexpr auto character
Definition: ascii.hpp:471
safe_any.hpp
BT::Grammar::BooleanLiteral::True::rule
static constexpr auto rule
Definition: any_types.hpp:141
lexyd::char_class_base
Definition: char_class.hpp:170
BT::Grammar::BooleanLiteral
Definition: any_types.hpp:137
BT::Grammar::xid_start_character
constexpr auto xid_start_character
Definition: any_types.hpp:55
lexy::_unicode_db::xid_continue
@ xid_continue
Definition: unicode_database.hpp:75
BT::Grammar::Integer::integer
Definition: any_types.hpp:69
lexyd::identifier
constexpr auto identifier(CharClass)
Creates an identifier that consists of one or more of the given characters.
Definition: identifier.hpp:287
lexyd::if_
constexpr auto if_(Branch)
If the branch condition matches, matches the branch then.
Definition: if.hpp:37
BT::Grammar::AnyValue::value
static constexpr auto value
Definition: any_types.hpp:159
BT::Grammar::Name::value
static constexpr auto value
Definition: any_types.hpp:63
BT::Grammar
Definition: any_types.hpp:22
BT::Grammar::_xid_start_character::char_class_match_swar
static constexpr auto char_class_match_swar(lexy::_detail::swar_int c)
Definition: any_types.hpp:50
BT::Grammar::BooleanLiteral::False::value
static constexpr auto value
Definition: any_types.hpp:147
BT::Grammar::StringLiteral
Definition: any_types.hpp:129
lexyd::capture
constexpr auto capture(Token)
Captures whatever the token matches as a lexeme; does not include trailing whitespace.
Definition: capture.hpp:127
BT::Grammar::BooleanLiteral::False::rule
static constexpr auto rule
Definition: any_types.hpp:146
lexyd::sign
constexpr auto sign
Matches a plus or minus sign or nothing, producing +1 or -1.
Definition: sign.hpp:43
lexy::_detail::ascii_set
Definition: char_class.hpp:14
BT::Grammar::Integer::integer::value
static constexpr auto value
Definition: any_types.hpp:72


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