Skip to content

feat: allow string parameters #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions ecsact/detail/grammar.hh
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,16 @@ struct parameter_value {
static constexpr auto value = lexy::forward<bool>;
};

static constexpr auto rule = lexy::dsl::p<number> | lexy::dsl::p<boolean>;
struct string {
static constexpr auto rule = lexy::dsl::identifier(lexy::dsl::ascii::alpha);
static constexpr auto value =
lexy::as_string<std::string_view, lexy::ascii_encoding>;
};

static constexpr auto rule = lexy::dsl::p<number> | lexy::dsl::p<boolean> |
lexy::dsl::p<string>;

using value_variant = std::variant<bool, int32_t>;
using value_variant = std::variant<bool, int32_t, std::string_view>;

static constexpr auto value = lexy::callback<value_variant>(
[](auto&& value) -> value_variant { return value_variant{value}; }
Expand All @@ -128,6 +135,17 @@ struct parameter {
param.value.data.bool_value = value;
}

static void _set_value(
ecsact_statement_parameter& param,
std::string_view value
) {
param.value.type = ECSACT_STATEMENT_PARAM_VALUE_TYPE_STRING;
param.value.data.string_value = ecsact_statement_sv{
.data = value.data(),
.length = static_cast<int>(value.size()),
};
}

static constexpr auto rule = lexy::dsl::p<parameter_name> >>
lexy::dsl::opt(lexy::dsl::lit_c<':'> >> lexy::dsl::p<parameter_value>);

Expand Down
18 changes: 16 additions & 2 deletions test/parse_test_parameter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ecsact/parse.h"

using namespace std::string_literals;
using namespace std::string_view_literals;

using test_param_expected = std::vector<std::tuple<
std::string,
Expand Down Expand Up @@ -62,11 +63,11 @@ void test_param(std::string params, test_param_expected expected) {
break;
case ECSACT_STATEMENT_PARAM_VALUE_TYPE_STRING:
EXPECT_EQ(
std::string(
std::string_view(
expected_value.string_value.data,
static_cast<size_t>(expected_value.string_value.length)
),
std::string(
std::string_view(
statement.parameters[i].value.data.string_value.data,
static_cast<size_t>(
statement.parameters[i].value.data.string_value.length
Expand Down Expand Up @@ -189,4 +190,17 @@ TEST(Parse, MultiParam) {
),
}
);

test_param(
"(cool_str_param: whatup)",
{
std::make_tuple(
"cool_str_param"s,
ECSACT_STATEMENT_PARAM_VALUE_TYPE_STRING,
ecsact_statement_parameter_value_data{
.string_value{"whatup"sv.data(), "whatup"sv.size()}
}
),
}
);
}