Skip to content

Commit 54f1849

Browse files
committed
Finish arrays.idl testing
Signed-off-by: Yangbo Long <[email protected]>
1 parent 4be7da0 commit 54f1849

File tree

3 files changed

+342
-44
lines changed

3 files changed

+342
-44
lines changed

src/cpp/fastdds/xtypes/dynamic_types/idl_parser/IdlGrammar.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ struct fixed_pt_const_type : kw_fixed {};
272272

273273
struct scoped_name_tail : seq<double_colon, identifier> {};
274274
struct scoped_name : seq<opt<double_colon>, identifier, star<scoped_name_tail>> {};
275-
struct positive_int_const : sor<scoped_name, const_expr> {};
275+
struct positive_int_const : const_expr {};
276276
struct fixed_pt_type : seq<kw_fixed, open_ang_bracket, positive_int_const, comma, positive_int_const, close_ang_bracket> {};
277-
struct string_size : seq<one<'<'>, positive_int_const, one<'>'>> {};
278-
struct wstring_size : seq<one<'<'>, positive_int_const, one<'>'>> {};
277+
struct string_size : seq<open_ang_bracket, positive_int_const, close_ang_bracket> {};
278+
struct wstring_size : seq<open_ang_bracket, positive_int_const, close_ang_bracket> {};
279279
struct collection_size : seq<comma, positive_int_const> {};
280280
struct string_type : seq<kw_string, opt<string_size>> {};
281281
struct wide_string_type : seq<kw_wstring, opt<wstring_size>> {};

src/cpp/fastdds/xtypes/dynamic_types/idl_parser/IdlParser.hpp

+81-41
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,12 @@ struct action<semicolon>
331331
{ \
332332
std::cout << "load_type_action: " << typeid(Rule).name() << " " \
333333
<< in.string() << std::endl; \
334+
\
334335
state["type"] = std::string(#id); \
336+
if (state["type"] == "string" || state["type"] == "wstring") \
337+
{ \
338+
state.erase("parsing_string"); \
339+
} \
335340
} \
336341
};
337342

@@ -424,57 +429,53 @@ struct action<open_bracket>
424429
};
425430

426431
template<>
427-
struct action<positive_int_const>
432+
struct action<fixed_array_size>
428433
{
429434
template<typename Input>
430435
static void apply(
431-
const Input& in,
432-
Context* ctx,
436+
const Input& /*in*/,
437+
Context* /*ctx*/,
433438
std::map<std::string, std::string>& state,
434439
std::vector<traits<DynamicData>::ref_type>& operands)
435440
{
436-
Module& module = ctx->module();
437-
std::string const_value = in.string();
438-
439-
if (state.count("arithmetic_expr"))
441+
DynamicData::_ref_type xdata;
442+
if (!operands.empty())
440443
{
441-
DynamicData::_ref_type xdata;
444+
xdata = operands.back();
445+
operands.pop_back();
442446
if (!operands.empty())
443447
{
444-
xdata = operands.back();
445-
operands.pop_back();
446-
if (!operands.empty())
447-
{
448-
throw std::runtime_error("Finished const expression parsing with non-empty operands stack.");
449-
}
448+
throw std::runtime_error("Finished array size parsing with non-empty operands stack.");
450449
}
451-
else if (module.has_constant(const_value))
450+
451+
int64_t value;
452+
xdata->get_int64_value(value, MEMBER_ID_INVALID);
453+
if (value <= 0)
452454
{
453-
xdata = module.constant(const_value);
455+
throw std::runtime_error("Array size is non-positive: " + std::to_string(value));
454456
}
455-
else
457+
458+
// Append the current array size to `current_array_sizes`, separating multiple dimensions with commas
459+
if (state["current_array_sizes"].empty())
456460
{
457-
throw std::runtime_error("Unknown const expression: " + const_value);
461+
state["current_array_sizes"] = std::to_string(value);
458462
}
459-
460-
int64_t value;
461-
xdata->get_int64_value(value, MEMBER_ID_INVALID);
462-
if (value < 0)
463+
else
463464
{
464-
throw std::runtime_error("Size value is negative: " + std::to_string(value));
465+
state["current_array_sizes"] += "," + std::to_string(value);
465466
}
466-
state["positive_int_const"] = std::to_string(value);
467467
}
468468
else
469469
{
470-
state["positive_int_const"] = const_value;
470+
throw std::runtime_error("Empty operands stack while parsing fixed_array_size");
471471
}
472+
state.erase("arithmetic_expr");
472473
}
473474

474475
};
475476

476477
template<>
477-
struct action<fixed_array_size>
478+
struct action<kw_string>
478479
{
479480
template<typename Input>
480481
static void apply(
@@ -483,19 +484,40 @@ struct action<fixed_array_size>
483484
std::map<std::string, std::string>& state,
484485
std::vector<traits<DynamicData>::ref_type>& /*operands*/)
485486
{
486-
if (state.count("positive_int_const"))
487+
state["parsing_string"] = "true";
488+
}
489+
490+
};
491+
492+
template<>
493+
struct action<kw_wstring>
494+
{
495+
template<typename Input>
496+
static void apply(
497+
const Input& /*in*/,
498+
Context* /*ctx*/,
499+
std::map<std::string, std::string>& state,
500+
std::vector<traits<DynamicData>::ref_type>& /*operands*/)
501+
{
502+
state["parsing_string"] = "true";
503+
}
504+
505+
};
506+
507+
template<>
508+
struct action<open_ang_bracket>
509+
{
510+
template<typename Input>
511+
static void apply(
512+
const Input& /*in*/,
513+
Context* /*ctx*/,
514+
std::map<std::string, std::string>& state,
515+
std::vector<traits<DynamicData>::ref_type>& /*operands*/)
516+
{
517+
if (state.count("parsing_string") && state["parsing_string"] == "true")
487518
{
488-
// Append the current array size to `current_array_sizes`, separating multiple dimensions with commas
489-
if (state["current_array_sizes"].empty())
490-
{
491-
state["current_array_sizes"] = state["positive_int_const"];
492-
}
493-
else
494-
{
495-
state["current_array_sizes"] += "," + state["positive_int_const"];
496-
}
519+
state["arithmetic_expr"] = "";
497520
}
498-
state.erase("arithmetic_expr");
499521
}
500522

501523
};
@@ -509,14 +531,32 @@ struct action<fixed_array_size>
509531
const Input& in, \
510532
Context* /*ctx*/, \
511533
std::map<std::string, std::string>& state, \
512-
std::vector<traits<DynamicData>::ref_type>& /*operands*/) \
534+
std::vector<traits<DynamicData>::ref_type>& operands) \
513535
{ \
514536
std::cout << "load_stringsize_action: " << typeid(Rule).name() << " " \
515537
<< in.string() << std::endl; \
516-
if (state.count("positive_int_const")) \
538+
\
539+
DynamicData::_ref_type xdata; \
540+
if (!operands.empty()) \
541+
{ \
542+
xdata = operands.back(); \
543+
operands.pop_back(); \
544+
if (!operands.empty()) \
545+
{ \
546+
throw std::runtime_error("Finished string size parsing with non-empty operands stack."); \
547+
} \
548+
\
549+
int64_t value; \
550+
xdata->get_int64_value(value, MEMBER_ID_INVALID); \
551+
if (value <= 0) \
552+
{ \
553+
throw std::runtime_error("String size is non-positive: " + std::to_string(value)); \
554+
} \
555+
state[#id] = std::to_string(value); \
556+
} \
557+
else \
517558
{ \
518-
state[#id] = state["positive_int_const"]; \
519-
state.erase("positive_int_const"); \
559+
throw std::runtime_error("Empty operands stack while parsing string size"); \
520560
} \
521561
} \
522562
};

0 commit comments

Comments
 (0)