This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add perldelta entry for ExtUtils::Manifest upgrade.
[perl5.git] / dquote_static.c
CommitLineData
04e98a4d
AD
1/* dquote_static.c
2 *
3efe3cb8 3 * This file contains static functions that are related to
04e98a4d
AD
4 * parsing double-quotish expressions, but are used in more than
5 * one file.
6 *
7 * It is currently #included by regcomp.c and toke.c.
8*/
9
881ffab6 10#define PERL_IN_DQUOTE_STATIC_C
881ffab6
KW
11#include "embed.h"
12
04e98a4d
AD
13/*
14 - regcurly - a little FSA that accepts {\d+,?\d*}
15 Pulled from regcomp.c.
16 */
04e98a4d 17PERL_STATIC_INLINE I32
ddeaf645 18S_regcurly(const char *s)
04e98a4d 19{
881ffab6 20 PERL_ARGS_ASSERT_REGCURLY;
04e98a4d
AD
21
22 if (*s++ != '{')
23 return FALSE;
24 if (!isDIGIT(*s))
25 return FALSE;
26 while (isDIGIT(*s))
27 s++;
2ec31dd9 28 if (*s == ',') {
04e98a4d 29 s++;
2ec31dd9
KW
30 while (isDIGIT(*s))
31 s++;
32 }
4d68ffa0 33
412f55bb 34 return *s == '}';
04e98a4d 35}
db30362b 36
68b355dd
KW
37/* XXX Add documentation after final interface and behavior is decided */
38/* May want to show context for error, so would pass Perl_bslash_c(pTHX_ const char* current, const char* start, const bool output_warning)
39 U8 source = *current;
68b355dd
KW
40*/
41
42STATIC char
421e43ba 43S_grok_bslash_c(pTHX_ const char source, const bool output_warning)
68b355dd
KW
44{
45
46 U8 result;
47
421e43ba 48 if (! isPRINT_A(source)) {
7357bd17
KW
49 Perl_croak(aTHX_ "%s",
50 "Character following \"\\c\" must be printable ASCII");
68b355dd 51 }
421e43ba
KW
52 else if (source == '{') {
53 assert(isPRINT_A(toCTRL('{')));
32d02813
KW
54
55 /* diag_listed_as: Use "%s" instead of "%s" */
421e43ba
KW
56 Perl_croak(aTHX_ "Use \"%c\" instead of \"\\c{\"", toCTRL('{'));
57 }
68b355dd
KW
58
59 result = toCTRL(source);
5e784d58 60 if (output_warning && isPRINT_A(result)) {
4d8be631
KW
61 U8 clearer[3];
62 U8 i = 0;
63 if (! isWORDCHAR(result)) {
64 clearer[i++] = '\\';
65 }
66 clearer[i++] = result;
67 clearer[i++] = '\0';
68b355dd 68
4d8be631
KW
69 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
70 "\"\\c%c\" is more clearly written simply as \"%s\"",
71 source,
72 clearer);
68b355dd
KW
73 }
74
75 return result;
76}
77
db30362b 78STATIC bool
00ce5563 79S_grok_bslash_o(pTHX_ char **s, UV *uv, const char** error_msg,
80f4111b 80 const bool output_warning, const bool strict,
17896a85 81 const bool silence_non_portable,
80f4111b 82 const bool UTF)
db30362b
KW
83{
84
85/* Documentation to be supplied when interface nailed down finally
86 * This returns FALSE if there is an error which the caller need not recover
87 * from; , otherwise TRUE. In either case the caller should look at *len
88 * On input:
00ce5563
KW
89 * s is the address of a pointer to a NULL terminated string that begins
90 * with 'o', and the previous character was a backslash. At exit, *s
91 * will be advanced to the byte just after those absorbed by this
92 * function. Hence the caller can continue parsing from there. In
93 * the case of an error, this routine has generally positioned *s to
94 * point just to the right of the first bad spot, so that a message
95 * that has a "<--" to mark the spot will be correctly positioned.
db30362b
KW
96 * uv points to a UV that will hold the output value, valid only if the
97 * return from the function is TRUE
db30362b
KW
98 * error_msg is a pointer that will be set to an internal buffer giving an
99 * error message upon failure (the return is FALSE). Untouched if
100 * function succeeds
101 * output_warning says whether to output any warning messages, or suppress
102 * them
80f4111b
KW
103 * strict is true if this should fail instead of warn if there are
104 * non-octal digits within the braces
17896a85
KW
105 * silence_non_portable is true if to suppress warnings about the code
106 * point returned being too large to fit on all platforms.
80f4111b 107 * UTF is true iff the string *s is encoded in UTF-8.
db30362b 108 */
00ce5563 109 char* e;
db30362b
KW
110 STRLEN numbers_len;
111 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
112 | PERL_SCAN_DISALLOW_PREFIX
113 /* XXX Until the message is improved in grok_oct, handle errors
114 * ourselves */
115 | PERL_SCAN_SILENT_ILLDIGIT;
116
117 PERL_ARGS_ASSERT_GROK_BSLASH_O;
118
119
00ce5563
KW
120 assert(**s == 'o');
121 (*s)++;
db30362b 122
00ce5563 123 if (**s != '{') {
db30362b
KW
124 *error_msg = "Missing braces on \\o{}";
125 return FALSE;
126 }
127
00ce5563 128 e = strchr(*s, '}');
db30362b 129 if (!e) {
00ce5563 130 (*s)++; /* Move past the '{' */
b8de99ca
KW
131 while (isOCTAL(**s)) { /* Position beyond the legal digits */
132 (*s)++;
133 }
00ce5563 134 *error_msg = "Missing right brace on \\o{";
db30362b
KW
135 return FALSE;
136 }
137
00ce5563
KW
138 (*s)++; /* Point to expected first digit (could be first byte of utf8
139 sequence if not a digit) */
140 numbers_len = e - *s;
db30362b 141 if (numbers_len == 0) {
00ce5563 142 (*s)++; /* Move past the } */
db30362b
KW
143 *error_msg = "Number with no digits";
144 return FALSE;
145 }
146
17896a85
KW
147 if (silence_non_portable) {
148 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
149 }
150
00ce5563 151 *uv = grok_oct(*s, &numbers_len, &flags, NULL);
db30362b
KW
152 /* Note that if has non-octal, will ignore everything starting with that up
153 * to the '}' */
154
80f4111b
KW
155 if (numbers_len != (STRLEN) (e - *s)) {
156 if (strict) {
157 *s += numbers_len;
158 *s += (UTF) ? UTF8SKIP(*s) : (STRLEN) 1;
159 *error_msg = "Non-octal character";
160 return FALSE;
161 }
162 else if (output_warning) {
b67d718a
KW
163 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
164 /* diag_listed_as: Non-octal character '%c'. Resolved as "%s" */
165 "Non-octal character '%c'. Resolved as \"\\o{%.*s}\"",
166 *(*s + numbers_len),
167 (int) numbers_len,
168 *s);
80f4111b 169 }
db30362b
KW
170 }
171
00ce5563
KW
172 /* Return past the '}' */
173 *s = e + 1;
174
db30362b
KW
175 return TRUE;
176}
177
a0481293 178PERL_STATIC_INLINE bool
00ce5563 179S_grok_bslash_x(pTHX_ char **s, UV *uv, const char** error_msg,
80f4111b 180 const bool output_warning, const bool strict,
17896a85 181 const bool silence_non_portable,
80f4111b 182 const bool UTF)
a0481293
KW
183{
184
185/* Documentation to be supplied when interface nailed down finally
186 * This returns FALSE if there is an error which the caller need not recover
187 * from; , otherwise TRUE. In either case the caller should look at *len
188 * On input:
00ce5563
KW
189 * s is the address of a pointer to a NULL terminated string that begins
190 * with 'x', and the previous character was a backslash. At exit, *s
191 * will be advanced to the byte just after those absorbed by this
192 * function. Hence the caller can continue parsing from there. In
193 * the case of an error, this routine has generally positioned *s to
194 * point just to the right of the first bad spot, so that a message
195 * that has a "<--" to mark the spot will be correctly positioned.
a0481293
KW
196 * uv points to a UV that will hold the output value, valid only if the
197 * return from the function is TRUE
a0481293
KW
198 * error_msg is a pointer that will be set to an internal buffer giving an
199 * error message upon failure (the return is FALSE). Untouched if
200 * function succeeds
201 * output_warning says whether to output any warning messages, or suppress
202 * them
80f4111b
KW
203 * strict is true if anything out of the ordinary should cause this to
204 * fail instead of warn or be silent. For example, it requires
205 * exactly 2 digits following the \x (when there are no braces).
206 * 3 digits could be a mistake, so is forbidden in this mode.
344451ec
KW
207 * silence_non_portable is true if to suppress warnings about the code
208 * point returned being too large to fit on all platforms.
80f4111b 209 * UTF is true iff the string *s is encoded in UTF-8.
a0481293 210 */
00ce5563 211 char* e;
a0481293 212 STRLEN numbers_len;
344451ec 213 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
a0481293
KW
214
215 PERL_ARGS_ASSERT_GROK_BSLASH_X;
216
4f8dbb2d 217 PERL_UNUSED_ARG(output_warning);
a0481293 218
00ce5563
KW
219 assert(**s == 'x');
220 (*s)++;
a0481293 221
80f4111b
KW
222 if (strict) {
223 flags |= PERL_SCAN_SILENT_ILLDIGIT;
224 }
225
00ce5563 226 if (**s != '{') {
80f4111b
KW
227 STRLEN len = (strict) ? 3 : 2;
228
00ce5563
KW
229 *uv = grok_hex(*s, &len, &flags, NULL);
230 *s += len;
80f4111b
KW
231 if (strict && len != 2) {
232 if (len < 2) {
233 *s += (UTF) ? UTF8SKIP(*s) : 1;
234 *error_msg = "Non-hex character";
235 }
236 else {
237 *error_msg = "Use \\x{...} for more than two hex characters";
238 }
239 return FALSE;
240 }
a0481293
KW
241 return TRUE;
242 }
243
00ce5563 244 e = strchr(*s, '}');
a0481293 245 if (!e) {
00ce5563 246 (*s)++; /* Move past the '{' */
b8de99ca
KW
247 while (isXDIGIT(**s)) { /* Position beyond the legal digits */
248 (*s)++;
249 }
a0481293
KW
250 /* XXX The corresponding message above for \o is just '\\o{'; other
251 * messages for other constructs include the '}', so are inconsistent.
252 */
253 *error_msg = "Missing right brace on \\x{}";
254 return FALSE;
255 }
256
00ce5563
KW
257 (*s)++; /* Point to expected first digit (could be first byte of utf8
258 sequence if not a digit) */
259 numbers_len = e - *s;
80f4111b
KW
260 if (numbers_len == 0) {
261 if (strict) {
262 (*s)++; /* Move past the } */
263 *error_msg = "Number with no digits";
264 return FALSE;
265 }
266 return TRUE;
267 }
268
344451ec 269 flags |= PERL_SCAN_ALLOW_UNDERSCORES;
17896a85
KW
270 if (silence_non_portable) {
271 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
272 }
80f4111b 273
00ce5563 274 *uv = grok_hex(*s, &numbers_len, &flags, NULL);
a0481293
KW
275 /* Note that if has non-hex, will ignore everything starting with that up
276 * to the '}' */
277
80f4111b
KW
278 if (strict && numbers_len != (STRLEN) (e - *s)) {
279 *s += numbers_len;
280 *s += (UTF) ? UTF8SKIP(*s) : 1;
281 *error_msg = "Non-hex character";
282 return FALSE;
283 }
284
00ce5563
KW
285 /* Return past the '}' */
286 *s = e + 1;
287
a0481293
KW
288 return TRUE;
289}
290
5e0a247b
KW
291STATIC char*
292S_form_short_octal_warning(pTHX_
293 const char * const s, /* Points to first non-octal */
294 const STRLEN len /* Length of octals string, so
295 (s-len) points to first
296 octal */
297) {
298 /* Return a character string consisting of a warning message for when a
299 * string constant in octal is weird, like "\078". */
300
301 const char * sans_leading_zeros = s - len;
302
303 PERL_ARGS_ASSERT_FORM_SHORT_OCTAL_WARNING;
304
305 assert(*s == '8' || *s == '9');
306
307 /* Remove the leading zeros, retaining one zero so won't be zero length */
308 while (*sans_leading_zeros == '0') sans_leading_zeros++;
309 if (sans_leading_zeros == s) {
310 sans_leading_zeros--;
311 }
312
313 return Perl_form(aTHX_
314 "'%.*s' resolved to '\\o{%.*s}%c'",
315 (int) (len + 2), s - len - 1,
316 (int) (s - sans_leading_zeros), sans_leading_zeros,
317 *s);
318}
319
04e98a4d
AD
320/*
321 * Local variables:
322 * c-indentation-style: bsd
323 * c-basic-offset: 4
14d04a33 324 * indent-tabs-mode: nil
04e98a4d
AD
325 * End:
326 *
14d04a33 327 * ex: set ts=8 sts=4 sw=4 et:
04e98a4d 328 */