This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
dquote_static.c: White-space only; no code changes
[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
5aaab254 18S_regcurly(pTHX_ 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 }
04e98a4d
AD
33 if (*s != '}')
34 return FALSE;
35 return TRUE;
36}
db30362b 37
68b355dd
KW
38/* XXX Add documentation after final interface and behavior is decided */
39/* May want to show context for error, so would pass Perl_bslash_c(pTHX_ const char* current, const char* start, const bool output_warning)
40 U8 source = *current;
68b355dd
KW
41*/
42
43STATIC char
17a3df4c 44S_grok_bslash_c(pTHX_ const char source, const bool utf8, const bool output_warning)
68b355dd
KW
45{
46
47 U8 result;
48
17a3df4c
KW
49 if (utf8) {
50 /* Trying to deprecate non-ASCII usages. This construct has never
51 * worked for a utf8 variant. So, even though are accepting non-ASCII
52 * Latin1 in 5.14, no need to make them work under utf8 */
53 if (! isASCII(source)) {
54 Perl_croak(aTHX_ "Character following \"\\c\" must be ASCII");
55 }
68b355dd
KW
56 }
57
58 result = toCTRL(source);
17a3df4c
KW
59 if (! isASCII(source)) {
60 Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
61 "Character following \"\\c\" must be ASCII");
62 }
63 else if (! isCNTRL(result) && output_warning) {
68b355dd 64 if (source == '{') {
17a3df4c
KW
65 Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
66 "\"\\c{\" is deprecated and is more clearly written as \";\"");
68b355dd 67 }
17a3df4c 68 else {
68b355dd
KW
69 U8 clearer[3];
70 U8 i = 0;
0eb30aeb 71 if (! isWORDCHAR(result)) {
68b355dd
KW
72 clearer[i++] = '\\';
73 }
74 clearer[i++] = result;
75 clearer[i++] = '\0';
76
17a3df4c
KW
77 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
78 "\"\\c%c\" is more clearly written simply as \"%s\"",
68b355dd
KW
79 source,
80 clearer);
81 }
82 }
83
84 return result;
85}
86
db30362b 87STATIC bool
00ce5563 88S_grok_bslash_o(pTHX_ char **s, UV *uv, const char** error_msg,
80f4111b
KW
89 const bool output_warning, const bool strict,
90 const bool UTF)
db30362b
KW
91{
92
93/* Documentation to be supplied when interface nailed down finally
94 * This returns FALSE if there is an error which the caller need not recover
95 * from; , otherwise TRUE. In either case the caller should look at *len
96 * On input:
00ce5563
KW
97 * s is the address of a pointer to a NULL terminated string that begins
98 * with 'o', and the previous character was a backslash. At exit, *s
99 * will be advanced to the byte just after those absorbed by this
100 * function. Hence the caller can continue parsing from there. In
101 * the case of an error, this routine has generally positioned *s to
102 * point just to the right of the first bad spot, so that a message
103 * that has a "<--" to mark the spot will be correctly positioned.
db30362b
KW
104 * uv points to a UV that will hold the output value, valid only if the
105 * return from the function is TRUE
db30362b
KW
106 * error_msg is a pointer that will be set to an internal buffer giving an
107 * error message upon failure (the return is FALSE). Untouched if
108 * function succeeds
109 * output_warning says whether to output any warning messages, or suppress
110 * them
80f4111b
KW
111 * strict is true if this should fail instead of warn if there are
112 * non-octal digits within the braces
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
123 PERL_ARGS_ASSERT_GROK_BSLASH_O;
124
125
00ce5563
KW
126 assert(**s == 'o');
127 (*s)++;
db30362b 128
00ce5563 129 if (**s != '{') {
db30362b
KW
130 *error_msg = "Missing braces on \\o{}";
131 return FALSE;
132 }
133
00ce5563 134 e = strchr(*s, '}');
db30362b 135 if (!e) {
00ce5563 136 (*s)++; /* Move past the '{' */
b8de99ca
KW
137 while (isOCTAL(**s)) { /* Position beyond the legal digits */
138 (*s)++;
139 }
00ce5563 140 *error_msg = "Missing right brace on \\o{";
db30362b
KW
141 return FALSE;
142 }
143
00ce5563
KW
144 (*s)++; /* Point to expected first digit (could be first byte of utf8
145 sequence if not a digit) */
146 numbers_len = e - *s;
db30362b 147 if (numbers_len == 0) {
00ce5563 148 (*s)++; /* Move past the } */
db30362b
KW
149 *error_msg = "Number with no digits";
150 return FALSE;
151 }
152
00ce5563 153 *uv = grok_oct(*s, &numbers_len, &flags, NULL);
db30362b
KW
154 /* Note that if has non-octal, will ignore everything starting with that up
155 * to the '}' */
156
80f4111b
KW
157 if (numbers_len != (STRLEN) (e - *s)) {
158 if (strict) {
159 *s += numbers_len;
160 *s += (UTF) ? UTF8SKIP(*s) : (STRLEN) 1;
161 *error_msg = "Non-octal character";
162 return FALSE;
163 }
164 else if (output_warning) {
b67d718a
KW
165 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
166 /* diag_listed_as: Non-octal character '%c'. Resolved as "%s" */
167 "Non-octal character '%c'. Resolved as \"\\o{%.*s}\"",
168 *(*s + numbers_len),
169 (int) numbers_len,
170 *s);
80f4111b 171 }
db30362b
KW
172 }
173
00ce5563
KW
174 /* Return past the '}' */
175 *s = e + 1;
176
db30362b
KW
177 return TRUE;
178}
179
a0481293 180PERL_STATIC_INLINE bool
00ce5563 181S_grok_bslash_x(pTHX_ char **s, UV *uv, const char** error_msg,
80f4111b
KW
182 const bool output_warning, const bool strict,
183 const bool UTF)
a0481293
KW
184{
185
186/* Documentation to be supplied when interface nailed down finally
187 * This returns FALSE if there is an error which the caller need not recover
188 * from; , otherwise TRUE. In either case the caller should look at *len
189 * On input:
00ce5563
KW
190 * s is the address of a pointer to a NULL terminated string that begins
191 * with 'x', and the previous character was a backslash. At exit, *s
192 * will be advanced to the byte just after those absorbed by this
193 * function. Hence the caller can continue parsing from there. In
194 * the case of an error, this routine has generally positioned *s to
195 * point just to the right of the first bad spot, so that a message
196 * that has a "<--" to mark the spot will be correctly positioned.
a0481293
KW
197 * uv points to a UV that will hold the output value, valid only if the
198 * return from the function is TRUE
a0481293
KW
199 * error_msg is a pointer that will be set to an internal buffer giving an
200 * error message upon failure (the return is FALSE). Untouched if
201 * function succeeds
202 * output_warning says whether to output any warning messages, or suppress
203 * them
80f4111b
KW
204 * strict is true if anything out of the ordinary should cause this to
205 * fail instead of warn or be silent. For example, it requires
206 * exactly 2 digits following the \x (when there are no braces).
207 * 3 digits could be a mistake, so is forbidden in this mode.
208 * UTF is true iff the string *s is encoded in UTF-8.
a0481293 209 */
00ce5563 210 char* e;
a0481293 211 STRLEN numbers_len;
80f4111b 212 I32 flags = 0;
a0481293
KW
213
214 PERL_ARGS_ASSERT_GROK_BSLASH_X;
215
4f8dbb2d 216 PERL_UNUSED_ARG(output_warning);
a0481293 217
00ce5563
KW
218 assert(**s == 'x');
219 (*s)++;
a0481293 220
80f4111b
KW
221 if (strict) {
222 flags |= PERL_SCAN_SILENT_ILLDIGIT;
223 }
224
00ce5563 225 if (**s != '{') {
80f4111b
KW
226 STRLEN len = (strict) ? 3 : 2;
227
228 flags |= PERL_SCAN_DISALLOW_PREFIX;
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
269 flags |= PERL_SCAN_ALLOW_UNDERSCORES|PERL_SCAN_DISALLOW_PREFIX;
270
00ce5563 271 *uv = grok_hex(*s, &numbers_len, &flags, NULL);
a0481293
KW
272 /* Note that if has non-hex, will ignore everything starting with that up
273 * to the '}' */
274
80f4111b
KW
275 if (strict && numbers_len != (STRLEN) (e - *s)) {
276 *s += numbers_len;
277 *s += (UTF) ? UTF8SKIP(*s) : 1;
278 *error_msg = "Non-hex character";
279 return FALSE;
280 }
281
00ce5563
KW
282 /* Return past the '}' */
283 *s = e + 1;
284
a0481293
KW
285 return TRUE;
286}
287
04e98a4d
AD
288/*
289 * Local variables:
290 * c-indentation-style: bsd
291 * c-basic-offset: 4
14d04a33 292 * indent-tabs-mode: nil
04e98a4d
AD
293 * End:
294 *
14d04a33 295 * ex: set ts=8 sts=4 sw=4 et:
04e98a4d 296 */