From 92635c65e5e265dc8114805af8974715539d90d2 Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@cs.stanford.edu>
Date: Fri, 23 Jul 2021 23:17:48 -0700
Subject: [PATCH] macro: Continue expanding macro even in face of errors in
 call.

In practice, it was more confusing not to expand it than to expand it.
---
 src/language/lexer/macro.c       | 29 +++--------
 tests/language/control/define.at | 86 ++++----------------------------
 2 files changed, 17 insertions(+), 98 deletions(-)

diff --git a/src/language/lexer/macro.c b/src/language/lexer/macro.c
index 270cae50fa..977436a604 100644
--- a/src/language/lexer/macro.c
+++ b/src/language/lexer/macro.c
@@ -498,9 +498,6 @@ macro_set_add (struct macro_set *set, struct macro *m)
 
 enum mc_state
   {
-    /* Error state. */
-    MC_ERROR,
-
     /* Accumulating tokens in mc->params toward the end of any type of
        argument. */
     MC_ARG,
@@ -696,20 +693,19 @@ mc_expected (struct macro_call *mc, const struct macro_token *actual,
             mc->param->name, mc->macro->name);
   free (expected_s);
 
-  mc->state = MC_ERROR;
-  return -1;
+  return mc_finished (mc);
 }
 
 static int
 mc_enclose (struct macro_call *mc, const struct macro_token *mt,
             const struct msg_location *loc)
 {
-  mc->n_tokens++;
-
   const struct token *token = &mt->token;
   const struct macro_param *p = mc->param;
   if (token_equal (&p->start, token))
     {
+      mc->n_tokens++;
+
       struct macro_tokens **argp = &mc->args[p - mc->macro->params];
       *argp = xzalloc (sizeof **argp);
       mc->state = MC_ARG;
@@ -754,13 +750,9 @@ mc_keyword (struct macro_call *mc, const struct macro_token *mt,
     {
       struct macro_tokens **argp = &mc->args[p - mc->macro->params];
       if (*argp)
-        {
-          mc_error (mc, loc,
-                    _("Argument %s multiply specified in call to macro %s."),
-                    p->name, mc->macro->name);
-          mc->state = MC_ERROR;
-          return -1;
-        }
+        mc_error (mc, loc,
+                  _("Argument %s multiply specified in call to macro %s."),
+                  p->name, mc->macro->name);
 
       *argp = xzalloc (sizeof **argp);
       mc->param = p;
@@ -776,11 +768,9 @@ static int
 mc_equals (struct macro_call *mc, const struct macro_token *mt,
            const struct msg_location *loc)
 {
-  const struct token *token = &mt->token;
-  mc->n_tokens++;
-
-  if (token->type == T_EQUALS)
+  if (mt->token.type == T_EQUALS)
     {
+      mc->n_tokens++;
       mc->state = mc->param->arg_type == ARG_ENCLOSE ? MC_ENCLOSE : MC_ARG;
       return 0;
     }
@@ -879,9 +869,6 @@ macro_call_add (struct macro_call *mc, const struct macro_token *mt,
 {
   switch (mc->state)
     {
-    case MC_ERROR:
-      return -1;
-
     case MC_ARG:
       return mc_add_arg (mc, mt, loc);
 
diff --git a/tests/language/control/define.at b/tests/language/control/define.at
index 876d650130..3c940c1513 100644
--- a/tests/language/control/define.at
+++ b/tests/language/control/define.at
@@ -505,9 +505,7 @@ AT_CHECK([pspp --testing-mode define.sps], [1], [dnl
 define.sps:3.8: error: DEBUG EXPAND: Found `.' while expecting `=' reading
 argument !arg1 to macro !k.
 
-note: unexpanded token "!k"
-
-note: unexpanded token "arg1"
+k( )
 
 define.sps:4.9: error: DEBUG EXPAND: Reached end of command expecting 1 more
 token in argument !arg1 to macro !k.
@@ -559,9 +557,7 @@ AT_CHECK([pspp --testing-mode define.sps], [1], [dnl
 define.sps:6.8: error: DEBUG EXPAND: Found `.' while expecting `=' reading
 argument !arg1 to macro !k.
 
-note: unexpanded token "!k"
-
-note: unexpanded token "arg1"
+k(, )
 
 define.sps:7.9: error: DEBUG EXPAND: Reached end of command expecting "/" in
 argument !arg1 to macro !k.
@@ -627,27 +623,17 @@ AT_CHECK([pspp --testing-mode define.sps], [1], [dnl
 define.sps:6.8: error: DEBUG EXPAND: Found `.' while expecting `=' reading
 argument !arg1 to macro !k.
 
-note: unexpanded token "!k"
-
-note: unexpanded token "arg1"
+k(, )
 
 define.sps:7.9: error: DEBUG EXPAND: Found `.' while expecting `@{:@' reading
 argument !arg1 to macro !k.
 
-note: unexpanded token "!k"
-
-note: unexpanded token "arg1"
-
-note: unexpanded token "="
+k(, )
 
 define.sps:8.9: error: DEBUG EXPAND: Found `x' while expecting `@{:@' reading
 argument !arg1 to macro !k.
 
-note: unexpanded token "!k"
-
-note: unexpanded token "arg1"
-
-note: unexpanded token "="
+k(, )
 
 note: unexpanded token "x"
 
@@ -659,78 +645,24 @@ k(x, )
 define.sps:10.17: error: DEBUG EXPAND: Found `.' while expecting `=' reading
 argument !arg2 to macro !k.
 
-note: unexpanded token "!k"
-
-note: unexpanded token "arg1"
-
-note: unexpanded token "="
-
-note: unexpanded token "@{:@"
-
-note: unexpanded token "x"
-
-note: unexpanded token "@:}@"
-
-note: unexpanded token "arg2"
+k(x, )
 
 define.sps:11.18: error: DEBUG EXPAND: Found `.' while expecting `{' reading
 argument !arg2 to macro !k.
 
-note: unexpanded token "!k"
-
-note: unexpanded token "arg1"
-
-note: unexpanded token "="
-
-note: unexpanded token "@{:@"
-
-note: unexpanded token "x"
-
-note: unexpanded token "@:}@"
-
-note: unexpanded token "arg2"
-
-note: unexpanded token "="
+k(x, )
 
 define.sps:12.18: error: DEBUG EXPAND: Found `y' while expecting `{' reading
 argument !arg2 to macro !k.
 
-note: unexpanded token "!k"
-
-note: unexpanded token "arg1"
-
-note: unexpanded token "="
-
-note: unexpanded token "@{:@"
-
-note: unexpanded token "x"
-
-note: unexpanded token "@:}@"
-
-note: unexpanded token "arg2"
-
-note: unexpanded token "="
+k(x, )
 
 note: unexpanded token "y"
 
 define.sps:13.18: error: DEBUG EXPAND: Found `@{:@' while expecting `{' reading
 argument !arg2 to macro !k.
 
-note: unexpanded token "!k"
-
-note: unexpanded token "arg1"
-
-note: unexpanded token "="
-
-note: unexpanded token "@{:@"
-
-note: unexpanded token "x"
-
-note: unexpanded token "@:}@"
-
-note: unexpanded token "arg2"
-
-note: unexpanded token "="
+k(x, )
 
 note: unexpanded token "@{:@"
 
-- 
2.30.2