]> git.draconx.ca Git - cdecl99.git/blobdiff - src/parse.y
libcdecl: Factor out open-coded list concatenation.
[cdecl99.git] / src / parse.y
index b77cfbb1f4f1e454c16ef77ea3131e96f87c78d5..cf24b25ae73d1e3377bc8804b492bb10d118f817 100644 (file)
@@ -76,6 +76,7 @@ const char *cdecl__token_name(unsigned token);
 }
 
 %{
+static void yyerror(YYLTYPE *, yyscan_t, struct cdecl **, const char *);
 static void free_decl(struct cdecl *);
 
 static void free_declspec(struct cdecl_declspec *x)
@@ -142,13 +143,17 @@ void cdecl__free(struct cdecl *decl)
        free_decl(decl);
 }
 
-static void
-yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
+/*
+ * Join two declaration specifier lists into a single list, with "a" being the
+ * head of the new list.
+ *
+ * The list "a" is assumed to be nonempty.
+ */
+static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
 {
-       if (strstr(err, "T_LEX_ERROR"))
-               return;
-
-       cdecl__err(CDECL_ENOPARSE, "%s", err);
+       while (a->next)
+               a = a->next;
+       a->next = b;
 }
 %}
 
@@ -475,27 +480,16 @@ type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
  * together three different specifiers lists.
  */
 post_specs: qualifiers typespec type_qual_specs {
+       $2->next = $3;
+       join_specs($2, $1);
        $$ = $2;
-       $$->next = $1;
-       for (struct cdecl_declspec *s = $$; s; s = s->next) {
-               if (!s->next) {
-                       s->next = $3;
-                       break;
-               }
-       }
 }
 
 english_declaration: storage_func_specs english_declarator post_specs {
+       join_specs($3, $1);
        ALLOC_STRUCT($$, struct cdecl,
                .specifiers = $3,
                .declarators = $2);
-
-       for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
-               if (!s->next) {
-                       s->next = $1;
-                       break;
-               }
-       }
 }
 
 english_declarator: {
@@ -614,3 +608,12 @@ const char *cdecl__token_name(unsigned token)
 {
        return yytname[YYTRANSLATE(token)];
 }
+
+static void
+yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
+{
+       if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
+               return;
+
+       cdecl__err(CDECL_ENOPARSE, "%s", err);
+}