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