3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 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 char ident_too_long[] = "Identifier too long";
30 static char c_without_g[] = "Use of /c modifier is meaningless without /g";
31 static char c_in_subst[] = "Use of /c modifier is meaningless in s///";
33 static void restore_rsfp(pTHX_ void *f);
34 #ifndef PERL_NO_UTF16_FILTER
35 static I32 utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen);
36 static I32 utf16rev_textfilter(pTHX_ int idx, SV *sv, int maxlen);
39 #define XFAKEBRACK 128
42 #ifdef USE_UTF8_SCRIPTS
43 # define UTF (!IN_BYTES)
45 # define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8))
48 /* In variables named $^X, these are the legal values for X.
49 * 1999-02-27 mjd-perl-patch@plover.com */
50 #define isCONTROLVAR(x) (isUPPER(x) || strchr("[\\]^_?", (x)))
52 /* On MacOS, respect nonbreaking spaces */
53 #ifdef MACOS_TRADITIONAL
54 #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\312'||(c)=='\t')
56 #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t')
59 /* LEX_* are values for PL_lex_state, the state of the lexer.
60 * They are arranged oddly so that the guard on the switch statement
61 * can get by with a single comparison (if the compiler is smart enough).
64 /* #define LEX_NOTPARSING 11 is done in perl.h. */
67 #define LEX_INTERPNORMAL 9
68 #define LEX_INTERPCASEMOD 8
69 #define LEX_INTERPPUSH 7
70 #define LEX_INTERPSTART 6
71 #define LEX_INTERPEND 5
72 #define LEX_INTERPENDMAYBE 4
73 #define LEX_INTERPCONCAT 3
74 #define LEX_INTERPCONST 2
75 #define LEX_FORMLINE 1
76 #define LEX_KNOWNEXT 0
79 static char* lex_state_names[] = {
100 /* CLINE is a macro that ensures PL_copline has a sane value */
105 #define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline))
108 * Convenience functions to return different tokens and prime the
109 * lexer for the next token. They all take an argument.
111 * TOKEN : generic token (used for '(', DOLSHARP, etc)
112 * OPERATOR : generic operator
113 * AOPERATOR : assignment operator
114 * PREBLOCK : beginning the block after an if, while, foreach, ...
115 * PRETERMBLOCK : beginning a non-code-defining {} block (eg, hash ref)
116 * PREREF : *EXPR where EXPR is not a simple identifier
117 * TERM : expression term
118 * LOOPX : loop exiting command (goto, last, dump, etc)
119 * FTST : file test operator
120 * FUN0 : zero-argument function
121 * FUN1 : not used, except for not, which isn't a UNIOP
122 * BOop : bitwise or or xor
124 * SHop : shift operator
125 * PWop : power operator
126 * PMop : pattern-matching operator
127 * Aop : addition-level operator
128 * Mop : multiplication-level operator
129 * Eop : equality-testing operator
130 * Rop : relational operator <= != gt
132 * Also see LOP and lop() below.
135 #ifdef DEBUGGING /* Serve -DT. */
136 # define REPORT(retval) tokereport(s,(int)retval)
138 # define REPORT(retval) (retval)
141 #define TOKEN(retval) return ( PL_bufptr = s, REPORT(retval))
142 #define OPERATOR(retval) return (PL_expect = XTERM, PL_bufptr = s, REPORT(retval))
143 #define AOPERATOR(retval) return ao((PL_expect = XTERM, PL_bufptr = s, REPORT(retval)))
144 #define PREBLOCK(retval) return (PL_expect = XBLOCK,PL_bufptr = s, REPORT(retval))
145 #define PRETERMBLOCK(retval) return (PL_expect = XTERMBLOCK,PL_bufptr = s, REPORT(retval))
146 #define PREREF(retval) return (PL_expect = XREF,PL_bufptr = s, REPORT(retval))
147 #define TERM(retval) return (CLINE, PL_expect = XOPERATOR, PL_bufptr = s, REPORT(retval))
148 #define LOOPX(f) return (yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)LOOPEX))
149 #define FTST(f) return (yylval.ival=f, PL_expect=XTERMORDORDOR, PL_bufptr=s, REPORT((int)UNIOP))
150 #define FUN0(f) return (yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0))
151 #define FUN1(f) return (yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC1))
152 #define BOop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITOROP)))
153 #define BAop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITANDOP)))
154 #define SHop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)SHIFTOP)))
155 #define PWop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)POWOP)))
156 #define PMop(f) return(yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MATCHOP))
157 #define Aop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)ADDOP)))
158 #define Mop(f) return ao((yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MULOP)))
159 #define Eop(f) return (yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)EQOP))
160 #define Rop(f) return (yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)RELOP))
162 /* This bit of chicanery makes a unary function followed by
163 * a parenthesis into a function with one argument, highest precedence.
164 * The UNIDOR macro is for unary functions that can be followed by the //
165 * operator (such as C<shift // 0>).
167 #define UNI2(f,x) return ( \
171 PL_last_uni = PL_oldbufptr, \
172 PL_last_lop_op = f, \
174 (*s == '(' || (s = skipspace(s), *s == '(') \
175 ? (int)FUNC1 : (int)UNIOP)))
176 #define UNI(f) UNI2(f,XTERM)
177 #define UNIDOR(f) UNI2(f,XTERMORDORDOR)
179 #define UNIBRACK(f) return ( \
182 PL_last_uni = PL_oldbufptr, \
184 (*s == '(' || (s = skipspace(s), *s == '(') \
185 ? (int)FUNC1 : (int)UNIOP)))
187 /* grandfather return to old style */
188 #define OLDLOP(f) return(yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)LSTOP)
192 /* how to interpret the yylval associated with the token */
196 TOKENTYPE_OPNUM, /* yylval.ival contains an opcode number */
202 static struct debug_tokens { int token, type; char *name;} debug_tokens[] =
204 { ADDOP, TOKENTYPE_OPNUM, "ADDOP" },
205 { ANDAND, TOKENTYPE_NONE, "ANDAND" },
206 { ANDOP, TOKENTYPE_NONE, "ANDOP" },
207 { ANONSUB, TOKENTYPE_IVAL, "ANONSUB" },
208 { ARROW, TOKENTYPE_NONE, "ARROW" },
209 { ASSIGNOP, TOKENTYPE_OPNUM, "ASSIGNOP" },
210 { BITANDOP, TOKENTYPE_OPNUM, "BITANDOP" },
211 { BITOROP, TOKENTYPE_OPNUM, "BITOROP" },
212 { COLONATTR, TOKENTYPE_NONE, "COLONATTR" },
213 { CONTINUE, TOKENTYPE_NONE, "CONTINUE" },
214 { DO, TOKENTYPE_NONE, "DO" },
215 { DOLSHARP, TOKENTYPE_NONE, "DOLSHARP" },
216 { DORDOR, TOKENTYPE_NONE, "DORDOR" },
217 { DOROP, TOKENTYPE_OPNUM, "DOROP" },
218 { DOTDOT, TOKENTYPE_IVAL, "DOTDOT" },
219 { ELSE, TOKENTYPE_NONE, "ELSE" },
220 { ELSIF, TOKENTYPE_IVAL, "ELSIF" },
221 { EQOP, TOKENTYPE_OPNUM, "EQOP" },
222 { FOR, TOKENTYPE_IVAL, "FOR" },
223 { FORMAT, TOKENTYPE_NONE, "FORMAT" },
224 { FUNC, TOKENTYPE_OPNUM, "FUNC" },
225 { FUNC0, TOKENTYPE_OPNUM, "FUNC0" },
226 { FUNC0SUB, TOKENTYPE_OPVAL, "FUNC0SUB" },
227 { FUNC1, TOKENTYPE_OPNUM, "FUNC1" },
228 { FUNCMETH, TOKENTYPE_OPVAL, "FUNCMETH" },
229 { HASHBRACK, TOKENTYPE_NONE, "HASHBRACK" },
230 { IF, TOKENTYPE_IVAL, "IF" },
231 { LABEL, TOKENTYPE_PVAL, "LABEL" },
232 { LOCAL, TOKENTYPE_IVAL, "LOCAL" },
233 { LOOPEX, TOKENTYPE_OPNUM, "LOOPEX" },
234 { LSTOP, TOKENTYPE_OPNUM, "LSTOP" },
235 { LSTOPSUB, TOKENTYPE_OPVAL, "LSTOPSUB" },
236 { MATCHOP, TOKENTYPE_OPNUM, "MATCHOP" },
237 { METHOD, TOKENTYPE_OPVAL, "METHOD" },
238 { MULOP, TOKENTYPE_OPNUM, "MULOP" },
239 { MY, TOKENTYPE_IVAL, "MY" },
240 { MYSUB, TOKENTYPE_NONE, "MYSUB" },
241 { NOAMP, TOKENTYPE_NONE, "NOAMP" },
242 { NOTOP, TOKENTYPE_NONE, "NOTOP" },
243 { OROP, TOKENTYPE_IVAL, "OROP" },
244 { OROR, TOKENTYPE_NONE, "OROR" },
245 { PACKAGE, TOKENTYPE_NONE, "PACKAGE" },
246 { PMFUNC, TOKENTYPE_OPVAL, "PMFUNC" },
247 { POSTDEC, TOKENTYPE_NONE, "POSTDEC" },
248 { POSTINC, TOKENTYPE_NONE, "POSTINC" },
249 { POWOP, TOKENTYPE_OPNUM, "POWOP" },
250 { PREDEC, TOKENTYPE_NONE, "PREDEC" },
251 { PREINC, TOKENTYPE_NONE, "PREINC" },
252 { PRIVATEREF, TOKENTYPE_OPVAL, "PRIVATEREF" },
253 { REFGEN, TOKENTYPE_NONE, "REFGEN" },
254 { RELOP, TOKENTYPE_OPNUM, "RELOP" },
255 { SHIFTOP, TOKENTYPE_OPNUM, "SHIFTOP" },
256 { SUB, TOKENTYPE_NONE, "SUB" },
257 { THING, TOKENTYPE_OPVAL, "THING" },
258 { UMINUS, TOKENTYPE_NONE, "UMINUS" },
259 { UNIOP, TOKENTYPE_OPNUM, "UNIOP" },
260 { UNIOPSUB, TOKENTYPE_OPVAL, "UNIOPSUB" },
261 { UNLESS, TOKENTYPE_IVAL, "UNLESS" },
262 { UNTIL, TOKENTYPE_IVAL, "UNTIL" },
263 { USE, TOKENTYPE_IVAL, "USE" },
264 { WHILE, TOKENTYPE_IVAL, "WHILE" },
265 { WORD, TOKENTYPE_OPVAL, "WORD" },
266 { 0, TOKENTYPE_NONE, 0 }
269 /* dump the returned token in rv, plus any optional arg in yylval */
272 S_tokereport(pTHX_ char* s, I32 rv)
276 enum token_type type = TOKENTYPE_NONE;
277 struct debug_tokens *p;
278 SV* report = NEWSV(0, 60);
280 Perl_sv_catpvf(aTHX_ report, "<== ");
282 for (p = debug_tokens; p->token; p++) {
283 if (p->token == (int)rv) {
290 Perl_sv_catpvf(aTHX_ report, "%s", name);
291 else if ((char)rv > ' ' && (char)rv < '~')
292 Perl_sv_catpvf(aTHX_ report, "'%c'", (char)rv);
294 Perl_sv_catpvf(aTHX_ report, "EOF");
296 Perl_sv_catpvf(aTHX_ report, "?? %"IVdf, (IV)rv);
299 case TOKENTYPE_GVVAL: /* doesn't appear to be used */
302 Perl_sv_catpvf(aTHX_ report, "(ival=%"IVdf")", yylval.ival);
304 case TOKENTYPE_OPNUM:
305 Perl_sv_catpvf(aTHX_ report, "(ival=op_%s)",
306 PL_op_name[yylval.ival]);
309 Perl_sv_catpvf(aTHX_ report, "(pval=\"%s\")", yylval.pval);
311 case TOKENTYPE_OPVAL:
313 Perl_sv_catpvf(aTHX_ report, "(opval=op_%s)",
314 PL_op_name[yylval.opval->op_type]);
316 Perl_sv_catpv(aTHX_ report, "(opval=null)");
319 Perl_sv_catpvf(aTHX_ report, " at line %d [", CopLINE(PL_curcop));
320 if (s - PL_bufptr > 0)
321 sv_catpvn(report, PL_bufptr, s - PL_bufptr);
323 if (PL_oldbufptr && *PL_oldbufptr)
324 sv_catpv(report, PL_tokenbuf);
326 PerlIO_printf(Perl_debug_log, "### %s]\n", SvPV_nolen(report));
336 * This subroutine detects &&=, ||=, and //= and turns an ANDAND, OROR or DORDOR
337 * into an OP_ANDASSIGN, OP_ORASSIGN, or OP_DORASSIGN
341 S_ao(pTHX_ int toketype)
343 if (*PL_bufptr == '=') {
345 if (toketype == ANDAND)
346 yylval.ival = OP_ANDASSIGN;
347 else if (toketype == OROR)
348 yylval.ival = OP_ORASSIGN;
349 else if (toketype == DORDOR)
350 yylval.ival = OP_DORASSIGN;
358 * When Perl expects an operator and finds something else, no_op
359 * prints the warning. It always prints "<something> found where
360 * operator expected. It prints "Missing semicolon on previous line?"
361 * if the surprise occurs at the start of the line. "do you need to
362 * predeclare ..." is printed out for code like "sub bar; foo bar $x"
363 * where the compiler doesn't know if foo is a method call or a function.
364 * It prints "Missing operator before end of line" if there's nothing
365 * after the missing operator, or "... before <...>" if there is something
366 * after the missing operator.
370 S_no_op(pTHX_ char *what, char *s)
372 char *oldbp = PL_bufptr;
373 bool is_first = (PL_oldbufptr == PL_linestart);
379 yywarn(Perl_form(aTHX_ "%s found where operator expected", what));
380 if (ckWARN_d(WARN_SYNTAX)) {
382 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
383 "\t(Missing semicolon on previous line?)\n");
384 else if (PL_oldoldbufptr && isIDFIRST_lazy_if(PL_oldoldbufptr,UTF)) {
386 for (t = PL_oldoldbufptr; *t && (isALNUM_lazy_if(t,UTF) || *t == ':'); t++) ;
387 if (t < PL_bufptr && isSPACE(*t))
388 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
389 "\t(Do you need to predeclare %.*s?)\n",
390 t - PL_oldoldbufptr, PL_oldoldbufptr);
394 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
395 "\t(Missing operator before %.*s?)\n", s - oldbp, oldbp);
403 * Complain about missing quote/regexp/heredoc terminator.
404 * If it's called with (char *)NULL then it cauterizes the line buffer.
405 * If we're in a delimited string and the delimiter is a control
406 * character, it's reformatted into a two-char sequence like ^C.
411 S_missingterm(pTHX_ char *s)
416 char *nl = strrchr(s,'\n');
422 iscntrl(PL_multi_close)
424 PL_multi_close < 32 || PL_multi_close == 127
428 tmpbuf[1] = toCTRL(PL_multi_close);
434 *tmpbuf = (char)PL_multi_close;
438 q = strchr(s,'"') ? '\'' : '"';
439 Perl_croak(aTHX_ "Can't find string terminator %c%s%c anywhere before EOF",q,s,q);
447 Perl_deprecate(pTHX_ char *s)
449 if (ckWARN(WARN_DEPRECATED))
450 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED), "Use of %s is deprecated", s);
454 Perl_deprecate_old(pTHX_ char *s)
456 /* This function should NOT be called for any new deprecated warnings */
457 /* Use Perl_deprecate instead */
459 /* It is here to maintain backward compatibility with the pre-5.8 */
460 /* warnings category hierarchy. The "deprecated" category used to */
461 /* live under the "syntax" category. It is now a top-level category */
462 /* in its own right. */
464 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
465 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
466 "Use of %s is deprecated", s);
471 * Deprecate a comma-less variable list.
477 deprecate_old("comma-less variable list");
481 * experimental text filters for win32 carriage-returns, utf16-to-utf8 and
482 * utf16-to-utf8-reversed.
485 #ifdef PERL_CR_FILTER
489 register char *s = SvPVX(sv);
490 register char *e = s + SvCUR(sv);
491 /* outer loop optimized to do nothing if there are no CR-LFs */
493 if (*s++ == '\r' && *s == '\n') {
494 /* hit a CR-LF, need to copy the rest */
495 register char *d = s - 1;
498 if (*s == '\r' && s[1] == '\n')
509 S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
511 I32 count = FILTER_READ(idx+1, sv, maxlen);
512 if (count > 0 && !maxlen)
520 * Initialize variables. Uses the Perl save_stack to save its state (for
521 * recursive calls to the parser).
525 Perl_lex_start(pTHX_ SV *line)
530 SAVEI32(PL_lex_dojoin);
531 SAVEI32(PL_lex_brackets);
532 SAVEI32(PL_lex_casemods);
533 SAVEI32(PL_lex_starts);
534 SAVEI32(PL_lex_state);
535 SAVEVPTR(PL_lex_inpat);
536 SAVEI32(PL_lex_inwhat);
537 if (PL_lex_state == LEX_KNOWNEXT) {
538 I32 toke = PL_nexttoke;
539 while (--toke >= 0) {
540 SAVEI32(PL_nexttype[toke]);
541 SAVEVPTR(PL_nextval[toke]);
543 SAVEI32(PL_nexttoke);
545 SAVECOPLINE(PL_curcop);
548 SAVEPPTR(PL_oldbufptr);
549 SAVEPPTR(PL_oldoldbufptr);
550 SAVEPPTR(PL_last_lop);
551 SAVEPPTR(PL_last_uni);
552 SAVEPPTR(PL_linestart);
553 SAVESPTR(PL_linestr);
554 SAVEGENERICPV(PL_lex_brackstack);
555 SAVEGENERICPV(PL_lex_casestack);
556 SAVEDESTRUCTOR_X(restore_rsfp, PL_rsfp);
557 SAVESPTR(PL_lex_stuff);
558 SAVEI32(PL_lex_defer);
559 SAVEI32(PL_sublex_info.sub_inwhat);
560 SAVESPTR(PL_lex_repl);
562 SAVEINT(PL_lex_expect);
564 PL_lex_state = LEX_NORMAL;
568 New(899, PL_lex_brackstack, 120, char);
569 New(899, PL_lex_casestack, 12, char);
571 *PL_lex_casestack = '\0';
574 PL_lex_stuff = Nullsv;
575 PL_lex_repl = Nullsv;
579 PL_sublex_info.sub_inwhat = 0;
581 if (SvREADONLY(PL_linestr))
582 PL_linestr = sv_2mortal(newSVsv(PL_linestr));
583 s = SvPV(PL_linestr, len);
584 if (!len || s[len-1] != ';') {
585 if (!(SvFLAGS(PL_linestr) & SVs_TEMP))
586 PL_linestr = sv_2mortal(newSVsv(PL_linestr));
587 sv_catpvn(PL_linestr, "\n;", 2);
589 SvTEMP_off(PL_linestr);
590 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = SvPVX(PL_linestr);
591 PL_bufend = PL_bufptr + SvCUR(PL_linestr);
592 PL_last_lop = PL_last_uni = Nullch;
598 * Finalizer for lexing operations. Must be called when the parser is
599 * done with the lexer.
605 PL_doextract = FALSE;
610 * This subroutine has nothing to do with tilting, whether at windmills
611 * or pinball tables. Its name is short for "increment line". It
612 * increments the current line number in CopLINE(PL_curcop) and checks
613 * to see whether the line starts with a comment of the form
614 * # line 500 "foo.pm"
615 * If so, it sets the current line number and file to the values in the comment.
619 S_incline(pTHX_ char *s)
626 CopLINE_inc(PL_curcop);
629 while (SPACE_OR_TAB(*s)) s++;
630 if (strnEQ(s, "line", 4))
634 if (SPACE_OR_TAB(*s))
638 while (SPACE_OR_TAB(*s)) s++;
644 while (SPACE_OR_TAB(*s))
646 if (*s == '"' && (t = strchr(s+1, '"'))) {
651 for (t = s; !isSPACE(*t); t++) ;
654 while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
656 if (*e != '\n' && *e != '\0')
657 return; /* false alarm */
662 CopFILE_free(PL_curcop);
663 CopFILE_set(PL_curcop, s);
666 CopLINE_set(PL_curcop, atoi(n)-1);
671 * Called to gobble the appropriate amount and type of whitespace.
672 * Skips comments as well.
676 S_skipspace(pTHX_ register char *s)
678 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
679 while (s < PL_bufend && SPACE_OR_TAB(*s))
685 SSize_t oldprevlen, oldoldprevlen;
686 SSize_t oldloplen = 0, oldunilen = 0;
687 while (s < PL_bufend && isSPACE(*s)) {
688 if (*s++ == '\n' && PL_in_eval && !PL_rsfp)
693 if (s < PL_bufend && *s == '#') {
694 while (s < PL_bufend && *s != '\n')
698 if (PL_in_eval && !PL_rsfp) {
705 /* only continue to recharge the buffer if we're at the end
706 * of the buffer, we're not reading from a source filter, and
707 * we're in normal lexing mode
709 if (s < PL_bufend || !PL_rsfp || PL_sublex_info.sub_inwhat ||
710 PL_lex_state == LEX_FORMLINE)
713 /* try to recharge the buffer */
714 if ((s = filter_gets(PL_linestr, PL_rsfp,
715 (prevlen = SvCUR(PL_linestr)))) == Nullch)
717 /* end of file. Add on the -p or -n magic */
720 ";}continue{print or die qq(-p destination: $!\\n);}");
721 PL_minus_n = PL_minus_p = 0;
723 else if (PL_minus_n) {
724 sv_setpvn(PL_linestr, ";}", 2);
728 sv_setpvn(PL_linestr,";", 1);
730 /* reset variables for next time we lex */
731 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart
733 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
734 PL_last_lop = PL_last_uni = Nullch;
736 /* Close the filehandle. Could be from -P preprocessor,
737 * STDIN, or a regular file. If we were reading code from
738 * STDIN (because the commandline held no -e or filename)
739 * then we don't close it, we reset it so the code can
740 * read from STDIN too.
743 if (PL_preprocess && !PL_in_eval)
744 (void)PerlProc_pclose(PL_rsfp);
745 else if ((PerlIO*)PL_rsfp == PerlIO_stdin())
746 PerlIO_clearerr(PL_rsfp);
748 (void)PerlIO_close(PL_rsfp);
753 /* not at end of file, so we only read another line */
754 /* make corresponding updates to old pointers, for yyerror() */
755 oldprevlen = PL_oldbufptr - PL_bufend;
756 oldoldprevlen = PL_oldoldbufptr - PL_bufend;
758 oldunilen = PL_last_uni - PL_bufend;
760 oldloplen = PL_last_lop - PL_bufend;
761 PL_linestart = PL_bufptr = s + prevlen;
762 PL_bufend = s + SvCUR(PL_linestr);
764 PL_oldbufptr = s + oldprevlen;
765 PL_oldoldbufptr = s + oldoldprevlen;
767 PL_last_uni = s + oldunilen;
769 PL_last_lop = s + oldloplen;
772 /* debugger active and we're not compiling the debugger code,
773 * so store the line into the debugger's array of lines
775 if (PERLDB_LINE && PL_curstash != PL_debstash) {
776 SV *sv = NEWSV(85,0);
778 sv_upgrade(sv, SVt_PVMG);
779 sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
782 av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv);
789 * Check the unary operators to ensure there's no ambiguity in how they're
790 * used. An ambiguous piece of code would be:
792 * This doesn't mean rand() + 5. Because rand() is a unary operator,
793 * the +5 is its argument.
802 if (PL_oldoldbufptr != PL_last_uni)
804 while (isSPACE(*PL_last_uni))
806 for (s = PL_last_uni; isALNUM_lazy_if(s,UTF) || *s == '-'; s++) ;
807 if ((t = strchr(s, '(')) && t < PL_bufptr)
809 if (ckWARN_d(WARN_AMBIGUOUS)){
812 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
813 "Warning: Use of \"%s\" without parentheses is ambiguous",
820 * LOP : macro to build a list operator. Its behaviour has been replaced
821 * with a subroutine, S_lop() for which LOP is just another name.
824 #define LOP(f,x) return lop(f,x,s)
828 * Build a list operator (or something that might be one). The rules:
829 * - if we have a next token, then it's a list operator [why?]
830 * - if the next thing is an opening paren, then it's a function
831 * - else it's a list operator
835 S_lop(pTHX_ I32 f, int x, char *s)
841 PL_last_lop = PL_oldbufptr;
842 PL_last_lop_op = (OPCODE)f;
844 return REPORT(LSTOP);
851 return REPORT(LSTOP);
856 * When the lexer realizes it knows the next token (for instance,
857 * it is reordering tokens for the parser) then it can call S_force_next
858 * to know what token to return the next time the lexer is called. Caller
859 * will need to set PL_nextval[], and possibly PL_expect to ensure the lexer
860 * handles the token correctly.
864 S_force_next(pTHX_ I32 type)
866 PL_nexttype[PL_nexttoke] = type;
868 if (PL_lex_state != LEX_KNOWNEXT) {
869 PL_lex_defer = PL_lex_state;
870 PL_lex_expect = PL_expect;
871 PL_lex_state = LEX_KNOWNEXT;
877 * When the lexer knows the next thing is a word (for instance, it has
878 * just seen -> and it knows that the next char is a word char, then
879 * it calls S_force_word to stick the next word into the PL_next lookahead.
882 * char *start : buffer position (must be within PL_linestr)
883 * int token : PL_next will be this type of bare word (e.g., METHOD,WORD)
884 * int check_keyword : if true, Perl checks to make sure the word isn't
885 * a keyword (do this if the word is a label, e.g. goto FOO)
886 * int allow_pack : if true, : characters will also be allowed (require,
888 * int allow_initial_tick : used by the "sub" lexer only.
892 S_force_word(pTHX_ register char *start, int token, int check_keyword, int allow_pack, int allow_initial_tick)
897 start = skipspace(start);
899 if (isIDFIRST_lazy_if(s,UTF) ||
900 (allow_pack && *s == ':') ||
901 (allow_initial_tick && *s == '\'') )
903 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, allow_pack, &len);
904 if (check_keyword && keyword(PL_tokenbuf, len))
906 if (token == METHOD) {
911 PL_expect = XOPERATOR;
914 PL_nextval[PL_nexttoke].opval = (OP*)newSVOP(OP_CONST,0, newSVpv(PL_tokenbuf,0));
915 PL_nextval[PL_nexttoke].opval->op_private |= OPpCONST_BARE;
916 if (UTF && !IN_BYTES && is_utf8_string((U8*)PL_tokenbuf, len))
917 SvUTF8_on(((SVOP*)PL_nextval[PL_nexttoke].opval)->op_sv);
925 * Called when the lexer wants $foo *foo &foo etc, but the program
926 * text only contains the "foo" portion. The first argument is a pointer
927 * to the "foo", and the second argument is the type symbol to prefix.
928 * Forces the next token to be a "WORD".
929 * Creates the symbol if it didn't already exist (via gv_fetchpv()).
933 S_force_ident(pTHX_ register char *s, int kind)
936 OP* o = (OP*)newSVOP(OP_CONST, 0, newSVpv(s,0));
937 PL_nextval[PL_nexttoke].opval = o;
940 o->op_private = OPpCONST_ENTERED;
941 /* XXX see note in pp_entereval() for why we forgo typo
942 warnings if the symbol must be introduced in an eval.
944 gv_fetchpv(s, PL_in_eval ? (GV_ADDMULTI | GV_ADDINEVAL) : TRUE,
945 kind == '$' ? SVt_PV :
946 kind == '@' ? SVt_PVAV :
947 kind == '%' ? SVt_PVHV :
955 Perl_str_to_version(pTHX_ SV *sv)
960 char *start = SvPVx(sv,len);
961 bool utf = SvUTF8(sv) ? TRUE : FALSE;
962 char *end = start + len;
963 while (start < end) {
967 n = utf8n_to_uvchr((U8*)start, len, &skip, 0);
972 retval += ((NV)n)/nshift;
981 * Forces the next token to be a version number.
982 * If the next token appears to be an invalid version number, (e.g. "v2b"),
983 * and if "guessing" is TRUE, then no new token is created (and the caller
984 * must use an alternative parsing method).
988 S_force_version(pTHX_ char *s, int guessing)
990 OP *version = Nullop;
999 while (isDIGIT(*d) || *d == '_' || *d == '.')
1001 if (*d == ';' || isSPACE(*d) || *d == '}' || !*d) {
1003 s = scan_num(s, &yylval);
1004 version = yylval.opval;
1005 ver = cSVOPx(version)->op_sv;
1006 if (SvPOK(ver) && !SvNIOK(ver)) {
1007 (void)SvUPGRADE(ver, SVt_PVNV);
1008 SvNVX(ver) = str_to_version(ver);
1009 SvNOK_on(ver); /* hint that it is a version */
1016 /* NOTE: The parser sees the package name and the VERSION swapped */
1017 PL_nextval[PL_nexttoke].opval = version;
1025 * Tokenize a quoted string passed in as an SV. It finds the next
1026 * chunk, up to end of string or a backslash. It may make a new
1027 * SV containing that chunk (if HINT_NEW_STRING is on). It also
1032 S_tokeq(pTHX_ SV *sv)
1035 register char *send;
1043 s = SvPV_force(sv, len);
1044 if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1)
1047 while (s < send && *s != '\\')
1052 if ( PL_hints & HINT_NEW_STRING ) {
1053 pv = sv_2mortal(newSVpvn(SvPVX(pv), len));
1059 if (s + 1 < send && (s[1] == '\\'))
1060 s++; /* all that, just for this */
1065 SvCUR_set(sv, d - SvPVX(sv));
1067 if ( PL_hints & HINT_NEW_STRING )
1068 return new_constant(NULL, 0, "q", sv, pv, "q");
1073 * Now come three functions related to double-quote context,
1074 * S_sublex_start, S_sublex_push, and S_sublex_done. They're used when
1075 * converting things like "\u\Lgnat" into ucfirst(lc("gnat")). They
1076 * interact with PL_lex_state, and create fake ( ... ) argument lists
1077 * to handle functions and concatenation.
1078 * They assume that whoever calls them will be setting up a fake
1079 * join call, because each subthing puts a ',' after it. This lets
1082 * join($, , 'lower ', lcfirst( 'uPpEr', ) ,)
1084 * (I'm not sure whether the spurious commas at the end of lcfirst's
1085 * arguments and join's arguments are created or not).
1090 * Assumes that yylval.ival is the op we're creating (e.g. OP_LCFIRST).
1092 * Pattern matching will set PL_lex_op to the pattern-matching op to
1093 * make (we return THING if yylval.ival is OP_NULL, PMFUNC otherwise).
1095 * OP_CONST and OP_READLINE are easy--just make the new op and return.
1097 * Everything else becomes a FUNC.
1099 * Sets PL_lex_state to LEX_INTERPPUSH unless (ival was OP_NULL or we
1100 * had an OP_CONST or OP_READLINE). This just sets us up for a
1101 * call to S_sublex_push().
1105 S_sublex_start(pTHX)
1107 register I32 op_type = yylval.ival;
1109 if (op_type == OP_NULL) {
1110 yylval.opval = PL_lex_op;
1114 if (op_type == OP_CONST || op_type == OP_READLINE) {
1115 SV *sv = tokeq(PL_lex_stuff);
1117 if (SvTYPE(sv) == SVt_PVIV) {
1118 /* Overloaded constants, nothing fancy: Convert to SVt_PV: */
1124 nsv = newSVpvn(p, len);
1130 yylval.opval = (OP*)newSVOP(op_type, 0, sv);
1131 PL_lex_stuff = Nullsv;
1132 /* Allow <FH> // "foo" */
1133 if (op_type == OP_READLINE)
1134 PL_expect = XTERMORDORDOR;
1138 PL_sublex_info.super_state = PL_lex_state;
1139 PL_sublex_info.sub_inwhat = op_type;
1140 PL_sublex_info.sub_op = PL_lex_op;
1141 PL_lex_state = LEX_INTERPPUSH;
1145 yylval.opval = PL_lex_op;
1155 * Create a new scope to save the lexing state. The scope will be
1156 * ended in S_sublex_done. Returns a '(', starting the function arguments
1157 * to the uc, lc, etc. found before.
1158 * Sets PL_lex_state to LEX_INTERPCONCAT.
1166 PL_lex_state = PL_sublex_info.super_state;
1167 SAVEI32(PL_lex_dojoin);
1168 SAVEI32(PL_lex_brackets);
1169 SAVEI32(PL_lex_casemods);
1170 SAVEI32(PL_lex_starts);
1171 SAVEI32(PL_lex_state);
1172 SAVEVPTR(PL_lex_inpat);
1173 SAVEI32(PL_lex_inwhat);
1174 SAVECOPLINE(PL_curcop);
1175 SAVEPPTR(PL_bufptr);
1176 SAVEPPTR(PL_bufend);
1177 SAVEPPTR(PL_oldbufptr);
1178 SAVEPPTR(PL_oldoldbufptr);
1179 SAVEPPTR(PL_last_lop);
1180 SAVEPPTR(PL_last_uni);
1181 SAVEPPTR(PL_linestart);
1182 SAVESPTR(PL_linestr);
1183 SAVEGENERICPV(PL_lex_brackstack);
1184 SAVEGENERICPV(PL_lex_casestack);
1186 PL_linestr = PL_lex_stuff;
1187 PL_lex_stuff = Nullsv;
1189 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart
1190 = SvPVX(PL_linestr);
1191 PL_bufend += SvCUR(PL_linestr);
1192 PL_last_lop = PL_last_uni = Nullch;
1193 SAVEFREESV(PL_linestr);
1195 PL_lex_dojoin = FALSE;
1196 PL_lex_brackets = 0;
1197 New(899, PL_lex_brackstack, 120, char);
1198 New(899, PL_lex_casestack, 12, char);
1199 PL_lex_casemods = 0;
1200 *PL_lex_casestack = '\0';
1202 PL_lex_state = LEX_INTERPCONCAT;
1203 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
1205 PL_lex_inwhat = PL_sublex_info.sub_inwhat;
1206 if (PL_lex_inwhat == OP_MATCH || PL_lex_inwhat == OP_QR || PL_lex_inwhat == OP_SUBST)
1207 PL_lex_inpat = PL_sublex_info.sub_op;
1209 PL_lex_inpat = Nullop;
1216 * Restores lexer state after a S_sublex_push.
1222 if (!PL_lex_starts++) {
1223 SV *sv = newSVpvn("",0);
1224 if (SvUTF8(PL_linestr))
1226 PL_expect = XOPERATOR;
1227 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
1231 if (PL_lex_casemods) { /* oops, we've got some unbalanced parens */
1232 PL_lex_state = LEX_INTERPCASEMOD;
1236 /* Is there a right-hand side to take care of? (s//RHS/ or tr//RHS/) */
1237 if (PL_lex_repl && (PL_lex_inwhat == OP_SUBST || PL_lex_inwhat == OP_TRANS)) {
1238 PL_linestr = PL_lex_repl;
1240 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
1241 PL_bufend += SvCUR(PL_linestr);
1242 PL_last_lop = PL_last_uni = Nullch;
1243 SAVEFREESV(PL_linestr);
1244 PL_lex_dojoin = FALSE;
1245 PL_lex_brackets = 0;
1246 PL_lex_casemods = 0;
1247 *PL_lex_casestack = '\0';
1249 if (SvEVALED(PL_lex_repl)) {
1250 PL_lex_state = LEX_INTERPNORMAL;
1252 /* we don't clear PL_lex_repl here, so that we can check later
1253 whether this is an evalled subst; that means we rely on the
1254 logic to ensure sublex_done() is called again only via the
1255 branch (in yylex()) that clears PL_lex_repl, else we'll loop */
1258 PL_lex_state = LEX_INTERPCONCAT;
1259 PL_lex_repl = Nullsv;
1265 PL_bufend = SvPVX(PL_linestr);
1266 PL_bufend += SvCUR(PL_linestr);
1267 PL_expect = XOPERATOR;
1268 PL_sublex_info.sub_inwhat = 0;
1276 Extracts a pattern, double-quoted string, or transliteration. This
1279 It looks at lex_inwhat and PL_lex_inpat to find out whether it's
1280 processing a pattern (PL_lex_inpat is true), a transliteration
1281 (lex_inwhat & OP_TRANS is true), or a double-quoted string.
1283 Returns a pointer to the character scanned up to. Iff this is
1284 advanced from the start pointer supplied (ie if anything was
1285 successfully parsed), will leave an OP for the substring scanned
1286 in yylval. Caller must intuit reason for not parsing further
1287 by looking at the next characters herself.
1291 double-quoted style: \r and \n
1292 regexp special ones: \D \s
1294 backrefs: \1 (deprecated in substitution replacements)
1295 case and quoting: \U \Q \E
1296 stops on @ and $, but not for $ as tail anchor
1298 In transliterations:
1299 characters are VERY literal, except for - not at the start or end
1300 of the string, which indicates a range. scan_const expands the
1301 range to the full set of intermediate characters.
1303 In double-quoted strings:
1305 double-quoted style: \r and \n
1307 backrefs: \1 (deprecated)
1308 case and quoting: \U \Q \E
1311 scan_const does *not* construct ops to handle interpolated strings.
1312 It stops processing as soon as it finds an embedded $ or @ variable
1313 and leaves it to the caller to work out what's going on.
1315 @ in pattern could be: @foo, @{foo}, @$foo, @'foo, @::foo.
1317 $ in pattern could be $foo or could be tail anchor. Assumption:
1318 it's a tail anchor if $ is the last thing in the string, or if it's
1319 followed by one of ")| \n\t"
1321 \1 (backreferences) are turned into $1
1323 The structure of the code is
1324 while (there's a character to process) {
1325 handle transliteration ranges
1326 skip regexp comments
1327 skip # initiated comments in //x patterns
1328 check for embedded @foo
1329 check for embedded scalars
1331 leave intact backslashes from leave (below)
1332 deprecate \1 in strings and sub replacements
1333 handle string-changing backslashes \l \U \Q \E, etc.
1334 switch (what was escaped) {
1335 handle - in a transliteration (becomes a literal -)
1336 handle \132 octal characters
1337 handle 0x15 hex characters
1338 handle \cV (control V)
1339 handle printf backslashes (\f, \r, \n, etc)
1341 } (end if backslash)
1342 } (end while character to read)
1347 S_scan_const(pTHX_ char *start)
1349 register char *send = PL_bufend; /* end of the constant */
1350 SV *sv = NEWSV(93, send - start); /* sv for the constant */
1351 register char *s = start; /* start of the constant */
1352 register char *d = SvPVX(sv); /* destination for copies */
1353 bool dorange = FALSE; /* are we in a translit range? */
1354 bool didrange = FALSE; /* did we just finish a range? */
1355 I32 has_utf8 = FALSE; /* Output constant is UTF8 */
1356 I32 this_utf8 = UTF; /* The source string is assumed to be UTF8 */
1359 const char *leaveit = /* set of acceptably-backslashed characters */
1361 ? "\\.^$@AGZdDwWsSbBpPXC+*?|()-nrtfeaxz0123456789[{]} \t\n\r\f\v#"
1364 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
1365 /* If we are doing a trans and we know we want UTF8 set expectation */
1366 has_utf8 = PL_sublex_info.sub_op->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF);
1367 this_utf8 = PL_sublex_info.sub_op->op_private & (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
1371 while (s < send || dorange) {
1372 /* get transliterations out of the way (they're most literal) */
1373 if (PL_lex_inwhat == OP_TRANS) {
1374 /* expand a range A-Z to the full set of characters. AIE! */
1376 I32 i; /* current expanded character */
1377 I32 min; /* first character in range */
1378 I32 max; /* last character in range */
1381 char *c = (char*)utf8_hop((U8*)d, -1);
1385 *c = (char)UTF_TO_NATIVE(0xff);
1386 /* mark the range as done, and continue */
1392 i = d - SvPVX(sv); /* remember current offset */
1393 SvGROW(sv, SvLEN(sv) + 256); /* never more than 256 chars in a range */
1394 d = SvPVX(sv) + i; /* refresh d after realloc */
1395 d -= 2; /* eat the first char and the - */
1397 min = (U8)*d; /* first char in range */
1398 max = (U8)d[1]; /* last char in range */
1402 "Invalid range \"%c-%c\" in transliteration operator",
1403 (char)min, (char)max);
1407 if ((isLOWER(min) && isLOWER(max)) ||
1408 (isUPPER(min) && isUPPER(max))) {
1410 for (i = min; i <= max; i++)
1412 *d++ = NATIVE_TO_NEED(has_utf8,i);
1414 for (i = min; i <= max; i++)
1416 *d++ = NATIVE_TO_NEED(has_utf8,i);
1421 for (i = min; i <= max; i++)
1424 /* mark the range as done, and continue */
1430 /* range begins (ignore - as first or last char) */
1431 else if (*s == '-' && s+1 < send && s != start) {
1433 Perl_croak(aTHX_ "Ambiguous range in transliteration operator");
1436 *d++ = (char)UTF_TO_NATIVE(0xff); /* use illegal utf8 byte--see pmtrans */
1448 /* if we get here, we're not doing a transliteration */
1450 /* skip for regexp comments /(?#comment)/ and code /(?{code})/,
1451 except for the last char, which will be done separately. */
1452 else if (*s == '(' && PL_lex_inpat && s[1] == '?') {
1454 while (s+1 < send && *s != ')')
1455 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1457 else if (s[2] == '{' /* This should match regcomp.c */
1458 || ((s[2] == 'p' || s[2] == '?') && s[3] == '{'))
1461 char *regparse = s + (s[2] == '{' ? 3 : 4);
1464 while (count && (c = *regparse)) {
1465 if (c == '\\' && regparse[1])
1473 if (*regparse != ')')
1474 regparse--; /* Leave one char for continuation. */
1475 while (s < regparse)
1476 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1480 /* likewise skip #-initiated comments in //x patterns */
1481 else if (*s == '#' && PL_lex_inpat &&
1482 ((PMOP*)PL_lex_inpat)->op_pmflags & PMf_EXTENDED) {
1483 while (s+1 < send && *s != '\n')
1484 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1487 /* check for embedded arrays
1488 (@foo, @::foo, @'foo, @{foo}, @$foo, @+, @-)
1490 else if (*s == '@' && s[1]
1491 && (isALNUM_lazy_if(s+1,UTF) || strchr(":'{$+-", s[1])))
1494 /* check for embedded scalars. only stop if we're sure it's a
1497 else if (*s == '$') {
1498 if (!PL_lex_inpat) /* not a regexp, so $ must be var */
1500 if (s + 1 < send && !strchr("()| \r\n\t", s[1]))
1501 break; /* in regexp, $ might be tail anchor */
1504 /* End of else if chain - OP_TRANS rejoin rest */
1507 if (*s == '\\' && s+1 < send) {
1510 /* some backslashes we leave behind */
1511 if (*leaveit && *s && strchr(leaveit, *s)) {
1512 *d++ = NATIVE_TO_NEED(has_utf8,'\\');
1513 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1517 /* deprecate \1 in strings and substitution replacements */
1518 if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat &&
1519 isDIGIT(*s) && *s != '0' && !isDIGIT(s[1]))
1521 if (ckWARN(WARN_SYNTAX))
1522 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "\\%c better written as $%c", *s, *s);
1527 /* string-change backslash escapes */
1528 if (PL_lex_inwhat != OP_TRANS && *s && strchr("lLuUEQ", *s)) {
1533 /* if we get here, it's either a quoted -, or a digit */
1536 /* quoted - in transliterations */
1538 if (PL_lex_inwhat == OP_TRANS) {
1545 if (ckWARN(WARN_MISC) &&
1548 Perl_warner(aTHX_ packWARN(WARN_MISC),
1549 "Unrecognized escape \\%c passed through",
1551 /* default action is to copy the quoted character */
1552 goto default_action;
1555 /* \132 indicates an octal constant */
1556 case '0': case '1': case '2': case '3':
1557 case '4': case '5': case '6': case '7':
1561 uv = grok_oct(s, &len, &flags, NULL);
1564 goto NUM_ESCAPE_INSERT;
1566 /* \x24 indicates a hex constant */
1570 char* e = strchr(s, '}');
1571 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
1572 PERL_SCAN_DISALLOW_PREFIX;
1577 yyerror("Missing right brace on \\x{}");
1581 uv = grok_hex(s, &len, &flags, NULL);
1587 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
1588 uv = grok_hex(s, &len, &flags, NULL);
1594 /* Insert oct or hex escaped character.
1595 * There will always enough room in sv since such
1596 * escapes will be longer than any UTF-8 sequence
1597 * they can end up as. */
1599 /* We need to map to chars to ASCII before doing the tests
1602 if (!UNI_IS_INVARIANT(NATIVE_TO_UNI(uv))) {
1603 if (!has_utf8 && uv > 255) {
1604 /* Might need to recode whatever we have
1605 * accumulated so far if it contains any
1608 * (Can't we keep track of that and avoid
1609 * this rescan? --jhi)
1613 for (c = (U8 *) SvPVX(sv); c < (U8 *)d; c++) {
1614 if (!NATIVE_IS_INVARIANT(*c)) {
1619 STRLEN offset = d - SvPVX(sv);
1621 d = SvGROW(sv, SvLEN(sv) + hicount + 1) + offset;
1625 while (src >= (U8 *)SvPVX(sv)) {
1626 if (!NATIVE_IS_INVARIANT(*src)) {
1627 U8 ch = NATIVE_TO_ASCII(*src);
1628 *dst-- = (U8)UTF8_EIGHT_BIT_LO(ch);
1629 *dst-- = (U8)UTF8_EIGHT_BIT_HI(ch);
1639 if (has_utf8 || uv > 255) {
1640 d = (char*)uvchr_to_utf8((U8*)d, uv);
1642 if (PL_lex_inwhat == OP_TRANS &&
1643 PL_sublex_info.sub_op) {
1644 PL_sublex_info.sub_op->op_private |=
1645 (PL_lex_repl ? OPpTRANS_FROM_UTF
1658 /* \N{LATIN SMALL LETTER A} is a named character */
1662 char* e = strchr(s, '}');
1668 yyerror("Missing right brace on \\N{}");
1672 if (e > s + 2 && s[1] == 'U' && s[2] == '+') {
1674 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
1675 PERL_SCAN_DISALLOW_PREFIX;
1678 uv = grok_hex(s, &len, &flags, NULL);
1680 goto NUM_ESCAPE_INSERT;
1682 res = newSVpvn(s + 1, e - s - 1);
1683 res = new_constant( Nullch, 0, "charnames",
1684 res, Nullsv, "\\N{...}" );
1686 sv_utf8_upgrade(res);
1687 str = SvPV(res,len);
1688 #ifdef EBCDIC_NEVER_MIND
1689 /* charnames uses pack U and that has been
1690 * recently changed to do the below uni->native
1691 * mapping, so this would be redundant (and wrong,
1692 * the code point would be doubly converted).
1693 * But leave this in just in case the pack U change
1694 * gets revoked, but the semantics is still
1695 * desireable for charnames. --jhi */
1697 UV uv = utf8_to_uvchr((U8*)str, 0);
1700 U8 tmpbuf[UTF8_MAXLEN+1], *d;
1702 d = uvchr_to_utf8(tmpbuf, UNI_TO_NATIVE(uv));
1703 sv_setpvn(res, (char *)tmpbuf, d - tmpbuf);
1704 str = SvPV(res, len);
1708 if (!has_utf8 && SvUTF8(res)) {
1709 char *ostart = SvPVX(sv);
1710 SvCUR_set(sv, d - ostart);
1713 sv_utf8_upgrade(sv);
1714 /* this just broke our allocation above... */
1715 SvGROW(sv, (STRLEN)(send - start));
1716 d = SvPVX(sv) + SvCUR(sv);
1719 if (len > (STRLEN)(e - s + 4)) { /* I _guess_ 4 is \N{} --jhi */
1720 char *odest = SvPVX(sv);
1722 SvGROW(sv, (SvLEN(sv) + len - (e - s + 4)));
1723 d = SvPVX(sv) + (d - odest);
1725 Copy(str, d, len, char);
1732 yyerror("Missing braces on \\N{}");
1735 /* \c is a control character */
1744 *d++ = NATIVE_TO_NEED(has_utf8,toCTRL(c));
1747 yyerror("Missing control char name in \\c");
1751 /* printf-style backslashes, formfeeds, newlines, etc */
1753 *d++ = NATIVE_TO_NEED(has_utf8,'\b');
1756 *d++ = NATIVE_TO_NEED(has_utf8,'\n');
1759 *d++ = NATIVE_TO_NEED(has_utf8,'\r');
1762 *d++ = NATIVE_TO_NEED(has_utf8,'\f');
1765 *d++ = NATIVE_TO_NEED(has_utf8,'\t');
1768 *d++ = ASCII_TO_NEED(has_utf8,'\033');
1771 *d++ = ASCII_TO_NEED(has_utf8,'\007');
1777 } /* end if (backslash) */
1780 /* If we started with encoded form, or already know we want it
1781 and then encode the next character */
1782 if ((has_utf8 || this_utf8) && !NATIVE_IS_INVARIANT((U8)(*s))) {
1784 UV uv = (this_utf8) ? utf8n_to_uvchr((U8*)s, send - s, &len, 0) : (UV) ((U8) *s);
1785 STRLEN need = UNISKIP(NATIVE_TO_UNI(uv));
1788 /* encoded value larger than old, need extra space (NOTE: SvCUR() not set here) */
1789 STRLEN off = d - SvPVX(sv);
1790 d = SvGROW(sv, SvLEN(sv) + (need-len)) + off;
1792 d = (char*)uvchr_to_utf8((U8*)d, uv);
1796 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1798 } /* while loop to process each character */
1800 /* terminate the string and set up the sv */
1802 SvCUR_set(sv, d - SvPVX(sv));
1803 if (SvCUR(sv) >= SvLEN(sv))
1804 Perl_croak(aTHX_ "panic: constant overflowed allocated space");
1807 if (PL_encoding && !has_utf8) {
1808 sv_recode_to_utf8(sv, PL_encoding);
1814 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
1815 PL_sublex_info.sub_op->op_private |=
1816 (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
1820 /* shrink the sv if we allocated more than we used */
1821 if (SvCUR(sv) + 5 < SvLEN(sv)) {
1822 SvLEN_set(sv, SvCUR(sv) + 1);
1823 Renew(SvPVX(sv), SvLEN(sv), char);
1826 /* return the substring (via yylval) only if we parsed anything */
1827 if (s > PL_bufptr) {
1828 if ( PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ) )
1829 sv = new_constant(start, s - start, (PL_lex_inpat ? "qr" : "q"),
1831 ( PL_lex_inwhat == OP_TRANS
1833 : ( (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat)
1836 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
1843 * Returns TRUE if there's more to the expression (e.g., a subscript),
1846 * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
1848 * ->[ and ->{ return TRUE
1849 * { and [ outside a pattern are always subscripts, so return TRUE
1850 * if we're outside a pattern and it's not { or [, then return FALSE
1851 * if we're in a pattern and the first char is a {
1852 * {4,5} (any digits around the comma) returns FALSE
1853 * if we're in a pattern and the first char is a [
1855 * [SOMETHING] has a funky algorithm to decide whether it's a
1856 * character class or not. It has to deal with things like
1857 * /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
1858 * anything else returns TRUE
1861 /* This is the one truly awful dwimmer necessary to conflate C and sed. */
1864 S_intuit_more(pTHX_ register char *s)
1866 if (PL_lex_brackets)
1868 if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
1870 if (*s != '{' && *s != '[')
1875 /* In a pattern, so maybe we have {n,m}. */
1892 /* On the other hand, maybe we have a character class */
1895 if (*s == ']' || *s == '^')
1898 /* this is terrifying, and it works */
1899 int weight = 2; /* let's weigh the evidence */
1901 unsigned char un_char = 255, last_un_char;
1902 char *send = strchr(s,']');
1903 char tmpbuf[sizeof PL_tokenbuf * 4];
1905 if (!send) /* has to be an expression */
1908 Zero(seen,256,char);
1911 else if (isDIGIT(*s)) {
1913 if (isDIGIT(s[1]) && s[2] == ']')
1919 for (; s < send; s++) {
1920 last_un_char = un_char;
1921 un_char = (unsigned char)*s;
1926 weight -= seen[un_char] * 10;
1927 if (isALNUM_lazy_if(s+1,UTF)) {
1928 scan_ident(s, send, tmpbuf, sizeof tmpbuf, FALSE);
1929 if ((int)strlen(tmpbuf) > 1 && gv_fetchpv(tmpbuf,FALSE, SVt_PV))
1934 else if (*s == '$' && s[1] &&
1935 strchr("[#!%*<>()-=",s[1])) {
1936 if (/*{*/ strchr("])} =",s[2]))
1945 if (strchr("wds]",s[1]))
1947 else if (seen['\''] || seen['"'])
1949 else if (strchr("rnftbxcav",s[1]))
1951 else if (isDIGIT(s[1])) {
1953 while (s[1] && isDIGIT(s[1]))
1963 if (strchr("aA01! ",last_un_char))
1965 if (strchr("zZ79~",s[1]))
1967 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
1968 weight -= 5; /* cope with negative subscript */
1971 if (!isALNUM(last_un_char)
1972 && !(last_un_char == '$' || last_un_char == '@'
1973 || last_un_char == '&')
1974 && isALPHA(*s) && s[1] && isALPHA(s[1])) {
1979 if (keyword(tmpbuf, d - tmpbuf))
1982 if (un_char == last_un_char + 1)
1984 weight -= seen[un_char];
1989 if (weight >= 0) /* probably a character class */
1999 * Does all the checking to disambiguate
2001 * between foo(bar) and bar->foo. Returns 0 if not a method, otherwise
2002 * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
2004 * First argument is the stuff after the first token, e.g. "bar".
2006 * Not a method if bar is a filehandle.
2007 * Not a method if foo is a subroutine prototyped to take a filehandle.
2008 * Not a method if it's really "Foo $bar"
2009 * Method if it's "foo $bar"
2010 * Not a method if it's really "print foo $bar"
2011 * Method if it's really "foo package::" (interpreted as package->foo)
2012 * Not a method if bar is known to be a subroutine ("sub bar; foo bar")
2013 * Not a method if bar is a filehandle or package, but is quoted with
2018 S_intuit_method(pTHX_ char *start, GV *gv)
2020 char *s = start + (*start == '$');
2021 char tmpbuf[sizeof PL_tokenbuf];
2029 if ((cv = GvCVu(gv))) {
2030 char *proto = SvPVX(cv);
2040 s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
2041 /* start is the beginning of the possible filehandle/object,
2042 * and s is the end of it
2043 * tmpbuf is a copy of it
2046 if (*start == '$') {
2047 if (gv || PL_last_lop_op == OP_PRINT || isUPPER(*PL_tokenbuf))
2052 return *s == '(' ? FUNCMETH : METHOD;
2054 if (!keyword(tmpbuf, len)) {
2055 if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
2060 indirgv = gv_fetchpv(tmpbuf, FALSE, SVt_PVCV);
2061 if (indirgv && GvCVu(indirgv))
2063 /* filehandle or package name makes it a method */
2064 if (!gv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, FALSE)) {
2066 if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
2067 return 0; /* no assumptions -- "=>" quotes bearword */
2069 PL_nextval[PL_nexttoke].opval = (OP*)newSVOP(OP_CONST, 0,
2070 newSVpvn(tmpbuf,len));
2071 PL_nextval[PL_nexttoke].opval->op_private = OPpCONST_BARE;
2075 return *s == '(' ? FUNCMETH : METHOD;
2083 * Return a string of Perl code to load the debugger. If PERL5DB
2084 * is set, it will return the contents of that, otherwise a
2085 * compile-time require of perl5db.pl.
2092 char *pdb = PerlEnv_getenv("PERL5DB");
2096 SETERRNO(0,SS_NORMAL);
2097 return "BEGIN { require 'perl5db.pl' }";
2103 /* Encoded script support. filter_add() effectively inserts a
2104 * 'pre-processing' function into the current source input stream.
2105 * Note that the filter function only applies to the current source file
2106 * (e.g., it will not affect files 'require'd or 'use'd by this one).
2108 * The datasv parameter (which may be NULL) can be used to pass
2109 * private data to this instance of the filter. The filter function
2110 * can recover the SV using the FILTER_DATA macro and use it to
2111 * store private buffers and state information.
2113 * The supplied datasv parameter is upgraded to a PVIO type
2114 * and the IoDIRP/IoANY field is used to store the function pointer,
2115 * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
2116 * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
2117 * private use must be set using malloc'd pointers.
2121 Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
2126 if (!PL_rsfp_filters)
2127 PL_rsfp_filters = newAV();
2129 datasv = NEWSV(255,0);
2130 if (!SvUPGRADE(datasv, SVt_PVIO))
2131 Perl_die(aTHX_ "Can't upgrade filter_add data to SVt_PVIO");
2132 IoANY(datasv) = (void *)funcp; /* stash funcp into spare field */
2133 IoFLAGS(datasv) |= IOf_FAKE_DIRP;
2134 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
2135 (void*)funcp, SvPV_nolen(datasv)));
2136 av_unshift(PL_rsfp_filters, 1);
2137 av_store(PL_rsfp_filters, 0, datasv) ;
2142 /* Delete most recently added instance of this filter function. */
2144 Perl_filter_del(pTHX_ filter_t funcp)
2147 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p", (void*)funcp));
2148 if (!PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
2150 /* if filter is on top of stack (usual case) just pop it off */
2151 datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
2152 if (IoANY(datasv) == (void *)funcp) {
2153 IoFLAGS(datasv) &= ~IOf_FAKE_DIRP;
2154 IoANY(datasv) = (void *)NULL;
2155 sv_free(av_pop(PL_rsfp_filters));
2159 /* we need to search for the correct entry and clear it */
2160 Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
2164 /* Invoke the idxth filter function for the current rsfp. */
2165 /* maxlen 0 = read one text line */
2167 Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
2172 if (!PL_rsfp_filters)
2174 if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */
2175 /* Provide a default input filter to make life easy. */
2176 /* Note that we append to the line. This is handy. */
2177 DEBUG_P(PerlIO_printf(Perl_debug_log,
2178 "filter_read %d: from rsfp\n", idx));
2182 int old_len = SvCUR(buf_sv) ;
2184 /* ensure buf_sv is large enough */
2185 SvGROW(buf_sv, (STRLEN)(old_len + maxlen)) ;
2186 if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len, maxlen)) <= 0){
2187 if (PerlIO_error(PL_rsfp))
2188 return -1; /* error */
2190 return 0 ; /* end of file */
2192 SvCUR_set(buf_sv, old_len + len) ;
2195 if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
2196 if (PerlIO_error(PL_rsfp))
2197 return -1; /* error */
2199 return 0 ; /* end of file */
2202 return SvCUR(buf_sv);
2204 /* Skip this filter slot if filter has been deleted */
2205 if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) {
2206 DEBUG_P(PerlIO_printf(Perl_debug_log,
2207 "filter_read %d: skipped (filter deleted)\n",
2209 return FILTER_READ(idx+1, buf_sv, maxlen); /* recurse */
2211 /* Get function pointer hidden within datasv */
2212 funcp = (filter_t)IoANY(datasv);
2213 DEBUG_P(PerlIO_printf(Perl_debug_log,
2214 "filter_read %d: via function %p (%s)\n",
2215 idx, (void*)funcp, SvPV_nolen(datasv)));
2216 /* Call function. The function is expected to */
2217 /* call "FILTER_READ(idx+1, buf_sv)" first. */
2218 /* Return: <0:error, =0:eof, >0:not eof */
2219 return (*funcp)(aTHX_ idx, buf_sv, maxlen);
2223 S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append)
2225 #ifdef PERL_CR_FILTER
2226 if (!PL_rsfp_filters) {
2227 filter_add(S_cr_textfilter,NULL);
2230 if (PL_rsfp_filters) {
2232 SvCUR_set(sv, 0); /* start with empty line */
2233 if (FILTER_READ(0, sv, 0) > 0)
2234 return ( SvPVX(sv) ) ;
2239 return (sv_gets(sv, fp, append));
2243 S_find_in_my_stash(pTHX_ char *pkgname, I32 len)
2247 if (len == 11 && *pkgname == '_' && strEQ(pkgname, "__PACKAGE__"))
2251 (pkgname[len - 2] == ':' && pkgname[len - 1] == ':') &&
2252 (gv = gv_fetchpv(pkgname, FALSE, SVt_PVHV)))
2254 return GvHV(gv); /* Foo:: */
2257 /* use constant CLASS => 'MyClass' */
2258 if ((gv = gv_fetchpv(pkgname, FALSE, SVt_PVCV))) {
2260 if (GvCV(gv) && (sv = cv_const_sv(GvCV(gv)))) {
2261 pkgname = SvPV_nolen(sv);
2265 return gv_stashpv(pkgname, FALSE);
2269 static char* exp_name[] =
2270 { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
2271 "ATTRTERM", "TERMBLOCK", "TERMORDORDOR"
2278 Works out what to call the token just pulled out of the input
2279 stream. The yacc parser takes care of taking the ops we return and
2280 stitching them into a tree.
2286 if read an identifier
2287 if we're in a my declaration
2288 croak if they tried to say my($foo::bar)
2289 build the ops for a my() declaration
2290 if it's an access to a my() variable
2291 are we in a sort block?
2292 croak if my($a); $a <=> $b
2293 build ops for access to a my() variable
2294 if in a dq string, and they've said @foo and we can't find @foo
2296 build ops for a bareword
2297 if we already built the token before, use it.
2302 #pragma segment Perl_yylex
2307 register char *s = PL_bufptr;
2314 I32 orig_keyword = 0;
2317 PerlIO_printf(Perl_debug_log, "### LEX_%s\n",
2318 lex_state_names[PL_lex_state]);
2320 /* check if there's an identifier for us to look at */
2321 if (PL_pending_ident)
2322 return REPORT(S_pending_ident(aTHX));
2324 /* no identifier pending identification */
2326 switch (PL_lex_state) {
2328 case LEX_NORMAL: /* Some compilers will produce faster */
2329 case LEX_INTERPNORMAL: /* code if we comment these out. */
2333 /* when we've already built the next token, just pull it out of the queue */
2336 yylval = PL_nextval[PL_nexttoke];
2338 PL_lex_state = PL_lex_defer;
2339 PL_expect = PL_lex_expect;
2340 PL_lex_defer = LEX_NORMAL;
2342 DEBUG_T({ PerlIO_printf(Perl_debug_log,
2343 "### Next token after '%s' was known, type %"IVdf"\n", PL_bufptr,
2344 (IV)PL_nexttype[PL_nexttoke]); });
2346 return REPORT(PL_nexttype[PL_nexttoke]);
2348 /* interpolated case modifiers like \L \U, including \Q and \E.
2349 when we get here, PL_bufptr is at the \
2351 case LEX_INTERPCASEMOD:
2353 if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
2354 Perl_croak(aTHX_ "panic: INTERPCASEMOD");
2356 /* handle \E or end of string */
2357 if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
2361 if (PL_lex_casemods) {
2362 oldmod = PL_lex_casestack[--PL_lex_casemods];
2363 PL_lex_casestack[PL_lex_casemods] = '\0';
2365 if (PL_bufptr != PL_bufend
2366 && (oldmod == 'L' || oldmod == 'U' || oldmod == 'Q')) {
2368 PL_lex_state = LEX_INTERPCONCAT;
2372 if (PL_bufptr != PL_bufend)
2374 PL_lex_state = LEX_INTERPCONCAT;
2378 DEBUG_T({ PerlIO_printf(Perl_debug_log,
2379 "### Saw case modifier at '%s'\n", PL_bufptr); });
2381 if (s[1] == '\\' && s[2] == 'E') {
2383 PL_lex_state = LEX_INTERPCONCAT;
2387 if (strnEQ(s, "L\\u", 3) || strnEQ(s, "U\\l", 3))
2388 tmp = *s, *s = s[2], s[2] = (char)tmp; /* misordered... */
2389 if ((*s == 'L' || *s == 'U') &&
2390 (strchr(PL_lex_casestack, 'L') || strchr(PL_lex_casestack, 'U'))) {
2391 PL_lex_casestack[--PL_lex_casemods] = '\0';
2394 if (PL_lex_casemods > 10)
2395 Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
2396 PL_lex_casestack[PL_lex_casemods++] = *s;
2397 PL_lex_casestack[PL_lex_casemods] = '\0';
2398 PL_lex_state = LEX_INTERPCONCAT;
2399 PL_nextval[PL_nexttoke].ival = 0;
2402 PL_nextval[PL_nexttoke].ival = OP_LCFIRST;
2404 PL_nextval[PL_nexttoke].ival = OP_UCFIRST;
2406 PL_nextval[PL_nexttoke].ival = OP_LC;
2408 PL_nextval[PL_nexttoke].ival = OP_UC;
2410 PL_nextval[PL_nexttoke].ival = OP_QUOTEMETA;
2412 Perl_croak(aTHX_ "panic: yylex");
2416 if (PL_lex_starts) {
2419 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
2420 if (PL_lex_casemods == 1 && PL_lex_inpat)
2429 case LEX_INTERPPUSH:
2430 return REPORT(sublex_push());
2432 case LEX_INTERPSTART:
2433 if (PL_bufptr == PL_bufend)
2434 return REPORT(sublex_done());
2435 DEBUG_T({ PerlIO_printf(Perl_debug_log,
2436 "### Interpolated variable at '%s'\n", PL_bufptr); });
2438 PL_lex_dojoin = (*PL_bufptr == '@');
2439 PL_lex_state = LEX_INTERPNORMAL;
2440 if (PL_lex_dojoin) {
2441 PL_nextval[PL_nexttoke].ival = 0;
2443 force_ident("\"", '$');
2444 PL_nextval[PL_nexttoke].ival = 0;
2446 PL_nextval[PL_nexttoke].ival = 0;
2448 PL_nextval[PL_nexttoke].ival = OP_JOIN; /* emulate join($", ...) */
2451 if (PL_lex_starts++) {
2453 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
2454 if (!PL_lex_casemods && PL_lex_inpat)
2461 case LEX_INTERPENDMAYBE:
2462 if (intuit_more(PL_bufptr)) {
2463 PL_lex_state = LEX_INTERPNORMAL; /* false alarm, more expr */
2469 if (PL_lex_dojoin) {
2470 PL_lex_dojoin = FALSE;
2471 PL_lex_state = LEX_INTERPCONCAT;
2474 if (PL_lex_inwhat == OP_SUBST && PL_linestr == PL_lex_repl
2475 && SvEVALED(PL_lex_repl))
2477 if (PL_bufptr != PL_bufend)
2478 Perl_croak(aTHX_ "Bad evalled substitution pattern");
2479 PL_lex_repl = Nullsv;
2482 case LEX_INTERPCONCAT:
2484 if (PL_lex_brackets)
2485 Perl_croak(aTHX_ "panic: INTERPCONCAT");
2487 if (PL_bufptr == PL_bufend)
2488 return REPORT(sublex_done());
2490 if (SvIVX(PL_linestr) == '\'') {
2491 SV *sv = newSVsv(PL_linestr);
2494 else if ( PL_hints & HINT_NEW_RE )
2495 sv = new_constant(NULL, 0, "qr", sv, sv, "q");
2496 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
2500 s = scan_const(PL_bufptr);
2502 PL_lex_state = LEX_INTERPCASEMOD;
2504 PL_lex_state = LEX_INTERPSTART;
2507 if (s != PL_bufptr) {
2508 PL_nextval[PL_nexttoke] = yylval;
2511 if (PL_lex_starts++) {
2512 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
2513 if (!PL_lex_casemods && PL_lex_inpat)
2526 PL_lex_state = LEX_NORMAL;
2527 s = scan_formline(PL_bufptr);
2528 if (!PL_lex_formbrack)
2534 PL_oldoldbufptr = PL_oldbufptr;
2537 PerlIO_printf(Perl_debug_log, "### Tokener expecting %s at [%s]\n",
2538 exp_name[PL_expect], s);
2544 if (isIDFIRST_lazy_if(s,UTF))
2546 Perl_croak(aTHX_ "Unrecognized character \\x%02X", *s & 255);
2549 goto fake_eof; /* emulate EOF on ^D or ^Z */
2554 if (PL_lex_brackets) {
2555 if (PL_lex_formbrack)
2556 yyerror("Format not terminated");
2558 yyerror("Missing right curly or square bracket");
2560 DEBUG_T( { PerlIO_printf(Perl_debug_log,
2561 "### Tokener got EOF\n");
2565 if (s++ < PL_bufend)
2566 goto retry; /* ignore stray nulls */
2569 if (!PL_in_eval && !PL_preambled) {
2570 PL_preambled = TRUE;
2571 sv_setpv(PL_linestr,incl_perldb());
2572 if (SvCUR(PL_linestr))
2573 sv_catpvn(PL_linestr,";", 1);
2575 while(AvFILLp(PL_preambleav) >= 0) {
2576 SV *tmpsv = av_shift(PL_preambleav);
2577 sv_catsv(PL_linestr, tmpsv);
2578 sv_catpvn(PL_linestr, ";", 1);
2581 sv_free((SV*)PL_preambleav);
2582 PL_preambleav = NULL;
2584 if (PL_minus_n || PL_minus_p) {
2585 sv_catpv(PL_linestr, "LINE: while (<>) {");
2587 sv_catpv(PL_linestr,"chomp;");
2590 if ((*PL_splitstr == '/' || *PL_splitstr == '\''
2591 || *PL_splitstr == '"')
2592 && strchr(PL_splitstr + 1, *PL_splitstr))
2593 Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
2595 /* "q\0${splitstr}\0" is legal perl. Yes, even NUL
2596 bytes can be used as quoting characters. :-) */
2597 /* The count here deliberately includes the NUL
2598 that terminates the C string constant. This
2599 embeds the opening NUL into the string. */
2600 sv_catpvn(PL_linestr, "our @F=split(q", 15);
2605 sv_catpvn(PL_linestr, s, 1);
2606 sv_catpvn(PL_linestr, s, 1);
2608 /* This loop will embed the trailing NUL of
2609 PL_linestr as the last thing it does before
2611 sv_catpvn(PL_linestr, ");", 2);
2615 sv_catpv(PL_linestr,"our @F=split(' ');");
2618 sv_catpvn(PL_linestr, "\n", 1);
2619 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2620 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
2621 PL_last_lop = PL_last_uni = Nullch;
2622 if (PERLDB_LINE && PL_curstash != PL_debstash) {
2623 SV *sv = NEWSV(85,0);
2625 sv_upgrade(sv, SVt_PVMG);
2626 sv_setsv(sv,PL_linestr);
2629 av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv);
2634 bof = PL_rsfp ? TRUE : FALSE;
2635 if ((s = filter_gets(PL_linestr, PL_rsfp, 0)) == Nullch) {
2638 if (PL_preprocess && !PL_in_eval)
2639 (void)PerlProc_pclose(PL_rsfp);
2640 else if ((PerlIO *)PL_rsfp == PerlIO_stdin())
2641 PerlIO_clearerr(PL_rsfp);
2643 (void)PerlIO_close(PL_rsfp);
2645 PL_doextract = FALSE;
2647 if (!PL_in_eval && (PL_minus_n || PL_minus_p)) {
2648 sv_setpv(PL_linestr,PL_minus_p
2649 ? ";}continue{print;}" : ";}");
2650 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2651 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
2652 PL_last_lop = PL_last_uni = Nullch;
2653 PL_minus_n = PL_minus_p = 0;
2656 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2657 PL_last_lop = PL_last_uni = Nullch;
2658 sv_setpv(PL_linestr,"");
2659 TOKEN(';'); /* not infinite loop because rsfp is NULL now */
2661 /* If it looks like the start of a BOM or raw UTF-16,
2662 * check if it in fact is. */
2668 #ifdef PERLIO_IS_STDIO
2669 # ifdef __GNU_LIBRARY__
2670 # if __GNU_LIBRARY__ == 1 /* Linux glibc5 */
2671 # define FTELL_FOR_PIPE_IS_BROKEN
2675 # if __GLIBC__ == 1 /* maybe some glibc5 release had it like this? */
2676 # define FTELL_FOR_PIPE_IS_BROKEN
2681 #ifdef FTELL_FOR_PIPE_IS_BROKEN
2682 /* This loses the possibility to detect the bof
2683 * situation on perl -P when the libc5 is being used.
2684 * Workaround? Maybe attach some extra state to PL_rsfp?
2687 bof = PerlIO_tell(PL_rsfp) == SvCUR(PL_linestr);
2689 bof = PerlIO_tell(PL_rsfp) == (Off_t)SvCUR(PL_linestr);
2692 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
2693 s = swallow_bom((U8*)s);
2697 /* Incest with pod. */
2698 if (*s == '=' && strnEQ(s, "=cut", 4)) {
2699 sv_setpv(PL_linestr, "");
2700 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2701 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
2702 PL_last_lop = PL_last_uni = Nullch;
2703 PL_doextract = FALSE;
2707 } while (PL_doextract);
2708 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = s;
2709 if (PERLDB_LINE && PL_curstash != PL_debstash) {
2710 SV *sv = NEWSV(85,0);
2712 sv_upgrade(sv, SVt_PVMG);
2713 sv_setsv(sv,PL_linestr);
2716 av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv);
2718 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
2719 PL_last_lop = PL_last_uni = Nullch;
2720 if (CopLINE(PL_curcop) == 1) {
2721 while (s < PL_bufend && isSPACE(*s))
2723 if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
2727 if (*s == '#' && *(s+1) == '!')
2729 #ifdef ALTERNATE_SHEBANG
2731 static char as[] = ALTERNATE_SHEBANG;
2732 if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
2733 d = s + (sizeof(as) - 1);
2735 #endif /* ALTERNATE_SHEBANG */
2744 while (*d && !isSPACE(*d))
2748 #ifdef ARG_ZERO_IS_SCRIPT
2749 if (ipathend > ipath) {
2751 * HP-UX (at least) sets argv[0] to the script name,
2752 * which makes $^X incorrect. And Digital UNIX and Linux,
2753 * at least, set argv[0] to the basename of the Perl
2754 * interpreter. So, having found "#!", we'll set it right.
2756 SV *x = GvSV(gv_fetchpv("\030", TRUE, SVt_PV)); /* $^X */
2757 assert(SvPOK(x) || SvGMAGICAL(x));
2758 if (sv_eq(x, CopFILESV(PL_curcop))) {
2759 sv_setpvn(x, ipath, ipathend - ipath);
2765 char *bstart = SvPV(CopFILESV(PL_curcop),blen);
2766 char *lstart = SvPV(x,llen);
2768 bstart += blen - llen;
2769 if (strnEQ(bstart, lstart, llen) && bstart[-1] == '/') {
2770 sv_setpvn(x, ipath, ipathend - ipath);
2775 TAINT_NOT; /* $^X is always tainted, but that's OK */
2777 #endif /* ARG_ZERO_IS_SCRIPT */
2782 d = instr(s,"perl -");
2784 d = instr(s,"perl");
2786 /* avoid getting into infinite loops when shebang
2787 * line contains "Perl" rather than "perl" */
2789 for (d = ipathend-4; d >= ipath; --d) {
2790 if ((*d == 'p' || *d == 'P')
2791 && !ibcmp(d, "perl", 4))
2801 #ifdef ALTERNATE_SHEBANG
2803 * If the ALTERNATE_SHEBANG on this system starts with a
2804 * character that can be part of a Perl expression, then if
2805 * we see it but not "perl", we're probably looking at the
2806 * start of Perl code, not a request to hand off to some
2807 * other interpreter. Similarly, if "perl" is there, but
2808 * not in the first 'word' of the line, we assume the line
2809 * contains the start of the Perl program.
2811 if (d && *s != '#') {
2813 while (*c && !strchr("; \t\r\n\f\v#", *c))
2816 d = Nullch; /* "perl" not in first word; ignore */
2818 *s = '#'; /* Don't try to parse shebang line */
2820 #endif /* ALTERNATE_SHEBANG */
2821 #ifndef MACOS_TRADITIONAL
2826 !instr(s,"indir") &&
2827 instr(PL_origargv[0],"perl"))
2833 while (s < PL_bufend && isSPACE(*s))
2835 if (s < PL_bufend) {
2836 Newz(899,newargv,PL_origargc+3,char*);
2838 while (s < PL_bufend && !isSPACE(*s))
2841 Copy(PL_origargv+1, newargv+2, PL_origargc+1, char*);
2844 newargv = PL_origargv;
2847 PerlProc_execv(ipath, EXEC_ARGV_CAST(newargv));
2849 Perl_croak(aTHX_ "Can't exec %s", ipath);
2853 U32 oldpdb = PL_perldb;
2854 bool oldn = PL_minus_n;
2855 bool oldp = PL_minus_p;
2857 while (*d && !isSPACE(*d)) d++;
2858 while (SPACE_OR_TAB(*d)) d++;
2861 bool switches_done = PL_doswitches;
2863 if (*d == 'M' || *d == 'm') {
2865 while (*d && !isSPACE(*d)) d++;
2866 Perl_croak(aTHX_ "Too late for \"-%.*s\" option",
2869 d = moreswitches(d);
2871 if (PL_doswitches && !switches_done) {
2872 int argc = PL_origargc;
2873 char **argv = PL_origargv;
2876 } while (argc && argv[0][0] == '-' && argv[0][1]);
2877 init_argv_symbols(argc,argv);
2879 if ((PERLDB_LINE && !oldpdb) ||
2880 ((PL_minus_n || PL_minus_p) && !(oldn || oldp)))
2881 /* if we have already added "LINE: while (<>) {",
2882 we must not do it again */
2884 sv_setpv(PL_linestr, "");
2885 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2886 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
2887 PL_last_lop = PL_last_uni = Nullch;
2888 PL_preambled = FALSE;
2890 (void)gv_fetchfile(PL_origfilename);
2893 if (PL_doswitches && !switches_done) {
2894 int argc = PL_origargc;
2895 char **argv = PL_origargv;
2898 } while (argc && argv[0][0] == '-' && argv[0][1]);
2899 init_argv_symbols(argc,argv);
2905 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
2907 PL_lex_state = LEX_FORMLINE;
2912 #ifdef PERL_STRICT_CR
2913 Perl_warn(aTHX_ "Illegal character \\%03o (carriage return)", '\r');
2915 "\t(Maybe you didn't strip carriage returns after a network transfer?)\n");
2917 case ' ': case '\t': case '\f': case 013:
2918 #ifdef MACOS_TRADITIONAL
2925 if (PL_lex_state != LEX_NORMAL || (PL_in_eval && !PL_rsfp)) {
2926 if (*s == '#' && s == PL_linestart && PL_in_eval && !PL_rsfp) {
2927 /* handle eval qq[#line 1 "foo"\n ...] */
2928 CopLINE_dec(PL_curcop);
2932 while (s < d && *s != '\n')
2936 else if (s > d) /* Found by Ilya: feed random input to Perl. */
2937 Perl_croak(aTHX_ "panic: input overflow");
2939 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
2941 PL_lex_state = LEX_FORMLINE;
2951 if (s[1] && isALPHA(s[1]) && !isALNUM(s[2])) {
2958 while (s < PL_bufend && SPACE_OR_TAB(*s))
2961 if (strnEQ(s,"=>",2)) {
2962 s = force_word(PL_bufptr,WORD,FALSE,FALSE,FALSE);
2963 DEBUG_T( { PerlIO_printf(Perl_debug_log,
2964 "### Saw unary minus before =>, forcing word '%s'\n", s);
2966 OPERATOR('-'); /* unary minus */
2968 PL_last_uni = PL_oldbufptr;
2970 case 'r': ftst = OP_FTEREAD; break;
2971 case 'w': ftst = OP_FTEWRITE; break;
2972 case 'x': ftst = OP_FTEEXEC; break;
2973 case 'o': ftst = OP_FTEOWNED; break;
2974 case 'R': ftst = OP_FTRREAD; break;
2975 case 'W': ftst = OP_FTRWRITE; break;
2976 case 'X': ftst = OP_FTREXEC; break;
2977 case 'O': ftst = OP_FTROWNED; break;
2978 case 'e': ftst = OP_FTIS; break;
2979 case 'z': ftst = OP_FTZERO; break;
2980 case 's': ftst = OP_FTSIZE; break;
2981 case 'f': ftst = OP_FTFILE; break;
2982 case 'd': ftst = OP_FTDIR; break;
2983 case 'l': ftst = OP_FTLINK; break;
2984 case 'p': ftst = OP_FTPIPE; break;
2985 case 'S': ftst = OP_FTSOCK; break;
2986 case 'u': ftst = OP_FTSUID; break;
2987 case 'g': ftst = OP_FTSGID; break;
2988 case 'k': ftst = OP_FTSVTX; break;
2989 case 'b': ftst = OP_FTBLK; break;
2990 case 'c': ftst = OP_FTCHR; break;
2991 case 't': ftst = OP_FTTTY; break;
2992 case 'T': ftst = OP_FTTEXT; break;
2993 case 'B': ftst = OP_FTBINARY; break;
2994 case 'M': case 'A': case 'C':
2995 gv_fetchpv("\024",TRUE, SVt_PV);
2997 case 'M': ftst = OP_FTMTIME; break;
2998 case 'A': ftst = OP_FTATIME; break;
2999 case 'C': ftst = OP_FTCTIME; break;
3007 PL_last_lop_op = (OPCODE)ftst;
3008 DEBUG_T( { PerlIO_printf(Perl_debug_log,
3009 "### Saw file test %c\n", (int)ftst);
3014 /* Assume it was a minus followed by a one-letter named
3015 * subroutine call (or a -bareword), then. */
3016 DEBUG_T( { PerlIO_printf(Perl_debug_log,
3017 "### '-%c' looked like a file test but was not\n",
3026 if (PL_expect == XOPERATOR)
3031 else if (*s == '>') {
3034 if (isIDFIRST_lazy_if(s,UTF)) {
3035 s = force_word(s,METHOD,FALSE,TRUE,FALSE);
3043 if (PL_expect == XOPERATOR)
3046 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
3048 OPERATOR('-'); /* unary minus */
3055 if (PL_expect == XOPERATOR)
3060 if (PL_expect == XOPERATOR)
3063 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
3069 if (PL_expect != XOPERATOR) {
3070 s = scan_ident(s, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
3071 PL_expect = XOPERATOR;
3072 force_ident(PL_tokenbuf, '*');
3085 if (PL_expect == XOPERATOR) {
3089 PL_tokenbuf[0] = '%';
3090 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, TRUE);
3091 if (!PL_tokenbuf[1]) {
3094 PL_pending_ident = '%';
3113 switch (PL_expect) {
3116 if (!PL_in_my || PL_lex_state != LEX_NORMAL)
3118 PL_bufptr = s; /* update in case we back off */
3124 PL_expect = XTERMBLOCK;
3128 while (isIDFIRST_lazy_if(s,UTF)) {
3129 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
3130 if (isLOWER(*s) && (tmp = keyword(PL_tokenbuf, len))) {
3131 if (tmp < 0) tmp = -tmp;
3147 d = scan_str(d,TRUE,TRUE);
3149 /* MUST advance bufptr here to avoid bogus
3150 "at end of line" context messages from yyerror().
3152 PL_bufptr = s + len;
3153 yyerror("Unterminated attribute parameter in attribute list");
3156 return REPORT(0); /* EOF indicator */
3160 SV *sv = newSVpvn(s, len);
3161 sv_catsv(sv, PL_lex_stuff);
3162 attrs = append_elem(OP_LIST, attrs,
3163 newSVOP(OP_CONST, 0, sv));
3164 SvREFCNT_dec(PL_lex_stuff);
3165 PL_lex_stuff = Nullsv;
3168 if (len == 6 && strnEQ(s, "unique", len)) {
3169 if (PL_in_my == KEY_our)
3171 GvUNIQUE_on(cGVOPx_gv(yylval.opval));
3173 ; /* skip to avoid loading attributes.pm */
3176 Perl_croak(aTHX_ "The 'unique' attribute may only be applied to 'our' variables");
3179 /* NOTE: any CV attrs applied here need to be part of
3180 the CVf_BUILTIN_ATTRS define in cv.h! */
3181 else if (!PL_in_my && len == 6 && strnEQ(s, "lvalue", len))
3182 CvLVALUE_on(PL_compcv);
3183 else if (!PL_in_my && len == 6 && strnEQ(s, "locked", len))
3184 CvLOCKED_on(PL_compcv);
3185 else if (!PL_in_my && len == 6 && strnEQ(s, "method", len))
3186 CvMETHOD_on(PL_compcv);
3187 else if (!PL_in_my && len == 9 && strnEQ(s, "assertion", len))
3188 CvASSERTION_on(PL_compcv);
3189 /* After we've set the flags, it could be argued that
3190 we don't need to do the attributes.pm-based setting
3191 process, and shouldn't bother appending recognized
3192 flags. To experiment with that, uncomment the
3193 following "else". (Note that's already been
3194 uncommented. That keeps the above-applied built-in
3195 attributes from being intercepted (and possibly
3196 rejected) by a package's attribute routines, but is
3197 justified by the performance win for the common case
3198 of applying only built-in attributes.) */
3200 attrs = append_elem(OP_LIST, attrs,
3201 newSVOP(OP_CONST, 0,
3205 if (*s == ':' && s[1] != ':')
3208 break; /* require real whitespace or :'s */
3210 tmp = (PL_expect == XOPERATOR ? '=' : '{'); /*'}(' for vi */
3211 if (*s != ';' && *s != '}' && *s != tmp && (tmp != '=' || *s != ')')) {
3212 char q = ((*s == '\'') ? '"' : '\'');
3213 /* If here for an expression, and parsed no attrs, back off. */
3214 if (tmp == '=' && !attrs) {
3218 /* MUST advance bufptr here to avoid bogus "at end of line"
3219 context messages from yyerror().
3223 yyerror("Unterminated attribute list");
3225 yyerror(Perl_form(aTHX_ "Invalid separator character %c%c%c in attribute list",
3233 PL_nextval[PL_nexttoke].opval = attrs;
3241 if (PL_last_lop == PL_oldoldbufptr || PL_last_uni == PL_oldoldbufptr)
3242 PL_oldbufptr = PL_oldoldbufptr; /* allow print(STDOUT 123) */
3259 if (PL_lex_brackets <= 0)
3260 yyerror("Unmatched right square bracket");
3263 if (PL_lex_state == LEX_INTERPNORMAL) {
3264 if (PL_lex_brackets == 0) {
3265 if (*s != '[' && *s != '{' && (*s != '-' || s[1] != '>'))
3266 PL_lex_state = LEX_INTERPEND;
3273 if (PL_lex_brackets > 100) {
3274 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
3276 switch (PL_expect) {
3278 if (PL_lex_formbrack) {
3282 if (PL_oldoldbufptr == PL_last_lop)
3283 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
3285 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
3286 OPERATOR(HASHBRACK);
3288 while (s < PL_bufend && SPACE_OR_TAB(*s))
3291 PL_tokenbuf[0] = '\0';
3292 if (d < PL_bufend && *d == '-') {
3293 PL_tokenbuf[0] = '-';
3295 while (d < PL_bufend && SPACE_OR_TAB(*d))
3298 if (d < PL_bufend && isIDFIRST_lazy_if(d,UTF)) {
3299 d = scan_word(d, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
3301 while (d < PL_bufend && SPACE_OR_TAB(*d))
3304 char minus = (PL_tokenbuf[0] == '-');
3305 s = force_word(s + minus, WORD, FALSE, TRUE, FALSE);
3313 PL_lex_brackstack[PL_lex_brackets++] = XSTATE;
3318 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
3323 if (PL_oldoldbufptr == PL_last_lop)
3324 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
3326 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
3329 if (PL_expect == XREF && PL_lex_state == LEX_INTERPNORMAL) {
3331 /* This hack is to get the ${} in the message. */
3333 yyerror("syntax error");
3336 OPERATOR(HASHBRACK);
3338 /* This hack serves to disambiguate a pair of curlies
3339 * as being a block or an anon hash. Normally, expectation
3340 * determines that, but in cases where we're not in a
3341 * position to expect anything in particular (like inside
3342 * eval"") we have to resolve the ambiguity. This code
3343 * covers the case where the first term in the curlies is a
3344 * quoted string. Most other cases need to be explicitly
3345 * disambiguated by prepending a `+' before the opening
3346 * curly in order to force resolution as an anon hash.
3348 * XXX should probably propagate the outer expectation
3349 * into eval"" to rely less on this hack, but that could
3350 * potentially break current behavior of eval"".
3354 if (*s == '\'' || *s == '"' || *s == '`') {
3355 /* common case: get past first string, handling escapes */
3356 for (t++; t < PL_bufend && *t != *s;)
3357 if (*t++ == '\\' && (*t == '\\' || *t == *s))
3361 else if (*s == 'q') {
3364 || ((*t == 'q' || *t == 'x') && ++t < PL_bufend
3367 /* skip q//-like construct */
3369 char open, close, term;
3372 while (t < PL_bufend && isSPACE(*t))
3374 /* check for q => */
3375 if (t+1 < PL_bufend && t[0] == '=' && t[1] == '>') {
3376 OPERATOR(HASHBRACK);
3380 if (term && (tmps = strchr("([{< )]}> )]}>",term)))
3384 for (t++; t < PL_bufend; t++) {
3385 if (*t == '\\' && t+1 < PL_bufend && open != '\\')
3387 else if (*t == open)
3391 for (t++; t < PL_bufend; t++) {
3392 if (*t == '\\' && t+1 < PL_bufend)
3394 else if (*t == close && --brackets <= 0)
3396 else if (*t == open)
3403 /* skip plain q word */
3404 while (t < PL_bufend && isALNUM_lazy_if(t,UTF))
3407 else if (isALNUM_lazy_if(t,UTF)) {
3409 while (t < PL_bufend && isALNUM_lazy_if(t,UTF))
3412 while (t < PL_bufend && isSPACE(*t))
3414 /* if comma follows first term, call it an anon hash */
3415 /* XXX it could be a comma expression with loop modifiers */
3416 if (t < PL_bufend && ((*t == ',' && (*s == 'q' || !isLOWER(*s)))
3417 || (*t == '=' && t[1] == '>')))
3418 OPERATOR(HASHBRACK);
3419 if (PL_expect == XREF)
3422 PL_lex_brackstack[PL_lex_brackets-1] = XSTATE;
3428 yylval.ival = CopLINE(PL_curcop);
3429 if (isSPACE(*s) || *s == '#')
3430 PL_copline = NOLINE; /* invalidate current command line number */
3435 if (PL_lex_brackets <= 0)
3436 yyerror("Unmatched right curly bracket");
3438 PL_expect = (expectation)PL_lex_brackstack[--PL_lex_brackets];
3439 if (PL_lex_brackets < PL_lex_formbrack && PL_lex_state != LEX_INTERPNORMAL)
3440 PL_lex_formbrack = 0;
3441 if (PL_lex_state == LEX_INTERPNORMAL) {
3442 if (PL_lex_brackets == 0) {
3443 if (PL_expect & XFAKEBRACK) {
3444 PL_expect &= XENUMMASK;
3445 PL_lex_state = LEX_INTERPEND;
3447 return yylex(); /* ignore fake brackets */
3449 if (*s == '-' && s[1] == '>')
3450 PL_lex_state = LEX_INTERPENDMAYBE;
3451 else if (*s != '[' && *s != '{')
3452 PL_lex_state = LEX_INTERPEND;
3455 if (PL_expect & XFAKEBRACK) {
3456 PL_expect &= XENUMMASK;
3458 return yylex(); /* ignore fake brackets */
3468 if (PL_expect == XOPERATOR) {
3469 if (ckWARN(WARN_SEMICOLON)
3470 && isIDFIRST_lazy_if(s,UTF) && PL_bufptr == PL_linestart)
3472 CopLINE_dec(PL_curcop);
3473 Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), PL_warn_nosemi);
3474 CopLINE_inc(PL_curcop);
3479 s = scan_ident(s - 1, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
3481 PL_expect = XOPERATOR;
3482 force_ident(PL_tokenbuf, '&');
3486 yylval.ival = (OPpENTERSUB_AMPER<<8);
3505 if (ckWARN(WARN_SYNTAX) && tmp && isSPACE(*s) && strchr("+-*/%.^&|<",tmp))
3506 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Reversed %c= operator",(int)tmp);
3508 if (PL_expect == XSTATE && isALPHA(tmp) &&
3509 (s == PL_linestart+1 || s[-2] == '\n') )
3511 if (PL_in_eval && !PL_rsfp) {
3516 if (strnEQ(s,"=cut",4)) {
3530 PL_doextract = TRUE;
3533 if (PL_lex_brackets < PL_lex_formbrack) {
3535 #ifdef PERL_STRICT_CR
3536 for (t = s; SPACE_OR_TAB(*t); t++) ;
3538 for (t = s; SPACE_OR_TAB(*t) || *t == '\r'; t++) ;
3540 if (*t == '\n' || *t == '#') {
3552 /* was this !=~ where !~ was meant?
3553 * warn on m:!=~\s+([/?]|[msy]\W|tr\W): */
3555 if (*s == '~' && ckWARN(WARN_SYNTAX)) {
3558 while (t < PL_bufend && isSPACE(*t))
3561 if (*t == '/' || *t == '?' ||
3562 ((*t == 'm' || *t == 's' || *t == 'y') && !isALNUM(t[1])) ||
3563 (*t == 't' && t[1] == 'r' && !isALNUM(t[2])))
3564 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
3565 "!=~ should be !~");
3574 if (PL_expect != XOPERATOR) {
3575 if (s[1] != '<' && !strchr(s,'>'))
3578 s = scan_heredoc(s);
3580 s = scan_inputsymbol(s);
3581 TERM(sublex_start());
3586 SHop(OP_LEFT_SHIFT);
3600 SHop(OP_RIGHT_SHIFT);
3609 if (PL_expect == XOPERATOR) {
3610 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
3613 return REPORT(','); /* grandfather non-comma-format format */
3617 if (s[1] == '#' && (isIDFIRST_lazy_if(s+2,UTF) || strchr("{$:+-", s[2]))) {
3618 PL_tokenbuf[0] = '@';
3619 s = scan_ident(s + 1, PL_bufend, PL_tokenbuf + 1,
3620 sizeof PL_tokenbuf - 1, FALSE);
3621 if (PL_expect == XOPERATOR)
3622 no_op("Array length", s);
3623 if (!PL_tokenbuf[1])
3625 PL_expect = XOPERATOR;
3626 PL_pending_ident = '#';
3630 PL_tokenbuf[0] = '$';
3631 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1,
3632 sizeof PL_tokenbuf - 1, FALSE);
3633 if (PL_expect == XOPERATOR)
3635 if (!PL_tokenbuf[1]) {
3637 yyerror("Final $ should be \\$ or $name");
3641 /* This kludge not intended to be bulletproof. */
3642 if (PL_tokenbuf[1] == '[' && !PL_tokenbuf[2]) {
3643 yylval.opval = newSVOP(OP_CONST, 0,
3644 newSViv(PL_compiling.cop_arybase));
3645 yylval.opval->op_private = OPpCONST_ARYBASE;
3651 if (PL_lex_state == LEX_NORMAL)
3654 if ((PL_expect != XREF || PL_oldoldbufptr == PL_last_lop) && intuit_more(s)) {
3657 PL_tokenbuf[0] = '@';
3658 if (ckWARN(WARN_SYNTAX)) {
3660 isSPACE(*t) || isALNUM_lazy_if(t,UTF) || *t == '$';
3663 PL_bufptr = skipspace(PL_bufptr);
3664 while (t < PL_bufend && *t != ']')
3666 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
3667 "Multidimensional syntax %.*s not supported",
3668 (t - PL_bufptr) + 1, PL_bufptr);
3672 else if (*s == '{') {
3673 PL_tokenbuf[0] = '%';
3674 if (ckWARN(WARN_SYNTAX) && strEQ(PL_tokenbuf+1, "SIG") &&
3675 (t = strchr(s, '}')) && (t = strchr(t, '=')))
3677 char tmpbuf[sizeof PL_tokenbuf];
3679 for (t++; isSPACE(*t); t++) ;
3680 if (isIDFIRST_lazy_if(t,UTF)) {
3681 t = scan_word(t, tmpbuf, sizeof tmpbuf, TRUE, &len);
3682 for (; isSPACE(*t); t++) ;
3683 if (*t == ';' && get_cv(tmpbuf, FALSE))
3684 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
3685 "You need to quote \"%s\"", tmpbuf);
3691 PL_expect = XOPERATOR;
3692 if (PL_lex_state == LEX_NORMAL && isSPACE((char)tmp)) {
3693 bool islop = (PL_last_lop == PL_oldoldbufptr);
3694 if (!islop || PL_last_lop_op == OP_GREPSTART)
3695 PL_expect = XOPERATOR;
3696 else if (strchr("$@\"'`q", *s))
3697 PL_expect = XTERM; /* e.g. print $fh "foo" */
3698 else if (strchr("&*<%", *s) && isIDFIRST_lazy_if(s+1,UTF))
3699 PL_expect = XTERM; /* e.g. print $fh &sub */
3700 else if (isIDFIRST_lazy_if(s,UTF)) {
3701 char tmpbuf[sizeof PL_tokenbuf];
3702 scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
3703 if ((tmp = keyword(tmpbuf, len))) {
3704 /* binary operators exclude handle interpretations */
3716 PL_expect = XTERM; /* e.g. print $fh length() */
3721 PL_expect = XTERM; /* e.g. print $fh subr() */
3724 else if (isDIGIT(*s))
3725 PL_expect = XTERM; /* e.g. print $fh 3 */
3726 else if (*s == '.' && isDIGIT(s[1]))
3727 PL_expect = XTERM; /* e.g. print $fh .3 */
3728 else if ((*s == '?' || *s == '-' || *s == '+')
3729 && !isSPACE(s[1]) && s[1] != '=')
3730 PL_expect = XTERM; /* e.g. print $fh -1 */
3731 else if (*s == '/' && !isSPACE(s[1]) && s[1] != '=' && s[1] != '/')
3732 PL_expect = XTERM; /* e.g. print $fh /.../
3733 XXX except DORDOR operator */
3734 else if (*s == '<' && s[1] == '<' && !isSPACE(s[2]) && s[2] != '=')
3735 PL_expect = XTERM; /* print $fh <<"EOF" */
3737 PL_pending_ident = '$';
3741 if (PL_expect == XOPERATOR)
3743 PL_tokenbuf[0] = '@';
3744 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
3745 if (!PL_tokenbuf[1]) {
3748 if (PL_lex_state == LEX_NORMAL)
3750 if ((PL_expect != XREF || PL_oldoldbufptr == PL_last_lop) && intuit_more(s)) {
3752 PL_tokenbuf[0] = '%';
3754 /* Warn about @ where they meant $. */
3755 if (ckWARN(WARN_SYNTAX)) {
3756 if (*s == '[' || *s == '{') {
3758 while (*t && (isALNUM_lazy_if(t,UTF) || strchr(" \t$#+-'\"", *t)))
3760 if (*t == '}' || *t == ']') {
3762 PL_bufptr = skipspace(PL_bufptr);
3763 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
3764 "Scalar value %.*s better written as $%.*s",
3765 t-PL_bufptr, PL_bufptr, t-PL_bufptr-1, PL_bufptr+1);
3770 PL_pending_ident = '@';
3773 case '/': /* may be division, defined-or, or pattern */
3774 if (PL_expect == XTERMORDORDOR && s[1] == '/') {
3778 case '?': /* may either be conditional or pattern */
3779 if(PL_expect == XOPERATOR) {
3787 /* A // operator. */
3797 /* Disable warning on "study /blah/" */
3798 if (PL_oldoldbufptr == PL_last_uni
3799 && (*PL_last_uni != 's' || s - PL_last_uni < 5
3800 || memNE(PL_last_uni, "study", 5)
3801 || isALNUM_lazy_if(PL_last_uni+5,UTF)
3804 s = scan_pat(s,OP_MATCH);
3805 TERM(sublex_start());
3809 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack
3810 #ifdef PERL_STRICT_CR
3813 && (s[1] == '\n' || (s[1] == '\r' && s[2] == '\n'))
3815 && (s == PL_linestart || s[-1] == '\n') )
3817 PL_lex_formbrack = 0;
3821 if (PL_expect == XOPERATOR || !isDIGIT(s[1])) {
3827 yylval.ival = OPf_SPECIAL;
3833 if (PL_expect != XOPERATOR)
3838 case '0': case '1': case '2': case '3': case '4':
3839 case '5': case '6': case '7': case '8': case '9':
3840 s = scan_num(s, &yylval);
3841 DEBUG_T( { PerlIO_printf(Perl_debug_log,
3842 "### Saw number in '%s'\n", s);
3844 if (PL_expect == XOPERATOR)
3849 s = scan_str(s,FALSE,FALSE);
3850 DEBUG_T( { PerlIO_printf(Perl_debug_log,
3851 "### Saw string before '%s'\n", s);
3853 if (PL_expect == XOPERATOR) {
3854 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
3857 return REPORT(','); /* grandfather non-comma-format format */
3863 missingterm((char*)0);
3864 yylval.ival = OP_CONST;
3865 TERM(sublex_start());
3868 s = scan_str(s,FALSE,FALSE);
3869 DEBUG_T( { PerlIO_printf(Perl_debug_log,
3870 "### Saw string before '%s'\n", s);
3872 if (PL_expect == XOPERATOR) {
3873 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
3876 return REPORT(','); /* grandfather non-comma-format format */
3882 missingterm((char*)0);
3883 yylval.ival = OP_CONST;
3884 for (d = SvPV(PL_lex_stuff, len); len; len--, d++) {
3885 if (*d == '$' || *d == '@' || *d == '\\' || !UTF8_IS_INVARIANT((U8)*d)) {
3886 yylval.ival = OP_STRINGIFY;
3890 TERM(sublex_start());
3893 s = scan_str(s,FALSE,FALSE);
3894 DEBUG_T( { PerlIO_printf(Perl_debug_log,
3895 "### Saw backtick string before '%s'\n", s);
3897 if (PL_expect == XOPERATOR)
3898 no_op("Backticks",s);
3900 missingterm((char*)0);
3901 yylval.ival = OP_BACKTICK;
3903 TERM(sublex_start());
3907 if (ckWARN(WARN_SYNTAX) && PL_lex_inwhat && isDIGIT(*s))
3908 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),"Can't use \\%c to mean $%c in expression",
3910 if (PL_expect == XOPERATOR)
3911 no_op("Backslash",s);
3915 if (isDIGIT(s[1]) && PL_expect != XOPERATOR) {
3919 while (isDIGIT(*start) || *start == '_')
3921 if (*start == '.' && isDIGIT(start[1])) {
3922 s = scan_num(s, &yylval);
3925 /* avoid v123abc() or $h{v1}, allow C<print v10;> */
3926 else if (!isALPHA(*start) && (PL_expect == XTERM
3927 || PL_expect == XREF || PL_expect == XSTATE
3928 || PL_expect == XTERMORDORDOR)) {
3932 gv = gv_fetchpv(s, FALSE, SVt_PVCV);
3935 s = scan_num(s, &yylval);
3942 if (isDIGIT(s[1]) && PL_expect == XOPERATOR) {
3982 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
3984 /* Some keywords can be followed by any delimiter, including ':' */
3985 tmp = ((len == 1 && strchr("msyq", PL_tokenbuf[0])) ||
3986 (len == 2 && ((PL_tokenbuf[0] == 't' && PL_tokenbuf[1] == 'r') ||
3987 (PL_tokenbuf[0] == 'q' &&
3988 strchr("qwxr", PL_tokenbuf[1])))));
3990 /* x::* is just a word, unless x is "CORE" */
3991 if (!tmp && *s == ':' && s[1] == ':' && strNE(PL_tokenbuf, "CORE"))
3995 while (d < PL_bufend && isSPACE(*d))
3996 d++; /* no comments skipped here, or s### is misparsed */
3998 /* Is this a label? */
3999 if (!tmp && PL_expect == XSTATE
4000 && d < PL_bufend && *d == ':' && *(d + 1) != ':') {
4002 yylval.pval = savepv(PL_tokenbuf);
4007 /* Check for keywords */
4008 tmp = keyword(PL_tokenbuf, len);
4010 /* Is this a word before a => operator? */
4011 if (*d == '=' && d[1] == '>') {
4013 yylval.opval = (OP*)newSVOP(OP_CONST, 0, newSVpv(PL_tokenbuf,0));
4014 yylval.opval->op_private = OPpCONST_BARE;
4015 if (UTF && !IN_BYTES && is_utf8_string((U8*)PL_tokenbuf, len))
4016 SvUTF8_on(((SVOP*)yylval.opval)->op_sv);
4020 if (tmp < 0) { /* second-class keyword? */
4021 GV *ogv = Nullgv; /* override (winner) */
4022 GV *hgv = Nullgv; /* hidden (loser) */
4023 if (PL_expect != XOPERATOR && (*s != ':' || s[1] != ':')) {
4025 if ((gv = gv_fetchpv(PL_tokenbuf, FALSE, SVt_PVCV)) &&
4028 if (GvIMPORTED_CV(gv))
4030 else if (! CvMETHOD(cv))
4034 (gvp = (GV**)hv_fetch(PL_globalstash,PL_tokenbuf,len,FALSE)) &&
4035 (gv = *gvp) != (GV*)&PL_sv_undef &&
4036 GvCVu(gv) && GvIMPORTED_CV(gv))
4043 tmp = 0; /* overridden by import or by GLOBAL */
4046 && -tmp==KEY_lock /* XXX generalizable kludge */
4048 && !hv_fetch(GvHVn(PL_incgv), "Thread.pm", 9, FALSE))
4050 tmp = 0; /* any sub overrides "weak" keyword */
4055 && PL_expect != XOPERATOR
4056 && PL_expect != XTERMORDORDOR)
4058 /* any sub overrides the "err" keyword, except when really an
4059 * operator is expected */
4062 else { /* no override */
4064 if (tmp == KEY_dump && ckWARN(WARN_MISC)) {
4065 Perl_warner(aTHX_ packWARN(WARN_MISC),
4066 "dump() better written as CORE::dump()");
4070 if (ckWARN(WARN_AMBIGUOUS) && hgv
4071 && tmp != KEY_x && tmp != KEY_CORE) /* never ambiguous */
4072 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
4073 "Ambiguous call resolved as CORE::%s(), %s",
4074 GvENAME(hgv), "qualify as such or use &");
4081 default: /* not a keyword */
4085 char lastchar = (PL_bufptr == PL_oldoldbufptr ? 0 : PL_bufptr[-1]);
4087 /* Get the rest if it looks like a package qualifier */
4089 if (*s == '\'' || (*s == ':' && s[1] == ':')) {
4091 s = scan_word(s, PL_tokenbuf + len, sizeof PL_tokenbuf - len,
4094 Perl_croak(aTHX_ "Bad name after %s%s", PL_tokenbuf,
4095 *s == '\'' ? "'" : "::");
4100 if (PL_expect == XOPERATOR) {
4101 if (PL_bufptr == PL_linestart) {
4102 CopLINE_dec(PL_curcop);
4103 Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), PL_warn_nosemi);
4104 CopLINE_inc(PL_curcop);
4107 no_op("Bareword",s);
4110 /* Look for a subroutine with this name in current package,
4111 unless name is "Foo::", in which case Foo is a bearword
4112 (and a package name). */
4115 PL_tokenbuf[len - 2] == ':' && PL_tokenbuf[len - 1] == ':')
4117 if (ckWARN(WARN_BAREWORD) && ! gv_fetchpv(PL_tokenbuf, FALSE, SVt_PVHV))
4118 Perl_warner(aTHX_ packWARN(WARN_BAREWORD),
4119 "Bareword \"%s\" refers to nonexistent package",
4122 PL_tokenbuf[len] = '\0';
4129 gv = gv_fetchpv(PL_tokenbuf, FALSE, SVt_PVCV);
4132 /* if we saw a global override before, get the right name */
4135 sv = newSVpvn("CORE::GLOBAL::",14);
4136 sv_catpv(sv,PL_tokenbuf);
4139 sv = newSVpv(PL_tokenbuf,0);
4141 /* Presume this is going to be a bareword of some sort. */
4144 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
4145 yylval.opval->op_private = OPpCONST_BARE;
4146 /* UTF-8 package name? */
4147 if (UTF && !IN_BYTES &&
4148 is_utf8_string((U8*)SvPVX(sv), SvCUR(sv)))
4151 /* And if "Foo::", then that's what it certainly is. */
4156 /* See if it's the indirect object for a list operator. */
4158 if (PL_oldoldbufptr &&
4159 PL_oldoldbufptr < PL_bufptr &&
4160 (PL_oldoldbufptr == PL_last_lop
4161 || PL_oldoldbufptr == PL_last_uni) &&
4162 /* NO SKIPSPACE BEFORE HERE! */
4163 (PL_expect == XREF ||
4164 ((PL_opargs[PL_last_lop_op] >> OASHIFT)& 7) == OA_FILEREF))
4166 bool immediate_paren = *s == '(';
4168 /* (Now we can afford to cross potential line boundary.) */
4171 /* Two barewords in a row may indicate method call. */
4173 if ((isIDFIRST_lazy_if(s,UTF) || *s == '$') && (tmp=intuit_method(s,gv)))
4176 /* If not a declared subroutine, it's an indirect object. */
4177 /* (But it's an indir obj regardless for sort.) */
4179 if ( !immediate_paren && (PL_last_lop_op == OP_SORT ||
4180 ((!gv || !GvCVu(gv)) &&
4181 (PL_last_lop_op != OP_MAPSTART &&
4182 PL_last_lop_op != OP_GREPSTART))))
4184 PL_expect = (PL_last_lop == PL_oldoldbufptr) ? XTERM : XOPERATOR;
4189 PL_expect = XOPERATOR;
4192 /* Is this a word before a => operator? */
4193 if (*s == '=' && s[1] == '>' && !pkgname) {
4195 sv_setpv(((SVOP*)yylval.opval)->op_sv, PL_tokenbuf);
4196 if (UTF && !IN_BYTES && is_utf8_string((U8*)PL_tokenbuf, len))
4197 SvUTF8_on(((SVOP*)yylval.opval)->op_sv);
4201 /* If followed by a paren, it's certainly a subroutine. */
4204 if (gv && GvCVu(gv)) {
4205 for (d = s + 1; SPACE_OR_TAB(*d); d++) ;
4206 if (*d == ')' && (sv = cv_const_sv(GvCV(gv)))) {
4211 PL_nextval[PL_nexttoke].opval = yylval.opval;
4212 PL_expect = XOPERATOR;
4218 /* If followed by var or block, call it a method (unless sub) */
4220 if ((*s == '$' || *s == '{') && (!gv || !GvCVu(gv))) {
4221 PL_last_lop = PL_oldbufptr;
4222 PL_last_lop_op = OP_METHOD;
4226 /* If followed by a bareword, see if it looks like indir obj. */
4229 && (isIDFIRST_lazy_if(s,UTF) || *s == '$')
4230 && (tmp = intuit_method(s,gv)))
4233 /* Not a method, so call it a subroutine (if defined) */
4235 if (gv && GvCVu(gv)) {
4237 if (lastchar == '-' && ckWARN_d(WARN_AMBIGUOUS))
4238 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
4239 "Ambiguous use of -%s resolved as -&%s()",
4240 PL_tokenbuf, PL_tokenbuf);
4241 /* Check for a constant sub */
4243 if ((sv = cv_const_sv(cv))) {
4245 SvREFCNT_dec(((SVOP*)yylval.opval)->op_sv);
4246 ((SVOP*)yylval.opval)->op_sv = SvREFCNT_inc(sv);
4247 yylval.opval->op_private = 0;