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 Perl_croak(aTHX_ "%s",
56 "Character following \"\\c\" must be printable ASCII");
58 else if (source == '{') {
59 assert(isPRINT_A(toCTRL('{')));
61 /* diag_listed_as: Use "%s" instead of "%s" */
62 Perl_croak(aTHX_ "Use \"%c\" instead of \"\\c{\"", toCTRL('{'));
65 result = toCTRL(source);
66 if (output_warning && isPRINT_A(result)) {
69 if (! isWORDCHAR(result)) {
72 clearer[i++] = result;
75 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
76 "\"\\c%c\" is more clearly written simply as \"%s\"",
85 S_grok_bslash_o(pTHX_ char **s, UV *uv, const char** error_msg,
86 const bool output_warning, const bool strict,
87 const bool silence_non_portable,
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
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.
102 * uv points to a UV that will hold the output value, valid only if the
103 * return from the function is TRUE
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
107 * output_warning says whether to output any warning messages, or suppress
109 * strict is true if this should fail instead of warn if there are
110 * non-octal digits within the braces
111 * silence_non_portable is true if to suppress warnings about the code
112 * point returned being too large to fit on all platforms.
113 * UTF is true iff the string *s is encoded in UTF-8.
117 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
118 | PERL_SCAN_DISALLOW_PREFIX
119 /* XXX Until the message is improved in grok_oct, handle errors
121 | PERL_SCAN_SILENT_ILLDIGIT;
123 PERL_ARGS_ASSERT_GROK_BSLASH_O;
130 *error_msg = "Missing braces on \\o{}";
136 (*s)++; /* Move past the '{' */
137 while (isOCTAL(**s)) { /* Position beyond the legal digits */
140 *error_msg = "Missing right brace on \\o{";
144 (*s)++; /* Point to expected first digit (could be first byte of utf8
145 sequence if not a digit) */
146 numbers_len = e - *s;
147 if (numbers_len == 0) {
148 (*s)++; /* Move past the } */
149 *error_msg = "Number with no digits";
153 if (silence_non_portable) {
154 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
157 *uv = grok_oct(*s, &numbers_len, &flags, NULL);
158 /* Note that if has non-octal, will ignore everything starting with that up
161 if (numbers_len != (STRLEN) (e - *s)) {
164 *s += (UTF) ? UTF8SKIP(*s) : (STRLEN) 1;
165 *error_msg = "Non-octal character";
168 else if (output_warning) {
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}\"",
178 /* Return past the '}' */
184 PERL_STATIC_INLINE bool
185 S_grok_bslash_x(pTHX_ char **s, UV *uv, const char** error_msg,
186 const bool output_warning, const bool strict,
187 const bool silence_non_portable,
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
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.
202 * uv points to a UV that will hold the output value, valid only if the
203 * return from the function is TRUE
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
207 * output_warning says whether to output any warning messages, or suppress
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.
213 * silence_non_portable is true if to suppress warnings about the code
214 * point returned being too large to fit on all platforms.
215 * UTF is true iff the string *s is encoded in UTF-8.
219 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
221 PERL_ARGS_ASSERT_GROK_BSLASH_X;
223 PERL_UNUSED_ARG(output_warning);
229 flags |= PERL_SCAN_SILENT_ILLDIGIT;
233 STRLEN len = (strict) ? 3 : 2;
235 *uv = grok_hex(*s, &len, &flags, NULL);
237 if (strict && len != 2) {
239 *s += (UTF) ? UTF8SKIP(*s) : 1;
240 *error_msg = "Non-hex character";
243 *error_msg = "Use \\x{...} for more than two hex characters";
252 (*s)++; /* Move past the '{' */
253 while (isXDIGIT(**s)) { /* Position beyond the legal digits */
256 /* XXX The corresponding message above for \o is just '\\o{'; other
257 * messages for other constructs include the '}', so are inconsistent.
259 *error_msg = "Missing right brace on \\x{}";
263 (*s)++; /* Point to expected first digit (could be first byte of utf8
264 sequence if not a digit) */
265 numbers_len = e - *s;
266 if (numbers_len == 0) {
268 (*s)++; /* Move past the } */
269 *error_msg = "Number with no digits";
275 flags |= PERL_SCAN_ALLOW_UNDERSCORES;
276 if (silence_non_portable) {
277 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
280 *uv = grok_hex(*s, &numbers_len, &flags, NULL);
281 /* Note that if has non-hex, will ignore everything starting with that up
284 if (strict && numbers_len != (STRLEN) (e - *s)) {
286 *s += (UTF) ? UTF8SKIP(*s) : 1;
287 *error_msg = "Non-hex character";
291 /* Return past the '}' */
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
304 /* Return a character string consisting of a warning message for when a
305 * string constant in octal is weird, like "\078". */
307 const char * sans_leading_zeros = s - len;
309 PERL_ARGS_ASSERT_FORM_SHORT_OCTAL_WARNING;
311 assert(*s == '8' || *s == '9');
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--;
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,
328 * c-indentation-style: bsd
330 * indent-tabs-mode: nil
333 * ex: set ts=8 sts=4 sw=4 et: