3 * This file contains functions that are related to
4 * parsing double-quotish expressions.
9 #define PERL_IN_DQUOTE_C
11 #include "dquote_inline.h"
13 /* XXX Add documentation after final interface and behavior is decided */
14 /* May want to show context for error, so would pass S_grok_bslash_c(pTHX_ const char* current, const char* start, const bool output_warning)
19 Perl_grok_bslash_c(pTHX_ const char source, const bool output_warning)
24 if (! isPRINT_A(source)) {
25 Perl_croak(aTHX_ "%s",
26 "Character following \"\\c\" must be printable ASCII");
28 else if (source == '{') {
29 const char control = toCTRL('{');
30 if (isPRINT_A(control)) {
31 /* diag_listed_as: Use "%s" instead of "%s" */
32 Perl_croak(aTHX_ "Use \"%c\" instead of \"\\c{\"", control);
35 Perl_croak(aTHX_ "Sequence \"\\c{\" invalid");
39 result = toCTRL(source);
40 if (output_warning && isPRINT_A(result)) {
43 if (! isWORDCHAR(result)) {
46 clearer[i++] = result;
49 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
50 "\"\\c%c\" is more clearly written simply as \"%s\"",
59 Perl_grok_bslash_o(pTHX_ char **s, const char * const send, UV *uv,
60 const char** error_msg,
61 const bool output_warning, const bool strict,
62 const bool silence_non_portable,
66 /* Documentation to be supplied when interface nailed down finally
67 * This returns FALSE if there is an error which the caller need not recover
68 * from; otherwise TRUE. In either case the caller should look at *len [???].
69 * It guarantees that the returned codepoint, *uv, when expressed as
70 * utf8 bytes, would fit within the skipped "\o{...}" bytes.
72 * s is the address of a pointer to a string. **s is 'o', and the
73 * previous character was a backslash. At exit, *s will be advanced
74 * to the byte just after those absorbed by this function. Hence the
75 * caller can continue parsing from there. In the case of an error,
76 * this routine has generally positioned *s to point just to the right
77 * of the first bad spot, so that a message that has a "<--" to mark
78 * the spot will be correctly positioned.
79 * send - 1 gives a limit in *s that this function is not permitted to
80 * look beyond. That is, the function may look at bytes only in the
82 * uv points to a UV that will hold the output value, valid only if the
83 * return from the function is TRUE
84 * error_msg is a pointer that will be set to an internal buffer giving an
85 * error message upon failure (the return is FALSE). Untouched if
87 * output_warning says whether to output any warning messages, or suppress
89 * strict is true if this should fail instead of warn if there are
90 * non-octal digits within the braces
91 * silence_non_portable is true if to suppress warnings about the code
92 * point returned being too large to fit on all platforms.
93 * UTF is true iff the string *s is encoded in UTF-8.
97 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
98 | PERL_SCAN_DISALLOW_PREFIX
99 /* XXX Until the message is improved in grok_oct, handle errors
101 | PERL_SCAN_SILENT_ILLDIGIT;
103 PERL_ARGS_ASSERT_GROK_BSLASH_O;
105 assert(*(*s - 1) == '\\');
110 *error_msg = "Missing braces on \\o{}";
114 e = (char *) memchr(*s, '}', send - *s);
116 (*s)++; /* Move past the '{' */
117 while (isOCTAL(**s)) { /* Position beyond the legal digits */
120 *error_msg = "Missing right brace on \\o{";
124 (*s)++; /* Point to expected first digit (could be first byte of utf8
125 sequence if not a digit) */
126 numbers_len = e - *s;
127 if (numbers_len == 0) {
128 (*s)++; /* Move past the } */
129 *error_msg = "Empty \\o{}";
133 if (silence_non_portable) {
134 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
137 *uv = grok_oct(*s, &numbers_len, &flags, NULL);
138 /* Note that if has non-octal, will ignore everything starting with that up
141 if (numbers_len != (STRLEN) (e - *s)) {
144 *s += (UTF) ? UTF8_SAFE_SKIP(*s, send) : 1;
145 *error_msg = "Non-octal character";
148 else if (output_warning) {
149 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
150 /* diag_listed_as: Non-octal character '%c'. Resolved as "%s" */
151 "Non-octal character '%c'. Resolved as \"\\o{%.*s}\"",
158 /* Return past the '}' */
165 Perl_grok_bslash_x(pTHX_ char **s, const char * const send, UV *uv,
166 const char** error_msg,
167 const bool output_warning, const bool strict,
168 const bool silence_non_portable,
172 /* Documentation to be supplied when interface nailed down finally
173 * This returns FALSE if there is an error which the caller need not recover
174 * from; otherwise TRUE.
175 * It guarantees that the returned codepoint, *uv, when expressed as
176 * utf8 bytes, would fit within the skipped "\x{...}" bytes.
179 * s is the address of a pointer to a string. **s is 'x', and the
180 * previous character was a backslash. At exit, *s will be advanced
181 * to the byte just after those absorbed by this function. Hence the
182 * caller can continue parsing from there. In the case of an error,
183 * this routine has generally positioned *s to point just to the right
184 * of the first bad spot, so that a message that has a "<--" to mark
185 * the spot will be correctly positioned.
186 * send - 1 gives a limit in *s that this function is not permitted to
187 * look beyond. That is, the function may look at bytes only in the
189 * uv points to a UV that will hold the output value, valid only if the
190 * return from the function is TRUE
191 * error_msg is a pointer that will be set to an internal buffer giving an
192 * error message upon failure (the return is FALSE). Untouched if
194 * output_warning says whether to output any warning messages, or suppress
196 * strict is true if anything out of the ordinary should cause this to
197 * fail instead of warn or be silent. For example, it requires
198 * exactly 2 digits following the \x (when there are no braces).
199 * 3 digits could be a mistake, so is forbidden in this mode.
200 * silence_non_portable is true if to suppress warnings about the code
201 * point returned being too large to fit on all platforms.
202 * UTF is true iff the string *s is encoded in UTF-8.
206 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
209 PERL_ARGS_ASSERT_GROK_BSLASH_X;
211 assert(*(*s - 1) == '\\');
215 if (strict || ! output_warning) {
216 flags |= PERL_SCAN_SILENT_ILLDIGIT;
220 STRLEN len = (strict) ? 3 : 2;
222 *uv = grok_hex(*s, &len, &flags, NULL);
224 if (strict && len != 2) {
226 *s += (UTF) ? UTF8_SAFE_SKIP(*s, send) : 1;
227 *error_msg = "Non-hex character";
230 *error_msg = "Use \\x{...} for more than two hex characters";
237 e = (char *) memchr(*s, '}', send - *s);
239 (*s)++; /* Move past the '{' */
240 while (isXDIGIT(**s)) { /* Position beyond the legal digits */
243 /* XXX The corresponding message above for \o is just '\\o{'; other
244 * messages for other constructs include the '}', so are inconsistent.
246 *error_msg = "Missing right brace on \\x{}";
250 (*s)++; /* Point to expected first digit (could be first byte of utf8
251 sequence if not a digit) */
252 numbers_len = e - *s;
253 if (numbers_len == 0) {
255 (*s)++; /* Move past the } */
256 *error_msg = "Number with no digits";
264 flags |= PERL_SCAN_ALLOW_UNDERSCORES;
265 if (silence_non_portable) {
266 flags |= PERL_SCAN_SILENT_NON_PORTABLE;
269 *uv = grok_hex(*s, &numbers_len, &flags, NULL);
270 /* Note that if has non-hex, will ignore everything starting with that up
273 if (strict && numbers_len != (STRLEN) (e - *s)) {
275 *s += (UTF) ? UTF8_SAFE_SKIP(*s, send) : 1;
276 *error_msg = "Non-hex character";
280 /* Return past the '}' */
287 * ex: set ts=8 sts=4 sw=4 et: