@@ -301,7 +301,8 @@ struct action<semicolon>
301
301
std::map<std::string, std::string>& state,
302
302
std::vector<traits<DynamicData>::ref_type>& /* operands*/ )
303
303
{
304
- if (!state[" type" ].empty () && state.count (" current_struct_member_name" ) && !state[" current_struct_member_name" ].empty ())
304
+ if (!state[" type" ].empty () && state.count (" current_struct_member_name" ) &&
305
+ !state[" current_struct_member_name" ].empty ())
305
306
{
306
307
// Add the type and name to the member lists
307
308
state[" struct_member_types" ] += state[" type" ] + " ;" ;
@@ -780,45 +781,49 @@ load_literal_action(oct_literal, octal, int64_t, TK_INT64, set_int64_value)
780
781
load_literal_action (hex_literal, hexa, int64_t , TK_INT64, set_int64_value)
781
782
load_literal_action (float_literal, float , long double , TK_FLOAT128, set_float128_value)
782
783
load_literal_action (fixed_pt_literal, fixed, long double , TK_FLOAT128, set_float128_value)
783
- load_literal_action (character_literal, char8, char , TK_CHAR8, set_char8_value)
784
784
load_literal_action (string_literal, string, std::string, TK_STRING8, set_string_value)
785
785
786
- template <>
787
- struct action <wide_character_literal>
788
- {
789
- template <typename Input>
790
- static void apply (
791
- const Input& in,
792
- Context* /* ctx*/ ,
793
- std::map<std::string, std::string>& state,
794
- std::vector<traits<DynamicData>::ref_type>& operands)
795
- {
796
- std::cout << " wide_character_literal: " << typeid (wide_character_literal).name ()
797
- << " " << in.string () << std::endl;
798
-
799
- if (state.count (" arithmetic_expr" ))
800
- {
801
- state[" arithmetic_expr" ] += (state[" arithmetic_expr" ].empty () ? " " : " ;" ) + std::string{" wchar" };
802
- }
803
-
804
- wchar_t value = L' \0 ' ;
805
- std::string content = in.string ().substr (2 , in.string ().size () - 3 );
806
- if (!content.empty ()) {
807
- value = static_cast <wchar_t >(content[0 ]);
808
- }
809
-
810
- DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance ()};
811
- DynamicType::_ref_type xtype {factory->get_primitive_type (TK_CHAR16)};
812
- DynamicData::_ref_type xdata {DynamicDataFactory::get_instance ()->create_data (xtype)};
813
- xdata->set_char16_value (MEMBER_ID_INVALID, value);
814
-
815
- if (state.count (" arithmetic_expr" ))
816
- {
817
- operands.push_back (xdata);
818
- }
819
- }
786
+ #define load_character_action (Rule, id, type, type_kind, set_value, offset ) \
787
+ template <> \
788
+ struct action <Rule> \
789
+ { \
790
+ template <typename Input> \
791
+ static void apply ( \
792
+ const Input& in, \
793
+ Context* /* ctx*/ , \
794
+ std::map<std::string, std::string>& state, \
795
+ std::vector<traits<DynamicData>::ref_type>& operands) \
796
+ { \
797
+ std::cout << #Rule << " : " << typeid (Rule).name () << " " << in.string () << std::endl; \
798
+ \
799
+ if (state.count (" arithmetic_expr" )) \
800
+ { \
801
+ state[" arithmetic_expr" ] += (state[" arithmetic_expr" ].empty () ? " " : " ;" ) + std::string{#id}; \
802
+ } \
803
+ \
804
+ const std::string content = in.string ().substr (offset, in.string ().size () - (offset + 1 )); \
805
+ if (content.empty () || (content.size () < 1 )) \
806
+ { \
807
+ EPROSIMA_LOG_ERROR (IDLPARSER, " Invalid character literal: " << content); \
808
+ throw std::runtime_error (" Invalid character literal: " + content); \
809
+ } \
810
+ \
811
+ type value = static_cast <type>(content[0 ]); \
812
+ \
813
+ DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance ()}; \
814
+ DynamicType::_ref_type xtype = factory->get_primitive_type (type_kind); \
815
+ DynamicData::_ref_type xdata {DynamicDataFactory::get_instance ()->create_data (xtype)}; \
816
+ xdata->set_value (MEMBER_ID_INVALID, value); \
817
+ \
818
+ if (state.count (" arithmetic_expr" )) \
819
+ { \
820
+ operands.push_back (xdata); \
821
+ } \
822
+ } \
823
+ };
820
824
821
- };
825
+ load_character_action (character_literal, char8, char , TK_CHAR8, set_char8_value, 1 )
826
+ load_character_action (wide_character_literal, char16, wchar_t , TK_CHAR16, set_char16_value, 2 )
822
827
823
828
template <>
824
829
struct action <wide_string_literal>
@@ -1349,6 +1354,7 @@ struct action<enum_dcl>
1349
1354
1350
1355
DynamicType::_ref_type member_type {factory->get_primitive_type (TK_INT32)};
1351
1356
DynamicData::_ref_type member_data {DynamicDataFactory::get_instance ()->create_data (member_type)};
1357
+ member_data->set_int32_value (MEMBER_ID_INVALID, (int32_t )i);
1352
1358
1353
1359
module.create_constant (tokens[i], member_data, false , true ); // Mark it as "from_enum"
1354
1360
}
@@ -1551,10 +1557,38 @@ struct action<case_label>
1551
1557
throw std::runtime_error (" Finished case label parsing with non-empty operands stack." );
1552
1558
}
1553
1559
1554
- int64_t value;
1555
- xdata->get_int64_value (value, MEMBER_ID_INVALID);
1560
+ std::string label;
1561
+ DynamicType::_ref_type xtype = xdata->type ();
1562
+ if (TK_INT64 == xtype->get_kind () || TK_INT32 == xtype->get_kind ())
1563
+ {
1564
+ int64_t value = 0 ;
1565
+ xdata->get_int64_value (value, MEMBER_ID_INVALID);
1566
+ label = std::to_string (value);
1567
+ }
1568
+ else if (TK_BOOLEAN == xtype->get_kind ())
1569
+ {
1570
+ bool value = false ;
1571
+ xdata->get_boolean_value (value, MEMBER_ID_INVALID);
1572
+ label = value ? " 1" : " 0" ;
1573
+ }
1574
+ else if (TK_CHAR8 == xtype->get_kind ())
1575
+ {
1576
+ char value = ' 0' ;
1577
+ xdata->get_char8_value (value, MEMBER_ID_INVALID);
1578
+ label = value;
1579
+ }
1580
+ else if (TK_CHAR16 == xtype->get_kind ())
1581
+ {
1582
+ wchar_t value = L' 0' ;
1583
+ xdata->get_char16_value (value, MEMBER_ID_INVALID);
1584
+ label = static_cast <char >(value);
1585
+ }
1586
+ else
1587
+ {
1588
+ EPROSIMA_LOG_ERROR (IDLPARSER, " Unsupported case label data type: " << xtype->get_kind ());
1589
+ throw std::runtime_error (" Unsupported case label data type: " + xtype->get_kind ());
1590
+ }
1556
1591
1557
- std::string label = std::to_string (value);
1558
1592
if (state[" union_labels" ].empty () || state[" union_labels" ].back () == ' ;' )
1559
1593
{
1560
1594
state[" union_labels" ] += label;
@@ -1677,7 +1711,14 @@ struct action<union_def>
1677
1711
}
1678
1712
else
1679
1713
{
1680
- labels[i].push_back (std::stoi (num_str));
1714
+ if (std::all_of (num_str.begin (), num_str.end (), ::isdigit))
1715
+ {
1716
+ labels[i].push_back (std::stoi (num_str));
1717
+ }
1718
+ else
1719
+ {
1720
+ labels[i].push_back (static_cast <int32_t >(num_str[0 ]));
1721
+ }
1681
1722
}
1682
1723
}
1683
1724
}
@@ -1693,7 +1734,6 @@ struct action<union_def>
1693
1734
MemberDescriptor::_ref_type member_descriptor {traits<MemberDescriptor>::make_shared ()};
1694
1735
member_descriptor->name (names[i]);
1695
1736
member_descriptor->type (member_type);
1696
- member_descriptor->id (i);
1697
1737
1698
1738
if (default_label_index == static_cast <int >(i))
1699
1739
{
0 commit comments