]> git.draconx.ca Git - cdecl99.git/commitdiff
libcdecl: Combine array length parser rules.
authorNick Bowler <nbowler@draconx.ca>
Thu, 20 Jul 2023 01:08:03 +0000 (21:08 -0400)
committerNick Bowler <nbowler@draconx.ca>
Thu, 20 Jul 2023 01:08:03 +0000 (21:08 -0400)
Some of the parser rules to handle incomplete array types versus arrays
with explicit length (not VLAs) are duplicated between the regular and
english parsers.

As this is the same in both cases we can use a new symbol and drop some
duplicate actions to simplify things a bit.

src/parse.y

index b57428db4080b1f0f892782684e31dfb50e04180..918cbd033a085870a70fc8f59a8058e468ebfbc5 100644 (file)
@@ -239,6 +239,7 @@ static struct cdecl *insert_identifier(struct cdecl *decl, char *ident)
 %token T_VLA       "variable-length"
 
 %type <strval>     vla_ident
+%type <uintval>    array_length
 %type <boolval>    varargs
 %type <spectype>   declspec_simple qualifier_simple
 %type <spectype>   typespec_simple typespec_tagged
@@ -377,10 +378,7 @@ vla_ident: T_IDENT | T_ASTERISK {
        strcpy($$, "");
 }
 
-array: T_LBRACKET T_UINT T_RBRACKET {
-       if ($2 == 0)
-               FAIL(_("array length must be positive"));
-
+array: T_LBRACKET array_length T_RBRACKET {
        ALLOC_STRUCT($$, struct cdecl_declarator,
                .type = CDECL_DECL_ARRAY,
                .u.array.length = $2);
@@ -388,9 +386,6 @@ array: T_LBRACKET T_UINT T_RBRACKET {
        ALLOC_STRUCT($$, struct cdecl_declarator,
                .type = CDECL_DECL_ARRAY,
                .u.array.vla = $2);
-} | T_LBRACKET T_RBRACKET {
-       ALLOC_STRUCT($$, struct cdecl_declarator,
-               .type = CDECL_DECL_ARRAY);
 }
 
 parameter: declspecs declarator {
@@ -563,17 +558,16 @@ english_array: T_VLA T_ARRAY english_vla T_OF {
        ALLOC_STRUCT($$, struct cdecl_declarator,
                .type = CDECL_DECL_ARRAY,
                .u.array.vla = $3);
-} | T_ARRAY T_UINT T_OF {
-       if ($2 == 0)
-               FAIL(_("array length must be positive"));
-
+} | T_ARRAY array_length T_OF {
        ALLOC_STRUCT($$, struct cdecl_declarator,
                .type = CDECL_DECL_ARRAY,
                .u.array.length = $2);
-} | T_ARRAY T_OF {
-       ALLOC_STRUCT($$, struct cdecl_declarator,
-               .type = CDECL_DECL_ARRAY,
-               .u.array.length = 0);
+}
+
+array_length: { $$ = 0; }
+array_length: T_UINT {
+       if (!($$ = $1))
+               FAIL(_("array length must be positive"));
 }
 
 english_vla: T_IDENT | {