3 * This file contains static functions that are related to
4 * parsing double-quotish expressions, but are used in more than
7 * It is currently #included by regcomp.c and toke.c.
10 #define PERL_IN_DQUOTE_STATIC_C
14 - regcurly - a little FSA that accepts {\d+,?\d*}
15 Pulled from regcomp.c.
17 PERL_STATIC_INLINE I32
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 */
24 PERL_ARGS_ASSERT_REGCURLY;
38 return (rbrace_must_be_escaped)
39 ? *s == '\\' && *(s+1) == '}'
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)
49 S_grok_bslash_c(pTHX_ const char source, const bool output_warning)
54 if (! isPRINT_A(source)) {
55 const char msg[] = "Character following \"\\c\" must be printable ASCII";
56 if (! isASCII(source)) {
57 Perl_croak(aTHX_ "%s", msg);
59 else if (output_warning) { /* Unprintables can be removed in v5.22 */
60 Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), "%s",
64 else if (source == '{') {
65 assert(isPRINT_A(toCTRL('{')));
67 /* diag_listed_as: Use "%s" instead of "%s" */
68 Perl_croak(aTHX_ "Use \"%c\" instead of \"\\c{\"", toCTRL('{'));
71 result = toCTRL(source);
72 if (output_warning && ! isCNTRL_L1(result)) {
73 /* We use isCNTRL_L1 above and not simply isCNTRL, because on EBCDIC
74 * machines, things like \cT map into a C1 control. */
77 if (! isWORDCHAR(result)) {
80 clearer[i++] = result;
83 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
84 "\"\\c%c\" is more clearly written simply as \"%s\"",
93 S_grok_bslash_o(pTHX_ char **s, UV *uv, const char** error_msg,
94 const bool output_warning, const bool strict,
95 const bool silence_non_portable,
99 /* Documentation to be supplied when interface nailed down finally
100 * This returns FALSE if there is an error which the caller need not recover
101 * from; , otherwise TRUE. In either case the caller should look at *len
103 * s is the address of a pointer to a NULL terminated string that begins
104 * with 'o', and the previous character was a backslash. At exit, *s
105 * will be advanced to the byte just after those absorbed by this
106 * function. Hence the caller can continue parsing from there. In
107 * the case of an error, this routine has generally positioned *s to
108 * point just to the right of the first bad spot, so that a message
109 * that has a "<--" to mark the spot will be correctly positioned.
110 * uv points to a UV that will hold the output value, valid only if the
111 * return from the function is TRUE
112 * error_msg is a pointer that will be set to an internal buffer giving an
113 * error message upon failure (the return is FALSE). Untouched if
115 * output_warning says whether to output any warning messages, or suppress
117 * strict is true if this should fail instead of warn if there are
118 * non-octal digits within the braces
119 * silence_non_portable is true if to suppress warnings about the code
120 * point returned being too large to fit on all platforms.
121 * UTF is true iff the string *s is encoded in UTF-8.
125 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
126 | PERL_SCAN_DISALLOW_PREFIX
127 /* XXX Until the message is improved in grok_oct, handle errors
129 | PERL_SCAN_SILENT_ILLDIGIT;
131 PERL_ARGS_ASSERT_GROK_BSLASH_O;
138 *error_msg = "Missing braces on \\o{}";
144 (*s)++; /* Move past the '{' */
145 while (isOCTAL(**s)) { /* Position beyond the legal digits */
148 *error_msg = "Missing right brace on \\o{";
152 (*s)++; /* Point to expected first digit (could be first byte of utf8
153 sequence if not a digit) */
154 numbers_len = e - *s;
155 if (numbers_len == 0) {
156 (*s)++; /* Move past the } */
157 *error_msg = "Number with no digits";
161 if (silence_non_portable) {
162 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
165 *uv = grok_oct(*s, &numbers_len, &flags, NULL);
166 /* Note that if has non-octal, will ignore everything starting with that up
169 if (numbers_len != (STRLEN) (e - *s)) {
172 *s += (UTF) ? UTF8SKIP(*s) : (STRLEN) 1;
173 *error_msg = "Non-octal character";
176 else if (output_warning) {
177 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
178 /* diag_listed_as: Non-octal character '%c'. Resolved as "%s" */
179 "Non-octal character '%c'. Resolved as \"\\o{%.*s}\"",
186 /* Return past the '}' */
192 PERL_STATIC_INLINE bool
193 S_grok_bslash_x(pTHX_ char **s, UV *uv, const char** error_msg,
194 const bool output_warning, const bool strict,
195 const bool silence_non_portable,
199 /* Documentation to be supplied when interface nailed down finally
200 * This returns FALSE if there is an error which the caller need not recover
201 * from; , otherwise TRUE. In either case the caller should look at *len
203 * s is the address of a pointer to a NULL terminated string that begins
204 * with 'x', and the previous character was a backslash. At exit, *s
205 * will be advanced to the byte just after those absorbed by this
206 * function. Hence the caller can continue parsing from there. In
207 * the case of an error, this routine has generally positioned *s to
208 * point just to the right of the first bad spot, so that a message
209 * that has a "<--" to mark the spot will be correctly positioned.
210 * uv points to a UV that will hold the output value, valid only if the
211 * return from the function is TRUE
212 * error_msg is a pointer that will be set to an internal buffer giving an
213 * error message upon failure (the return is FALSE). Untouched if
215 * output_warning says whether to output any warning messages, or suppress
217 * strict is true if anything out of the ordinary should cause this to
218 * fail instead of warn or be silent. For example, it requires
219 * exactly 2 digits following the \x (when there are no braces).
220 * 3 digits could be a mistake, so is forbidden in this mode.
221 * silence_non_portable is true if to suppress warnings about the code
222 * point returned being too large to fit on all platforms.
223 * UTF is true iff the string *s is encoded in UTF-8.
227 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
229 PERL_ARGS_ASSERT_GROK_BSLASH_X;
231 PERL_UNUSED_ARG(output_warning);
237 flags |= PERL_SCAN_SILENT_ILLDIGIT;
241 STRLEN len = (strict) ? 3 : 2;
243 *uv = grok_hex(*s, &len, &flags, NULL);
245 if (strict && len != 2) {
247 *s += (UTF) ? UTF8SKIP(*s) : 1;
248 *error_msg = "Non-hex character";
251 *error_msg = "Use \\x{...} for more than two hex characters";
260 (*s)++; /* Move past the '{' */
261 while (isXDIGIT(**s)) { /* Position beyond the legal digits */
264 /* XXX The corresponding message above for \o is just '\\o{'; other
265 * messages for other constructs include the '}', so are inconsistent.
267 *error_msg = "Missing right brace on \\x{}";
271 (*s)++; /* Point to expected first digit (could be first byte of utf8
272 sequence if not a digit) */
273 numbers_len = e - *s;
274 if (numbers_len == 0) {
276 (*s)++; /* Move past the } */
277 *error_msg = "Number with no digits";
283 flags |= PERL_SCAN_ALLOW_UNDERSCORES;
284 if (silence_non_portable) {
285 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
288 *uv = grok_hex(*s, &numbers_len, &flags, NULL);
289 /* Note that if has non-hex, will ignore everything starting with that up
292 if (strict && numbers_len != (STRLEN) (e - *s)) {
294 *s += (UTF) ? UTF8SKIP(*s) : 1;
295 *error_msg = "Non-hex character";
299 /* Return past the '}' */
306 S_form_short_octal_warning(pTHX_
307 const char * const s, /* Points to first non-octal */
308 const STRLEN len /* Length of octals string, so
309 (s-len) points to first
312 /* Return a character string consisting of a warning message for when a
313 * string constant in octal is weird, like "\078". */
315 const char * sans_leading_zeros = s - len;
317 PERL_ARGS_ASSERT_FORM_SHORT_OCTAL_WARNING;
319 assert(*s == '8' || *s == '9');
321 /* Remove the leading zeros, retaining one zero so won't be zero length */
322 while (*sans_leading_zeros == '0') sans_leading_zeros++;
323 if (sans_leading_zeros == s) {
324 sans_leading_zeros--;
327 return Perl_form(aTHX_
328 "'%.*s' resolved to '\\o{%.*s}%c'",
329 (int) (len + 2), s - len - 1,
330 (int) (s - sans_leading_zeros), sans_leading_zeros,
336 * c-indentation-style: bsd
338 * indent-tabs-mode: nil
341 * ex: set ts=8 sts=4 sw=4 et: