Skip to content

Commit 58cddcc

Browse files
committed
Update grammar rule for enums to allow specifying an underlying type
1 parent 0022844 commit 58cddcc

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/ansi-c/parser.y

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ extern char *yyansi_ctext;
267267

268268
%start grammar
269269

270-
%expect 1 /* the famous "dangling `else'" ambiguity */
270+
%expect 2 /* the famous "dangling `else'" ambiguity */
271271
/* results in one shift/reduce conflict */
272272
/* that we don't want to be reported */
273273

@@ -1790,38 +1790,54 @@ bit_field_size:
17901790
enum_name:
17911791
enum_key
17921792
gcc_type_attribute_opt
1793+
enum_underlying_type_opt
17931794
{
17941795
// an anon enum
17951796
}
17961797
'{' enumerator_list_opt '}'
17971798
gcc_type_attribute_opt
17981799
{
1799-
parser_stack($1).operands().swap(parser_stack($5).operands());
1800-
$$=merge($1, merge($2, $7)); // throw in the gcc attributes
1800+
parser_stack($1).operands().swap(parser_stack($6).operands());
1801+
$$=merge($1, merge($2, $8)); // throw in the gcc attributes
18011802
}
18021803
| enum_key
18031804
gcc_type_attribute_opt
18041805
identifier_or_typedef_name
1806+
enum_underlying_type_opt
18051807
{
18061808
// an enum with tag
18071809
parser_stack($1).set(ID_tag, parser_stack($3));
18081810
}
1809-
'{' enumerator_list_opt '}'
1811+
braced_enumerator_list_opt
18101812
gcc_type_attribute_opt
18111813
{
1812-
parser_stack($1).operands().swap(parser_stack($6).operands());
1813-
$$=merge($1, merge($2, $8)); // throw in the gcc attributes
1814+
if(!parser_stack($6).is_nil())
1815+
{
1816+
parser_stack($1).operands().swap(parser_stack($6).operands());
1817+
}
1818+
else
1819+
{
1820+
parser_stack($1).id(ID_c_enum_tag);
1821+
}
1822+
1823+
$$=merge($1, merge($2, $7)); // throw in the gcc attributes
18141824
}
1815-
| enum_key
1816-
gcc_type_attribute_opt
1817-
identifier_or_typedef_name
1818-
gcc_type_attribute_opt
1825+
;
1826+
1827+
enum_underlying_type_opt:
1828+
/* empty */
1829+
| ':' basic_type_name
1830+
1831+
braced_enumerator_list_opt:
1832+
/* empty */
18191833
{
1820-
parser_stack($1).id(ID_c_enum_tag); // tag only
1821-
parser_stack($1).set(ID_tag, parser_stack($3));
1822-
$$=merge($1, merge($2, $4)); // throw in the gcc attributes
1834+
init($$);
1835+
parser_stack($$).make_nil();
1836+
}
1837+
| '{' enumerator_list_opt '}'
1838+
{
1839+
$$=$2;
18231840
}
1824-
;
18251841

18261842
enum_key: TOK_ENUM
18271843
{

0 commit comments

Comments
 (0)