message: Intern file names in msg_location to make them cheaper to copy.
[pspp] / src / language / control / define.c
index 23749eb69eef61dad94e3a4a8d403f0a8b443680..f33e885925a807b67071d1832b2de8442f3c7845 100644 (file)
@@ -23,6 +23,7 @@
 #include "language/lexer/macro.h"
 #include "language/lexer/scan.h"
 #include "language/lexer/token.h"
+#include "libpspp/intern.h"
 #include "libpspp/message.h"
 
 #include "gl/xalloc.h"
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
-static bool
-force_macro_id (struct lexer *lexer)
-{
-  return lex_token (lexer) == T_MACRO_ID || lex_force_id (lexer);
-}
-
 static bool
 match_macro_id (struct lexer *lexer, const char *keyword)
 {
@@ -96,17 +91,30 @@ dup_arg_type (struct lexer *lexer, bool *saw_arg_type)
 int
 cmd_define (struct lexer *lexer, struct dataset *ds UNUSED)
 {
-  if (!force_macro_id (lexer))
-    return CMD_FAILURE;
+  /* Parse macro name.
+
+     The macro name is a T_STRING token, even though it's an identifier,
+     because that's the way that the segmenter prevents it from getting
+     macro-expanded. */
+  if (lex_token (lexer) != T_STRING)
+    {
+      lex_error (lexer, _("expecting identifier"));
+      return CMD_FAILURE;
+    }
+  const char *name = lex_tokcstr (lexer);
+  if (!id_is_plausible (name + (name[0] == '!'), false))
+    {
+      lex_error (lexer, _("expecting identifier"));
+      return CMD_FAILURE;
+    }
 
-  /* Parse macro name. */
   struct macro *m = xmalloc (sizeof *m);
   *m = (struct macro) {
-    .name = ss_xstrdup (lex_tokss (lexer)),
+    .name = xstrdup (name),
     .location = xmalloc (sizeof *m->location),
   };
   *m->location = (struct msg_location) {
-    .file_name = xstrdup_if_nonnull (lex_get_file_name (lexer)),
+    .file_name = intern_new_if_nonnull (lex_get_file_name (lexer)),
     .first_line = lex_get_first_line_number (lexer, 0),
   };
   lex_get (lexer);