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