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