This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add 5.21.10 epigraph
[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 52 else if (source == '{') {
a27ed980
KW
53 const char control = toCTRL('{');
54 if (isPRINT_A(control)) {
55 /* diag_listed_as: Use "%s" instead of "%s" */
56 Perl_croak(aTHX_ "Use \"%c\" instead of \"\\c{\"", control);
57 }
58 else {
59 Perl_croak(aTHX_ "Sequence \"\\c{\" invalid");
60 }
421e43ba 61 }
68b355dd
KW
62
63 result = toCTRL(source);
5e784d58 64 if (output_warning && isPRINT_A(result)) {
4d8be631
KW
65 U8 clearer[3];
66 U8 i = 0;
67 if (! isWORDCHAR(result)) {
68 clearer[i++] = '\\';
69 }
70 clearer[i++] = result;
71 clearer[i++] = '\0';
68b355dd 72
4d8be631
KW
73 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
74 "\"\\c%c\" is more clearly written simply as \"%s\"",
75 source,
76 clearer);
68b355dd
KW
77 }
78
79 return result;
80}
81
db30362b 82STATIC bool
00ce5563 83S_grok_bslash_o(pTHX_ char **s, UV *uv, const char** error_msg,
80f4111b 84 const bool output_warning, const bool strict,
17896a85 85 const bool silence_non_portable,
80f4111b 86 const bool UTF)
db30362b
KW
87{
88
89/* Documentation to be supplied when interface nailed down finally
90 * This returns FALSE if there is an error which the caller need not recover
61caa4da
DM
91 * from; otherwise TRUE. In either case the caller should look at *len [???].
92 * It guarantees that the returned codepoint, *uv, when expressed as
7a4ca5b4 93 * utf8 bytes, would fit within the skipped "\o{...}" bytes.
db30362b 94 * On input:
00ce5563
KW
95 * s is the address of a pointer to a NULL terminated string that begins
96 * with 'o', and the previous character was a backslash. At exit, *s
97 * will be advanced to the byte just after those absorbed by this
98 * function. Hence the caller can continue parsing from there. In
99 * the case of an error, this routine has generally positioned *s to
100 * point just to the right of the first bad spot, so that a message
101 * that has a "<--" to mark the spot will be correctly positioned.
db30362b
KW
102 * uv points to a UV that will hold the output value, valid only if the
103 * return from the function is TRUE
db30362b
KW
104 * error_msg is a pointer that will be set to an internal buffer giving an
105 * error message upon failure (the return is FALSE). Untouched if
106 * function succeeds
107 * output_warning says whether to output any warning messages, or suppress
108 * them
80f4111b
KW
109 * strict is true if this should fail instead of warn if there are
110 * non-octal digits within the braces
17896a85
KW
111 * silence_non_portable is true if to suppress warnings about the code
112 * point returned being too large to fit on all platforms.
80f4111b 113 * UTF is true iff the string *s is encoded in UTF-8.
db30362b 114 */
00ce5563 115 char* e;
db30362b
KW
116 STRLEN numbers_len;
117 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
118 | PERL_SCAN_DISALLOW_PREFIX
119 /* XXX Until the message is improved in grok_oct, handle errors
120 * ourselves */
121 | PERL_SCAN_SILENT_ILLDIGIT;
122
61caa4da
DM
123#ifdef DEBUGGING
124 char *start = *s - 1;
125 assert(*start == '\\');
126#endif
127
db30362b
KW
128 PERL_ARGS_ASSERT_GROK_BSLASH_O;
129
130
00ce5563
KW
131 assert(**s == 'o');
132 (*s)++;
db30362b 133
00ce5563 134 if (**s != '{') {
db30362b
KW
135 *error_msg = "Missing braces on \\o{}";
136 return FALSE;
137 }
138
00ce5563 139 e = strchr(*s, '}');
db30362b 140 if (!e) {
00ce5563 141 (*s)++; /* Move past the '{' */
b8de99ca
KW
142 while (isOCTAL(**s)) { /* Position beyond the legal digits */
143 (*s)++;
144 }
00ce5563 145 *error_msg = "Missing right brace on \\o{";
db30362b
KW
146 return FALSE;
147 }
148
00ce5563
KW
149 (*s)++; /* Point to expected first digit (could be first byte of utf8
150 sequence if not a digit) */
151 numbers_len = e - *s;
db30362b 152 if (numbers_len == 0) {
00ce5563 153 (*s)++; /* Move past the } */
db30362b
KW
154 *error_msg = "Number with no digits";
155 return FALSE;
156 }
157
17896a85
KW
158 if (silence_non_portable) {
159 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
160 }
161
00ce5563 162 *uv = grok_oct(*s, &numbers_len, &flags, NULL);
db30362b
KW
163 /* Note that if has non-octal, will ignore everything starting with that up
164 * to the '}' */
165
80f4111b
KW
166 if (numbers_len != (STRLEN) (e - *s)) {
167 if (strict) {
168 *s += numbers_len;
169 *s += (UTF) ? UTF8SKIP(*s) : (STRLEN) 1;
170 *error_msg = "Non-octal character";
171 return FALSE;
172 }
173 else if (output_warning) {
b67d718a
KW
174 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
175 /* diag_listed_as: Non-octal character '%c'. Resolved as "%s" */
176 "Non-octal character '%c'. Resolved as \"\\o{%.*s}\"",
177 *(*s + numbers_len),
178 (int) numbers_len,
179 *s);
80f4111b 180 }
db30362b
KW
181 }
182
00ce5563
KW
183 /* Return past the '}' */
184 *s = e + 1;
185
7a4ca5b4 186 /* guarantee replacing "\o{...}" with utf8 bytes fits within
61caa4da
DM
187 * existing space */
188 assert(OFFUNISKIP(*uv) < *s - start);
189
db30362b
KW
190 return TRUE;
191}
192
a0481293 193PERL_STATIC_INLINE bool
00ce5563 194S_grok_bslash_x(pTHX_ char **s, UV *uv, const char** error_msg,
80f4111b 195 const bool output_warning, const bool strict,
17896a85 196 const bool silence_non_portable,
80f4111b 197 const bool UTF)
a0481293
KW
198{
199
200/* Documentation to be supplied when interface nailed down finally
201 * This returns FALSE if there is an error which the caller need not recover
61caa4da
DM
202 * from; otherwise TRUE.
203 * It guarantees that the returned codepoint, *uv, when expressed as
204 * utf8 bytes, would fit within the skipped "\x{...}" bytes.
205 *
a0481293 206 * On input:
00ce5563
KW
207 * s is the address of a pointer to a NULL terminated string that begins
208 * with 'x', and the previous character was a backslash. At exit, *s
209 * will be advanced to the byte just after those absorbed by this
210 * function. Hence the caller can continue parsing from there. In
211 * the case of an error, this routine has generally positioned *s to
212 * point just to the right of the first bad spot, so that a message
213 * that has a "<--" to mark the spot will be correctly positioned.
a0481293
KW
214 * uv points to a UV that will hold the output value, valid only if the
215 * return from the function is TRUE
a0481293
KW
216 * error_msg is a pointer that will be set to an internal buffer giving an
217 * error message upon failure (the return is FALSE). Untouched if
218 * function succeeds
219 * output_warning says whether to output any warning messages, or suppress
220 * them
80f4111b
KW
221 * strict is true if anything out of the ordinary should cause this to
222 * fail instead of warn or be silent. For example, it requires
223 * exactly 2 digits following the \x (when there are no braces).
224 * 3 digits could be a mistake, so is forbidden in this mode.
344451ec
KW
225 * silence_non_portable is true if to suppress warnings about the code
226 * point returned being too large to fit on all platforms.
80f4111b 227 * UTF is true iff the string *s is encoded in UTF-8.
a0481293 228 */
00ce5563 229 char* e;
a0481293 230 STRLEN numbers_len;
344451ec 231 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
61caa4da
DM
232#ifdef DEBUGGING
233 char *start = *s - 1;
234 assert(*start == '\\');
235#endif
a0481293
KW
236
237 PERL_ARGS_ASSERT_GROK_BSLASH_X;
238
00ce5563
KW
239 assert(**s == 'x');
240 (*s)++;
a0481293 241
879eb604 242 if (strict || ! output_warning) {
80f4111b
KW
243 flags |= PERL_SCAN_SILENT_ILLDIGIT;
244 }
245
00ce5563 246 if (**s != '{') {
80f4111b
KW
247 STRLEN len = (strict) ? 3 : 2;
248
00ce5563
KW
249 *uv = grok_hex(*s, &len, &flags, NULL);
250 *s += len;
80f4111b
KW
251 if (strict && len != 2) {
252 if (len < 2) {
253 *s += (UTF) ? UTF8SKIP(*s) : 1;
254 *error_msg = "Non-hex character";
255 }
256 else {
257 *error_msg = "Use \\x{...} for more than two hex characters";
258 }
259 return FALSE;
260 }
61caa4da 261 goto ok;
a0481293
KW
262 }
263
00ce5563 264 e = strchr(*s, '}');
a0481293 265 if (!e) {
00ce5563 266 (*s)++; /* Move past the '{' */
b8de99ca
KW
267 while (isXDIGIT(**s)) { /* Position beyond the legal digits */
268 (*s)++;
269 }
a0481293
KW
270 /* XXX The corresponding message above for \o is just '\\o{'; other
271 * messages for other constructs include the '}', so are inconsistent.
272 */
273 *error_msg = "Missing right brace on \\x{}";
274 return FALSE;
275 }
276
00ce5563
KW
277 (*s)++; /* Point to expected first digit (could be first byte of utf8
278 sequence if not a digit) */
279 numbers_len = e - *s;
80f4111b
KW
280 if (numbers_len == 0) {
281 if (strict) {
282 (*s)++; /* Move past the } */
283 *error_msg = "Number with no digits";
284 return FALSE;
285 }
eb9a585f
HS
286 *s = e + 1;
287 *uv = 0;
61caa4da 288 goto ok;
80f4111b
KW
289 }
290
344451ec 291 flags |= PERL_SCAN_ALLOW_UNDERSCORES;
17896a85
KW
292 if (silence_non_portable) {
293 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
294 }
80f4111b 295
00ce5563 296 *uv = grok_hex(*s, &numbers_len, &flags, NULL);
a0481293
KW
297 /* Note that if has non-hex, will ignore everything starting with that up
298 * to the '}' */
299
80f4111b
KW
300 if (strict && numbers_len != (STRLEN) (e - *s)) {
301 *s += numbers_len;
302 *s += (UTF) ? UTF8SKIP(*s) : 1;
303 *error_msg = "Non-hex character";
304 return FALSE;
305 }
306
00ce5563
KW
307 /* Return past the '}' */
308 *s = e + 1;
309
61caa4da
DM
310 ok:
311 /* guarantee replacing "\x{...}" with utf8 bytes fits within
312 * existing space */
313 assert(OFFUNISKIP(*uv) < *s - start);
a0481293
KW
314 return TRUE;
315}
316
5e0a247b
KW
317STATIC char*
318S_form_short_octal_warning(pTHX_
319 const char * const s, /* Points to first non-octal */
320 const STRLEN len /* Length of octals string, so
321 (s-len) points to first
322 octal */
323) {
324 /* Return a character string consisting of a warning message for when a
325 * string constant in octal is weird, like "\078". */
326
327 const char * sans_leading_zeros = s - len;
328
329 PERL_ARGS_ASSERT_FORM_SHORT_OCTAL_WARNING;
330
331 assert(*s == '8' || *s == '9');
332
333 /* Remove the leading zeros, retaining one zero so won't be zero length */
334 while (*sans_leading_zeros == '0') sans_leading_zeros++;
335 if (sans_leading_zeros == s) {
336 sans_leading_zeros--;
337 }
338
339 return Perl_form(aTHX_
340 "'%.*s' resolved to '\\o{%.*s}%c'",
341 (int) (len + 2), s - len - 1,
342 (int) (s - sans_leading_zeros), sans_leading_zeros,
343 *s);
344}
345
04e98a4d
AD
346/*
347 * Local variables:
348 * c-indentation-style: bsd
349 * c-basic-offset: 4
14d04a33 350 * indent-tabs-mode: nil
04e98a4d
AD
351 * End:
352 *
14d04a33 353 * ex: set ts=8 sts=4 sw=4 et:
04e98a4d 354 */