More error messages.
[pspp] / src / language / lexer / lexer.c
index 562fcc5827d3a4662d237633cb3785dee6e0cf33..07ce2beef0bed47b3b9b69954d15e50f732e6562 100644 (file)
@@ -1867,7 +1867,30 @@ lex_source_get (const struct lex_source *src_)
       return true;
     }
 
-  /* The first 'n_call' tokens starting at 'middle' will be replaced by a
+  /* Now expand the macro.
+
+     We temporarily add the macro call's tokens to the source in case the macro
+     expansion calls msg() to report an error and error processing tries to get
+     the location of the error with, e.g. lex_get_first_line_number(), which
+     would re-enter this code.  This is a kluge; it might be cleaner to pass
+     the line number into macro_expander_get_expansion(). */
+  src->middle += n_call;
+  struct macro_tokens expansion = { .n = 0 };
+  macro_expander_get_expansion (me, src->reader->syntax, &expansion);
+  macro_expander_destroy (me);
+  src->middle -= n_call;
+
+  /* Convert the macro expansion into syntax for possible error messages later. */
+  size_t *ofs = xnmalloc (expansion.n, sizeof *ofs);
+  size_t *len = xnmalloc (expansion.n, sizeof *len);
+  struct string s = DS_EMPTY_INITIALIZER;
+  macro_tokens_to_representation (&expansion, &s, ofs, len);
+
+  if (settings_get_mprint ())
+    output_item_submit (text_item_create (TEXT_ITEM_LOG, ds_cstr (&s),
+                                          _("Macro Expansion")));
+
+  /* The first 'n_call' tokens starting at 'middle' will be replaced by the
      macro expansion.  There might be more tokens after that, up to 'front'.
 
      Figure out the boundary of the macro call in the syntax, to go into the
@@ -1893,21 +1916,6 @@ lex_source_get (const struct lex_source *src_)
                                  & (src->capacity - 1)];
   src->front = src->middle;
 
-  /* Now expand the macro. */
-  struct macro_tokens expansion = { .n = 0 };
-  macro_expander_get_expansion (me, src->reader->syntax, &expansion);
-  macro_expander_destroy (me);
-
-  /* Convert the macro expansion into syntax for possible error messages later. */
-  size_t *ofs = xnmalloc (expansion.n, sizeof *ofs);
-  size_t *len = xnmalloc (expansion.n, sizeof *len);
-  struct string s = DS_EMPTY_INITIALIZER;
-  macro_tokens_to_representation (&expansion, &s, ofs, len);
-
-  if (settings_get_mprint ())
-    output_item_submit (text_item_create (TEXT_ITEM_LOG, ds_cstr (&s),
-                                          _("Macro Expansion")));
-
   /* Append the macro expansion tokens to the lookahead. */
   char *macro_rep = ds_steal_cstr (&s);
   size_t *ref_cnt = xmalloc (sizeof *ref_cnt);