Commit | Line | Data |
e807ee50 |
1 | /* stolen_chunk_of_toke.c - from perl 5.8.8 toke.c |
2 | * |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
4 | * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public |
7 | * License or the Artistic License, as specified in the README file. |
8 | * |
9 | */ |
10 | |
11 | /* |
12 | * "It all comes from here, the stench and the peril." --Frodo |
13 | */ |
14 | |
15 | /* |
16 | * this is all blatantly stolen. I sincerely hopes it doesn't fuck anything |
17 | * up but if it does blame me (Matt S Trout), not the poor original authors |
18 | */ |
19 | |
71511651 |
20 | /* the following #defines are stolen from assorted headers, not toke.c (mst) */ |
e807ee50 |
21 | |
78160085 |
22 | #define skipspace(a) S_skipspace(aTHX_ a) |
23 | #define incline(a) S_incline(aTHX_ a) |
24 | #define filter_gets(a,b,c) S_filter_gets(aTHX_ a,b,c) |
25 | #define scan_str(a,b,c) S_scan_str(aTHX_ a,b,c) |
26 | #define scan_word(a,b,c,d,e) S_scan_word(aTHX_ a,b,c,d,e) |
92997e69 |
27 | #define scan_ident(a,b,c,d,e) S_scan_ident(aTHX_ a,b,c,d,e) |
78160085 |
28 | |
29 | STATIC void S_incline(pTHX_ char *s); |
30 | STATIC char* S_skipspace(pTHX_ char *s); |
31 | STATIC char * S_filter_gets(pTHX_ SV *sv, PerlIO *fp, STRLEN append); |
32 | STATIC char* S_scan_str(pTHX_ char *start, int keep_quoted, int keep_delims); |
33 | STATIC char* S_scan_word(pTHX_ char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp); |
34 | |
e807ee50 |
35 | #define DPTR2FPTR(t,p) ((t)PTR2nat(p)) /* data pointer to function pointer */ |
36 | #define FPTR2DPTR(t,p) ((t)PTR2nat(p)) /* function pointer to data pointer */ |
c630715a |
37 | #define PTR2nat(p) (PTRV)(p) /* pointer to integer of PTRSIZE */ |
e807ee50 |
38 | #define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t), |
14ab3472 |
39 | |
40 | /* conditionalise these two because as of 5.9.5 we already get them from |
41 | the headers (mst) */ |
42 | #ifndef Newx |
e807ee50 |
43 | #define Newx(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))) |
14ab3472 |
44 | #endif |
45 | #ifndef SvPVX_const |
0ba8c7aa |
46 | #define SvPVX_const(sv) ((const char*) (0 + SvPVX(sv))) |
14ab3472 |
47 | #endif |
48 | |
0ba8c7aa |
49 | #define SvPV_renew(sv,n) \ |
50 | STMT_START { SvLEN_set(sv, n); \ |
51 | SvPV_set((sv), (MEM_WRAP_CHECK_(n,char) \ |
52 | (char*)saferealloc((Malloc_t)SvPVX(sv), \ |
53 | (MEM_SIZE)((n))))); \ |
54 | } STMT_END |
e807ee50 |
55 | |
c630715a |
56 | /* On MacOS, respect nonbreaking spaces */ |
57 | #ifdef MACOS_TRADITIONAL |
58 | #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\312'||(c)=='\t') |
59 | #else |
60 | #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t') |
61 | #endif |
e807ee50 |
62 | |
0ba8c7aa |
63 | #define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline)) |
64 | |
e807ee50 |
65 | #define LEX_NORMAL 10 /* normal code (ie not within "...") */ |
66 | #define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */ |
67 | #define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */ |
68 | #define LEX_INTERPPUSH 7 /* starting a new sublex parse level */ |
69 | #define LEX_INTERPSTART 6 /* expecting the start of a $var */ |
70 | |
71 | /* at end of code, eg "$x" followed by: */ |
72 | #define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */ |
73 | #define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */ |
74 | |
75 | #define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of |
76 | string or after \E, $foo, etc */ |
77 | #define LEX_INTERPCONST 2 /* NOT USED */ |
78 | #define LEX_FORMLINE 1 /* expecting a format line */ |
79 | #define LEX_KNOWNEXT 0 /* next token known; just return it */ |
80 | |
14ab3472 |
81 | /* and these two are my own madness (mst) */ |
71511651 |
82 | |
83 | #if PERL_REVISION == 5 && PERL_VERSION == 8 && PERL_SUBVERSION >= 8 |
84 | #define PERL_5_8_8_PLUS |
85 | #endif |
86 | |
e38f5a74 |
87 | #if PERL_REVISION == 5 && PERL_VERSION > 8 |
88 | #define PERL_5_9_PLUS |
89 | #endif |
90 | |
91 | #ifdef PERL_5_9_PLUS |
14ab3472 |
92 | /* 5.9+ moves a bunch of things to a PL_parser struct so we need to |
93 | declare the backcompat macros for things to still work (mst) */ |
94 | |
95 | /* XXX temporary backwards compatibility */ |
96 | #define PL_lex_brackets (PL_parser->lex_brackets) |
97 | #define PL_lex_brackstack (PL_parser->lex_brackstack) |
98 | #define PL_lex_casemods (PL_parser->lex_casemods) |
99 | #define PL_lex_casestack (PL_parser->lex_casestack) |
100 | #define PL_lex_defer (PL_parser->lex_defer) |
101 | #define PL_lex_dojoin (PL_parser->lex_dojoin) |
102 | #define PL_lex_expect (PL_parser->lex_expect) |
103 | #define PL_lex_formbrack (PL_parser->lex_formbrack) |
104 | #define PL_lex_inpat (PL_parser->lex_inpat) |
105 | #define PL_lex_inwhat (PL_parser->lex_inwhat) |
106 | #define PL_lex_op (PL_parser->lex_op) |
107 | #define PL_lex_repl (PL_parser->lex_repl) |
108 | #define PL_lex_starts (PL_parser->lex_starts) |
109 | #define PL_lex_stuff (PL_parser->lex_stuff) |
110 | #define PL_multi_start (PL_parser->multi_start) |
111 | #define PL_multi_open (PL_parser->multi_open) |
112 | #define PL_multi_close (PL_parser->multi_close) |
113 | #define PL_pending_ident (PL_parser->pending_ident) |
114 | #define PL_preambled (PL_parser->preambled) |
115 | #define PL_sublex_info (PL_parser->sublex_info) |
116 | #define PL_linestr (PL_parser->linestr) |
117 | #define PL_sublex_info (PL_parser->sublex_info) |
118 | #define PL_linestr (PL_parser->linestr) |
119 | #define PL_expect (PL_parser->expect) |
120 | #define PL_copline (PL_parser->copline) |
121 | #define PL_bufptr (PL_parser->bufptr) |
122 | #define PL_oldbufptr (PL_parser->oldbufptr) |
123 | #define PL_oldoldbufptr (PL_parser->oldoldbufptr) |
124 | #define PL_linestart (PL_parser->linestart) |
125 | #define PL_bufend (PL_parser->bufend) |
126 | #define PL_last_uni (PL_parser->last_uni) |
127 | #define PL_last_lop (PL_parser->last_lop) |
128 | #define PL_last_lop_op (PL_parser->last_lop_op) |
129 | #define PL_lex_state (PL_parser->lex_state) |
130 | #define PL_rsfp (PL_parser->rsfp) |
131 | #define PL_rsfp_filters (PL_parser->rsfp_filters) |
132 | #define PL_in_my (PL_parser->in_my) |
133 | #define PL_in_my_stash (PL_parser->in_my_stash) |
134 | #define PL_tokenbuf (PL_parser->tokenbuf) |
135 | #define PL_multi_end (PL_parser->multi_end) |
136 | #define PL_error_count (PL_parser->error_count) |
137 | /* these three are from the non-PERL_MAD path but I don't -think- I need |
138 | the PERL_MAD stuff since my code isn't really populating things (mst) */ |
139 | # define PL_nexttoke (PL_parser->nexttoke) |
140 | # define PL_nexttype (PL_parser->nexttype) |
141 | # define PL_nextval (PL_parser->nextval) |
142 | /* end of backcompat macros form 5.9 toke.c (mst) */ |
78160085 |
143 | #endif |
14ab3472 |
144 | |
145 | /* when ccflags include -DDEBUGGING we need this for earlier 5.8 perls */ |
146 | #ifndef SvPV_nolen_const |
147 | #define SvPV_nolen_const SvPV_nolen |
148 | #endif |
149 | |
71511651 |
150 | /* and now we're back to the toke.c stuff again (mst) */ |
151 | |
e807ee50 |
152 | static const char ident_too_long[] = |
153 | "Identifier too long"; |
154 | static const char c_without_g[] = |
155 | "Use of /c modifier is meaningless without /g"; |
156 | static const char c_in_subst[] = |
157 | "Use of /c modifier is meaningless in s///"; |
158 | |
159 | #ifdef USE_UTF8_SCRIPTS |
160 | # define UTF (!IN_BYTES) |
161 | #else |
162 | # define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8)) |
163 | #endif |
164 | |
165 | /* Invoke the idxth filter function for the current rsfp. */ |
166 | /* maxlen 0 = read one text line */ |
167 | I32 |
168 | Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen) |
169 | { |
170 | filter_t funcp; |
171 | SV *datasv = NULL; |
172 | |
173 | if (!PL_rsfp_filters) |
174 | return -1; |
175 | if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */ |
176 | /* Provide a default input filter to make life easy. */ |
177 | /* Note that we append to the line. This is handy. */ |
178 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
179 | "filter_read %d: from rsfp\n", idx)); |
180 | if (maxlen) { |
181 | /* Want a block */ |
182 | int len ; |
183 | const int old_len = SvCUR(buf_sv); |
184 | |
185 | /* ensure buf_sv is large enough */ |
186 | SvGROW(buf_sv, (STRLEN)(old_len + maxlen)) ; |
187 | if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len, maxlen)) <= 0){ |
188 | if (PerlIO_error(PL_rsfp)) |
189 | return -1; /* error */ |
190 | else |
191 | return 0 ; /* end of file */ |
192 | } |
193 | SvCUR_set(buf_sv, old_len + len) ; |
194 | } else { |
195 | /* Want a line */ |
196 | if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) { |
197 | if (PerlIO_error(PL_rsfp)) |
198 | return -1; /* error */ |
199 | else |
200 | return 0 ; /* end of file */ |
201 | } |
202 | } |
203 | return SvCUR(buf_sv); |
204 | } |
205 | /* Skip this filter slot if filter has been deleted */ |
206 | if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) { |
207 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
208 | "filter_read %d: skipped (filter deleted)\n", |
209 | idx)); |
210 | return FILTER_READ(idx+1, buf_sv, maxlen); /* recurse */ |
211 | } |
212 | /* Get function pointer hidden within datasv */ |
213 | funcp = DPTR2FPTR(filter_t, IoANY(datasv)); |
214 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
215 | "filter_read %d: via function %p (%s)\n", |
216 | idx, datasv, SvPV_nolen_const(datasv))); |
217 | /* Call function. The function is expected to */ |
218 | /* call "FILTER_READ(idx+1, buf_sv)" first. */ |
219 | /* Return: <0:error, =0:eof, >0:not eof */ |
220 | return (*funcp)(aTHX_ idx, buf_sv, maxlen); |
221 | } |
222 | |
223 | STATIC char * |
224 | S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append) |
225 | { |
226 | #ifdef PERL_CR_FILTER |
227 | if (!PL_rsfp_filters) { |
228 | filter_add(S_cr_textfilter,NULL); |
229 | } |
230 | #endif |
231 | if (PL_rsfp_filters) { |
232 | if (!append) |
233 | SvCUR_set(sv, 0); /* start with empty line */ |
234 | if (FILTER_READ(0, sv, 0) > 0) |
235 | return ( SvPVX(sv) ) ; |
236 | else |
237 | return Nullch ; |
238 | } |
239 | else |
240 | return (sv_gets(sv, fp, append)); |
241 | } |
242 | |
243 | /* |
244 | * S_skipspace |
245 | * Called to gobble the appropriate amount and type of whitespace. |
246 | * Skips comments as well. |
247 | */ |
248 | |
249 | STATIC char * |
250 | S_skipspace(pTHX_ register char *s) |
251 | { |
252 | if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) { |
253 | while (s < PL_bufend && SPACE_OR_TAB(*s)) |
254 | s++; |
255 | return s; |
256 | } |
257 | for (;;) { |
258 | STRLEN prevlen; |
259 | SSize_t oldprevlen, oldoldprevlen; |
260 | SSize_t oldloplen = 0, oldunilen = 0; |
261 | while (s < PL_bufend && isSPACE(*s)) { |
262 | if (*s++ == '\n' && PL_in_eval && !PL_rsfp) |
263 | incline(s); |
264 | } |
265 | |
266 | /* comment */ |
267 | if (s < PL_bufend && *s == '#') { |
268 | while (s < PL_bufend && *s != '\n') |
269 | s++; |
270 | if (s < PL_bufend) { |
271 | s++; |
272 | if (PL_in_eval && !PL_rsfp) { |
273 | incline(s); |
274 | continue; |
275 | } |
276 | } |
277 | } |
278 | |
279 | /* only continue to recharge the buffer if we're at the end |
280 | * of the buffer, we're not reading from a source filter, and |
281 | * we're in normal lexing mode |
282 | */ |
283 | if (s < PL_bufend || !PL_rsfp || PL_sublex_info.sub_inwhat || |
284 | PL_lex_state == LEX_FORMLINE) |
285 | return s; |
286 | |
287 | /* try to recharge the buffer */ |
288 | if ((s = filter_gets(PL_linestr, PL_rsfp, |
289 | (prevlen = SvCUR(PL_linestr)))) == Nullch) |
290 | { |
291 | /* end of file. Add on the -p or -n magic */ |
292 | if (PL_minus_p) { |
293 | sv_setpv(PL_linestr, |
294 | ";}continue{print or die qq(-p destination: $!\\n);}"); |
295 | PL_minus_n = PL_minus_p = 0; |
296 | } |
297 | else if (PL_minus_n) { |
298 | sv_setpvn(PL_linestr, ";}", 2); |
299 | PL_minus_n = 0; |
300 | } |
301 | else |
302 | sv_setpvn(PL_linestr,";", 1); |
303 | |
304 | /* reset variables for next time we lex */ |
305 | PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart |
306 | = SvPVX(PL_linestr); |
307 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); |
308 | PL_last_lop = PL_last_uni = Nullch; |
309 | |
35fc8ada |
310 | /* In perl versions previous to p4-rawid: //depot/perl@32954 -P |
311 | * preprocessors were supported here. We don't support -P at all, even |
312 | * on perls that support it, and use the following chunk from blead |
313 | * perl. (rafl) |
314 | */ |
315 | |
316 | /* Close the filehandle. Could be from |
e807ee50 |
317 | * STDIN, or a regular file. If we were reading code from |
318 | * STDIN (because the commandline held no -e or filename) |
319 | * then we don't close it, we reset it so the code can |
320 | * read from STDIN too. |
321 | */ |
322 | |
35fc8ada |
323 | if ((PerlIO*)PL_rsfp == PerlIO_stdin()) |
e807ee50 |
324 | PerlIO_clearerr(PL_rsfp); |
325 | else |
326 | (void)PerlIO_close(PL_rsfp); |
327 | PL_rsfp = Nullfp; |
328 | return s; |
329 | } |
330 | |
331 | /* not at end of file, so we only read another line */ |
332 | /* make corresponding updates to old pointers, for yyerror() */ |
333 | oldprevlen = PL_oldbufptr - PL_bufend; |
334 | oldoldprevlen = PL_oldoldbufptr - PL_bufend; |
335 | if (PL_last_uni) |
336 | oldunilen = PL_last_uni - PL_bufend; |
337 | if (PL_last_lop) |
338 | oldloplen = PL_last_lop - PL_bufend; |
339 | PL_linestart = PL_bufptr = s + prevlen; |
340 | PL_bufend = s + SvCUR(PL_linestr); |
341 | s = PL_bufptr; |
342 | PL_oldbufptr = s + oldprevlen; |
343 | PL_oldoldbufptr = s + oldoldprevlen; |
344 | if (PL_last_uni) |
345 | PL_last_uni = s + oldunilen; |
346 | if (PL_last_lop) |
347 | PL_last_lop = s + oldloplen; |
348 | incline(s); |
349 | |
350 | /* debugger active and we're not compiling the debugger code, |
351 | * so store the line into the debugger's array of lines |
352 | */ |
353 | if (PERLDB_LINE && PL_curstash != PL_debstash) { |
354 | SV * const sv = NEWSV(85,0); |
355 | |
356 | sv_upgrade(sv, SVt_PVMG); |
357 | sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr); |
358 | (void)SvIOK_on(sv); |
359 | SvIV_set(sv, 0); |
360 | av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv); |
361 | } |
362 | } |
363 | } |
364 | |
365 | STATIC char * |
366 | S_scan_word(pTHX_ register char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp) |
367 | { |
368 | register char *d = dest; |
369 | register char * const e = d + destlen - 3; /* two-character token, ending NUL */ |
370 | for (;;) { |
371 | if (d >= e) |
372 | Perl_croak(aTHX_ ident_too_long); |
373 | if (isALNUM(*s)) /* UTF handled below */ |
374 | *d++ = *s++; |
375 | else if (*s == '\'' && allow_package && isIDFIRST_lazy_if(s+1,UTF)) { |
376 | *d++ = ':'; |
377 | *d++ = ':'; |
378 | s++; |
379 | } |
380 | else if (*s == ':' && s[1] == ':' && allow_package && s[2] != '$') { |
381 | *d++ = *s++; |
382 | *d++ = *s++; |
383 | } |
384 | else if (UTF && UTF8_IS_START(*s) && isALNUM_utf8((U8*)s)) { |
385 | char *t = s + UTF8SKIP(s); |
386 | while (UTF8_IS_CONTINUED(*t) && is_utf8_mark((U8*)t)) |
387 | t += UTF8SKIP(t); |
388 | if (d + (t - s) > e) |
389 | Perl_croak(aTHX_ ident_too_long); |
390 | Copy(s, d, t - s, char); |
391 | d += t - s; |
392 | s = t; |
393 | } |
394 | else { |
395 | *d = '\0'; |
396 | *slp = d - dest; |
397 | return s; |
398 | } |
399 | } |
400 | } |
401 | |
402 | /* |
403 | * S_incline |
404 | * This subroutine has nothing to do with tilting, whether at windmills |
405 | * or pinball tables. Its name is short for "increment line". It |
406 | * increments the current line number in CopLINE(PL_curcop) and checks |
407 | * to see whether the line starts with a comment of the form |
408 | * # line 500 "foo.pm" |
409 | * If so, it sets the current line number and file to the values in the comment. |
410 | */ |
411 | |
412 | STATIC void |
78160085 |
413 | S_incline(pTHX_ char *s) |
e807ee50 |
414 | { |
415 | char *t; |
78160085 |
416 | char *n; |
417 | char *e; |
e807ee50 |
418 | char ch; |
419 | |
420 | CopLINE_inc(PL_curcop); |
421 | if (*s++ != '#') |
422 | return; |
423 | while (SPACE_OR_TAB(*s)) s++; |
424 | if (strnEQ(s, "line", 4)) |
425 | s += 4; |
426 | else |
427 | return; |
428 | if (SPACE_OR_TAB(*s)) |
429 | s++; |
430 | else |
431 | return; |
432 | while (SPACE_OR_TAB(*s)) s++; |
433 | if (!isDIGIT(*s)) |
434 | return; |
435 | n = s; |
436 | while (isDIGIT(*s)) |
437 | s++; |
438 | while (SPACE_OR_TAB(*s)) |
439 | s++; |
440 | if (*s == '"' && (t = strchr(s+1, '"'))) { |
441 | s++; |
442 | e = t + 1; |
443 | } |
444 | else { |
78160085 |
445 | for (t = s; !isSPACE(*t); t++) ; |
e807ee50 |
446 | e = t; |
447 | } |
448 | while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f') |
449 | e++; |
450 | if (*e != '\n' && *e != '\0') |
451 | return; /* false alarm */ |
452 | |
453 | ch = *t; |
454 | *t = '\0'; |
455 | if (t - s > 0) { |
71511651 |
456 | /* this chunk was added to S_incline during 5.8.8. I don't know why but I don't |
457 | honestly care since I probably want to be bug-compatible anyway (mst) */ |
458 | |
459 | /* ... my kingdom for a perl parser in perl ... (mst) */ |
460 | |
461 | #ifdef PERL_5_8_8_PLUS |
e807ee50 |
462 | #ifndef USE_ITHREADS |
463 | const char *cf = CopFILE(PL_curcop); |
464 | if (cf && strlen(cf) > 7 && strnEQ(cf, "(eval ", 6)) { |
465 | /* must copy *{"::_<(eval N)[oldfilename:L]"} |
466 | * to *{"::_<newfilename"} */ |
467 | char smallbuf[256], smallbuf2[256]; |
468 | char *tmpbuf, *tmpbuf2; |
469 | GV **gvp, *gv2; |
470 | STRLEN tmplen = strlen(cf); |
471 | STRLEN tmplen2 = strlen(s); |
472 | if (tmplen + 3 < sizeof smallbuf) |
473 | tmpbuf = smallbuf; |
474 | else |
475 | Newx(tmpbuf, tmplen + 3, char); |
476 | if (tmplen2 + 3 < sizeof smallbuf2) |
477 | tmpbuf2 = smallbuf2; |
478 | else |
479 | Newx(tmpbuf2, tmplen2 + 3, char); |
480 | tmpbuf[0] = tmpbuf2[0] = '_'; |
481 | tmpbuf[1] = tmpbuf2[1] = '<'; |
482 | memcpy(tmpbuf + 2, cf, ++tmplen); |
483 | memcpy(tmpbuf2 + 2, s, ++tmplen2); |
484 | ++tmplen; ++tmplen2; |
485 | gvp = (GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, FALSE); |
486 | if (gvp) { |
487 | gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE); |
488 | if (!isGV(gv2)) |
489 | gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE); |
490 | /* adjust ${"::_<newfilename"} to store the new file name */ |
491 | GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2); |
492 | GvHV(gv2) = (HV*)SvREFCNT_inc(GvHV(*gvp)); |
493 | GvAV(gv2) = (AV*)SvREFCNT_inc(GvAV(*gvp)); |
494 | } |
495 | if (tmpbuf != smallbuf) Safefree(tmpbuf); |
496 | if (tmpbuf2 != smallbuf2) Safefree(tmpbuf2); |
497 | } |
498 | #endif |
71511651 |
499 | #endif |
500 | /* second endif closes out the "are we 5.8.(8+)" conditional */ |
e807ee50 |
501 | CopFILE_free(PL_curcop); |
502 | CopFILE_set(PL_curcop, s); |
503 | } |
504 | *t = ch; |
505 | CopLINE_set(PL_curcop, atoi(n)-1); |
506 | } |
0ba8c7aa |
507 | |
508 | /* scan_str |
509 | takes: start position in buffer |
510 | keep_quoted preserve \ on the embedded delimiter(s) |
511 | keep_delims preserve the delimiters around the string |
512 | returns: position to continue reading from buffer |
513 | side-effects: multi_start, multi_close, lex_repl or lex_stuff, and |
514 | updates the read buffer. |
515 | |
516 | This subroutine pulls a string out of the input. It is called for: |
517 | q single quotes q(literal text) |
518 | ' single quotes 'literal text' |
519 | qq double quotes qq(interpolate $here please) |
520 | " double quotes "interpolate $here please" |
521 | qx backticks qx(/bin/ls -l) |
522 | ` backticks `/bin/ls -l` |
523 | qw quote words @EXPORT_OK = qw( func() $spam ) |
524 | m// regexp match m/this/ |
525 | s/// regexp substitute s/this/that/ |
526 | tr/// string transliterate tr/this/that/ |
527 | y/// string transliterate y/this/that/ |
528 | ($*@) sub prototypes sub foo ($) |
529 | (stuff) sub attr parameters sub foo : attr(stuff) |
530 | <> readline or globs <FOO>, <>, <$fh>, or <*.c> |
531 | |
532 | In most of these cases (all but <>, patterns and transliterate) |
533 | yylex() calls scan_str(). m// makes yylex() call scan_pat() which |
534 | calls scan_str(). s/// makes yylex() call scan_subst() which calls |
535 | scan_str(). tr/// and y/// make yylex() call scan_trans() which |
536 | calls scan_str(). |
537 | |
538 | It skips whitespace before the string starts, and treats the first |
539 | character as the delimiter. If the delimiter is one of ([{< then |
540 | the corresponding "close" character )]}> is used as the closing |
541 | delimiter. It allows quoting of delimiters, and if the string has |
542 | balanced delimiters ([{<>}]) it allows nesting. |
543 | |
544 | On success, the SV with the resulting string is put into lex_stuff or, |
545 | if that is already non-NULL, into lex_repl. The second case occurs only |
546 | when parsing the RHS of the special constructs s/// and tr/// (y///). |
547 | For convenience, the terminating delimiter character is stuffed into |
548 | SvIVX of the SV. |
549 | */ |
550 | |
551 | STATIC char * |
552 | S_scan_str(pTHX_ char *start, int keep_quoted, int keep_delims) |
553 | { |
554 | SV *sv; /* scalar value: string */ |
555 | char *tmps; /* temp string, used for delimiter matching */ |
556 | register char *s = start; /* current position in the buffer */ |
557 | register char term; /* terminating character */ |
558 | register char *to; /* current position in the sv's data */ |
559 | I32 brackets = 1; /* bracket nesting level */ |
560 | bool has_utf8 = FALSE; /* is there any utf8 content? */ |
561 | I32 termcode; /* terminating char. code */ |
71511651 |
562 | /* 5.8.7+ uses UTF8_MAXBYTES but also its utf8.h defs _MAXLEN to it so |
563 | I'm reasonably hopeful this won't destroy anything (mst) */ |
564 | U8 termstr[UTF8_MAXLEN]; /* terminating string */ |
0ba8c7aa |
565 | STRLEN termlen; /* length of terminating string */ |
566 | char *last = NULL; /* last position for nesting bracket */ |
567 | |
568 | /* skip space before the delimiter */ |
569 | if (isSPACE(*s)) |
570 | s = skipspace(s); |
571 | |
572 | /* mark where we are, in case we need to report errors */ |
573 | CLINE; |
574 | |
575 | /* after skipping whitespace, the next character is the terminator */ |
576 | term = *s; |
577 | if (!UTF) { |
578 | termcode = termstr[0] = term; |
579 | termlen = 1; |
580 | } |
581 | else { |
582 | termcode = utf8_to_uvchr((U8*)s, &termlen); |
583 | Copy(s, termstr, termlen, U8); |
584 | if (!UTF8_IS_INVARIANT(term)) |
585 | has_utf8 = TRUE; |
586 | } |
587 | |
588 | /* mark where we are */ |
589 | PL_multi_start = CopLINE(PL_curcop); |
590 | PL_multi_open = term; |
591 | |
592 | /* find corresponding closing delimiter */ |
593 | if (term && (tmps = strchr("([{< )]}> )]}>",term))) |
594 | termcode = termstr[0] = term = tmps[5]; |
595 | |
596 | PL_multi_close = term; |
597 | |
598 | /* create a new SV to hold the contents. 87 is leak category, I'm |
599 | assuming. 79 is the SV's initial length. What a random number. */ |
600 | sv = NEWSV(87,79); |
601 | sv_upgrade(sv, SVt_PVIV); |
602 | SvIV_set(sv, termcode); |
603 | (void)SvPOK_only(sv); /* validate pointer */ |
604 | |
605 | /* move past delimiter and try to read a complete string */ |
606 | if (keep_delims) |
607 | sv_catpvn(sv, s, termlen); |
608 | s += termlen; |
609 | for (;;) { |
610 | if (PL_encoding && !UTF) { |
611 | bool cont = TRUE; |
612 | |
613 | while (cont) { |
614 | int offset = s - SvPVX_const(PL_linestr); |
615 | const bool found = sv_cat_decode(sv, PL_encoding, PL_linestr, |
616 | &offset, (char*)termstr, termlen); |
617 | const char *ns = SvPVX_const(PL_linestr) + offset; |
618 | char *svlast = SvEND(sv) - 1; |
619 | |
620 | for (; s < ns; s++) { |
621 | if (*s == '\n' && !PL_rsfp) |
622 | CopLINE_inc(PL_curcop); |
623 | } |
624 | if (!found) |
625 | goto read_more_line; |
626 | else { |
627 | /* handle quoted delimiters */ |
628 | if (SvCUR(sv) > 1 && *(svlast-1) == '\\') { |
629 | const char *t; |
630 | for (t = svlast-2; t >= SvPVX_const(sv) && *t == '\\';) |
631 | t--; |
632 | if ((svlast-1 - t) % 2) { |
633 | if (!keep_quoted) { |
634 | *(svlast-1) = term; |
635 | *svlast = '\0'; |
636 | SvCUR_set(sv, SvCUR(sv) - 1); |
637 | } |
638 | continue; |
639 | } |
640 | } |
641 | if (PL_multi_open == PL_multi_close) { |
642 | cont = FALSE; |
643 | } |
644 | else { |
645 | const char *t; |
646 | char *w; |
647 | if (!last) |
648 | last = SvPVX(sv); |
649 | for (t = w = last; t < svlast; w++, t++) { |
650 | /* At here, all closes are "was quoted" one, |
651 | so we don't check PL_multi_close. */ |
652 | if (*t == '\\') { |
653 | if (!keep_quoted && *(t+1) == PL_multi_open) |
654 | t++; |
655 | else |
656 | *w++ = *t++; |
657 | } |
658 | else if (*t == PL_multi_open) |
659 | brackets++; |
660 | |
661 | *w = *t; |
662 | } |
663 | if (w < t) { |
664 | *w++ = term; |
665 | *w = '\0'; |
666 | SvCUR_set(sv, w - SvPVX_const(sv)); |
667 | } |
668 | last = w; |
669 | if (--brackets <= 0) |
670 | cont = FALSE; |
671 | } |
672 | } |
673 | } |
674 | if (!keep_delims) { |
675 | SvCUR_set(sv, SvCUR(sv) - 1); |
676 | *SvEND(sv) = '\0'; |
677 | } |
678 | break; |
679 | } |
680 | |
681 | /* extend sv if need be */ |
682 | SvGROW(sv, SvCUR(sv) + (PL_bufend - s) + 1); |
683 | /* set 'to' to the next character in the sv's string */ |
684 | to = SvPVX(sv)+SvCUR(sv); |
685 | |
686 | /* if open delimiter is the close delimiter read unbridle */ |
687 | if (PL_multi_open == PL_multi_close) { |
688 | for (; s < PL_bufend; s++,to++) { |
689 | /* embedded newlines increment the current line number */ |
690 | if (*s == '\n' && !PL_rsfp) |
691 | CopLINE_inc(PL_curcop); |
692 | /* handle quoted delimiters */ |
693 | if (*s == '\\' && s+1 < PL_bufend && term != '\\') { |
694 | if (!keep_quoted && s[1] == term) |
695 | s++; |
696 | /* any other quotes are simply copied straight through */ |
697 | else |
698 | *to++ = *s++; |
699 | } |
700 | /* terminate when run out of buffer (the for() condition), or |
701 | have found the terminator */ |
702 | else if (*s == term) { |
703 | if (termlen == 1) |
704 | break; |
705 | if (s+termlen <= PL_bufend && memEQ(s, (char*)termstr, termlen)) |
706 | break; |
707 | } |
708 | else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF) |
709 | has_utf8 = TRUE; |
710 | *to = *s; |
711 | } |
712 | } |
713 | |
714 | /* if the terminator isn't the same as the start character (e.g., |
715 | matched brackets), we have to allow more in the quoting, and |
716 | be prepared for nested brackets. |
717 | */ |
718 | else { |
719 | /* read until we run out of string, or we find the terminator */ |
720 | for (; s < PL_bufend; s++,to++) { |
721 | /* embedded newlines increment the line count */ |
722 | if (*s == '\n' && !PL_rsfp) |
723 | CopLINE_inc(PL_curcop); |
724 | /* backslashes can escape the open or closing characters */ |
725 | if (*s == '\\' && s+1 < PL_bufend) { |
726 | if (!keep_quoted && |
727 | ((s[1] == PL_multi_open) || (s[1] == PL_multi_close))) |
728 | s++; |
729 | else |
730 | *to++ = *s++; |
731 | } |
732 | /* allow nested opens and closes */ |
733 | else if (*s == PL_multi_close && --brackets <= 0) |
734 | break; |
735 | else if (*s == PL_multi_open) |
736 | brackets++; |
737 | else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF) |
738 | has_utf8 = TRUE; |
739 | *to = *s; |
740 | } |
741 | } |
742 | /* terminate the copied string and update the sv's end-of-string */ |
743 | *to = '\0'; |
744 | SvCUR_set(sv, to - SvPVX_const(sv)); |
745 | |
746 | /* |
747 | * this next chunk reads more into the buffer if we're not done yet |
748 | */ |
749 | |
750 | if (s < PL_bufend) |
751 | break; /* handle case where we are done yet :-) */ |
752 | |
753 | #ifndef PERL_STRICT_CR |
754 | if (to - SvPVX_const(sv) >= 2) { |
755 | if ((to[-2] == '\r' && to[-1] == '\n') || |
756 | (to[-2] == '\n' && to[-1] == '\r')) |
757 | { |
758 | to[-2] = '\n'; |
759 | to--; |
760 | SvCUR_set(sv, to - SvPVX_const(sv)); |
761 | } |
762 | else if (to[-1] == '\r') |
763 | to[-1] = '\n'; |
764 | } |
765 | else if (to - SvPVX_const(sv) == 1 && to[-1] == '\r') |
766 | to[-1] = '\n'; |
767 | #endif |
768 | |
769 | read_more_line: |
770 | /* if we're out of file, or a read fails, bail and reset the current |
771 | line marker so we can report where the unterminated string began |
772 | */ |
773 | if (!PL_rsfp || |
774 | !(PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = filter_gets(PL_linestr, PL_rsfp, 0))) { |
775 | sv_free(sv); |
776 | CopLINE_set(PL_curcop, (line_t)PL_multi_start); |
777 | return Nullch; |
778 | } |
779 | /* we read a line, so increment our line counter */ |
780 | CopLINE_inc(PL_curcop); |
781 | |
782 | /* update debugger info */ |
783 | if (PERLDB_LINE && PL_curstash != PL_debstash) { |
784 | SV *sv = NEWSV(88,0); |
785 | |
786 | sv_upgrade(sv, SVt_PVMG); |
787 | sv_setsv(sv,PL_linestr); |
788 | (void)SvIOK_on(sv); |
789 | SvIV_set(sv, 0); |
790 | av_store(CopFILEAV(PL_curcop), (I32)CopLINE(PL_curcop), sv); |
791 | } |
792 | |
793 | /* having changed the buffer, we must update PL_bufend */ |
794 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); |
795 | PL_last_lop = PL_last_uni = Nullch; |
796 | } |
797 | |
798 | /* at this point, we have successfully read the delimited string */ |
799 | |
800 | if (!PL_encoding || UTF) { |
801 | if (keep_delims) |
802 | sv_catpvn(sv, s, termlen); |
803 | s += termlen; |
804 | } |
805 | if (has_utf8 || PL_encoding) |
806 | SvUTF8_on(sv); |
807 | |
808 | PL_multi_end = CopLINE(PL_curcop); |
809 | |
810 | /* if we allocated too much space, give some back */ |
811 | if (SvCUR(sv) + 5 < SvLEN(sv)) { |
812 | SvLEN_set(sv, SvCUR(sv) + 1); |
71511651 |
813 | /* 5.8.8 uses SvPV_renew, no prior version actually has the damn thing (mst) */ |
814 | #ifdef PERL_5_8_8_PLUS |
0ba8c7aa |
815 | SvPV_renew(sv, SvLEN(sv)); |
71511651 |
816 | #else |
817 | Renew(SvPVX(sv), SvLEN(sv), char); |
818 | #endif |
0ba8c7aa |
819 | } |
820 | |
821 | /* decide whether this is the first or second quoted string we've read |
822 | for this op |
823 | */ |
824 | |
825 | if (PL_lex_stuff) |
826 | PL_lex_repl = sv; |
827 | else |
828 | PL_lex_stuff = sv; |
829 | return s; |
830 | } |
831 | |
832 | /* |
833 | * S_force_next |
834 | * When the lexer realizes it knows the next token (for instance, |
835 | * it is reordering tokens for the parser) then it can call S_force_next |
836 | * to know what token to return the next time the lexer is called. Caller |
837 | * will need to set PL_nextval[], and possibly PL_expect to ensure the lexer |
838 | * handles the token correctly. |
839 | */ |
840 | |
841 | STATIC void |
842 | S_force_next(pTHX_ I32 type) |
843 | { |
844 | PL_nexttype[PL_nexttoke] = type; |
845 | PL_nexttoke++; |
846 | if (PL_lex_state != LEX_KNOWNEXT) { |
847 | PL_lex_defer = PL_lex_state; |
848 | PL_lex_expect = PL_expect; |
849 | PL_lex_state = LEX_KNOWNEXT; |
850 | } |
851 | } |
92997e69 |
852 | |
853 | #define XFAKEBRACK 128 |
854 | |
855 | STATIC char * |
856 | S_scan_ident(pTHX_ register char *s, register const char *send, char *dest, STRLEN destlen, I32 ck_uni) |
857 | { |
858 | register char *d; |
859 | register char *e; |
860 | char *bracket = Nullch; |
861 | char funny = *s++; |
862 | |
863 | if (isSPACE(*s)) |
864 | s = skipspace(s); |
865 | d = dest; |
866 | e = d + destlen - 3; /* two-character token, ending NUL */ |
867 | if (isDIGIT(*s)) { |
868 | while (isDIGIT(*s)) { |
869 | if (d >= e) |
870 | Perl_croak(aTHX_ ident_too_long); |
871 | *d++ = *s++; |
872 | } |
873 | } |
874 | else { |
875 | for (;;) { |
876 | if (d >= e) |
877 | Perl_croak(aTHX_ ident_too_long); |
878 | if (isALNUM(*s)) /* UTF handled below */ |
879 | *d++ = *s++; |
880 | else if (*s == '\'' && isIDFIRST_lazy_if(s+1,UTF)) { |
881 | *d++ = ':'; |
882 | *d++ = ':'; |
883 | s++; |
884 | } |
885 | else if (*s == ':' && s[1] == ':') { |
886 | *d++ = *s++; |
887 | *d++ = *s++; |
888 | } |
889 | else if (UTF && UTF8_IS_START(*s) && isALNUM_utf8((U8*)s)) { |
890 | char *t = s + UTF8SKIP(s); |
891 | while (UTF8_IS_CONTINUED(*t) && is_utf8_mark((U8*)t)) |
892 | t += UTF8SKIP(t); |
893 | if (d + (t - s) > e) |
894 | Perl_croak(aTHX_ ident_too_long); |
895 | Copy(s, d, t - s, char); |
896 | d += t - s; |
897 | s = t; |
898 | } |
899 | else |
900 | break; |
901 | } |
902 | } |
903 | *d = '\0'; |
904 | d = dest; |
905 | if (*d) { |
906 | if (PL_lex_state != LEX_NORMAL) |
907 | PL_lex_state = LEX_INTERPENDMAYBE; |
908 | return s; |
909 | } |
910 | if (*s == '$' && s[1] && |
911 | (isALNUM_lazy_if(s+1,UTF) || s[1] == '$' || s[1] == '{' || strnEQ(s+1,"::",2)) ) |
912 | { |
913 | return s; |
914 | } |
915 | if (*s == '{') { |
916 | bracket = s; |
917 | s++; |
918 | } |
919 | else if (ck_uni) |
920 | check_uni(); |
921 | if (s < send) |
922 | *d = *s++; |
923 | d[1] = '\0'; |
924 | if (*d == '^' && *s && isCONTROLVAR(*s)) { |
925 | *d = toCTRL(*s); |
926 | s++; |
927 | } |
928 | if (bracket) { |
929 | if (isSPACE(s[-1])) { |
930 | while (s < send) { |
931 | const char ch = *s++; |
932 | if (!SPACE_OR_TAB(ch)) { |
933 | *d = ch; |
934 | break; |
935 | } |
936 | } |
937 | } |
938 | if (isIDFIRST_lazy_if(d,UTF)) { |
939 | d++; |
940 | if (UTF) { |
941 | e = s; |
942 | while ((e < send && isALNUM_lazy_if(e,UTF)) || *e == ':') { |
943 | e += UTF8SKIP(e); |
944 | while (e < send && UTF8_IS_CONTINUED(*e) && is_utf8_mark((U8*)e)) |
945 | e += UTF8SKIP(e); |
946 | } |
947 | Copy(s, d, e - s, char); |
948 | d += e - s; |
949 | s = e; |
950 | } |
951 | else { |
952 | while ((isALNUM(*s) || *s == ':') && d < e) |
953 | *d++ = *s++; |
954 | if (d >= e) |
955 | Perl_croak(aTHX_ ident_too_long); |
956 | } |
957 | *d = '\0'; |
958 | while (s < send && SPACE_OR_TAB(*s)) s++; |
959 | if ((*s == '[' || (*s == '{' && strNE(dest, "sub")))) { |
960 | if (ckWARN(WARN_AMBIGUOUS) && keyword(dest, d - dest)) { |
961 | const char *brack = *s == '[' ? "[...]" : "{...}"; |
962 | Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS), |
963 | "Ambiguous use of %c{%s%s} resolved to %c%s%s", |
964 | funny, dest, brack, funny, dest, brack); |
965 | } |
966 | bracket++; |
967 | PL_lex_brackstack[PL_lex_brackets++] = (char)(XOPERATOR | XFAKEBRACK); |
968 | return s; |
969 | } |
970 | } |
971 | /* Handle extended ${^Foo} variables |
972 | * 1999-02-27 mjd-perl-patch@plover.com */ |
973 | else if (!isALNUM(*d) && !isPRINT(*d) /* isCTRL(d) */ |
974 | && isALNUM(*s)) |
975 | { |
976 | d++; |
977 | while (isALNUM(*s) && d < e) { |
978 | *d++ = *s++; |
979 | } |
980 | if (d >= e) |
981 | Perl_croak(aTHX_ ident_too_long); |
982 | *d = '\0'; |
983 | } |
984 | if (*s == '}') { |
985 | s++; |
986 | if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) { |
987 | PL_lex_state = LEX_INTERPEND; |
988 | PL_expect = XREF; |
989 | } |
990 | if (funny == '#') |
991 | funny = '@'; |
992 | if (PL_lex_state == LEX_NORMAL) { |
993 | if (ckWARN(WARN_AMBIGUOUS) && |
994 | (keyword(dest, d - dest) || get_cv(dest, FALSE))) |
995 | { |
996 | Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS), |
997 | "Ambiguous use of %c{%s} resolved to %c%s", |
998 | funny, dest, funny, dest); |
999 | } |
1000 | } |
1001 | } |
1002 | else { |
1003 | s = bracket; /* let the parser handle it */ |
1004 | *dest = '\0'; |
1005 | } |
1006 | } |
1007 | else if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets && !intuit_more(s)) |
1008 | PL_lex_state = LEX_INTERPEND; |
1009 | return s; |
1010 | } |