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
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.
12 * "It all comes from here, the stench and the peril." --Frodo
16 * This file is the lexer for Perl. It's closely linked to the
19 * The main routine is yylex(), which returns the next token.
23 #define PERL_IN_TOKE_C
26 #define yylval (PL_parser->yylval)
28 static const char ident_too_long[] = "Identifier too long";
29 static const char commaless_variable_list[] = "comma-less variable list";
31 static void restore_rsfp(pTHX_ void *f);
32 #ifndef PERL_NO_UTF16_FILTER
33 static I32 utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen);
34 static I32 utf16rev_textfilter(pTHX_ int idx, SV *sv, int maxlen);
38 # define CURMAD(slot,sv) if (PL_madskills) { curmad(slot,sv); sv = 0; }
39 # define NEXTVAL_NEXTTOKE PL_nexttoke[PL_curforce].next_val
41 # define CURMAD(slot,sv)
42 # define NEXTVAL_NEXTTOKE PL_nextval[PL_nexttoke]
45 #define XFAKEBRACK 128
48 #ifdef USE_UTF8_SCRIPTS
49 # define UTF (!IN_BYTES)
51 # define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8))
54 /* In variables named $^X, these are the legal values for X.
55 * 1999-02-27 mjd-perl-patch@plover.com */
56 #define isCONTROLVAR(x) (isUPPER(x) || strchr("[\\]^_?", (x)))
58 /* On MacOS, respect nonbreaking spaces */
59 #ifdef MACOS_TRADITIONAL
60 #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\312'||(c)=='\t')
62 #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t')
65 /* LEX_* are values for PL_lex_state, the state of the lexer.
66 * They are arranged oddly so that the guard on the switch statement
67 * can get by with a single comparison (if the compiler is smart enough).
70 /* #define LEX_NOTPARSING 11 is done in perl.h. */
72 #define LEX_NORMAL 10 /* normal code (ie not within "...") */
73 #define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */
74 #define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */
75 #define LEX_INTERPPUSH 7 /* starting a new sublex parse level */
76 #define LEX_INTERPSTART 6 /* expecting the start of a $var */
78 /* at end of code, eg "$x" followed by: */
79 #define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */
80 #define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */
82 #define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of
83 string or after \E, $foo, etc */
84 #define LEX_INTERPCONST 2 /* NOT USED */
85 #define LEX_FORMLINE 1 /* expecting a format line */
86 #define LEX_KNOWNEXT 0 /* next token known; just return it */
90 static const char* const lex_state_names[] = {
109 #include "keywords.h"
111 /* CLINE is a macro that ensures PL_copline has a sane value */
116 #define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline))
119 # define SKIPSPACE0(s) skipspace0(s)
120 # define SKIPSPACE1(s) skipspace1(s)
121 # define SKIPSPACE2(s,tsv) skipspace2(s,&tsv)
122 # define PEEKSPACE(s) skipspace2(s,0)
124 # define SKIPSPACE0(s) skipspace(s)
125 # define SKIPSPACE1(s) skipspace(s)
126 # define SKIPSPACE2(s,tsv) skipspace(s)
127 # define PEEKSPACE(s) skipspace(s)
131 * Convenience functions to return different tokens and prime the
132 * lexer for the next token. They all take an argument.
134 * TOKEN : generic token (used for '(', DOLSHARP, etc)
135 * OPERATOR : generic operator
136 * AOPERATOR : assignment operator
137 * PREBLOCK : beginning the block after an if, while, foreach, ...
138 * PRETERMBLOCK : beginning a non-code-defining {} block (eg, hash ref)
139 * PREREF : *EXPR where EXPR is not a simple identifier
140 * TERM : expression term
141 * LOOPX : loop exiting command (goto, last, dump, etc)
142 * FTST : file test operator
143 * FUN0 : zero-argument function
144 * FUN1 : not used, except for not, which isn't a UNIOP
145 * BOop : bitwise or or xor
147 * SHop : shift operator
148 * PWop : power operator
149 * PMop : pattern-matching operator
150 * Aop : addition-level operator
151 * Mop : multiplication-level operator
152 * Eop : equality-testing operator
153 * Rop : relational operator <= != gt
155 * Also see LOP and lop() below.
158 #ifdef DEBUGGING /* Serve -DT. */
159 # define REPORT(retval) tokereport((I32)retval)
161 # define REPORT(retval) (retval)
164 #define TOKEN(retval) return ( PL_bufptr = s, REPORT(retval))
165 #define OPERATOR(retval) return (PL_expect = XTERM, PL_bufptr = s, REPORT(retval))
166 #define AOPERATOR(retval) return ao((PL_expect = XTERM, PL_bufptr = s, REPORT(retval)))
167 #define PREBLOCK(retval) return (PL_expect = XBLOCK,PL_bufptr = s, REPORT(retval))
168 #define PRETERMBLOCK(retval) return (PL_expect = XTERMBLOCK,PL_bufptr = s, REPORT(retval))
169 #define PREREF(retval) return (PL_expect = XREF,PL_bufptr = s, REPORT(retval))
170 #define TERM(retval) return (CLINE, PL_expect = XOPERATOR, PL_bufptr = s, REPORT(retval))
171 #define LOOPX(f) return (yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)LOOPEX))
172 #define FTST(f) return (yylval.ival=f, PL_expect=XTERMORDORDOR, PL_bufptr=s, REPORT((int)UNIOP))
173 #define FUN0(f) return (yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0))
174 #define FUN1(f) return (yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC1))
175 #define BOop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITOROP)))
176 #define BAop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITANDOP)))
177 #define SHop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)SHIFTOP)))
178 #define PWop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)POWOP)))
179 #define PMop(f) return(yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MATCHOP))
180 #define Aop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)ADDOP)))
181 #define Mop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MULOP)))
182 #define Eop(f) return (yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)EQOP))
183 #define Rop(f) return (yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)RELOP))
185 /* This bit of chicanery makes a unary function followed by
186 * a parenthesis into a function with one argument, highest precedence.
187 * The UNIDOR macro is for unary functions that can be followed by the //
188 * operator (such as C<shift // 0>).
190 #define UNI2(f,x) { \
194 PL_last_uni = PL_oldbufptr; \
195 PL_last_lop_op = f; \
197 return REPORT( (int)FUNC1 ); \
199 return REPORT( *s=='(' ? (int)FUNC1 : (int)UNIOP ); \
201 #define UNI(f) UNI2(f,XTERM)
202 #define UNIDOR(f) UNI2(f,XTERMORDORDOR)
204 #define UNIBRACK(f) { \
207 PL_last_uni = PL_oldbufptr; \
209 return REPORT( (int)FUNC1 ); \
211 return REPORT( (*s == '(') ? (int)FUNC1 : (int)UNIOP ); \
214 /* grandfather return to old style */
215 #define OLDLOP(f) return(yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)LSTOP)
219 /* how to interpret the yylval associated with the token */
223 TOKENTYPE_OPNUM, /* yylval.ival contains an opcode number */
229 static struct debug_tokens {
231 enum token_type type;
233 } const debug_tokens[] =
235 { ADDOP, TOKENTYPE_OPNUM, "ADDOP" },
236 { ANDAND, TOKENTYPE_NONE, "ANDAND" },
237 { ANDOP, TOKENTYPE_NONE, "ANDOP" },
238 { ANONSUB, TOKENTYPE_IVAL, "ANONSUB" },
239 { ARROW, TOKENTYPE_NONE, "ARROW" },
240 { ASSIGNOP, TOKENTYPE_OPNUM, "ASSIGNOP" },
241 { BITANDOP, TOKENTYPE_OPNUM, "BITANDOP" },
242 { BITOROP, TOKENTYPE_OPNUM, "BITOROP" },
243 { COLONATTR, TOKENTYPE_NONE, "COLONATTR" },
244 { CONTINUE, TOKENTYPE_NONE, "CONTINUE" },
245 { DEFAULT, TOKENTYPE_NONE, "DEFAULT" },
246 { DO, TOKENTYPE_NONE, "DO" },
247 { DOLSHARP, TOKENTYPE_NONE, "DOLSHARP" },
248 { DORDOR, TOKENTYPE_NONE, "DORDOR" },
249 { DOROP, TOKENTYPE_OPNUM, "DOROP" },
250 { DOTDOT, TOKENTYPE_IVAL, "DOTDOT" },
251 { ELSE, TOKENTYPE_NONE, "ELSE" },
252 { ELSIF, TOKENTYPE_IVAL, "ELSIF" },
253 { EQOP, TOKENTYPE_OPNUM, "EQOP" },
254 { FOR, TOKENTYPE_IVAL, "FOR" },
255 { FORMAT, TOKENTYPE_NONE, "FORMAT" },
256 { FUNC, TOKENTYPE_OPNUM, "FUNC" },
257 { FUNC0, TOKENTYPE_OPNUM, "FUNC0" },
258 { FUNC0SUB, TOKENTYPE_OPVAL, "FUNC0SUB" },
259 { FUNC1, TOKENTYPE_OPNUM, "FUNC1" },
260 { FUNCMETH, TOKENTYPE_OPVAL, "FUNCMETH" },
261 { GIVEN, TOKENTYPE_IVAL, "GIVEN" },
262 { HASHBRACK, TOKENTYPE_NONE, "HASHBRACK" },
263 { IF, TOKENTYPE_IVAL, "IF" },
264 { LABEL, TOKENTYPE_PVAL, "LABEL" },
265 { LOCAL, TOKENTYPE_IVAL, "LOCAL" },
266 { LOOPEX, TOKENTYPE_OPNUM, "LOOPEX" },
267 { LSTOP, TOKENTYPE_OPNUM, "LSTOP" },
268 { LSTOPSUB, TOKENTYPE_OPVAL, "LSTOPSUB" },
269 { MATCHOP, TOKENTYPE_OPNUM, "MATCHOP" },
270 { METHOD, TOKENTYPE_OPVAL, "METHOD" },
271 { MULOP, TOKENTYPE_OPNUM, "MULOP" },
272 { MY, TOKENTYPE_IVAL, "MY" },
273 { MYSUB, TOKENTYPE_NONE, "MYSUB" },
274 { NOAMP, TOKENTYPE_NONE, "NOAMP" },
275 { NOTOP, TOKENTYPE_NONE, "NOTOP" },
276 { OROP, TOKENTYPE_IVAL, "OROP" },
277 { OROR, TOKENTYPE_NONE, "OROR" },
278 { PACKAGE, TOKENTYPE_NONE, "PACKAGE" },
279 { PMFUNC, TOKENTYPE_OPVAL, "PMFUNC" },
280 { POSTDEC, TOKENTYPE_NONE, "POSTDEC" },
281 { POSTINC, TOKENTYPE_NONE, "POSTINC" },
282 { POWOP, TOKENTYPE_OPNUM, "POWOP" },
283 { PREDEC, TOKENTYPE_NONE, "PREDEC" },
284 { PREINC, TOKENTYPE_NONE, "PREINC" },
285 { PRIVATEREF, TOKENTYPE_OPVAL, "PRIVATEREF" },
286 { REFGEN, TOKENTYPE_NONE, "REFGEN" },
287 { RELOP, TOKENTYPE_OPNUM, "RELOP" },
288 { SHIFTOP, TOKENTYPE_OPNUM, "SHIFTOP" },
289 { SUB, TOKENTYPE_NONE, "SUB" },
290 { THING, TOKENTYPE_OPVAL, "THING" },
291 { UMINUS, TOKENTYPE_NONE, "UMINUS" },
292 { UNIOP, TOKENTYPE_OPNUM, "UNIOP" },
293 { UNIOPSUB, TOKENTYPE_OPVAL, "UNIOPSUB" },
294 { UNLESS, TOKENTYPE_IVAL, "UNLESS" },
295 { UNTIL, TOKENTYPE_IVAL, "UNTIL" },
296 { USE, TOKENTYPE_IVAL, "USE" },
297 { WHEN, TOKENTYPE_IVAL, "WHEN" },
298 { WHILE, TOKENTYPE_IVAL, "WHILE" },
299 { WORD, TOKENTYPE_OPVAL, "WORD" },
300 { 0, TOKENTYPE_NONE, NULL }
303 /* dump the returned token in rv, plus any optional arg in yylval */
306 S_tokereport(pTHX_ I32 rv)
310 const char *name = NULL;
311 enum token_type type = TOKENTYPE_NONE;
312 const struct debug_tokens *p;
313 SV* const report = newSVpvs("<== ");
315 for (p = debug_tokens; p->token; p++) {
316 if (p->token == (int)rv) {
323 Perl_sv_catpv(aTHX_ report, name);
324 else if ((char)rv > ' ' && (char)rv < '~')
325 Perl_sv_catpvf(aTHX_ report, "'%c'", (char)rv);
327 sv_catpvs(report, "EOF");
329 Perl_sv_catpvf(aTHX_ report, "?? %"IVdf, (IV)rv);
332 case TOKENTYPE_GVVAL: /* doesn't appear to be used */
335 Perl_sv_catpvf(aTHX_ report, "(ival=%"IVdf")", (IV)yylval.ival);
337 case TOKENTYPE_OPNUM:
338 Perl_sv_catpvf(aTHX_ report, "(ival=op_%s)",
339 PL_op_name[yylval.ival]);
342 Perl_sv_catpvf(aTHX_ report, "(pval=\"%s\")", yylval.pval);
344 case TOKENTYPE_OPVAL:
346 Perl_sv_catpvf(aTHX_ report, "(opval=op_%s)",
347 PL_op_name[yylval.opval->op_type]);
348 if (yylval.opval->op_type == OP_CONST) {
349 Perl_sv_catpvf(aTHX_ report, " %s",
350 SvPEEK(cSVOPx_sv(yylval.opval)));
355 sv_catpvs(report, "(opval=null)");
358 PerlIO_printf(Perl_debug_log, "### %s\n\n", SvPV_nolen_const(report));
364 /* print the buffer with suitable escapes */
367 S_printbuf(pTHX_ const char* fmt, const char* s)
369 SV* const tmp = newSVpvs("");
370 PerlIO_printf(Perl_debug_log, fmt, pv_display(tmp, s, strlen(s), 0, 60));
379 * This subroutine detects &&=, ||=, and //= and turns an ANDAND, OROR or DORDOR
380 * into an OP_ANDASSIGN, OP_ORASSIGN, or OP_DORASSIGN
384 S_ao(pTHX_ int toketype)
387 if (*PL_bufptr == '=') {
389 if (toketype == ANDAND)
390 yylval.ival = OP_ANDASSIGN;
391 else if (toketype == OROR)
392 yylval.ival = OP_ORASSIGN;
393 else if (toketype == DORDOR)
394 yylval.ival = OP_DORASSIGN;
402 * When Perl expects an operator and finds something else, no_op
403 * prints the warning. It always prints "<something> found where
404 * operator expected. It prints "Missing semicolon on previous line?"
405 * if the surprise occurs at the start of the line. "do you need to
406 * predeclare ..." is printed out for code like "sub bar; foo bar $x"
407 * where the compiler doesn't know if foo is a method call or a function.
408 * It prints "Missing operator before end of line" if there's nothing
409 * after the missing operator, or "... before <...>" if there is something
410 * after the missing operator.
414 S_no_op(pTHX_ const char *what, char *s)
417 char * const oldbp = PL_bufptr;
418 const bool is_first = (PL_oldbufptr == PL_linestart);
424 yywarn(Perl_form(aTHX_ "%s found where operator expected", what));
425 if (ckWARN_d(WARN_SYNTAX)) {
427 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
428 "\t(Missing semicolon on previous line?)\n");
429 else if (PL_oldoldbufptr && isIDFIRST_lazy_if(PL_oldoldbufptr,UTF)) {
431 for (t = PL_oldoldbufptr; (isALNUM_lazy_if(t,UTF) || *t == ':'); t++)
433 if (t < PL_bufptr && isSPACE(*t))
434 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
435 "\t(Do you need to predeclare %.*s?)\n",
436 (int)(t - PL_oldoldbufptr), PL_oldoldbufptr);
440 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
441 "\t(Missing operator before %.*s?)\n", (int)(s - oldbp), oldbp);
449 * Complain about missing quote/regexp/heredoc terminator.
450 * If it's called with NULL then it cauterizes the line buffer.
451 * If we're in a delimited string and the delimiter is a control
452 * character, it's reformatted into a two-char sequence like ^C.
457 S_missingterm(pTHX_ char *s)
463 char * const nl = strrchr(s,'\n');
469 iscntrl(PL_multi_close)
471 PL_multi_close < 32 || PL_multi_close == 127
475 tmpbuf[1] = (char)toCTRL(PL_multi_close);
480 *tmpbuf = (char)PL_multi_close;
484 q = strchr(s,'"') ? '\'' : '"';
485 Perl_croak(aTHX_ "Can't find string terminator %c%s%c anywhere before EOF",q,s,q);
488 #define FEATURE_IS_ENABLED(name) \
489 ((0 != (PL_hints & HINT_LOCALIZE_HH)) \
490 && S_feature_is_enabled(aTHX_ STR_WITH_LEN(name)))
492 * S_feature_is_enabled
493 * Check whether the named feature is enabled.
496 S_feature_is_enabled(pTHX_ const char *name, STRLEN namelen)
499 HV * const hinthv = GvHV(PL_hintgv);
500 char he_name[32] = "feature_";
501 (void) my_strlcpy(&he_name[8], name, 24);
503 return (hinthv && hv_exists(hinthv, he_name, 8 + namelen));
511 Perl_deprecate(pTHX_ const char *s)
513 if (ckWARN(WARN_DEPRECATED))
514 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED), "Use of %s is deprecated", s);
518 Perl_deprecate_old(pTHX_ const char *s)
520 /* This function should NOT be called for any new deprecated warnings */
521 /* Use Perl_deprecate instead */
523 /* It is here to maintain backward compatibility with the pre-5.8 */
524 /* warnings category hierarchy. The "deprecated" category used to */
525 /* live under the "syntax" category. It is now a top-level category */
526 /* in its own right. */
528 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
529 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
530 "Use of %s is deprecated", s);
534 * experimental text filters for win32 carriage-returns, utf16-to-utf8 and
535 * utf16-to-utf8-reversed.
538 #ifdef PERL_CR_FILTER
542 register const char *s = SvPVX_const(sv);
543 register const char * const e = s + SvCUR(sv);
544 /* outer loop optimized to do nothing if there are no CR-LFs */
546 if (*s++ == '\r' && *s == '\n') {
547 /* hit a CR-LF, need to copy the rest */
548 register char *d = s - 1;
551 if (*s == '\r' && s[1] == '\n')
562 S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
564 const I32 count = FILTER_READ(idx+1, sv, maxlen);
565 if (count > 0 && !maxlen)
573 * Initialize variables. Uses the Perl save_stack to save its state (for
574 * recursive calls to the parser).
578 Perl_lex_start(pTHX_ SV *line)
584 SAVEI32(PL_lex_dojoin);
585 SAVEI32(PL_lex_brackets);
586 SAVEI32(PL_lex_casemods);
587 SAVEI32(PL_lex_starts);
588 SAVEI32(PL_lex_state);
589 SAVEVPTR(PL_lex_inpat);
590 SAVEI32(PL_lex_inwhat);
592 if (PL_lex_state == LEX_KNOWNEXT) {
593 I32 toke = PL_lasttoke;
594 while (--toke >= 0) {
595 SAVEI32(PL_nexttoke[toke].next_type);
596 SAVEVPTR(PL_nexttoke[toke].next_val);
598 SAVEVPTR(PL_nexttoke[toke].next_mad);
600 SAVEI32(PL_lasttoke);
603 SAVESPTR(PL_thistoken);
604 SAVESPTR(PL_thiswhite);
605 SAVESPTR(PL_nextwhite);
606 SAVESPTR(PL_thisopen);
607 SAVESPTR(PL_thisclose);
608 SAVESPTR(PL_thisstuff);
609 SAVEVPTR(PL_thismad);
610 SAVEI32(PL_realtokenstart);
611 SAVEI32(PL_faketokens);
613 SAVEI32(PL_curforce);
615 if (PL_lex_state == LEX_KNOWNEXT) {
616 I32 toke = PL_nexttoke;
617 while (--toke >= 0) {
618 SAVEI32(PL_nexttype[toke]);
619 SAVEVPTR(PL_nextval[toke]);
621 SAVEI32(PL_nexttoke);
624 SAVECOPLINE(PL_curcop);
627 SAVEPPTR(PL_oldbufptr);
628 SAVEPPTR(PL_oldoldbufptr);
629 SAVEPPTR(PL_last_lop);
630 SAVEPPTR(PL_last_uni);
631 SAVEPPTR(PL_linestart);
632 SAVESPTR(PL_linestr);
633 SAVEGENERICPV(PL_lex_brackstack);
634 SAVEGENERICPV(PL_lex_casestack);
635 SAVEDESTRUCTOR_X(restore_rsfp, PL_rsfp);
636 SAVESPTR(PL_lex_stuff);
637 SAVEI32(PL_lex_defer);
638 SAVEI32(PL_sublex_info.sub_inwhat);
639 SAVESPTR(PL_lex_repl);
641 SAVEINT(PL_lex_expect);
643 PL_lex_state = LEX_NORMAL;
647 Newx(PL_lex_brackstack, 120, char);
648 Newx(PL_lex_casestack, 12, char);
650 *PL_lex_casestack = '\0';
662 PL_sublex_info.sub_inwhat = 0;
663 s = SvPV_const(line, len);
664 if (SvREADONLY(line) || !len || s[len-1] != ';') {
665 PL_linestr = len ? newSVsv(line) : newSVpvn(s, 0);
666 if (!len || s[len-1] != ';')
667 sv_catpvs(PL_linestr, "\n;");
670 SvREFCNT_inc_simple_void_NN(line);
673 /* PL_linestr needs to survive until end of scope, not just the next
674 FREETMPS. See changes 17505 and 17546 which fixed the symptoms only. */
675 SAVEFREESV(PL_linestr);
676 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = SvPVX(PL_linestr);
677 PL_bufend = PL_bufptr + SvCUR(PL_linestr);
678 PL_last_lop = PL_last_uni = NULL;
684 * Finalizer for lexing operations. Must be called when the parser is
685 * done with the lexer.
692 PL_doextract = FALSE;
697 * This subroutine has nothing to do with tilting, whether at windmills
698 * or pinball tables. Its name is short for "increment line". It
699 * increments the current line number in CopLINE(PL_curcop) and checks
700 * to see whether the line starts with a comment of the form
701 * # line 500 "foo.pm"
702 * If so, it sets the current line number and file to the values in the comment.
706 S_incline(pTHX_ char *s)
714 CopLINE_inc(PL_curcop);
717 while (SPACE_OR_TAB(*s))
719 if (strnEQ(s, "line", 4))
723 if (SPACE_OR_TAB(*s))
727 while (SPACE_OR_TAB(*s))
735 while (SPACE_OR_TAB(*s))
737 if (*s == '"' && (t = strchr(s+1, '"'))) {
747 while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
749 if (*e != '\n' && *e != '\0')
750 return; /* false alarm */
756 const char * const cf = CopFILE(PL_curcop);
757 STRLEN tmplen = cf ? strlen(cf) : 0;
758 if (tmplen > 7 && strnEQ(cf, "(eval ", 6)) {
759 /* must copy *{"::_<(eval N)[oldfilename:L]"}
760 * to *{"::_<newfilename"} */
761 char smallbuf[256], smallbuf2[256];
762 char *tmpbuf, *tmpbuf2;
764 STRLEN tmplen2 = strlen(s);
765 if (tmplen + 3 < sizeof smallbuf)
768 Newx(tmpbuf, tmplen + 3, char);
769 if (tmplen2 + 3 < sizeof smallbuf2)
772 Newx(tmpbuf2, tmplen2 + 3, char);
773 tmpbuf[0] = tmpbuf2[0] = '_';
774 tmpbuf[1] = tmpbuf2[1] = '<';
775 memcpy(tmpbuf + 2, cf, ++tmplen);
776 memcpy(tmpbuf2 + 2, s, ++tmplen2);
778 gvp = (GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, FALSE);
780 gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE);
782 gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE);
783 /* adjust ${"::_<newfilename"} to store the new file name */
784 GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2);
785 GvHV(gv2) = (HV*)SvREFCNT_inc(GvHV(*gvp));
786 GvAV(gv2) = (AV*)SvREFCNT_inc(GvAV(*gvp));
789 if (tmpbuf != smallbuf) Safefree(tmpbuf);
790 if (tmpbuf2 != smallbuf2) Safefree(tmpbuf2);
793 CopFILE_free(PL_curcop);
794 CopFILE_set(PL_curcop, s);
797 CopLINE_set(PL_curcop, atoi(n)-1);
801 /* skip space before PL_thistoken */
804 S_skipspace0(pTHX_ register char *s)
811 PL_thiswhite = newSVpvs("");
812 sv_catsv(PL_thiswhite, PL_skipwhite);
813 sv_free(PL_skipwhite);
816 PL_realtokenstart = s - SvPVX(PL_linestr);
820 /* skip space after PL_thistoken */
823 S_skipspace1(pTHX_ register char *s)
825 const char *start = s;
826 I32 startoff = start - SvPVX(PL_linestr);
831 start = SvPVX(PL_linestr) + startoff;
832 if (!PL_thistoken && PL_realtokenstart >= 0) {
833 const char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
834 PL_thistoken = newSVpvn(tstart, start - tstart);
836 PL_realtokenstart = -1;
839 PL_nextwhite = newSVpvs("");
840 sv_catsv(PL_nextwhite, PL_skipwhite);
841 sv_free(PL_skipwhite);
848 S_skipspace2(pTHX_ register char *s, SV **svp)
851 const I32 bufptroff = PL_bufptr - SvPVX(PL_linestr);
852 const I32 startoff = s - SvPVX(PL_linestr);
855 PL_bufptr = SvPVX(PL_linestr) + bufptroff;
856 if (!PL_madskills || !svp)
858 start = SvPVX(PL_linestr) + startoff;
859 if (!PL_thistoken && PL_realtokenstart >= 0) {
860 char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
861 PL_thistoken = newSVpvn(tstart, start - tstart);
862 PL_realtokenstart = -1;
867 sv_setsv(*svp, PL_skipwhite);
868 sv_free(PL_skipwhite);
877 S_update_debugger_info_pv(pTHX_ const char *buf, STRLEN len)
879 AV *av = CopFILEAVx(PL_curcop);
881 SV * const sv = newSV(0);
882 sv_upgrade(sv, SVt_PVMG);
883 sv_setpvn(sv, buf, len);
886 av_store(av, (I32)CopLINE(PL_curcop), sv);
891 S_update_debugger_info_sv(pTHX_ SV *orig_sv)
893 AV *av = CopFILEAVx(PL_curcop);
895 SV * const sv = newSV(0);
896 sv_upgrade(sv, SVt_PVMG);
897 sv_setsv(sv, orig_sv);
900 av_store(av, (I32)CopLINE(PL_curcop), sv);
906 * Called to gobble the appropriate amount and type of whitespace.
907 * Skips comments as well.
911 S_skipspace(pTHX_ register char *s)
916 int startoff = s - SvPVX(PL_linestr);
919 sv_free(PL_skipwhite);
924 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
925 while (s < PL_bufend && SPACE_OR_TAB(*s))
935 SSize_t oldprevlen, oldoldprevlen;
936 SSize_t oldloplen = 0, oldunilen = 0;
937 while (s < PL_bufend && isSPACE(*s)) {
938 if (*s++ == '\n' && PL_in_eval && !PL_rsfp)
943 if (s < PL_bufend && *s == '#') {
944 while (s < PL_bufend && *s != '\n')
948 if (PL_in_eval && !PL_rsfp) {
955 /* only continue to recharge the buffer if we're at the end
956 * of the buffer, we're not reading from a source filter, and
957 * we're in normal lexing mode
959 if (s < PL_bufend || !PL_rsfp || PL_sublex_info.sub_inwhat ||
960 PL_lex_state == LEX_FORMLINE)
967 /* try to recharge the buffer */
969 curoff = s - SvPVX(PL_linestr);
972 if ((s = filter_gets(PL_linestr, PL_rsfp,
973 (prevlen = SvCUR(PL_linestr)))) == NULL)
976 if (PL_madskills && curoff != startoff) {
978 PL_skipwhite = newSVpvs("");
979 sv_catpvn(PL_skipwhite, SvPVX(PL_linestr) + startoff,
983 /* mustn't throw out old stuff yet if madpropping */
984 SvCUR(PL_linestr) = curoff;
985 s = SvPVX(PL_linestr) + curoff;
987 if (curoff && s[-1] == '\n')
991 /* end of file. Add on the -p or -n magic */
992 /* XXX these shouldn't really be added here, can't set PL_faketokens */
996 ";}continue{print or die qq(-p destination: $!\\n);}");
999 ";}continue{print or die qq(-p destination: $!\\n);}");
1001 PL_minus_n = PL_minus_p = 0;
1003 else if (PL_minus_n) {
1005 sv_catpvn(PL_linestr, ";}", 2);
1007 sv_setpvn(PL_linestr, ";}", 2);
1013 sv_catpvn(PL_linestr,";", 1);
1015 sv_setpvn(PL_linestr,";", 1);
1018 /* reset variables for next time we lex */
1019 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart
1025 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
1026 PL_last_lop = PL_last_uni = NULL;
1028 /* Close the filehandle. Could be from -P preprocessor,
1029 * STDIN, or a regular file. If we were reading code from
1030 * STDIN (because the commandline held no -e or filename)
1031 * then we don't close it, we reset it so the code can
1032 * read from STDIN too.
1035 if (PL_preprocess && !PL_in_eval)
1036 (void)PerlProc_pclose(PL_rsfp);
1037 else if ((PerlIO*)PL_rsfp == PerlIO_stdin())
1038 PerlIO_clearerr(PL_rsfp);
1040 (void)PerlIO_close(PL_rsfp);
1045 /* not at end of file, so we only read another line */
1046 /* make corresponding updates to old pointers, for yyerror() */
1047 oldprevlen = PL_oldbufptr - PL_bufend;
1048 oldoldprevlen = PL_oldoldbufptr - PL_bufend;
1050 oldunilen = PL_last_uni - PL_bufend;
1052 oldloplen = PL_last_lop - PL_bufend;
1053 PL_linestart = PL_bufptr = s + prevlen;
1054 PL_bufend = s + SvCUR(PL_linestr);
1056 PL_oldbufptr = s + oldprevlen;
1057 PL_oldoldbufptr = s + oldoldprevlen;
1059 PL_last_uni = s + oldunilen;
1061 PL_last_lop = s + oldloplen;
1064 /* debugger active and we're not compiling the debugger code,
1065 * so store the line into the debugger's array of lines
1067 if (PERLDB_LINE && PL_curstash != PL_debstash)
1068 update_debugger_info_pv(PL_bufptr, PL_bufend - PL_bufptr);
1075 PL_skipwhite = newSVpvs("");
1076 curoff = s - SvPVX(PL_linestr);
1077 if (curoff - startoff)
1078 sv_catpvn(PL_skipwhite, SvPVX(PL_linestr) + startoff,
1087 * Check the unary operators to ensure there's no ambiguity in how they're
1088 * used. An ambiguous piece of code would be:
1090 * This doesn't mean rand() + 5. Because rand() is a unary operator,
1091 * the +5 is its argument.
1101 if (PL_oldoldbufptr != PL_last_uni)
1103 while (isSPACE(*PL_last_uni))
1106 while (isALNUM_lazy_if(s,UTF) || *s == '-')
1108 if ((t = strchr(s, '(')) && t < PL_bufptr)
1111 if (ckWARN_d(WARN_AMBIGUOUS)){
1112 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
1113 "Warning: Use of \"%.*s\" without parentheses is ambiguous",
1114 (int)(s - PL_last_uni), PL_last_uni);
1119 * LOP : macro to build a list operator. Its behaviour has been replaced
1120 * with a subroutine, S_lop() for which LOP is just another name.
1123 #define LOP(f,x) return lop(f,x,s)
1127 * Build a list operator (or something that might be one). The rules:
1128 * - if we have a next token, then it's a list operator [why?]
1129 * - if the next thing is an opening paren, then it's a function
1130 * - else it's a list operator
1134 S_lop(pTHX_ I32 f, int x, char *s)
1141 PL_last_lop = PL_oldbufptr;
1142 PL_last_lop_op = (OPCODE)f;
1145 return REPORT(LSTOP);
1148 return REPORT(LSTOP);
1151 return REPORT(FUNC);
1154 return REPORT(FUNC);
1156 return REPORT(LSTOP);
1162 * Sets up for an eventual force_next(). start_force(0) basically does
1163 * an unshift, while start_force(-1) does a push. yylex removes items
1168 S_start_force(pTHX_ int where)
1172 if (where < 0) /* so people can duplicate start_force(PL_curforce) */
1173 where = PL_lasttoke;
1174 assert(PL_curforce < 0 || PL_curforce == where);
1175 if (PL_curforce != where) {
1176 for (i = PL_lasttoke; i > where; --i) {
1177 PL_nexttoke[i] = PL_nexttoke[i-1];
1181 if (PL_curforce < 0) /* in case of duplicate start_force() */
1182 Zero(&PL_nexttoke[where], 1, NEXTTOKE);
1183 PL_curforce = where;
1186 curmad('^', newSVpvs(""));
1187 CURMAD('_', PL_nextwhite);
1192 S_curmad(pTHX_ char slot, SV *sv)
1198 if (PL_curforce < 0)
1199 where = &PL_thismad;
1201 where = &PL_nexttoke[PL_curforce].next_mad;
1204 sv_setpvn(sv, "", 0);
1207 if (UTF && is_utf8_string((U8*)SvPVX(sv), SvCUR(sv)))
1209 else if (PL_encoding) {
1210 sv_recode_to_utf8(sv, PL_encoding);
1215 /* keep a slot open for the head of the list? */
1216 if (slot != '_' && *where && (*where)->mad_key == '^') {
1217 (*where)->mad_key = slot;
1218 sv_free((*where)->mad_val);
1219 (*where)->mad_val = (void*)sv;
1222 addmad(newMADsv(slot, sv), where, 0);
1225 # define start_force(where) NOOP
1226 # define curmad(slot, sv) NOOP
1231 * When the lexer realizes it knows the next token (for instance,
1232 * it is reordering tokens for the parser) then it can call S_force_next
1233 * to know what token to return the next time the lexer is called. Caller
1234 * will need to set PL_nextval[] (or PL_nexttoke[].next_val with PERL_MAD),
1235 * and possibly PL_expect to ensure the lexer handles the token correctly.
1239 S_force_next(pTHX_ I32 type)
1243 if (PL_curforce < 0)
1244 start_force(PL_lasttoke);
1245 PL_nexttoke[PL_curforce].next_type = type;
1246 if (PL_lex_state != LEX_KNOWNEXT)
1247 PL_lex_defer = PL_lex_state;
1248 PL_lex_state = LEX_KNOWNEXT;
1249 PL_lex_expect = PL_expect;
1252 PL_nexttype[PL_nexttoke] = type;
1254 if (PL_lex_state != LEX_KNOWNEXT) {
1255 PL_lex_defer = PL_lex_state;
1256 PL_lex_expect = PL_expect;
1257 PL_lex_state = LEX_KNOWNEXT;
1263 S_newSV_maybe_utf8(pTHX_ const char *start, STRLEN len)
1266 SV * const sv = newSVpvn(start,len);
1267 if (UTF && !IN_BYTES && is_utf8_string((const U8*)start, len))
1274 * When the lexer knows the next thing is a word (for instance, it has
1275 * just seen -> and it knows that the next char is a word char, then
1276 * it calls S_force_word to stick the next word into the PL_next lookahead.
1279 * char *start : buffer position (must be within PL_linestr)
1280 * int token : PL_next will be this type of bare word (e.g., METHOD,WORD)
1281 * int check_keyword : if true, Perl checks to make sure the word isn't
1282 * a keyword (do this if the word is a label, e.g. goto FOO)
1283 * int allow_pack : if true, : characters will also be allowed (require,
1284 * use, etc. do this)
1285 * int allow_initial_tick : used by the "sub" lexer only.
1289 S_force_word(pTHX_ register char *start, int token, int check_keyword, int allow_pack, int allow_initial_tick)
1295 start = SKIPSPACE1(start);
1297 if (isIDFIRST_lazy_if(s,UTF) ||
1298 (allow_pack && *s == ':') ||
1299 (allow_initial_tick && *s == '\'') )
1301 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, allow_pack, &len);
1302 if (check_keyword && keyword(PL_tokenbuf, len, 0))
1304 start_force(PL_curforce);
1306 curmad('X', newSVpvn(start,s-start));
1307 if (token == METHOD) {
1312 PL_expect = XOPERATOR;
1315 NEXTVAL_NEXTTOKE.opval
1316 = (OP*)newSVOP(OP_CONST,0,
1317 S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
1318 NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
1326 * Called when the lexer wants $foo *foo &foo etc, but the program
1327 * text only contains the "foo" portion. The first argument is a pointer
1328 * to the "foo", and the second argument is the type symbol to prefix.
1329 * Forces the next token to be a "WORD".
1330 * Creates the symbol if it didn't already exist (via gv_fetchpv()).
1334 S_force_ident(pTHX_ register const char *s, int kind)
1338 const STRLEN len = strlen(s);
1339 OP* const o = (OP*)newSVOP(OP_CONST, 0, newSVpvn(s, len));
1340 start_force(PL_curforce);
1341 NEXTVAL_NEXTTOKE.opval = o;
1344 o->op_private = OPpCONST_ENTERED;
1345 /* XXX see note in pp_entereval() for why we forgo typo
1346 warnings if the symbol must be introduced in an eval.
1348 gv_fetchpvn_flags(s, len,
1349 PL_in_eval ? (GV_ADDMULTI | GV_ADDINEVAL)
1351 kind == '$' ? SVt_PV :
1352 kind == '@' ? SVt_PVAV :
1353 kind == '%' ? SVt_PVHV :
1361 Perl_str_to_version(pTHX_ SV *sv)
1366 const char *start = SvPV_const(sv,len);
1367 const char * const end = start + len;
1368 const bool utf = SvUTF8(sv) ? TRUE : FALSE;
1369 while (start < end) {
1373 n = utf8n_to_uvchr((U8*)start, len, &skip, 0);
1378 retval += ((NV)n)/nshift;
1387 * Forces the next token to be a version number.
1388 * If the next token appears to be an invalid version number, (e.g. "v2b"),
1389 * and if "guessing" is TRUE, then no new token is created (and the caller
1390 * must use an alternative parsing method).
1394 S_force_version(pTHX_ char *s, int guessing)
1400 I32 startoff = s - SvPVX(PL_linestr);
1409 while (isDIGIT(*d) || *d == '_' || *d == '.')
1413 start_force(PL_curforce);
1414 curmad('X', newSVpvn(s,d-s));
1417 if (*d == ';' || isSPACE(*d) || *d == '}' || !*d) {
1419 s = scan_num(s, &yylval);
1420 version = yylval.opval;
1421 ver = cSVOPx(version)->op_sv;
1422 if (SvPOK(ver) && !SvNIOK(ver)) {
1423 SvUPGRADE(ver, SVt_PVNV);
1424 SvNV_set(ver, str_to_version(ver));
1425 SvNOK_on(ver); /* hint that it is a version */
1428 else if (guessing) {
1431 sv_free(PL_nextwhite); /* let next token collect whitespace */
1433 s = SvPVX(PL_linestr) + startoff;
1441 if (PL_madskills && !version) {
1442 sv_free(PL_nextwhite); /* let next token collect whitespace */
1444 s = SvPVX(PL_linestr) + startoff;
1447 /* NOTE: The parser sees the package name and the VERSION swapped */
1448 start_force(PL_curforce);
1449 NEXTVAL_NEXTTOKE.opval = version;
1457 * Tokenize a quoted string passed in as an SV. It finds the next
1458 * chunk, up to end of string or a backslash. It may make a new
1459 * SV containing that chunk (if HINT_NEW_STRING is on). It also
1464 S_tokeq(pTHX_ SV *sv)
1468 register char *send;
1476 s = SvPV_force(sv, len);
1477 if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1)
1480 while (s < send && *s != '\\')
1485 if ( PL_hints & HINT_NEW_STRING ) {
1486 pv = sv_2mortal(newSVpvn(SvPVX_const(pv), len));
1492 if (s + 1 < send && (s[1] == '\\'))
1493 s++; /* all that, just for this */
1498 SvCUR_set(sv, d - SvPVX_const(sv));
1500 if ( PL_hints & HINT_NEW_STRING )
1501 return new_constant(NULL, 0, "q", sv, pv, "q");
1506 * Now come three functions related to double-quote context,
1507 * S_sublex_start, S_sublex_push, and S_sublex_done. They're used when
1508 * converting things like "\u\Lgnat" into ucfirst(lc("gnat")). They
1509 * interact with PL_lex_state, and create fake ( ... ) argument lists
1510 * to handle functions and concatenation.
1511 * They assume that whoever calls them will be setting up a fake
1512 * join call, because each subthing puts a ',' after it. This lets
1515 * join($, , 'lower ', lcfirst( 'uPpEr', ) ,)
1517 * (I'm not sure whether the spurious commas at the end of lcfirst's
1518 * arguments and join's arguments are created or not).
1523 * Assumes that yylval.ival is the op we're creating (e.g. OP_LCFIRST).
1525 * Pattern matching will set PL_lex_op to the pattern-matching op to
1526 * make (we return THING if yylval.ival is OP_NULL, PMFUNC otherwise).
1528 * OP_CONST and OP_READLINE are easy--just make the new op and return.
1530 * Everything else becomes a FUNC.
1532 * Sets PL_lex_state to LEX_INTERPPUSH unless (ival was OP_NULL or we
1533 * had an OP_CONST or OP_READLINE). This just sets us up for a
1534 * call to S_sublex_push().
1538 S_sublex_start(pTHX)
1541 register const I32 op_type = yylval.ival;
1543 if (op_type == OP_NULL) {
1544 yylval.opval = PL_lex_op;
1548 if (op_type == OP_CONST || op_type == OP_READLINE) {
1549 SV *sv = tokeq(PL_lex_stuff);
1551 if (SvTYPE(sv) == SVt_PVIV) {
1552 /* Overloaded constants, nothing fancy: Convert to SVt_PV: */
1554 const char * const p = SvPV_const(sv, len);
1555 SV * const nsv = newSVpvn(p, len);
1561 yylval.opval = (OP*)newSVOP(op_type, 0, sv);
1562 PL_lex_stuff = NULL;
1563 /* Allow <FH> // "foo" */
1564 if (op_type == OP_READLINE)
1565 PL_expect = XTERMORDORDOR;
1568 else if (op_type == OP_BACKTICK && PL_lex_op) {
1569 /* readpipe() vas overriden */
1570 cSVOPx(cLISTOPx(cUNOPx(PL_lex_op)->op_first)->op_first->op_sibling)->op_sv = tokeq(PL_lex_stuff);
1571 yylval.opval = PL_lex_op;
1573 PL_lex_stuff = NULL;
1577 PL_sublex_info.super_state = PL_lex_state;
1578 PL_sublex_info.sub_inwhat = op_type;
1579 PL_sublex_info.sub_op = PL_lex_op;
1580 PL_lex_state = LEX_INTERPPUSH;
1584 yylval.opval = PL_lex_op;
1594 * Create a new scope to save the lexing state. The scope will be
1595 * ended in S_sublex_done. Returns a '(', starting the function arguments
1596 * to the uc, lc, etc. found before.
1597 * Sets PL_lex_state to LEX_INTERPCONCAT.
1606 PL_lex_state = PL_sublex_info.super_state;
1607 SAVEI32(PL_lex_dojoin);
1608 SAVEI32(PL_lex_brackets);
1609 SAVEI32(PL_lex_casemods);
1610 SAVEI32(PL_lex_starts);
1611 SAVEI32(PL_lex_state);
1612 SAVEVPTR(PL_lex_inpat);
1613 SAVEI32(PL_lex_inwhat);
1614 SAVECOPLINE(PL_curcop);
1615 SAVEPPTR(PL_bufptr);
1616 SAVEPPTR(PL_bufend);
1617 SAVEPPTR(PL_oldbufptr);
1618 SAVEPPTR(PL_oldoldbufptr);
1619 SAVEPPTR(PL_last_lop);
1620 SAVEPPTR(PL_last_uni);
1621 SAVEPPTR(PL_linestart);
1622 SAVESPTR(PL_linestr);
1623 SAVEGENERICPV(PL_lex_brackstack);
1624 SAVEGENERICPV(PL_lex_casestack);
1626 PL_linestr = PL_lex_stuff;
1627 PL_lex_stuff = NULL;
1629 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart
1630 = SvPVX(PL_linestr);
1631 PL_bufend += SvCUR(PL_linestr);
1632 PL_last_lop = PL_last_uni = NULL;
1633 SAVEFREESV(PL_linestr);
1635 PL_lex_dojoin = FALSE;
1636 PL_lex_brackets = 0;
1637 Newx(PL_lex_brackstack, 120, char);
1638 Newx(PL_lex_casestack, 12, char);
1639 PL_lex_casemods = 0;
1640 *PL_lex_casestack = '\0';
1642 PL_lex_state = LEX_INTERPCONCAT;
1643 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
1645 PL_lex_inwhat = PL_sublex_info.sub_inwhat;
1646 if (PL_lex_inwhat == OP_MATCH || PL_lex_inwhat == OP_QR || PL_lex_inwhat == OP_SUBST)
1647 PL_lex_inpat = PL_sublex_info.sub_op;
1649 PL_lex_inpat = NULL;
1656 * Restores lexer state after a S_sublex_push.
1663 if (!PL_lex_starts++) {
1664 SV * const sv = newSVpvs("");
1665 if (SvUTF8(PL_linestr))
1667 PL_expect = XOPERATOR;
1668 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
1672 if (PL_lex_casemods) { /* oops, we've got some unbalanced parens */
1673 PL_lex_state = LEX_INTERPCASEMOD;
1677 /* Is there a right-hand side to take care of? (s//RHS/ or tr//RHS/) */
1678 if (PL_lex_repl && (PL_lex_inwhat == OP_SUBST || PL_lex_inwhat == OP_TRANS)) {
1679 PL_linestr = PL_lex_repl;
1681 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
1682 PL_bufend += SvCUR(PL_linestr);
1683 PL_last_lop = PL_last_uni = NULL;
1684 SAVEFREESV(PL_linestr);
1685 PL_lex_dojoin = FALSE;
1686 PL_lex_brackets = 0;
1687 PL_lex_casemods = 0;
1688 *PL_lex_casestack = '\0';
1690 if (SvEVALED(PL_lex_repl)) {
1691 PL_lex_state = LEX_INTERPNORMAL;
1693 /* we don't clear PL_lex_repl here, so that we can check later
1694 whether this is an evalled subst; that means we rely on the
1695 logic to ensure sublex_done() is called again only via the
1696 branch (in yylex()) that clears PL_lex_repl, else we'll loop */
1699 PL_lex_state = LEX_INTERPCONCAT;
1709 PL_endwhite = newSVpvs("");
1710 sv_catsv(PL_endwhite, PL_thiswhite);
1714 sv_setpvn(PL_thistoken,"",0);
1716 PL_realtokenstart = -1;
1720 PL_bufend = SvPVX(PL_linestr);
1721 PL_bufend += SvCUR(PL_linestr);
1722 PL_expect = XOPERATOR;
1723 PL_sublex_info.sub_inwhat = 0;
1731 Extracts a pattern, double-quoted string, or transliteration. This
1734 It looks at PL_lex_inwhat and PL_lex_inpat to find out whether it's
1735 processing a pattern (PL_lex_inpat is true), a transliteration
1736 (PL_lex_inwhat == OP_TRANS is true), or a double-quoted string.
1738 Returns a pointer to the character scanned up to. If this is
1739 advanced from the start pointer supplied (i.e. if anything was
1740 successfully parsed), will leave an OP for the substring scanned
1741 in yylval. Caller must intuit reason for not parsing further
1742 by looking at the next characters herself.
1746 double-quoted style: \r and \n
1747 regexp special ones: \D \s
1750 case and quoting: \U \Q \E
1751 stops on @ and $, but not for $ as tail anchor
1753 In transliterations:
1754 characters are VERY literal, except for - not at the start or end
1755 of the string, which indicates a range. If the range is in bytes,
1756 scan_const expands the range to the full set of intermediate
1757 characters. If the range is in utf8, the hyphen is replaced with
1758 a certain range mark which will be handled by pmtrans() in op.c.
1760 In double-quoted strings:
1762 double-quoted style: \r and \n
1764 deprecated backrefs: \1 (in substitution replacements)
1765 case and quoting: \U \Q \E
1768 scan_const does *not* construct ops to handle interpolated strings.
1769 It stops processing as soon as it finds an embedded $ or @ variable
1770 and leaves it to the caller to work out what's going on.
1772 embedded arrays (whether in pattern or not) could be:
1773 @foo, @::foo, @'foo, @{foo}, @$foo, @+, @-.
1775 $ in double-quoted strings must be the symbol of an embedded scalar.
1777 $ in pattern could be $foo or could be tail anchor. Assumption:
1778 it's a tail anchor if $ is the last thing in the string, or if it's
1779 followed by one of "()| \r\n\t"
1781 \1 (backreferences) are turned into $1
1783 The structure of the code is
1784 while (there's a character to process) {
1785 handle transliteration ranges
1786 skip regexp comments /(?#comment)/ and codes /(?{code})/
1787 skip #-initiated comments in //x patterns
1788 check for embedded arrays
1789 check for embedded scalars
1791 leave intact backslashes from leaveit (below)
1792 deprecate \1 in substitution replacements
1793 handle string-changing backslashes \l \U \Q \E, etc.
1794 switch (what was escaped) {
1795 handle \- in a transliteration (becomes a literal -)
1796 handle \132 (octal characters)
1797 handle \x15 and \x{1234} (hex characters)
1798 handle \N{name} (named characters)
1799 handle \cV (control characters)
1800 handle printf-style backslashes (\f, \r, \n, etc)
1802 } (end if backslash)
1803 } (end while character to read)
1808 S_scan_const(pTHX_ char *start)
1811 register char *send = PL_bufend; /* end of the constant */
1812 SV *sv = newSV(send - start); /* sv for the constant */
1813 register char *s = start; /* start of the constant */
1814 register char *d = SvPVX(sv); /* destination for copies */
1815 bool dorange = FALSE; /* are we in a translit range? */
1816 bool didrange = FALSE; /* did we just finish a range? */
1817 I32 has_utf8 = FALSE; /* Output constant is UTF8 */
1818 I32 this_utf8 = UTF; /* The source string is assumed to be UTF8 */
1821 UV literal_endpoint = 0;
1822 bool native_range = TRUE; /* turned to FALSE if the first endpoint is Unicode. */
1825 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
1826 /* If we are doing a trans and we know we want UTF8 set expectation */
1827 has_utf8 = PL_sublex_info.sub_op->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF);
1828 this_utf8 = PL_sublex_info.sub_op->op_private & (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
1832 while (s < send || dorange) {
1833 /* get transliterations out of the way (they're most literal) */
1834 if (PL_lex_inwhat == OP_TRANS) {
1835 /* expand a range A-Z to the full set of characters. AIE! */
1837 I32 i; /* current expanded character */
1838 I32 min; /* first character in range */
1839 I32 max; /* last character in range */
1850 char * const c = (char*)utf8_hop((U8*)d, -1);
1854 *c = (char)UTF_TO_NATIVE(0xff);
1855 /* mark the range as done, and continue */
1861 i = d - SvPVX_const(sv); /* remember current offset */
1864 SvLEN(sv) + (has_utf8 ?
1865 (512 - UTF_CONTINUATION_MARK +
1868 /* How many two-byte within 0..255: 128 in UTF-8,
1869 * 96 in UTF-8-mod. */
1871 SvGROW(sv, SvLEN(sv) + 256); /* never more than 256 chars in a range */
1873 d = SvPVX(sv) + i; /* refresh d after realloc */
1877 for (j = 0; j <= 1; j++) {
1878 char * const c = (char*)utf8_hop((U8*)d, -1);
1879 const UV uv = utf8n_to_uvchr((U8*)c, d - c, NULL, 0);
1885 max = (U8)0xff; /* only to \xff */
1886 uvmax = uv; /* \x{100} to uvmax */
1888 d = c; /* eat endpoint chars */
1893 d -= 2; /* eat the first char and the - */
1894 min = (U8)*d; /* first char in range */
1895 max = (U8)d[1]; /* last char in range */
1902 "Invalid range \"%c-%c\" in transliteration operator",
1903 (char)min, (char)max);
1907 if (literal_endpoint == 2 &&
1908 ((isLOWER(min) && isLOWER(max)) ||
1909 (isUPPER(min) && isUPPER(max)))) {
1911 for (i = min; i <= max; i++)
1913 *d++ = NATIVE_TO_NEED(has_utf8,i);
1915 for (i = min; i <= max; i++)
1917 *d++ = NATIVE_TO_NEED(has_utf8,i);
1922 for (i = min; i <= max; i++)
1925 const U8 ch = (U8)NATIVE_TO_UTF(i);
1926 if (UNI_IS_INVARIANT(ch))
1929 *d++ = (U8)UTF8_EIGHT_BIT_HI(ch);
1930 *d++ = (U8)UTF8_EIGHT_BIT_LO(ch);
1939 d = (char*)uvchr_to_utf8((U8*)d, 0x100);
1941 *d++ = (char)UTF_TO_NATIVE(0xff);
1943 d = (char*)uvchr_to_utf8((U8*)d, uvmax);
1947 /* mark the range as done, and continue */
1951 literal_endpoint = 0;
1956 /* range begins (ignore - as first or last char) */
1957 else if (*s == '-' && s+1 < send && s != start) {
1959 Perl_croak(aTHX_ "Ambiguous range in transliteration operator");
1966 *d++ = (char)UTF_TO_NATIVE(0xff); /* use illegal utf8 byte--see pmtrans */
1976 literal_endpoint = 0;
1977 native_range = TRUE;
1982 /* if we get here, we're not doing a transliteration */
1984 /* skip for regexp comments /(?#comment)/ and code /(?{code})/,
1985 except for the last char, which will be done separately. */
1986 else if (*s == '(' && PL_lex_inpat && s[1] == '?') {
1988 while (s+1 < send && *s != ')')
1989 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1991 else if (s[2] == '{' /* This should match regcomp.c */
1992 || ((s[2] == 'p' || s[2] == '?') && s[3] == '{'))
1995 char *regparse = s + (s[2] == '{' ? 3 : 4);
1998 while (count && (c = *regparse)) {
1999 if (c == '\\' && regparse[1])
2007 if (*regparse != ')')
2008 regparse--; /* Leave one char for continuation. */
2009 while (s < regparse)
2010 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
2014 /* likewise skip #-initiated comments in //x patterns */
2015 else if (*s == '#' && PL_lex_inpat &&
2016 ((PMOP*)PL_lex_inpat)->op_pmflags & PMf_EXTENDED) {
2017 while (s+1 < send && *s != '\n')
2018 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
2021 /* check for embedded arrays
2022 (@foo, @::foo, @'foo, @{foo}, @$foo, @+, @-)
2024 else if (*s == '@' && s[1]) {
2025 if (isALNUM_lazy_if(s+1,UTF))
2027 if (strchr(":'{$", s[1]))
2029 if (!PL_lex_inpat && (s[1] == '+' || s[1] == '-'))
2030 break; /* in regexp, neither @+ nor @- are interpolated */
2033 /* check for embedded scalars. only stop if we're sure it's a
2036 else if (*s == '$') {
2037 if (!PL_lex_inpat) /* not a regexp, so $ must be var */
2039 if (s + 1 < send && !strchr("()| \r\n\t", s[1]))
2040 break; /* in regexp, $ might be tail anchor */
2043 /* End of else if chain - OP_TRANS rejoin rest */
2046 if (*s == '\\' && s+1 < send) {
2049 /* deprecate \1 in strings and substitution replacements */
2050 if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat &&
2051 isDIGIT(*s) && *s != '0' && !isDIGIT(s[1]))
2053 if (ckWARN(WARN_SYNTAX))
2054 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "\\%c better written as $%c", *s, *s);
2059 /* string-change backslash escapes */
2060 if (PL_lex_inwhat != OP_TRANS && *s && strchr("lLuUEQ", *s)) {
2064 /* skip any other backslash escapes in a pattern */
2065 else if (PL_lex_inpat) {
2066 *d++ = NATIVE_TO_NEED(has_utf8,'\\');
2067 goto default_action;
2070 /* if we get here, it's either a quoted -, or a digit */
2073 /* quoted - in transliterations */
2075 if (PL_lex_inwhat == OP_TRANS) {
2082 if ((isALPHA(*s) || isDIGIT(*s)) &&
2084 Perl_warner(aTHX_ packWARN(WARN_MISC),
2085 "Unrecognized escape \\%c passed through",
2087 /* default action is to copy the quoted character */
2088 goto default_action;
2091 /* \132 indicates an octal constant */
2092 case '0': case '1': case '2': case '3':
2093 case '4': case '5': case '6': case '7':
2097 uv = grok_oct(s, &len, &flags, NULL);
2100 goto NUM_ESCAPE_INSERT;
2102 /* \x24 indicates a hex constant */
2106 char* const e = strchr(s, '}');
2107 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
2108 PERL_SCAN_DISALLOW_PREFIX;
2113 yyerror("Missing right brace on \\x{}");
2117 uv = grok_hex(s, &len, &flags, NULL);
2123 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
2124 uv = grok_hex(s, &len, &flags, NULL);
2130 /* Insert oct or hex escaped character.
2131 * There will always enough room in sv since such
2132 * escapes will be longer than any UTF-8 sequence
2133 * they can end up as. */
2135 /* We need to map to chars to ASCII before doing the tests
2138 if (!UNI_IS_INVARIANT(NATIVE_TO_UNI(uv))) {
2139 if (!has_utf8 && uv > 255) {
2140 /* Might need to recode whatever we have
2141 * accumulated so far if it contains any
2144 * (Can't we keep track of that and avoid
2145 * this rescan? --jhi)
2149 for (c = (U8 *) SvPVX(sv); c < (U8 *)d; c++) {
2150 if (!NATIVE_IS_INVARIANT(*c)) {
2155 const STRLEN offset = d - SvPVX_const(sv);
2157 d = SvGROW(sv, SvLEN(sv) + hicount + 1) + offset;
2161 while (src >= (const U8 *)SvPVX_const(sv)) {
2162 if (!NATIVE_IS_INVARIANT(*src)) {
2163 const U8 ch = NATIVE_TO_ASCII(*src);
2164 *dst-- = (U8)UTF8_EIGHT_BIT_LO(ch);
2165 *dst-- = (U8)UTF8_EIGHT_BIT_HI(ch);
2175 if (has_utf8 || uv > 255) {
2176 d = (char*)uvchr_to_utf8((U8*)d, uv);
2178 if (PL_lex_inwhat == OP_TRANS &&
2179 PL_sublex_info.sub_op) {
2180 PL_sublex_info.sub_op->op_private |=
2181 (PL_lex_repl ? OPpTRANS_FROM_UTF
2185 if (uv > 255 && !dorange)
2186 native_range = FALSE;
2198 /* \N{LATIN SMALL LETTER A} is a named character */
2202 char* e = strchr(s, '}');
2209 yyerror("Missing right brace on \\N{}");
2213 if (e > s + 2 && s[1] == 'U' && s[2] == '+') {
2215 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
2216 PERL_SCAN_DISALLOW_PREFIX;
2219 uv = grok_hex(s, &len, &flags, NULL);
2220 if ( e > s && len != (STRLEN)(e - s) ) {
2224 goto NUM_ESCAPE_INSERT;
2226 res = newSVpvn(s + 1, e - s - 1);
2227 type = newSVpvn(s - 2,e - s + 3);
2228 res = new_constant( NULL, 0, "charnames",
2229 res, NULL, SvPVX(type) );
2232 sv_utf8_upgrade(res);
2233 str = SvPV_const(res,len);
2234 #ifdef EBCDIC_NEVER_MIND
2235 /* charnames uses pack U and that has been
2236 * recently changed to do the below uni->native
2237 * mapping, so this would be redundant (and wrong,
2238 * the code point would be doubly converted).
2239 * But leave this in just in case the pack U change
2240 * gets revoked, but the semantics is still
2241 * desireable for charnames. --jhi */
2243 UV uv = utf8_to_uvchr((const U8*)str, 0);
2246 U8 tmpbuf[UTF8_MAXBYTES+1], *d;
2248 d = uvchr_to_utf8(tmpbuf, UNI_TO_NATIVE(uv));
2249 sv_setpvn(res, (char *)tmpbuf, d - tmpbuf);
2250 str = SvPV_const(res, len);
2254 if (!has_utf8 && SvUTF8(res)) {
2255 const char * const ostart = SvPVX_const(sv);
2256 SvCUR_set(sv, d - ostart);
2259 sv_utf8_upgrade(sv);
2260 /* this just broke our allocation above... */
2261 SvGROW(sv, (STRLEN)(send - start));
2262 d = SvPVX(sv) + SvCUR(sv);
2265 if (len > (STRLEN)(e - s + 4)) { /* I _guess_ 4 is \N{} --jhi */
2266 const char * const odest = SvPVX_const(sv);
2268 SvGROW(sv, (SvLEN(sv) + len - (e - s + 4)));
2269 d = SvPVX(sv) + (d - odest);
2273 native_range = FALSE; /* \N{} is guessed to be Unicode */
2275 Copy(str, d, len, char);
2282 yyerror("Missing braces on \\N{}");
2285 /* \c is a control character */
2294 *d++ = NATIVE_TO_NEED(has_utf8,toCTRL(c));
2297 yyerror("Missing control char name in \\c");
2301 /* printf-style backslashes, formfeeds, newlines, etc */
2303 *d++ = NATIVE_TO_NEED(has_utf8,'\b');
2306 *d++ = NATIVE_TO_NEED(has_utf8,'\n');
2309 *d++ = NATIVE_TO_NEED(has_utf8,'\r');
2312 *d++ = NATIVE_TO_NEED(has_utf8,'\f');
2315 *d++ = NATIVE_TO_NEED(has_utf8,'\t');
2318 *d++ = ASCII_TO_NEED(has_utf8,'\033');
2321 *d++ = ASCII_TO_NEED(has_utf8,'\007');
2327 } /* end if (backslash) */
2334 /* If we started with encoded form, or already know we want it
2335 and then encode the next character */
2336 if ((has_utf8 || this_utf8) && !NATIVE_IS_INVARIANT((U8)(*s))) {
2338 const UV nextuv = (this_utf8) ? utf8n_to_uvchr((U8*)s, send - s, &len, 0) : (UV) ((U8) *s);
2339 const STRLEN need = UNISKIP(NATIVE_TO_UNI(nextuv));
2342 /* encoded value larger than old, need extra space (NOTE: SvCUR() not set here) */
2343 const STRLEN off = d - SvPVX_const(sv);
2344 d = SvGROW(sv, SvLEN(sv) + (need-len)) + off;
2346 d = (char*)uvchr_to_utf8((U8*)d, nextuv);
2349 if (uv > 255 && !dorange)
2350 native_range = FALSE;
2354 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
2356 } /* while loop to process each character */
2358 /* terminate the string and set up the sv */
2360 SvCUR_set(sv, d - SvPVX_const(sv));
2361 if (SvCUR(sv) >= SvLEN(sv))
2362 Perl_croak(aTHX_ "panic: constant overflowed allocated space");
2365 if (PL_encoding && !has_utf8) {
2366 sv_recode_to_utf8(sv, PL_encoding);
2372 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
2373 PL_sublex_info.sub_op->op_private |=
2374 (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
2378 /* shrink the sv if we allocated more than we used */
2379 if (SvCUR(sv) + 5 < SvLEN(sv)) {
2380 SvPV_shrink_to_cur(sv);
2383 /* return the substring (via yylval) only if we parsed anything */
2384 if (s > PL_bufptr) {
2385 if ( PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ) )
2386 sv = new_constant(start, s - start,
2387 (const char *)(PL_lex_inpat ? "qr" : "q"),
2390 (( PL_lex_inwhat == OP_TRANS
2392 : ( (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat)
2395 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
2402 * Returns TRUE if there's more to the expression (e.g., a subscript),
2405 * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
2407 * ->[ and ->{ return TRUE
2408 * { and [ outside a pattern are always subscripts, so return TRUE
2409 * if we're outside a pattern and it's not { or [, then return FALSE
2410 * if we're in a pattern and the first char is a {
2411 * {4,5} (any digits around the comma) returns FALSE
2412 * if we're in a pattern and the first char is a [
2414 * [SOMETHING] has a funky algorithm to decide whether it's a
2415 * character class or not. It has to deal with things like
2416 * /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
2417 * anything else returns TRUE
2420 /* This is the one truly awful dwimmer necessary to conflate C and sed. */
2423 S_intuit_more(pTHX_ register char *s)
2426 if (PL_lex_brackets)
2428 if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
2430 if (*s != '{' && *s != '[')
2435 /* In a pattern, so maybe we have {n,m}. */
2452 /* On the other hand, maybe we have a character class */
2455 if (*s == ']' || *s == '^')
2458 /* this is terrifying, and it works */
2459 int weight = 2; /* let's weigh the evidence */
2461 unsigned char un_char = 255, last_un_char;
2462 const char * const send = strchr(s,']');
2463 char tmpbuf[sizeof PL_tokenbuf * 4];
2465 if (!send) /* has to be an expression */
2468 Zero(seen,256,char);
2471 else if (isDIGIT(*s)) {
2473 if (isDIGIT(s[1]) && s[2] == ']')
2479 for (; s < send; s++) {
2480 last_un_char = un_char;
2481 un_char = (unsigned char)*s;
2486 weight -= seen[un_char] * 10;
2487 if (isALNUM_lazy_if(s+1,UTF)) {
2489 scan_ident(s, send, tmpbuf, sizeof tmpbuf, FALSE);
2490 len = (int)strlen(tmpbuf);
2491 if (len > 1 && gv_fetchpvn_flags(tmpbuf, len, 0, SVt_PV))
2496 else if (*s == '$' && s[1] &&
2497 strchr("[#!%*<>()-=",s[1])) {
2498 if (/*{*/ strchr("])} =",s[2]))
2507 if (strchr("wds]",s[1]))
2509 else if (seen[(U8)'\''] || seen[(U8)'"'])
2511 else if (strchr("rnftbxcav",s[1]))
2513 else if (isDIGIT(s[1])) {
2515 while (s[1] && isDIGIT(s[1]))
2525 if (strchr("aA01! ",last_un_char))
2527 if (strchr("zZ79~",s[1]))
2529 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
2530 weight -= 5; /* cope with negative subscript */
2533 if (!isALNUM(last_un_char)
2534 && !(last_un_char == '$' || last_un_char == '@'
2535 || last_un_char == '&')
2536 && isALPHA(*s) && s[1] && isALPHA(s[1])) {
2541 if (keyword(tmpbuf, d - tmpbuf, 0))
2544 if (un_char == last_un_char + 1)
2546 weight -= seen[un_char];
2551 if (weight >= 0) /* probably a character class */
2561 * Does all the checking to disambiguate
2563 * between foo(bar) and bar->foo. Returns 0 if not a method, otherwise
2564 * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
2566 * First argument is the stuff after the first token, e.g. "bar".
2568 * Not a method if bar is a filehandle.
2569 * Not a method if foo is a subroutine prototyped to take a filehandle.
2570 * Not a method if it's really "Foo $bar"
2571 * Method if it's "foo $bar"
2572 * Not a method if it's really "print foo $bar"
2573 * Method if it's really "foo package::" (interpreted as package->foo)
2574 * Not a method if bar is known to be a subroutine ("sub bar; foo bar")
2575 * Not a method if bar is a filehandle or package, but is quoted with
2580 S_intuit_method(pTHX_ char *start, GV *gv, CV *cv)
2583 char *s = start + (*start == '$');
2584 char tmpbuf[sizeof PL_tokenbuf];
2592 if (SvTYPE(gv) == SVt_PVGV && GvIO(gv))
2596 const char *proto = SvPVX_const(cv);
2607 s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
2608 /* start is the beginning of the possible filehandle/object,
2609 * and s is the end of it
2610 * tmpbuf is a copy of it
2613 if (*start == '$') {
2614 if (gv || PL_last_lop_op == OP_PRINT || isUPPER(*PL_tokenbuf))
2617 len = start - SvPVX(PL_linestr);
2621 start = SvPVX(PL_linestr) + len;
2625 return *s == '(' ? FUNCMETH : METHOD;
2627 if (!keyword(tmpbuf, len, 0)) {
2628 if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
2632 soff = s - SvPVX(PL_linestr);
2636 indirgv = gv_fetchpvn_flags(tmpbuf, len, 0, SVt_PVCV);
2637 if (indirgv && GvCVu(indirgv))
2639 /* filehandle or package name makes it a method */
2640 if (!gv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, FALSE)) {
2642 soff = s - SvPVX(PL_linestr);
2645 if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
2646 return 0; /* no assumptions -- "=>" quotes bearword */
2648 start_force(PL_curforce);
2649 NEXTVAL_NEXTTOKE.opval = (OP*)newSVOP(OP_CONST, 0,
2650 newSVpvn(tmpbuf,len));
2651 NEXTVAL_NEXTTOKE.opval->op_private = OPpCONST_BARE;
2653 curmad('X', newSVpvn(start,SvPVX(PL_linestr) + soff - start));
2658 PL_bufptr = SvPVX(PL_linestr) + soff; /* restart before space */
2660 return *s == '(' ? FUNCMETH : METHOD;
2668 * Return a string of Perl code to load the debugger. If PERL5DB
2669 * is set, it will return the contents of that, otherwise a
2670 * compile-time require of perl5db.pl.
2678 const char * const pdb = PerlEnv_getenv("PERL5DB");
2682 SETERRNO(0,SS_NORMAL);
2683 return "BEGIN { require 'perl5db.pl' }";
2689 /* Encoded script support. filter_add() effectively inserts a
2690 * 'pre-processing' function into the current source input stream.
2691 * Note that the filter function only applies to the current source file
2692 * (e.g., it will not affect files 'require'd or 'use'd by this one).
2694 * The datasv parameter (which may be NULL) can be used to pass
2695 * private data to this instance of the filter. The filter function
2696 * can recover the SV using the FILTER_DATA macro and use it to
2697 * store private buffers and state information.
2699 * The supplied datasv parameter is upgraded to a PVIO type
2700 * and the IoDIRP/IoANY field is used to store the function pointer,
2701 * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
2702 * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
2703 * private use must be set using malloc'd pointers.
2707 Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
2713 if (!PL_rsfp_filters)
2714 PL_rsfp_filters = newAV();
2717 SvUPGRADE(datasv, SVt_PVIO);
2718 IoANY(datasv) = FPTR2DPTR(void *, funcp); /* stash funcp into spare field */
2719 IoFLAGS(datasv) |= IOf_FAKE_DIRP;
2720 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
2721 FPTR2DPTR(void *, IoANY(datasv)),
2722 SvPV_nolen(datasv)));
2723 av_unshift(PL_rsfp_filters, 1);
2724 av_store(PL_rsfp_filters, 0, datasv) ;
2729 /* Delete most recently added instance of this filter function. */
2731 Perl_filter_del(pTHX_ filter_t funcp)
2737 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p",
2738 FPTR2DPTR(void*, funcp)));
2740 if (!PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
2742 /* if filter is on top of stack (usual case) just pop it off */
2743 datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
2744 if (IoANY(datasv) == FPTR2DPTR(void *, funcp)) {
2745 IoFLAGS(datasv) &= ~IOf_FAKE_DIRP;
2746 IoANY(datasv) = (void *)NULL;
2747 sv_free(av_pop(PL_rsfp_filters));
2751 /* we need to search for the correct entry and clear it */
2752 Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
2756 /* Invoke the idxth filter function for the current rsfp. */
2757 /* maxlen 0 = read one text line */
2759 Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
2764 /* This API is bad. It should have been using unsigned int for maxlen.
2765 Not sure if we want to change the API, but if not we should sanity
2766 check the value here. */
2767 const unsigned int correct_length
2776 if (!PL_rsfp_filters)
2778 if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */
2779 /* Provide a default input filter to make life easy. */
2780 /* Note that we append to the line. This is handy. */
2781 DEBUG_P(PerlIO_printf(Perl_debug_log,
2782 "filter_read %d: from rsfp\n", idx));
2783 if (correct_length) {
2786 const int old_len = SvCUR(buf_sv);
2788 /* ensure buf_sv is large enough */
2789 SvGROW(buf_sv, (STRLEN)(old_len + correct_length)) ;
2790 if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len,
2791 correct_length)) <= 0) {
2792 if (PerlIO_error(PL_rsfp))
2793 return -1; /* error */
2795 return 0 ; /* end of file */
2797 SvCUR_set(buf_sv, old_len + len) ;
2800 if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
2801 if (PerlIO_error(PL_rsfp))
2802 return -1; /* error */
2804 return 0 ; /* end of file */
2807 return SvCUR(buf_sv);
2809 /* Skip this filter slot if filter has been deleted */
2810 if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) {
2811 DEBUG_P(PerlIO_printf(Perl_debug_log,
2812 "filter_read %d: skipped (filter deleted)\n",
2814 return FILTER_READ(idx+1, buf_sv, correct_length); /* recurse */
2816 /* Get function pointer hidden within datasv */
2817 funcp = DPTR2FPTR(filter_t, IoANY(datasv));
2818 DEBUG_P(PerlIO_printf(Perl_debug_log,
2819 "filter_read %d: via function %p (%s)\n",
2820 idx, (void*)datasv, SvPV_nolen_const(datasv)));
2821 /* Call function. The function is expected to */
2822 /* call "FILTER_READ(idx+1, buf_sv)" first. */
2823 /* Return: <0:error, =0:eof, >0:not eof */
2824 return (*funcp)(aTHX_ idx, buf_sv, correct_length);
2828 S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append)
2831 #ifdef PERL_CR_FILTER
2832 if (!PL_rsfp_filters) {
2833 filter_add(S_cr_textfilter,NULL);
2836 if (PL_rsfp_filters) {
2838 SvCUR_set(sv, 0); /* start with empty line */
2839 if (FILTER_READ(0, sv, 0) > 0)
2840 return ( SvPVX(sv) ) ;
2845 return (sv_gets(sv, fp, append));
2849 S_find_in_my_stash(pTHX_ const char *pkgname, I32 len)
2854 if (len == 11 && *pkgname == '_' && strEQ(pkgname, "__PACKAGE__"))
2858 (pkgname[len - 2] == ':' && pkgname[len - 1] == ':') &&
2859 (gv = gv_fetchpvn_flags(pkgname, len, 0, SVt_PVHV)))
2861 return GvHV(gv); /* Foo:: */
2864 /* use constant CLASS => 'MyClass' */
2865 gv = gv_fetchpvn_flags(pkgname, len, 0, SVt_PVCV);
2866 if (gv && GvCV(gv)) {
2867 SV * const sv = cv_const_sv(GvCV(gv));
2869 pkgname = SvPV_nolen_const(sv);
2872 return gv_stashpv(pkgname, FALSE);
2876 * S_readpipe_override
2877 * Check whether readpipe() is overriden, and generates the appropriate
2878 * optree, provided sublex_start() is called afterwards.
2881 S_readpipe_override(pTHX)
2884 GV *gv_readpipe = gv_fetchpvs("readpipe", GV_NOTQUAL, SVt_PVCV);
2885 yylval.ival = OP_BACKTICK;
2887 && GvCVu(gv_readpipe) && GvIMPORTED_CV(gv_readpipe))
2889 ((gvp = (GV**)hv_fetchs(PL_globalstash, "readpipe", FALSE))
2890 && (gv_readpipe = *gvp) != (GV*)&PL_sv_undef
2891 && GvCVu(gv_readpipe) && GvIMPORTED_CV(gv_readpipe)))
2893 PL_lex_op = (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
2894 append_elem(OP_LIST,
2895 newSVOP(OP_CONST, 0, &PL_sv_undef), /* value will be read later */
2896 newCVREF(0, newGVOP(OP_GV, 0, gv_readpipe))));
2906 * The intent of this yylex wrapper is to minimize the changes to the
2907 * tokener when we aren't interested in collecting madprops. It remains
2908 * to be seen how successful this strategy will be...
2915 char *s = PL_bufptr;
2917 /* make sure PL_thiswhite is initialized */
2921 /* just do what yylex would do on pending identifier; leave PL_thiswhite alone */
2922 if (PL_pending_ident)
2923 return S_pending_ident(aTHX);
2925 /* previous token ate up our whitespace? */
2926 if (!PL_lasttoke && PL_nextwhite) {
2927 PL_thiswhite = PL_nextwhite;
2931 /* isolate the token, and figure out where it is without whitespace */
2932 PL_realtokenstart = -1;
2936 assert(PL_curforce < 0);
2938 if (!PL_thismad || PL_thismad->mad_key == '^') { /* not forced already? */
2939 if (!PL_thistoken) {
2940 if (PL_realtokenstart < 0 || !CopLINE(PL_curcop))
2941 PL_thistoken = newSVpvs("");
2943 char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
2944 PL_thistoken = newSVpvn(tstart, s - tstart);
2947 if (PL_thismad) /* install head */
2948 CURMAD('X', PL_thistoken);
2951 /* last whitespace of a sublex? */
2952 if (optype == ')' && PL_endwhite) {
2953 CURMAD('X', PL_endwhite);
2958 /* if no whitespace and we're at EOF, bail. Otherwise fake EOF below. */
2959 if (!PL_thiswhite && !PL_endwhite && !optype) {
2960 sv_free(PL_thistoken);
2965 /* put off final whitespace till peg */
2966 if (optype == ';' && !PL_rsfp) {
2967 PL_nextwhite = PL_thiswhite;
2970 else if (PL_thisopen) {
2971 CURMAD('q', PL_thisopen);
2973 sv_free(PL_thistoken);
2977 /* Store actual token text as madprop X */
2978 CURMAD('X', PL_thistoken);
2982 /* add preceding whitespace as madprop _ */
2983 CURMAD('_', PL_thiswhite);
2987 /* add quoted material as madprop = */
2988 CURMAD('=', PL_thisstuff);
2992 /* add terminating quote as madprop Q */
2993 CURMAD('Q', PL_thisclose);
2997 /* special processing based on optype */
3001 /* opval doesn't need a TOKEN since it can already store mp */
3012 append_madprops(PL_thismad, yylval.opval, 0);
3020 addmad(newMADsv('p', PL_endwhite), &PL_thismad, 0);
3029 /* remember any fake bracket that lexer is about to discard */
3030 if (PL_lex_brackets == 1 &&
3031 ((expectation)PL_lex_brackstack[0] & XFAKEBRACK))
3034 while (s < PL_bufend && (*s == ' ' || *s == '\t'))
3037 PL_thiswhite = newSVpvn(PL_bufptr, ++s - PL_bufptr);
3038 addmad(newMADsv('#', PL_thiswhite), &PL_thismad, 0);
3041 break; /* don't bother looking for trailing comment */
3050 /* attach a trailing comment to its statement instead of next token */
3054 if (PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == optype) {
3056 while (s < PL_bufend && (*s == ' ' || *s == '\t'))
3058 if (*s == '\n' || *s == '#') {
3059 while (s < PL_bufend && *s != '\n')
3063 PL_thiswhite = newSVpvn(PL_bufptr, s - PL_bufptr);
3064 addmad(newMADsv('#', PL_thiswhite), &PL_thismad, 0);
3081 /* Create new token struct. Note: opvals return early above. */
3082 yylval.tkval = newTOKEN(optype, yylval, PL_thismad);
3089 S_tokenize_use(pTHX_ int is_use, char *s) {
3091 if (PL_expect != XSTATE)
3092 yyerror(Perl_form(aTHX_ "\"%s\" not allowed in expression",
3093 is_use ? "use" : "no"));
3095 if (isDIGIT(*s) || (*s == 'v' && isDIGIT(s[1]))) {
3096 s = force_version(s, TRUE);
3097 if (*s == ';' || (s = SKIPSPACE1(s), *s == ';')) {
3098 start_force(PL_curforce);
3099 NEXTVAL_NEXTTOKE.opval = NULL;
3102 else if (*s == 'v') {
3103 s = force_word(s,WORD,FALSE,TRUE,FALSE);
3104 s = force_version(s, FALSE);
3108 s = force_word(s,WORD,FALSE,TRUE,FALSE);
3109 s = force_version(s, FALSE);
3111 yylval.ival = is_use;
3115 static const char* const exp_name[] =
3116 { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
3117 "ATTRTERM", "TERMBLOCK", "TERMORDORDOR"
3124 Works out what to call the token just pulled out of the input
3125 stream. The yacc parser takes care of taking the ops we return and
3126 stitching them into a tree.
3132 if read an identifier
3133 if we're in a my declaration
3134 croak if they tried to say my($foo::bar)
3135 build the ops for a my() declaration
3136 if it's an access to a my() variable
3137 are we in a sort block?
3138 croak if my($a); $a <=> $b
3139 build ops for access to a my() variable
3140 if in a dq string, and they've said @foo and we can't find @foo
3142 build ops for a bareword
3143 if we already built the token before, use it.
3148 #pragma segment Perl_yylex
3154 register char *s = PL_bufptr;
3159 /* orig_keyword, gvp, and gv are initialized here because
3160 * jump to the label just_a_word_zero can bypass their
3161 * initialization later. */
3162 I32 orig_keyword = 0;
3167 SV* tmp = newSVpvs("");
3168 PerlIO_printf(Perl_debug_log, "### %"IVdf":LEX_%s/X%s %s\n",
3169 (IV)CopLINE(PL_curcop),
3170 lex_state_names[PL_lex_state],
3171 exp_name[PL_expect],
3172 pv_display(tmp, s, strlen(s), 0, 60));
3175 /* check if there's an identifier for us to look at */
3176 if (PL_pending_ident)
3177 return REPORT(S_pending_ident(aTHX));
3179 /* no identifier pending identification */
3181 switch (PL_lex_state) {
3183 case LEX_NORMAL: /* Some compilers will produce faster */
3184 case LEX_INTERPNORMAL: /* code if we comment these out. */
3188 /* when we've already built the next token, just pull it out of the queue */
3192 yylval = PL_nexttoke[PL_lasttoke].next_val;
3194 PL_thismad = PL_nexttoke[PL_lasttoke].next_mad;
3195 PL_nexttoke[PL_lasttoke].next_mad = 0;
3196 if (PL_thismad && PL_thismad->mad_key == '_') {
3197 PL_thiswhite = (SV*)PL_thismad->mad_val;
3198 PL_thismad->mad_val = 0;
3199 mad_free(PL_thismad);
3204 PL_lex_state = PL_lex_defer;
3205 PL_expect = PL_lex_expect;
3206 PL_lex_defer = LEX_NORMAL;
3207 if (!PL_nexttoke[PL_lasttoke].next_type)
3212 yylval = PL_nextval[PL_nexttoke];
3214 PL_lex_state = PL_lex_defer;
3215 PL_expect = PL_lex_expect;
3216 PL_lex_defer = LEX_NORMAL;
3220 /* FIXME - can these be merged? */
3221 return(PL_nexttoke[PL_lasttoke].next_type);
3223 return REPORT(PL_nexttype[PL_nexttoke]);
3226 /* interpolated case modifiers like \L \U, including \Q and \E.
3227 when we get here, PL_bufptr is at the \
3229 case LEX_INTERPCASEMOD:
3231 if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
3232 Perl_croak(aTHX_ "panic: INTERPCASEMOD");
3234 /* handle \E or end of string */
3235 if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
3237 if (PL_lex_casemods) {
3238 const char oldmod = PL_lex_casestack[--PL_lex_casemods];
3239 PL_lex_casestack[PL_lex_casemods] = '\0';
3241 if (PL_bufptr != PL_bufend
3242 && (oldmod == 'L' || oldmod == 'U' || oldmod == 'Q')) {
3244 PL_lex_state = LEX_INTERPCONCAT;
3247 PL_thistoken = newSVpvs("\\E");
3253 while (PL_bufptr != PL_bufend &&
3254 PL_bufptr[0] == '\\' && PL_bufptr[1] == 'E') {
3256 PL_thiswhite = newSVpvs("");
3257 sv_catpvn(PL_thiswhite, PL_bufptr, 2);
3261 if (PL_bufptr != PL_bufend)
3264 PL_lex_state = LEX_INTERPCONCAT;
3268 DEBUG_T({ PerlIO_printf(Perl_debug_log,
3269 "### Saw case modifier\n"); });
3271 if (s[1] == '\\' && s[2] == 'E') {
3274 PL_thiswhite = newSVpvs("");
3275 sv_catpvn(PL_thiswhite, PL_bufptr, 4);
3278 PL_lex_state = LEX_INTERPCONCAT;
3283 if (!PL_madskills) /* when just compiling don't need correct */
3284 if (strnEQ(s, "L\\u", 3) || strnEQ(s, "U\\l", 3))
3285 tmp = *s, *s = s[2], s[2] = (char)tmp; /* misordered... */
3286 if ((*s == 'L' || *s == 'U') &&
3287 (strchr(PL_lex_casestack, 'L') || strchr(PL_lex_casestack, 'U'))) {
3288 PL_lex_casestack[--PL_lex_casemods] = '\0';
3291 if (PL_lex_casemods > 10)
3292 Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
3293 PL_lex_casestack[PL_lex_casemods++] = *s;
3294 PL_lex_casestack[PL_lex_casemods] = '\0';
3295 PL_lex_state = LEX_INTERPCONCAT;
3296 start_force(PL_curforce);
3297 NEXTVAL_NEXTTOKE.ival = 0;
3299 start_force(PL_curforce);
3301 NEXTVAL_NEXTTOKE.ival = OP_LCFIRST;
3303 NEXTVAL_NEXTTOKE.ival = OP_UCFIRST;
3305 NEXTVAL_NEXTTOKE.ival = OP_LC;
3307 NEXTVAL_NEXTTOKE.ival = OP_UC;
3309 NEXTVAL_NEXTTOKE.ival = OP_QUOTEMETA;
3311 Perl_croak(aTHX_ "panic: yylex");
3313 SV* const tmpsv = newSVpvs("");
3314 Perl_sv_catpvf(aTHX_ tmpsv, "\\%c", *s);
3320 if (PL_lex_starts) {
3326 sv_free(PL_thistoken);
3327 PL_thistoken = newSVpvs("");
3330 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
3331 if (PL_lex_casemods == 1 && PL_lex_inpat)
3340 case LEX_INTERPPUSH:
3341 return REPORT(sublex_push());
3343 case LEX_INTERPSTART:
3344 if (PL_bufptr == PL_bufend)
3345 return REPORT(sublex_done());
3346 DEBUG_T({ PerlIO_printf(Perl_debug_log,
3347 "### Interpolated variable\n"); });
3349 PL_lex_dojoin = (*PL_bufptr == '@');
3350 PL_lex_state = LEX_INTERPNORMAL;
3351 if (PL_lex_dojoin) {
3352 start_force(PL_curforce);
3353 NEXTVAL_NEXTTOKE.ival = 0;
3355 start_force(PL_curforce);
3356 force_ident("\"", '$');
3357 start_force(PL_curforce);
3358 NEXTVAL_NEXTTOKE.ival = 0;
3360 start_force(PL_curforce);
3361 NEXTVAL_NEXTTOKE.ival = 0;
3363 start_force(PL_curforce);
3364 NEXTVAL_NEXTTOKE.ival = OP_JOIN; /* emulate join($", ...) */
3367 if (PL_lex_starts++) {
3372 sv_free(PL_thistoken);
3373 PL_thistoken = newSVpvs("");
3376 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
3377 if (!PL_lex_casemods && PL_lex_inpat)
3384 case LEX_INTERPENDMAYBE:
3385 if (intuit_more(PL_bufptr)) {
3386 PL_lex_state = LEX_INTERPNORMAL; /* false alarm, more expr */
3392 if (PL_lex_dojoin) {
3393 PL_lex_dojoin = FALSE;
3394 PL_lex_state = LEX_INTERPCONCAT;
3398 sv_free(PL_thistoken);
3399 PL_thistoken = newSVpvs("");
3404 if (PL_lex_inwhat == OP_SUBST && PL_linestr == PL_lex_repl
3405 && SvEVALED(PL_lex_repl))
3407 if (PL_bufptr != PL_bufend)
3408 Perl_croak(aTHX_ "Bad evalled substitution pattern");
3412 case LEX_INTERPCONCAT:
3414 if (PL_lex_brackets)
3415 Perl_croak(aTHX_ "panic: INTERPCONCAT");
3417 if (PL_bufptr == PL_bufend)
3418 return REPORT(sublex_done());
3420 if (SvIVX(PL_linestr) == '\'') {
3421 SV *sv = newSVsv(PL_linestr);
3424 else if ( PL_hints & HINT_NEW_RE )
3425 sv = new_constant(NULL, 0, "qr", sv, sv, "q");
3426 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
3430 s = scan_const(PL_bufptr);
3432 PL_lex_state = LEX_INTERPCASEMOD;
3434 PL_lex_state = LEX_INTERPSTART;
3437 if (s != PL_bufptr) {
3438 start_force(PL_curforce);
3440 curmad('X', newSVpvn(PL_bufptr,s-PL_bufptr));
3442 NEXTVAL_NEXTTOKE = yylval;
3445 if (PL_lex_starts++) {
3449 sv_free(PL_thistoken);
3450 PL_thistoken = newSVpvs("");
3453 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
3454 if (!PL_lex_casemods && PL_lex_inpat)
3467 PL_lex_state = LEX_NORMAL;
3468 s = scan_formline(PL_bufptr);
3469 if (!PL_lex_formbrack)
3475 PL_oldoldbufptr = PL_oldbufptr;
3481 sv_free(PL_thistoken);
3484 PL_realtokenstart = s - SvPVX(PL_linestr); /* assume but undo on ws */
3488 if (isIDFIRST_lazy_if(s,UTF))
3490 Perl_croak(aTHX_ "Unrecognized character \\x%02X", *s & 255);
3493 goto fake_eof; /* emulate EOF on ^D or ^Z */
3502 if (PL_lex_brackets) {
3503 yyerror((const char *)
3505 ? "Format not terminated"
3506 : "Missing right curly or square bracket"));
3508 DEBUG_T( { PerlIO_printf(Perl_debug_log,
3509 "### Tokener got EOF\n");
3513 if (s++ < PL_bufend)
3514 goto retry; /* ignore stray nulls */
3517 if (!PL_in_eval && !PL_preambled) {
3518 PL_preambled = TRUE;
3523 sv_setpv(PL_linestr,incl_perldb());
3524 if (SvCUR(PL_linestr))
3525 sv_catpvs(PL_linestr,";");
3527 while(AvFILLp(PL_preambleav) >= 0) {
3528 SV *tmpsv = av_shift(PL_preambleav);
3529 sv_catsv(PL_linestr, tmpsv);
3530 sv_catpvs(PL_linestr, ";");
3533 sv_free((SV*)PL_preambleav);
3534 PL_preambleav = NULL;
3536 if (PL_minus_n || PL_minus_p) {
3537 sv_catpvs(PL_linestr, "LINE: while (<>) {");
3539 sv_catpvs(PL_linestr,"chomp;");
3542 if ((*PL_splitstr == '/' || *PL_splitstr == '\''
3543 || *PL_splitstr == '"')
3544 && strchr(PL_splitstr + 1, *PL_splitstr))
3545 Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
3547 /* "q\0${splitstr}\0" is legal perl. Yes, even NUL
3548 bytes can be used as quoting characters. :-) */
3549 const char *splits = PL_splitstr;
3550 sv_catpvs(PL_linestr, "our @F=split(q\0");
3553 if (*splits == '\\')
3554 sv_catpvn(PL_linestr, splits, 1);
3555 sv_catpvn(PL_linestr, splits, 1);
3556 } while (*splits++);
3557 /* This loop will embed the trailing NUL of
3558 PL_linestr as the last thing it does before
3560 sv_catpvs(PL_linestr, ");");
3564 sv_catpvs(PL_linestr,"our @F=split(' ');");
3568 sv_catpvs(PL_linestr,"use feature ':5.10';");
3569 sv_catpvs(PL_linestr, "\n");
3570 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
3571 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3572 PL_last_lop = PL_last_uni = NULL;
3573 if (PERLDB_LINE && PL_curstash != PL_debstash)
3574 update_debugger_info_sv(PL_linestr);
3578 bof = PL_rsfp ? TRUE : FALSE;
3579 if ((s = filter_gets(PL_linestr, PL_rsfp, 0)) == NULL) {
3582 PL_realtokenstart = -1;
3585 if (PL_preprocess && !PL_in_eval)
3586 (void)PerlProc_pclose(PL_rsfp);
3587 else if ((PerlIO *)PL_rsfp == PerlIO_stdin())
3588 PerlIO_clearerr(PL_rsfp);
3590 (void)PerlIO_close(PL_rsfp);
3592 PL_doextract = FALSE;
3594 if (!PL_in_eval && (PL_minus_n || PL_minus_p)) {
3599 sv_setpv(PL_linestr,
3602 ? ";}continue{print;}" : ";}"));
3603 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
3604 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3605 PL_last_lop = PL_last_uni = NULL;
3606 PL_minus_n = PL_minus_p = 0;
3609 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
3610 PL_last_lop = PL_last_uni = NULL;
3611 sv_setpvn(PL_linestr,"",0);
3612 TOKEN(';'); /* not infinite loop because rsfp is NULL now */
3614 /* If it looks like the start of a BOM or raw UTF-16,
3615 * check if it in fact is. */
3621 #ifdef PERLIO_IS_STDIO
3622 # ifdef __GNU_LIBRARY__
3623 # if __GNU_LIBRARY__ == 1 /* Linux glibc5 */
3624 # define FTELL_FOR_PIPE_IS_BROKEN
3628 # if __GLIBC__ == 1 /* maybe some glibc5 release had it like this? */
3629 # define FTELL_FOR_PIPE_IS_BROKEN
3634 #ifdef FTELL_FOR_PIPE_IS_BROKEN
3635 /* This loses the possibility to detect the bof
3636 * situation on perl -P when the libc5 is being used.
3637 * Workaround? Maybe attach some extra state to PL_rsfp?
3640 bof = PerlIO_tell(PL_rsfp) == SvCUR(PL_linestr);
3642 bof = PerlIO_tell(PL_rsfp) == (Off_t)SvCUR(PL_linestr);
3645 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3646 s = swallow_bom((U8*)s);
3650 /* Incest with pod. */
3653 sv_catsv(PL_thiswhite, PL_linestr);
3655 if (*s == '=' && strnEQ(s, "=cut", 4)) {
3656 sv_setpvn(PL_linestr, "", 0);
3657 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
3658 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3659 PL_last_lop = PL_last_uni = NULL;
3660 PL_doextract = FALSE;
3664 } while (PL_doextract);
3665 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = s;
3666 if (PERLDB_LINE && PL_curstash != PL_debstash)
3667 update_debugger_info_sv(PL_linestr);
3668 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3669 PL_last_lop = PL_last_uni = NULL;
3670 if (CopLINE(PL_curcop) == 1) {
3671 while (s < PL_bufend && isSPACE(*s))
3673 if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
3677 PL_thiswhite = newSVpvn(PL_linestart, s - PL_linestart);
3681 if (*s == '#' && *(s+1) == '!')
3683 #ifdef ALTERNATE_SHEBANG
3685 static char const as[] = ALTERNATE_SHEBANG;
3686 if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
3687 d = s + (sizeof(as) - 1);
3689 #endif /* ALTERNATE_SHEBANG */
3698 while (*d && !isSPACE(*d))
3702 #ifdef ARG_ZERO_IS_SCRIPT
3703 if (ipathend > ipath) {
3705 * HP-UX (at least) sets argv[0] to the script name,
3706 * which makes $^X incorrect. And Digital UNIX and Linux,
3707 * at least, set argv[0] to the basename of the Perl
3708 * interpreter. So, having found "#!", we'll set it right.
3710 SV * const x = GvSV(gv_fetchpvs("\030", GV_ADD|GV_NOTQUAL,
3712 assert(SvPOK(x) || SvGMAGICAL(x));
3713 if (sv_eq(x, CopFILESV(PL_curcop))) {
3714 sv_setpvn(x, ipath, ipathend - ipath);
3720 const char *bstart = SvPV_const(CopFILESV(PL_curcop),blen);
3721 const char * const lstart = SvPV_const(x,llen);
3723 bstart += blen - llen;
3724 if (strnEQ(bstart, lstart, llen) && bstart[-1] == '/') {
3725 sv_setpvn(x, ipath, ipathend - ipath);
3730 TAINT_NOT; /* $^X is always tainted, but that's OK */
3732 #endif /* ARG_ZERO_IS_SCRIPT */
3737 d = instr(s,"perl -");
3739 d = instr(s,"perl");
3741 /* avoid getting into infinite loops when shebang
3742 * line contains "Perl" rather than "perl" */
3744 for (d = ipathend-4; d >= ipath; --d) {
3745 if ((*d == 'p' || *d == 'P')
3746 && !ibcmp(d, "perl", 4))
3756 #ifdef ALTERNATE_SHEBANG
3758 * If the ALTERNATE_SHEBANG on this system starts with a
3759 * character that can be part of a Perl expression, then if
3760 * we see it but not "perl", we're probably looking at the
3761 * start of Perl code, not a request to hand off to some
3762 * other interpreter. Similarly, if "perl" is there, but
3763 * not in the first 'word' of the line, we assume the line
3764 * contains the start of the Perl program.
3766 if (d && *s != '#') {
3767 const char *c = ipath;
3768 while (*c && !strchr("; \t\r\n\f\v#", *c))
3771 d = NULL; /* "perl" not in first word; ignore */
3773 *s = '#'; /* Don't try to parse shebang line */
3775 #endif /* ALTERNATE_SHEBANG */
3776 #ifndef MACOS_TRADITIONAL
3781 !instr(s,"indir") &&
3782 instr(PL_origargv[0],"perl"))
3789 while (s < PL_bufend && isSPACE(*s))
3791 if (s < PL_bufend) {
3792 Newxz(newargv,PL_origargc+3,char*);
3794 while (s < PL_bufend && !isSPACE(*s))
3797 Copy(PL_origargv+1, newargv+2, PL_origargc+1, char*);
3800 newargv = PL_origargv;
3803 PerlProc_execv(ipath, EXEC_ARGV_CAST(newargv));
3805 Perl_croak(aTHX_ "Can't exec %s", ipath);
3809 while (*d && !isSPACE(*d))
3811 while (SPACE_OR_TAB(*d))
3815 const bool switches_done = PL_doswitches;
3816 const U32 oldpdb = PL_perldb;
3817 const bool oldn = PL_minus_n;
3818 const bool oldp = PL_minus_p;
3821 if (*d == 'M' || *d == 'm' || *d == 'C') {
3822 const char * const m = d;
3823 while (*d && !isSPACE(*d))
3825 Perl_croak(aTHX_ "Too late for \"-%.*s\" option",
3828 d = moreswitches(d);
3830 if (PL_doswitches && !switches_done) {
3831 int argc = PL_origargc;
3832 char **argv = PL_origargv;
3835 } while (argc && argv[0][0] == '-' && argv[0][1]);
3836 init_argv_symbols(argc,argv);
3838 if ((PERLDB_LINE && !oldpdb) ||
3839 ((PL_minus_n || PL_minus_p) && !(oldn || oldp)))
3840 /* if we have already added "LINE: while (<>) {",
3841 we must not do it again */
3843 sv_setpvn(PL_linestr, "", 0);
3844 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
3845 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3846 PL_last_lop = PL_last_uni = NULL;
3847 PL_preambled = FALSE;
3849 (void)gv_fetchfile(PL_origfilename);
3856 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
3858 PL_lex_state = LEX_FORMLINE;
3863 #ifdef PERL_STRICT_CR
3864 Perl_warn(aTHX_ "Illegal character \\%03o (carriage return)", '\r');
3866 "\t(Maybe you didn't strip carriage returns after a network transfer?)\n");
3868 case ' ': case '\t': case '\f': case 013:
3869 #ifdef MACOS_TRADITIONAL
3873 PL_realtokenstart = -1;
3882 PL_realtokenstart = -1;
3886 if (PL_lex_state != LEX_NORMAL || (PL_in_eval && !PL_rsfp)) {
3887 if (*s == '#' && s == PL_linestart && PL_in_eval && !PL_rsfp) {
3888 /* handle eval qq[#line 1 "foo"\n ...] */
3889 CopLINE_dec(PL_curcop);
3892 if (PL_madskills && !PL_lex_formbrack && !PL_in_eval) {
3894 if (!PL_in_eval || PL_rsfp)
3899 while (d < PL_bufend && *d != '\n')
3903 else if (d > PL_bufend) /* Found by Ilya: feed random input to Perl. */
3904 Perl_croak(aTHX_ "panic: input overflow");
3907 PL_thiswhite = newSVpvn(s, d - s);
3912 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
3914 PL_lex_state = LEX_FORMLINE;
3920 if (PL_madskills && CopLINE(PL_curcop) >= 1 && !PL_lex_formbrack) {
3921 if (CopLINE(PL_curcop) == 1 && s[0] == '#' && s[1] == '!') {
3924 TOKEN(PEG); /* make sure any #! line is accessible */
3929 /* if (PL_madskills && PL_lex_formbrack) { */
3931 while (d < PL_bufend && *d != '\n')
3935 else if (d > PL_bufend) /* Found by Ilya: feed random input to Perl. */
3936 Perl_croak(aTHX_ "panic: input overflow");
3937 if (PL_madskills && CopLINE(PL_curcop) >= 1) {
3939 PL_thiswhite = newSVpvs("");
3940 if (CopLINE(PL_curcop) == 1) {
3941 sv_setpvn(PL_thiswhite, "", 0);
3944 sv_catpvn(PL_thiswhite, s, d - s);
3958 if (s[1] && isALPHA(s[1]) && !isALNUM(s[2])) {
3966 while (s < PL_bufend && SPACE_OR_TAB(*s))
3969 if (strnEQ(s,"=>",2)) {
3970 s = force_word(PL_bufptr,WORD,FALSE,FALSE,FALSE);
3971 DEBUG_T( { printbuf("### Saw unary minus before =>, forcing word %s\n", s); } );
3972 OPERATOR('-'); /* unary minus */
3974 PL_last_uni = PL_oldbufptr;
3976 case 'r': ftst = OP_FTEREAD; break;
3977 case 'w': ftst = OP_FTEWRITE; break;
3978 case 'x': ftst = OP_FTEEXEC; break;
3979 case 'o': ftst = OP_FTEOWNED; break;
3980 case 'R': ftst = OP_FTRREAD; break;
3981 case 'W': ftst = OP_FTRWRITE; break;
3982 case 'X': ftst = OP_FTREXEC; break;
3983 case 'O': ftst = OP_FTROWNED; break;
3984 case 'e': ftst = OP_FTIS; break;
3985 case 'z': ftst = OP_FTZERO; break;
3986 case 's': ftst = OP_FTSIZE; break;
3987 case 'f': ftst = OP_FTFILE; break;
3988 case 'd': ftst = OP_FTDIR; break;
3989 case 'l': ftst = OP_FTLINK; break;
3990 case 'p': ftst = OP_FTPIPE; break;
3991 case 'S': ftst = OP_FTSOCK; break;
3992 case 'u': ftst = OP_FTSUID; break;
3993 case 'g': ftst = OP_FTSGID; break;
3994 case 'k': ftst = OP_FTSVTX; break;
3995 case 'b': ftst = OP_FTBLK; break;
3996 case 'c': ftst = OP_FTCHR; break;
3997 case 't': ftst = OP_FTTTY; break;
3998 case 'T': ftst = OP_FTTEXT; break;
3999 case 'B': ftst = OP_FTBINARY; break;
4000 case 'M': case 'A': case 'C':
4001 gv_fetchpvs("\024", GV_ADD|GV_NOTQUAL, SVt_PV);
4003 case 'M': ftst = OP_FTMTIME; break;
4004 case 'A': ftst = OP_FTATIME; break;
4005 case 'C': ftst = OP_FTCTIME; break;
4013 PL_last_lop_op = (OPCODE)ftst;
4014 DEBUG_T( { PerlIO_printf(Perl_debug_log,
4015 "### Saw file test %c\n", (int)tmp);
4020 /* Assume it was a minus followed by a one-letter named
4021 * subroutine call (or a -bareword), then. */
4022 DEBUG_T( { PerlIO_printf(Perl_debug_log,
4023 "### '-%c' looked like a file test but was not\n",
4030 const char tmp = *s++;
4033 if (PL_expect == XOPERATOR)
4038 else if (*s == '>') {
4041 if (isIDFIRST_lazy_if(s,UTF)) {
4042 s = force_word(s,METHOD,FALSE,TRUE,FALSE);
4050 if (PL_expect == XOPERATOR)
4053 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
4055 OPERATOR('-'); /* unary minus */
4061 const char tmp = *s++;
4064 if (PL_expect == XOPERATOR)
4069 if (PL_expect == XOPERATOR)
4072 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
4079 if (PL_expect != XOPERATOR) {
4080 s = scan_ident(s, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
4081 PL_expect = XOPERATOR;
4082 force_ident(PL_tokenbuf, '*');
4095 if (PL_expect == XOPERATOR) {
4099 PL_tokenbuf[0] = '%';
4100 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, TRUE);
4101 if (!PL_tokenbuf[1]) {
4104 PL_pending_ident = '%';
4115 && (PL_expect == XOPERATOR || PL_expect == XTERMORDORDOR)
4116 && FEATURE_IS_ENABLED("~~"))
4123 const char tmp = *s++;
4129 goto just_a_word_zero_gv;
4132 switch (PL_expect) {
4138 if (!PL_in_my || PL_lex_state != LEX_NORMAL)
4140 PL_bufptr = s; /* update in case we back off */
4146 PL_expect = XTERMBLOCK;
4149 stuffstart = s - SvPVX(PL_linestr) - 1;
4153 while (isIDFIRST_lazy_if(s,UTF)) {
4156 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
4157 if (isLOWER(*s) && (tmp = keyword(PL_tokenbuf, len, 0))) {
4158 if (tmp < 0) tmp = -tmp;
4173 sv = newSVpvn(s, len);
4175 d = scan_str(d,TRUE,TRUE);
4177 /* MUST advance bufptr here to avoid bogus
4178 "at end of line" context messages from yyerror().
4180 PL_bufptr = s + len;
4181 yyerror("Unterminated attribute parameter in attribute list");
4185 return REPORT(0); /* EOF indicator */
4189 sv_catsv(sv, PL_lex_stuff);
4190 attrs = append_elem(OP_LIST, attrs,
4191 newSVOP(OP_CONST, 0, sv));
4192 SvREFCNT_dec(PL_lex_stuff);
4193 PL_lex_stuff = NULL;
4196 if (len == 6 && strnEQ(SvPVX(sv), "unique", len)) {
4198 if (PL_in_my == KEY_our) {
4200 GvUNIQUE_on(cGVOPx_gv(yylval.opval));
4202 /* skip to avoid loading attributes.pm */
4204 deprecate(":unique");
4207 Perl_croak(aTHX_ "The 'unique' attribute may only be applied to 'our' variables");
4210 /* NOTE: any CV attrs applied here need to be part of
4211 the CVf_BUILTIN_ATTRS define in cv.h! */
4212 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "lvalue", len)) {
4214 CvLVALUE_on(PL_compcv);
4216 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "locked", len)) {
4218 CvLOCKED_on(PL_compcv);
4220 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "method", len)) {
4222 CvMETHOD_on(PL_compcv);
4224 else if (!PL_in_my && len == 9 && strnEQ(SvPVX(sv), "assertion", len)) {
4226 CvASSERTION_on(PL_compcv);
4228 /* After we've set the flags, it could be argued that
4229 we don't need to do the attributes.pm-based setting
4230 process, and shouldn't bother appending recognized
4231 flags. To experiment with that, uncomment the
4232 following "else". (Note that's already been
4233 uncommented. That keeps the above-applied built-in
4234 attributes from being intercepted (and possibly
4235 rejected) by a package's attribute routines, but is
4236 justified by the performance win for the common case
4237 of applying only built-in attributes.) */
4239 attrs = append_elem(OP_LIST, attrs,
4240 newSVOP(OP_CONST, 0,
4244 if (*s == ':' && s[1] != ':')
4247 break; /* require real whitespace or :'s */
4248 /* XXX losing whitespace on sequential attributes here */
4252 = (PL_expect == XOPERATOR ? '=' : '{'); /*'}(' for vi */
4253 if (*s != ';' && *s != '}' && *s != tmp
4254 && (tmp != '=' || *s != ')')) {
4255 const char q = ((*s == '\'') ? '"' : '\'');
4256 /* If here for an expression, and parsed no attrs, back
4258 if (tmp == '=' && !attrs) {
4262 /* MUST advance bufptr here to avoid bogus "at end of line"
4263 context messages from yyerror().
4266 yyerror( (const char *)
4268 ? Perl_form(aTHX_ "Invalid separator character "
4269 "%c%c%c in attribute list", q, *s, q)
4270 : "Unterminated attribute list" ) );
4278 start_force(PL_curforce);
4279 NEXTVAL_NEXTTOKE.opval = attrs;
4280 CURMAD('_', PL_nextwhite);
4285 PL_thistoken = newSVpvn(SvPVX(PL_linestr) + stuffstart,
4286 (s - SvPVX(PL_linestr)) - stuffstart);
4294 if (PL_last_lop == PL_oldoldbufptr || PL_last_uni == PL_oldoldbufptr)
4295 PL_oldbufptr = PL_oldoldbufptr; /* allow print(STDOUT 123) */
4303 const char tmp = *s++;
4308 const char tmp = *s++;
4316 if (PL_lex_brackets <= 0)
4317 yyerror("Unmatched right square bracket");
4320 if (PL_lex_state == LEX_INTERPNORMAL) {
4321 if (PL_lex_brackets == 0) {
4322 if (*s != '[' && *s != '{' && (*s != '-' || s[1] != '>'))
4323 PL_lex_state = LEX_INTERPEND;
4330 if (PL_lex_brackets > 100) {
4331 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
4333 switch (PL_expect) {
4335 if (PL_lex_formbrack) {
4339 if (PL_oldoldbufptr == PL_last_lop)
4340 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
4342 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
4343 OPERATOR(HASHBRACK);
4345 while (s < PL_bufend && SPACE_OR_TAB(*s))
4348 PL_tokenbuf[0] = '\0';
4349 if (d < PL_bufend && *d == '-') {
4350 PL_tokenbuf[0] = '-';
4352 while (d < PL_bufend && SPACE_OR_TAB(*d))
4355 if (d < PL_bufend && isIDFIRST_lazy_if(d,UTF)) {
4356 d = scan_word(d, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
4358 while (d < PL_bufend && SPACE_OR_TAB(*d))
4361 const char minus = (PL_tokenbuf[0] == '-');
4362 s = force_word(s + minus, WORD, FALSE, TRUE, FALSE);
4370 PL_lex_brackstack[PL_lex_brackets++] = XSTATE;
4375 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
4380 if (PL_oldoldbufptr == PL_last_lop)
4381 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
4383 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
4386 if (PL_expect == XREF && PL_lex_state == LEX_INTERPNORMAL) {
4388 /* This hack is to get the ${} in the message. */
4390 yyerror("syntax error");
4393 OPERATOR(HASHBRACK);
4395 /* This hack serves to disambiguate a pair of curlies
4396 * as being a block or an anon hash. Normally, expectation
4397 * determines that, but in cases where we're not in a
4398 * position to expect anything in particular (like inside
4399 * eval"") we have to resolve the ambiguity. This code
4400 * covers the case where the first term in the curlies is a
4401 * quoted string. Most other cases need to be explicitly
4402 * disambiguated by prepending a "+" before the opening
4403 * curly in order to force resolution as an anon hash.
4405 * XXX should probably propagate the outer expectation
4406 * into eval"" to rely less on this hack, but that could
4407 * potentially break current behavior of eval"".
4411 if (*s == '\'' || *s == '"' || *s == '`') {
4412 /* common case: get past first string, handling escapes */
4413 for (t++; t < PL_bufend && *t&