1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2005, 2009, 2010, 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* This file implements parts of identifier.h that call the msg() function.
18 This allows test programs that do not use those functions to avoid linking
19 additional object files. */
23 #include "data/identifier.h"
28 #include "libpspp/cast.h"
29 #include "libpspp/i18n.h"
30 #include "libpspp/message.h"
32 #include "gl/c-ctype.h"
35 #define _(msgid) gettext (msgid)
37 /* Returns true if UTF-8 string ID is an acceptable identifier in encoding
38 DICT_ENCODING (UTF-8 if null), false otherwise. If ISSUE_ERROR is true,
39 issues an explanatory error message on failure. */
41 id_is_valid (const char *id, const char *dict_encoding, bool issue_error)
45 if (!id_is_plausible (id, issue_error))
48 if (dict_encoding != NULL)
50 /* XXX need to reject recoded strings that contain the fallback
52 dict_len = recode_string_len (dict_encoding, "UTF-8", id, -1);
55 dict_len = strlen (id);
57 if (dict_len > ID_MAX_LEN)
60 msg (SE, _("Identifier `%s' exceeds %d-byte limit."),
68 /* Returns true if UTF-8 string ID is an plausible identifier, false
69 otherwise. If ISSUE_ERROR is true, issues an explanatory error message on
72 id_is_plausible (const char *id, bool issue_error)
74 const uint8_t *bad_unit;
80 /* ID cannot be the empty string. */
84 msg (SE, _("Identifier cannot be empty string."));
88 /* ID cannot be a reserved word. */
89 if (lex_id_to_token (ss_cstr (id)) != T_ID)
92 msg (SE, _("`%s' may not be used as an identifier because it "
93 "is a reserved word."), id);
97 bad_unit = u8_check (CHAR_CAST (const uint8_t *, id), strlen (id));
100 /* If this message ever appears, it probably indicates a PSPP bug since
101 it shouldn't be possible to get invalid UTF-8 this far. */
103 msg (SE, _("`%s' may not be used as an identifier because it "
104 "contains ill-formed UTF-8 at byte offset %tu."),
105 id, CHAR_CAST (const char *, bad_unit) - id);
109 /* Check that it is a valid identifier. */
110 mblen = u8_strmbtouc (&uc, CHAR_CAST (uint8_t *, id));
111 if (!lex_uc_is_id1 (uc))
114 msg (SE, _("Character %s (in `%s') may not appear "
115 "as the first character in a identifier."),
116 uc_name (uc, ucname), id);
120 for (s = CHAR_CAST (uint8_t *, id + mblen);
121 (mblen = u8_strmbtouc (&uc, s)) != 0;
123 if (!lex_uc_is_idn (uc))
126 msg (SE, _("Character %s (in `%s') may not appear in an "
128 uc_name (uc, ucname), id);