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 |
5aaab254 | 18 | S_regcurly(pTHX_ const char *s) |
04e98a4d | 19 | { |
881ffab6 | 20 | PERL_ARGS_ASSERT_REGCURLY; |
04e98a4d AD |
21 | |
22 | if (*s++ != '{') | |
23 | return FALSE; | |
24 | if (!isDIGIT(*s)) | |
25 | return FALSE; | |
26 | while (isDIGIT(*s)) | |
27 | s++; | |
2ec31dd9 | 28 | if (*s == ',') { |
04e98a4d | 29 | s++; |
2ec31dd9 KW |
30 | while (isDIGIT(*s)) |
31 | s++; | |
32 | } | |
04e98a4d AD |
33 | if (*s != '}') |
34 | return FALSE; | |
35 | return TRUE; | |
36 | } | |
db30362b | 37 | |
68b355dd KW |
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) | |
40 | U8 source = *current; | |
68b355dd KW |
41 | */ |
42 | ||
43 | STATIC char | |
17a3df4c | 44 | S_grok_bslash_c(pTHX_ const char source, const bool utf8, const bool output_warning) |
68b355dd KW |
45 | { |
46 | ||
47 | U8 result; | |
48 | ||
17a3df4c KW |
49 | if (utf8) { |
50 | /* Trying to deprecate non-ASCII usages. This construct has never | |
51 | * worked for a utf8 variant. So, even though are accepting non-ASCII | |
52 | * Latin1 in 5.14, no need to make them work under utf8 */ | |
53 | if (! isASCII(source)) { | |
54 | Perl_croak(aTHX_ "Character following \"\\c\" must be ASCII"); | |
55 | } | |
68b355dd KW |
56 | } |
57 | ||
58 | result = toCTRL(source); | |
17a3df4c KW |
59 | if (! isASCII(source)) { |
60 | Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), | |
61 | "Character following \"\\c\" must be ASCII"); | |
62 | } | |
63 | else if (! isCNTRL(result) && output_warning) { | |
68b355dd | 64 | if (source == '{') { |
17a3df4c KW |
65 | Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), |
66 | "\"\\c{\" is deprecated and is more clearly written as \";\""); | |
68b355dd | 67 | } |
17a3df4c | 68 | else { |
68b355dd KW |
69 | U8 clearer[3]; |
70 | U8 i = 0; | |
0eb30aeb | 71 | if (! isWORDCHAR(result)) { |
68b355dd KW |
72 | clearer[i++] = '\\'; |
73 | } | |
74 | clearer[i++] = result; | |
75 | clearer[i++] = '\0'; | |
76 | ||
17a3df4c KW |
77 | Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), |
78 | "\"\\c%c\" is more clearly written simply as \"%s\"", | |
68b355dd KW |
79 | source, |
80 | clearer); | |
81 | } | |
82 | } | |
83 | ||
84 | return result; | |
85 | } | |
86 | ||
db30362b | 87 | STATIC bool |
00ce5563 KW |
88 | S_grok_bslash_o(pTHX_ char **s, UV *uv, const char** error_msg, |
89 | const bool output_warning) | |
db30362b KW |
90 | { |
91 | ||
92 | /* Documentation to be supplied when interface nailed down finally | |
93 | * This returns FALSE if there is an error which the caller need not recover | |
94 | * from; , otherwise TRUE. In either case the caller should look at *len | |
95 | * On input: | |
00ce5563 KW |
96 | * s is the address of a pointer to a NULL terminated string that begins |
97 | * with 'o', and the previous character was a backslash. At exit, *s | |
98 | * will be advanced to the byte just after those absorbed by this | |
99 | * function. Hence the caller can continue parsing from there. In | |
100 | * the case of an error, this routine has generally positioned *s to | |
101 | * point just to the right of the first bad spot, so that a message | |
102 | * that has a "<--" to mark the spot will be correctly positioned. | |
db30362b KW |
103 | * uv points to a UV that will hold the output value, valid only if the |
104 | * return from the function is TRUE | |
db30362b KW |
105 | * error_msg is a pointer that will be set to an internal buffer giving an |
106 | * error message upon failure (the return is FALSE). Untouched if | |
107 | * function succeeds | |
108 | * output_warning says whether to output any warning messages, or suppress | |
109 | * them | |
110 | */ | |
00ce5563 | 111 | char* e; |
db30362b KW |
112 | STRLEN numbers_len; |
113 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES | |
114 | | PERL_SCAN_DISALLOW_PREFIX | |
115 | /* XXX Until the message is improved in grok_oct, handle errors | |
116 | * ourselves */ | |
117 | | PERL_SCAN_SILENT_ILLDIGIT; | |
118 | ||
119 | PERL_ARGS_ASSERT_GROK_BSLASH_O; | |
120 | ||
121 | ||
00ce5563 KW |
122 | assert(**s == 'o'); |
123 | (*s)++; | |
db30362b | 124 | |
00ce5563 | 125 | if (**s != '{') { |
db30362b KW |
126 | *error_msg = "Missing braces on \\o{}"; |
127 | return FALSE; | |
128 | } | |
129 | ||
00ce5563 | 130 | e = strchr(*s, '}'); |
db30362b | 131 | if (!e) { |
00ce5563 KW |
132 | (*s)++; /* Move past the '{' */ |
133 | *error_msg = "Missing right brace on \\o{"; | |
db30362b KW |
134 | return FALSE; |
135 | } | |
136 | ||
00ce5563 KW |
137 | (*s)++; /* Point to expected first digit (could be first byte of utf8 |
138 | sequence if not a digit) */ | |
139 | numbers_len = e - *s; | |
db30362b | 140 | if (numbers_len == 0) { |
00ce5563 | 141 | (*s)++; /* Move past the } */ |
db30362b KW |
142 | *error_msg = "Number with no digits"; |
143 | return FALSE; | |
144 | } | |
145 | ||
00ce5563 | 146 | *uv = grok_oct(*s, &numbers_len, &flags, NULL); |
db30362b KW |
147 | /* Note that if has non-octal, will ignore everything starting with that up |
148 | * to the '}' */ | |
149 | ||
00ce5563 | 150 | if (output_warning && numbers_len != (STRLEN) (e - *s)) { |
db30362b KW |
151 | Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT), |
152 | /* diag_listed_as: Non-octal character '%c'. Resolved as "%s" */ | |
153 | "Non-octal character '%c'. Resolved as \"\\o{%.*s}\"", | |
00ce5563 | 154 | *(*s + numbers_len), |
db30362b | 155 | (int) numbers_len, |
00ce5563 | 156 | *s); |
db30362b KW |
157 | } |
158 | ||
00ce5563 KW |
159 | /* Return past the '}' */ |
160 | *s = e + 1; | |
161 | ||
db30362b KW |
162 | return TRUE; |
163 | } | |
164 | ||
a0481293 | 165 | PERL_STATIC_INLINE bool |
00ce5563 KW |
166 | S_grok_bslash_x(pTHX_ char **s, UV *uv, const char** error_msg, |
167 | const bool output_warning) | |
a0481293 KW |
168 | { |
169 | ||
170 | /* Documentation to be supplied when interface nailed down finally | |
171 | * This returns FALSE if there is an error which the caller need not recover | |
172 | * from; , otherwise TRUE. In either case the caller should look at *len | |
173 | * On input: | |
00ce5563 KW |
174 | * s is the address of a pointer to a NULL terminated string that begins |
175 | * with 'x', and the previous character was a backslash. At exit, *s | |
176 | * will be advanced to the byte just after those absorbed by this | |
177 | * function. Hence the caller can continue parsing from there. In | |
178 | * the case of an error, this routine has generally positioned *s to | |
179 | * point just to the right of the first bad spot, so that a message | |
180 | * that has a "<--" to mark the spot will be correctly positioned. | |
a0481293 KW |
181 | * uv points to a UV that will hold the output value, valid only if the |
182 | * return from the function is TRUE | |
a0481293 KW |
183 | * error_msg is a pointer that will be set to an internal buffer giving an |
184 | * error message upon failure (the return is FALSE). Untouched if | |
185 | * function succeeds | |
186 | * output_warning says whether to output any warning messages, or suppress | |
187 | * them | |
188 | */ | |
00ce5563 | 189 | char* e; |
a0481293 KW |
190 | STRLEN numbers_len; |
191 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES | |
192 | | PERL_SCAN_DISALLOW_PREFIX; | |
193 | ||
194 | PERL_ARGS_ASSERT_GROK_BSLASH_X; | |
195 | ||
4f8dbb2d | 196 | PERL_UNUSED_ARG(output_warning); |
a0481293 | 197 | |
00ce5563 KW |
198 | assert(**s == 'x'); |
199 | (*s)++; | |
a0481293 | 200 | |
00ce5563 KW |
201 | if (**s != '{') { |
202 | I32 flags = PERL_SCAN_DISALLOW_PREFIX; | |
203 | STRLEN len = 2; | |
204 | *uv = grok_hex(*s, &len, &flags, NULL); | |
205 | *s += len; | |
a0481293 KW |
206 | return TRUE; |
207 | } | |
208 | ||
00ce5563 | 209 | e = strchr(*s, '}'); |
a0481293 | 210 | if (!e) { |
00ce5563 | 211 | (*s)++; /* Move past the '{' */ |
a0481293 KW |
212 | /* XXX The corresponding message above for \o is just '\\o{'; other |
213 | * messages for other constructs include the '}', so are inconsistent. | |
214 | */ | |
215 | *error_msg = "Missing right brace on \\x{}"; | |
216 | return FALSE; | |
217 | } | |
218 | ||
00ce5563 KW |
219 | (*s)++; /* Point to expected first digit (could be first byte of utf8 |
220 | sequence if not a digit) */ | |
221 | numbers_len = e - *s; | |
222 | *uv = grok_hex(*s, &numbers_len, &flags, NULL); | |
a0481293 KW |
223 | /* Note that if has non-hex, will ignore everything starting with that up |
224 | * to the '}' */ | |
225 | ||
00ce5563 KW |
226 | /* Return past the '}' */ |
227 | *s = e + 1; | |
228 | ||
a0481293 KW |
229 | return TRUE; |
230 | } | |
231 | ||
04e98a4d AD |
232 | /* |
233 | * Local variables: | |
234 | * c-indentation-style: bsd | |
235 | * c-basic-offset: 4 | |
14d04a33 | 236 | * indent-tabs-mode: nil |
04e98a4d AD |
237 | * End: |
238 | * | |
14d04a33 | 239 | * ex: set ts=8 sts=4 sw=4 et: |
04e98a4d | 240 | */ |