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_snprintf(hex_string, sizeof(hex_string),
3207 "\\N{U+%X", (unsigned int) uv);
3209 /* Make sure there is enough space to hold it */
3210 d = off + SvGROW(sv, off
3212 + (STRLEN)(send - e)
3213 + 2); /* '}' + NUL */
3215 Copy(hex_string, d, output_length, char);
3218 /* For each subsequent character, append dot and
3219 * its ordinal in hex */
3220 while ((str += char_length) < str_end) {
3221 const STRLEN off = d - SvPVX_const(sv);
3222 U32 uv = utf8n_to_uvuni((U8 *) str,
3226 if (uv == 0 && NATIVE_TO_ASCII(*str) != '\0') {
3227 uv = UNICODE_REPLACEMENT;
3231 my_snprintf(hex_string, sizeof(hex_string),
3232 ".%X", (unsigned int) uv);
3234 d = off + SvGROW(sv, off
3236 + (STRLEN)(send - e)
3237 + 2); /* '}' + NUL */
3238 Copy(hex_string, d, output_length, char);
3242 *d++ = '}'; /* Done. Add the trailing brace */
3245 else { /* Here, not in a pattern. Convert the name to a
3248 /* If destination is not in utf8, unconditionally
3249 * recode it to be so. This is because \N{} implies
3250 * Unicode semantics, and scalars have to be in utf8
3251 * to guarantee those semantics */
3253 SvCUR_set(sv, d - SvPVX_const(sv));
3256 /* See Note on sizing above. */
3257 sv_utf8_upgrade_flags_grow(sv,
3258 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3259 len + (STRLEN)(send - s) + 1);
3260 d = SvPVX(sv) + SvCUR(sv);
3262 } else if (len > (STRLEN)(e - s + 4)) { /* I _guess_ 4 is \N{} --jhi */
3264 /* See Note on sizing above. (NOTE: SvCUR() is not
3265 * set correctly here). */
3266 const STRLEN off = d - SvPVX_const(sv);
3267 d = off + SvGROW(sv, off + len + (STRLEN)(send - s) + 1);
3269 Copy(str, d, len, char);
3274 /* Deprecate non-approved name syntax */
3275 if (ckWARN_d(WARN_DEPRECATED)) {
3276 bool problematic = FALSE;
3279 /* For non-ut8 input, look to see that the first
3280 * character is an alpha, then loop through the rest
3281 * checking that each is a continuation */
3283 if (! isALPHAU(*i)) problematic = TRUE;
3284 else for (i = s + 1; i < e; i++) {
3285 if (isCHARNAME_CONT(*i)) continue;
3291 /* Similarly for utf8. For invariants can check
3292 * directly. We accept anything above the latin1
3293 * range because it is immaterial to Perl if it is
3294 * correct or not, and is expensive to check. But
3295 * it is fairly easy in the latin1 range to convert
3296 * the variants into a single character and check
3298 if (UTF8_IS_INVARIANT(*i)) {
3299 if (! isALPHAU(*i)) problematic = TRUE;
3300 } else if (UTF8_IS_DOWNGRADEABLE_START(*i)) {
3301 if (! isALPHAU(UNI_TO_NATIVE(UTF8_ACCUMULATE(*i,
3307 if (! problematic) for (i = s + UTF8SKIP(s);
3311 if (UTF8_IS_INVARIANT(*i)) {
3312 if (isCHARNAME_CONT(*i)) continue;
3313 } else if (! UTF8_IS_DOWNGRADEABLE_START(*i)) {
3315 } else if (isCHARNAME_CONT(
3317 UTF8_ACCUMULATE(*i, *(i+1)))))
3326 /* The e-i passed to the final %.*s makes sure that
3327 * should the trailing NUL be missing that this
3328 * print won't run off the end of the string */
3329 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
3330 "Deprecated character in \\N{...}; marked by <-- HERE in \\N{%.*s<-- HERE %.*s",
3331 (int)(i - s + 1), s, (int)(e - i), i + 1);
3334 } /* End \N{NAME} */
3337 native_range = FALSE; /* \N{} is defined to be Unicode */
3339 s = e + 1; /* Point to just after the '}' */
3342 /* \c is a control character */
3346 *d++ = grok_bslash_c(*s++, 1);
3349 yyerror("Missing control char name in \\c");
3353 /* printf-style backslashes, formfeeds, newlines, etc */
3355 *d++ = NATIVE_TO_NEED(has_utf8,'\b');
3358 *d++ = NATIVE_TO_NEED(has_utf8,'\n');
3361 *d++ = NATIVE_TO_NEED(has_utf8,'\r');
3364 *d++ = NATIVE_TO_NEED(has_utf8,'\f');
3367 *d++ = NATIVE_TO_NEED(has_utf8,'\t');
3370 *d++ = ASCII_TO_NEED(has_utf8,'\033');
3373 *d++ = ASCII_TO_NEED(has_utf8,'\007');
3379 } /* end if (backslash) */
3386 /* If we started with encoded form, or already know we want it,
3387 then encode the next character */
3388 if (! NATIVE_IS_INVARIANT((U8)(*s)) && (this_utf8 || has_utf8)) {
3392 /* One might think that it is wasted effort in the case of the
3393 * source being utf8 (this_utf8 == TRUE) to take the next character
3394 * in the source, convert it to an unsigned value, and then convert
3395 * it back again. But the source has not been validated here. The
3396 * routine that does the conversion checks for errors like
3399 const UV nextuv = (this_utf8) ? utf8n_to_uvchr((U8*)s, send - s, &len, 0) : (UV) ((U8) *s);
3400 const STRLEN need = UNISKIP(NATIVE_TO_UNI(nextuv));
3402 SvCUR_set(sv, d - SvPVX_const(sv));
3405 /* See Note on sizing above. */
3406 sv_utf8_upgrade_flags_grow(sv,
3407 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3408 need + (STRLEN)(send - s) + 1);
3409 d = SvPVX(sv) + SvCUR(sv);
3411 } else if (need > len) {
3412 /* encoded value larger than old, may need extra space (NOTE:
3413 * SvCUR() is not set correctly here). See Note on sizing
3415 const STRLEN off = d - SvPVX_const(sv);
3416 d = SvGROW(sv, off + need + (STRLEN)(send - s) + 1) + off;
3420 d = (char*)uvchr_to_utf8((U8*)d, nextuv);
3422 if (uv > 255 && !dorange)
3423 native_range = FALSE;
3427 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
3429 } /* while loop to process each character */
3431 /* terminate the string and set up the sv */
3433 SvCUR_set(sv, d - SvPVX_const(sv));
3434 if (SvCUR(sv) >= SvLEN(sv))
3435 Perl_croak(aTHX_ "panic: constant overflowed allocated space");
3438 if (PL_encoding && !has_utf8) {
3439 sv_recode_to_utf8(sv, PL_encoding);
3445 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
3446 PL_sublex_info.sub_op->op_private |=
3447 (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
3451 /* shrink the sv if we allocated more than we used */
3452 if (SvCUR(sv) + 5 < SvLEN(sv)) {
3453 SvPV_shrink_to_cur(sv);
3456 /* return the substring (via pl_yylval) only if we parsed anything */
3457 if (s > PL_bufptr) {
3458 if ( PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ) ) {
3459 const char *const key = PL_lex_inpat ? "qr" : "q";
3460 const STRLEN keylen = PL_lex_inpat ? 2 : 1;
3464 if (PL_lex_inwhat == OP_TRANS) {
3467 } else if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat) {
3475 sv = S_new_constant(aTHX_ start, s - start, key, keylen, sv, NULL,
3478 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
3485 * Returns TRUE if there's more to the expression (e.g., a subscript),
3488 * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
3490 * ->[ and ->{ return TRUE
3491 * { and [ outside a pattern are always subscripts, so return TRUE
3492 * if we're outside a pattern and it's not { or [, then return FALSE
3493 * if we're in a pattern and the first char is a {
3494 * {4,5} (any digits around the comma) returns FALSE
3495 * if we're in a pattern and the first char is a [
3497 * [SOMETHING] has a funky algorithm to decide whether it's a
3498 * character class or not. It has to deal with things like
3499 * /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
3500 * anything else returns TRUE
3503 /* This is the one truly awful dwimmer necessary to conflate C and sed. */
3506 S_intuit_more(pTHX_ register char *s)
3510 PERL_ARGS_ASSERT_INTUIT_MORE;
3512 if (PL_lex_brackets)
3514 if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
3516 if (*s != '{' && *s != '[')
3521 /* In a pattern, so maybe we have {n,m}. */
3529 /* On the other hand, maybe we have a character class */
3532 if (*s == ']' || *s == '^')
3535 /* this is terrifying, and it works */
3536 int weight = 2; /* let's weigh the evidence */
3538 unsigned char un_char = 255, last_un_char;
3539 const char * const send = strchr(s,']');
3540 char tmpbuf[sizeof PL_tokenbuf * 4];
3542 if (!send) /* has to be an expression */
3545 Zero(seen,256,char);
3548 else if (isDIGIT(*s)) {
3550 if (isDIGIT(s[1]) && s[2] == ']')
3556 for (; s < send; s++) {
3557 last_un_char = un_char;
3558 un_char = (unsigned char)*s;
3563 weight -= seen[un_char] * 10;
3564 if (isALNUM_lazy_if(s+1,UTF)) {
3566 scan_ident(s, send, tmpbuf, sizeof tmpbuf, FALSE);
3567 len = (int)strlen(tmpbuf);
3568 if (len > 1 && gv_fetchpvn_flags(tmpbuf, len, 0, SVt_PV))
3573 else if (*s == '$' && s[1] &&
3574 strchr("[#!%*<>()-=",s[1])) {
3575 if (/*{*/ strchr("])} =",s[2]))
3584 if (strchr("wds]",s[1]))
3586 else if (seen[(U8)'\''] || seen[(U8)'"'])
3588 else if (strchr("rnftbxcav",s[1]))
3590 else if (isDIGIT(s[1])) {
3592 while (s[1] && isDIGIT(s[1]))
3602 if (strchr("aA01! ",last_un_char))
3604 if (strchr("zZ79~",s[1]))
3606 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
3607 weight -= 5; /* cope with negative subscript */
3610 if (!isALNUM(last_un_char)
3611 && !(last_un_char == '$' || last_un_char == '@'
3612 || last_un_char == '&')
3613 && isALPHA(*s) && s[1] && isALPHA(s[1])) {
3618 if (keyword(tmpbuf, d - tmpbuf, 0))
3621 if (un_char == last_un_char + 1)
3623 weight -= seen[un_char];
3628 if (weight >= 0) /* probably a character class */
3638 * Does all the checking to disambiguate
3640 * between foo(bar) and bar->foo. Returns 0 if not a method, otherwise
3641 * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
3643 * First argument is the stuff after the first token, e.g. "bar".
3645 * Not a method if bar is a filehandle.
3646 * Not a method if foo is a subroutine prototyped to take a filehandle.
3647 * Not a method if it's really "Foo $bar"
3648 * Method if it's "foo $bar"
3649 * Not a method if it's really "print foo $bar"
3650 * Method if it's really "foo package::" (interpreted as package->foo)
3651 * Not a method if bar is known to be a subroutine ("sub bar; foo bar")
3652 * Not a method if bar is a filehandle or package, but is quoted with
3657 S_intuit_method(pTHX_ char *start, GV *gv, CV *cv)
3660 char *s = start + (*start == '$');
3661 char tmpbuf[sizeof PL_tokenbuf];
3668 PERL_ARGS_ASSERT_INTUIT_METHOD;
3671 if (SvTYPE(gv) == SVt_PVGV && GvIO(gv))
3675 const char *proto = SvPVX_const(cv);
3686 s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
3687 /* start is the beginning of the possible filehandle/object,
3688 * and s is the end of it
3689 * tmpbuf is a copy of it
3692 if (*start == '$') {
3693 if (gv || PL_last_lop_op == OP_PRINT || PL_last_lop_op == OP_SAY ||
3694 isUPPER(*PL_tokenbuf))
3697 len = start - SvPVX(PL_linestr);
3701 start = SvPVX(PL_linestr) + len;
3705 return *s == '(' ? FUNCMETH : METHOD;
3707 if (!keyword(tmpbuf, len, 0)) {
3708 if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
3712 soff = s - SvPVX(PL_linestr);
3716 indirgv = gv_fetchpvn_flags(tmpbuf, len, 0, SVt_PVCV);
3717 if (indirgv && GvCVu(indirgv))
3719 /* filehandle or package name makes it a method */
3720 if (!gv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, 0)) {
3722 soff = s - SvPVX(PL_linestr);
3725 if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
3726 return 0; /* no assumptions -- "=>" quotes bearword */
3728 start_force(PL_curforce);
3729 NEXTVAL_NEXTTOKE.opval = (OP*)newSVOP(OP_CONST, 0,
3730 S_newSV_maybe_utf8(aTHX_ tmpbuf, len));
3731 NEXTVAL_NEXTTOKE.opval->op_private = OPpCONST_BARE;
3733 curmad('X', newSVpvn(start,SvPVX(PL_linestr) + soff - start));
3738 PL_bufptr = SvPVX(PL_linestr) + soff; /* restart before space */
3740 return *s == '(' ? FUNCMETH : METHOD;
3746 /* Encoded script support. filter_add() effectively inserts a
3747 * 'pre-processing' function into the current source input stream.
3748 * Note that the filter function only applies to the current source file
3749 * (e.g., it will not affect files 'require'd or 'use'd by this one).
3751 * The datasv parameter (which may be NULL) can be used to pass
3752 * private data to this instance of the filter. The filter function
3753 * can recover the SV using the FILTER_DATA macro and use it to
3754 * store private buffers and state information.
3756 * The supplied datasv parameter is upgraded to a PVIO type
3757 * and the IoDIRP/IoANY field is used to store the function pointer,
3758 * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
3759 * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
3760 * private use must be set using malloc'd pointers.
3764 Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
3773 if (!PL_rsfp_filters)
3774 PL_rsfp_filters = newAV();
3777 SvUPGRADE(datasv, SVt_PVIO);
3778 IoANY(datasv) = FPTR2DPTR(void *, funcp); /* stash funcp into spare field */
3779 IoFLAGS(datasv) |= IOf_FAKE_DIRP;
3780 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
3781 FPTR2DPTR(void *, IoANY(datasv)),
3782 SvPV_nolen(datasv)));
3783 av_unshift(PL_rsfp_filters, 1);
3784 av_store(PL_rsfp_filters, 0, datasv) ;
3789 /* Delete most recently added instance of this filter function. */
3791 Perl_filter_del(pTHX_ filter_t funcp)
3796 PERL_ARGS_ASSERT_FILTER_DEL;
3799 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p",
3800 FPTR2DPTR(void*, funcp)));
3802 if (!PL_parser || !PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
3804 /* if filter is on top of stack (usual case) just pop it off */
3805 datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
3806 if (IoANY(datasv) == FPTR2DPTR(void *, funcp)) {
3807 sv_free(av_pop(PL_rsfp_filters));
3811 /* we need to search for the correct entry and clear it */
3812 Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
3816 /* Invoke the idxth filter function for the current rsfp. */
3817 /* maxlen 0 = read one text line */
3819 Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
3824 /* This API is bad. It should have been using unsigned int for maxlen.
3825 Not sure if we want to change the API, but if not we should sanity
3826 check the value here. */
3827 const unsigned int correct_length
3836 PERL_ARGS_ASSERT_FILTER_READ;
3838 if (!PL_parser || !PL_rsfp_filters)
3840 if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */
3841 /* Provide a default input filter to make life easy. */
3842 /* Note that we append to the line. This is handy. */
3843 DEBUG_P(PerlIO_printf(Perl_debug_log,
3844 "filter_read %d: from rsfp\n", idx));
3845 if (correct_length) {
3848 const int old_len = SvCUR(buf_sv);
3850 /* ensure buf_sv is large enough */
3851 SvGROW(buf_sv, (STRLEN)(old_len + correct_length + 1)) ;
3852 if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len,
3853 correct_length)) <= 0) {
3854 if (PerlIO_error(PL_rsfp))
3855 return -1; /* error */
3857 return 0 ; /* end of file */
3859 SvCUR_set(buf_sv, old_len + len) ;
3860 SvPVX(buf_sv)[old_len + len] = '\0';
3863 if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
3864 if (PerlIO_error(PL_rsfp))
3865 return -1; /* error */
3867 return 0 ; /* end of file */
3870 return SvCUR(buf_sv);
3872 /* Skip this filter slot if filter has been deleted */
3873 if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) {
3874 DEBUG_P(PerlIO_printf(Perl_debug_log,
3875 "filter_read %d: skipped (filter deleted)\n",
3877 return FILTER_READ(idx+1, buf_sv, correct_length); /* recurse */
3879 /* Get function pointer hidden within datasv */
3880 funcp = DPTR2FPTR(filter_t, IoANY(datasv));
3881 DEBUG_P(PerlIO_printf(Perl_debug_log,
3882 "filter_read %d: via function %p (%s)\n",
3883 idx, (void*)datasv, SvPV_nolen_const(datasv)));
3884 /* Call function. The function is expected to */
3885 /* call "FILTER_READ(idx+1, buf_sv)" first. */
3886 /* Return: <0:error, =0:eof, >0:not eof */
3887 return (*funcp)(aTHX_ idx, buf_sv, correct_length);
3891 S_filter_gets(pTHX_ register SV *sv, STRLEN append)
3895 PERL_ARGS_ASSERT_FILTER_GETS;
3897 #ifdef PERL_CR_FILTER
3898 if (!PL_rsfp_filters) {
3899 filter_add(S_cr_textfilter,NULL);
3902 if (PL_rsfp_filters) {
3904 SvCUR_set(sv, 0); /* start with empty line */
3905 if (FILTER_READ(0, sv, 0) > 0)
3906 return ( SvPVX(sv) ) ;
3911 return (sv_gets(sv, PL_rsfp, append));
3915 S_find_in_my_stash(pTHX_ const char *pkgname, STRLEN len)
3920 PERL_ARGS_ASSERT_FIND_IN_MY_STASH;
3922 if (len == 11 && *pkgname == '_' && strEQ(pkgname, "__PACKAGE__"))
3926 (pkgname[len - 2] == ':' && pkgname[len - 1] == ':') &&
3927 (gv = gv_fetchpvn_flags(pkgname, len, 0, SVt_PVHV)))
3929 return GvHV(gv); /* Foo:: */
3932 /* use constant CLASS => 'MyClass' */
3933 gv = gv_fetchpvn_flags(pkgname, len, 0, SVt_PVCV);
3934 if (gv && GvCV(gv)) {
3935 SV * const sv = cv_const_sv(GvCV(gv));
3937 pkgname = SvPV_const(sv, len);
3940 return gv_stashpvn(pkgname, len, 0);
3944 * S_readpipe_override
3945 * Check whether readpipe() is overriden, and generates the appropriate
3946 * optree, provided sublex_start() is called afterwards.
3949 S_readpipe_override(pTHX)
3952 GV *gv_readpipe = gv_fetchpvs("readpipe", GV_NOTQUAL, SVt_PVCV);
3953 pl_yylval.ival = OP_BACKTICK;
3955 && GvCVu(gv_readpipe) && GvIMPORTED_CV(gv_readpipe))
3957 ((gvp = (GV**)hv_fetchs(PL_globalstash, "readpipe", FALSE))
3958 && (gv_readpipe = *gvp) && isGV_with_GP(gv_readpipe)
3959 && GvCVu(gv_readpipe) && GvIMPORTED_CV(gv_readpipe)))
3961 PL_lex_op = (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
3962 op_append_elem(OP_LIST,
3963 newSVOP(OP_CONST, 0, &PL_sv_undef), /* value will be read later */
3964 newCVREF(0, newGVOP(OP_GV, 0, gv_readpipe))));
3971 * The intent of this yylex wrapper is to minimize the changes to the
3972 * tokener when we aren't interested in collecting madprops. It remains
3973 * to be seen how successful this strategy will be...
3980 char *s = PL_bufptr;
3982 /* make sure PL_thiswhite is initialized */
3986 /* just do what yylex would do on pending identifier; leave PL_thiswhite alone */
3987 if (PL_lex_state != LEX_KNOWNEXT && PL_pending_ident)
3988 return S_pending_ident(aTHX);
3990 /* previous token ate up our whitespace? */
3991 if (!PL_lasttoke && PL_nextwhite) {
3992 PL_thiswhite = PL_nextwhite;
3996 /* isolate the token, and figure out where it is without whitespace */
3997 PL_realtokenstart = -1;
4001 assert(PL_curforce < 0);
4003 if (!PL_thismad || PL_thismad->mad_key == '^') { /* not forced already? */
4004 if (!PL_thistoken) {
4005 if (PL_realtokenstart < 0 || !CopLINE(PL_curcop))
4006 PL_thistoken = newSVpvs("");
4008 char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
4009 PL_thistoken = newSVpvn(tstart, s - tstart);
4012 if (PL_thismad) /* install head */
4013 CURMAD('X', PL_thistoken);
4016 /* last whitespace of a sublex? */
4017 if (optype == ')' && PL_endwhite) {
4018 CURMAD('X', PL_endwhite);
4023 /* if no whitespace and we're at EOF, bail. Otherwise fake EOF below. */
4024 if (!PL_thiswhite && !PL_endwhite && !optype) {
4025 sv_free(PL_thistoken);
4030 /* put off final whitespace till peg */
4031 if (optype == ';' && !PL_rsfp) {
4032 PL_nextwhite = PL_thiswhite;
4035 else if (PL_thisopen) {
4036 CURMAD('q', PL_thisopen);
4038 sv_free(PL_thistoken);
4042 /* Store actual token text as madprop X */
4043 CURMAD('X', PL_thistoken);
4047 /* add preceding whitespace as madprop _ */
4048 CURMAD('_', PL_thiswhite);
4052 /* add quoted material as madprop = */
4053 CURMAD('=', PL_thisstuff);
4057 /* add terminating quote as madprop Q */
4058 CURMAD('Q', PL_thisclose);
4062 /* special processing based on optype */
4066 /* opval doesn't need a TOKEN since it can already store mp */
4076 if (pl_yylval.opval)
4077 append_madprops(PL_thismad, pl_yylval.opval, 0);
4085 addmad(newMADsv('p', PL_endwhite), &PL_thismad, 0);
4094 /* remember any fake bracket that lexer is about to discard */
4095 if (PL_lex_brackets == 1 &&
4096 ((expectation)PL_lex_brackstack[0] & XFAKEBRACK))
4099 while (s < PL_bufend && (*s == ' ' || *s == '\t'))
4102 PL_thiswhite = newSVpvn(PL_bufptr, ++s - PL_bufptr);
4103 addmad(newMADsv('#', PL_thiswhite), &PL_thismad, 0);
4106 break; /* don't bother looking for trailing comment */
4115 /* attach a trailing comment to its statement instead of next token */
4119 if (PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == optype) {
4121 while (s < PL_bufend && (*s == ' ' || *s == '\t'))
4123 if (*s == '\n' || *s == '#') {
4124 while (s < PL_bufend && *s != '\n')
4128 PL_thiswhite = newSVpvn(PL_bufptr, s - PL_bufptr);
4129 addmad(newMADsv('#', PL_thiswhite), &PL_thismad, 0);
4146 /* Create new token struct. Note: opvals return early above. */
4147 pl_yylval.tkval = newTOKEN(optype, pl_yylval, PL_thismad);
4154 S_tokenize_use(pTHX_ int is_use, char *s) {
4157 PERL_ARGS_ASSERT_TOKENIZE_USE;
4159 if (PL_expect != XSTATE)
4160 yyerror(Perl_form(aTHX_ "\"%s\" not allowed in expression",
4161 is_use ? "use" : "no"));
4163 if (isDIGIT(*s) || (*s == 'v' && isDIGIT(s[1]))) {
4164 s = force_version(s, TRUE);
4165 if (*s == ';' || *s == '}'
4166 || (s = SKIPSPACE1(s), (*s == ';' || *s == '}'))) {
4167 start_force(PL_curforce);
4168 NEXTVAL_NEXTTOKE.opval = NULL;
4171 else if (*s == 'v') {
4172 s = force_word(s,WORD,FALSE,TRUE,FALSE);
4173 s = force_version(s, FALSE);
4177 s = force_word(s,WORD,FALSE,TRUE,FALSE);
4178 s = force_version(s, FALSE);
4180 pl_yylval.ival = is_use;
4184 static const char* const exp_name[] =
4185 { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
4186 "ATTRTERM", "TERMBLOCK", "TERMORDORDOR"
4190 #define word_takes_any_delimeter(p,l) S_word_takes_any_delimeter(p,l)
4192 S_word_takes_any_delimeter(char *p, STRLEN len)
4194 return (len == 1 && strchr("msyq", p[0])) ||
4196 (p[0] == 't' && p[1] == 'r') ||
4197 (p[0] == 'q' && strchr("qwxr", p[1]))));
4203 Works out what to call the token just pulled out of the input
4204 stream. The yacc parser takes care of taking the ops we return and
4205 stitching them into a tree.
4211 if read an identifier
4212 if we're in a my declaration
4213 croak if they tried to say my($foo::bar)
4214 build the ops for a my() declaration
4215 if it's an access to a my() variable
4216 are we in a sort block?
4217 croak if my($a); $a <=> $b
4218 build ops for access to a my() variable
4219 if in a dq string, and they've said @foo and we can't find @foo
4221 build ops for a bareword
4222 if we already built the token before, use it.
4227 #pragma segment Perl_yylex
4233 register char *s = PL_bufptr;
4239 /* orig_keyword, gvp, and gv are initialized here because
4240 * jump to the label just_a_word_zero can bypass their
4241 * initialization later. */
4242 I32 orig_keyword = 0;
4247 SV* tmp = newSVpvs("");
4248 PerlIO_printf(Perl_debug_log, "### %"IVdf":LEX_%s/X%s %s\n",
4249 (IV)CopLINE(PL_curcop),
4250 lex_state_names[PL_lex_state],
4251 exp_name[PL_expect],
4252 pv_display(tmp, s, strlen(s), 0, 60));
4255 /* check if there's an identifier for us to look at */
4256 if (PL_lex_state != LEX_KNOWNEXT && PL_pending_ident)
4257 return REPORT(S_pending_ident(aTHX));
4259 /* no identifier pending identification */
4261 switch (PL_lex_state) {
4263 case LEX_NORMAL: /* Some compilers will produce faster */
4264 case LEX_INTERPNORMAL: /* code if we comment these out. */
4268 /* when we've already built the next token, just pull it out of the queue */
4272 pl_yylval = PL_nexttoke[PL_lasttoke].next_val;
4274 PL_thismad = PL_nexttoke[PL_lasttoke].next_mad;
4275 PL_nexttoke[PL_lasttoke].next_mad = 0;
4276 if (PL_thismad && PL_thismad->mad_key == '_') {
4277 PL_thiswhite = MUTABLE_SV(PL_thismad->mad_val);
4278 PL_thismad->mad_val = 0;
4279 mad_free(PL_thismad);
4284 PL_lex_state = PL_lex_defer;
4285 PL_expect = PL_lex_expect;
4286 PL_lex_defer = LEX_NORMAL;
4287 if (!PL_nexttoke[PL_lasttoke].next_type)
4292 pl_yylval = PL_nextval[PL_nexttoke];
4294 PL_lex_state = PL_lex_defer;
4295 PL_expect = PL_lex_expect;
4296 PL_lex_defer = LEX_NORMAL;
4302 next_type = PL_nexttoke[PL_lasttoke].next_type;
4304 next_type = PL_nexttype[PL_nexttoke];
4306 if (next_type & (1<<24)) {
4307 if (PL_lex_brackets > 100)
4308 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
4309 PL_lex_brackstack[PL_lex_brackets++] = (next_type >> 16) & 0xff;
4310 next_type &= 0xffff;
4313 /* FIXME - can these be merged? */
4316 return REPORT(next_type);
4320 /* interpolated case modifiers like \L \U, including \Q and \E.
4321 when we get here, PL_bufptr is at the \
4323 case LEX_INTERPCASEMOD:
4325 if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
4326 Perl_croak(aTHX_ "panic: INTERPCASEMOD");
4328 /* handle \E or end of string */
4329 if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
4331 if (PL_lex_casemods) {
4332 const char oldmod = PL_lex_casestack[--PL_lex_casemods];
4333 PL_lex_casestack[PL_lex_casemods] = '\0';
4335 if (PL_bufptr != PL_bufend
4336 && (oldmod == 'L' || oldmod == 'U' || oldmod == 'Q')) {
4338 PL_lex_state = LEX_INTERPCONCAT;
4341 PL_thistoken = newSVpvs("\\E");
4347 while (PL_bufptr != PL_bufend &&
4348 PL_bufptr[0] == '\\' && PL_bufptr[1] == 'E') {
4350 PL_thiswhite = newSVpvs("");
4351 sv_catpvn(PL_thiswhite, PL_bufptr, 2);
4355 if (PL_bufptr != PL_bufend)
4358 PL_lex_state = LEX_INTERPCONCAT;
4362 DEBUG_T({ PerlIO_printf(Perl_debug_log,
4363 "### Saw case modifier\n"); });
4365 if (s[1] == '\\' && s[2] == 'E') {
4368 PL_thiswhite = newSVpvs("");
4369 sv_catpvn(PL_thiswhite, PL_bufptr, 4);
4372 PL_lex_state = LEX_INTERPCONCAT;
4377 if (!PL_madskills) /* when just compiling don't need correct */
4378 if (strnEQ(s, "L\\u", 3) || strnEQ(s, "U\\l", 3))
4379 tmp = *s, *s = s[2], s[2] = (char)tmp; /* misordered... */
4380 if ((*s == 'L' || *s == 'U') &&
4381 (strchr(PL_lex_casestack, 'L') || strchr(PL_lex_casestack, 'U'))) {
4382 PL_lex_casestack[--PL_lex_casemods] = '\0';
4385 if (PL_lex_casemods > 10)
4386 Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
4387 PL_lex_casestack[PL_lex_casemods++] = *s;
4388 PL_lex_casestack[PL_lex_casemods] = '\0';
4389 PL_lex_state = LEX_INTERPCONCAT;
4390 start_force(PL_curforce);
4391 NEXTVAL_NEXTTOKE.ival = 0;
4393 start_force(PL_curforce);
4395 NEXTVAL_NEXTTOKE.ival = OP_LCFIRST;
4397 NEXTVAL_NEXTTOKE.ival = OP_UCFIRST;
4399 NEXTVAL_NEXTTOKE.ival = OP_LC;
4401 NEXTVAL_NEXTTOKE.ival = OP_UC;
4403 NEXTVAL_NEXTTOKE.ival = OP_QUOTEMETA;
4405 Perl_croak(aTHX_ "panic: yylex");
4407 SV* const tmpsv = newSVpvs("\\ ");
4408 /* replace the space with the character we want to escape
4410 SvPVX(tmpsv)[1] = *s;
4416 if (PL_lex_starts) {
4422 sv_free(PL_thistoken);
4423 PL_thistoken = newSVpvs("");
4426 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
4427 if (PL_lex_casemods == 1 && PL_lex_inpat)
4436 case LEX_INTERPPUSH:
4437 return REPORT(sublex_push());
4439 case LEX_INTERPSTART:
4440 if (PL_bufptr == PL_bufend)
4441 return REPORT(sublex_done());
4442 DEBUG_T({ PerlIO_printf(Perl_debug_log,
4443 "### Interpolated variable\n"); });
4445 PL_lex_dojoin = (*PL_bufptr == '@');
4446 PL_lex_state = LEX_INTERPNORMAL;
4447 if (PL_lex_dojoin) {
4448 start_force(PL_curforce);
4449 NEXTVAL_NEXTTOKE.ival = 0;
4451 start_force(PL_curforce);
4452 force_ident("\"", '$');
4453 start_force(PL_curforce);
4454 NEXTVAL_NEXTTOKE.ival = 0;
4456 start_force(PL_curforce);
4457 NEXTVAL_NEXTTOKE.ival = 0;
4459 start_force(PL_curforce);
4460 NEXTVAL_NEXTTOKE.ival = OP_JOIN; /* emulate join($", ...) */
4463 if (PL_lex_starts++) {
4468 sv_free(PL_thistoken);
4469 PL_thistoken = newSVpvs("");
4472 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
4473 if (!PL_lex_casemods && PL_lex_inpat)
4480 case LEX_INTERPENDMAYBE:
4481 if (intuit_more(PL_bufptr)) {
4482 PL_lex_state = LEX_INTERPNORMAL; /* false alarm, more expr */
4488 if (PL_lex_dojoin) {
4489 PL_lex_dojoin = FALSE;
4490 PL_lex_state = LEX_INTERPCONCAT;
4494 sv_free(PL_thistoken);
4495 PL_thistoken = newSVpvs("");
4500 if (PL_lex_inwhat == OP_SUBST && PL_linestr == PL_lex_repl
4501 && SvEVALED(PL_lex_repl))
4503 if (PL_bufptr != PL_bufend)
4504 Perl_croak(aTHX_ "Bad evalled substitution pattern");
4508 case LEX_INTERPCONCAT:
4510 if (PL_lex_brackets)
4511 Perl_croak(aTHX_ "panic: INTERPCONCAT");
4513 if (PL_bufptr == PL_bufend)
4514 return REPORT(sublex_done());
4516 if (SvIVX(PL_linestr) == '\'') {
4517 SV *sv = newSVsv(PL_linestr);
4520 else if ( PL_hints & HINT_NEW_RE )
4521 sv = new_constant(NULL, 0, "qr", sv, sv, "q", 1);
4522 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
4526 s = scan_const(PL_bufptr);
4528 PL_lex_state = LEX_INTERPCASEMOD;
4530 PL_lex_state = LEX_INTERPSTART;
4533 if (s != PL_bufptr) {
4534 start_force(PL_curforce);
4536 curmad('X', newSVpvn(PL_bufptr,s-PL_bufptr));
4538 NEXTVAL_NEXTTOKE = pl_yylval;
4541 if (PL_lex_starts++) {
4545 sv_free(PL_thistoken);
4546 PL_thistoken = newSVpvs("");
4549 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
4550 if (!PL_lex_casemods && PL_lex_inpat)
4563 PL_lex_state = LEX_NORMAL;
4564 s = scan_formline(PL_bufptr);
4565 if (!PL_lex_formbrack)
4571 PL_oldoldbufptr = PL_oldbufptr;
4577 sv_free(PL_thistoken);
4580 PL_realtokenstart = s - SvPVX(PL_linestr); /* assume but undo on ws */
4584 if (isIDFIRST_lazy_if(s,UTF))
4587 unsigned char c = *s;
4588 len = UTF ? Perl_utf8_length(aTHX_ (U8 *) PL_linestart, (U8 *) s) : (STRLEN) (s - PL_linestart);
4589 if (len > UNRECOGNIZED_PRECEDE_COUNT) {
4590 d = UTF ? (char *) Perl_utf8_hop(aTHX_ (U8 *) s, -UNRECOGNIZED_PRECEDE_COUNT) : s - UNRECOGNIZED_PRECEDE_COUNT;
4595 Perl_croak(aTHX_ "Unrecognized character \\x%02X; marked by <-- HERE after %s<-- HERE near column %d", c, d, (int) len + 1);
4599 goto fake_eof; /* emulate EOF on ^D or ^Z */
4608 if (PL_lex_brackets &&
4609 PL_lex_brackstack[PL_lex_brackets-1] != XFAKEEOF) {
4610 yyerror((const char *)
4612 ? "Format not terminated"
4613 : "Missing right curly or square bracket"));
4615 DEBUG_T( { PerlIO_printf(Perl_debug_log,
4616 "### Tokener got EOF\n");
4620 if (s++ < PL_bufend)
4621 goto retry; /* ignore stray nulls */
4624 if (!PL_in_eval && !PL_preambled) {
4625 PL_preambled = TRUE;
4631 /* Generate a string of Perl code to load the debugger.
4632 * If PERL5DB is set, it will return the contents of that,
4633 * otherwise a compile-time require of perl5db.pl. */
4635 const char * const pdb = PerlEnv_getenv("PERL5DB");
4638 sv_setpv(PL_linestr, pdb);
4639 sv_catpvs(PL_linestr,";");
4641 SETERRNO(0,SS_NORMAL);
4642 sv_setpvs(PL_linestr, "BEGIN { require 'perl5db.pl' };");
4645 sv_setpvs(PL_linestr,"");
4646 if (PL_preambleav) {
4647 SV **svp = AvARRAY(PL_preambleav);
4648 SV **const end = svp + AvFILLp(PL_preambleav);
4650 sv_catsv(PL_linestr, *svp);
4652 sv_catpvs(PL_linestr, ";");
4654 sv_free(MUTABLE_SV(PL_preambleav));
4655 PL_preambleav = NULL;
4658 sv_catpvs(PL_linestr,
4659 "use feature ':5." STRINGIFY(PERL_VERSION) "';");
4660 if (PL_minus_n || PL_minus_p) {
4661 sv_catpvs(PL_linestr, "LINE: while (<>) {"/*}*/);
4663 sv_catpvs(PL_linestr,"chomp;");
4666 if ((*PL_splitstr == '/' || *PL_splitstr == '\''
4667 || *PL_splitstr == '"')
4668 && strchr(PL_splitstr + 1, *PL_splitstr))
4669 Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
4671 /* "q\0${splitstr}\0" is legal perl. Yes, even NUL
4672 bytes can be used as quoting characters. :-) */
4673 const char *splits = PL_splitstr;
4674 sv_catpvs(PL_linestr, "our @F=split(q\0");
4677 if (*splits == '\\')
4678 sv_catpvn(PL_linestr, splits, 1);
4679 sv_catpvn(PL_linestr, splits, 1);
4680 } while (*splits++);
4681 /* This loop will embed the trailing NUL of
4682 PL_linestr as the last thing it does before
4684 sv_catpvs(PL_linestr, ");");
4688 sv_catpvs(PL_linestr,"our @F=split(' ');");
4691 sv_catpvs(PL_linestr, "\n");
4692 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
4693 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
4694 PL_last_lop = PL_last_uni = NULL;
4695 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
4696 update_debugger_info(PL_linestr, NULL, 0);
4701 bof = PL_rsfp ? TRUE : FALSE;
4704 fake_eof = LEX_FAKE_EOF;
4706 PL_bufptr = PL_bufend;
4707 CopLINE_inc(PL_curcop);
4708 if (!lex_next_chunk(fake_eof)) {
4709 CopLINE_dec(PL_curcop);
4711 TOKEN(';'); /* not infinite loop because rsfp is NULL now */
4713 CopLINE_dec(PL_curcop);
4716 PL_realtokenstart = -1;
4719 /* If it looks like the start of a BOM or raw UTF-16,
4720 * check if it in fact is. */
4721 if (bof && PL_rsfp &&
4726 bof = PerlIO_tell(PL_rsfp) == (Off_t)SvCUR(PL_linestr);
4728 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
4729 s = swallow_bom((U8*)s);
4732 if (PL_parser->in_pod) {
4733 /* Incest with pod. */
4736 sv_catsv(PL_thiswhite, PL_linestr);
4738 if (*s == '=' && strnEQ(s, "=cut", 4) && !isALPHA(s[4])) {
4739 sv_setpvs(PL_linestr, "");
4740 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
4741 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
4742 PL_last_lop = PL_last_uni = NULL;
4743 PL_parser->in_pod = 0;
4748 } while (PL_parser->in_pod);
4749 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = s;
4750 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
4751 PL_last_lop = PL_last_uni = NULL;
4752 if (CopLINE(PL_curcop) == 1) {
4753 while (s < PL_bufend && isSPACE(*s))
4755 if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
4759 PL_thiswhite = newSVpvn(PL_linestart, s - PL_linestart);
4763 if (*s == '#' && *(s+1) == '!')
4765 #ifdef ALTERNATE_SHEBANG
4767 static char const as[] = ALTERNATE_SHEBANG;
4768 if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
4769 d = s + (sizeof(as) - 1);
4771 #endif /* ALTERNATE_SHEBANG */
4780 while (*d && !isSPACE(*d))
4784 #ifdef ARG_ZERO_IS_SCRIPT
4785 if (ipathend > ipath) {
4787 * HP-UX (at least) sets argv[0] to the script name,
4788 * which makes $^X incorrect. And Digital UNIX and Linux,
4789 * at least, set argv[0] to the basename of the Perl
4790 * interpreter. So, having found "#!", we'll set it right.
4792 SV * const x = GvSV(gv_fetchpvs("\030", GV_ADD|GV_NOTQUAL,
4794 assert(SvPOK(x) || SvGMAGICAL(x));
4795 if (sv_eq(x, CopFILESV(PL_curcop))) {
4796 sv_setpvn(x, ipath, ipathend - ipath);
4802 const char *bstart = SvPV_const(CopFILESV(PL_curcop),blen);
4803 const char * const lstart = SvPV_const(x,llen);
4805 bstart += blen - llen;
4806 if (strnEQ(bstart, lstart, llen) && bstart[-1] == '/') {
4807 sv_setpvn(x, ipath, ipathend - ipath);
4812 TAINT_NOT; /* $^X is always tainted, but that's OK */
4814 #endif /* ARG_ZERO_IS_SCRIPT */
4819 d = instr(s,"perl -");
4821 d = instr(s,"perl");
4823 /* avoid getting into infinite loops when shebang
4824 * line contains "Perl" rather than "perl" */
4826 for (d = ipathend-4; d >= ipath; --d) {
4827 if ((*d == 'p' || *d == 'P')
4828 && !ibcmp(d, "perl", 4))
4838 #ifdef ALTERNATE_SHEBANG
4840 * If the ALTERNATE_SHEBANG on this system starts with a
4841 * character that can be part of a Perl expression, then if
4842 * we see it but not "perl", we're probably looking at the
4843 * start of Perl code, not a request to hand off to some
4844 * other interpreter. Similarly, if "perl" is there, but
4845 * not in the first 'word' of the line, we assume the line
4846 * contains the start of the Perl program.
4848 if (d && *s != '#') {
4849 const char *c = ipath;
4850 while (*c && !strchr("; \t\r\n\f\v#", *c))
4853 d = NULL; /* "perl" not in first word; ignore */
4855 *s = '#'; /* Don't try to parse shebang line */
4857 #endif /* ALTERNATE_SHEBANG */
4862 !instr(s,"indir") &&
4863 instr(PL_origargv[0],"perl"))
4870 while (s < PL_bufend && isSPACE(*s))
4872 if (s < PL_bufend) {
4873 Newx(newargv,PL_origargc+3,char*);
4875 while (s < PL_bufend && !isSPACE(*s))
4878 Copy(PL_origargv+1, newargv+2, PL_origargc+1, char*);
4881 newargv = PL_origargv;
4884 PerlProc_execv(ipath, EXEC_ARGV_CAST(newargv));
4886 Perl_croak(aTHX_ "Can't exec %s", ipath);
4889 while (*d && !isSPACE(*d))
4891 while (SPACE_OR_TAB(*d))
4895 const bool switches_done = PL_doswitches;
4896 const U32 oldpdb = PL_perldb;
4897 const bool oldn = PL_minus_n;
4898 const bool oldp = PL_minus_p;
4902 bool baduni = FALSE;
4904 const char *d2 = d1 + 1;
4905 if (parse_unicode_opts((const char **)&d2)
4909 if (baduni || *d1 == 'M' || *d1 == 'm') {
4910 const char * const m = d1;
4911 while (*d1 && !isSPACE(*d1))
4913 Perl_croak(aTHX_ "Too late for \"-%.*s\" option",
4916 d1 = moreswitches(d1);
4918 if (PL_doswitches && !switches_done) {
4919 int argc = PL_origargc;
4920 char **argv = PL_origargv;
4923 } while (argc && argv[0][0] == '-' && argv[0][1]);
4924 init_argv_symbols(argc,argv);
4926 if (((PERLDB_LINE || PERLDB_SAVESRC) && !oldpdb) ||
4927 ((PL_minus_n || PL_minus_p) && !(oldn || oldp)))
4928 /* if we have already added "LINE: while (<>) {",
4929 we must not do it again */
4931 sv_setpvs(PL_linestr, "");
4932 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
4933 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
4934 PL_last_lop = PL_last_uni = NULL;
4935 PL_preambled = FALSE;
4936 if (PERLDB_LINE || PERLDB_SAVESRC)
4937 (void)gv_fetchfile(PL_origfilename);
4944 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
4946 PL_lex_state = LEX_FORMLINE;
4951 #ifdef PERL_STRICT_CR
4952 Perl_warn(aTHX_ "Illegal character \\%03o (carriage return)", '\r');
4954 "\t(Maybe you didn't strip carriage returns after a network transfer?)\n");
4956 case ' ': case '\t': case '\f': case 013:
4958 PL_realtokenstart = -1;
4960 PL_thiswhite = newSVpvs("");
4961 sv_catpvn(PL_thiswhite, s, 1);
4968 PL_realtokenstart = -1;
4972 if (PL_lex_state != LEX_NORMAL || (PL_in_eval && !PL_rsfp)) {
4973 if (*s == '#' && s == PL_linestart && PL_in_eval && !PL_rsfp) {
4974 /* handle eval qq[#line 1 "foo"\n ...] */
4975 CopLINE_dec(PL_curcop);
4978 if (PL_madskills && !PL_lex_formbrack && !PL_in_eval) {
4980 if (!PL_in_eval || PL_rsfp)
4985 while (d < PL_bufend && *d != '\n')
4989 else if (d > PL_bufend) /* Found by Ilya: feed random input to Perl. */
4990 Perl_croak(aTHX_ "panic: input overflow");
4993 PL_thiswhite = newSVpvn(s, d - s);
4998 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
5000 PL_lex_state = LEX_FORMLINE;
5006 if (PL_madskills && CopLINE(PL_curcop) >= 1 && !PL_lex_formbrack) {
5007 if (CopLINE(PL_curcop) == 1 && s[0] == '#' && s[1] == '!') {
5010 TOKEN(PEG); /* make sure any #! line is accessible */
5015 /* if (PL_madskills && PL_lex_formbrack) { */
5017 while (d < PL_bufend && *d != '\n')
5021 else if (d > PL_bufend) /* Found by Ilya: feed random input to Perl. */
5022 Perl_croak(aTHX_ "panic: input overflow");
5023 if (PL_madskills && CopLINE(PL_curcop) >= 1) {
5025 PL_thiswhite = newSVpvs("");
5026 if (CopLINE(PL_curcop) == 1) {
5027 sv_setpvs(PL_thiswhite, "");
5030 sv_catpvn(PL_thiswhite, s, d - s);
5044 if (s[1] && isALPHA(s[1]) && !isALNUM(s[2])) {
5052 while (s < PL_bufend && SPACE_OR_TAB(*s))
5055 if (strnEQ(s,"=>",2)) {
5056 s = force_word(PL_bufptr,WORD,FALSE,FALSE,FALSE);
5057 DEBUG_T( { printbuf("### Saw unary minus before =>, forcing word %s\n", s); } );
5058 OPERATOR('-'); /* unary minus */
5060 PL_last_uni = PL_oldbufptr;
5062 case 'r': ftst = OP_FTEREAD; break;
5063 case 'w': ftst = OP_FTEWRITE; break;
5064 case 'x': ftst = OP_FTEEXEC; break;
5065 case 'o': ftst = OP_FTEOWNED; break;
5066 case 'R': ftst = OP_FTRREAD; break;
5067 case 'W': ftst = OP_FTRWRITE; break;
5068 case 'X': ftst = OP_FTREXEC; break;
5069 case 'O': ftst = OP_FTROWNED; break;
5070 case 'e': ftst = OP_FTIS; break;
5071 case 'z': ftst = OP_FTZERO; break;
5072 case 's': ftst = OP_FTSIZE; break;
5073 case 'f': ftst = OP_FTFILE; break;
5074 case 'd': ftst = OP_FTDIR; break;
5075 case 'l': ftst = OP_FTLINK; break;
5076 case 'p': ftst = OP_FTPIPE; break;
5077 case 'S': ftst = OP_FTSOCK; break;
5078 case 'u': ftst = OP_FTSUID; break;
5079 case 'g': ftst = OP_FTSGID; break;
5080 case 'k': ftst = OP_FTSVTX; break;
5081 case 'b': ftst = OP_FTBLK; break;
5082 case 'c': ftst = OP_FTCHR; break;
5083 case 't': ftst = OP_FTTTY; break;
5084 case 'T': ftst = OP_FTTEXT; break;
5085 case 'B': ftst = OP_FTBINARY; break;
5086 case 'M': case 'A': case 'C':
5087 gv_fetchpvs("\024", GV_ADD|GV_NOTQUAL, SVt_PV);
5089 case 'M': ftst = OP_FTMTIME; break;
5090 case 'A': ftst = OP_FTATIME; break;
5091 case 'C': ftst = OP_FTCTIME; break;
5099 PL_last_lop_op = (OPCODE)ftst;
5100 DEBUG_T( { PerlIO_printf(Perl_debug_log,
5101 "### Saw file test %c\n", (int)tmp);
5106 /* Assume it was a minus followed by a one-letter named
5107 * subroutine call (or a -bareword), then. */
5108 DEBUG_T( { PerlIO_printf(Perl_debug_log,
5109 "### '-%c' looked like a file test but was not\n",
5116 const char tmp = *s++;
5119 if (PL_expect == XOPERATOR)
5124 else if (*s == '>') {
5127 if (isIDFIRST_lazy_if(s,UTF)) {
5128 s = force_word(s,METHOD,FALSE,TRUE,FALSE);
5136 if (PL_expect == XOPERATOR)
5139 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
5141 OPERATOR('-'); /* unary minus */
5147 const char tmp = *s++;
5150 if (PL_expect == XOPERATOR)
5155 if (PL_expect == XOPERATOR)
5158 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
5165 if (PL_expect != XOPERATOR) {
5166 s = scan_ident(s, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
5167 PL_expect = XOPERATOR;
5168 force_ident(PL_tokenbuf, '*');
5181 if (PL_expect == XOPERATOR) {
5185 PL_tokenbuf[0] = '%';
5186 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1,
5187 sizeof PL_tokenbuf - 1, FALSE);
5188 if (!PL_tokenbuf[1]) {
5191 PL_pending_ident = '%';
5198 if (PL_lex_brackets > 100)
5199 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
5200 PL_lex_brackstack[PL_lex_brackets++] = 0;
5202 const char tmp = *s++;
5207 && (PL_expect == XOPERATOR || PL_expect == XTERMORDORDOR))
5214 const char tmp = *s++;
5220 goto just_a_word_zero_gv;
5223 switch (PL_expect) {
5229 if (!PL_in_my || PL_lex_state != LEX_NORMAL)
5231 PL_bufptr = s; /* update in case we back off */
5234 "Use of := for an empty attribute list is not allowed");
5241 PL_expect = XTERMBLOCK;
5244 stuffstart = s - SvPVX(PL_linestr) - 1;
5248 while (isIDFIRST_lazy_if(s,UTF)) {
5251 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
5252 if (isLOWER(*s) && (tmp = keyword(PL_tokenbuf, len, 0))) {
5253 if (tmp < 0) tmp = -tmp;
5268 sv = newSVpvn(s, len);
5270 d = scan_str(d,TRUE,TRUE);
5272 /* MUST advance bufptr here to avoid bogus
5273 "at end of line" context messages from yyerror().
5275 PL_bufptr = s + len;
5276 yyerror("Unterminated attribute parameter in attribute list");
5280 return REPORT(0); /* EOF indicator */
5284 sv_catsv(sv, PL_lex_stuff);
5285 attrs = op_append_elem(OP_LIST, attrs,
5286 newSVOP(OP_CONST, 0, sv));
5287 SvREFCNT_dec(PL_lex_stuff);
5288 PL_lex_stuff = NULL;
5291 if (len == 6 && strnEQ(SvPVX(sv), "unique", len)) {
5293 if (PL_in_my == KEY_our) {
5294 deprecate(":unique");
5297 Perl_croak(aTHX_ "The 'unique' attribute may only be applied to 'our' variables");
5300 /* NOTE: any CV attrs applied here need to be part of
5301 the CVf_BUILTIN_ATTRS define in cv.h! */
5302 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "lvalue", len)) {
5304 CvLVALUE_on(PL_compcv);
5306 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "locked", len)) {
5308 deprecate(":locked");
5310 else if (!PL_in_my && len == 6 && strnEQ(SvPVX(sv), "method", len)) {
5312 CvMETHOD_on(PL_compcv);
5314 /* After we've set the flags, it could be argued that
5315 we don't need to do the attributes.pm-based setting
5316 process, and shouldn't bother appending recognized
5317 flags. To experiment with that, uncomment the
5318 following "else". (Note that's already been
5319 uncommented. That keeps the above-applied built-in
5320 attributes from being intercepted (and possibly
5321 rejected) by a package's attribute routines, but is
5322 justified by the performance win for the common case
5323 of applying only built-in attributes.) */
5325 attrs = op_append_elem(OP_LIST, attrs,
5326 newSVOP(OP_CONST, 0,
5330 if (*s == ':' && s[1] != ':')
5333 break; /* require real whitespace or :'s */
5334 /* XXX losing whitespace on sequential attributes here */
5338 = (PL_expect == XOPERATOR ? '=' : '{'); /*'}(' for vi */
5339 if (*s != ';' && *s != '}' && *s != tmp
5340 && (tmp != '=' || *s != ')')) {
5341 const char q = ((*s == '\'') ? '"' : '\'');
5342 /* If here for an expression, and parsed no attrs, back
5344 if (tmp == '=' && !attrs) {
5348 /* MUST advance bufptr here to avoid bogus "at end of line"
5349 context messages from yyerror().
5352 yyerror( (const char *)
5354 ? Perl_form(aTHX_ "Invalid separator character "
5355 "%c%c%c in attribute list", q, *s, q)
5356 : "Unterminated attribute list" ) );
5364 start_force(PL_curforce);
5365 NEXTVAL_NEXTTOKE.opval = attrs;
5366 CURMAD('_', PL_nextwhite);
5371 PL_thistoken = newSVpvn(SvPVX(PL_linestr) + stuffstart,
5372 (s - SvPVX(PL_linestr)) - stuffstart);
5380 if (PL_last_lop == PL_oldoldbufptr || PL_last_uni == PL_oldoldbufptr)
5381 PL_oldbufptr = PL_oldoldbufptr; /* allow print(STDOUT 123) */
5389 const char tmp = *s++;
5394 const char tmp = *s++;
5401 if (PL_lex_brackets && PL_lex_brackstack[PL_lex_brackets-1] == XFAKEEOF)
5404 if (PL_lex_brackets <= 0)
5405 yyerror("Unmatched right square bracket");
5408 if (PL_lex_state == LEX_INTERPNORMAL) {
5409 if (PL_lex_brackets == 0) {
5410 if (*s == '-' && s[1] == '>')
5411 PL_lex_state = LEX_INTERPENDMAYBE;
5412 else if (*s != '[' && *s != '{')
5413 PL_lex_state = LEX_INTERPEND;
5420 if (PL_lex_brackets > 100) {
5421 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
5423 switch (PL_expect) {
5425 if (PL_lex_formbrack) {
5429 if (PL_oldoldbufptr == PL_last_lop)
5430 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
5432 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
5433 OPERATOR(HASHBRACK);
5435 while (s < PL_bufend && SPACE_OR_TAB(*s))
5438 PL_tokenbuf[0] = '\0';
5439 if (d < PL_bufend && *d == '-') {
5440 PL_tokenbuf[0] = '-';
5442 while (d < PL_bufend && SPACE_OR_TAB(*d))
5445 if (d < PL_bufend && isIDFIRST_lazy_if(d,UTF)) {
5446 d = scan_word(d, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
5448 while (d < PL_bufend && SPACE_OR_TAB(*d))
5451 const char minus = (PL_tokenbuf[0] == '-');
5452 s = force_word(s + minus, WORD, FALSE, TRUE, FALSE);
5460 PL_lex_brackstack[PL_lex_brackets++] = XSTATE;
5465 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
5470 if (PL_oldoldbufptr == PL_last_lop)
5471 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
5473 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
5476 if (PL_expect == XREF && PL_lex_state == LEX_INTERPNORMAL) {
5478 /* This hack is to get the ${} in the message. */
5480 yyerror("syntax error");
5483 OPERATOR(HASHBRACK);
5485 /* This hack serves to disambiguate a pair of curlies
5486 * as being a block or an anon hash. Normally, expectation
5487 * determines that, but in cases where we're not in a
5488 * position to expect anything in particular (like inside
5489 * eval"") we have to resolve the ambiguity. This code
5490 * covers the case where the first term in the curlies is a
5491 * quoted string. Most other cases need to be explicitly
5492 * disambiguated by prepending a "+" before the opening
5493 * curly in order to force resolution as an anon hash.
5495 * XXX should probably propagate the outer expectation
5496 * into eval"" to rely less on this hack, but that could
5497 * potentially break current behavior of eval"".
5501 if (*s == '\'' || *s == '"' || *s == '`') {
5502 /* common case: get past first string, handling escapes */
5503 for (t++; t < PL_bufend && *t != *s;)
5504 if (*t++ == '\\' && (*t == '\\' || *t == *s))
5508 else if (*s == 'q') {
5511 || ((*t == 'q' || *t == 'x') && ++t < PL_bufend
5514 /* skip q//-like construct */
5516 char open, close, term;
5519 while (t < PL_bufend && isSPACE(*t))
5521 /* check for q => */
5522 if (t+1 < PL_bufend && t[0] == '=' && t[1] == '>') {
5523 OPERATOR(HASHBRACK);
5527 if (term && (tmps = strchr("([{< )]}> )]}>",term)))
5531 for (t++; t < PL_bufend; t++) {
5532 if (*t == '\\' && t+1 < PL_bufend && open != '\\')
5534 else if (*t == open)
5538 for (t++; t < PL_bufend; t++) {
5539 if (*t == '\\' && t+1 < PL_bufend)
5541 else if (*t == close && --brackets <= 0)
5543 else if (*t == open)
5550 /* skip plain q word */
5551 while (t < PL_bufend && isALNUM_lazy_if(t,UTF))
5554 else if (isALNUM_lazy_if(t,UTF)) {
5556 while (t < PL_bufend && isALNUM_lazy_if(t,UTF))
5559 while (t < PL_bufend && isSPACE(*t))
5561 /* if comma follows first term, call it an anon hash */
5562 /* XXX it could be a comma expression with loop modifiers */
5563 if (t < PL_bufend && ((*t == ',' && (*s == 'q' || !isLOWER(*s)))
5564 || (*t == '=' && t[1] == '>')))
5565 OPERATOR(HASHBRACK);
5566 if (PL_expect == XREF)
5569 PL_lex_brackstack[PL_lex_brackets-1] = XSTATE;
5575 pl_yylval.ival = CopLINE(PL_curcop);
5576 if (isSPACE(*s) || *s == '#')
5577 PL_copline = NOLINE; /* invalidate current command line number */
5580 if (PL_lex_brackets && PL_lex_brackstack[PL_lex_brackets-1] == XFAKEEOF)
5584 if (PL_lex_brackets <= 0)
5585 yyerror("Unmatched right curly bracket");
5587 PL_expect = (expectation)PL_lex_brackstack[--PL_lex_brackets];
5588 if (PL_lex_brackets < PL_lex_formbrack && PL_lex_state != LEX_INTERPNORMAL)
5589 PL_lex_formbrack = 0;
5590 if (PL_lex_state == LEX_INTERPNORMAL) {
5591 if (PL_lex_brackets == 0) {
5592 if (PL_expect & XFAKEBRACK) {
5593 PL_expect &= XENUMMASK;
5594 PL_lex_state = LEX_INTERPEND;
5599 PL_thiswhite = newSVpvs("");
5600 sv_catpvs(PL_thiswhite,"}");
5603 return yylex(); /* ignore fake brackets */
5605 if (*s == '-' && s[1] == '>')
5606 PL_lex_state = LEX_INTERPENDMAYBE;
5607 else if (*s != '[' && *s != '{')
5608 PL_lex_state = LEX_INTERPEND;
5611 if (PL_expect & XFAKEBRACK) {
5612 PL_expect &= XENUMMASK;
5614 return yylex(); /* ignore fake brackets */
5616 start_force(PL_curforce);
5618 curmad('X', newSVpvn(s-1,1));
5619 CURMAD('_', PL_thiswhite);
5624 PL_thistoken = newSVpvs("");
5632 if (PL_expect == XOPERATOR) {
5633 if (PL_bufptr == PL_linestart && ckWARN(WARN_SEMICOLON)
5634 && isIDFIRST_lazy_if(s,UTF))
5636 CopLINE_dec(PL_curcop);
5637 Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
5638 CopLINE_inc(PL_curcop);
5643 s = scan_ident(s - 1, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
5645 PL_expect = XOPERATOR;
5646 force_ident(PL_tokenbuf, '&');
5650 pl_yylval.ival = (OPpENTERSUB_AMPER<<8);
5662 const char tmp = *s++;
5669 if (tmp && isSPACE(*s) && ckWARN(WARN_SYNTAX)
5670 && strchr("+-*/%.^&|<",tmp))
5671 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5672 "Reversed %c= operator",(int)tmp);
5674 if (PL_expect == XSTATE && isALPHA(tmp) &&
5675 (s == PL_linestart+1 || s[-2] == '\n') )
5677 if (PL_in_eval && !PL_rsfp) {
5682 if (strnEQ(s,"=cut",4)) {
5698 PL_thiswhite = newSVpvs("");
5699 sv_catpvn(PL_thiswhite, PL_linestart,
5700 PL_bufend - PL_linestart);
5704 PL_parser->in_pod = 1;
5708 if (PL_lex_brackets < PL_lex_formbrack) {
5710 #ifdef PERL_STRICT_CR
5711 while (SPACE_OR_TAB(*t))
5713 while (SPACE_OR_TAB(*t) || *t == '\r')
5716 if (*t == '\n' || *t == '#') {
5727 const char tmp = *s++;
5729 /* was this !=~ where !~ was meant?
5730 * warn on m:!=~\s+([/?]|[msy]\W|tr\W): */
5732 if (*s == '~' && ckWARN(WARN_SYNTAX)) {
5733 const char *t = s+1;
5735 while (t < PL_bufend && isSPACE(*t))
5738 if (*t == '/' || *t == '?' ||
5739 ((*t == 'm' || *t == 's' || *t == 'y')
5740 && !isALNUM(t[1])) ||
5741 (*t == 't' && t[1] == 'r' && !isALNUM(t[2])))
5742 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5743 "!=~ should be !~");
5753 if (PL_expect != XOPERATOR) {
5754 if (s[1] != '<' && !strchr(s,'>'))
5757 s = scan_heredoc(s);
5759 s = scan_inputsymbol(s);
5760 TERM(sublex_start());
5766 SHop(OP_LEFT_SHIFT);
5780 const char tmp = *s++;
5782 SHop(OP_RIGHT_SHIFT);
5783 else if (tmp == '=')
5792 if (PL_expect == XOPERATOR) {
5793 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
5794 return deprecate_commaless_var_list();
5798 if (s[1] == '#' && (isIDFIRST_lazy_if(s+2,UTF) || strchr("{$:+-@", s[2]))) {
5799 PL_tokenbuf[0] = '@';
5800 s = scan_ident(s + 1, PL_bufend, PL_tokenbuf + 1,
5801 sizeof PL_tokenbuf - 1, FALSE);
5802 if (PL_expect == XOPERATOR)
5803 no_op("Array length", s);
5804 if (!PL_tokenbuf[1])
5806 PL_expect = XOPERATOR;
5807 PL_pending_ident = '#';
5811 PL_tokenbuf[0] = '$';
5812 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1,
5813 sizeof PL_tokenbuf - 1, FALSE);
5814 if (PL_expect == XOPERATOR)
5816 if (!PL_tokenbuf[1]) {
5818 yyerror("Final $ should be \\$ or $name");
5822 /* This kludge not intended to be bulletproof. */
5823 if (PL_tokenbuf[1] == '[' && !PL_tokenbuf[2]) {
5824 pl_yylval.opval = newSVOP(OP_CONST, 0,
5825 newSViv(CopARYBASE_get(&PL_compiling)));
5826 pl_yylval.opval->op_private = OPpCONST_ARYBASE;
5832 const char tmp = *s;
5833 if (PL_lex_state == LEX_NORMAL || PL_lex_brackets)
5836 if ((PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
5837 && intuit_more(s)) {
5839 PL_tokenbuf[0] = '@';
5840 if (ckWARN(WARN_SYNTAX)) {
5843 while (isSPACE(*t) || isALNUM_lazy_if(t,UTF) || *t == '$')
5846 PL_bufptr = PEEKSPACE(PL_bufptr); /* XXX can realloc */
5847 while (t < PL_bufend && *t != ']')
5849 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5850 "Multidimensional syntax %.*s not supported",
5851 (int)((t - PL_bufptr) + 1), PL_bufptr);
5855 else if (*s == '{') {
5857 PL_tokenbuf[0] = '%';
5858 if (strEQ(PL_tokenbuf+1, "SIG") && ckWARN(WARN_SYNTAX)
5859 && (t = strchr(s, '}')) && (t = strchr(t, '=')))
5861 char tmpbuf[sizeof PL_tokenbuf];
5864 } while (isSPACE(*t));
5865 if (isIDFIRST_lazy_if(t,UTF)) {
5867 t = scan_word(t, tmpbuf, sizeof tmpbuf, TRUE,
5871 if (*t == ';' && get_cvn_flags(tmpbuf, len, 0))
5872 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5873 "You need to quote \"%s\"",
5880 PL_expect = XOPERATOR;
5881 if (PL_lex_state == LEX_NORMAL && isSPACE((char)tmp)) {
5882 const bool islop = (PL_last_lop == PL_oldoldbufptr);
5883 if (!islop || PL_last_lop_op == OP_GREPSTART)
5884 PL_expect = XOPERATOR;
5885 else if (strchr("$@\"'`q", *s))
5886 PL_expect = XTERM; /* e.g. print $fh "foo" */
5887 else if (strchr("&*<%", *s) && isIDFIRST_lazy_if(s+1,UTF))
5888 PL_expect = XTERM; /* e.g. print $fh &sub */
5889 else if (isIDFIRST_lazy_if(s,UTF)) {
5890 char tmpbuf[sizeof PL_tokenbuf];
5892 scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
5893 if ((t2 = keyword(tmpbuf, len, 0))) {
5894 /* binary operators exclude handle interpretations */
5906 PL_expect = XTERM; /* e.g. print $fh length() */
5911 PL_expect = XTERM; /* e.g. print $fh subr() */
5914 else if (isDIGIT(*s))
5915 PL_expect = XTERM; /* e.g. print $fh 3 */
5916 else if (*s == '.' && isDIGIT(s[1]))
5917 PL_expect = XTERM; /* e.g. print $fh .3 */
5918 else if ((*s == '?' || *s == '-' || *s == '+')
5919 && !isSPACE(s[1]) && s[1] != '=')
5920 PL_expect = XTERM; /* e.g. print $fh -1 */
5921 else if (*s == '/' && !isSPACE(s[1]) && s[1] != '='
5923 PL_expect = XTERM; /* e.g. print $fh /.../
5924 XXX except DORDOR operator
5926 else if (*s == '<' && s[1] == '<' && !isSPACE(s[2])
5928 PL_expect = XTERM; /* print $fh <<"EOF" */
5931 PL_pending_ident = '$';
5935 if (PL_expect == XOPERATOR)
5937 PL_tokenbuf[0] = '@';
5938 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
5939 if (!PL_tokenbuf[1]) {
5942 if (PL_lex_state == LEX_NORMAL)
5944 if ((PL_expect != XREF || PL_oldoldbufptr == PL_last_lop) && intuit_more(s)) {
5946 PL_tokenbuf[0] = '%';
5948 /* Warn about @ where they meant $. */
5949 if (*s == '[' || *s == '{') {
5950 if (ckWARN(WARN_SYNTAX)) {
5951 const char *t = s + 1;
5952 while (*t && (isALNUM_lazy_if(t,UTF) || strchr(" \t$#+-'\"", *t)))
5954 if (*t == '}' || *t == ']') {
5956 PL_bufptr = PEEKSPACE(PL_bufptr); /* XXX can realloc */
5957 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5958 "Scalar value %.*s better written as $%.*s",
5959 (int)(t-PL_bufptr), PL_bufptr,
5960 (int)(t-PL_bufptr-1), PL_bufptr+1);
5965 PL_pending_ident = '@';
5968 case '/': /* may be division, defined-or, or pattern */
5969 if (PL_expect == XTERMORDORDOR && s[1] == '/') {
5973 case '?': /* may either be conditional or pattern */
5974 if (PL_expect == XOPERATOR) {
5982 /* A // operator. */
5992 /* Disable warning on "study /blah/" */
5993 if (PL_oldoldbufptr == PL_last_uni
5994 && (*PL_last_uni != 's' || s - PL_last_uni < 5
5995 || memNE(PL_last_uni, "study", 5)
5996 || isALNUM_lazy_if(PL_last_uni+5,UTF)
6000 deprecate("?PATTERN? without explicit operator");
6001 s = scan_pat(s,OP_MATCH);
6002 TERM(sublex_start());
6006 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack
6007 #ifdef PERL_STRICT_CR
6010 && (s[1] == '\n' || (s[1] == '\r' && s[2] == '\n'))
6012 && (s == PL_linestart || s[-1] == '\n') )
6014 PL_lex_formbrack = 0;
6018 if (PL_expect == XSTATE && s[1] == '.' && s[2] == '.') {
6022 if (PL_expect == XOPERATOR || !isDIGIT(s[1])) {
6028 pl_yylval.ival = OPf_SPECIAL;
6037 case '0': case '1': case '2': case '3': case '4':
6038 case '5': case '6': case '7': case '8': case '9':
6039 s = scan_num(s, &pl_yylval);
6040 DEBUG_T( { printbuf("### Saw number in %s\n", s); } );
6041 if (PL_expect == XOPERATOR)
6046 s = scan_str(s,!!PL_madskills,FALSE);
6047 DEBUG_T( { printbuf("### Saw string before %s\n", s); } );
6048 if (PL_expect == XOPERATOR) {
6049 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
6050 return deprecate_commaless_var_list();
6057 pl_yylval.ival = OP_CONST;
6058 TERM(sublex_start());
6061 s = scan_str(s,!!PL_madskills,FALSE);
6062 DEBUG_T( { printbuf("### Saw string before %s\n", s); } );
6063 if (PL_expect == XOPERATOR) {
6064 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
6065 return deprecate_commaless_var_list();
6072 pl_yylval.ival = OP_CONST;
6073 /* FIXME. I think that this can be const if char *d is replaced by
6074 more localised variables. */
6075 for (d = SvPV(PL_lex_stuff, len); len; len--, d++) {
6076 if (*d == '$' || *d == '@' || *d == '\\' || !UTF8_IS_INVARIANT((U8)*d)) {
6077 pl_yylval.ival = OP_STRINGIFY;
6081 TERM(sublex_start());
6084 s = scan_str(s,!!PL_madskills,FALSE);
6085 DEBUG_T( { printbuf("### Saw backtick string before %s\n", s); } );
6086 if (PL_expect == XOPERATOR)
6087 no_op("Backticks",s);
6090 readpipe_override();
6091 TERM(sublex_start());
6095 if (PL_lex_inwhat && isDIGIT(*s))
6096 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),"Can't use \\%c to mean $%c in expression",
6098 if (PL_expect == XOPERATOR)
6099 no_op("Backslash",s);
6103 if (isDIGIT(s[1]) && PL_expect != XOPERATOR) {
6104 char *start = s + 2;
6105 while (isDIGIT(*start) || *start == '_')
6107 if (*start == '.' && isDIGIT(start[1])) {
6108 s = scan_num(s, &pl_yylval);
6111 /* avoid v123abc() or $h{v1}, allow C<print v10;> */
6112 else if (!isALPHA(*start) && (PL_expect == XTERM
6113 || PL_expect == XREF || PL_expect == XSTATE
6114 || PL_expect == XTERMORDORDOR)) {
6115 GV *const gv = gv_fetchpvn_flags(s, start - s, 0, SVt_PVCV);
6117 s = scan_num(s, &pl_yylval);
6124 if (isDIGIT(s[1]) && PL_expect == XOPERATOR) {
6167 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
6169 /* Some keywords can be followed by any delimiter, including ':' */
6170 anydelim = word_takes_any_delimeter(PL_tokenbuf, len);
6172 /* x::* is just a word, unless x is "CORE" */
6173 if (!anydelim && *s == ':' && s[1] == ':' && strNE(PL_tokenbuf, "CORE"))
6177 while (d < PL_bufend && isSPACE(*d))
6178 d++; /* no comments skipped here, or s### is misparsed */
6180 /* Is this a word before a => operator? */
6181 if (*d == '=' && d[1] == '>') {
6184 = (OP*)newSVOP(OP_CONST, 0,
6185 S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
6186 pl_yylval.opval->op_private = OPpCONST_BARE;
6190 /* Check for plugged-in keyword */
6194 char *saved_bufptr = PL_bufptr;
6196 result = PL_keyword_plugin(aTHX_ PL_tokenbuf, len, &o);
6198 if (result == KEYWORD_PLUGIN_DECLINE) {
6199 /* not a plugged-in keyword */
6200 PL_bufptr = saved_bufptr;
6201 } else if (result == KEYWORD_PLUGIN_STMT) {
6202 pl_yylval.opval = o;
6205 return REPORT(PLUGSTMT);
6206 } else if (result == KEYWORD_PLUGIN_EXPR) {
6207 pl_yylval.opval = o;
6209 PL_expect = XOPERATOR;
6210 return REPORT(PLUGEXPR);
6212 Perl_croak(aTHX_ "Bad plugin affecting keyword '%s'",
6217 /* Check for built-in keyword */
6218 tmp = keyword(PL_tokenbuf, len, 0);
6220 /* Is this a label? */
6221 if (!anydelim && PL_expect == XSTATE
6222 && d < PL_bufend && *d == ':' && *(d + 1) != ':') {
6224 pl_yylval.pval = CopLABEL_alloc(PL_tokenbuf);
6229 if (tmp < 0) { /* second-class keyword? */
6230 GV *ogv = NULL; /* override (winner) */
6231 GV *hgv = NULL; /* hidden (loser) */
6232 if (PL_expect != XOPERATOR && (*s != ':' || s[1] != ':')) {
6234 if ((gv = gv_fetchpvn_flags(PL_tokenbuf, len, 0, SVt_PVCV)) &&
6237 if (GvIMPORTED_CV(gv))
6239 else if (! CvMETHOD(cv))
6243 (gvp = (GV**)hv_fetch(PL_globalstash,PL_tokenbuf,len,FALSE)) &&
6244 (gv = *gvp) && isGV_with_GP(gv) &&
6245 GvCVu(gv) && GvIMPORTED_CV(gv))
6252 tmp = 0; /* overridden by import or by GLOBAL */
6255 && -tmp==KEY_lock /* XXX generalizable kludge */
6258 tmp = 0; /* any sub overrides "weak" keyword */
6260 else { /* no override */
6262 if (tmp == KEY_dump) {
6263 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
6264 "dump() better written as CORE::dump()");
6268 if (hgv && tmp != KEY_x && tmp != KEY_CORE) /* never ambiguous */
6269 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
6270 "Ambiguous call resolved as CORE::%s(), "
6271 "qualify as such or use &",
6279 default: /* not a keyword */
6280 /* Trade off - by using this evil construction we can pull the
6281 variable gv into the block labelled keylookup. If not, then
6282 we have to give it function scope so that the goto from the
6283 earlier ':' case doesn't bypass the initialisation. */
6285 just_a_word_zero_gv:
6293 const char lastchar = (PL_bufptr == PL_oldoldbufptr ? 0 : PL_bufptr[-1]);
6297 SV *nextPL_nextwhite = 0;
6301 /* Get the rest if it looks like a package qualifier */
6303 if (*s == '\'' || (*s == ':' && s[1] == ':')) {
6305 s = scan_word(s, PL_tokenbuf + len, sizeof PL_tokenbuf - len,
6308 Perl_croak(aTHX_ "Bad name after %s%s", PL_tokenbuf,
6309 *s == '\'' ? "'" : "::");
6314 if (PL_expect == XOPERATOR) {
6315 if (PL_bufptr == PL_linestart) {
6316 CopLINE_dec(PL_curcop);
6317 Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
6318 CopLINE_inc(PL_curcop);
6321 no_op("Bareword",s);
6324 /* Look for a subroutine with this name in current package,
6325 unless name is "Foo::", in which case Foo is a bearword
6326 (and a package name). */
6328 if (len > 2 && !PL_madskills &&
6329 PL_tokenbuf[len - 2] == ':' && PL_tokenbuf[len - 1] == ':')
6331 if (ckWARN(WARN_BAREWORD)
6332 && ! gv_fetchpvn_flags(PL_tokenbuf, len, 0, SVt_PVHV))
6333 Perl_warner(aTHX_ packWARN(WARN_BAREWORD),
6334 "Bareword \"%s\" refers to nonexistent package",
6337 PL_tokenbuf[len] = '\0';
6343 /* Mustn't actually add anything to a symbol table.
6344 But also don't want to "initialise" any placeholder
6345 constants that might already be there into full
6346 blown PVGVs with attached PVCV. */
6347 gv = gv_fetchpvn_flags(PL_tokenbuf, len,
6348 GV_NOADD_NOINIT, SVt_PVCV);
6353 /* if we saw a global override before, get the right name */
6355 sv = S_newSV_maybe_utf8(aTHX_ PL_tokenbuf,
6356 len ? len : strlen(PL_tokenbuf));
6358 SV * const tmp_sv = sv;
6359 sv = newSVpvs("CORE::GLOBAL::");
6360 sv_catsv(sv, tmp_sv);
6361 SvREFCNT_dec(tmp_sv);
6365 if (PL_madskills && !PL_thistoken) {
6366 char *start = SvPVX(PL_linestr) + PL_realtokenstart;
6367 PL_thistoken = newSVpvn(start,s - start);
6368 PL_realtokenstart = s - SvPVX(PL_linestr);
6372 /* Presume this is going to be a bareword of some sort. */
6374 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
6375 pl_yylval.opval->op_private = OPpCONST_BARE;
6377 /* And if "Foo::", then that's what it certainly is. */
6382 OP *const_op = newSVOP(OP_CONST, 0, SvREFCNT_inc(sv));
6383 const_op->op_private = OPpCONST_BARE;
6384 rv2cv_op = newCVREF(0, const_op);
6386 cv = rv2cv_op_cv(rv2cv_op, 0);
6388 /* See if it's the indirect object for a list operator. */
6390 if (PL_oldoldbufptr &&
6391 PL_oldoldbufptr < PL_bufptr &&
6392 (PL_oldoldbufptr == PL_last_lop
6393 || PL_oldoldbufptr == PL_last_uni) &&
6394 /* NO SKIPSPACE BEFORE HERE! */
6395 (PL_expect == XREF ||
6396 ((PL_opargs[PL_last_lop_op] >> OASHIFT)& 7) == OA_FILEREF))
6398 bool immediate_paren = *s == '(';
6400 /* (Now we can afford to cross potential line boundary.) */
6401 s = SKIPSPACE2(s,nextPL_nextwhite);
6403 PL_nextwhite = nextPL_nextwhite; /* assume no & deception */
6406 /* Two barewords in a row may indicate method call. */
6408 if ((isIDFIRST_lazy_if(s,UTF) || *s == '$') &&
6409 (tmp = intuit_method(s, gv, cv))) {
6414 /* If not a declared subroutine, it's an indirect object. */
6415 /* (But it's an indir obj regardless for sort.) */
6416 /* Also, if "_" follows a filetest operator, it's a bareword */
6419 ( !immediate_paren && (PL_last_lop_op == OP_SORT ||
6421 (PL_last_lop_op != OP_MAPSTART &&
6422 PL_last_lop_op != OP_GREPSTART))))
6423 || (PL_tokenbuf[0] == '_' && PL_tokenbuf[1] == '\0'
6424 && ((PL_opargs[PL_last_lop_op] & OA_CLASS_MASK) == OA_FILESTATOP))
6427 PL_expect = (PL_last_lop == PL_oldoldbufptr) ? XTERM : XOPERATOR;
6432 PL_expect = XOPERATOR;
6435 s = SKIPSPACE2(s,nextPL_nextwhite);
6436 PL_nextwhite = nextPL_nextwhite;
6441 /* Is this a word before a => operator? */
6442 if (*s == '=' && s[1] == '>' && !pkgname) {
6445 sv_setpv(((SVOP*)pl_yylval.opval)->op_sv, PL_tokenbuf);
6446 if (UTF && !IN_BYTES && is_utf8_string((U8*)PL_tokenbuf, len))
6447 SvUTF8_on(((SVOP*)pl_yylval.opval)->op_sv);
6451 /* If followed by a paren, it's certainly a subroutine. */
6456 while (SPACE_OR_TAB(*d))
6458 if (*d == ')' && (sv = cv_const_sv(cv))) {
6465 PL_nextwhite = PL_thiswhite;
6468 start_force(PL_curforce);
6470 NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
6471 PL_expect = XOPERATOR;
6474 PL_nextwhite = nextPL_nextwhite;
6475 curmad('X', PL_thistoken);
6476 PL_thistoken = newSVpvs("");
6485 /* If followed by var or block, call it a method (unless sub) */
6487 if ((*s == '$' || *s == '{') && !cv) {
6489 PL_last_lop = PL_oldbufptr;
6490 PL_last_lop_op = OP_METHOD;
6494 /* If followed by a bareword, see if it looks like indir obj. */
6497 && (isIDFIRST_lazy_if(s,UTF) || *s == '$')
6498 && (tmp = intuit_method(s, gv, cv))) {
6503 /* Not a method, so call it a subroutine (if defined) */
6506 if (lastchar == '-')
6507 Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
6508 "Ambiguous use of -%s resolved as -&%s()",
6509 PL_tokenbuf, PL_tokenbuf);
6510 /* Check for a constant sub */
6511 if ((sv = cv_const_sv(cv))) {
6514 SvREFCNT_dec(((SVOP*)pl_yylval.opval)->op_sv);
6515 ((SVOP*)pl_yylval.opval)->op_sv = SvREFCNT_inc_simple(sv);
6516 pl_yylval.opval->op_private = 0;
6517 pl_yylval.opval->op_flags |= OPf_SPECIAL;
6521 op_free(pl_yylval.opval);
6522 pl_yylval.opval = rv2cv_op;
6523 pl_yylval.opval->op_private |= OPpENTERSUB_NOPAREN;
6524 PL_last_lop = PL_oldbufptr;
6525 PL_last_lop_op = OP_ENTERSUB;
6526 /* Is there a prototype? */
6534 const char *proto = SvPV_const(MUTABLE_SV(cv), protolen);
6537 while (*proto == ';')
6542 *proto == '$' || *proto == '_'
6543 || *proto == '*' || *proto == '+'
6548 *proto == '\\' && proto[1] && proto[2] == '\0'
6552 if (*proto == '\\' && proto[1] == '[') {
6553 const char *p = proto + 2;
6554 while(*p && *p != ']')
6556 if(*p == ']' && !p[1]) OPERATOR(UNIOPSUB);
6558 if (*proto == '&' && *s == '{') {
6560 sv_setpvs(PL_subname, "__ANON__");
6562 sv_setpvs(PL_subname, "__ANON__::__ANON__");
6569 PL_nextwhite = PL_thiswhite;
6572 start_force(PL_curforce);
6573 NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
6576 PL_nextwhite = nextPL_nextwhite;
6577 curmad('X', PL_thistoken);
6578 PL_thistoken = newSVpvs("");
6585 /* Guess harder when madskills require "best effort". */
6586 if (PL_madskills && (!gv || !GvCVu(gv))) {
6587 int probable_sub = 0;
6588 if (strchr("\"'`$@%0123456789!*+{[<", *s))
6590 else if (isALPHA(*s)) {
6594 d = scan_word(d, tmpbuf, sizeof tmpbuf, TRUE, &tmplen);
6595 if (!keyword(tmpbuf, tmplen, 0))
6598 while (d < PL_bufend && isSPACE(*d))
6600 if (*d == '=' && d[1] == '>')
6605 gv = gv_fetchpv(PL_tokenbuf, GV_ADD, SVt_PVCV);
6606 op_free(pl_yylval.opval);
6607 pl_yylval.opval = rv2cv_op;
6608 pl_yylval.opval->op_private |= OPpENTERSUB_NOPAREN;
6609 PL_last_lop = PL_oldbufptr;
6610 PL_last_lop_op = OP_ENTERSUB;
6611 PL_nextwhite = PL_thiswhite;
6613 start_force(PL_curforce);
6614 NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
6616 PL_nextwhite = nextPL_nextwhite;
6617 curmad('X', PL_thistoken);
6618 PL_thistoken = newSVpvs("");
6623 NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
6630 /* Call it a bare word */
6632 if (PL_hints & HINT_STRICT_SUBS)
6633 pl_yylval.opval->op_private |= OPpCONST_STRICT;
6636 /* after "print" and similar functions (corresponding to
6637 * "F? L" in opcode.pl), whatever wasn't already parsed as
6638 * a filehandle should be subject to "strict subs".
6639 * Likewise for the optional indirect-object argument to system
6640 * or exec, which can't be a bareword */
6641 if ((PL_last_lop_op == OP_PRINT
6642 || PL_last_lop_op == OP_PRTF
6643 || PL_last_lop_op == OP_SAY
6644 || PL_last_lop_op == OP_SYSTEM
6645 || PL_last_lop_op == OP_EXEC)
6646 && (PL_hints & HINT_STRICT_SUBS))
6647 pl_yylval.opval->op_private |= OPpCONST_STRICT;
6648 if (lastchar != '-') {
6649 if (ckWARN(WARN_RESERVED)) {
6653 if (!*d && !gv_stashpv(PL_tokenbuf, 0))
6654 Perl_warner(aTHX_ packWARN(WARN_RESERVED), PL_warn_reserved,
6662 if ((lastchar == '*' || lastchar == '%' || lastchar == '&')) {
6663 Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
6664 "Operator or semicolon missing before %c%s",
6665 lastchar, PL_tokenbuf);
6666 Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
6667 "Ambiguous use of %c resolved as operator %c",
6668 lastchar, lastchar);
6674 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0,
6675 newSVpv(CopFILE(PL_curcop),0));
6679 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0,
6680 Perl_newSVpvf(aTHX_ "%"IVdf, (IV)CopLINE(PL_curcop)));
6683 case KEY___PACKAGE__:
6684 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0,
6686 ? newSVhek(HvNAME_HEK(PL_curstash))
6693 if (PL_rsfp && (!PL_in_eval || PL_tokenbuf[2] == 'D')) {
6694 const char *pname = "main";
6695 if (PL_tokenbuf[2] == 'D')
6696 pname = HvNAME_get(PL_curstash ? PL_curstash : PL_defstash);
6697 gv = gv_fetchpv(Perl_form(aTHX_ "%s::DATA", pname), GV_ADD,
6701 GvIOp(gv) = newIO();
6702 IoIFP(GvIOp(gv)) = PL_rsfp;
6703 #if defined(HAS_FCNTL) && defined(F_SETFD)
6705 const int fd = PerlIO_fileno(PL_rsfp);
6706 fcntl(fd,F_SETFD,fd >= 3);
6709 /* Mark this internal pseudo-handle as clean */
6710 IoFLAGS(GvIOp(gv)) |= IOf_UNTAINT;
6711 if ((PerlIO*)PL_rsfp == PerlIO_stdin())
6712 IoTYPE(GvIOp(gv)) = IoTYPE_STD;
6714 IoTYPE(GvIOp(gv)) = IoTYPE_RDONLY;
6715 #if defined(WIN32) && !defined(PERL_TEXTMODE_SCRIPTS)
6716 /* if the script was opened in binmode, we need to revert
6717 * it to text mode for compatibility; but only iff it has CRs
6718 * XXX this is a questionable hack at best. */
6719 if (PL_bufend-PL_bufptr > 2
6720 && PL_bufend[-1] == '\n' && PL_bufend[-2] == '\r')
6723 if (IoTYPE(GvIOp(gv)) == IoTYPE_RDONLY) {
6724 loc = PerlIO_tell(PL_rsfp);
6725 (void)PerlIO_seek(PL_rsfp, 0L, 0);
6728 if (PerlLIO_setmode(PL_rsfp, O_TEXT) != -1) {
6730 if (PerlLIO_setmode(PerlIO_fileno(PL_rsfp), O_TEXT) != -1) {
6731 #endif /* NETWARE */
6732 #ifdef PERLIO_IS_STDIO /* really? */
6733 # if defined(__BORLANDC__)
6734 /* XXX see note in do_binmode() */
6735 ((FILE*)PL_rsfp)->flags &= ~_F_BIN;
6739 PerlIO_seek(PL_rsfp, loc, 0);
6743 #ifdef PERLIO_LAYERS
6746 PerlIO_apply_layers(aTHX_ PL_rsfp, NULL, ":utf8");
6747 else if (PL_encoding) {
6754 XPUSHs(PL_encoding);
6756 call_method("name", G_SCALAR);
6760 PerlIO_apply_layers(aTHX_ PL_rsfp, NULL,
6761 Perl_form(aTHX_ ":encoding(%"SVf")",
6770 if (PL_realtokenstart >= 0) {
6771 char *tstart = SvPVX(PL_linestr) + PL_realtokenstart;
6773 PL_endwhite = newSVpvs("");
6774 sv_catsv(PL_endwhite, PL_thiswhite);
6776 sv_catpvn(PL_endwhite, tstart, PL_bufend - tstart);
6777 PL_realtokenstart = -1;
6779 while ((s = filter_gets(PL_endwhite, SvCUR(PL_endwhite)))
6795 if (PL_expect == XSTATE) {
6802 if (*s == ':' && s[1] == ':') {
6805 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
6806 if (!(tmp = keyword(PL_tokenbuf, len, 0)))
6807 Perl_croak(aTHX_ "CORE::%s is not a keyword", PL_tokenbuf);
6810 else if (tmp == KEY_require || tmp == KEY_do)
6811 /* that's a way to remember we saw "CORE::" */
6824 LOP(OP_ACCEPT,XTERM);
6830 LOP(OP_ATAN2,XTERM);
6836 LOP(OP_BINMODE,XTERM);
6839 LOP(OP_BLESS,XTERM);
6848 /* When 'use switch' is in effect, continue has a dual
6849 life as a control operator. */
6851 if (!FEATURE_IS_ENABLED("switch"))
6854 /* We have to disambiguate the two senses of
6855 "continue". If the next token is a '{' then
6856 treat it as the start of a continue block;
6857 otherwise treat it as a control operator.
6869 (void)gv_fetchpvs("ENV", GV_ADD|GV_NOTQUAL, SVt_PVHV);
6886 if (!PL_cryptseen) {
6887 PL_cryptseen = TRUE;
6891 LOP(OP_CRYPT,XTERM);
6894 LOP(OP_CHMOD,XTERM);
6897 LOP(OP_CHOWN,XTERM);
6900 LOP(OP_CONNECT,XTERM);
6919 s = force_word(s,WORD,TRUE,TRUE,FALSE);
6920 if (orig_keyword == KEY_do) {
6929 PL_hints |= HINT_BLOCK_SCOPE;
6939 Perl_populate_isa(aTHX_ STR_WITH_LEN("AnyDBM_File::ISA"),
6940 STR_WITH_LEN("NDBM_File::"),
6941 STR_WITH_LEN("DB_File::"),
6942 STR_WITH_LEN("GDBM_File::"),
6943 STR_WITH_LEN("SDBM_File::"),
6944 STR_WITH_LEN("ODBM_File::"),
6946 LOP(OP_DBMOPEN,XTERM);
6952 s = force_word(s,WORD,TRUE,FALSE,FALSE);
6959 pl_yylval.ival = CopLINE(PL_curcop);
6975 if (*s == '{') { /* block eval */
6976 PL_expect = XTERMBLOCK;
6977 UNIBRACK(OP_ENTERTRY);
6979 else { /* string eval */
6981 UNIBRACK(OP_ENTEREVAL);
6996 case KEY_endhostent:
7002 case KEY_endservent:
7005 case KEY_endprotoent:
7016 pl_yylval.ival = CopLINE(PL_curcop);
7018 if (PL_expect == XSTATE && isIDFIRST_lazy_if(s,UTF)) {
7021 int soff = s - SvPVX(PL_linestr); /* for skipspace realloc */
7024 if ((PL_bufend - p) >= 3 &&
7025 strnEQ(p, "my", 2) && isSPACE(*(p + 2)))
7027 else if ((PL_bufend - p) >= 4 &&
7028 strnEQ(p, "our", 3) && isSPACE(*(p + 3)))
7031 if (isIDFIRST_lazy_if(p,UTF)) {
7032 p = scan_ident(p, PL_bufend,
7033 PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
7037 Perl_croak(aTHX_ "Missing $ on loop variable");
7039 s = SvPVX(PL_linestr) + soff;
7045 LOP(OP_FORMLINE,XTERM);
7051 LOP(OP_FCNTL,XTERM);
7057 LOP(OP_FLOCK,XTERM);
7066 LOP(OP_GREPSTART, XREF);
7069 s = force_word(s,WORD,TRUE,FALSE,FALSE);
7084 case KEY_getpriority:
7085 LOP(OP_GETPRIORITY,XTERM);
7087 case KEY_getprotobyname:
7090 case KEY_getprotobynumber:
7091 LOP(OP_GPBYNUMBER,XTERM);
7093 case KEY_getprotoent:
7105 case KEY_getpeername:
7106 UNI(OP_GETPEERNAME);
7108 case KEY_gethostbyname:
7111 case KEY_gethostbyaddr:
7112 LOP(OP_GHBYADDR,XTERM);
7114 case KEY_gethostent:
7117 case KEY_getnetbyname:
7120 case KEY_getnetbyaddr:
7121 LOP(OP_GNBYADDR,XTERM);
7126 case KEY_getservbyname:
7127 LOP(OP_GSBYNAME,XTERM);
7129 case KEY_getservbyport:
7130 LOP(OP_GSBYPORT,XTERM);
7132 case KEY_getservent:
7135 case KEY_getsockname:
7136 UNI(OP_GETSOCKNAME);
7138 case KEY_getsockopt:
7139 LOP(OP_GSOCKOPT,XTERM);
7154 pl_yylval.ival = CopLINE(PL_curcop);
7164 pl_yylval.ival = CopLINE(PL_curcop);
7168 LOP(OP_INDEX,XTERM);
7174 LOP(OP_IOCTL,XTERM);
7186 s = force_word(s,WORD,TRUE,FALSE,FALSE);
7218 LOP(OP_LISTEN,XTERM);
7227 s = scan_pat(s,OP_MATCH);
7228 TERM(sublex_start());
7231 LOP(OP_MAPSTART, XREF);
7234 LOP(OP_MKDIR,XTERM);
7237 LOP(OP_MSGCTL,XTERM);
7240 LOP(OP_MSGGET,XTERM);
7243 LOP(OP_MSGRCV,XTERM);
7246 LOP(OP_MSGSND,XTERM);
7251 PL_in_my = (U16)tmp;
7253 if (isIDFIRST_lazy_if(s,UTF)) {
7257 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
7258 if (len == 3 && strnEQ(PL_tokenbuf, "sub", 3))
7260 PL_in_my_stash = find_in_my_stash(PL_tokenbuf, len);
7261 if (!PL_in_my_stash) {
7264 my_snprintf(tmpbuf, sizeof(tmpbuf), "No such class %.1000s", PL_tokenbuf);
7268 if (PL_madskills) { /* just add type to declarator token */
7269 sv_catsv(PL_thistoken, PL_nextwhite);
7271 sv_catpvn(PL_thistoken, start, s - start);
7279 s = force_word(s,WORD,TRUE,FALSE,FALSE);
7286 s = tokenize_use(0, s);
7290 if (*s == '(' || (s = SKIPSPACE1(s), *s == '('))
7297 if (isIDFIRST_lazy_if(s,UTF)) {
7299 for (d = s; isALNUM_lazy_if(d,UTF);)
7301 for (t=d; isSPACE(*t);)
7303 if ( *t && strchr("|&*+-=!?:.", *t) && ckWARN_d(WARN_PRECEDENCE)
7305 && !(t[0] == '=' && t[1] == '>')
7307 int parms_len = (int)(d-s);
7308 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
7309 "Precedence problem: open %.*s should be open(%.*s)",
7310 parms_len, s, parms_len, s);
7316 pl_yylval.ival = OP_OR;
7326 LOP(OP_OPEN_DIR,XTERM);
7329 checkcomma(s,PL_tokenbuf,"filehandle");
7333 checkcomma(s,PL_tokenbuf,"filehandle");
7352 s = force_word(s,WORD,FALSE,TRUE,FALSE);
7354 s = force_strict_version(s);
7355 PL_lex_expect = XBLOCK;
7359 LOP(OP_PIPE_OP,XTERM);
7362 s = scan_str(s,!!PL_madskills,FALSE);
7365 pl_yylval.ival = OP_CONST;
7366 TERM(sublex_start());
7373 s = scan_str(s,!!PL_madskills,FALSE);
7376 PL_expect = XOPERATOR;
7377 if (SvCUR(PL_lex_stuff)) {
7379 d = SvPV_force(PL_lex_stuff, len);
7381 for (; isSPACE(*d) && len; --len, ++d)
7386 if (!warned && ckWARN(WARN_QW)) {
7387 for (; !isSPACE(*d) && len; --len, ++d) {
7389 Perl_warner(aTHX_ packWARN(WARN_QW),
7390 "Possible attempt to separate words with commas");
7393 else if (*d == '#') {
7394 Perl_warner(aTHX_ packWARN(WARN_QW),
7395 "Possible attempt to put comments in qw() list");
7401 for (; !isSPACE(*d) && len; --len, ++d)
7404 sv = newSVpvn_utf8(b, d-b, DO_UTF8(PL_lex_stuff));
7405 words = op_append_elem(OP_LIST, words,
7406 newSVOP(OP_CONST, 0, tokeq(sv)));
7411 words = newNULLLIST();
7413 SvREFCNT_dec(PL_lex_stuff);
7414 PL_lex_stuff = NULL;
7416 PL_expect = XOPERATOR;
7417 pl_yylval.opval = sawparens(words);
7422 s = scan_str(s,!!PL_madskills,FALSE);
7425 pl_yylval.ival = OP_STRINGIFY;
7426 if (SvIVX(PL_lex_stuff) == '\'')
7427 SvIV_set(PL_lex_stuff, 0); /* qq'$foo' should intepolate */
7428 TERM(sublex_start());
7431 s = scan_pat(s,OP_QR);
7432 TERM(sublex_start());
7435 s = scan_str(s,!!PL_madskills,FALSE);
7438 readpipe_override();
7439 TERM(sublex_start());
7447 s = force_version(s, FALSE);
7449 else if (*s != 'v' || !isDIGIT(s[1])
7450 || (s = force_version(s, TRUE), *s == 'v'))
7452 *PL_tokenbuf = '\0';
7453 s = force_word(s,WORD,TRUE,TRUE,FALSE);
7454 if (isIDFIRST_lazy_if(PL_tokenbuf,UTF))
7455 gv_stashpvn(PL_tokenbuf, strlen(PL_tokenbuf), GV_ADD);
7457 yyerror("<> should be quotes");
7459 if (orig_keyword == KEY_require) {
7467 PL_last_uni = PL_oldbufptr;
7468 PL_last_lop_op = OP_REQUIRE;
7470 return REPORT( (int)REQUIRE );
7476 s = force_word(s,WORD,TRUE,FALSE,FALSE);
7480 LOP(OP_RENAME,XTERM);
7489 LOP(OP_RINDEX,XTERM);
7498 UNIDOR(OP_READLINE);
7501 UNIDOR(OP_BACKTICK);
7510 LOP(OP_REVERSE,XTERM);
7513 UNIDOR(OP_READLINK);
7520 if (pl_yylval.opval)
7521 TERM(sublex_start());
7523 TOKEN(1); /* force error */
7526 checkcomma(s,PL_tokenbuf,"filehandle");
7536 LOP(OP_SELECT,XTERM);
7542 LOP(OP_SEMCTL,XTERM);
7545 LOP(OP_SEMGET,XTERM);
7548 LOP(OP_SEMOP,XTERM);
7554 LOP(OP_SETPGRP,XTERM);
7556 case KEY_setpriority:
7557 LOP(OP_SETPRIORITY,XTERM);
7559 case KEY_sethostent:
7565 case KEY_setservent:
7568 case KEY_setprotoent:
7578 LOP(OP_SEEKDIR,XTERM);
7580 case KEY_setsockopt:
7581 LOP(OP_SSOCKOPT,XTERM);
7587 LOP(OP_SHMCTL,XTERM);
7590 LOP(OP_SHMGET,XTERM);
7593 LOP(OP_SHMREAD,XTERM);
7596 LOP(OP_SHMWRITE,XTERM);
7599 LOP(OP_SHUTDOWN,XTERM);
7608 LOP(OP_SOCKET,XTERM);
7610 case KEY_socketpair:
7611 LOP(OP_SOCKPAIR,XTERM);
7614 checkcomma(s,PL_tokenbuf,"subroutine name");
7616 if (*s == ';' || *s == ')') /* probably a close */
7617 Perl_croak(aTHX_ "sort is now a reserved word");
7619 s = force_word(s,WORD,TRUE,TRUE,FALSE);
7623 LOP(OP_SPLIT,XTERM);
7626 LOP(OP_SPRINTF,XTERM);
7629 LOP(OP_SPLICE,XTERM);
7644 LOP(OP_SUBSTR,XTERM);
7650 char tmpbuf[sizeof PL_tokenbuf];
7651 SSize_t tboffset = 0;
7652 expectation attrful;
7653 bool have_name, have_proto;
7654 const int key = tmp;
7659 char *tstart = SvPVX(PL_linestr) + PL_realtokenstart;
7660 SV *subtoken = newSVpvn(tstart, s - tstart);
7664 s = SKIPSPACE2(s,tmpwhite);
7669 if (isIDFIRST_lazy_if(s,UTF) || *s == '\'' ||
7670 (*s == ':' && s[1] == ':'))
7673 SV *nametoke = NULL;
7677 attrful = XATTRBLOCK;
7678 /* remember buffer pos'n for later force_word */
7679 tboffset = s - PL_oldbufptr;
7680 d = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
7683 nametoke = newSVpvn(s, d - s);
7685 if (memchr(tmpbuf, ':', len))
7686 sv_setpvn(PL_subname, tmpbuf, len);
7688 sv_setsv(PL_subname,PL_curstname);
7689 sv_catpvs(PL_subname,"::");
7690 sv_catpvn(PL_subname,tmpbuf,len);
7697 CURMAD('X', nametoke);
7698 CURMAD('_', tmpwhite);
7699 (void) force_word(PL_oldbufptr + tboffset, WORD,
7702 s = SKIPSPACE2(d,tmpwhite);
7709 Perl_croak(aTHX_ "Missing name in \"my sub\"");
7710 PL_expect = XTERMBLOCK;
7711 attrful = XATTRTERM;
7712 sv_setpvs(PL_subname,"?");
7716 if (key == KEY_format) {
7718 PL_lex_formbrack = PL_lex_brackets + 1;
7720 PL_thistoken = subtoken;
7724 (void) force_word(PL_oldbufptr + tboffset, WORD,
7730 /* Look for a prototype */
7733 bool bad_proto = FALSE;
7734 bool in_brackets = FALSE;
7735 char greedy_proto = ' ';
7736 bool proto_after_greedy_proto = FALSE;
7737 bool must_be_last = FALSE;
7738 bool underscore = FALSE;
7739 bool seen_underscore = FALSE;
7740 const bool warnillegalproto = ckWARN(WARN_ILLEGALPROTO);
7742 s = scan_str(s,!!PL_madskills,FALSE);
7744 Perl_croak(aTHX_ "Prototype not terminated");
7745 /* strip spaces and check for bad characters */
7746 d = SvPVX(PL_lex_stuff);
7748 for (p = d; *p; ++p) {
7752 if (warnillegalproto) {
7754 proto_after_greedy_proto = TRUE;
7755 if (!strchr("$@%*;[]&\\_+", *p)) {
7767 else if ( *p == ']' ) {
7768 in_brackets = FALSE;
7770 else if ( (*p == '@' || *p == '%') &&
7771 ( tmp < 2 || d[tmp-2] != '\\' ) &&
7773 must_be_last = TRUE;
7776 else if ( *p == '_' ) {
7777 underscore = seen_underscore = TRUE;
7784 if (proto_after_greedy_proto)
7785 Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
7786 "Prototype after '%c' for %"SVf" : %s",
7787 greedy_proto, SVfARG(PL_subname), d);
7789 Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
7790 "Illegal character %sin prototype for %"SVf" : %s",
7791 seen_underscore ? "after '_' " : "",
7792 SVfARG(PL_subname), d);
7793 SvCUR_set(PL_lex_stuff, tmp);
7798 CURMAD('q', PL_thisopen);
7799 CURMAD('_', tmpwhite);
7800 CURMAD('=', PL_thisstuff);
7801 CURMAD('Q', PL_thisclose);
7802 NEXTVAL_NEXTTOKE.opval =
7803 (OP*)newSVOP(OP_CONST, 0, PL_lex_stuff);
7804 PL_lex_stuff = NULL;
7807 s = SKIPSPACE2(s,tmpwhite);
7815 if (*s == ':' && s[1] != ':')
7816 PL_expect = attrful;
7817 else if (*s != '{' && key == KEY_sub) {
7819 Perl_croak(aTHX_ "Illegal declaration of anonymous subroutine");
7820 else if (*s != ';' && *s != '}')
7821 Perl_croak(aTHX_ "Illegal declaration of subroutine %"SVf, SVfARG(PL_subname));
7828 curmad('^', newSVpvs(""));
7829 CURMAD('_', tmpwhite);
7833 PL_thistoken = subtoken;
7836 NEXTVAL_NEXTTOKE.opval =
7837 (OP*)newSVOP(OP_CONST, 0, PL_lex_stuff);
7838 PL_lex_stuff = NULL;
7844 sv_setpvs(PL_subname, "__ANON__");
7846 sv_setpvs(PL_subname, "__ANON__::__ANON__");
7850 (void) force_word(PL_oldbufptr + tboffset, WORD,
7859 LOP(OP_SYSTEM,XREF);
7862 LOP(OP_SYMLINK,XTERM);
7865 LOP(OP_SYSCALL,XTERM);
7868 LOP(OP_SYSOPEN,XTERM);
7871 LOP(OP_SYSSEEK,XTERM);
7874 LOP(OP_SYSREAD,XTERM);
7877 LOP(OP_SYSWRITE,XTERM);
7881 TERM(sublex_start());
7902 LOP(OP_TRUNCATE,XTERM);
7914 pl_yylval.ival = CopLINE(PL_curcop);
7918 pl_yylval.ival = CopLINE(PL_curcop);
7922 LOP(OP_UNLINK,XTERM);
7928 LOP(OP_UNPACK,XTERM);
7931 LOP(OP_UTIME,XTERM);
7937 LOP(OP_UNSHIFT,XTERM);
7940 s = tokenize_use(1, s);
7950 pl_yylval.ival = CopLINE(PL_curcop);
7954 pl_yylval.ival = CopLINE(PL_curcop);
7958 PL_hints |= HINT_BLOCK_SCOPE;
7965 LOP(OP_WAITPID,XTERM);
7974 ctl_l[0] = toCTRL('L');
7976 gv_fetchpvn_flags(ctl_l, 1, GV_ADD|GV_NOTQUAL, SVt_PV);
7979 /* Make sure $^L is defined */
7980 gv_fetchpvs("\f", GV_ADD|GV_NOTQUAL, SVt_PV);
7985 if (PL_expect == XOPERATOR)
7991 pl_yylval.ival = OP_XOR;
7996 TERM(sublex_start());
8001 #pragma segment Main
8005 S_pending_ident(pTHX)
8010 /* pit holds the identifier we read and pending_ident is reset */
8011 char pit = PL_pending_ident;
8012 const STRLEN tokenbuf_len = strlen(PL_tokenbuf);
8013 /* All routes through this function want to know if there is a colon. */
8014 const char *const has_colon = (const char*) memchr (PL_tokenbuf, ':', tokenbuf_len);
8015 PL_pending_ident = 0;
8017 /* PL_realtokenstart = realtokenend = PL_bufptr - SvPVX(PL_linestr); */
8018 DEBUG_T({ PerlIO_printf(Perl_debug_log,
8019 "### Pending identifier '%s'\n", PL_tokenbuf); });
8021 /* if we're in a my(), we can't allow dynamics here.
8022 $foo'bar has already been turned into $foo::bar, so
8023 just check for colons.
8025 if it's a legal name, the OP is a PADANY.
8028 if (PL_in_my == KEY_our) { /* "our" is merely analogous to "my" */
8030 yyerror(Perl_form(aTHX_ "No package name allowed for "
8031 "variable %s in \"our\"",
8033 tmp = allocmy(PL_tokenbuf, tokenbuf_len, 0);
8037 yyerror(Perl_form(aTHX_ PL_no_myglob,
8038 PL_in_my == KEY_my ? "my" : "state", PL_tokenbuf));
8040 pl_yylval.opval = newOP(OP_PADANY, 0);
8041 pl_yylval.opval->op_targ = allocmy(PL_tokenbuf, tokenbuf_len, 0);
8047 build the ops for accesses to a my() variable.
8049 Deny my($a) or my($b) in a sort block, *if* $a or $b is
8050 then used in a comparison. This catches most, but not
8051 all cases. For instance, it catches
8052 sort { my($a); $a <=> $b }
8054 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
8055 (although why you'd do that is anyone's guess).
8060 tmp = pad_findmy(PL_tokenbuf, tokenbuf_len, 0);
8061 if (tmp != NOT_IN_PAD) {
8062 /* might be an "our" variable" */
8063 if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
8064 /* build ops for a bareword */
8065 HV * const stash = PAD_COMPNAME_OURSTASH(tmp);
8066 HEK * const stashname = HvNAME_HEK(stash);
8067 SV * const sym = newSVhek(stashname);
8068 sv_catpvs(sym, "::");
8069 sv_catpvn(sym, PL_tokenbuf+1, tokenbuf_len - 1);
8070 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sym);
8071 pl_yylval.opval->op_private = OPpCONST_ENTERED;
8074 ? (GV_ADDMULTI | GV_ADDINEVAL)
8077 ((PL_tokenbuf[0] == '$') ? SVt_PV
8078 : (PL_tokenbuf[0] == '@') ? SVt_PVAV
8083 /* if it's a sort block and they're naming $a or $b */
8084 if (PL_last_lop_op == OP_SORT &&
8085 PL_tokenbuf[0] == '$' &&
8086 (PL_tokenbuf[1] == 'a' || PL_tokenbuf[1] == 'b')
8089 for (d = PL_in_eval ? PL_oldoldbufptr : PL_linestart;
8090 d < PL_bufend && *d != '\n';
8093 if (strnEQ(d,"<=>",3) || strnEQ(d,"cmp",3)) {
8094 Perl_croak(aTHX_ "Can't use \"my %s\" in sort comparison",
8100 pl_yylval.opval = newOP(OP_PADANY, 0);
8101 pl_yylval.opval->op_targ = tmp;
8107 Whine if they've said @foo in a doublequoted string,
8108 and @foo isn't a variable we can find in the symbol
8111 if (ckWARN(WARN_AMBIGUOUS) &&
8112 pit == '@' && PL_lex_state != LEX_NORMAL && !PL_lex_brackets) {
8113 GV *const gv = gv_fetchpvn_flags(PL_tokenbuf + 1, tokenbuf_len - 1, 0,
8115 if ((!gv || ((PL_tokenbuf[0] == '@') ? !GvAV(gv) : !GvHV(gv)))
8116 /* DO NOT warn for @- and @+ */
8117 && !( PL_tokenbuf[2] == '\0' &&
8118 ( PL_tokenbuf[1] == '-' || PL_tokenbuf[1] == '+' ))
8121 /* Downgraded from fatal to warning 20000522 mjd */
8122 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
8123 "Possible unintended interpolation of %s in string",
8128 /* build ops for a bareword */
8129 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, newSVpvn(PL_tokenbuf + 1,
8131 pl_yylval.opval->op_private = OPpCONST_ENTERED;
8132 gv_fetchpvn_flags(PL_tokenbuf+1, tokenbuf_len - 1,
8133 PL_in_eval ? (GV_ADDMULTI | GV_ADDINEVAL) : GV_ADD,
8134 ((PL_tokenbuf[0] == '$') ? SVt_PV
8135 : (PL_tokenbuf[0] == '@') ? SVt_PVAV
8141 * The following code was generated by perl_keyword.pl.
8145 Perl_keyword (pTHX_ const char *name, I32 len, bool all_keywords)
8149 PERL_ARGS_ASSERT_KEYWORD;
8153 case 1: /* 5 tokens of length 1 */
8185 case 2: /* 18 tokens of length 2 */
8331 case 3: /* 29 tokens of length 3 */
8335 if (name[1] == 'N' &&
8398 if (name[1] == 'i' &&
8430 if (name[1] == 'o' &&
8439 if (name[1] == 'e' &&
8448 if (name[1] == 'n' &&
8457 if (name[1] == 'o' &&
8466 if (name[1] == 'a' &&
8475 if (name[1] == 'o' &&
8537 if (name[1] == 'e' &&
8551 return (all_keywords || FEATURE_IS_ENABLED("say") ? KEY_say : 0);
8577 if (name[1] == 'i' &&
8586 if (name[1] == 's' &&
8595 if (name[1] == 'e' &&
8604 if (name[1] == 'o' &&
8616 case 4: /* 41 tokens of length 4 */
8620 if (name[1] == 'O' &&
8630 if (name[1] == 'N' &&
8640 if (name[1] == 'i' &&
8650 if (name[1] == 'h' &&
8660 if (name[1] == 'u' &&
8673 if (name[2] == 'c' &&
8682 if (name[2] == 's' &&
8691 if (name[2] == 'a' &&
8727 if (name[1] == 'o' &&
8740 if (name[2] == 't' &&
8749 if (name[2] == 'o' &&
8758 if (name[2] == 't' &&
8767 if (name[2] == 'e' &&
8780 if (name[1] == 'o' &&
8793 if (name[2] == 'y' &&
8802 if (name[2] == 'l' &&
8818 if (name[2] == 's' &&
8827 if (name[2] == 'n' &&
8836 if (name[2] == 'c' &&
8849 if (name[1] == 'e' &&
8859 if (name[1] == 'p' &&
8872 if (name[2] == 'c' &&
8881 if (name[2] == 'p' &&
8890 if (name[2] == 's' &&
8906 if (name[2] == 'n' &&
8976 if (name[2] == 'r' &&
8985 if (name[2] == 'r' &&
8994 if (name[2] == 'a' &&
9010 if (name[2] == 'l' &&
9072 if (name[2] == 'e' &&
9075 return (all_keywords || FEATURE_IS_ENABLED("switch") ? KEY_when : 0);
9088 case 5: /* 39 tokens of length 5 */
9092 if (name[1] == 'E' &&
9103 if (name[1] == 'H' &&
9117 if (name[2] == 'a' &&
9127 if (name[2] == 'a' &&
9144 if (name[2] == 'e' &&
9154 if (name[2] == 'e' &&
9158 return (all_keywords || FEATURE_IS_ENABLED("switch") ? -KEY_break : 0);
9174 if (name[3] == 'i' &&
9183 if (name[3] == 'o' &&
9219 if (name[2] == 'o' &&
9229 if (name[2] == 'y' &&
9243 if (name[1] == 'l' &&
9257 if (name[2] == 'n' &&
9267 if (name[2] == 'o' &&
9281 if (name[1] == 'i' &&
9286 return (all_keywords || FEATURE_IS_ENABLED("switch") ? KEY_given : 0);
9295 if (name[2] == 'd' &&
9305 if (name[2] == 'c' &&
9322 if (name[2] == 'c' &&
9332 if (name[2] == 't' &&
9346 if (name[1] == 'k' &&
9357 if (name[1] == 'r' &&
9371 if (name[2] == 's' &&
9381 if (name[2] == 'd' &&
9398 if (name[2] == 'm' &&
9408 if (name[2] == 'i' &&
9418 if (name[2] == 'e' &&
9428 if (name[2] == 'l' &&
9438 if (name[2] == 'a' &&
9451 if (name[3] == 't' &&
9454 return (all_keywords || FEATURE_IS_ENABLED("state") ? KEY_state : 0);
9460 if (name[3] == 'd' &&
9477 if (name[1] == 'i' &&
9491 if (name[2] == 'a' &&
9504 if (name[3] == 'e' &&
9539 if (name[2] == 'i' &&
9556 if (name[2] == 'i' &&
9566 if (name[2] == 'i' &&
9583 case 6: /* 33 tokens of length 6 */
9587 if (name[1] == 'c' &&
9602 if (name[2] == 'l' &&
9613 if (name[2] == 'r' &&
9628 if (name[1] == 'e' &&
9643 if (name[2] == 's' &&
9648 Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX), "elseif should be elsif");
9654 if (name[2] == 'i' &&
9672 if (name[2] == 'l' &&
9683 if (name[2] == 'r' &&
9698 if (name[1] == 'm' &&
9713 if (name[2] == 'n' &&
9724 if (name[2] == 's' &&
9739 if (name[1] == 's' &&
9745 if (name[4] == 't' &&
9754 if (name[4] == 'e' &&
9763 if (name[4] == 'c' &&
9772 if (name[4] == 'n' &&
9788 if (name[1] == 'r' &&
9806 if (name[3] == 'a' &&
9816 if (name[3] == 'u' &&
9830 if (name[2] == 'n' &&
9848 if (name[2] == 'a' &&
9862 if (name[3] == 'e' &&
9875 if (name[4] == 't' &&
9884 if (name[4] == 'e' &&
9906 if (name[4] == 't' &&
9915 if (name[4] == 'e' &&
9931 if (name[2] == 'c' &&
9942 if (name[2] == 'l' &&
9953 if (name[2] == 'b' &&
9964 if (name[2] == 's' &&
9987 if (name[4] == 's' &&
9996 if (name[4] == 'n' &&
10009 if (name[3] == 'a' &&
10013 return -KEY_unpack;
10026 if (name[1] == 'a' &&
10032 return -KEY_values;
10041 case 7: /* 29 tokens of length 7 */
10045 if (name[1] == 'E' &&
10052 return KEY_DESTROY;
10058 if (name[1] == '_' &&
10065 return KEY___END__;
10071 if (name[1] == 'i' &&
10078 return -KEY_binmode;
10084 if (name[1] == 'o' &&
10091 return -KEY_connect;
10100 if (name[2] == 'm' &&
10106 return -KEY_dbmopen;
10112 if (name[2] == 'f')
10117 if (name[4] == 'u' &&
10121 return (all_keywords || FEATURE_IS_ENABLED("switch") ? KEY_default : 0);
10127 if (name[4] == 'n' &&
10131 return KEY_defined;
10148 if (name[1] == 'o' &&
10155 return KEY_foreach;
10161 if (name[1] == 'e' &&
10168 if (name[5] == 'r' &&
10171 return -KEY_getpgrp;
10177 if (name[5] == 'i' &&
10180 return -KEY_getppid;
10193 if (name[1] == 'c' &&
10200 return -KEY_lcfirst;
10206 if (name[1] == 'p' &&
10213 return -KEY_opendir;
10219 if (name[1] == 'a' &&
10226 return KEY_package;
10232 if (name[1] == 'e')
10237 if (name[3] == 'd' &&
10242 return -KEY_readdir;
10248 if (name[3] == 'u' &&
10253 return KEY_require;
10259 if (name[3] == 'e' &&
10264 return -KEY_reverse;
10283 if (name[3] == 'k' &&
10288 return -KEY_seekdir;
10294 if (name[3] == 'p' &&
10299 return -KEY_setpgrp;
10309 if (name[2] == 'm' &&
10315 return -KEY_shmread;
10321 if (name[2] == 'r' &&
10327 return -KEY_sprintf;
10336 if (name[3] == 'l' &&
10341 return -KEY_symlink;
10350 if (name[4] == 'a' &&
10354 return -KEY_syscall;
10360 if (name[4] == 'p' &&
10364 return -KEY_sysopen;
10370 if (name[4] == 'e' &&
10374 return -KEY_sysread;
10380 if (name[4] == 'e' &&
10384 return -KEY_sysseek;
10402 if (name[1] == 'e' &&
10409 return -KEY_telldir;
10418 if (name[2] == 'f' &&
10424 return -KEY_ucfirst;
10430 if (name[2] == 's' &&
10436 return -KEY_unshift;
10446 if (name[1] == 'a' &&
10453 return -KEY_waitpid;
10462 case 8: /* 26 tokens of length 8 */
10466 if (name[1] == 'U' &&
10474 return KEY_AUTOLOAD;
10480 if (name[1] == '_')
10485 if (name[3] == 'A' &&
10491 return KEY___DATA__;
10497 if (name[3] == 'I' &&
10503 return -KEY___FILE__;
10509 if (name[3] == 'I' &&
10515 return -KEY___LINE__;
10531 if (name[2] == 'o' &&
10538 return -KEY_closedir;
10544 if (name[2] == 'n' &&
10551 return -KEY_continue;
10561 if (name[1] == 'b' &&
10569 return -KEY_dbmclose;
10575 if (name[1] == 'n' &&
10581 if (name[4] == 'r' &&
10586 return -KEY_endgrent;
10592 if (name[4] == 'w' &&
10597 return -KEY_endpwent;
10610 if (name[1] == 'o' &&
10618 return -KEY_formline;
10624 if (name[1] == 'e' &&
10630 if (name[4] == 'r')
10635 if (name[6] == 'n' &&
10638 return -KEY_getgrent;
10644 if (name[6] == 'i' &&
10647 return -KEY_getgrgid;
10653 if (name[6] == 'a' &&
10656 return -KEY_getgrnam;
10669 if (name[4] == 'o' &&
10674 return -KEY_getlogin;
10680 if (name[4] == 'w')
10685 if (name[6] == 'n' &&
10688 return -KEY_getpwent;
10694 if (name[6] == 'a' &&
10697 return -KEY_getpwnam;
10703 if (name[6] == 'i' &&
10706 return -KEY_getpwuid;
10726 if (name[1] == 'e' &&
10733 if (name[5] == 'i' &&
10740 return -KEY_readline;
10745 return -KEY_readlink;
10756 if (name[5] == 'i' &&
10760 return -KEY_readpipe;
10776 if (name[2] == 't')
10781 if (name[4] == 'r' &&
10786 return -KEY_setgrent;
10792 if (name[4] == 'w' &&
10797 return -KEY_setpwent;
10813 if (name[3] == 'w' &&
10819 return -KEY_shmwrite;
10825 if (name[3] == 't' &&
10831 return -KEY_shutdown;
10841 if (name[2] == 's' &&
10848 return -KEY_syswrite;
10858 if (name[1] == 'r' &&
10866 return -KEY_truncate;
10875 case 9: /* 9 tokens of length 9 */
10879 if (name[1] == 'N' &&
10888 return KEY_UNITCHECK;
10894 if (name[1] == 'n' &&
10903 return -KEY_endnetent;
10909 if (name[1] == 'e' &&
10918 return -KEY_getnetent;
10924 if (name[1] == 'o' &&
10933 return -KEY_localtime;
10939 if (name[1] == 'r' &&
10948 return KEY_prototype;
10954 if (name[1] == 'u' &&
10963 return -KEY_quotemeta;
10969 if (name[1] == 'e' &&
10978 return -KEY_rewinddir;
10984 if (name[1] == 'e' &&
10993 return -KEY_setnetent;
10999 if (name[1] == 'a' &&
11008 return -KEY_wantarray;
11017 case 10: /* 9 tokens of length 10 */
11021 if (name[1] == 'n' &&
11027 if (name[4] == 'o' &&
11034 return -KEY_endhostent;
11040 if (name[4] == 'e' &&
11047 return -KEY_endservent;
11060 if (name[1] == 'e' &&
11066 if (name[4] == 'o' &&
11073 return -KEY_gethostent;
11082 if (name[5] == 'r' &&
11088 return -KEY_getservent;
11094 if (name[5] == 'c' &&
11100 return -KEY_getsockopt;
11120 if (name[2] == 't')
11125 if (name[4] == 'o' &&
11132 return -KEY_sethostent;
11141 if (name[5] == 'r' &&
11147 return -KEY_setservent;
11153 if (name[5] == 'c' &&
11159 return -KEY_setsockopt;
11176 if (name[2] == 'c' &&
11185 return -KEY_socketpair;
11198 case 11: /* 8 tokens of length 11 */
11202 if (name[1] == '_' &&
11212 { /* __PACKAGE__ */
11213 return -KEY___PACKAGE__;
11219 if (name[1] == 'n' &&
11229 { /* endprotoent */
11230 return -KEY_endprotoent;
11236 if (name[1] == 'e' &&
11245 if (name[5] == 'e' &&
11251 { /* getpeername */
11252 return -KEY_getpeername;
11261 if (name[6] == 'o' &&
11266 { /* getpriority */
11267 return -KEY_getpriority;
11273 if (name[6] == 't' &&
11278 { /* getprotoent */
11279 return -KEY_getprotoent;
11293 if (name[4] == 'o' &&
11300 { /* getsockname */
11301 return -KEY_getsockname;
11314 if (name[1] == 'e' &&
11322 if (name[6] == 'o' &&
11327 { /* setpriority */
11328 return -KEY_setpriority;
11334 if (name[6] == 't' &&
11339 { /* setprotoent */
11340 return -KEY_setprotoent;
11356 case 12: /* 2 tokens of length 12 */
11357 if (name[0] == 'g' &&
11369 if (name[9] == 'd' &&
11372 { /* getnetbyaddr */
11373 return -KEY_getnetbyaddr;
11379 if (name[9] == 'a' &&
11382 { /* getnetbyname */
11383 return -KEY_getnetbyname;
11395 case 13: /* 4 tokens of length 13 */
11396 if (name[0] == 'g' &&
11403 if (name[4] == 'o' &&
11412 if (name[10] == 'd' &&
11415 { /* gethostbyaddr */
11416 return -KEY_gethostbyaddr;
11422 if (name[10] == 'a' &&
11425 { /* gethostbyname */
11426 return -KEY_gethostbyname;
11439 if (name[4] == 'e' &&
11448 if (name[10] == 'a' &&
11451 { /* getservbyname */
11452 return -KEY_getservbyname;
11458 if (name[10] == 'o' &&
11461 { /* getservbyport */
11462 return -KEY_getservbyport;
11481 case 14: /* 1 tokens of length 14 */
11482 if (name[0] == 'g' &&
11496 { /* getprotobyname */
11497 return -KEY_getprotobyname;
11502 case 16: /* 1 tokens of length 16 */
11503 if (name[0] == 'g' &&
11519 { /* getprotobynumber */
11520 return -KEY_getprotobynumber;
11534 S_checkcomma(pTHX_ const char *s, const char *name, const char *what)
11538 PERL_ARGS_ASSERT_CHECKCOMMA;
11540 if (*s == ' ' && s[1] == '(') { /* XXX gotta be a better way */
11541 if (ckWARN(WARN_SYNTAX)) {
11544 for (w = s+2; *w && level; w++) {
11547 else if (*w == ')')
11550 while (isSPACE(*w))
11552 /* the list of chars below is for end of statements or
11553 * block / parens, boolean operators (&&, ||, //) and branch
11554 * constructs (or, and, if, until, unless, while, err, for).
11555 * Not a very solid hack... */
11556 if (!*w || !strchr(";&/|})]oaiuwef!=", *w))
11557 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11558 "%s (...) interpreted as function",name);
11561 while (s < PL_bufend && isSPACE(*s))
11565 while (s < PL_bufend && isSPACE(*s))
11567 if (isIDFIRST_lazy_if(s,UTF)) {
11568 const char * const w = s++;
11569 while (isALNUM_lazy_if(s,UTF))
11571 while (s < PL_bufend && isSPACE(*s))
11575 if (keyword(w, s - w, 0))
11578 gv = gv_fetchpvn_flags(w, s - w, 0, SVt_PVCV);
11579 if (gv && GvCVu(gv))
11581 Perl_croak(aTHX_ "No comma allowed after %s", what);
11586 /* Either returns sv, or mortalizes sv and returns a new SV*.
11587 Best used as sv=new_constant(..., sv, ...).
11588 If s, pv are NULL, calls subroutine with one argument,
11589 and type is used with error messages only. */
11592 S_new_constant(pTHX_ const char *s, STRLEN len, const char *key, STRLEN keylen,
11593 SV *sv, SV *pv, const char *type, STRLEN typelen)
11596 HV * const table = GvHV(PL_hintgv); /* ^H */
11600 const char *why1 = "", *why2 = "", *why3 = "";
11602 PERL_ARGS_ASSERT_NEW_CONSTANT;
11604 if (!table || !(PL_hints & HINT_LOCALIZE_HH)) {
11607 why2 = (const char *)
11608 (strEQ(key,"charnames")
11609 ? "(possibly a missing \"use charnames ...\")"
11611 msg = Perl_newSVpvf(aTHX_ "Constant(%s) unknown: %s",
11612 (type ? type: "undef"), why2);
11614 /* This is convoluted and evil ("goto considered harmful")
11615 * but I do not understand the intricacies of all the different
11616 * failure modes of %^H in here. The goal here is to make
11617 * the most probable error message user-friendly. --jhi */
11622 msg = Perl_newSVpvf(aTHX_ "Constant(%s): %s%s%s",
11623 (type ? type: "undef"), why1, why2, why3);
11625 yyerror(SvPVX_const(msg));
11630 /* charnames doesn't work well if there have been errors found */
11631 if (PL_error_count > 0 && strEQ(key,"charnames"))
11632 return &PL_sv_undef;
11634 cvp = hv_fetch(table, key, keylen, FALSE);
11635 if (!cvp || !SvOK(*cvp)) {
11638 why3 = "} is not defined";
11641 sv_2mortal(sv); /* Parent created it permanently */
11644 pv = newSVpvn_flags(s, len, SVs_TEMP);
11646 typesv = newSVpvn_flags(type, typelen, SVs_TEMP);
11648 typesv = &PL_sv_undef;
11650 PUSHSTACKi(PERLSI_OVERLOAD);
11662 call_sv(cv, G_SCALAR | ( PL_in_eval ? 0 : G_EVAL));
11666 /* Check the eval first */
11667 if (!PL_in_eval && SvTRUE(ERRSV)) {
11668 sv_catpvs(ERRSV, "Propagated");
11669 yyerror(SvPV_nolen_const(ERRSV)); /* Duplicates the message inside eval */
11671 res = SvREFCNT_inc_simple(sv);
11675 SvREFCNT_inc_simple_void(res);
11684 why1 = "Call to &{$^H{";
11686 why3 = "}} did not return a defined value";
11694 /* Returns a NUL terminated string, with the length of the string written to
11698 S_scan_word(pTHX_ register char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp)
11701 register char *d = dest;
11702 register char * const e = d + destlen - 3; /* two-character token, ending NUL */
11704 PERL_ARGS_ASSERT_SCAN_WORD;
11708 Perl_croak(aTHX_ ident_too_long);
11709 if (isALNUM(*s)) /* UTF handled below */
11711 else if (allow_package && (*s == '\'') && isIDFIRST_lazy_if(s+1,UTF)) {
11716 else if (allow_package && (s[0] == ':') && (s[1] == ':') && (s[2] != '$')) {
11720 else if (UTF && UTF8_IS_START(*s) && isALNUM_utf8((U8*)s)) {
11721 char *t = s + UTF8SKIP(s);
11723 while (UTF8_IS_CONTINUED(*t) && is_utf8_mark((U8*)t))
11727 Perl_croak(aTHX_ ident_too_long);
11728 Copy(s, d, len, char);
11741 S_scan_ident(pTHX_ register char *s, register const char *send, char *dest, STRLEN destlen, I32 ck_uni)
11744 char *bracket = NULL;
11746 register char *d = dest;
11747 register char * const e = d + destlen - 3; /* two-character token, ending NUL */
11749 PERL_ARGS_ASSERT_SCAN_IDENT;
11754 while (isDIGIT(*s)) {
11756 Perl_croak(aTHX_ ident_too_long);
11763 Perl_croak(aTHX_ ident_too_long);
11764 if (isALNUM(*s)) /* UTF handled below */
11766 else if (*s == '\'' && isIDFIRST_lazy_if(s+1,UTF)) {
11771 else if (*s == ':' && s[1] == ':') {
11775 else if (UTF && UTF8_IS_START(*s) && isALNUM_utf8((U8*)s)) {
11776 char *t = s + UTF8SKIP(s);
11777 while (UTF8_IS_CONTINUED(*t) && is_utf8_mark((U8*)t))
11779 if (d + (t - s) > e)
11780 Perl_croak(aTHX_ ident_too_long);
11781 Copy(s, d, t - s, char);
11792 if (PL_lex_state != LEX_NORMAL)
11793 PL_lex_state = LEX_INTERPENDMAYBE;
11796 if (*s == '$' && s[1] &&
11797 (isALNUM_lazy_if(s+1,UTF) || s[1] == '$' || s[1] == '{' || strnEQ(s+1,"::",2)) )
11810 if (*d == '^' && *s && isCONTROLVAR(*s)) {
11815 if (isSPACE(s[-1])) {
11817 const char ch = *s++;
11818 if (!SPACE_OR_TAB(ch)) {
11824 if (isIDFIRST_lazy_if(d,UTF)) {
11828 while ((end < send && isALNUM_lazy_if(end,UTF)) || *end == ':') {
11829 end += UTF8SKIP(end);
11830 while (end < send && UTF8_IS_CONTINUED(*end) && is_utf8_mark((U8*)end))
11831 end += UTF8SKIP(end);
11833 Copy(s, d, end - s, char);
11838 while ((isALNUM(*s) || *s == ':') && d < e)
11841 Perl_croak(aTHX_ ident_too_long);
11844 while (s < send && SPACE_OR_TAB(*s))
11846 if ((*s == '[' || (*s == '{' && strNE(dest, "sub")))) {
11847 if (ckWARN(WARN_AMBIGUOUS) && keyword(dest, d - dest, 0)) {
11848 const char * const brack =
11850 ((*s == '[') ? "[...]" : "{...}");
11851 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
11852 "Ambiguous use of %c{%s%s} resolved to %c%s%s",
11853 funny, dest, brack, funny, dest, brack);
11856 PL_lex_brackstack[PL_lex_brackets++] = (char)(XOPERATOR | XFAKEBRACK);
11860 /* Handle extended ${^Foo} variables
11861 * 1999-02-27 mjd-perl-patch@plover.com */
11862 else if (!isALNUM(*d) && !isPRINT(*d) /* isCTRL(d) */
11866 while (isALNUM(*s) && d < e) {
11870 Perl_croak(aTHX_ ident_too_long);
11875 if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) {
11876 PL_lex_state = LEX_INTERPEND;
11879 if (PL_lex_state == LEX_NORMAL) {
11880 if (ckWARN(WARN_AMBIGUOUS) &&
11881 (keyword(dest, d - dest, 0)
11882 || get_cvn_flags(dest, d - dest, 0)))
11886 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
11887 "Ambiguous use of %c{%s} resolved to %c%s",
11888 funny, dest, funny, dest);
11893 s = bracket; /* let the parser handle it */
11897 else if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets && !intuit_more(s))
11898 PL_lex_state = LEX_INTERPEND;
11903 S_pmflag(U32 pmfl, const char ch) {
11905 CASE_STD_PMMOD_FLAGS_PARSE_SET(&pmfl);
11906 case GLOBAL_PAT_MOD: pmfl |= PMf_GLOBAL; break;
11907 case CONTINUE_PAT_MOD: pmfl |= PMf_CONTINUE; break;
11908 case ONCE_PAT_MOD: pmfl |= PMf_KEEP; break;
11909 case KEEPCOPY_PAT_MOD: pmfl |= PMf_KEEPCOPY; break;
11910 case NONDESTRUCT_PAT_MOD: pmfl |= PMf_NONDESTRUCT; break;
11916 S_scan_pat(pTHX_ char *start, I32 type)
11920 char *s = scan_str(start,!!PL_madskills,FALSE);
11921 const char * const valid_flags =
11922 (const char *)((type == OP_QR) ? QR_PAT_MODS : M_PAT_MODS);
11927 PERL_ARGS_ASSERT_SCAN_PAT;
11930 const char * const delimiter = skipspace(start);
11934 ? "Search pattern not terminated or ternary operator parsed as search pattern"
11935 : "Search pattern not terminated" ));
11938 pm = (PMOP*)newPMOP(type, 0);
11939 if (PL_multi_open == '?') {
11940 /* This is the only point in the code that sets PMf_ONCE: */
11941 pm->op_pmflags |= PMf_ONCE;
11943 /* Hence it's safe to do this bit of PMOP book-keeping here, which
11944 allows us to restrict the list needed by reset to just the ??
11946 assert(type != OP_TRANS);
11948 MAGIC *mg = mg_find((const SV *)PL_curstash, PERL_MAGIC_symtab);
11951 mg = sv_magicext(MUTABLE_SV(PL_curstash), 0, PERL_MAGIC_symtab, 0, 0,
11954 elements = mg->mg_len / sizeof(PMOP**);
11955 Renewc(mg->mg_ptr, elements + 1, PMOP*, char);
11956 ((PMOP**)mg->mg_ptr) [elements++] = pm;
11957 mg->mg_len = elements * sizeof(PMOP**);
11958 PmopSTASH_set(pm,PL_curstash);
11964 while (*s && strchr(valid_flags, *s))
11965 pm->op_pmflags = S_pmflag(pm->op_pmflags, *s++);
11968 Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX),
11969 "Having no space between pattern and following word is deprecated");
11973 if (PL_madskills && modstart != s) {
11974 SV* tmptoken = newSVpvn(modstart, s - modstart);
11975 append_madprops(newMADPROP('m', MAD_SV, tmptoken, 0), (OP*)pm, 0);
11978 /* issue a warning if /c is specified,but /g is not */
11979 if ((pm->op_pmflags & PMf_CONTINUE) && !(pm->op_pmflags & PMf_GLOBAL))
11981 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
11982 "Use of /c modifier is meaningless without /g" );
11985 PL_lex_op = (OP*)pm;
11986 pl_yylval.ival = OP_MATCH;
11991 S_scan_subst(pTHX_ char *start)
12002 PERL_ARGS_ASSERT_SCAN_SUBST;
12004 pl_yylval.ival = OP_NULL;
12006 s = scan_str(start,!!PL_madskills,FALSE);
12009 Perl_croak(aTHX_ "Substitution pattern not terminated");
12011 if (s[-1] == PL_multi_open)
12014 if (PL_madskills) {
12015 CURMAD('q', PL_thisopen);
12016 CURMAD('_', PL_thiswhite);
12017 CURMAD('E', PL_thisstuff);
12018 CURMAD('Q', PL_thisclose);
12019 PL_realtokenstart = s - SvPVX(PL_linestr);
12023 first_start = PL_multi_start;
12024 s = scan_str(s,!!PL_madskills,FALSE);
12026 if (PL_lex_stuff) {
12027 SvREFCNT_dec(PL_lex_stuff);
12028 PL_lex_stuff = NULL;
12030 Perl_croak(aTHX_ "Substitution replacement not terminated");
12032 PL_multi_start = first_start; /* so whole substitution is taken together */
12034 pm = (PMOP*)newPMOP(OP_SUBST, 0);
12037 if (PL_madskills) {
12038 CURMAD('z', PL_thisopen);
12039 CURMAD('R', PL_thisstuff);
12040 CURMAD('Z', PL_thisclose);
12046 if (*s == EXEC_PAT_MOD) {
12050 else if (strchr(S_PAT_MODS, *s))
12051 pm->op_pmflags = S_pmflag(pm->op_pmflags, *s++);
12054 Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX),
12055 "Having no space between pattern and following word is deprecated");
12063 if (PL_madskills) {
12065 curmad('m', newSVpvn(modstart, s - modstart));
12066 append_madprops(PL_thismad, (OP*)pm, 0);
12070 if ((pm->op_pmflags & PMf_CONTINUE)) {
12071 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), "Use of /c modifier is meaningless in s///" );
12075 SV * const repl = newSVpvs("");
12077 PL_sublex_info.super_bufptr = s;
12078 PL_sublex_info.super_bufend = PL_bufend;
12080 pm->op_pmflags |= PMf_EVAL;
12083 sv_catpvs(repl, "eval ");
12085 sv_catpvs(repl, "do ");
12087 sv_catpvs(repl, "{");
12088 sv_catsv(repl, PL_lex_repl);
12089 if (strchr(SvPVX(PL_lex_repl), '#'))
12090 sv_catpvs(repl, "\n");
12091 sv_catpvs(repl, "}");
12093 SvREFCNT_dec(PL_lex_repl);
12094 PL_lex_repl = repl;
12097 PL_lex_op = (OP*)pm;
12098 pl_yylval.ival = OP_SUBST;
12103 S_scan_trans(pTHX_ char *start)
12112 bool nondestruct = 0;
12117 PERL_ARGS_ASSERT_SCAN_TRANS;
12119 pl_yylval.ival = OP_NULL;
12121 s = scan_str(start,!!PL_madskills,FALSE);
12123 Perl_croak(aTHX_ "Transliteration pattern not terminated");
12125 if (s[-1] == PL_multi_open)
12128 if (PL_madskills) {
12129 CURMAD('q', PL_thisopen);
12130 CURMAD('_', PL_thiswhite);
12131 CURMAD('E', PL_thisstuff);
12132 CURMAD('Q', PL_thisclose);
12133 PL_realtokenstart = s - SvPVX(PL_linestr);
12137 s = scan_str(s,!!PL_madskills,FALSE);
12139 if (PL_lex_stuff) {
12140 SvREFCNT_dec(PL_lex_stuff);
12141 PL_lex_stuff = NULL;
12143 Perl_croak(aTHX_ "Transliteration replacement not terminated");
12145 if (PL_madskills) {
12146 CURMAD('z', PL_thisopen);
12147 CURMAD('R', PL_thisstuff);
12148 CURMAD('Z', PL_thisclose);
12151 complement = del = squash = 0;
12158 complement = OPpTRANS_COMPLEMENT;
12161 del = OPpTRANS_DELETE;
12164 squash = OPpTRANS_SQUASH;
12176 tbl = (short *)PerlMemShared_calloc(complement&&!del?258:256, sizeof(short));
12177 o = newPVOP(nondestruct ? OP_TRANSR : OP_TRANS, 0, (char*)tbl);
12178 o->op_private &= ~OPpTRANS_ALL;
12179 o->op_private |= del|squash|complement|
12180 (DO_UTF8(PL_lex_stuff)? OPpTRANS_FROM_UTF : 0)|
12181 (DO_UTF8(PL_lex_repl) ? OPpTRANS_TO_UTF : 0);
12184 pl_yylval.ival = nondestruct ? OP_TRANSR : OP_TRANS;
12187 if (PL_madskills) {
12189 curmad('m', newSVpvn(modstart, s - modstart));
12190 append_madprops(PL_thismad, o, 0);
12199 S_scan_heredoc(pTHX_ register char *s)
12203 I32 op_type = OP_SCALAR;
12207 const char *found_newline;
12211 const int outer = (PL_rsfp && !(PL_lex_inwhat == OP_SCALAR));
12213 I32 stuffstart = s - SvPVX(PL_linestr);
12216 PL_realtokenstart = -1;
12219 PERL_ARGS_ASSERT_SCAN_HEREDOC;
12223 e = PL_tokenbuf + sizeof PL_tokenbuf - 1;
12227 while (SPACE_OR_TAB(*peek))
12229 if (*peek == '`' || *peek == '\'' || *peek =='"') {
12232 s = delimcpy(d, e, s, PL_bufend, term, &len);
12242 if (!isALNUM_lazy_if(s,UTF))
12243 deprecate("bare << to mean <<\"\"");
12244 for (; isALNUM_lazy_if(s,UTF); s++) {
12249 if (d >= PL_tokenbuf + sizeof PL_tokenbuf - 1)
12250 Perl_croak(aTHX_ "Delimiter for here document is too long");
12253 len = d - PL_tokenbuf;
12256 if (PL_madskills) {
12257 tstart = PL_tokenbuf + !outer;
12258 PL_thisclose = newSVpvn(tstart, len - !outer);
12259 tstart = SvPVX(PL_linestr) + stuffstart;
12260 PL_thisopen = newSVpvn(tstart, s - tstart);
12261 stuffstart = s - SvPVX(PL_linestr);
12264 #ifndef PERL_STRICT_CR
12265 d = strchr(s, '\r');
12267 char * const olds = s;
12269 while (s < PL_bufend) {
12275 else if (*s == '\n' && s[1] == '\r') { /* \015\013 on a mac? */
12284 SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
12291 if ( outer || !(found_newline = (char*)memchr((void*)s, '\n', PL_bufend - s)) ) {
12292 herewas = newSVpvn(s,PL_bufend-s);
12296 herewas = newSVpvn(s-1,found_newline-s+1);
12299 herewas = newSVpvn(s,found_newline-s);
12303 if (PL_madskills) {
12304 tstart = SvPVX(PL_linestr) + stuffstart;
12306 sv_catpvn(PL_thisstuff, tstart, s - tstart);
12308 PL_thisstuff = newSVpvn(tstart, s - tstart);
12311 s += SvCUR(herewas);
12314 stuffstart = s - SvPVX(PL_linestr);
12320 tmpstr = newSV_type(SVt_PVIV);
12321 SvGROW(tmpstr, 80);
12322 if (term == '\'') {
12323 op_type = OP_CONST;
12324 SvIV_set(tmpstr, -1);
12326 else if (term == '`') {
12327 op_type = OP_BACKTICK;
12328 SvIV_set(tmpstr, '\\');
12332 PL_multi_start = CopLINE(PL_curcop);
12333 PL_multi_open = PL_multi_close = '<';
12334 term = *PL_tokenbuf;
12335 if (PL_lex_inwhat == OP_SUBST && PL_in_eval && !PL_rsfp) {
12336 char * const bufptr = PL_sublex_info.super_bufptr;
12337 char * const bufend = PL_sublex_info.super_bufend;
12338 char * const olds = s - SvCUR(herewas);
12339 s = strchr(bufptr, '\n');
12343 while (s < bufend &&
12344 (*s != term || memNE(s,PL_tokenbuf,len)) ) {
12346 CopLINE_inc(PL_curcop);
12349 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
12350 missingterm(PL_tokenbuf);
12352 sv_setpvn(herewas,bufptr,d-bufptr+1);
12353 sv_setpvn(tmpstr,d+1,s-d);
12355 sv_catpvn(herewas,s,bufend-s);
12356 Copy(SvPVX_const(herewas),bufptr,SvCUR(herewas) + 1,char);
12363 while (s < PL_bufend &&
12364 (*s != term || memNE(s,PL_tokenbuf,len)) ) {
12366 CopLINE_inc(PL_curcop);
12368 if (s >= PL_bufend) {
12369 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
12370 missingterm(PL_tokenbuf);
12372 sv_setpvn(tmpstr,d+1,s-d);
12374 if (PL_madskills) {
12376 sv_catpvn(PL_thisstuff, d + 1, s - d);
12378 PL_thisstuff = newSVpvn(d + 1, s - d);
12379 stuffstart = s - SvPVX(PL_linestr);
12383 CopLINE_inc(PL_curcop); /* the preceding stmt passes a newline */
12385 sv_catpvn(herewas,s,PL_bufend-s);
12386 sv_setsv(PL_linestr,herewas);
12387 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart = SvPVX(PL_linestr);
12388 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
12389 PL_last_lop = PL_last_uni = NULL;
12392 sv_setpvs(tmpstr,""); /* avoid "uninitialized" warning */
12393 while (s >= PL_bufend) { /* multiple line string? */
12395 if (PL_madskills) {
12396 tstart = SvPVX(PL_linestr) + stuffstart;
12398 sv_catpvn(PL_thisstuff, tstart, PL_bufend - tstart);
12400 PL_thisstuff = newSVpvn(tstart, PL_bufend - tstart);
12404 CopLINE_inc(PL_curcop);
12405 if (!outer || !lex_next_chunk(0)) {
12406 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
12407 missingterm(PL_tokenbuf);
12409 CopLINE_dec(PL_curcop);
12412 stuffstart = s - SvPVX(PL_linestr);
12414 CopLINE_inc(PL_curcop);
12415 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
12416 PL_last_lop = PL_last_uni = NULL;
12417 #ifndef PERL_STRICT_CR
12418 if (PL_bufend - PL_linestart >= 2) {
12419 if ((PL_bufend[-2] == '\r' && PL_bufend[-1] == '\n') ||
12420 (PL_bufend[-2] == '\n' && PL_bufend[-1] == '\r'))
12422 PL_bufend[-2] = '\n';
12424 SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
12426 else if (PL_bufend[-1] == '\r')
12427 PL_bufend[-1] = '\n';
12429 else if (PL_bufend - PL_linestart == 1 && PL_bufend[-1] == '\r')
12430 PL_bufend[-1] = '\n';
12432 if (*s == term && memEQ(s,PL_tokenbuf,len)) {
12433 STRLEN off = PL_bufend - 1 - SvPVX_const(PL_linestr);
12434 *(SvPVX(PL_linestr) + off ) = ' ';
12435 sv_catsv(PL_linestr,herewas);
12436 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
12437 s = SvPVX(PL_linestr) + off; /* In case PV of PL_linestr moved. */
12441 sv_catsv(tmpstr,PL_linestr);
12446 PL_multi_end = CopLINE(PL_curcop);
12447 if (SvCUR(tmpstr) + 5 < SvLEN(tmpstr)) {
12448 SvPV_shrink_to_cur(tmpstr);
12450 SvREFCNT_dec(herewas);
12452 if (UTF && is_utf8_string((U8*)SvPVX_const(tmpstr), SvCUR(tmpstr)))
12454 else if (PL_encoding)
12455 sv_recode_to_utf8(tmpstr, PL_encoding);
12457 PL_lex_stuff = tmpstr;
12458 pl_yylval.ival = op_type;
12462 /* scan_inputsymbol
12463 takes: current position in input buffer
12464 returns: new position in input buffer
12465 side-effects: pl_yylval and lex_op are set.
12470 <FH> read from filehandle
12471 <pkg::FH> read from package qualified filehandle
12472 <pkg'FH> read from package qualified filehandle
12473 <$fh> read from filehandle in $fh
12474 <*.h> filename glob
12479 S_scan_inputsymbol(pTHX_ char *start)
12482 register char *s = start; /* current position in buffer */
12485 char *d = PL_tokenbuf; /* start of temp holding space */
12486 const char * const e = PL_tokenbuf + sizeof PL_tokenbuf; /* end of temp holding space */
12488 PERL_ARGS_ASSERT_SCAN_INPUTSYMBOL;
12490 end = strchr(s, '\n');
12493 s = delimcpy(d, e, s + 1, end, '>', &len); /* extract until > */
12495 /* die if we didn't have space for the contents of the <>,
12496 or if it didn't end, or if we see a newline
12499 if (len >= (I32)sizeof PL_tokenbuf)
12500 Perl_croak(aTHX_ "Excessively long <> operator");
12502 Perl_croak(aTHX_ "Unterminated <> operator");
12507 Remember, only scalar variables are interpreted as filehandles by
12508 this code. Anything more complex (e.g., <$fh{$num}>) will be
12509 treated as a glob() call.
12510 This code makes use of the fact that except for the $ at the front,
12511 a scalar variable and a filehandle look the same.
12513 if (*d == '$' && d[1]) d++;
12515 /* allow <Pkg'VALUE> or <Pkg::VALUE> */
12516 while (*d && (isALNUM_lazy_if(d,UTF) || *d == '\'' || *d == ':'))
12519 /* If we've tried to read what we allow filehandles to look like, and
12520 there's still text left, then it must be a glob() and not a getline.
12521 Use scan_str to pull out the stuff between the <> and treat it
12522 as nothing more than a string.
12525 if (d - PL_tokenbuf != len) {
12526 pl_yylval.ival = OP_GLOB;
12527 s = scan_str(start,!!PL_madskills,FALSE);
12529 Perl_croak(aTHX_ "Glob not terminated");
12533 bool readline_overriden = FALSE;
12536 /* we're in a filehandle read situation */
12539 /* turn <> into <ARGV> */
12541 Copy("ARGV",d,5,char);
12543 /* Check whether readline() is overriden */
12544 gv_readline = gv_fetchpvs("readline", GV_NOTQUAL, SVt_PVCV);
12546 && GvCVu(gv_readline) && GvIMPORTED_CV(gv_readline))
12548 ((gvp = (GV**)hv_fetchs(PL_globalstash, "readline", FALSE))
12549 && (gv_readline = *gvp) && isGV_with_GP(gv_readline)
12550 && GvCVu(gv_readline) && GvIMPORTED_CV(gv_readline)))
12551 readline_overriden = TRUE;
12553 /* if <$fh>, create the ops to turn the variable into a
12557 /* try to find it in the pad for this block, otherwise find
12558 add symbol table ops
12560 const PADOFFSET tmp = pad_findmy(d, len, 0);
12561 if (tmp != NOT_IN_PAD) {
12562 if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
12563 HV * const stash = PAD_COMPNAME_OURSTASH(tmp);
12564 HEK * const stashname = HvNAME_HEK(stash);
12565 SV * const sym = sv_2mortal(newSVhek(stashname));
12566 sv_catpvs(sym, "::");
12567 sv_catpv(sym, d+1);
12572 OP * const o = newOP(OP_PADSV, 0);
12574 PL_lex_op = readline_overriden
12575 ? (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
12576 op_append_elem(OP_LIST, o,
12577 newCVREF(0, newGVOP(OP_GV,0,gv_readline))))
12578 : (OP*)newUNOP(OP_READLINE, 0, o);
12587 ? (GV_ADDMULTI | GV_ADDINEVAL)
12590 PL_lex_op = readline_overriden
12591 ? (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
12592 op_append_elem(OP_LIST,
12593 newUNOP(OP_RV2SV, 0, newGVOP(OP_GV, 0, gv)),
12594 newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
12595 : (OP*)newUNOP(OP_READLINE, 0,
12596 newUNOP(OP_RV2SV, 0,
12597 newGVOP(OP_GV, 0, gv)));
12599 if (!readline_overriden)
12600 PL_lex_op->op_flags |= OPf_SPECIAL;
12601 /* we created the ops in PL_lex_op, so make pl_yylval.ival a null op */
12602 pl_yylval.ival = OP_NULL;
12605 /* If it's none of the above, it must be a literal filehandle
12606 (<Foo::BAR> or <FOO>) so build a simple readline OP */
12608 GV * const gv = gv_fetchpv(d, GV_ADD, SVt_PVIO);
12609 PL_lex_op = readline_overriden
12610 ? (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
12611 op_append_elem(OP_LIST,
12612 newGVOP(OP_GV, 0, gv),
12613 newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
12614 : (OP*)newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, gv));
12615 pl_yylval.ival = OP_NULL;
12624 takes: start position in buffer
12625 keep_quoted preserve \ on the embedded delimiter(s)
12626 keep_delims preserve the delimiters around the string
12627 returns: position to continue reading from buffer
12628 side-effects: multi_start, multi_close, lex_repl or lex_stuff, and
12629 updates the read buffer.
12631 This subroutine pulls a string out of the input. It is called for:
12632 q single quotes q(literal text)
12633 ' single quotes 'literal text'
12634 qq double quotes qq(interpolate $here please)
12635 " double quotes "interpolate $here please"
12636 qx backticks qx(/bin/ls -l)
12637 ` backticks `/bin/ls -l`
12638 qw quote words @EXPORT_OK = qw( func() $spam )
12639 m// regexp match m/this/
12640 s/// regexp substitute s/this/that/
12641 tr/// string transliterate tr/this/that/
12642 y/// string transliterate y/this/that/
12643 ($*@) sub prototypes sub foo ($)
12644 (stuff) sub attr parameters sub foo : attr(stuff)
12645 <> readline or globs <FOO>, <>, <$fh>, or <*.c>
12647 In most of these cases (all but <>, patterns and transliterate)
12648 yylex() calls scan_str(). m// makes yylex() call scan_pat() which
12649 calls scan_str(). s/// makes yylex() call scan_subst() which calls
12650 scan_str(). tr/// and y/// make yylex() call scan_trans() which
12653 It skips whitespace before the string starts, and treats the first
12654 character as the delimiter. If the delimiter is one of ([{< then
12655 the corresponding "close" character )]}> is used as the closing
12656 delimiter. It allows quoting of delimiters, and if the string has
12657 balanced delimiters ([{<>}]) it allows nesting.
12659 On success, the SV with the resulting string is put into lex_stuff or,
12660 if that is already non-NULL, into lex_repl. The second case occurs only
12661 when parsing the RHS of the special constructs s/// and tr/// (y///).
12662 For convenience, the terminating delimiter character is stuffed into
12667 S_scan_str(pTHX_ char *start, int keep_quoted, int keep_delims)
12670 SV *sv; /* scalar value: string */
12671 const char *tmps; /* temp string, used for delimiter matching */
12672 register char *s = start; /* current position in the buffer */
12673 register char term; /* terminating character */
12674 register char *to; /* current position in the sv's data */
12675 I32 brackets = 1; /* bracket nesting level */
12676 bool has_utf8 = FALSE; /* is there any utf8 content? */
12677 I32 termcode; /* terminating char. code */
12678 U8 termstr[UTF8_MAXBYTES]; /* terminating string */
12679 STRLEN termlen; /* length of terminating string */
12680 int last_off = 0; /* last position for nesting bracket */
12686 PERL_ARGS_ASSERT_SCAN_STR;
12688 /* skip space before the delimiter */
12694 if (PL_realtokenstart >= 0) {
12695 stuffstart = PL_realtokenstart;
12696 PL_realtokenstart = -1;
12699 stuffstart = start - SvPVX(PL_linestr);
12701 /* mark where we are, in case we need to report errors */
12704 /* after skipping whitespace, the next character is the terminator */
12707 termcode = termstr[0] = term;
12711 termcode = utf8_to_uvchr((U8*)s, &termlen);
12712 Copy(s, termstr, termlen, U8);
12713 if (!UTF8_IS_INVARIANT(term))
12717 /* mark where we are */
12718 PL_multi_start = CopLINE(PL_curcop);
12719 PL_multi_open = term;
12721 /* find corresponding closing delimiter */
12722 if (term && (tmps = strchr("([{< )]}> )]}>",term)))
12723 termcode = termstr[0] = term = tmps[5];
12725 PL_multi_close = term;
12727 /* create a new SV to hold the contents. 79 is the SV's initial length.
12728 What a random number. */
12729 sv = newSV_type(SVt_PVIV);
12731 SvIV_set(sv, termcode);
12732 (void)SvPOK_only(sv); /* validate pointer */
12734 /* move past delimiter and try to read a complete string */
12736 sv_catpvn(sv, s, termlen);
12739 tstart = SvPVX(PL_linestr) + stuffstart;
12740 if (!PL_thisopen && !keep_delims) {
12741 PL_thisopen = newSVpvn(tstart, s - tstart);
12742 stuffstart = s - SvPVX(PL_linestr);
12746 if (PL_encoding && !UTF) {
12750 int offset = s - SvPVX_const(PL_linestr);
12751 const bool found = sv_cat_decode(sv, PL_encoding, PL_linestr,
12752 &offset, (char*)termstr, termlen);
12753 const char * const ns = SvPVX_const(PL_linestr) + offset;
12754 char * const svlast = SvEND(sv) - 1;
12756 for (; s < ns; s++) {
12757 if (*s == '\n' && !PL_rsfp)
12758 CopLINE_inc(PL_curcop);
12761 goto read_more_line;
12763 /* handle quoted delimiters */
12764 if (SvCUR(sv) > 1 && *(svlast-1) == '\\') {
12766 for (t = svlast-2; t >= SvPVX_const(sv) && *t == '\\';)
12768 if ((svlast-1 - t) % 2) {
12769 if (!keep_quoted) {
12770 *(svlast-1) = term;
12772 SvCUR_set(sv, SvCUR(sv) - 1);
12777 if (PL_multi_open == PL_multi_close) {
12783 for (t = w = SvPVX(sv)+last_off; t < svlast; w++, t++) {
12784 /* At here, all closes are "was quoted" one,
12785 so we don't check PL_multi_close. */
12787 if (!keep_quoted && *(t+1) == PL_multi_open)
12792 else if (*t == PL_multi_open)
12800 SvCUR_set(sv, w - SvPVX_const(sv));
12802 last_off = w - SvPVX(sv);
12803 if (--brackets <= 0)
12808 if (!keep_delims) {
12809 SvCUR_set(sv, SvCUR(sv) - 1);
12815 /* extend sv if need be */
12816 SvGROW(sv, SvCUR(sv) + (PL_bufend - s) + 1);
12817 /* set 'to' to the next character in the sv's string */
12818 to = SvPVX(sv)+SvCUR(sv);
12820 /* if open delimiter is the close delimiter read unbridle */
12821 if (PL_multi_open == PL_multi_close) {
12822 for (; s < PL_bufend; s++,to++) {
12823 /* embedded newlines increment the current line number */
12824 if (*s == '\n' && !PL_rsfp)
12825 CopLINE_inc(PL_curcop);
12826 /* handle quoted delimiters */
12827 if (*s == '\\' && s+1 < PL_bufend && term != '\\') {
12828 if (!keep_quoted && s[1] == term)
12830 /* any other quotes are simply copied straight through */
12834 /* terminate when run out of buffer (the for() condition), or
12835 have found the terminator */
12836 else if (*s == term) {
12839 if (s+termlen <= PL_bufend && memEQ(s, (char*)termstr, termlen))
12842 else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF)
12848 /* if the terminator isn't the same as the start character (e.g.,
12849 matched brackets), we have to allow more in the quoting, and
12850 be prepared for nested brackets.
12853 /* read until we run out of string, or we find the terminator */
12854 for (; s < PL_bufend; s++,to++) {
12855 /* embedded newlines increment the line count */
12856 if (*s == '\n' && !PL_rsfp)
12857 CopLINE_inc(PL_curcop);
12858 /* backslashes can escape the open or closing characters */
12859 if (*s == '\\' && s+1 < PL_bufend) {
12860 if (!keep_quoted &&
12861 ((s[1] == PL_multi_open) || (s[1] == PL_multi_close)))
12866 /* allow nested opens and closes */
12867 else if (*s == PL_multi_close && --brackets <= 0)
12869 else if (*s == PL_multi_open)
12871 else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF)
12876 /* terminate the copied string and update the sv's end-of-string */
12878 SvCUR_set(sv, to - SvPVX_const(sv));
12881 * this next chunk reads more into the buffer if we're not done yet
12885 break; /* handle case where we are done yet :-) */
12887 #ifndef PERL_STRICT_CR
12888 if (to - SvPVX_const(sv) >= 2) {
12889 if ((to[-2] == '\r' && to[-1] == '\n') ||
12890 (to[-2] == '\n' && to[-1] == '\r'))
12894 SvCUR_set(sv, to - SvPVX_const(sv));
12896 else if (to[-1] == '\r')
12899 else if (to - SvPVX_const(sv) == 1 && to[-1] == '\r')
12904 /* if we're out of file, or a read fails, bail and reset the current
12905 line marker so we can report where the unterminated string began
12908 if (PL_madskills) {
12909 char * const tstart = SvPVX(PL_linestr) + stuffstart;
12911 sv_catpvn(PL_thisstuff, tstart, PL_bufend - tstart);
12913 PL_thisstuff = newSVpvn(tstart, PL_bufend - tstart);
12916 CopLINE_inc(PL_curcop);
12917 PL_bufptr = PL_bufend;
12918 if (!lex_next_chunk(0)) {
12920 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
12929 /* at this point, we have successfully read the delimited string */
12931 if (!PL_encoding || UTF) {
12933 if (PL_madskills) {
12934 char * const tstart = SvPVX(PL_linestr) + stuffstart;
12935 const int len = s - tstart;
12937 sv_catpvn(PL_thisstuff, tstart, len);
12939 PL_thisstuff = newSVpvn(tstart, len);
12940 if (!PL_thisclose && !keep_delims)
12941 PL_thisclose = newSVpvn(s,termlen);
12946 sv_catpvn(sv, s, termlen);
12951 if (PL_madskills) {
12952 char * const tstart = SvPVX(PL_linestr) + stuffstart;
12953 const int len = s - tstart - termlen;
12955 sv_catpvn(PL_thisstuff, tstart, len);
12957 PL_thisstuff = newSVpvn(tstart, len);
12958 if (!PL_thisclose && !keep_delims)
12959 PL_thisclose = newSVpvn(s - termlen,termlen);
12963 if (has_utf8 || PL_encoding)
12966 PL_multi_end = CopLINE(PL_curcop);
12968 /* if we allocated too much space, give some back */
12969 if (SvCUR(sv) + 5 < SvLEN(sv)) {
12970 SvLEN_set(sv, SvCUR(sv) + 1);
12971 SvPV_renew(sv, SvLEN(sv));
12974 /* decide whether this is the first or second quoted string we've read
12987 takes: pointer to position in buffer
12988 returns: pointer to new position in buffer
12989 side-effects: builds ops for the constant in pl_yylval.op
12991 Read a number in any of the formats that Perl accepts:
12993 \d(_?\d)*(\.(\d(_?\d)*)?)?[Ee][\+\-]?(\d(_?\d)*) 12 12.34 12.
12994 \.\d(_?\d)*[Ee][\+\-]?(\d(_?\d)*) .34
12997 0x[0-9A-Fa-f](_?[0-9A-Fa-f])*
12999 Like most scan_ routines, it uses the PL_tokenbuf buffer to hold the
13002 If it reads a number without a decimal point or an exponent, it will
13003 try converting the number to an integer and see if it can do so
13004 without loss of precision.
13008 Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
13011 register const char *s = start; /* current position in buffer */
13012 register char *d; /* destination in temp buffer */
13013 register char *e; /* end of temp buffer */
13014 NV nv; /* number read, as a double */
13015 SV *sv = NULL; /* place to put the converted number */
13016 bool floatit; /* boolean: int or float? */
13017 const char *lastub = NULL; /* position of last underbar */
13018 static char const number_too_long[] = "Number too long";
13020 PERL_ARGS_ASSERT_SCAN_NUM;
13022 /* We use the first character to decide what type of number this is */
13026 Perl_croak(aTHX_ "panic: scan_num");
13028 /* if it starts with a 0, it could be an octal number, a decimal in
13029 0.13 disguise, or a hexadecimal number, or a binary number. */
13033 u holds the "number so far"
13034 shift the power of 2 of the base
13035 (hex == 4, octal == 3, binary == 1)
13036 overflowed was the number more than we can hold?
13038 Shift is used when we add a digit. It also serves as an "are
13039 we in octal/hex/binary?" indicator to disallow hex characters
13040 when in octal mode.
13045 bool overflowed = FALSE;
13046 bool just_zero = TRUE; /* just plain 0 or binary number? */
13047 static const NV nvshift[5] = { 1.0, 2.0, 4.0, 8.0, 16.0 };
13048 static const char* const bases[5] =
13049 { "", "binary", "", "octal", "hexadecimal" };
13050 static const char* const Bases[5] =
13051 { "", "Binary", "", "Octal", "Hexadecimal" };
13052 static const char* const maxima[5] =
13054 "0b11111111111111111111111111111111",
13058 const char *base, *Base, *max;
13060 /* check for hex */
13061 if (s[1] == 'x' || s[1] == 'X') {
13065 } else if (s[1] == 'b' || s[1] == 'B') {
13070 /* check for a decimal in disguise */
13071 else if (s[1] == '.' || s[1] == 'e' || s[1] == 'E')
13073 /* so it must be octal */
13080 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13081 "Misplaced _ in number");
13085 base = bases[shift];
13086 Base = Bases[shift];
13087 max = maxima[shift];
13089 /* read the rest of the number */
13091 /* x is used in the overflow test,
13092 b is the digit we're adding on. */
13097 /* if we don't mention it, we're done */
13101 /* _ are ignored -- but warned about if consecutive */
13103 if (lastub && s == lastub + 1)
13104 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13105 "Misplaced _ in number");
13109 /* 8 and 9 are not octal */
13110 case '8': case '9':
13112 yyerror(Perl_form(aTHX_ "Illegal octal digit '%c'", *s));
13116 case '2': case '3': case '4':
13117 case '5': case '6': case '7':
13119 yyerror(Perl_form(aTHX_ "Illegal binary digit '%c'", *s));
13122 case '0': case '1':
13123 b = *s++ & 15; /* ASCII digit -> value of digit */
13127 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
13128 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
13129 /* make sure they said 0x */
13132 b = (*s++ & 7) + 9;
13134 /* Prepare to put the digit we have onto the end
13135 of the number so far. We check for overflows.
13141 x = u << shift; /* make room for the digit */
13143 if ((x >> shift) != u
13144 && !(PL_hints & HINT_NEW_BINARY)) {
13147 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
13148 "Integer overflow in %s number",
13151 u = x | b; /* add the digit to the end */
13154 n *= nvshift[shift];
13155 /* If an NV has not enough bits in its
13156 * mantissa to represent an UV this summing of
13157 * small low-order numbers is a waste of time
13158 * (because the NV cannot preserve the
13159 * low-order bits anyway): we could just
13160 * remember when did we overflow and in the
13161 * end just multiply n by the right
13169 /* if we get here, we had success: make a scalar value from
13174 /* final misplaced underbar check */
13175 if (s[-1] == '_') {
13176 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Misplaced _ in number");
13180 if (n > 4294967295.0)
13181 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
13182 "%s number > %s non-portable",
13188 if (u > 0xffffffff)
13189 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
13190 "%s number > %s non-portable",
13195 if (just_zero && (PL_hints & HINT_NEW_INTEGER))
13196 sv = new_constant(start, s - start, "integer",
13197 sv, NULL, NULL, 0);
13198 else if (PL_hints & HINT_NEW_BINARY)
13199 sv = new_constant(start, s - start, "binary", sv, NULL, NULL, 0);
13204 handle decimal numbers.
13205 we're also sent here when we read a 0 as the first digit
13207 case '1': case '2': case '3': case '4': case '5':
13208 case '6': case '7': case '8': case '9': case '.':
13211 e = PL_tokenbuf + sizeof PL_tokenbuf - 6; /* room for various punctuation */
13214 /* read next group of digits and _ and copy into d */
13215 while (isDIGIT(*s) || *s == '_') {
13216 /* skip underscores, checking for misplaced ones
13220 if (lastub && s == lastub + 1)
13221 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13222 "Misplaced _ in number");
13226 /* check for end of fixed-length buffer */
13228 Perl_croak(aTHX_ number_too_long);
13229 /* if we're ok, copy the character */
13234 /* final misplaced underbar check */
13235 if (lastub && s == lastub + 1) {
13236 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Misplaced _ in number");
13239 /* read a decimal portion if there is one. avoid
13240 3..5 being interpreted as the number 3. followed
13243 if (*s == '.' && s[1] != '.') {
13248 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13249 "Misplaced _ in number");
13253 /* copy, ignoring underbars, until we run out of digits.
13255 for (; isDIGIT(*s) || *s == '_'; s++) {
13256 /* fixed length buffer check */
13258 Perl_croak(aTHX_ number_too_long);
13260 if (lastub && s == lastub + 1)
13261 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13262 "Misplaced _ in number");
13268 /* fractional part ending in underbar? */
13269 if (s[-1] == '_') {
13270 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13271 "Misplaced _ in number");
13273 if (*s == '.' && isDIGIT(s[1])) {
13274 /* oops, it's really a v-string, but without the "v" */
13280 /* read exponent part, if present */
13281 if ((*s == 'e' || *s == 'E') && strchr("+-0123456789_", s[1])) {
13285 /* regardless of whether user said 3E5 or 3e5, use lower 'e' */
13286 *d++ = 'e'; /* At least some Mach atof()s don't grok 'E' */
13288 /* stray preinitial _ */
13290 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13291 "Misplaced _ in number");
13295 /* allow positive or negative exponent */
13296 if (*s == '+' || *s == '-')
13299 /* stray initial _ */
13301 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13302 "Misplaced _ in number");
13306 /* read digits of exponent */
13307 while (isDIGIT(*s) || *s == '_') {
13310 Perl_croak(aTHX_ number_too_long);
13314 if (((lastub && s == lastub + 1) ||
13315 (!isDIGIT(s[1]) && s[1] != '_')))
13316 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
13317 "Misplaced _ in number");
13325 We try to do an integer conversion first if no characters
13326 indicating "float" have been found.
13331 const int flags = grok_number (PL_tokenbuf, d - PL_tokenbuf, &uv);
13333 if (flags == IS_NUMBER_IN_UV) {
13335 sv = newSViv(uv); /* Prefer IVs over UVs. */
13338 } else if (flags == (IS_NUMBER_IN_UV | IS_NUMBER_NEG)) {
13339 if (uv <= (UV) IV_MIN)
13340 sv = newSViv(-(IV)uv);
13347 /* terminate the string */
13349 nv = Atof(PL_tokenbuf);
13354 ? (PL_hints & HINT_NEW_FLOAT) : (PL_hints & HINT_NEW_INTEGER) ) {
13355 const char *const key = floatit ? "float" : "integer";
13356 const STRLEN keylen = floatit ? 5 : 7;
13357 sv = S_new_constant(aTHX_ PL_tokenbuf, d - PL_tokenbuf,
13358 key, keylen, sv, NULL, NULL, 0);
13362 /* if it starts with a v, it could be a v-string */
13365 sv = newSV(5); /* preallocate storage space */
13366 s = scan_vstring(s, PL_bufend, sv);
13370 /* make the op for the constant and return */
13373 lvalp->opval = newSVOP(OP_CONST, 0, sv);
13375 lvalp->opval = NULL;
13381 S_scan_formline(pTHX_ register char *s)
13384 register char *eol;
13386 SV * const stuff = newSVpvs("");
13387 bool needargs = FALSE;
13388 bool eofmt = FALSE;
13390 char *tokenstart = s;
13391 SV* savewhite = NULL;
13393 if (PL_madskills) {
13394 savewhite = PL_thiswhite;
13399 PERL_ARGS_ASSERT_SCAN_FORMLINE;
13401 while (!needargs) {
13404 #ifdef PERL_STRICT_CR
13405 while (SPACE_OR_TAB(*t))
13408 while (SPACE_OR_TAB(*t) || *t == '\r')
13411 if (*t == '\n' || t == PL_bufend) {
13416 if (PL_in_eval && !PL_rsfp) {
13417 eol = (char *) memchr(s,'\n',PL_bufend-s);
13422 eol = PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
13424 for (t = s; t < eol; t++) {
13425 if (*t == '~' && t[1] == '~' && SvCUR(stuff)) {
13427 goto enough; /* ~~ must be first line in formline */
13429 if (*t == '@' || *t == '^')
13433 sv_catpvn(stuff, s, eol-s);
13434 #ifndef PERL_STRICT_CR
13435 if (eol-s > 1 && eol[-2] == '\r' && eol[-1] == '\n') {
13436 char *end = SvPVX(stuff) + SvCUR(stuff);
13439 SvCUR_set(stuff, SvCUR(stuff) - 1);
13450 if (PL_madskills) {
13452 sv_catpvn(PL_thistoken, tokenstart, PL_bufend - tokenstart);
13454 PL_thistoken = newSVpvn(tokenstart, PL_bufend - tokenstart);
13457 PL_bufptr = PL_bufend;
13458 CopLINE_inc(PL_curcop);
13459 got_some = lex_next_chunk(0);
13460 CopLINE_dec(PL_curcop);
13463 tokenstart = PL_bufptr;
13471 if (SvCUR(stuff)) {
13474 PL_lex_state = LEX_NORMAL;
13475 start_force(PL_curforce);
13476 NEXTVAL_NEXTTOKE.ival = 0;
13480 PL_lex_state = LEX_FORMLINE;
13482 if (UTF && is_utf8_string((U8*)SvPVX_const(stuff), SvCUR(stuff)))
13484 else if (PL_encoding)
13485 sv_recode_to_utf8(stuff, PL_encoding);
13487 start_force(PL_curforce);
13488 NEXTVAL_NEXTTOKE.opval = (OP*)newSVOP(OP_CONST, 0, stuff);
13490 start_force(PL_curforce);
13491 NEXTVAL_NEXTTOKE.ival = OP_FORMLINE;
13495 SvREFCNT_dec(stuff);
13497 PL_lex_formbrack = 0;
13501 if (PL_madskills) {
13503 sv_catpvn(PL_thistoken, tokenstart, s - tokenstart);
13505 PL_thistoken = newSVpvn(tokenstart, s - tokenstart);
13506 PL_thiswhite = savewhite;
13513 Perl_start_subparse(pTHX_ I32 is_format, U32 flags)
13516 const I32 oldsavestack_ix = PL_savestack_ix;
13517 CV* const outsidecv = PL_compcv;
13520 assert(SvTYPE(PL_compcv) == SVt_PVCV);
13522 SAVEI32(PL_subline);
13523 save_item(PL_subname);
13524 SAVESPTR(PL_compcv);
13526 PL_compcv = MUTABLE_CV(newSV_type(is_format ? SVt_PVFM : SVt_PVCV));
13527 CvFLAGS(PL_compcv) |= flags;
13529 PL_subline = CopLINE(PL_curcop);
13530 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE|padnew_SAVESUB);
13531 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outsidecv));
13532 CvOUTSIDE_SEQ(PL_compcv) = PL_cop_seqmax;
13534 return oldsavestack_ix;
13538 #pragma segment Perl_yylex
13541 S_yywarn(pTHX_ const char *const s)
13545 PERL_ARGS_ASSERT_YYWARN;
13547 PL_in_eval |= EVAL_WARNONLY;
13549 PL_in_eval &= ~EVAL_WARNONLY;
13554 Perl_yyerror(pTHX_ const char *const s)
13557 const char *where = NULL;
13558 const char *context = NULL;
13561 int yychar = PL_parser->yychar;
13563 PERL_ARGS_ASSERT_YYERROR;
13565 if (!yychar || (yychar == ';' && !PL_rsfp))
13567 else if (PL_oldoldbufptr && PL_bufptr > PL_oldoldbufptr &&
13568 PL_bufptr - PL_oldoldbufptr < 200 && PL_oldoldbufptr != PL_oldbufptr &&
13569 PL_oldbufptr != PL_bufptr) {
13572 The code below is removed for NetWare because it abends/crashes on NetWare
13573 when the script has error such as not having the closing quotes like:
13574 if ($var eq "value)
13575 Checking of white spaces is anyway done in NetWare code.
13578 while (isSPACE(*PL_oldoldbufptr))
13581 context = PL_oldoldbufptr;
13582 contlen = PL_bufptr - PL_oldoldbufptr;
13584 else if (PL_oldbufptr && PL_bufptr > PL_oldbufptr &&
13585 PL_bufptr - PL_oldbufptr < 200 && PL_oldbufptr != PL_bufptr) {
13588 The code below is removed for NetWare because it abends/crashes on NetWare
13589 when the script has error such as not having the closing quotes like:
13590 if ($var eq "value)
13591 Checking of white spaces is anyway done in NetWare code.
13594 while (isSPACE(*PL_oldbufptr))
13597 context = PL_oldbufptr;
13598 contlen = PL_bufptr - PL_oldbufptr;
13600 else if (yychar > 255)
13601 where = "next token ???";
13602 else if (yychar == -2) { /* YYEMPTY */
13603 if (PL_lex_state == LEX_NORMAL ||
13604 (PL_lex_state == LEX_KNOWNEXT && PL_lex_defer == LEX_NORMAL))
13605 where = "at end of line";
13606 else if (PL_lex_inpat)
13607 where = "within pattern";
13609 where = "within string";
13612 SV * const where_sv = newSVpvs_flags("next char ", SVs_TEMP);
13614 Perl_sv_catpvf(aTHX_ where_sv, "^%c", toCTRL(yychar));
13615 else if (isPRINT_LC(yychar)) {
13616 const char string = yychar;
13617 sv_catpvn(where_sv, &string, 1);
13620 Perl_sv_catpvf(aTHX_ where_sv, "\\%03o", yychar & 255);
13621 where = SvPVX_const(where_sv);
13623 msg = sv_2mortal(newSVpv(s, 0));
13624 Perl_sv_catpvf(aTHX_ msg, " at %s line %"IVdf", ",
13625 OutCopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
13627 Perl_sv_catpvf(aTHX_ msg, "near \"%.*s\"\n", contlen, context);
13629 Perl_sv_catpvf(aTHX_ msg, "%s\n", where);
13630 if (PL_multi_start < PL_multi_end && (U32)(CopLINE(PL_curcop) - PL_multi_end) <= 1) {
13631 Perl_sv_catpvf(aTHX_ msg,
13632 " (Might be a runaway multi-line %c%c string starting on line %"IVdf")\n",
13633 (int)PL_multi_open,(int)PL_multi_close,(IV)PL_multi_start);
13636 if (PL_in_eval & EVAL_WARNONLY) {
13637 Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX), "%"SVf, SVfARG(msg));
13641 if (PL_error_count >= 10) {
13642 if (PL_in_eval && SvCUR(ERRSV))
13643 Perl_croak(aTHX_ "%"SVf"%s has too many errors.\n",
13644 SVfARG(ERRSV), OutCopFILE(PL_curcop));
13646 Perl_croak(aTHX_ "%s has too many errors.\n",
13647 OutCopFILE(PL_curcop));
13650 PL_in_my_stash = NULL;
13654 #pragma segment Main
13658 S_swallow_bom(pTHX_ U8 *s)
13661 const STRLEN slen = SvCUR(PL_linestr);
13663 PERL_ARGS_ASSERT_SWALLOW_BOM;
13667 if (s[1] == 0xFE) {
13668 /* UTF-16 little-endian? (or UTF-32LE?) */
13669 if (s[2] == 0 && s[3] == 0) /* UTF-32 little-endian */
13670 Perl_croak(aTHX_ "Unsupported script encoding UTF-32LE");
13671 #ifndef PERL_NO_UTF16_FILTER
13672 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (BOM)\n");
13674 if (PL_bufend > (char*)s) {
13675 s = add_utf16_textfilter(s, TRUE);
13678 Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
13683 if (s[1] == 0xFF) { /* UTF-16 big-endian? */
13684 #ifndef PERL_NO_UTF16_FILTER
13685 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (BOM)\n");
13687 if (PL_bufend > (char *)s) {
13688 s = add_utf16_textfilter(s, FALSE);
13691 Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
13696 if (slen > 2 && s[1] == 0xBB && s[2] == 0xBF) {
13697 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-8 script encoding (BOM)\n");
13698 s += 3; /* UTF-8 */
13704 if (s[2] == 0xFE && s[3] == 0xFF) {
13705 /* UTF-32 big-endian */
13706 Perl_croak(aTHX_ "Unsupported script encoding UTF-32BE");
13709 else if (s[2] == 0 && s[3] != 0) {
13712 * are a good indicator of UTF-16BE. */
13713 #ifndef PERL_NO_UTF16_FILTER
13714 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (no BOM)\n");
13715 s = add_utf16_textfilter(s, FALSE);
13717 Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
13723 if (slen > 3 && s[1] == 0x73 && s[2] == 0x66 && s[3] == 0x73) {
13724 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-8 script encoding (BOM)\n");
13725 s += 4; /* UTF-8 */
13731 if (slen > 3 && s[1] == 0 && s[2] != 0 && s[3] == 0) {
13734 * are a good indicator of UTF-16LE. */
13735 #ifndef PERL_NO_UTF16_FILTER
13736 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (no BOM)\n");
13737 s = add_utf16_textfilter(s, TRUE);
13739 Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
13747 #ifndef PERL_NO_UTF16_FILTER
13749 S_utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen)
13752 SV *const filter = FILTER_DATA(idx);
13753 /* We re-use this each time round, throwing the contents away before we
13755 SV *const utf16_buffer = MUTABLE_SV(IoTOP_GV(filter));
13756 SV *const utf8_buffer = filter;
13757 IV status = IoPAGE(filter);
13758 const bool reverse = cBOOL(IoLINES(filter));
13761 PERL_ARGS_ASSERT_UTF16_TEXTFILTER;
13763 /* As we're automatically added, at the lowest level, and hence only called
13764 from this file, we can be sure that we're not called in block mode. Hence
13765 don't bother writing code to deal with block mode. */
13767 Perl_croak(aTHX_ "panic: utf16_textfilter called in block mode (for %d characters)", maxlen);
13770 Perl_croak(aTHX_ "panic: utf16_textfilter called after error (status=%"IVdf")", status);
13772 DEBUG_P(PerlIO_printf(Perl_debug_log,
13773 "utf16_textfilter(%p,%ce): idx=%d maxlen=%d status=%"IVdf" utf16=%"UVuf" utf8=%"UVuf"\n",
13774 FPTR2DPTR(void *, S_utf16_textfilter),
13775 reverse ? 'l' : 'b', idx, maxlen, status,
13776 (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
13783 /* First, look in our buffer of existing UTF-8 data: */
13784 char *nl = (char *)memchr(SvPVX(utf8_buffer), '\n', SvCUR(utf8_buffer));
13788 } else if (status == 0) {
13790 IoPAGE(filter) = 0;
13791 nl = SvEND(utf8_buffer);
13794 STRLEN got = nl - SvPVX(utf8_buffer);
13795 /* Did we have anything to append? */
13797 sv_catpvn(sv, SvPVX(utf8_buffer), got);
13798 /* Everything else in this code works just fine if SVp_POK isn't
13799 set. This, however, needs it, and we need it to work, else
13800 we loop infinitely because the buffer is never consumed. */
13801 sv_chop(utf8_buffer, nl);
13805 /* OK, not a complete line there, so need to read some more UTF-16.
13806 Read an extra octect if the buffer currently has an odd number. */
13810 if (SvCUR(utf16_buffer) >= 2) {
13811 /* Location of the high octet of the last complete code point.
13812 Gosh, UTF-16 is a pain. All the benefits of variable length,
13813 *coupled* with all the benefits of partial reads and
13815 const U8 *const last_hi = (U8*)SvPVX(utf16_buffer)
13816 + ((SvCUR(utf16_buffer) & ~1) - (reverse ? 1 : 2));
13818 if (*last_hi < 0xd8 || *last_hi > 0xdb) {
13822 /* We have the first half of a surrogate. Read more. */
13823 DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter partial surrogate detected at %p\n", last_hi));
13826 status = FILTER_READ(idx + 1, utf16_buffer,
13827 160 + (SvCUR(utf16_buffer) & 1));
13828 DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter status=%"IVdf" SvCUR(sv)=%"UVuf"\n", status, (UV)SvCUR(utf16_buffer)));
13829 DEBUG_P({ sv_dump(utf16_buffer); sv_dump(utf8_buffer);});
13832 IoPAGE(filter) = status;
13837 chars = SvCUR(utf16_buffer) >> 1;
13838 have = SvCUR(utf8_buffer);
13839 SvGROW(utf8_buffer, have + chars * 3 + 1);
13842 end = utf16_to_utf8_reversed((U8*)SvPVX(utf16_buffer),
13843 (U8*)SvPVX_const(utf8_buffer) + have,
13844 chars * 2, &newlen);
13846 end = utf16_to_utf8((U8*)SvPVX(utf16_buffer),
13847 (U8*)SvPVX_const(utf8_buffer) + have,
13848 chars * 2, &newlen);
13850 SvCUR_set(utf8_buffer, have + newlen);
13853 /* No need to keep this SV "well-formed" with a '\0' after the end, as
13854 it's private to us, and utf16_to_utf8{,reversed} take a
13855 (pointer,length) pair, rather than a NUL-terminated string. */
13856 if(SvCUR(utf16_buffer) & 1) {
13857 *SvPVX(utf16_buffer) = SvEND(utf16_buffer)[-1];
13858 SvCUR_set(utf16_buffer, 1);
13860 SvCUR_set(utf16_buffer, 0);
13863 DEBUG_P(PerlIO_printf(Perl_debug_log,
13864 "utf16_textfilter: returns, status=%"IVdf" utf16=%"UVuf" utf8=%"UVuf"\n",
13866 (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
13867 DEBUG_P({ sv_dump(utf8_buffer); sv_dump(sv);});
13872 S_add_utf16_textfilter(pTHX_ U8 *const s, bool reversed)
13874 SV *filter = filter_add(S_utf16_textfilter, NULL);
13876 PERL_ARGS_ASSERT_ADD_UTF16_TEXTFILTER;
13878 IoTOP_GV(filter) = MUTABLE_GV(newSVpvn((char *)s, PL_bufend - (char*)s));
13879 sv_setpvs(filter, "");
13880 IoLINES(filter) = reversed;
13881 IoPAGE(filter) = 1; /* Not EOF */
13883 /* Sadly, we have to return a valid pointer, come what may, so we have to
13884 ignore any error return from this. */
13885 SvCUR_set(PL_linestr, 0);
13886 if (FILTER_READ(0, PL_linestr, 0)) {
13887 SvUTF8_on(PL_linestr);
13889 SvUTF8_on(PL_linestr);
13891 PL_bufend = SvEND(PL_linestr);
13892 return (U8*)SvPVX(PL_linestr);
13897 Returns a pointer to the next character after the parsed
13898 vstring, as well as updating the passed in sv.
13900 Function must be called like
13903 s = scan_vstring(s,e,sv);
13905 where s and e are the start and end of the string.
13906 The sv should already be large enough to store the vstring
13907 passed in, for performance reasons.
13912 Perl_scan_vstring(pTHX_ const char *s, const char *const e, SV *sv)
13915 const char *pos = s;
13916 const char *start = s;
13918 PERL_ARGS_ASSERT_SCAN_VSTRING;
13920 if (*pos == 'v') pos++; /* get past 'v' */
13921 while (pos < e && (isDIGIT(*pos) || *pos == '_'))
13923 if ( *pos != '.') {
13924 /* this may not be a v-string if followed by => */
13925 const char *next = pos;
13926 while (next < e && isSPACE(*next))
13928 if ((e - next) >= 2 && *next == '=' && next[1] == '>' ) {
13929 /* return string not v-string */
13930 sv_setpvn(sv,(char *)s,pos-s);
13931 return (char *)pos;
13935 if (!isALPHA(*pos)) {
13936 U8 tmpbuf[UTF8_MAXBYTES+1];
13939 s++; /* get past 'v' */
13944 /* this is atoi() that tolerates underscores */
13947 const char *end = pos;
13949 while (--end >= s) {
13951 const UV orev = rev;
13952 rev += (*end - '0') * mult;
13955 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
13956 "Integer overflow in decimal number");
13960 if (rev > 0x7FFFFFFF)
13961 Perl_croak(aTHX_ "In EBCDIC the v-string components cannot exceed 2147483647");
13963 /* Append native character for the rev point */
13964 tmpend = uvchr_to_utf8(tmpbuf, rev);
13965 sv_catpvn(sv, (const char*)tmpbuf, tmpend - tmpbuf);
13966 if (!UNI_IS_INVARIANT(NATIVE_TO_UNI(rev)))
13968 if (pos + 1 < e && *pos == '.' && isDIGIT(pos[1]))
13974 while (pos < e && (isDIGIT(*pos) || *pos == '_'))
13978 sv_magic(sv,NULL,PERL_MAGIC_vstring,(const char*)start, pos-start);
13985 Perl_keyword_plugin_standard(pTHX_
13986 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
13988 PERL_ARGS_ASSERT_KEYWORD_PLUGIN_STANDARD;
13989 PERL_UNUSED_CONTEXT;
13990 PERL_UNUSED_ARG(keyword_ptr);
13991 PERL_UNUSED_ARG(keyword_len);
13992 PERL_UNUSED_ARG(op_ptr);
13993 return KEYWORD_PLUGIN_DECLINE;
13996 #define parse_recdescent(g) S_parse_recdescent(aTHX_ g)
13998 S_parse_recdescent(pTHX_ int gramtype)
14000 SAVEI32(PL_lex_brackets);
14001 if (PL_lex_brackets > 100)
14002 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
14003 PL_lex_brackstack[PL_lex_brackets++] = XFAKEEOF;
14004 if(yyparse(gramtype) && !PL_parser->error_count)
14005 qerror(Perl_mess(aTHX_ "Parse error"));
14008 #define parse_recdescent_for_op(g) S_parse_recdescent_for_op(aTHX_ g)
14010 S_parse_recdescent_for_op(pTHX_ int gramtype)
14014 SAVEVPTR(PL_eval_root);
14015 PL_eval_root = NULL;
14016 parse_recdescent(gramtype);
14023 =for apidoc Amx|OP *|parse_block|U32 flags
14025 Parse a single complete Perl code block. This consists of an opening
14026 brace, a sequence of statements, and a closing brace. The block
14027 constitutes a lexical scope, so C<my> variables and various compile-time
14028 effects can be contained within it. It is up to the caller to ensure
14029 that the dynamic parser state (L</PL_parser> et al) is correctly set to
14030 reflect the source of the code to be parsed and the lexical context for
14033 The op tree representing the code block is returned. This is always a
14034 real op, never a null pointer. It will normally be a C<lineseq> list,
14035 including C<nextstate> or equivalent ops. No ops to construct any kind
14036 of runtime scope are included by virtue of it being a block.
14038 If an error occurs in parsing or compilation, in most cases a valid op
14039 tree (most likely null) is returned anyway. The error is reflected in
14040 the parser state, normally resulting in a single exception at the top
14041 level of parsing which covers all the compilation errors that occurred.
14042 Some compilation errors, however, will throw an exception immediately.
14044 The I<flags> parameter is reserved for future use, and must always
14051 Perl_parse_block(pTHX_ U32 flags)
14054 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_block");
14055 return parse_recdescent_for_op(GRAMBLOCK);
14059 =for apidoc Amx|OP *|parse_barestmt|U32 flags
14061 Parse a single unadorned Perl statement. This may be a normal imperative
14062 statement or a declaration that has compile-time effect. It does not
14063 include any label or other affixture. It is up to the caller to ensure
14064 that the dynamic parser state (L</PL_parser> et al) is correctly set to
14065 reflect the source of the code to be parsed and the lexical context for
14068 The op tree representing the statement is returned. This may be a
14069 null pointer if the statement is null, for example if it was actually
14070 a subroutine definition (which has compile-time side effects). If not
14071 null, it will be ops directly implementing the statement, suitable to
14072 pass to L</newSTATEOP>. It will not normally include a C<nextstate> or
14073 equivalent op (except for those embedded in a scope contained entirely
14074 within the statement).
14076 If an error occurs in parsing or compilation, in most cases a valid op
14077 tree (most likely null) is returned anyway. The error is reflected in
14078 the parser state, normally resulting in a single exception at the top
14079 level of parsing which covers all the compilation errors that occurred.
14080 Some compilation errors, however, will throw an exception immediately.
14082 The I<flags> parameter is reserved for future use, and must always
14089 Perl_parse_barestmt(pTHX_ U32 flags)
14092 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_barestmt");
14093 return parse_recdescent_for_op(GRAMBARESTMT);
14097 =for apidoc Amx|SV *|parse_label|U32 flags
14099 Parse a single label, possibly optional, of the type that may prefix a
14100 Perl statement. It is up to the caller to ensure that the dynamic parser
14101 state (L</PL_parser> et al) is correctly set to reflect the source of
14102 the code to be parsed. If I<flags> includes C<PARSE_OPTIONAL> then the
14103 label is optional, otherwise it is mandatory.
14105 The name of the label is returned in the form of a fresh scalar. If an
14106 optional label is absent, a null pointer is returned.
14108 If an error occurs in parsing, which can only occur if the label is
14109 mandatory, a valid label is returned anyway. The error is reflected in
14110 the parser state, normally resulting in a single exception at the top
14111 level of parsing which covers all the compilation errors that occurred.
14117 Perl_parse_label(pTHX_ U32 flags)
14119 if (flags & ~PARSE_OPTIONAL)
14120 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_label");
14121 if (PL_lex_state == LEX_KNOWNEXT) {
14122 PL_parser->yychar = yylex();
14123 if (PL_parser->yychar == LABEL) {
14124 char *lpv = pl_yylval.pval;
14125 STRLEN llen = strlen(lpv);
14127 PL_parser->yychar = YYEMPTY;
14128 lsv = newSV_type(SVt_PV);
14129 SvPV_set(lsv, lpv);
14130 SvCUR_set(lsv, llen);
14131 SvLEN_set(lsv, llen+1);
14141 STRLEN wlen, bufptr_pos;
14145 if (!isIDFIRST_A(c))
14149 } while(isWORDCHAR_A(c));
14151 if (word_takes_any_delimeter(s, wlen))
14153 bufptr_pos = s - SvPVX(PL_linestr);
14155 lex_read_space(LEX_KEEP_PREVIOUS);
14157 s = SvPVX(PL_linestr) + bufptr_pos;
14158 if (t[0] == ':' && t[1] != ':') {
14159 PL_oldoldbufptr = PL_oldbufptr;
14162 return newSVpvn(s, wlen);
14166 if (flags & PARSE_OPTIONAL) {
14169 qerror(Perl_mess(aTHX_ "Parse error"));
14170 return newSVpvs("x");
14177 =for apidoc Amx|OP *|parse_fullstmt|U32 flags
14179 Parse a single complete Perl statement. This may be a normal imperative
14180 statement or a declaration that has compile-time effect, and may include
14181 an optional label. It is up to the caller to ensure that the dynamic
14182 parser state (L</PL_parser> et al) is correctly set to reflect the source
14183 of the code to be parsed and the lexical context for the statement.
14185 The op tree representing the statement is returned. This may be a
14186 null pointer if the statement is null, for example if it was actually
14187 a subroutine definition (which has compile-time side effects). If not
14188 null, it will be the result of a L</newSTATEOP> call, normally including
14189 a C<nextstate> or equivalent op.
14191 If an error occurs in parsing or compilation, in most cases a valid op
14192 tree (most likely null) is returned anyway. The error is reflected in
14193 the parser state, normally resulting in a single exception at the top
14194 level of parsing which covers all the compilation errors that occurred.
14195 Some compilation errors, however, will throw an exception immediately.
14197 The I<flags> parameter is reserved for future use, and must always
14204 Perl_parse_fullstmt(pTHX_ U32 flags)
14207 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_fullstmt");
14208 return parse_recdescent_for_op(GRAMFULLSTMT);
14212 =for apidoc Amx|OP *|parse_stmtseq|U32 flags
14214 Parse a sequence of zero or more Perl statements. These may be normal
14215 imperative statements, including optional labels, or declarations
14216 that have compile-time effect, or any mixture thereof. The statement
14217 sequence ends when a closing brace or end-of-file is encountered in a
14218 place where a new statement could have validly started. It is up to
14219 the caller to ensure that the dynamic parser state (L</PL_parser> et al)
14220 is correctly set to reflect the source of the code to be parsed and the
14221 lexical context for the statements.
14223 The op tree representing the statement sequence is returned. This may
14224 be a null pointer if the statements were all null, for example if there
14225 were no statements or if there were only subroutine definitions (which
14226 have compile-time side effects). If not null, it will be a C<lineseq>
14227 list, normally including C<nextstate> or equivalent ops.
14229 If an error occurs in parsing or compilation, in most cases a valid op
14230 tree is returned anyway. The error is reflected in the parser state,
14231 normally resulting in a single exception at the top level of parsing
14232 which covers all the compilation errors that occurred. Some compilation
14233 errors, however, will throw an exception immediately.
14235 The I<flags> parameter is reserved for future use, and must always
14242 Perl_parse_stmtseq(pTHX_ U32 flags)
14247 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_fullstmt");
14248 stmtseqop = parse_recdescent_for_op(GRAMSTMTSEQ);
14249 c = lex_peek_unichar(0);
14250 if (c != -1 && c != /*{*/'}')
14251 qerror(Perl_mess(aTHX_ "Parse error"));
14256 Perl_munge_qwlist_to_paren_list(pTHX_ OP *qwlist)
14258 PERL_ARGS_ASSERT_MUNGE_QWLIST_TO_PAREN_LIST;
14259 deprecate("qw(...) as parentheses");
14261 if (qwlist->op_type == OP_STUB) {
14265 start_force(PL_curforce);
14266 NEXTVAL_NEXTTOKE.opval = qwlist;
14274 * c-indentation-style: bsd
14275 * c-basic-offset: 4
14276 * indent-tabs-mode: t
14279 * ex: set ts=8 sts=4 sw=4 noet: