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 yychar (*PL_yycharp)
27 #define yylval (*PL_yylvalp)
29 static const char ident_too_long[] = "Identifier too long";
30 static const char commaless_variable_list[] = "comma-less variable list";
32 static void restore_rsfp(pTHX_ void *f);
33 #ifndef PERL_NO_UTF16_FILTER
34 static I32 utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen);
35 static I32 utf16rev_textfilter(pTHX_ int idx, SV *sv, int maxlen);
39 # define CURMAD(slot,sv) if (PL_madskills) { curmad(slot,sv); sv = 0; }
40 # define NEXTVAL_NEXTTOKE PL_nexttoke[PL_curforce].next_val
42 # define CURMAD(slot,sv)
43 # define NEXTVAL_NEXTTOKE PL_nextval[PL_nexttoke]
46 #define XFAKEBRACK 128
49 #ifdef USE_UTF8_SCRIPTS
50 # define UTF (!IN_BYTES)
52 # define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8))
55 /* In variables named $^X, these are the legal values for X.
56 * 1999-02-27 mjd-perl-patch@plover.com */
57 #define isCONTROLVAR(x) (isUPPER(x) || strchr("[\\]^_?", (x)))
59 /* On MacOS, respect nonbreaking spaces */
60 #ifdef MACOS_TRADITIONAL
61 #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\312'||(c)=='\t')
63 #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t')
66 /* LEX_* are values for PL_lex_state, the state of the lexer.
67 * They are arranged oddly so that the guard on the switch statement
68 * can get by with a single comparison (if the compiler is smart enough).
71 /* #define LEX_NOTPARSING 11 is done in perl.h. */
73 #define LEX_NORMAL 10 /* normal code (ie not within "...") */
74 #define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */
75 #define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */
76 #define LEX_INTERPPUSH 7 /* starting a new sublex parse level */
77 #define LEX_INTERPSTART 6 /* expecting the start of a $var */
79 /* at end of code, eg "$x" followed by: */
80 #define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */
81 #define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */
83 #define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of
84 string or after \E, $foo, etc */
85 #define LEX_INTERPCONST 2 /* NOT USED */
86 #define LEX_FORMLINE 1 /* expecting a format line */
87 #define LEX_KNOWNEXT 0 /* next token known; just return it */
91 static const char* const lex_state_names[] = {
110 #include "keywords.h"
112 /* CLINE is a macro that ensures PL_copline has a sane value */
117 #define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline))
120 # define SKIPSPACE0(s) skipspace0(s)
121 # define SKIPSPACE1(s) skipspace1(s)
122 # define SKIPSPACE2(s,tsv) skipspace2(s,&tsv)
123 # define PEEKSPACE(s) skipspace2(s,0)
125 # define SKIPSPACE0(s) skipspace(s)
126 # define SKIPSPACE1(s) skipspace(s)
127 # define SKIPSPACE2(s,tsv) skipspace(s)
128 # define PEEKSPACE(s) skipspace(s)
132 * Convenience functions to return different tokens and prime the
133 * lexer for the next token. They all take an argument.
135 * TOKEN : generic token (used for '(', DOLSHARP, etc)
136 * OPERATOR : generic operator
137 * AOPERATOR : assignment operator
138 * PREBLOCK : beginning the block after an if, while, foreach, ...
139 * PRETERMBLOCK : beginning a non-code-defining {} block (eg, hash ref)
140 * PREREF : *EXPR where EXPR is not a simple identifier
141 * TERM : expression term
142 * LOOPX : loop exiting command (goto, last, dump, etc)
143 * FTST : file test operator
144 * FUN0 : zero-argument function
145 * FUN1 : not used, except for not, which isn't a UNIOP
146 * BOop : bitwise or or xor
148 * SHop : shift operator
149 * PWop : power operator
150 * PMop : pattern-matching operator
151 * Aop : addition-level operator
152 * Mop : multiplication-level operator
153 * Eop : equality-testing operator
154 * Rop : relational operator <= != gt
156 * Also see LOP and lop() below.
159 #ifdef DEBUGGING /* Serve -DT. */
160 # define REPORT(retval) tokereport((I32)retval)
162 # define REPORT(retval) (retval)
165 #define TOKEN(retval) return ( PL_bufptr = s, REPORT(retval))
166 #define OPERATOR(retval) return (PL_expect = XTERM, PL_bufptr = s, REPORT(retval))
167 #define AOPERATOR(retval) return ao((PL_expect = XTERM, PL_bufptr = s, REPORT(retval)))
168 #define PREBLOCK(retval) return (PL_expect = XBLOCK,PL_bufptr = s, REPORT(retval))
169 #define PRETERMBLOCK(retval) return (PL_expect = XTERMBLOCK,PL_bufptr = s, REPORT(retval))
170 #define PREREF(retval) return (PL_expect = XREF,PL_bufptr = s, REPORT(retval))
171 #define TERM(retval) return (CLINE, PL_expect = XOPERATOR, PL_bufptr = s, REPORT(retval))
172 #define LOOPX(f) return (yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)LOOPEX))
173 #define FTST(f) return (yylval.ival=f, PL_expect=XTERMORDORDOR, PL_bufptr=s, REPORT((int)UNIOP))
174 #define FUN0(f) return (yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0))
175 #define FUN1(f) return (yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC1))
176 #define BOop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITOROP)))
177 #define BAop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITANDOP)))
178 #define SHop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)SHIFTOP)))
179 #define PWop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)POWOP)))
180 #define PMop(f) return(yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MATCHOP))
181 #define Aop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)ADDOP)))
182 #define Mop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MULOP)))
183 #define Eop(f) return (yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)EQOP))
184 #define Rop(f) return (yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)RELOP))
186 /* This bit of chicanery makes a unary function followed by
187 * a parenthesis into a function with one argument, highest precedence.
188 * The UNIDOR macro is for unary functions that can be followed by the //
189 * operator (such as C<shift // 0>).
191 #define UNI2(f,x) { \
195 PL_last_uni = PL_oldbufptr; \
196 PL_last_lop_op = f; \
198 return REPORT( (int)FUNC1 ); \
200 return REPORT( *s=='(' ? (int)FUNC1 : (int)UNIOP ); \
202 #define UNI(f) UNI2(f,XTERM)
203 #define UNIDOR(f) UNI2(f,XTERMORDORDOR)
205 #define UNIBRACK(f) { \
208 PL_last_uni = PL_oldbufptr; \
210 return REPORT( (int)FUNC1 ); \
212 return REPORT( (*s == '(') ? (int)FUNC1 : (int)UNIOP ); \
215 /* grandfather return to old style */
216 #define OLDLOP(f) return(yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)LSTOP)
220 /* how to interpret the yylval associated with the token */
224 TOKENTYPE_OPNUM, /* yylval.ival contains an opcode number */
230 static struct debug_tokens {
232 enum token_type type;
234 } const debug_tokens[] =
236 { ADDOP, TOKENTYPE_OPNUM, "ADDOP" },
237 { ANDAND, TOKENTYPE_NONE, "ANDAND" },
238 { ANDOP, TOKENTYPE_NONE, "ANDOP" },
239 { ANONSUB, TOKENTYPE_IVAL, "ANONSUB" },
240 { ARROW, TOKENTYPE_NONE, "ARROW" },
241 { ASSIGNOP, TOKENTYPE_OPNUM, "ASSIGNOP" },
242 { BITANDOP, TOKENTYPE_OPNUM, "BITANDOP" },
243 { BITOROP, TOKENTYPE_OPNUM, "BITOROP" },
244 { COLONATTR, TOKENTYPE_NONE, "COLONATTR" },
245 { CONTINUE, TOKENTYPE_NONE, "CONTINUE" },
246 { DEFAULT, TOKENTYPE_NONE, "DEFAULT" },
247 { DO, TOKENTYPE_NONE, "DO" },
248 { DOLSHARP, TOKENTYPE_NONE, "DOLSHARP" },
249 { DORDOR, TOKENTYPE_NONE, "DORDOR" },
250 { DOROP, TOKENTYPE_OPNUM, "DOROP" },
251 { DOTDOT, TOKENTYPE_IVAL, "DOTDOT" },
252 { ELSE, TOKENTYPE_NONE, "ELSE" },
253 { ELSIF, TOKENTYPE_IVAL, "ELSIF" },
254 { EQOP, TOKENTYPE_OPNUM, "EQOP" },
255 { FOR, TOKENTYPE_IVAL, "FOR" },
256 { FORMAT, TOKENTYPE_NONE, "FORMAT" },
257 { FUNC, TOKENTYPE_OPNUM, "FUNC" },
258 { FUNC0, TOKENTYPE_OPNUM, "FUNC0" },
259 { FUNC0SUB, TOKENTYPE_OPVAL, "FUNC0SUB" },
260 { FUNC1, TOKENTYPE_OPNUM, "FUNC1" },
261 { FUNCMETH, TOKENTYPE_OPVAL, "FUNCMETH" },
262 { GIVEN, TOKENTYPE_IVAL, "GIVEN" },
263 { HASHBRACK, TOKENTYPE_NONE, "HASHBRACK" },
264 { IF, TOKENTYPE_IVAL, "IF" },
265 { LABEL, TOKENTYPE_PVAL, "LABEL" },
266 { LOCAL, TOKENTYPE_IVAL, "LOCAL" },
267 { LOOPEX, TOKENTYPE_OPNUM, "LOOPEX" },
268 { LSTOP, TOKENTYPE_OPNUM, "LSTOP" },
269 { LSTOPSUB, TOKENTYPE_OPVAL, "LSTOPSUB" },
270 { MATCHOP, TOKENTYPE_OPNUM, "MATCHOP" },
271 { METHOD, TOKENTYPE_OPVAL, "METHOD" },
272 { MULOP, TOKENTYPE_OPNUM, "MULOP" },
273 { MY, TOKENTYPE_IVAL, "MY" },
274 { MYSUB, TOKENTYPE_NONE, "MYSUB" },
275 { NOAMP, TOKENTYPE_NONE, "NOAMP" },
276 { NOTOP, TOKENTYPE_NONE, "NOTOP" },
277 { OROP, TOKENTYPE_IVAL, "OROP" },
278 { OROR, TOKENTYPE_NONE, "OROR" },
279 { PACKAGE, TOKENTYPE_NONE, "PACKAGE" },
280 { PMFUNC, TOKENTYPE_OPVAL, "PMFUNC" },
281 { POSTDEC, TOKENTYPE_NONE, "POSTDEC" },
282 { POSTINC, TOKENTYPE_NONE, "POSTINC" },
283 { POWOP, TOKENTYPE_OPNUM, "POWOP" },
284 { PREDEC, TOKENTYPE_NONE, "PREDEC" },
285 { PREINC, TOKENTYPE_NONE, "PREINC" },
286 { PRIVATEREF, TOKENTYPE_OPVAL, "PRIVATEREF" },
287 { REFGEN, TOKENTYPE_NONE, "REFGEN" },
288 { RELOP, TOKENTYPE_OPNUM, "RELOP" },
289 { SHIFTOP, TOKENTYPE_OPNUM, "SHIFTOP" },
290 { SUB, TOKENTYPE_NONE, "SUB" },
291 { THING, TOKENTYPE_OPVAL, "THING" },
292 { UMINUS, TOKENTYPE_NONE, "UMINUS" },
293 { UNIOP, TOKENTYPE_OPNUM, "UNIOP" },
294 { UNIOPSUB, TOKENTYPE_OPVAL, "UNIOPSUB" },
295 { UNLESS, TOKENTYPE_IVAL, "UNLESS" },
296 { UNTIL, TOKENTYPE_IVAL, "UNTIL" },
297 { USE, TOKENTYPE_IVAL, "USE" },
298 { WHEN, TOKENTYPE_IVAL, "WHEN" },
299 { WHILE, TOKENTYPE_IVAL, "WHILE" },
300 { WORD, TOKENTYPE_OPVAL, "WORD" },
301 { 0, TOKENTYPE_NONE, NULL }
304 /* dump the returned token in rv, plus any optional arg in yylval */
307 S_tokereport(pTHX_ I32 rv)
311 const char *name = NULL;
312 enum token_type type = TOKENTYPE_NONE;
313 const struct debug_tokens *p;
314 SV* const report = newSVpvs("<== ");
316 for (p = debug_tokens; p->token; p++) {
317 if (p->token == (int)rv) {
324 Perl_sv_catpv(aTHX_ report, name);
325 else if ((char)rv > ' ' && (char)rv < '~')
326 Perl_sv_catpvf(aTHX_ report, "'%c'", (char)rv);
328 sv_catpvs(report, "EOF");
330 Perl_sv_catpvf(aTHX_ report, "?? %"IVdf, (IV)rv);
333 case TOKENTYPE_GVVAL: /* doesn't appear to be used */
336 Perl_sv_catpvf(aTHX_ report, "(ival=%"IVdf")", (IV)yylval.ival);
338 case TOKENTYPE_OPNUM:
339 Perl_sv_catpvf(aTHX_ report, "(ival=op_%s)",
340 PL_op_name[yylval.ival]);
343 Perl_sv_catpvf(aTHX_ report, "(pval=\"%s\")", yylval.pval);
345 case TOKENTYPE_OPVAL:
347 Perl_sv_catpvf(aTHX_ report, "(opval=op_%s)",
348 PL_op_name[yylval.opval->op_type]);
349 if (yylval.opval->op_type == OP_CONST) {
350 Perl_sv_catpvf(aTHX_ report, " %s",
351 SvPEEK(cSVOPx_sv(yylval.opval)));
356 sv_catpvs(report, "(opval=null)");
359 PerlIO_printf(Perl_debug_log, "### %s\n\n", SvPV_nolen_const(report));
365 /* print the buffer with suitable escapes */
368 S_printbuf(pTHX_ const char* fmt, const char* s)
370 SV* const tmp = newSVpvs("");
371 PerlIO_printf(Perl_debug_log, fmt, pv_display(tmp, s, strlen(s), 0, 60));
380 * This subroutine detects &&=, ||=, and //= and turns an ANDAND, OROR or DORDOR
381 * into an OP_ANDASSIGN, OP_ORASSIGN, or OP_DORASSIGN
385 S_ao(pTHX_ int toketype)
388 if (*PL_bufptr == '=') {
390 if (toketype == ANDAND)
391 yylval.ival = OP_ANDASSIGN;
392 else if (toketype == OROR)
393 yylval.ival = OP_ORASSIGN;
394 else if (toketype == DORDOR)
395 yylval.ival = OP_DORASSIGN;
403 * When Perl expects an operator and finds something else, no_op
404 * prints the warning. It always prints "<something> found where
405 * operator expected. It prints "Missing semicolon on previous line?"
406 * if the surprise occurs at the start of the line. "do you need to
407 * predeclare ..." is printed out for code like "sub bar; foo bar $x"
408 * where the compiler doesn't know if foo is a method call or a function.
409 * It prints "Missing operator before end of line" if there's nothing
410 * after the missing operator, or "... before <...>" if there is something
411 * after the missing operator.
415 S_no_op(pTHX_ const char *what, char *s)
418 char * const oldbp = PL_bufptr;
419 const bool is_first = (PL_oldbufptr == PL_linestart);
425 yywarn(Perl_form(aTHX_ "%s found where operator expected", what));
426 if (ckWARN_d(WARN_SYNTAX)) {
428 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
429 "\t(Missing semicolon on previous line?)\n");
430 else if (PL_oldoldbufptr && isIDFIRST_lazy_if(PL_oldoldbufptr,UTF)) {
432 for (t = PL_oldoldbufptr; (isALNUM_lazy_if(t,UTF) || *t == ':'); t++)
434 if (t < PL_bufptr && isSPACE(*t))
435 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
436 "\t(Do you need to predeclare %.*s?)\n",
437 (int)(t - PL_oldoldbufptr), PL_oldoldbufptr);
441 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
442 "\t(Missing operator before %.*s?)\n", (int)(s - oldbp), oldbp);
450 * Complain about missing quote/regexp/heredoc terminator.
451 * If it's called with NULL then it cauterizes the line buffer.
452 * If we're in a delimited string and the delimiter is a control
453 * character, it's reformatted into a two-char sequence like ^C.
458 S_missingterm(pTHX_ char *s)
464 char * const nl = strrchr(s,'\n');
470 iscntrl(PL_multi_close)
472 PL_multi_close < 32 || PL_multi_close == 127
476 tmpbuf[1] = (char)toCTRL(PL_multi_close);
481 *tmpbuf = (char)PL_multi_close;
485 q = strchr(s,'"') ? '\'' : '"';
486 Perl_croak(aTHX_ "Can't find string terminator %c%s%c anywhere before EOF",q,s,q);
489 #define FEATURE_IS_ENABLED(name) \
490 ((0 != (PL_hints & HINT_LOCALIZE_HH)) \
491 && S_feature_is_enabled(aTHX_ STR_WITH_LEN(name)))
493 * S_feature_is_enabled
494 * Check whether the named feature is enabled.
497 S_feature_is_enabled(pTHX_ const char *name, STRLEN namelen)
500 HV * const hinthv = GvHV(PL_hintgv);
501 char he_name[32] = "feature_";
502 (void) my_strlcpy(&he_name[8], name, 24);
504 return (hinthv && hv_exists(hinthv, he_name, 8 + namelen));
512 Perl_deprecate(pTHX_ const char *s)
514 if (ckWARN(WARN_DEPRECATED))
515 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED), "Use of %s is deprecated", s);
519 Perl_deprecate_old(pTHX_ const char *s)
521 /* This function should NOT be called for any new deprecated warnings */
522 /* Use Perl_deprecate instead */
524 /* It is here to maintain backward compatibility with the pre-5.8 */
525 /* warnings category hierarchy. The "deprecated" category used to */
526 /* live under the "syntax" category. It is now a top-level category */
527 /* in its own right. */
529 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
530 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
531 "Use of %s is deprecated", s);
535 * experimental text filters for win32 carriage-returns, utf16-to-utf8 and
536 * utf16-to-utf8-reversed.
539 #ifdef PERL_CR_FILTER
543 register const char *s = SvPVX_const(sv);
544 register const char * const e = s + SvCUR(sv);
545 /* outer loop optimized to do nothing if there are no CR-LFs */
547 if (*s++ == '\r' && *s == '\n') {
548 /* hit a CR-LF, need to copy the rest */
549 register char *d = s - 1;
552 if (*s == '\r' && s[1] == '\n')
563 S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
565 const I32 count = FILTER_READ(idx+1, sv, maxlen);
566 if (count > 0 && !maxlen)
574 * Initialize variables. Uses the Perl save_stack to save its state (for
575 * recursive calls to the parser).
579 Perl_lex_start(pTHX_ SV *line)
585 SAVEI32(PL_lex_dojoin);
586 SAVEI32(PL_lex_brackets);
587 SAVEI32(PL_lex_casemods);
588 SAVEI32(PL_lex_starts);
589 SAVEI32(PL_lex_state);
590 SAVEVPTR(PL_lex_inpat);
591 SAVEI32(PL_lex_inwhat);
593 if (PL_lex_state == LEX_KNOWNEXT) {
594 I32 toke = PL_lasttoke;
595 while (--toke >= 0) {
596 SAVEI32(PL_nexttoke[toke].next_type);
597 SAVEVPTR(PL_nexttoke[toke].next_val);
599 SAVEVPTR(PL_nexttoke[toke].next_mad);
601 SAVEI32(PL_lasttoke);
604 SAVESPTR(PL_thistoken);
605 SAVESPTR(PL_thiswhite);
606 SAVESPTR(PL_nextwhite);
607 SAVESPTR(PL_thisopen);
608 SAVESPTR(PL_thisclose);
609 SAVESPTR(PL_thisstuff);
610 SAVEVPTR(PL_thismad);
611 SAVEI32(PL_realtokenstart);
612 SAVEI32(PL_faketokens);
614 SAVEI32(PL_curforce);
616 if (PL_lex_state == LEX_KNOWNEXT) {
617 I32 toke = PL_nexttoke;
618 while (--toke >= 0) {
619 SAVEI32(PL_nexttype[toke]);
620 SAVEVPTR(PL_nextval[toke]);
622 SAVEI32(PL_nexttoke);
625 SAVECOPLINE(PL_curcop);
628 SAVEPPTR(PL_oldbufptr);
629 SAVEPPTR(PL_oldoldbufptr);
630 SAVEPPTR(PL_last_lop);
631 SAVEPPTR(PL_last_uni);
632 SAVEPPTR(PL_linestart);
633 SAVESPTR(PL_linestr);
634 SAVEGENERICPV(PL_lex_brackstack);
635 SAVEGENERICPV(PL_lex_casestack);
636 SAVEDESTRUCTOR_X(restore_rsfp, PL_rsfp);
637 SAVESPTR(PL_lex_stuff);
638 SAVEI32(PL_lex_defer);
639 SAVEI32(PL_sublex_info.sub_inwhat);
640 SAVESPTR(PL_lex_repl);
642 SAVEINT(PL_lex_expect);
644 PL_lex_state = LEX_NORMAL;
648 Newx(PL_lex_brackstack, 120, char);
649 Newx(PL_lex_casestack, 12, char);
651 *PL_lex_casestack = '\0';
663 PL_sublex_info.sub_inwhat = 0;
665 if (SvREADONLY(PL_linestr))
666 PL_linestr = sv_2mortal(newSVsv(PL_linestr));
667 s = SvPV_const(PL_linestr, len);
668 if (!len || s[len-1] != ';') {
669 if (!(SvFLAGS(PL_linestr) & SVs_TEMP))
670 PL_linestr = sv_2mortal(newSVsv(PL_linestr));
671 sv_catpvs(PL_linestr, "\n;");
673 SvTEMP_off(PL_linestr);
674 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = SvPVX(PL_linestr);
675 PL_bufend = PL_bufptr + SvCUR(PL_linestr);
676 PL_last_lop = PL_last_uni = NULL;
682 * Finalizer for lexing operations. Must be called when the parser is
683 * done with the lexer.
690 PL_doextract = FALSE;
695 * This subroutine has nothing to do with tilting, whether at windmills
696 * or pinball tables. Its name is short for "increment line". It
697 * increments the current line number in CopLINE(PL_curcop) and checks
698 * to see whether the line starts with a comment of the form
699 * # line 500 "foo.pm"
700 * If so, it sets the current line number and file to the values in the comment.
704 S_incline(pTHX_ char *s)
712 CopLINE_inc(PL_curcop);
715 while (SPACE_OR_TAB(*s))
717 if (strnEQ(s, "line", 4))
721 if (SPACE_OR_TAB(*s))
725 while (SPACE_OR_TAB(*s))
733 while (SPACE_OR_TAB(*s))
735 if (*s == '"' && (t = strchr(s+1, '"'))) {
745 while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
747 if (*e != '\n' && *e != '\0')
748 return; /* false alarm */
754 const char * const cf = CopFILE(PL_curcop);
755 STRLEN tmplen = cf ? strlen(cf) : 0;
756 if (tmplen > 7 && strnEQ(cf, "(eval ", 6)) {
757 /* must copy *{"::_<(eval N)[oldfilename:L]"}
758 * to *{"::_<newfilename"} */
759 char smallbuf[256], smallbuf2[256];
760 char *tmpbuf, *tmpbuf2;
762 STRLEN tmplen2 = strlen(s);
763 if (tmplen + 3 < sizeof smallbuf)
766 Newx(tmpbuf, tmplen + 3, char);
767 if (tmplen2 + 3 < sizeof smallbuf2)
770 Newx(tmpbuf2, tmplen2 + 3, char);
771 tmpbuf[0] = tmpbuf2[0] = '_';
772 tmpbuf[1] = tmpbuf2[1] = '<';
773 memcpy(tmpbuf + 2, cf, ++tmplen);
774 memcpy(tmpbuf2 + 2, s, ++tmplen2);
776 gvp = (GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, FALSE);
778 gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE);
780 gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE);
781 /* adjust ${"::_<newfilename"} to store the new file name */
782 GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2);
783 GvHV(gv2) = (HV*)SvREFCNT_inc(GvHV(*gvp));
784 GvAV(gv2) = (AV*)SvREFCNT_inc(GvAV(*gvp));
786 if (tmpbuf != smallbuf) Safefree(tmpbuf);
787 if (tmpbuf2 != smallbuf2) Safefree(tmpbuf2);
790 CopFILE_free(PL_curcop);
791 CopFILE_set(PL_curcop, s);
794 CopLINE_set(PL_curcop, atoi(n)-1);
798 /* skip space before PL_thistoken */
801 S_skipspace0(pTHX_ register char *s)
808 PL_thiswhite = newSVpvn("",0);
809 sv_catsv(PL_thiswhite, PL_skipwhite);
810 sv_free(PL_skipwhite);
813 PL_realtokenstart = s - SvPVX(PL_linestr);
817 /* skip space after PL_thistoken */
820 S_skipspace1(pTHX_ register char *s)
822 const char *start = s;
823 I32 startoff = start - SvPVX(PL_linestr);
828 start = SvPVX(PL_linestr) + startoff;
829 if (!PL_thistoken && PL_realtokenstart >= 0) {
830 const char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
831 PL_thistoken = newSVpvn(tstart, start - tstart);
833 PL_realtokenstart = -1;
836 PL_nextwhite = newSVpvn("",0);
837 sv_catsv(PL_nextwhite, PL_skipwhite);
838 sv_free(PL_skipwhite);
845 S_skipspace2(pTHX_ register char *s, SV **svp)
848 const I32 bufptroff = PL_bufptr - SvPVX(PL_linestr);
849 const I32 startoff = s - SvPVX(PL_linestr);
852 PL_bufptr = SvPVX(PL_linestr) + bufptroff;
853 if (!PL_madskills || !svp)
855 start = SvPVX(PL_linestr) + startoff;
856 if (!PL_thistoken && PL_realtokenstart >= 0) {
857 char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
858 PL_thistoken = newSVpvn(tstart, start - tstart);
859 PL_realtokenstart = -1;
863 *svp = newSVpvn("",0);
864 sv_setsv(*svp, PL_skipwhite);
865 sv_free(PL_skipwhite);
875 * Called to gobble the appropriate amount and type of whitespace.
876 * Skips comments as well.
880 S_skipspace(pTHX_ register char *s)
885 int startoff = s - SvPVX(PL_linestr);
888 sv_free(PL_skipwhite);
893 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
894 while (s < PL_bufend && SPACE_OR_TAB(*s))
904 SSize_t oldprevlen, oldoldprevlen;
905 SSize_t oldloplen = 0, oldunilen = 0;
906 while (s < PL_bufend && isSPACE(*s)) {
907 if (*s++ == '\n' && PL_in_eval && !PL_rsfp)
912 if (s < PL_bufend && *s == '#') {
913 while (s < PL_bufend && *s != '\n')
917 if (PL_in_eval && !PL_rsfp) {
924 /* only continue to recharge the buffer if we're at the end
925 * of the buffer, we're not reading from a source filter, and
926 * we're in normal lexing mode
928 if (s < PL_bufend || !PL_rsfp || PL_sublex_info.sub_inwhat ||
929 PL_lex_state == LEX_FORMLINE)
936 /* try to recharge the buffer */
938 curoff = s - SvPVX(PL_linestr);
941 if ((s = filter_gets(PL_linestr, PL_rsfp,
942 (prevlen = SvCUR(PL_linestr)))) == NULL)
945 if (PL_madskills && curoff != startoff) {
947 PL_skipwhite = newSVpvn("",0);
948 sv_catpvn(PL_skipwhite, SvPVX(PL_linestr) + startoff,
952 /* mustn't throw out old stuff yet if madpropping */
953 SvCUR(PL_linestr) = curoff;
954 s = SvPVX(PL_linestr) + curoff;
956 if (curoff && s[-1] == '\n')
960 /* end of file. Add on the -p or -n magic */
961 /* XXX these shouldn't really be added here, can't set PL_faketokens */
965 ";}continue{print or die qq(-p destination: $!\\n);}");
968 ";}continue{print or die qq(-p destination: $!\\n);}");
970 PL_minus_n = PL_minus_p = 0;
972 else if (PL_minus_n) {
974 sv_catpvn(PL_linestr, ";}", 2);
976 sv_setpvn(PL_linestr, ";}", 2);
982 sv_catpvn(PL_linestr,";", 1);
984 sv_setpvn(PL_linestr,";", 1);
987 /* reset variables for next time we lex */
988 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart
994 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
995 PL_last_lop = PL_last_uni = NULL;
997 /* Close the filehandle. Could be from -P preprocessor,
998 * STDIN, or a regular file. If we were reading code from
999 * STDIN (because the commandline held no -e or filename)
1000 * then we don't close it, we reset it so the code can
1001 * read from STDIN too.
1004 if (PL_preprocess && !PL_in_eval)
1005 (void)PerlProc_pclose(PL_rsfp);
1006 else if ((PerlIO*)PL_rsfp == PerlIO_stdin())
1007 PerlIO_clearerr(PL_rsfp);
1009 (void)PerlIO_close(PL_rsfp);
1014 /* not at end of file, so we only read another line */
1015 /* make corresponding updates to old pointers, for yyerror() */
1016 oldprevlen = PL_oldbufptr - PL_bufend;
1017 oldoldprevlen = PL_oldoldbufptr - PL_bufend;
1019 oldunilen = PL_last_uni - PL_bufend;
1021 oldloplen = PL_last_lop - PL_bufend;
1022 PL_linestart = PL_bufptr = s + prevlen;
1023 PL_bufend = s + SvCUR(PL_linestr);
1025 PL_oldbufptr = s + oldprevlen;
1026 PL_oldoldbufptr = s + oldoldprevlen;
1028 PL_last_uni = s + oldunilen;
1030 PL_last_lop = s + oldloplen;
1033 /* debugger active and we're not compiling the debugger code,
1034 * so store the line into the debugger's array of lines
1036 if (PERLDB_LINE && PL_curstash != PL_debstash) {
1037 SV * const sv = newSV(0);
1039 sv_upgrade(sv, SVt_PVMG);
1040 sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
1043 av_store(CopFILEAVx(PL_curcop),(I32)CopLINE(PL_curcop),sv);
1051 PL_skipwhite = newSVpvn("",0);
1052 curoff = s - SvPVX(PL_linestr);
1053 if (curoff - startoff)
1054 sv_catpvn(PL_skipwhite, SvPVX(PL_linestr) + startoff,
1063 * Check the unary operators to ensure there's no ambiguity in how they're
1064 * used. An ambiguous piece of code would be:
1066 * This doesn't mean rand() + 5. Because rand() is a unary operator,
1067 * the +5 is its argument.
1077 if (PL_oldoldbufptr != PL_last_uni)
1079 while (isSPACE(*PL_last_uni))
1082 while (isALNUM_lazy_if(s,UTF) || *s == '-')
1084 if ((t = strchr(s, '(')) && t < PL_bufptr)
1087 if (ckWARN_d(WARN_AMBIGUOUS)){
1088 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
1089 "Warning: Use of \"%.*s\" without parentheses is ambiguous",
1090 (int)(s - PL_last_uni), PL_last_uni);
1095 * LOP : macro to build a list operator. Its behaviour has been replaced
1096 * with a subroutine, S_lop() for which LOP is just another name.
1099 #define LOP(f,x) return lop(f,x,s)
1103 * Build a list operator (or something that might be one). The rules:
1104 * - if we have a next token, then it's a list operator [why?]
1105 * - if the next thing is an opening paren, then it's a function
1106 * - else it's a list operator
1110 S_lop(pTHX_ I32 f, int x, char *s)
1117 PL_last_lop = PL_oldbufptr;
1118 PL_last_lop_op = (OPCODE)f;
1121 return REPORT(LSTOP);
1124 return REPORT(LSTOP);
1127 return REPORT(FUNC);
1130 return REPORT(FUNC);
1132 return REPORT(LSTOP);
1138 * Sets up for an eventual force_next(). start_force(0) basically does
1139 * an unshift, while start_force(-1) does a push. yylex removes items
1144 S_start_force(pTHX_ int where)
1148 if (where < 0) /* so people can duplicate start_force(PL_curforce) */
1149 where = PL_lasttoke;
1150 assert(PL_curforce < 0 || PL_curforce == where);
1151 if (PL_curforce != where) {
1152 for (i = PL_lasttoke; i > where; --i) {
1153 PL_nexttoke[i] = PL_nexttoke[i-1];
1157 if (PL_curforce < 0) /* in case of duplicate start_force() */
1158 Zero(&PL_nexttoke[where], 1, NEXTTOKE);
1159 PL_curforce = where;
1162 curmad('^', newSVpvn("",0));
1163 CURMAD('_', PL_nextwhite);
1168 S_curmad(pTHX_ char slot, SV *sv)
1174 if (PL_curforce < 0)
1175 where = &PL_thismad;
1177 where = &PL_nexttoke[PL_curforce].next_mad;
1180 sv_setpvn(sv, "", 0);
1183 if (UTF && is_utf8_string((U8*)SvPVX(sv), SvCUR(sv)))
1185 else if (PL_encoding) {
1186 sv_recode_to_utf8(sv, PL_encoding);
1191 /* keep a slot open for the head of the list? */
1192 if (slot != '_' && *where && (*where)->mad_key == '^') {
1193 (*where)->mad_key = slot;
1194 sv_free((*where)->mad_val);
1195 (*where)->mad_val = (void*)sv;
1198 addmad(newMADsv(slot, sv), where, 0);
1201 # define start_force(where) NOOP
1202 # define curmad(slot, sv) NOOP
1207 * When the lexer realizes it knows the next token (for instance,
1208 * it is reordering tokens for the parser) then it can call S_force_next
1209 * to know what token to return the next time the lexer is called. Caller
1210 * will need to set PL_nextval[] (or PL_nexttoke[].next_val with PERL_MAD),
1211 * and possibly PL_expect to ensure the lexer handles the token correctly.
1215 S_force_next(pTHX_ I32 type)
1219 if (PL_curforce < 0)
1220 start_force(PL_lasttoke);
1221 PL_nexttoke[PL_curforce].next_type = type;
1222 if (PL_lex_state != LEX_KNOWNEXT)
1223 PL_lex_defer = PL_lex_state;
1224 PL_lex_state = LEX_KNOWNEXT;
1225 PL_lex_expect = PL_expect;
1228 PL_nexttype[PL_nexttoke] = type;
1230 if (PL_lex_state != LEX_KNOWNEXT) {
1231 PL_lex_defer = PL_lex_state;
1232 PL_lex_expect = PL_expect;
1233 PL_lex_state = LEX_KNOWNEXT;
1239 S_newSV_maybe_utf8(pTHX_ const char *start, STRLEN len)
1242 SV * const sv = newSVpvn(start,len);
1243 if (UTF && !IN_BYTES && is_utf8_string((const U8*)start, len))
1250 * When the lexer knows the next thing is a word (for instance, it has
1251 * just seen -> and it knows that the next char is a word char, then
1252 * it calls S_force_word to stick the next word into the PL_next lookahead.
1255 * char *start : buffer position (must be within PL_linestr)
1256 * int token : PL_next will be this type of bare word (e.g., METHOD,WORD)
1257 * int check_keyword : if true, Perl checks to make sure the word isn't
1258 * a keyword (do this if the word is a label, e.g. goto FOO)
1259 * int allow_pack : if true, : characters will also be allowed (require,
1260 * use, etc. do this)
1261 * int allow_initial_tick : used by the "sub" lexer only.
1265 S_force_word(pTHX_ register char *start, int token, int check_keyword, int allow_pack, int allow_initial_tick)
1271 start = SKIPSPACE1(start);
1273 if (isIDFIRST_lazy_if(s,UTF) ||
1274 (allow_pack && *s == ':') ||
1275 (allow_initial_tick && *s == '\'') )
1277 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, allow_pack, &len);
1278 if (check_keyword && keyword(PL_tokenbuf, len))
1280 start_force(PL_curforce);
1282 curmad('X', newSVpvn(start,s-start));
1283 if (token == METHOD) {
1288 PL_expect = XOPERATOR;
1291 NEXTVAL_NEXTTOKE.opval
1292 = (OP*)newSVOP(OP_CONST,0,
1293 S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
1294 NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
1302 * Called when the lexer wants $foo *foo &foo etc, but the program
1303 * text only contains the "foo" portion. The first argument is a pointer
1304 * to the "foo", and the second argument is the type symbol to prefix.
1305 * Forces the next token to be a "WORD".
1306 * Creates the symbol if it didn't already exist (via gv_fetchpv()).
1310 S_force_ident(pTHX_ register const char *s, int kind)
1314 const STRLEN len = strlen(s);
1315 OP* const o = (OP*)newSVOP(OP_CONST, 0, newSVpvn(s, len));
1316 start_force(PL_curforce);
1317 NEXTVAL_NEXTTOKE.opval = o;
1320 o->op_private = OPpCONST_ENTERED;
1321 /* XXX see note in pp_entereval() for why we forgo typo
1322 warnings if the symbol must be introduced in an eval.
1324 gv_fetchpvn_flags(s, len,
1325 PL_in_eval ? (GV_ADDMULTI | GV_ADDINEVAL)
1327 kind == '$' ? SVt_PV :
1328 kind == '@' ? SVt_PVAV :
1329 kind == '%' ? SVt_PVHV :
1337 Perl_str_to_version(pTHX_ SV *sv)
1342 const char *start = SvPV_const(sv,len);
1343 const char * const end = start + len;
1344 const bool utf = SvUTF8(sv) ? TRUE : FALSE;
1345 while (start < end) {
1349 n = utf8n_to_uvchr((U8*)start, len, &skip, 0);
1354 retval += ((NV)n)/nshift;
1363 * Forces the next token to be a version number.
1364 * If the next token appears to be an invalid version number, (e.g. "v2b"),
1365 * and if "guessing" is TRUE, then no new token is created (and the caller
1366 * must use an alternative parsing method).
1370 S_force_version(pTHX_ char *s, int guessing)
1376 I32 startoff = s - SvPVX(PL_linestr);
1385 while (isDIGIT(*d) || *d == '_' || *d == '.')
1389 start_force(PL_curforce);
1390 curmad('X', newSVpvn(s,d-s));
1393 if (*d == ';' || isSPACE(*d) || *d == '}' || !*d) {
1395 s = scan_num(s, &yylval);
1396 version = yylval.opval;
1397 ver = cSVOPx(version)->op_sv;
1398 if (SvPOK(ver) && !SvNIOK(ver)) {
1399 SvUPGRADE(ver, SVt_PVNV);
1400 SvNV_set(ver, str_to_version(ver));
1401 SvNOK_on(ver); /* hint that it is a version */
1404 else if (guessing) {
1407 sv_free(PL_nextwhite); /* let next token collect whitespace */
1409 s = SvPVX(PL_linestr) + startoff;
1417 if (PL_madskills && !version) {
1418 sv_free(PL_nextwhite); /* let next token collect whitespace */
1420 s = SvPVX(PL_linestr) + startoff;
1423 /* NOTE: The parser sees the package name and the VERSION swapped */
1424 start_force(PL_curforce);
1425 NEXTVAL_NEXTTOKE.opval = version;
1433 * Tokenize a quoted string passed in as an SV. It finds the next
1434 * chunk, up to end of string or a backslash. It may make a new
1435 * SV containing that chunk (if HINT_NEW_STRING is on). It also
1440 S_tokeq(pTHX_ SV *sv)
1444 register char *send;
1452 s = SvPV_force(sv, len);
1453 if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1)
1456 while (s < send && *s != '\\')
1461 if ( PL_hints & HINT_NEW_STRING ) {
1462 pv = sv_2mortal(newSVpvn(SvPVX_const(pv), len));
1468 if (s + 1 < send && (s[1] == '\\'))
1469 s++; /* all that, just for this */
1474 SvCUR_set(sv, d - SvPVX_const(sv));
1476 if ( PL_hints & HINT_NEW_STRING )
1477 return new_constant(NULL, 0, "q", sv, pv, "q");
1482 * Now come three functions related to double-quote context,
1483 * S_sublex_start, S_sublex_push, and S_sublex_done. They're used when
1484 * converting things like "\u\Lgnat" into ucfirst(lc("gnat")). They
1485 * interact with PL_lex_state, and create fake ( ... ) argument lists
1486 * to handle functions and concatenation.
1487 * They assume that whoever calls them will be setting up a fake
1488 * join call, because each subthing puts a ',' after it. This lets
1491 * join($, , 'lower ', lcfirst( 'uPpEr', ) ,)
1493 * (I'm not sure whether the spurious commas at the end of lcfirst's
1494 * arguments and join's arguments are created or not).
1499 * Assumes that yylval.ival is the op we're creating (e.g. OP_LCFIRST).
1501 * Pattern matching will set PL_lex_op to the pattern-matching op to
1502 * make (we return THING if yylval.ival is OP_NULL, PMFUNC otherwise).
1504 * OP_CONST and OP_READLINE are easy--just make the new op and return.
1506 * Everything else becomes a FUNC.
1508 * Sets PL_lex_state to LEX_INTERPPUSH unless (ival was OP_NULL or we
1509 * had an OP_CONST or OP_READLINE). This just sets us up for a
1510 * call to S_sublex_push().
1514 S_sublex_start(pTHX)
1517 register const I32 op_type = yylval.ival;
1519 if (op_type == OP_NULL) {
1520 yylval.opval = PL_lex_op;
1524 if (op_type == OP_CONST || op_type == OP_READLINE) {
1525 SV *sv = tokeq(PL_lex_stuff);
1527 if (SvTYPE(sv) == SVt_PVIV) {
1528 /* Overloaded constants, nothing fancy: Convert to SVt_PV: */
1530 const char * const p = SvPV_const(sv, len);
1531 SV * const nsv = newSVpvn(p, len);
1537 yylval.opval = (OP*)newSVOP(op_type, 0, sv);
1538 PL_lex_stuff = NULL;
1539 /* Allow <FH> // "foo" */
1540 if (op_type == OP_READLINE)
1541 PL_expect = XTERMORDORDOR;
1545 PL_sublex_info.super_state = PL_lex_state;
1546 PL_sublex_info.sub_inwhat = op_type;
1547 PL_sublex_info.sub_op = PL_lex_op;
1548 PL_lex_state = LEX_INTERPPUSH;
1552 yylval.opval = PL_lex_op;
1562 * Create a new scope to save the lexing state. The scope will be
1563 * ended in S_sublex_done. Returns a '(', starting the function arguments
1564 * to the uc, lc, etc. found before.
1565 * Sets PL_lex_state to LEX_INTERPCONCAT.
1574 PL_lex_state = PL_sublex_info.super_state;
1575 SAVEI32(PL_lex_dojoin);
1576 SAVEI32(PL_lex_brackets);
1577 SAVEI32(PL_lex_casemods);
1578 SAVEI32(PL_lex_starts);
1579 SAVEI32(PL_lex_state);
1580 SAVEVPTR(PL_lex_inpat);
1581 SAVEI32(PL_lex_inwhat);
1582 SAVECOPLINE(PL_curcop);
1583 SAVEPPTR(PL_bufptr);
1584 SAVEPPTR(PL_bufend);
1585 SAVEPPTR(PL_oldbufptr);
1586 SAVEPPTR(PL_oldoldbufptr);
1587 SAVEPPTR(PL_last_lop);
1588 SAVEPPTR(PL_last_uni);
1589 SAVEPPTR(PL_linestart);
1590 SAVESPTR(PL_linestr);
1591 SAVEGENERICPV(PL_lex_brackstack);
1592 SAVEGENERICPV(PL_lex_casestack);
1594 PL_linestr = PL_lex_stuff;
1595 PL_lex_stuff = NULL;
1597 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart
1598 = SvPVX(PL_linestr);
1599 PL_bufend += SvCUR(PL_linestr);
1600 PL_last_lop = PL_last_uni = NULL;
1601 SAVEFREESV(PL_linestr);
1603 PL_lex_dojoin = FALSE;
1604 PL_lex_brackets = 0;
1605 Newx(PL_lex_brackstack, 120, char);
1606 Newx(PL_lex_casestack, 12, char);
1607 PL_lex_casemods = 0;
1608 *PL_lex_casestack = '\0';
1610 PL_lex_state = LEX_INTERPCONCAT;
1611 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
1613 PL_lex_inwhat = PL_sublex_info.sub_inwhat;
1614 if (PL_lex_inwhat == OP_MATCH || PL_lex_inwhat == OP_QR || PL_lex_inwhat == OP_SUBST)
1615 PL_lex_inpat = PL_sublex_info.sub_op;
1617 PL_lex_inpat = NULL;
1624 * Restores lexer state after a S_sublex_push.
1631 if (!PL_lex_starts++) {
1632 SV * const sv = newSVpvs("");
1633 if (SvUTF8(PL_linestr))
1635 PL_expect = XOPERATOR;
1636 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
1640 if (PL_lex_casemods) { /* oops, we've got some unbalanced parens */
1641 PL_lex_state = LEX_INTERPCASEMOD;
1645 /* Is there a right-hand side to take care of? (s//RHS/ or tr//RHS/) */
1646 if (PL_lex_repl && (PL_lex_inwhat == OP_SUBST || PL_lex_inwhat == OP_TRANS)) {
1647 PL_linestr = PL_lex_repl;
1649 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
1650 PL_bufend += SvCUR(PL_linestr);
1651 PL_last_lop = PL_last_uni = NULL;
1652 SAVEFREESV(PL_linestr);
1653 PL_lex_dojoin = FALSE;
1654 PL_lex_brackets = 0;
1655 PL_lex_casemods = 0;
1656 *PL_lex_casestack = '\0';
1658 if (SvEVALED(PL_lex_repl)) {
1659 PL_lex_state = LEX_INTERPNORMAL;
1661 /* we don't clear PL_lex_repl here, so that we can check later
1662 whether this is an evalled subst; that means we rely on the
1663 logic to ensure sublex_done() is called again only via the
1664 branch (in yylex()) that clears PL_lex_repl, else we'll loop */
1667 PL_lex_state = LEX_INTERPCONCAT;
1677 PL_endwhite = newSVpvn("",0);
1678 sv_catsv(PL_endwhite, PL_thiswhite);
1682 sv_setpvn(PL_thistoken,"",0);
1684 PL_realtokenstart = -1;
1688 PL_bufend = SvPVX(PL_linestr);
1689 PL_bufend += SvCUR(PL_linestr);
1690 PL_expect = XOPERATOR;
1691 PL_sublex_info.sub_inwhat = 0;
1699 Extracts a pattern, double-quoted string, or transliteration. This
1702 It looks at PL_lex_inwhat and PL_lex_inpat to find out whether it's
1703 processing a pattern (PL_lex_inpat is true), a transliteration
1704 (PL_lex_inwhat == OP_TRANS is true), or a double-quoted string.
1706 Returns a pointer to the character scanned up to. If this is
1707 advanced from the start pointer supplied (i.e. if anything was
1708 successfully parsed), will leave an OP for the substring scanned
1709 in yylval. Caller must intuit reason for not parsing further
1710 by looking at the next characters herself.
1714 double-quoted style: \r and \n
1715 regexp special ones: \D \s
1718 case and quoting: \U \Q \E
1719 stops on @ and $, but not for $ as tail anchor
1721 In transliterations:
1722 characters are VERY literal, except for - not at the start or end
1723 of the string, which indicates a range. If the range is in bytes,
1724 scan_const expands the range to the full set of intermediate
1725 characters. If the range is in utf8, the hyphen is replaced with
1726 a certain range mark which will be handled by pmtrans() in op.c.
1728 In double-quoted strings:
1730 double-quoted style: \r and \n
1732 deprecated backrefs: \1 (in substitution replacements)
1733 case and quoting: \U \Q \E
1736 scan_const does *not* construct ops to handle interpolated strings.
1737 It stops processing as soon as it finds an embedded $ or @ variable
1738 and leaves it to the caller to work out what's going on.
1740 embedded arrays (whether in pattern or not) could be:
1741 @foo, @::foo, @'foo, @{foo}, @$foo, @+, @-.
1743 $ in double-quoted strings must be the symbol of an embedded scalar.
1745 $ in pattern could be $foo or could be tail anchor. Assumption:
1746 it's a tail anchor if $ is the last thing in the string, or if it's
1747 followed by one of "()| \r\n\t"
1749 \1 (backreferences) are turned into $1
1751 The structure of the code is
1752 while (there's a character to process) {
1753 handle transliteration ranges
1754 skip regexp comments /(?#comment)/ and codes /(?{code})/
1755 skip #-initiated comments in //x patterns
1756 check for embedded arrays
1757 check for embedded scalars
1759 leave intact backslashes from leaveit (below)
1760 deprecate \1 in substitution replacements
1761 handle string-changing backslashes \l \U \Q \E, etc.
1762 switch (what was escaped) {
1763 handle \- in a transliteration (becomes a literal -)
1764 handle \132 (octal characters)
1765 handle \x15 and \x{1234} (hex characters)
1766 handle \N{name} (named characters)
1767 handle \cV (control characters)
1768 handle printf-style backslashes (\f, \r, \n, etc)
1770 } (end if backslash)
1771 } (end while character to read)
1776 S_scan_const(pTHX_ char *start)
1779 register char *send = PL_bufend; /* end of the constant */
1780 SV *sv = newSV(send - start); /* sv for the constant */
1781 register char *s = start; /* start of the constant */
1782 register char *d = SvPVX(sv); /* destination for copies */
1783 bool dorange = FALSE; /* are we in a translit range? */
1784 bool didrange = FALSE; /* did we just finish a range? */
1785 I32 has_utf8 = FALSE; /* Output constant is UTF8 */
1786 I32 this_utf8 = UTF; /* The source string is assumed to be UTF8 */
1789 UV literal_endpoint = 0;
1790 bool native_range = TRUE; /* turned to FALSE if the first endpoint is Unicode. */
1793 const char * const leaveit = /* set of acceptably-backslashed characters */
1796 ? "\\.^$@AGZdDwWsSbBpPXC+*?|()-Nnrtfeaxcz0123456789[{]} \t\n\r\f\v#"
1799 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
1800 /* If we are doing a trans and we know we want UTF8 set expectation */
1801 has_utf8 = PL_sublex_info.sub_op->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF);
1802 this_utf8 = PL_sublex_info.sub_op->op_private & (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
1806 while (s < send || dorange) {
1807 /* get transliterations out of the way (they're most literal) */
1808 if (PL_lex_inwhat == OP_TRANS) {
1809 /* expand a range A-Z to the full set of characters. AIE! */
1811 I32 i; /* current expanded character */
1812 I32 min; /* first character in range */
1813 I32 max; /* last character in range */
1824 char * const c = (char*)utf8_hop((U8*)d, -1);
1828 *c = (char)UTF_TO_NATIVE(0xff);
1829 /* mark the range as done, and continue */
1835 i = d - SvPVX_const(sv); /* remember current offset */
1838 SvLEN(sv) + (has_utf8 ?
1839 (512 - UTF_CONTINUATION_MARK +
1842 /* How many two-byte within 0..255: 128 in UTF-8,
1843 * 96 in UTF-8-mod. */
1845 SvGROW(sv, SvLEN(sv) + 256); /* never more than 256 chars in a range */
1847 d = SvPVX(sv) + i; /* refresh d after realloc */
1851 for (j = 0; j <= 1; j++) {
1852 char * const c = (char*)utf8_hop((U8*)d, -1);
1853 const UV uv = utf8n_to_uvchr((U8*)c, d - c, NULL, 0);
1859 max = (U8)0xff; /* only to \xff */
1860 uvmax = uv; /* \x{100} to uvmax */
1862 d = c; /* eat endpoint chars */
1867 d -= 2; /* eat the first char and the - */
1868 min = (U8)*d; /* first char in range */
1869 max = (U8)d[1]; /* last char in range */
1876 "Invalid range \"%c-%c\" in transliteration operator",
1877 (char)min, (char)max);
1881 if (literal_endpoint == 2 &&
1882 ((isLOWER(min) && isLOWER(max)) ||
1883 (isUPPER(min) && isUPPER(max)))) {
1885 for (i = min; i <= max; i++)
1887 *d++ = NATIVE_TO_NEED(has_utf8,i);
1889 for (i = min; i <= max; i++)
1891 *d++ = NATIVE_TO_NEED(has_utf8,i);
1896 for (i = min; i <= max; i++)
1899 const U8 ch = (U8)NATIVE_TO_UTF(i);
1900 if (UNI_IS_INVARIANT(ch))
1903 *d++ = (U8)UTF8_EIGHT_BIT_HI(ch);
1904 *d++ = (U8)UTF8_EIGHT_BIT_LO(ch);
1913 d = (char*)uvchr_to_utf8((U8*)d, 0x100);
1915 *d++ = (char)UTF_TO_NATIVE(0xff);
1917 d = (char*)uvchr_to_utf8((U8*)d, uvmax);
1921 /* mark the range as done, and continue */
1925 literal_endpoint = 0;
1930 /* range begins (ignore - as first or last char) */
1931 else if (*s == '-' && s+1 < send && s != start) {
1933 Perl_croak(aTHX_ "Ambiguous range in transliteration operator");
1940 *d++ = (char)UTF_TO_NATIVE(0xff); /* use illegal utf8 byte--see pmtrans */
1950 literal_endpoint = 0;
1951 native_range = TRUE;
1956 /* if we get here, we're not doing a transliteration */
1958 /* skip for regexp comments /(?#comment)/ and code /(?{code})/,
1959 except for the last char, which will be done separately. */
1960 else if (*s == '(' && PL_lex_inpat && s[1] == '?') {
1962 while (s+1 < send && *s != ')')
1963 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1965 else if (s[2] == '{' /* This should match regcomp.c */
1966 || ((s[2] == 'p' || s[2] == '?') && s[3] == '{'))
1969 char *regparse = s + (s[2] == '{' ? 3 : 4);
1972 while (count && (c = *regparse)) {
1973 if (c == '\\' && regparse[1])
1981 if (*regparse != ')')
1982 regparse--; /* Leave one char for continuation. */
1983 while (s < regparse)
1984 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1988 /* likewise skip #-initiated comments in //x patterns */
1989 else if (*s == '#' && PL_lex_inpat &&
1990 ((PMOP*)PL_lex_inpat)->op_pmflags & PMf_EXTENDED) {
1991 while (s+1 < send && *s != '\n')
1992 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1995 /* check for embedded arrays
1996 (@foo, @::foo, @'foo, @{foo}, @$foo, @+, @-)
1998 else if (*s == '@' && s[1]) {
1999 if (isALNUM_lazy_if(s+1,UTF))
2001 if (strchr(":'{$", s[1]))
2003 if (!PL_lex_inpat && (s[1] == '+' || s[1] == '-'))
2004 break; /* in regexp, neither @+ nor @- are interpolated */
2007 /* check for embedded scalars. only stop if we're sure it's a
2010 else if (*s == '$') {
2011 if (!PL_lex_inpat) /* not a regexp, so $ must be var */
2013 if (s + 1 < send && !strchr("()| \r\n\t", s[1]))
2014 break; /* in regexp, $ might be tail anchor */
2017 /* End of else if chain - OP_TRANS rejoin rest */
2020 if (*s == '\\' && s+1 < send) {
2023 /* some backslashes we leave behind */
2024 if (*leaveit && *s && strchr(leaveit, *s)) {
2025 *d++ = NATIVE_TO_NEED(has_utf8,'\\');
2026 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
2030 /* deprecate \1 in strings and substitution replacements */
2031 if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat &&
2032 isDIGIT(*s) && *s != '0' && !isDIGIT(s[1]))
2034 if (ckWARN(WARN_SYNTAX))
2035 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "\\%c better written as $%c", *s, *s);
2040 /* string-change backslash escapes */
2041 if (PL_lex_inwhat != OP_TRANS && *s && strchr("lLuUEQ", *s)) {
2046 /* if we get here, it's either a quoted -, or a digit */
2049 /* quoted - in transliterations */
2051 if (PL_lex_inwhat == OP_TRANS) {
2058 if ((isALPHA(*s) || isDIGIT(*s)) &&
2060 Perl_warner(aTHX_ packWARN(WARN_MISC),
2061 "Unrecognized escape \\%c passed through",
2063 /* default action is to copy the quoted character */
2064 goto default_action;
2067 /* \132 indicates an octal constant */
2068 case '0': case '1': case '2': case '3':
2069 case '4': case '5': case '6': case '7':
2073 uv = grok_oct(s, &len, &flags, NULL);
2076 goto NUM_ESCAPE_INSERT;
2078 /* \x24 indicates a hex constant */
2082 char* const e = strchr(s, '}');
2083 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
2084 PERL_SCAN_DISALLOW_PREFIX;
2089 yyerror("Missing right brace on \\x{}");
2093 uv = grok_hex(s, &len, &flags, NULL);
2099 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
2100 uv = grok_hex(s, &len, &flags, NULL);
2106 /* Insert oct or hex escaped character.
2107 * There will always enough room in sv since such
2108 * escapes will be longer than any UTF-8 sequence
2109 * they can end up as. */
2111 /* We need to map to chars to ASCII before doing the tests
2114 if (!UNI_IS_INVARIANT(NATIVE_TO_UNI(uv))) {
2115 if (!has_utf8 && uv > 255) {
2116 /* Might need to recode whatever we have
2117 * accumulated so far if it contains any
2120 * (Can't we keep track of that and avoid
2121 * this rescan? --jhi)
2125 for (c = (U8 *) SvPVX(sv); c < (U8 *)d; c++) {
2126 if (!NATIVE_IS_INVARIANT(*c)) {
2131 const STRLEN offset = d - SvPVX_const(sv);
2133 d = SvGROW(sv, SvLEN(sv) + hicount + 1) + offset;
2137 while (src >= (const U8 *)SvPVX_const(sv)) {
2138 if (!NATIVE_IS_INVARIANT(*src)) {
2139 const U8 ch = NATIVE_TO_ASCII(*src);
2140 *dst-- = (U8)UTF8_EIGHT_BIT_LO(ch);
2141 *dst-- = (U8)UTF8_EIGHT_BIT_HI(ch);
2151 if (has_utf8 || uv > 255) {
2152 d = (char*)uvchr_to_utf8((U8*)d, uv);
2154 if (PL_lex_inwhat == OP_TRANS &&
2155 PL_sublex_info.sub_op) {
2156 PL_sublex_info.sub_op->op_private |=
2157 (PL_lex_repl ? OPpTRANS_FROM_UTF
2161 if (uv > 255 && !dorange)
2162 native_range = FALSE;
2174 /* \N{LATIN SMALL LETTER A} is a named character */
2178 char* e = strchr(s, '}');
2185 yyerror("Missing right brace on \\N{}");
2189 if (e > s + 2 && s[1] == 'U' && s[2] == '+') {
2191 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
2192 PERL_SCAN_DISALLOW_PREFIX;
2195 uv = grok_hex(s, &len, &flags, NULL);
2196 if ( len != e - s ) {
2200 goto NUM_ESCAPE_INSERT;
2202 res = newSVpvn(s + 1, e - s - 1);
2203 type = newSVpvn(s - 2,e - s + 3);
2204 res = new_constant( NULL, 0, "charnames",
2205 res, NULL, SvPVX(type) );
2208 sv_utf8_upgrade(res);
2209 str = SvPV_const(res,len);
2210 #ifdef EBCDIC_NEVER_MIND
2211 /* charnames uses pack U and that has been
2212 * recently changed to do the below uni->native
2213 * mapping, so this would be redundant (and wrong,
2214 * the code point would be doubly converted).
2215 * But leave this in just in case the pack U change
2216 * gets revoked, but the semantics is still
2217 * desireable for charnames. --jhi */
2219 UV uv = utf8_to_uvchr((const U8*)str, 0);
2222 U8 tmpbuf[UTF8_MAXBYTES+1], *d;
2224 d = uvchr_to_utf8(tmpbuf, UNI_TO_NATIVE(uv));
2225 sv_setpvn(res, (char *)tmpbuf, d - tmpbuf);
2226 str = SvPV_const(res, len);
2230 if (!has_utf8 && SvUTF8(res)) {
2231 const char * const ostart = SvPVX_const(sv);
2232 SvCUR_set(sv, d - ostart);
2235 sv_utf8_upgrade(sv);
2236 /* this just broke our allocation above... */
2237 SvGROW(sv, (STRLEN)(send - start));
2238 d = SvPVX(sv) + SvCUR(sv);
2241 if (len > (STRLEN)(e - s + 4)) { /* I _guess_ 4 is \N{} --jhi */
2242 const char * const odest = SvPVX_const(sv);
2244 SvGROW(sv, (SvLEN(sv) + len - (e - s + 4)));
2245 d = SvPVX(sv) + (d - odest);
2249 native_range = FALSE; /* \N{} is guessed to be Unicode */
2251 Copy(str, d, len, char);
2258 yyerror("Missing braces on \\N{}");
2261 /* \c is a control character */
2270 *d++ = NATIVE_TO_NEED(has_utf8,toCTRL(c));
2273 yyerror("Missing control char name in \\c");
2277 /* printf-style backslashes, formfeeds, newlines, etc */
2279 *d++ = NATIVE_TO_NEED(has_utf8,'\b');
2282 *d++ = NATIVE_TO_NEED(has_utf8,'\n');
2285 *d++ = NATIVE_TO_NEED(has_utf8,'\r');
2288 *d++ = NATIVE_TO_NEED(has_utf8,'\f');
2291 *d++ = NATIVE_TO_NEED(has_utf8,'\t');
2294 *d++ = ASCII_TO_NEED(has_utf8,'\033');
2297 *d++ = ASCII_TO_NEED(has_utf8,'\007');
2303 } /* end if (backslash) */
2310 /* If we started with encoded form, or already know we want it
2311 and then encode the next character */
2312 if ((has_utf8 || this_utf8) && !NATIVE_IS_INVARIANT((U8)(*s))) {
2314 const UV nextuv = (this_utf8) ? utf8n_to_uvchr((U8*)s, send - s, &len, 0) : (UV) ((U8) *s);
2315 const STRLEN need = UNISKIP(NATIVE_TO_UNI(nextuv));
2318 /* encoded value larger than old, need extra space (NOTE: SvCUR() not set here) */
2319 const STRLEN off = d - SvPVX_const(sv);
2320 d = SvGROW(sv, SvLEN(sv) + (need-len)) + off;
2322 d = (char*)uvchr_to_utf8((U8*)d, nextuv);
2325 if (uv > 255 && !dorange)
2326 native_range = FALSE;
2330 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
2332 } /* while loop to process each character */
2334 /* terminate the string and set up the sv */
2336 SvCUR_set(sv, d - SvPVX_const(sv));
2337 if (SvCUR(sv) >= SvLEN(sv))
2338 Perl_croak(aTHX_ "panic: constant overflowed allocated space");
2341 if (PL_encoding && !has_utf8) {
2342 sv_recode_to_utf8(sv, PL_encoding);
2348 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
2349 PL_sublex_info.sub_op->op_private |=
2350 (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
2354 /* shrink the sv if we allocated more than we used */
2355 if (SvCUR(sv) + 5 < SvLEN(sv)) {
2356 SvPV_shrink_to_cur(sv);
2359 /* return the substring (via yylval) only if we parsed anything */
2360 if (s > PL_bufptr) {
2361 if ( PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ) )
2362 sv = new_constant(start, s - start,
2363 (const char *)(PL_lex_inpat ? "qr" : "q"),
2366 (( PL_lex_inwhat == OP_TRANS
2368 : ( (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat)
2371 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
2378 * Returns TRUE if there's more to the expression (e.g., a subscript),
2381 * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
2383 * ->[ and ->{ return TRUE
2384 * { and [ outside a pattern are always subscripts, so return TRUE
2385 * if we're outside a pattern and it's not { or [, then return FALSE
2386 * if we're in a pattern and the first char is a {
2387 * {4,5} (any digits around the comma) returns FALSE
2388 * if we're in a pattern and the first char is a [
2390 * [SOMETHING] has a funky algorithm to decide whether it's a
2391 * character class or not. It has to deal with things like
2392 * /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
2393 * anything else returns TRUE
2396 /* This is the one truly awful dwimmer necessary to conflate C and sed. */
2399 S_intuit_more(pTHX_ register char *s)
2402 if (PL_lex_brackets)
2404 if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
2406 if (*s != '{' && *s != '[')
2411 /* In a pattern, so maybe we have {n,m}. */
2428 /* On the other hand, maybe we have a character class */
2431 if (*s == ']' || *s == '^')
2434 /* this is terrifying, and it works */
2435 int weight = 2; /* let's weigh the evidence */
2437 unsigned char un_char = 255, last_un_char;
2438 const char * const send = strchr(s,']');
2439 char tmpbuf[sizeof PL_tokenbuf * 4];
2441 if (!send) /* has to be an expression */
2444 Zero(seen,256,char);
2447 else if (isDIGIT(*s)) {
2449 if (isDIGIT(s[1]) && s[2] == ']')
2455 for (; s < send; s++) {
2456 last_un_char = un_char;
2457 un_char = (unsigned char)*s;
2462 weight -= seen[un_char] * 10;
2463 if (isALNUM_lazy_if(s+1,UTF)) {
2465 scan_ident(s, send, tmpbuf, sizeof tmpbuf, FALSE);
2466 len = (int)strlen(tmpbuf);
2467 if (len > 1 && gv_fetchpvn_flags(tmpbuf, len, 0, SVt_PV))
2472 else if (*s == '$' && s[1] &&
2473 strchr("[#!%*<>()-=",s[1])) {
2474 if (/*{*/ strchr("])} =",s[2]))
2483 if (strchr("wds]",s[1]))
2485 else if (seen[(U8)'\''] || seen[(U8)'"'])
2487 else if (strchr("rnftbxcav",s[1]))
2489 else if (isDIGIT(s[1])) {
2491 while (s[1] && isDIGIT(s[1]))
2501 if (strchr("aA01! ",last_un_char))
2503 if (strchr("zZ79~",s[1]))
2505 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
2506 weight -= 5; /* cope with negative subscript */
2509 if (!isALNUM(last_un_char)
2510 && !(last_un_char == '$' || last_un_char == '@'
2511 || last_un_char == '&')
2512 && isALPHA(*s) && s[1] && isALPHA(s[1])) {
2517 if (keyword(tmpbuf, d - tmpbuf))
2520 if (un_char == last_un_char + 1)
2522 weight -= seen[un_char];
2527 if (weight >= 0) /* probably a character class */
2537 * Does all the checking to disambiguate
2539 * between foo(bar) and bar->foo. Returns 0 if not a method, otherwise
2540 * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
2542 * First argument is the stuff after the first token, e.g. "bar".
2544 * Not a method if bar is a filehandle.
2545 * Not a method if foo is a subroutine prototyped to take a filehandle.
2546 * Not a method if it's really "Foo $bar"
2547 * Method if it's "foo $bar"
2548 * Not a method if it's really "print foo $bar"
2549 * Method if it's really "foo package::" (interpreted as package->foo)
2550 * Not a method if bar is known to be a subroutine ("sub bar; foo bar")
2551 * Not a method if bar is a filehandle or package, but is quoted with
2556 S_intuit_method(pTHX_ char *start, GV *gv, CV *cv)
2559 char *s = start + (*start == '$');
2560 char tmpbuf[sizeof PL_tokenbuf];
2568 if (SvTYPE(gv) == SVt_PVGV && GvIO(gv))
2572 const char *proto = SvPVX_const(cv);
2583 s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
2584 /* start is the beginning of the possible filehandle/object,
2585 * and s is the end of it
2586 * tmpbuf is a copy of it
2589 if (*start == '$') {
2590 if (gv || PL_last_lop_op == OP_PRINT || isUPPER(*PL_tokenbuf))
2593 len = start - SvPVX(PL_linestr);
2597 start = SvPVX(PL_linestr) + len;
2601 return *s == '(' ? FUNCMETH : METHOD;
2603 if (!keyword(tmpbuf, len)) {
2604 if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
2608 soff = s - SvPVX(PL_linestr);
2612 indirgv = gv_fetchpvn_flags(tmpbuf, len, 0, SVt_PVCV);
2613 if (indirgv && GvCVu(indirgv))
2615 /* filehandle or package name makes it a method */
2616 if (!gv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, FALSE)) {
2618 soff = s - SvPVX(PL_linestr);
2621 if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
2622 return 0; /* no assumptions -- "=>" quotes bearword */
2624 start_force(PL_curforce);
2625 NEXTVAL_NEXTTOKE.opval = (OP*)newSVOP(OP_CONST, 0,
2626 newSVpvn(tmpbuf,len));
2627 NEXTVAL_NEXTTOKE.opval->op_private = OPpCONST_BARE;
2629 curmad('X', newSVpvn(start,SvPVX(PL_linestr) + soff - start));
2634 PL_bufptr = SvPVX(PL_linestr) + soff; /* restart before space */
2636 return *s == '(' ? FUNCMETH : METHOD;
2644 * Return a string of Perl code to load the debugger. If PERL5DB
2645 * is set, it will return the contents of that, otherwise a
2646 * compile-time require of perl5db.pl.
2654 const char * const pdb = PerlEnv_getenv("PERL5DB");
2658 SETERRNO(0,SS_NORMAL);
2659 return "BEGIN { require 'perl5db.pl' }";
2665 /* Encoded script support. filter_add() effectively inserts a
2666 * 'pre-processing' function into the current source input stream.
2667 * Note that the filter function only applies to the current source file
2668 * (e.g., it will not affect files 'require'd or 'use'd by this one).
2670 * The datasv parameter (which may be NULL) can be used to pass
2671 * private data to this instance of the filter. The filter function
2672 * can recover the SV using the FILTER_DATA macro and use it to
2673 * store private buffers and state information.
2675 * The supplied datasv parameter is upgraded to a PVIO type
2676 * and the IoDIRP/IoANY field is used to store the function pointer,
2677 * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
2678 * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
2679 * private use must be set using malloc'd pointers.
2683 Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
2689 if (!PL_rsfp_filters)
2690 PL_rsfp_filters = newAV();
2693 SvUPGRADE(datasv, SVt_PVIO);
2694 IoANY(datasv) = FPTR2DPTR(void *, funcp); /* stash funcp into spare field */
2695 IoFLAGS(datasv) |= IOf_FAKE_DIRP;
2696 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
2697 FPTR2DPTR(void *, IoANY(datasv)),
2698 SvPV_nolen(datasv)));
2699 av_unshift(PL_rsfp_filters, 1);
2700 av_store(PL_rsfp_filters, 0, datasv) ;
2705 /* Delete most recently added instance of this filter function. */
2707 Perl_filter_del(pTHX_ filter_t funcp)
2713 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p",
2714 FPTR2DPTR(void*, funcp)));
2716 if (!PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
2718 /* if filter is on top of stack (usual case) just pop it off */
2719 datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
2720 if (IoANY(datasv) == FPTR2DPTR(void *, funcp)) {
2721 IoFLAGS(datasv) &= ~IOf_FAKE_DIRP;
2722 IoANY(datasv) = (void *)NULL;
2723 sv_free(av_pop(PL_rsfp_filters));
2727 /* we need to search for the correct entry and clear it */
2728 Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
2732 /* Invoke the idxth filter function for the current rsfp. */
2733 /* maxlen 0 = read one text line */
2735 Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
2740 /* This API is bad. It should have been using unsigned int for maxlen.
2741 Not sure if we want to change the API, but if not we should sanity
2742 check the value here. */
2743 const unsigned int correct_length
2752 if (!PL_rsfp_filters)
2754 if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */
2755 /* Provide a default input filter to make life easy. */
2756 /* Note that we append to the line. This is handy. */
2757 DEBUG_P(PerlIO_printf(Perl_debug_log,
2758 "filter_read %d: from rsfp\n", idx));
2759 if (correct_length) {
2762 const int old_len = SvCUR(buf_sv);
2764 /* ensure buf_sv is large enough */
2765 SvGROW(buf_sv, (STRLEN)(old_len + correct_length)) ;
2766 if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len,
2767 correct_length)) <= 0) {
2768 if (PerlIO_error(PL_rsfp))
2769 return -1; /* error */
2771 return 0 ; /* end of file */
2773 SvCUR_set(buf_sv, old_len + len) ;
2776 if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
2777 if (PerlIO_error(PL_rsfp))
2778 return -1; /* error */
2780 return 0 ; /* end of file */
2783 return SvCUR(buf_sv);
2785 /* Skip this filter slot if filter has been deleted */
2786 if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) {
2787 DEBUG_P(PerlIO_printf(Perl_debug_log,
2788 "filter_read %d: skipped (filter deleted)\n",
2790 return FILTER_READ(idx+1, buf_sv, correct_length); /* recurse */
2792 /* Get function pointer hidden within datasv */
2793 funcp = DPTR2FPTR(filter_t, IoANY(datasv));
2794 DEBUG_P(PerlIO_printf(Perl_debug_log,
2795 "filter_read %d: via function %p (%s)\n",
2796 idx, (void*)datasv, SvPV_nolen_const(datasv)));
2797 /* Call function. The function is expected to */
2798 /* call "FILTER_READ(idx+1, buf_sv)" first. */
2799 /* Return: <0:error, =0:eof, >0:not eof */
2800 return (*funcp)(aTHX_ idx, buf_sv, correct_length);
2804 S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append)
2807 #ifdef PERL_CR_FILTER
2808 if (!PL_rsfp_filters) {
2809 filter_add(S_cr_textfilter,NULL);
2812 if (PL_rsfp_filters) {
2814 SvCUR_set(sv, 0); /* start with empty line */
2815 if (FILTER_READ(0, sv, 0) > 0)
2816 return ( SvPVX(sv) ) ;
2821 return (sv_gets(sv, fp, append));
2825 S_find_in_my_stash(pTHX_ const char *pkgname, I32 len)
2830 if (len == 11 && *pkgname == '_' && strEQ(pkgname, "__PACKAGE__"))
2834 (pkgname[len - 2] == ':' && pkgname[len - 1] == ':') &&
2835 (gv = gv_fetchpvn_flags(pkgname, len, 0, SVt_PVHV)))
2837 return GvHV(gv); /* Foo:: */
2840 /* use constant CLASS => 'MyClass' */
2841 gv = gv_fetchpvn_flags(pkgname, len, 0, SVt_PVCV);
2842 if (gv && GvCV(gv)) {
2843 SV * const sv = cv_const_sv(GvCV(gv));
2845 pkgname = SvPV_nolen_const(sv);
2848 return gv_stashpv(pkgname, FALSE);
2854 * The intent of this yylex wrapper is to minimize the changes to the
2855 * tokener when we aren't interested in collecting madprops. It remains
2856 * to be seen how successful this strategy will be...
2863 char *s = PL_bufptr;
2865 /* make sure PL_thiswhite is initialized */
2869 /* just do what yylex would do on pending identifier; leave PL_thiswhite alone */
2870 if (PL_pending_ident)
2871 return S_pending_ident(aTHX);
2873 /* previous token ate up our whitespace? */
2874 if (!PL_lasttoke && PL_nextwhite) {
2875 PL_thiswhite = PL_nextwhite;
2879 /* isolate the token, and figure out where it is without whitespace */
2880 PL_realtokenstart = -1;
2884 assert(PL_curforce < 0);
2886 if (!PL_thismad || PL_thismad->mad_key == '^') { /* not forced already? */
2887 if (!PL_thistoken) {
2888 if (PL_realtokenstart < 0 || !CopLINE(PL_curcop))
2889 PL_thistoken = newSVpvn("",0);
2891 char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
2892 PL_thistoken = newSVpvn(tstart, s - tstart);
2895 if (PL_thismad) /* install head */
2896 CURMAD('X', PL_thistoken);
2899 /* last whitespace of a sublex? */
2900 if (optype == ')' && PL_endwhite) {
2901 CURMAD('X', PL_endwhite);
2906 /* if no whitespace and we're at EOF, bail. Otherwise fake EOF below. */
2907 if (!PL_thiswhite && !PL_endwhite && !optype) {
2908 sv_free(PL_thistoken);
2913 /* put off final whitespace till peg */
2914 if (optype == ';' && !PL_rsfp) {
2915 PL_nextwhite = PL_thiswhite;
2918 else if (PL_thisopen) {
2919 CURMAD('q', PL_thisopen);
2921 sv_free(PL_thistoken);
2925 /* Store actual token text as madprop X */
2926 CURMAD('X', PL_thistoken);
2930 /* add preceding whitespace as madprop _ */
2931 CURMAD('_', PL_thiswhite);
2935 /* add quoted material as madprop = */
2936 CURMAD('=', PL_thisstuff);
2940 /* add terminating quote as madprop Q */
2941 CURMAD('Q', PL_thisclose);
2945 /* special processing based on optype */
2949 /* opval doesn't need a TOKEN since it can already store mp */
2960 append_madprops(PL_thismad, yylval.opval, 0);
2968 addmad(newMADsv('p', PL_endwhite), &PL_thismad, 0);
2977 /* remember any fake bracket that lexer is about to discard */
2978 if (PL_lex_brackets == 1 &&
2979 ((expectation)PL_lex_brackstack[0] & XFAKEBRACK))
2982 while (s < PL_bufend && (*s == ' ' || *s == '\t'))
2985 PL_thiswhite = newSVpvn(PL_bufptr, ++s - PL_bufptr);
2986 addmad(newMADsv('#', PL_thiswhite), &PL_thismad, 0);
2989 break; /* don't bother looking for trailing comment */
2998 /* attach a trailing comment to its statement instead of next token */
3002 if (PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == optype) {
3004 while (s < PL_bufend && (*s == ' ' || *s == '\t'))
3006 if (*s == '\n' || *s == '#') {
3007 while (s < PL_bufend && *s != '\n')
3011 PL_thiswhite = newSVpvn(PL_bufptr, s - PL_bufptr);
3012 addmad(newMADsv('#', PL_thiswhite), &PL_thismad, 0);
3029 /* Create new token struct. Note: opvals return early above. */
3030 yylval.tkval = newTOKEN(optype, yylval, PL_thismad);
3037 S_tokenize_use(pTHX_ int is_use, char *s) {
3039 if (PL_expect != XSTATE)
3040 yyerror(Perl_form(aTHX_ "\"%s\" not allowed in expression",
3041 is_use ? "use" : "no"));
3043 if (isDIGIT(*s) || (*s == 'v' && isDIGIT(s[1]))) {
3044 s = force_version(s, TRUE);
3045 if (*s == ';' || (s = SKIPSPACE1(s), *s == ';')) {
3046 start_force(PL_curforce);
3047 NEXTVAL_NEXTTOKE.opval = NULL;
3050 else if (*s == 'v') {
3051 s = force_word(s,WORD,FALSE,TRUE,FALSE);
3052 s = force_version(s, FALSE);
3056 s = force_word(s,WORD,FALSE,TRUE,FALSE);
3057 s = force_version(s, FALSE);
3059 yylval.ival = is_use;
3063 static const char* const exp_name[] =
3064 { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
3065 "ATTRTERM", "TERMBLOCK", "TERMORDORDOR"
3072 Works out what to call the token just pulled out of the input
3073 stream. The yacc parser takes care of taking the ops we return and
3074 stitching them into a tree.
3080 if read an identifier
3081 if we're in a my declaration
3082 croak if they tried to say my($foo::bar)
3083 build the ops for a my() declaration
3084 if it's an access to a my() variable
3085 are we in a sort block?
3086 croak if my($a); $a <=> $b
3087 build ops for access to a my() variable
3088 if in a dq string, and they've said @foo and we can't find @foo
3090 build ops for a bareword
3091 if we already built the token before, use it.
3096 #pragma segment Perl_yylex
3102 register char *s = PL_bufptr;
3107 /* orig_keyword, gvp, and gv are initialized here because
3108 * jump to the label just_a_word_zero can bypass their
3109 * initialization later. */
3110 I32 orig_keyword = 0;
3115 SV* tmp = newSVpvs("");
3116 PerlIO_printf(Perl_debug_log, "### %"IVdf":LEX_%s/X%s %s\n",
3117 (IV)CopLINE(PL_curcop),
3118 lex_state_names[PL_lex_state],
3119 exp_name[PL_expect],
3120 pv_display(tmp, s, strlen(s), 0, 60));
3123 /* check if there's an identifier for us to look at */
3124 if (PL_pending_ident)
3125 return REPORT(S_pending_ident(aTHX));
3127 /* no identifier pending identification */
3129 switch (PL_lex_state) {
3131 case LEX_NORMAL: /* Some compilers will produce faster */
3132 case LEX_INTERPNORMAL: /* code if we comment these out. */
3136 /* when we've already built the next token, just pull it out of the queue */
3140 yylval = PL_nexttoke[PL_lasttoke].next_val;
3142 PL_thismad = PL_nexttoke[PL_lasttoke].next_mad;
3143 PL_nexttoke[PL_lasttoke].next_mad = 0;
3144 if (PL_thismad && PL_thismad->mad_key == '_') {
3145 PL_thiswhite = (SV*)PL_thismad->mad_val;
3146 PL_thismad->mad_val = 0;
3147 mad_free(PL_thismad);
3152 PL_lex_state = PL_lex_defer;
3153 PL_expect = PL_lex_expect;
3154 PL_lex_defer = LEX_NORMAL;
3155 if (!PL_nexttoke[PL_lasttoke].next_type)
3160 yylval = PL_nextval[PL_nexttoke];
3162 PL_lex_state = PL_lex_defer;
3163 PL_expect = PL_lex_expect;
3164 PL_lex_defer = LEX_NORMAL;
3168 /* FIXME - can these be merged? */
3169 return(PL_nexttoke[PL_lasttoke].next_type);
3171 return REPORT(PL_nexttype[PL_nexttoke]);
3174 /* interpolated case modifiers like \L \U, including \Q and \E.
3175 when we get here, PL_bufptr is at the \
3177 case LEX_INTERPCASEMOD:
3179 if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
3180 Perl_croak(aTHX_ "panic: INTERPCASEMOD");
3182 /* handle \E or end of string */
3183 if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
3185 if (PL_lex_casemods) {
3186 const char oldmod = PL_lex_casestack[--PL_lex_casemods];
3187 PL_lex_casestack[PL_lex_casemods] = '\0';
3189 if (PL_bufptr != PL_bufend
3190 && (oldmod == 'L' || oldmod == 'U' || oldmod == 'Q')) {
3192 PL_lex_state = LEX_INTERPCONCAT;
3195 PL_thistoken = newSVpvn("\\E",2);
3201 while (PL_bufptr != PL_bufend &&
3202 PL_bufptr[0] == '\\' && PL_bufptr[1] == 'E') {
3204 PL_thiswhite = newSVpvn("",0);
3205 sv_catpvn(PL_thiswhite, PL_bufptr, 2);
3209 if (PL_bufptr != PL_bufend)
3212 PL_lex_state = LEX_INTERPCONCAT;
3216 DEBUG_T({ PerlIO_printf(Perl_debug_log,
3217 "### Saw case modifier\n"); });
3219 if (s[1] == '\\' && s[2] == 'E') {
3222 PL_thiswhite = newSVpvn("",0);
3223 sv_catpvn(PL_thiswhite, PL_bufptr, 4);
3226 PL_lex_state = LEX_INTERPCONCAT;
3231 if (!PL_madskills) /* when just compiling don't need correct */
3232 if (strnEQ(s, "L\\u", 3) || strnEQ(s, "U\\l", 3))
3233 tmp = *s, *s = s[2], s[2] = (char)tmp; /* misordered... */
3234 if ((*s == 'L' || *s == 'U') &&
3235 (strchr(PL_lex_casestack, 'L') || strchr(PL_lex_casestack, 'U'))) {
3236 PL_lex_casestack[--PL_lex_casemods] = '\0';
3239 if (PL_lex_casemods > 10)
3240 Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
3241 PL_lex_casestack[PL_lex_casemods++] = *s;
3242 PL_lex_casestack[PL_lex_casemods] = '\0';
3243 PL_lex_state = LEX_INTERPCONCAT;
3244 start_force(PL_curforce);
3245 NEXTVAL_NEXTTOKE.ival = 0;
3247 start_force(PL_curforce);
3249 NEXTVAL_NEXTTOKE.ival = OP_LCFIRST;
3251 NEXTVAL_NEXTTOKE.ival = OP_UCFIRST;
3253 NEXTVAL_NEXTTOKE.ival = OP_LC;
3255 NEXTVAL_NEXTTOKE.ival = OP_UC;
3257 NEXTVAL_NEXTTOKE.ival = OP_QUOTEMETA;
3259 Perl_croak(aTHX_ "panic: yylex");
3261 SV* const tmpsv = newSVpvn("",0);
3262 Perl_sv_catpvf(aTHX_ tmpsv, "\\%c", *s);
3268 if (PL_lex_starts) {
3274 sv_free(PL_thistoken);
3275 PL_thistoken = newSVpvn("",0);
3278 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
3279 if (PL_lex_casemods == 1 && PL_lex_inpat)
3288 case LEX_INTERPPUSH:
3289 return REPORT(sublex_push());
3291 case LEX_INTERPSTART:
3292 if (PL_bufptr == PL_bufend)
3293 return REPORT(sublex_done());
3294 DEBUG_T({ PerlIO_printf(Perl_debug_log,
3295 "### Interpolated variable\n"); });
3297 PL_lex_dojoin = (*PL_bufptr == '@');
3298 PL_lex_state = LEX_INTERPNORMAL;
3299 if (PL_lex_dojoin) {
3300 start_force(PL_curforce);
3301 NEXTVAL_NEXTTOKE.ival = 0;
3303 start_force(PL_curforce);
3304 force_ident("\"", '$');
3305 start_force(PL_curforce);
3306 NEXTVAL_NEXTTOKE.ival = 0;
3308 start_force(PL_curforce);
3309 NEXTVAL_NEXTTOKE.ival = 0;
3311 start_force(PL_curforce);
3312 NEXTVAL_NEXTTOKE.ival = OP_JOIN; /* emulate join($", ...) */
3315 if (PL_lex_starts++) {
3320 sv_free(PL_thistoken);
3321 PL_thistoken = newSVpvn("",0);
3324 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
3325 if (!PL_lex_casemods && PL_lex_inpat)
3332 case LEX_INTERPENDMAYBE:
3333 if (intuit_more(PL_bufptr)) {
3334 PL_lex_state = LEX_INTERPNORMAL; /* false alarm, more expr */
3340 if (PL_lex_dojoin) {
3341 PL_lex_dojoin = FALSE;
3342 PL_lex_state = LEX_INTERPCONCAT;
3346 sv_free(PL_thistoken);
3347 PL_thistoken = newSVpvn("",0);
3352 if (PL_lex_inwhat == OP_SUBST && PL_linestr == PL_lex_repl
3353 && SvEVALED(PL_lex_repl))
3355 if (PL_bufptr != PL_bufend)
3356 Perl_croak(aTHX_ "Bad evalled substitution pattern");
3360 case LEX_INTERPCONCAT:
3362 if (PL_lex_brackets)
3363 Perl_croak(aTHX_ "panic: INTERPCONCAT");
3365 if (PL_bufptr == PL_bufend)
3366 return REPORT(sublex_done());
3368 if (SvIVX(PL_linestr) == '\'') {
3369 SV *sv = newSVsv(PL_linestr);
3372 else if ( PL_hints & HINT_NEW_RE )
3373 sv = new_constant(NULL, 0, "qr", sv, sv, "q");
3374 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
3378 s = scan_const(PL_bufptr);
3380 PL_lex_state = LEX_INTERPCASEMOD;
3382 PL_lex_state = LEX_INTERPSTART;
3385 if (s != PL_bufptr) {
3386 start_force(PL_curforce);
3388 curmad('X', newSVpvn(PL_bufptr,s-PL_bufptr));
3390 NEXTVAL_NEXTTOKE = yylval;
3393 if (PL_lex_starts++) {
3397 sv_free(PL_thistoken);
3398 PL_thistoken = newSVpvn("",0);
3401 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
3402 if (!PL_lex_casemods && PL_lex_inpat)
3415 PL_lex_state = LEX_NORMAL;
3416 s = scan_formline(PL_bufptr);
3417 if (!PL_lex_formbrack)
3423 PL_oldoldbufptr = PL_oldbufptr;
3429 sv_free(PL_thistoken);
3432 PL_realtokenstart = s - SvPVX(PL_linestr); /* assume but undo on ws */
3436 if (isIDFIRST_lazy_if(s,UTF))
3438 Perl_croak(aTHX_ "Unrecognized character \\x%02X", *s & 255);
3441 goto fake_eof; /* emulate EOF on ^D or ^Z */
3450 if (PL_lex_brackets) {
3451 yyerror((const char *)
3453 ? "Format not terminated"
3454 : "Missing right curly or square bracket"));
3456 DEBUG_T( { PerlIO_printf(Perl_debug_log,
3457 "### Tokener got EOF\n");
3461 if (s++ < PL_bufend)
3462 goto retry; /* ignore stray nulls */
3465 if (!PL_in_eval && !PL_preambled) {
3466 PL_preambled = TRUE;
3471 sv_setpv(PL_linestr,incl_perldb());
3472 if (SvCUR(PL_linestr))
3473 sv_catpvs(PL_linestr,";");
3475 while(AvFILLp(PL_preambleav) >= 0) {
3476 SV *tmpsv = av_shift(PL_preambleav);
3477 sv_catsv(PL_linestr, tmpsv);
3478 sv_catpvs(PL_linestr, ";");
3481 sv_free((SV*)PL_preambleav);
3482 PL_preambleav = NULL;
3484 if (PL_minus_n || PL_minus_p) {
3485 sv_catpvs(PL_linestr, "LINE: while (<>) {");
3487 sv_catpvs(PL_linestr,"chomp;");
3490 if ((*PL_splitstr == '/' || *PL_splitstr == '\''
3491 || *PL_splitstr == '"')
3492 && strchr(PL_splitstr + 1, *PL_splitstr))
3493 Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
3495 /* "q\0${splitstr}\0" is legal perl. Yes, even NUL
3496 bytes can be used as quoting characters. :-) */
3497 const char *splits = PL_splitstr;
3498 sv_catpvs(PL_linestr, "our @F=split(q\0");
3501 if (*splits == '\\')
3502 sv_catpvn(PL_linestr, splits, 1);
3503 sv_catpvn(PL_linestr, splits, 1);
3504 } while (*splits++);
3505 /* This loop will embed the trailing NUL of
3506 PL_linestr as the last thing it does before
3508 sv_catpvs(PL_linestr, ");");
3512 sv_catpvs(PL_linestr,"our @F=split(' ');");
3516 sv_catpvs(PL_linestr,"use feature ':5.10';");
3517 sv_catpvs(PL_linestr, "\n");
3518 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
3519 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3520 PL_last_lop = PL_last_uni = NULL;
3521 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3522 SV * const sv = newSV(0);
3524 sv_upgrade(sv, SVt_PVMG);
3525 sv_setsv(sv,PL_linestr);
3528 av_store(CopFILEAVx(PL_curcop),(I32)CopLINE(PL_curcop),sv);
3533 bof = PL_rsfp ? TRUE : FALSE;
3534 if ((s = filter_gets(PL_linestr, PL_rsfp, 0)) == NULL) {
3537 PL_realtokenstart = -1;
3540 if (PL_preprocess && !PL_in_eval)
3541 (void)PerlProc_pclose(PL_rsfp);
3542 else if ((PerlIO *)PL_rsfp == PerlIO_stdin())
3543 PerlIO_clearerr(PL_rsfp);
3545 (void)PerlIO_close(PL_rsfp);
3547 PL_doextract = FALSE;
3549 if (!PL_in_eval && (PL_minus_n || PL_minus_p)) {
3554 sv_setpv(PL_linestr,
3557 ? ";}continue{print;}" : ";}"));
3558 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
3559 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3560 PL_last_lop = PL_last_uni = NULL;
3561 PL_minus_n = PL_minus_p = 0;
3564 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
3565 PL_last_lop = PL_last_uni = NULL;
3566 sv_setpvn(PL_linestr,"",0);
3567 TOKEN(';'); /* not infinite loop because rsfp is NULL now */
3569 /* If it looks like the start of a BOM or raw UTF-16,
3570 * check if it in fact is. */
3576 #ifdef PERLIO_IS_STDIO
3577 # ifdef __GNU_LIBRARY__
3578 # if __GNU_LIBRARY__ == 1 /* Linux glibc5 */
3579 # define FTELL_FOR_PIPE_IS_BROKEN
3583 # if __GLIBC__ == 1 /* maybe some glibc5 release had it like this? */
3584 # define FTELL_FOR_PIPE_IS_BROKEN
3589 #ifdef FTELL_FOR_PIPE_IS_BROKEN
3590 /* This loses the possibility to detect the bof
3591 * situation on perl -P when the libc5 is being used.
3592 * Workaround? Maybe attach some extra state to PL_rsfp?
3595 bof = PerlIO_tell(PL_rsfp) == SvCUR(PL_linestr);
3597 bof = PerlIO_tell(PL_rsfp) == (Off_t)SvCUR(PL_linestr);
3600 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3601 s = swallow_bom((U8*)s);
3605 /* Incest with pod. */
3608 sv_catsv(PL_thiswhite, PL_linestr);
3610 if (*s == '=' && strnEQ(s, "=cut", 4)) {
3611 sv_setpvn(PL_linestr, "", 0);
3612 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
3613 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3614 PL_last_lop = PL_last_uni = NULL;
3615 PL_doextract = FALSE;
3619 } while (PL_doextract);
3620 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = s;
3621 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3622 SV * const sv = newSV(0);
3624 sv_upgrade(sv, SVt_PVMG);
3625 sv_setsv(sv,PL_linestr);
3628 av_store(CopFILEAVx(PL_curcop),(I32)CopLINE(PL_curcop),sv);
3630 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3631 PL_last_lop = PL_last_uni = NULL;
3632 if (CopLINE(PL_curcop) == 1) {
3633 while (s < PL_bufend && isSPACE(*s))
3635 if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
3639 PL_thiswhite = newSVpvn(PL_linestart, s - PL_linestart);
3643 if (*s == '#' && *(s+1) == '!')
3645 #ifdef ALTERNATE_SHEBANG
3647 static char const as[] = ALTERNATE_SHEBANG;
3648 if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
3649 d = s + (sizeof(as) - 1);
3651 #endif /* ALTERNATE_SHEBANG */
3660 while (*d && !isSPACE(*d))
3664 #ifdef ARG_ZERO_IS_SCRIPT
3665 if (ipathend > ipath) {
3667 * HP-UX (at least) sets argv[0] to the script name,
3668 * which makes $^X incorrect. And Digital UNIX and Linux,
3669 * at least, set argv[0] to the basename of the Perl
3670 * interpreter. So, having found "#!", we'll set it right.
3672 SV * const x = GvSV(gv_fetchpvs("\030", GV_ADD|GV_NOTQUAL,
3674 assert(SvPOK(x) || SvGMAGICAL(x));
3675 if (sv_eq(x, CopFILESV(PL_curcop))) {
3676 sv_setpvn(x, ipath, ipathend - ipath);
3682 const char *bstart = SvPV_const(CopFILESV(PL_curcop),blen);
3683 const char * const lstart = SvPV_const(x,llen);
3685 bstart += blen - llen;
3686 if (strnEQ(bstart, lstart, llen) && bstart[-1] == '/') {
3687 sv_setpvn(x, ipath, ipathend - ipath);
3692 TAINT_NOT; /* $^X is always tainted, but that's OK */
3694 #endif /* ARG_ZERO_IS_SCRIPT */
3699 d = instr(s,"perl -");
3701 d = instr(s,"perl");
3703 /* avoid getting into infinite loops when shebang
3704 * line contains "Perl" rather than "perl" */
3706 for (d = ipathend-4; d >= ipath; --d) {
3707 if ((*d == 'p' || *d == 'P')
3708 && !ibcmp(d, "perl", 4))
3718 #ifdef ALTERNATE_SHEBANG
3720 * If the ALTERNATE_SHEBANG on this system starts with a
3721 * character that can be part of a Perl expression, then if
3722 * we see it but not "perl", we're probably looking at the
3723 * start of Perl code, not a request to hand off to some
3724 * other interpreter. Similarly, if "perl" is there, but
3725 * not in the first 'word' of the line, we assume the line
3726 * contains the start of the Perl program.
3728 if (d && *s != '#') {
3729 const char *c = ipath;
3730 while (*c && !strchr("; \t\r\n\f\v#", *c))
3733 d = NULL; /* "perl" not in first word; ignore */
3735 *s = '#'; /* Don't try to parse shebang line */
3737 #endif /* ALTERNATE_SHEBANG */
3738 #ifndef MACOS_TRADITIONAL
3743 !instr(s,"indir") &&
3744 instr(PL_origargv[0],"perl"))
3751 while (s < PL_bufend && isSPACE(*s))
3753 if (s < PL_bufend) {
3754 Newxz(newargv,PL_origargc+3,char*);
3756 while (s < PL_bufend && !isSPACE(*s))
3759 Copy(PL_origargv+1, newargv+2, PL_origargc+1, char*);
3762 newargv = PL_origargv;
3765 PerlProc_execv(ipath, EXEC_ARGV_CAST(newargv));
3767 Perl_croak(aTHX_ "Can't exec %s", ipath);
3771 while (*d && !isSPACE(*d))
3773 while (SPACE_OR_TAB(*d))
3777 const bool switches_done = PL_doswitches;
3778 const U32 oldpdb = PL_perldb;
3779 const bool oldn = PL_minus_n;
3780 const bool oldp = PL_minus_p;
3783 if (*d == 'M' || *d == 'm' || *d == 'C') {
3784 const char * const m = d;
3785 while (*d && !isSPACE(*d))
3787 Perl_croak(aTHX_ "Too late for \"-%.*s\" option",
3790 d = moreswitches(d);
3792 if (PL_doswitches && !switches_done) {
3793 int argc = PL_origargc;
3794 char **argv = PL_origargv;
3797 } while (argc && argv[0][0] == '-' && argv[0][1]);
3798 init_argv_symbols(argc,argv);
3800 if ((PERLDB_LINE && !oldpdb) ||
3801 ((PL_minus_n || PL_minus_p) && !(oldn || oldp)))
3802 /* if we have already added "LINE: while (<>) {",
3803 we must not do it again */
3805 sv_setpvn(PL_linestr, "", 0);
3806 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
3807 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
3808 PL_last_lop = PL_last_uni = NULL;
3809 PL_preambled = FALSE;
3811 (void)gv_fetchfile(PL_origfilename);
3818 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
3820 PL_lex_state = LEX_FORMLINE;
3825 #ifdef PERL_STRICT_CR
3826 Perl_warn(aTHX_ "Illegal character \\%03o (carriage return)", '\r');
3828 "\t(Maybe you didn't strip carriage returns after a network transfer?)\n");
3830 case ' ': case '\t': case '\f': case 013:
3831 #ifdef MACOS_TRADITIONAL
3835 PL_realtokenstart = -1;
3844 PL_realtokenstart = -1;
3848 if (PL_lex_state != LEX_NORMAL || (PL_in_eval && !PL_rsfp)) {
3849 if (*s == '#' && s == PL_linestart && PL_in_eval && !PL_rsfp) {
3850 /* handle eval qq[#line 1 "foo"\n ...] */
3851 CopLINE_dec(PL_curcop);
3854 if (PL_madskills && !PL_lex_formbrack && !PL_in_eval) {
3856 if (!PL_in_eval || PL_rsfp)
3861 while (d < PL_bufend && *d != '\n')
3865 else if (d > PL_bufend) /* Found by Ilya: feed random input to Perl. */
3866 Perl_croak(aTHX_ "panic: input overflow");
3869 PL_thiswhite = newSVpvn(s, d - s);
3874 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
3876 PL_lex_state = LEX_FORMLINE;
3882 if (PL_madskills && CopLINE(PL_curcop) >= 1 && !PL_lex_formbrack) {
3883 if (CopLINE(PL_curcop) == 1 && s[0] == '#' && s[1] == '!') {
3886 TOKEN(PEG); /* make sure any #! line is accessible */
3891 /* if (PL_madskills && PL_lex_formbrack) { */
3893 while (d < PL_bufend && *d != '\n')
3897 else if (d > PL_bufend) /* Found by Ilya: feed random input to Perl. */
3898 Perl_croak(aTHX_ "panic: input overflow");
3899 if (PL_madskills && CopLINE(PL_curcop) >= 1) {
3901 PL_thiswhite = newSVpvn("",0);
3902 if (CopLINE(PL_curcop) == 1) {
3903 sv_setpvn(PL_thiswhite, "", 0);
3906 sv_catpvn(PL_thiswhite, s, d - s);
3920 if (s[1] && isALPHA(s[1]) && !isALNUM(s[2])) {
3928 while (s < PL_bufend && SPACE_OR_TAB(*s))
3931 if (strnEQ(s,"=>",2)) {
3932 s = force_word(PL_bufptr,WORD,FALSE,FALSE,FALSE);
3933 DEBUG_T( { printbuf("### Saw unary minus before =>, forcing word %s\n", s); } );
3934 OPERATOR('-'); /* unary minus */
3936 PL_last_uni = PL_oldbufptr;
3938 case 'r': ftst = OP_FTEREAD; break;
3939 case 'w': ftst = OP_FTEWRITE; break;
3940 case 'x': ftst = OP_FTEEXEC; break;
3941 case 'o': ftst = OP_FTEOWNED; break;
3942 case 'R': ftst = OP_FTRREAD; break;
3943 case 'W': ftst = OP_FTRWRITE; break;
3944 case 'X': ftst = OP_FTREXEC; break;
3945 case 'O': ftst = OP_FTROWNED; break;
3946 case 'e': ftst = OP_FTIS; break;
3947 case 'z': ftst = OP_FTZERO; break;
3948 case 's': ftst = OP_FTSIZE; break;
3949 case 'f': ftst = OP_FTFILE; break;
3950 case 'd': ftst = OP_FTDIR; break;
3951 case 'l': ftst = OP_FTLINK; break;
3952 case 'p': ftst = OP_FTPIPE; break;
3953 case 'S': ftst = OP_FTSOCK; break;
3954 case 'u': ftst = OP_FTSUID; break;
3955 case 'g': ftst = OP_FTSGID; break;
3956 case 'k': ftst = OP_FTSVTX; break;
3957 case 'b': ftst = OP_FTBLK; break;
3958 case 'c': ftst = OP_FTCHR; break;
3959 case 't': ftst = OP_FTTTY; break;
3960 case 'T': ftst = OP_FTTEXT; break;
3961 case 'B': ftst = OP_FTBINARY; break;
3962 case 'M': case 'A': case 'C':
3963 gv_fetchpvs("\024", GV_ADD|GV_NOTQUAL, SVt_PV);
3965 case 'M': ftst = OP_FTMTIME; break;
3966 case 'A': ftst = OP_FTATIME; break;
3967 case 'C': ftst = OP_FTCTIME; break;
3975 PL_last_lop_op = (OPCODE)ftst;
3976 DEBUG_T( { PerlIO_printf(Perl_debug_log,
3977 "### Saw file test %c\n", (int)tmp);
3982 /* Assume it was a minus followed by a one-letter named
3983 * subroutine call (or a -bareword), then. */
3984 DEBUG_T( { PerlIO_printf(Perl_debug_log,
3985 "### '-%c' looked like a file test but was not\n",
3992 const char tmp = *s++;
3995 if (PL_expect == XOPERATOR)
4000 else if (*s == '>') {
4003 if (isIDFIRST_lazy_if(s,UTF)) {
4004 s = force_word(s,METHOD,FALSE,TRUE,FALSE);
4012 if (PL_expect == XOPERATOR)
4015 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
4017 OPERATOR('-'); /* unary minus */
4023 const char tmp = *s++;
4026 if (PL_expect == XOPERATOR)
4031 if (PL_expect == XOPERATOR)
4034 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
4041 if (PL_expect != XOPERATOR) {
4042 s = scan_ident(s, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
4043 PL_expect = XOPERATOR;
4044 force_ident(PL_tokenbuf, '*');
4057 if (PL_expect == XOPERATOR) {
4061 PL_tokenbuf[0] = '%';
4062 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, TRUE);
4063 if (!PL_tokenbuf[1]) {
4066 PL_pending_ident = '%';
4077 && (PL_expect == XOPERATOR || PL_expect == XTERMORDORDOR)
4078 && FEATURE_IS_ENABLED("~~"))
4085 const char tmp = *s++;
4091 goto just_a_word_zero_gv;
4094 switch (PL_expect) {
4100 if (!PL_in_my || PL_lex_state != LEX_NORMAL)
4102 PL_bufptr = s; /* update in case we back off */
4108 PL_expect = XTERMBLOCK;
4111 stuffstart = s - SvPVX(PL_linestr) - 1;
4115 while (isIDFIRST_lazy_if(s,UTF)) {
4118 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
4119 if (isLOWER(*s) && (tmp = keyword(PL_tokenbuf, len))) {
4120 if (tmp < 0) tmp = -tmp;
4135 sv = newSVpvn(s, len);
4137 d = scan_str(d,TRUE,TRUE);
4139 /* MUST advance bufptr here to avoid bogus
4140 "at end of line" context messages from yyerror().
4142 PL_bufptr = s + len;
4143 yyerror("Unterminated attribute parameter in attribute list");
4147 return REPORT(0); /* EOF indicator */
4151 sv_catsv(sv, PL_lex_stuff);
4152 attrs = append_elem(OP_LIST, attrs,
4153 newSVOP(OP_CONST, 0, sv));
4154 SvREFCNT_dec(PL_lex_stuff);
4155 PL_lex_stuff = NULL;
4158 if (len == 6 && strnEQ(SvPVX(sv), "unique", len)) {
4160 if (PL_in_my == KEY_our) {
4162 GvUNIQUE_on(cGVOPx_gv(yylval.opval));
4164 /* skip to avoid loading attributes.pm */
4166 deprecate(":unique");
4169 Perl_croak(aTHX_ "The 'unique' attribute may only be applied to 'our' variables");
4172 /* NOTE: any CV attrs applied here need to be part of
4173 the CVf_BUILTIN_ATTRS define in cv.h! */
4174 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "lvalue", len)) {
4176 CvLVALUE_on(PL_compcv);
4178 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "locked", len)) {
4180 CvLOCKED_on(PL_compcv);
4182 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "method", len)) {
4184 CvMETHOD_on(PL_compcv);
4186 else if (!PL_in_my && len == 9 && strnEQ(SvPVX(sv), "assertion", len)) {
4188 CvASSERTION_on(PL_compcv);
4190 /* After we've set the flags, it could be argued that
4191 we don't need to do the attributes.pm-based setting
4192 process, and shouldn't bother appending recognized
4193 flags. To experiment with that, uncomment the
4194 following "else". (Note that's already been
4195 uncommented. That keeps the above-applied built-in
4196 attributes from being intercepted (and possibly
4197 rejected) by a package's attribute routines, but is
4198 justified by the performance win for the common case
4199 of applying only built-in attributes.) */
4201 attrs = append_elem(OP_LIST, attrs,
4202 newSVOP(OP_CONST, 0,
4206 if (*s == ':' && s[1] != ':')
4209 break; /* require real whitespace or :'s */
4210 /* XXX losing whitespace on sequential attributes here */
4214 = (PL_expect == XOPERATOR ? '=' : '{'); /*'}(' for vi */
4215 if (*s != ';' && *s != '}' && *s != tmp
4216 && (tmp != '=' || *s != ')')) {
4217 const char q = ((*s == '\'') ? '"' : '\'');
4218 /* If here for an expression, and parsed no attrs, back
4220 if (tmp == '=' && !attrs) {
4224 /* MUST advance bufptr here to avoid bogus "at end of line"
4225 context messages from yyerror().
4228 yyerror( (const char *)
4230 ? Perl_form(aTHX_ "Invalid separator character "
4231 "%c%c%c in attribute list", q, *s, q)
4232 : "Unterminated attribute list" ) );
4240 start_force(PL_curforce);
4241 NEXTVAL_NEXTTOKE.opval = attrs;
4242 CURMAD('_', PL_nextwhite);
4247 PL_thistoken = newSVpvn(SvPVX(PL_linestr) + stuffstart,
4248 (s - SvPVX(PL_linestr)) - stuffstart);
4256 if (PL_last_lop == PL_oldoldbufptr || PL_last_uni == PL_oldoldbufptr)
4257 PL_oldbufptr = PL_oldoldbufptr; /* allow print(STDOUT 123) */
4265 const char tmp = *s++;
4270 const char tmp = *s++;
4278 if (PL_lex_brackets <= 0)
4279 yyerror("Unmatched right square bracket");
4282 if (PL_lex_state == LEX_INTERPNORMAL) {
4283 if (PL_lex_brackets == 0) {
4284 if (*s != '[' && *s != '{' && (*s != '-' || s[1] != '>'))
4285 PL_lex_state = LEX_INTERPEND;
4292 if (PL_lex_brackets > 100) {
4293 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
4295 switch (PL_expect) {
4297 if (PL_lex_formbrack) {
4301 if (PL_oldoldbufptr == PL_last_lop)
4302 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
4304 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
4305 OPERATOR(HASHBRACK);
4307 while (s < PL_bufend && SPACE_OR_TAB(*s))
4310 PL_tokenbuf[0] = '\0';
4311 if (d < PL_bufend && *d == '-') {
4312 PL_tokenbuf[0] = '-';
4314 while (d < PL_bufend && SPACE_OR_TAB(*d))
4317 if (d < PL_bufend && isIDFIRST_lazy_if(d,UTF)) {
4318 d = scan_word(d, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
4320 while (d < PL_bufend && SPACE_OR_TAB(*d))
4323 const char minus = (PL_tokenbuf[0] == '-');
4324 s = force_word(s + minus, WORD, FALSE, TRUE, FALSE);
4332 PL_lex_brackstack[PL_lex_brackets++] = XSTATE;
4337 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
4342 if (PL_oldoldbufptr == PL_last_lop)
4343 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
4345 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
4348 if (PL_expect == XREF && PL_lex_state == LEX_INTERPNORMAL) {
4350 /* This hack is to get the ${} in the message. */
4352 yyerror("syntax error");
4355 OPERATOR(HASHBRACK);
4357 /* This hack serves to disambiguate a pair of curlies
4358 * as being a block or an anon hash. Normally, expectation
4359 * determines that, but in cases where we're not in a
4360 * position to expect anything in particular (like inside
4361 * eval"") we have to resolve the ambiguity. This code
4362 * covers the case where the first term in the curlies is a
4363 * quoted string. Most other cases need to be explicitly
4364 * disambiguated by prepending a "+" before the opening
4365 * curly in order to force resolution as an anon hash.
4367 * XXX should probably propagate the outer expectation
4368 * into eval"" to rely less on this hack, but that could
4369 * potentially break current behavior of eval"".
4373 if (*s == '\'' || *s == '"' || *s == '`') {
4374 /* common case: get past first string, handling escapes */
4375 for (t++; t < PL_bufend && *t != *s;)
4376 if (*t++ == '\\' && (*t == '\\' || *t == *s))
4380 else if (*s == 'q') {
4383 || ((*t == 'q' || *t == 'x') && ++t < PL_bufend
4386 /* skip q//-like construct */
4388 char open, close, term;
4391 while (t < PL_bufend && isSPACE(*t))
4393 /* check for q => */
4394 if (t+1 < PL_bufend && t[0] == '=' && t[1] == '>') {
4395 OPERATOR(HASHBRACK);
4399 if (term && (tmps = strchr("([{< )]}> )]}>",term)))
<