Skip to content

Commit

Permalink
feat: allow string parameters (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy authored Jun 1, 2024
1 parent 2ea78c1 commit 177a12c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
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()}
}
),
}
);
}

0 comments on commit 177a12c

Please sign in to comment.