X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fcontrol%2Fdefine.c;h=e1d9622ff4067b882a92a8bc259c1bdf8dac2025;hb=56fc407a19b93a622be43a196fca0eabac148023;hp=23749eb69eef61dad94e3a4a8d403f0a8b443680;hpb=99e99bc433add91bc30f340895307122dfba9366;p=pspp diff --git a/src/language/control/define.c b/src/language/control/define.c index 23749eb69e..e1d9622ff4 100644 --- a/src/language/control/define.c +++ b/src/language/control/define.c @@ -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" @@ -30,12 +31,6 @@ #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,19 +91,26 @@ 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)), - .location = xmalloc (sizeof *m->location), - }; - *m->location = (struct msg_location) { - .file_name = xstrdup_if_nonnull (lex_get_file_name (lexer)), - .first_line = lex_get_first_line_number (lexer, 0), - }; + *m = (struct macro) { .name = xstrdup (name) }; + struct msg_point macro_start = lex_ofs_start_point (lexer, lex_ofs (lexer)); lex_get (lexer); if (!lex_force_match (lexer, T_LPAREN)) @@ -274,7 +276,14 @@ cmd_define (struct lexer *lexer, struct dataset *ds UNUSED) ds_put_byte (&body, '\n'); lex_get (lexer); } - m->location->last_line = lex_get_last_line_number (lexer, 0); + + struct msg_point macro_end = lex_ofs_end_point (lexer, lex_ofs (lexer) - 1); + m->location = xmalloc (sizeof *m->location); + *m->location = (struct msg_location) { + .file_name = intern_new_if_nonnull (lex_get_file_name (lexer)), + .start = { .line = macro_start.line }, + .end = { .line = macro_end.line }, + }; macro_tokens_from_string (&m->body, body.ss, lex_get_syntax_mode (lexer)); ds_destroy (&body);