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)
21 PERL_ARGS_ASSERT_REGCURLY;
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)
44 S_grok_bslash_c(pTHX_ const char source, const bool output_warning)
49 if (! isPRINT_A(source)) {
50 Perl_croak(aTHX_ "%s",
51 "Character following \"\\c\" must be printable ASCII");
53 else if (source == '{') {
54 assert(isPRINT_A(toCTRL('{')));
56 /* diag_listed_as: Use "%s" instead of "%s" */
57 Perl_croak(aTHX_ "Use \"%c\" instead of \"\\c{\"", toCTRL('{'));
60 result = toCTRL(source);
61 if (output_warning && isPRINT_A(result)) {
64 if (! isWORDCHAR(result)) {
67 clearer[i++] = result;
70 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
71 "\"\\c%c\" is more clearly written simply as \"%s\"",
80 S_grok_bslash_o(pTHX_ char **s, UV *uv, const char** error_msg,
81 const bool output_warning, const bool strict,
82 const bool silence_non_portable,
86 /* Documentation to be supplied when interface nailed down finally
87 * This returns FALSE if there is an error which the caller need not recover
88 * from; , otherwise TRUE. In either case the caller should look at *len
90 * s is the address of a pointer to a NULL terminated string that begins
91 * with 'o', and the previous character was a backslash. At exit, *s
92 * will be advanced to the byte just after those absorbed by this
93 * function. Hence the caller can continue parsing from there. In
94 * the case of an error, this routine has generally positioned *s to
95 * point just to the right of the first bad spot, so that a message
96 * that has a "<--" to mark the spot will be correctly positioned.
97 * uv points to a UV that will hold the output value, valid only if the
98 * return from the function is TRUE
99 * error_msg is a pointer that will be set to an internal buffer giving an
100 * error message upon failure (the return is FALSE). Untouched if
102 * output_warning says whether to output any warning messages, or suppress
104 * strict is true if this should fail instead of warn if there are
105 * non-octal digits within the braces
106 * silence_non_portable is true if to suppress warnings about the code
107 * point returned being too large to fit on all platforms.
108 * UTF is true iff the string *s is encoded in UTF-8.
112 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
113 | PERL_SCAN_DISALLOW_PREFIX
114 /* XXX Until the message is improved in grok_oct, handle errors
116 | PERL_SCAN_SILENT_ILLDIGIT;
118 PERL_ARGS_ASSERT_GROK_BSLASH_O;
125 *error_msg = "Missing braces on \\o{}";
131 (*s)++; /* Move past the '{' */
132 while (isOCTAL(**s)) { /* Position beyond the legal digits */
135 *error_msg = "Missing right brace on \\o{";
139 (*s)++; /* Point to expected first digit (could be first byte of utf8
140 sequence if not a digit) */
141 numbers_len = e - *s;
142 if (numbers_len == 0) {
143 (*s)++; /* Move past the } */
144 *error_msg = "Number with no digits";
148 if (silence_non_portable) {
149 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
152 *uv = grok_oct(*s, &numbers_len, &flags, NULL);
153 /* Note that if has non-octal, will ignore everything starting with that up
156 if (numbers_len != (STRLEN) (e - *s)) {
159 *s += (UTF) ? UTF8SKIP(*s) : (STRLEN) 1;
160 *error_msg = "Non-octal character";
163 else if (output_warning) {
164 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
165 /* diag_listed_as: Non-octal character '%c'. Resolved as "%s" */
166 "Non-octal character '%c'. Resolved as \"\\o{%.*s}\"",
173 /* Return past the '}' */
179 PERL_STATIC_INLINE bool
180 S_grok_bslash_x(pTHX_ char **s, UV *uv, const char** error_msg,
181 const bool output_warning, const bool strict,
182 const bool silence_non_portable,
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
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.
197 * uv points to a UV that will hold the output value, valid only if the
198 * return from the function is TRUE
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
202 * output_warning says whether to output any warning messages, or suppress
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 * silence_non_portable is true if to suppress warnings about the code
209 * point returned being too large to fit on all platforms.
210 * UTF is true iff the string *s is encoded in UTF-8.
214 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
216 PERL_ARGS_ASSERT_GROK_BSLASH_X;
218 PERL_UNUSED_ARG(output_warning);
224 flags |= PERL_SCAN_SILENT_ILLDIGIT;
228 STRLEN len = (strict) ? 3 : 2;
230 *uv = grok_hex(*s, &len, &flags, NULL);
232 if (strict && len != 2) {
234 *s += (UTF) ? UTF8SKIP(*s) : 1;
235 *error_msg = "Non-hex character";
238 *error_msg = "Use \\x{...} for more than two hex characters";
247 (*s)++; /* Move past the '{' */
248 while (isXDIGIT(**s)) { /* Position beyond the legal digits */
251 /* XXX The corresponding message above for \o is just '\\o{'; other
252 * messages for other constructs include the '}', so are inconsistent.
254 *error_msg = "Missing right brace on \\x{}";
258 (*s)++; /* Point to expected first digit (could be first byte of utf8
259 sequence if not a digit) */
260 numbers_len = e - *s;
261 if (numbers_len == 0) {
263 (*s)++; /* Move past the } */
264 *error_msg = "Number with no digits";
270 flags |= PERL_SCAN_ALLOW_UNDERSCORES;
271 if (silence_non_portable) {
272 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
275 *uv = grok_hex(*s, &numbers_len, &flags, NULL);
276 /* Note that if has non-hex, will ignore everything starting with that up
279 if (strict && numbers_len != (STRLEN) (e - *s)) {
281 *s += (UTF) ? UTF8SKIP(*s) : 1;
282 *error_msg = "Non-hex character";
286 /* Return past the '}' */
293 S_form_short_octal_warning(pTHX_
294 const char * const s, /* Points to first non-octal */
295 const STRLEN len /* Length of octals string, so
296 (s-len) points to first
299 /* Return a character string consisting of a warning message for when a
300 * string constant in octal is weird, like "\078". */
302 const char * sans_leading_zeros = s - len;
304 PERL_ARGS_ASSERT_FORM_SHORT_OCTAL_WARNING;
306 assert(*s == '8' || *s == '9');
308 /* Remove the leading zeros, retaining one zero so won't be zero length */
309 while (*sans_leading_zeros == '0') sans_leading_zeros++;
310 if (sans_leading_zeros == s) {
311 sans_leading_zeros--;
314 return Perl_form(aTHX_
315 "'%.*s' resolved to '\\o{%.*s}%c'",
316 (int) (len + 2), s - len - 1,
317 (int) (s - sans_leading_zeros), sans_leading_zeros,
323 * c-indentation-style: bsd
325 * indent-tabs-mode: nil
328 * ex: set ts=8 sts=4 sw=4 et: