macro: Make ARG_CHAREND and ARG_ENCLOSE more uniform in struct macro_param.
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 24 Jul 2021 05:50:55 +0000 (22:50 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 24 Jul 2021 06:26:49 +0000 (23:26 -0700)
A few pieces of code want to find the end of a parameter and it's easier
if the "end" token is always the same member.

src/language/control/define.c
src/language/lexer/macro.c
src/language/lexer/macro.h

index 48dda4c7afb0331afa63113d9cc0fc5e36ae7a08..23749eb69eef61dad94e3a4a8d403f0a8b443680 100644 (file)
@@ -219,10 +219,9 @@ cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
                 goto error;
 
               p->arg_type = ARG_CHAREND;
-              p->charend = (struct token) { .type = T_STOP };
 
               if (!lex_force_match (lexer, T_LPAREN)
-                  || !parse_quoted_token (lexer, &p->charend)
+                  || !parse_quoted_token (lexer, &p->end)
                   || !lex_force_match (lexer, T_RPAREN))
                 goto error;
             }
@@ -232,12 +231,11 @@ cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
                 goto error;
 
               p->arg_type = ARG_ENCLOSE;
-              p->enclose[0] = p->enclose[1] = (struct token) { .type = T_STOP };
 
               if (!lex_force_match (lexer, T_LPAREN)
-                  || !parse_quoted_token (lexer, &p->enclose[0])
+                  || !parse_quoted_token (lexer, &p->start)
                   || !lex_force_match (lexer, T_COMMA)
-                  || !parse_quoted_token (lexer, &p->enclose[1])
+                  || !parse_quoted_token (lexer, &p->end)
                   || !lex_force_match (lexer, T_RPAREN))
                 goto error;
             }
index d20b74c9fe20e9f2c8b6d8ec71078313d9ead6fd..a6d08b917d8c476ae72a1f9295e38f1abb675540 100644 (file)
@@ -418,24 +418,8 @@ macro_destroy (struct macro *m)
       free (p->name);
 
       macro_tokens_uninit (&p->def);
-
-      switch (p->arg_type)
-        {
-        case ARG_N_TOKENS:
-          break;
-
-        case ARG_CHAREND:
-          token_uninit (&p->charend);
-          break;
-
-        case ARG_ENCLOSE:
-          token_uninit (&p->enclose[0]);
-          token_uninit (&p->enclose[1]);
-          break;
-
-        case ARG_CMDEND:
-          break;
-        }
+      token_uninit (&p->start);
+      token_uninit (&p->end);
     }
   free (m->params);
   macro_tokens_uninit (&m->body);
@@ -650,8 +634,7 @@ mc_add_arg (struct macro_call *mc, const struct macro_token *mt,
     {
       next_arg = (p->arg_type == ARG_CMDEND
                   ? token->type == T_ENDCMD || token->type == T_STOP
-                  : token_equal (token, (p->arg_type == ARG_CHAREND
-                                         ? &p->charend : &p->enclose[1])));
+                  : token_equal (token, &p->end));
       add_token = !next_arg;
     }
 
@@ -688,13 +671,13 @@ mc_enclose (struct macro_call *mc, const struct macro_token *mt,
   const struct token *token = &mt->token;
   mc->n_tokens++;
 
-  if (token_equal (&mc->param->enclose[0], token))
+  if (token_equal (&mc->param->start, token))
     {
       mc->state = MC_ARG;
       return 0;
     }
 
-  return mc_expected (mc, mt, loc, &mc->param->enclose[0]);
+  return mc_expected (mc, mt, loc, &mc->param->start);
 }
 
 static const struct macro_param *
index 72a4138505bca9c520f3fae385bdba34349a48c2..4a6f73dff97cb12b7c657c162615c14384e893c4 100644 (file)
@@ -81,12 +81,10 @@ struct macro_param
         ARG_CMDEND
       }
     arg_type;
-    union
-      {
-        int n_tokens;            /* ARG_N_TOKENS. */
-        struct token charend;    /* ARG_CHAREND. */
-        struct token enclose[2]; /* ARG_ENCLOSE. */
-      };
+
+    int n_tokens;               /* ARG_N_TOKENS only. */
+    struct token start;         /* ARG_ENCLOSE only. */
+    struct token end;           /* ARG_ENCLOSE and ARG_CHAREND only. */
   };
 
 /* A macro. */