3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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
14 * [p.719 of _The Lord of the Rings_, IV/ix: "Shelob's Lair"]
18 * This file is the lexer for Perl. It's closely linked to the
21 * The main routine is yylex(), which returns the next token.
25 =head1 Lexer interface
27 This is the lower layer of the Perl parser, managing characters and tokens.
29 =for apidoc AmU|yy_parser *|PL_parser
31 Pointer to a structure encapsulating the state of the parsing operation
32 currently in progress. The pointer can be locally changed to perform
33 a nested parse without interfering with the state of an outer parse.
34 Individual members of C<PL_parser> have their own documentation.
40 #define PERL_IN_TOKE_C
42 #include "dquote_static.c"
44 #define new_constant(a,b,c,d,e,f,g) \
45 S_new_constant(aTHX_ a,b,STR_WITH_LEN(c),d,e,f, g)
47 #define pl_yylval (PL_parser->yylval)
49 /* XXX temporary backwards compatibility */
50 #define PL_lex_brackets (PL_parser->lex_brackets)
51 #define PL_lex_brackstack (PL_parser->lex_brackstack)
52 #define PL_lex_casemods (PL_parser->lex_casemods)
53 #define PL_lex_casestack (PL_parser->lex_casestack)
54 #define PL_lex_defer (PL_parser->lex_defer)
55 #define PL_lex_dojoin (PL_parser->lex_dojoin)
56 #define PL_lex_expect (PL_parser->lex_expect)
57 #define PL_lex_formbrack (PL_parser->lex_formbrack)
58 #define PL_lex_inpat (PL_parser->lex_inpat)
59 #define PL_lex_inwhat (PL_parser->lex_inwhat)
60 #define PL_lex_op (PL_parser->lex_op)
61 #define PL_lex_repl (PL_parser->lex_repl)
62 #define PL_lex_starts (PL_parser->lex_starts)
63 #define PL_lex_stuff (PL_parser->lex_stuff)
64 #define PL_multi_start (PL_parser->multi_start)
65 #define PL_multi_open (PL_parser->multi_open)
66 #define PL_multi_close (PL_parser->multi_close)
67 #define PL_pending_ident (PL_parser->pending_ident)
68 #define PL_preambled (PL_parser->preambled)
69 #define PL_sublex_info (PL_parser->sublex_info)
70 #define PL_linestr (PL_parser->linestr)
71 #define PL_expect (PL_parser->expect)
72 #define PL_copline (PL_parser->copline)
73 #define PL_bufptr (PL_parser->bufptr)
74 #define PL_oldbufptr (PL_parser->oldbufptr)
75 #define PL_oldoldbufptr (PL_parser->oldoldbufptr)
76 #define PL_linestart (PL_parser->linestart)
77 #define PL_bufend (PL_parser->bufend)
78 #define PL_last_uni (PL_parser->last_uni)
79 #define PL_last_lop (PL_parser->last_lop)
80 #define PL_last_lop_op (PL_parser->last_lop_op)
81 #define PL_lex_state (PL_parser->lex_state)
82 #define PL_rsfp (PL_parser->rsfp)
83 #define PL_rsfp_filters (PL_parser->rsfp_filters)
84 #define PL_in_my (PL_parser->in_my)
85 #define PL_in_my_stash (PL_parser->in_my_stash)
86 #define PL_tokenbuf (PL_parser->tokenbuf)
87 #define PL_multi_end (PL_parser->multi_end)
88 #define PL_error_count (PL_parser->error_count)
91 # define PL_endwhite (PL_parser->endwhite)
92 # define PL_faketokens (PL_parser->faketokens)
93 # define PL_lasttoke (PL_parser->lasttoke)
94 # define PL_nextwhite (PL_parser->nextwhite)
95 # define PL_realtokenstart (PL_parser->realtokenstart)
96 # define PL_skipwhite (PL_parser->skipwhite)
97 # define PL_thisclose (PL_parser->thisclose)
98 # define PL_thismad (PL_parser->thismad)
99 # define PL_thisopen (PL_parser->thisopen)
100 # define PL_thisstuff (PL_parser->thisstuff)
101 # define PL_thistoken (PL_parser->thistoken)
102 # define PL_thiswhite (PL_parser->thiswhite)
103 # define PL_thiswhite (PL_parser->thiswhite)
104 # define PL_nexttoke (PL_parser->nexttoke)
105 # define PL_curforce (PL_parser->curforce)
107 # define PL_nexttoke (PL_parser->nexttoke)
108 # define PL_nexttype (PL_parser->nexttype)
109 # define PL_nextval (PL_parser->nextval)
112 /* This can't be done with embed.fnc, because struct yy_parser contains a
113 member named pending_ident, which clashes with the generated #define */
115 S_pending_ident(pTHX);
117 static const char ident_too_long[] = "Identifier too long";
120 # define CURMAD(slot,sv) if (PL_madskills) { curmad(slot,sv); sv = 0; }
121 # define NEXTVAL_NEXTTOKE PL_nexttoke[PL_curforce].next_val
123 # define CURMAD(slot,sv)
124 # define NEXTVAL_NEXTTOKE PL_nextval[PL_nexttoke]
127 #define XENUMMASK 0x3f
128 #define XFAKEEOF 0x40
129 #define XFAKEBRACK 0x80
131 #ifdef USE_UTF8_SCRIPTS
132 # define UTF (!IN_BYTES)
134 # define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8))
137 /* The maximum number of characters preceding the unrecognized one to display */
138 #define UNRECOGNIZED_PRECEDE_COUNT 10
140 /* In variables named $^X, these are the legal values for X.
141 * 1999-02-27 mjd-perl-patch@plover.com */
142 #define isCONTROLVAR(x) (isUPPER(x) || strchr("[\\]^_?", (x)))
144 #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t')
146 /* LEX_* are values for PL_lex_state, the state of the lexer.
147 * They are arranged oddly so that the guard on the switch statement
148 * can get by with a single comparison (if the compiler is smart enough).
151 /* #define LEX_NOTPARSING 11 is done in perl.h. */
153 #define LEX_NORMAL 10 /* normal code (ie not within "...") */
154 #define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */
155 #define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */
156 #define LEX_INTERPPUSH 7 /* starting a new sublex parse level */
157 #define LEX_INTERPSTART 6 /* expecting the start of a $var */
159 /* at end of code, eg "$x" followed by: */
160 #define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */
161 #define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */
163 #define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of
164 string or after \E, $foo, etc */
165 #define LEX_INTERPCONST 2 /* NOT USED */
166 #define LEX_FORMLINE 1 /* expecting a format line */
167 #define LEX_KNOWNEXT 0 /* next token known; just return it */
171 static const char* const lex_state_names[] = {
190 #include "keywords.h"
192 /* CLINE is a macro that ensures PL_copline has a sane value */
197 #define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline))
200 # define SKIPSPACE0(s) skipspace0(s)
201 # define SKIPSPACE1(s) skipspace1(s)
202 # define SKIPSPACE2(s,tsv) skipspace2(s,&tsv)
203 # define PEEKSPACE(s) skipspace2(s,0)
205 # define SKIPSPACE0(s) skipspace(s)
206 # define SKIPSPACE1(s) skipspace(s)
207 # define SKIPSPACE2(s,tsv) skipspace(s)
208 # define PEEKSPACE(s) skipspace(s)
212 * Convenience functions to return different tokens and prime the
213 * lexer for the next token. They all take an argument.
215 * TOKEN : generic token (used for '(', DOLSHARP, etc)
216 * OPERATOR : generic operator
217 * AOPERATOR : assignment operator
218 * PREBLOCK : beginning the block after an if, while, foreach, ...
219 * PRETERMBLOCK : beginning a non-code-defining {} block (eg, hash ref)
220 * PREREF : *EXPR where EXPR is not a simple identifier
221 * TERM : expression term
222 * LOOPX : loop exiting command (goto, last, dump, etc)
223 * FTST : file test operator
224 * FUN0 : zero-argument function
225 * FUN1 : not used, except for not, which isn't a UNIOP
226 * BOop : bitwise or or xor
228 * SHop : shift operator
229 * PWop : power operator
230 * PMop : pattern-matching operator
231 * Aop : addition-level operator
232 * Mop : multiplication-level operator
233 * Eop : equality-testing operator
234 * Rop : relational operator <= != gt
236 * Also see LOP and lop() below.
239 #ifdef DEBUGGING /* Serve -DT. */
240 # define REPORT(retval) tokereport((I32)retval, &pl_yylval)
242 # define REPORT(retval) (retval)
245 #define TOKEN(retval) return ( PL_bufptr = s, REPORT(retval))
246 #define OPERATOR(retval) return (PL_expect = XTERM, PL_bufptr = s, REPORT(retval))
247 #define AOPERATOR(retval) return ao((PL_expect = XTERM, PL_bufptr = s, REPORT(retval)))
248 #define PREBLOCK(retval) return (PL_expect = XBLOCK,PL_bufptr = s, REPORT(retval))
249 #define PRETERMBLOCK(retval) return (PL_expect = XTERMBLOCK,PL_bufptr = s, REPORT(retval))
250 #define PREREF(retval) return (PL_expect = XREF,PL_bufptr = s, REPORT(retval))
251 #define TERM(retval) return (CLINE, PL_expect = XOPERATOR, PL_bufptr = s, REPORT(retval))
252 #define LOOPX(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)LOOPEX))
253 #define FTST(f) return (pl_yylval.ival=f, PL_expect=XTERMORDORDOR, PL_bufptr=s, REPORT((int)UNIOP))
254 #define FUN0(f) return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0))
255 #define FUN1(f) return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC1))
256 #define BOop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITOROP)))
257 #define BAop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITANDOP)))
258 #define SHop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)SHIFTOP)))
259 #define PWop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)POWOP)))
260 #define PMop(f) return(pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MATCHOP))
261 #define Aop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)ADDOP)))
262 #define Mop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MULOP)))
263 #define Eop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)EQOP))
264 #define Rop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)RELOP))
266 /* This bit of chicanery makes a unary function followed by
267 * a parenthesis into a function with one argument, highest precedence.
268 * The UNIDOR macro is for unary functions that can be followed by the //
269 * operator (such as C<shift // 0>).
271 #define UNI2(f,x) { \
272 pl_yylval.ival = f; \
275 PL_last_uni = PL_oldbufptr; \
276 PL_last_lop_op = f; \
278 return REPORT( (int)FUNC1 ); \
280 return REPORT( *s=='(' ? (int)FUNC1 : (int)UNIOP ); \
282 #define UNI(f) UNI2(f,XTERM)
283 #define UNIDOR(f) UNI2(f,XTERMORDORDOR)
285 #define UNIBRACK(f) { \
286 pl_yylval.ival = f; \
288 PL_last_uni = PL_oldbufptr; \
290 return REPORT( (int)FUNC1 ); \
292 return REPORT( (*s == '(') ? (int)FUNC1 : (int)UNIOP ); \
295 /* grandfather return to old style */
296 #define OLDLOP(f) return(pl_yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)LSTOP)
300 /* how to interpret the pl_yylval associated with the token */
304 TOKENTYPE_OPNUM, /* pl_yylval.ival contains an opcode number */
310 static struct debug_tokens {
312 enum token_type type;
314 } const debug_tokens[] =
316 { ADDOP, TOKENTYPE_OPNUM, "ADDOP" },
317 { ANDAND, TOKENTYPE_NONE, "ANDAND" },
318 { ANDOP, TOKENTYPE_NONE, "ANDOP" },
319 { ANONSUB, TOKENTYPE_IVAL, "ANONSUB" },
320 { ARROW, TOKENTYPE_NONE, "ARROW" },
321 { ASSIGNOP, TOKENTYPE_OPNUM, "ASSIGNOP" },
322 { BITANDOP, TOKENTYPE_OPNUM, "BITANDOP" },
323 { BITOROP, TOKENTYPE_OPNUM, "BITOROP" },
324 { COLONATTR, TOKENTYPE_NONE, "COLONATTR" },
325 { CONTINUE, TOKENTYPE_NONE, "CONTINUE" },
326 { DEFAULT, TOKENTYPE_NONE, "DEFAULT" },
327 { DO, TOKENTYPE_NONE, "DO" },
328 { DOLSHARP, TOKENTYPE_NONE, "DOLSHARP" },
329 { DORDOR, TOKENTYPE_NONE, "DORDOR" },
330 { DOROP, TOKENTYPE_OPNUM, "DOROP" },
331 { DOTDOT, TOKENTYPE_IVAL, "DOTDOT" },
332 { ELSE, TOKENTYPE_NONE, "ELSE" },
333 { ELSIF, TOKENTYPE_IVAL, "ELSIF" },
334 { EQOP, TOKENTYPE_OPNUM, "EQOP" },
335 { FOR, TOKENTYPE_IVAL, "FOR" },
336 { FORMAT, TOKENTYPE_NONE, "FORMAT" },
337 { FUNC, TOKENTYPE_OPNUM, "FUNC" },
338 { FUNC0, TOKENTYPE_OPNUM, "FUNC0" },
339 { FUNC0SUB, TOKENTYPE_OPVAL, "FUNC0SUB" },
340 { FUNC1, TOKENTYPE_OPNUM, "FUNC1" },
341 { FUNCMETH, TOKENTYPE_OPVAL, "FUNCMETH" },
342 { GIVEN, TOKENTYPE_IVAL, "GIVEN" },
343 { HASHBRACK, TOKENTYPE_NONE, "HASHBRACK" },
344 { IF, TOKENTYPE_IVAL, "IF" },
345 { LABEL, TOKENTYPE_PVAL, "LABEL" },
346 { LOCAL, TOKENTYPE_IVAL, "LOCAL" },
347 { LOOPEX, TOKENTYPE_OPNUM, "LOOPEX" },
348 { LSTOP, TOKENTYPE_OPNUM, "LSTOP" },
349 { LSTOPSUB, TOKENTYPE_OPVAL, "LSTOPSUB" },
350 { MATCHOP, TOKENTYPE_OPNUM, "MATCHOP" },
351 { METHOD, TOKENTYPE_OPVAL, "METHOD" },
352 { MULOP, TOKENTYPE_OPNUM, "MULOP" },
353 { MY, TOKENTYPE_IVAL, "MY" },
354 { MYSUB, TOKENTYPE_NONE, "MYSUB" },
355 { NOAMP, TOKENTYPE_NONE, "NOAMP" },
356 { NOTOP, TOKENTYPE_NONE, "NOTOP" },
357 { OROP, TOKENTYPE_IVAL, "OROP" },
358 { OROR, TOKENTYPE_NONE, "OROR" },
359 { PACKAGE, TOKENTYPE_NONE, "PACKAGE" },
360 { PLUGEXPR, TOKENTYPE_OPVAL, "PLUGEXPR" },
361 { PLUGSTMT, TOKENTYPE_OPVAL, "PLUGSTMT" },
362 { PMFUNC, TOKENTYPE_OPVAL, "PMFUNC" },
363 { POSTDEC, TOKENTYPE_NONE, "POSTDEC" },
364 { POSTINC, TOKENTYPE_NONE, "POSTINC" },
365 { POWOP, TOKENTYPE_OPNUM, "POWOP" },
366 { PREDEC, TOKENTYPE_NONE, "PREDEC" },
367 { PREINC, TOKENTYPE_NONE, "PREINC" },
368 { PRIVATEREF, TOKENTYPE_OPVAL, "PRIVATEREF" },
369 { REFGEN, TOKENTYPE_NONE, "REFGEN" },
370 { RELOP, TOKENTYPE_OPNUM, "RELOP" },
371 { SHIFTOP, TOKENTYPE_OPNUM, "SHIFTOP" },
372 { SUB, TOKENTYPE_NONE, "SUB" },
373 { THING, TOKENTYPE_OPVAL, "THING" },
374 { UMINUS, TOKENTYPE_NONE, "UMINUS" },
375 { UNIOP, TOKENTYPE_OPNUM, "UNIOP" },
376 { UNIOPSUB, TOKENTYPE_OPVAL, "UNIOPSUB" },
377 { UNLESS, TOKENTYPE_IVAL, "UNLESS" },
378 { UNTIL, TOKENTYPE_IVAL, "UNTIL" },
379 { USE, TOKENTYPE_IVAL, "USE" },
380 { WHEN, TOKENTYPE_IVAL, "WHEN" },
381 { WHILE, TOKENTYPE_IVAL, "WHILE" },
382 { WORD, TOKENTYPE_OPVAL, "WORD" },
383 { YADAYADA, TOKENTYPE_IVAL, "YADAYADA" },
384 { 0, TOKENTYPE_NONE, NULL }
387 /* dump the returned token in rv, plus any optional arg in pl_yylval */
390 S_tokereport(pTHX_ I32 rv, const YYSTYPE* lvalp)
394 PERL_ARGS_ASSERT_TOKEREPORT;
397 const char *name = NULL;
398 enum token_type type = TOKENTYPE_NONE;
399 const struct debug_tokens *p;
400 SV* const report = newSVpvs("<== ");
402 for (p = debug_tokens; p->token; p++) {
403 if (p->token == (int)rv) {
410 Perl_sv_catpv(aTHX_ report, name);
411 else if ((char)rv > ' ' && (char)rv < '~')
412 Perl_sv_catpvf(aTHX_ report, "'%c'", (char)rv);
414 sv_catpvs(report, "EOF");
416 Perl_sv_catpvf(aTHX_ report, "?? %"IVdf, (IV)rv);
419 case TOKENTYPE_GVVAL: /* doesn't appear to be used */
422 Perl_sv_catpvf(aTHX_ report, "(ival=%"IVdf")", (IV)lvalp->ival);
424 case TOKENTYPE_OPNUM:
425 Perl_sv_catpvf(aTHX_ report, "(ival=op_%s)",
426 PL_op_name[lvalp->ival]);
429 Perl_sv_catpvf(aTHX_ report, "(pval=\"%s\")", lvalp->pval);
431 case TOKENTYPE_OPVAL:
433 Perl_sv_catpvf(aTHX_ report, "(opval=op_%s)",
434 PL_op_name[lvalp->opval->op_type]);
435 if (lvalp->opval->op_type == OP_CONST) {
436 Perl_sv_catpvf(aTHX_ report, " %s",
437 SvPEEK(cSVOPx_sv(lvalp->opval)));
442 sv_catpvs(report, "(opval=null)");
445 PerlIO_printf(Perl_debug_log, "### %s\n\n", SvPV_nolen_const(report));
451 /* print the buffer with suitable escapes */
454 S_printbuf(pTHX_ const char *const fmt, const char *const s)
456 SV* const tmp = newSVpvs("");
458 PERL_ARGS_ASSERT_PRINTBUF;
460 PerlIO_printf(Perl_debug_log, fmt, pv_display(tmp, s, strlen(s), 0, 60));
467 S_deprecate_commaless_var_list(pTHX) {
469 deprecate("comma-less variable list");
470 return REPORT(','); /* grandfather non-comma-format format */
476 * This subroutine detects &&=, ||=, and //= and turns an ANDAND, OROR or DORDOR
477 * into an OP_ANDASSIGN, OP_ORASSIGN, or OP_DORASSIGN
481 S_ao(pTHX_ int toketype)
484 if (*PL_bufptr == '=') {
486 if (toketype == ANDAND)
487 pl_yylval.ival = OP_ANDASSIGN;
488 else if (toketype == OROR)
489 pl_yylval.ival = OP_ORASSIGN;
490 else if (toketype == DORDOR)
491 pl_yylval.ival = OP_DORASSIGN;
499 * When Perl expects an operator and finds something else, no_op
500 * prints the warning. It always prints "<something> found where
501 * operator expected. It prints "Missing semicolon on previous line?"
502 * if the surprise occurs at the start of the line. "do you need to
503 * predeclare ..." is printed out for code like "sub bar; foo bar $x"
504 * where the compiler doesn't know if foo is a method call or a function.
505 * It prints "Missing operator before end of line" if there's nothing
506 * after the missing operator, or "... before <...>" if there is something
507 * after the missing operator.
511 S_no_op(pTHX_ const char *const what, char *s)
514 char * const oldbp = PL_bufptr;
515 const bool is_first = (PL_oldbufptr == PL_linestart);
517 PERL_ARGS_ASSERT_NO_OP;
523 yywarn(Perl_form(aTHX_ "%s found where operator expected", what));
524 if (ckWARN_d(WARN_SYNTAX)) {
526 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
527 "\t(Missing semicolon on previous line?)\n");
528 else if (PL_oldoldbufptr && isIDFIRST_lazy_if(PL_oldoldbufptr,UTF)) {
530 for (t = PL_oldoldbufptr; (isALNUM_lazy_if(t,UTF) || *t == ':'); t++)
532 if (t < PL_bufptr && isSPACE(*t))
533 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
534 "\t(Do you need to predeclare %.*s?)\n",
535 (int)(t - PL_oldoldbufptr), PL_oldoldbufptr);
539 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
540 "\t(Missing operator before %.*s?)\n", (int)(s - oldbp), oldbp);
548 * Complain about missing quote/regexp/heredoc terminator.
549 * If it's called with NULL then it cauterizes the line buffer.
550 * If we're in a delimited string and the delimiter is a control
551 * character, it's reformatted into a two-char sequence like ^C.
556 S_missingterm(pTHX_ char *s)
562 char * const nl = strrchr(s,'\n');
566 else if (isCNTRL(PL_multi_close)) {
568 tmpbuf[1] = (char)toCTRL(PL_multi_close);
573 *tmpbuf = (char)PL_multi_close;
577 q = strchr(s,'"') ? '\'' : '"';
578 Perl_croak(aTHX_ "Can't find string terminator %c%s%c anywhere before EOF",q,s,q);
581 #define FEATURE_IS_ENABLED(name) \
582 ((0 != (PL_hints & HINT_LOCALIZE_HH)) \
583 && S_feature_is_enabled(aTHX_ STR_WITH_LEN(name)))
584 /* The longest string we pass in. */
585 #define MAX_FEATURE_LEN (sizeof("unicode_strings")-1)
588 * S_feature_is_enabled
589 * Check whether the named feature is enabled.
592 S_feature_is_enabled(pTHX_ const char *const name, STRLEN namelen)
595 HV * const hinthv = GvHV(PL_hintgv);
596 char he_name[8 + MAX_FEATURE_LEN] = "feature_";
598 PERL_ARGS_ASSERT_FEATURE_IS_ENABLED;
600 assert(namelen <= MAX_FEATURE_LEN);
601 memcpy(&he_name[8], name, namelen);
603 return (hinthv && hv_exists(hinthv, he_name, 8 + namelen));
607 * experimental text filters for win32 carriage-returns, utf16-to-utf8 and
608 * utf16-to-utf8-reversed.
611 #ifdef PERL_CR_FILTER
615 register const char *s = SvPVX_const(sv);
616 register const char * const e = s + SvCUR(sv);
618 PERL_ARGS_ASSERT_STRIP_RETURN;
620 /* outer loop optimized to do nothing if there are no CR-LFs */
622 if (*s++ == '\r' && *s == '\n') {
623 /* hit a CR-LF, need to copy the rest */
624 register char *d = s - 1;
627 if (*s == '\r' && s[1] == '\n')
638 S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
640 const I32 count = FILTER_READ(idx+1, sv, maxlen);
641 if (count > 0 && !maxlen)
648 =for apidoc Amx|void|lex_start|SV *line|PerlIO *rsfp|U32 flags
650 Creates and initialises a new lexer/parser state object, supplying
651 a context in which to lex and parse from a new source of Perl code.
652 A pointer to the new state object is placed in L</PL_parser>. An entry
653 is made on the save stack so that upon unwinding the new state object
654 will be destroyed and the former value of L</PL_parser> will be restored.
655 Nothing else need be done to clean up the parsing context.
657 The code to be parsed comes from I<line> and I<rsfp>. I<line>, if
658 non-null, provides a string (in SV form) containing code to be parsed.
659 A copy of the string is made, so subsequent modification of I<line>
660 does not affect parsing. I<rsfp>, if non-null, provides an input stream
661 from which code will be read to be parsed. If both are non-null, the
662 code in I<line> comes first and must consist of complete lines of input,
663 and I<rsfp> supplies the remainder of the source.
665 The I<flags> parameter is reserved for future use, and must always
672 Perl_lex_start(pTHX_ SV *line, PerlIO *rsfp, U32 flags)
675 const char *s = NULL;
677 yy_parser *parser, *oparser;
679 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_start");
681 /* create and initialise a parser */
683 Newxz(parser, 1, yy_parser);
684 parser->old_parser = oparser = PL_parser;
687 parser->stack = NULL;
689 parser->stack_size = 0;
691 /* on scope exit, free this parser and restore any outer one */
693 parser->saved_curcop = PL_curcop;
695 /* initialise lexer state */
698 parser->curforce = -1;
700 parser->nexttoke = 0;
702 parser->error_count = oparser ? oparser->error_count : 0;
703 parser->copline = NOLINE;
704 parser->lex_state = LEX_NORMAL;
705 parser->expect = XSTATE;
707 parser->rsfp_filters = newAV();
709 Newx(parser->lex_brackstack, 120, char);
710 Newx(parser->lex_casestack, 12, char);
711 *parser->lex_casestack = '\0';
714 s = SvPV_const(line, len);
720 parser->linestr = newSVpvs("\n;");
722 parser->linestr = newSVpvn_flags(s, len, SvUTF8(line));
724 sv_catpvs(parser->linestr, "\n;");
726 parser->oldoldbufptr =
729 parser->linestart = SvPVX(parser->linestr);
730 parser->bufend = parser->bufptr + SvCUR(parser->linestr);
731 parser->last_lop = parser->last_uni = NULL;
737 /* delete a parser object */
740 Perl_parser_free(pTHX_ const yy_parser *parser)
742 PERL_ARGS_ASSERT_PARSER_FREE;
744 PL_curcop = parser->saved_curcop;
745 SvREFCNT_dec(parser->linestr);
747 if (parser->rsfp == PerlIO_stdin())
748 PerlIO_clearerr(parser->rsfp);
749 else if (parser->rsfp && (!parser->old_parser ||
750 (parser->old_parser && parser->rsfp != parser->old_parser->rsfp)))
751 PerlIO_close(parser->rsfp);
752 SvREFCNT_dec(parser->rsfp_filters);
754 Safefree(parser->lex_brackstack);
755 Safefree(parser->lex_casestack);
756 PL_parser = parser->old_parser;
762 =for apidoc AmxU|SV *|PL_parser-E<gt>linestr
764 Buffer scalar containing the chunk currently under consideration of the
765 text currently being lexed. This is always a plain string scalar (for
766 which C<SvPOK> is true). It is not intended to be used as a scalar by
767 normal scalar means; instead refer to the buffer directly by the pointer
768 variables described below.
770 The lexer maintains various C<char*> pointers to things in the
771 C<PL_parser-E<gt>linestr> buffer. If C<PL_parser-E<gt>linestr> is ever
772 reallocated, all of these pointers must be updated. Don't attempt to
773 do this manually, but rather use L</lex_grow_linestr> if you need to
774 reallocate the buffer.
776 The content of the text chunk in the buffer is commonly exactly one
777 complete line of input, up to and including a newline terminator,
778 but there are situations where it is otherwise. The octets of the
779 buffer may be intended to be interpreted as either UTF-8 or Latin-1.
780 The function L</lex_bufutf8> tells you which. Do not use the C<SvUTF8>
781 flag on this scalar, which may disagree with it.
783 For direct examination of the buffer, the variable
784 L</PL_parser-E<gt>bufend> points to the end of the buffer. The current
785 lexing position is pointed to by L</PL_parser-E<gt>bufptr>. Direct use
786 of these pointers is usually preferable to examination of the scalar
787 through normal scalar means.
789 =for apidoc AmxU|char *|PL_parser-E<gt>bufend
791 Direct pointer to the end of the chunk of text currently being lexed, the
792 end of the lexer buffer. This is equal to C<SvPVX(PL_parser-E<gt>linestr)
793 + SvCUR(PL_parser-E<gt>linestr)>. A NUL character (zero octet) is
794 always located at the end of the buffer, and does not count as part of
795 the buffer's contents.
797 =for apidoc AmxU|char *|PL_parser-E<gt>bufptr
799 Points to the current position of lexing inside the lexer buffer.
800 Characters around this point may be freely examined, within
801 the range delimited by C<SvPVX(L</PL_parser-E<gt>linestr>)> and
802 L</PL_parser-E<gt>bufend>. The octets of the buffer may be intended to be
803 interpreted as either UTF-8 or Latin-1, as indicated by L</lex_bufutf8>.
805 Lexing code (whether in the Perl core or not) moves this pointer past
806 the characters that it consumes. It is also expected to perform some
807 bookkeeping whenever a newline character is consumed. This movement
808 can be more conveniently performed by the function L</lex_read_to>,
809 which handles newlines appropriately.
811 Interpretation of the buffer's octets can be abstracted out by
812 using the slightly higher-level functions L</lex_peek_unichar> and
813 L</lex_read_unichar>.
815 =for apidoc AmxU|char *|PL_parser-E<gt>linestart
817 Points to the start of the current line inside the lexer buffer.
818 This is useful for indicating at which column an error occurred, and
819 not much else. This must be updated by any lexing code that consumes
820 a newline; the function L</lex_read_to> handles this detail.
826 =for apidoc Amx|bool|lex_bufutf8
828 Indicates whether the octets in the lexer buffer
829 (L</PL_parser-E<gt>linestr>) should be interpreted as the UTF-8 encoding
830 of Unicode characters. If not, they should be interpreted as Latin-1
831 characters. This is analogous to the C<SvUTF8> flag for scalars.
833 In UTF-8 mode, it is not guaranteed that the lexer buffer actually
834 contains valid UTF-8. Lexing code must be robust in the face of invalid
837 The actual C<SvUTF8> flag of the L</PL_parser-E<gt>linestr> scalar
838 is significant, but not the whole story regarding the input character
839 encoding. Normally, when a file is being read, the scalar contains octets
840 and its C<SvUTF8> flag is off, but the octets should be interpreted as
841 UTF-8 if the C<use utf8> pragma is in effect. During a string eval,
842 however, the scalar may have the C<SvUTF8> flag on, and in this case its
843 octets should be interpreted as UTF-8 unless the C<use bytes> pragma
844 is in effect. This logic may change in the future; use this function
845 instead of implementing the logic yourself.
851 Perl_lex_bufutf8(pTHX)
857 =for apidoc Amx|char *|lex_grow_linestr|STRLEN len
859 Reallocates the lexer buffer (L</PL_parser-E<gt>linestr>) to accommodate
860 at least I<len> octets (including terminating NUL). Returns a
861 pointer to the reallocated buffer. This is necessary before making
862 any direct modification of the buffer that would increase its length.
863 L</lex_stuff_pvn> provides a more convenient way to insert text into
866 Do not use C<SvGROW> or C<sv_grow> directly on C<PL_parser-E<gt>linestr>;
867 this function updates all of the lexer's variables that point directly
874 Perl_lex_grow_linestr(pTHX_ STRLEN len)
878 STRLEN bufend_pos, bufptr_pos, oldbufptr_pos, oldoldbufptr_pos;
879 STRLEN linestart_pos, last_uni_pos, last_lop_pos;
880 linestr = PL_parser->linestr;
881 buf = SvPVX(linestr);
882 if (len <= SvLEN(linestr))
884 bufend_pos = PL_parser->bufend - buf;
885 bufptr_pos = PL_parser->bufptr - buf;
886 oldbufptr_pos = PL_parser->oldbufptr - buf;
887 oldoldbufptr_pos = PL_parser->oldoldbufptr - buf;
888 linestart_pos = PL_parser->linestart - buf;
889 last_uni_pos = PL_parser->last_uni ? PL_parser->last_uni - buf : 0;
890 last_lop_pos = PL_parser->last_lop ? PL_parser->last_lop - buf : 0;
891 buf = sv_grow(linestr, len);
892 PL_parser->bufend = buf + bufend_pos;
893 PL_parser->bufptr = buf + bufptr_pos;
894 PL_parser->oldbufptr = buf + oldbufptr_pos;
895 PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
896 PL_parser->linestart = buf + linestart_pos;
897 if (PL_parser->last_uni)
898 PL_parser->last_uni = buf + last_uni_pos;
899 if (PL_parser->last_lop)
900 PL_parser->last_lop = buf + last_lop_pos;
905 =for apidoc Amx|void|lex_stuff_pvn|const char *pv|STRLEN len|U32 flags
907 Insert characters into the lexer buffer (L</PL_parser-E<gt>linestr>),
908 immediately after the current lexing point (L</PL_parser-E<gt>bufptr>),
909 reallocating the buffer if necessary. This means that lexing code that
910 runs later will see the characters as if they had appeared in the input.
911 It is not recommended to do this as part of normal parsing, and most
912 uses of this facility run the risk of the inserted characters being
913 interpreted in an unintended manner.
915 The string to be inserted is represented by I<len> octets starting
916 at I<pv>. These octets are interpreted as either UTF-8 or Latin-1,
917 according to whether the C<LEX_STUFF_UTF8> flag is set in I<flags>.
918 The characters are recoded for the lexer buffer, according to how the
919 buffer is currently being interpreted (L</lex_bufutf8>). If a string
920 to be inserted is available as a Perl scalar, the L</lex_stuff_sv>
921 function is more convenient.
927 Perl_lex_stuff_pvn(pTHX_ const char *pv, STRLEN len, U32 flags)
931 PERL_ARGS_ASSERT_LEX_STUFF_PVN;
932 if (flags & ~(LEX_STUFF_UTF8))
933 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_stuff_pvn");
935 if (flags & LEX_STUFF_UTF8) {
939 const char *p, *e = pv+len;
940 for (p = pv; p != e; p++)
941 highhalf += !!(((U8)*p) & 0x80);
944 lex_grow_linestr(SvCUR(PL_parser->linestr)+1+len+highhalf);
945 bufptr = PL_parser->bufptr;
946 Move(bufptr, bufptr+len+highhalf, PL_parser->bufend+1-bufptr, char);
947 SvCUR_set(PL_parser->linestr,
948 SvCUR(PL_parser->linestr) + len+highhalf);
949 PL_parser->bufend += len+highhalf;
950 for (p = pv; p != e; p++) {
953 *bufptr++ = (char)(0xc0 | (c >> 6));
954 *bufptr++ = (char)(0x80 | (c & 0x3f));
961 if (flags & LEX_STUFF_UTF8) {
963 const char *p, *e = pv+len;
964 for (p = pv; p != e; p++) {
967 Perl_croak(aTHX_ "Lexing code attempted to stuff "
968 "non-Latin-1 character into Latin-1 input");
969 } else if (c >= 0xc2 && p+1 != e &&
970 (((U8)p[1]) & 0xc0) == 0x80) {
973 } else if (c >= 0x80) {
974 /* malformed UTF-8 */
976 SAVESPTR(PL_warnhook);
977 PL_warnhook = PERL_WARNHOOK_FATAL;
978 utf8n_to_uvuni((U8*)p, e-p, NULL, 0);
984 lex_grow_linestr(SvCUR(PL_parser->linestr)+1+len-highhalf);
985 bufptr = PL_parser->bufptr;
986 Move(bufptr, bufptr+len-highhalf, PL_parser->bufend+1-bufptr, char);
987 SvCUR_set(PL_parser->linestr,
988 SvCUR(PL_parser->linestr) + len-highhalf);
989 PL_parser->bufend += len-highhalf;
990 for (p = pv; p != e; p++) {
993 *bufptr++ = (char)(((c & 0x3) << 6) | (p[1] & 0x3f));
1001 lex_grow_linestr(SvCUR(PL_parser->linestr)+1+len);
1002 bufptr = PL_parser->bufptr;
1003 Move(bufptr, bufptr+len, PL_parser->bufend+1-bufptr, char);
1004 SvCUR_set(PL_parser->linestr, SvCUR(PL_parser->linestr) + len);
1005 PL_parser->bufend += len;
1006 Copy(pv, bufptr, len, char);
1012 =for apidoc Amx|void|lex_stuff_pv|const char *pv|U32 flags
1014 Insert characters into the lexer buffer (L</PL_parser-E<gt>linestr>),
1015 immediately after the current lexing point (L</PL_parser-E<gt>bufptr>),
1016 reallocating the buffer if necessary. This means that lexing code that
1017 runs later will see the characters as if they had appeared in the input.
1018 It is not recommended to do this as part of normal parsing, and most
1019 uses of this facility run the risk of the inserted characters being
1020 interpreted in an unintended manner.
1022 The string to be inserted is represented by octets starting at I<pv>
1023 and continuing to the first nul. These octets are interpreted as either
1024 UTF-8 or Latin-1, according to whether the C<LEX_STUFF_UTF8> flag is set
1025 in I<flags>. The characters are recoded for the lexer buffer, according
1026 to how the buffer is currently being interpreted (L</lex_bufutf8>).
1027 If it is not convenient to nul-terminate a string to be inserted, the
1028 L</lex_stuff_pvn> function is more appropriate.
1034 Perl_lex_stuff_pv(pTHX_ const char *pv, U32 flags)
1036 PERL_ARGS_ASSERT_LEX_STUFF_PV;
1037 lex_stuff_pvn(pv, strlen(pv), flags);
1041 =for apidoc Amx|void|lex_stuff_sv|SV *sv|U32 flags
1043 Insert characters into the lexer buffer (L</PL_parser-E<gt>linestr>),
1044 immediately after the current lexing point (L</PL_parser-E<gt>bufptr>),
1045 reallocating the buffer if necessary. This means that lexing code that
1046 runs later will see the characters as if they had appeared in the input.
1047 It is not recommended to do this as part of normal parsing, and most
1048 uses of this facility run the risk of the inserted characters being
1049 interpreted in an unintended manner.
1051 The string to be inserted is the string value of I<sv>. The characters
1052 are recoded for the lexer buffer, according to how the buffer is currently
1053 being interpreted (L</lex_bufutf8>). If a string to be inserted is
1054 not already a Perl scalar, the L</lex_stuff_pvn> function avoids the
1055 need to construct a scalar.
1061 Perl_lex_stuff_sv(pTHX_ SV *sv, U32 flags)
1065 PERL_ARGS_ASSERT_LEX_STUFF_SV;
1067 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_stuff_sv");
1069 lex_stuff_pvn(pv, len, flags | (SvUTF8(sv) ? LEX_STUFF_UTF8 : 0));
1073 =for apidoc Amx|void|lex_unstuff|char *ptr
1075 Discards text about to be lexed, from L</PL_parser-E<gt>bufptr> up to
1076 I<ptr>. Text following I<ptr> will be moved, and the buffer shortened.
1077 This hides the discarded text from any lexing code that runs later,
1078 as if the text had never appeared.
1080 This is not the normal way to consume lexed text. For that, use
1087 Perl_lex_unstuff(pTHX_ char *ptr)
1091 PERL_ARGS_ASSERT_LEX_UNSTUFF;
1092 buf = PL_parser->bufptr;
1094 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_unstuff");
1097 bufend = PL_parser->bufend;
1099 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_unstuff");
1100 unstuff_len = ptr - buf;
1101 Move(ptr, buf, bufend+1-ptr, char);
1102 SvCUR_set(PL_parser->linestr, SvCUR(PL_parser->linestr) - unstuff_len);
1103 PL_parser->bufend = bufend - unstuff_len;
1107 =for apidoc Amx|void|lex_read_to|char *ptr
1109 Consume text in the lexer buffer, from L</PL_parser-E<gt>bufptr> up
1110 to I<ptr>. This advances L</PL_parser-E<gt>bufptr> to match I<ptr>,
1111 performing the correct bookkeeping whenever a newline character is passed.
1112 This is the normal way to consume lexed text.
1114 Interpretation of the buffer's octets can be abstracted out by
1115 using the slightly higher-level functions L</lex_peek_unichar> and
1116 L</lex_read_unichar>.
1122 Perl_lex_read_to(pTHX_ char *ptr)
1125 PERL_ARGS_ASSERT_LEX_READ_TO;
1126 s = PL_parser->bufptr;
1127 if (ptr < s || ptr > PL_parser->bufend)
1128 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_to");
1129 for (; s != ptr; s++)
1131 CopLINE_inc(PL_curcop);
1132 PL_parser->linestart = s+1;
1134 PL_parser->bufptr = ptr;
1138 =for apidoc Amx|void|lex_discard_to|char *ptr
1140 Discards the first part of the L</PL_parser-E<gt>linestr> buffer,
1141 up to I<ptr>. The remaining content of the buffer will be moved, and
1142 all pointers into the buffer updated appropriately. I<ptr> must not
1143 be later in the buffer than the position of L</PL_parser-E<gt>bufptr>:
1144 it is not permitted to discard text that has yet to be lexed.
1146 Normally it is not necessarily to do this directly, because it suffices to
1147 use the implicit discarding behaviour of L</lex_next_chunk> and things
1148 based on it. However, if a token stretches across multiple lines,
1149 and the lexing code has kept multiple lines of text in the buffer for
1150 that purpose, then after completion of the token it would be wise to
1151 explicitly discard the now-unneeded earlier lines, to avoid future
1152 multi-line tokens growing the buffer without bound.
1158 Perl_lex_discard_to(pTHX_ char *ptr)
1162 PERL_ARGS_ASSERT_LEX_DISCARD_TO;
1163 buf = SvPVX(PL_parser->linestr);
1165 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_discard_to");
1168 if (ptr > PL_parser->bufptr)
1169 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_discard_to");
1170 discard_len = ptr - buf;
1171 if (PL_parser->oldbufptr < ptr)
1172 PL_parser->oldbufptr = ptr;
1173 if (PL_parser->oldoldbufptr < ptr)
1174 PL_parser->oldoldbufptr = ptr;
1175 if (PL_parser->last_uni && PL_parser->last_uni < ptr)
1176 PL_parser->last_uni = NULL;
1177 if (PL_parser->last_lop && PL_parser->last_lop < ptr)
1178 PL_parser->last_lop = NULL;
1179 Move(ptr, buf, PL_parser->bufend+1-ptr, char);
1180 SvCUR_set(PL_parser->linestr, SvCUR(PL_parser->linestr) - discard_len);
1181 PL_parser->bufend -= discard_len;
1182 PL_parser->bufptr -= discard_len;
1183 PL_parser->oldbufptr -= discard_len;
1184 PL_parser->oldoldbufptr -= discard_len;
1185 if (PL_parser->last_uni)
1186 PL_parser->last_uni -= discard_len;
1187 if (PL_parser->last_lop)
1188 PL_parser->last_lop -= discard_len;
1192 =for apidoc Amx|bool|lex_next_chunk|U32 flags
1194 Reads in the next chunk of text to be lexed, appending it to
1195 L</PL_parser-E<gt>linestr>. This should be called when lexing code has
1196 looked to the end of the current chunk and wants to know more. It is
1197 usual, but not necessary, for lexing to have consumed the entirety of
1198 the current chunk at this time.
1200 If L</PL_parser-E<gt>bufptr> is pointing to the very end of the current
1201 chunk (i.e., the current chunk has been entirely consumed), normally the
1202 current chunk will be discarded at the same time that the new chunk is
1203 read in. If I<flags> includes C<LEX_KEEP_PREVIOUS>, the current chunk
1204 will not be discarded. If the current chunk has not been entirely
1205 consumed, then it will not be discarded regardless of the flag.
1207 Returns true if some new text was added to the buffer, or false if the
1208 buffer has reached the end of the input text.
1213 #define LEX_FAKE_EOF 0x80000000
1216 Perl_lex_next_chunk(pTHX_ U32 flags)
1220 STRLEN old_bufend_pos, new_bufend_pos;
1221 STRLEN bufptr_pos, oldbufptr_pos, oldoldbufptr_pos;
1222 STRLEN linestart_pos, last_uni_pos, last_lop_pos;
1223 bool got_some_for_debugger = 0;
1225 if (flags & ~(LEX_KEEP_PREVIOUS|LEX_FAKE_EOF))
1226 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_next_chunk");
1227 linestr = PL_parser->linestr;
1228 buf = SvPVX(linestr);
1229 if (!(flags & LEX_KEEP_PREVIOUS) &&
1230 PL_parser->bufptr == PL_parser->bufend) {
1231 old_bufend_pos = bufptr_pos = oldbufptr_pos = oldoldbufptr_pos = 0;
1233 if (PL_parser->last_uni != PL_parser->bufend)
1234 PL_parser->last_uni = NULL;
1235 if (PL_parser->last_lop != PL_parser->bufend)
1236 PL_parser->last_lop = NULL;
1237 last_uni_pos = last_lop_pos = 0;
1241 old_bufend_pos = PL_parser->bufend - buf;
1242 bufptr_pos = PL_parser->bufptr - buf;
1243 oldbufptr_pos = PL_parser->oldbufptr - buf;
1244 oldoldbufptr_pos = PL_parser->oldoldbufptr - buf;
1245 linestart_pos = PL_parser->linestart - buf;
1246 last_uni_pos = PL_parser->last_uni ? PL_parser->last_uni - buf : 0;
1247 last_lop_pos = PL_parser->last_lop ? PL_parser->last_lop - buf : 0;
1249 if (flags & LEX_FAKE_EOF) {
1251 } else if (!PL_parser->rsfp) {
1253 } else if (filter_gets(linestr, old_bufend_pos)) {
1255 got_some_for_debugger = 1;
1257 if (!SvPOK(linestr)) /* can get undefined by filter_gets */
1258 sv_setpvs(linestr, "");
1260 /* End of real input. Close filehandle (unless it was STDIN),
1261 * then add implicit termination.
1263 if ((PerlIO*)PL_parser->rsfp == PerlIO_stdin())
1264 PerlIO_clearerr(PL_parser->rsfp);
1265 else if (PL_parser->rsfp)
1266 (void)PerlIO_close(PL_parser->rsfp);
1267 PL_parser->rsfp = NULL;
1268 PL_parser->in_pod = 0;
1270 if (PL_madskills && !PL_in_eval && (PL_minus_p || PL_minus_n))
1273 if (!PL_in_eval && PL_minus_p) {
1275 /*{*/";}continue{print or die qq(-p destination: $!\\n);}");
1276 PL_minus_n = PL_minus_p = 0;
1277 } else if (!PL_in_eval && PL_minus_n) {
1278 sv_catpvs(linestr, /*{*/";}");
1281 sv_catpvs(linestr, ";");
1284 buf = SvPVX(linestr);
1285 new_bufend_pos = SvCUR(linestr);
1286 PL_parser->bufend = buf + new_bufend_pos;
1287 PL_parser->bufptr = buf + bufptr_pos;
1288 PL_parser->oldbufptr = buf + oldbufptr_pos;
1289 PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
1290 PL_parser->linestart = buf + linestart_pos;
1291 if (PL_parser->last_uni)
1292 PL_parser->last_uni = buf + last_uni_pos;
1293 if (PL_parser->last_lop)
1294 PL_parser->last_lop = buf + last_lop_pos;
1295 if (got_some_for_debugger && (PERLDB_LINE || PERLDB_SAVESRC) &&
1296 PL_curstash != PL_debstash) {
1297 /* debugger active and we're not compiling the debugger code,
1298 * so store the line into the debugger's array of lines
1300 update_debugger_info(NULL, buf+old_bufend_pos,
1301 new_bufend_pos-old_bufend_pos);
1307 =for apidoc Amx|I32|lex_peek_unichar|U32 flags
1309 Looks ahead one (Unicode) character in the text currently being lexed.
1310 Returns the codepoint (unsigned integer value) of the next character,
1311 or -1 if lexing has reached the end of the input text. To consume the
1312 peeked character, use L</lex_read_unichar>.
1314 If the next character is in (or extends into) the next chunk of input
1315 text, the next chunk will be read in. Normally the current chunk will be
1316 discarded at the same time, but if I<flags> includes C<LEX_KEEP_PREVIOUS>
1317 then the current chunk will not be discarded.
1319 If the input is being interpreted as UTF-8 and a UTF-8 encoding error
1320 is encountered, an exception is generated.
1326 Perl_lex_peek_unichar(pTHX_ U32 flags)
1330 if (flags & ~(LEX_KEEP_PREVIOUS))
1331 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_peek_unichar");
1332 s = PL_parser->bufptr;
1333 bufend = PL_parser->bufend;
1339 if (!lex_next_chunk(flags))
1341 s = PL_parser->bufptr;
1342 bufend = PL_parser->bufend;
1348 len = PL_utf8skip[head];
1349 while ((STRLEN)(bufend-s) < len) {
1350 if (!lex_next_chunk(flags | LEX_KEEP_PREVIOUS))
1352 s = PL_parser->bufptr;
1353 bufend = PL_parser->bufend;
1356 unichar = utf8n_to_uvuni((U8*)s, bufend-s, &retlen, UTF8_CHECK_ONLY);
1357 if (retlen == (STRLEN)-1) {
1358 /* malformed UTF-8 */
1360 SAVESPTR(PL_warnhook);
1361 PL_warnhook = PERL_WARNHOOK_FATAL;
1362 utf8n_to_uvuni((U8*)s, bufend-s, NULL, 0);
1368 if (!lex_next_chunk(flags))
1370 s = PL_parser->bufptr;
1377 =for apidoc Amx|I32|lex_read_unichar|U32 flags
1379 Reads the next (Unicode) character in the text currently being lexed.
1380 Returns the codepoint (unsigned integer value) of the character read,
1381 and moves L</PL_parser-E<gt>bufptr> past the character, or returns -1
1382 if lexing has reached the end of the input text. To non-destructively
1383 examine the next character, use L</lex_peek_unichar> instead.
1385 If the next character is in (or extends into) the next chunk of input
1386 text, the next chunk will be read in. Normally the current chunk will be
1387 discarded at the same time, but if I<flags> includes C<LEX_KEEP_PREVIOUS>
1388 then the current chunk will not be discarded.
1390 If the input is being interpreted as UTF-8 and a UTF-8 encoding error
1391 is encountered, an exception is generated.
1397 Perl_lex_read_unichar(pTHX_ U32 flags)
1400 if (flags & ~(LEX_KEEP_PREVIOUS))
1401 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_unichar");
1402 c = lex_peek_unichar(flags);
1405 CopLINE_inc(PL_curcop);
1406 PL_parser->bufptr += UTF8SKIP(PL_parser->bufptr);
1412 =for apidoc Amx|void|lex_read_space|U32 flags
1414 Reads optional spaces, in Perl style, in the text currently being
1415 lexed. The spaces may include ordinary whitespace characters and
1416 Perl-style comments. C<#line> directives are processed if encountered.
1417 L</PL_parser-E<gt>bufptr> is moved past the spaces, so that it points
1418 at a non-space character (or the end of the input text).
1420 If spaces extend into the next chunk of input text, the next chunk will
1421 be read in. Normally the current chunk will be discarded at the same
1422 time, but if I<flags> includes C<LEX_KEEP_PREVIOUS> then the current
1423 chunk will not be discarded.
1428 #define LEX_NO_NEXT_CHUNK 0x80000000
1431 Perl_lex_read_space(pTHX_ U32 flags)
1434 bool need_incline = 0;
1435 if (flags & ~(LEX_KEEP_PREVIOUS|LEX_NO_NEXT_CHUNK))
1436 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_space");
1439 sv_free(PL_skipwhite);
1440 PL_skipwhite = NULL;
1443 PL_skipwhite = newSVpvs("");
1444 #endif /* PERL_MAD */
1445 s = PL_parser->bufptr;
1446 bufend = PL_parser->bufend;
1452 } while (!(c == '\n' || (c == 0 && s == bufend)));
1453 } else if (c == '\n') {
1455 PL_parser->linestart = s;
1460 } else if (isSPACE(c)) {
1462 } else if (c == 0 && s == bufend) {
1466 sv_catpvn(PL_skipwhite, PL_parser->bufptr, s-PL_parser->bufptr);
1467 #endif /* PERL_MAD */
1468 if (flags & LEX_NO_NEXT_CHUNK)
1470 PL_parser->bufptr = s;
1471 CopLINE_inc(PL_curcop);
1472 got_more = lex_next_chunk(flags);
1473 CopLINE_dec(PL_curcop);
1474 s = PL_parser->bufptr;
1475 bufend = PL_parser->bufend;
1478 if (need_incline && PL_parser->rsfp) {
1488 sv_catpvn(PL_skipwhite, PL_parser->bufptr, s-PL_parser->bufptr);
1489 #endif /* PERL_MAD */
1490 PL_parser->bufptr = s;
1495 * This subroutine has nothing to do with tilting, whether at windmills
1496 * or pinball tables. Its name is short for "increment line". It
1497 * increments the current line number in CopLINE(PL_curcop) and checks
1498 * to see whether the line starts with a comment of the form
1499 * # line 500 "foo.pm"
1500 * If so, it sets the current line number and file to the values in the comment.
1504 S_incline(pTHX_ const char *s)
1511 PERL_ARGS_ASSERT_INCLINE;
1513 CopLINE_inc(PL_curcop);
1516 while (SPACE_OR_TAB(*s))
1518 if (strnEQ(s, "line", 4))
1522 if (SPACE_OR_TAB(*s))
1526 while (SPACE_OR_TAB(*s))
1534 if (!SPACE_OR_TAB(*s) && *s != '\r' && *s != '\n' && *s != '\0')
1536 while (SPACE_OR_TAB(*s))
1538 if (*s == '"' && (t = strchr(s+1, '"'))) {
1544 while (!isSPACE(*t))
1548 while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
1550 if (*e != '\n' && *e != '\0')
1551 return; /* false alarm */
1554 const STRLEN len = t - s;
1555 #ifndef USE_ITHREADS
1556 SV *const temp_sv = CopFILESV(PL_curcop);
1561 cf = SvPVX(temp_sv);
1562 tmplen = SvCUR(temp_sv);
1568 if (tmplen > 7 && strnEQ(cf, "(eval ", 6)) {
1569 /* must copy *{"::_<(eval N)[oldfilename:L]"}
1570 * to *{"::_<newfilename"} */
1571 /* However, the long form of evals is only turned on by the
1572 debugger - usually they're "(eval %lu)" */
1576 STRLEN tmplen2 = len;
1577 if (tmplen + 2 <= sizeof smallbuf)
1580 Newx(tmpbuf, tmplen + 2, char);
1583 memcpy(tmpbuf + 2, cf, tmplen);
1585 gvp = (GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, FALSE);
1590 if (tmplen2 + 2 <= sizeof smallbuf)
1593 Newx(tmpbuf2, tmplen2 + 2, char);
1595 if (tmpbuf2 != smallbuf || tmpbuf != smallbuf) {
1596 /* Either they malloc'd it, or we malloc'd it,
1597 so no prefix is present in ours. */
1602 memcpy(tmpbuf2 + 2, s, tmplen2);
1605 gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE);
1607 gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE);
1608 /* adjust ${"::_<newfilename"} to store the new file name */
1609 GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2);
1610 GvHV(gv2) = MUTABLE_HV(SvREFCNT_inc(GvHV(*gvp)));
1611 GvAV(gv2) = MUTABLE_AV(SvREFCNT_inc(GvAV(*gvp)));
1614 if (tmpbuf2 != smallbuf) Safefree(tmpbuf2);
1616 if (tmpbuf != smallbuf) Safefree(tmpbuf);
1619 CopFILE_free(PL_curcop);
1620 CopFILE_setn(PL_curcop, s, len);
1622 CopLINE_set(PL_curcop, atoi(n)-1);
1626 /* skip space before PL_thistoken */
1629 S_skipspace0(pTHX_ register char *s)
1631 PERL_ARGS_ASSERT_SKIPSPACE0;
1638 PL_thiswhite = newSVpvs("");
1639 sv_catsv(PL_thiswhite, PL_skipwhite);
1640 sv_free(PL_skipwhite);
1643 PL_realtokenstart = s - SvPVX(PL_linestr);
1647 /* skip space after PL_thistoken */
1650 S_skipspace1(pTHX_ register char *s)
1652 const char *start = s;
1653 I32 startoff = start - SvPVX(PL_linestr);
1655 PERL_ARGS_ASSERT_SKIPSPACE1;
1660 start = SvPVX(PL_linestr) + startoff;
1661 if (!PL_thistoken && PL_realtokenstart >= 0) {
1662 const char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
1663 PL_thistoken = newSVpvn(tstart, start - tstart);
1665 PL_realtokenstart = -1;
1668 PL_nextwhite = newSVpvs("");
1669 sv_catsv(PL_nextwhite, PL_skipwhite);
1670 sv_free(PL_skipwhite);
1677 S_skipspace2(pTHX_ register char *s, SV **svp)
1680 const I32 bufptroff = PL_bufptr - SvPVX(PL_linestr);
1681 const I32 startoff = s - SvPVX(PL_linestr);
1683 PERL_ARGS_ASSERT_SKIPSPACE2;
1686 PL_bufptr = SvPVX(PL_linestr) + bufptroff;
1687 if (!PL_madskills || !svp)
1689 start = SvPVX(PL_linestr) + startoff;
1690 if (!PL_thistoken && PL_realtokenstart >= 0) {
1691 char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
1692 PL_thistoken = newSVpvn(tstart, start - tstart);
1693 PL_realtokenstart = -1;
1697 *svp = newSVpvs("");
1698 sv_setsv(*svp, PL_skipwhite);
1699 sv_free(PL_skipwhite);
1708 S_update_debugger_info(pTHX_ SV *orig_sv, const char *const buf, STRLEN len)
1710 AV *av = CopFILEAVx(PL_curcop);
1712 SV * const sv = newSV_type(SVt_PVMG);
1714 sv_setsv(sv, orig_sv);
1716 sv_setpvn(sv, buf, len);
1719 av_store(av, (I32)CopLINE(PL_curcop), sv);
1725 * Called to gobble the appropriate amount and type of whitespace.
1726 * Skips comments as well.
1730 S_skipspace(pTHX_ register char *s)
1734 #endif /* PERL_MAD */
1735 PERL_ARGS_ASSERT_SKIPSPACE;
1738 sv_free(PL_skipwhite);
1739 PL_skipwhite = NULL;
1741 #endif /* PERL_MAD */
1742 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
1743 while (s < PL_bufend && SPACE_OR_TAB(*s))
1746 STRLEN bufptr_pos = PL_bufptr - SvPVX(PL_linestr);
1748 lex_read_space(LEX_KEEP_PREVIOUS |
1749 (PL_sublex_info.sub_inwhat || PL_lex_state == LEX_FORMLINE ?
1750 LEX_NO_NEXT_CHUNK : 0));
1752 PL_bufptr = SvPVX(PL_linestr) + bufptr_pos;
1753 if (PL_linestart > PL_bufptr)
1754 PL_bufptr = PL_linestart;
1759 PL_skipwhite = newSVpvn(start, s-start);
1760 #endif /* PERL_MAD */
1766 * Check the unary operators to ensure there's no ambiguity in how they're
1767 * used. An ambiguous piece of code would be:
1769 * This doesn't mean rand() + 5. Because rand() is a unary operator,
1770 * the +5 is its argument.
1780 if (PL_oldoldbufptr != PL_last_uni)
1782 while (isSPACE(*PL_last_uni))
1785 while (isALNUM_lazy_if(s,UTF) || *s == '-')
1787 if ((t = strchr(s, '(')) && t < PL_bufptr)
1790 Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
1791 "Warning: Use of \"%.*s\" without parentheses is ambiguous",
1792 (int)(s - PL_last_uni), PL_last_uni);
1796 * LOP : macro to build a list operator. Its behaviour has been replaced
1797 * with a subroutine, S_lop() for which LOP is just another name.
1800 #define LOP(f,x) return lop(f,x,s)
1804 * Build a list operator (or something that might be one). The rules:
1805 * - if we have a next token, then it's a list operator [why?]
1806 * - if the next thing is an opening paren, then it's a function
1807 * - else it's a list operator
1811 S_lop(pTHX_ I32 f, int x, char *s)
1815 PERL_ARGS_ASSERT_LOP;
1821 PL_last_lop = PL_oldbufptr;
1822 PL_last_lop_op = (OPCODE)f;
1825 return REPORT(LSTOP);
1828 return REPORT(LSTOP);
1831 return REPORT(FUNC);
1834 return REPORT(FUNC);
1836 return REPORT(LSTOP);
1842 * Sets up for an eventual force_next(). start_force(0) basically does
1843 * an unshift, while start_force(-1) does a push. yylex removes items
1848 S_start_force(pTHX_ int where)
1852 if (where < 0) /* so people can duplicate start_force(PL_curforce) */
1853 where = PL_lasttoke;
1854 assert(PL_curforce < 0 || PL_curforce == where);
1855 if (PL_curforce != where) {
1856 for (i = PL_lasttoke; i > where; --i) {
1857 PL_nexttoke[i] = PL_nexttoke[i-1];
1861 if (PL_curforce < 0) /* in case of duplicate start_force() */
1862 Zero(&PL_nexttoke[where], 1, NEXTTOKE);
1863 PL_curforce = where;
1866 curmad('^', newSVpvs(""));
1867 CURMAD('_', PL_nextwhite);
1872 S_curmad(pTHX_ char slot, SV *sv)
1878 if (PL_curforce < 0)
1879 where = &PL_thismad;
1881 where = &PL_nexttoke[PL_curforce].next_mad;
1887 if (UTF && is_utf8_string((U8*)SvPVX(sv), SvCUR(sv)))
1889 else if (PL_encoding) {
1890 sv_recode_to_utf8(sv, PL_encoding);
1895 /* keep a slot open for the head of the list? */
1896 if (slot != '_' && *where && (*where)->mad_key == '^') {
1897 (*where)->mad_key = slot;
1898 sv_free(MUTABLE_SV(((*where)->mad_val)));
1899 (*where)->mad_val = (void*)sv;
1902 addmad(newMADsv(slot, sv), where, 0);
1905 # define start_force(where) NOOP
1906 # define curmad(slot, sv) NOOP
1911 * When the lexer realizes it knows the next token (for instance,
1912 * it is reordering tokens for the parser) then it can call S_force_next
1913 * to know what token to return the next time the lexer is called. Caller
1914 * will need to set PL_nextval[] (or PL_nexttoke[].next_val with PERL_MAD),
1915 * and possibly PL_expect to ensure the lexer handles the token correctly.
1919 S_force_next(pTHX_ I32 type)
1924 PerlIO_printf(Perl_debug_log, "### forced token:\n");
1925 tokereport(type, &NEXTVAL_NEXTTOKE);
1929 if (PL_curforce < 0)
1930 start_force(PL_lasttoke);
1931 PL_nexttoke[PL_curforce].next_type = type;
1932 if (PL_lex_state != LEX_KNOWNEXT)
1933 PL_lex_defer = PL_lex_state;
1934 PL_lex_state = LEX_KNOWNEXT;
1935 PL_lex_expect = PL_expect;
1938 PL_nexttype[PL_nexttoke] = type;
1940 if (PL_lex_state != LEX_KNOWNEXT) {
1941 PL_lex_defer = PL_lex_state;
1942 PL_lex_expect = PL_expect;
1943 PL_lex_state = LEX_KNOWNEXT;
1951 int yyc = PL_parser->yychar;
1952 if (yyc != YYEMPTY) {
1955 NEXTVAL_NEXTTOKE = PL_parser->yylval;
1956 if (yyc == '{'/*}*/ || yyc == HASHBRACK || yyc == '['/*]*/) {
1958 yyc |= (1<<24) | (PL_lex_brackstack[PL_lex_brackets] << 16);
1962 PL_parser->yychar = YYEMPTY;
1967 S_newSV_maybe_utf8(pTHX_ const char *const start, STRLEN len)
1970 SV * const sv = newSVpvn_utf8(start, len,
1973 && !is_ascii_string((const U8*)start, len)
1974 && is_utf8_string((const U8*)start, len));
1980 * When the lexer knows the next thing is a word (for instance, it has
1981 * just seen -> and it knows that the next char is a word char, then
1982 * it calls S_force_word to stick the next word into the PL_nexttoke/val
1986 * char *start : buffer position (must be within PL_linestr)
1987 * int token : PL_next* will be this type of bare word (e.g., METHOD,WORD)
1988 * int check_keyword : if true, Perl checks to make sure the word isn't
1989 * a keyword (do this if the word is a label, e.g. goto FOO)
1990 * int allow_pack : if true, : characters will also be allowed (require,
1991 * use, etc. do this)
1992 * int allow_initial_tick : used by the "sub" lexer only.
1996 S_force_word(pTHX_ register char *start, int token, int check_keyword, int allow_pack, int allow_initial_tick)
2002 PERL_ARGS_ASSERT_FORCE_WORD;
2004 start = SKIPSPACE1(start);
2006 if (isIDFIRST_lazy_if(s,UTF) ||
2007 (allow_pack && *s == ':') ||
2008 (allow_initial_tick && *s == '\'') )
2010 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, allow_pack, &len);
2011 if (check_keyword && keyword(PL_tokenbuf, len, 0))
2013 start_force(PL_curforce);
2015 curmad('X', newSVpvn(start,s-start));
2016 if (token == METHOD) {
2021 PL_expect = XOPERATOR;
2025 curmad('g', newSVpvs( "forced" ));
2026 NEXTVAL_NEXTTOKE.opval
2027 = (OP*)newSVOP(OP_CONST,0,
2028 S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
2029 NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
2037 * Called when the lexer wants $foo *foo &foo etc, but the program
2038 * text only contains the "foo" portion. The first argument is a pointer
2039 * to the "foo", and the second argument is the type symbol to prefix.
2040 * Forces the next token to be a "WORD".
2041 * Creates the symbol if it didn't already exist (via gv_fetchpv()).
2045 S_force_ident(pTHX_ register const char *s, int kind)
2049 PERL_ARGS_ASSERT_FORCE_IDENT;
2052 const STRLEN len = strlen(s);
2053 OP* const o = (OP*)newSVOP(OP_CONST, 0, newSVpvn(s, len));
2054 start_force(PL_curforce);
2055 NEXTVAL_NEXTTOKE.opval = o;
2058 o->op_private = OPpCONST_ENTERED;
2059 /* XXX see note in pp_entereval() for why we forgo typo
2060 warnings if the symbol must be introduced in an eval.
2062 gv_fetchpvn_flags(s, len,
2063 PL_in_eval ? (GV_ADDMULTI | GV_ADDINEVAL)
2065 kind == '$' ? SVt_PV :
2066 kind == '@' ? SVt_PVAV :
2067 kind == '%' ? SVt_PVHV :
2075 Perl_str_to_version(pTHX_ SV *sv)
2080 const char *start = SvPV_const(sv,len);
2081 const char * const end = start + len;
2082 const bool utf = SvUTF8(sv) ? TRUE : FALSE;
2084 PERL_ARGS_ASSERT_STR_TO_VERSION;
2086 while (start < end) {
2090 n = utf8n_to_uvchr((U8*)start, len, &skip, 0);
2095 retval += ((NV)n)/nshift;
2104 * Forces the next token to be a version number.
2105 * If the next token appears to be an invalid version number, (e.g. "v2b"),
2106 * and if "guessing" is TRUE, then no new token is created (and the caller
2107 * must use an alternative parsing method).
2111 S_force_version(pTHX_ char *s, int guessing)
2117 I32 startoff = s - SvPVX(PL_linestr);
2120 PERL_ARGS_ASSERT_FORCE_VERSION;
2128 while (isDIGIT(*d) || *d == '_' || *d == '.')
2132 start_force(PL_curforce);
2133 curmad('X', newSVpvn(s,d-s));
2136 if (*d == ';' || isSPACE(*d) || *d == '{' || *d == '}' || !*d) {
2138 #ifdef USE_LOCALE_NUMERIC
2139 char *loc = setlocale(LC_NUMERIC, "C");
2141 s = scan_num(s, &pl_yylval);
2142 #ifdef USE_LOCALE_NUMERIC
2143 setlocale(LC_NUMERIC, loc);
2145 version = pl_yylval.opval;
2146 ver = cSVOPx(version)->op_sv;
2147 if (SvPOK(ver) && !SvNIOK(ver)) {
2148 SvUPGRADE(ver, SVt_PVNV);
2149 SvNV_set(ver, str_to_version(ver));
2150 SvNOK_on(ver); /* hint that it is a version */
2153 else if (guessing) {
2156 sv_free(PL_nextwhite); /* let next token collect whitespace */
2158 s = SvPVX(PL_linestr) + startoff;
2166 if (PL_madskills && !version) {
2167 sv_free(PL_nextwhite); /* let next token collect whitespace */
2169 s = SvPVX(PL_linestr) + startoff;
2172 /* NOTE: The parser sees the package name and the VERSION swapped */
2173 start_force(PL_curforce);
2174 NEXTVAL_NEXTTOKE.opval = version;
2181 * S_force_strict_version
2182 * Forces the next token to be a version number using strict syntax rules.
2186 S_force_strict_version(pTHX_ char *s)
2191 I32 startoff = s - SvPVX(PL_linestr);
2193 const char *errstr = NULL;
2195 PERL_ARGS_ASSERT_FORCE_STRICT_VERSION;
2197 while (isSPACE(*s)) /* leading whitespace */
2200 if (is_STRICT_VERSION(s,&errstr)) {
2202 s = (char *)scan_version(s, ver, 0);
2203 version = newSVOP(OP_CONST, 0, ver);
2205 else if ( (*s != ';' && *s != '{' && *s != '}' ) &&
2206 (s = SKIPSPACE1(s), (*s != ';' && *s != '{' && *s != '}' )))
2210 yyerror(errstr); /* version required */
2215 if (PL_madskills && !version) {
2216 sv_free(PL_nextwhite); /* let next token collect whitespace */
2218 s = SvPVX(PL_linestr) + startoff;
2221 /* NOTE: The parser sees the package name and the VERSION swapped */
2222 start_force(PL_curforce);
2223 NEXTVAL_NEXTTOKE.opval = version;
2231 * Tokenize a quoted string passed in as an SV. It finds the next
2232 * chunk, up to end of string or a backslash. It may make a new
2233 * SV containing that chunk (if HINT_NEW_STRING is on). It also
2238 S_tokeq(pTHX_ SV *sv)
2242 register char *send;
2247 PERL_ARGS_ASSERT_TOKEQ;
2252 s = SvPV_force(sv, len);
2253 if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1)
2256 /* This is relying on the SV being "well formed" with a trailing '\0' */
2257 while (s < send && !(*s == '\\' && s[1] == '\\'))
2262 if ( PL_hints & HINT_NEW_STRING ) {
2263 pv = newSVpvn_flags(SvPVX_const(pv), len, SVs_TEMP | SvUTF8(sv));
2267 if (s + 1 < send && (s[1] == '\\'))
2268 s++; /* all that, just for this */
2273 SvCUR_set(sv, d - SvPVX_const(sv));
2275 if ( PL_hints & HINT_NEW_STRING )
2276 return new_constant(NULL, 0, "q", sv, pv, "q", 1);
2281 * Now come three functions related to double-quote context,
2282 * S_sublex_start, S_sublex_push, and S_sublex_done. They're used when
2283 * converting things like "\u\Lgnat" into ucfirst(lc("gnat")). They
2284 * interact with PL_lex_state, and create fake ( ... ) argument lists
2285 * to handle functions and concatenation.
2286 * They assume that whoever calls them will be setting up a fake
2287 * join call, because each subthing puts a ',' after it. This lets
2290 * join($, , 'lower ', lcfirst( 'uPpEr', ) ,)
2292 * (I'm not sure whether the spurious commas at the end of lcfirst's
2293 * arguments and join's arguments are created or not).
2298 * Assumes that pl_yylval.ival is the op we're creating (e.g. OP_LCFIRST).
2300 * Pattern matching will set PL_lex_op to the pattern-matching op to
2301 * make (we return THING if pl_yylval.ival is OP_NULL, PMFUNC otherwise).
2303 * OP_CONST and OP_READLINE are easy--just make the new op and return.
2305 * Everything else becomes a FUNC.
2307 * Sets PL_lex_state to LEX_INTERPPUSH unless (ival was OP_NULL or we
2308 * had an OP_CONST or OP_READLINE). This just sets us up for a
2309 * call to S_sublex_push().
2313 S_sublex_start(pTHX)
2316 register const I32 op_type = pl_yylval.ival;
2318 if (op_type == OP_NULL) {
2319 pl_yylval.opval = PL_lex_op;
2323 if (op_type == OP_CONST || op_type == OP_READLINE) {
2324 SV *sv = tokeq(PL_lex_stuff);
2326 if (SvTYPE(sv) == SVt_PVIV) {
2327 /* Overloaded constants, nothing fancy: Convert to SVt_PV: */
2329 const char * const p = SvPV_const(sv, len);
2330 SV * const nsv = newSVpvn_flags(p, len, SvUTF8(sv));
2334 pl_yylval.opval = (OP*)newSVOP(op_type, 0, sv);
2335 PL_lex_stuff = NULL;
2336 /* Allow <FH> // "foo" */
2337 if (op_type == OP_READLINE)
2338 PL_expect = XTERMORDORDOR;
2341 else if (op_type == OP_BACKTICK && PL_lex_op) {
2342 /* readpipe() vas overriden */
2343 cSVOPx(cLISTOPx(cUNOPx(PL_lex_op)->op_first)->op_first->op_sibling)->op_sv = tokeq(PL_lex_stuff);
2344 pl_yylval.opval = PL_lex_op;
2346 PL_lex_stuff = NULL;
2350 PL_sublex_info.super_state = PL_lex_state;
2351 PL_sublex_info.sub_inwhat = (U16)op_type;
2352 PL_sublex_info.sub_op = PL_lex_op;
2353 PL_lex_state = LEX_INTERPPUSH;
2357 pl_yylval.opval = PL_lex_op;
2367 * Create a new scope to save the lexing state. The scope will be
2368 * ended in S_sublex_done. Returns a '(', starting the function arguments
2369 * to the uc, lc, etc. found before.
2370 * Sets PL_lex_state to LEX_INTERPCONCAT.
2379 PL_lex_state = PL_sublex_info.super_state;
2380 SAVEBOOL(PL_lex_dojoin);
2381 SAVEI32(PL_lex_brackets);
2382 SAVEI32(PL_lex_casemods);
2383 SAVEI32(PL_lex_starts);
2384 SAVEI8(PL_lex_state);
2385 SAVEVPTR(PL_lex_inpat);
2386 SAVEI16(PL_lex_inwhat);
2387 SAVECOPLINE(PL_curcop);
2388 SAVEPPTR(PL_bufptr);
2389 SAVEPPTR(PL_bufend);
2390 SAVEPPTR(PL_oldbufptr);
2391 SAVEPPTR(PL_oldoldbufptr);
2392 SAVEPPTR(PL_last_lop);
2393 SAVEPPTR(PL_last_uni);
2394 SAVEPPTR(PL_linestart);
2395 SAVESPTR(PL_linestr);
2396 SAVEGENERICPV(PL_lex_brackstack);
2397 SAVEGENERICPV(PL_lex_casestack);
2399 PL_linestr = PL_lex_stuff;
2400 PL_lex_stuff = NULL;
2402 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart
2403 = SvPVX(PL_linestr);
2404 PL_bufend += SvCUR(PL_linestr);
2405 PL_last_lop = PL_last_uni = NULL;
2406 SAVEFREESV(PL_linestr);
2408 PL_lex_dojoin = FALSE;
2409 PL_lex_brackets = 0;
2410 Newx(PL_lex_brackstack, 120, char);
2411 Newx(PL_lex_casestack, 12, char);
2412 PL_lex_casemods = 0;
2413 *PL_lex_casestack = '\0';
2415 PL_lex_state = LEX_INTERPCONCAT;
2416 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
2418 PL_lex_inwhat = PL_sublex_info.sub_inwhat;
2419 if (PL_lex_inwhat == OP_TRANSR) PL_lex_inwhat = OP_TRANS;
2420 if (PL_lex_inwhat == OP_MATCH || PL_lex_inwhat == OP_QR || PL_lex_inwhat == OP_SUBST)
2421 PL_lex_inpat = PL_sublex_info.sub_op;
2423 PL_lex_inpat = NULL;
2430 * Restores lexer state after a S_sublex_push.
2437 if (!PL_lex_starts++) {
2438 SV * const sv = newSVpvs("");
2439 if (SvUTF8(PL_linestr))
2441 PL_expect = XOPERATOR;
2442 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
2446 if (PL_lex_casemods) { /* oops, we've got some unbalanced parens */
2447 PL_lex_state = LEX_INTERPCASEMOD;
2451 /* Is there a right-hand side to take care of? (s//RHS/ or tr//RHS/) */
2452 assert(PL_lex_inwhat != OP_TRANSR);
2453 if (PL_lex_repl && (PL_lex_inwhat == OP_SUBST || PL_lex_inwhat == OP_TRANS)) {
2454 PL_linestr = PL_lex_repl;
2456 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
2457 PL_bufend += SvCUR(PL_linestr);
2458 PL_last_lop = PL_last_uni = NULL;
2459 SAVEFREESV(PL_linestr);
2460 PL_lex_dojoin = FALSE;
2461 PL_lex_brackets = 0;
2462 PL_lex_casemods = 0;
2463 *PL_lex_casestack = '\0';
2465 if (SvEVALED(PL_lex_repl)) {
2466 PL_lex_state = LEX_INTERPNORMAL;
2468 /* we don't clear PL_lex_repl here, so that we can check later
2469 whether this is an evalled subst; that means we rely on the
2470 logic to ensure sublex_done() is called again only via the
2471 branch (in yylex()) that clears PL_lex_repl, else we'll loop */
2474 PL_lex_state = LEX_INTERPCONCAT;
2484 PL_endwhite = newSVpvs("");
2485 sv_catsv(PL_endwhite, PL_thiswhite);
2489 sv_setpvs(PL_thistoken,"");
2491 PL_realtokenstart = -1;
2495 PL_bufend = SvPVX(PL_linestr);
2496 PL_bufend += SvCUR(PL_linestr);
2497 PL_expect = XOPERATOR;
2498 PL_sublex_info.sub_inwhat = 0;
2506 Extracts a pattern, double-quoted string, or transliteration. This
2509 It looks at PL_lex_inwhat and PL_lex_inpat to find out whether it's
2510 processing a pattern (PL_lex_inpat is true), a transliteration
2511 (PL_lex_inwhat == OP_TRANS is true), or a double-quoted string.
2513 Returns a pointer to the character scanned up to. If this is
2514 advanced from the start pointer supplied (i.e. if anything was
2515 successfully parsed), will leave an OP for the substring scanned
2516 in pl_yylval. Caller must intuit reason for not parsing further
2517 by looking at the next characters herself.
2521 constants: \N{NAME} only
2522 case and quoting: \U \Q \E
2523 stops on @ and $, but not for $ as tail anchor
2525 In transliterations:
2526 characters are VERY literal, except for - not at the start or end
2527 of the string, which indicates a range. If the range is in bytes,
2528 scan_const expands the range to the full set of intermediate
2529 characters. If the range is in utf8, the hyphen is replaced with
2530 a certain range mark which will be handled by pmtrans() in op.c.
2532 In double-quoted strings:
2534 double-quoted style: \r and \n
2535 constants: \x31, etc.
2536 deprecated backrefs: \1 (in substitution replacements)
2537 case and quoting: \U \Q \E
2540 scan_const does *not* construct ops to handle interpolated strings.
2541 It stops processing as soon as it finds an embedded $ or @ variable
2542 and leaves it to the caller to work out what's going on.
2544 embedded arrays (whether in pattern or not) could be:
2545 @foo, @::foo, @'foo, @{foo}, @$foo, @+, @-.
2547 $ in double-quoted strings must be the symbol of an embedded scalar.
2549 $ in pattern could be $foo or could be tail anchor. Assumption:
2550 it's a tail anchor if $ is the last thing in the string, or if it's
2551 followed by one of "()| \r\n\t"
2553 \1 (backreferences) are turned into $1
2555 The structure of the code is
2556 while (there's a character to process) {
2557 handle transliteration ranges
2558 skip regexp comments /(?#comment)/ and codes /(?{code})/
2559 skip #-initiated comments in //x patterns
2560 check for embedded arrays
2561 check for embedded scalars
2563 deprecate \1 in substitution replacements
2564 handle string-changing backslashes \l \U \Q \E, etc.
2565 switch (what was escaped) {
2566 handle \- in a transliteration (becomes a literal -)
2567 if a pattern and not \N{, go treat as regular character
2568 handle \132 (octal characters)
2569 handle \x15 and \x{1234} (hex characters)
2570 handle \N{name} (named characters, also \N{3,5} in a pattern)
2571 handle \cV (control characters)
2572 handle printf-style backslashes (\f, \r, \n, etc)
2575 } (end if backslash)
2576 handle regular character
2577 } (end while character to read)
2582 S_scan_const(pTHX_ char *start)
2585 register char *send = PL_bufend; /* end of the constant */
2586 SV *sv = newSV(send - start); /* sv for the constant. See
2587 note below on sizing. */
2588 register char *s = start; /* start of the constant */
2589 register char *d = SvPVX(sv); /* destination for copies */
2590 bool dorange = FALSE; /* are we in a translit range? */
2591 bool didrange = FALSE; /* did we just finish a range? */
2592 I32 has_utf8 = FALSE; /* Output constant is UTF8 */
2593 I32 this_utf8 = UTF; /* Is the source string assumed
2594 to be UTF8? But, this can
2595 show as true when the source
2596 isn't utf8, as for example
2597 when it is entirely composed
2600 /* Note on sizing: The scanned constant is placed into sv, which is
2601 * initialized by newSV() assuming one byte of output for every byte of
2602 * input. This routine expects newSV() to allocate an extra byte for a
2603 * trailing NUL, which this routine will append if it gets to the end of
2604 * the input. There may be more bytes of input than output (eg., \N{LATIN
2605 * CAPITAL LETTER A}), or more output than input if the constant ends up
2606 * recoded to utf8, but each time a construct is found that might increase
2607 * the needed size, SvGROW() is called. Its size parameter each time is
2608 * based on the best guess estimate at the time, namely the length used so
2609 * far, plus the length the current construct will occupy, plus room for
2610 * the trailing NUL, plus one byte for every input byte still unscanned */
2614 UV literal_endpoint = 0;
2615 bool native_range = TRUE; /* turned to FALSE if the first endpoint is Unicode. */
2618 PERL_ARGS_ASSERT_SCAN_CONST;
2620 assert(PL_lex_inwhat != OP_TRANSR);
2621 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
2622 /* If we are doing a trans and we know we want UTF8 set expectation */
2623 has_utf8 = PL_sublex_info.sub_op->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF);
2624 this_utf8 = PL_sublex_info.sub_op->op_private & (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
2628 while (s < send || dorange) {
2630 /* get transliterations out of the way (they're most literal) */
2631 if (PL_lex_inwhat == OP_TRANS) {
2632 /* expand a range A-Z to the full set of characters. AIE! */
2634 I32 i; /* current expanded character */
2635 I32 min; /* first character in range */
2636 I32 max; /* last character in range */
2647 char * const c = (char*)utf8_hop((U8*)d, -1);
2651 *c = (char)UTF_TO_NATIVE(0xff);
2652 /* mark the range as done, and continue */
2658 i = d - SvPVX_const(sv); /* remember current offset */
2661 SvLEN(sv) + (has_utf8 ?
2662 (512 - UTF_CONTINUATION_MARK +
2665 /* How many two-byte within 0..255: 128 in UTF-8,
2666 * 96 in UTF-8-mod. */
2668 SvGROW(sv, SvLEN(sv) + 256); /* never more than 256 chars in a range */
2670 d = SvPVX(sv) + i; /* refresh d after realloc */
2674 for (j = 0; j <= 1; j++) {
2675 char * const c = (char*)utf8_hop((U8*)d, -1);
2676 const UV uv = utf8n_to_uvchr((U8*)c, d - c, NULL, 0);
2682 max = (U8)0xff; /* only to \xff */
2683 uvmax = uv; /* \x{100} to uvmax */
2685 d = c; /* eat endpoint chars */
2690 d -= 2; /* eat the first char and the - */
2691 min = (U8)*d; /* first char in range */
2692 max = (U8)d[1]; /* last char in range */
2699 "Invalid range \"%c-%c\" in transliteration operator",
2700 (char)min, (char)max);
2704 if (literal_endpoint == 2 &&
2705 ((isLOWER(min) && isLOWER(max)) ||
2706 (isUPPER(min) && isUPPER(max)))) {
2708 for (i = min; i <= max; i++)
2710 *d++ = NATIVE_TO_NEED(has_utf8,i);
2712 for (i = min; i <= max; i++)
2714 *d++ = NATIVE_TO_NEED(has_utf8,i);
2719 for (i = min; i <= max; i++)
2722 const U8 ch = (U8)NATIVE_TO_UTF(i);
2723 if (UNI_IS_INVARIANT(ch))
2726 *d++ = (U8)UTF8_EIGHT_BIT_HI(ch);
2727 *d++ = (U8)UTF8_EIGHT_BIT_LO(ch);
2736 d = (char*)uvchr_to_utf8((U8*)d, 0x100);
2738 *d++ = (char)UTF_TO_NATIVE(0xff);
2740 d = (char*)uvchr_to_utf8((U8*)d, uvmax);
2744 /* mark the range as done, and continue */
2748 literal_endpoint = 0;
2753 /* range begins (ignore - as first or last char) */
2754 else if (*s == '-' && s+1 < send && s != start) {
2756 Perl_croak(aTHX_ "Ambiguous range in transliteration operator");
2763 *d++ = (char)UTF_TO_NATIVE(0xff); /* use illegal utf8 byte--see pmtrans */
2773 literal_endpoint = 0;
2774 native_range = TRUE;
2779 /* if we get here, we're not doing a transliteration */
2781 /* skip for regexp comments /(?#comment)/ and code /(?{code})/,
2782 except for the last char, which will be done separately. */
2783 else if (*s == '(' && PL_lex_inpat && s[1] == '?') {
2785 while (s+1 < send && *s != ')')
2786 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
2788 else if (s[2] == '{' /* This should match regcomp.c */
2789 || (s[2] == '?' && s[3] == '{'))
2792 char *regparse = s + (s[2] == '{' ? 3 : 4);
2795 while (count && (c = *regparse)) {
2796 if (c == '\\' && regparse[1])
2804 if (*regparse != ')')
2805 regparse--; /* Leave one char for continuation. */
2806 while (s < regparse)
2807 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
2811 /* likewise skip #-initiated comments in //x patterns */
2812 else if (*s == '#' && PL_lex_inpat &&
2813 ((PMOP*)PL_lex_inpat)->op_pmflags & PMf_EXTENDED) {
2814 while (s+1 < send && *s != '\n')
2815 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
2818 /* check for embedded arrays
2819 (@foo, @::foo, @'foo, @{foo}, @$foo, @+, @-)
2821 else if (*s == '@' && s[1]) {
2822 if (isALNUM_lazy_if(s+1,UTF))
2824 if (strchr(":'{$", s[1]))
2826 if (!PL_lex_inpat && (s[1] == '+' || s[1] == '-'))
2827 break; /* in regexp, neither @+ nor @- are interpolated */
2830 /* check for embedded scalars. only stop if we're sure it's a
2833 else if (*s == '$') {
2834 if (!PL_lex_inpat) /* not a regexp, so $ must be var */
2836 if (s + 1 < send && !strchr("()| \r\n\t", s[1])) {
2838 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
2839 "Possible unintended interpolation of $\\ in regex");
2841 break; /* in regexp, $ might be tail anchor */
2845 /* End of else if chain - OP_TRANS rejoin rest */
2848 if (*s == '\\' && s+1 < send) {
2849 char* e; /* Can be used for ending '}', etc. */
2853 /* warn on \1 - \9 in substitution replacements, but note that \11
2854 * is an octal; and \19 is \1 followed by '9' */
2855 if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat &&
2856 isDIGIT(*s) && *s != '0' && !isDIGIT(s[1]))
2858 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "\\%c better written as $%c", *s, *s);
2863 /* string-change backslash escapes */
2864 if (PL_lex_inwhat != OP_TRANS && *s && strchr("lLuUEQ", *s)) {
2868 /* In a pattern, process \N, but skip any other backslash escapes.
2869 * This is because we don't want to translate an escape sequence
2870 * into a meta symbol and have the regex compiler use the meta
2871 * symbol meaning, e.g. \x{2E} would be confused with a dot. But
2872 * in spite of this, we do have to process \N here while the proper
2873 * charnames handler is in scope. See bugs #56444 and #62056.
2874 * There is a complication because \N in a pattern may also stand
2875 * for 'match a non-nl', and not mean a charname, in which case its
2876 * processing should be deferred to the regex compiler. To be a
2877 * charname it must be followed immediately by a '{', and not look
2878 * like \N followed by a curly quantifier, i.e., not something like
2879 * \N{3,}. regcurly returns a boolean indicating if it is a legal
2881 else if (PL_lex_inpat
2884 || regcurly(s + 1)))
2886 *d++ = NATIVE_TO_NEED(has_utf8,'\\');
2887 goto default_action;
2892 /* quoted - in transliterations */
2894 if (PL_lex_inwhat == OP_TRANS) {
2901 if ((isALPHA(*s) || isDIGIT(*s)))
2902 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
2903 "Unrecognized escape \\%c passed through",
2905 /* default action is to copy the quoted character */
2906 goto default_action;
2909 /* eg. \132 indicates the octal constant 0132 */
2910 case '0': case '1': case '2': case '3':
2911 case '4': case '5': case '6': case '7':
2915 uv = NATIVE_TO_UNI(grok_oct(s, &len, &flags, NULL));
2918 goto NUM_ESCAPE_INSERT;
2920 /* eg. \o{24} indicates the octal constant \024 */
2926 bool valid = grok_bslash_o(s, &uv, &len, &error, 1);
2932 goto NUM_ESCAPE_INSERT;
2935 /* eg. \x24 indicates the hex constant 0x24 */
2939 char* const e = strchr(s, '}');
2940 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
2941 PERL_SCAN_DISALLOW_PREFIX;
2946 yyerror("Missing right brace on \\x{}");
2950 uv = NATIVE_TO_UNI(grok_hex(s, &len, &flags, NULL));
2956 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
2957 uv = NATIVE_TO_UNI(grok_hex(s, &len, &flags, NULL));
2963 /* Insert oct or hex escaped character. There will always be
2964 * enough room in sv since such escapes will be longer than any
2965 * UTF-8 sequence they can end up as, except if they force us
2966 * to recode the rest of the string into utf8 */
2968 /* Here uv is the ordinal of the next character being added in
2969 * unicode (converted from native). */
2970 if (!UNI_IS_INVARIANT(uv)) {
2971 if (!has_utf8 && uv > 255) {
2972 /* Might need to recode whatever we have accumulated so
2973 * far if it contains any chars variant in utf8 or
2976 SvCUR_set(sv, d - SvPVX_const(sv));
2979 /* See Note on sizing above. */
2980 sv_utf8_upgrade_flags_grow(sv,
2981 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
2982 UNISKIP(uv) + (STRLEN)(send - s) + 1);
2983 d = SvPVX(sv) + SvCUR(sv);
2988 d = (char*)uvuni_to_utf8((U8*)d, uv);
2989 if (PL_lex_inwhat == OP_TRANS &&
2990 PL_sublex_info.sub_op) {
2991 PL_sublex_info.sub_op->op_private |=
2992 (PL_lex_repl ? OPpTRANS_FROM_UTF
2996 if (uv > 255 && !dorange)
2997 native_range = FALSE;
3010 /* In a non-pattern \N must be a named character, like \N{LATIN
3011 * SMALL LETTER A} or \N{U+0041}. For patterns, it also can
3012 * mean to match a non-newline. For non-patterns, named
3013 * characters are converted to their string equivalents. In
3014 * patterns, named characters are not converted to their
3015 * ultimate forms for the same reasons that other escapes
3016 * aren't. Instead, they are converted to the \N{U+...} form
3017 * to get the value from the charnames that is in effect right
3018 * now, while preserving the fact that it was a named character
3019 * so that the regex compiler knows this */
3021 /* This section of code doesn't generally use the
3022 * NATIVE_TO_NEED() macro to transform the input. I (khw) did
3023 * a close examination of this macro and determined it is a
3024 * no-op except on utfebcdic variant characters. Every
3025 * character generated by this that would normally need to be
3026 * enclosed by this macro is invariant, so the macro is not
3027 * needed, and would complicate use of copy(). There are other
3028 * parts of this file where the macro is used inconsistently,
3029 * but are saved by it being a no-op */
3031 /* The structure of this section of code (besides checking for
3032 * errors and upgrading to utf8) is:
3033 * Further disambiguate between the two meanings of \N, and if
3034 * not a charname, go process it elsewhere
3035 * If of form \N{U+...}, pass it through if a pattern;
3036 * otherwise convert to utf8
3037 * Otherwise must be \N{NAME}: convert to \N{U+c1.c2...} if a
3038 * pattern; otherwise convert to utf8 */
3040 /* Here, s points to the 'N'; the test below is guaranteed to
3041 * succeed if we are being called on a pattern as we already
3042 * know from a test above that the next character is a '{'.
3043 * On a non-pattern \N must mean 'named sequence, which
3044 * requires braces */
3047 yyerror("Missing braces on \\N{}");
3052 /* If there is no matching '}', it is an error. */
3053 if (! (e = strchr(s, '}'))) {
3054 if (! PL_lex_inpat) {
3055 yyerror("Missing right brace on \\N{}");
3057 yyerror("Missing right brace on \\N{} or unescaped left brace after \\N.");
3062 /* Here it looks like a named character */
3066 /* XXX This block is temporary code. \N{} implies that the
3067 * pattern is to have Unicode semantics, and therefore
3068 * currently has to be encoded in utf8. By putting it in
3069 * utf8 now, we save a whole pass in the regular expression
3070 * compiler. Once that code is changed so Unicode
3071 * semantics doesn't necessarily have to be in utf8, this
3072 * block should be removed */
3074 SvCUR_set(sv, d - SvPVX_const(sv));
3077 /* See Note on sizing above. */
3078 sv_utf8_upgrade_flags_grow(sv,
3079 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3080 /* 5 = '\N{' + cur char + NUL */
3081 (STRLEN)(send - s) + 5);
3082 d = SvPVX(sv) + SvCUR(sv);
3087 if (*s == 'U' && s[1] == '+') { /* \N{U+...} */
3088 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
3089 | PERL_SCAN_DISALLOW_PREFIX;
3092 /* For \N{U+...}, the '...' is a unicode value even on
3093 * EBCDIC machines */
3094 s += 2; /* Skip to next char after the 'U+' */
3096 uv = grok_hex(s, &len, &flags, NULL);
3097 if (len == 0 || len != (STRLEN)(e - s)) {
3098 yyerror("Invalid hexadecimal number in \\N{U+...}");
3105 /* Pass through to the regex compiler unchanged. The
3106 * reason we evaluated the number above is to make sure
3107 * there wasn't a syntax error. */
3108 s -= 5; /* Include the '\N{U+' */
3109 Copy(s, d, e - s + 1, char); /* 1 = include the } */
3112 else { /* Not a pattern: convert the hex to string */
3114 /* If destination is not in utf8, unconditionally
3115 * recode it to be so. This is because \N{} implies
3116 * Unicode semantics, and scalars have to be in utf8
3117 * to guarantee those semantics */
3119 SvCUR_set(sv, d - SvPVX_const(sv));
3122 /* See Note on sizing above. */
3123 sv_utf8_upgrade_flags_grow(
3125 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3126 UNISKIP(uv) + (STRLEN)(send - e) + 1);
3127 d = SvPVX(sv) + SvCUR(sv);
3131 /* Add the string to the output */
3132 if (UNI_IS_INVARIANT(uv)) {
3135 else d = (char*)uvuni_to_utf8((U8*)d, uv);
3138 else { /* Here is \N{NAME} but not \N{U+...}. */
3140 SV *res; /* result from charnames */
3141 const char *str; /* the string in 'res' */
3142 STRLEN len; /* its length */
3144 /* Get the value for NAME */
3145 res = newSVpvn(s, e - s);
3146 res = new_constant( NULL, 0, "charnames",
3147 /* includes all of: \N{...} */
3148 res, NULL, s - 3, e - s + 4 );
3150 /* Most likely res will be in utf8 already since the
3151 * standard charnames uses pack U, but a custom translator
3152 * can leave it otherwise, so make sure. XXX This can be
3153 * revisited to not have charnames use utf8 for characters
3154 * that don't need it when regexes don't have to be in utf8
3155 * for Unicode semantics. If doing so, remember EBCDIC */
3156 sv_utf8_upgrade(res);
3157 str = SvPV_const(res, len);
3159 /* Don't accept malformed input */
3160 if (! is_utf8_string((U8 *) str, len)) {
3161 yyerror("Malformed UTF-8 returned by \\N");
3163 else if (PL_lex_inpat) {
3165 if (! len) { /* The name resolved to an empty string */
3166 Copy("\\N{}", d, 4, char);
3170 /* In order to not lose information for the regex
3171 * compiler, pass the result in the specially made
3172 * syntax: \N{U+c1.c2.c3...}, where c1 etc. are
3173 * the code points in hex of each character
3174 * returned by charnames */
3176 const char *str_end = str + len;
3177 STRLEN char_length; /* cur char's byte length */
3178 STRLEN output_length; /* and the number of bytes
3179 after this is translated
3181 const STRLEN off = d - SvPVX_const(sv);
3183 /* 2 hex per byte; 2 chars for '\N'; 2 chars for
3184 * max('U+', '.'); and 1 for NUL */
3185 char hex_string[2 * UTF8_MAXBYTES + 5];
3187 /* Get the first character of the result. */
3188 U32 uv = utf8n_to_uvuni((U8 *) str,
3193 /* The call to is_utf8_string() above hopefully
3194 * guarantees that there won't be an error. But
3195 * it's easy here to make sure. The function just
3196 * above warns and returns 0 if invalid utf8, but
3197 * it can also return 0 if the input is validly a
3198 * NUL. Disambiguate */
3199 if (uv == 0 && NATIVE_TO_ASCII(*str) != '\0') {
3200 uv = UNICODE_REPLACEMENT;
3203 /* Convert first code point to hex, including the
3204 * boiler plate before it */
3206 my_sprintf(hex_string, "\\N{U+%X", (unsigned int) uv);
3208 /* Make sure there is enough space to hold it */
3209 d = off + SvGROW(sv, off
3211 + (STRLEN)(send - e)
3212 + 2); /* '}' + NUL */
3214 Copy(hex_string, d, output_length, char);
3217 /* For each subsequent character, append dot and
3218 * its ordinal in hex */
3219 while ((str += char_length) < str_end) {
3220 const STRLEN off = d - SvPVX_const(sv);
3221 U32 uv = utf8n_to_uvuni((U8 *) str,
3225 if (uv == 0 && NATIVE_TO_ASCII(*str) != '\0') {
3226 uv = UNICODE_REPLACEMENT;
3230 my_sprintf(hex_string, ".%X", (unsigned int) uv);
3232 d = off + SvGROW(sv, off
3234 + (STRLEN)(send - e)
3235 + 2); /* '}' + NUL */
3236 Copy(hex_string, d, output_length, char);
3240 *d++ = '}'; /* Done. Add the trailing brace */
3243 else { /* Here, not in a pattern. Convert the name to a
3246 /* If destination is not in utf8, unconditionally
3247 * recode it to be so. This is because \N{} implies
3248 * Unicode semantics, and scalars have to be in utf8
3249 * to guarantee those semantics */
3251 SvCUR_set(sv, d - SvPVX_const(sv));
3254 /* See Note on sizing above. */
3255 sv_utf8_upgrade_flags_grow(sv,
3256 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3257 len + (STRLEN)(send - s) + 1);
3258 d = SvPVX(sv) + SvCUR(sv);
3260 } else if (len > (STRLEN)(e - s + 4)) { /* I _guess_ 4 is \N{} --jhi */
3262 /* See Note on sizing above. (NOTE: SvCUR() is not
3263 * set correctly here). */
3264 const STRLEN off = d - SvPVX_const(sv);
3265 d = off + SvGROW(sv, off + len + (STRLEN)(send - s) + 1);
3267 Copy(str, d, len, char);
3272 /* Deprecate non-approved name syntax */
3273 if (ckWARN_d(WARN_DEPRECATED)) {
3274 bool problematic = FALSE;
3277 /* For non-ut8 input, look to see that the first
3278 * character is an alpha, then loop through the rest
3279 * checking that each is a continuation */
3281 if (! isALPHAU(*i)) problematic = TRUE;
3282 else for (i = s + 1; i < e; i++) {
3283 if (isCHARNAME_CONT(*i)) continue;
3289 /* Similarly for utf8. For invariants can check
3290 * directly. We accept anything above the latin1
3291 * range because it is immaterial to Perl if it is
3292 * correct or not, and is expensive to check. But
3293 * it is fairly easy in the latin1 range to convert
3294 * the variants into a single character and check
3296 if (UTF8_IS_INVARIANT(*i)) {
3297 if (! isALPHAU(*i)) problematic = TRUE;
3298 } else if (UTF8_IS_DOWNGRADEABLE_START(*i)) {
3299 if (! isALPHAU(UNI_TO_NATIVE(UTF8_ACCUMULATE(*i,
3305 if (! problematic) for (i = s + UTF8SKIP(s);
3309 if (UTF8_IS_INVARIANT(*i)) {
3310 if (isCHARNAME_CONT(*i)) continue;
3311 } else if (! UTF8_IS_DOWNGRADEABLE_START(*i)) {
3313 } else if (isCHARNAME_CONT(
3315 UTF8_ACCUMULATE(*i, *(i+1)))))
3324 /* The e-i passed to the final %.*s makes sure that
3325 * should the trailing NUL be missing that this
3326 * print won't run off the end of the string */
3327 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
3328 "Deprecated character in \\N{...}; marked by <-- HERE in \\N{%.*s<-- HERE %.*s",
3329 (int)(i - s + 1), s, (int)(e - i), i + 1);
3332 } /* End \N{NAME} */
3335 native_range = FALSE; /* \N{} is defined to be Unicode */
3337 s = e + 1; /* Point to just after the '}' */
3340 /* \c is a control character */
3344 *d++ = grok_bslash_c(*s++, 1);
3347 yyerror("Missing control char name in \\c");
3351 /* printf-style backslashes, formfeeds, newlines, etc */
3353 *d++ = NATIVE_TO_NEED(has_utf8,'\b');
3356 *d++ = NATIVE_TO_NEED(has_utf8,'\n');
3359 *d++ = NATIVE_TO_NEED(has_utf8,'\r');
3362 *d++ = NATIVE_TO_NEED(has_utf8,'\f');
3365 *d++ = NATIVE_TO_NEED(has_utf8,'\t');
3368 *d++ = ASCII_TO_NEED(has_utf8,'\033');
3371 *d++ = ASCII_TO_NEED(has_utf8,'\007');
3377 } /* end if (backslash) */
3384 /* If we started with encoded form, or already know we want it,
3385 then encode the next character */
3386 if (! NATIVE_IS_INVARIANT((U8)(*s)) && (this_utf8 || has_utf8)) {
3390 /* One might think that it is wasted effort in the case of the
3391 * source being utf8 (this_utf8 == TRUE) to take the next character
3392 * in the source, convert it to an unsigned value, and then convert
3393 * it back again. But the source has not been validated here. The
3394 * routine that does the conversion checks for errors like
3397 const UV nextuv = (this_utf8) ? utf8n_to_uvchr((U8*)s, send - s, &len, 0) : (UV) ((U8) *s);
3398 const STRLEN need = UNISKIP(NATIVE_TO_UNI(nextuv));
3400 SvCUR_set(sv, d - SvPVX_const(sv));
3403 /* See Note on sizing above. */
3404 sv_utf8_upgrade_flags_grow(sv,
3405 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3406 need + (STRLEN)(send - s) + 1);
3407 d = SvPVX(sv) + SvCUR(sv);
3409 } else if (need > len) {
3410 /* encoded value larger than old, may need extra space (NOTE:
3411 * SvCUR() is not set correctly here). See Note on sizing
3413 const STRLEN off = d - SvPVX_const(sv);
3414 d = SvGROW(sv, off + need + (STRLEN)(send - s) + 1) + off;
3418 d = (char*)uvchr_to_utf8((U8*)d, nextuv);
3420 if (uv > 255 && !dorange)
3421 native_range = FALSE;
3425 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
3427 } /* while loop to process each character */
3429 /* terminate the string and set up the sv */
3431 SvCUR_set(sv, d - SvPVX_const(sv));
3432 if (SvCUR(sv) >= SvLEN(sv))
3433 Perl_croak(aTHX_ "panic: constant overflowed allocated space");
3436 if (PL_encoding && !has_utf8) {
3437 sv_recode_to_utf8(sv, PL_encoding);
3443 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
3444 PL_sublex_info.sub_op->op_private |=
3445 (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
3449 /* shrink the sv if we allocated more than we used */
3450 if (SvCUR(sv) + 5 < SvLEN(sv)) {
3451 SvPV_shrink_to_cur(sv);
3454 /* return the substring (via pl_yylval) only if we parsed anything */
3455 if (s > PL_bufptr) {
3456 if ( PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ) ) {
3457 const char *const key = PL_lex_inpat ? "qr" : "q";
3458 const STRLEN keylen = PL_lex_inpat ? 2 : 1;
3462 if (PL_lex_inwhat == OP_TRANS) {
3465 } else if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat) {
3473 sv = S_new_constant(aTHX_ start, s - start, key, keylen, sv, NULL,
3476 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
3483 * Returns TRUE if there's more to the expression (e.g., a subscript),
3486 * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
3488 * ->[ and ->{ return TRUE
3489 * { and [ outside a pattern are always subscripts, so return TRUE
3490 * if we're outside a pattern and it's not { or [, then return FALSE
3491 * if we're in a pattern and the first char is a {
3492 * {4,5} (any digits around the comma) returns FALSE
3493 * if we're in a pattern and the first char is a [
3495 * [SOMETHING] has a funky algorithm to decide whether it's a
3496 * character class or not. It has to deal with things like
3497 * /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
3498 * anything else returns TRUE
3501 /* This is the one truly awful dwimmer necessary to conflate C and sed. */
3504 S_intuit_more(pTHX_ register char *s)
3508 PERL_ARGS_ASSERT_INTUIT_MORE;
3510 if (PL_lex_brackets)
3512 if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
3514 if (*s != '{' && *s != '[')
3519 /* In a pattern, so maybe we have {n,m}. */
3527 /* On the other hand, maybe we have a character class */
3530 if (*s == ']' || *s == '^')
3533 /* this is terrifying, and it works */
3534 int weight = 2; /* let's weigh the evidence */
3536 unsigned char un_char = 255, last_un_char;
3537 const char * const send = strchr(s,']');
3538 char tmpbuf[sizeof PL_tokenbuf * 4];
3540 if (!send) /* has to be an expression */
3543 Zero(seen,256,char);
3546 else if (isDIGIT(*s)) {
3548 if (isDIGIT(s[1]) && s[2] == ']')
3554 for (; s < send; s++) {
3555 last_un_char = un_char;
3556 un_char = (unsigned char)*s;
3561 weight -= seen[un_char] * 10;
3562 if (isALNUM_lazy_if(s+1,UTF)) {
3564 scan_ident(s, send, tmpbuf, sizeof tmpbuf, FALSE);
3565 len = (int)strlen(tmpbuf);
3566 if (len > 1 && gv_fetchpvn_flags(tmpbuf, len, 0, SVt_PV))
3571 else if (*s == '$' && s[1] &&
3572 strchr("[#!%*<>()-=",s[1])) {
3573 if (/*{*/ strchr("])} =",s[2]))
3582 if (strchr("wds]",s[1]))
3584 else if (seen[(U8)'\''] || seen[(U8)'"'])
3586 else if (strchr("rnftbxcav",s[1]))
3588 else if (isDIGIT(s[1])) {
3590 while (s[1] && isDIGIT(s[1]))
3600 if (strchr("aA01! ",last_un_char))
3602 if (strchr("zZ79~",s[1]))
3604 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
3605 weight -= 5; /* cope with negative subscript */
3608 if (!isALNUM(last_un_char)
3609 && !(last_un_char == '$' || last_un_char == '@'
3610 || last_un_char == '&')
3611 && isALPHA(*s) && s[1] && isALPHA(s[1])) {
3616 if (keyword(tmpbuf, d - tmpbuf, 0))
3619 if (un_char == last_un_char + 1)
3621 weight -= seen[un_char];
3626 if (weight >= 0) /* probably a character class */
3636 * Does all the checking to disambiguate
3638 * between foo(bar) and bar->foo. Returns 0 if not a method, otherwise
3639 * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
3641 * First argument is the stuff after the first token, e.g. "bar".
3643 * Not a method if bar is a filehandle.
3644 * Not a method if foo is a subroutine prototyped to take a filehandle.
3645 * Not a method if it's really "Foo $bar"
3646 * Method if it's "foo $bar"
3647 * Not a method if it's really "print foo $bar"
3648 * Method if it's really "foo package::" (interpreted as package->foo)
3649 * Not a method if bar is known to be a subroutine ("sub bar; foo bar")
3650 * Not a method if bar is a filehandle or package, but is quoted with
3655 S_intuit_method(pTHX_ char *start, GV *gv, CV *cv)
3658 char *s = start + (*start == '$');
3659 char tmpbuf[sizeof PL_tokenbuf];
3666 PERL_ARGS_ASSERT_INTUIT_METHOD;
3669 if (SvTYPE(gv) == SVt_PVGV && GvIO(gv))
3673 const char *proto = SvPVX_const(cv);
3684 s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
3685 /* start is the beginning of the possible filehandle/object,
3686 * and s is the end of it
3687 * tmpbuf is a copy of it
3690 if (*start == '$') {
3691 if (gv || PL_last_lop_op == OP_PRINT || PL_last_lop_op == OP_SAY ||
3692 isUPPER(*PL_tokenbuf))
3695 len = start - SvPVX(PL_linestr);
3699 start = SvPVX(PL_linestr) + len;
3703 return *s == '(' ? FUNCMETH : METHOD;
3705 if (!keyword(tmpbuf, len, 0)) {
3706 if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
3710 soff = s - SvPVX(PL_linestr);
3714 indirgv = gv_fetchpvn_flags(tmpbuf, len, 0, SVt_PVCV);
3715 if (indirgv && GvCVu(indirgv))
3717 /* filehandle or package name makes it a method */
3718 if (!gv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, 0)) {
3720 soff = s - SvPVX(PL_linestr);
3723 if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
3724 return 0; /* no assumptions -- "=>" quotes bearword */
3726 start_force(PL_curforce);
3727 NEXTVAL_NEXTTOKE.opval = (OP*)newSVOP(OP_CONST, 0,
3728 S_newSV_maybe_utf8(aTHX_ tmpbuf, len));
3729 NEXTVAL_NEXTTOKE.opval->op_private = OPpCONST_BARE;
3731 curmad('X', newSVpvn(start,SvPVX(PL_linestr) + soff - start));
3736 PL_bufptr = SvPVX(PL_linestr) + soff; /* restart before space */
3738 return *s == '(' ? FUNCMETH : METHOD;
3744 /* Encoded script support. filter_add() effectively inserts a
3745 * 'pre-processing' function into the current source input stream.
3746 * Note that the filter function only applies to the current source file
3747 * (e.g., it will not affect files 'require'd or 'use'd by this one).
3749 * The datasv parameter (which may be NULL) can be used to pass
3750 * private data to this instance of the filter. The filter function
3751 * can recover the SV using the FILTER_DATA macro and use it to
3752 * store private buffers and state information.
3754 * The supplied datasv parameter is upgraded to a PVIO type
3755 * and the IoDIRP/IoANY field is used to store the function pointer,
3756 * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
3757 * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
3758 * private use must be set using malloc'd pointers.
3762 Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
3771 if (!PL_rsfp_filters)
3772 PL_rsfp_filters = newAV();
3775 SvUPGRADE(datasv, SVt_PVIO);
3776 IoANY(datasv) = FPTR2DPTR(void *, funcp); /* stash funcp into spare field */
3777 IoFLAGS(datasv) |= IOf_FAKE_DIRP;
3778 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
3779 FPTR2DPTR(void *, IoANY(datasv)),
3780 SvPV_nolen(datasv)));
3781 av_unshift(PL_rsfp_filters, 1);
3782 av_store(PL_rsfp_filters, 0, datasv) ;
3787 /* Delete most recently added instance of this filter function. */
3789 Perl_filter_del(pTHX_ filter_t funcp)
3794 PERL_ARGS_ASSERT_FILTER_DEL;
3797 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p",
3798 FPTR2DPTR(void*, funcp)));
3800 if (!PL_parser || !PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
3802 /* if filter is on top of stack (usual case) just pop it off */
3803 datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
3804 if (IoANY(datasv) == FPTR2DPTR(void *, funcp)) {
3805 sv_free(av_pop(PL_rsfp_filters));
3809 /* we need to search for the correct entry and clear it */
3810 Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
3814 /* Invoke the idxth filter function for the current rsfp. */
3815 /* maxlen 0 = read one text line */
3817 Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
3822 /* This API is bad. It should have been using unsigned int for maxlen.
3823 Not sure if we want to change the API, but if not we should sanity
3824 check the value here. */
3825 const unsigned int correct_length
3834 PERL_ARGS_ASSERT_FILTER_READ;
3836 if (!PL_parser || !PL_rsfp_filters)
3838 if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */
3839 /* Provide a default input filter to make life easy. */
3840 /* Note that we append to the line. This is handy. */
3841 DEBUG_P(PerlIO_printf(Perl_debug_log,
3842 "filter_read %d: from rsfp\n", idx));
3843 if (correct_length) {
3846 const int old_len = SvCUR(buf_sv);
3848 /* ensure buf_sv is large enough */
3849 SvGROW(buf_sv, (STRLEN)(old_len + correct_length + 1)) ;
3850 if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len,
3851 correct_length)) <= 0) {
3852 if (PerlIO_error(PL_rsfp))
3853 return -1; /* error */
3855 return 0 ; /* end of file */
3857 SvCUR_set(buf_sv, old_len + len) ;
3858 SvPVX(buf_sv)[old_len + len] = '\0';
3861 if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
3862 if (PerlIO_error(PL_rsfp))
3863 return -1; /* error */
3865 return 0 ; /* end of file */
3868 return SvCUR(buf_sv);
3870 /* Skip this filter slot if filter has been deleted */
3871 if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) {
3872 DEBUG_P(PerlIO_printf(Perl_debug_log,
3873 "filter_read %d: skipped (filter deleted)\n",
3875 return FILTER_READ(idx+1, buf_sv, correct_length); /* recurse */
3877 /* Get function pointer hidden within datasv */
3878 funcp = DPTR2FPTR(filter_t, IoANY(datasv));
3879 DEBUG_P(PerlIO_printf(Perl_debug_log,
3880 "filter_read %d: via function %p (%s)\n",
3881 idx, (void*)datasv, SvPV_nolen_const(datasv)));
3882 /* Call function. The function is expected to */
3883 /* call "FILTER_READ(idx+1, buf_sv)" first. */
3884 /* Return: <0:error, =0:eof, >0:not eof */
3885 return (*funcp)(aTHX_ idx, buf_sv, correct_length);
3889 S_filter_gets(pTHX_ register SV *sv, STRLEN append)
3893 PERL_ARGS_ASSERT_FILTER_GETS;
3895 #ifdef PERL_CR_FILTER
3896 if (!PL_rsfp_filters) {
3897 filter_add(S_cr_textfilter,NULL);
3900 if (PL_rsfp_filters) {
3902 SvCUR_set(sv, 0); /* start with empty line */
3903 if (FILTER_READ(0, sv, 0) > 0)
3904 return ( SvPVX(sv) ) ;
3909 return (sv_gets(sv, PL_rsfp, append));
3913 S_find_in_my_stash(pTHX_ const char *pkgname, STRLEN len)
3918 PERL_ARGS_ASSERT_FIND_IN_MY_STASH;
3920 if (len == 11 && *pkgname == '_' && strEQ(pkgname, "__PACKAGE__"))
3924 (pkgname[len - 2] == ':' && pkgname[len - 1] == ':') &&
3925 (gv = gv_fetchpvn_flags(pkgname, len, 0, SVt_PVHV)))
3927 return GvHV(gv); /* Foo:: */
3930 /* use constant CLASS => 'MyClass' */
3931 gv = gv_fetchpvn_flags(pkgname, len, 0, SVt_PVCV);
3932 if (gv && GvCV(gv)) {
3933 SV * const sv = cv_const_sv(GvCV(gv));
3935 pkgname = SvPV_const(sv, len);
3938 return gv_stashpvn(pkgname, len, 0);
3942 * S_readpipe_override
3943 * Check whether readpipe() is overriden, and generates the appropriate
3944 * optree, provided sublex_start() is called afterwards.
3947 S_readpipe_override(pTHX)
3950 GV *gv_readpipe = gv_fetchpvs("readpipe", GV_NOTQUAL, SVt_PVCV);
3951 pl_yylval.ival = OP_BACKTICK;
3953 && GvCVu(gv_readpipe) && GvIMPORTED_CV(gv_readpipe))
3955 ((gvp = (GV**)hv_fetchs(PL_globalstash, "readpipe", FALSE))
3956 && (gv_readpipe = *gvp) && isGV_with_GP(gv_readpipe)
3957 && GvCVu(gv_readpipe) && GvIMPORTED_CV(gv_readpipe)))
3959 PL_lex_op = (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
3960 op_append_elem(OP_LIST,
3961 newSVOP(OP_CONST, 0, &PL_sv_undef), /* value will be read later */
3962 newCVREF(0, newGVOP(OP_GV, 0, gv_readpipe))));
3969 * The intent of this yylex wrapper is to minimize the changes to the
3970 * tokener when we aren't interested in collecting madprops. It remains
3971 * to be seen how successful this strategy will be...
3978 char *s = PL_bufptr;
3980 /* make sure PL_thiswhite is initialized */
3984 /* just do what yylex would do on pending identifier; leave PL_thiswhite alone */
3985 if (PL_lex_state != LEX_KNOWNEXT && PL_pending_ident)
3986 return S_pending_ident(aTHX);
3988 /* previous token ate up our whitespace? */
3989 if (!PL_lasttoke && PL_nextwhite) {
3990 PL_thiswhite = PL_nextwhite;
3994 /* isolate the token, and figure out where it is without whitespace */
3995 PL_realtokenstart = -1;
3999 assert(PL_curforce < 0);
4001 if (!PL_thismad || PL_thismad->mad_key == '^') { /* not forced already? */
4002 if (!PL_thistoken) {
4003 if (PL_realtokenstart < 0 || !CopLINE(PL_curcop))
4004 PL_thistoken = newSVpvs("");
4006 char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
4007 PL_thistoken = newSVpvn(tstart, s - tstart);
4010 if (PL_thismad) /* install head */
4011 CURMAD('X', PL_thistoken);
4014 /* last whitespace of a sublex? */
4015 if (optype == ')' && PL_endwhite) {
4016 CURMAD('X', PL_endwhite);
4021 /* if no whitespace and we're at EOF, bail. Otherwise fake EOF below. */
4022 if (!PL_thiswhite && !PL_endwhite && !optype) {
4023 sv_free(PL_thistoken);
4028 /* put off final whitespace till peg */
4029 if (optype == ';' && !PL_rsfp) {
4030 PL_nextwhite = PL_thiswhite;
4033 else if (PL_thisopen) {
4034 CURMAD('q', PL_thisopen);
4036 sv_free(PL_thistoken);
4040 /* Store actual token text as madprop X */
4041 CURMAD('X', PL_thistoken);
4045 /* add preceding whitespace as madprop _ */
4046 CURMAD('_', PL_thiswhite);
4050 /* add quoted material as madprop = */
4051 CURMAD('=', PL_thisstuff);
4055 /* add terminating quote as madprop Q */
4056 CURMAD('Q', PL_thisclose);
4060 /* special processing based on optype */
4064 /* opval doesn't need a TOKEN since it can already store mp */
4074 if (pl_yylval.opval)
4075 append_madprops(PL_thismad, pl_yylval.opval, 0);
4083 addmad(newMADsv('p', PL_endwhite), &PL_thismad, 0);
4092 /* remember any fake bracket that lexer is about to discard */
4093 if (PL_lex_brackets == 1 &&
4094 ((expectation)PL_lex_brackstack[0] & XFAKEBRACK))
4097 while (s < PL_bufend && (*s == ' ' || *s == '\t'))
4100 PL_thiswhite = newSVpvn(PL_bufptr, ++s - PL_bufptr);
4101 addmad(newMADsv('#', PL_thiswhite), &PL_thismad, 0);
4104 break; /* don't bother looking for trailing comment */
4113 /* attach a trailing comment to its statement instead of next token */
4117 if (PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == optype) {
4119 while (s < PL_bufend && (*s == ' ' || *s == '\t'))
4121 if (*s == '\n' || *s == '#') {
4122 while (s < PL_bufend && *s != '\n')
4126 PL_thiswhite = newSVpvn(PL_bufptr, s - PL_bufptr);
4127 addmad(newMADsv('#', PL_thiswhite), &PL_thismad, 0);
4144 /* Create new token struct. Note: opvals return early above. */
4145 pl_yylval.tkval = newTOKEN(optype, pl_yylval, PL_thismad);
4152 S_tokenize_use(pTHX_ int is_use, char *s) {
4155 PERL_ARGS_ASSERT_TOKENIZE_USE;
4157 if (PL_expect != XSTATE)
4158 yyerror(Perl_form(aTHX_ "\"%s\" not allowed in expression",
4159 is_use ? "use" : "no"));
4161 if (isDIGIT(*s) || (*s == 'v' && isDIGIT(s[1]))) {
4162 s = force_version(s, TRUE);
4163 if (*s == ';' || *s == '}'
4164 || (s = SKIPSPACE1(s), (*s == ';' || *s == '}'))) {
4165 start_force(PL_curforce);
4166 NEXTVAL_NEXTTOKE.opval = NULL;
4169 else if (*s == 'v') {
4170 s = force_word(s,WORD,FALSE,TRUE,FALSE);
4171 s = force_version(s, FALSE);
4175 s = force_word(s,WORD,FALSE,TRUE,FALSE);
4176 s = force_version(s, FALSE);
4178 pl_yylval.ival = is_use;
4182 static const char* const exp_name[] =
4183 { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
4184 "ATTRTERM", "TERMBLOCK", "TERMORDORDOR"
4188 #define word_takes_any_delimeter(p,l) S_word_takes_any_delimeter(p,l)
4190 S_word_takes_any_delimeter(char *p, STRLEN len)
4192 return (len == 1 && strchr("msyq", p[0])) ||
4194 (p[0] == 't' && p[1] == 'r') ||
4195 (p[0] == 'q' && strchr("qwxr", p[1]))));
4201 Works out what to call the token just pulled out of the input
4202 stream. The yacc parser takes care of taking the ops we return and
4203 stitching them into a tree.
4209 if read an identifier
4210 if we're in a my declaration
4211 croak if they tried to say my($foo::bar)
4212 build the ops for a my() declaration
4213 if it's an access to a my() variable
4214 are we in a sort block?
4215 croak if my($a); $a <=> $b
4216 build ops for access to a my() variable
4217 if in a dq string, and they've said @foo and we can't find @foo
4219 build ops for a bareword
4220 if we already built the token before, use it.
4225 #pragma segment Perl_yylex
4231 register char *s = PL_bufptr;
4237 /* orig_keyword, gvp, and gv are initialized here because
4238 * jump to the label just_a_word_zero can bypass their
4239 * initialization later. */
4240 I32 orig_keyword = 0;
4245 SV* tmp = newSVpvs("");
4246 PerlIO_printf(Perl_debug_log, "### %"IVdf":LEX_%s/X%s %s\n",
4247 (IV)CopLINE(PL_curcop),
4248 lex_state_names[PL_lex_state],
4249 exp_name[PL_expect],
4250 pv_display(tmp, s, strlen(s), 0, 60));
4253 /* check if there's an identifier for us to look at */
4254 if (PL_lex_state != LEX_KNOWNEXT && PL_pending_ident)
4255 return REPORT(S_pending_ident(aTHX));
4257 /* no identifier pending identification */
4259 switch (PL_lex_state) {
4261 case LEX_NORMAL: /* Some compilers will produce faster */
4262 case LEX_INTERPNORMAL: /* code if we comment these out. */
4266 /* when we've already built the next token, just pull it out of the queue */
4270 pl_yylval = PL_nexttoke[PL_lasttoke].next_val;
4272 PL_thismad = PL_nexttoke[PL_lasttoke].next_mad;
4273 PL_nexttoke[PL_lasttoke].next_mad = 0;
4274 if (PL_thismad && PL_thismad->mad_key == '_') {
4275 PL_thiswhite = MUTABLE_SV(PL_thismad->mad_val);
4276 PL_thismad->mad_val = 0;
4277 mad_free(PL_thismad);
4282 PL_lex_state = PL_lex_defer;
4283 PL_expect = PL_lex_expect;
4284 PL_lex_defer = LEX_NORMAL;
4285 if (!PL_nexttoke[PL_lasttoke].next_type)
4290 pl_yylval = PL_nextval[PL_nexttoke];
4292 PL_lex_state = PL_lex_defer;
4293 PL_expect = PL_lex_expect;
4294 PL_lex_defer = LEX_NORMAL;
4300 next_type = PL_nexttoke[PL_lasttoke].next_type;
4302 next_type = PL_nexttype[PL_nexttoke];
4304 if (next_type & (1<<24)) {
4305 if (PL_lex_brackets > 100)
4306 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
4307 PL_lex_brackstack[PL_lex_brackets++] = (next_type >> 16) & 0xff;
4308 next_type &= 0xffff;
4311 /* FIXME - can these be merged? */
4314 return REPORT(next_type);
4318 /* interpolated case modifiers like \L \U, including \Q and \E.
4319 when we get here, PL_bufptr is at the \
4321 case LEX_INTERPCASEMOD:
4323 if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
4324 Perl_croak(aTHX_ "panic: INTERPCASEMOD");
4326 /* handle \E or end of string */
4327 if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
4329 if (PL_lex_casemods) {
4330 const char oldmod = PL_lex_casestack[--PL_lex_casemods];
4331 PL_lex_casestack[PL_lex_casemods] = '\0';
4333 if (PL_bufptr != PL_bufend
4334 && (oldmod == 'L' || oldmod == 'U' || oldmod == 'Q')) {
4336 PL_lex_state = LEX_INTERPCONCAT;
4339 PL_thistoken = newSVpvs("\\E");
4345 while (PL_bufptr != PL_bufend &&
4346 PL_bufptr[0] == '\\' && PL_bufptr[1] == 'E') {
4348 PL_thiswhite = newSVpvs("");
4349 sv_catpvn(PL_thiswhite, PL_bufptr, 2);
4353 if (PL_bufptr != PL_bufend)
4356 PL_lex_state = LEX_INTERPCONCAT;
4360 DEBUG_T({ PerlIO_printf(Perl_debug_log,
4361 "### Saw case modifier\n"); });
4363 if (s[1] == '\\' && s[2] == 'E') {
4366 PL_thiswhite = newSVpvs("");
4367 sv_catpvn(PL_thiswhite, PL_bufptr, 4);
4370 PL_lex_state = LEX_INTERPCONCAT;
4375 if (!PL_madskills) /* when just compiling don't need correct */
4376 if (strnEQ(s, "L\\u", 3) || strnEQ(s, "U\\l", 3))
4377 tmp = *s, *s = s[2], s[2] = (char)tmp; /* misordered... */
4378 if ((*s == 'L' || *s == 'U') &&
4379 (strchr(PL_lex_casestack, 'L') || strchr(PL_lex_casestack, 'U'))) {
4380 PL_lex_casestack[--PL_lex_casemods] = '\0';
4383 if (PL_lex_casemods > 10)
4384 Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
4385 PL_lex_casestack[PL_lex_casemods++] = *s;
4386 PL_lex_casestack[PL_lex_casemods] = '\0';
4387 PL_lex_state = LEX_INTERPCONCAT;
4388 start_force(PL_curforce);
4389 NEXTVAL_NEXTTOKE.ival = 0;
4391 start_force(PL_curforce);
4393 NEXTVAL_NEXTTOKE.ival = OP_LCFIRST;
4395 NEXTVAL_NEXTTOKE.ival = OP_UCFIRST;
4397 NEXTVAL_NEXTTOKE.ival = OP_LC;
4399 NEXTVAL_NEXTTOKE.ival = OP_UC;
4401 NEXTVAL_NEXTTOKE.ival = OP_QUOTEMETA;
4403 Perl_croak(aTHX_ "panic: yylex");
4405 SV* const tmpsv = newSVpvs("\\ ");
4406 /* replace the space with the character we want to escape
4408 SvPVX(tmpsv)[1] = *s;
4414 if (PL_lex_starts) {
4420 sv_free(PL_thistoken);
4421 PL_thistoken = newSVpvs("");
4424 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
4425 if (PL_lex_casemods == 1 && PL_lex_inpat)
4434 case LEX_INTERPPUSH:
4435 return REPORT(sublex_push());
4437 case LEX_INTERPSTART:
4438 if (PL_bufptr == PL_bufend)
4439 return REPORT(sublex_done());
4440 DEBUG_T({ PerlIO_printf(Perl_debug_log,
4441 "### Interpolated variable\n"); });
4443 PL_lex_dojoin = (*PL_bufptr == '@');
4444 PL_lex_state = LEX_INTERPNORMAL;
4445 if (PL_lex_dojoin) {
4446 start_force(PL_curforce);
4447 NEXTVAL_NEXTTOKE.ival = 0;
4449 start_force(PL_curforce);
4450 force_ident("\"", '$');
4451 start_force(PL_curforce);
4452 NEXTVAL_NEXTTOKE.ival = 0;
4454 start_force(PL_curforce);
4455 NEXTVAL_NEXTTOKE.ival = 0;
4457 start_force(PL_curforce);
4458 NEXTVAL_NEXTTOKE.ival = OP_JOIN; /* emulate join($", ...) */
4461 if (PL_lex_starts++) {
4466 sv_free(PL_thistoken);
4467 PL_thistoken = newSVpvs("");
4470 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
4471 if (!PL_lex_casemods && PL_lex_inpat)
4478 case LEX_INTERPENDMAYBE:
4479 if (intuit_more(PL_bufptr)) {
4480 PL_lex_state = LEX_INTERPNORMAL; /* false alarm, more expr */
4486 if (PL_lex_dojoin) {
4487 PL_lex_dojoin = FALSE;
4488 PL_lex_state = LEX_INTERPCONCAT;
4492 sv_free(PL_thistoken);
4493 PL_thistoken = newSVpvs("");
4498 if (PL_lex_inwhat == OP_SUBST && PL_linestr == PL_lex_repl
4499 && SvEVALED(PL_lex_repl))
4501 if (PL_bufptr != PL_bufend)
4502 Perl_croak(aTHX_ "Bad evalled substitution pattern");
4506 case LEX_INTERPCONCAT:
4508 if (PL_lex_brackets)
4509 Perl_croak(aTHX_ "panic: INTERPCONCAT");
4511 if (PL_bufptr == PL_bufend)
4512 return REPORT(sublex_done());
4514 if (SvIVX(PL_linestr) == '\'') {
4515 SV *sv = newSVsv(PL_linestr);
4518 else if ( PL_hints & HINT_NEW_RE )
4519 sv = new_constant(NULL, 0, "qr", sv, sv, "q", 1);
4520 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
4524 s = scan_const(PL_bufptr);
4526 PL_lex_state = LEX_INTERPCASEMOD;
4528 PL_lex_state = LEX_INTERPSTART;
4531 if (s != PL_bufptr) {
4532 start_force(PL_curforce);
4534 curmad('X', newSVpvn(PL_bufptr,s-PL_bufptr));
4536 NEXTVAL_NEXTTOKE = pl_yylval;
4539 if (PL_lex_starts++) {
4543 sv_free(PL_thistoken);
4544 PL_thistoken = newSVpvs("");
4547 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
4548 if (!PL_lex_casemods && PL_lex_inpat)
4561 PL_lex_state = LEX_NORMAL;
4562 s = scan_formline(PL_bufptr);
4563 if (!PL_lex_formbrack)
4569 PL_oldoldbufptr = PL_oldbufptr;
4575 sv_free(PL_thistoken);
4578 PL_realtokenstart = s - SvPVX(PL_linestr); /* assume but undo on ws */
4582 if (isIDFIRST_lazy_if(s,UTF))
4585 unsigned char c = *s;
4586 len = UTF ? Perl_utf8_length(aTHX_ (U8 *) PL_linestart, (U8 *) s) : (STRLEN) (s - PL_linestart);
4587 if (len > UNRECOGNIZED_PRECEDE_COUNT) {
4588 d = UTF ? (char *) Perl_utf8_hop(aTHX_ (U8 *) s, -UNRECOGNIZED_PRECEDE_COUNT) : s - UNRECOGNIZED_PRECEDE_COUNT;
4593 Perl_croak(aTHX_ "Unrecognized character \\x%02X; marked by <-- HERE after %s<-- HERE near column %d", c, d, (int) len + 1);
4597 goto fake_eof; /* emulate EOF on ^D or ^Z */
4606 if (PL_lex_brackets &&
4607 PL_lex_brackstack[PL_lex_brackets-1] != XFAKEEOF) {
4608 yyerror((const char *)
4610 ? "Format not terminated"
4611 : "Missing right curly or square bracket"));
4613 DEBUG_T( { PerlIO_printf(Perl_debug_log,
4614 "### Tokener got EOF\n");
4618 if (s++ < PL_bufend)
4619 goto retry; /* ignore stray nulls */
4622 if (!PL_in_eval && !PL_preambled) {
4623 PL_preambled = TRUE;
4629 /* Generate a string of Perl code to load the debugger.
4630 * If PERL5DB is set, it will return the contents of that,
4631 * otherwise a compile-time require of perl5db.pl. */
4633 const char * const pdb = PerlEnv_getenv("PERL5DB");
4636 sv_setpv(PL_linestr, pdb);
4637 sv_catpvs(PL_linestr,";");
4639 SETERRNO(0,SS_NORMAL);
4640 sv_setpvs(PL_linestr, "BEGIN { require 'perl5db.pl' };");
4643 sv_setpvs(PL_linestr,"");
4644 if (PL_preambleav) {
4645 SV **svp = AvARRAY(PL_preambleav);
4646 SV **const end = svp + AvFILLp(PL_preambleav);
4648 sv_catsv(PL_linestr, *svp);
4650 sv_catpvs(PL_linestr, ";");
4652 sv_free(MUTABLE_SV(PL_preambleav));
4653 PL_preambleav = NULL;
4656 sv_catpvs(PL_linestr,
4657 "use feature ':5." STRINGIFY(PERL_VERSION) "';");
4658 if (PL_minus_n || PL_minus_p) {
4659 sv_catpvs(PL_linestr, "LINE: while (<>) {"/*}*/);
4661 sv_catpvs(PL_linestr,"chomp;");
4664 if ((*PL_splitstr == '/' || *PL_splitstr == '\''
4665 || *PL_splitstr == '"')
4666 && strchr(PL_splitstr + 1, *PL_splitstr))
4667 Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
4669 /* "q\0${splitstr}\0" is legal perl. Yes, even NUL
4670 bytes can be used as quoting characters. :-) */
4671 const char *splits = PL_splitstr;
4672 sv_catpvs(PL_linestr, "our @F=split(q\0");
4675 if (*splits == '\\')
4676 sv_catpvn(PL_linestr, splits, 1);
4677 sv_catpvn(PL_linestr, splits, 1);
4678 } while (*splits++);
4679 /* This loop will embed the trailing NUL of
4680 PL_linestr as the last thing it does before
4682 sv_catpvs(PL_linestr, ");");
4686 sv_catpvs(PL_linestr,"our @F=split(' ');");
4689 sv_catpvs(PL_linestr, "\n");
4690 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
4691 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
4692 PL_last_lop = PL_last_uni = NULL;
4693 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
4694 update_debugger_info(PL_linestr, NULL, 0);
4699 bof = PL_rsfp ? TRUE : FALSE;
4702 fake_eof = LEX_FAKE_EOF;
4704 PL_bufptr = PL_bufend;
4705 CopLINE_inc(PL_curcop);
4706 if (!lex_next_chunk(fake_eof)) {
4707 CopLINE_dec(PL_curcop);
4709 TOKEN(';'); /* not infinite loop because rsfp is NULL now */
4711 CopLINE_dec(PL_curcop);
4714 PL_realtokenstart = -1;
4717 /* If it looks like the start of a BOM or raw UTF-16,
4718 * check if it in fact is. */
4719 if (bof && PL_rsfp &&
4724 bof = PerlIO_tell(PL_rsfp) == (Off_t)SvCUR(PL_linestr);
4726 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
4727 s = swallow_bom((U8*)s);
4730 if (PL_parser->in_pod) {
4731 /* Incest with pod. */
4734 sv_catsv(PL_thiswhite, PL_linestr);
4736 if (*s == '=' && strnEQ(s, "=cut", 4) && !isALPHA(s[4])) {
4737 sv_setpvs(PL_linestr, "");
4738 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
4739 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
4740 PL_last_lop = PL_last_uni = NULL;
4741 PL_parser->in_pod = 0;
4746 } while (PL_parser->in_pod);
4747 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = s;
4748 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
4749 PL_last_lop = PL_last_uni = NULL;
4750 if (CopLINE(PL_curcop) == 1) {
4751 while (s < PL_bufend && isSPACE(*s))
4753 if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
4757 PL_thiswhite = newSVpvn(PL_linestart, s - PL_linestart);
4761 if (*s == '#' && *(s+1) == '!')
4763 #ifdef ALTERNATE_SHEBANG
4765 static char const as[] = ALTERNATE_SHEBANG;
4766 if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
4767 d = s + (sizeof(as) - 1);
4769 #endif /* ALTERNATE_SHEBANG */
4778 while (*d && !isSPACE(*d))
4782 #ifdef ARG_ZERO_IS_SCRIPT
4783 if (ipathend > ipath) {
4785 * HP-UX (at least) sets argv[0] to the script name,
4786 * which makes $^X incorrect. And Digital UNIX and Linux,
4787 * at least, set argv[0] to the basename of the Perl
4788 * interpreter. So, having found "#!", we'll set it right.
4790 SV * const x = GvSV(gv_fetchpvs("\030", GV_ADD|GV_NOTQUAL,
4792 assert(SvPOK(x) || SvGMAGICAL(x));
4793 if (sv_eq(x, CopFILESV(PL_curcop))) {
4794 sv_setpvn(x, ipath, ipathend - ipath);
4800 const char *bstart = SvPV_const(CopFILESV(PL_curcop),blen);
4801 const char * const lstart = SvPV_const(x,llen);
4803 bstart += blen - llen;
4804 if (strnEQ(bstart, lstart, llen) && bstart[-1] == '/') {
4805 sv_setpvn(x, ipath, ipathend - ipath);
4810 TAINT_NOT; /* $^X is always tainted, but that's OK */
4812 #endif /* ARG_ZERO_IS_SCRIPT */
4817 d = instr(s,"perl -");
4819 d = instr(s,"perl");
4821 /* avoid getting into infinite loops when shebang
4822 * line contains "Perl" rather than "perl" */
4824 for (d = ipathend-4; d >= ipath; --d) {
4825 if ((*d == 'p' || *d == 'P')
4826 && !ibcmp(d, "perl", 4))
4836 #ifdef ALTERNATE_SHEBANG
4838 * If the ALTERNATE_SHEBANG on this system starts with a
4839 * character that can be part of a Perl expression, then if
4840 * we see it but not "perl", we're probably looking at the
4841 * start of Perl code, not a request to hand off to some
4842 * other interpreter. Similarly, if "perl" is there, but
4843 * not in the first 'word' of the line, we assume the line
4844 * contains the start of the Perl program.
4846 if (d && *s != '#') {
4847 const char *c = ipath;
4848 while (*c && !strchr("; \t\r\n\f\v#", *c))
4851 d = NULL; /* "perl" not in first word; ignore */
4853 *s = '#'; /* Don't try to parse shebang line */
4855 #endif /* ALTERNATE_SHEBANG */
4860 !instr(s,"indir") &&
4861 instr(PL_origargv[0],"perl"))
4868 while (s < PL_bufend && isSPACE(*s))
4870 if (s < PL_bufend) {
4871 Newx(newargv,PL_origargc+3,char*);
4873 while (s < PL_bufend && !isSPACE(*s))
4876 Copy(PL_origargv+1, newargv+2, PL_origargc+1, char*);
4879 newargv = PL_origargv;
4882 PerlProc_execv(ipath, EXEC_ARGV_CAST(newargv));
4884 Perl_croak(aTHX_ "Can't exec %s", ipath);
4887 while (*d && !isSPACE(*d))
4889 while (SPACE_OR_TAB(*d))
4893 const bool switches_done = PL_doswitches;
4894 const U32 oldpdb = PL_perldb;
4895 const bool oldn = PL_minus_n;
4896 const bool oldp = PL_minus_p;
4900 bool baduni = FALSE;
4902 const char *d2 = d1 + 1;
4903 if (parse_unicode_opts((const char **)&d2)
4907 if (baduni || *d1 == 'M' || *d1 == 'm') {
4908 const char * const m = d1;
4909 while (*d1 && !isSPACE(*d1))
4911 Perl_croak(aTHX_ "Too late for \"-%.*s\" option",
4914 d1 = moreswitches(d1);
4916 if (PL_doswitches && !switches_done) {
4917 int argc = PL_origargc;
4918 char **argv = PL_origargv;
4921 } while (argc && argv[0][0] == '-' && argv[0][1]);
4922 init_argv_symbols(argc,argv);
4924 if (((PERLDB_LINE || PERLDB_SAVESRC) && !oldpdb) ||
4925 ((PL_minus_n || PL_minus_p) && !(oldn || oldp)))
4926 /* if we have already added "LINE: while (<>) {",
4927 we must not do it again */
4929 sv_setpvs(PL_linestr, "");
4930 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
4931 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
4932 PL_last_lop = PL_last_uni = NULL;
4933 PL_preambled = FALSE;
4934 if (PERLDB_LINE || PERLDB_SAVESRC)
4935 (void)gv_fetchfile(PL_origfilename);
4942 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
4944 PL_lex_state = LEX_FORMLINE;
4949 #ifdef PERL_STRICT_CR
4950 Perl_warn(aTHX_ "Illegal character \\%03o (carriage return)", '\r');
4952 "\t(Maybe you didn't strip carriage returns after a network transfer?)\n");
4954 case ' ': case '\t': case '\f': case 013:
4956 PL_realtokenstart = -1;
4958 PL_thiswhite = newSVpvs("");
4959 sv_catpvn(PL_thiswhite, s, 1);
4966 PL_realtokenstart = -1;
4970 if (PL_lex_state != LEX_NORMAL || (PL_in_eval && !PL_rsfp)) {
4971 if (*s == '#' && s == PL_linestart && PL_in_eval && !PL_rsfp) {
4972 /* handle eval qq[#line 1 "foo"\n ...] */
4973 CopLINE_dec(PL_curcop);
4976 if (PL_madskills && !PL_lex_formbrack && !PL_in_eval) {
4978 if (!PL_in_eval || PL_rsfp)
4983 while (d < PL_bufend && *d != '\n')
4987 else if (d > PL_bufend) /* Found by Ilya: feed random input to Perl. */
4988 Perl_croak(aTHX_ "panic: input overflow");
4991 PL_thiswhite = newSVpvn(s, d - s);
4996 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
4998 PL_lex_state = LEX_FORMLINE;
5004 if (PL_madskills && CopLINE(PL_curcop) >= 1 && !PL_lex_formbrack) {
5005 if (CopLINE(PL_curcop) == 1 && s[0] == '#' && s[1] == '!') {
5008 TOKEN(PEG); /* make sure any #! line is accessible */
5013 /* if (PL_madskills && PL_lex_formbrack) { */
5015 while (d < PL_bufend && *d != '\n')
5019 else if (d > PL_bufend) /* Found by Ilya: feed random input to Perl. */
5020 Perl_croak(aTHX_ "panic: input overflow");
5021 if (PL_madskills && CopLINE(PL_curcop) >= 1) {
5023 PL_thiswhite = newSVpvs("");
5024 if (CopLINE(PL_curcop) == 1) {
5025 sv_setpvs(PL_thiswhite, "");
5028 sv_catpvn(PL_thiswhite, s, d - s);
5042 if (s[1] && isALPHA(s[1]) && !isALNUM(s[2])) {
5050 while (s < PL_bufend && SPACE_OR_TAB(*s))
5053 if (strnEQ(s,"=>",2)) {
5054 s = force_word(PL_bufptr,WORD,FALSE,FALSE,FALSE);
5055 DEBUG_T( { printbuf("### Saw unary minus before =>, forcing word %s\n", s); } );
5056 OPERATOR('-'); /* unary minus */
5058 PL_last_uni = PL_oldbufptr;
5060 case 'r': ftst = OP_FTEREAD; break;
5061 case 'w': ftst = OP_FTEWRITE; break;
5062 case 'x': ftst = OP_FTEEXEC; break;
5063 case 'o': ftst = OP_FTEOWNED; break;
5064 case 'R': ftst = OP_FTRREAD; break;
5065 case 'W': ftst = OP_FTRWRITE; break;
5066 case 'X': ftst = OP_FTREXEC; break;
5067 case 'O': ftst = OP_FTROWNED; break;
5068 case 'e': ftst = OP_FTIS; break;
5069 case 'z': ftst = OP_FTZERO; break;
5070 case 's': ftst = OP_FTSIZE; break;
5071 case 'f': ftst = OP_FTFILE; break;
5072 case 'd': ftst = OP_FTDIR; break;
5073 case 'l': ftst = OP_FTLINK; break;
5074 case 'p': ftst = OP_FTPIPE; break;
5075 case 'S': ftst = OP_FTSOCK; break;
5076 case 'u': ftst = OP_FTSUID; break;
5077 case 'g': ftst = OP_FTSGID; break;
5078 case 'k': ftst = OP_FTSVTX; break;
5079 case 'b': ftst = OP_FTBLK; break;
5080 case 'c': ftst = OP_FTCHR; break;
5081 case 't': ftst = OP_FTTTY; break;
5082 case 'T': ftst = OP_FTTEXT; break;
5083 case 'B': ftst = OP_FTBINARY; break;
5084 case 'M': case 'A': case 'C':
5085 gv_fetchpvs("\024", GV_ADD|GV_NOTQUAL, SVt_PV);
5087 case 'M': ftst = OP_FTMTIME; break;
5088 case 'A': ftst = OP_FTATIME; break;
5089 case 'C': ftst = OP_FTCTIME; break;
5097 PL_last_lop_op = (OPCODE)ftst;
5098 DEBUG_T( { PerlIO_printf(Perl_debug_log,
5099 "### Saw file test %c\n", (int)tmp);
5104 /* Assume it was a minus followed by a one-letter named
5105 * subroutine call (or a -bareword), then. */
5106 DEBUG_T( { PerlIO_printf(Perl_debug_log,
5107 "### '-%c' looked like a file test but was not\n",
5114 const char tmp = *s++;
5117 if (PL_expect == XOPERATOR)
5122 else if (*s == '>') {
5125 if (isIDFIRST_lazy_if(s,UTF)) {
5126 s = force_word(s,METHOD,FALSE,TRUE,FALSE);
5134 if (PL_expect == XOPERATOR)
5137 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
5139 OPERATOR('-'); /* unary minus */
5145 const char tmp = *s++;
5148 if (PL_expect == XOPERATOR)
5153 if (PL_expect == XOPERATOR)
5156 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
5163 if (PL_expect != XOPERATOR) {
5164 s = scan_ident(s, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
5165 PL_expect = XOPERATOR;
5166 force_ident(PL_tokenbuf, '*');
5179 if (PL_expect == XOPERATOR) {
5183 PL_tokenbuf[0] = '%';
5184 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1,
5185 sizeof PL_tokenbuf - 1, FALSE);
5186 if (!PL_tokenbuf[1]) {
5189 PL_pending_ident = '%';
5196 if (PL_lex_brackets > 100)
5197 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
5198 PL_lex_brackstack[PL_lex_brackets++] = 0;
5200 const char tmp = *s++;
5205 && (PL_expect == XOPERATOR || PL_expect == XTERMORDORDOR))
5212 const char tmp = *s++;
5218 goto just_a_word_zero_gv;
5221 switch (PL_expect) {
5227 if (!PL_in_my || PL_lex_state != LEX_NORMAL)
5229 PL_bufptr = s; /* update in case we back off */
5231 deprecate(":= for an empty attribute list");
5238 PL_expect = XTERMBLOCK;
5241 stuffstart = s - SvPVX(PL_linestr) - 1;
5245 while (isIDFIRST_lazy_if(s,UTF)) {
5248 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
5249 if (isLOWER(*s) && (tmp = keyword(PL_tokenbuf, len, 0))) {
5250 if (tmp < 0) tmp = -tmp;
5265 sv = newSVpvn(s, len);
5267 d = scan_str(d,TRUE,TRUE);
5269 /* MUST advance bufptr here to avoid bogus
5270 "at end of line" context messages from yyerror().
5272 PL_bufptr = s + len;
5273 yyerror("Unterminated attribute parameter in attribute list");
5277 return REPORT(0); /* EOF indicator */
5281 sv_catsv(sv, PL_lex_stuff);
5282 attrs = op_append_elem(OP_LIST, attrs,
5283 newSVOP(OP_CONST, 0, sv));
5284 SvREFCNT_dec(PL_lex_stuff);
5285 PL_lex_stuff = NULL;
5288 if (len == 6 && strnEQ(SvPVX(sv), "unique", len)) {
5290 if (PL_in_my == KEY_our) {
5291 deprecate(":unique");
5294 Perl_croak(aTHX_ "The 'unique' attribute may only be applied to 'our' variables");
5297 /* NOTE: any CV attrs applied here need to be part of
5298 the CVf_BUILTIN_ATTRS define in cv.h! */
5299 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "lvalue", len)) {
5301 CvLVALUE_on(PL_compcv);
5303 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "locked", len)) {
5305 deprecate(":locked");
5307 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "method", len)) {
5309 CvMETHOD_on(PL_compcv);
5311 /* After we've set the flags, it could be argued that
5312 we don't need to do the attributes.pm-based setting
5313 process, and shouldn't bother appending recognized
5314 flags. To experiment with that, uncomment the
5315 following "else". (Note that's already been
5316 uncommented. That keeps the above-applied built-in
5317 attributes from being intercepted (and possibly
5318 rejected) by a package's attribute routines, but is
5319 justified by the performance win for the common case
5320 of applying only built-in attributes.) */
5322 attrs = op_append_elem(OP_LIST, attrs,
5323 newSVOP(OP_CONST, 0,
5327 if (*s == ':' && s[1] != ':')
5330 break; /* require real whitespace or :'s */
5331 /* XXX losing whitespace on sequential attributes here */
5335 = (PL_expect == XOPERATOR ? '=' : '{'); /*'}(' for vi */
5336 if (*s != ';' && *s != '}' && *s != tmp
5337 && (tmp != '=' || *s != ')')) {
5338 const char q = ((*s == '\'') ? '"' : '\'');
5339 /* If here for an expression, and parsed no attrs, back
5341 if (tmp == '=' && !attrs) {
5345 /* MUST advance bufptr here to avoid bogus "at end of line"
5346 context messages from yyerror().
5349 yyerror( (const char *)
5351 ? Perl_form(aTHX_ "Invalid separator character "
5352 "%c%c%c in attribute list", q, *s, q)
5353 : "Unterminated attribute list" ) );
5361 start_force(PL_curforce);
5362 NEXTVAL_NEXTTOKE.opval = attrs;
5363 CURMAD('_', PL_nextwhite);
5368 PL_thistoken = newSVpvn(SvPVX(PL_linestr) + stuffstart,
5369 (s - SvPVX(PL_linestr)) - stuffstart);
5377 if (PL_last_lop == PL_oldoldbufptr || PL_last_uni == PL_oldoldbufptr)
5378 PL_oldbufptr = PL_oldoldbufptr; /* allow print(STDOUT 123) */
5386 const char tmp = *s++;
5391 const char tmp = *s++;
5398 if (PL_lex_brackets && PL_lex_brackstack[PL_lex_brackets-1] == XFAKEEOF)
5401 if (PL_lex_brackets <= 0)
5402 yyerror("Unmatched right square bracket");
5405 if (PL_lex_state == LEX_INTERPNORMAL) {
5406 if (PL_lex_brackets == 0) {
5407 if (*s == '-' && s[1] == '>')
5408 PL_lex_state = LEX_INTERPENDMAYBE;
5409 else if (*s != '[' && *s != '{')
5410 PL_lex_state = LEX_INTERPEND;
5417 if (PL_lex_brackets > 100) {
5418 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
5420 switch (PL_expect) {
5422 if (PL_lex_formbrack) {
5426 if (PL_oldoldbufptr == PL_last_lop)
5427 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
5429 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
5430 OPERATOR(HASHBRACK);
5432 while (s < PL_bufend && SPACE_OR_TAB(*s))
5435 PL_tokenbuf[0] = '\0';
5436 if (d < PL_bufend && *d == '-') {
5437 PL_tokenbuf[0] = '-';
5439 while (d < PL_bufend && SPACE_OR_TAB(*d))
5442 if (d < PL_bufend && isIDFIRST_lazy_if(d,UTF)) {
5443 d = scan_word(d, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
5445 while (d < PL_bufend && SPACE_OR_TAB(*d))
5448 const char minus = (PL_tokenbuf[0] == '-');
5449 s = force_word(s + minus, WORD, FALSE, TRUE, FALSE);
5457 PL_lex_brackstack[PL_lex_brackets++] = XSTATE;
5462 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
5467 if (PL_oldoldbufptr == PL_last_lop)
5468 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
5470 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
5473 if (PL_expect == XREF && PL_lex_state == LEX_INTERPNORMAL) {
5475 /* This hack is to get the ${} in the message. */
5477 yyerror("syntax error");
5480 OPERATOR(HASHBRACK);
5482 /* This hack serves to disambiguate a pair of curlies
5483 * as being a block or an anon hash. Normally, expectation
5484 * determines that, but in cases where we're not in a
5485 * position to expect anything in particular (like inside
5486 * eval"") we have to resolve the ambiguity. This code
5487 * covers the case where the first term in the curlies is a
5488 * quoted string. Most other cases need to be explicitly
5489 * disambiguated by prepending a "+" before the opening
5490 * curly in order to force resolution as an anon hash.
5492 * XXX should probably propagate the outer expectation
5493 * into eval"" to rely less on this hack, but that could
5494 * potentially break current behavior of eval"".
5498 if (*s == '\'' || *s == '"' || *s == '`') {
5499 /* common case: get past first string, handling escapes */
5500 for (t++; t < PL_bufend && *t != *s;)
5501 if (*t++ == '\\' && (*t == '\\' || *t == *s))
5505 else if (*s == 'q') {
5508 || ((*t == 'q' || *t == 'x') && ++t < PL_bufend
5511 /* skip q//-like construct */
5513 char open, close, term;
5516 while (t < PL_bufend && isSPACE(*t))
5518 /* check for q => */
5519 if (t+1 < PL_bufend && t[0] == '=' && t[1] == '>') {
5520 OPERATOR(HASHBRACK);
5524 if (term && (tmps = strchr("([{< )]}> )]}>",term)))
5528 for (t++; t < PL_bufend; t++) {
5529 if (*t == '\\' && t+1 < PL_bufend && open != '\\')
5531 else if (*t == open)
5535 for (t++; t < PL_bufend; t++) {
5536 if (*t == '\\' && t+1 < PL_bufend)
5538 else if (*t == close && --brackets <= 0)
5540 else if (*t == open)
5547 /* skip plain q word */
5548 while (t < PL_bufend && isALNUM_lazy_if(t,UTF))
5551 else if (isALNUM_lazy_if(t,UTF)) {
5553 while (t < PL_bufend && isALNUM_lazy_if(t,UTF))
5556 while (t < PL_bufend && isSPACE(*t))
5558 /* if comma follows first term, call it an anon hash */
5559 /* XXX it could be a comma expression with loop modifiers */
5560 if (t < PL_bufend && ((*t == ',' && (*s == 'q' || !isLOWER(*s)))
5561 || (*t == '=' && t[1] == '>')))
5562 OPERATOR(HASHBRACK);
5563 if (PL_expect == XREF)
5566 PL_lex_brackstack[PL_lex_brackets-1] = XSTATE;
5572 pl_yylval.ival = CopLINE(PL_curcop);
5573 if (isSPACE(*s) || *s == '#')
5574 PL_copline = NOLINE; /* invalidate current command line number */
5577 if (PL_lex_brackets && PL_lex_brackstack[PL_lex_brackets-1] == XFAKEEOF)
5581 if (PL_lex_brackets <= 0)
5582 yyerror("Unmatched right curly bracket");
5584 PL_expect = (expectation)PL_lex_brackstack[--PL_lex_brackets];
5585 if (PL_lex_brackets < PL_lex_formbrack && PL_lex_state != LEX_INTERPNORMAL)
5586 PL_lex_formbrack = 0;
5587 if (PL_lex_state == LEX_INTERPNORMAL) {
5588 if (PL_lex_brackets == 0) {
5589 if (PL_expect & XFAKEBRACK) {
5590 PL_expect &= XENUMMASK;
5591 PL_lex_state = LEX_INTERPEND;
5596 PL_thiswhite = newSVpvs("");
5597 sv_catpvs(PL_thiswhite,"}");
5600 return yylex(); /* ignore fake brackets */
5602 if (*s == '-' && s[1] == '>')
5603 PL_lex_state = LEX_INTERPENDMAYBE;
5604 else if (*s != '[' && *s != '{')
5605 PL_lex_state = LEX_INTERPEND;
5608 if (PL_expect & XFAKEBRACK) {
5609 PL_expect &= XENUMMASK;
5611 return yylex(); /* ignore fake brackets */
5613 start_force(PL_curforce);
5615 curmad('X', newSVpvn(s-1,1));
5616 CURMAD('_', PL_thiswhite);
5621 PL_thistoken = newSVpvs("");
5629 if (PL_expect == XOPERATOR) {
5630 if (PL_bufptr == PL_linestart && ckWARN(WARN_SEMICOLON)
5631 && isIDFIRST_lazy_if(s,UTF))
5633 CopLINE_dec(PL_curcop);
5634 Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
5635 CopLINE_inc(PL_curcop);
5640 s = scan_ident(s - 1, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
5642 PL_expect = XOPERATOR;
5643 force_ident(PL_tokenbuf, '&');
5647 pl_yylval.ival = (OPpENTERSUB_AMPER<<8);
5659 const char tmp = *s++;
5666 if (tmp && isSPACE(*s) && ckWARN(WARN_SYNTAX)
5667 && strchr("+-*/%.^&|<",tmp))
5668 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5669 "Reversed %c= operator",(int)tmp);
5671 if (PL_expect == XSTATE && isALPHA(tmp) &&
5672 (s == PL_linestart+1 || s[-2] == '\n') )
5674 if (PL_in_eval && !PL_rsfp) {
5679 if (strnEQ(s,"=cut",4)) {
5695 PL_thiswhite = newSVpvs("");
5696 sv_catpvn(PL_thiswhite, PL_linestart,
5697 PL_bufend - PL_linestart);
5701 PL_parser->in_pod = 1;
5705 if (PL_lex_brackets < PL_lex_formbrack) {
5707 #ifdef PERL_STRICT_CR
5708 while (SPACE_OR_TAB(*t))
5710 while (SPACE_OR_TAB(*t) || *t == '\r')
5713 if (*t == '\n' || *t == '#') {
5724 const char tmp = *s++;
5726 /* was this !=~ where !~ was meant?
5727 * warn on m:!=~\s+([/?]|[msy]\W|tr\W): */
5729 if (*s == '~' && ckWARN(WARN_SYNTAX)) {
5730 const char *t = s+1;
5732 while (t < PL_bufend && isSPACE(*t))
5735 if (*t == '/' || *t == '?' ||
5736 ((*t == 'm' || *t == 's' || *t == 'y')
5737 && !isALNUM(t[1])) ||
5738 (*t == 't' && t[1] == 'r' && !isALNUM(t[2])))
5739 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5740 "!=~ should be !~");
5750 if (PL_expect != XOPERATOR) {
5751 if (s[1] != '<' && !strchr(s,'>'))
5754 s = scan_heredoc(s);
5756 s = scan_inputsymbol(s);
5757 TERM(sublex_start());
5763 SHop(OP_LEFT_SHIFT);
5777 const char tmp = *s++;
5779 SHop(OP_RIGHT_SHIFT);
5780 else if (tmp == '=')
5789 if (PL_expect == XOPERATOR) {
5790 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
5791 return deprecate_commaless_var_list();
5795 if (s[1] == '#' && (isIDFIRST_lazy_if(s+2,UTF) || strchr("{$:+-@", s[2]))) {
5796 PL_tokenbuf[0] = '@';
5797 s = scan_ident(s + 1, PL_bufend, PL_tokenbuf + 1,
5798 sizeof PL_tokenbuf - 1, FALSE);
5799 if (PL_expect == XOPERATOR)
5800 no_op("Array length", s);
5801 if (!PL_tokenbuf[1])
5803 PL_expect = XOPERATOR;
5804 PL_pending_ident = '#';
5808 PL_tokenbuf[0] = '$';
5809 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1,
5810 sizeof PL_tokenbuf - 1, FALSE);
5811 if (PL_expect == XOPERATOR)
5813 if (!PL_tokenbuf[1]) {
5815 yyerror("Final $ should be \\$ or $name");
5819 /* This kludge not intended to be bulletproof. */
5820 if (PL_tokenbuf[1] == '[' && !PL_tokenbuf[2]) {
5821 pl_yylval.opval = newSVOP(OP_CONST, 0,
5822 newSViv(CopARYBASE_get(&PL_compiling)));
5823 pl_yylval.opval->op_private = OPpCONST_ARYBASE;
5829 const char tmp = *s;
5830 if (PL_lex_state == LEX_NORMAL || PL_lex_brackets)
5833 if ((PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
5834 && intuit_more(s)) {
5836 PL_tokenbuf[0] = '@';
5837 if (ckWARN(WARN_SYNTAX)) {
5840 while (isSPACE(*t) || isALNUM_lazy_if(t,UTF) || *t == '$')
5843 PL_bufptr = PEEKSPACE(PL_bufptr); /* XXX can realloc */
5844 while (t < PL_bufend && *t != ']')
5846 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5847 "Multidimensional syntax %.*s not supported",
5848 (int)((t - PL_bufptr) + 1), PL_bufptr);
5852 else if (*s == '{') {
5854 PL_tokenbuf[0] = '%';
5855 if (strEQ(PL_tokenbuf+1, "SIG") && ckWARN(WARN_SYNTAX)
5856 && (t = strchr(s, '}')) && (t = strchr(t, '=')))
5858 char tmpbuf[sizeof PL_tokenbuf];
5861 } while (isSPACE(*t));
5862 if (isIDFIRST_lazy_if(t,UTF)) {
5864 t = scan_word(t, tmpbuf, sizeof tmpbuf, TRUE,
5868 if (*t == ';' && get_cvn_flags(tmpbuf, len, 0))
5869 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5870 "You need to quote \"%s\"",
5877 PL_expect = XOPERATOR;
5878 if (PL_lex_state == LEX_NORMAL && isSPACE((char)tmp)) {
5879 const bool islop = (PL_last_lop == PL_oldoldbufptr);
5880 if (!islop || PL_last_lop_op == OP_GREPSTART)
5881 PL_expect = XOPERATOR;
5882 else if (strchr("$@\"'`q", *s))
5883 PL_expect = XTERM; /* e.g. print $fh "foo" */
5884 else if (strchr("&*<%", *s) && isIDFIRST_lazy_if(s+1,UTF))
5885 PL_expect = XTERM; /* e.g. print $fh &sub */
5886 else if (isIDFIRST_lazy_if(s,UTF)) {
5887 char tmpbuf[sizeof PL_tokenbuf];
5889 scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
5890 if ((t2 = keyword(tmpbuf, len, 0))) {
5891 /* binary operators exclude handle interpretations */
5903 PL_expect = XTERM; /* e.g. print $fh length() */
5908 PL_expect = XTERM; /* e.g. print $fh subr() */
5911 else if (isDIGIT(*s))
5912 PL_expect = XTERM; /* e.g. print $fh 3 */
5913 else if (*s == '.' && isDIGIT(s[1]))
5914 PL_expect = XTERM; /* e.g. print $fh .3 */
5915 else if ((*s == '?' || *s == '-' || *s == '+')
5916 && !isSPACE(s[1]) && s[1] != '=')
5917 PL_expect = XTERM; /* e.g. print $fh -1 */
5918 else if (*s == '/' && !isSPACE(s[1]) && s[1] != '='
5920 PL_expect = XTERM; /* e.g. print $fh /.../
5921 XXX except DORDOR operator
5923 else if (*s == '<' && s[1] == '<' && !isSPACE(s[2])
5925 PL_expect = XTERM; /* print $fh <<"EOF" */
5928 PL_pending_ident = '$';
5932 if (PL_expect == XOPERATOR)
5934 PL_tokenbuf[0] = '@';
5935 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
5936 if (!PL_tokenbuf[1]) {
5939 if (PL_lex_state == LEX_NORMAL)
5941 if ((PL_expect != XREF || PL_oldoldbufptr == PL_last_lop) && intuit_more(s)) {
5943 PL_tokenbuf[0] = '%';
5945 /* Warn about @ where they meant $. */
5946 if (*s == '[' || *s == '{') {
5947 if (ckWARN(WARN_SYNTAX)) {
5948 const char *t = s + 1;
5949 while (*t && (isALNUM_lazy_if(t,UTF) || strchr(" \t$#+-'\"", *t)))
5951 if (*t == '}' || *t == ']') {
5953 PL_bufptr = PEEKSPACE(PL_bufptr); /* XXX can realloc */
5954 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5955 "Scalar value %.*s better written as $%.*s",
5956 (int)(t-PL_bufptr), PL_bufptr,
5957 (int)(t-PL_bufptr-1), PL_bufptr+1);
5962 PL_pending_ident = '@';
5965 case '/': /* may be division, defined-or, or pattern */
5966 if (PL_expect == XTERMORDORDOR && s[1] == '/') {
5970 case '?': /* may either be conditional or pattern */
5971 if (PL_expect == XOPERATOR) {
5979 /* A // operator. */
5989 /* Disable warning on "study /blah/" */
5990 if (PL_oldoldbufptr == PL_last_uni
5991 && (*PL_last_uni != 's' || s - PL_last_uni < 5
5992 || memNE(PL_last_uni, "study", 5)
5993 || isALNUM_lazy_if(PL_last_uni+5,UTF)
5996 s = scan_pat(s,OP_MATCH);
5997 TERM(sublex_start());
6001 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack
6002 #ifdef PERL_STRICT_CR
6005 && (s[1] == '\n' || (s[1] == '\r' && s[2] == '\n'))
6007 && (s == PL_linestart || s[-1] == '\n') )
6009 PL_lex_formbrack = 0;
6013 if (PL_expect == XSTATE && s[1] == '.' && s[2] == '.') {
6017 if (PL_expect == XOPERATOR || !isDIGIT(s[1])) {
6023 pl_yylval.ival = OPf_SPECIAL;
6032 case '0': case '1': case '2': case '3': case '4':
6033 case '5': case '6': case '7': case '8': case '9':
6034 s = scan_num(s, &pl_yylval);
6035 DEBUG_T( { printbuf("### Saw number in %s\n", s); } );
6036 if (PL_expect == XOPERATOR)
6041 s = scan_str(s,!!PL_madskills,FALSE);
6042 DEBUG_T( { printbuf("### Saw string before %s\n", s); } );
6043 if (PL_expect == XOPERATOR) {
6044 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
6045 return deprecate_commaless_var_list();
6052 pl_yylval.ival = OP_CONST;
6053 TERM(sublex_start());
6056 s = scan_str(s,!!PL_madskills,FALSE);
6057 DEBUG_T( { printbuf("### Saw string before %s\n", s); } );
6058 if (PL_expect == XOPERATOR) {
6059 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
6060 return deprecate_commaless_var_list();
6067 pl_yylval.ival = OP_CONST;
6068 /* FIXME. I think that this can be const if char *d is replaced by
6069 more localised variables. */
6070 for (d = SvPV(PL_lex_stuff, len); len; len--, d++) {
6071 if (*d == '$' || *d == '@' || *d == '\\' || !UTF8_IS_INVARIANT((U8)*d)) {
6072 pl_yylval.ival = OP_STRINGIFY;
6076 TERM(sublex_start());
6079 s = scan_str(s,!!PL_madskills,FALSE);
6080 DEBUG_T( { printbuf("### Saw backtick string before %s\n", s); } );
6081 if (PL_expect == XOPERATOR)
6082 no_op("Backticks",s);
6085 readpipe_override();
6086 TERM(sublex_start());
6090 if (PL_lex_inwhat && isDIGIT(*s))
6091 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),"Can't use \\%c to mean $%c in expression",
6093 if (PL_expect == XOPERATOR)
6094 no_op("Backslash",s);
6098 if (isDIGIT(s[1]) && PL_expect != XOPERATOR) {
6099 char *start = s + 2;
6100 while (isDIGIT(*start) || *start == '_')
6102 if (*start == '.' && isDIGIT(start[1])) {
6103 s = scan_num(s, &pl_yylval);
6106 /* avoid v123abc() or $h{v1}, allow C<print v10;> */
6107 else if (!isALPHA(*start) && (PL_expect == XTERM
6108 || PL_expect == XREF || PL_expect == XSTATE
6109 || PL_expect == XTERMORDORDOR)) {
6110 GV *const gv = gv_fetchpvn_flags(s, start - s, 0, SVt_PVCV);
6112 s = scan_num(s, &pl_yylval);
6119 if (isDIGIT(s[1]) && PL_expect == XOPERATOR) {
6162 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
6164 /* Some keywords can be followed by any delimiter, including ':' */
6165 anydelim = word_takes_any_delimeter(PL_tokenbuf, len);
6167 /* x::* is just a word, unless x is "CORE" */
6168 if (!anydelim && *s == ':' && s[1] == ':' && strNE(PL_tokenbuf, "CORE"))
6172 while (d < PL_bufend && isSPACE(*d))
6173 d++; /* no comments skipped here, or s### is misparsed */
6175 /* Is this a word before a => operator? */
6176 if (*d == '=' && d[1] == '>') {
6179 = (OP*)newSVOP(OP_CONST, 0,
6180 S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
6181 pl_yylval.opval->op_private = OPpCONST_BARE;
6185 /* Check for plugged-in keyword */
6189 char *saved_bufptr = PL_bufptr;
6191 result = PL_keyword_plugin(aTHX_ PL_tokenbuf, len, &o);
6193 if (result == KEYWORD_PLUGIN_DECLINE) {
6194 /* not a plugged-in keyword */
6195 PL_bufptr = saved_bufptr;
6196 } else if (result == KEYWORD_PLUGIN_STMT) {
6197 pl_yylval.opval = o;
6200 return REPORT(PLUGSTMT);
6201 } else if (result == KEYWORD_PLUGIN_EXPR) {
6202 pl_yylval.opval = o;
6204 PL_expect = XOPERATOR;
6205 return REPORT(PLUGEXPR);
6207 Perl_croak(aTHX_ "Bad plugin affecting keyword '%s'",
6212 /* Check for built-in keyword */
6213 tmp = keyword(PL_tokenbuf, len, 0);
6215 /* Is this a label? */
6216 if (!anydelim && PL_expect == XSTATE
6217 && d < PL_bufend && *d == ':' && *(d + 1) != ':') {
6219 pl_yylval.pval = CopLABEL_alloc(PL_tokenbuf);
6224 if (tmp < 0) { /* second-class keyword? */
6225 GV *ogv = NULL; /* override (winner) */
6226 GV *hgv = NULL; /* hidden (loser) */
6227 if (PL_expect != XOPERATOR && (*s != ':' || s[1] != ':')) {
6229 if ((gv = gv_fetchpvn_flags(PL_tokenbuf, len, 0, SVt_PVCV)) &&
6232 if (GvIMPORTED_CV(gv))
6234 else if (! CvMETHOD(cv))
6238 (gvp = (GV**)hv_fetch(PL_globalstash,PL_tokenbuf,len,FALSE)) &&
6239 (gv = *gvp) && isGV_with_GP(gv) &&
6240 GvCVu(gv) && GvIMPORTED_CV(gv))
6247 tmp = 0; /* overridden by import or by GLOBAL */
6250 && -tmp==KEY_lock /* XXX generalizable kludge */
6253 tmp = 0; /* any sub overrides "weak" keyword */
6255 else { /* no override */
6257 if (tmp == KEY_dump) {
6258 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
6259 "dump() better written as CORE::dump()");
6263 if (hgv && tmp != KEY_x && tmp != KEY_CORE) /* never ambiguous */
6264 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
6265 "Ambiguous call resolved as CORE::%s(), "
6266 "qualify as such or use &",
6274 default: /* not a keyword */
6275 /* Trade off - by using this evil construction we can pull the
6276 variable gv into the block labelled keylookup. If not, then
6277 we have to give it function scope so that the goto from the
6278 earlier ':' case doesn't bypass the initialisation. */
6280 just_a_word_zero_gv:
6288 const char lastchar = (PL_bufptr == PL_oldoldbufptr ? 0 : PL_bufptr[-1]);
6292 SV *nextPL_nextwhite = 0;
6296 /* Get the rest if it looks like a package qualifier */
6298 if (*s == '\'' || (*s == ':' && s[1] == ':')) {
6300 s = scan_word(s, PL_tokenbuf + len, sizeof PL_tokenbuf - len,
6303 Perl_croak(aTHX_ "Bad name after %s%s", PL_tokenbuf,
6304 *s == '\'' ? "'" : "::");
6309 if (PL_expect == XOPERATOR) {
6310 if (PL_bufptr == PL_linestart) {
6311 CopLINE_dec(PL_curcop);
6312 Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
6313 CopLINE_inc(PL_curcop);
6316 no_op("Bareword",s);
6319 /* Look for a subroutine with this name in current package,
6320 unless name is "Foo::", in which case Foo is a bearword
6321 (and a package name). */
6323 if (len > 2 && !PL_madskills &&
6324 PL_tokenbuf[len - 2] == ':' && PL_tokenbuf[len - 1] == ':')
6326 if (ckWARN(WARN_BAREWORD)
6327 && ! gv_fetchpvn_flags(PL_tokenbuf, len, 0, SVt_PVHV))
6328 Perl_warner(aTHX_ packWARN(WARN_BAREWORD),
6329 "Bareword \"%s\" refers to nonexistent package",
6332 PL_tokenbuf[len] = '\0';
6338 /* Mustn't actually add anything to a symbol table.
6339 But also don't want to "initialise" any placeholder
6340 constants that might already be there into full
6341 blown PVGVs with attached PVCV. */
6342 gv = gv_fetchpvn_flags(PL_tokenbuf, len,
6343 GV_NOADD_NOINIT, SVt_PVCV);
6348 /* if we saw a global override before, get the right name */
6350 sv = S_newSV_maybe_utf8(aTHX_ PL_tokenbuf,
6351 len ? len : strlen(PL_tokenbuf));
6353 SV * const tmp_sv = sv;
6354 sv = newSVpvs("CORE::GLOBAL::");
6355 sv_catsv(sv, tmp_sv);
6356 SvREFCNT_dec(tmp_sv);
6360 if (PL_madskills && !PL_thistoken) {
6361 char *start = SvPVX(PL_linestr) + PL_realtokenstart;
6362 PL_thistoken = newSVpvn(start,s - start);
6363 PL_realtokenstart = s - SvPVX(PL_linestr);
6367 /* Presume this is going to be a bareword of some sort. */
6369 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
6370 pl_yylval.opval->op_private = OPpCONST_BARE;
6372 /* And if "Foo::", then that's what it certainly is. */
6377 OP *const_op = newSVOP(OP_CONST, 0, SvREFCNT_inc(sv));
6378 const_op->op_private = OPpCONST_BARE;
6379 rv2cv_op = newCVREF(0, const_op);
6381 cv = rv2cv_op_cv(rv2cv_op, 0);
6383 /* See if it's the indirect object for a list operator. */
6385 if (PL_oldoldbufptr &&
6386 PL_oldoldbufptr < PL_bufptr &&
6387 (PL_oldoldbufptr == PL_last_lop
6388 || PL_oldoldbufptr == PL_last_uni) &&
6389 /* NO SKIPSPACE BEFORE HERE! */
6390 (PL_expect == XREF ||
6391 ((PL_opargs[PL_last_lop_op] >> OASHIFT)& 7) == OA_FILEREF))
6393 bool immediate_paren = *s == '(';
6395 /* (Now we can afford to cross potential line boundary.) */
6396 s = SKIPSPACE2(s,nextPL_nextwhite);
6398 PL_nextwhite = nextPL_nextwhite; /* assume no & deception */
6401 /* Two barewords in a row may indicate method call. */
6403 if ((isIDFIRST_lazy_if(s,UTF) || *s == '$') &&
6404 (tmp = intuit_method(s, gv, cv))) {
6409 /* If not a declared subroutine, it's an indirect object. */
6410 /* (But it's an indir obj regardless for sort.) */
6411 /* Also, if "_" follows a filetest operator, it's a bareword */
6414 ( !immediate_paren && (PL_last_lop_op == OP_SORT ||
6416 (PL_last_lop_op != OP_MAPSTART &&
6417 PL_last_lop_op != OP_GREPSTART))))
6418 || (PL_tokenbuf[0] == '_' && PL_tokenbuf[1] == '\0'
6419 && ((PL_opargs[PL_last_lop_op] & OA_CLASS_MASK) == OA_FILESTATOP))
6422 PL_expect = (PL_last_lop == PL_oldoldbufptr) ? XTERM : XOPERATOR;
6427 PL_expect = XOPERATOR;
6430 s = SKIPSPACE2(s,nextPL_nextwhite);
6431 PL_nextwhite = nextPL_nextwhite;
6436 /* Is this a word before a => operator? */
6437 if (*s == '=' && s[1] == '>' && !pkgname) {
6440 sv_setpv(((SVOP*)pl_yylval.opval)->op_sv, PL_tokenbuf);
6441 if (UTF && !IN_BYTES && is_utf8_string((U8*)PL_tokenbuf, len))
6442 SvUTF8_on(((SVOP*)pl_yylval.opval)->op_sv);
6446 /* If followed by a paren, it's certainly a subroutine. */
6451 while (SPACE_OR_TAB(*d))
6453 if (*d == ')' && (sv = cv_const_sv(cv))) {
6460 PL_nextwhite = PL_thiswhite;
6463 start_force(PL_curforce);
6465 NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
6466 PL_expect = XOPERATOR;
6469 PL_nextwhite = nextPL_nextwhite;
6470 curmad('X', PL_thistoken);
6471 PL_thistoken = newSVpvs("");
6480 /* If followed by var or block, call it a method (unless sub) */
6482 if ((*s == '$' || *s == '{') && !cv) {
6484 PL_last_lop = PL_oldbufptr;
6485 PL_last_lop_op = OP_METHOD;
6489 /* If followed by a bareword, see if it looks like indir obj. */
6492 && (isIDFIRST_lazy_if(s,UTF) || *s == '$')
6493 && (tmp = intuit_method(s, gv, cv))) {
6498 /* Not a method, so call it a subroutine (if defined) */
6501 if (lastchar == '-')
6502 Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
6503 "Ambiguous use of -%s resolved as -&%s()",
6504 PL_tokenbuf, PL_tokenbuf);
6505 /* Check for a constant sub */
6506 if ((sv = cv_const_sv(cv))) {
6509 SvREFCNT_dec(((SVOP*)pl_yylval.opval)->op_sv);
6510 ((SVOP*)pl_yylval.opval)->op_sv = SvREFCNT_inc_simple(sv);
6511 pl_yylval.opval->op_private = 0;
6515 op_free(pl_yylval.opval);
6516 pl_yylval.opval = rv2cv_op;
6517 pl_yylval.opval->op_private |= OPpENTERSUB_NOPAREN;
6518 PL_last_lop = PL_oldbufptr;
6519 PL_last_lop_op = OP_ENTERSUB;
6520 /* Is there a prototype? */
6528 const char *proto = SvPV_const(MUTABLE_SV(cv), protolen);
6531 while (*proto == ';')
6536 *proto == '$' || *proto == '_'
6537 || *proto == '*' || *proto == '+'
6542 *proto == '\\' && proto[1] && proto[2] == '\0'
6546 if (*proto == '\\' && proto[1] == '[') {
6547 const char *p = proto + 2;
6548 while(*p && *p != ']')
6550 if(*p == ']' && !p[1]) OPERATOR(UNIOPSUB);
6552 if (*proto == '&' && *s == '{') {
6554 sv_setpvs(PL_subname, "__ANON__");
6556 sv_setpvs(PL_subname, "__ANON__::__ANON__");
6563 PL_nextwhite = PL_thiswhite;
6566 start_force(PL_curforce);
6567 NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
6570 PL_nextwhite = nextPL_nextwhite;
6571 curmad('X', PL_thistoken);
6572 PL_thistoken = newSVpvs("");
6579 /* Guess harder when madskills require "best effort". */
6580 if (PL_madskills && (!gv || !GvCVu(gv))) {
6581 int probable_sub = 0;
6582 if (strchr("\"'`$@%0123456789!*+{[<", *s))
6584 else if (isALPHA(*s)) {
6588 d = scan_word(d, tmpbuf, sizeof tmpbuf, TRUE, &tmplen);
6589 if (!keyword(tmpbuf, tmplen, 0))
6592 while (d < PL_bufend && isSPACE(*d))
6594 if (*d == '=' && d[1] == '>')
6599 gv = gv_fetchpv(PL_tokenbuf, GV_ADD, SVt_PVCV);
6600 op_free(pl_yylval.opval);
6601 pl_yylval.opval = rv2cv_op;
6602 pl_yylval.opval->op_private |= OPpENTERSUB_NOPAREN;
6603 PL_last_lop = PL_oldbufptr;
6604 PL_last_lop_op = OP_ENTERSUB;
6605 PL_nextwhite = PL_thiswhite;
6607 start_force(PL_curforce);
6608 NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
6610 PL_nextwhite = nextPL_nextwhite;
6611 curmad('X', PL_thistoken);
6612 PL_thistoken = newSVpvs("");
6617 NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
6624 /* Call it a bare word */
6626 if (PL_hints & HINT_STRICT_SUBS)
6627 pl_yylval.opval->op_private |= OPpCONST_STRICT;
6630 /* after "print" and similar functions (corresponding to
6631 * "F? L" in opcode.pl), whatever wasn't already parsed as
6632 * a filehandle should be subject to "strict subs".
6633 * Likewise for the optional indirect-object argument to system
6634 * or exec, which can't be a bareword */
6635 if ((PL_last_lop_op == OP_PRINT
6636 || PL_last_lop_op == OP_PRTF
6637 || PL_last_lop_op == OP_SAY
6638 || PL_last_lop_op == OP_SYSTEM
6639 || PL_last_lop_op == OP_EXEC)
6640 && (PL_hints & HINT_STRICT_SUBS))
6641 pl_yylval.opval->op_private |= OPpCONST_STRICT;
6642 if (lastchar != '-') {
6643 if (ckWARN(WARN_RESERVED)) {
6647 if (!*d && !gv_stashpv(PL_tokenbuf, 0))
6648 Perl_warner(aTHX_ packWARN(WARN_RESERVED), PL_warn_reserved,
6656 if ((lastchar == '*' || lastchar == '%' || lastchar == '&')) {
6657 Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
6658 "Operator or semicolon missing before %c%s",
6659 lastchar, PL_tokenbuf);
6660 Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
6661 "Ambiguous use of %c resolved as operator %c",
6662 lastchar, lastchar);
6668 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0,
6669 newSVpv(CopFILE(PL_curcop),0));
6673 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0,
6674 Perl_newSVpvf(aTHX_ "%"IVdf, (IV)CopLINE(PL_curcop)));
6677 case KEY___PACKAGE__:
6678 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0,
6680 ? newSVhek(HvNAME_HEK(PL_curstash))
6687 if (PL_rsfp && (!PL_in_eval || PL_tokenbuf[2] == 'D')) {
6688 const char *pname = "main";
6689 if (PL_tokenbuf[2] == 'D')
6690 pname = HvNAME_get(PL_curstash ? PL_curstash : PL_defstash);
6691 gv = gv_fetchpv(Perl_form(aTHX_ "%s::DATA", pname), GV_ADD,
6695 GvIOp(gv) = newIO();
6696 IoIFP(GvIOp(gv)) = PL_rsfp;
6697 #if defined(HAS_FCNTL) && defined(F_SETFD)
6699 const int fd = PerlIO_fileno(PL_rsfp);
6700 fcntl(fd,F_SETFD,fd >= 3);
6703 /* Mark this internal pseudo-handle as clean */
6704 IoFLAGS(GvIOp(gv)) |= IOf_UNTAINT;
6705 if ((PerlIO*)PL_rsfp == PerlIO_stdin())
6706 IoTYPE(GvIOp(gv)) = IoTYPE_STD;
6708 IoTYPE(GvIOp(gv)) = IoTYPE_RDONLY;
6709 #if defined(WIN32) && !defined(PERL_TEXTMODE_SCRIPTS)
6710 /* if the script was opened in binmode, we need to revert
6711 * it to text mode for compatibility; but only iff it has CRs
6712 * XXX this is a questionable hack at best. */
6713 if (PL_bufend-PL_bufptr > 2
6714 && PL_bufend[-1] == '\n' && PL_bufend[-2] == '\r')
6717 if (IoTYPE(GvIOp(gv)) == IoTYPE_RDONLY) {
6718 loc = PerlIO_tell(PL_rsfp);
6719 (void)PerlIO_seek(PL_rsfp, 0L, 0);
6722 if (PerlLIO_setmode(PL_rsfp, O_TEXT) != -1) {
6724 if (PerlLIO_setmode(PerlIO_fileno(PL_rsfp), O_TEXT) != -1) {
6725 #endif /* NETWARE */
6726 #ifdef PERLIO_IS_STDIO /* really? */
6727 # if defined(__BORLANDC__)
6728 /* XXX see note in do_binmode() */
6729 ((FILE*)PL_rsfp)->flags &= ~_F_BIN;
6733 PerlIO_seek(PL_rsfp, loc, 0);
6737 #ifdef PERLIO_LAYERS
6740 PerlIO_apply_layers(aTHX_ PL_rsfp, NULL, ":utf8");
6741 else if (PL_encoding) {
6748 XPUSHs(PL_encoding);
6750 call_method("name", G_SCALAR);
6754 PerlIO_apply_layers(aTHX_ PL_rsfp, NULL,
6755 Perl_form(aTHX_ ":encoding(%"SVf")",
6764 if (PL_realtokenstart >= 0) {
6765 char *tstart = SvPVX(PL_linestr) + PL_realtokenstart;
6767 PL_endwhite = newSVpvs("");
6768 sv_catsv(PL_endwhite, PL_thiswhite);
6770 sv_catpvn(PL_endwhite, tstart, PL_bufend - tstart);
6771 PL_realtokenstart = -1;
6773 while ((s = filter_gets(PL_endwhite, SvCUR(PL_endwhite)))
6789 if (PL_expect == XSTATE) {
6796 if (*s == ':' && s[1] == ':') {
6799 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
6800 if (!(tmp = keyword(PL_tokenbuf, len, 0)))
6801 Perl_croak(aTHX_ "CORE::%s is not a keyword", PL_tokenbuf);
6804 else if (tmp == KEY_require || tmp == KEY_do)
6805 /* that's a way to remember we saw "CORE::" */
6818 LOP(OP_ACCEPT,XTERM);
6824 LOP(OP_ATAN2,XTERM);
6830 LOP(OP_BINMODE,XTERM);
6833 LOP(OP_BLESS,XTERM);
6842 /* When 'use switch' is in effect, continue has a dual
6843 life as a control operator. */
6845 if (!FEATURE_IS_ENABLED("switch"))
6848 /* We have to disambiguate the two senses of
6849 "continue". If the next token is a '{' then
6850 treat it as the start of a continue block;
6851 otherwise treat it as a control operator.
6863 (void)gv_fetchpvs("ENV", GV_ADD|GV_NOTQUAL, SVt_PVHV);
6880 if (!PL_cryptseen) {
6881 PL_cryptseen = TRUE;
6885 LOP(OP_CRYPT,XTERM);
6888 LOP(OP_CHMOD,XTERM);
6891 LOP(OP_CHOWN,XTERM);
6894 LOP(OP_CONNECT,XTERM);
6913 s = force_word(s,WORD,TRUE,TRUE,FALSE);
6914 if (orig_keyword == KEY_do) {
6923 PL_hints |= HINT_BLOCK_SCOPE;
6933 Perl_populate_isa(aTHX_ STR_WITH_LEN("AnyDBM_File::ISA"),
6934 STR_WITH_LEN("NDBM_File::"),
6935 STR_WITH_LEN("DB_File::"),
6936 STR_WITH_LEN("GDBM_File::"),
6937 STR_WITH_LEN("SDBM_File::"),
6938 STR_WITH_LEN("ODBM_File::"),
6940 LOP(OP_DBMOPEN,XTERM);
6946 s = force_word(s,WORD,TRUE,FALSE,FALSE);
6953 pl_yylval.ival = CopLINE(PL_curcop);
6969 if (*s == '{') { /* block eval */
6970 PL_expect = XTERMBLOCK;
6971 UNIBRACK(OP_ENTERTRY);
6973 else { /* string eval */
6975 UNIBRACK(OP_ENTEREVAL);
6990 case KEY_endhostent:
6996 case KEY_endservent:
6999 case KEY_endprotoent:
7010 pl_yylval.ival = CopLINE(PL_curcop);
7012 if (PL_expect == XSTATE && isIDFIRST_lazy_if(s,UTF)) {
7015 int soff = s - SvPVX(PL_linestr); /* for skipspace realloc */
7018 if ((PL_bufend - p) >= 3 &&
7019 strnEQ(p, "my", 2) && isSPACE(*(p + 2)))
7021 else if ((PL_bufend - p) >= 4 &&
7022 strnEQ(p, "our", 3) && isSPACE(*(p + 3)))
7025 if (isIDFIRST_lazy_if(p,UTF)) {
7026 p = scan_ident(p, PL_bufend,
7027 PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
7031 Perl_croak(aTHX_ "Missing $ on loop variable");
7033 s = SvPVX(PL_linestr) + soff;
7039 LOP(OP_FORMLINE,XTERM);
7045 LOP(OP_FCNTL,XTERM);
7051 LOP(OP_FLOCK,XTERM);
7060 LOP(OP_GREPSTART, XREF);
7063 s = force_word(s,WORD,TRUE,FALSE,FALSE);
7078 case KEY_getpriority:
7079 LOP(OP_GETPRIORITY,XTERM);
7081 case KEY_getprotobyname:
7084 case KEY_getprotobynumber:
7085 LOP(OP_GPBYNUMBER,XTERM);
7087 case KEY_getprotoent:
7099 case KEY_getpeername:
7100 UNI(OP_GETPEERNAME);
7102 case KEY_gethostbyname:
7105 case KEY_gethostbyaddr:
7106 LOP(OP_GHBYADDR,XTERM);
7108 case KEY_gethostent:
7111 case KEY_getnetbyname:
7114 case KEY_getnetbyaddr:
7115 LOP(OP_GNBYADDR,XTERM);
7120 case KEY_getservbyname:
7121 LOP(OP_GSBYNAME,XTERM);
7123 case KEY_getservbyport:
7124 LOP(OP_GSBYPORT,XTERM);
7126 case KEY_getservent:
7129 case KEY_getsockname:
7130 UNI(OP_GETSOCKNAME);
7132 case KEY_getsockopt:
7133 LOP(OP_GSOCKOPT,XTERM);
7148 pl_yylval.ival = CopLINE(PL_curcop);
7158 pl_yylval.ival = CopLINE(PL_curcop);
7162 LOP(OP_INDEX,XTERM);
7168 LOP(OP_IOCTL,XTERM);
7180 s = force_word(s,WORD,TRUE,FALSE,FALSE);
7212 LOP(OP_LISTEN,XTERM);
7221 s = scan_pat(s,OP_MATCH);
7222 TERM(sublex_start());
7225 LOP(OP_MAPSTART, XREF);
7228 LOP(OP_MKDIR,XTERM);
7231 LOP(OP_MSGCTL,XTERM);
7234 LOP(OP_MSGGET,XTERM);
7237 LOP(OP_MSGRCV,XTERM);
7240 LOP(OP_MSGSND,XTERM);
7245 PL_in_my = (U16)tmp;
7247 if (isIDFIRST_lazy_if(s,UTF)) {
7251 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
7252 if (len == 3 && strnEQ(PL_tokenbuf, "sub", 3))
7254 PL_in_my_stash = find_in_my_stash(PL_tokenbuf, len);
7255 if (!PL_in_my_stash) {
7258 my_snprintf(tmpbuf, sizeof(tmpbuf), "No such class %.1000s", PL_tokenbuf);
7262 if (PL_madskills) { /* just add type to declarator token */
7263 sv_catsv(PL_thistoken, PL_nextwhite);
7265 sv_catpvn(PL_thistoken, start, s - start);
7273 s = force_word(s,WORD,TRUE,FALSE,FALSE);
7280 s = tokenize_use(0, s);
7284 if (*s == '(' || (s = SKIPSPACE1(s), *s == '('))
7291 if (isIDFIRST_lazy_if(s,UTF)) {
7293 for (d = s; isALNUM_lazy_if(d,UTF);)
7295 for (t=d; isSPACE(*t);)
7297 if ( *t && strchr("|&*+-=!?:.", *t) && ckWARN_d(WARN_PRECEDENCE)
7299 && !(t[0] == '=' && t[1] == '>')
7301 int parms_len = (int)(d-s);
7302 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
7303 "Precedence problem: open %.*s should be open(%.*s)",
7304 parms_len, s, parms_len, s);
7310 pl_yylval.ival = OP_OR;
7320 LOP(OP_OPEN_DIR,XTERM);
7323 checkcomma(s,PL_tokenbuf,"filehandle");
7327 checkcomma(s,PL_tokenbuf,"filehandle");
7346 s = force_word(s,WORD,FALSE,TRUE,FALSE);
7348 s = force_strict_version(s);
7349 PL_lex_expect = XBLOCK;
7353 LOP(OP_PIPE_OP,XTERM);
7356 s = scan_str(s,!!PL_madskills,FALSE);
7359 pl_yylval.ival = OP_CONST;
7360 TERM(sublex_start());
7367 s = scan_str(s,!!PL_madskills,FALSE);
7370 PL_expect = XOPERATOR;
7371 if (SvCUR(PL_lex_stuff)) {
7373 d = SvPV_force(PL_lex_stuff, len);
7375 for (; isSPACE(*d) && len; --len, ++d)
7380 if (!warned && ckWARN(WARN_QW)) {
7381 for (; !isSPACE(*d) && len; --len, ++d) {
7383 Perl_warner(aTHX_ packWARN(WARN_QW),
7384 "Possible attempt to separate words with commas");
7387 else if (*d == '#') {
7388 Perl_warner(aTHX_ packWARN(WARN_QW),
7389 "Possible attempt to put comments in qw() list");
7395 for (; !isSPACE(*d) && len; --len, ++d)
7398 sv = newSVpvn_utf8(b, d-b, DO_UTF8(PL_lex_stuff));
7399 words = op_append_elem(OP_LIST, words,
7400 newSVOP(OP_CONST, 0, tokeq(sv)));
7405 words = newNULLLIST();
7407 SvREFCNT_dec(PL_lex_stuff);
7408 PL_lex_stuff = NULL;
7410 PL_expect = XOPERATOR;
7411 pl_yylval.opval = sawparens(words);
7416 s = scan_str(s,!!PL_madskills,FALSE);
7419 pl_yylval.ival = OP_STRINGIFY;
7420 if (SvIVX(PL_lex_stuff) == '\'')
7421 SvIV_set(PL_lex_stuff, 0); /* qq'$foo' should intepolate */
7422 TERM(sublex_start());
7425 s = scan_pat(s,OP_QR);
7426 TERM(sublex_start());
7429 s = scan_str(s,!!PL_madskills,FALSE);
7432 readpipe_override();
7433 TERM(sublex_start());
7441 s = force_version(s, FALSE);
7443 else if (*s != 'v' || !isDIGIT(s[1])
7444 || (s = force_version(s, TRUE), *s == 'v'))
7446 *PL_tokenbuf = '\0';
7447 s = force_word(s,WORD,TRUE,TRUE,FALSE);
7448 if (isIDFIRST_lazy_if(PL_tokenbuf,UTF))
7449 gv_stashpvn(PL_tokenbuf, strlen(PL_tokenbuf), GV_ADD);
7451 yyerror("<> should be quotes");
7453 if (orig_keyword == KEY_require) {
7461 PL_last_uni = PL_oldbufptr;
7462 PL_last_lop_op = OP_REQUIRE;
7464 return REPORT( (int)REQUIRE );
7470 s = force_word(s,WORD,TRUE,FALSE,FALSE);
7474 LOP(OP_RENAME,XTERM);
7483 LOP(OP_RINDEX,XTERM);
7492 UNIDOR(OP_READLINE);
7495 UNIDOR(OP_BACKTICK);
7504 LOP(OP_REVERSE,XTERM);
7507 UNIDOR(OP_READLINK);
7514 if (pl_yylval.opval)
7515 TERM(sublex_start());
7517 TOKEN(1); /* force error */
7520 checkcomma(s,PL_tokenbuf,"filehandle");
7530 LOP(OP_SELECT,XTERM);
7536 LOP(OP_SEMCTL,XTERM);
7539 LOP(OP_SEMGET,XTERM);
7542 LOP(OP_SEMOP,XTERM);
7548 LOP(OP_SETPGRP,XTERM);
7550 case KEY_setpriority:
7551 LOP(OP_SETPRIORITY,XTERM);
7553 case KEY_sethostent:
7559 case KEY_setservent:
7562 case KEY_setprotoent:
7572 LOP(OP_SEEKDIR,XTERM);
7574 case KEY_setsockopt:
7575 LOP(OP_SSOCKOPT,XTERM);
7581 LOP(OP_SHMCTL,XTERM);
7584 LOP(OP_SHMGET,XTERM);
7587 LOP(OP_SHMREAD,XTERM);
7590 LOP(OP_SHMWRITE,XTERM);
7593 LOP(OP_SHUTDOWN,XTERM);
7602 LOP(OP_SOCKET,XTERM);
7604 case KEY_socketpair:
7605 LOP(OP_SOCKPAIR,XTERM);
7608 checkcomma(s,PL_tokenbuf,"subroutine name");
7610 if (*s == ';' || *s == ')') /* probably a close */
7611 Perl_croak(aTHX_ "sort is now a reserved word");
7613 s = force_word(s,WORD,TRUE,TRUE,FALSE);
7617 LOP(OP_SPLIT,XTERM);
7620 LOP(OP_SPRINTF,XTERM);
7623 LOP(OP_SPLICE,XTERM);
7638 LOP(OP_SUBSTR,XTERM);
7644 char tmpbuf[sizeof PL_tokenbuf];
7645 SSize_t tboffset = 0;
7646 expectation attrful;
7647 bool have_name, have_proto;
7648 const int key = tmp;
7653 char *tstart = SvPVX(PL_linestr) + PL_realtokenstart;
7654 SV *subtoken = newSVpvn(tstart, s - tstart);
7658 s = SKIPSPACE2(s,tmpwhite);
7663 if (isIDFIRST_lazy_if(s,UTF) || *s == '\'' ||
7664 (*s == ':' && s[1] == ':'))
7667 SV *nametoke = NULL;
7671 attrful = XATTRBLOCK;
7672 /* remember buffer pos'n for later force_word */
7673 tboffset = s - PL_oldbufptr;
7674 d = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
7677 nametoke = newSVpvn(s, d - s);
7679 if (memchr(tmpbuf, ':', len))
7680 sv_setpvn(PL_subname, tmpbuf, len);
7682 sv_setsv(PL_subname,PL_curstname);
7683 sv_catpvs(PL_subname,"::");
7684 sv_catpvn(PL_subname,tmpbuf,len);
7691 CURMAD('X', nametoke);
7692 CURMAD('_', tmpwhite);
7693 (void) force_word(PL_oldbufptr + tboffset, WORD,
7696 s = SKIPSPACE2(d,tmpwhite);
7703 Perl_croak(aTHX_ "Missing name in \"my sub\"");
7704 PL_expect = XTERMBLOCK;
7705 attrful = XATTRTERM;
7706 sv_setpvs(PL_subname,"?");
7710 if (key == KEY_format) {
7712 PL_lex_formbrack = PL_lex_brackets + 1;
7714 PL_thistoken = subtoken;
7718 (void) force_word(PL_oldbufptr + tboffset, WORD,
7724 /* Look for a prototype */
7727 bool bad_proto = FALSE;
7728 bool in_brackets = FALSE;
7729 char greedy_proto = ' ';
7730 bool proto_after_greedy_proto = FALSE;
7731 bool must_be_last = FALSE;
7732 bool underscore = FALSE;
7733 bool seen_underscore = FALSE;
7734 const bool warnillegalproto = ckWARN(WARN_ILLEGALPROTO);
7736 s = scan_str(s,!!PL_madskills,FALSE);
7738 Perl_croak(aTHX_ "Prototype not terminated");
7739 /* strip spaces and check for bad characters */
7740 d = SvPVX(PL_lex_stuff);
7742 for (p = d; *p; ++p) {
7746 if (warnillegalproto) {
7748 proto_after_greedy_proto = TRUE;
7749 if (!strchr("$@%*;[]&\\_+", *p)) {
7761 else if ( *p == ']' ) {
7762 in_brackets = FALSE;
7764 else if ( (*p == '@' || *p == '%') &&
7765 ( tmp < 2 || d[tmp-2] != '\\' ) &&
7767 must_be_last = TRUE;
7770 else if ( *p == '_' ) {
7771 underscore = seen_underscore = TRUE;
7778 if (proto_after_greedy_proto)
7779 Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
7780 "Prototype after '%c' for %"SVf" : %s",
7781 greedy_proto, SVfARG(PL_subname), d);
7783 Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
7784 "Illegal character %sin prototype for %"SVf" : %s",
7785 seen_underscore ? "after '_' " : "",
7786 SVfARG(PL_subname), d);
7787 SvCUR_set(PL_lex_stuff, tmp);
7792 CURMAD('q', PL_thisopen);
7793 CURMAD('_', tmpwhite);
7794 CURMAD('=', PL_thisstuff);
7795 CURMAD('Q', PL_thisclose);
7796 NEXTVAL_NEXTTOKE.opval =
7797 (OP*)newSVOP(OP_CONST, 0, PL_lex_stuff);
7798 PL_lex_stuff = NULL;
7801 s = SKIPSPACE2(s,tmpwhite);
7809 if (*s == ':' && s[1] != ':')
7810 PL_expect = attrful;
7811 else if (*s != '{' && key == KEY_sub) {
7813 Perl_croak(aTHX_ "Illegal declaration of anonymous subroutine");
7814 else if (*s != ';' && *s != '}')
7815 Perl_croak(aTHX_ "Illegal declaration of subroutine %"SVf, SVfARG(PL_subname));
7822 curmad('^', newSVpvs(""));
7823 CURMAD('_', tmpwhite);
7827 PL_thistoken = subtoken;
7830 NEXTVAL_NEXTTOKE.opval =
7831 (OP*)newSVOP(OP_CONST, 0, PL_lex_stuff);
7832 PL_lex_stuff = NULL;
7838 sv_setpvs(PL_subname, "__ANON__");
7840 sv_setpvs(PL_subname, "__ANON__::__ANON__");
7844 (void) force_word(PL_oldbufptr + tboffset, WORD,
7853 LOP(OP_SYSTEM,XREF);
7856 LOP(OP_SYMLINK,XTERM);
7859 LOP(OP_SYSCALL,XTERM);
7862 LOP(OP_SYSOPEN,XTERM);
7865 LOP(OP_SYSSEEK,XTERM);
7868 LOP(OP_SYSREAD,XTERM);
7871 LOP(OP_SYSWRITE,XTERM);
7875 TERM(sublex_start());
7896 LOP(OP_TRUNCATE,XTERM);
7908 pl_yylval.ival = CopLINE(PL_curcop);
7912 pl_yylval.ival = CopLINE(PL_curcop);
7916 LOP(OP_UNLINK,XTERM);
7922 LOP(OP_UNPACK,XTERM);
7925 LOP(OP_UTIME,XTERM);
7931 LOP(OP_UNSHIFT,XTERM);
7934 s = tokenize_use(1, s);
7944 pl_yylval.ival = CopLINE(PL_curcop);
7948 pl_yylval.ival = CopLINE(PL_curcop);
7952 PL_hints |= HINT_BLOCK_SCOPE;
7959 LOP(OP_WAITPID,XTERM);
7968 ctl_l[0] = toCTRL('L');
7970 gv_fetchpvn_flags(ctl_l, 1, GV_ADD|GV_NOTQUAL, SVt_PV);
7973 /* Make sure $^L is defined */
7974 gv_fetchpvs("\f", GV_ADD|GV_NOTQUAL, SVt_PV);
7979 if (PL_expect == XOPERATOR)
7985 pl_yylval.ival = OP_XOR;
7990 TERM(sublex_start());
7995 #pragma segment Main
7999 S_pending_ident(pTHX)
8004 /* pit holds the identifier we read and pending_ident is reset */
8005 char pit = PL_pending_ident;
8006 const STRLEN tokenbuf_len = strlen(PL_tokenbuf);
8007 /* All routes through this function want to know if there is a colon. */
8008 const char *const has_colon = (const char*) memchr (PL_tokenbuf, ':', tokenbuf_len);
8009 PL_pending_ident = 0;
8011 /* PL_realtokenstart = realtokenend = PL_bufptr - SvPVX(PL_linestr); */
8012 DEBUG_T({ PerlIO_printf(Perl_debug_log,
8013 "### Pending identifier '%s'\n", PL_tokenbuf); });
8015 /* if we're in a my(), we can't allow dynamics here.
8016 $foo'bar has already been turned into $foo::bar, so
8017 just check for colons.
8019 if it's a legal name, the OP is a PADANY.
8022 if (PL_in_my == KEY_our) { /* "our" is merely analogous to "my" */
8024 yyerror(Perl_form(aTHX_ "No package name allowed for "
8025 "variable %s in \"our\"",
8027 tmp = allocmy(PL_tokenbuf, tokenbuf_len, 0);
8031 yyerror(Perl_form(aTHX_ PL_no_myglob,
8032 PL_in_my == KEY_my ? "my" : "state", PL_tokenbuf));
8034 pl_yylval.opval = newOP(OP_PADANY, 0);
8035 pl_yylval.opval->op_targ = allocmy(PL_tokenbuf, tokenbuf_len, 0);
8041 build the ops for accesses to a my() variable.
8043 Deny my($a) or my($b) in a sort block, *if* $a or $b is
8044 then used in a comparison. This catches most, but not
8045 all cases. For instance, it catches
8046 sort { my($a); $a <=> $b }
8048 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
8049 (although why you'd do that is anyone's guess).
8054 tmp = pad_findmy(PL_tokenbuf, tokenbuf_len, 0);
8055 if (tmp != NOT_IN_PAD) {
8056 /* might be an "our" variable" */
8057 if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
8058 /* build ops for a bareword */
8059 HV * const stash = PAD_COMPNAME_OURSTASH(tmp);
8060 HEK * const stashname = HvNAME_HEK(stash);
8061 SV * const sym = newSVhek(stashname);
8062 sv_catpvs(sym, "::");
8063 sv_catpvn(sym, PL_tokenbuf+1, tokenbuf_len - 1);
8064 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sym);
8065 pl_yylval.opval->op_private = OPpCONST_ENTERED;
8068 ? (GV_ADDMULTI | GV_ADDINEVAL)
8071 ((PL_tokenbuf[0] == '$') ? SVt_PV
8072 : (PL_tokenbuf[0] == '@') ? SVt_PVAV
8077 /* if it's a sort block and they're naming $a or $b */
8078 if (PL_last_lop_op == OP_SORT &&
8079 PL_tokenbuf[0] == '$' &&
8080 (PL_tokenbuf[1] == 'a' || PL_tokenbuf[1] == 'b')
8083 for (d = PL_in_eval ? PL_oldoldbufptr : PL_linestart;
8084 d < PL_bufend && *d != '\n';
8087 if (strnEQ(d,"<=>",3) || strnEQ(d,"cmp",3)) {
8088 Perl_croak(aTHX_ "Can't use \"my %s\" in sort comparison",
8094 pl_yylval.opval = newOP(OP_PADANY, 0);
8095 pl_yylval.opval->op_targ = tmp;
8101 Whine if they've said @foo in a doublequoted string,
8102 and @foo isn't a variable we can find in the symbol
8105 if (ckWARN(WARN_AMBIGUOUS) &&
8106 pit == '@' && PL_lex_state != LEX_NORMAL && !PL_lex_brackets) {
8107 GV *const gv = gv_fetchpvn_flags(PL_tokenbuf + 1, tokenbuf_len - 1, 0,
8109 if ((!gv || ((PL_tokenbuf[0] == '@') ? !GvAV(gv) : !GvHV(gv)))
8110 /* DO NOT warn for @- and @+ */
8111 && !( PL_tokenbuf[2] == '\0' &&
8112 ( PL_tokenbuf[1] == '-' || PL_tokenbuf[1] == '+' ))
8115 /* Downgraded from fatal to warning 20000522 mjd */
8116 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
8117 "Possible unintended interpolation of %s in string",
8122 /* build ops for a bareword */
8123 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, newSVpvn(PL_tokenbuf + 1,
8125 pl_yylval.opval->op_private = OPpCONST_ENTERED;
8126 gv_fetchpvn_flags(PL_tokenbuf+1, tokenbuf_len - 1,
8127 PL_in_eval ? (GV_ADDMULTI | GV_ADDINEVAL) : GV_ADD,
8128 ((PL_tokenbuf[0] == '$') ? SVt_PV
8129 : (PL_tokenbuf[0] == '@') ? SVt_PVAV
8135 * The following code was generated by perl_keyword.pl.
8139 Perl_keyword (pTHX_ const char *name, I32 len, bool all_keywords)
8143 PERL_ARGS_ASSERT_KEYWORD;
8147 case 1: /* 5 tokens of length 1 */
8179 case 2: /* 18 tokens of length 2 */
8325 case 3: /* 29 tokens of length 3 */
8329 if (name[1] == 'N' &&
8392 if (name[1] == 'i' &&
8424 if (name[1] == 'o' &&
8433 if (name[1] == 'e' &&
8442 if (name[1] == 'n' &&
8451 if (name[1] == 'o' &&
8460 if (name[1] == 'a' &&
8469 if (name[1] == 'o' &&
8531 if (name[1] == 'e' &&
8545 return (all_keywords || FEATURE_IS_ENABLED("say") ? KEY_say : 0);
8571 if (name[1] == 'i' &&
8580 if (name[1] == 's' &&
8589 if (name[1] == 'e' &&
8598 if (name[1] == 'o' &&
8610 case 4: /* 41 tokens of length 4 */
8614 if (name[1] == 'O' &&
8624 if (name[1] == 'N' &&
8634 if (name[1] == 'i' &&
8644 if (name[1] == 'h' &&
8654 if (name[1] == 'u' &&
8667 if (name[2] == 'c' &&
8676 if (name[2] == 's' &&
8685 if (name[2] == 'a' &&
8721 if (name[1] == 'o' &&
8734 if (name[2] == 't' &&
8743 if (name[2] == 'o' &&
8752 if (name[2] == 't' &&
8761 if (name[2] == 'e' &&
8774 if (name[1] == 'o' &&
8787 if (name[2] == 'y' &&
8796 if (name[2] == 'l' &&
8812 if (name[2] == 's' &&
8821 if (name[2] == 'n' &&
8830 if (name[2] == 'c' &&
8843 if (name[1] == 'e' &&
8853 if (name[1] == 'p' &&
8866 if (name[2] == 'c' &&
8875 if (name[2] == 'p' &&
8884 if (name[2] == 's' &&
8900 if (name[2] == 'n' &&
8970 if (name[2] == 'r' &&
8979 if (name[2] == 'r' &&
8988 if (name[2] == 'a' &&
9004 if (name[2] == 'l' &&
9066 if (name[2] == 'e' &&
9069 return (all_keywords || FEATURE_IS_ENABLED("switch") ? KEY_when : 0);
9082 case 5: /* 39 tokens of length 5 */
9086 if (name[1] == 'E' &&
9097 if (name[1] == 'H' &&
9111 if (name[2] == 'a' &&
9121 if (name[2] == 'a' &&
9138 if (name[2] == 'e' &&
9148 if (name[2] == 'e' &&
9152 return (all_keywords || FEATURE_IS_ENABLED("switch") ? -KEY_break : 0);
9168 if (name[3] == 'i' &&
9177 if (name[3] == 'o' &&
9213 if (name[2] == 'o' &&
9223 if (name[2] == 'y' &&
9237 if (name[1] == 'l' &&
9251 if (name[2] == 'n' &&
9261 if (name[2] == 'o' &&
9275 if (name[1] == 'i' &&
9280 return (all_keywords || FEATURE_IS_ENABLED("switch") ? KEY_given : 0);
9289 if (name[2] == 'd' &&
9299 if (name[2] == 'c' &&
9316 if (name[2] == 'c' &&
9326 if (name[2] == 't' &&
9340 if (name[1] == 'k' &&
9351 if (name[1] == 'r' &&
9365 if (name[2] == 's' &&
9375 if (name[2] == 'd' &&
9392 if (name[2] == 'm' &&
9402 if (name[2] == 'i' &&
9412 if (name[2] == 'e' &&
9422 if (name[2] == 'l' &&
9432 if (name[2] == 'a' &&
9445 if (name[3] == 't' &&
9448 return (all_keywords || FEATURE_IS_ENABLED("state") ? KEY_state : 0);
9454 if (name[3] == 'd' &&
9471 if (name[1] == 'i' &&
9485 if (name[2] == 'a' &&
9498 if (name[3] == 'e' &&
9533 if (name[2] == 'i' &&
9550 if (name[2] == 'i' &&
9560 if (name[2] == 'i' &&
9577 case 6: /* 33 tokens of length 6 */
9581 if (name[1] == 'c' &&
9596 if (name[2] == 'l' &&
9607 if (name[2] == 'r' &&
9622 if (name[1] == 'e' &&
9637 if (name[2] == 's' &&
9642 Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX), "elseif should be elsif");
9648 if (name[2] == 'i' &&
9666 if (name[2] == 'l' &&
9677 if (name[2] == 'r' &&
9692 if (name[1] == 'm' &&
9707 if (name[2] == 'n' &&
9718 if (name[2] == 's' &&
9733 if (name[1] == 's' &&
9739 if (name[4] == 't' &&
9748 if (name[4] == 'e' &&
9757 if (name[4] == 'c' &&
9766 if (name[4] == 'n' &&
9782 if (name[1] == 'r' &&
9800 if (name[3] == 'a' &&
9810 if (name[3] == 'u' &&
9824 if (name[2] == 'n' &&
9842 if (name[2] == 'a' &&
9856 if (name[3] == 'e' &&
9869 if (name[4] == 't' &&
9878 if (name[4] == 'e' &&
9900 if (name[4] == 't' &&
9909 if (name[4] == 'e' &&
9925 if (name[2] == 'c' &&
9936 if (name[2] == 'l' &&
9947 if (name[2] == 'b' &&
9958 if (name[2] == 's' &&
9981 if (name[4] == 's' &&
9990 if (name[4] == 'n' &&
10003 if (name[3] == 'a' &&
10007 return -KEY_unpack;
10020 if (name[1] == 'a' &&
10026 return -KEY_values;
10035 case 7: /* 29 tokens of length 7 */
10039 if (name[1] == 'E' &&
10046 return KEY_DESTROY;
10052 if (name[1] == '_' &&
10059 return KEY___END__;
10065 if (name[1] == 'i' &&
10072 return -KEY_binmode;
10078 if (name[1] == 'o' &&
10085 return -KEY_connect;
10094 if (name[2] == 'm' &&
10100 return -KEY_dbmopen;
10106 if (name[2] == 'f')
10111 if (name[4] == 'u' &&
10115 return (all_keywords || FEATURE_IS_ENABLED("switch") ? KEY_default : 0);
10121 if (name[4] == 'n' &&
10125 return KEY_defined;
10142 if (name[1] == 'o' &&
10149 return KEY_foreach;
10155 if (name[1] == 'e' &&
10162 if (name[5] == 'r' &&
10165 return -KEY_getpgrp;
10171 if (name[5] == 'i' &&
10174 return -KEY_getppid;
10187 if (name[1] == 'c' &&
10194 return -KEY_lcfirst;
10200 if (name[1] == 'p' &&
10207 return -KEY_opendir;
10213 if (name[1] == 'a' &&
10220 return KEY_package;
10226 if (name[1] == 'e')
10231 if (name[3] == 'd' &&
10236 return -KEY_readdir;
10242 if (name[3] == 'u' &&
10247 return KEY_require;
10253 if (name[3] == 'e' &&
10258 return -KEY_reverse;
10277 if (name[3] == 'k' &&
10282 return -KEY_seekdir;
10288 if (name[3] == 'p' &&
10293 return -KEY_setpgrp;
10303 if (name[2] == 'm' &&
10309 return -KEY_shmread;
10315 if (name[2] == 'r' &&
10321 return -KEY_sprintf;
10330 if (name[3] == 'l' &&
10335 return -KEY_symlink;
10344 if (name[4] == 'a' &&
10348 return -KEY_syscall;
10354 if (name[4] == 'p' &&
10358 return -KEY_sysopen;
10364 if (name[4] == 'e' &&
10368 return -KEY_sysread;
10374 if (name[4] == 'e' &&
10378 return -KEY_sysseek;
10396 if (name[1] == 'e' &&
10403 return -KEY_telldir;
10412 if (name[2] == 'f' &&
10418 return -KEY_ucfirst;
10424 if (name[2] == 's' &&
10430 return -KEY_unshift;
10440 if (name[1] == 'a' &&
10447 return -KEY_waitpid;
10456 case 8: /* 26 tokens of length 8 */
10460 if (name[1] == 'U' &&
10468 return KEY_AUTOLOAD;
10474 if (name[1] == '_')
10479 if (name[3] == 'A' &&
10485 return KEY___DATA__;
10491 if (name[3] == 'I' &&
10497 return -KEY___FILE__;
10503 if (name[3] == 'I' &&
10509 return -KEY___LINE__;
10525 if (name[2] == 'o' &&
10532 return -KEY_closedir;
10538 if (name[2] == 'n' &&
10545 return -KEY_continue;
10555 if (name[1] == 'b' &&
10563 return -KEY_dbmclose;
10569 if (name[1] == 'n' &&
10575 if (name[4] == 'r' &&
10580 return -KEY_endgrent;
10586 if (name[4] == 'w' &&
10591 return -KEY_endpwent;
10604 if (name[1] == 'o' &&
10612 return -KEY_formline;
10618 if (name[1] == 'e' &&
10624 if (name[4] == 'r')
10629 if (name[6] == 'n' &&
10632 return -KEY_getgrent;
10638 if (name[6] == 'i' &&
10641 return -KEY_getgrgid;
10647 if (name[6] == 'a' &&
10650 return -KEY_getgrnam;
10663 if (name[4] == 'o' &&
10668 return -KEY_getlogin;
10674 if (name[4] == 'w')
10679 if (name[6] == 'n' &&
10682 return -KEY_getpwent;
10688 if (name[6] == 'a' &&
10691 return -KEY_getpwnam;
10697 if (name[6] == 'i' &&
10700 return -KEY_getpwuid;
10720 if (name[1] == 'e' &&
10727 if (name[5] == 'i' &&
10734 return -KEY_readline;
10739 return -KEY_readlink;
10750 if (name[5] == 'i' &&
10754 return -KEY_readpipe;
10770 if (name[2] == 't')
10775 if (name[4] == 'r' &&
10780 return -KEY_setgrent;
10786 if (name[4] == 'w' &&
10791 return -KEY_setpwent;
10807 if (name[3] == 'w' &&
10813 return -KEY_shmwrite;
10819 if (name[3] == 't' &&
10825 return -KEY_shutdown;
10835 if (name[2] == 's' &&
10842 return -KEY_syswrite;
10852 if (name[1] == 'r' &&
10860 return -KEY_truncate;
10869 case 9: /* 9 tokens of length 9 */
10873 if (name[1] == 'N' &&
10882 return KEY_UNITCHECK;
10888 if (name[1] == 'n' &&
10897 return -KEY_endnetent;
10903 if (name[1] == 'e' &&
10912 return -KEY_getnetent;
10918 if (name[1] == 'o' &&
10927 return -KEY_localtime;
10933 if (name[1] == 'r' &&
10942 return KEY_prototype;
10948 if (name[1] == 'u' &&
10957 return -KEY_quotemeta;
10963 if (name[1] == 'e' &&
10972 return -KEY_rewinddir;
10978 if (name[1] == 'e' &&
10987 return -KEY_setnetent;
10993 if (name[1] == 'a' &&
11002 return -KEY_wantarray;
11011 case 10: /* 9 tokens of length 10 */
11015 if (name[1] == 'n' &&
11021 if (name[4] == 'o' &&
11028 return -KEY_endhostent;
11034 if (name[4] == 'e' &&
11041 return -KEY_endservent;
11054 if (name[1] == 'e' &&
11060 if (name[4] == 'o' &&
11067 return -KEY_gethostent;
11076 if (name[5] == 'r' &&
11082 return -KEY_getservent;
11088 if (name[5] == 'c' &&
11094 return -KEY_getsockopt;
11114 if (name[2] == 't')
11119 if (name[4] == 'o' &&
11126 return -KEY_sethostent;
11135 if (name[5] == 'r' &&
11141 return -KEY_setservent;
11147 if (name[5] == 'c' &&
11153 return -KEY_setsockopt;
11170 if (name[2] == 'c' &&
11179 return -KEY_socketpair;
11192 case 11: /* 8 tokens of length 11 */
11196 if (name[1] == '_' &&
11206 { /* __PACKAGE__ */
11207 return -KEY___PACKAGE__;
11213 if (name[1] == 'n' &&
11223 { /* endprotoent */
11224 return -KEY_endprotoent;
11230 if (name[1] == 'e' &&
11239 if (name[5] == 'e' &&
11245 { /* getpeername */
11246 return -KEY_getpeername;
11255 if (name[6] == 'o' &&
11260 { /* getpriority */
11261 return -KEY_getpriority;
11267 if (name[6] == 't' &&
11272 { /* getprotoent */
11273 return -KEY_getprotoent;
11287 if (name[4] == 'o' &&
11294 { /* getsockname */
11295 return -KEY_getsockname;
11308 if (name[1] == 'e' &&
11316 if (name[6] == 'o' &&
11321 { /* setpriority */
11322 return -KEY_setpriority;
11328 if (name[6] == 't' &&
11333 { /* setprotoent */
11334 return -KEY_setprotoent;
11350 case 12: /* 2 tokens of length 12 */
11351 if (name[0] == 'g' &&
11363 if (name[9] == 'd' &&
11366 { /* getnetbyaddr */
11367 return -KEY_getnetbyaddr;
11373 if (name[9] == 'a' &&
11376 { /* getnetbyname */
11377 return -KEY_getnetbyname;
11389 case 13: /* 4 tokens of length 13 */
11390 if (name[0] == 'g' &&
11397 if (name[4] == 'o' &&
11406 if (name[10] == 'd' &&
11409 { /* gethostbyaddr */
11410 return -KEY_gethostbyaddr;
11416 if (name[10] == 'a' &&
11419 { /* gethostbyname */
11420 return -KEY_gethostbyname;
11433 if (name[4] == 'e' &&
11442 if (name[10] == 'a' &&
11445 { /* getservbyname */
11446 return -KEY_getservbyname;
11452 if (name[10] == 'o' &&
11455 { /* getservbyport */
11456 return -KEY_getservbyport;
11475 case 14: /* 1 tokens of length 14 */
11476 if (name[0] == 'g' &&
11490 { /* getprotobyname */
11491 return -KEY_getprotobyname;
11496 case 16: /* 1 tokens of length 16 */
11497 if (name[0] == 'g' &&
11513 { /* getprotobynumber */
11514 return -KEY_getprotobynumber;
11528 S_checkcomma(pTHX_ const char *s, const char *name, const char *what)
11532 PERL_ARGS_ASSERT_CHECKCOMMA;
11534 if (*s == ' ' && s[1] == '(') { /* XXX gotta be a better way */
11535 if (ckWARN(WARN_SYNTAX)) {
11538 for (w = s+2; *w && level; w++) {
11541 else if (*w == ')')
11544 while (isSPACE(*w))
11546 /* the list of chars below is for end of statements or
11547 * block / parens, boolean operators (&&, ||, //) and branch
11548 * constructs (or, and, if, until, unless, while, err, for).
11549 * Not a very solid hack... */
11550 if (!*w || !strchr(";&/|})]oaiuwef!=", *w))
11551 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11552 "%s (...) interpreted as function",name);
11555 while (s < PL_bufend && isSPACE(*s))
11559 while (s < PL_bufend && isSPACE(*s))
11561 if (isIDFIRST_lazy_if(s,UTF)) {
11562 const char * const w = s++;
11563 while (isALNUM_lazy_if(s,UTF))
11565 while (s < PL_bufend && isSPACE(*s))
11569 if (keyword(w, s - w, 0))
11572 gv = gv_fetchpvn_flags(w, s - w, 0, SVt_PVCV);
11573 if (gv && GvCVu(gv))
11575 Perl_croak(aTHX_ "No comma allowed after %s", what);
11580 /* Either returns sv, or mortalizes sv and returns a new SV*.
11581 Best used as sv=new_constant(..., sv, ...).
11582 If s, pv are NULL, calls subroutine with one argument,
11583 and type is used with error messages only. */
11586 S_new_constant(pTHX_ const char *s, STRLEN len, const char *key, STRLEN keylen,
11587 SV *sv, SV *pv, const char *type, STRLEN typelen)
11590 HV * const table = GvHV(PL_hintgv); /* ^H */
11594 const char *why1 = "", *why2 = "", *why3 = "";
11596 PERL_ARGS_ASSERT_NEW_CONSTANT;
11598 if (!table || !(PL_hints & HINT_LOCALIZE_HH)) {
11601 why2 = (const char *)
11602 (strEQ(key,"charnames")
11603 ? "(possibly a missing \"use charnames ...\")"
11605 msg = Perl_newSVpvf(aTHX_ "Constant(%s) unknown: %s",
11606 (type ? type: "undef"), why2);
11608 /* This is convoluted and evil ("goto considered harmful")
11609 * but I do not understand the intricacies of all the different
11610 * failure modes of %^H in here. The goal here is to make
11611 * the most probable error message user-friendly. --jhi */
11616 msg = Perl_newSVpvf(aTHX_ "Constant(%s): %s%s%s",
11617 (type ? type: "undef"), why1, why2, why3);
11619 yyerror(SvPVX_const(msg));
11624 /* charnames doesn't work well if there have been errors found */
11625 if (PL_error_count > 0 && strEQ(key,"charnames"))
11626 return &PL_sv_undef;
11628 cvp = hv_fetch(table, key, keylen, FALSE);
11629 if (!cvp || !SvOK(*cvp)) {
11632 why3 = "} is not defined";
11635 sv_2mortal(sv); /* Parent created it permanently */
11638 pv = newSVpvn_flags(s, len, SVs_TEMP);
11640 typesv = newSVpvn_flags(type, typelen, SVs_TEMP);
11642 typesv = &PL_sv_undef;
11644 PUSHSTACKi(PERLSI_OVERLOAD);
11656 call_sv(cv, G_SCALAR | ( PL_in_eval ? 0 : G_EVAL));
11660 /* Check the eval first */
11661 if (!PL_in_eval && SvTRUE(ERRSV)) {
11662 sv_catpvs(ERRSV, "Propagated");
11663 yyerror(SvPV_nolen_const(ERRSV)); /* Duplicates the message inside eval */
11665 res = SvREFCNT_inc_simple(sv);
11669 SvREFCNT_inc_simple_void(res);
11678 why1 = "Call to &{$^H{";
11680 why3 = "}} did not return a defined value";
11688 /* Returns a NUL terminated string, with the length of the string written to
11692 S_scan_word(pTHX_ register char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp)
11695 register char *d = dest;
11696 register char * const e = d + destlen - 3; /* two-character token, ending NUL */
11698 PERL_ARGS_ASSERT_SCAN_WORD;
11702 Perl_croak(aTHX_ ident_too_long);
11703 if (isALNUM(*s)) /* UTF handled below */
11705 else if (allow_package && (*s == '\'') && isIDFIRST_lazy_if(s+1,UTF)) {
11710 else if (allow_package && (s[0] == ':') && (s[1] == ':') && (s[2] != '$')) {
11714 else if (UTF && UTF8_IS_START(*s) && isALNUM_utf8((U8*)s)) {
11715 char *t = s + UTF8SKIP(s);
11717 while (UTF8_IS_CONTINUED(*t) && is_utf8_mark((U8*)t))
11721 Perl_croak(aTHX_ ident_too_long);
11722 Copy(s, d, len, char);
11735 S_scan_ident(pTHX_ register char *s, register const char *send, char *dest, STRLEN destlen, I32 ck_uni)
11738 char *bracket = NULL;
11740 register char *d = dest;
11741 register char * const e = d + destlen - 3; /* two-character token, ending NUL */
11743 PERL_ARGS_ASSERT_SCAN_IDENT;
11748 while (isDIGIT(*s)) {
11750 Perl_croak(aTHX_ ident_too_long);
11757 Perl_croak(aTHX_ ident_too_long);
11758 if (isALNUM(*s)) /* UTF handled below */
11760 else if (*s == '\'' && isIDFIRST_lazy_if(s+1,UTF)) {
11765 else if (*s == ':' && s[1] == ':') {
11769 else if (UTF && UTF8_IS_START(*s) && isALNUM_utf8((U8*)s)) {
11770 char *t = s + UTF8SKIP(s);
11771 while (UTF8_IS_CONTINUED(*t) && is_utf8_mark((U8*)t))
11773 if (d + (t - s) > e)
11774 Perl_croak(aTHX_ ident_too_long);
11775 Copy(s, d, t - s, char);
11786 if (PL_lex_state != LEX_NORMAL)
11787 PL_lex_state = LEX_INTERPENDMAYBE;
11790 if (*s == '$' && s[1] &&
11791 (isALNUM_lazy_if(s+1,UTF) || s[1] == '$' || s[1] == '{' || strnEQ(s+1,"::",2)) )
11804 if (*d == '^' && *s && isCONTROLVAR(*s)) {
11809 if (isSPACE(s[-1])) {
11811 const char ch = *s++;
11812 if (!SPACE_OR_TAB(ch)) {
11818 if (isIDFIRST_lazy_if(d,UTF)) {
11822 while ((end < send && isALNUM_lazy_if(end,UTF)) || *end == ':') {
11823 end += UTF8SKIP(end);
11824 while (end < send && UTF8_IS_CONTINUED(*end) && is_utf8_mark((U8*)end))
11825 end += UTF8SKIP(end);
11827 Copy(s, d, end - s, char);
11832 while ((isALNUM(*s) || *s == ':') && d < e)
11835 Perl_croak(aTHX_ ident_too_long);
11838 while (s < send && SPACE_OR_TAB(*s))
11840 if ((*s == '[' || (*s == '{' && strNE(dest, "sub")))) {
11841 if (ckWARN(WARN_AMBIGUOUS) && keyword(dest, d - dest, 0)) {
11842 const char * const brack =
11844 ((*s == '[') ? "[...]" : "{...}");
11845 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
11846 "Ambiguous use of %c{%s%s} resolved to %c%s%s",
11847 funny, dest, brack, funny, dest, brack);
11850 PL_lex_brackstack[PL_lex_brackets++] = (char)(XOPERATOR | XFAKEBRACK);
11854 /* Handle extended ${^Foo} variables
11855 * 1999-02-27 mjd-perl-patch@plover.com */
11856 else if (!isALNUM(*d) && !isPRINT(*d) /* isCTRL(d) */
11860 while (isALNUM(*s) && d < e) {
11864 Perl_croak(aTHX_ ident_too_long);
11869 if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) {
11870 PL_lex_state = LEX_INTERPEND;
11873 if (PL_lex_state == LEX_NORMAL) {
11874 if (ckWARN(WARN_AMBIGUOUS) &&
11875 (keyword(dest, d - dest, 0)
11876 || get_cvn_flags(dest, d - dest, 0)))
11880 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
11881 "Ambiguous use of %c{%s} resolved to %c%s",
11882 funny, dest, funny, dest);
11887 s = bracket; /* let the parser handle it */
11891 else if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets && !intuit_more(s))
11892 PL_lex_state = LEX_INTERPEND;
11897 S_pmflag(U32 pmfl, const char ch) {
11899 CASE_STD_PMMOD_FLAGS_PARSE_SET(&pmfl);
11900 case GLOBAL_PAT_MOD: pmfl |= PMf_GLOBAL; break;
11901 case CONTINUE_PAT_MOD: pmfl |= PMf_CONTINUE; break;
11902 case ONCE_PAT_MOD: pmfl |= PMf_KEEP; break;
11903 case KEEPCOPY_PAT_MOD: pmfl |= PMf_KEEPCOPY; break;
11904 case NONDESTRUCT_PAT_MOD: pmfl |= PMf_NONDESTRUCT; break;
11910 S_scan_pat(pTHX_ char *start, I32 type)
11914 char *s = scan_str(start,!!PL_madskills,FALSE);
11915 const char * const valid_flags =
11916 (const char *)((type == OP_QR) ? QR_PAT_MODS : M_PAT_MODS);
11921 PERL_ARGS_ASSERT_SCAN_PAT;
11924 const char * const delimiter = skipspace(start);
11928 ? "Search pattern not terminated or ternary operator parsed as search pattern"
11929 : "Search pattern not terminated" ));
11932 pm = (PMOP*)newPMOP(type, 0);
11933 if (PL_multi_open == '?') {
11934 /* This is the only point in the code that sets PMf_ONCE: */
11935 pm->op_pmflags |= PMf_ONCE;
11937 /* Hence it's safe to do this bit of PMOP book-keeping here, which
11938 allows us to restrict the list needed by reset to just the ??
11940 assert(type != OP_TRANS);
11942 MAGIC *mg = mg_find((const SV *)PL_curstash, PERL_MAGIC_symtab);
11945 mg = sv_magicext(MUTABLE_SV(PL_curstash), 0, PERL_MAGIC_symtab, 0, 0,
11948 elements = mg->mg_len / sizeof(PMOP**);
11949 Renewc(mg->mg_ptr, elements + 1, PMOP*, char);
11950 ((PMOP**)mg->mg_ptr) [elements++] = pm;
11951 mg->mg_len = elements * sizeof(PMOP**);
11952 PmopSTASH_set(pm,PL_curstash);
11958 while (*s && strchr(valid_flags, *s))
11959 pm->op_pmflags = S_pmflag(pm->op_pmflags, *s++);
11962 Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX),
11963 "Having no space between pattern and following word is deprecated");
11967 if (PL_madskills && modstart != s) {
11968 SV* tmptoken = newSVpvn(modstart, s - modstart);
11969 append_madprops(newMADPROP('m', MAD_SV, tmptoken, 0), (OP*)pm, 0);
11972 /* issue a warning if /c is specified,but /g is not */
11973 if ((pm->op_pmflags & PMf_CONTINUE) && !(pm->op_pmflags & PMf_GLOBAL))
11975 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
11976 "Use of /c modifier is meaningless without /g" );
11979 PL_lex_op = (OP*)pm;
11980 pl_yylval.ival = OP_MATCH;
11985 S_scan_subst(pTHX_ char *start)
11996 PERL_ARGS_ASSERT_SCAN_SUBST;
11998 pl_yylval.ival = OP_NULL;
12000 s = scan_str(start,!!PL_madskills,FALSE);
12003 Perl_croak(aTHX_ "Substitution pattern not terminated");
12005 if (s[-1] == PL_multi_open)
12008 if (PL_madskills) {
12009 CURMAD('q', PL_thisopen);
12010 CURMAD('_', PL_thiswhite);
12011 CURMAD('E', PL_thisstuff);
12012 CURMAD('Q', PL_thisclose);
12013 PL_realtokenstart = s - SvPVX(PL_linestr);
12017 first_start = PL_multi_start;
12018 s = scan_str(s,!!PL_madskills,FALSE);
12020 if (PL_lex_stuff) {
12021 SvREFCNT_dec(PL_lex_stuff);
12022 PL_lex_stuff = NULL;
12024 Perl_croak(aTHX_ "Substitution replacement not terminated");
12026 PL_multi_start = first_start; /* so whole substitution is taken together */
12028 pm = (PMOP*)newPMOP(OP_SUBST, 0);
12031 if (PL_madskills) {
12032 CURMAD('z', PL_thisopen);
12033 CURMAD('R', PL_thisstuff);
12034 CURMAD('Z', PL_thisclose);
12040 if (*s == EXEC_PAT_MOD) {
12044 else if (strchr(S_PAT_MODS, *s))
12045 pm->op_pmflags = S_pmflag(pm->op_pmflags, *s++);
12048 Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX),
12049 "Having no space between pattern and following word is deprecated");
12057 if (PL_madskills) {
12059 curmad('m', newSVpvn(modstart, s - modstart));
12060 append_madprops(PL_thismad, (OP*)pm, 0);
12064 if ((pm->op_pmflags & PMf_CONTINUE)) {
12065 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), "Use of /c modifier is meaningless in s///" );
12069 SV * const repl = newSVpvs("");
12071 PL_sublex_info.super_bufptr = s;
12072 PL_sublex_info.super_bufend = PL_bufend;
12074 pm->op_pmflags |= PMf_EVAL;
12077 sv_catpvs(repl, "eval ");
12079 sv_catpvs(repl, "do ");
12081 sv_catpvs(repl, "{");
12082 sv_catsv(repl, PL_lex_repl);
12083 if (strchr(SvPVX(PL_lex_repl), '#'))
12084 sv_catpvs(repl, "\n");
12085 sv_catpvs(repl, "}");
12087 SvREFCNT_dec(PL_lex_repl);
12088 PL_lex_repl = repl;
12091 PL_lex_op = (OP*)pm;
12092 pl_yylval.ival = OP_SUBST;
12097 S_scan_trans(pTHX_ char *start)
12106 bool nondestruct = 0;
12111 PERL_ARGS_ASSERT_SCAN_TRANS;
12113 pl_yylval.ival = OP_NULL;
12115 s = scan_str(start,!!PL_madskills,FALSE);
12117 Perl_croak(aTHX_ "Transliteration pattern not terminated");
12119 if (s[-1] == PL_multi_open)
12122 if (PL_madskills) {
12123 CURMAD('q', PL_thisopen);
12124 CURMAD('_', PL_thiswhite);
12125 CURMAD('E', PL_thisstuff);
12126 CURMAD('Q', PL_thisclose);
12127 PL_realtokenstart = s - SvPVX(PL_linestr);
12131 s = scan_str(s,!!PL_madskills,FALSE);
12133 if (PL_lex_stuff) {
12134 SvREFCNT_dec(PL_lex_stuff);
12135 PL_lex_stuff = NULL;
12137 Perl_croak(aTHX_ "Transliteration replacement not terminated");
12139 if (PL_madskills) {
12140 CURMAD('z', PL_thisopen);
12141 CURMAD('R', PL_thisstuff);
12142 CURMAD('Z', PL_thisclose);
12145 complement = del = squash = 0;
12152 complement = OPpTRANS_COMPLEMENT;
12155 del = OPpTRANS_DELETE;
12158 squash = OPpTRANS_SQUASH;
12170 tbl = (short *)PerlMemShared_calloc(complement&&!del?258:256, sizeof(short));
12171 o = newPVOP(nondestruct ? OP_TRANSR : OP_TRANS, 0, (char*)tbl);
12172 o->op_private &= ~OPpTRANS_ALL;
12173 o->op_private |= del|squash|complement|
12174 (DO_UTF8(PL_lex_stuff)? OPpTRANS_FROM_UTF : 0)|
12175 (DO_UTF8(PL_lex_repl) ? OPpTRANS_TO_UTF : 0);
12178 pl_yylval.ival = nondestruct ? OP_TRANSR : OP_TRANS;
12181 if (PL_madskills) {
12183 curmad('m', newSVpvn(modstart, s - modstart));
12184 append_madprops(PL_thismad, o, 0);
12193 S_scan_heredoc(pTHX_ register char *s)
12197 I32 op_type = OP_SCALAR;
12201 const char *found_newline;
12205 const int outer = (PL_rsfp && !(PL_lex_inwhat == OP_SCALAR));
12207 I32 stuffstart = s - SvPVX(PL_linestr);
12210 PL_realtokenstart = -1;
12213 PERL_ARGS_ASSERT_SCAN_HEREDOC;
12217 e = PL_tokenbuf + sizeof PL_tokenbuf - 1;
12221 while (SPACE_OR_TAB(*peek))
12223 if (*peek == '`' || *peek == '\'' || *peek =='"') {
12226 s = delimcpy(d, e, s, PL_bufend, term, &len);
12236 if (!isALNUM_lazy_if(s,UTF))
12237 deprecate("bare << to mean <<\"\"");
12238 for (; isALNUM_lazy_if(s,UTF); s++) {
12243 if (d >= PL_tokenbuf + sizeof PL_tokenbuf - 1)
12244 Perl_croak(aTHX_ "Delimiter for here document is too long");
12247 len = d - PL_tokenbuf;
12250 if (PL_madskills) {
12251 tstart = PL_tokenbuf + !outer;
12252 PL_thisclose = newSVpvn(tstart, len - !outer);
12253 tstart = SvPVX(PL_linestr) + stuffstart;
12254 PL_thisopen = newSVpvn(tstart, s - tstart);
12255 stuffstart = s - SvPVX(PL_linestr);
12258 #ifndef PERL_STRICT_CR
12259 d = strchr(s, '\r');
12261 char * const olds = s;
12263 while (s < PL_bufend) {
12269 else if (*s == '\n' && s[1] == '\r') { /* \015\013 on a mac? */
12278 SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
12285 if ( outer || !(found_newline = (char*)memchr((void*)s, '\n', PL_bufend - s)) ) {
12286 herewas = newSVpvn(s,PL_bufend-s);
12290 herewas = newSVpvn(s-1,found_newline-s+1);
12293 herewas = newSVpvn(s,found_newline-s);
12297 if (PL_madskills) {
12298 tstart = SvPVX(PL_linestr) + stuffstart;
12300 sv_catpvn(PL_thisstuff, tstart, s - tstart);
12302 PL_thisstuff = newSVpvn(tstart, s - tstart);
12305 s += SvCUR(herewas);
12308 stuffstart = s - SvPVX(PL_linestr);
12314 tmpstr = newSV_type(SVt_PVIV);
12315 SvGROW(tmpstr, 80);
12316 if (term == '\'') {
12317 op_type = OP_CONST;
12318 SvIV_set(tmpstr, -1);
12320 else if (term == '`') {
12321 op_type = OP_BACKTICK;
12322 SvIV_set(tmpstr, '\\');
12326 PL_multi_start = CopLINE(PL_curcop);
12327 PL_multi_open = PL_multi_close = '<';
12328 term = *PL_tokenbuf;
12329 if (PL_lex_inwhat == OP_SUBST && PL_in_eval && !PL_rsfp) {
12330 char * const bufptr = PL_sublex_info.super_bufptr;
12331 char * const bufend = PL_sublex_info.super_bufend;
12332 char * const olds = s - SvCUR(herewas);
12333 s = strchr(bufptr, '\n');
12337 while (s < bufend &&
12338 (*s != term || memNE(s,PL_tokenbuf,len)) ) {
12340 CopLINE_inc(PL_curcop);
12343 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
12344 missingterm(PL_tokenbuf);
12346 sv_setpvn(herewas,bufptr,d-bufptr+1);
12347 sv_setpvn(tmpstr,d+1,s-d);
12349 sv_catpvn(herewas,s,bufend-s);
12350 Copy(SvPVX_const(herewas),bufptr,SvCUR(herewas) + 1,char);
12357 while (s < PL_bufend &&
12358 (*s != term || memNE(s,PL_tokenbuf,len)) ) {
12360 CopLINE_inc(PL_curcop);
12362 if (s >= PL_bufend) {
12363 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
12364 missingterm(PL_tokenbuf);
12366 sv_setpvn(tmpstr,d+1,s-d);
12368 if (PL_madskills) {
12370 sv_catpvn(PL_thisstuff, d + 1, s - d);
12372 PL_thisstuff = newSVpvn(d + 1, s - d);
12373 stuffstart = s - SvPVX(PL_linestr);
12377 CopLINE_inc(PL_curcop); /* the preceding stmt passes a newline */
12379 sv_catpvn(herewas,s,PL_bufend-s);
12380 sv_setsv(PL_linestr,herewas);
12381 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart = SvPVX(PL_linestr);
12382 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
12383 PL_last_lop = PL_last_uni = NULL;
12386 sv_setpvs(tmpstr,""); /* avoid "uninitialized" warning */
12387 while (s >= PL_bufend) { /* multiple line string? */
12389 if (PL_madskills) {
12390 tstart = SvPVX(PL_linestr) + stuffstart;
12392 sv_catpvn(PL_thisstuff, tstart, PL_bufend - tstart);
12394 PL_thisstuff = newSVpvn(tstart, PL_bufend - tstart);
12398 CopLINE_inc(PL_curcop);
12399 if (!outer || !lex_next_chunk(0)) {
12400 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
12401 missingterm(PL_tokenbuf);
12403 CopLINE_dec(PL_curcop);
12406 stuffstart = s - SvPVX(PL_linestr);
12408 CopLINE_inc(PL_curcop);
12409 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
12410 PL_last_lop = PL_last_uni = NULL;
12411 #ifndef PERL_STRICT_CR
12412 if (PL_bufend - PL_linestart >= 2) {
12413 if ((PL_bufend[-2] == '\r' && PL_bufend[-1] == '\n') ||
12414 (PL_bufend[-2] == '\n' && PL_bufend[-1] == '\r'))
12416 PL_bufend[-2] = '\n';
12418 SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
12420 else if (PL_bufend[-1] == '\r')
12421 PL_bufend[-1] = '\n';
12423 else if (PL_bufend - PL_linestart == 1 && PL_bufend[-1] == '\r')
12424 PL_bufend[-1] = '\n';
12426 if (*s == term && memEQ(s,PL_tokenbuf,len)) {
12427 STRLEN off = PL_bufend - 1 - SvPVX_const(PL_linestr);
12428 *(SvPVX(PL_linestr) + off ) = ' ';
12429 sv_catsv(PL_linestr,herewas);
12430 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
12431 s = SvPVX(PL_linestr) + off; /* In case PV of PL_linestr moved. */
12435 sv_catsv(tmpstr,PL_linestr);
12440 PL_multi_end = CopLINE(PL_curcop);
12441 if (SvCUR(tmpstr) + 5 < SvLEN(tmpstr)) {
12442 SvPV_shrink_to_cur(tmpstr);
12444 SvREFCNT_dec(herewas);
12446 if (UTF && is_utf8_string((U8*)SvPVX_const(tmpstr), SvCUR(tmpstr)))
12448 else if (PL_encoding)
12449 sv_recode_to_utf8(tmpstr, PL_encoding);
12451 PL_lex_stuff = tmpstr;
12452 pl_yylval.ival = op_type;
12456 /* scan_inputsymbol
12457 takes: current position in input buffer
12458 returns: new position in input buffer
12459 side-effects: pl_yylval and lex_op are set.
12464 <FH> read from filehandle
12465 <pkg::FH> read from package qualified filehandle
12466 <pkg'FH> read from package qualified filehandle
12467 <$fh> read from filehandle in $fh
12468 <*.h> filename glob
12473 S_scan_inputsymbol(pTHX_ char *start)
12476 register char *s = start; /* current position in buffer */
12479 char *d = PL_tokenbuf; /* start of temp holding space */
12480 const char * const e = PL_tokenbuf + sizeof PL_tokenbuf; /* end of temp holding space */
12482 PERL_ARGS_ASSERT_SCAN_INPUTSYMBOL;
12484 end = strchr(s, '\n');
12487 s = delimcpy(d, e, s + 1, end, '>', &len); /* extract until > */
12489 /* die if we didn't have space for the contents of the <>,
12490 or if it didn't end, or if we see a newline
12493 if (len >= (I32)sizeof PL_tokenbuf)
12494 Perl_croak(aTHX_ "Excessively long <> operator");
12496 Perl_croak(aTHX_ "Unterminated <> operator");
12501 Remember, only scalar variables are interpreted as filehandles by
12502 this code. Anything more complex (e.g., <$fh{$num}>) will be
12503 treated as a glob() call.
12504 This code makes use of the fact that except for the $ at the front,
12505 a scalar variable and a filehandle look the same.
12507 if (*d == '$' && d[1]) d++;
12509 /* allow <Pkg'VALUE> or <Pkg::VALUE> */
12510 while (*d && (isALNUM_lazy_if(d,UTF) || *d == '\'' || *d == ':'))
12513 /* If we've tried to read what we allow filehandles to look like, and
12514 there's still text left, then it must be a glob() and not a getline.
12515 Use scan_str to pull out the stuff between the <> and treat it
12516 as nothing more than a string.
12519 if (d - PL_tokenbuf != len) {
12520 pl_yylval.ival = OP_GLOB;
12521 s = scan_str(start,!!PL_madskills,FALSE);
12523 Perl_croak(aTHX_ "Glob not terminated");
12527 bool readline_overriden = FALSE;
12530 /* we're in a filehandle read situation */
12533 /* turn <> into <ARGV> */
12535 Copy("ARGV",d,5,char);
12537 /* Check whether readline() is overriden */
12538 gv_readline = gv_fetchpvs("readline", GV_NOTQUAL, SVt_PVCV);
12540 && GvCVu(gv_readline) && GvIMPORTED_CV(gv_readline))
12542 ((gvp = (GV**)hv_fetchs(PL_globalstash, "readline", FALSE))
12543 && (gv_readline = *gvp) && isGV_with_GP(gv_readline)
12544 && GvCVu(gv_readline) && GvIMPORTED_CV(gv_readline)))
12545 readline_overriden = TRUE;
12547 /* if <$fh>, create the ops to turn the variable into a
12551 /* try to find it in the pad for this block, otherwise find
12552 add symbol table ops
12554 const PADOFFSET tmp = pad_findmy(d, len, 0);
12555 if (tmp != NOT_IN_PAD) {
12556 if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
12557 HV * const stash = PAD_COMPNAME_OURSTASH(tmp);
12558 HEK * const stashname = HvNAME_HEK(stash);
12559 SV * const sym = sv_2mortal(newSVhek(stashname));
12560 sv_catpvs(sym, "::");
12561 sv_catpv(sym, d+1);
12566 OP * const o = newOP(OP_PADSV, 0);
12568 PL_lex_op = readline_overriden
12569 ? (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
12570 op_append_elem(OP_LIST, o,
12571 newCVREF(0, newGVOP(OP_GV,0,gv_readline))))
12572 : (OP*)newUNOP(OP_READLINE, 0, o);
12581 ? (GV_ADDMULTI | GV_ADDINEVAL)
12584 PL_lex_op = readline_overriden
12585 ? (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
12586 op_append_elem(OP_LIST,
12587 newUNOP(OP_RV2SV, 0, newGVOP(OP_GV, 0, gv)),
12588 newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
12589 : (OP*)newUNOP(OP_READLINE, 0,
12590 newUNOP(OP_RV2SV, 0,
12591 newGVOP(OP_GV, 0, gv)));
12593 if (!readline_overriden)
12594 PL_lex_op->op_flags |= OPf_SPECIAL;
12595 /* we created the ops in PL_lex_op, so make pl_yylval.ival a null op */
12596 pl_yylval.ival = OP_NULL;
12599 /* If it's none of the above, it must be a literal filehandle
12600 (<Foo::BAR> or <FOO>) so build a simple readline OP */
12602 GV * const gv = gv_fetchpv(d, GV_ADD, SVt_PVIO);
12603 PL_lex_op = readline_overriden
12604 ? (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
12605 op_append_elem(OP_LIST,
12606 newGVOP(OP_GV, 0, gv),
12607 newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
12608 : (OP*)newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, gv));
12609 pl_yylval.ival = OP_NULL;
12618 takes: start position in buffer
12619 keep_quoted preserve \ on the embedded delimiter(s)
12620 keep_delims preserve the delimiters around the string
12621 returns: position to continue reading from buffer
12622 side-effects: multi_start, multi_close, lex_repl or lex_stuff, and
12623 updates the read buffer.
12625 This subroutine pulls a string out of the input. It is called for:
12626 q single quotes q(literal text)
12627 ' single quotes 'literal text'
12628 qq double quotes qq(interpolate $here please)
12629 " double quotes "interpolate $here please"
12630 qx backticks qx(/bin/ls -l)
12631 ` backticks `/bin/ls -l`
12632 qw quote words @EXPORT_OK = qw( func() $spam )
12633 m// regexp match m/this/
12634 s/// regexp substitute s/this/that/
12635 tr/// string transliterate tr/this/that/
12636 y/// string transliterate y/this/that/
12637 ($*@) sub prototypes sub foo ($)
12638 (stuff) sub attr parameters sub foo : attr(stuff)
12639 <> readline or globs <FOO>, <>, <$fh>, or <*.c>
12641 In most of these cases (all but <>, patterns and transliterate)
12642 yylex() calls scan_str(). m// makes yylex() call scan_pat() which
12643 calls scan_str(). s/// makes yylex() call scan_subst() which calls
12644 scan_str(). tr/// and y/// make yylex() call scan_trans() which
12647 It skips whitespace before the string starts, and treats the first
12648 character as the delimiter. If the delimiter is one of ([{< then
12649 the corresponding "close" character )]}> is used as the closing
12650 delimiter. It allows quoting of delimiters, and if the string has
12651 balanced delimiters ([{<>}]) it allows nesting.
12653 On success, the SV with the resulting string is put into lex_stuff or,
12654 if that is already non-NULL, into lex_repl. The second case occurs only
12655 when parsing the RHS of the special constructs s/// and tr/// (y///).
12656 For convenience, the terminating delimiter character is stuffed into
12661 S_scan_str(pTHX_ char *start, int keep_quoted, int keep_delims)
12664 SV *sv; /* scalar value: string */
12665 const char *tmps; /* temp string, used for delimiter matching */
12666 register char *s = start; /* current position in the buffer */
12667 register char term; /* terminating character */
12668 register char *to; /* current position in the sv's data */
12669 I32 brackets = 1; /* bracket nesting level */
12670 bool has_utf8 = FALSE; /* is there any utf8 content? */
12671 I32 termcode; /* terminating char. code */
12672 U8 termstr[UTF8_MAXBYTES]; /* terminating string */
12673 STRLEN termlen; /* length of terminating string */
12674 int last_off = 0; /* last position for nesting bracket */
12680 PERL_ARGS_ASSERT_SCAN_STR;
12682 /* skip space before the delimiter */
12688 if (PL_realtokenstart >= 0) {
12689 stuffstart = PL_realtokenstart;
12690 PL_realtokenstart = -1;
12693 stuffstart = start - SvPVX(PL_linestr);
12695 /* mark where we are, in case we need to report errors */
12698 /* after skipping whitespace, the next character is the terminator */
12701 termcode = termstr[0] = term;
12705 termcode = utf8_to_uvchr((U8*)s, &termlen);
12706 Copy(s, termstr, termlen, U8);
12707 if (!UTF8_IS_INVARIANT(term))
12711 /* mark where we are */
12712 PL_multi_start = CopLINE(PL_curcop);
12713 PL_multi_open = term;
12715 /* find corresponding closing delimiter */
12716 if (term && (tmps = strchr("([{< )]}> )]}>",term)))
12717 termcode = termstr[0] = term = tmps[5];
12719 PL_multi_close = term;
12721 /* create a new SV to hold the contents. 79 is the SV's initial length.
12722 What a random number. */
12723 sv = newSV_type(SVt_PVIV);
12725 SvIV_set(sv, termcode);
12726 (void)SvPOK_only(sv); /* validate pointer */
12728 /* move past delimiter and try to read a complete string */
12730 sv_catpvn(sv, s, termlen);
12733 tstart = SvPVX(PL_linestr) + stuffstart;
12734 if (!PL_thisopen && !keep_delims) {
12735 PL_thisopen = newSVpvn(tstart, s - tstart);
12736 stuffstart = s - SvPVX(PL_linestr);
12740 if (PL_encoding && !UTF) {
12744 int offset = s - SvPVX_const(PL_linestr);
12745 const bool found = sv_cat_decode(sv, PL_encoding, PL_linestr,
12746 &offset, (char*)termstr, termlen);
12747 const char * const ns = SvPVX_const(PL_linestr) + offset;
12748 char * const svlast = SvEND(sv) - 1;
12750 for (; s < ns; s++) {
12751 if (*s == '\n' && !PL_rsfp)
12752 CopLINE_inc(PL_curcop);
12755 goto read_more_line;
12757 /* handle quoted delimiters */
12758 if (SvCUR(sv) > 1 && *(svlast-1) == '\\') {
12760 for (t = svlast-2; t >= SvPVX_const(sv) && *t == '\\';)
12762 if ((svlast-1 - t) % 2) {
12763 if (!keep_quoted) {
12764 *(svlast-1) = term;
12766 SvCUR_set(sv, SvCUR(sv) - 1);
12771 if (PL_multi_open == PL_multi_close) {
12777 for (t = w = SvPVX(sv)+last_off; t < svlast; w++, t++) {
12778 /* At here, all closes are "was quoted" one,
12779 so we don't check PL_multi_close. */
12781 if (!keep_quoted && *(t+1) == PL_multi_open)
12786 else if (*t == PL_multi_open)
12794 SvCUR_set(sv, w - SvPVX_const(sv));
12796 last_off = w - SvPVX(sv);
12797 if (--brackets <= 0)
12802 if (!keep_delims) {
12803 SvCUR_set(sv, SvCUR(sv) - 1);
12809 /* extend sv if need be */
12810 SvGROW(sv, SvCUR(sv) + (PL_bufend - s) + 1);
12811 /* set 'to' to the next character in the sv's string */
12812 to = SvPVX(sv)+SvCUR(sv);
12814 /* if open delimiter is the close delimiter read unbridle */
12815 if (PL_multi_open == PL_multi_close) {
12816 for (; s < PL_bufend; s++,to++) {
12817 /* embedded newlines increment the current line number */
12818 if (*s == '\n' && !PL_rsfp)
12819 CopLINE_inc(PL_curcop);
12820 /* handle quoted delimiters */
12821 if (*s == '\\' && s+1 < PL_bufend && term != '\\') {
12822 if (!keep_quoted && s[1] == term)
12824 /* any other quotes are simply copied straight through */
12828 /* terminate when run out of buffer (the for() condition), or
12829 have found the terminator */
12830 else if (*s == term) {
12833 if (s+termlen <= PL_bufend && memEQ(s, (char*)termstr, termlen))
12836 else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF)
12842 /* if the terminator isn't the same as the start character (e.g.,
12843 matched brackets), we have to allow more in the quoting, and
12844 be prepared for nested brackets.
12847 /* read until we run out of string, or we find the terminator */
12848 for (; s < PL_bufend; s++,to++) {
12849 /* embedded newlines increment the line count */
12850 if (*s == '\n' && !PL_rsfp)
12851 CopLINE_inc(PL_curcop);
12852 /* backslashes can escape the open or closing characters */
12853 if (*s == '\\' && s+1 < PL_bufend) {
12854 if (!keep_quoted &&
12855 ((s[1] == PL_multi_open) || (s[1] == PL_multi_close)))
12860 /* allow nested opens and closes */
12861 else if (*s == PL_multi_close && --brackets <= 0)
12863 else if (*s == PL_multi_open)
12865 else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF)
12870 /* terminate the copied string and update the sv's end-of-string */
12872 SvCUR_set(sv, to - SvPVX_const(sv));
12875 * this next chunk reads more into the buffer if we're not done yet
12879 break; /* handle case where we are done yet :-) */
12881 #ifndef PERL_STRICT_CR
12882 if (to - SvPVX_const(sv) >= 2) {
12883 if ((to[-2] == '\r' && to[-1] == '\n') ||
12884 (to[-2] == '\n' && to[-1] == '\r'))
12888 SvCUR_set(sv, to - SvPVX_const(sv));
12890 else if (to[-1] == '\r')
12893 else if (to - SvPVX_const(sv) == 1 && to[-1] == '\r')
12898 /* if we're out of file, or a read fails, bail and reset the current
12899 line marker so we can report where the unterminated string began
12902 if (PL_madskills) {
12903 char * const tstart = SvPVX(PL_linestr) + stuffstart;
12905 sv_catpvn(PL_thisstuff, tstart, PL_bufend - tstart);
12907 PL_thisstuff = newSVpvn(tstart, PL_bufend - tstart);
12910 CopLINE_inc(PL_curcop);
12911 PL_bufptr = PL_bufend;
12912 if (!lex_next_chunk(0)) {
12914 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
12923 /* at this point, we have successfully read the delimited string */
12925 if (!PL_encoding || UTF) {
12927 if (PL_madskills) {
12928 char * const tstart = SvPVX(PL_linestr) + stuffstart;
12929 const int len = s - tstart;
12931 sv_catpvn(PL_thisstuff, tstart, len);
12933 PL_thisstuff = newSVpvn(tstart, len);
12934 if (!PL_thisclose && !keep_delims)
12935 PL_thisclose = newSVpvn(s,termlen);
12940 sv_catpvn(sv, s, termlen);
12945 if (PL_madskills) {
12946 char * const tstart = SvPVX(PL_linestr) + stuffstart;
12947 const int len = s - tstart - termlen;
12949 sv_catpvn(PL_thisstuff, tstart, len);
12951 PL_thisstuff = newSVpvn(tstart, len);
12952 if (!PL_thisclose && !keep_delims)
12953 PL_thisclose = newSVpvn(s - termlen,termlen);
12957 if (has_utf8 || PL_encoding)
12960 PL_multi_end = CopLINE(PL_curcop);
12962 /* if we allocated too much space, give some back */
12963 if (SvCUR(sv) + 5 < SvLEN(sv)) {
12964 SvLEN_set(sv, SvCUR(sv) + 1);
12965 SvPV_renew(sv, SvLEN(sv));
12968 /* decide whether this is the first or second quoted string we've read
12981 takes: pointer to position in buffer
12982 returns: pointer to new position in buffer
12983 side-effects: builds ops for the constant in pl_yylval.op
12985 Read a number in any of the formats that Perl accepts:
12987 \d(_?\d)*(\.(\d(_?\d)*)?)?[Ee][\+\-]?(\d(_?\d)*) 12 12.34 12.
12988 \.\d(_?\d)*[Ee][\+\-]?(\d(_?\d)*) .34
12991 0x[0-9A-Fa-f](_?[0-9A-Fa-f])*
12993 Like most scan_ routines, it uses the PL_tokenbuf buffer to hold the
12996 If it reads a number without a decimal point or an exponent, it will
12997 try converting the number to an integer and see if it can do so
12998 without loss of precision.
13002 Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
13005 register const char *s = start; /* current position in buffer */
13006 register char *d; /* destination in temp buffer */
13007 register char *e; /* end of temp buffer */
13008 NV nv; /* number read, as a double */
13009 SV *sv = NULL; /* place to put the converted number */
13010 bool floatit; /* boolean: int or float? */
13011 const char *lastub = NULL; /* position of last underbar */
13012 static char const number_too_long[] = "Number too long";
13014 PERL_ARGS_ASSERT_SCAN_NUM;
13016 /* We use the first character to decide what type of number this is */
13020 Perl_croak(aTHX_ "panic: scan_num");
13022 /* if it starts with a 0, it could be an octal number, a decimal in
13023 0.13 disguise, or a hexadecimal number, or a binary number. */
13027 u holds the "number so far"
13028 shift the power of 2 of the base
13029 (hex == 4, octal == 3, binary == 1)
13030 overflowed was the number more than we can hold?
13032 Shift is used when we add a digit. It also serves as an "are
13033 we in octal/hex/binary?" indicator to disallow hex characters
13034 when in octal mode.
13039 bool overflowed = FALSE;
13040 bool just_zero = TRUE; /* just plain 0 or binary number? */
13041 static const NV nvshift[5] = { 1.0, 2.0, 4.0, 8.0, 16.0 };
13042 static const char* const bases[5] =
13043 { "", "binary", "", "octal", "hexadecimal" };
13044 static const char* const Bases[5] =
13045 { "", "Binary", "", "Octal", "Hexadecimal" };
13046 static const char* const maxima[5] =
13048 "0b11111111111111111111111111111111",
13052 const char *base, *Base, *max;
13054 /* check for hex */
13055 if (s[1] == 'x' || s[1] == 'X') {
13059 } else if (s[1] == 'b' || s[1] == 'B') {
13064 /* check for a decimal in disguise */
13065 else if (s[1] == '.' || s[1] == 'e' || s[1] == 'E')
13067 /* so it must be octal */
13074 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13075 "Misplaced _ in number");
13079 base = bases[shift];
13080 Base = Bases[shift];
13081 max = maxima[shift];
13083 /* read the rest of the number */
13085 /* x is used in the overflow test,
13086 b is the digit we're adding on. */
13091 /* if we don't mention it, we're done */
13095 /* _ are ignored -- but warned about if consecutive */
13097 if (lastub && s == lastub + 1)
13098 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13099 "Misplaced _ in number");
13103 /* 8 and 9 are not octal */
13104 case '8': case '9':
13106 yyerror(Perl_form(aTHX_ "Illegal octal digit '%c'", *s));
13110 case '2': case '3': case '4':
13111 case '5': case '6': case '7':
13113 yyerror(Perl_form(aTHX_ "Illegal binary digit '%c'", *s));
13116 case '0': case '1':
13117 b = *s++ & 15; /* ASCII digit -> value of digit */
13121 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
13122 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
13123 /* make sure they said 0x */
13126 b = (*s++ & 7) + 9;
13128 /* Prepare to put the digit we have onto the end
13129 of the number so far. We check for overflows.
13135 x = u << shift; /* make room for the digit */
13137 if ((x >> shift) != u
13138 && !(PL_hints & HINT_NEW_BINARY)) {
13141 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
13142 "Integer overflow in %s number",
13145 u = x | b; /* add the digit to the end */
13148 n *= nvshift[shift];
13149 /* If an NV has not enough bits in its
13150 * mantissa to represent an UV this summing of
13151 * small low-order numbers is a waste of time
13152 * (because the NV cannot preserve the
13153 * low-order bits anyway): we could just
13154 * remember when did we overflow and in the
13155 * end just multiply n by the right
13163 /* if we get here, we had success: make a scalar value from
13168 /* final misplaced underbar check */
13169 if (s[-1] == '_') {
13170 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Misplaced _ in number");
13174 if (n > 4294967295.0)
13175 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
13176 "%s number > %s non-portable",
13182 if (u > 0xffffffff)
13183 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
13184 "%s number > %s non-portable",
13189 if (just_zero && (PL_hints & HINT_NEW_INTEGER))
13190 sv = new_constant(start, s - start, "integer",
13191 sv, NULL, NULL, 0);
13192 else if (PL_hints & HINT_NEW_BINARY)
13193 sv = new_constant(start, s - start, "binary", sv, NULL, NULL, 0);
13198 handle decimal numbers.
13199 we're also sent here when we read a 0 as the first digit
13201 case '1': case '2': case '3': case '4': case '5':
13202 case '6': case '7': case '8': case '9': case '.':
13205 e = PL_tokenbuf + sizeof PL_tokenbuf - 6; /* room for various punctuation */
13208 /* read next group of digits and _ and copy into d */
13209 while (isDIGIT(*s) || *s == '_') {
13210 /* skip underscores, checking for misplaced ones
13214 if (lastub && s == lastub + 1)
13215 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13216 "Misplaced _ in number");
13220 /* check for end of fixed-length buffer */
13222 Perl_croak(aTHX_ number_too_long);
13223 /* if we're ok, copy the character */
13228 /* final misplaced underbar check */
13229 if (lastub && s == lastub + 1) {
13230 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Misplaced _ in number");
13233 /* read a decimal portion if there is one. avoid
13234 3..5 being interpreted as the number 3. followed
13237 if (*s == '.' && s[1] != '.') {
13242 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13243 "Misplaced _ in number");
13247 /* copy, ignoring underbars, until we run out of digits.
13249 for (; isDIGIT(*s) || *s == '_'; s++) {
13250 /* fixed length buffer check */
13252 Perl_croak(aTHX_ number_too_long);
13254 if (lastub && s == lastub + 1)
13255 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13256 "Misplaced _ in number");
13262 /* fractional part ending in underbar? */
13263 if (s[-1] == '_') {
13264 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13265 "Misplaced _ in number");
13267 if (*s == '.' && isDIGIT(s[1])) {
13268 /* oops, it's really a v-string, but without the "v" */
13274 /* read exponent part, if present */
13275 if ((*s == 'e' || *s == 'E') && strchr("+-0123456789_", s[1])) {
13279 /* regardless of whether user said 3E5 or 3e5, use lower 'e' */
13280 *d++ = 'e'; /* At least some Mach atof()s don't grok 'E' */
13282 /* stray preinitial _ */
13284 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13285 "Misplaced _ in number");
13289 /* allow positive or negative exponent */
13290 if (*s == '+' || *s == '-')
13293 /* stray initial _ */
13295 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13296 "Misplaced _ in number");
13300 /* read digits of exponent */
13301 while (isDIGIT(*s) || *s == '_') {
13304 Perl_croak(aTHX_ number_too_long);
13308 if (((lastub && s == lastub + 1) ||
13309 (!isDIGIT(s[1]) && s[1] != '_')))
13310 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13311 "Misplaced _ in number");
13319 We try to do an integer conversion first if no characters
13320 indicating "float" have been found.
13325 const int flags = grok_number (PL_tokenbuf, d - PL_tokenbuf, &uv);
13327 if (flags == IS_NUMBER_IN_UV) {
13329 sv = newSViv(uv); /* Prefer IVs over UVs. */
13332 } else if (flags == (IS_NUMBER_IN_UV | IS_NUMBER_NEG)) {
13333 if (uv <= (UV) IV_MIN)
13334 sv = newSViv(-(IV)uv);
13341 /* terminate the string */
13343 nv = Atof(PL_tokenbuf);
13348 ? (PL_hints & HINT_NEW_FLOAT) : (PL_hints & HINT_NEW_INTEGER) ) {
13349 const char *const key = floatit ? "float" : "integer";
13350 const STRLEN keylen = floatit ? 5 : 7;
13351 sv = S_new_constant(aTHX_ PL_tokenbuf, d - PL_tokenbuf,
13352 key, keylen, sv, NULL, NULL, 0);
13356 /* if it starts with a v, it could be a v-string */
13359 sv = newSV(5); /* preallocate storage space */
13360 s = scan_vstring(s, PL_bufend, sv);
13364 /* make the op for the constant and return */
13367 lvalp->opval = newSVOP(OP_CONST, 0, sv);
13369 lvalp->opval = NULL;
13375 S_scan_formline(pTHX_ register char *s)
13378 register char *eol;
13380 SV * const stuff = newSVpvs("");
13381 bool needargs = FALSE;
13382 bool eofmt = FALSE;
13384 char *tokenstart = s;
13385 SV* savewhite = NULL;
13387 if (PL_madskills) {
13388 savewhite = PL_thiswhite;
13393 PERL_ARGS_ASSERT_SCAN_FORMLINE;
13395 while (!needargs) {
13398 #ifdef PERL_STRICT_CR
13399 while (SPACE_OR_TAB(*t))
13402 while (SPACE_OR_TAB(*t) || *t == '\r')
13405 if (*t == '\n' || t == PL_bufend) {
13410 if (PL_in_eval && !PL_rsfp) {
13411 eol = (char *) memchr(s,'\n',PL_bufend-s);
13416 eol = PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
13418 for (t = s; t < eol; t++) {
13419 if (*t == '~' && t[1] == '~' && SvCUR(stuff)) {
13421 goto enough; /* ~~ must be first line in formline */
13423 if (*t == '@' || *t == '^')
13427 sv_catpvn(stuff, s, eol-s);
13428 #ifndef PERL_STRICT_CR
13429 if (eol-s > 1 && eol[-2] == '\r' && eol[-1] == '\n') {
13430 char *end = SvPVX(stuff) + SvCUR(stuff);
13433 SvCUR_set(stuff, SvCUR(stuff) - 1);
13444 if (PL_madskills) {
13446 sv_catpvn(PL_thistoken, tokenstart, PL_bufend - tokenstart);
13448 PL_thistoken = newSVpvn(tokenstart, PL_bufend - tokenstart);
13451 PL_bufptr = PL_bufend;
13452 CopLINE_inc(PL_curcop);
13453 got_some = lex_next_chunk(0);
13454 CopLINE_dec(PL_curcop);
13457 tokenstart = PL_bufptr;
13465 if (SvCUR(stuff)) {
13468 PL_lex_state = LEX_NORMAL;
13469 start_force(PL_curforce);
13470 NEXTVAL_NEXTTOKE.ival = 0;
13474 PL_lex_state = LEX_FORMLINE;
13476 if (UTF && is_utf8_string((U8*)SvPVX_const(stuff), SvCUR(stuff)))
13478 else if (PL_encoding)
13479 sv_recode_to_utf8(stuff, PL_encoding);
13481 start_force(PL_curforce);
13482 NEXTVAL_NEXTTOKE.opval = (OP*)newSVOP(OP_CONST, 0, stuff);
13484 start_force(PL_curforce);
13485 NEXTVAL_NEXTTOKE.ival = OP_FORMLINE;
13489 SvREFCNT_dec(stuff);
13491 PL_lex_formbrack = 0;
13495 if (PL_madskills) {
13497 sv_catpvn(PL_thistoken, tokenstart, s - tokenstart);
13499 PL_thistoken = newSVpvn(tokenstart, s - tokenstart);
13500 PL_thiswhite = savewhite;
13507 Perl_start_subparse(pTHX_ I32 is_format, U32 flags)
13510 const I32 oldsavestack_ix = PL_savestack_ix;
13511 CV* const outsidecv = PL_compcv;
13514 assert(SvTYPE(PL_compcv) == SVt_PVCV);
13516 SAVEI32(PL_subline);
13517 save_item(PL_subname);
13518 SAVESPTR(PL_compcv);
13520 PL_compcv = MUTABLE_CV(newSV_type(is_format ? SVt_PVFM : SVt_PVCV));
13521 CvFLAGS(PL_compcv) |= flags;
13523 PL_subline = CopLINE(PL_curcop);
13524 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE|padnew_SAVESUB);
13525 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outsidecv));
13526 CvOUTSIDE_SEQ(PL_compcv) = PL_cop_seqmax;
13528 return oldsavestack_ix;
13532 #pragma segment Perl_yylex
13535 S_yywarn(pTHX_ const char *const s)
13539 PERL_ARGS_ASSERT_YYWARN;
13541 PL_in_eval |= EVAL_WARNONLY;
13543 PL_in_eval &= ~EVAL_WARNONLY;
13548 Perl_yyerror(pTHX_ const char *const s)
13551 const char *where = NULL;
13552 const char *context = NULL;
13555 int yychar = PL_parser->yychar;
13557 PERL_ARGS_ASSERT_YYERROR;
13559 if (!yychar || (yychar == ';' && !PL_rsfp))
13561 else if (PL_oldoldbufptr && PL_bufptr > PL_oldoldbufptr &&
13562 PL_bufptr - PL_oldoldbufptr < 200 && PL_oldoldbufptr != PL_oldbufptr &&
13563 PL_oldbufptr != PL_bufptr) {
13566 The code below is removed for NetWare because it abends/crashes on NetWare
13567 when the script has error such as not having the closing quotes like:
13568 if ($var eq "value)
13569 Checking of white spaces is anyway done in NetWare code.
13572 while (isSPACE(*PL_oldoldbufptr))
13575 context = PL_oldoldbufptr;
13576 contlen = PL_bufptr - PL_oldoldbufptr;
13578 else if (PL_oldbufptr && PL_bufptr > PL_oldbufptr &&
13579 PL_bufptr - PL_oldbufptr < 200 && PL_oldbufptr != PL_bufptr) {
13582 The code below is removed for NetWare because it abends/crashes on NetWare
13583 when the script has error such as not having the closing quotes like:
13584 if ($var eq "value)
13585 Checking of white spaces is anyway done in NetWare code.
13588 while (isSPACE(*PL_oldbufptr))
13591 context = PL_oldbufptr;
13592 contlen = PL_bufptr - PL_oldbufptr;
13594 else if (yychar > 255)
13595 where = "next token ???";
13596 else if (yychar == -2) { /* YYEMPTY */
13597 if (PL_lex_state == LEX_NORMAL ||
13598 (PL_lex_state == LEX_KNOWNEXT && PL_lex_defer == LEX_NORMAL))
13599 where = "at end of line";
13600 else if (PL_lex_inpat)
13601 where = "within pattern";
13603 where = "within string";
13606 SV * const where_sv = newSVpvs_flags("next char ", SVs_TEMP);
13608 Perl_sv_catpvf(aTHX_ where_sv, "^%c", toCTRL(yychar));
13609 else if (isPRINT_LC(yychar)) {
13610 const char string = yychar;
13611 sv_catpvn(where_sv, &string, 1);
13614 Perl_sv_catpvf(aTHX_ where_sv, "\\%03o", yychar & 255);
13615 where = SvPVX_const(where_sv);
13617 msg = sv_2mortal(newSVpv(s, 0));
13618 Perl_sv_catpvf(aTHX_ msg, " at %s line %"IVdf", ",
13619 OutCopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
13621 Perl_sv_catpvf(aTHX_ msg, "near \"%.*s\"\n", contlen, context);
13623 Perl_sv_catpvf(aTHX_ msg, "%s\n", where);
13624 if (PL_multi_start < PL_multi_end && (U32)(CopLINE(PL_curcop) - PL_multi_end) <= 1) {
13625 Perl_sv_catpvf(aTHX_ msg,
13626 " (Might be a runaway multi-line %c%c string starting on line %"IVdf")\n",
13627 (int)PL_multi_open,(int)PL_multi_close,(IV)PL_multi_start);
13630 if (PL_in_eval & EVAL_WARNONLY) {
13631 Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX), "%"SVf, SVfARG(msg));
13635 if (PL_error_count >= 10) {
13636 if (PL_in_eval && SvCUR(ERRSV))
13637 Perl_croak(aTHX_ "%"SVf"%s has too many errors.\n",
13638 SVfARG(ERRSV), OutCopFILE(PL_curcop));
13640 Perl_croak(aTHX_ "%s has too many errors.\n",
13641 OutCopFILE(PL_curcop));
13644 PL_in_my_stash = NULL;
13648 #pragma segment Main
13652 S_swallow_bom(pTHX_ U8 *s)
13655 const STRLEN slen = SvCUR(PL_linestr);
13657 PERL_ARGS_ASSERT_SWALLOW_BOM;
13661 if (s[1] == 0xFE) {
13662 /* UTF-16 little-endian? (or UTF-32LE?) */
13663 if (s[2] == 0 && s[3] == 0) /* UTF-32 little-endian */
13664 Perl_croak(aTHX_ "Unsupported script encoding UTF-32LE");
13665 #ifndef PERL_NO_UTF16_FILTER
13666 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (BOM)\n");
13668 if (PL_bufend > (char*)s) {
13669 s = add_utf16_textfilter(s, TRUE);
13672 Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
13677 if (s[1] == 0xFF) { /* UTF-16 big-endian? */
13678 #ifndef PERL_NO_UTF16_FILTER
13679 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (BOM)\n");
13681 if (PL_bufend > (char *)s) {
13682 s = add_utf16_textfilter(s, FALSE);
13685 Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
13690 if (slen > 2 && s[1] == 0xBB && s[2] == 0xBF) {
13691 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-8 script encoding (BOM)\n");
13692 s += 3; /* UTF-8 */
13698 if (s[2] == 0xFE && s[3] == 0xFF) {
13699 /* UTF-32 big-endian */
13700 Perl_croak(aTHX_ "Unsupported script encoding UTF-32BE");
13703 else if (s[2] == 0 && s[3] != 0) {
13706 * are a good indicator of UTF-16BE. */
13707 #ifndef PERL_NO_UTF16_FILTER
13708 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (no BOM)\n");
13709 s = add_utf16_textfilter(s, FALSE);
13711 Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
13717 if (slen > 3 && s[1] == 0x73 && s[2] == 0x66 && s[3] == 0x73) {
13718 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-8 script encoding (BOM)\n");
13719 s += 4; /* UTF-8 */
13725 if (slen > 3 && s[1] == 0 && s[2] != 0 && s[3] == 0) {
13728 * are a good indicator of UTF-16LE. */
13729 #ifndef PERL_NO_UTF16_FILTER
13730 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (no BOM)\n");
13731 s = add_utf16_textfilter(s, TRUE);
13733 Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
13741 #ifndef PERL_NO_UTF16_FILTER
13743 S_utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen)
13746 SV *const filter = FILTER_DATA(idx);
13747 /* We re-use this each time round, throwing the contents away before we
13749 SV *const utf16_buffer = MUTABLE_SV(IoTOP_GV(filter));
13750 SV *const utf8_buffer = filter;
13751 IV status = IoPAGE(filter);
13752 const bool reverse = cBOOL(IoLINES(filter));
13755 PERL_ARGS_ASSERT_UTF16_TEXTFILTER;
13757 /* As we're automatically added, at the lowest level, and hence only called
13758 from this file, we can be sure that we're not called in block mode. Hence
13759 don't bother writing code to deal with block mode. */
13761 Perl_croak(aTHX_ "panic: utf16_textfilter called in block mode (for %d characters)", maxlen);
13764 Perl_croak(aTHX_ "panic: utf16_textfilter called after error (status=%"IVdf")", status);
13766 DEBUG_P(PerlIO_printf(Perl_debug_log,
13767 "utf16_textfilter(%p,%ce): idx=%d maxlen=%d status=%"IVdf" utf16=%"UVuf" utf8=%"UVuf"\n",
13768 FPTR2DPTR(void *, S_utf16_textfilter),
13769 reverse ? 'l' : 'b', idx, maxlen, status,
13770 (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
13777 /* First, look in our buffer of existing UTF-8 data: */
13778 char *nl = (char *)memchr(SvPVX(utf8_buffer), '\n', SvCUR(utf8_buffer));
13782 } else if (status == 0) {
13784 IoPAGE(filter) = 0;
13785 nl = SvEND(utf8_buffer);
13788 STRLEN got = nl - SvPVX(utf8_buffer);
13789 /* Did we have anything to append? */
13791 sv_catpvn(sv, SvPVX(utf8_buffer), got);
13792 /* Everything else in this code works just fine if SVp_POK isn't
13793 set. This, however, needs it, and we need it to work, else
13794 we loop infinitely because the buffer is never consumed. */
13795 sv_chop(utf8_buffer, nl);
13799 /* OK, not a complete line there, so need to read some more UTF-16.
13800 Read an extra octect if the buffer currently has an odd number. */
13804 if (SvCUR(utf16_buffer) >= 2) {
13805 /* Location of the high octet of the last complete code point.
13806 Gosh, UTF-16 is a pain. All the benefits of variable length,
13807 *coupled* with all the benefits of partial reads and
13809 const U8 *const last_hi = (U8*)SvPVX(utf16_buffer)
13810 + ((SvCUR(utf16_buffer) & ~1) - (reverse ? 1 : 2));
13812 if (*last_hi < 0xd8 || *last_hi > 0xdb) {
13816 /* We have the first half of a surrogate. Read more. */
13817 DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter partial surrogate detected at %p\n", last_hi));
13820 status = FILTER_READ(idx + 1, utf16_buffer,
13821 160 + (SvCUR(utf16_buffer) & 1));
13822 DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter status=%"IVdf" SvCUR(sv)=%"UVuf"\n", status, (UV)SvCUR(utf16_buffer)));
13823 DEBUG_P({ sv_dump(utf16_buffer); sv_dump(utf8_buffer);});
13826 IoPAGE(filter) = status;
13831 chars = SvCUR(utf16_buffer) >> 1;
13832 have = SvCUR(utf8_buffer);
13833 SvGROW(utf8_buffer, have + chars * 3 + 1);
13836 end = utf16_to_utf8_reversed((U8*)SvPVX(utf16_buffer),
13837 (U8*)SvPVX_const(utf8_buffer) + have,
13838 chars * 2, &newlen);
13840 end = utf16_to_utf8((U8*)SvPVX(utf16_buffer),
13841 (U8*)SvPVX_const(utf8_buffer) + have,
13842 chars * 2, &newlen);
13844 SvCUR_set(utf8_buffer, have + newlen);
13847 /* No need to keep this SV "well-formed" with a '\0' after the end, as
13848 it's private to us, and utf16_to_utf8{,reversed} take a
13849 (pointer,length) pair, rather than a NUL-terminated string. */
13850 if(SvCUR(utf16_buffer) & 1) {
13851 *SvPVX(utf16_buffer) = SvEND(utf16_buffer)[-1];
13852 SvCUR_set(utf16_buffer, 1);
13854 SvCUR_set(utf16_buffer, 0);
13857 DEBUG_P(PerlIO_printf(Perl_debug_log,
13858 "utf16_textfilter: returns, status=%"IVdf" utf16=%"UVuf" utf8=%"UVuf"\n",
13860 (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
13861 DEBUG_P({ sv_dump(utf8_buffer); sv_dump(sv);});
13866 S_add_utf16_textfilter(pTHX_ U8 *const s, bool reversed)
13868 SV *filter = filter_add(S_utf16_textfilter, NULL);
13870 PERL_ARGS_ASSERT_ADD_UTF16_TEXTFILTER;
13872 IoTOP_GV(filter) = MUTABLE_GV(newSVpvn((char *)s, PL_bufend - (char*)s));
13873 sv_setpvs(filter, "");
13874 IoLINES(filter) = reversed;
13875 IoPAGE(filter) = 1; /* Not EOF */
13877 /* Sadly, we have to return a valid pointer, come what may, so we have to
13878 ignore any error return from this. */
13879 SvCUR_set(PL_linestr, 0);
13880 if (FILTER_READ(0, PL_linestr, 0)) {
13881 SvUTF8_on(PL_linestr);
13883 SvUTF8_on(PL_linestr);
13885 PL_bufend = SvEND(PL_linestr);
13886 return (U8*)SvPVX(PL_linestr);
13891 Returns a pointer to the next character after the parsed
13892 vstring, as well as updating the passed in sv.
13894 Function must be called like
13897 s = scan_vstring(s,e,sv);
13899 where s and e are the start and end of the string.
13900 The sv should already be large enough to store the vstring
13901 passed in, for performance reasons.
13906 Perl_scan_vstring(pTHX_ const char *s, const char *const e, SV *sv)
13909 const char *pos = s;
13910 const char *start = s;
13912 PERL_ARGS_ASSERT_SCAN_VSTRING;
13914 if (*pos == 'v') pos++; /* get past 'v' */
13915 while (pos < e && (isDIGIT(*pos) || *pos == '_'))
13917 if ( *pos != '.') {
13918 /* this may not be a v-string if followed by => */
13919 const char *next = pos;
13920 while (next < e && isSPACE(*next))
13922 if ((e - next) >= 2 && *next == '=' && next[1] == '>' ) {
13923 /* return string not v-string */
13924 sv_setpvn(sv,(char *)s,pos-s);
13925 return (char *)pos;
13929 if (!isALPHA(*pos)) {
13930 U8 tmpbuf[UTF8_MAXBYTES+1];
13933 s++; /* get past 'v' */
13938 /* this is atoi() that tolerates underscores */
13941 const char *end = pos;
13943 while (--end >= s) {
13945 const UV orev = rev;
13946 rev += (*end - '0') * mult;
13949 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
13950 "Integer overflow in decimal number");
13954 if (rev > 0x7FFFFFFF)
13955 Perl_croak(aTHX_ "In EBCDIC the v-string components cannot exceed 2147483647");
13957 /* Append native character for the rev point */
13958 tmpend = uvchr_to_utf8(tmpbuf, rev);
13959 sv_catpvn(sv, (const char*)tmpbuf, tmpend - tmpbuf);
13960 if (!UNI_IS_INVARIANT(NATIVE_TO_UNI(rev)))
13962 if (pos + 1 < e && *pos == '.' && isDIGIT(pos[1]))
13968 while (pos < e && (isDIGIT(*pos) || *pos == '_'))
13972 sv_magic(sv,NULL,PERL_MAGIC_vstring,(const char*)start, pos-start);
13979 Perl_keyword_plugin_standard(pTHX_
13980 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
13982 PERL_ARGS_ASSERT_KEYWORD_PLUGIN_STANDARD;
13983 PERL_UNUSED_CONTEXT;
13984 PERL_UNUSED_ARG(keyword_ptr);
13985 PERL_UNUSED_ARG(keyword_len);
13986 PERL_UNUSED_ARG(op_ptr);
13987 return KEYWORD_PLUGIN_DECLINE;
13990 #define parse_recdescent(g) S_parse_recdescent(aTHX_ g)
13992 S_parse_recdescent(pTHX_ int gramtype)
13994 SAVEI32(PL_lex_brackets);
13995 if (PL_lex_brackets > 100)
13996 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
13997 PL_lex_brackstack[PL_lex_brackets++] = XFAKEEOF;
13998 if(yyparse(gramtype) && !PL_parser->error_count)
13999 qerror(Perl_mess(aTHX_ "Parse error"));
14002 #define parse_recdescent_for_op(g) S_parse_recdescent_for_op(aTHX_ g)
14004 S_parse_recdescent_for_op(pTHX_ int gramtype)
14008 SAVEVPTR(PL_eval_root);
14009 PL_eval_root = NULL;
14010 parse_recdescent(gramtype);
14017 =for apidoc Amx|OP *|parse_block|U32 flags
14019 Parse a single complete Perl code block. This consists of an opening
14020 brace, a sequence of statements, and a closing brace. The block
14021 constitutes a lexical scope, so C<my> variables and various compile-time
14022 effects can be contained within it. It is up to the caller to ensure
14023 that the dynamic parser state (L</PL_parser> et al) is correctly set to
14024 reflect the source of the code to be parsed and the lexical context for
14027 The op tree representing the code block is returned. This is always a
14028 real op, never a null pointer. It will normally be a C<lineseq> list,
14029 including C<nextstate> or equivalent ops. No ops to construct any kind
14030 of runtime scope are included by virtue of it being a block.
14032 If an error occurs in parsing or compilation, in most cases a valid op
14033 tree (most likely null) is returned anyway. The error is reflected in
14034 the parser state, normally resulting in a single exception at the top
14035 level of parsing which covers all the compilation errors that occurred.
14036 Some compilation errors, however, will throw an exception immediately.
14038 The I<flags> parameter is reserved for future use, and must always
14045 Perl_parse_block(pTHX_ U32 flags)
14048 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_block");
14049 return parse_recdescent_for_op(GRAMBLOCK);
14053 =for apidoc Amx|OP *|parse_barestmt|U32 flags
14055 Parse a single unadorned Perl statement. This may be a normal imperative
14056 statement or a declaration that has compile-time effect. It does not
14057 include any label or other affixture. It is up to the caller to ensure
14058 that the dynamic parser state (L</PL_parser> et al) is correctly set to
14059 reflect the source of the code to be parsed and the lexical context for
14062 The op tree representing the statement is returned. This may be a
14063 null pointer if the statement is null, for example if it was actually
14064 a subroutine definition (which has compile-time side effects). If not
14065 null, it will be ops directly implementing the statement, suitable to
14066 pass to L</newSTATEOP>. It will not normally include a C<nextstate> or
14067 equivalent op (except for those embedded in a scope contained entirely
14068 within the statement).
14070 If an error occurs in parsing or compilation, in most cases a valid op
14071 tree (most likely null) is returned anyway. The error is reflected in
14072 the parser state, normally resulting in a single exception at the top
14073 level of parsing which covers all the compilation errors that occurred.
14074 Some compilation errors, however, will throw an exception immediately.
14076 The I<flags> parameter is reserved for future use, and must always
14083 Perl_parse_barestmt(pTHX_ U32 flags)
14086 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_barestmt");
14087 return parse_recdescent_for_op(GRAMBARESTMT);
14091 =for apidoc Amx|SV *|parse_label|U32 flags
14093 Parse a single label, possibly optional, of the type that may prefix a
14094 Perl statement. It is up to the caller to ensure that the dynamic parser
14095 state (L</PL_parser> et al) is correctly set to reflect the source of
14096 the code to be parsed. If I<flags> includes C<PARSE_OPTIONAL> then the
14097 label is optional, otherwise it is mandatory.
14099 The name of the label is returned in the form of a fresh scalar. If an
14100 optional label is absent, a null pointer is returned.
14102 If an error occurs in parsing, which can only occur if the label is
14103 mandatory, a valid label is returned anyway. The error is reflected in
14104 the parser state, normally resulting in a single exception at the top
14105 level of parsing which covers all the compilation errors that occurred.
14111 Perl_parse_label(pTHX_ U32 flags)
14113 if (flags & ~PARSE_OPTIONAL)
14114 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_label");
14115 if (PL_lex_state == LEX_KNOWNEXT) {
14116 PL_parser->yychar = yylex();
14117 if (PL_parser->yychar == LABEL) {
14118 char *lpv = pl_yylval.pval;
14119 STRLEN llen = strlen(lpv);
14121 PL_parser->yychar = YYEMPTY;
14122 lsv = newSV_type(SVt_PV);
14123 SvPV_set(lsv, lpv);
14124 SvCUR_set(lsv, llen);
14125 SvLEN_set(lsv, llen+1);
14135 STRLEN wlen, bufptr_pos;
14139 if (!isIDFIRST_A(c))
14143 } while(isWORDCHAR_A(c));
14145 if (word_takes_any_delimeter(s, wlen))
14147 bufptr_pos = s - SvPVX(PL_linestr);
14149 lex_read_space(LEX_KEEP_PREVIOUS);
14151 s = SvPVX(PL_linestr) + bufptr_pos;
14152 if (t[0] == ':' && t[1] != ':') {
14153 PL_oldoldbufptr = PL_oldbufptr;
14156 return newSVpvn(s, wlen);
14160 if (flags & PARSE_OPTIONAL) {
14163 qerror(Perl_mess(aTHX_ "Parse error"));
14164 return newSVpvs("x");
14171 =for apidoc Amx|OP *|parse_fullstmt|U32 flags
14173 Parse a single complete Perl statement. This may be a normal imperative
14174 statement or a declaration that has compile-time effect, and may include
14175 an optional label. It is up to the caller to ensure that the dynamic
14176 parser state (L</PL_parser> et al) is correctly set to reflect the source
14177 of the code to be parsed and the lexical context for the statement.
14179 The op tree representing the statement is returned. This may be a
14180 null pointer if the statement is null, for example if it was actually
14181 a subroutine definition (which has compile-time side effects). If not
14182 null, it will be the result of a L</newSTATEOP> call, normally including
14183 a C<nextstate> or equivalent op.
14185 If an error occurs in parsing or compilation, in most cases a valid op
14186 tree (most likely null) is returned anyway. The error is reflected in
14187 the parser state, normally resulting in a single exception at the top
14188 level of parsing which covers all the compilation errors that occurred.
14189 Some compilation errors, however, will throw an exception immediately.
14191 The I<flags> parameter is reserved for future use, and must always
14198 Perl_parse_fullstmt(pTHX_ U32 flags)
14201 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_fullstmt");
14202 return parse_recdescent_for_op(GRAMFULLSTMT);
14206 =for apidoc Amx|OP *|parse_stmtseq|U32 flags
14208 Parse a sequence of zero or more Perl statements. These may be normal
14209 imperative statements, including optional labels, or declarations
14210 that have compile-time effect, or any mixture thereof. The statement
14211 sequence ends when a closing brace or end-of-file is encountered in a
14212 place where a new statement could have validly started. It is up to
14213 the caller to ensure that the dynamic parser state (L</PL_parser> et al)
14214 is correctly set to reflect the source of the code to be parsed and the
14215 lexical context for the statements.
14217 The op tree representing the statement sequence is returned. This may
14218 be a null pointer if the statements were all null, for example if there
14219 were no statements or if there were only subroutine definitions (which
14220 have compile-time side effects). If not null, it will be a C<lineseq>
14221 list, normally including C<nextstate> or equivalent ops.
14223 If an error occurs in parsing or compilation, in most cases a valid op
14224 tree is returned anyway. The error is reflected in the parser state,
14225 normally resulting in a single exception at the top level of parsing
14226 which covers all the compilation errors that occurred. Some compilation
14227 errors, however, will throw an exception immediately.
14229 The I<flags> parameter is reserved for future use, and must always
14236 Perl_parse_stmtseq(pTHX_ U32 flags)
14241 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_fullstmt");
14242 stmtseqop = parse_recdescent_for_op(GRAMSTMTSEQ);
14243 c = lex_peek_unichar(0);
14244 if (c != -1 && c != /*{*/'}')
14245 qerror(Perl_mess(aTHX_ "Parse error"));
14250 Perl_munge_qwlist_to_paren_list(pTHX_ OP *qwlist)
14252 PERL_ARGS_ASSERT_MUNGE_QWLIST_TO_PAREN_LIST;
14253 deprecate("qw(...) as parentheses");
14255 if (qwlist->op_type == OP_STUB) {
14259 start_force(PL_curforce);
14260 NEXTVAL_NEXTTOKE.opval = qwlist;
14268 * c-indentation-style: bsd
14269 * c-basic-offset: 4
14270 * indent-tabs-mode: t
14273 * ex: set ts=8 sts=4 sw=4 noet: