1 /* toke.c
2 *
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
5 *
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.
8 *
9 */
10
11 /*
12 * 'It all comes from here, the stench and the peril.' --Frodo
13 *
14 * [p.719 of _The Lord of the Rings_, IV/ix: "Shelob's Lair"]
15 */
16
17 /*
18 * This file is the lexer for Perl. It's closely linked to the
19 * parser, perly.y.
20 *
21 * The main routine is yylex(), which returns the next token.
22 */
23
24 /*
25 =head1 Lexer interface
26 This is the lower layer of the Perl parser, managing characters and tokens.
27
28 =for apidoc AmU|yy_parser *|PL_parser
29
30 Pointer to a structure encapsulating the state of the parsing operation
31 currently in progress. The pointer can be locally changed to perform
32 a nested parse without interfering with the state of an outer parse.
33 Individual members of C<PL_parser> have their own documentation.
34
35 =cut
36 */
37
38 #include "EXTERN.h"
39 #define PERL_IN_TOKE_C
40 #include "perl.h"
41 #include "dquote_inline.h"
42 #include "invlist_inline.h"
43
44 #define new_constant(a,b,c,d,e,f,g, h) \
45 S_new_constant(aTHX_ a,b,STR_WITH_LEN(c),d,e,f, g, h)
46
47 #define pl_yylval (PL_parser->yylval)
48
49 /* XXX temporary backwards compatibility */
50 #define PL_lex_brackets (PL_parser->lex_brackets)
51 #define PL_lex_allbrackets (PL_parser->lex_allbrackets)
52 #define PL_lex_fakeeof (PL_parser->lex_fakeeof)
53 #define PL_lex_brackstack (PL_parser->lex_brackstack)
54 #define PL_lex_casemods (PL_parser->lex_casemods)
55 #define PL_lex_casestack (PL_parser->lex_casestack)
56 #define PL_lex_dojoin (PL_parser->lex_dojoin)
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_preambled (PL_parser->preambled)
68 #define PL_linestr (PL_parser->linestr)
69 #define PL_expect (PL_parser->expect)
70 #define PL_copline (PL_parser->copline)
71 #define PL_bufptr (PL_parser->bufptr)
72 #define PL_oldbufptr (PL_parser->oldbufptr)
73 #define PL_oldoldbufptr (PL_parser->oldoldbufptr)
74 #define PL_linestart (PL_parser->linestart)
75 #define PL_bufend (PL_parser->bufend)
76 #define PL_last_uni (PL_parser->last_uni)
77 #define PL_last_lop (PL_parser->last_lop)
78 #define PL_last_lop_op (PL_parser->last_lop_op)
79 #define PL_lex_state (PL_parser->lex_state)
80 #define PL_rsfp (PL_parser->rsfp)
81 #define PL_rsfp_filters (PL_parser->rsfp_filters)
82 #define PL_in_my (PL_parser->in_my)
83 #define PL_in_my_stash (PL_parser->in_my_stash)
84 #define PL_tokenbuf (PL_parser->tokenbuf)
85 #define PL_multi_end (PL_parser->multi_end)
86 #define PL_error_count (PL_parser->error_count)
87
88 # define PL_nexttoke (PL_parser->nexttoke)
89 # define PL_nexttype (PL_parser->nexttype)
90 # define PL_nextval (PL_parser->nextval)
91
92
93 #define SvEVALED(sv) \
94 (SvTYPE(sv) >= SVt_PVNV \
95 && ((XPVIV*)SvANY(sv))->xiv_u.xivu_eval_seen)
96
97 static const char* const ident_too_long = "Identifier too long";
98
99 # define NEXTVAL_NEXTTOKE PL_nextval[PL_nexttoke]
100
101 #define XENUMMASK 0x3f
102 #define XFAKEEOF 0x40
103 #define XFAKEBRACK 0x80
104
105 #ifdef USE_UTF8_SCRIPTS
106 # define UTF cBOOL(!IN_BYTES)
107 #else
108 # define UTF cBOOL((PL_linestr && DO_UTF8(PL_linestr)) || ( !(PL_parser->lex_flags & LEX_IGNORE_UTF8_HINTS) && (PL_hints & HINT_UTF8)))
109 #endif
110
111 /* The maximum number of characters preceding the unrecognized one to display */
112 #define UNRECOGNIZED_PRECEDE_COUNT 10
113
114 /* In variables named $^X, these are the legal values for X.
115 * 1999-02-27 mjd-perl-patch@plover.com */
116 #define isCONTROLVAR(x) (isUPPER(x) || strchr("[\\]^_?", (x)))
117
118 #define SPACE_OR_TAB(c) isBLANK_A(c)
119
120 #define HEXFP_PEEK(s) \
121 (((s[0] == '.') && \
122 (isXDIGIT(s[1]) || isALPHA_FOLD_EQ(s[1], 'p'))) || \
123 isALPHA_FOLD_EQ(s[0], 'p'))
124
125 /* LEX_* are values for PL_lex_state, the state of the lexer.
126 * They are arranged oddly so that the guard on the switch statement
127 * can get by with a single comparison (if the compiler is smart enough).
128 *
129 * These values refer to the various states within a sublex parse,
130 * i.e. within a double quotish string
131 */
132
133 /* #define LEX_NOTPARSING 11 is done in perl.h. */
134
135 #define LEX_NORMAL 10 /* normal code (ie not within "...") */
136 #define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */
137 #define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */
138 #define LEX_INTERPPUSH 7 /* starting a new sublex parse level */
139 #define LEX_INTERPSTART 6 /* expecting the start of a $var */
140
141 /* at end of code, eg "$x" followed by: */
142 #define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */
143 #define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */
144
145 #define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of
146 string or after \E, $foo, etc */
147 #define LEX_INTERPCONST 2 /* NOT USED */
148 #define LEX_FORMLINE 1 /* expecting a format line */
149
150
151 #ifdef DEBUGGING
152 static const char* const lex_state_names[] = {
153 "KNOWNEXT",
154 "FORMLINE",
155 "INTERPCONST",
156 "INTERPCONCAT",
157 "INTERPENDMAYBE",
158 "INTERPEND",
159 "INTERPSTART",
160 "INTERPPUSH",
161 "INTERPCASEMOD",
162 "INTERPNORMAL",
163 "NORMAL"
164 };
165 #endif
166
167 #include "keywords.h"
168
169 /* CLINE is a macro that ensures PL_copline has a sane value */
170
171 #define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline))
172
173 /*
174 * Convenience functions to return different tokens and prime the
175 * lexer for the next token. They all take an argument.
176 *
177 * TOKEN : generic token (used for '(', DOLSHARP, etc)
178 * OPERATOR : generic operator
179 * AOPERATOR : assignment operator
180 * PREBLOCK : beginning the block after an if, while, foreach, ...
181 * PRETERMBLOCK : beginning a non-code-defining {} block (eg, hash ref)
182 * PREREF : *EXPR where EXPR is not a simple identifier
183 * TERM : expression term
184 * POSTDEREF : postfix dereference (->$* ->@[...] etc.)
185 * LOOPX : loop exiting command (goto, last, dump, etc)
186 * FTST : file test operator
187 * FUN0 : zero-argument function
188 * FUN0OP : zero-argument function, with its op created in this file
189 * FUN1 : not used, except for not, which isn't a UNIOP
190 * BOop : bitwise or or xor
191 * BAop : bitwise and
192 * BCop : bitwise complement
193 * SHop : shift operator
194 * PWop : power operator
195 * PMop : pattern-matching operator
196 * Aop : addition-level operator
197 * AopNOASSIGN : addition-level operator that is never part of .=
198 * Mop : multiplication-level operator
199 * Eop : equality-testing operator
200 * Rop : relational operator <= != gt
201 *
202 * Also see LOP and lop() below.
203 */
204
205 #ifdef DEBUGGING /* Serve -DT. */
206 # define REPORT(retval) tokereport((I32)retval, &pl_yylval)
207 #else
208 # define REPORT(retval) (retval)
209 #endif
210
211 #define TOKEN(retval) return ( PL_bufptr = s, REPORT(retval))
212 #define OPERATOR(retval) return (PL_expect = XTERM, PL_bufptr = s, REPORT(retval))
213 #define AOPERATOR(retval) return ao((PL_expect = XTERM, PL_bufptr = s, retval))
214 #define PREBLOCK(retval) return (PL_expect = XBLOCK,PL_bufptr = s, REPORT(retval))
215 #define PRETERMBLOCK(retval) return (PL_expect = XTERMBLOCK,PL_bufptr = s, REPORT(retval))
216 #define PREREF(retval) return (PL_expect = XREF,PL_bufptr = s, REPORT(retval))
217 #define TERM(retval) return (CLINE, PL_expect = XOPERATOR, PL_bufptr = s, REPORT(retval))
218 #define POSTDEREF(f) return (PL_bufptr = s, S_postderef(aTHX_ REPORT(f),s[1]))
219 #define LOOPX(f) return (PL_bufptr = force_word(s,BAREWORD,TRUE,FALSE), \
220 pl_yylval.ival=f, \
221 PL_expect = PL_nexttoke ? XOPERATOR : XTERM, \
222 REPORT((int)LOOPEX))
223 #define FTST(f) return (pl_yylval.ival=f, PL_expect=XTERMORDORDOR, PL_bufptr=s, REPORT((int)UNIOP))
224 #define FUN0(f) return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0))
225 #define FUN0OP(f) return (pl_yylval.opval=f, CLINE, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0OP))
226 #define FUN1(f) return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC1))
227 #define BOop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)BITOROP))
228 #define BAop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)BITANDOP))
229 #define BCop(f) return pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr = s, \
230 REPORT('~')
231 #define SHop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)SHIFTOP))
232 #define PWop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)POWOP))
233 #define PMop(f) return(pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MATCHOP))
234 #define Aop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)ADDOP))
235 #define AopNOASSIGN(f) return (pl_yylval.ival=f, PL_bufptr=s, REPORT((int)ADDOP))
236 #define Mop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)MULOP))
237 #define Eop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)EQOP))
238 #define Rop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)RELOP))
239
240 /* This bit of chicanery makes a unary function followed by
241 * a parenthesis into a function with one argument, highest precedence.
242 * The UNIDOR macro is for unary functions that can be followed by the //
243 * operator (such as C<shift // 0>).
244 */
245 #define UNI3(f,x,have_x) { \
246 pl_yylval.ival = f; \
247 if (have_x) PL_expect = x; \
248 PL_bufptr = s; \
249 PL_last_uni = PL_oldbufptr; \
250 PL_last_lop_op = (f) < 0 ? -(f) : (f); \
251 if (*s == '(') \
252 return REPORT( (int)FUNC1 ); \
253 s = skipspace(s); \
254 return REPORT( *s=='(' ? (int)FUNC1 : (int)UNIOP ); \
255 }
256 #define UNI(f) UNI3(f,XTERM,1)
257 #define UNIDOR(f) UNI3(f,XTERMORDORDOR,1)
258 #define UNIPROTO(f,optional) { \
259 if (optional) PL_last_uni = PL_oldbufptr; \
260 OPERATOR(f); \
261 }
262
263 #define UNIBRACK(f) UNI3(f,0,0)
264
265 /* grandfather return to old style */
266 #define OLDLOP(f) \
267 do { \
268 if (!PL_lex_allbrackets && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC) \
269 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC; \
270 pl_yylval.ival = (f); \
271 PL_expect = XTERM; \
272 PL_bufptr = s; \
273 return (int)LSTOP; \
274 } while(0)
275
276 #define COPLINE_INC_WITH_HERELINES \
277 STMT_START { \
278 CopLINE_inc(PL_curcop); \
279 if (PL_parser->herelines) \
280 CopLINE(PL_curcop) += PL_parser->herelines, \
281 PL_parser->herelines = 0; \
282 } STMT_END
283 /* Called after scan_str to update CopLINE(PL_curcop), but only when there
284 * is no sublex_push to follow. */
285 #define COPLINE_SET_FROM_MULTI_END \
286 STMT_START { \
287 CopLINE_set(PL_curcop, PL_multi_end); \
288 if (PL_multi_end != PL_multi_start) \
289 PL_parser->herelines = 0; \
290 } STMT_END
291
292
293 #ifdef DEBUGGING
294
295 /* how to interpret the pl_yylval associated with the token */
296 enum token_type {
297 TOKENTYPE_NONE,
298 TOKENTYPE_IVAL,
299 TOKENTYPE_OPNUM, /* pl_yylval.ival contains an opcode number */
300 TOKENTYPE_PVAL,
301 TOKENTYPE_OPVAL
302 };
303
304 static struct debug_tokens {
305 const int token;
306 enum token_type type;
307 const char *name;
308 } const debug_tokens[] =
309 {
310 { ADDOP, TOKENTYPE_OPNUM, "ADDOP" },
311 { ANDAND, TOKENTYPE_NONE, "ANDAND" },
312 { ANDOP, TOKENTYPE_NONE, "ANDOP" },
313 { ANONSUB, TOKENTYPE_IVAL, "ANONSUB" },
314 { ANON_SIGSUB, TOKENTYPE_IVAL, "ANON_SIGSUB" },
315 { ARROW, TOKENTYPE_NONE, "ARROW" },
316 { ASSIGNOP, TOKENTYPE_OPNUM, "ASSIGNOP" },
317 { BITANDOP, TOKENTYPE_OPNUM, "BITANDOP" },
318 { BITOROP, TOKENTYPE_OPNUM, "BITOROP" },
319 { COLONATTR, TOKENTYPE_NONE, "COLONATTR" },
320 { CONTINUE, TOKENTYPE_NONE, "CONTINUE" },
321 { DEFAULT, TOKENTYPE_NONE, "DEFAULT" },
322 { DO, TOKENTYPE_NONE, "DO" },
323 { DOLSHARP, TOKENTYPE_NONE, "DOLSHARP" },
324 { DORDOR, TOKENTYPE_NONE, "DORDOR" },
325 { DOROP, TOKENTYPE_OPNUM, "DOROP" },
326 { DOTDOT, TOKENTYPE_IVAL, "DOTDOT" },
327 { ELSE, TOKENTYPE_NONE, "ELSE" },
328 { ELSIF, TOKENTYPE_IVAL, "ELSIF" },
329 { EQOP, TOKENTYPE_OPNUM, "EQOP" },
330 { FOR, TOKENTYPE_IVAL, "FOR" },
331 { FORMAT, TOKENTYPE_NONE, "FORMAT" },
332 { FORMLBRACK, TOKENTYPE_NONE, "FORMLBRACK" },
333 { FORMRBRACK, TOKENTYPE_NONE, "FORMRBRACK" },
334 { FUNC, TOKENTYPE_OPNUM, "FUNC" },
335 { FUNC0, TOKENTYPE_OPNUM, "FUNC0" },
336 { FUNC0OP, TOKENTYPE_OPVAL, "FUNC0OP" },
337 { FUNC0SUB, TOKENTYPE_OPVAL, "FUNC0SUB" },
338 { FUNC1, TOKENTYPE_OPNUM, "FUNC1" },
339 { FUNCMETH, TOKENTYPE_OPVAL, "FUNCMETH" },
340 { GIVEN, TOKENTYPE_IVAL, "GIVEN" },
341 { HASHBRACK, TOKENTYPE_NONE, "HASHBRACK" },
342 { IF, TOKENTYPE_IVAL, "IF" },
343 { LABEL, TOKENTYPE_OPVAL, "LABEL" },
344 { LOCAL, TOKENTYPE_IVAL, "LOCAL" },
345 { LOOPEX, TOKENTYPE_OPNUM, "LOOPEX" },
346 { LSTOP, TOKENTYPE_OPNUM, "LSTOP" },
347 { LSTOPSUB, TOKENTYPE_OPVAL, "LSTOPSUB" },
348 { MATCHOP, TOKENTYPE_OPNUM, "MATCHOP" },
349 { METHOD, TOKENTYPE_OPVAL, "METHOD" },
350 { MULOP, TOKENTYPE_OPNUM, "MULOP" },
351 { MY, TOKENTYPE_IVAL, "MY" },
352 { NOAMP, TOKENTYPE_NONE, "NOAMP" },
353 { NOTOP, TOKENTYPE_NONE, "NOTOP" },
354 { OROP, TOKENTYPE_IVAL, "OROP" },
355 { OROR, TOKENTYPE_NONE, "OROR" },
356 { PACKAGE, TOKENTYPE_NONE, "PACKAGE" },
357 { PLUGEXPR, TOKENTYPE_OPVAL, "PLUGEXPR" },
358 { PLUGSTMT, TOKENTYPE_OPVAL, "PLUGSTMT" },
359 { PMFUNC, TOKENTYPE_OPVAL, "PMFUNC" },
360 { POSTJOIN, TOKENTYPE_NONE, "POSTJOIN" },
361 { POSTDEC, TOKENTYPE_NONE, "POSTDEC" },
362 { POSTINC, TOKENTYPE_NONE, "POSTINC" },
363 { POWOP, TOKENTYPE_OPNUM, "POWOP" },
364 { PREDEC, TOKENTYPE_NONE, "PREDEC" },
365 { PREINC, TOKENTYPE_NONE, "PREINC" },
366 { PRIVATEREF, TOKENTYPE_OPVAL, "PRIVATEREF" },
367 { QWLIST, TOKENTYPE_OPVAL, "QWLIST" },
368 { REFGEN, TOKENTYPE_NONE, "REFGEN" },
369 { RELOP, TOKENTYPE_OPNUM, "RELOP" },
370 { REQUIRE, TOKENTYPE_NONE, "REQUIRE" },
371 { SHIFTOP, TOKENTYPE_OPNUM, "SHIFTOP" },
372 { SIGSUB, TOKENTYPE_NONE, "SIGSUB" },
373 { SUB, TOKENTYPE_NONE, "SUB" },
374 { THING, TOKENTYPE_OPVAL, "THING" },
375 { UMINUS, TOKENTYPE_NONE, "UMINUS" },
376 { UNIOP, TOKENTYPE_OPNUM, "UNIOP" },
377 { UNIOPSUB, TOKENTYPE_OPVAL, "UNIOPSUB" },
378 { UNLESS, TOKENTYPE_IVAL, "UNLESS" },
379 { UNTIL, TOKENTYPE_IVAL, "UNTIL" },
380 { USE, TOKENTYPE_IVAL, "USE" },
381 { WHEN, TOKENTYPE_IVAL, "WHEN" },
382 { WHILE, TOKENTYPE_IVAL, "WHILE" },
383 { BAREWORD, TOKENTYPE_OPVAL, "BAREWORD" },
384 { YADAYADA, TOKENTYPE_IVAL, "YADAYADA" },
385 { 0, TOKENTYPE_NONE, NULL }
386 };
387
388 /* dump the returned token in rv, plus any optional arg in pl_yylval */
389
390 STATIC int
S_tokereport(pTHX_ I32 rv,const YYSTYPE * lvalp)391 S_tokereport(pTHX_ I32 rv, const YYSTYPE* lvalp)
392 {
393 PERL_ARGS_ASSERT_TOKEREPORT;
394
395 if (DEBUG_T_TEST) {
396 const char *name = NULL;
397 enum token_type type = TOKENTYPE_NONE;
398 const struct debug_tokens *p;
399 SV* const report = newSVpvs("<== ");
400
401 for (p = debug_tokens; p->token; p++) {
402 if (p->token == (int)rv) {
403 name = p->name;
404 type = p->type;
405 break;
406 }
407 }
408 if (name)
409 Perl_sv_catpv(aTHX_ report, name);
410 else if (isGRAPH(rv))
411 {
412 Perl_sv_catpvf(aTHX_ report, "'%c'", (char)rv);
413 if ((char)rv == 'p')
414 sv_catpvs(report, " (pending identifier)");
415 }
416 else if (!rv)
417 sv_catpvs(report, "EOF");
418 else
419 Perl_sv_catpvf(aTHX_ report, "?? %" IVdf, (IV)rv);
420 switch (type) {
421 case TOKENTYPE_NONE:
422 break;
423 case TOKENTYPE_IVAL:
424 Perl_sv_catpvf(aTHX_ report, "(ival=%" IVdf ")", (IV)lvalp->ival);
425 break;
426 case TOKENTYPE_OPNUM:
427 Perl_sv_catpvf(aTHX_ report, "(ival=op_%s)",
428 PL_op_name[lvalp->ival]);
429 break;
430 case TOKENTYPE_PVAL:
431 Perl_sv_catpvf(aTHX_ report, "(pval=\"%s\")", lvalp->pval);
432 break;
433 case TOKENTYPE_OPVAL:
434 if (lvalp->opval) {
435 Perl_sv_catpvf(aTHX_ report, "(opval=op_%s)",
436 PL_op_name[lvalp->opval->op_type]);
437 if (lvalp->opval->op_type == OP_CONST) {
438 Perl_sv_catpvf(aTHX_ report, " %s",
439 SvPEEK(cSVOPx_sv(lvalp->opval)));
440 }
441
442 }
443 else
444 sv_catpvs(report, "(opval=null)");
445 break;
446 }
447 PerlIO_printf(Perl_debug_log, "### %s\n\n", SvPV_nolen_const(report));
448 };
449 return (int)rv;
450 }
451
452
453 /* print the buffer with suitable escapes */
454
455 STATIC void
S_printbuf(pTHX_ const char * const fmt,const char * const s)456 S_printbuf(pTHX_ const char *const fmt, const char *const s)
457 {
458 SV* const tmp = newSVpvs("");
459
460 PERL_ARGS_ASSERT_PRINTBUF;
461
462 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral); /* fmt checked by caller */
463 PerlIO_printf(Perl_debug_log, fmt, pv_display(tmp, s, strlen(s), 0, 60));
464 GCC_DIAG_RESTORE_STMT;
465 SvREFCNT_dec(tmp);
466 }
467
468 #endif
469
470 /*
471 * S_ao
472 *
473 * This subroutine looks for an '=' next to the operator that has just been
474 * parsed and turns it into an ASSIGNOP if it finds one.
475 */
476
477 STATIC int
S_ao(pTHX_ int toketype)478 S_ao(pTHX_ int toketype)
479 {
480 if (*PL_bufptr == '=') {
481 PL_bufptr++;
482 if (toketype == ANDAND)
483 pl_yylval.ival = OP_ANDASSIGN;
484 else if (toketype == OROR)
485 pl_yylval.ival = OP_ORASSIGN;
486 else if (toketype == DORDOR)
487 pl_yylval.ival = OP_DORASSIGN;
488 toketype = ASSIGNOP;
489 }
490 return REPORT(toketype);
491 }
492
493 /*
494 * S_no_op
495 * When Perl expects an operator and finds something else, no_op
496 * prints the warning. It always prints "<something> found where
497 * operator expected. It prints "Missing semicolon on previous line?"
498 * if the surprise occurs at the start of the line. "do you need to
499 * predeclare ..." is printed out for code like "sub bar; foo bar $x"
500 * where the compiler doesn't know if foo is a method call or a function.
501 * It prints "Missing operator before end of line" if there's nothing
502 * after the missing operator, or "... before <...>" if there is something
503 * after the missing operator.
504 *
505 * PL_bufptr is expected to point to the start of the thing that was found,
506 * and s after the next token or partial token.
507 */
508
509 STATIC void
S_no_op(pTHX_ const char * const what,char * s)510 S_no_op(pTHX_ const char *const what, char *s)
511 {
512 char * const oldbp = PL_bufptr;
513 const bool is_first = (PL_oldbufptr == PL_linestart);
514
515 PERL_ARGS_ASSERT_NO_OP;
516
517 if (!s)
518 s = oldbp;
519 else
520 PL_bufptr = s;
521 yywarn(Perl_form(aTHX_ "%s found where operator expected", what), UTF ? SVf_UTF8 : 0);
522 if (ckWARN_d(WARN_SYNTAX)) {
523 if (is_first)
524 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
525 "\t(Missing semicolon on previous line?)\n");
526 else if (PL_oldoldbufptr && isIDFIRST_lazy_if_safe(PL_oldoldbufptr,
527 PL_bufend,
528 UTF))
529 {
530 const char *t;
531 for (t = PL_oldoldbufptr;
532 (isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF) || *t == ':');
533 t += UTF ? UTF8SKIP(t) : 1)
534 {
535 NOOP;
536 }
537 if (t < PL_bufptr && isSPACE(*t))
538 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
539 "\t(Do you need to predeclare %" UTF8f "?)\n",
540 UTF8fARG(UTF, t - PL_oldoldbufptr, PL_oldoldbufptr));
541 }
542 else {
543 assert(s >= oldbp);
544 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
545 "\t(Missing operator before %" UTF8f "?)\n",
546 UTF8fARG(UTF, s - oldbp, oldbp));
547 }
548 }
549 PL_bufptr = oldbp;
550 }
551
552 /*
553 * S_missingterm
554 * Complain about missing quote/regexp/heredoc terminator.
555 * If it's called with NULL then it cauterizes the line buffer.
556 * If we're in a delimited string and the delimiter is a control
557 * character, it's reformatted into a two-char sequence like ^C.
558 * This is fatal.
559 */
560
561 STATIC void
S_missingterm(pTHX_ char * s,STRLEN len)562 S_missingterm(pTHX_ char *s, STRLEN len)
563 {
564 char tmpbuf[UTF8_MAXBYTES + 1];
565 char q;
566 bool uni = FALSE;
567 SV *sv;
568 if (s) {
569 char * const nl = (char *) my_memrchr(s, '\n', len);
570 if (nl) {
571 *nl = '\0';
572 len = nl - s;
573 }
574 uni = UTF;
575 }
576 else if (PL_multi_close < 32) {
577 *tmpbuf = '^';
578 tmpbuf[1] = (char)toCTRL(PL_multi_close);
579 tmpbuf[2] = '\0';
580 s = tmpbuf;
581 len = 2;
582 }
583 else {
584 if (LIKELY(PL_multi_close < 256)) {
585 *tmpbuf = (char)PL_multi_close;
586 tmpbuf[1] = '\0';
587 len = 1;
588 }
589 else {
590 char *end = (char *)uvchr_to_utf8((U8 *)tmpbuf, PL_multi_close);
591 *end = '\0';
592 len = end - tmpbuf;
593 uni = TRUE;
594 }
595 s = tmpbuf;
596 }
597 q = memchr(s, '"', len) ? '\'' : '"';
598 sv = sv_2mortal(newSVpvn(s, len));
599 if (uni)
600 SvUTF8_on(sv);
601 Perl_croak(aTHX_ "Can't find string terminator %c%" SVf "%c"
602 " anywhere before EOF", q, SVfARG(sv), q);
603 }
604
605 #include "feature.h"
606
607 /*
608 * Check whether the named feature is enabled.
609 */
610 bool
Perl_feature_is_enabled(pTHX_ const char * const name,STRLEN namelen)611 Perl_feature_is_enabled(pTHX_ const char *const name, STRLEN namelen)
612 {
613 char he_name[8 + MAX_FEATURE_LEN] = "feature_";
614
615 PERL_ARGS_ASSERT_FEATURE_IS_ENABLED;
616
617 assert(CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM);
618
619 if (namelen > MAX_FEATURE_LEN)
620 return FALSE;
621 memcpy(&he_name[8], name, namelen);
622
623 return cBOOL(cop_hints_fetch_pvn(PL_curcop, he_name, 8 + namelen, 0,
624 REFCOUNTED_HE_EXISTS));
625 }
626
627 /*
628 * experimental text filters for win32 carriage-returns, utf16-to-utf8 and
629 * utf16-to-utf8-reversed.
630 */
631
632 #ifdef PERL_CR_FILTER
633 static void
strip_return(SV * sv)634 strip_return(SV *sv)
635 {
636 const char *s = SvPVX_const(sv);
637 const char * const e = s + SvCUR(sv);
638
639 PERL_ARGS_ASSERT_STRIP_RETURN;
640
641 /* outer loop optimized to do nothing if there are no CR-LFs */
642 while (s < e) {
643 if (*s++ == '\r' && *s == '\n') {
644 /* hit a CR-LF, need to copy the rest */
645 char *d = s - 1;
646 *d++ = *s++;
647 while (s < e) {
648 if (*s == '\r' && s[1] == '\n')
649 s++;
650 *d++ = *s++;
651 }
652 SvCUR(sv) -= s - d;
653 return;
654 }
655 }
656 }
657
658 STATIC I32
S_cr_textfilter(pTHX_ int idx,SV * sv,int maxlen)659 S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
660 {
661 const I32 count = FILTER_READ(idx+1, sv, maxlen);
662 if (count > 0 && !maxlen)
663 strip_return(sv);
664 return count;
665 }
666 #endif
667
668 /*
669 =for apidoc Amx|void|lex_start|SV *line|PerlIO *rsfp|U32 flags
670
671 Creates and initialises a new lexer/parser state object, supplying
672 a context in which to lex and parse from a new source of Perl code.
673 A pointer to the new state object is placed in L</PL_parser>. An entry
674 is made on the save stack so that upon unwinding, the new state object
675 will be destroyed and the former value of L</PL_parser> will be restored.
676 Nothing else need be done to clean up the parsing context.
677
678 The code to be parsed comes from C<line> and C<rsfp>. C<line>, if
679 non-null, provides a string (in SV form) containing code to be parsed.
680 A copy of the string is made, so subsequent modification of C<line>
681 does not affect parsing. C<rsfp>, if non-null, provides an input stream
682 from which code will be read to be parsed. If both are non-null, the
683 code in C<line> comes first and must consist of complete lines of input,
684 and C<rsfp> supplies the remainder of the source.
685
686 The C<flags> parameter is reserved for future use. Currently it is only
687 used by perl internally, so extensions should always pass zero.
688
689 =cut
690 */
691
692 /* LEX_START_SAME_FILTER indicates that this is not a new file, so it
693 can share filters with the current parser.
694 LEX_START_DONT_CLOSE indicates that the file handle wasn't opened by the
695 caller, hence isn't owned by the parser, so shouldn't be closed on parser
696 destruction. This is used to handle the case of defaulting to reading the
697 script from the standard input because no filename was given on the command
698 line (without getting confused by situation where STDIN has been closed, so
699 the script handle is opened on fd 0) */
700
701 void
Perl_lex_start(pTHX_ SV * line,PerlIO * rsfp,U32 flags)702 Perl_lex_start(pTHX_ SV *line, PerlIO *rsfp, U32 flags)
703 {
704 const char *s = NULL;
705 yy_parser *parser, *oparser;
706
707 if (flags && flags & ~LEX_START_FLAGS)
708 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_start");
709
710 /* create and initialise a parser */
711
712 Newxz(parser, 1, yy_parser);
713 parser->old_parser = oparser = PL_parser;
714 PL_parser = parser;
715
716 parser->stack = NULL;
717 parser->stack_max1 = NULL;
718 parser->ps = NULL;
719
720 /* on scope exit, free this parser and restore any outer one */
721 SAVEPARSER(parser);
722 parser->saved_curcop = PL_curcop;
723
724 /* initialise lexer state */
725
726 parser->nexttoke = 0;
727 parser->error_count = oparser ? oparser->error_count : 0;
728 parser->copline = parser->preambling = NOLINE;
729 parser->lex_state = LEX_NORMAL;
730 parser->expect = XSTATE;
731 parser->rsfp = rsfp;
732 parser->recheck_utf8_validity = TRUE;
733 parser->rsfp_filters =
734 !(flags & LEX_START_SAME_FILTER) || !oparser
735 ? NULL
736 : MUTABLE_AV(SvREFCNT_inc(
737 oparser->rsfp_filters
738 ? oparser->rsfp_filters
739 : (oparser->rsfp_filters = newAV())
740 ));
741
742 Newx(parser->lex_brackstack, 120, char);
743 Newx(parser->lex_casestack, 12, char);
744 *parser->lex_casestack = '\0';
745 Newxz(parser->lex_shared, 1, LEXSHARED);
746
747 if (line) {
748 STRLEN len;
749 const U8* first_bad_char_loc;
750
751 s = SvPV_const(line, len);
752
753 if ( SvUTF8(line)
754 && UNLIKELY(! is_utf8_string_loc((U8 *) s,
755 SvCUR(line),
756 &first_bad_char_loc)))
757 {
758 _force_out_malformed_utf8_message(first_bad_char_loc,
759 (U8 *) s + SvCUR(line),
760 0,
761 1 /* 1 means die */ );
762 NOT_REACHED; /* NOTREACHED */
763 }
764
765 parser->linestr = flags & LEX_START_COPIED
766 ? SvREFCNT_inc_simple_NN(line)
767 : newSVpvn_flags(s, len, SvUTF8(line));
768 if (!rsfp)
769 sv_catpvs(parser->linestr, "\n;");
770 } else {
771 parser->linestr = newSVpvn("\n;", rsfp ? 1 : 2);
772 }
773
774 parser->oldoldbufptr =
775 parser->oldbufptr =
776 parser->bufptr =
777 parser->linestart = SvPVX(parser->linestr);
778 parser->bufend = parser->bufptr + SvCUR(parser->linestr);
779 parser->last_lop = parser->last_uni = NULL;
780
781 STATIC_ASSERT_STMT(FITS_IN_8_BITS(LEX_IGNORE_UTF8_HINTS|LEX_EVALBYTES
782 |LEX_DONT_CLOSE_RSFP));
783 parser->lex_flags = (U8) (flags & (LEX_IGNORE_UTF8_HINTS|LEX_EVALBYTES
784 |LEX_DONT_CLOSE_RSFP));
785
786 parser->in_pod = parser->filtered = 0;
787 }
788
789
790 /* delete a parser object */
791
792 void
Perl_parser_free(pTHX_ const yy_parser * parser)793 Perl_parser_free(pTHX_ const yy_parser *parser)
794 {
795 PERL_ARGS_ASSERT_PARSER_FREE;
796
797 PL_curcop = parser->saved_curcop;
798 SvREFCNT_dec(parser->linestr);
799
800 if (PL_parser->lex_flags & LEX_DONT_CLOSE_RSFP)
801 PerlIO_clearerr(parser->rsfp);
802 else if (parser->rsfp && (!parser->old_parser
803 || (parser->old_parser && parser->rsfp != parser->old_parser->rsfp)))
804 PerlIO_close(parser->rsfp);
805 SvREFCNT_dec(parser->rsfp_filters);
806 SvREFCNT_dec(parser->lex_stuff);
807 SvREFCNT_dec(parser->lex_sub_repl);
808
809 Safefree(parser->lex_brackstack);
810 Safefree(parser->lex_casestack);
811 Safefree(parser->lex_shared);
812 PL_parser = parser->old_parser;
813 Safefree(parser);
814 }
815
816 void
Perl_parser_free_nexttoke_ops(pTHX_ yy_parser * parser,OPSLAB * slab)817 Perl_parser_free_nexttoke_ops(pTHX_ yy_parser *parser, OPSLAB *slab)
818 {
819 I32 nexttoke = parser->nexttoke;
820 PERL_ARGS_ASSERT_PARSER_FREE_NEXTTOKE_OPS;
821 while (nexttoke--) {
822 if (S_is_opval_token(parser->nexttype[nexttoke] & 0xffff)
823 && parser->nextval[nexttoke].opval
824 && parser->nextval[nexttoke].opval->op_slabbed
825 && OpSLAB(parser->nextval[nexttoke].opval) == slab) {
826 op_free(parser->nextval[nexttoke].opval);
827 parser->nextval[nexttoke].opval = NULL;
828 }
829 }
830 }
831
832
833 /*
834 =for apidoc AmxU|SV *|PL_parser-E<gt>linestr
835
836 Buffer scalar containing the chunk currently under consideration of the
837 text currently being lexed. This is always a plain string scalar (for
838 which C<SvPOK> is true). It is not intended to be used as a scalar by
839 normal scalar means; instead refer to the buffer directly by the pointer
840 variables described below.
841
842 The lexer maintains various C<char*> pointers to things in the
843 C<PL_parser-E<gt>linestr> buffer. If C<PL_parser-E<gt>linestr> is ever
844 reallocated, all of these pointers must be updated. Don't attempt to
845 do this manually, but rather use L</lex_grow_linestr> if you need to
846 reallocate the buffer.
847
848 The content of the text chunk in the buffer is commonly exactly one
849 complete line of input, up to and including a newline terminator,
850 but there are situations where it is otherwise. The octets of the
851 buffer may be intended to be interpreted as either UTF-8 or Latin-1.
852 The function L</lex_bufutf8> tells you which. Do not use the C<SvUTF8>
853 flag on this scalar, which may disagree with it.
854
855 For direct examination of the buffer, the variable
856 L</PL_parser-E<gt>bufend> points to the end of the buffer. The current
857 lexing position is pointed to by L</PL_parser-E<gt>bufptr>. Direct use
858 of these pointers is usually preferable to examination of the scalar
859 through normal scalar means.
860
861 =for apidoc AmxU|char *|PL_parser-E<gt>bufend
862
863 Direct pointer to the end of the chunk of text currently being lexed, the
864 end of the lexer buffer. This is equal to C<SvPVX(PL_parser-E<gt>linestr)
865 + SvCUR(PL_parser-E<gt>linestr)>. A C<NUL> character (zero octet) is
866 always located at the end of the buffer, and does not count as part of
867 the buffer's contents.
868
869 =for apidoc AmxU|char *|PL_parser-E<gt>bufptr
870
871 Points to the current position of lexing inside the lexer buffer.
872 Characters around this point may be freely examined, within
873 the range delimited by C<SvPVX(L</PL_parser-E<gt>linestr>)> and
874 L</PL_parser-E<gt>bufend>. The octets of the buffer may be intended to be
875 interpreted as either UTF-8 or Latin-1, as indicated by L</lex_bufutf8>.
876
877 Lexing code (whether in the Perl core or not) moves this pointer past
878 the characters that it consumes. It is also expected to perform some
879 bookkeeping whenever a newline character is consumed. This movement
880 can be more conveniently performed by the function L</lex_read_to>,
881 which handles newlines appropriately.
882
883 Interpretation of the buffer's octets can be abstracted out by
884 using the slightly higher-level functions L</lex_peek_unichar> and
885 L</lex_read_unichar>.
886
887 =for apidoc AmxU|char *|PL_parser-E<gt>linestart
888
889 Points to the start of the current line inside the lexer buffer.
890 This is useful for indicating at which column an error occurred, and
891 not much else. This must be updated by any lexing code that consumes
892 a newline; the function L</lex_read_to> handles this detail.
893
894 =cut
895 */
896
897 /*
898 =for apidoc Amx|bool|lex_bufutf8
899
900 Indicates whether the octets in the lexer buffer
901 (L</PL_parser-E<gt>linestr>) should be interpreted as the UTF-8 encoding
902 of Unicode characters. If not, they should be interpreted as Latin-1
903 characters. This is analogous to the C<SvUTF8> flag for scalars.
904
905 In UTF-8 mode, it is not guaranteed that the lexer buffer actually
906 contains valid UTF-8. Lexing code must be robust in the face of invalid
907 encoding.
908
909 The actual C<SvUTF8> flag of the L</PL_parser-E<gt>linestr> scalar
910 is significant, but not the whole story regarding the input character
911 encoding. Normally, when a file is being read, the scalar contains octets
912 and its C<SvUTF8> flag is off, but the octets should be interpreted as
913 UTF-8 if the C<use utf8> pragma is in effect. During a string eval,
914 however, the scalar may have the C<SvUTF8> flag on, and in this case its
915 octets should be interpreted as UTF-8 unless the C<use bytes> pragma
916 is in effect. This logic may change in the future; use this function
917 instead of implementing the logic yourself.
918
919 =cut
920 */
921
922 bool
Perl_lex_bufutf8(pTHX)923 Perl_lex_bufutf8(pTHX)
924 {
925 return UTF;
926 }
927
928 /*
929 =for apidoc Amx|char *|lex_grow_linestr|STRLEN len
930
931 Reallocates the lexer buffer (L</PL_parser-E<gt>linestr>) to accommodate
932 at least C<len> octets (including terminating C<NUL>). Returns a
933 pointer to the reallocated buffer. This is necessary before making
934 any direct modification of the buffer that would increase its length.
935 L</lex_stuff_pvn> provides a more convenient way to insert text into
936 the buffer.
937
938 Do not use C<SvGROW> or C<sv_grow> directly on C<PL_parser-E<gt>linestr>;
939 this function updates all of the lexer's variables that point directly
940 into the buffer.
941
942 =cut
943 */
944
945 char *
Perl_lex_grow_linestr(pTHX_ STRLEN len)946 Perl_lex_grow_linestr(pTHX_ STRLEN len)
947 {
948 SV *linestr;
949 char *buf;
950 STRLEN bufend_pos, bufptr_pos, oldbufptr_pos, oldoldbufptr_pos;
951 STRLEN linestart_pos, last_uni_pos, last_lop_pos, re_eval_start_pos;
952 bool current;
953
954 linestr = PL_parser->linestr;
955 buf = SvPVX(linestr);
956 if (len <= SvLEN(linestr))
957 return buf;
958
959 /* Is the lex_shared linestr SV the same as the current linestr SV?
960 * Only in this case does re_eval_start need adjusting, since it
961 * points within lex_shared->ls_linestr's buffer */
962 current = ( !PL_parser->lex_shared->ls_linestr
963 || linestr == PL_parser->lex_shared->ls_linestr);
964
965 bufend_pos = PL_parser->bufend - buf;
966 bufptr_pos = PL_parser->bufptr - buf;
967 oldbufptr_pos = PL_parser->oldbufptr - buf;
968 oldoldbufptr_pos = PL_parser->oldoldbufptr - buf;
969 linestart_pos = PL_parser->linestart - buf;
970 last_uni_pos = PL_parser->last_uni ? PL_parser->last_uni - buf : 0;
971 last_lop_pos = PL_parser->last_lop ? PL_parser->last_lop - buf : 0;
972 re_eval_start_pos = (current && PL_parser->lex_shared->re_eval_start) ?
973 PL_parser->lex_shared->re_eval_start - buf : 0;
974
975 buf = sv_grow(linestr, len);
976
977 PL_parser->bufend = buf + bufend_pos;
978 PL_parser->bufptr = buf + bufptr_pos;
979 PL_parser->oldbufptr = buf + oldbufptr_pos;
980 PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
981 PL_parser->linestart = buf + linestart_pos;
982 if (PL_parser->last_uni)
983 PL_parser->last_uni = buf + last_uni_pos;
984 if (PL_parser->last_lop)
985 PL_parser->last_lop = buf + last_lop_pos;
986 if (current && PL_parser->lex_shared->re_eval_start)
987 PL_parser->lex_shared->re_eval_start = buf + re_eval_start_pos;
988 return buf;
989 }
990
991 /*
992 =for apidoc Amx|void|lex_stuff_pvn|const char *pv|STRLEN len|U32 flags
993
994 Insert characters into the lexer buffer (L</PL_parser-E<gt>linestr>),
995 immediately after the current lexing point (L</PL_parser-E<gt>bufptr>),
996 reallocating the buffer if necessary. This means that lexing code that
997 runs later will see the characters as if they had appeared in the input.
998 It is not recommended to do this as part of normal parsing, and most
999 uses of this facility run the risk of the inserted characters being
1000 interpreted in an unintended manner.
1001
1002 The string to be inserted is represented by C<len> octets starting
1003 at C<pv>. These octets are interpreted as either UTF-8 or Latin-1,
1004 according to whether the C<LEX_STUFF_UTF8> flag is set in C<flags>.
1005 The characters are recoded for the lexer buffer, according to how the
1006 buffer is currently being interpreted (L</lex_bufutf8>). If a string
1007 to be inserted is available as a Perl scalar, the L</lex_stuff_sv>
1008 function is more convenient.
1009
1010 =cut
1011 */
1012
1013 void
Perl_lex_stuff_pvn(pTHX_ const char * pv,STRLEN len,U32 flags)1014 Perl_lex_stuff_pvn(pTHX_ const char *pv, STRLEN len, U32 flags)
1015 {
1016 dVAR;
1017 char *bufptr;
1018 PERL_ARGS_ASSERT_LEX_STUFF_PVN;
1019 if (flags & ~(LEX_STUFF_UTF8))
1020 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_stuff_pvn");
1021 if (UTF) {
1022 if (flags & LEX_STUFF_UTF8) {
1023 goto plain_copy;
1024 } else {
1025 STRLEN highhalf = variant_under_utf8_count((U8 *) pv,
1026 (U8 *) pv + len);
1027 const char *p, *e = pv+len;;
1028 if (!highhalf)
1029 goto plain_copy;
1030 lex_grow_linestr(SvCUR(PL_parser->linestr)+1+len+highhalf);
1031 bufptr = PL_parser->bufptr;
1032 Move(bufptr, bufptr+len+highhalf, PL_parser->bufend+1-bufptr, char);
1033 SvCUR_set(PL_parser->linestr,
1034 SvCUR(PL_parser->linestr) + len+highhalf);
1035 PL_parser->bufend += len+highhalf;
1036 for (p = pv; p != e; p++) {
1037 append_utf8_from_native_byte(*p, (U8 **) &bufptr);
1038 }
1039 }
1040 } else {
1041 if (flags & LEX_STUFF_UTF8) {
1042 STRLEN highhalf = 0;
1043 const char *p, *e = pv+len;
1044 for (p = pv; p != e; p++) {
1045 U8 c = (U8)*p;
1046 if (UTF8_IS_ABOVE_LATIN1(c)) {
1047 Perl_croak(aTHX_ "Lexing code attempted to stuff "
1048 "non-Latin-1 character into Latin-1 input");
1049 } else if (UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(p, e)) {
1050 p++;
1051 highhalf++;
1052 } else assert(UTF8_IS_INVARIANT(c));
1053 }
1054 if (!highhalf)
1055 goto plain_copy;
1056 lex_grow_linestr(SvCUR(PL_parser->linestr)+1+len-highhalf);
1057 bufptr = PL_parser->bufptr;
1058 Move(bufptr, bufptr+len-highhalf, PL_parser->bufend+1-bufptr, char);
1059 SvCUR_set(PL_parser->linestr,
1060 SvCUR(PL_parser->linestr) + len-highhalf);
1061 PL_parser->bufend += len-highhalf;
1062 p = pv;
1063 while (p < e) {
1064 if (UTF8_IS_INVARIANT(*p)) {
1065 *bufptr++ = *p;
1066 p++;
1067 }
1068 else {
1069 assert(p < e -1 );
1070 *bufptr++ = EIGHT_BIT_UTF8_TO_NATIVE(*p, *(p+1));
1071 p += 2;
1072 }
1073 }
1074 } else {
1075 plain_copy:
1076 lex_grow_linestr(SvCUR(PL_parser->linestr)+1+len);
1077 bufptr = PL_parser->bufptr;
1078 Move(bufptr, bufptr+len, PL_parser->bufend+1-bufptr, char);
1079 SvCUR_set(PL_parser->linestr, SvCUR(PL_parser->linestr) + len);
1080 PL_parser->bufend += len;
1081 Copy(pv, bufptr, len, char);
1082 }
1083 }
1084 }
1085
1086 /*
1087 =for apidoc Amx|void|lex_stuff_pv|const char *pv|U32 flags
1088
1089 Insert characters into the lexer buffer (L</PL_parser-E<gt>linestr>),
1090 immediately after the current lexing point (L</PL_parser-E<gt>bufptr>),
1091 reallocating the buffer if necessary. This means that lexing code that
1092 runs later will see the characters as if they had appeared in the input.
1093 It is not recommended to do this as part of normal parsing, and most
1094 uses of this facility run the risk of the inserted characters being
1095 interpreted in an unintended manner.
1096
1097 The string to be inserted is represented by octets starting at C<pv>
1098 and continuing to the first nul. These octets are interpreted as either
1099 UTF-8 or Latin-1, according to whether the C<LEX_STUFF_UTF8> flag is set
1100 in C<flags>. The characters are recoded for the lexer buffer, according
1101 to how the buffer is currently being interpreted (L</lex_bufutf8>).
1102 If it is not convenient to nul-terminate a string to be inserted, the
1103 L</lex_stuff_pvn> function is more appropriate.
1104
1105 =cut
1106 */
1107
1108 void
Perl_lex_stuff_pv(pTHX_ const char * pv,U32 flags)1109 Perl_lex_stuff_pv(pTHX_ const char *pv, U32 flags)
1110 {
1111 PERL_ARGS_ASSERT_LEX_STUFF_PV;
1112 lex_stuff_pvn(pv, strlen(pv), flags);
1113 }
1114
1115 /*
1116 =for apidoc Amx|void|lex_stuff_sv|SV *sv|U32 flags
1117
1118 Insert characters into the lexer buffer (L</PL_parser-E<gt>linestr>),
1119 immediately after the current lexing point (L</PL_parser-E<gt>bufptr>),
1120 reallocating the buffer if necessary. This means that lexing code that
1121 runs later will see the characters as if they had appeared in the input.
1122 It is not recommended to do this as part of normal parsing, and most
1123 uses of this facility run the risk of the inserted characters being
1124 interpreted in an unintended manner.
1125
1126 The string to be inserted is the string value of C<sv>. The characters
1127 are recoded for the lexer buffer, according to how the buffer is currently
1128 being interpreted (L</lex_bufutf8>). If a string to be inserted is
1129 not already a Perl scalar, the L</lex_stuff_pvn> function avoids the
1130 need to construct a scalar.
1131
1132 =cut
1133 */
1134
1135 void
Perl_lex_stuff_sv(pTHX_ SV * sv,U32 flags)1136 Perl_lex_stuff_sv(pTHX_ SV *sv, U32 flags)
1137 {
1138 char *pv;
1139 STRLEN len;
1140 PERL_ARGS_ASSERT_LEX_STUFF_SV;
1141 if (flags)
1142 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_stuff_sv");
1143 pv = SvPV(sv, len);
1144 lex_stuff_pvn(pv, len, flags | (SvUTF8(sv) ? LEX_STUFF_UTF8 : 0));
1145 }
1146
1147 /*
1148 =for apidoc Amx|void|lex_unstuff|char *ptr
1149
1150 Discards text about to be lexed, from L</PL_parser-E<gt>bufptr> up to
1151 C<ptr>. Text following C<ptr> will be moved, and the buffer shortened.
1152 This hides the discarded text from any lexing code that runs later,
1153 as if the text had never appeared.
1154
1155 This is not the normal way to consume lexed text. For that, use
1156 L</lex_read_to>.
1157
1158 =cut
1159 */
1160
1161 void
Perl_lex_unstuff(pTHX_ char * ptr)1162 Perl_lex_unstuff(pTHX_ char *ptr)
1163 {
1164 char *buf, *bufend;
1165 STRLEN unstuff_len;
1166 PERL_ARGS_ASSERT_LEX_UNSTUFF;
1167 buf = PL_parser->bufptr;
1168 if (ptr < buf)
1169 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_unstuff");
1170 if (ptr == buf)
1171 return;
1172 bufend = PL_parser->bufend;
1173 if (ptr > bufend)
1174 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_unstuff");
1175 unstuff_len = ptr - buf;
1176 Move(ptr, buf, bufend+1-ptr, char);
1177 SvCUR_set(PL_parser->linestr, SvCUR(PL_parser->linestr) - unstuff_len);
1178 PL_parser->bufend = bufend - unstuff_len;
1179 }
1180
1181 /*
1182 =for apidoc Amx|void|lex_read_to|char *ptr
1183
1184 Consume text in the lexer buffer, from L</PL_parser-E<gt>bufptr> up
1185 to C<ptr>. This advances L</PL_parser-E<gt>bufptr> to match C<ptr>,
1186 performing the correct bookkeeping whenever a newline character is passed.
1187 This is the normal way to consume lexed text.
1188
1189 Interpretation of the buffer's octets can be abstracted out by
1190 using the slightly higher-level functions L</lex_peek_unichar> and
1191 L</lex_read_unichar>.
1192
1193 =cut
1194 */
1195
1196 void
Perl_lex_read_to(pTHX_ char * ptr)1197 Perl_lex_read_to(pTHX_ char *ptr)
1198 {
1199 char *s;
1200 PERL_ARGS_ASSERT_LEX_READ_TO;
1201 s = PL_parser->bufptr;
1202 if (ptr < s || ptr > PL_parser->bufend)
1203 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_to");
1204 for (; s != ptr; s++)
1205 if (*s == '\n') {
1206 COPLINE_INC_WITH_HERELINES;
1207 PL_parser->linestart = s+1;
1208 }
1209 PL_parser->bufptr = ptr;
1210 }
1211
1212 /*
1213 =for apidoc Amx|void|lex_discard_to|char *ptr
1214
1215 Discards the first part of the L</PL_parser-E<gt>linestr> buffer,
1216 up to C<ptr>. The remaining content of the buffer will be moved, and
1217 all pointers into the buffer updated appropriately. C<ptr> must not
1218 be later in the buffer than the position of L</PL_parser-E<gt>bufptr>:
1219 it is not permitted to discard text that has yet to be lexed.
1220
1221 Normally it is not necessarily to do this directly, because it suffices to
1222 use the implicit discarding behaviour of L</lex_next_chunk> and things
1223 based on it. However, if a token stretches across multiple lines,
1224 and the lexing code has kept multiple lines of text in the buffer for
1225 that purpose, then after completion of the token it would be wise to
1226 explicitly discard the now-unneeded earlier lines, to avoid future
1227 multi-line tokens growing the buffer without bound.
1228
1229 =cut
1230 */
1231
1232 void
Perl_lex_discard_to(pTHX_ char * ptr)1233 Perl_lex_discard_to(pTHX_ char *ptr)
1234 {
1235 char *buf;
1236 STRLEN discard_len;
1237 PERL_ARGS_ASSERT_LEX_DISCARD_TO;
1238 buf = SvPVX(PL_parser->linestr);
1239 if (ptr < buf)
1240 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_discard_to");
1241 if (ptr == buf)
1242 return;
1243 if (ptr > PL_parser->bufptr)
1244 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_discard_to");
1245 discard_len = ptr - buf;
1246 if (PL_parser->oldbufptr < ptr)
1247 PL_parser->oldbufptr = ptr;
1248 if (PL_parser->oldoldbufptr < ptr)
1249 PL_parser->oldoldbufptr = ptr;
1250 if (PL_parser->last_uni && PL_parser->last_uni < ptr)
1251 PL_parser->last_uni = NULL;
1252 if (PL_parser->last_lop && PL_parser->last_lop < ptr)
1253 PL_parser->last_lop = NULL;
1254 Move(ptr, buf, PL_parser->bufend+1-ptr, char);
1255 SvCUR_set(PL_parser->linestr, SvCUR(PL_parser->linestr) - discard_len);
1256 PL_parser->bufend -= discard_len;
1257 PL_parser->bufptr -= discard_len;
1258 PL_parser->oldbufptr -= discard_len;
1259 PL_parser->oldoldbufptr -= discard_len;
1260 if (PL_parser->last_uni)
1261 PL_parser->last_uni -= discard_len;
1262 if (PL_parser->last_lop)
1263 PL_parser->last_lop -= discard_len;
1264 }
1265
1266 void
Perl_notify_parser_that_changed_to_utf8(pTHX)1267 Perl_notify_parser_that_changed_to_utf8(pTHX)
1268 {
1269 /* Called when $^H is changed to indicate that HINT_UTF8 has changed from
1270 * off to on. At compile time, this has the effect of entering a 'use
1271 * utf8' section. This means that any input was not previously checked for
1272 * UTF-8 (because it was off), but now we do need to check it, or our
1273 * assumptions about the input being sane could be wrong, and we could
1274 * segfault. This routine just sets a flag so that the next time we look
1275 * at the input we do the well-formed UTF-8 check. If we aren't in the
1276 * proper phase, there may not be a parser object, but if there is, setting
1277 * the flag is harmless */
1278
1279 if (PL_parser) {
1280 PL_parser->recheck_utf8_validity = TRUE;
1281 }
1282 }
1283
1284 /*
1285 =for apidoc Amx|bool|lex_next_chunk|U32 flags
1286
1287 Reads in the next chunk of text to be lexed, appending it to
1288 L</PL_parser-E<gt>linestr>. This should be called when lexing code has
1289 looked to the end of the current chunk and wants to know more. It is
1290 usual, but not necessary, for lexing to have consumed the entirety of
1291 the current chunk at this time.
1292
1293 If L</PL_parser-E<gt>bufptr> is pointing to the very end of the current
1294 chunk (i.e., the current chunk has been entirely consumed), normally the
1295 current chunk will be discarded at the same time that the new chunk is
1296 read in. If C<flags> has the C<LEX_KEEP_PREVIOUS> bit set, the current chunk
1297 will not be discarded. If the current chunk has not been entirely
1298 consumed, then it will not be discarded regardless of the flag.
1299
1300 Returns true if some new text was added to the buffer, or false if the
1301 buffer has reached the end of the input text.
1302
1303 =cut
1304 */
1305
1306 #define LEX_FAKE_EOF 0x80000000
1307 #define LEX_NO_TERM 0x40000000 /* here-doc */
1308
1309 bool
Perl_lex_next_chunk(pTHX_ U32 flags)1310 Perl_lex_next_chunk(pTHX_ U32 flags)
1311 {
1312 SV *linestr;
1313 char *buf;
1314 STRLEN old_bufend_pos, new_bufend_pos;
1315 STRLEN bufptr_pos, oldbufptr_pos, oldoldbufptr_pos;
1316 STRLEN linestart_pos, last_uni_pos, last_lop_pos;
1317 bool got_some_for_debugger = 0;
1318 bool got_some;
1319
1320 if (flags & ~(LEX_KEEP_PREVIOUS|LEX_FAKE_EOF|LEX_NO_TERM))
1321 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_next_chunk");
1322 if (!(flags & LEX_NO_TERM) && PL_lex_inwhat)
1323 return FALSE;
1324 linestr = PL_parser->linestr;
1325 buf = SvPVX(linestr);
1326 if (!(flags & LEX_KEEP_PREVIOUS)
1327 && PL_parser->bufptr == PL_parser->bufend)
1328 {
1329 old_bufend_pos = bufptr_pos = oldbufptr_pos = oldoldbufptr_pos = 0;
1330 linestart_pos = 0;
1331 if (PL_parser->last_uni != PL_parser->bufend)
1332 PL_parser->last_uni = NULL;
1333 if (PL_parser->last_lop != PL_parser->bufend)
1334 PL_parser->last_lop = NULL;
1335 last_uni_pos = last_lop_pos = 0;
1336 *buf = 0;
1337 SvCUR(linestr) = 0;
1338 } else {
1339 old_bufend_pos = PL_parser->bufend - buf;
1340 bufptr_pos = PL_parser->bufptr - buf;
1341 oldbufptr_pos = PL_parser->oldbufptr - buf;
1342 oldoldbufptr_pos = PL_parser->oldoldbufptr - buf;
1343 linestart_pos = PL_parser->linestart - buf;
1344 last_uni_pos = PL_parser->last_uni ? PL_parser->last_uni - buf : 0;
1345 last_lop_pos = PL_parser->last_lop ? PL_parser->last_lop - buf : 0;
1346 }
1347 if (flags & LEX_FAKE_EOF) {
1348 goto eof;
1349 } else if (!PL_parser->rsfp && !PL_parser->filtered) {
1350 got_some = 0;
1351 } else if (filter_gets(linestr, old_bufend_pos)) {
1352 got_some = 1;
1353 got_some_for_debugger = 1;
1354 } else if (flags & LEX_NO_TERM) {
1355 got_some = 0;
1356 } else {
1357 if (!SvPOK(linestr)) /* can get undefined by filter_gets */
1358 SvPVCLEAR(linestr);
1359 eof:
1360 /* End of real input. Close filehandle (unless it was STDIN),
1361 * then add implicit termination.
1362 */
1363 if (PL_parser->lex_flags & LEX_DONT_CLOSE_RSFP)
1364 PerlIO_clearerr(PL_parser->rsfp);
1365 else if (PL_parser->rsfp)
1366 (void)PerlIO_close(PL_parser->rsfp);
1367 PL_parser->rsfp = NULL;
1368 PL_parser->in_pod = PL_parser->filtered = 0;
1369 if (!PL_in_eval && PL_minus_p) {
1370 sv_catpvs(linestr,
1371 /*{*/";}continue{print or die qq(-p destination: $!\\n);}");
1372 PL_minus_n = PL_minus_p = 0;
1373 } else if (!PL_in_eval && PL_minus_n) {
1374 sv_catpvs(linestr, /*{*/";}");
1375 PL_minus_n = 0;
1376 } else
1377 sv_catpvs(linestr, ";");
1378 got_some = 1;
1379 }
1380 buf = SvPVX(linestr);
1381 new_bufend_pos = SvCUR(linestr);
1382 PL_parser->bufend = buf + new_bufend_pos;
1383 PL_parser->bufptr = buf + bufptr_pos;
1384
1385 if (UTF) {
1386 const U8* first_bad_char_loc;
1387 if (UNLIKELY(! is_utf8_string_loc(
1388 (U8 *) PL_parser->bufptr,
1389 PL_parser->bufend - PL_parser->bufptr,
1390 &first_bad_char_loc)))
1391 {
1392 _force_out_malformed_utf8_message(first_bad_char_loc,
1393 (U8 *) PL_parser->bufend,
1394 0,
1395 1 /* 1 means die */ );
1396 NOT_REACHED; /* NOTREACHED */
1397 }
1398 }
1399
1400 PL_parser->oldbufptr = buf + oldbufptr_pos;
1401 PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
1402 PL_parser->linestart = buf + linestart_pos;
1403 if (PL_parser->last_uni)
1404 PL_parser->last_uni = buf + last_uni_pos;
1405 if (PL_parser->last_lop)
1406 PL_parser->last_lop = buf + last_lop_pos;
1407 if (PL_parser->preambling != NOLINE) {
1408 CopLINE_set(PL_curcop, PL_parser->preambling + 1);
1409 PL_parser->preambling = NOLINE;
1410 }
1411 if ( got_some_for_debugger
1412 && PERLDB_LINE_OR_SAVESRC
1413 && PL_curstash != PL_debstash)
1414 {
1415 /* debugger active and we're not compiling the debugger code,
1416 * so store the line into the debugger's array of lines
1417 */
1418 update_debugger_info(NULL, buf+old_bufend_pos,
1419 new_bufend_pos-old_bufend_pos);
1420 }
1421 return got_some;
1422 }
1423
1424 /*
1425 =for apidoc Amx|I32|lex_peek_unichar|U32 flags
1426
1427 Looks ahead one (Unicode) character in the text currently being lexed.
1428 Returns the codepoint (unsigned integer value) of the next character,
1429 or -1 if lexing has reached the end of the input text. To consume the
1430 peeked character, use L</lex_read_unichar>.
1431
1432 If the next character is in (or extends into) the next chunk of input
1433 text, the next chunk will be read in. Normally the current chunk will be
1434 discarded at the same time, but if C<flags> has the C<LEX_KEEP_PREVIOUS>
1435 bit set, then the current chunk will not be discarded.
1436
1437 If the input is being interpreted as UTF-8 and a UTF-8 encoding error
1438 is encountered, an exception is generated.
1439
1440 =cut
1441 */
1442
1443 I32
Perl_lex_peek_unichar(pTHX_ U32 flags)1444 Perl_lex_peek_unichar(pTHX_ U32 flags)
1445 {
1446 dVAR;
1447 char *s, *bufend;
1448 if (flags & ~(LEX_KEEP_PREVIOUS))
1449 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_peek_unichar");
1450 s = PL_parser->bufptr;
1451 bufend = PL_parser->bufend;
1452 if (UTF) {
1453 U8 head;
1454 I32 unichar;
1455 STRLEN len, retlen;
1456 if (s == bufend) {
1457 if (!lex_next_chunk(flags))
1458 return -1;
1459 s = PL_parser->bufptr;
1460 bufend = PL_parser->bufend;
1461 }
1462 head = (U8)*s;
1463 if (UTF8_IS_INVARIANT(head))
1464 return head;
1465 if (UTF8_IS_START(head)) {
1466 len = UTF8SKIP(&head);
1467 while ((STRLEN)(bufend-s) < len) {
1468 if (!lex_next_chunk(flags | LEX_KEEP_PREVIOUS))
1469 break;
1470 s = PL_parser->bufptr;
1471 bufend = PL_parser->bufend;
1472 }
1473 }
1474 unichar = utf8n_to_uvchr((U8*)s, bufend-s, &retlen, UTF8_CHECK_ONLY);
1475 if (retlen == (STRLEN)-1) {
1476 _force_out_malformed_utf8_message((U8 *) s,
1477 (U8 *) bufend,
1478 0,
1479 1 /* 1 means die */ );
1480 NOT_REACHED; /* NOTREACHED */
1481 }
1482 return unichar;
1483 } else {
1484 if (s == bufend) {
1485 if (!lex_next_chunk(flags))
1486 return -1;
1487 s = PL_parser->bufptr;
1488 }
1489 return (U8)*s;
1490 }
1491 }
1492
1493 /*
1494 =for apidoc Amx|I32|lex_read_unichar|U32 flags
1495
1496 Reads the next (Unicode) character in the text currently being lexed.
1497 Returns the codepoint (unsigned integer value) of the character read,
1498 and moves L</PL_parser-E<gt>bufptr> past the character, or returns -1
1499 if lexing has reached the end of the input text. To non-destructively
1500 examine the next character, use L</lex_peek_unichar> instead.
1501
1502 If the next character is in (or extends into) the next chunk of input
1503 text, the next chunk will be read in. Normally the current chunk will be
1504 discarded at the same time, but if C<flags> has the C<LEX_KEEP_PREVIOUS>
1505 bit set, then the current chunk will not be discarded.
1506
1507 If the input is being interpreted as UTF-8 and a UTF-8 encoding error
1508 is encountered, an exception is generated.
1509
1510 =cut
1511 */
1512
1513 I32
Perl_lex_read_unichar(pTHX_ U32 flags)1514 Perl_lex_read_unichar(pTHX_ U32 flags)
1515 {
1516 I32 c;
1517 if (flags & ~(LEX_KEEP_PREVIOUS))
1518 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_unichar");
1519 c = lex_peek_unichar(flags);
1520 if (c != -1) {
1521 if (c == '\n')
1522 COPLINE_INC_WITH_HERELINES;
1523 if (UTF)
1524 PL_parser->bufptr += UTF8SKIP(PL_parser->bufptr);
1525 else
1526 ++(PL_parser->bufptr);
1527 }
1528 return c;
1529 }
1530
1531 /*
1532 =for apidoc Amx|void|lex_read_space|U32 flags
1533
1534 Reads optional spaces, in Perl style, in the text currently being
1535 lexed. The spaces may include ordinary whitespace characters and
1536 Perl-style comments. C<#line> directives are processed if encountered.
1537 L</PL_parser-E<gt>bufptr> is moved past the spaces, so that it points
1538 at a non-space character (or the end of the input text).
1539
1540 If spaces extend into the next chunk of input text, the next chunk will
1541 be read in. Normally the current chunk will be discarded at the same
1542 time, but if C<flags> has the C<LEX_KEEP_PREVIOUS> bit set, then the current
1543 chunk will not be discarded.
1544
1545 =cut
1546 */
1547
1548 #define LEX_NO_INCLINE 0x40000000
1549 #define LEX_NO_NEXT_CHUNK 0x80000000
1550
1551 void
Perl_lex_read_space(pTHX_ U32 flags)1552 Perl_lex_read_space(pTHX_ U32 flags)
1553 {
1554 char *s, *bufend;
1555 const bool can_incline = !(flags & LEX_NO_INCLINE);
1556 bool need_incline = 0;
1557 if (flags & ~(LEX_KEEP_PREVIOUS|LEX_NO_NEXT_CHUNK|LEX_NO_INCLINE))
1558 Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_space");
1559 s = PL_parser->bufptr;
1560 bufend = PL_parser->bufend;
1561 while (1) {
1562 char c = *s;
1563 if (c == '#') {
1564 do {
1565 c = *++s;
1566 } while (!(c == '\n' || (c == 0 && s == bufend)));
1567 } else if (c == '\n') {
1568 s++;
1569 if (can_incline) {
1570 PL_parser->linestart = s;
1571 if (s == bufend)
1572 need_incline = 1;
1573 else
1574 incline(s, bufend);
1575 }
1576 } else if (isSPACE(c)) {
1577 s++;
1578 } else if (c == 0 && s == bufend) {
1579 bool got_more;
1580 line_t l;
1581 if (flags & LEX_NO_NEXT_CHUNK)
1582 break;
1583 PL_parser->bufptr = s;
1584 l = CopLINE(PL_curcop);
1585 CopLINE(PL_curcop) += PL_parser->herelines + 1;
1586 got_more = lex_next_chunk(flags);
1587 CopLINE_set(PL_curcop, l);
1588 s = PL_parser->bufptr;
1589 bufend = PL_parser->bufend;
1590 if (!got_more)
1591 break;
1592 if (can_incline && need_incline && PL_parser->rsfp) {
1593 incline(s, bufend);
1594 need_incline = 0;
1595 }
1596 } else if (!c) {
1597 s++;
1598 } else {
1599 break;
1600 }
1601 }
1602 PL_parser->bufptr = s;
1603 }
1604
1605 /*
1606
1607 =for apidoc EXMp|bool|validate_proto|SV *name|SV *proto|bool warn
1608
1609 This function performs syntax checking on a prototype, C<proto>.
1610 If C<warn> is true, any illegal characters or mismatched brackets
1611 will trigger illegalproto warnings, declaring that they were
1612 detected in the prototype for C<name>.
1613
1614 The return value is C<true> if this is a valid prototype, and
1615 C<false> if it is not, regardless of whether C<warn> was C<true> or
1616 C<false>.
1617
1618 Note that C<NULL> is a valid C<proto> and will always return C<true>.
1619
1620 =cut
1621
1622 */
1623
1624 bool
Perl_validate_proto(pTHX_ SV * name,SV * proto,bool warn,bool curstash)1625 Perl_validate_proto(pTHX_ SV *name, SV *proto, bool warn, bool curstash)
1626 {
1627 STRLEN len, origlen;
1628 char *p;
1629 bool bad_proto = FALSE;
1630 bool in_brackets = FALSE;
1631 bool after_slash = FALSE;
1632 char greedy_proto = ' ';
1633 bool proto_after_greedy_proto = FALSE;
1634 bool must_be_last = FALSE;
1635 bool underscore = FALSE;
1636 bool bad_proto_after_underscore = FALSE;
1637
1638 PERL_ARGS_ASSERT_VALIDATE_PROTO;
1639
1640 if (!proto)
1641 return TRUE;
1642
1643 p = SvPV(proto, len);
1644 origlen = len;
1645 for (; len--; p++) {
1646 if (!isSPACE(*p)) {
1647 if (must_be_last)
1648 proto_after_greedy_proto = TRUE;
1649 if (underscore) {
1650 if (!strchr(";@%", *p))
1651 bad_proto_after_underscore = TRUE;
1652 underscore = FALSE;
1653 }
1654 if (!strchr("$@%*;[]&\\_+", *p) || *p == '\0') {
1655 bad_proto = TRUE;
1656 }
1657 else {
1658 if (*p == '[')
1659 in_brackets = TRUE;
1660 else if (*p == ']')
1661 in_brackets = FALSE;
1662 else if ((*p == '@' || *p == '%')
1663 && !after_slash
1664 && !in_brackets )
1665 {
1666 must_be_last = TRUE;
1667 greedy_proto = *p;
1668 }
1669 else if (*p == '_')
1670 underscore = TRUE;
1671 }
1672 if (*p == '\\')
1673 after_slash = TRUE;
1674 else
1675 after_slash = FALSE;
1676 }
1677 }
1678
1679 if (warn) {
1680 SV *tmpsv = newSVpvs_flags("", SVs_TEMP);
1681 p -= origlen;
1682 p = SvUTF8(proto)
1683 ? sv_uni_display(tmpsv, newSVpvn_flags(p, origlen, SVs_TEMP | SVf_UTF8),
1684 origlen, UNI_DISPLAY_ISPRINT)
1685 : pv_pretty(tmpsv, p, origlen, 60, NULL, NULL, PERL_PV_ESCAPE_NONASCII);
1686
1687 if (curstash && !memchr(SvPVX(name), ':', SvCUR(name))) {
1688 SV *name2 = sv_2mortal(newSVsv(PL_curstname));
1689 sv_catpvs(name2, "::");
1690 sv_catsv(name2, (SV *)name);
1691 name = name2;
1692 }
1693
1694 if (proto_after_greedy_proto)
1695 Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1696 "Prototype after '%c' for %" SVf " : %s",
1697 greedy_proto, SVfARG(name), p);
1698 if (in_brackets)
1699 Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1700 "Missing ']' in prototype for %" SVf " : %s",
1701 SVfARG(name), p);
1702 if (bad_proto)
1703 Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1704 "Illegal character in prototype for %" SVf " : %s",
1705 SVfARG(name), p);
1706 if (bad_proto_after_underscore)
1707 Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1708 "Illegal character after '_' in prototype for %" SVf " : %s",
1709 SVfARG(name), p);
1710 }
1711
1712 return (! (proto_after_greedy_proto || bad_proto) );
1713 }
1714
1715 /*
1716 * S_incline
1717 * This subroutine has nothing to do with tilting, whether at windmills
1718 * or pinball tables. Its name is short for "increment line". It
1719 * increments the current line number in CopLINE(PL_curcop) and checks
1720 * to see whether the line starts with a comment of the form
1721 * # line 500 "foo.pm"
1722 * If so, it sets the current line number and file to the values in the comment.
1723 */
1724
1725 STATIC void
S_incline(pTHX_ const char * s,const char * end)1726 S_incline(pTHX_ const char *s, const char *end)
1727 {
1728 const char *t;
1729 const char *n;
1730 const char *e;
1731 line_t line_num;
1732 UV uv;
1733
1734 PERL_ARGS_ASSERT_INCLINE;
1735
1736 assert(end >= s);
1737
1738 COPLINE_INC_WITH_HERELINES;
1739 if (!PL_rsfp && !PL_parser->filtered && PL_lex_state == LEX_NORMAL
1740 && s+1 == PL_bufend && *s == ';') {
1741 /* fake newline in string eval */
1742 CopLINE_dec(PL_curcop);
1743 return;
1744 }
1745 if (*s++ != '#')
1746 return;
1747 while (SPACE_OR_TAB(*s))
1748 s++;
1749 if (memBEGINs(s, (STRLEN) (end - s), "line"))
1750 s += sizeof("line") - 1;
1751 else
1752 return;
1753 if (SPACE_OR_TAB(*s))
1754 s++;
1755 else
1756 return;
1757 while (SPACE_OR_TAB(*s))
1758 s++;
1759 if (!isDIGIT(*s))
1760 return;
1761
1762 n = s;
1763 while (isDIGIT(*s))
1764 s++;
1765 if (!SPACE_OR_TAB(*s) && *s != '\r' && *s != '\n' && *s != '\0')
1766 return;
1767 while (SPACE_OR_TAB(*s))
1768 s++;
1769 if (*s == '"' && (t = (char *) memchr(s+1, '"', end - s))) {
1770 s++;
1771 e = t + 1;
1772 }
1773 else {
1774 t = s;
1775 while (*t && !isSPACE(*t))
1776 t++;
1777 e = t;
1778 }
1779 while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
1780 e++;
1781 if (*e != '\n' && *e != '\0')
1782 return; /* false alarm */
1783
1784 if (!grok_atoUV(n, &uv, &e))
1785 return;
1786 line_num = ((line_t)uv) - 1;
1787
1788 if (t - s > 0) {
1789 const STRLEN len = t - s;
1790
1791 if (!PL_rsfp && !PL_parser->filtered) {
1792 /* must copy *{"::_<(eval N)[oldfilename:L]"}
1793 * to *{"::_<newfilename"} */
1794 /* However, the long form of evals is only turned on by the
1795 debugger - usually they're "(eval %lu)" */
1796 GV * const cfgv = CopFILEGV(PL_curcop);
1797 if (cfgv) {
1798 char smallbuf[128];
1799 STRLEN tmplen2 = len;
1800 char *tmpbuf2;
1801 GV *gv2;
1802
1803 if (tmplen2 + 2 <= sizeof smallbuf)
1804 tmpbuf2 = smallbuf;
1805 else
1806 Newx(tmpbuf2, tmplen2 + 2, char);
1807
1808 tmpbuf2[0] = '_';
1809 tmpbuf2[1] = '<';
1810
1811 memcpy(tmpbuf2 + 2, s, tmplen2);
1812 tmplen2 += 2;
1813
1814 gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE);
1815 if (!isGV(gv2)) {
1816 gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE);
1817 /* adjust ${"::_<newfilename"} to store the new file name */
1818 GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2);
1819 /* The line number may differ. If that is the case,
1820 alias the saved lines that are in the array.
1821 Otherwise alias the whole array. */
1822 if (CopLINE(PL_curcop) == line_num) {
1823 GvHV(gv2) = MUTABLE_HV(SvREFCNT_inc(GvHV(cfgv)));
1824 GvAV(gv2) = MUTABLE_AV(SvREFCNT_inc(GvAV(cfgv)));
1825 }
1826 else if (GvAV(cfgv)) {
1827 AV * const av = GvAV(cfgv);
1828 const line_t start = CopLINE(PL_curcop)+1;
1829 SSize_t items = AvFILLp(av) - start;
1830 if (items > 0) {
1831 AV * const av2 = GvAVn(gv2);
1832 SV **svp = AvARRAY(av) + start;
1833 Size_t l = line_num+1;
1834 while (items-- && l < SSize_t_MAX && l == (line_t)l)
1835 av_store(av2, (SSize_t)l++, SvREFCNT_inc(*svp++));
1836 }
1837 }
1838 }
1839
1840 if (tmpbuf2 != smallbuf) Safefree(tmpbuf2);
1841 }
1842 }
1843 CopFILE_free(PL_curcop);
1844 CopFILE_setn(PL_curcop, s, len);
1845 }
1846 CopLINE_set(PL_curcop, line_num);
1847 }
1848
1849 STATIC void
S_update_debugger_info(pTHX_ SV * orig_sv,const char * const buf,STRLEN len)1850 S_update_debugger_info(pTHX_ SV *orig_sv, const char *const buf, STRLEN len)
1851 {
1852 AV *av = CopFILEAVx(PL_curcop);
1853 if (av) {
1854 SV * sv;
1855 if (PL_parser->preambling == NOLINE) sv = newSV_type(SVt_PVMG);
1856 else {
1857 sv = *av_fetch(av, 0, 1);
1858 SvUPGRADE(sv, SVt_PVMG);
1859 }
1860 if (!SvPOK(sv)) SvPVCLEAR(sv);
1861 if (orig_sv)
1862 sv_catsv(sv, orig_sv);
1863 else
1864 sv_catpvn(sv, buf, len);
1865 if (!SvIOK(sv)) {
1866 (void)SvIOK_on(sv);
1867 SvIV_set(sv, 0);
1868 }
1869 if (PL_parser->preambling == NOLINE)
1870 av_store(av, CopLINE(PL_curcop), sv);
1871 }
1872 }
1873
1874 /*
1875 * skipspace
1876 * Called to gobble the appropriate amount and type of whitespace.
1877 * Skips comments as well.
1878 * Returns the next character after the whitespace that is skipped.
1879 *
1880 * peekspace
1881 * Same thing, but look ahead without incrementing line numbers or
1882 * adjusting PL_linestart.
1883 */
1884
1885 #define skipspace(s) skipspace_flags(s, 0)
1886 #define peekspace(s) skipspace_flags(s, LEX_NO_INCLINE)
1887
1888 char *
Perl_skipspace_flags(pTHX_ char * s,U32 flags)1889 Perl_skipspace_flags(pTHX_ char *s, U32 flags)
1890 {
1891 PERL_ARGS_ASSERT_SKIPSPACE_FLAGS;
1892 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
1893 while (s < PL_bufend && (SPACE_OR_TAB(*s) || !*s))
1894 s++;
1895 } else {
1896 STRLEN bufptr_pos = PL_bufptr - SvPVX(PL_linestr);
1897 PL_bufptr = s;
1898 lex_read_space(flags | LEX_KEEP_PREVIOUS |
1899 (PL_lex_inwhat || PL_lex_state == LEX_FORMLINE ?
1900 LEX_NO_NEXT_CHUNK : 0));
1901 s = PL_bufptr;
1902 PL_bufptr = SvPVX(PL_linestr) + bufptr_pos;
1903 if (PL_linestart > PL_bufptr)
1904 PL_bufptr = PL_linestart;
1905 return s;
1906 }
1907 return s;
1908 }
1909
1910 /*
1911 * S_check_uni
1912 * Check the unary operators to ensure there's no ambiguity in how they're
1913 * used. An ambiguous piece of code would be:
1914 * rand + 5
1915 * This doesn't mean rand() + 5. Because rand() is a unary operator,
1916 * the +5 is its argument.
1917 */
1918
1919 STATIC void
S_check_uni(pTHX)1920 S_check_uni(pTHX)
1921 {
1922 const char *s;
1923
1924 if (PL_oldoldbufptr != PL_last_uni)
1925 return;
1926 while (isSPACE(*PL_last_uni))
1927 PL_last_uni++;
1928 s = PL_last_uni;
1929 while (isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF) || *s == '-')
1930 s += UTF ? UTF8SKIP(s) : 1;
1931 if (s < PL_bufptr && memchr(s, '(', PL_bufptr - s))
1932 return;
1933
1934 Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
1935 "Warning: Use of \"%" UTF8f "\" without parentheses is ambiguous",
1936 UTF8fARG(UTF, (int)(s - PL_last_uni), PL_last_uni));
1937 }
1938
1939 /*
1940 * LOP : macro to build a list operator. Its behaviour has been replaced
1941 * with a subroutine, S_lop() for which LOP is just another name.
1942 */
1943
1944 #define LOP(f,x) return lop(f,x,s)
1945
1946 /*
1947 * S_lop
1948 * Build a list operator (or something that might be one). The rules:
1949 * - if we have a next token, then it's a list operator (no parens) for
1950 * which the next token has already been parsed; e.g.,
1951 * sort foo @args
1952 * sort foo (@args)
1953 * - if the next thing is an opening paren, then it's a function
1954 * - else it's a list operator
1955 */
1956
1957 STATIC I32
S_lop(pTHX_ I32 f,U8 x,char * s)1958 S_lop(pTHX_ I32 f, U8 x, char *s)
1959 {
1960 PERL_ARGS_ASSERT_LOP;
1961
1962 pl_yylval.ival = f;
1963 CLINE;
1964 PL_bufptr = s;
1965 PL_last_lop = PL_oldbufptr;
1966 PL_last_lop_op = (OPCODE)f;
1967 if (PL_nexttoke)
1968 goto lstop;
1969 PL_expect = x;
1970 if (*s == '(')
1971 return REPORT(FUNC);
1972 s = skipspace(s);
1973 if (*s == '(')
1974 return REPORT(FUNC);
1975 else {
1976 lstop:
1977 if (!PL_lex_allbrackets && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
1978 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
1979 return REPORT(LSTOP);
1980 }
1981 }
1982
1983 /*
1984 * S_force_next
1985 * When the lexer realizes it knows the next token (for instance,
1986 * it is reordering tokens for the parser) then it can call S_force_next
1987 * to know what token to return the next time the lexer is called. Caller
1988 * will need to set PL_nextval[] and possibly PL_expect to ensure
1989 * the lexer handles the token correctly.
1990 */
1991
1992 STATIC void
S_force_next(pTHX_ I32 type)1993 S_force_next(pTHX_ I32 type)
1994 {
1995 #ifdef DEBUGGING
1996 if (DEBUG_T_TEST) {
1997 PerlIO_printf(Perl_debug_log, "### forced token:\n");
1998 tokereport(type, &NEXTVAL_NEXTTOKE);
1999 }
2000 #endif
2001 assert(PL_nexttoke < C_ARRAY_LENGTH(PL_nexttype));
2002 PL_nexttype[PL_nexttoke] = type;
2003 PL_nexttoke++;
2004 }
2005
2006 /*
2007 * S_postderef
2008 *
2009 * This subroutine handles postfix deref syntax after the arrow has already
2010 * been emitted. @* $* etc. are emitted as two separate tokens right here.
2011 * @[ @{ %[ %{ *{ are emitted also as two tokens, but this function emits
2012 * only the first, leaving yylex to find the next.
2013 */
2014
2015 static int
S_postderef(pTHX_ int const funny,char const next)2016 S_postderef(pTHX_ int const funny, char const next)
2017 {
2018 assert(funny == DOLSHARP || strchr("$@%&*", funny));
2019 if (next == '*') {
2020 PL_expect = XOPERATOR;
2021 if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) {
2022 assert('@' == funny || '$' == funny || DOLSHARP == funny);
2023 PL_lex_state = LEX_INTERPEND;
2024 if ('@' == funny)
2025 force_next(POSTJOIN);
2026 }
2027 force_next(next);
2028 PL_bufptr+=2;
2029 }
2030 else {
2031 if ('@' == funny && PL_lex_state == LEX_INTERPNORMAL
2032 && !PL_lex_brackets)
2033 PL_lex_dojoin = 2;
2034 PL_expect = XOPERATOR;
2035 PL_bufptr++;
2036 }
2037 return funny;
2038 }
2039
2040 void
Perl_yyunlex(pTHX)2041 Perl_yyunlex(pTHX)
2042 {
2043 int yyc = PL_parser->yychar;
2044 if (yyc != YYEMPTY) {
2045 if (yyc) {
2046 NEXTVAL_NEXTTOKE = PL_parser->yylval;
2047 if (yyc == '{'/*}*/ || yyc == HASHBRACK || yyc == '['/*]*/) {
2048 PL_lex_allbrackets--;
2049 PL_lex_brackets--;
2050 yyc |= (3<<24) | (PL_lex_brackstack[PL_lex_brackets] << 16);
2051 } else if (yyc == '('/*)*/) {
2052 PL_lex_allbrackets--;
2053 yyc |= (2<<24);
2054 }
2055 force_next(yyc);
2056 }
2057 PL_parser->yychar = YYEMPTY;
2058 }
2059 }
2060
2061 STATIC SV *
S_newSV_maybe_utf8(pTHX_ const char * const start,STRLEN len)2062 S_newSV_maybe_utf8(pTHX_ const char *const start, STRLEN len)
2063 {
2064 SV * const sv = newSVpvn_utf8(start, len,
2065 ! IN_BYTES
2066 && UTF
2067 && len != 0
2068 && is_utf8_non_invariant_string((const U8*)start, len));
2069 return sv;
2070 }
2071
2072 /*
2073 * S_force_word
2074 * When the lexer knows the next thing is a word (for instance, it has
2075 * just seen -> and it knows that the next char is a word char, then
2076 * it calls S_force_word to stick the next word into the PL_nexttoke/val
2077 * lookahead.
2078 *
2079 * Arguments:
2080 * char *start : buffer position (must be within PL_linestr)
2081 * int token : PL_next* will be this type of bare word
2082 * (e.g., METHOD,BAREWORD)
2083 * int check_keyword : if true, Perl checks to make sure the word isn't
2084 * a keyword (do this if the word is a label, e.g. goto FOO)
2085 * int allow_pack : if true, : characters will also be allowed (require,
2086 * use, etc. do this)
2087 */
2088
2089 STATIC char *
S_force_word(pTHX_ char * start,int token,int check_keyword,int allow_pack)2090 S_force_word(pTHX_ char *start, int token, int check_keyword, int allow_pack)
2091 {
2092 char *s;
2093 STRLEN len;
2094
2095 PERL_ARGS_ASSERT_FORCE_WORD;
2096
2097 start = skipspace(start);
2098 s = start;
2099 if ( isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)
2100 || (allow_pack && *s == ':' && s[1] == ':') )
2101 {
2102 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, allow_pack, &len);
2103 if (check_keyword) {
2104 char *s2 = PL_tokenbuf;
2105 STRLEN len2 = len;
2106 if (allow_pack && memBEGINPs(s2, len, "CORE::")) {
2107 s2 += sizeof("CORE::") - 1;
2108 len2 -= sizeof("CORE::") - 1;
2109 }
2110 if (keyword(s2, len2, 0))
2111 return start;
2112 }
2113 if (token == METHOD) {
2114 s = skipspace(s);
2115 if (*s == '(')
2116 PL_expect = XTERM;
2117 else {
2118 PL_expect = XOPERATOR;
2119 }
2120 }
2121 NEXTVAL_NEXTTOKE.opval
2122 = newSVOP(OP_CONST,0,
2123 S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
2124 NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
2125 force_next(token);
2126 }
2127 return s;
2128 }
2129
2130 /*
2131 * S_force_ident
2132 * Called when the lexer wants $foo *foo &foo etc, but the program
2133 * text only contains the "foo" portion. The first argument is a pointer
2134 * to the "foo", and the second argument is the type symbol to prefix.
2135 * Forces the next token to be a "BAREWORD".
2136 * Creates the symbol if it didn't already exist (via gv_fetchpv()).
2137 */
2138
2139 STATIC void
S_force_ident(pTHX_ const char * s,int kind)2140 S_force_ident(pTHX_ const char *s, int kind)
2141 {
2142 PERL_ARGS_ASSERT_FORCE_IDENT;
2143
2144 if (s[0]) {
2145 const STRLEN len = s[1] ? strlen(s) : 1; /* s = "\"" see yylex */
2146 OP* const o = newSVOP(OP_CONST, 0, newSVpvn_flags(s, len,
2147 UTF ? SVf_UTF8 : 0));
2148 NEXTVAL_NEXTTOKE.opval = o;
2149 force_next(BAREWORD);
2150 if (kind) {
2151 o->op_private = OPpCONST_ENTERED;
2152 /* XXX see note in pp_entereval() for why we forgo typo
2153 warnings if the symbol must be introduced in an eval.
2154 GSAR 96-10-12 */
2155 gv_fetchpvn_flags(s, len,
2156 (PL_in_eval ? GV_ADDMULTI
2157 : GV_ADD) | ( UTF ? SVf_UTF8 : 0 ),
2158 kind == '$' ? SVt_PV :
2159 kind == '@' ? SVt_PVAV :
2160 kind == '%' ? SVt_PVHV :
2161 SVt_PVGV
2162 );
2163 }
2164 }
2165 }
2166
2167 static void
S_force_ident_maybe_lex(pTHX_ char pit)2168 S_force_ident_maybe_lex(pTHX_ char pit)
2169 {
2170 NEXTVAL_NEXTTOKE.ival = pit;
2171 force_next('p');
2172 }
2173
2174 NV
Perl_str_to_version(pTHX_ SV * sv)2175 Perl_str_to_version(pTHX_ SV *sv)
2176 {
2177 NV retval = 0.0;
2178 NV nshift = 1.0;
2179 STRLEN len;
2180 const char *start = SvPV_const(sv,len);
2181 const char * const end = start + len;
2182 const bool utf = cBOOL(SvUTF8(sv));
2183
2184 PERL_ARGS_ASSERT_STR_TO_VERSION;
2185
2186 while (start < end) {
2187 STRLEN skip;
2188 UV n;
2189 if (utf)
2190 n = utf8n_to_uvchr((U8*)start, len, &skip, 0);
2191 else {
2192 n = *(U8*)start;
2193 skip = 1;
2194 }
2195 retval += ((NV)n)/nshift;
2196 start += skip;
2197 nshift *= 1000;
2198 }
2199 return retval;
2200 }
2201
2202 /*
2203 * S_force_version
2204 * Forces the next token to be a version number.
2205 * If the next token appears to be an invalid version number, (e.g. "v2b"),
2206 * and if "guessing" is TRUE, then no new token is created (and the caller
2207 * must use an alternative parsing method).
2208 */
2209
2210 STATIC char *
S_force_version(pTHX_ char * s,int guessing)2211 S_force_version(pTHX_ char *s, int guessing)
2212 {
2213 OP *version = NULL;
2214 char *d;
2215
2216 PERL_ARGS_ASSERT_FORCE_VERSION;
2217
2218 s = skipspace(s);
2219
2220 d = s;
2221 if (*d == 'v')
2222 d++;
2223 if (isDIGIT(*d)) {
2224 while (isDIGIT(*d) || *d == '_' || *d == '.')
2225 d++;
2226 if (*d == ';' || isSPACE(*d) || *d == '{' || *d == '}' || !*d) {
2227 SV *ver;
2228 s = scan_num(s, &pl_yylval);
2229 version = pl_yylval.opval;
2230 ver = cSVOPx(version)->op_sv;
2231 if (SvPOK(ver) && !SvNIOK(ver)) {
2232 SvUPGRADE(ver, SVt_PVNV);
2233 SvNV_set(ver, str_to_version(ver));
2234 SvNOK_on(ver); /* hint that it is a version */
2235 }
2236 }
2237 else if (guessing) {
2238 return s;
2239 }
2240 }
2241
2242 /* NOTE: The parser sees the package name and the VERSION swapped */
2243 NEXTVAL_NEXTTOKE.opval = version;
2244 force_next(BAREWORD);
2245
2246 return s;
2247 }
2248
2249 /*
2250 * S_force_strict_version
2251 * Forces the next token to be a version number using strict syntax rules.
2252 */
2253
2254 STATIC char *
S_force_strict_version(pTHX_ char * s)2255 S_force_strict_version(pTHX_ char *s)
2256 {
2257 OP *version = NULL;
2258 const char *errstr = NULL;
2259
2260 PERL_ARGS_ASSERT_FORCE_STRICT_VERSION;
2261
2262 while (isSPACE(*s)) /* leading whitespace */
2263 s++;
2264
2265 if (is_STRICT_VERSION(s,&errstr)) {
2266 SV *ver = newSV(0);
2267 s = (char *)scan_version(s, ver, 0);
2268 version = newSVOP(OP_CONST, 0, ver);
2269 }
2270 else if ((*s != ';' && *s != '{' && *s != '}' )
2271 && (s = skipspace(s), (*s != ';' && *s != '{' && *s != '}' )))
2272 {
2273 PL_bufptr = s;
2274 if (errstr)
2275 yyerror(errstr); /* version required */
2276 return s;
2277 }
2278
2279 /* NOTE: The parser sees the package name and the VERSION swapped */
2280 NEXTVAL_NEXTTOKE.opval = version;
2281 force_next(BAREWORD);
2282
2283 return s;
2284 }
2285
2286 /*
2287 * S_tokeq
2288 * Turns any \\ into \ in a quoted string passed in in 'sv', returning 'sv',
2289 * modified as necessary. However, if HINT_NEW_STRING is on, 'sv' is
2290 * unchanged, and a new SV containing the modified input is returned.
2291 */
2292
2293 STATIC SV *
S_tokeq(pTHX_ SV * sv)2294 S_tokeq(pTHX_ SV *sv)
2295 {
2296 char *s;
2297 char *send;
2298 char *d;
2299 SV *pv = sv;
2300
2301 PERL_ARGS_ASSERT_TOKEQ;
2302
2303 assert (SvPOK(sv));
2304 assert (SvLEN(sv));
2305 assert (!SvIsCOW(sv));
2306 if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1) /* <<'heredoc' */
2307 goto finish;
2308 s = SvPVX(sv);
2309 send = SvEND(sv);
2310 /* This is relying on the SV being "well formed" with a trailing '\0' */
2311 while (s < send && !(*s == '\\' && s[1] == '\\'))
2312 s++;
2313 if (s == send)
2314 goto finish;
2315 d = s;
2316 if ( PL_hints & HINT_NEW_STRING ) {
2317 pv = newSVpvn_flags(SvPVX_const(pv), SvCUR(sv),
2318 SVs_TEMP | SvUTF8(sv));
2319 }
2320 while (s < send) {
2321 if (*s == '\\') {
2322 if (s + 1 < send && (s[1] == '\\'))
2323 s++; /* all that, just for this */
2324 }
2325 *d++ = *s++;
2326 }
2327 *d = '\0';
2328 SvCUR_set(sv, d - SvPVX_const(sv));
2329 finish:
2330 if ( PL_hints & HINT_NEW_STRING )
2331 return new_constant(NULL, 0, "q", sv, pv, "q", 1, NULL);
2332 return sv;
2333 }
2334
2335 /*
2336 * Now come three functions related to double-quote context,
2337 * S_sublex_start, S_sublex_push, and S_sublex_done. They're used when
2338 * converting things like "\u\Lgnat" into ucfirst(lc("gnat")). They
2339 * interact with PL_lex_state, and create fake ( ... ) argument lists
2340 * to handle functions and concatenation.
2341 * For example,
2342 * "foo\lbar"
2343 * is tokenised as
2344 * stringify ( const[foo] concat lcfirst ( const[bar] ) )
2345 */
2346
2347 /*
2348 * S_sublex_start
2349 * Assumes that pl_yylval.ival is the op we're creating (e.g. OP_LCFIRST).
2350 *
2351 * Pattern matching will set PL_lex_op to the pattern-matching op to
2352 * make (we return THING if pl_yylval.ival is OP_NULL, PMFUNC otherwise).
2353 *
2354 * OP_CONST is easy--just make the new op and return.
2355 *
2356 * Everything else becomes a FUNC.
2357 *
2358 * Sets PL_lex_state to LEX_INTERPPUSH unless ival was OP_NULL or we
2359 * had an OP_CONST. This just sets us up for a
2360 * call to S_sublex_push().
2361 */
2362
2363 STATIC I32
S_sublex_start(pTHX)2364 S_sublex_start(pTHX)
2365 {
2366 const I32 op_type = pl_yylval.ival;
2367
2368 if (op_type == OP_NULL) {
2369 pl_yylval.opval = PL_lex_op;
2370 PL_lex_op = NULL;
2371 return THING;
2372 }
2373 if (op_type == OP_CONST) {
2374 SV *sv = PL_lex_stuff;
2375 PL_lex_stuff = NULL;
2376 sv = tokeq(sv);
2377
2378 if (SvTYPE(sv) == SVt_PVIV) {
2379 /* Overloaded constants, nothing fancy: Convert to SVt_PV: */
2380 STRLEN len;
2381 const char * const p = SvPV_const(sv, len);
2382 SV * const nsv = newSVpvn_flags(p, len, SvUTF8(sv));
2383 SvREFCNT_dec(sv);
2384 sv = nsv;
2385 }
2386 pl_yylval.opval = newSVOP(op_type, 0, sv);
2387 return THING;
2388 }
2389
2390 PL_parser->lex_super_state = PL_lex_state;
2391 PL_parser->lex_sub_inwhat = (U16)op_type;
2392 PL_parser->lex_sub_op = PL_lex_op;
2393 PL_parser->sub_no_recover = FALSE;
2394 PL_parser->sub_error_count = PL_error_count;
2395 PL_lex_state = LEX_INTERPPUSH;
2396
2397 PL_expect = XTERM;
2398 if (PL_lex_op) {
2399 pl_yylval.opval = PL_lex_op;
2400 PL_lex_op = NULL;
2401 return PMFUNC;
2402 }
2403 else
2404 return FUNC;
2405 }
2406
2407 /*
2408 * S_sublex_push
2409 * Create a new scope to save the lexing state. The scope will be
2410 * ended in S_sublex_done. Returns a '(', starting the function arguments
2411 * to the uc, lc, etc. found before.
2412 * Sets PL_lex_state to LEX_INTERPCONCAT.
2413 */
2414
2415 STATIC I32
S_sublex_push(pTHX)2416 S_sublex_push(pTHX)
2417 {
2418 LEXSHARED *shared;
2419 const bool is_heredoc = PL_multi_close == '<';
2420 ENTER;
2421
2422 PL_lex_state = PL_parser->lex_super_state;
2423 SAVEI8(PL_lex_dojoin);
2424 SAVEI32(PL_lex_brackets);
2425 SAVEI32(PL_lex_allbrackets);
2426 SAVEI32(PL_lex_formbrack);
2427 SAVEI8(PL_lex_fakeeof);
2428 SAVEI32(PL_lex_casemods);
2429 SAVEI32(PL_lex_starts);
2430 SAVEI8(PL_lex_state);
2431 SAVESPTR(PL_lex_repl);
2432 SAVEVPTR(PL_lex_inpat);
2433 SAVEI16(PL_lex_inwhat);
2434 if (is_heredoc)
2435 {
2436 SAVECOPLINE(PL_curcop);
2437 SAVEI32(PL_multi_end);
2438 SAVEI32(PL_parser->herelines);
2439 PL_parser->herelines = 0;
2440 }
2441 SAVEIV(PL_multi_close);
2442 SAVEPPTR(PL_bufptr);
2443 SAVEPPTR(PL_bufend);
2444 SAVEPPTR(PL_oldbufptr);
2445 SAVEPPTR(PL_oldoldbufptr);
2446 SAVEPPTR(PL_last_lop);
2447 SAVEPPTR(PL_last_uni);
2448 SAVEPPTR(PL_linestart);
2449 SAVESPTR(PL_linestr);
2450 SAVEGENERICPV(PL_lex_brackstack);
2451 SAVEGENERICPV(PL_lex_casestack);
2452 SAVEGENERICPV(PL_parser->lex_shared);
2453 SAVEBOOL(PL_parser->lex_re_reparsing);
2454 SAVEI32(PL_copline);
2455
2456 /* The here-doc parser needs to be able to peek into outer lexing
2457 scopes to find the body of the here-doc. So we put PL_linestr and
2458 PL_bufptr into lex_shared, to ‘share’ those values.
2459 */
2460 PL_parser->lex_shared->ls_linestr = PL_linestr;
2461 PL_parser->lex_shared->ls_bufptr = PL_bufptr;
2462
2463 PL_linestr = PL_lex_stuff;
2464 PL_lex_repl = PL_parser->lex_sub_repl;
2465 PL_lex_stuff = NULL;
2466 PL_parser->lex_sub_repl = NULL;
2467
2468 /* Arrange for PL_lex_stuff to be freed on scope exit, in case it gets
2469 set for an inner quote-like operator and then an error causes scope-
2470 popping. We must not have a PL_lex_stuff value left dangling, as
2471 that breaks assumptions elsewhere. See bug #123617. */
2472 SAVEGENERICSV(PL_lex_stuff);
2473 SAVEGENERICSV(PL_parser->lex_sub_repl);
2474
2475 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart
2476 = SvPVX(PL_linestr);
2477 PL_bufend += SvCUR(PL_linestr);
2478 PL_last_lop = PL_last_uni = NULL;
2479 SAVEFREESV(PL_linestr);
2480 if (PL_lex_repl) SAVEFREESV(PL_lex_repl);
2481
2482 PL_lex_dojoin = FALSE;
2483 PL_lex_brackets = PL_lex_formbrack = 0;
2484 PL_lex_allbrackets = 0;
2485 PL_lex_fakeeof = LEX_FAKEEOF_NEVER;
2486 Newx(PL_lex_brackstack, 120, char);
2487 Newx(PL_lex_casestack, 12, char);
2488 PL_lex_casemods = 0;
2489 *PL_lex_casestack = '\0';
2490 PL_lex_starts = 0;
2491 PL_lex_state = LEX_INTERPCONCAT;
2492 if (is_heredoc)
2493 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
2494 PL_copline = NOLINE;
2495
2496 Newxz(shared, 1, LEXSHARED);
2497 shared->ls_prev = PL_parser->lex_shared;
2498 PL_parser->lex_shared = shared;
2499
2500 PL_lex_inwhat = PL_parser->lex_sub_inwhat;
2501 if (PL_lex_inwhat == OP_TRANSR) PL_lex_inwhat = OP_TRANS;
2502 if (PL_lex_inwhat == OP_MATCH || PL_lex_inwhat == OP_QR || PL_lex_inwhat == OP_SUBST)
2503 PL_lex_inpat = PL_parser->lex_sub_op;
2504 else
2505 PL_lex_inpat = NULL;
2506
2507 PL_parser->lex_re_reparsing = cBOOL(PL_in_eval & EVAL_RE_REPARSING);
2508 PL_in_eval &= ~EVAL_RE_REPARSING;
2509
2510 return '(';
2511 }
2512
2513 /*
2514 * S_sublex_done
2515 * Restores lexer state after a S_sublex_push.
2516 */
2517
2518 STATIC I32
S_sublex_done(pTHX)2519 S_sublex_done(pTHX)
2520 {
2521 if (!PL_lex_starts++) {
2522 SV * const sv = newSVpvs("");
2523 if (SvUTF8(PL_linestr))
2524 SvUTF8_on(sv);
2525 PL_expect = XOPERATOR;
2526 pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
2527 return THING;
2528 }
2529
2530 if (PL_lex_casemods) { /* oops, we've got some unbalanced parens */
2531 PL_lex_state = LEX_INTERPCASEMOD;
2532 return yylex();
2533 }
2534
2535 /* Is there a right-hand side to take care of? (s//RHS/ or tr//RHS/) */
2536 assert(PL_lex_inwhat != OP_TRANSR);
2537 if (PL_lex_repl) {
2538 assert (PL_lex_inwhat == OP_SUBST || PL_lex_inwhat == OP_TRANS);
2539 PL_linestr = PL_lex_repl;
2540 PL_lex_inpat = 0;
2541 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
2542 PL_bufend += SvCUR(PL_linestr);
2543 PL_last_lop = PL_last_uni = NULL;
2544 PL_lex_dojoin = FALSE;
2545 PL_lex_brackets = 0;
2546 PL_lex_allbrackets = 0;
2547 PL_lex_fakeeof = LEX_FAKEEOF_NEVER;
2548 PL_lex_casemods = 0;
2549 *PL_lex_casestack = '\0';
2550 PL_lex_starts = 0;
2551 if (SvEVALED(PL_lex_repl)) {
2552 PL_lex_state = LEX_INTERPNORMAL;
2553 PL_lex_starts++;
2554 /* we don't clear PL_lex_repl here, so that we can check later
2555 whether this is an evalled subst; that means we rely on the
2556 logic to ensure sublex_done() is called again only via the
2557 branch (in yylex()) that clears PL_lex_repl, else we'll loop */
2558 }
2559 else {
2560 PL_lex_state = LEX_INTERPCONCAT;
2561 PL_lex_repl = NULL;
2562 }
2563 if (SvTYPE(PL_linestr) >= SVt_PVNV) {
2564 CopLINE(PL_curcop) +=
2565 ((XPVNV*)SvANY(PL_linestr))->xnv_u.xnv_lines
2566 + PL_parser->herelines;
2567 PL_parser->herelines = 0;
2568 }
2569 return '/';
2570 }
2571 else {
2572 const line_t l = CopLINE(PL_curcop);
2573 LEAVE;
2574 if (PL_parser->sub_error_count != PL_error_count) {
2575 if (PL_parser->sub_no_recover) {
2576 yyquit();
2577 NOT_REACHED;
2578 }
2579 }
2580 if (PL_multi_close == '<')
2581 PL_parser->herelines += l - PL_multi_end;
2582 PL_bufend = SvPVX(PL_linestr);
2583 PL_bufend += SvCUR(PL_linestr);
2584 PL_expect = XOPERATOR;
2585 return ')';
2586 }
2587 }
2588
2589 STATIC SV*
S_get_and_check_backslash_N_name_wrapper(pTHX_ const char * s,const char * const e)2590 S_get_and_check_backslash_N_name_wrapper(pTHX_ const char* s, const char* const e)
2591 {
2592 /* This justs wraps get_and_check_backslash_N_name() to output any error
2593 * message it returns. */
2594
2595 const char * error_msg = NULL;
2596 SV * result;
2597
2598 PERL_ARGS_ASSERT_GET_AND_CHECK_BACKSLASH_N_NAME_WRAPPER;
2599
2600 /* charnames doesn't work well if there have been errors found */
2601 if (PL_error_count > 0) {
2602 return NULL;
2603 }
2604
2605 result = get_and_check_backslash_N_name(s, e, cBOOL(UTF), &error_msg);
2606
2607 if (error_msg) {
2608 yyerror_pv(error_msg, UTF ? SVf_UTF8 : 0);
2609 }
2610
2611 return result;
2612 }
2613
2614 SV*
Perl_get_and_check_backslash_N_name(pTHX_ const char * s,const char * const e,const bool is_utf8,const char ** error_msg)2615 Perl_get_and_check_backslash_N_name(pTHX_ const char* s,
2616 const char* const e,
2617 const bool is_utf8,
2618 const char ** error_msg)
2619 {
2620 /* <s> points to first character of interior of \N{}, <e> to one beyond the
2621 * interior, hence to the "}". Finds what the name resolves to, returning
2622 * an SV* containing it; NULL if no valid one found.
2623 *
2624 * 'is_utf8' is TRUE if we know we want the result to be UTF-8 even if it
2625 * doesn't have to be. */
2626
2627 SV* res;
2628 HV * table;
2629 SV **cvp;
2630 SV *cv;
2631 SV *rv;
2632 HV *stash;
2633 const char* backslash_ptr = s - 3; /* Points to the <\> of \N{... */
2634 dVAR;
2635
2636 PERL_ARGS_ASSERT_GET_AND_CHECK_BACKSLASH_N_NAME;
2637
2638 assert(e >= s);
2639 assert(s > (char *) 3);
2640
2641 res = newSVpvn_flags(s, e - s, (is_utf8) ? SVf_UTF8 : 0);
2642
2643 if (!SvCUR(res)) {
2644 SvREFCNT_dec_NN(res);
2645 /* diag_listed_as: Unknown charname '%s' */
2646 *error_msg = Perl_form(aTHX_ "Unknown charname ''");
2647 return NULL;
2648 }
2649
2650 res = new_constant( NULL, 0, "charnames", res, NULL, backslash_ptr,
2651 /* include the <}> */
2652 e - backslash_ptr + 1, error_msg);
2653 if (! SvPOK(res)) {
2654 SvREFCNT_dec_NN(res);
2655 return NULL;
2656 }
2657
2658 /* See if the charnames handler is the Perl core's, and if so, we can skip
2659 * the validation needed for a user-supplied one, as Perl's does its own
2660 * validation. */
2661 table = GvHV(PL_hintgv); /* ^H */
2662 cvp = hv_fetchs(table, "charnames", FALSE);
2663 if (cvp && (cv = *cvp) && SvROK(cv) && (rv = SvRV(cv),
2664 SvTYPE(rv) == SVt_PVCV) && ((stash = CvSTASH(rv)) != NULL))
2665 {
2666 const char * const name = HvNAME(stash);
2667 if (memEQs(name, HvNAMELEN(stash), "_charnames")) {
2668 return res;
2669 }
2670 }
2671
2672 /* Here, it isn't Perl's charname handler. We can't rely on a
2673 * user-supplied handler to validate the input name. For non-ut8 input,
2674 * look to see that the first character is legal. Then loop through the
2675 * rest checking that each is a continuation */
2676
2677 /* This code makes the reasonable assumption that the only Latin1-range
2678 * characters that begin a character name alias are alphabetic, otherwise
2679 * would have to create a isCHARNAME_BEGIN macro */
2680
2681 if (! is_utf8) {
2682 if (! isALPHAU(*s)) {
2683 goto bad_charname;
2684 }
2685 s++;
2686 while (s < e) {
2687 if (! isCHARNAME_CONT(*s)) {
2688 goto bad_charname;
2689 }
2690 if (*s == ' ' && *(s-1) == ' ') {
2691 goto multi_spaces;
2692 }
2693 s++;
2694 }
2695 }
2696 else {
2697 /* Similarly for utf8. For invariants can check directly; for other
2698 * Latin1, can calculate their code point and check; otherwise use a
2699 * swash */
2700 if (UTF8_IS_INVARIANT(*s)) {
2701 if (! isALPHAU(*s)) {
2702 goto bad_charname;
2703 }
2704 s++;
2705 } else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
2706 if (! isALPHAU(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s+1)))) {
2707 goto bad_charname;
2708 }
2709 s += 2;
2710 }
2711 else {
2712 if (! _invlist_contains_cp(PL_utf8_charname_begin,
2713 utf8_to_uvchr_buf((U8 *) s,
2714 (U8 *) e,
2715 NULL)))
2716 {
2717 goto bad_charname;
2718 }
2719 s += UTF8SKIP(s);
2720 }
2721
2722 while (s < e) {
2723 if (UTF8_IS_INVARIANT(*s)) {
2724 if (! isCHARNAME_CONT(*s)) {
2725 goto bad_charname;
2726 }
2727 if (*s == ' ' && *(s-1) == ' ') {
2728 goto multi_spaces;
2729 }
2730 s++;
2731 }
2732 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
2733 if (! isCHARNAME_CONT(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s+1))))
2734 {
2735 goto bad_charname;
2736 }
2737 s += 2;
2738 }
2739 else {
2740 if (! _invlist_contains_cp(PL_utf8_charname_continue,
2741 utf8_to_uvchr_buf((U8 *) s,
2742 (U8 *) e,
2743 NULL)))
2744 {
2745 goto bad_charname;
2746 }
2747 s += UTF8SKIP(s);
2748 }
2749 }
2750 }
2751 if (*(s-1) == ' ') {
2752 /* diag_listed_as: charnames alias definitions may not contain
2753 trailing white-space; marked by <-- HERE in %s
2754 */
2755 *error_msg = Perl_form(aTHX_
2756 "charnames alias definitions may not contain trailing "
2757 "white-space; marked by <-- HERE in %.*s<-- HERE %.*s",
2758 (int)(s - backslash_ptr + 1), backslash_ptr,
2759 (int)(e - s + 1), s + 1);
2760 return NULL;
2761 }
2762
2763 if (SvUTF8(res)) { /* Don't accept malformed charname value */
2764 const U8* first_bad_char_loc;
2765 STRLEN len;
2766 const char* const str = SvPV_const(res, len);
2767 if (UNLIKELY(! is_utf8_string_loc((U8 *) str, len,
2768 &first_bad_char_loc)))
2769 {
2770 _force_out_malformed_utf8_message(first_bad_char_loc,
2771 (U8 *) PL_parser->bufend,
2772 0,
2773 0 /* 0 means don't die */ );
2774 /* diag_listed_as: Malformed UTF-8 returned by \N{%s}
2775 immediately after '%s' */
2776 *error_msg = Perl_form(aTHX_
2777 "Malformed UTF-8 returned by %.*s immediately after '%.*s'",
2778 (int) (e - backslash_ptr + 1), backslash_ptr,
2779 (int) ((char *) first_bad_char_loc - str), str);
2780 return NULL;
2781 }
2782 }
2783
2784 return res;
2785
2786 bad_charname: {
2787
2788 /* The final %.*s makes sure that should the trailing NUL be missing
2789 * that this print won't run off the end of the string */
2790 /* diag_listed_as: Invalid character in \N{...}; marked by <-- HERE
2791 in \N{%s} */
2792 *error_msg = Perl_form(aTHX_
2793 "Invalid character in \\N{...}; marked by <-- HERE in %.*s<-- HERE %.*s",
2794 (int)(s - backslash_ptr + 1), backslash_ptr,
2795 (int)(e - s + 1), s + 1);
2796 return NULL;
2797 }
2798
2799 multi_spaces:
2800 /* diag_listed_as: charnames alias definitions may not contain a
2801 sequence of multiple spaces; marked by <-- HERE
2802 in %s */
2803 *error_msg = Perl_form(aTHX_
2804 "charnames alias definitions may not contain a sequence of "
2805 "multiple spaces; marked by <-- HERE in %.*s<-- HERE %.*s",
2806 (int)(s - backslash_ptr + 1), backslash_ptr,
2807 (int)(e - s + 1), s + 1);
2808 return NULL;
2809 }
2810
2811 /*
2812 scan_const
2813
2814 Extracts the next constant part of a pattern, double-quoted string,
2815 or transliteration. This is terrifying code.
2816
2817 For example, in parsing the double-quoted string "ab\x63$d", it would
2818 stop at the '$' and return an OP_CONST containing 'abc'.
2819
2820 It looks at PL_lex_inwhat and PL_lex_inpat to find out whether it's
2821 processing a pattern (PL_lex_inpat is true), a transliteration
2822 (PL_lex_inwhat == OP_TRANS is true), or a double-quoted string.
2823
2824 Returns a pointer to the character scanned up to. If this is
2825 advanced from the start pointer supplied (i.e. if anything was
2826 successfully parsed), will leave an OP_CONST for the substring scanned
2827 in pl_yylval. Caller must intuit reason for not parsing further
2828 by looking at the next characters herself.
2829
2830 In patterns:
2831 expand:
2832 \N{FOO} => \N{U+hex_for_character_FOO}
2833 (if FOO expands to multiple characters, expands to \N{U+xx.XX.yy ...})
2834
2835 pass through:
2836 all other \-char, including \N and \N{ apart from \N{ABC}
2837
2838 stops on:
2839 @ and $ where it appears to be a var, but not for $ as tail anchor
2840 \l \L \u \U \Q \E
2841 (?{ or (??{
2842
2843 In transliterations:
2844 characters are VERY literal, except for - not at the start or end
2845 of the string, which indicates a range. However some backslash sequences
2846 are recognized: \r, \n, and the like
2847 \007 \o{}, \x{}, \N{}
2848 If all elements in the transliteration are below 256,
2849 scan_const expands the range to the full set of intermediate
2850 characters. If the range is in utf8, the hyphen is replaced with
2851 a certain range mark which will be handled by pmtrans() in op.c.
2852
2853 In double-quoted strings:
2854 backslashes:
2855 all those recognized in transliterations
2856 deprecated backrefs: \1 (in substitution replacements)
2857 case and quoting: \U \Q \E
2858 stops on @ and $
2859
2860 scan_const does *not* construct ops to handle interpolated strings.
2861 It stops processing as soon as it finds an embedded $ or @ variable
2862 and leaves it to the caller to work out what's going on.
2863
2864 embedded arrays (whether in pattern or not) could be:
2865 @foo, @::foo, @'foo, @{foo}, @$foo, @+, @-.
2866
2867 $ in double-quoted strings must be the symbol of an embedded scalar.
2868
2869 $ in pattern could be $foo or could be tail anchor. Assumption:
2870 it's a tail anchor if $ is the last thing in the string, or if it's
2871 followed by one of "()| \r\n\t"
2872
2873 \1 (backreferences) are turned into $1 in substitutions
2874
2875 The structure of the code is
2876 while (there's a character to process) {
2877 handle transliteration ranges
2878 skip regexp comments /(?#comment)/ and codes /(?{code})/
2879 skip #-initiated comments in //x patterns
2880 check for embedded arrays
2881 check for embedded scalars
2882 if (backslash) {
2883 deprecate \1 in substitution replacements
2884 handle string-changing backslashes \l \U \Q \E, etc.
2885 switch (what was escaped) {
2886 handle \- in a transliteration (becomes a literal -)
2887 if a pattern and not \N{, go treat as regular character
2888 handle \132 (octal characters)
2889 handle \x15 and \x{1234} (hex characters)
2890 handle \N{name} (named characters, also \N{3,5} in a pattern)
2891 handle \cV (control characters)
2892 handle printf-style backslashes (\f, \r, \n, etc)
2893 } (end switch)
2894 continue
2895 } (end if backslash)
2896 handle regular character
2897 } (end while character to read)
2898
2899 */
2900
2901 STATIC char *
S_scan_const(pTHX_ char * start)2902 S_scan_const(pTHX_ char *start)
2903 {
2904 char *send = PL_bufend; /* end of the constant */
2905 SV *sv = newSV(send - start); /* sv for the constant. See note below
2906 on sizing. */
2907 char *s = start; /* start of the constant */
2908 char *d = SvPVX(sv); /* destination for copies */
2909 bool dorange = FALSE; /* are we in a translit range? */
2910 bool didrange = FALSE; /* did we just finish a range? */
2911 bool in_charclass = FALSE; /* within /[...]/ */
2912 bool d_is_utf8 = FALSE; /* Output constant is UTF8 */
2913 bool s_is_utf8 = cBOOL(UTF); /* Is the source string assumed to be
2914 UTF8? But, this can show as true
2915 when the source isn't utf8, as for
2916 example when it is entirely composed
2917 of hex constants */
2918 STRLEN utf8_variant_count = 0; /* When not in UTF-8, this counts the
2919 number of characters found so far
2920 that will expand (into 2 bytes)
2921 should we have to convert to
2922 UTF-8) */
2923 SV *res; /* result from charnames */
2924 STRLEN offset_to_max = 0; /* The offset in the output to where the range
2925 high-end character is temporarily placed */
2926
2927 /* Does something require special handling in tr/// ? This avoids extra
2928 * work in a less likely case. As such, khw didn't feel it was worth
2929 * adding any branches to the more mainline code to handle this, which
2930 * means that this doesn't get set in some circumstances when things like
2931 * \x{100} get expanded out. As a result there needs to be extra testing
2932 * done in the tr code */
2933 bool has_above_latin1 = FALSE;
2934
2935 /* Note on sizing: The scanned constant is placed into sv, which is
2936 * initialized by newSV() assuming one byte of output for every byte of
2937 * input. This routine expects newSV() to allocate an extra byte for a
2938 * trailing NUL, which this routine will append if it gets to the end of
2939 * the input. There may be more bytes of input than output (eg., \N{LATIN
2940 * CAPITAL LETTER A}), or more output than input if the constant ends up
2941 * recoded to utf8, but each time a construct is found that might increase
2942 * the needed size, SvGROW() is called. Its size parameter each time is
2943 * based on the best guess estimate at the time, namely the length used so
2944 * far, plus the length the current construct will occupy, plus room for
2945 * the trailing NUL, plus one byte for every input byte still unscanned */
2946
2947 UV uv = UV_MAX; /* Initialize to weird value to try to catch any uses
2948 before set */
2949 #ifdef EBCDIC
2950 int backslash_N = 0; /* ? was the character from \N{} */
2951 int non_portable_endpoint = 0; /* ? In a range is an endpoint
2952 platform-specific like \x65 */
2953 #endif
2954
2955 PERL_ARGS_ASSERT_SCAN_CONST;
2956
2957 assert(PL_lex_inwhat != OP_TRANSR);
2958 if (PL_lex_inwhat == OP_TRANS && PL_parser->lex_sub_op) {
2959 /* If we are doing a trans and we know we want UTF8 set expectation */
2960 d_is_utf8 = PL_parser->lex_sub_op->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF);
2961 s_is_utf8 = PL_parser->lex_sub_op->op_private & (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
2962 }
2963
2964 /* Protect sv from errors and fatal warnings. */
2965 ENTER_with_name("scan_const");
2966 SAVEFREESV(sv);
2967
2968 /* A bunch of code in the loop below assumes that if s[n] exists and is not
2969 * NUL, then s[n+1] exists. This assertion makes sure that assumption is
2970 * valid */
2971 assert(*send == '\0');
2972
2973 while (s < send
2974 || dorange /* Handle tr/// range at right edge of input */
2975 ) {
2976
2977 /* get transliterations out of the way (they're most literal) */
2978 if (PL_lex_inwhat == OP_TRANS) {
2979
2980 /* But there isn't any special handling necessary unless there is a
2981 * range, so for most cases we just drop down and handle the value
2982 * as any other. There are two exceptions.
2983 *
2984 * 1. A hyphen indicates that we are actually going to have a
2985 * range. In this case, skip the '-', set a flag, then drop
2986 * down to handle what should be the end range value.
2987 * 2. After we've handled that value, the next time through, that
2988 * flag is set and we fix up the range.
2989 *
2990 * Ranges entirely within Latin1 are expanded out entirely, in
2991 * order to make the transliteration a simple table look-up.
2992 * Ranges that extend above Latin1 have to be done differently, so
2993 * there is no advantage to expanding them here, so they are
2994 * stored here as Min, ILLEGAL_UTF8_BYTE, Max. The illegal byte
2995 * signifies a hyphen without any possible ambiguity. On EBCDIC
2996 * machines, if the range is expressed as Unicode, the Latin1
2997 * portion is expanded out even if the range extends above
2998 * Latin1. This is because each code point in it has to be
2999 * processed here individually to get its native translation */
3000
3001 if (! dorange) {
3002
3003 /* Here, we don't think we're in a range. If the new character
3004 * is not a hyphen; or if it is a hyphen, but it's too close to
3005 * either edge to indicate a range, or if we haven't output any
3006 * characters yet then it's a regular character. */
3007 if (*s != '-' || s >= send - 1 || s == start || d == SvPVX(sv)) {
3008
3009 /* A regular character. Process like any other, but first
3010 * clear any flags */
3011 didrange = FALSE;
3012 dorange = FALSE;
3013 #ifdef EBCDIC
3014 non_portable_endpoint = 0;
3015 backslash_N = 0;
3016 #endif
3017 /* The tests here for being above Latin1 and similar ones
3018 * in the following 'else' suffice to find all such
3019 * occurences in the constant, except those added by a
3020 * backslash escape sequence, like \x{100}. Mostly, those
3021 * set 'has_above_latin1' as appropriate */
3022 if (s_is_utf8 && UTF8_IS_ABOVE_LATIN1(*s)) {
3023 has_above_latin1 = TRUE;
3024 }
3025
3026 /* Drops down to generic code to process current byte */
3027 }
3028 else { /* Is a '-' in the context where it means a range */
3029 if (didrange) { /* Something like y/A-C-Z// */
3030 Perl_croak(aTHX_ "Ambiguous range in transliteration"
3031 " operator");
3032 }
3033
3034 dorange = TRUE;
3035
3036 s++; /* Skip past the hyphen */
3037
3038 /* d now points to where the end-range character will be
3039 * placed. Save it so won't have to go finding it later,
3040 * and drop down to get that character. (Actually we
3041 * instead save the offset, to handle the case where a
3042 * realloc in the meantime could change the actual
3043 * pointer). We'll finish processing the range the next
3044 * time through the loop */
3045 offset_to_max = d - SvPVX_const(sv);
3046
3047 if (s_is_utf8 && UTF8_IS_ABOVE_LATIN1(*s)) {
3048 has_above_latin1 = TRUE;
3049 }
3050
3051 /* Drops down to generic code to process current byte */
3052 }
3053 } /* End of not a range */
3054 else {
3055 /* Here we have parsed a range. Now must handle it. At this
3056 * point:
3057 * 'sv' is a SV* that contains the output string we are
3058 * constructing. The final two characters in that string
3059 * are the range start and range end, in order.
3060 * 'd' points to just beyond the range end in the 'sv' string,
3061 * where we would next place something
3062 * 'offset_to_max' is the offset in 'sv' at which the character
3063 * (the range's maximum end point) before 'd' begins.
3064 */
3065 char * max_ptr = SvPVX(sv) + offset_to_max;
3066 char * min_ptr;
3067 IV range_min;
3068 IV range_max; /* last character in range */
3069 STRLEN grow;
3070 Size_t offset_to_min = 0;
3071 Size_t extras = 0;
3072 #ifdef EBCDIC
3073 bool convert_unicode;
3074 IV real_range_max = 0;
3075 #endif
3076 /* Get the code point values of the range ends. */
3077 if (d_is_utf8) {
3078 /* We know the utf8 is valid, because we just constructed
3079 * it ourselves in previous loop iterations */
3080 min_ptr = (char*) utf8_hop( (U8*) max_ptr, -1);
3081 range_min = valid_utf8_to_uvchr( (U8*) min_ptr, NULL);
3082 range_max = valid_utf8_to_uvchr( (U8*) max_ptr, NULL);
3083
3084 /* This compensates for not all code setting
3085 * 'has_above_latin1', so that we don't skip stuff that
3086 * should be executed */
3087 if (range_max > 255) {
3088 has_above_latin1 = TRUE;
3089 }
3090 }
3091 else {
3092 min_ptr = max_ptr - 1;
3093 range_min = * (U8*) min_ptr;
3094 range_max = * (U8*) max_ptr;
3095 }
3096
3097 /* If the range is just a single code point, like tr/a-a/.../,
3098 * that code point is already in the output, twice. We can
3099 * just back up over the second instance and avoid all the rest
3100 * of the work. But if it is a variant character, it's been
3101 * counted twice, so decrement. (This unlikely scenario is
3102 * special cased, like the one for a range of 2 code points
3103 * below, only because the main-line code below needs a range
3104 * of 3 or more to work without special casing. Might as well
3105 * get it out of the way now.) */
3106 if (UNLIKELY(range_max == range_min)) {
3107 d = max_ptr;
3108 if (! d_is_utf8 && ! UVCHR_IS_INVARIANT(range_max)) {
3109 utf8_variant_count--;
3110 }
3111 goto range_done;
3112 }
3113
3114 #ifdef EBCDIC
3115 /* On EBCDIC platforms, we may have to deal with portable
3116 * ranges. These happen if at least one range endpoint is a
3117 * Unicode value (\N{...}), or if the range is a subset of
3118 * [A-Z] or [a-z], and both ends are literal characters,
3119 * like 'A', and not like \x{C1} */
3120 convert_unicode =
3121 cBOOL(backslash_N) /* \N{} forces Unicode,
3122 hence portable range */
3123 || ( ! non_portable_endpoint
3124 && (( isLOWER_A(range_min) && isLOWER_A(range_max))
3125 || (isUPPER_A(range_min) && isUPPER_A(range_max))));
3126 if (convert_unicode) {
3127
3128 /* Special handling is needed for these portable ranges.
3129 * They are defined to be in Unicode terms, which includes
3130 * all the Unicode code points between the end points.
3131 * Convert to Unicode to get the Unicode range. Later we
3132 * will convert each code point in the range back to
3133 * native. */
3134 range_min = NATIVE_TO_UNI(range_min);
3135 range_max = NATIVE_TO_UNI(range_max);
3136 }
3137 #endif
3138
3139 if (range_min > range_max) {
3140 #ifdef EBCDIC
3141 if (convert_unicode) {
3142 /* Need to convert back to native for meaningful
3143 * messages for this platform */
3144 range_min = UNI_TO_NATIVE(range_min);
3145 range_max = UNI_TO_NATIVE(range_max);
3146 }
3147 #endif
3148 /* Use the characters themselves for the error message if
3149 * ASCII printables; otherwise some visible representation
3150 * of them */
3151 if (isPRINT_A(range_min) && isPRINT_A(range_max)) {
3152 Perl_croak(aTHX_
3153 "Invalid range \"%c-%c\" in transliteration operator",
3154 (char)range_min, (char)range_max);
3155 }
3156 #ifdef EBCDIC
3157 else if (convert_unicode) {
3158 /* diag_listed_as: Invalid range "%s" in transliteration operator */
3159 Perl_croak(aTHX_
3160 "Invalid range \"\\N{U+%04" UVXf "}-\\N{U+%04"
3161 UVXf "}\" in transliteration operator",
3162 range_min, range_max);
3163 }
3164 #endif
3165 else {
3166 /* diag_listed_as: Invalid range "%s" in transliteration operator */
3167 Perl_croak(aTHX_
3168 "Invalid range \"\\x{%04" UVXf "}-\\x{%04" UVXf "}\""
3169 " in transliteration operator",
3170 range_min, range_max);
3171 }
3172 }
3173
3174 /* If the range is exactly two code points long, they are
3175 * already both in the output */
3176 if (UNLIKELY(range_min + 1 == range_max)) {
3177 goto range_done;
3178 }
3179
3180 /* Here the range contains at least 3 code points */
3181
3182 if (d_is_utf8) {
3183
3184 /* If everything in the transliteration is below 256, we
3185 * can avoid special handling later. A translation table
3186 * for each of those bytes is created by op.c. So we
3187 * expand out all ranges to their constituent code points.
3188 * But if we've encountered something above 255, the
3189 * expanding won't help, so skip doing that. But if it's
3190 * EBCDIC, we may have to look at each character below 256
3191 * if we have to convert to/from Unicode values */
3192 if ( has_above_latin1
3193 #ifdef EBCDIC
3194 && (range_min > 255 || ! convert_unicode)
3195 #endif
3196 ) {
3197 const STRLEN off = d - SvPVX(sv);
3198 const STRLEN extra = 1 + (send - s) + 1;
3199 char *e;
3200
3201 /* Move the high character one byte to the right; then
3202 * insert between it and the range begin, an illegal
3203 * byte which serves to indicate this is a range (using
3204 * a '-' would be ambiguous). */
3205
3206 if (off + extra > SvLEN(sv)) {
3207 d = off + SvGROW(sv, off + extra);
3208 max_ptr = d - off + offset_to_max;
3209 }
3210
3211 e = d++;
3212 while (e-- > max_ptr) {
3213 *(e + 1) = *e;
3214 }
3215 *(e + 1) = (char) ILLEGAL_UTF8_BYTE;
3216 goto range_done;
3217 }
3218
3219 /* Here, we're going to expand out the range. For EBCDIC
3220 * the range can extend above 255 (not so in ASCII), so
3221 * for EBCDIC, split it into the parts above and below
3222 * 255/256 */
3223 #ifdef EBCDIC
3224 if (range_max > 255) {
3225 real_range_max = range_max;
3226 range_max = 255;
3227 }
3228 #endif
3229 }
3230
3231 /* Here we need to expand out the string to contain each
3232 * character in the range. Grow the output to handle this.
3233 * For non-UTF8, we need a byte for each code point in the
3234 * range, minus the three that we've already allocated for: the
3235 * hyphen, the min, and the max. For UTF-8, we need this
3236 * plus an extra byte for each code point that occupies two
3237 * bytes (is variant) when in UTF-8 (except we've already
3238 * allocated for the end points, including if they are
3239 * variants). For ASCII platforms and Unicode ranges on EBCDIC
3240 * platforms, it's easy to calculate a precise number. To
3241 * start, we count the variants in the range, which we need
3242 * elsewhere in this function anyway. (For the case where it
3243 * isn't easy to calculate, 'extras' has been initialized to 0,
3244 * and the calculation is done in a loop further down.) */
3245 #ifdef EBCDIC
3246 if (convert_unicode)
3247 #endif
3248 {
3249 /* This is executed unconditionally on ASCII, and for
3250 * Unicode ranges on EBCDIC. Under these conditions, all
3251 * code points above a certain value are variant; and none
3252 * under that value are. We just need to find out how much
3253 * of the range is above that value. We don't count the
3254 * end points here, as they will already have been counted
3255 * as they were parsed. */
3256 if (range_min >= UTF_CONTINUATION_MARK) {
3257
3258 /* The whole range is made up of variants */
3259 extras = (range_max - 1) - (range_min + 1) + 1;
3260 }
3261 else if (range_max >= UTF_CONTINUATION_MARK) {
3262
3263 /* Only the higher portion of the range is variants */
3264 extras = (range_max - 1) - UTF_CONTINUATION_MARK + 1;
3265 }
3266
3267 utf8_variant_count += extras;
3268 }
3269
3270 /* The base growth is the number of code points in the range,
3271 * not including the endpoints, which have already been sized
3272 * for (and output). We don't subtract for the hyphen, as it
3273 * has been parsed but not output, and the SvGROW below is
3274 * based only on what's been output plus what's left to parse.
3275 * */
3276 grow = (range_max - 1) - (range_min + 1) + 1;
3277
3278 if (d_is_utf8) {
3279 #ifdef EBCDIC
3280 /* In some cases in EBCDIC, we haven't yet calculated a
3281 * precise amount needed for the UTF-8 variants. Just
3282 * assume the worst case, that everything will expand by a
3283 * byte */
3284 if (! convert_unicode) {
3285 grow *= 2;
3286 }
3287 else
3288 #endif
3289 {
3290 /* Otherwise we know exactly how many variants there
3291 * are in the range. */
3292 grow += extras;
3293 }
3294 }
3295
3296 /* Grow, but position the output to overwrite the range min end
3297 * point, because in some cases we overwrite that */
3298 SvCUR_set(sv, d - SvPVX_const(sv));
3299 offset_to_min = min_ptr - SvPVX_const(sv);
3300
3301 /* See Note on sizing above. */
3302 d = offset_to_min + SvGROW(sv, SvCUR(sv)
3303 + (send - s)
3304 + grow
3305 + 1 /* Trailing NUL */ );
3306
3307 /* Now, we can expand out the range. */
3308 #ifdef EBCDIC
3309 if (convert_unicode) {
3310 SSize_t i;
3311
3312 /* Recall that the min and max are now in Unicode terms, so
3313 * we have to convert each character to its native
3314 * equivalent */
3315 if (d_is_utf8) {
3316 for (i = range_min; i <= range_max; i++) {
3317 append_utf8_from_native_byte(
3318 LATIN1_TO_NATIVE((U8) i),
3319 (U8 **) &d);
3320 }
3321 }
3322 else {
3323 for (i = range_min; i <= range_max; i++) {
3324 *d++ = (char)LATIN1_TO_NATIVE((U8) i);
3325 }
3326 }
3327 }
3328 else
3329 #endif
3330 /* Always gets run for ASCII, and sometimes for EBCDIC. */
3331 {
3332 /* Here, no conversions are necessary, which means that the
3333 * first character in the range is already in 'd' and
3334 * valid, so we can skip overwriting it */
3335 if (d_is_utf8) {
3336 SSize_t i;
3337 d += UTF8SKIP(d);
3338 for (i = range_min + 1; i <= range_max; i++) {
3339 append_utf8_from_native_byte((U8) i, (U8 **) &d);
3340 }
3341 }
3342 else {
3343 SSize_t i;
3344 d++;
3345 assert(range_min + 1 <= range_max);
3346 for (i = range_min + 1; i < range_max; i++) {
3347 #ifdef EBCDIC
3348 /* In this case on EBCDIC, we haven't calculated
3349 * the variants. Do it here, as we go along */
3350 if (! UVCHR_IS_INVARIANT(i)) {
3351 utf8_variant_count++;
3352 }
3353 #endif
3354 *d++ = (char)i;
3355 }
3356
3357 /* The range_max is done outside the loop so as to
3358 * avoid having to special case not incrementing
3359 * 'utf8_variant_count' on EBCDIC (it's already been
3360 * counted when originally parsed) */
3361 *d++ = (char) range_max;
3362 }
3363 }
3364
3365 #ifdef EBCDIC
3366 /* If the original range extended above 255, add in that
3367 * portion. */
3368 if (real_range_max) {
3369 *d++ = (char) UTF8_TWO_BYTE_HI(0x100);
3370 *d++ = (char) UTF8_TWO_BYTE_LO(0x100);
3371 if (real_range_max > 0x100) {
3372 if (real_range_max > 0x101) {
3373 *d++ = (char) ILLEGAL_UTF8_BYTE;
3374 }
3375 d = (char*)uvchr_to_utf8((U8*)d, real_range_max);
3376 }
3377 }
3378 #endif
3379
3380 range_done:
3381 /* mark the range as done, and continue */
3382 didrange = TRUE;
3383 dorange = FALSE;
3384 #ifdef EBCDIC
3385 non_portable_endpoint = 0;
3386 backslash_N = 0;
3387 #endif
3388 continue;
3389 } /* End of is a range */
3390 } /* End of transliteration. Joins main code after these else's */
3391 else if (*s == '[' && PL_lex_inpat && !in_charclass) {
3392 char *s1 = s-1;
3393 int esc = 0;
3394 while (s1 >= start && *s1-- == '\\')
3395 esc = !esc;
3396 if (!esc)
3397 in_charclass = TRUE;
3398 }
3399 else if (*s == ']' && PL_lex_inpat && in_charclass) {
3400 char *s1 = s-1;
3401 int esc = 0;
3402 while (s1 >= start && *s1-- == '\\')
3403 esc = !esc;
3404 if (!esc)
3405 in_charclass = FALSE;
3406 }
3407 /* skip for regexp comments /(?#comment)/, except for the last
3408 * char, which will be done separately. Stop on (?{..}) and
3409 * friends */
3410 else if (*s == '(' && PL_lex_inpat && s[1] == '?' && !in_charclass) {
3411 if (s[2] == '#') {
3412 if (s_is_utf8) {
3413 PERL_UINT_FAST8_T len = UTF8SKIP(s);
3414
3415 while (s + len < send && *s != ')') {
3416 Copy(s, d, len, U8);
3417 d += len;
3418 s += len;
3419 len = UTF8_SAFE_SKIP(s, send);
3420 }
3421 }
3422 else while (s+1 < send && *s != ')') {
3423 *d++ = *s++;
3424 }
3425 }
3426 else if (!PL_lex_casemods
3427 && ( s[2] == '{' /* This should match regcomp.c */
3428 || (s[2] == '?' && s[3] == '{')))
3429 {
3430 break;
3431 }
3432 }
3433 /* likewise skip #-initiated comments in //x patterns */
3434 else if (*s == '#'
3435 && PL_lex_inpat
3436 && !in_charclass
3437 && ((PMOP*)PL_lex_inpat)->op_pmflags & RXf_PMf_EXTENDED)
3438 {
3439 while (s < send && *s != '\n')
3440 *d++ = *s++;
3441 }
3442 /* no further processing of single-quoted regex */
3443 else if (PL_lex_inpat && SvIVX(PL_linestr) == '\'')
3444 goto default_action;
3445
3446 /* check for embedded arrays
3447 * (@foo, @::foo, @'foo, @{foo}, @$foo, @+, @-)
3448 */
3449 else if (*s == '@' && s[1]) {
3450 if (UTF
3451 ? isIDFIRST_utf8_safe(s+1, send)
3452 : isWORDCHAR_A(s[1]))
3453 {
3454 break;
3455 }
3456 if (strchr(":'{$", s[1]))
3457 break;
3458 if (!PL_lex_inpat && (s[1] == '+' || s[1] == '-'))
3459 break; /* in regexp, neither @+ nor @- are interpolated */
3460 }
3461 /* check for embedded scalars. only stop if we're sure it's a
3462 * variable. */
3463 else if (*s == '$') {
3464 if (!PL_lex_inpat) /* not a regexp, so $ must be var */
3465 break;
3466 if (s + 1 < send && !strchr("()| \r\n\t", s[1])) {
3467 if (s[1] == '\\') {
3468 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
3469 "Possible unintended interpolation of $\\ in regex");
3470 }
3471 break; /* in regexp, $ might be tail anchor */
3472 }
3473 }
3474
3475 /* End of else if chain - OP_TRANS rejoin rest */
3476
3477 if (UNLIKELY(s >= send)) {
3478 assert(s == send);
3479 break;
3480 }
3481
3482 /* backslashes */
3483 if (*s == '\\' && s+1 < send) {
3484 char* e; /* Can be used for ending '}', etc. */
3485
3486 s++;
3487
3488 /* warn on \1 - \9 in substitution replacements, but note that \11
3489 * is an octal; and \19 is \1 followed by '9' */
3490 if (PL_lex_inwhat == OP_SUBST
3491 && !PL_lex_inpat
3492 && isDIGIT(*s)
3493 && *s != '0'
3494 && !isDIGIT(s[1]))
3495 {
3496 /* diag_listed_as: \%d better written as $%d */
3497 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "\\%c better written as $%c", *s, *s);
3498 *--s = '$';
3499 break;
3500 }
3501
3502 /* string-change backslash escapes */
3503 if (PL_lex_inwhat != OP_TRANS && *s && strchr("lLuUEQF", *s)) {
3504 --s;
3505 break;
3506 }
3507 /* In a pattern, process \N, but skip any other backslash escapes.
3508 * This is because we don't want to translate an escape sequence
3509 * into a meta symbol and have the regex compiler use the meta
3510 * symbol meaning, e.g. \x{2E} would be confused with a dot. But
3511 * in spite of this, we do have to process \N here while the proper
3512 * charnames handler is in scope. See bugs #56444 and #62056.
3513 *
3514 * There is a complication because \N in a pattern may also stand
3515 * for 'match a non-nl', and not mean a charname, in which case its
3516 * processing should be deferred to the regex compiler. To be a
3517 * charname it must be followed immediately by a '{', and not look
3518 * like \N followed by a curly quantifier, i.e., not something like
3519 * \N{3,}. regcurly returns a boolean indicating if it is a legal
3520 * quantifier */
3521 else if (PL_lex_inpat
3522 && (*s != 'N'
3523 || s[1] != '{'
3524 || regcurly(s + 1)))
3525 {
3526 *d++ = '\\';
3527 goto default_action;
3528 }
3529
3530 switch (*s) {
3531 default:
3532 {
3533 if ((isALPHANUMERIC(*s)))
3534 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
3535 "Unrecognized escape \\%c passed through",
3536 *s);
3537 /* default action is to copy the quoted character */
3538 goto default_action;
3539 }
3540
3541 /* eg. \132 indicates the octal constant 0132 */
3542 case '0': case '1': case '2': case '3':
3543 case '4': case '5': case '6': case '7':
3544 {
3545 I32 flags = PERL_SCAN_SILENT_ILLDIGIT;
3546 STRLEN len = 3;
3547 uv = grok_oct(s, &len, &flags, NULL);
3548 s += len;
3549 if (len < 3 && s < send && isDIGIT(*s)
3550 && ckWARN(WARN_MISC))
3551 {
3552 Perl_warner(aTHX_ packWARN(WARN_MISC),
3553 "%s", form_short_octal_warning(s, len));
3554 }
3555 }
3556 goto NUM_ESCAPE_INSERT;
3557
3558 /* eg. \o{24} indicates the octal constant \024 */
3559 case 'o':
3560 {
3561 const char* error;
3562
3563 bool valid = grok_bslash_o(&s, send,
3564 &uv, &error,
3565 TRUE, /* Output warning */
3566 FALSE, /* Not strict */
3567 TRUE, /* Output warnings for
3568 non-portables */
3569 UTF);
3570 if (! valid) {
3571 yyerror(error);
3572 uv = 0; /* drop through to ensure range ends are set */
3573 }
3574 goto NUM_ESCAPE_INSERT;
3575 }
3576
3577 /* eg. \x24 indicates the hex constant 0x24 */
3578 case 'x':
3579 {
3580 const char* error;
3581
3582 bool valid = grok_bslash_x(&s, send,
3583 &uv, &error,
3584 TRUE, /* Output warning */
3585 FALSE, /* Not strict */
3586 TRUE, /* Output warnings for
3587 non-portables */
3588 UTF);
3589 if (! valid) {
3590 yyerror(error);
3591 uv = 0; /* drop through to ensure range ends are set */
3592 }
3593 }
3594
3595 NUM_ESCAPE_INSERT:
3596 /* Insert oct or hex escaped character. */
3597
3598 /* Here uv is the ordinal of the next character being added */
3599 if (UVCHR_IS_INVARIANT(uv)) {
3600 *d++ = (char) uv;
3601 }
3602 else {
3603 if (!d_is_utf8 && uv > 255) {
3604
3605 /* Here, 'uv' won't fit unless we convert to UTF-8.
3606 * If we've only seen invariants so far, all we have to
3607 * do is turn on the flag */
3608 if (utf8_variant_count == 0) {
3609 SvUTF8_on(sv);
3610 }
3611 else {
3612 SvCUR_set(sv, d - SvPVX_const(sv));
3613 SvPOK_on(sv);
3614 *d = '\0';
3615
3616 sv_utf8_upgrade_flags_grow(
3617 sv,
3618 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3619
3620 /* Since we're having to grow here,
3621 * make sure we have enough room for
3622 * this escape and a NUL, so the
3623 * code immediately below won't have
3624 * to actually grow again */
3625 UVCHR_SKIP(uv)
3626 + (STRLEN)(send - s) + 1);
3627 d = SvPVX(sv) + SvCUR(sv);
3628 }
3629
3630 has_above_latin1 = TRUE;
3631 d_is_utf8 = TRUE;
3632 }
3633
3634 if (! d_is_utf8) {
3635 *d++ = (char)uv;
3636 utf8_variant_count++;
3637 }
3638 else {
3639 /* Usually, there will already be enough room in 'sv'
3640 * since such escapes are likely longer than any UTF-8
3641 * sequence they can end up as. This isn't the case on
3642 * EBCDIC where \x{40000000} contains 12 bytes, and the
3643 * UTF-8 for it contains 14. And, we have to allow for
3644 * a trailing NUL. It probably can't happen on ASCII
3645 * platforms, but be safe. See Note on sizing above. */
3646 const STRLEN needed = d - SvPVX(sv)
3647 + UVCHR_SKIP(uv)
3648 + (send - s)
3649 + 1;
3650 if (UNLIKELY(needed > SvLEN(sv))) {
3651 SvCUR_set(sv, d - SvPVX_const(sv));
3652 d = SvCUR(sv) + SvGROW(sv, needed);
3653 }
3654
3655 d = (char*)uvchr_to_utf8((U8*)d, uv);
3656 if (PL_lex_inwhat == OP_TRANS
3657 && PL_parser->lex_sub_op)
3658 {
3659 PL_parser->lex_sub_op->op_private |=
3660 (PL_lex_repl ? OPpTRANS_FROM_UTF
3661 : OPpTRANS_TO_UTF);
3662 }
3663 }
3664 }
3665 #ifdef EBCDIC
3666 non_portable_endpoint++;
3667 #endif
3668 continue;
3669
3670 case 'N':
3671 /* In a non-pattern \N must be like \N{U+0041}, or it can be a
3672 * named character, like \N{LATIN SMALL LETTER A}, or a named
3673 * sequence, like \N{LATIN CAPITAL LETTER A WITH MACRON AND
3674 * GRAVE} (except y/// can't handle the latter, croaking). For
3675 * convenience all three forms are referred to as "named
3676 * characters" below.
3677 *
3678 * For patterns, \N also can mean to match a non-newline. Code
3679 * before this 'switch' statement should already have handled
3680 * this situation, and hence this code only has to deal with
3681 * the named character cases.
3682 *
3683 * For non-patterns, the named characters are converted to
3684 * their string equivalents. In patterns, named characters are
3685 * not converted to their ultimate forms for the same reasons
3686 * that other escapes aren't (mainly that the ultimate
3687 * character could be considered a meta-symbol by the regex
3688 * compiler). Instead, they are converted to the \N{U+...}
3689 * form to get the value from the charnames that is in effect
3690 * right now, while preserving the fact that it was a named
3691 * character, so that the regex compiler knows this.
3692 *
3693 * The structure of this section of code (besides checking for
3694 * errors and upgrading to utf8) is:
3695 * If the named character is of the form \N{U+...}, pass it
3696 * through if a pattern; otherwise convert the code point
3697 * to utf8
3698 * Otherwise must be some \N{NAME}: convert to
3699 * \N{U+c1.c2...} if a pattern; otherwise convert to utf8
3700 *
3701 * Transliteration is an exception. The conversion to utf8 is
3702 * only done if the code point requires it to be representable.
3703 *
3704 * Here, 's' points to the 'N'; the test below is guaranteed to
3705 * succeed if we are being called on a pattern, as we already
3706 * know from a test above that the next character is a '{'. A
3707 * non-pattern \N must mean 'named character', which requires
3708 * braces */
3709 s++;
3710 if (*s != '{') {
3711 yyerror("Missing braces on \\N{}");
3712 *d++ = '\0';
3713 continue;
3714 }
3715 s++;
3716
3717 /* If there is no matching '}', it is an error. */
3718 if (! (e = (char *) memchr(s, '}', send - s))) {
3719 if (! PL_lex_inpat) {
3720 yyerror("Missing right brace on \\N{}");
3721 } else {
3722 yyerror("Missing right brace on \\N{} or unescaped left brace after \\N");
3723 }
3724 yyquit(); /* Have exhausted the input. */
3725 }
3726
3727 /* Here it looks like a named character */
3728
3729 if (*s == 'U' && s[1] == '+') { /* \N{U+...} */
3730 s += 2; /* Skip to next char after the 'U+' */
3731 if (PL_lex_inpat) {
3732
3733 /* In patterns, we can have \N{U+xxxx.yyyy.zzzz...} */
3734 /* Check the syntax. */
3735 const char *orig_s;
3736 orig_s = s - 5;
3737 if (!isXDIGIT(*s)) {
3738 bad_NU:
3739 yyerror(
3740 "Invalid hexadecimal number in \\N{U+...}"
3741 );
3742 s = e + 1;
3743 *d++ = '\0';
3744 continue;
3745 }
3746 while (++s < e) {
3747 if (isXDIGIT(*s))
3748 continue;
3749 else if ((*s == '.' || *s == '_')
3750 && isXDIGIT(s[1]))
3751 continue;
3752 goto bad_NU;
3753 }
3754
3755 /* Pass everything through unchanged.
3756 * +1 is for the '}' */
3757 Copy(orig_s, d, e - orig_s + 1, char);
3758 d += e - orig_s + 1;
3759 }
3760 else { /* Not a pattern: convert the hex to string */
3761 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
3762 | PERL_SCAN_SILENT_ILLDIGIT
3763 | PERL_SCAN_DISALLOW_PREFIX;
3764 STRLEN len = e - s;
3765 uv = grok_hex(s, &len, &flags, NULL);
3766 if (len == 0 || (len != (STRLEN)(e - s)))
3767 goto bad_NU;
3768
3769 /* For non-tr///, if the destination is not in utf8,
3770 * unconditionally recode it to be so. This is
3771 * because \N{} implies Unicode semantics, and scalars
3772 * have to be in utf8 to guarantee those semantics.
3773 * tr/// doesn't care about Unicode rules, so no need
3774 * there to upgrade to UTF-8 for small enough code
3775 * points */
3776 if (! d_is_utf8 && ( uv > 0xFF
3777 || PL_lex_inwhat != OP_TRANS))
3778 {
3779 /* See Note on sizing above. */
3780 const STRLEN extra = OFFUNISKIP(uv) + (send - e) + 1;
3781
3782 SvCUR_set(sv, d - SvPVX_const(sv));
3783 SvPOK_on(sv);
3784 *d = '\0';
3785
3786 if (utf8_variant_count == 0) {
3787 SvUTF8_on(sv);
3788 d = SvCUR(sv) + SvGROW(sv, SvCUR(sv) + extra);
3789 }
3790 else {
3791 sv_utf8_upgrade_flags_grow(
3792 sv,
3793 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3794 extra);
3795 d = SvPVX(sv) + SvCUR(sv);
3796 }
3797
3798 d_is_utf8 = TRUE;
3799 has_above_latin1 = TRUE;
3800 }
3801
3802 /* Add the (Unicode) code point to the output. */
3803 if (! d_is_utf8 || OFFUNI_IS_INVARIANT(uv)) {
3804 *d++ = (char) LATIN1_TO_NATIVE(uv);
3805 }
3806 else {
3807 d = (char*) uvoffuni_to_utf8_flags((U8*)d, uv, 0);
3808 }
3809 }
3810 }
3811 else /* Here is \N{NAME} but not \N{U+...}. */
3812 if (! (res = get_and_check_backslash_N_name_wrapper(s, e)))
3813 { /* Failed. We should die eventually, but for now use a NUL
3814 to keep parsing */
3815 *d++ = '\0';
3816 }
3817 else { /* Successfully evaluated the name */
3818 STRLEN len;
3819 const char *str = SvPV_const(res, len);
3820 if (PL_lex_inpat) {
3821
3822 if (! len) { /* The name resolved to an empty string */
3823 const char empty_N[] = "\\N{_}";
3824 Copy(empty_N, d, sizeof(empty_N) - 1, char);
3825 d += sizeof(empty_N) - 1;
3826 }
3827 else {
3828 /* In order to not lose information for the regex
3829 * compiler, pass the result in the specially made
3830 * syntax: \N{U+c1.c2.c3...}, where c1 etc. are
3831 * the code points in hex of each character
3832 * returned by charnames */
3833
3834 const char *str_end = str + len;
3835 const STRLEN off = d - SvPVX_const(sv);
3836
3837 if (! SvUTF8(res)) {
3838 /* For the non-UTF-8 case, we can determine the
3839 * exact length needed without having to parse
3840 * through the string. Each character takes up
3841 * 2 hex digits plus either a trailing dot or
3842 * the "}" */
3843 const char initial_text[] = "\\N{U+";
3844 const STRLEN initial_len = sizeof(initial_text)
3845 - 1;
3846 d = off + SvGROW(sv, off
3847 + 3 * len
3848
3849 /* +1 for trailing NUL */
3850 + initial_len + 1
3851
3852 + (STRLEN)(send - e));
3853 Copy(initial_text, d, initial_len, char);
3854 d += initial_len;
3855 while (str < str_end) {
3856 char hex_string[4];
3857 int len =
3858 my_snprintf(hex_string,
3859 sizeof(hex_string),
3860 "%02X.",
3861
3862 /* The regex compiler is
3863 * expecting Unicode, not
3864 * native */
3865 NATIVE_TO_LATIN1(*str));
3866 PERL_MY_SNPRINTF_POST_GUARD(len,
3867 sizeof(hex_string));
3868 Copy(hex_string, d, 3, char);
3869 d += 3;
3870 str++;
3871 }
3872 d--; /* Below, we will overwrite the final
3873 dot with a right brace */
3874 }
3875 else {
3876 STRLEN char_length; /* cur char's byte length */
3877
3878 /* and the number of bytes after this is
3879 * translated into hex digits */
3880 STRLEN output_length;
3881
3882 /* 2 hex per byte; 2 chars for '\N'; 2 chars
3883 * for max('U+', '.'); and 1 for NUL */
3884 char hex_string[2 * UTF8_MAXBYTES + 5];
3885
3886 /* Get the first character of the result. */
3887 U32 uv = utf8n_to_uvchr((U8 *) str,
3888 len,
3889 &char_length,
3890 UTF8_ALLOW_ANYUV);
3891 /* Convert first code point to Unicode hex,
3892 * including the boiler plate before it. */
3893 output_length =
3894 my_snprintf(hex_string, sizeof(hex_string),
3895 "\\N{U+%X",
3896 (unsigned int) NATIVE_TO_UNI(uv));
3897
3898 /* Make sure there is enough space to hold it */
3899 d = off + SvGROW(sv, off
3900 + output_length
3901 + (STRLEN)(send - e)
3902 + 2); /* '}' + NUL */
3903 /* And output it */
3904 Copy(hex_string, d, output_length, char);
3905 d += output_length;
3906
3907 /* For each subsequent character, append dot and
3908 * its Unicode code point in hex */
3909 while ((str += char_length) < str_end) {
3910 const STRLEN off = d - SvPVX_const(sv);
3911 U32 uv = utf8n_to_uvchr((U8 *) str,
3912 str_end - str,
3913 &char_length,
3914 UTF8_ALLOW_ANYUV);
3915 output_length =
3916 my_snprintf(hex_string,
3917 sizeof(hex_string),
3918 ".%X",
3919 (unsigned int) NATIVE_TO_UNI(uv));
3920
3921 d = off + SvGROW(sv, off
3922 + output_length
3923 + (STRLEN)(send - e)
3924 + 2); /* '}' + NUL */
3925 Copy(hex_string, d, output_length, char);
3926 d += output_length;
3927 }
3928 }
3929
3930 *d++ = '}'; /* Done. Add the trailing brace */
3931 }
3932 }
3933 else { /* Here, not in a pattern. Convert the name to a
3934 * string. */
3935
3936 if (PL_lex_inwhat == OP_TRANS) {
3937 str = SvPV_const(res, len);
3938 if (len > ((SvUTF8(res))
3939 ? UTF8SKIP(str)
3940 : 1U))
3941 {
3942 yyerror(Perl_form(aTHX_
3943 "%.*s must not be a named sequence"
3944 " in transliteration operator",
3945 /* +1 to include the "}" */
3946 (int) (e + 1 - start), start));
3947 *d++ = '\0';
3948 goto end_backslash_N;
3949 }
3950
3951 if (SvUTF8(res) && UTF8_IS_ABOVE_LATIN1(*str)) {
3952 has_above_latin1 = TRUE;
3953 }
3954
3955 }
3956 else if (! SvUTF8(res)) {
3957 /* Make sure \N{} return is UTF-8. This is because
3958 * \N{} implies Unicode semantics, and scalars have
3959 * to be in utf8 to guarantee those semantics; but
3960 * not needed in tr/// */
3961 sv_utf8_upgrade_flags(res, 0);
3962 str = SvPV_const(res, len);
3963 }
3964
3965 /* Upgrade destination to be utf8 if this new
3966 * component is */
3967 if (! d_is_utf8 && SvUTF8(res)) {
3968 /* See Note on sizing above. */
3969 const STRLEN extra = len + (send - s) + 1;
3970
3971 SvCUR_set(sv, d - SvPVX_const(sv));
3972 SvPOK_on(sv);
3973 *d = '\0';
3974
3975 if (utf8_variant_count == 0) {
3976 SvUTF8_on(sv);
3977 d = SvCUR(sv) + SvGROW(sv, SvCUR(sv) + extra);
3978 }
3979 else {
3980 sv_utf8_upgrade_flags_grow(sv,
3981 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3982 extra);
3983 d = SvPVX(sv) + SvCUR(sv);
3984 }
3985 d_is_utf8 = TRUE;
3986 } else if (len > (STRLEN)(e - s + 4)) { /* I _guess_ 4 is \N{} --jhi */
3987
3988 /* See Note on sizing above. (NOTE: SvCUR() is not
3989 * set correctly here). */
3990 const STRLEN extra = len + (send - e) + 1;
3991 const STRLEN off = d - SvPVX_const(sv);
3992 d = off + SvGROW(sv, off + extra);
3993 }
3994 Copy(str, d, len, char);
3995 d += len;
3996 }
3997
3998 SvREFCNT_dec(res);
3999
4000 } /* End \N{NAME} */
4001
4002 end_backslash_N:
4003 #ifdef EBCDIC
4004 backslash_N++; /* \N{} is defined to be Unicode */
4005 #endif
4006 s = e + 1; /* Point to just after the '}' */
4007 continue;
4008
4009 /* \c is a control character */
4010 case 'c':
4011 s++;
4012 if (s < send) {
4013 *d++ = grok_bslash_c(*s, 1);
4014 }
4015 else {
4016 yyerror("Missing control char name in \\c");
4017 yyquit(); /* Are at end of input, no sense continuing */
4018 }
4019 #ifdef EBCDIC
4020 non_portable_endpoint++;
4021 #endif
4022 break;
4023
4024 /* printf-style backslashes, formfeeds, newlines, etc */
4025 case 'b':
4026 *d++ = '\b';
4027 break;
4028 case 'n':
4029 *d++ = '\n';
4030 break;
4031 case 'r':
4032 *d++ = '\r';
4033 break;
4034 case 'f':
4035 *d++ = '\f';
4036 break;
4037 case 't':
4038 *d++ = '\t';
4039 break;
4040 case 'e':
4041 *d++ = ESC_NATIVE;
4042 break;
4043 case 'a':
4044 *d++ = '\a';
4045 break;
4046 } /* end switch */
4047
4048 s++;
4049 continue;
4050 } /* end if (backslash) */
4051
4052 default_action:
4053 /* Just copy the input to the output, though we may have to convert
4054 * to/from UTF-8.
4055 *
4056 * If the input has the same representation in UTF-8 as not, it will be
4057 * a single byte, and we don't care about UTF8ness; just copy the byte */
4058 if (NATIVE_BYTE_IS_INVARIANT((U8)(*s))) {
4059 *d++ = *s++;
4060 }
4061 else if (! s_is_utf8 && ! d_is_utf8) {
4062 /* If neither source nor output is UTF-8, is also a single byte,
4063 * just copy it; but this byte counts should we later have to
4064 * convert to UTF-8 */
4065 *d++ = *s++;
4066 utf8_variant_count++;
4067 }
4068 else if (s_is_utf8 && d_is_utf8) { /* Both UTF-8, can just copy */
4069 const STRLEN len = UTF8SKIP(s);
4070
4071 /* We expect the source to have already been checked for
4072 * malformedness */
4073 assert(isUTF8_CHAR((U8 *) s, (U8 *) send));
4074
4075 Copy(s, d, len, U8);
4076 d += len;
4077 s += len;
4078 }
4079 else if (s_is_utf8) { /* UTF8ness matters: convert output to utf8 */
4080 STRLEN need = send - s + 1; /* See Note on sizing above. */
4081
4082 SvCUR_set(sv, d - SvPVX_const(sv));
4083 SvPOK_on(sv);
4084 *d = '\0';
4085
4086 if (utf8_variant_count == 0) {
4087 SvUTF8_on(sv);
4088 d = SvCUR(sv) + SvGROW(sv, SvCUR(sv) + need);
4089 }
4090 else {
4091 sv_utf8_upgrade_flags_grow(sv,
4092 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4093 need);
4094 d = SvPVX(sv) + SvCUR(sv);
4095 }
4096 d_is_utf8 = TRUE;
4097 goto default_action; /* Redo, having upgraded so both are UTF-8 */
4098 }
4099 else { /* UTF8ness matters: convert this non-UTF8 source char to
4100 UTF-8 for output. It will occupy 2 bytes */
4101 if (d + 2 >= SvEND(sv)) {
4102 const STRLEN extra = 2 + (send - s - 1) + 1;
4103 const STRLEN off = d - SvPVX_const(sv);
4104 d = off + SvGROW(sv, off + extra);
4105 }
4106 *d++ = UTF8_EIGHT_BIT_HI(*s);
4107 *d++ = UTF8_EIGHT_BIT_LO(*s);
4108 s++;
4109 }
4110 } /* while loop to process each character */
4111
4112 {
4113 const STRLEN off = d - SvPVX(sv);
4114
4115 /* See if room for the terminating NUL */
4116 if (UNLIKELY(off >= SvLEN(sv))) {
4117
4118 #ifndef DEBUGGING
4119
4120 if (off > SvLEN(sv))
4121 #endif
4122 Perl_croak(aTHX_ "panic: constant overflowed allocated space,"
4123 " %" UVuf " >= %" UVuf, (UV)off, (UV)SvLEN(sv));
4124
4125 /* Whew! Here we don't have room for the terminating NUL, but
4126 * everything else so far has fit. It's not too late to grow
4127 * to fit the NUL and continue on. But it is a bug, as the code
4128 * above was supposed to have made room for this, so under
4129 * DEBUGGING builds, we panic anyway. */
4130 d = off + SvGROW(sv, off + 1);
4131 }
4132 }
4133
4134 /* terminate the string and set up the sv */
4135 *d = '\0';
4136 SvCUR_set(sv, d - SvPVX_const(sv));
4137
4138 SvPOK_on(sv);
4139 if (d_is_utf8) {
4140 SvUTF8_on(sv);
4141 if (PL_lex_inwhat == OP_TRANS && PL_parser->lex_sub_op) {
4142 PL_parser->lex_sub_op->op_private |=
4143 (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
4144 }
4145 }
4146
4147 /* shrink the sv if we allocated more than we used */
4148 if (SvCUR(sv) + 5 < SvLEN(sv)) {
4149 SvPV_shrink_to_cur(sv);
4150 }
4151
4152 /* return the substring (via pl_yylval) only if we parsed anything */
4153 if (s > start) {
4154 char *s2 = start;
4155 for (; s2 < s; s2++) {
4156 if (*s2 == '\n')
4157 COPLINE_INC_WITH_HERELINES;
4158 }
4159 SvREFCNT_inc_simple_void_NN(sv);
4160 if ( (PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ))
4161 && ! PL_parser->lex_re_reparsing)
4162 {
4163 const char *const key = PL_lex_inpat ? "qr" : "q";
4164 const STRLEN keylen = PL_lex_inpat ? 2 : 1;
4165 const char *type;
4166 STRLEN typelen;
4167
4168 if (PL_lex_inwhat == OP_TRANS) {
4169 type = "tr";
4170 typelen = 2;
4171 } else if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat) {
4172 type = "s";
4173 typelen = 1;
4174 } else if (PL_lex_inpat && SvIVX(PL_linestr) == '\'') {
4175 type = "q";
4176 typelen = 1;
4177 } else {
4178 type = "qq";
4179 typelen = 2;
4180 }
4181
4182 sv = S_new_constant(aTHX_ start, s - start, key, keylen, sv, NULL,
4183 type, typelen, NULL);
4184 }
4185 pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
4186 }
4187 LEAVE_with_name("scan_const");
4188 return s;
4189 }
4190
4191 /* S_intuit_more
4192 * Returns TRUE if there's more to the expression (e.g., a subscript),
4193 * FALSE otherwise.
4194 *
4195 * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
4196 *
4197 * ->[ and ->{ return TRUE
4198 * ->$* ->$#* ->@* ->@[ ->@{ return TRUE if postderef_qq is enabled
4199 * { and [ outside a pattern are always subscripts, so return TRUE
4200 * if we're outside a pattern and it's not { or [, then return FALSE
4201 * if we're in a pattern and the first char is a {
4202 * {4,5} (any digits around the comma) returns FALSE
4203 * if we're in a pattern and the first char is a [
4204 * [] returns FALSE
4205 * [SOMETHING] has a funky algorithm to decide whether it's a
4206 * character class or not. It has to deal with things like
4207 * /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
4208 * anything else returns TRUE
4209 */
4210
4211 /* This is the one truly awful dwimmer necessary to conflate C and sed. */
4212
4213 STATIC int
S_intuit_more(pTHX_ char * s,char * e)4214 S_intuit_more(pTHX_ char *s, char *e)
4215 {
4216 PERL_ARGS_ASSERT_INTUIT_MORE;
4217
4218 if (PL_lex_brackets)
4219 return TRUE;
4220 if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
4221 return TRUE;
4222 if (*s == '-' && s[1] == '>'
4223 && FEATURE_POSTDEREF_QQ_IS_ENABLED
4224 && ( (s[2] == '$' && (s[3] == '*' || (s[3] == '#' && s[4] == '*')))
4225 ||(s[2] == '@' && strchr("*[{",s[3])) ))
4226 return TRUE;
4227 if (*s != '{' && *s != '[')
4228 return FALSE;
4229 PL_parser->sub_no_recover = TRUE;
4230 if (!PL_lex_inpat)
4231 return TRUE;
4232
4233 /* In a pattern, so maybe we have {n,m}. */
4234 if (*s == '{') {
4235 if (regcurly(s)) {
4236 return FALSE;
4237 }
4238 return TRUE;
4239 }
4240
4241 /* On the other hand, maybe we have a character class */
4242
4243 s++;
4244 if (*s == ']' || *s == '^')
4245 return FALSE;
4246 else {
4247 /* this is terrifying, and it works */
4248 int weight;
4249 char seen[256];
4250 const char * const send = (char *) memchr(s, ']', e - s);
4251 unsigned char un_char, last_un_char;
4252 char tmpbuf[sizeof PL_tokenbuf * 4];
4253
4254 if (!send) /* has to be an expression */
4255 return TRUE;
4256 weight = 2; /* let's weigh the evidence */
4257
4258 if (*s == '$')
4259 weight -= 3;
4260 else if (isDIGIT(*s)) {
4261 if (s[1] != ']') {
4262 if (isDIGIT(s[1]) && s[2] == ']')
4263 weight -= 10;
4264 }
4265 else
4266 weight -= 100;
4267 }
4268 Zero(seen,256,char);
4269 un_char = 255;
4270 for (; s < send; s++) {
4271 last_un_char = un_char;
4272 un_char = (unsigned char)*s;
4273 switch (*s) {
4274 case '@':
4275 case '&':
4276 case '$':
4277 weight -= seen[un_char] * 10;
4278 if (isWORDCHAR_lazy_if_safe(s+1, PL_bufend, UTF)) {
4279 int len;
4280 scan_ident(s, tmpbuf, sizeof tmpbuf, FALSE);
4281 len = (int)strlen(tmpbuf);
4282 if (len > 1 && gv_fetchpvn_flags(tmpbuf, len,
4283 UTF ? SVf_UTF8 : 0, SVt_PV))
4284 weight -= 100;
4285 else
4286 weight -= 10;
4287 }
4288 else if (*s == '$'
4289 && s[1]
4290 && strchr("[#!%*<>()-=",s[1]))
4291 {
4292 if (/*{*/ strchr("])} =",s[2]))
4293 weight -= 10;
4294 else
4295 weight -= 1;
4296 }
4297 break;
4298 case '\\':
4299 un_char = 254;
4300 if (s[1]) {
4301 if (strchr("wds]",s[1]))
4302 weight += 100;
4303 else if (seen[(U8)'\''] || seen[(U8)'"'])
4304 weight += 1;
4305 else if (strchr("rnftbxcav",s[1]))
4306 weight += 40;
4307 else if (isDIGIT(s[1])) {
4308 weight += 40;
4309 while (s[1] && isDIGIT(s[1]))
4310 s++;
4311 }
4312 }
4313 else
4314 weight += 100;
4315 break;
4316 case '-':
4317 if (s[1] == '\\')
4318 weight += 50;
4319 if (strchr("aA01! ",last_un_char))
4320 weight += 30;
4321 if (strchr("zZ79~",s[1]))
4322 weight += 30;
4323 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
4324 weight -= 5; /* cope with negative subscript */
4325 break;
4326 default:
4327 if (!isWORDCHAR(last_un_char)
4328 && !(last_un_char == '$' || last_un_char == '@'
4329 || last_un_char == '&')
4330 && isALPHA(*s) && s[1] && isALPHA(s[1])) {
4331 char *d = s;
4332 while (isALPHA(*s))
4333 s++;
4334 if (keyword(d, s - d, 0))
4335 weight -= 150;
4336 }
4337 if (un_char == last_un_char + 1)
4338 weight += 5;
4339 weight -= seen[un_char];
4340 break;
4341 }
4342 seen[un_char]++;
4343 }
4344 if (weight >= 0) /* probably a character class */
4345 return FALSE;
4346 }
4347
4348 return TRUE;
4349 }
4350
4351 /*
4352 * S_intuit_method
4353 *
4354 * Does all the checking to disambiguate
4355 * foo bar
4356 * between foo(bar) and bar->foo. Returns 0 if not a method, otherwise
4357 * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
4358 *
4359 * First argument is the stuff after the first token, e.g. "bar".
4360 *
4361 * Not a method if foo is a filehandle.
4362 * Not a method if foo is a subroutine prototyped to take a filehandle.
4363 * Not a method if it's really "Foo $bar"
4364 * Method if it's "foo $bar"
4365 * Not a method if it's really "print foo $bar"
4366 * Method if it's really "foo package::" (interpreted as package->foo)
4367 * Not a method if bar is known to be a subroutine ("sub bar; foo bar")
4368 * Not a method if bar is a filehandle or package, but is quoted with
4369 * =>
4370 */
4371
4372 STATIC int
S_intuit_method(pTHX_ char * start,SV * ioname,CV * cv)4373 S_intuit_method(pTHX_ char *start, SV *ioname, CV *cv)
4374 {
4375 char *s = start + (*start == '$');
4376 char tmpbuf[sizeof PL_tokenbuf];
4377 STRLEN len;
4378 GV* indirgv;
4379 /* Mustn't actually add anything to a symbol table.
4380 But also don't want to "initialise" any placeholder
4381 constants that might already be there into full
4382 blown PVGVs with attached PVCV. */
4383 GV * const gv =
4384 ioname ? gv_fetchsv(ioname, GV_NOADD_NOINIT, SVt_PVCV) : NULL;
4385
4386 PERL_ARGS_ASSERT_INTUIT_METHOD;
4387
4388 if (gv && SvTYPE(gv) == SVt_PVGV && GvIO(gv))
4389 return 0;
4390 if (cv && SvPOK(cv)) {
4391 const char *proto = CvPROTO(cv);
4392 if (proto) {
4393 while (*proto && (isSPACE(*proto) || *proto == ';'))
4394 proto++;
4395 if (*proto == '*')
4396 return 0;
4397 }
4398 }
4399
4400 if (*start == '$') {
4401 SSize_t start_off = start - SvPVX(PL_linestr);
4402 if (cv || PL_last_lop_op == OP_PRINT || PL_last_lop_op == OP_SAY
4403 || isUPPER(*PL_tokenbuf))
4404 return 0;
4405 /* this could be $# */
4406 if (isSPACE(*s))
4407 s = skipspace(s);
4408 PL_bufptr = SvPVX(PL_linestr) + start_off;
4409 PL_expect = XREF;
4410 return *s == '(' ? FUNCMETH : METHOD;
4411 }
4412
4413 s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
4414 /* start is the beginning of the possible filehandle/object,
4415 * and s is the end of it
4416 * tmpbuf is a copy of it (but with single quotes as double colons)
4417 */
4418
4419 if (!keyword(tmpbuf, len, 0)) {
4420 if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
4421 len -= 2;
4422 tmpbuf[len] = '\0';
4423 goto bare_package;
4424 }
4425 indirgv = gv_fetchpvn_flags(tmpbuf, len,
4426 GV_NOADD_NOINIT|( UTF ? SVf_UTF8 : 0 ),
4427 SVt_PVCV);
4428 if (indirgv && SvTYPE(indirgv) != SVt_NULL
4429 && (!isGV(indirgv) || GvCVu(indirgv)))
4430 return 0;
4431 /* filehandle or package name makes it a method */
4432 if (!cv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, UTF ? SVf_UTF8 : 0)) {
4433 s = skipspace(s);
4434 if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
4435 return 0; /* no assumptions -- "=>" quotes bareword */
4436 bare_package:
4437 NEXTVAL_NEXTTOKE.opval = newSVOP(OP_CONST, 0,
4438 S_newSV_maybe_utf8(aTHX_ tmpbuf, len));
4439 NEXTVAL_NEXTTOKE.opval->op_private = OPpCONST_BARE;
4440 PL_expect = XTERM;
4441 force_next(BAREWORD);
4442 PL_bufptr = s;
4443 return *s == '(' ? FUNCMETH : METHOD;
4444 }
4445 }
4446 return 0;
4447 }
4448
4449 /* Encoded script support. filter_add() effectively inserts a
4450 * 'pre-processing' function into the current source input stream.
4451 * Note that the filter function only applies to the current source file
4452 * (e.g., it will not affect files 'require'd or 'use'd by this one).
4453 *
4454 * The datasv parameter (which may be NULL) can be used to pass
4455 * private data to this instance of the filter. The filter function
4456 * can recover the SV using the FILTER_DATA macro and use it to
4457 * store private buffers and state information.
4458 *
4459 * The supplied datasv parameter is upgraded to a PVIO type
4460 * and the IoDIRP/IoANY field is used to store the function pointer,
4461 * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
4462 * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
4463 * private use must be set using malloc'd pointers.
4464 */
4465
4466 SV *
Perl_filter_add(pTHX_ filter_t funcp,SV * datasv)4467 Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
4468 {
4469 if (!funcp)
4470 return NULL;
4471
4472 if (!PL_parser)
4473 return NULL;
4474
4475 if (PL_parser->lex_flags & LEX_IGNORE_UTF8_HINTS)
4476 Perl_croak(aTHX_ "Source filters apply only to byte streams");
4477
4478 if (!PL_rsfp_filters)
4479 PL_rsfp_filters = newAV();
4480 if (!datasv)
4481 datasv = newSV(0);
4482 SvUPGRADE(datasv, SVt_PVIO);
4483 IoANY(datasv) = FPTR2DPTR(void *, funcp); /* stash funcp into spare field */
4484 IoFLAGS(datasv) |= IOf_FAKE_DIRP;
4485 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
4486 FPTR2DPTR(void *, IoANY(datasv)),
4487 SvPV_nolen(datasv)));
4488 av_unshift(PL_rsfp_filters, 1);
4489 av_store(PL_rsfp_filters, 0, datasv) ;
4490 if (
4491 !PL_parser->filtered
4492 && PL_parser->lex_flags & LEX_EVALBYTES
4493 && PL_bufptr < PL_bufend
4494 ) {
4495 const char *s = PL_bufptr;
4496 while (s < PL_bufend) {
4497 if (*s == '\n') {
4498 SV *linestr = PL_parser->linestr;
4499 char *buf = SvPVX(linestr);
4500 STRLEN const bufptr_pos = PL_parser->bufptr - buf;
4501 STRLEN const oldbufptr_pos = PL_parser->oldbufptr - buf;
4502 STRLEN const oldoldbufptr_pos=PL_parser->oldoldbufptr-buf;
4503 STRLEN const linestart_pos = PL_parser->linestart - buf;
4504 STRLEN const last_uni_pos =
4505 PL_parser->last_uni ? PL_parser->last_uni - buf : 0;
4506 STRLEN const last_lop_pos =
4507 PL_parser->last_lop ? PL_parser->last_lop - buf : 0;
4508 av_push(PL_rsfp_filters, linestr);
4509 PL_parser->linestr =
4510 newSVpvn(SvPVX(linestr), ++s-SvPVX(linestr));
4511 buf = SvPVX(PL_parser->linestr);
4512 PL_parser->bufend = buf + SvCUR(PL_parser->linestr);
4513 PL_parser->bufptr = buf + bufptr_pos;
4514 PL_parser->oldbufptr = buf + oldbufptr_pos;
4515 PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
4516 PL_parser->linestart = buf + linestart_pos;
4517 if (PL_parser->last_uni)
4518 PL_parser->last_uni = buf + last_uni_pos;
4519 if (PL_parser->last_lop)
4520 PL_parser->last_lop = buf + last_lop_pos;
4521 SvLEN_set(linestr, SvCUR(linestr));
4522 SvCUR_set(linestr, s - SvPVX(linestr));
4523 PL_parser->filtered = 1;
4524 break;
4525 }
4526 s++;
4527 }
4528 }
4529 return(datasv);
4530 }
4531
4532
4533 /* Delete most recently added instance of this filter function. */
4534 void
Perl_filter_del(pTHX_ filter_t funcp)4535 Perl_filter_del(pTHX_ filter_t funcp)
4536 {
4537 SV *datasv;
4538
4539 PERL_ARGS_ASSERT_FILTER_DEL;
4540
4541 #ifdef DEBUGGING
4542 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p",
4543 FPTR2DPTR(void*, funcp)));
4544 #endif
4545 if (!PL_parser || !PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
4546 return;
4547 /* if filter is on top of stack (usual case) just pop it off */
4548 datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
4549 if (IoANY(datasv) == FPTR2DPTR(void *, funcp)) {
4550 sv_free(av_pop(PL_rsfp_filters));
4551
4552 return;
4553 }
4554 /* we need to search for the correct entry and clear it */
4555 Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
4556 }
4557
4558
4559 /* Invoke the idxth filter function for the current rsfp. */
4560 /* maxlen 0 = read one text line */
4561 I32
Perl_filter_read(pTHX_ int idx,SV * buf_sv,int maxlen)4562 Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
4563 {
4564 filter_t funcp;
4565 I32 ret;
4566 SV *datasv = NULL;
4567 /* This API is bad. It should have been using unsigned int for maxlen.
4568 Not sure if we want to change the API, but if not we should sanity
4569 check the value here. */
4570 unsigned int correct_length = maxlen < 0 ? PERL_INT_MAX : maxlen;
4571
4572 PERL_ARGS_ASSERT_FILTER_READ;
4573
4574 if (!PL_parser || !PL_rsfp_filters)
4575 return -1;
4576 if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */
4577 /* Provide a default input filter to make life easy. */
4578 /* Note that we append to the line. This is handy. */
4579 DEBUG_P(PerlIO_printf(Perl_debug_log,
4580 "filter_read %d: from rsfp\n", idx));
4581 if (correct_length) {
4582 /* Want a block */
4583 int len ;
4584 const int old_len = SvCUR(buf_sv);
4585
4586 /* ensure buf_sv is large enough */
4587 SvGROW(buf_sv, (STRLEN)(old_len + correct_length + 1)) ;
4588 if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len,
4589 correct_length)) <= 0) {
4590 if (PerlIO_error(PL_rsfp))
4591 return -1; /* error */
4592 else
4593 return 0 ; /* end of file */
4594 }
4595 SvCUR_set(buf_sv, old_len + len) ;
4596 SvPVX(buf_sv)[old_len + len] = '\0';
4597 } else {
4598 /* Want a line */
4599 if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
4600 if (PerlIO_error(PL_rsfp))
4601 return -1; /* error */
4602 else
4603 return 0 ; /* end of file */
4604 }
4605 }
4606 return SvCUR(buf_sv);
4607 }
4608 /* Skip this filter slot if filter has been deleted */
4609 if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) {
4610 DEBUG_P(PerlIO_printf(Perl_debug_log,
4611 "filter_read %d: skipped (filter deleted)\n",
4612 idx));
4613 return FILTER_READ(idx+1, buf_sv, correct_length); /* recurse */
4614 }
4615 if (SvTYPE(datasv) != SVt_PVIO) {
4616 if (correct_length) {
4617 /* Want a block */
4618 const STRLEN remainder = SvLEN(datasv) - SvCUR(datasv);
4619 if (!remainder) return 0; /* eof */
4620 if (correct_length > remainder) correct_length = remainder;
4621 sv_catpvn(buf_sv, SvEND(datasv), correct_length);
4622 SvCUR_set(datasv, SvCUR(datasv) + correct_length);
4623 } else {
4624 /* Want a line */
4625 const char *s = SvEND(datasv);
4626 const char *send = SvPVX(datasv) + SvLEN(datasv);
4627 while (s < send) {
4628 if (*s == '\n') {
4629 s++;
4630 break;
4631 }
4632 s++;
4633 }
4634 if (s == send) return 0; /* eof */
4635 sv_catpvn(buf_sv, SvEND(datasv), s-SvEND(datasv));
4636 SvCUR_set(datasv, s-SvPVX(datasv));
4637 }
4638 return SvCUR(buf_sv);
4639 }
4640 /* Get function pointer hidden within datasv */
4641 funcp = DPTR2FPTR(filter_t, IoANY(datasv));
4642 DEBUG_P(PerlIO_printf(Perl_debug_log,
4643 "filter_read %d: via function %p (%s)\n",
4644 idx, (void*)datasv, SvPV_nolen_const(datasv)));
4645 /* Call function. The function is expected to */
4646 /* call "FILTER_READ(idx+1, buf_sv)" first. */
4647 /* Return: <0:error, =0:eof, >0:not eof */
4648 ENTER;
4649 save_scalar(PL_errgv);
4650 ret = (*funcp)(aTHX_ idx, buf_sv, correct_length);
4651 LEAVE;
4652 return ret;
4653 }
4654
4655 STATIC char *
S_filter_gets(pTHX_ SV * sv,STRLEN append)4656 S_filter_gets(pTHX_ SV *sv, STRLEN append)
4657 {
4658 PERL_ARGS_ASSERT_FILTER_GETS;
4659
4660 #ifdef PERL_CR_FILTER
4661 if (!PL_rsfp_filters) {
4662 filter_add(S_cr_textfilter,NULL);
4663 }
4664 #endif
4665 if (PL_rsfp_filters) {
4666 if (!append)
4667 SvCUR_set(sv, 0); /* start with empty line */
4668 if (FILTER_READ(0, sv, 0) > 0)
4669 return ( SvPVX(sv) ) ;
4670 else
4671 return NULL ;
4672 }
4673 else
4674 return (sv_gets(sv, PL_rsfp, append));
4675 }
4676
4677 STATIC HV *
S_find_in_my_stash(pTHX_ const char * pkgname,STRLEN len)4678 S_find_in_my_stash(pTHX_ const char *pkgname, STRLEN len)
4679 {
4680 GV *gv;
4681
4682 PERL_ARGS_ASSERT_FIND_IN_MY_STASH;
4683
4684 if (memEQs(pkgname, len, "__PACKAGE__"))
4685 return PL_curstash;
4686
4687 if (len > 2
4688 && (pkgname[len - 2] == ':' && pkgname[len - 1] == ':')
4689 && (gv = gv_fetchpvn_flags(pkgname,
4690 len,
4691 ( UTF ? SVf_UTF8 : 0 ), SVt_PVHV)))
4692 {
4693 return GvHV(gv); /* Foo:: */
4694 }
4695
4696 /* use constant CLASS => 'MyClass' */
4697 gv = gv_fetchpvn_flags(pkgname, len, UTF ? SVf_UTF8 : 0, SVt_PVCV);
4698 if (gv && GvCV(gv)) {
4699 SV * const sv = cv_const_sv(GvCV(gv));
4700 if (sv)
4701 return gv_stashsv(sv, 0);
4702 }
4703
4704 return gv_stashpvn(pkgname, len, UTF ? SVf_UTF8 : 0);
4705 }
4706
4707
4708 STATIC char *
S_tokenize_use(pTHX_ int is_use,char * s)4709 S_tokenize_use(pTHX_ int is_use, char *s) {
4710 PERL_ARGS_ASSERT_TOKENIZE_USE;
4711
4712 if (PL_expect != XSTATE)
4713 /* diag_listed_as: "use" not allowed in expression */
4714 yyerror(Perl_form(aTHX_ "\"%s\" not allowed in expression",
4715 is_use ? "use" : "no"));
4716 PL_expect = XTERM;
4717 s = skipspace(s);
4718 if (isDIGIT(*s) || (*s == 'v' && isDIGIT(s[1]))) {
4719 s = force_version(s, TRUE);
4720 if (*s == ';' || *s == '}'
4721 || (s = skipspace(s), (*s == ';' || *s == '}'))) {
4722 NEXTVAL_NEXTTOKE.opval = NULL;
4723 force_next(BAREWORD);
4724 }
4725 else if (*s == 'v') {
4726 s = force_word(s,BAREWORD,FALSE,TRUE);
4727 s = force_version(s, FALSE);
4728 }
4729 }
4730 else {
4731 s = force_word(s,BAREWORD,FALSE,TRUE);
4732 s = force_version(s, FALSE);
4733 }
4734 pl_yylval.ival = is_use;
4735 return s;
4736 }
4737 #ifdef DEBUGGING
4738 static const char* const exp_name[] =
4739 { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
4740 "ATTRTERM", "TERMBLOCK", "XBLOCKTERM", "POSTDEREF",
4741 "SIGVAR", "TERMORDORDOR"
4742 };
4743 #endif
4744
4745 #define word_takes_any_delimiter(p,l) S_word_takes_any_delimiter(p,l)
4746 STATIC bool
S_word_takes_any_delimiter(char * p,STRLEN len)4747 S_word_takes_any_delimiter(char *p, STRLEN len)
4748 {
4749 return (len == 1 && strchr("msyq", p[0]))
4750 || (len == 2
4751 && ((p[0] == 't' && p[1] == 'r')
4752 || (p[0] == 'q' && strchr("qwxr", p[1]))));
4753 }
4754
4755 static void
S_check_scalar_slice(pTHX_ char * s)4756 S_check_scalar_slice(pTHX_ char *s)
4757 {
4758 s++;
4759 while (SPACE_OR_TAB(*s)) s++;
4760 if (*s == 'q' && s[1] == 'w' && !isWORDCHAR_lazy_if_safe(s+2,
4761 PL_bufend,
4762 UTF))
4763 {
4764 return;
4765 }
4766 while ( isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF)
4767 || (*s && strchr(" \t$#+-'\"", *s)))
4768 {
4769 s += UTF ? UTF8SKIP(s) : 1;
4770 }
4771 if (*s == '}' || *s == ']')
4772 pl_yylval.ival = OPpSLICEWARNING;
4773 }
4774
4775 #define lex_token_boundary() S_lex_token_boundary(aTHX)
4776 static void
S_lex_token_boundary(pTHX)4777 S_lex_token_boundary(pTHX)
4778 {
4779 PL_oldoldbufptr = PL_oldbufptr;
4780 PL_oldbufptr = PL_bufptr;
4781 }
4782
4783 #define vcs_conflict_marker(s) S_vcs_conflict_marker(aTHX_ s)
4784 static char *
S_vcs_conflict_marker(pTHX_ char * s)4785 S_vcs_conflict_marker(pTHX_ char *s)
4786 {
4787 lex_token_boundary();
4788 PL_bufptr = s;
4789 yyerror("Version control conflict marker");
4790 while (s < PL_bufend && *s != '\n')
4791 s++;
4792 return s;
4793 }
4794
4795 /*
4796 yylex
4797
4798 Works out what to call the token just pulled out of the input
4799 stream. The yacc parser takes care of taking the ops we return and
4800 stitching them into a tree.
4801
4802 Returns:
4803 The type of the next token
4804
4805 Structure:
4806 Check if we have already built the token; if so, use it.
4807 Switch based on the current state:
4808 - if we have a case modifier in a string, deal with that
4809 - handle other cases of interpolation inside a string
4810 - scan the next line if we are inside a format
4811 In the normal state, switch on the next character:
4812 - default:
4813 if alphabetic, go to key lookup
4814 unrecognized character - croak
4815 - 0/4/26: handle end-of-line or EOF
4816 - cases for whitespace
4817 - \n and #: handle comments and line numbers
4818 - various operators, brackets and sigils
4819 - numbers
4820 - quotes
4821 - 'v': vstrings (or go to key lookup)
4822 - 'x' repetition operator (or go to key lookup)
4823 - other ASCII alphanumerics (key lookup begins here):
4824 word before => ?
4825 keyword plugin
4826 scan built-in keyword (but do nothing with it yet)
4827 check for statement label
4828 check for lexical subs
4829 goto just_a_word if there is one
4830 see whether built-in keyword is overridden
4831 switch on keyword number:
4832 - default: just_a_word:
4833 not a built-in keyword; handle bareword lookup
4834 disambiguate between method and sub call
4835 fall back to bareword
4836 - cases for built-in keywords
4837 */
4838
4839
4840 int
Perl_yylex(pTHX)4841 Perl_yylex(pTHX)
4842 {
4843 dVAR;
4844 char *s = PL_bufptr;
4845 char *d;
4846 STRLEN len;
4847 bool bof = FALSE;
4848 const bool saw_infix_sigil = cBOOL(PL_parser->saw_infix_sigil);
4849 U8 formbrack = 0;
4850 U32 fake_eof = 0;
4851
4852 /* orig_keyword, gvp, and gv are initialized here because
4853 * jump to the label just_a_word_zero can bypass their
4854 * initialization later. */
4855 I32 orig_keyword = 0;
4856 GV *gv = NULL;
4857 GV **gvp = NULL;
4858
4859 if (UNLIKELY(PL_parser->recheck_utf8_validity)) {
4860 const U8* first_bad_char_loc;
4861 if (UTF && UNLIKELY(! is_utf8_string_loc((U8 *) PL_bufptr,
4862 PL_bufend - PL_bufptr,
4863 &first_bad_char_loc)))
4864 {
4865 _force_out_malformed_utf8_message(first_bad_char_loc,
4866 (U8 *) PL_bufend,
4867 0,
4868 1 /* 1 means die */ );
4869 NOT_REACHED; /* NOTREACHED */
4870 }
4871 PL_parser->recheck_utf8_validity = FALSE;
4872 }
4873 DEBUG_T( {
4874 SV* tmp = newSVpvs("");
4875 PerlIO_printf(Perl_debug_log, "### %" IVdf ":LEX_%s/X%s %s\n",
4876 (IV)CopLINE(PL_curcop),
4877 lex_state_names[PL_lex_state],
4878 exp_name[PL_expect],
4879 pv_display(tmp, s, strlen(s), 0, 60));
4880 SvREFCNT_dec(tmp);
4881 } );
4882
4883 /* when we've already built the next token, just pull it out of the queue */
4884 if (PL_nexttoke) {
4885 PL_nexttoke--;
4886 pl_yylval = PL_nextval[PL_nexttoke];
4887 {
4888 I32 next_type;
4889 next_type = PL_nexttype[PL_nexttoke];
4890 if (next_type & (7<<24)) {
4891 if (next_type & (1<<24)) {
4892 if (PL_lex_brackets > 100)
4893 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
4894 PL_lex_brackstack[PL_lex_brackets++] =
4895 (char) ((next_type >> 16) & 0xff);
4896 }
4897 if (next_type & (2<<24))
4898 PL_lex_allbrackets++;
4899 if (next_type & (4<<24))
4900 PL_lex_allbrackets--;
4901 next_type &= 0xffff;
4902 }
4903 return REPORT(next_type == 'p' ? pending_ident() : next_type);
4904 }
4905 }
4906
4907 switch (PL_lex_state) {
4908 case LEX_NORMAL:
4909 case LEX_INTERPNORMAL:
4910 break;
4911
4912 /* interpolated case modifiers like \L \U, including \Q and \E.
4913 when we get here, PL_bufptr is at the \
4914 */
4915 case LEX_INTERPCASEMOD:
4916 #ifdef DEBUGGING
4917 if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
4918 Perl_croak(aTHX_
4919 "panic: INTERPCASEMOD bufptr=%p, bufend=%p, *bufptr=%u",
4920 PL_bufptr, PL_bufend, *PL_bufptr);
4921 #endif
4922 /* handle \E or end of string */
4923 if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
4924 /* if at a \E */
4925 if (PL_lex_casemods) {
4926 const char oldmod = PL_lex_casestack[--PL_lex_casemods];
4927 PL_lex_casestack[PL_lex_casemods] = '\0';
4928
4929 if (PL_bufptr != PL_bufend
4930 && (oldmod == 'L' || oldmod == 'U' || oldmod == 'Q'
4931 || oldmod == 'F')) {
4932 PL_bufptr += 2;
4933 PL_lex_state = LEX_INTERPCONCAT;
4934 }
4935 PL_lex_allbrackets--;
4936 return REPORT(')');
4937 }
4938 else if ( PL_bufptr != PL_bufend && PL_bufptr[1] == 'E' ) {
4939 /* Got an unpaired \E */
4940 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4941 "Useless use of \\E");
4942 }
4943 if (PL_bufptr != PL_bufend)
4944 PL_bufptr += 2;
4945 PL_lex_state = LEX_INTERPCONCAT;
4946 return yylex();
4947 }
4948 else {
4949 DEBUG_T({ PerlIO_printf(Perl_debug_log,
4950 "### Saw case modifier\n"); });
4951 s = PL_bufptr + 1;
4952 if (s[1] == '\\' && s[2] == 'E') {
4953 PL_bufptr = s + 3;
4954 PL_lex_state = LEX_INTERPCONCAT;
4955 return yylex();
4956 }
4957 else {
4958 I32 tmp;
4959 if ( memBEGINs(s, (STRLEN) (PL_bufend - s), "L\\u")
4960 || memBEGINs(s, (STRLEN) (PL_bufend - s), "U\\l"))
4961 {
4962 tmp = *s, *s = s[2], s[2] = (char)tmp; /* misordered... */
4963 }
4964 if ((*s == 'L' || *s == 'U' || *s == 'F')
4965 && (strpbrk(PL_lex_casestack, "LUF")))
4966 {
4967 PL_lex_casestack[--PL_lex_casemods] = '\0';
4968 PL_lex_allbrackets--;
4969 return REPORT(')');
4970 }
4971 if (PL_lex_casemods > 10)
4972 Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
4973 PL_lex_casestack[PL_lex_casemods++] = *s;
4974 PL_lex_casestack[PL_lex_casemods] = '\0';
4975 PL_lex_state = LEX_INTERPCONCAT;
4976 NEXTVAL_NEXTTOKE.ival = 0;
4977 force_next((2<<24)|'(');
4978 if (*s == 'l')
4979 NEXTVAL_NEXTTOKE.ival = OP_LCFIRST;
4980 else if (*s == 'u')
4981 NEXTVAL_NEXTTOKE.ival = OP_UCFIRST;
4982 else if (*s == 'L')
4983 NEXTVAL_NEXTTOKE.ival = OP_LC;
4984 else if (*s == 'U')
4985 NEXTVAL_NEXTTOKE.ival = OP_UC;
4986 else if (*s == 'Q')
4987 NEXTVAL_NEXTTOKE.ival = OP_QUOTEMETA;
4988 else if (*s == 'F')
4989 NEXTVAL_NEXTTOKE.ival = OP_FC;
4990 else
4991 Perl_croak(aTHX_ "panic: yylex, *s=%u", *s);
4992 PL_bufptr = s + 1;
4993 }
4994 force_next(FUNC);
4995 if (PL_lex_starts) {
4996 s = PL_bufptr;
4997 PL_lex_starts = 0;
4998 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
4999 if (PL_lex_casemods == 1 && PL_lex_inpat)
5000 TOKEN(',');
5001 else
5002 AopNOASSIGN(OP_CONCAT);
5003 }
5004 else
5005 return yylex();
5006 }
5007
5008 case LEX_INTERPPUSH:
5009 return REPORT(sublex_push());
5010
5011 case LEX_INTERPSTART:
5012 if (PL_bufptr == PL_bufend)
5013 return REPORT(sublex_done());
5014 DEBUG_T({ if(*PL_bufptr != '(') PerlIO_printf(Perl_debug_log,
5015 "### Interpolated variable\n"); });
5016 PL_expect = XTERM;
5017 /* for /@a/, we leave the joining for the regex engine to do
5018 * (unless we're within \Q etc) */
5019 PL_lex_dojoin = (*PL_bufptr == '@'
5020 && (!PL_lex_inpat || PL_lex_casemods));
5021 PL_lex_state = LEX_INTERPNORMAL;
5022 if (PL_lex_dojoin) {
5023 NEXTVAL_NEXTTOKE.ival = 0;
5024 force_next(',');
5025 force_ident("\"", '$');
5026 NEXTVAL_NEXTTOKE.ival = 0;
5027 force_next('$');
5028 NEXTVAL_NEXTTOKE.ival = 0;
5029 force_next((2<<24)|'(');
5030 NEXTVAL_NEXTTOKE.ival = OP_JOIN; /* emulate join($", ...) */
5031 force_next(FUNC);
5032 }
5033 /* Convert (?{...}) and friends to 'do {...}' */
5034 if (PL_lex_inpat && *PL_bufptr == '(') {
5035 PL_parser->lex_shared->re_eval_start = PL_bufptr;
5036 PL_bufptr += 2;
5037 if (*PL_bufptr != '{')
5038 PL_bufptr++;
5039 PL_expect = XTERMBLOCK;
5040 force_next(DO);
5041 }
5042
5043 if (PL_lex_starts++) {
5044 s = PL_bufptr;
5045 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
5046 if (!PL_lex_casemods && PL_lex_inpat)
5047 TOKEN(',');
5048 else
5049 AopNOASSIGN(OP_CONCAT);
5050 }
5051 return yylex();
5052
5053 case LEX_INTERPENDMAYBE:
5054 if (intuit_more(PL_bufptr, PL_bufend)) {
5055 PL_lex_state = LEX_INTERPNORMAL; /* false alarm, more expr */
5056 break;
5057 }
5058 /* FALLTHROUGH */
5059
5060 case LEX_INTERPEND:
5061 if (PL_lex_dojoin) {
5062 const U8 dojoin_was = PL_lex_dojoin;
5063 PL_lex_dojoin = FALSE;
5064 PL_lex_state = LEX_INTERPCONCAT;
5065 PL_lex_allbrackets--;
5066 return REPORT(dojoin_was == 1 ? (int)')' : (int)POSTJOIN);
5067 }
5068 if (PL_lex_inwhat == OP_SUBST && PL_linestr == PL_lex_repl
5069 && SvEVALED(PL_lex_repl))
5070 {
5071 if (PL_bufptr != PL_bufend)
5072 Perl_croak(aTHX_ "Bad evalled substitution pattern");
5073 PL_lex_repl = NULL;
5074 }
5075 /* Paranoia. re_eval_start is adjusted when S_scan_heredoc sets
5076 re_eval_str. If the here-doc body’s length equals the previous
5077 value of re_eval_start, re_eval_start will now be null. So
5078 check re_eval_str as well. */
5079 if (PL_parser->lex_shared->re_eval_start
5080 || PL_parser->lex_shared->re_eval_str) {
5081 SV *sv;
5082 if (*PL_bufptr != ')')
5083 Perl_croak(aTHX_ "Sequence (?{...}) not terminated with ')'");
5084 PL_bufptr++;
5085 /* having compiled a (?{..}) expression, return the original
5086 * text too, as a const */
5087 if (PL_parser->lex_shared->re_eval_str) {
5088 sv = PL_parser->lex_shared->re_eval_str;
5089 PL_parser->lex_shared->re_eval_str = NULL;
5090 SvCUR_set(sv,
5091 PL_bufptr - PL_parser->lex_shared->re_eval_start);
5092 SvPV_shrink_to_cur(sv);
5093 }
5094 else sv = newSVpvn(PL_parser->lex_shared->re_eval_start,
5095 PL_bufptr - PL_parser->lex_shared->re_eval_start);
5096 NEXTVAL_NEXTTOKE.opval =
5097 newSVOP(OP_CONST, 0,
5098 sv);
5099 force_next(THING);
5100 PL_parser->lex_shared->re_eval_start = NULL;
5101 PL_expect = XTERM;
5102 return REPORT(',');
5103 }
5104
5105 /* FALLTHROUGH */
5106 case LEX_INTERPCONCAT:
5107 #ifdef DEBUGGING
5108 if (PL_lex_brackets)
5109 Perl_croak(aTHX_ "panic: INTERPCONCAT, lex_brackets=%ld",
5110 (long) PL_lex_brackets);
5111 #endif
5112 if (PL_bufptr == PL_bufend)
5113 return REPORT(sublex_done());
5114
5115 /* m'foo' still needs to be parsed for possible (?{...}) */
5116 if (SvIVX(PL_linestr) == '\'' && !PL_lex_inpat) {
5117 SV *sv = newSVsv(PL_linestr);
5118 sv = tokeq(sv);
5119 pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
5120 s = PL_bufend;
5121 }
5122 else {
5123 int save_error_count = PL_error_count;
5124
5125 s = scan_const(PL_bufptr);
5126
5127 /* Set flag if this was a pattern and there were errors. op.c will
5128 * refuse to compile a pattern with this flag set. Otherwise, we
5129 * could get segfaults, etc. */
5130 if (PL_lex_inpat && PL_error_count > save_error_count) {
5131 ((PMOP*)PL_lex_inpat)->op_pmflags |= PMf_HAS_ERROR;
5132 }
5133 if (*s == '\\')
5134 PL_lex_state = LEX_INTERPCASEMOD;
5135 else
5136 PL_lex_state = LEX_INTERPSTART;
5137 }
5138
5139 if (s != PL_bufptr) {
5140 NEXTVAL_NEXTTOKE = pl_yylval;
5141 PL_expect = XTERM;
5142 force_next(THING);
5143 if (PL_lex_starts++) {
5144 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
5145 if (!PL_lex_casemods && PL_lex_inpat)
5146 TOKEN(',');
5147 else
5148 AopNOASSIGN(OP_CONCAT);
5149 }
5150 else {
5151 PL_bufptr = s;
5152 return yylex();
5153 }
5154 }
5155
5156 return yylex();
5157 case LEX_FORMLINE:
5158 if (PL_parser->sub_error_count != PL_error_count) {
5159 /* There was an error parsing a formline, which tends to
5160 mess up the parser.
5161 Unlike interpolated sub-parsing, we can't treat any of
5162 these as recoverable, so no need to check sub_no_recover.
5163 */
5164 yyquit();
5165 }
5166 assert(PL_lex_formbrack);
5167 s = scan_formline(PL_bufptr);
5168 if (!PL_lex_formbrack)
5169 {
5170 formbrack = 1;
5171 goto rightbracket;
5172 }
5173 PL_bufptr = s;
5174 return yylex();
5175 }
5176
5177 /* We really do *not* want PL_linestr ever becoming a COW. */
5178 assert (!SvIsCOW(PL_linestr));
5179 s = PL_bufptr;
5180 PL_oldoldbufptr = PL_oldbufptr;
5181 PL_oldbufptr = s;
5182 PL_parser->saw_infix_sigil = 0;
5183
5184 if (PL_in_my == KEY_sigvar) {
5185 /* we expect the sigil and optional var name part of a
5186 * signature element here. Since a '$' is not necessarily
5187 * followed by a var name, handle it specially here; the general
5188 * yylex code would otherwise try to interpret whatever follows
5189 * as a var; e.g. ($, ...) would be seen as the var '$,'
5190 */
5191
5192 U8 sigil;
5193
5194 s = skipspace(s);
5195 sigil = *s++;
5196 PL_bufptr = s; /* for error reporting */
5197 switch (sigil) {
5198 case '$':
5199 case '@':
5200 case '%':
5201 /* spot stuff that looks like an prototype */
5202 if (strchr("$:@%&*;\\[]", *s)) {
5203 yyerror("Illegal character following sigil in a subroutine signature");
5204 break;
5205 }
5206 /* '$#' is banned, while '$ # comment' isn't */
5207 if (*s == '#') {
5208 yyerror("'#' not allowed immediately following a sigil in a subroutine signature");
5209 break;
5210 }
5211 s = skipspace(s);
5212 if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
5213 char *dest = PL_tokenbuf + 1;
5214 /* read var name, including sigil, into PL_tokenbuf */
5215 PL_tokenbuf[0] = sigil;
5216 parse_ident(&s, &dest, dest + sizeof(PL_tokenbuf) - 1,
5217 0, cBOOL(UTF), FALSE, FALSE);
5218 *dest = '\0';
5219 assert(PL_tokenbuf[1]); /* we have a variable name */
5220 }
5221 else {
5222 *PL_tokenbuf = 0;
5223 PL_in_my = 0;
5224 }
5225
5226 s = skipspace(s);
5227 /* parse the = for the default ourselves to avoid '+=' etc being accepted here
5228 * as the ASSIGNOP, and exclude other tokens that start with =
5229 */
5230 if (*s == '=' && (!s[1] || strchr("=~>", s[1]) == 0)) {
5231 /* save now to report with the same context as we did when
5232 * all ASSIGNOPS were accepted */
5233 PL_oldbufptr = s;
5234
5235 ++s;
5236 NEXTVAL_NEXTTOKE.ival = 0;
5237 force_next(ASSIGNOP);
5238 PL_expect = XTERM;
5239 }
5240 else if (*s == ',' || *s == ')') {
5241 PL_expect = XOPERATOR;
5242 }
5243 else {
5244 /* make sure the context shows the unexpected character and
5245 * hopefully a bit more */
5246 if (*s) ++s;
5247 while (*s && *s != '$' && *s != '@' && *s != '%' && *s != ')')
5248 s++;
5249 PL_bufptr = s; /* for error reporting */
5250 yyerror("Illegal operator following parameter in a subroutine signature");
5251 PL_in_my = 0;
5252 }
5253 if (*PL_tokenbuf) {
5254 NEXTVAL_NEXTTOKE.ival = sigil;
5255 force_next('p'); /* force a signature pending identifier */
5256 }
5257 break;
5258
5259 case ')':
5260 PL_expect = XBLOCK;
5261 break;
5262 case ',': /* handle ($a,,$b) */
5263 break;
5264
5265 default:
5266 PL_in_my = 0;
5267 yyerror("A signature parameter must start with '$', '@' or '%'");
5268 /* very crude error recovery: skip to likely next signature
5269 * element */
5270 while (*s && *s != '$' && *s != '@' && *s != '%' && *s != ')')
5271 s++;
5272 break;
5273 }
5274 TOKEN(sigil);
5275 }
5276
5277 retry:
5278 switch (*s) {
5279 default:
5280 if (UTF) {
5281 if (isIDFIRST_utf8_safe(s, PL_bufend)) {
5282 goto keylookup;
5283 }
5284 }
5285 else if (isALNUMC(*s)) {
5286 goto keylookup;
5287 }
5288 {
5289 SV *dsv = newSVpvs_flags("", SVs_TEMP);
5290 const char *c;
5291 if (UTF) {
5292 STRLEN skiplen = UTF8SKIP(s);
5293 STRLEN stravail = PL_bufend - s;
5294 c = sv_uni_display(dsv, newSVpvn_flags(s,
5295 skiplen > stravail ? stravail : skiplen,
5296 SVs_TEMP | SVf_UTF8),
5297 10, UNI_DISPLAY_ISPRINT);
5298 }
5299 else {
5300 c = Perl_form(aTHX_ "\\x%02X", (unsigned char)*s);
5301 }
5302
5303 if (s >= PL_linestart) {
5304 d = PL_linestart;
5305 }
5306 else {
5307 /* somehow (probably due to a parse failure), PL_linestart has advanced
5308 * pass PL_bufptr, get a reasonable beginning of line
5309 */
5310 d = s;
5311 while (d > SvPVX(PL_linestr) && d[-1] && d[-1] != '\n')
5312 --d;
5313 }
5314 len = UTF ? Perl_utf8_length(aTHX_ (U8 *) d, (U8 *) s) : (STRLEN) (s - d);
5315 if (len > UNRECOGNIZED_PRECEDE_COUNT) {
5316 d = UTF ? (char *) utf8_hop_back((U8 *) s, -UNRECOGNIZED_PRECEDE_COUNT, (U8 *)d) : s - UNRECOGNIZED_PRECEDE_COUNT;
5317 }
5318
5319 Perl_croak(aTHX_ "Unrecognized character %s; marked by <-- HERE after %" UTF8f "<-- HERE near column %d", c,
5320 UTF8fARG(UTF, (s - d), d),
5321 (int) len + 1);
5322 }
5323 case 4:
5324 case 26:
5325 goto fake_eof; /* emulate EOF on ^D or ^Z */
5326 case 0:
5327 if ((!PL_rsfp || PL_lex_inwhat)
5328 && (!PL_parser->filtered || s+1 < PL_bufend)) {
5329 PL_last_uni = 0;
5330 PL_last_lop = 0;
5331 if (PL_lex_brackets
5332 && PL_lex_brackstack[PL_lex_brackets-1] != XFAKEEOF)
5333 {
5334 yyerror((const char *)
5335 (PL_lex_formbrack
5336 ? "Format not terminated"
5337 : "Missing right curly or square bracket"));
5338 }
5339 DEBUG_T( { PerlIO_printf(Perl_debug_log,
5340 "### Tokener got EOF\n");
5341 } );
5342 TOKEN(0);
5343 }
5344 if (s++ < PL_bufend)
5345 goto retry; /* ignore stray nulls */
5346 PL_last_uni = 0;
5347 PL_last_lop = 0;
5348 if (!PL_in_eval && !PL_preambled) {
5349 PL_preambled = TRUE;
5350 if (PL_perldb) {
5351 /* Generate a string of Perl code to load the debugger.
5352 * If PERL5DB is set, it will return the contents of that,
5353 * otherwise a compile-time require of perl5db.pl. */
5354
5355 const char * const pdb = PerlEnv_getenv("PERL5DB");
5356
5357 if (pdb) {
5358 sv_setpv(PL_linestr, pdb);
5359 sv_catpvs(PL_linestr,";");
5360 } else {
5361 SETERRNO(0,SS_NORMAL);
5362 sv_setpvs(PL_linestr, "BEGIN { require 'perl5db.pl' };");
5363 }
5364 PL_parser->preambling = CopLINE(PL_curcop);
5365 } else
5366 SvPVCLEAR(PL_linestr);
5367 if (PL_preambleav) {
5368 SV **svp = AvARRAY(PL_preambleav);
5369 SV **const end = svp + AvFILLp(PL_preambleav);
5370 while(svp <= end) {
5371 sv_catsv(PL_linestr, *svp);
5372 ++svp;
5373 sv_catpvs(PL_linestr, ";");
5374 }
5375 sv_free(MUTABLE_SV(PL_preambleav));
5376 PL_preambleav = NULL;
5377 }
5378 if (PL_minus_E)
5379 sv_catpvs(PL_linestr,
5380 "use feature ':5." STRINGIFY(PERL_VERSION) "';");
5381 if (PL_minus_n || PL_minus_p) {
5382 sv_catpvs(PL_linestr, "LINE: while (<>) {"/*}*/);
5383 if (PL_minus_l)
5384 sv_catpvs(PL_linestr,"chomp;");
5385 if (PL_minus_a) {
5386 if (PL_minus_F) {
5387 if ( ( *PL_splitstr == '/'
5388 || *PL_splitstr == '\''
5389 || *PL_splitstr == '"')
5390 && strchr(PL_splitstr + 1, *PL_splitstr))
5391 {
5392 /* strchr is ok, because -F pattern can't contain
5393 * embeddded NULs */
5394 Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
5395 }
5396 else {
5397 /* "q\0${splitstr}\0" is legal perl. Yes, even NUL
5398 bytes can be used as quoting characters. :-) */
5399 const char *splits = PL_splitstr;
5400 sv_catpvs(PL_linestr, "our @F=split(q\0");
5401 do {
5402 /* Need to \ \s */
5403 if (*splits == '\\')
5404 sv_catpvn(PL_linestr, splits, 1);
5405 sv_catpvn(PL_linestr, splits, 1);
5406 } while (*splits++);
5407 /* This loop will embed the trailing NUL of
5408 PL_linestr as the last thing it does before
5409 terminating. */
5410 sv_catpvs(PL_linestr, ");");
5411 }
5412 }
5413 else
5414 sv_catpvs(PL_linestr,"our @F=split(' ');");
5415 }
5416 }
5417 sv_catpvs(PL_linestr, "\n");
5418 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
5419 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
5420 PL_last_lop = PL_last_uni = NULL;
5421 if (PERLDB_LINE_OR_SAVESRC && PL_curstash != PL_debstash)
5422 update_debugger_info(PL_linestr, NULL, 0);
5423 goto retry;
5424 }
5425 do {
5426 fake_eof = 0;
5427 bof = cBOOL(PL_rsfp);
5428 if (0) {
5429 fake_eof:
5430 fake_eof = LEX_FAKE_EOF;
5431 }
5432 PL_bufptr = PL_bufend;
5433 COPLINE_INC_WITH_HERELINES;
5434 if (!lex_next_chunk(fake_eof)) {
5435 CopLINE_dec(PL_curcop);
5436 s = PL_bufptr;
5437 TOKEN(';'); /* not infinite loop because rsfp is NULL now */
5438 }
5439 CopLINE_dec(PL_curcop);
5440 s = PL_bufptr;
5441 /* If it looks like the start of a BOM or raw UTF-16,
5442 * check if it in fact is. */
5443 if (bof && PL_rsfp
5444 && ( *s == 0
5445 || *(U8*)s == BOM_UTF8_FIRST_BYTE
5446 || *(U8*)s >= 0xFE
5447 || s[1] == 0))
5448 {
5449 Off_t offset = (IV)PerlIO_tell(PL_rsfp);
5450 bof = (offset == (Off_t)SvCUR(PL_linestr));
5451 #if defined(PERLIO_USING_CRLF) && defined(PERL_TEXTMODE_SCRIPTS)
5452 /* offset may include swallowed CR */
5453 if (!bof)
5454 bof = (offset == (Off_t)SvCUR(PL_linestr)+1);
5455 #endif
5456 if (bof) {
5457 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
5458 s = swallow_bom((U8*)s);
5459 }
5460 }
5461 if (PL_parser->in_pod) {
5462 /* Incest with pod. */
5463 if ( memBEGINPs(s, (STRLEN) (PL_bufend - s), "=cut")
5464 && !isALPHA(s[4]))
5465 {
5466 SvPVCLEAR(PL_linestr);
5467 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
5468 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
5469 PL_last_lop = PL_last_uni = NULL;
5470 PL_parser->in_pod = 0;
5471 }
5472 }
5473 if (PL_rsfp || PL_parser->filtered)
5474 incline(s, PL_bufend);
5475 } while (PL_parser->in_pod);
5476 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = s;
5477 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
5478 PL_last_lop = PL_last_uni = NULL;
5479 if (CopLINE(PL_curcop) == 1) {
5480 while (s < PL_bufend && isSPACE(*s))
5481 s++;
5482 if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
5483 s++;
5484 d = NULL;
5485 if (!PL_in_eval) {
5486 if (*s == '#' && *(s+1) == '!')
5487 d = s + 2;
5488 #ifdef ALTERNATE_SHEBANG
5489 else {
5490 static char const as[] = ALTERNATE_SHEBANG;
5491 if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
5492 d = s + (sizeof(as) - 1);
5493 }
5494 #endif /* ALTERNATE_SHEBANG */
5495 }
5496 if (d) {
5497 char *ipath;
5498 char *ipathend;
5499
5500 while (isSPACE(*d))
5501 d++;
5502 ipath = d;
5503 while (*d && !isSPACE(*d))
5504 d++;
5505 ipathend = d;
5506
5507 #ifdef ARG_ZERO_IS_SCRIPT
5508 if (ipathend > ipath) {
5509 /*
5510 * HP-UX (at least) sets argv[0] to the script name,
5511 * which makes $^X incorrect. And Digital UNIX and Linux,
5512 * at least, set argv[0] to the basename of the Perl
5513 * interpreter. So, having found "#!", we'll set it right.
5514 */
5515 SV* copfilesv = CopFILESV(PL_curcop);
5516 if (copfilesv) {
5517 SV * const x =
5518 GvSV(gv_fetchpvs("\030", GV_ADD|GV_NOTQUAL,
5519 SVt_PV)); /* $^X */
5520 assert(SvPOK(x) || SvGMAGICAL(x));
5521 if (sv_eq(x, copfilesv)) {
5522 sv_setpvn(x, ipath, ipathend - ipath);
5523 SvSETMAGIC(x);
5524 }
5525 else {
5526 STRLEN blen;
5527 STRLEN llen;
5528 const char *bstart = SvPV_const(copfilesv, blen);
5529 const char * const lstart = SvPV_const(x, llen);
5530 if (llen < blen) {
5531 bstart += blen - llen;
5532 if (strnEQ(bstart, lstart, llen) && bstart[-1] == '/') {
5533 sv_setpvn(x, ipath, ipathend - ipath);
5534 SvSETMAGIC(x);
5535 }
5536 }
5537 }
5538 }
5539 else {
5540 /* Anything to do if no copfilesv? */
5541 }
5542 TAINT_NOT; /* $^X is always tainted, but that's OK */
5543 }
5544 #endif /* ARG_ZERO_IS_SCRIPT */
5545
5546 /*
5547 * Look for options.
5548 */
5549 d = instr(s,"perl -");
5550 if (!d) {
5551 d = instr(s,"perl");
5552 #if defined(DOSISH)
5553 /* avoid getting into infinite loops when shebang
5554 * line contains "Perl" rather than "perl" */
5555 if (!d) {
5556 for (d = ipathend-4; d >= ipath; --d) {
5557 if (isALPHA_FOLD_EQ(*d, 'p')
5558 && !ibcmp(d, "perl", 4))
5559 {
5560 break;
5561 }
5562 }
5563 if (d < ipath)
5564 d = NULL;
5565 }
5566 #endif
5567 }
5568 #ifdef ALTERNATE_SHEBANG
5569 /*
5570 * If the ALTERNATE_SHEBANG on this system starts with a
5571 * character that can be part of a Perl expression, then if
5572 * we see it but not "perl", we're probably looking at the
5573 * start of Perl code, not a request to hand off to some
5574 * other interpreter. Similarly, if "perl" is there, but
5575 * not in the first 'word' of the line, we assume the line
5576 * contains the start of the Perl program.
5577 */
5578 if (d && *s != '#') {
5579 const char *c = ipath;
5580 while (*c && !strchr("; \t\r\n\f\v#", *c))
5581 c++;
5582 if (c < d)
5583 d = NULL; /* "perl" not in first word; ignore */
5584 else
5585 *s = '#'; /* Don't try to parse shebang line */
5586 }
5587 #endif /* ALTERNATE_SHEBANG */
5588 if (!d
5589 && *s == '#'
5590 && ipathend > ipath
5591 && !PL_minus_c
5592 && !instr(s,"indir")
5593 && instr(PL_origargv[0],"perl"))
5594 {
5595 dVAR;
5596 char **newargv;
5597
5598 *ipathend = '\0';
5599 s = ipathend + 1;
5600 while (s < PL_bufend && isSPACE(*s))
5601 s++;
5602 if (s < PL_bufend) {
5603 Newx(newargv,PL_origargc+3,char*);
5604 newargv[1] = s;
5605 while (s < PL_bufend && !isSPACE(*s))
5606 s++;
5607 *s = '\0';
5608 Copy(PL_origargv+1, newargv+2, PL_origargc+1, char*);
5609 }
5610 else
5611 newargv = PL_origargv;
5612 newargv[0] = ipath;
5613 PERL_FPU_PRE_EXEC
5614 PerlProc_execv(ipath, EXEC_ARGV_CAST(newargv));
5615 PERL_FPU_POST_EXEC
5616 Perl_croak(aTHX_ "Can't exec %s", ipath);
5617 }
5618 if (d) {
5619 while (*d && !isSPACE(*d))
5620 d++;
5621 while (SPACE_OR_TAB(*d))
5622 d++;
5623
5624 if (*d++ == '-') {
5625 const bool switches_done = PL_doswitches;
5626 const U32 oldpdb = PL_perldb;
5627 const bool oldn = PL_minus_n;
5628 const bool oldp = PL_minus_p;
5629 const char *d1 = d;
5630
5631 do {
5632 bool baduni = FALSE;
5633 if (*d1 == 'C') {
5634 const char *d2 = d1 + 1;
5635 if (parse_unicode_opts((const char **)&d2)
5636 != PL_unicode)
5637 baduni = TRUE;
5638 }
5639 if (baduni || isALPHA_FOLD_EQ(*d1, 'M')) {
5640 const char * const m = d1;
5641 while (*d1 && !isSPACE(*d1))
5642 d1++;
5643 Perl_croak(aTHX_ "Too late for \"-%.*s\" option",
5644 (int)(d1 - m), m);
5645 }
5646 d1 = moreswitches(d1);
5647 } while (d1);
5648 if (PL_doswitches && !switches_done) {
5649 int argc = PL_origargc;
5650 char **argv = PL_origargv;
5651 do {
5652 argc--,argv++;
5653 } while (argc && argv[0][0] == '-' && argv[0][1]);
5654 init_argv_symbols(argc,argv);
5655 }
5656 if ( (PERLDB_LINE_OR_SAVESRC && !oldpdb)
5657 || ((PL_minus_n || PL_minus_p) && !(oldn || oldp)))
5658 /* if we have already added "LINE: while (<>) {",
5659 we must not do it again */
5660 {
5661 SvPVCLEAR(PL_linestr);
5662 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
5663 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
5664 PL_last_lop = PL_last_uni = NULL;
5665 PL_preambled = FALSE;
5666 if (PERLDB_LINE_OR_SAVESRC)
5667 (void)gv_fetchfile(PL_origfilename);
5668 goto retry;
5669 }
5670 }
5671 }
5672 }
5673 }
5674 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
5675 PL_lex_state = LEX_FORMLINE;
5676 force_next(FORMRBRACK);
5677 TOKEN(';');
5678 }
5679 goto retry;
5680 case '\r':
5681 #ifdef PERL_STRICT_CR
5682 Perl_warn(aTHX_ "Illegal character \\%03o (carriage return)", '\r');
5683 Perl_croak(aTHX_
5684 "\t(Maybe you didn't strip carriage returns after a network transfer?)\n");
5685 #endif
5686 case ' ': case '\t': case '\f': case '\v':
5687 s++;
5688 goto retry;
5689 case '#':
5690 case '\n':
5691 if (PL_lex_state != LEX_NORMAL
5692 || (PL_in_eval && !PL_rsfp && !PL_parser->filtered))
5693 {
5694 const bool in_comment = *s == '#';
5695 if (*s == '#' && s == PL_linestart && PL_in_eval
5696 && !PL_rsfp && !PL_parser->filtered) {
5697 /* handle eval qq[#line 1 "foo"\n ...] */
5698 CopLINE_dec(PL_curcop);
5699 incline(s, PL_bufend);
5700 }
5701 d = s;
5702 while (d < PL_bufend && *d != '\n')
5703 d++;
5704 if (d < PL_bufend)
5705 d++;
5706 s = d;
5707 if (in_comment && d == PL_bufend
5708 && PL_lex_state == LEX_INTERPNORMAL
5709 && PL_lex_inwhat == OP_SUBST && PL_lex_repl == PL_linestr
5710 && SvEVALED(PL_lex_repl) && d[-1] == '}') s--;
5711 else
5712 incline(s, PL_bufend);
5713 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
5714 PL_lex_state = LEX_FORMLINE;
5715 force_next(FORMRBRACK);
5716 TOKEN(';');
5717 }
5718 }
5719 else {
5720 while (s < PL_bufend && *s != '\n')
5721 s++;
5722 if (s < PL_bufend)
5723 {
5724 s++;
5725 if (s < PL_bufend)
5726 incline(s, PL_bufend);
5727 }
5728 }
5729 goto retry;
5730 case '-':
5731 if (s[1] && isALPHA(s[1]) && !isWORDCHAR(s[2])) {
5732 I32 ftst = 0;
5733 char tmp;
5734
5735 s++;
5736 PL_bufptr = s;
5737 tmp = *s++;
5738
5739 while (s < PL_bufend && SPACE_OR_TAB(*s))
5740 s++;
5741
5742 if (memBEGINs(s, (STRLEN) (PL_bufend - s), "=>")) {
5743 s = force_word(PL_bufptr,BAREWORD,FALSE,FALSE);
5744 DEBUG_T( { printbuf("### Saw unary minus before =>, forcing word %s\n", s); } );
5745 OPERATOR('-'); /* unary minus */
5746 }
5747 switch (tmp) {
5748 case 'r': ftst = OP_FTEREAD; break;
5749 case 'w': ftst = OP_FTEWRITE; break;
5750 case 'x': ftst = OP_FTEEXEC; break;
5751 case 'o': ftst = OP_FTEOWNED; break;
5752 case 'R': ftst = OP_FTRREAD; break;
5753 case 'W': ftst = OP_FTRWRITE; break;
5754 case 'X': ftst = OP_FTREXEC; break;
5755 case 'O': ftst = OP_FTROWNED; break;
5756 case 'e': ftst = OP_FTIS; break;
5757 case 'z': ftst = OP_FTZERO; break;
5758 case 's': ftst = OP_FTSIZE; break;
5759 case 'f': ftst = OP_FTFILE; break;
5760 case 'd': ftst = OP_FTDIR; break;
5761 case 'l': ftst = OP_FTLINK; break;
5762 case 'p': ftst = OP_FTPIPE; break;
5763 case 'S': ftst = OP_FTSOCK; break;
5764 case 'u': ftst = OP_FTSUID; break;
5765 case 'g': ftst = OP_FTSGID; break;
5766 case 'k': ftst = OP_FTSVTX; break;
5767 case 'b': ftst = OP_FTBLK; break;
5768 case 'c': ftst = OP_FTCHR; break;
5769 case 't': ftst = OP_FTTTY; break;
5770 case 'T': ftst = OP_FTTEXT; break;
5771 case 'B': ftst = OP_FTBINARY; break;
5772 case 'M': case 'A': case 'C':
5773 gv_fetchpvs("\024", GV_ADD|GV_NOTQUAL, SVt_PV);
5774 switch (tmp) {
5775 case 'M': ftst = OP_FTMTIME; break;
5776 case 'A': ftst = OP_FTATIME; break;
5777 case 'C': ftst = OP_FTCTIME; break;
5778 default: break;
5779 }
5780 break;
5781 default:
5782 break;
5783 }
5784 if (ftst) {
5785 PL_last_uni = PL_oldbufptr;
5786 PL_last_lop_op = (OPCODE)ftst;
5787 DEBUG_T( { PerlIO_printf(Perl_debug_log,
5788 "### Saw file test %c\n", (int)tmp);
5789 } );
5790 FTST(ftst);
5791 }
5792 else {
5793 /* Assume it was a minus followed by a one-letter named
5794 * subroutine call (or a -bareword), then. */
5795 DEBUG_T( { PerlIO_printf(Perl_debug_log,
5796 "### '-%c' looked like a file test but was not\n",
5797 (int) tmp);
5798 } );
5799 s = --PL_bufptr;
5800 }
5801 }
5802 {
5803 const char tmp = *s++;
5804 if (*s == tmp) {
5805 s++;
5806 if (PL_expect == XOPERATOR)
5807 TERM(POSTDEC);
5808 else
5809 OPERATOR(PREDEC);
5810 }
5811 else if (*s == '>') {
5812 s++;
5813 s = skipspace(s);
5814 if (((*s == '$' || *s == '&') && s[1] == '*')
5815 ||(*s == '$' && s[1] == '#' && s[2] == '*')
5816 ||((*s == '@' || *s == '%') && strchr("*[{", s[1]))
5817 ||(*s == '*' && (s[1] == '*' || s[1] == '{'))
5818 )
5819 {
5820 PL_expect = XPOSTDEREF;
5821 TOKEN(ARROW);
5822 }
5823 if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
5824 s = force_word(s,METHOD,FALSE,TRUE);
5825 TOKEN(ARROW);
5826 }
5827 else if (*s == '$')
5828 OPERATOR(ARROW);
5829 else
5830 TERM(ARROW);
5831 }
5832 if (PL_expect == XOPERATOR) {
5833 if (*s == '='
5834 && !PL_lex_allbrackets
5835 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5836 {
5837 s--;
5838 TOKEN(0);
5839 }
5840 Aop(OP_SUBTRACT);
5841 }
5842 else {
5843 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
5844 check_uni();
5845 OPERATOR('-'); /* unary minus */
5846 }
5847 }
5848
5849 case '+':
5850 {
5851 const char tmp = *s++;
5852 if (*s == tmp) {
5853 s++;
5854 if (PL_expect == XOPERATOR)
5855 TERM(POSTINC);
5856 else
5857 OPERATOR(PREINC);
5858 }
5859 if (PL_expect == XOPERATOR) {
5860 if (*s == '='
5861 && !PL_lex_allbrackets
5862 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5863 {
5864 s--;
5865 TOKEN(0);
5866 }
5867 Aop(OP_ADD);
5868 }
5869 else {
5870 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
5871 check_uni();
5872 OPERATOR('+');
5873 }
5874 }
5875
5876 case '*':
5877 if (PL_expect == XPOSTDEREF) POSTDEREF('*');
5878 if (PL_expect != XOPERATOR) {
5879 s = scan_ident(s, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
5880 PL_expect = XOPERATOR;
5881 force_ident(PL_tokenbuf, '*');
5882 if (!*PL_tokenbuf)
5883 PREREF('*');
5884 TERM('*');
5885 }
5886 s++;
5887 if (*s == '*') {
5888 s++;
5889 if (*s == '=' && !PL_lex_allbrackets
5890 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5891 {
5892 s -= 2;
5893 TOKEN(0);
5894 }
5895 PWop(OP_POW);
5896 }
5897 if (*s == '='
5898 && !PL_lex_allbrackets
5899 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5900 {
5901 s--;
5902 TOKEN(0);
5903 }
5904 PL_parser->saw_infix_sigil = 1;
5905 Mop(OP_MULTIPLY);
5906
5907 case '%':
5908 {
5909 if (PL_expect == XOPERATOR) {
5910 if (s[1] == '='
5911 && !PL_lex_allbrackets
5912 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5913 {
5914 TOKEN(0);
5915 }
5916 ++s;
5917 PL_parser->saw_infix_sigil = 1;
5918 Mop(OP_MODULO);
5919 }
5920 else if (PL_expect == XPOSTDEREF) POSTDEREF('%');
5921 PL_tokenbuf[0] = '%';
5922 s = scan_ident(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
5923 pl_yylval.ival = 0;
5924 if (!PL_tokenbuf[1]) {
5925 PREREF('%');
5926 }
5927 if ( (PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
5928 && intuit_more(s, PL_bufend)) {
5929 if (*s == '[')
5930 PL_tokenbuf[0] = '@';
5931 }
5932 PL_expect = XOPERATOR;
5933 force_ident_maybe_lex('%');
5934 TERM('%');
5935 }
5936 case '^':
5937 d = s;
5938 bof = FEATURE_BITWISE_IS_ENABLED;
5939 if (bof && s[1] == '.')
5940 s++;
5941 if (!PL_lex_allbrackets && PL_lex_fakeeof >=
5942 (s[1] == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_BITWISE))
5943 {
5944 s = d;
5945 TOKEN(0);
5946 }
5947 s++;
5948 BOop(bof ? d == s-2 ? OP_SBIT_XOR : OP_NBIT_XOR : OP_BIT_XOR);
5949 case '[':
5950 if (PL_lex_brackets > 100)
5951 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
5952 PL_lex_brackstack[PL_lex_brackets++] = 0;
5953 PL_lex_allbrackets++;
5954 {
5955 const char tmp = *s++;
5956 OPERATOR(tmp);
5957 }
5958 case '~':
5959 if (s[1] == '~'
5960 && (PL_expect == XOPERATOR || PL_expect == XTERMORDORDOR))
5961 {
5962 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
5963 TOKEN(0);
5964 s += 2;
5965 Perl_ck_warner_d(aTHX_
5966 packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
5967 "Smartmatch is experimental");
5968 Eop(OP_SMARTMATCH);
5969 }
5970 s++;
5971 if ((bof = FEATURE_BITWISE_IS_ENABLED) && *s == '.') {
5972 s++;
5973 BCop(OP_SCOMPLEMENT);
5974 }
5975 BCop(bof ? OP_NCOMPLEMENT : OP_COMPLEMENT);
5976 case ',':
5977 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMMA)
5978 TOKEN(0);
5979 s++;
5980 OPERATOR(',');
5981 case ':':
5982 if (s[1] == ':') {
5983 len = 0;
5984 goto just_a_word_zero_gv;
5985 }
5986 s++;
5987 {
5988 OP *attrs;
5989
5990 switch (PL_expect) {
5991 case XOPERATOR:
5992 if (!PL_in_my || PL_lex_state != LEX_NORMAL)
5993 break;
5994 PL_bufptr = s; /* update in case we back off */
5995 if (*s == '=') {
5996 Perl_croak(aTHX_
5997 "Use of := for an empty attribute list is not allowed");
5998 }
5999 goto grabattrs;
6000 case XATTRBLOCK:
6001 PL_expect = XBLOCK;
6002 goto grabattrs;
6003 case XATTRTERM:
6004 PL_expect = XTERMBLOCK;
6005 grabattrs:
6006 /* NB: as well as parsing normal attributes, we also end up
6007 * here if there is something looking like attributes
6008 * following a signature (which is illegal, but used to be
6009 * legal in 5.20..5.26). If the latter, we still parse the
6010 * attributes so that error messages(s) are less confusing,
6011 * but ignore them (parser->sig_seen).
6012 */
6013 s = skipspace(s);
6014 attrs = NULL;
6015 while (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
6016 bool sig = PL_parser->sig_seen;
6017 I32 tmp;
6018 SV *sv;
6019 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
6020 if (isLOWER(*s) && (tmp = keyword(PL_tokenbuf, len, 0))) {
6021 if (tmp < 0) tmp = -tmp;
6022 switch (tmp) {
6023 case KEY_or:
6024 case KEY_and:
6025 case KEY_for:
6026 case KEY_foreach:
6027 case KEY_unless:
6028 case KEY_if:
6029 case KEY_while:
6030 case KEY_until:
6031 goto got_attrs;
6032 default:
6033 break;
6034 }
6035 }
6036 sv = newSVpvn_flags(s, len, UTF ? SVf_UTF8 : 0);
6037 if (*d == '(') {
6038 d = scan_str(d,TRUE,TRUE,FALSE,NULL);
6039 if (!d) {
6040 if (attrs)
6041 op_free(attrs);
6042 sv_free(sv);
6043 Perl_croak(aTHX_ "Unterminated attribute parameter in attribute list");
6044 }
6045 COPLINE_SET_FROM_MULTI_END;
6046 }
6047 if (PL_lex_stuff) {
6048 sv_catsv(sv, PL_lex_stuff);
6049 attrs = op_append_elem(OP_LIST, attrs,
6050 newSVOP(OP_CONST, 0, sv));
6051 SvREFCNT_dec_NN(PL_lex_stuff);
6052 PL_lex_stuff = NULL;
6053 }
6054 else {
6055 /* NOTE: any CV attrs applied here need to be part of
6056 the CVf_BUILTIN_ATTRS define in cv.h! */
6057 if (!PL_in_my && memEQs(SvPVX(sv), len, "lvalue")) {
6058 sv_free(sv);
6059 if (!sig)
6060 CvLVALUE_on(PL_compcv);
6061 }
6062 else if (!PL_in_my && memEQs(SvPVX(sv), len, "method")) {
6063 sv_free(sv);
6064 if (!sig)
6065 CvMETHOD_on(PL_compcv);
6066 }
6067 else if (!PL_in_my && memEQs(SvPVX(sv), len, "const"))
6068 {
6069 sv_free(sv);
6070 if (!sig) {
6071 Perl_ck_warner_d(aTHX_
6072 packWARN(WARN_EXPERIMENTAL__CONST_ATTR),
6073 ":const is experimental"
6074 );
6075 CvANONCONST_on(PL_compcv);
6076 if (!CvANON(PL_compcv))
6077 yyerror(":const is not permitted on named "
6078 "subroutines");
6079 }
6080 }
6081 /* After we've set the flags, it could be argued that
6082 we don't need to do the attributes.pm-based setting
6083 process, and shouldn't bother appending recognized
6084 flags. To experiment with that, uncomment the
6085 following "else". (Note that's already been
6086 uncommented. That keeps the above-applied built-in
6087 attributes from being intercepted (and possibly
6088 rejected) by a package's attribute routines, but is
6089 justified by the performance win for the common case
6090 of applying only built-in attributes.) */
6091 else
6092 attrs = op_append_elem(OP_LIST, attrs,
6093 newSVOP(OP_CONST, 0,
6094 sv));
6095 }
6096 s = skipspace(d);
6097 if (*s == ':' && s[1] != ':')
6098 s = skipspace(s+1);
6099 else if (s == d)
6100 break; /* require real whitespace or :'s */
6101 /* XXX losing whitespace on sequential attributes here */
6102 }
6103 {
6104 if (*s != ';'
6105 && *s != '}'
6106 && !(PL_expect == XOPERATOR
6107 ? (*s == '=' || *s == ')')
6108 : (*s == '{' || *s == '(')))
6109 {
6110 const char q = ((*s == '\'') ? '"' : '\'');
6111 /* If here for an expression, and parsed no attrs, back
6112 off. */
6113 if (PL_expect == XOPERATOR && !attrs) {
6114 s = PL_bufptr;
6115 break;
6116 }
6117 /* MUST advance bufptr here to avoid bogus "at end of line"
6118 context messages from yyerror().
6119 */
6120 PL_bufptr = s;
6121 yyerror( (const char *)
6122 (*s
6123 ? Perl_form(aTHX_ "Invalid separator character "
6124 "%c%c%c in attribute list", q, *s, q)
6125 : "Unterminated attribute list" ) );
6126 if (attrs)
6127 op_free(attrs);
6128 OPERATOR(':');
6129 }
6130 }
6131 got_attrs:
6132 if (PL_parser->sig_seen) {
6133 /* see comment about about sig_seen and parser error
6134 * handling */
6135 if (attrs)
6136 op_free(attrs);
6137 Perl_croak(aTHX_ "Subroutine attributes must come "
6138 "before the signature");
6139 }
6140 if (attrs) {
6141 NEXTVAL_NEXTTOKE.opval = attrs;
6142 force_next(THING);
6143 }
6144 TOKEN(COLONATTR);
6145 }
6146 }
6147 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_CLOSING) {
6148 s--;
6149 TOKEN(0);
6150 }
6151 PL_lex_allbrackets--;
6152 OPERATOR(':');
6153 case '(':
6154 s++;
6155 if (PL_last_lop == PL_oldoldbufptr || PL_last_uni == PL_oldoldbufptr)
6156 PL_oldbufptr = PL_oldoldbufptr; /* allow print(STDOUT 123) */
6157 else
6158 PL_expect = XTERM;
6159 s = skipspace(s);
6160 PL_lex_allbrackets++;
6161 TOKEN('(');
6162 case ';':
6163 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
6164 TOKEN(0);
6165 CLINE;
6166 s++;
6167 PL_expect = XSTATE;
6168 TOKEN(';');
6169 case ')':
6170 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_CLOSING)
6171 TOKEN(0);
6172 s++;
6173 PL_lex_allbrackets--;
6174 s = skipspace(s);
6175 if (*s == '{')
6176 PREBLOCK(')');
6177 TERM(')');
6178 case ']':
6179 if (PL_lex_brackets && PL_lex_brackstack[PL_lex_brackets-1] == XFAKEEOF)
6180 TOKEN(0);
6181 s++;
6182 if (PL_lex_brackets <= 0)
6183 /* diag_listed_as: Unmatched right %s bracket */
6184 yyerror("Unmatched right square bracket");
6185 else
6186 --PL_lex_brackets;
6187 PL_lex_allbrackets--;
6188 if (PL_lex_state == LEX_INTERPNORMAL) {
6189 if (PL_lex_brackets == 0) {
6190 if (*s == '-' && s[1] == '>')
6191 PL_lex_state = LEX_INTERPENDMAYBE;
6192 else if (*s != '[' && *s != '{')
6193 PL_lex_state = LEX_INTERPEND;
6194 }
6195 }
6196 TERM(']');
6197 case '{':
6198 s++;
6199 leftbracket:
6200 if (PL_lex_brackets > 100) {
6201 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
6202 }
6203 switch (PL_expect) {
6204 case XTERM:
6205 case XTERMORDORDOR:
6206 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
6207 PL_lex_allbrackets++;
6208 OPERATOR(HASHBRACK);
6209 case XOPERATOR:
6210 while (s < PL_bufend && SPACE_OR_TAB(*s))
6211 s++;
6212 d = s;
6213 PL_tokenbuf[0] = '\0';
6214 if (d < PL_bufend && *d == '-') {
6215 PL_tokenbuf[0] = '-';
6216 d++;
6217 while (d < PL_bufend && SPACE_OR_TAB(*d))
6218 d++;
6219 }
6220 if (d < PL_bufend && isIDFIRST_lazy_if_safe(d, PL_bufend, UTF)) {
6221 d = scan_word(d, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
6222 FALSE, &len);
6223 while (d < PL_bufend && SPACE_OR_TAB(*d))
6224 d++;
6225 if (*d == '}') {
6226 const char minus = (PL_tokenbuf[0] == '-');
6227 s = force_word(s + minus, BAREWORD, FALSE, TRUE);
6228 if (minus)
6229 force_next('-');
6230 }
6231 }
6232 /* FALLTHROUGH */
6233 case XATTRTERM:
6234 case XTERMBLOCK:
6235 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
6236 PL_lex_allbrackets++;
6237 PL_expect = XSTATE;
6238 break;
6239 case XATTRBLOCK:
6240 case XBLOCK:
6241 PL_lex_brackstack[PL_lex_brackets++] = XSTATE;
6242 PL_lex_allbrackets++;
6243 PL_expect = XSTATE;
6244 break;
6245 case XBLOCKTERM:
6246 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
6247 PL_lex_allbrackets++;
6248 PL_expect = XSTATE;
6249 break;
6250 default: {
6251 const char *t;
6252 if (PL_oldoldbufptr == PL_last_lop)
6253 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
6254 else
6255 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
6256 PL_lex_allbrackets++;
6257 s = skipspace(s);
6258 if (*s == '}') {
6259 if (PL_expect == XREF && PL_lex_state == LEX_INTERPNORMAL) {
6260 PL_expect = XTERM;
6261 /* This hack is to get the ${} in the message. */
6262 PL_bufptr = s+1;
6263 yyerror("syntax error");
6264 break;
6265 }
6266 OPERATOR(HASHBRACK);
6267 }
6268 if (PL_expect == XREF && PL_oldoldbufptr != PL_last_lop) {
6269 /* ${...} or @{...} etc., but not print {...}
6270 * Skip the disambiguation and treat this as a block.
6271 */
6272 goto block_expectation;
6273 }
6274 /* This hack serves to disambiguate a pair of curlies
6275 * as being a block or an anon hash. Normally, expectation
6276 * determines that, but in cases where we're not in a
6277 * position to expect anything in particular (like inside
6278 * eval"") we have to resolve the ambiguity. This code
6279 * covers the case where the first term in the curlies is a
6280 * quoted string. Most other cases need to be explicitly
6281 * disambiguated by prepending a "+" before the opening
6282 * curly in order to force resolution as an anon hash.
6283 *
6284 * XXX should probably propagate the outer expectation
6285 * into eval"" to rely less on this hack, but that could
6286 * potentially break current behavior of eval"".
6287 * GSAR 97-07-21
6288 */
6289 t = s;
6290 if (*s == '\'' || *s == '"' || *s == '`') {
6291 /* common case: get past first string, handling escapes */
6292 for (t++; t < PL_bufend && *t != *s;)
6293 if (*t++ == '\\')
6294 t++;
6295 t++;
6296 }
6297 else if (*s == 'q') {
6298 if (++t < PL_bufend
6299 && (!isWORDCHAR(*t)
6300 || ((*t == 'q' || *t == 'x') && ++t < PL_bufend
6301 && !isWORDCHAR(*t))))
6302 {
6303 /* skip q//-like construct */
6304 const char *tmps;
6305 char open, close, term;
6306 I32 brackets = 1;
6307
6308 while (t < PL_bufend && isSPACE(*t))
6309 t++;
6310 /* check for q => */
6311 if (t+1 < PL_bufend && t[0] == '=' && t[1] == '>') {
6312 OPERATOR(HASHBRACK);
6313 }
6314 term = *t;
6315 open = term;
6316 if (term && (tmps = strchr("([{< )]}> )]}>",term)))
6317 term = tmps[5];
6318 close = term;
6319 if (open == close)
6320 for (t++; t < PL_bufend; t++) {
6321 if (*t == '\\' && t+1 < PL_bufend && open != '\\')
6322 t++;
6323 else if (*t == open)
6324 break;
6325 }
6326 else {
6327 for (t++; t < PL_bufend; t++) {
6328 if (*t == '\\' && t+1 < PL_bufend)
6329 t++;
6330 else if (*t == close && --brackets <= 0)
6331 break;
6332 else if (*t == open)
6333 brackets++;
6334 }
6335 }
6336 t++;
6337 }
6338 else
6339 /* skip plain q word */
6340 while ( t < PL_bufend
6341 && isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF))
6342 {
6343 t += UTF ? UTF8SKIP(t) : 1;
6344 }
6345 }
6346 else if (isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF)) {
6347 t += UTF ? UTF8SKIP(t) : 1;
6348 while ( t < PL_bufend
6349 && isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF))
6350 {
6351 t += UTF ? UTF8SKIP(t) : 1;
6352 }
6353 }
6354 while (t < PL_bufend && isSPACE(*t))
6355 t++;
6356 /* if comma follows first term, call it an anon hash */
6357 /* XXX it could be a comma expression with loop modifiers */
6358 if (t < PL_bufend && ((*t == ',' && (*s == 'q' || !isLOWER(*s)))
6359 || (*t == '=' && t[1] == '>')))
6360 OPERATOR(HASHBRACK);
6361 if (PL_expect == XREF)
6362 {
6363 block_expectation:
6364 /* If there is an opening brace or 'sub:', treat it
6365 as a term to make ${{...}}{k} and &{sub:attr...}
6366 dwim. Otherwise, treat it as a statement, so
6367 map {no strict; ...} works.
6368 */
6369 s = skipspace(s);
6370 if (*s == '{') {
6371 PL_expect = XTERM;
6372 break;
6373 }
6374 if (memBEGINs(s, (STRLEN) (PL_bufend - s), "sub")) {
6375 PL_bufptr = s;
6376 d = s + 3;
6377 d = skipspace(d);
6378 s = PL_bufptr;
6379 if (*d == ':') {
6380 PL_expect = XTERM;
6381 break;
6382 }
6383 }
6384 PL_expect = XSTATE;
6385 }
6386 else {
6387 PL_lex_brackstack[PL_lex_brackets-1] = XSTATE;
6388 PL_expect = XSTATE;
6389 }
6390 }
6391 break;
6392 }
6393 pl_yylval.ival = CopLINE(PL_curcop);
6394 PL_copline = NOLINE; /* invalidate current command line number */
6395 TOKEN(formbrack ? '=' : '{');
6396 case '}':
6397 if (PL_lex_brackets && PL_lex_brackstack[PL_lex_brackets-1] == XFAKEEOF)
6398 TOKEN(0);
6399 rightbracket:
6400 assert(s != PL_bufend);
6401 s++;
6402 if (PL_lex_brackets <= 0)
6403 /* diag_listed_as: Unmatched right %s bracket */
6404 yyerror("Unmatched right curly bracket");
6405 else
6406 PL_expect = (expectation)PL_lex_brackstack[--PL_lex_brackets];
6407 PL_lex_allbrackets--;
6408 if (PL_lex_state == LEX_INTERPNORMAL) {
6409 if (PL_lex_brackets == 0) {
6410 if (PL_expect & XFAKEBRACK) {
6411 PL_expect &= XENUMMASK;
6412 PL_lex_state = LEX_INTERPEND;
6413 PL_bufptr = s;
6414 return yylex(); /* ignore fake brackets */
6415 }
6416 if (PL_lex_inwhat == OP_SUBST && PL_lex_repl == PL_linestr
6417 && SvEVALED(PL_lex_repl))
6418 PL_lex_state = LEX_INTERPEND;
6419 else if (*s == '-' && s[1] == '>')
6420 PL_lex_state = LEX_INTERPENDMAYBE;
6421 else if (*s != '[' && *s != '{')
6422 PL_lex_state = LEX_INTERPEND;
6423 }
6424 }
6425 if (PL_expect & XFAKEBRACK) {
6426 PL_expect &= XENUMMASK;
6427 PL_bufptr = s;
6428 return yylex(); /* ignore fake brackets */
6429 }
6430 force_next(formbrack ? '.' : '}');
6431 if (formbrack) LEAVE_with_name("lex_format");
6432 if (formbrack == 2) { /* means . where arguments were expected */
6433 force_next(';');
6434 TOKEN(FORMRBRACK);
6435 }
6436 TOKEN(';');
6437 case '&':
6438 if (PL_expect == XPOSTDEREF) POSTDEREF('&');
6439 s++;
6440 if (*s++ == '&') {
6441 if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6442 (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_LOGIC)) {
6443 s -= 2;
6444 TOKEN(0);
6445 }
6446 AOPERATOR(ANDAND);
6447 }
6448 s--;
6449 if (PL_expect == XOPERATOR) {
6450 if ( PL_bufptr == PL_linestart
6451 && ckWARN(WARN_SEMICOLON)
6452 && isIDFIRST_lazy_if_safe(s, PL_bufend, UTF))
6453 {
6454 CopLINE_dec(PL_curcop);
6455 Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
6456 CopLINE_inc(PL_curcop);
6457 }
6458 d = s;
6459 if ((bof = FEATURE_BITWISE_IS_ENABLED) && *s == '.')
6460 s++;
6461 if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6462 (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_BITWISE)) {
6463 s = d;
6464 s--;
6465 TOKEN(0);
6466 }
6467 if (d == s) {
6468 PL_parser->saw_infix_sigil = 1;
6469 BAop(bof ? OP_NBIT_AND : OP_BIT_AND);
6470 }
6471 else
6472 BAop(OP_SBIT_AND);
6473 }
6474
6475 PL_tokenbuf[0] = '&';
6476 s = scan_ident(s - 1, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, TRUE);
6477 pl_yylval.ival = (OPpENTERSUB_AMPER<<8);
6478 if (PL_tokenbuf[1]) {
6479 force_ident_maybe_lex('&');
6480 }
6481 else
6482 PREREF('&');
6483 TERM('&');
6484
6485 case '|':
6486 s++;
6487 if (*s++ == '|') {
6488 if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6489 (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_LOGIC)) {
6490 s -= 2;
6491 TOKEN(0);
6492 }
6493 AOPERATOR(OROR);
6494 }
6495 s--;
6496 d = s;
6497 if ((bof = FEATURE_BITWISE_IS_ENABLED) && *s == '.')
6498 s++;
6499 if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6500 (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_BITWISE)) {
6501 s = d - 1;
6502 TOKEN(0);
6503 }
6504 BOop(bof ? s == d ? OP_NBIT_OR : OP_SBIT_OR : OP_BIT_OR);
6505 case '=':
6506 s++;
6507 {
6508 const char tmp = *s++;
6509 if (tmp == '=') {
6510 if ( (s == PL_linestart+2 || s[-3] == '\n')
6511 && memBEGINs(s, (STRLEN) (PL_bufend - s), "====="))
6512 {
6513 s = vcs_conflict_marker(s + 5);
6514 goto retry;
6515 }
6516 if (!PL_lex_allbrackets
6517 && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6518 {
6519 s -= 2;
6520 TOKEN(0);
6521 }
6522 Eop(OP_EQ);
6523 }
6524 if (tmp == '>') {
6525 if (!PL_lex_allbrackets
6526 && PL_lex_fakeeof >= LEX_FAKEEOF_COMMA)
6527 {
6528 s -= 2;
6529 TOKEN(0);
6530 }
6531 OPERATOR(',');
6532 }
6533 if (tmp == '~')
6534 PMop(OP_MATCH);
6535 if (tmp && isSPACE(*s) && ckWARN(WARN_SYNTAX)
6536 && strchr("+-*/%.^&|<",tmp))
6537 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6538 "Reversed %c= operator",(int)tmp);
6539 s--;
6540 if (PL_expect == XSTATE
6541 && isALPHA(tmp)
6542 && (s == PL_linestart+1 || s[-2] == '\n') )
6543 {
6544 if ( (PL_in_eval && !PL_rsfp && !PL_parser->filtered)
6545 || PL_lex_state != LEX_NORMAL)
6546 {
6547 d = PL_bufend;
6548 while (s < d) {
6549 if (*s++ == '\n') {
6550 incline(s, PL_bufend);
6551 if (memBEGINs(s, (STRLEN) (PL_bufend - s), "=cut"))
6552 {
6553 s = (char *) memchr(s,'\n', d - s);
6554 if (s)
6555 s++;
6556 else
6557 s = d;
6558 incline(s, PL_bufend);
6559 goto retry;
6560 }
6561 }
6562 }
6563 goto retry;
6564 }
6565 s = PL_bufend;
6566 PL_parser->in_pod = 1;
6567 goto retry;
6568 }
6569 }
6570 if (PL_expect == XBLOCK) {
6571 const char *t = s;
6572 #ifdef PERL_STRICT_CR
6573 while (SPACE_OR_TAB(*t))
6574 #else
6575 while (SPACE_OR_TAB(*t) || *t == '\r')
6576 #endif
6577 t++;
6578 if (*t == '\n' || *t == '#') {
6579 formbrack = 1;
6580 ENTER_with_name("lex_format");
6581 SAVEI8(PL_parser->form_lex_state);
6582 SAVEI32(PL_lex_formbrack);
6583 PL_parser->form_lex_state = PL_lex_state;
6584 PL_lex_formbrack = PL_lex_brackets + 1;
6585 PL_parser->sub_error_count = PL_error_count;
6586 goto leftbracket;
6587 }
6588 }
6589 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN) {
6590 s--;
6591 TOKEN(0);
6592 }
6593 pl_yylval.ival = 0;
6594 OPERATOR(ASSIGNOP);
6595 case '!':
6596 s++;
6597 {
6598 const char tmp = *s++;
6599 if (tmp == '=') {
6600 /* was this !=~ where !~ was meant?
6601 * warn on m:!=~\s+([/?]|[msy]\W|tr\W): */
6602
6603 if (*s == '~' && ckWARN(WARN_SYNTAX)) {
6604 const char *t = s+1;
6605
6606 while (t < PL_bufend && isSPACE(*t))
6607 ++t;
6608
6609 if (*t == '/' || *t == '?'
6610 || ((*t == 'm' || *t == 's' || *t == 'y')
6611 && !isWORDCHAR(t[1]))
6612 || (*t == 't' && t[1] == 'r' && !isWORDCHAR(t[2])))
6613 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6614 "!=~ should be !~");
6615 }
6616 if (!PL_lex_allbrackets
6617 && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6618 {
6619 s -= 2;
6620 TOKEN(0);
6621 }
6622 Eop(OP_NE);
6623 }
6624 if (tmp == '~')
6625 PMop(OP_NOT);
6626 }
6627 s--;
6628 OPERATOR('!');
6629 case '<':
6630 if (PL_expect != XOPERATOR) {
6631 if (s[1] != '<' && !memchr(s,'>', PL_bufend - s))
6632 check_uni();
6633 if (s[1] == '<' && s[2] != '>') {
6634 if ( (s == PL_linestart || s[-1] == '\n')
6635 && memBEGINs(s+2, (STRLEN) (PL_bufend - (s+2)), "<<<<<"))
6636 {
6637 s = vcs_conflict_marker(s + 7);
6638 goto retry;
6639 }
6640 s = scan_heredoc(s);
6641 }
6642 else
6643 s = scan_inputsymbol(s);
6644 PL_expect = XOPERATOR;
6645 TOKEN(sublex_start());
6646 }
6647 s++;
6648 {
6649 char tmp = *s++;
6650 if (tmp == '<') {
6651 if ( (s == PL_linestart+2 || s[-3] == '\n')
6652 && memBEGINs(s, (STRLEN) (PL_bufend - s), "<<<<<"))
6653 {
6654 s = vcs_conflict_marker(s + 5);
6655 goto retry;
6656 }
6657 if (*s == '=' && !PL_lex_allbrackets
6658 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
6659 {
6660 s -= 2;
6661 TOKEN(0);
6662 }
6663 SHop(OP_LEFT_SHIFT);
6664 }
6665 if (tmp == '=') {
6666 tmp = *s++;
6667 if (tmp == '>') {
6668 if (!PL_lex_allbrackets
6669 && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6670 {
6671 s -= 3;
6672 TOKEN(0);
6673 }
6674 Eop(OP_NCMP);
6675 }
6676 s--;
6677 if (!PL_lex_allbrackets
6678 && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6679 {
6680 s -= 2;
6681 TOKEN(0);
6682 }
6683 Rop(OP_LE);
6684 }
6685 }
6686 s--;
6687 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE) {
6688 s--;
6689 TOKEN(0);
6690 }
6691 Rop(OP_LT);
6692 case '>':
6693 s++;
6694 {
6695 const char tmp = *s++;
6696 if (tmp == '>') {
6697 if ( (s == PL_linestart+2 || s[-3] == '\n')
6698 && memBEGINs(s, (STRLEN) (PL_bufend - s), ">>>>>"))
6699 {
6700 s = vcs_conflict_marker(s + 5);
6701 goto retry;
6702 }
6703 if (*s == '=' && !PL_lex_allbrackets
6704 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
6705 {
6706 s -= 2;
6707 TOKEN(0);
6708 }
6709 SHop(OP_RIGHT_SHIFT);
6710 }
6711 else if (tmp == '=') {
6712 if (!PL_lex_allbrackets
6713 && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6714 {
6715 s -= 2;
6716 TOKEN(0);
6717 }
6718 Rop(OP_GE);
6719 }
6720 }
6721 s--;
6722 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE) {
6723 s--;
6724 TOKEN(0);
6725 }
6726 Rop(OP_GT);
6727
6728 case '$':
6729 CLINE;
6730
6731 if (PL_expect == XPOSTDEREF) {
6732 if (s[1] == '#') {
6733 s++;
6734 POSTDEREF(DOLSHARP);
6735 }
6736 POSTDEREF('$');
6737 }
6738
6739 if ( s[1] == '#'
6740 && ( isIDFIRST_lazy_if_safe(s+2, PL_bufend, UTF)
6741 || strchr("{$:+-@", s[2])))
6742 {
6743 PL_tokenbuf[0] = '@';
6744 s = scan_ident(s + 1, PL_tokenbuf + 1,
6745 sizeof PL_tokenbuf - 1, FALSE);
6746 if (PL_expect == XOPERATOR) {
6747 d = s;
6748 if (PL_bufptr > s) {
6749 d = PL_bufptr-1;
6750 PL_bufptr = PL_oldbufptr;
6751 }
6752 no_op("Array length", d);
6753 }
6754 if (!PL_tokenbuf[1])
6755 PREREF(DOLSHARP);
6756 PL_expect = XOPERATOR;
6757 force_ident_maybe_lex('#');
6758 TOKEN(DOLSHARP);
6759 }
6760
6761 PL_tokenbuf[0] = '$';
6762 s = scan_ident(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
6763 if (PL_expect == XOPERATOR) {
6764 d = s;
6765 if (PL_bufptr > s) {
6766 d = PL_bufptr-1;
6767 PL_bufptr = PL_oldbufptr;
6768 }
6769 no_op("Scalar", d);
6770 }
6771 if (!PL_tokenbuf[1]) {
6772 if (s == PL_bufend)
6773 yyerror("Final $ should be \\$ or $name");
6774 PREREF('$');
6775 }
6776
6777 d = s;
6778 {
6779 const char tmp = *s;
6780 if (PL_lex_state == LEX_NORMAL || PL_lex_brackets)
6781 s = skipspace(s);
6782
6783 if ( (PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
6784 && intuit_more(s, PL_bufend)) {
6785 if (*s == '[') {
6786 PL_tokenbuf[0] = '@';
6787 if (ckWARN(WARN_SYNTAX)) {
6788 char *t = s+1;
6789
6790 while ( isSPACE(*t)
6791 || isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF)
6792 || *t == '$')
6793 {
6794 t += UTF ? UTF8SKIP(t) : 1;
6795 }
6796 if (*t++ == ',') {
6797 PL_bufptr = skipspace(PL_bufptr); /* XXX can realloc */
6798 while (t < PL_bufend && *t != ']')
6799 t++;
6800 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6801 "Multidimensional syntax %" UTF8f " not supported",
6802 UTF8fARG(UTF,(int)((t - PL_bufptr) + 1), PL_bufptr));
6803 }
6804 }
6805 }
6806 else if (*s == '{') {
6807 char *t;
6808 PL_tokenbuf[0] = '%';
6809 if ( strEQ(PL_tokenbuf+1, "SIG")
6810 && ckWARN(WARN_SYNTAX)
6811 && (t = (char *) memchr(s, '}', PL_bufend - s))
6812 && (t = (char *) memchr(t, '=', PL_bufend - t)))
6813 {
6814 char tmpbuf[sizeof PL_tokenbuf];
6815 do {
6816 t++;
6817 } while (isSPACE(*t));
6818 if (isIDFIRST_lazy_if_safe(t, PL_bufend, UTF)) {
6819 STRLEN len;
6820 t = scan_word(t, tmpbuf, sizeof tmpbuf, TRUE,
6821 &len);
6822 while (isSPACE(*t))
6823 t++;
6824 if ( *t == ';'
6825 && get_cvn_flags(tmpbuf, len, UTF
6826 ? SVf_UTF8
6827 : 0))
6828 {
6829 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6830 "You need to quote \"%" UTF8f "\"",
6831 UTF8fARG(UTF, len, tmpbuf));
6832 }
6833 }
6834 }
6835 }
6836 }
6837
6838 PL_expect = XOPERATOR;
6839 if (PL_lex_state == LEX_NORMAL && isSPACE((char)tmp)) {
6840 const bool islop = (PL_last_lop == PL_oldoldbufptr);
6841 if (!islop || PL_last_lop_op == OP_GREPSTART)
6842 PL_expect = XOPERATOR;
6843 else if (strchr("$@\"'`q", *s))
6844 PL_expect = XTERM; /* e.g. print $fh "foo" */
6845 else if ( strchr("&*<%", *s)
6846 && isIDFIRST_lazy_if_safe(s+1, PL_bufend, UTF))
6847 {
6848 PL_expect = XTERM; /* e.g. print $fh &sub */
6849 }
6850 else if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
6851 char tmpbuf[sizeof PL_tokenbuf];
6852 int t2;
6853 scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
6854 if ((t2 = keyword(tmpbuf, len, 0))) {
6855 /* binary operators exclude handle interpretations */
6856 switch (t2) {
6857 case -KEY_x:
6858 case -KEY_eq:
6859 case -KEY_ne:
6860 case -KEY_gt:
6861 case -KEY_lt:
6862 case -KEY_ge:
6863 case -KEY_le:
6864 case -KEY_cmp:
6865 break;
6866 default:
6867 PL_expect = XTERM; /* e.g. print $fh length() */
6868 break;
6869 }
6870 }
6871 else {
6872 PL_expect = XTERM; /* e.g. print $fh subr() */
6873 }
6874 }
6875 else if (isDIGIT(*s))
6876 PL_expect = XTERM; /* e.g. print $fh 3 */
6877 else if (*s == '.' && isDIGIT(s[1]))
6878 PL_expect = XTERM; /* e.g. print $fh .3 */
6879 else if ((*s == '?' || *s == '-' || *s == '+')
6880 && !isSPACE(s[1]) && s[1] != '=')
6881 PL_expect = XTERM; /* e.g. print $fh -1 */
6882 else if (*s == '/' && !isSPACE(s[1]) && s[1] != '='
6883 && s[1] != '/')
6884 PL_expect = XTERM; /* e.g. print $fh /.../
6885 XXX except DORDOR operator
6886 */
6887 else if (*s == '<' && s[1] == '<' && !isSPACE(s[2])
6888 && s[2] != '=')
6889 PL_expect = XTERM; /* print $fh <<"EOF" */
6890 }
6891 }
6892 force_ident_maybe_lex('$');
6893 TOKEN('$');
6894
6895 case '@':
6896 if (PL_expect == XPOSTDEREF)
6897 POSTDEREF('@');
6898 PL_tokenbuf[0] = '@';
6899 s = scan_ident(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
6900 if (PL_expect == XOPERATOR) {
6901 d = s;
6902 if (PL_bufptr > s) {
6903 d = PL_bufptr-1;
6904 PL_bufptr = PL_oldbufptr;
6905 }
6906 no_op("Array", d);
6907 }
6908 pl_yylval.ival = 0;
6909 if (!PL_tokenbuf[1]) {
6910 PREREF('@');
6911 }
6912 if (PL_lex_state == LEX_NORMAL)
6913 s = skipspace(s);
6914 if ( (PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
6915 && intuit_more(s, PL_bufend))
6916 {
6917 if (*s == '{')
6918 PL_tokenbuf[0] = '%';
6919
6920 /* Warn about @ where they meant $. */
6921 if (*s == '[' || *s == '{') {
6922 if (ckWARN(WARN_SYNTAX)) {
6923 S_check_scalar_slice(aTHX_ s);
6924 }
6925 }
6926 }
6927 PL_expect = XOPERATOR;
6928 force_ident_maybe_lex('@');
6929 TERM('@');
6930
6931 case '/': /* may be division, defined-or, or pattern */
6932 if ((PL_expect == XOPERATOR || PL_expect == XTERMORDORDOR) && s[1] == '/') {
6933 if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6934 (s[2] == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_LOGIC))
6935 TOKEN(0);
6936 s += 2;
6937 AOPERATOR(DORDOR);
6938 }
6939 else if (PL_expect == XOPERATOR) {
6940 s++;
6941 if (*s == '=' && !PL_lex_allbrackets
6942 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
6943 {
6944 s--;
6945 TOKEN(0);
6946 }
6947 Mop(OP_DIVIDE);
6948 }
6949 else {
6950 /* Disable warning on "study /blah/" */
6951 if ( PL_oldoldbufptr == PL_last_uni
6952 && ( *PL_last_uni != 's' || s - PL_last_uni < 5
6953 || memNE(PL_last_uni, "study", 5)
6954 || isWORDCHAR_lazy_if_safe(PL_last_uni+5, PL_bufend, UTF)
6955 ))
6956 check_uni();
6957 s = scan_pat(s,OP_MATCH);
6958 TERM(sublex_start());
6959 }
6960
6961 case '?': /* conditional */
6962 s++;
6963 if (!PL_lex_allbrackets
6964 && PL_lex_fakeeof >= LEX_FAKEEOF_IFELSE)
6965 {
6966 s--;
6967 TOKEN(0);
6968 }
6969 PL_lex_allbrackets++;
6970 OPERATOR('?');
6971
6972 case '.':
6973 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack
6974 #ifdef PERL_STRICT_CR
6975 && s[1] == '\n'
6976 #else
6977 && (s[1] == '\n' || (s[1] == '\r' && s[2] == '\n'))
6978 #endif
6979 && (s == PL_linestart || s[-1] == '\n') )
6980 {
6981 PL_expect = XSTATE;
6982 formbrack = 2; /* dot seen where arguments expected */
6983 goto rightbracket;
6984 }
6985 if (PL_expect == XSTATE && s[1] == '.' && s[2] == '.') {
6986 s += 3;
6987 OPERATOR(YADAYADA);
6988 }
6989 if (PL_expect == XOPERATOR || !isDIGIT(s[1])) {
6990 char tmp = *s++;
6991 if (*s == tmp) {
6992 if (!PL_lex_allbrackets
6993 && PL_lex_fakeeof >= LEX_FAKEEOF_RANGE)
6994 {
6995 s--;
6996 TOKEN(0);
6997 }
6998 s++;
6999 if (*s == tmp) {
7000 s++;
7001 pl_yylval.ival = OPf_SPECIAL;
7002 }
7003 else
7004 pl_yylval.ival = 0;
7005 OPERATOR(DOTDOT);
7006 }
7007 if (*s == '=' && !PL_lex_allbrackets
7008 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
7009 {
7010 s--;
7011 TOKEN(0);
7012 }
7013 Aop(OP_CONCAT);
7014 }
7015 /* FALLTHROUGH */
7016 case '0': case '1': case '2': case '3': case '4':
7017 case '5': case '6': case '7': case '8': case '9':
7018 s = scan_num(s, &pl_yylval);
7019 DEBUG_T( { printbuf("### Saw number in %s\n", s); } );
7020 if (PL_expect == XOPERATOR)
7021 no_op("Number",s);
7022 TERM(THING);
7023
7024 case '\'':
7025 s = scan_str(s,FALSE,FALSE,FALSE,NULL);
7026 if (!s)
7027 missingterm(NULL, 0);
7028 COPLINE_SET_FROM_MULTI_END;
7029 DEBUG_T( { printbuf("### Saw string before %s\n", s); } );
7030 if (PL_expect == XOPERATOR) {
7031 no_op("String",s);
7032 }
7033 pl_yylval.ival = OP_CONST;
7034 TERM(sublex_start());
7035
7036 case '"':
7037 s = scan_str(s,FALSE,FALSE,FALSE,NULL);
7038 DEBUG_T( {
7039 if (s)
7040 printbuf("### Saw string before %s\n", s);
7041 else
7042 PerlIO_printf(Perl_debug_log,
7043 "### Saw unterminated string\n");
7044 } );
7045 if (PL_expect == XOPERATOR) {
7046 no_op("String",s);
7047 }
7048 if (!s)
7049 missingterm(NULL, 0);
7050 pl_yylval.ival = OP_CONST;
7051 /* FIXME. I think that this can be const if char *d is replaced by
7052 more localised variables. */
7053 for (d = SvPV(PL_lex_stuff, len); len; len--, d++) {
7054 if (*d == '$' || *d == '@' || *d == '\\' || !UTF8_IS_INVARIANT((U8)*d)) {
7055 pl_yylval.ival = OP_STRINGIFY;
7056 break;
7057 }
7058 }
7059 if (pl_yylval.ival == OP_CONST)
7060 COPLINE_SET_FROM_MULTI_END;
7061 TERM(sublex_start());
7062
7063 case '`':
7064 s = scan_str(s,FALSE,FALSE,FALSE,NULL);
7065 DEBUG_T( {
7066 if (s)
7067 printbuf("### Saw backtick string before %s\n", s);
7068 else
7069 PerlIO_printf(Perl_debug_log,
7070 "### Saw unterminated backtick string\n");
7071 } );
7072 if (PL_expect == XOPERATOR)
7073 no_op("Backticks",s);
7074 if (!s)
7075 missingterm(NULL, 0);
7076 pl_yylval.ival = OP_BACKTICK;
7077 TERM(sublex_start());
7078
7079 case '\\':
7080 s++;
7081 if (PL_lex_inwhat == OP_SUBST && PL_lex_repl == PL_linestr
7082 && isDIGIT(*s))
7083 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),"Can't use \\%c to mean $%c in expression",
7084 *s, *s);
7085 if (PL_expect == XOPERATOR)
7086 no_op("Backslash",s);
7087 OPERATOR(REFGEN);
7088
7089 case 'v':
7090 if (isDIGIT(s[1]) && PL_expect != XOPERATOR) {
7091 char *start = s + 2;
7092 while (isDIGIT(*start) || *start == '_')
7093 start++;
7094 if (*start == '.' && isDIGIT(start[1])) {
7095 s = scan_num(s, &pl_yylval);
7096 TERM(THING);
7097 }
7098 else if ((*start == ':' && start[1] == ':')
7099 || (PL_expect == XSTATE && *start == ':'))
7100 goto keylookup;
7101 else if (PL_expect == XSTATE) {
7102 d = start;
7103 while (d < PL_bufend && isSPACE(*d)) d++;
7104 if (*d == ':') goto keylookup;
7105 }
7106 /* avoid v123abc() or $h{v1}, allow C<print v10;> */
7107 if (!isALPHA(*start) && (PL_expect == XTERM
7108 || PL_expect == XREF || PL_expect == XSTATE
7109 || PL_expect == XTERMORDORDOR)) {
7110 GV *const gv = gv_fetchpvn_flags(s, start - s,
7111 UTF ? SVf_UTF8 : 0, SVt_PVCV);
7112 if (!gv) {
7113 s = scan_num(s, &pl_yylval);
7114 TERM(THING);
7115 }
7116 }
7117 }
7118 goto keylookup;
7119 case 'x':
7120 if (isDIGIT(s[1]) && PL_expect == XOPERATOR) {
7121 s++;
7122 Mop(OP_REPEAT);
7123 }
7124 goto keylookup;
7125
7126 case '_':
7127 case 'a': case 'A':
7128 case 'b': case 'B':
7129 case 'c': case 'C':
7130 case 'd': case 'D':
7131 case 'e': case 'E':
7132 case 'f': case 'F':
7133 case 'g': case 'G':
7134 case 'h': case 'H':
7135 case 'i': case 'I':
7136 case 'j': case 'J':
7137 case 'k': case 'K':
7138 case 'l': case 'L':
7139 case 'm': case 'M':
7140 case 'n': case 'N':
7141 case 'o': case 'O':
7142 case 'p': case 'P':
7143 case 'q': case 'Q':
7144 case 'r': case 'R':
7145 case 's': case 'S':
7146 case 't': case 'T':
7147 case 'u': case 'U':
7148 case 'V':
7149 case 'w': case 'W':
7150 case 'X':
7151 case 'y': case 'Y':
7152 case 'z': case 'Z':
7153
7154 keylookup: {
7155 bool anydelim;
7156 bool lex;
7157 I32 tmp;
7158 SV *sv;
7159 CV *cv;
7160 PADOFFSET off;
7161 OP *rv2cv_op;
7162
7163 lex = FALSE;
7164 orig_keyword = 0;
7165 off = 0;
7166 sv = NULL;
7167 cv = NULL;
7168 gv = NULL;
7169 gvp = NULL;
7170 rv2cv_op = NULL;
7171
7172 PL_bufptr = s;
7173 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
7174
7175 /* Some keywords can be followed by any delimiter, including ':' */
7176 anydelim = word_takes_any_delimiter(PL_tokenbuf, len);
7177
7178 /* x::* is just a word, unless x is "CORE" */
7179 if (!anydelim && *s == ':' && s[1] == ':') {
7180 if (memEQs(PL_tokenbuf, len, "CORE")) goto case_KEY_CORE;
7181 goto just_a_word;
7182 }
7183
7184 d = s;
7185 while (d < PL_bufend && isSPACE(*d))
7186 d++; /* no comments skipped here, or s### is misparsed */
7187
7188 /* Is this a word before a => operator? */
7189 if (*d == '=' && d[1] == '>') {
7190 fat_arrow:
7191 CLINE;
7192 pl_yylval.opval
7193 = newSVOP(OP_CONST, 0,
7194 S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
7195 pl_yylval.opval->op_private = OPpCONST_BARE;
7196 TERM(BAREWORD);
7197 }
7198
7199 /* Check for plugged-in keyword */
7200 {
7201 OP *o;
7202 int result;
7203 char *saved_bufptr = PL_bufptr;
7204 PL_bufptr = s;
7205 result = PL_keyword_plugin(aTHX_ PL_tokenbuf, len, &o);
7206 s = PL_bufptr;
7207 if (result == KEYWORD_PLUGIN_DECLINE) {
7208 /* not a plugged-in keyword */
7209 PL_bufptr = saved_bufptr;
7210 } else if (result == KEYWORD_PLUGIN_STMT) {
7211 pl_yylval.opval = o;
7212 CLINE;
7213 if (!PL_nexttoke) PL_expect = XSTATE;
7214 return REPORT(PLUGSTMT);
7215 } else if (result == KEYWORD_PLUGIN_EXPR) {
7216 pl_yylval.opval = o;
7217 CLINE;
7218 if (!PL_nexttoke) PL_expect = XOPERATOR;
7219 return REPORT(PLUGEXPR);
7220 } else {
7221 Perl_croak(aTHX_ "Bad plugin affecting keyword '%s'",
7222 PL_tokenbuf);
7223 }
7224 }
7225
7226 /* Check for built-in keyword */
7227 tmp = keyword(PL_tokenbuf, len, 0);
7228
7229 /* Is this a label? */
7230 if (!anydelim && PL_expect == XSTATE
7231 && d < PL_bufend && *d == ':' && *(d + 1) != ':') {
7232 s = d + 1;
7233 pl_yylval.opval =
7234 newSVOP(OP_CONST, 0,
7235 newSVpvn_flags(PL_tokenbuf, len, UTF ? SVf_UTF8 : 0));
7236 CLINE;
7237 TOKEN(LABEL);
7238 }
7239
7240 /* Check for lexical sub */
7241 if (PL_expect != XOPERATOR) {
7242 char tmpbuf[sizeof PL_tokenbuf + 1];
7243 *tmpbuf = '&';
7244 Copy(PL_tokenbuf, tmpbuf+1, len, char);
7245 off = pad_findmy_pvn(tmpbuf, len+1, 0);
7246 if (off != NOT_IN_PAD) {
7247 assert(off); /* we assume this is boolean-true below */
7248 if (PAD_COMPNAME_FLAGS_isOUR(off)) {
7249 HV * const stash = PAD_COMPNAME_OURSTASH(off);
7250 HEK * const stashname = HvNAME_HEK(stash);
7251 sv = newSVhek(stashname);
7252 sv_catpvs(sv, "::");
7253 sv_catpvn_flags(sv, PL_tokenbuf, len,
7254 (UTF ? SV_CATUTF8 : SV_CATBYTES));
7255 gv = gv_fetchsv(sv, GV_NOADD_NOINIT | SvUTF8(sv),
7256 SVt_PVCV);
7257 off = 0;
7258 if (!gv) {
7259 sv_free(sv);
7260 sv = NULL;
7261 goto just_a_word;
7262 }
7263 }
7264 else {
7265 rv2cv_op = newOP(OP_PADANY, 0);
7266 rv2cv_op->op_targ = off;
7267 cv = find_lexical_cv(off);
7268 }
7269 lex = TRUE;
7270 goto just_a_word;
7271 }
7272 off = 0;
7273 }
7274
7275 if (tmp < 0) { /* second-class keyword? */
7276 GV *ogv = NULL; /* override (winner) */
7277 GV *hgv = NULL; /* hidden (loser) */
7278 if (PL_expect != XOPERATOR && (*s != ':' || s[1] != ':')) {
7279 CV *cv;
7280 if ((gv = gv_fetchpvn_flags(PL_tokenbuf, len,
7281 (UTF ? SVf_UTF8 : 0)|GV_NOTQUAL,
7282 SVt_PVCV))
7283 && (cv = GvCVu(gv)))
7284 {
7285 if (GvIMPORTED_CV(gv))
7286 ogv = gv;
7287 else if (! CvMETHOD(cv))
7288 hgv = gv;
7289 }
7290 if (!ogv
7291 && (gvp = (GV**)hv_fetch(PL_globalstash, PL_tokenbuf,
7292 len, FALSE))
7293 && (gv = *gvp)
7294 && (isGV_with_GP(gv)
7295 ? GvCVu(gv) && GvIMPORTED_CV(gv)
7296 : SvPCS_IMPORTED(gv)
7297 && (gv_init(gv, PL_globalstash, PL_tokenbuf,
7298 len, 0), 1)))
7299 {
7300 ogv = gv;
7301 }
7302 }
7303 if (ogv) {
7304 orig_keyword = tmp;
7305 tmp = 0; /* overridden by import or by GLOBAL */
7306 }
7307 else if (gv && !gvp
7308 && -tmp==KEY_lock /* XXX generalizable kludge */
7309 && GvCVu(gv))
7310 {
7311 tmp = 0; /* any sub overrides "weak" keyword */
7312 }
7313 else { /* no override */
7314 tmp = -tmp;
7315 if (tmp == KEY_dump) {
7316 Perl_croak(aTHX_ "dump() must be written as CORE::dump() as of Perl 5.30");
7317 }
7318 gv = NULL;
7319 gvp = 0;
7320 if (hgv && tmp != KEY_x) /* never ambiguous */
7321 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
7322 "Ambiguous call resolved as CORE::%s(), "
7323 "qualify as such or use &",
7324 GvENAME(hgv));
7325 }
7326 }
7327
7328 if (tmp && tmp != KEY___DATA__ && tmp != KEY___END__
7329 && (!anydelim || *s != '#')) {
7330 /* no override, and not s### either; skipspace is safe here
7331 * check for => on following line */
7332 bool arrow;
7333 STRLEN bufoff = PL_bufptr - SvPVX(PL_linestr);
7334 STRLEN soff = s - SvPVX(PL_linestr);
7335 s = peekspace(s);
7336 arrow = *s == '=' && s[1] == '>';
7337 PL_bufptr = SvPVX(PL_linestr) + bufoff;
7338 s = SvPVX(PL_linestr) + soff;
7339 if (arrow)
7340 goto fat_arrow;
7341 }
7342
7343 reserved_word:
7344 switch (tmp) {
7345
7346 /* Trade off - by using this evil construction we can pull the
7347 variable gv into the block labelled keylookup. If not, then
7348 we have to give it function scope so that the goto from the
7349 earlier ':' case doesn't bypass the initialisation. */
7350 just_a_word_zero_gv:
7351 sv = NULL;
7352 cv = NULL;
7353 gv = NULL;
7354 gvp = NULL;
7355 rv2cv_op = NULL;
7356 orig_keyword = 0;
7357 lex = 0;
7358 off = 0;
7359 /* FALLTHROUGH */
7360 default: /* not a keyword */
7361 just_a_word: {
7362 int pkgname = 0;
7363 const char lastchar = (PL_bufptr == PL_oldoldbufptr ? 0 : PL_bufptr[-1]);
7364 bool safebw;
7365 bool no_op_error = FALSE;
7366
7367 if (PL_expect == XOPERATOR) {
7368 if (PL_bufptr == PL_linestart) {
7369 CopLINE_dec(PL_curcop);
7370 Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
7371 CopLINE_inc(PL_curcop);
7372 }
7373 else
7374 /* We want to call no_op with s pointing after the
7375 bareword, so defer it. But we want it to come
7376 before the Bad name croak. */
7377 no_op_error = TRUE;
7378 }
7379
7380 /* Get the rest if it looks like a package qualifier */
7381
7382 if (*s == '\'' || (*s == ':' && s[1] == ':')) {
7383 STRLEN morelen;
7384 s = scan_word(s, PL_tokenbuf + len, sizeof PL_tokenbuf - len,
7385 TRUE, &morelen);
7386 if (no_op_error) {
7387 no_op("Bareword",s);
7388 no_op_error = FALSE;
7389 }
7390 if (!morelen)
7391 Perl_croak(aTHX_ "Bad name after %" UTF8f "%s",
7392 UTF8fARG(UTF, len, PL_tokenbuf),
7393 *s == '\'' ? "'" : "::");
7394 len += morelen;
7395 pkgname = 1;
7396 }
7397
7398 if (no_op_error)
7399 no_op("Bareword",s);
7400
7401 /* See if the name is "Foo::",
7402 in which case Foo is a bareword
7403 (and a package name). */
7404
7405 if (len > 2
7406 && PL_tokenbuf[len - 2] == ':'
7407 && PL_tokenbuf[len - 1] == ':')
7408 {
7409 if (ckWARN(WARN_BAREWORD)
7410 && ! gv_fetchpvn_flags(PL_tokenbuf, len, UTF ? SVf_UTF8 : 0, SVt_PVHV))
7411 Perl_warner(aTHX_ packWARN(WARN_BAREWORD),
7412 "Bareword \"%" UTF8f
7413 "\" refers to nonexistent package",
7414 UTF8fARG(UTF, len, PL_tokenbuf));
7415 len -= 2;
7416 PL_tokenbuf[len] = '\0';
7417 gv = NULL;
7418 gvp = 0;
7419 safebw = TRUE;
7420 }
7421 else {
7422 safebw = FALSE;
7423 }
7424
7425 /* if we saw a global override before, get the right name */
7426
7427 if (!sv)
7428 sv = S_newSV_maybe_utf8(aTHX_ PL_tokenbuf,
7429 len);
7430 if (gvp) {
7431 SV * const tmp_sv = sv;
7432 sv = newSVpvs("CORE::GLOBAL::");
7433 sv_catsv(sv, tmp_sv);
7434 SvREFCNT_dec(tmp_sv);
7435 }
7436
7437
7438 /* Presume this is going to be a bareword of some sort. */
7439 CLINE;
7440 pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
7441 pl_yylval.opval->op_private = OPpCONST_BARE;
7442
7443 /* And if "Foo::", then that's what it certainly is. */
7444 if (safebw)
7445 goto safe_bareword;
7446
7447 if (!off)
7448 {
7449 OP *const_op = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(sv));
7450 const_op->op_private = OPpCONST_BARE;
7451 rv2cv_op =
7452 newCVREF(OPpMAY_RETURN_CONSTANT<<8, const_op);
7453 cv = lex
7454 ? isGV(gv)
7455 ? GvCV(gv)
7456 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
7457 ? (CV *)SvRV(gv)
7458 : ((CV *)gv)
7459 : rv2cv_op_cv(rv2cv_op, RV2CVOPCV_RETURN_STUB);
7460 }
7461
7462 /* Use this var to track whether intuit_method has been
7463 called. intuit_method returns 0 or > 255. */
7464 tmp = 1;
7465
7466 /* See if it's the indirect object for a list operator. */
7467
7468 if (PL_oldoldbufptr
7469 && PL_oldoldbufptr < PL_bufptr
7470 && (PL_oldoldbufptr == PL_last_lop
7471 || PL_oldoldbufptr == PL_last_uni)
7472 && /* NO SKIPSPACE BEFORE HERE! */
7473 (PL_expect == XREF
7474 || ((PL_opargs[PL_last_lop_op] >> OASHIFT)& 7)
7475 == OA_FILEREF))
7476 {
7477 bool immediate_paren = *s == '(';
7478 SSize_t s_off;
7479
7480 /* (Now we can afford to cross potential line boundary.) */
7481 s = skipspace(s);
7482
7483 /* intuit_method() can indirectly call lex_next_chunk(),
7484 * invalidating s
7485 */
7486 s_off = s - SvPVX(PL_linestr);
7487 /* Two barewords in a row may indicate method call. */
7488 if ( ( isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)
7489 || *s == '$')
7490 && (tmp = intuit_method(s, lex ? NULL : sv, cv)))
7491 {
7492 /* the code at method: doesn't use s */
7493 goto method;
7494 }
7495 s = SvPVX(PL_linestr) + s_off;
7496
7497 /* If not a declared subroutine, it's an indirect object. */
7498 /* (But it's an indir obj regardless for sort.) */
7499 /* Also, if "_" follows a filetest operator, it's a bareword */
7500
7501 if (
7502 ( !immediate_paren && (PL_last_lop_op == OP_SORT
7503 || (!cv
7504 && (PL_last_lop_op != OP_MAPSTART
7505 && PL_last_lop_op != OP_GREPSTART))))
7506 || (PL_tokenbuf[0] == '_' && PL_tokenbuf[1] == '\0'
7507 && ((PL_opargs[PL_last_lop_op] & OA_CLASS_MASK)
7508 == OA_FILESTATOP))
7509 )
7510 {
7511 PL_expect = (PL_last_lop == PL_oldoldbufptr) ? XTERM : XOPERATOR;
7512 goto bareword;
7513 }
7514 }
7515
7516 PL_expect = XOPERATOR;
7517 s = skipspace(s);
7518
7519 /* Is this a word before a => operator? */
7520 if (*s == '=' && s[1] == '>' && !pkgname) {
7521 op_free(rv2cv_op);
7522 CLINE;
7523 if (gvp || (lex && !off)) {
7524 assert (cSVOPx(pl_yylval.opval)->op_sv == sv);
7525 /* This is our own scalar, created a few lines
7526 above, so this is safe. */
7527 SvREADONLY_off(sv);
7528 sv_setpv(sv, PL_tokenbuf);
7529 if (UTF && !IN_BYTES
7530 && is_utf8_string((U8*)PL_tokenbuf, len))
7531 SvUTF8_on(sv);
7532 SvREADONLY_on(sv);
7533 }
7534 TERM(BAREWORD);
7535 }
7536
7537 /* If followed by a paren, it's certainly a subroutine. */
7538 if (*s == '(') {
7539 CLINE;
7540 if (cv) {
7541 d = s + 1;
7542 while (SPACE_OR_TAB(*d))
7543 d++;
7544 if (*d == ')' && (sv = cv_const_sv_or_av(cv))) {
7545 s = d + 1;
7546 goto its_constant;
7547 }
7548 }
7549 NEXTVAL_NEXTTOKE.opval =
7550 off ? rv2cv_op : pl_yylval.opval;
7551 if (off)
7552 op_free(pl_yylval.opval), force_next(PRIVATEREF);
7553 else op_free(rv2cv_op), force_next(BAREWORD);
7554 pl_yylval.ival = 0;
7555 TOKEN('&');
7556 }
7557
7558 /* If followed by var or block, call it a method (unless sub) */
7559
7560 if ((*s == '$' || *s == '{') && !cv) {
7561 op_free(rv2cv_op);
7562 PL_last_lop = PL_oldbufptr;
7563 PL_last_lop_op = OP_METHOD;
7564 if (!PL_lex_allbrackets
7565 && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7566 {
7567 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7568 }
7569 PL_expect = XBLOCKTERM;
7570 PL_bufptr = s;
7571 return REPORT(METHOD);
7572 }
7573
7574 /* If followed by a bareword, see if it looks like indir obj. */
7575
7576 if ( tmp == 1
7577 && !orig_keyword
7578 && (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF) || *s == '$')
7579 && (tmp = intuit_method(s, lex ? NULL : sv, cv)))
7580 {
7581 method:
7582 if (lex && !off) {
7583 assert(cSVOPx(pl_yylval.opval)->op_sv == sv);
7584 SvREADONLY_off(sv);
7585 sv_setpvn(sv, PL_tokenbuf, len);
7586 if (UTF && !IN_BYTES
7587 && is_utf8_string((U8*)PL_tokenbuf, len))
7588 SvUTF8_on (sv);
7589 else SvUTF8_off(sv);
7590 }
7591 op_free(rv2cv_op);
7592 if (tmp == METHOD && !PL_lex_allbrackets
7593 && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7594 {
7595 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7596 }
7597 return REPORT(tmp);
7598 }
7599
7600 /* Not a method, so call it a subroutine (if defined) */
7601
7602 if (cv) {
7603 /* Check for a constant sub */
7604 if ((sv = cv_const_sv_or_av(cv))) {
7605 its_constant:
7606 op_free(rv2cv_op);
7607 SvREFCNT_dec(((SVOP*)pl_yylval.opval)->op_sv);
7608 ((SVOP*)pl_yylval.opval)->op_sv = SvREFCNT_inc_simple(sv);
7609 if (SvTYPE(sv) == SVt_PVAV)
7610 pl_yylval.opval = newUNOP(OP_RV2AV, OPf_PARENS,
7611 pl_yylval.opval);
7612 else {
7613 pl_yylval.opval->op_private = 0;
7614 pl_yylval.opval->op_folded = 1;
7615 pl_yylval.opval->op_flags |= OPf_SPECIAL;
7616 }
7617 TOKEN(BAREWORD);
7618 }
7619
7620 op_free(pl_yylval.opval);
7621 pl_yylval.opval =
7622 off ? newCVREF(0, rv2cv_op) : rv2cv_op;
7623 pl_yylval.opval->op_private |= OPpENTERSUB_NOPAREN;
7624 PL_last_lop = PL_oldbufptr;
7625 PL_last_lop_op = OP_ENTERSUB;
7626 /* Is there a prototype? */
7627 if (
7628 SvPOK(cv))
7629 {
7630 STRLEN protolen = CvPROTOLEN(cv);
7631 const char *proto = CvPROTO(cv);
7632 bool optional;
7633 proto = S_strip_spaces(aTHX_ proto, &protolen);
7634 if (!protolen)
7635 TERM(FUNC0SUB);
7636 if ((optional = *proto == ';'))
7637 do
7638 proto++;
7639 while (*proto == ';');
7640 if (
7641 (
7642 (
7643 *proto == '$' || *proto == '_'
7644 || *proto == '*' || *proto == '+'
7645 )
7646 && proto[1] == '\0'
7647 )
7648 || (
7649 *proto == '\\' && proto[1] && proto[2] == '\0'
7650 )
7651 )
7652 UNIPROTO(UNIOPSUB,optional);
7653 if (*proto == '\\' && proto[1] == '[') {
7654 const char *p = proto + 2;
7655 while(*p && *p != ']')
7656 ++p;
7657 if(*p == ']' && !p[1])
7658 UNIPROTO(UNIOPSUB,optional);
7659 }
7660 if (*proto == '&' && *s == '{') {
7661 if (PL_curstash)
7662 sv_setpvs(PL_subname, "__ANON__");
7663 else
7664 sv_setpvs(PL_subname, "__ANON__::__ANON__");
7665 if (!PL_lex_allbrackets
7666 && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7667 {
7668 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7669 }
7670 PREBLOCK(LSTOPSUB);
7671 }
7672 }
7673 NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
7674 PL_expect = XTERM;
7675 force_next(off ? PRIVATEREF : BAREWORD);
7676 if (!PL_lex_allbrackets
7677 && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7678 {
7679 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7680 }
7681 TOKEN(NOAMP);
7682 }
7683
7684 /* Call it a bare word */
7685
7686 if (PL_hints & HINT_STRICT_SUBS)
7687 pl_yylval.opval->op_private |= OPpCONST_STRICT;
7688 else {
7689 bareword:
7690 /* after "print" and similar functions (corresponding to
7691 * "F? L" in opcode.pl), whatever wasn't already parsed as
7692 * a filehandle should be subject to "strict subs".
7693 * Likewise for the optional indirect-object argument to system
7694 * or exec, which can't be a bareword */
7695 if ((PL_last_lop_op == OP_PRINT
7696 || PL_last_lop_op == OP_PRTF
7697 || PL_last_lop_op == OP_SAY
7698 || PL_last_lop_op == OP_SYSTEM
7699 || PL_last_lop_op == OP_EXEC)
7700 && (PL_hints & HINT_STRICT_SUBS))
7701 pl_yylval.opval->op_private |= OPpCONST_STRICT;
7702 if (lastchar != '-') {
7703 if (ckWARN(WARN_RESERVED)) {
7704 d = PL_tokenbuf;
7705 while (isLOWER(*d))
7706 d++;
7707 if (!*d && !gv_stashpv(PL_tokenbuf, UTF ? SVf_UTF8 : 0))
7708 {
7709 /* PL_warn_reserved is constant */
7710 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
7711 Perl_warner(aTHX_ packWARN(WARN_RESERVED), PL_warn_reserved,
7712 PL_tokenbuf);
7713 GCC_DIAG_RESTORE_STMT;
7714 }
7715 }
7716 }
7717 }
7718 op_free(rv2cv_op);
7719
7720 safe_bareword:
7721 if ((lastchar == '*' || lastchar == '%' || lastchar == '&')
7722 && saw_infix_sigil) {
7723 Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
7724 "Operator or semicolon missing before %c%" UTF8f,
7725 lastchar,
7726 UTF8fARG(UTF, strlen(PL_tokenbuf),
7727 PL_tokenbuf));
7728 Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
7729 "Ambiguous use of %c resolved as operator %c",
7730 lastchar, lastchar);
7731 }
7732 TOKEN(BAREWORD);
7733 }
7734
7735 case KEY___FILE__:
7736 FUN0OP(
7737 newSVOP(OP_CONST, 0, newSVpv(CopFILE(PL_curcop),0))
7738 );
7739
7740 case KEY___LINE__:
7741 FUN0OP(
7742 newSVOP(OP_CONST, 0,
7743 Perl_newSVpvf(aTHX_ "%" IVdf, (IV)CopLINE(PL_curcop)))
7744 );
7745
7746 case KEY___PACKAGE__:
7747 FUN0OP(
7748 newSVOP(OP_CONST, 0,
7749 (PL_curstash
7750 ? newSVhek(HvNAME_HEK(PL_curstash))
7751 : &PL_sv_undef))
7752 );
7753
7754 case KEY___DATA__:
7755 case KEY___END__: {
7756 GV *gv;
7757 if (PL_rsfp && (!PL_in_eval || PL_tokenbuf[2] == 'D')) {
7758 HV * const stash = PL_tokenbuf[2] == 'D' && PL_curstash
7759 ? PL_curstash
7760 : PL_defstash;
7761 gv = (GV *)*hv_fetchs(stash, "DATA", 1);
7762 if (!isGV(gv))
7763 gv_init(gv,stash,"DATA",4,0);
7764 GvMULTI_on(gv);
7765 if (!GvIO(gv))
7766 GvIOp(gv) = newIO();
7767 IoIFP(GvIOp(gv)) = PL_rsfp;
7768 /* Mark this internal pseudo-handle as clean */
7769 IoFLAGS(GvIOp(gv)) |= IOf_UNTAINT;
7770 if ((PerlIO*)PL_rsfp == PerlIO_stdin())
7771 IoTYPE(GvIOp(gv)) = IoTYPE_STD;
7772 else
7773 IoTYPE(GvIOp(gv)) = IoTYPE_RDONLY;
7774 #if defined(WIN32) && !defined(PERL_TEXTMODE_SCRIPTS)
7775 /* if the script was opened in binmode, we need to revert
7776 * it to text mode for compatibility; but only iff it has CRs
7777 * XXX this is a questionable hack at best. */
7778 if (PL_bufend-PL_bufptr > 2
7779 && PL_bufend[-1] == '\n' && PL_bufend[-2] == '\r')
7780 {
7781 Off_t loc = 0;
7782 if (IoTYPE(GvIOp(gv)) == IoTYPE_RDONLY) {
7783 loc = PerlIO_tell(PL_rsfp);
7784 (void)PerlIO_seek(PL_rsfp, 0L, 0);
7785 }
7786 #ifdef NETWARE
7787 if (PerlLIO_setmode(PL_rsfp, O_TEXT) != -1) {
7788 #else
7789 if (PerlLIO_setmode(PerlIO_fileno(PL_rsfp), O_TEXT) != -1) {
7790 #endif /* NETWARE */
7791 if (loc > 0)
7792 PerlIO_seek(PL_rsfp, loc, 0);
7793 }
7794 }
7795 #endif
7796 #ifdef PERLIO_LAYERS
7797 if (!IN_BYTES) {
7798 if (UTF)
7799 PerlIO_apply_layers(aTHX_ PL_rsfp, NULL, ":utf8");
7800 }
7801 #endif
7802 PL_rsfp = NULL;
7803 }
7804 goto fake_eof;
7805 }
7806
7807 case KEY___SUB__:
7808 FUN0OP(CvCLONE(PL_compcv)
7809 ? newOP(OP_RUNCV, 0)
7810 : newPVOP(OP_RUNCV,0,NULL));
7811
7812 case KEY_AUTOLOAD:
7813 case KEY_DESTROY:
7814 case KEY_BEGIN:
7815 case KEY_UNITCHECK:
7816 case KEY_CHECK:
7817 case KEY_INIT:
7818 case KEY_END:
7819 if (PL_expect == XSTATE) {
7820 s = PL_bufptr;
7821 goto really_sub;
7822 }
7823 goto just_a_word;
7824
7825 case_KEY_CORE:
7826 {
7827 STRLEN olen = len;
7828 d = s;
7829 s += 2;
7830 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
7831 if ((*s == ':' && s[1] == ':')
7832 || (!(tmp = keyword(PL_tokenbuf, len, 1)) && *s == '\''))
7833 {
7834 s = d;
7835 len = olen;
7836 Copy(PL_bufptr, PL_tokenbuf, olen, char);
7837 goto just_a_word;
7838 }
7839 if (!tmp)
7840 Perl_croak(aTHX_ "CORE::%" UTF8f " is not a keyword",
7841 UTF8fARG(UTF, len, PL_tokenbuf));
7842 if (tmp < 0)
7843 tmp = -tmp;
7844 else if (tmp == KEY_require || tmp == KEY_do
7845 || tmp == KEY_glob)
7846 /* that's a way to remember we saw "CORE::" */
7847 orig_keyword = tmp;
7848 goto reserved_word;
7849 }
7850
7851 case KEY_abs:
7852 UNI(OP_ABS);
7853
7854 case KEY_alarm:
7855 UNI(OP_ALARM);
7856
7857 case KEY_accept:
7858 LOP(OP_ACCEPT,XTERM);
7859
7860 case KEY_and:
7861 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
7862 return REPORT(0);
7863 OPERATOR(ANDOP);
7864
7865 case KEY_atan2:
7866 LOP(OP_ATAN2,XTERM);
7867
7868 case KEY_bind:
7869 LOP(OP_BIND,XTERM);
7870
7871 case KEY_binmode:
7872 LOP(OP_BINMODE,XTERM);
7873
7874 case KEY_bless:
7875 LOP(OP_BLESS,XTERM);
7876
7877 case KEY_break:
7878 FUN0(OP_BREAK);
7879
7880 case KEY_chop:
7881 UNI(OP_CHOP);
7882
7883 case KEY_continue:
7884 /* We have to disambiguate the two senses of
7885 "continue". If the next token is a '{' then
7886 treat it as the start of a continue block;
7887 otherwise treat it as a control operator.
7888 */
7889 s = skipspace(s);
7890 if (*s == '{')
7891 PREBLOCK(CONTINUE);
7892 else
7893 FUN0(OP_CONTINUE);
7894
7895 case KEY_chdir:
7896 /* may use HOME */
7897 (void)gv_fetchpvs("ENV", GV_ADD|GV_NOTQUAL, SVt_PVHV);
7898 UNI(OP_CHDIR);
7899
7900 case KEY_close:
7901 UNI(OP_CLOSE);
7902
7903 case KEY_closedir:
7904 UNI(OP_CLOSEDIR);
7905
7906 case KEY_cmp:
7907 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7908 return REPORT(0);
7909 Eop(OP_SCMP);
7910
7911 case KEY_caller:
7912 UNI(OP_CALLER);
7913
7914 case KEY_crypt:
7915 #ifdef FCRYPT
7916 if (!PL_cryptseen) {
7917 PL_cryptseen = TRUE;
7918 init_des();
7919 }
7920 #endif
7921 LOP(OP_CRYPT,XTERM);
7922
7923 case KEY_chmod:
7924 LOP(OP_CHMOD,XTERM);
7925
7926 case KEY_chown:
7927 LOP(OP_CHOWN,XTERM);
7928
7929 case KEY_connect:
7930 LOP(OP_CONNECT,XTERM);
7931
7932 case KEY_chr:
7933 UNI(OP_CHR);
7934
7935 case KEY_cos:
7936 UNI(OP_COS);
7937
7938 case KEY_chroot:
7939 UNI(OP_CHROOT);
7940
7941 case KEY_default:
7942 PREBLOCK(DEFAULT);
7943
7944 case KEY_do:
7945 s = skipspace(s);
7946 if (*s == '{')
7947 PRETERMBLOCK(DO);
7948 if (*s != '\'') {
7949 *PL_tokenbuf = '&';
7950 d = scan_word(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
7951 1, &len);
7952 if (len && memNEs(PL_tokenbuf+1, len, "CORE")
7953 && !keyword(PL_tokenbuf + 1, len, 0)) {
7954 SSize_t off = s-SvPVX(PL_linestr);
7955 d = skipspace(d);
7956 s = SvPVX(PL_linestr)+off;
7957 if (*d == '(') {
7958 force_ident_maybe_lex('&');
7959 s = d;
7960 }
7961 }
7962 }
7963 if (orig_keyword == KEY_do) {
7964 orig_keyword = 0;
7965 pl_yylval.ival = 1;
7966 }
7967 else
7968 pl_yylval.ival = 0;
7969 OPERATOR(DO);
7970
7971 case KEY_die:
7972 PL_hints |= HINT_BLOCK_SCOPE;
7973 LOP(OP_DIE,XTERM);
7974
7975 case KEY_defined:
7976 UNI(OP_DEFINED);
7977
7978 case KEY_delete:
7979 UNI(OP_DELETE);
7980
7981 case KEY_dbmopen:
7982 Perl_populate_isa(aTHX_ STR_WITH_LEN("AnyDBM_File::ISA"),
7983 STR_WITH_LEN("NDBM_File::"),
7984 STR_WITH_LEN("DB_File::"),
7985 STR_WITH_LEN("GDBM_File::"),
7986 STR_WITH_LEN("SDBM_File::"),
7987 STR_WITH_LEN("ODBM_File::"),
7988 NULL);
7989 LOP(OP_DBMOPEN,XTERM);
7990
7991 case KEY_dbmclose:
7992 UNI(OP_DBMCLOSE);
7993
7994 case KEY_dump:
7995 LOOPX(OP_DUMP);
7996
7997 case KEY_else:
7998 PREBLOCK(ELSE);
7999
8000 case KEY_elsif:
8001 pl_yylval.ival = CopLINE(PL_curcop);
8002 OPERATOR(ELSIF);
8003
8004 case KEY_eq:
8005 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8006 return REPORT(0);
8007 Eop(OP_SEQ);
8008
8009 case KEY_exists:
8010 UNI(OP_EXISTS);
8011
8012 case KEY_exit:
8013 UNI(OP_EXIT);
8014
8015 case KEY_eval:
8016 s = skipspace(s);
8017 if (*s == '{') { /* block eval */
8018 PL_expect = XTERMBLOCK;
8019 UNIBRACK(OP_ENTERTRY);
8020 }
8021 else { /* string eval */
8022 PL_expect = XTERM;
8023 UNIBRACK(OP_ENTEREVAL);
8024 }
8025
8026 case KEY_evalbytes:
8027 PL_expect = XTERM;
8028 UNIBRACK(-OP_ENTEREVAL);
8029
8030 case KEY_eof:
8031 UNI(OP_EOF);
8032
8033 case KEY_exp:
8034 UNI(OP_EXP);
8035
8036 case KEY_each:
8037 UNI(OP_EACH);
8038
8039 case KEY_exec:
8040 LOP(OP_EXEC,XREF);
8041
8042 case KEY_endhostent:
8043 FUN0(OP_EHOSTENT);
8044
8045 case KEY_endnetent:
8046 FUN0(OP_ENETENT);
8047
8048 case KEY_endservent:
8049 FUN0(OP_ESERVENT);
8050
8051 case KEY_endprotoent:
8052 FUN0(OP_EPROTOENT);
8053
8054 case KEY_endpwent:
8055 FUN0(OP_EPWENT);
8056
8057 case KEY_endgrent:
8058 FUN0(OP_EGRENT);
8059
8060 case KEY_for:
8061 case KEY_foreach:
8062 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8063 return REPORT(0);
8064 pl_yylval.ival = CopLINE(PL_curcop);
8065 s = skipspace(s);
8066 if ( PL_expect == XSTATE
8067 && isIDFIRST_lazy_if_safe(s, PL_bufend, UTF))
8068 {
8069 char *p = s;
8070 SSize_t s_off = s - SvPVX(PL_linestr);
8071
8072 if ( memBEGINPs(p, (STRLEN) (PL_bufend - p), "my")
8073 && isSPACE(*(p + 2)))
8074 {
8075 p += 2;
8076 }
8077 else if ( memBEGINPs(p, (STRLEN) (PL_bufend - p), "our")
8078 && isSPACE(*(p + 3)))
8079 {
8080 p += 3;
8081 }
8082
8083 p = skipspace(p);
8084 /* skip optional package name, as in "for my abc $x (..)" */
8085 if (isIDFIRST_lazy_if_safe(p, PL_bufend, UTF)) {
8086 p = scan_word(p, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
8087 p = skipspace(p);
8088 }
8089 if (*p != '$' && *p != '\\')
8090 Perl_croak(aTHX_ "Missing $ on loop variable");
8091
8092 /* The buffer may have been reallocated, update s */
8093 s = SvPVX(PL_linestr) + s_off;
8094 }
8095 OPERATOR(FOR);
8096
8097 case KEY_formline:
8098 LOP(OP_FORMLINE,XTERM);
8099
8100 case KEY_fork:
8101 FUN0(OP_FORK);
8102
8103 case KEY_fc:
8104 UNI(OP_FC);
8105
8106 case KEY_fcntl:
8107 LOP(OP_FCNTL,XTERM);
8108
8109 case KEY_fileno:
8110 UNI(OP_FILENO);
8111
8112 case KEY_flock:
8113 LOP(OP_FLOCK,XTERM);
8114
8115 case KEY_gt:
8116 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8117 return REPORT(0);
8118 Rop(OP_SGT);
8119
8120 case KEY_ge:
8121 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8122 return REPORT(0);
8123 Rop(OP_SGE);
8124
8125 case KEY_grep:
8126 LOP(OP_GREPSTART, XREF);
8127
8128 case KEY_goto:
8129 LOOPX(OP_GOTO);
8130
8131 case KEY_gmtime:
8132 UNI(OP_GMTIME);
8133
8134 case KEY_getc:
8135 UNIDOR(OP_GETC);
8136
8137 case KEY_getppid:
8138 FUN0(OP_GETPPID);
8139
8140 case KEY_getpgrp:
8141 UNI(OP_GETPGRP);
8142
8143 case KEY_getpriority:
8144 LOP(OP_GETPRIORITY,XTERM);
8145
8146 case KEY_getprotobyname:
8147 UNI(OP_GPBYNAME);
8148
8149 case KEY_getprotobynumber:
8150 LOP(OP_GPBYNUMBER,XTERM);
8151
8152 case KEY_getprotoent:
8153 FUN0(OP_GPROTOENT);
8154
8155 case KEY_getpwent:
8156 FUN0(OP_GPWENT);
8157
8158 case KEY_getpwnam:
8159 UNI(OP_GPWNAM);
8160
8161 case KEY_getpwuid:
8162 UNI(OP_GPWUID);
8163
8164 case KEY_getpeername:
8165 UNI(OP_GETPEERNAME);
8166
8167 case KEY_gethostbyname:
8168 UNI(OP_GHBYNAME);
8169
8170 case KEY_gethostbyaddr:
8171 LOP(OP_GHBYADDR,XTERM);
8172
8173 case KEY_gethostent:
8174 FUN0(OP_GHOSTENT);
8175
8176 case KEY_getnetbyname:
8177 UNI(OP_GNBYNAME);
8178
8179 case KEY_getnetbyaddr:
8180 LOP(OP_GNBYADDR,XTERM);
8181
8182 case KEY_getnetent:
8183 FUN0(OP_GNETENT);
8184
8185 case KEY_getservbyname:
8186 LOP(OP_GSBYNAME,XTERM);
8187
8188 case KEY_getservbyport:
8189 LOP(OP_GSBYPORT,XTERM);
8190
8191 case KEY_getservent:
8192 FUN0(OP_GSERVENT);
8193
8194 case KEY_getsockname:
8195 UNI(OP_GETSOCKNAME);
8196
8197 case KEY_getsockopt:
8198 LOP(OP_GSOCKOPT,XTERM);
8199
8200 case KEY_getgrent:
8201 FUN0(OP_GGRENT);
8202
8203 case KEY_getgrnam:
8204 UNI(OP_GGRNAM);
8205
8206 case KEY_getgrgid:
8207 UNI(OP_GGRGID);
8208
8209 case KEY_getlogin:
8210 FUN0(OP_GETLOGIN);
8211
8212 case KEY_given:
8213 pl_yylval.ival = CopLINE(PL_curcop);
8214 Perl_ck_warner_d(aTHX_
8215 packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
8216 "given is experimental");
8217 OPERATOR(GIVEN);
8218
8219 case KEY_glob:
8220 LOP(
8221 orig_keyword==KEY_glob ? -OP_GLOB : OP_GLOB,
8222 XTERM
8223 );
8224
8225 case KEY_hex:
8226 UNI(OP_HEX);
8227
8228 case KEY_if:
8229 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8230 return REPORT(0);
8231 pl_yylval.ival = CopLINE(PL_curcop);
8232 OPERATOR(IF);
8233
8234 case KEY_index:
8235 LOP(OP_INDEX,XTERM);
8236
8237 case KEY_int:
8238 UNI(OP_INT);
8239
8240 case KEY_ioctl:
8241 LOP(OP_IOCTL,XTERM);
8242
8243 case KEY_join:
8244 LOP(OP_JOIN,XTERM);
8245
8246 case KEY_keys:
8247 UNI(OP_KEYS);
8248
8249 case KEY_kill:
8250 LOP(OP_KILL,XTERM);
8251
8252 case KEY_last:
8253 LOOPX(OP_LAST);
8254
8255 case KEY_lc:
8256 UNI(OP_LC);
8257
8258 case KEY_lcfirst:
8259 UNI(OP_LCFIRST);
8260
8261 case KEY_local:
8262 OPERATOR(LOCAL);
8263
8264 case KEY_length:
8265 UNI(OP_LENGTH);
8266
8267 case KEY_lt:
8268 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8269 return REPORT(0);
8270 Rop(OP_SLT);
8271
8272 case KEY_le:
8273 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8274 return REPORT(0);
8275 Rop(OP_SLE);
8276
8277 case KEY_localtime:
8278 UNI(OP_LOCALTIME);
8279
8280 case KEY_log:
8281 UNI(OP_LOG);
8282
8283 case KEY_link:
8284 LOP(OP_LINK,XTERM);
8285
8286 case KEY_listen:
8287 LOP(OP_LISTEN,XTERM);
8288
8289 case KEY_lock:
8290 UNI(OP_LOCK);
8291
8292 case KEY_lstat:
8293 UNI(OP_LSTAT);
8294
8295 case KEY_m:
8296 s = scan_pat(s,OP_MATCH);
8297 TERM(sublex_start());
8298
8299 case KEY_map:
8300 LOP(OP_MAPSTART, XREF);
8301
8302 case KEY_mkdir:
8303 LOP(OP_MKDIR,XTERM);
8304
8305 case KEY_msgctl:
8306 LOP(OP_MSGCTL,XTERM);
8307
8308 case KEY_msgget:
8309 LOP(OP_MSGGET,XTERM);
8310
8311 case KEY_msgrcv:
8312 LOP(OP_MSGRCV,XTERM);
8313
8314 case KEY_msgsnd:
8315 LOP(OP_MSGSND,XTERM);
8316
8317 case KEY_our:
8318 case KEY_my:
8319 case KEY_state:
8320 if (PL_in_my) {
8321 PL_bufptr = s;
8322 yyerror(Perl_form(aTHX_
8323 "Can't redeclare \"%s\" in \"%s\"",
8324 tmp == KEY_my ? "my" :
8325 tmp == KEY_state ? "state" : "our",
8326 PL_in_my == KEY_my ? "my" :
8327 PL_in_my == KEY_state ? "state" : "our"));
8328 }
8329 PL_in_my = (U16)tmp;
8330 s = skipspace(s);
8331 if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
8332 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
8333 if (memEQs(PL_tokenbuf, len, "sub"))
8334 goto really_sub;
8335 PL_in_my_stash = find_in_my_stash(PL_tokenbuf, len);
8336 if (!PL_in_my_stash) {
8337 char tmpbuf[1024];
8338 int len;
8339 PL_bufptr = s;
8340 len = my_snprintf(tmpbuf, sizeof(tmpbuf), "No such class %.1000s", PL_tokenbuf);
8341 PERL_MY_SNPRINTF_POST_GUARD(len, sizeof(tmpbuf));
8342 yyerror_pv(tmpbuf, UTF ? SVf_UTF8 : 0);
8343 }
8344 }
8345 else if (*s == '\\') {
8346 if (!FEATURE_MYREF_IS_ENABLED)
8347 Perl_croak(aTHX_ "The experimental declared_refs "
8348 "feature is not enabled");
8349 Perl_ck_warner_d(aTHX_
8350 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
8351 "Declaring references is experimental");
8352 }
8353 OPERATOR(MY);
8354
8355 case KEY_next:
8356 LOOPX(OP_NEXT);
8357
8358 case KEY_ne:
8359 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8360 return REPORT(0);
8361 Eop(OP_SNE);
8362
8363 case KEY_no:
8364 s = tokenize_use(0, s);
8365 TOKEN(USE);
8366
8367 case KEY_not:
8368 if (*s == '(' || (s = skipspace(s), *s == '('))
8369 FUN1(OP_NOT);
8370 else {
8371 if (!PL_lex_allbrackets
8372 && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
8373 {
8374 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
8375 }
8376 OPERATOR(NOTOP);
8377 }
8378
8379 case KEY_open:
8380 s = skipspace(s);
8381 if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
8382 const char *t;
8383 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE,
8384 &len);
8385 for (t=d; isSPACE(*t);)
8386 t++;
8387 if ( *t && strchr("|&*+-=!?:.", *t) && ckWARN_d(WARN_PRECEDENCE)
8388 /* [perl #16184] */
8389 && !(t[0] == '=' && t[1] == '>')
8390 && !(t[0] == ':' && t[1] == ':')
8391 && !keyword(s, d-s, 0)
8392 ) {
8393 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
8394 "Precedence problem: open %" UTF8f " should be open(%" UTF8f ")",
8395 UTF8fARG(UTF, d-s, s), UTF8fARG(UTF, d-s, s));
8396 }
8397 }
8398 LOP(OP_OPEN,XTERM);
8399
8400 case KEY_or:
8401 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
8402 return REPORT(0);
8403 pl_yylval.ival = OP_OR;
8404 OPERATOR(OROP);
8405
8406 case KEY_ord:
8407 UNI(OP_ORD);
8408
8409 case KEY_oct:
8410 UNI(OP_OCT);
8411
8412 case KEY_opendir:
8413 LOP(OP_OPEN_DIR,XTERM);
8414
8415 case KEY_print:
8416 checkcomma(s,PL_tokenbuf,"filehandle");
8417 LOP(OP_PRINT,XREF);
8418
8419 case KEY_printf:
8420 checkcomma(s,PL_tokenbuf,"filehandle");
8421 LOP(OP_PRTF,XREF);
8422
8423 case KEY_prototype:
8424 UNI(OP_PROTOTYPE);
8425
8426 case KEY_push:
8427 LOP(OP_PUSH,XTERM);
8428
8429 case KEY_pop:
8430 UNIDOR(OP_POP);
8431
8432 case KEY_pos:
8433 UNIDOR(OP_POS);
8434
8435 case KEY_pack:
8436 LOP(OP_PACK,XTERM);
8437
8438 case KEY_package:
8439 s = force_word(s,BAREWORD,FALSE,TRUE);
8440 s = skipspace(s);
8441 s = force_strict_version(s);
8442 PREBLOCK(PACKAGE);
8443
8444 case KEY_pipe:
8445 LOP(OP_PIPE_OP,XTERM);
8446
8447 case KEY_q:
8448 s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8449 if (!s)
8450 missingterm(NULL, 0);
8451 COPLINE_SET_FROM_MULTI_END;
8452 pl_yylval.ival = OP_CONST;
8453 TERM(sublex_start());
8454
8455 case KEY_quotemeta:
8456 UNI(OP_QUOTEMETA);
8457
8458 case KEY_qw: {
8459 OP *words = NULL;
8460 s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8461 if (!s)
8462 missingterm(NULL, 0);
8463 COPLINE_SET_FROM_MULTI_END;
8464 PL_expect = XOPERATOR;
8465 if (SvCUR(PL_lex_stuff)) {
8466 int warned_comma = !ckWARN(WARN_QW);
8467 int warned_comment = warned_comma;
8468 d = SvPV_force(PL_lex_stuff, len);
8469 while (len) {
8470 for (; isSPACE(*d) && len; --len, ++d)
8471 /**/;
8472 if (len) {
8473 SV *sv;
8474 const char *b = d;
8475 if (!warned_comma || !warned_comment) {
8476 for (; !isSPACE(*d) && len; --len, ++d) {
8477 if (!warned_comma && *d == ',') {
8478 Perl_warner(aTHX_ packWARN(WARN_QW),
8479 "Possible attempt to separate words with commas");
8480 ++warned_comma;
8481 }
8482 else if (!warned_comment && *d == '#') {
8483 Perl_warner(aTHX_ packWARN(WARN_QW),
8484 "Possible attempt to put comments in qw() list");
8485 ++warned_comment;
8486 }
8487 }
8488 }
8489 else {
8490 for (; !isSPACE(*d) && len; --len, ++d)
8491 /**/;
8492 }
8493 sv = newSVpvn_utf8(b, d-b, DO_UTF8(PL_lex_stuff));
8494 words = op_append_elem(OP_LIST, words,
8495 newSVOP(OP_CONST, 0, tokeq(sv)));
8496 }
8497 }
8498 }
8499 if (!words)
8500 words = newNULLLIST();
8501 SvREFCNT_dec_NN(PL_lex_stuff);
8502 PL_lex_stuff = NULL;
8503 PL_expect = XOPERATOR;
8504 pl_yylval.opval = sawparens(words);
8505 TOKEN(QWLIST);
8506 }
8507
8508 case KEY_qq:
8509 s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8510 if (!s)
8511 missingterm(NULL, 0);
8512 pl_yylval.ival = OP_STRINGIFY;
8513 if (SvIVX(PL_lex_stuff) == '\'')
8514 SvIV_set(PL_lex_stuff, 0); /* qq'$foo' should interpolate */
8515 TERM(sublex_start());
8516
8517 case KEY_qr:
8518 s = scan_pat(s,OP_QR);
8519 TERM(sublex_start());
8520
8521 case KEY_qx:
8522 s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8523 if (!s)
8524 missingterm(NULL, 0);
8525 pl_yylval.ival = OP_BACKTICK;
8526 TERM(sublex_start());
8527
8528 case KEY_return:
8529 OLDLOP(OP_RETURN);
8530
8531 case KEY_require:
8532 s = skipspace(s);
8533 if (isDIGIT(*s)) {
8534 s = force_version(s, FALSE);
8535 }
8536 else if (*s != 'v' || !isDIGIT(s[1])
8537 || (s = force_version(s, TRUE), *s == 'v'))
8538 {
8539 *PL_tokenbuf = '\0';
8540 s = force_word(s,BAREWORD,TRUE,TRUE);
8541 if (isIDFIRST_lazy_if_safe(PL_tokenbuf,
8542 PL_tokenbuf + sizeof(PL_tokenbuf),
8543 UTF))
8544 {
8545 gv_stashpvn(PL_tokenbuf, strlen(PL_tokenbuf),
8546 GV_ADD | (UTF ? SVf_UTF8 : 0));
8547 }
8548 else if (*s == '<')
8549 yyerror("<> at require-statement should be quotes");
8550 }
8551 if (orig_keyword == KEY_require) {
8552 orig_keyword = 0;
8553 pl_yylval.ival = 1;
8554 }
8555 else
8556 pl_yylval.ival = 0;
8557 PL_expect = PL_nexttoke ? XOPERATOR : XTERM;
8558 PL_bufptr = s;
8559 PL_last_uni = PL_oldbufptr;
8560 PL_last_lop_op = OP_REQUIRE;
8561 s = skipspace(s);
8562 return REPORT( (int)REQUIRE );
8563
8564 case KEY_reset:
8565 UNI(OP_RESET);
8566
8567 case KEY_redo:
8568 LOOPX(OP_REDO);
8569
8570 case KEY_rename:
8571 LOP(OP_RENAME,XTERM);
8572
8573 case KEY_rand:
8574 UNI(OP_RAND);
8575
8576 case KEY_rmdir:
8577 UNI(OP_RMDIR);
8578
8579 case KEY_rindex:
8580 LOP(OP_RINDEX,XTERM);
8581
8582 case KEY_read:
8583 LOP(OP_READ,XTERM);
8584
8585 case KEY_readdir:
8586 UNI(OP_READDIR);
8587
8588 case KEY_readline:
8589 UNIDOR(OP_READLINE);
8590
8591 case KEY_readpipe:
8592 UNIDOR(OP_BACKTICK);
8593
8594 case KEY_rewinddir:
8595 UNI(OP_REWINDDIR);
8596
8597 case KEY_recv:
8598 LOP(OP_RECV,XTERM);
8599
8600 case KEY_reverse:
8601 LOP(OP_REVERSE,XTERM);
8602
8603 case KEY_readlink:
8604 UNIDOR(OP_READLINK);
8605
8606 case KEY_ref:
8607 UNI(OP_REF);
8608
8609 case KEY_s:
8610 s = scan_subst(s);
8611 if (pl_yylval.opval)
8612 TERM(sublex_start());
8613 else
8614 TOKEN(1); /* force error */
8615
8616 case KEY_say:
8617 checkcomma(s,PL_tokenbuf,"filehandle");
8618 LOP(OP_SAY,XREF);
8619
8620 case KEY_chomp:
8621 UNI(OP_CHOMP);
8622
8623 case KEY_scalar:
8624 UNI(OP_SCALAR);
8625
8626 case KEY_select:
8627 LOP(OP_SELECT,XTERM);
8628
8629 case KEY_seek:
8630 LOP(OP_SEEK,XTERM);
8631
8632 case KEY_semctl:
8633 LOP(OP_SEMCTL,XTERM);
8634
8635 case KEY_semget:
8636 LOP(OP_SEMGET,XTERM);
8637
8638 case KEY_semop:
8639 LOP(OP_SEMOP,XTERM);
8640
8641 case KEY_send:
8642 LOP(OP_SEND,XTERM);
8643
8644 case KEY_setpgrp:
8645 LOP(OP_SETPGRP,XTERM);
8646
8647 case KEY_setpriority:
8648 LOP(OP_SETPRIORITY,XTERM);
8649
8650 case KEY_sethostent:
8651 UNI(OP_SHOSTENT);
8652
8653 case KEY_setnetent:
8654 UNI(OP_SNETENT);
8655
8656 case KEY_setservent:
8657 UNI(OP_SSERVENT);
8658
8659 case KEY_setprotoent:
8660 UNI(OP_SPROTOENT);
8661
8662 case KEY_setpwent:
8663 FUN0(OP_SPWENT);
8664
8665 case KEY_setgrent:
8666 FUN0(OP_SGRENT);
8667
8668 case KEY_seekdir:
8669 LOP(OP_SEEKDIR,XTERM);
8670
8671 case KEY_setsockopt:
8672 LOP(OP_SSOCKOPT,XTERM);
8673
8674 case KEY_shift:
8675 UNIDOR(OP_SHIFT);
8676
8677 case KEY_shmctl:
8678 LOP(OP_SHMCTL,XTERM);
8679
8680 case KEY_shmget:
8681 LOP(OP_SHMGET,XTERM);
8682
8683 case KEY_shmread:
8684 LOP(OP_SHMREAD,XTERM);
8685
8686 case KEY_shmwrite:
8687 LOP(OP_SHMWRITE,XTERM);
8688
8689 case KEY_shutdown:
8690 LOP(OP_SHUTDOWN,XTERM);
8691
8692 case KEY_sin:
8693 UNI(OP_SIN);
8694
8695 case KEY_sleep:
8696 UNI(OP_SLEEP);
8697
8698 case KEY_socket:
8699 LOP(OP_SOCKET,XTERM);
8700
8701 case KEY_socketpair:
8702 LOP(OP_SOCKPAIR,XTERM);
8703
8704 case KEY_sort:
8705 checkcomma(s,PL_tokenbuf,"subroutine name");
8706 s = skipspace(s);
8707 PL_expect = XTERM;
8708 s = force_word(s,BAREWORD,TRUE,TRUE);
8709 LOP(OP_SORT,XREF);
8710
8711 case KEY_split:
8712 LOP(OP_SPLIT,XTERM);
8713
8714 case KEY_sprintf:
8715 LOP(OP_SPRINTF,XTERM);
8716
8717 case KEY_splice:
8718 LOP(OP_SPLICE,XTERM);
8719
8720 case KEY_sqrt:
8721 UNI(OP_SQRT);
8722
8723 case KEY_srand:
8724 UNI(OP_SRAND);
8725
8726 case KEY_stat:
8727 UNI(OP_STAT);
8728
8729 case KEY_study:
8730 UNI(OP_STUDY);
8731
8732 case KEY_substr:
8733 LOP(OP_SUBSTR,XTERM);
8734
8735 case KEY_format:
8736 case KEY_sub:
8737 really_sub:
8738 {
8739 char * const tmpbuf = PL_tokenbuf + 1;
8740 bool have_name, have_proto;
8741 const int key = tmp;
8742 SV *format_name = NULL;
8743 bool is_sigsub = FEATURE_SIGNATURES_IS_ENABLED;
8744
8745 SSize_t off = s-SvPVX(PL_linestr);
8746 s = skipspace(s);
8747 d = SvPVX(PL_linestr)+off;
8748
8749 SAVEBOOL(PL_parser->sig_seen);
8750 PL_parser->sig_seen = FALSE;
8751
8752 if ( isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)
8753 || *s == '\''
8754 || (*s == ':' && s[1] == ':'))
8755 {
8756
8757 PL_expect = XATTRBLOCK;
8758 d = scan_word(s, tmpbuf, sizeof PL_tokenbuf - 1, TRUE,
8759 &len);
8760 if (key == KEY_format)
8761 format_name = S_newSV_maybe_utf8(aTHX_ s, d - s);
8762 *PL_tokenbuf = '&';
8763 if (memchr(tmpbuf, ':', len) || key != KEY_sub
8764 || pad_findmy_pvn(
8765 PL_tokenbuf, len + 1, 0
8766 ) != NOT_IN_PAD)
8767 sv_setpvn(PL_subname, tmpbuf, len);
8768 else {
8769 sv_setsv(PL_subname,PL_curstname);
8770 sv_catpvs(PL_subname,"::");
8771 sv_catpvn(PL_subname,tmpbuf,len);
8772 }
8773 if (SvUTF8(PL_linestr))
8774 SvUTF8_on(PL_subname);
8775 have_name = TRUE;
8776
8777
8778 s = skipspace(d);
8779 }
8780 else {
8781 if (key == KEY_my || key == KEY_our || key==KEY_state)
8782 {
8783 *d = '\0';
8784 /* diag_listed_as: Missing name in "%s sub" */
8785 Perl_croak(aTHX_
8786 "Missing name in \"%s\"", PL_bufptr);
8787 }
8788 PL_expect = XATTRTERM;
8789 sv_setpvs(PL_subname,"?");
8790 have_name = FALSE;
8791 }
8792
8793 if (key == KEY_format) {
8794 if (format_name) {
8795 NEXTVAL_NEXTTOKE.opval
8796 = newSVOP(OP_CONST,0, format_name);
8797 NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
8798 force_next(BAREWORD);
8799 }
8800 PREBLOCK(FORMAT);
8801 }
8802
8803 /* Look for a prototype */
8804 if (*s == '(' && !is_sigsub) {
8805 s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8806 if (!s)
8807 Perl_croak(aTHX_ "Prototype not terminated");
8808 COPLINE_SET_FROM_MULTI_END;
8809 (void)validate_proto(PL_subname, PL_lex_stuff,
8810 ckWARN(WARN_ILLEGALPROTO), 0);
8811 have_proto = TRUE;
8812
8813 s = skipspace(s);
8814 }
8815 else
8816 have_proto = FALSE;
8817
8818 if ( !(*s == ':' && s[1] != ':')
8819 && (*s != '{' && *s != '(') && key != KEY_format)
8820 {
8821 assert(key == KEY_sub || key == KEY_AUTOLOAD ||
8822 key == KEY_DESTROY || key == KEY_BEGIN ||
8823 key == KEY_UNITCHECK || key == KEY_CHECK ||
8824 key == KEY_INIT || key == KEY_END ||
8825 key == KEY_my || key == KEY_state ||
8826 key == KEY_our);
8827 if (!have_name)
8828 Perl_croak(aTHX_ "Illegal declaration of anonymous subroutine");
8829 else if (*s != ';' && *s != '}')
8830 Perl_croak(aTHX_ "Illegal declaration of subroutine %" SVf, SVfARG(PL_subname));
8831 }
8832
8833 if (have_proto) {
8834 NEXTVAL_NEXTTOKE.opval =
8835 newSVOP(OP_CONST, 0, PL_lex_stuff);
8836 PL_lex_stuff = NULL;
8837 force_next(THING);
8838 }
8839 if (!have_name) {
8840 if (PL_curstash)
8841 sv_setpvs(PL_subname, "__ANON__");
8842 else
8843 sv_setpvs(PL_subname, "__ANON__::__ANON__");
8844 if (is_sigsub)
8845 TOKEN(ANON_SIGSUB);
8846 else
8847 TOKEN(ANONSUB);
8848 }
8849 force_ident_maybe_lex('&');
8850 if (is_sigsub)
8851 TOKEN(SIGSUB);
8852 else
8853 TOKEN(SUB);
8854 }
8855
8856 case KEY_system:
8857 LOP(OP_SYSTEM,XREF);
8858
8859 case KEY_symlink:
8860 LOP(OP_SYMLINK,XTERM);
8861
8862 case KEY_syscall:
8863 LOP(OP_SYSCALL,XTERM);
8864
8865 case KEY_sysopen:
8866 LOP(OP_SYSOPEN,XTERM);
8867
8868 case KEY_sysseek:
8869 LOP(OP_SYSSEEK,XTERM);
8870
8871 case KEY_sysread:
8872 LOP(OP_SYSREAD,XTERM);
8873
8874 case KEY_syswrite:
8875 LOP(OP_SYSWRITE,XTERM);
8876
8877 case KEY_tr:
8878 case KEY_y:
8879 s = scan_trans(s);
8880 TERM(sublex_start());
8881
8882 case KEY_tell:
8883 UNI(OP_TELL);
8884
8885 case KEY_telldir:
8886 UNI(OP_TELLDIR);
8887
8888 case KEY_tie:
8889 LOP(OP_TIE,XTERM);
8890
8891 case KEY_tied:
8892 UNI(OP_TIED);
8893
8894 case KEY_time:
8895 FUN0(OP_TIME);
8896
8897 case KEY_times:
8898 FUN0(OP_TMS);
8899
8900 case KEY_truncate:
8901 LOP(OP_TRUNCATE,XTERM);
8902
8903 case KEY_uc:
8904 UNI(OP_UC);
8905
8906 case KEY_ucfirst:
8907 UNI(OP_UCFIRST);
8908
8909 case KEY_untie:
8910 UNI(OP_UNTIE);
8911
8912 case KEY_until:
8913 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8914 return REPORT(0);
8915 pl_yylval.ival = CopLINE(PL_curcop);
8916 OPERATOR(UNTIL);
8917
8918 case KEY_unless:
8919 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8920 return REPORT(0);
8921 pl_yylval.ival = CopLINE(PL_curcop);
8922 OPERATOR(UNLESS);
8923
8924 case KEY_unlink:
8925 LOP(OP_UNLINK,XTERM);
8926
8927 case KEY_undef:
8928 UNIDOR(OP_UNDEF);
8929
8930 case KEY_unpack:
8931 LOP(OP_UNPACK,XTERM);
8932
8933 case KEY_utime:
8934 LOP(OP_UTIME,XTERM);
8935
8936 case KEY_umask:
8937 UNIDOR(OP_UMASK);
8938
8939 case KEY_unshift:
8940 LOP(OP_UNSHIFT,XTERM);
8941
8942 case KEY_use:
8943 s = tokenize_use(1, s);
8944 TOKEN(USE);
8945
8946 case KEY_values:
8947 UNI(OP_VALUES);
8948
8949 case KEY_vec:
8950 LOP(OP_VEC,XTERM);
8951
8952 case KEY_when:
8953 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8954 return REPORT(0);
8955 pl_yylval.ival = CopLINE(PL_curcop);
8956 Perl_ck_warner_d(aTHX_
8957 packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
8958 "when is experimental");
8959 OPERATOR(WHEN);
8960
8961 case KEY_while:
8962 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8963 return REPORT(0);
8964 pl_yylval.ival = CopLINE(PL_curcop);
8965 OPERATOR(WHILE);
8966
8967 case KEY_warn:
8968 PL_hints |= HINT_BLOCK_SCOPE;
8969 LOP(OP_WARN,XTERM);
8970
8971 case KEY_wait:
8972 FUN0(OP_WAIT);
8973
8974 case KEY_waitpid:
8975 LOP(OP_WAITPID,XTERM);
8976
8977 case KEY_wantarray:
8978 FUN0(OP_WANTARRAY);
8979
8980 case KEY_write:
8981 /* Make sure $^L is defined. 0x0C is CTRL-L on ASCII platforms, and
8982 * we use the same number on EBCDIC */
8983 gv_fetchpvs("\x0C", GV_ADD|GV_NOTQUAL, SVt_PV);
8984 UNI(OP_ENTERWRITE);
8985
8986 case KEY_x:
8987 if (PL_expect == XOPERATOR) {
8988 if (*s == '=' && !PL_lex_allbrackets
8989 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
8990 {
8991 return REPORT(0);
8992 }
8993 Mop(OP_REPEAT);
8994 }
8995 check_uni();
8996 goto just_a_word;
8997
8998 case KEY_xor:
8999 if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
9000 return REPORT(0);
9001 pl_yylval.ival = OP_XOR;
9002 OPERATOR(OROP);
9003 }
9004 }}
9005 }
9006
9007 /*
9008 S_pending_ident
9009
9010 Looks up an identifier in the pad or in a package
9011
9012 is_sig indicates that this is a subroutine signature variable
9013 rather than a plain pad var.
9014
9015 Returns:
9016 PRIVATEREF if this is a lexical name.
9017 BAREWORD if this belongs to a package.
9018
9019 Structure:
9020 if we're in a my declaration
9021 croak if they tried to say my($foo::bar)
9022 build the ops for a my() declaration
9023 if it's an access to a my() variable
9024 build ops for access to a my() variable
9025 if in a dq string, and they've said @foo and we can't find @foo
9026 warn
9027 build ops for a bareword
9028 */
9029
9030 static int
9031 S_pending_ident(pTHX)
9032 {
9033 PADOFFSET tmp = 0;
9034 const char pit = (char)pl_yylval.ival;
9035 const STRLEN tokenbuf_len = strlen(PL_tokenbuf);
9036 /* All routes through this function want to know if there is a colon. */
9037 const char *const has_colon = (const char*) memchr (PL_tokenbuf, ':', tokenbuf_len);
9038
9039 DEBUG_T({ PerlIO_printf(Perl_debug_log,
9040 "### Pending identifier '%s'\n", PL_tokenbuf); });
9041 assert(tokenbuf_len >= 2);
9042
9043 /* if we're in a my(), we can't allow dynamics here.
9044 $foo'bar has already been turned into $foo::bar, so
9045 just check for colons.
9046
9047 if it's a legal name, the OP is a PADANY.
9048 */
9049 if (PL_in_my) {
9050 if (PL_in_my == KEY_our) { /* "our" is merely analogous to "my" */
9051 if (has_colon)
9052 /* diag_listed_as: No package name allowed for variable %s
9053 in "our" */
9054 yyerror_pv(Perl_form(aTHX_ "No package name allowed for "
9055 "%se %s in \"our\"",
9056 *PL_tokenbuf=='&' ?"subroutin":"variabl",
9057 PL_tokenbuf), UTF ? SVf_UTF8 : 0);
9058 tmp = allocmy(PL_tokenbuf, tokenbuf_len, UTF ? SVf_UTF8 : 0);
9059 }
9060 else {
9061 OP *o;
9062 if (has_colon) {
9063 /* "my" variable %s can't be in a package */
9064 /* PL_no_myglob is constant */
9065 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
9066 yyerror_pv(Perl_form(aTHX_ PL_no_myglob,
9067 PL_in_my == KEY_my ? "my" : "state",
9068 *PL_tokenbuf == '&' ? "subroutin" : "variabl",
9069 PL_tokenbuf),
9070 UTF ? SVf_UTF8 : 0);
9071 GCC_DIAG_RESTORE_STMT;
9072 }
9073
9074 if (PL_in_my == KEY_sigvar) {
9075 /* A signature 'padop' needs in addition, an op_first to
9076 * point to a child sigdefelem, and an extra field to hold
9077 * the signature index. We can achieve both by using an
9078 * UNOP_AUX and (ab)using the op_aux field to hold the
9079 * index. If we ever need more fields, use a real malloced
9080 * aux strut instead.
9081 */
9082 o = newUNOP_AUX(OP_ARGELEM, 0, NULL,
9083 INT2PTR(UNOP_AUX_item *,
9084 (PL_parser->sig_elems)));
9085 o->op_private |= ( PL_tokenbuf[0] == '$' ? OPpARGELEM_SV
9086 : PL_tokenbuf[0] == '@' ? OPpARGELEM_AV
9087 : OPpARGELEM_HV);
9088 }
9089 else
9090 o = newOP(OP_PADANY, 0);
9091 o->op_targ = allocmy(PL_tokenbuf, tokenbuf_len,
9092 UTF ? SVf_UTF8 : 0);
9093 if (PL_in_my == KEY_sigvar)
9094 PL_in_my = 0;
9095
9096 pl_yylval.opval = o;
9097 return PRIVATEREF;
9098 }
9099 }
9100
9101 /*
9102 build the ops for accesses to a my() variable.
9103 */
9104
9105 if (!has_colon) {
9106 if (!PL_in_my)
9107 tmp = pad_findmy_pvn(PL_tokenbuf, tokenbuf_len,
9108 0);
9109 if (tmp != NOT_IN_PAD) {
9110 /* might be an "our" variable" */
9111 if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
9112 /* build ops for a bareword */
9113 HV * const stash = PAD_COMPNAME_OURSTASH(tmp);
9114 HEK * const stashname = HvNAME_HEK(stash);
9115 SV * const sym = newSVhek(stashname);
9116 sv_catpvs(sym, "::");
9117 sv_catpvn_flags(sym, PL_tokenbuf+1, tokenbuf_len > 0 ? tokenbuf_len - 1 : 0, (UTF ? SV_CATUTF8 : SV_CATBYTES ));
9118 pl_yylval.opval = newSVOP(OP_CONST, 0, sym);
9119 pl_yylval.opval->op_private = OPpCONST_ENTERED;
9120 if (pit != '&')
9121 gv_fetchsv(sym,
9122 GV_ADDMULTI,
9123 ((PL_tokenbuf[0] == '$') ? SVt_PV
9124 : (PL_tokenbuf[0] == '@') ? SVt_PVAV
9125 : SVt_PVHV));
9126 return BAREWORD;
9127 }
9128
9129 pl_yylval.opval = newOP(OP_PADANY, 0);
9130 pl_yylval.opval->op_targ = tmp;
9131 return PRIVATEREF;
9132 }
9133 }
9134
9135 /*
9136 Whine if they've said @foo or @foo{key} in a doublequoted string,
9137 and @foo (or %foo) isn't a variable we can find in the symbol
9138 table.
9139 */
9140 if (ckWARN(WARN_AMBIGUOUS)
9141 && pit == '@'
9142 && PL_lex_state != LEX_NORMAL
9143 && !PL_lex_brackets)
9144 {
9145 GV *const gv = gv_fetchpvn_flags(PL_tokenbuf + 1, tokenbuf_len > 0 ? tokenbuf_len - 1 : 0,
9146 ( UTF ? SVf_UTF8 : 0 ) | GV_ADDMG,
9147 SVt_PVAV);
9148 if ((!gv || ((PL_tokenbuf[0] == '@') ? !GvAV(gv) : !GvHV(gv)))
9149 )
9150 {
9151 /* Downgraded from fatal to warning 20000522 mjd */
9152 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
9153 "Possible unintended interpolation of %" UTF8f
9154 " in string",
9155 UTF8fARG(UTF, tokenbuf_len, PL_tokenbuf));
9156 }
9157 }
9158
9159 /* build ops for a bareword */
9160 pl_yylval.opval = newSVOP(OP_CONST, 0,
9161 newSVpvn_flags(PL_tokenbuf + 1,
9162 tokenbuf_len > 0 ? tokenbuf_len - 1 : 0,
9163 UTF ? SVf_UTF8 : 0 ));
9164 pl_yylval.opval->op_private = OPpCONST_ENTERED;
9165 if (pit != '&')
9166 gv_fetchpvn_flags(PL_tokenbuf+1, tokenbuf_len > 0 ? tokenbuf_len - 1 : 0,
9167 (PL_in_eval ? GV_ADDMULTI : GV_ADD)
9168 | ( UTF ? SVf_UTF8 : 0 ),
9169 ((PL_tokenbuf[0] == '$') ? SVt_PV
9170 : (PL_tokenbuf[0] == '@') ? SVt_PVAV
9171 : SVt_PVHV));
9172 return BAREWORD;
9173 }
9174
9175 STATIC void
9176 S_checkcomma(pTHX_ const char *s, const char *name, const char *what)
9177 {
9178 PERL_ARGS_ASSERT_CHECKCOMMA;
9179
9180 if (*s == ' ' && s[1] == '(') { /* XXX gotta be a better way */
9181 if (ckWARN(WARN_SYNTAX)) {
9182 int level = 1;
9183 const char *w;
9184 for (w = s+2; *w && level; w++) {
9185 if (*w == '(')
9186 ++level;
9187 else if (*w == ')')
9188 --level;
9189 }
9190 while (isSPACE(*w))
9191 ++w;
9192 /* the list of chars below is for end of statements or
9193 * block / parens, boolean operators (&&, ||, //) and branch
9194 * constructs (or, and, if, until, unless, while, err, for).
9195 * Not a very solid hack... */
9196 if (!*w || !strchr(";&/|})]oaiuwef!=", *w))
9197 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9198 "%s (...) interpreted as function",name);
9199 }
9200 }
9201 while (s < PL_bufend && isSPACE(*s))
9202 s++;
9203 if (*s == '(')
9204 s++;
9205 while (s < PL_bufend && isSPACE(*s))
9206 s++;
9207 if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
9208 const char * const w = s;
9209 s += UTF ? UTF8SKIP(s) : 1;
9210 while (isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF))
9211 s += UTF ? UTF8SKIP(s) : 1;
9212 while (s < PL_bufend && isSPACE(*s))
9213 s++;
9214 if (*s == ',') {
9215 GV* gv;
9216 if (keyword(w, s - w, 0))
9217 return;
9218
9219 gv = gv_fetchpvn_flags(w, s - w, ( UTF ? SVf_UTF8 : 0 ), SVt_PVCV);
9220 if (gv && GvCVu(gv))
9221 return;
9222 if (s - w <= 254) {
9223 PADOFFSET off;
9224 char tmpbuf[256];
9225 Copy(w, tmpbuf+1, s - w, char);
9226 *tmpbuf = '&';
9227 off = pad_findmy_pvn(tmpbuf, s-w+1, 0);
9228 if (off != NOT_IN_PAD) return;
9229 }
9230 Perl_croak(aTHX_ "No comma allowed after %s", what);
9231 }
9232 }
9233 }
9234
9235 /* S_new_constant(): do any overload::constant lookup.
9236
9237 Either returns sv, or mortalizes/frees sv and returns a new SV*.
9238 Best used as sv=new_constant(..., sv, ...).
9239 If s, pv are NULL, calls subroutine with one argument,
9240 and <type> is used with error messages only.
9241 <type> is assumed to be well formed UTF-8.
9242
9243 If error_msg is not NULL, *error_msg will be set to any error encountered.
9244 Otherwise yyerror() will be used to output it */
9245
9246 STATIC SV *
9247 S_new_constant(pTHX_ const char *s, STRLEN len, const char *key, STRLEN keylen,
9248 SV *sv, SV *pv, const char *type, STRLEN typelen,
9249 const char ** error_msg)
9250 {
9251 dSP;
9252 HV * table = GvHV(PL_hintgv); /* ^H */
9253 SV *res;
9254 SV *errsv = NULL;
9255 SV **cvp;
9256 SV *cv, *typesv;
9257 const char *why1 = "", *why2 = "", *why3 = "";
9258
9259 PERL_ARGS_ASSERT_NEW_CONSTANT;
9260 /* We assume that this is true: */
9261 if (*key == 'c') { assert (strEQ(key, "charnames")); }
9262 assert(type || s);
9263
9264 sv_2mortal(sv); /* Parent created it permanently */
9265 if (!table
9266 || ! (PL_hints & HINT_LOCALIZE_HH)
9267 || ! (cvp = hv_fetch(table, key, keylen, FALSE))
9268 || ! SvOK(*cvp))
9269 {
9270 char *msg;
9271
9272 /* Here haven't found what we're looking for. If it is charnames,
9273 * perhaps it needs to be loaded. Try doing that before giving up */
9274 if (*key == 'c') {
9275 Perl_load_module(aTHX_
9276 0,
9277 newSVpvs("_charnames"),
9278 /* version parameter; no need to specify it, as if
9279 * we get too early a version, will fail anyway,
9280 * not being able to find '_charnames' */
9281 NULL,
9282 newSVpvs(":full"),
9283 newSVpvs(":short"),
9284 NULL);
9285 assert(sp == PL_stack_sp);
9286 table = GvHV(PL_hintgv);
9287 if (table
9288 && (PL_hints & HINT_LOCALIZE_HH)
9289 && (cvp = hv_fetch(table, key, keylen, FALSE))
9290 && SvOK(*cvp))
9291 {
9292 goto now_ok;
9293 }
9294 }
9295 if (!table || !(PL_hints & HINT_LOCALIZE_HH)) {
9296 msg = Perl_form(aTHX_
9297 "Constant(%.*s) unknown",
9298 (int)(type ? typelen : len),
9299 (type ? type: s));
9300 }
9301 else {
9302 why1 = "$^H{";
9303 why2 = key;
9304 why3 = "} is not defined";
9305 report:
9306 if (*key == 'c') {
9307 msg = Perl_form(aTHX_
9308 /* The +3 is for '\N{'; -4 for that, plus '}' */
9309 "Unknown charname '%.*s'", (int)typelen - 4, type + 3
9310 );
9311 }
9312 else {
9313 msg = Perl_form(aTHX_ "Constant(%.*s): %s%s%s",
9314 (int)(type ? typelen : len),
9315 (type ? type: s), why1, why2, why3);
9316 }
9317 }
9318 if (error_msg) {
9319 *error_msg = msg;
9320 }
9321 else {
9322 yyerror_pv(msg, UTF ? SVf_UTF8 : 0);
9323 }
9324 return SvREFCNT_inc_simple_NN(sv);
9325 }
9326 now_ok:
9327 cv = *cvp;
9328 if (!pv && s)
9329 pv = newSVpvn_flags(s, len, SVs_TEMP);
9330 if (type && pv)
9331 typesv = newSVpvn_flags(type, typelen, SVs_TEMP);
9332 else
9333 typesv = &PL_sv_undef;
9334
9335 PUSHSTACKi(PERLSI_OVERLOAD);
9336 ENTER ;
9337 SAVETMPS;
9338
9339 PUSHMARK(SP) ;
9340 EXTEND(sp, 3);
9341 if (pv)
9342 PUSHs(pv);
9343 PUSHs(sv);
9344 if (pv)
9345 PUSHs(typesv);
9346 PUTBACK;
9347 call_sv(cv, G_SCALAR | ( PL_in_eval ? 0 : G_EVAL));
9348
9349 SPAGAIN ;
9350
9351 /* Check the eval first */
9352 if (!PL_in_eval && ((errsv = ERRSV), SvTRUE_NN(errsv))) {
9353 STRLEN errlen;
9354 const char * errstr;
9355 sv_catpvs(errsv, "Propagated");
9356 errstr = SvPV_const(errsv, errlen);
9357 yyerror_pvn(errstr, errlen, 0); /* Duplicates the message inside eval */
9358 (void)POPs;
9359 res = SvREFCNT_inc_simple_NN(sv);
9360 }
9361 else {
9362 res = POPs;
9363 SvREFCNT_inc_simple_void_NN(res);
9364 }
9365
9366 PUTBACK ;
9367 FREETMPS ;
9368 LEAVE ;
9369 POPSTACK;
9370
9371 if (!SvOK(res)) {
9372 why1 = "Call to &{$^H{";
9373 why2 = key;
9374 why3 = "}} did not return a defined value";
9375 sv = res;
9376 (void)sv_2mortal(sv);
9377 goto report;
9378 }
9379
9380 return res;
9381 }
9382
9383 PERL_STATIC_INLINE void
9384 S_parse_ident(pTHX_ char **s, char **d, char * const e, int allow_package,
9385 bool is_utf8, bool check_dollar, bool tick_warn)
9386 {
9387 int saw_tick = 0;
9388 const char *olds = *s;
9389 PERL_ARGS_ASSERT_PARSE_IDENT;
9390
9391 while (*s < PL_bufend) {
9392 if (*d >= e)
9393 Perl_croak(aTHX_ "%s", ident_too_long);
9394 if (is_utf8 && isIDFIRST_utf8_safe(*s, PL_bufend)) {
9395 /* The UTF-8 case must come first, otherwise things
9396 * like c\N{COMBINING TILDE} would start failing, as the
9397 * isWORDCHAR_A case below would gobble the 'c' up.
9398 */
9399
9400 char *t = *s + UTF8SKIP(*s);
9401 while (isIDCONT_utf8_safe((const U8*) t, (const U8*) PL_bufend)) {
9402 t += UTF8SKIP(t);
9403 }
9404 if (*d + (t - *s) > e)
9405 Perl_croak(aTHX_ "%s", ident_too_long);
9406 Copy(*s, *d, t - *s, char);
9407 *d += t - *s;
9408 *s = t;
9409 }
9410 else if ( isWORDCHAR_A(**s) ) {
9411 do {
9412 *(*d)++ = *(*s)++;
9413 } while (isWORDCHAR_A(**s) && *d < e);
9414 }
9415 else if ( allow_package
9416 && **s == '\''
9417 && isIDFIRST_lazy_if_safe((*s)+1, PL_bufend, is_utf8))
9418 {
9419 *(*d)++ = ':';
9420 *(*d)++ = ':';
9421 (*s)++;
9422 saw_tick++;
9423 }
9424 else if (allow_package && **s == ':' && (*s)[1] == ':'
9425 /* Disallow things like Foo::$bar. For the curious, this is
9426 * the code path that triggers the "Bad name after" warning
9427 * when looking for barewords.
9428 */
9429 && !(check_dollar && (*s)[2] == '$')) {
9430 *(*d)++ = *(*s)++;
9431 *(*d)++ = *(*s)++;
9432 }
9433 else
9434 break;
9435 }
9436 if (UNLIKELY(tick_warn && saw_tick && PL_lex_state == LEX_INTERPNORMAL
9437 && !PL_lex_brackets && ckWARN(WARN_SYNTAX))) {
9438 char *d;
9439 char *d2;
9440 Newx(d, *s - olds + saw_tick + 2, char); /* +2 for $# */
9441 d2 = d;
9442 SAVEFREEPV(d);
9443 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9444 "Old package separator used in string");
9445 if (olds[-1] == '#')
9446 *d2++ = olds[-2];
9447 *d2++ = olds[-1];
9448 while (olds < *s) {
9449 if (*olds == '\'') {
9450 *d2++ = '\\';
9451 *d2++ = *olds++;
9452 }
9453 else
9454 *d2++ = *olds++;
9455 }
9456 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9457 "\t(Did you mean \"%" UTF8f "\" instead?)\n",
9458 UTF8fARG(is_utf8, d2-d, d));
9459 }
9460 return;
9461 }
9462
9463 /* Returns a NUL terminated string, with the length of the string written to
9464 *slp
9465 */
9466 char *
9467 Perl_scan_word(pTHX_ char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp)
9468 {
9469 char *d = dest;
9470 char * const e = d + destlen - 3; /* two-character token, ending NUL */
9471 bool is_utf8 = cBOOL(UTF);
9472
9473 PERL_ARGS_ASSERT_SCAN_WORD;
9474
9475 parse_ident(&s, &d, e, allow_package, is_utf8, TRUE, FALSE);
9476 *d = '\0';
9477 *slp = d - dest;
9478 return s;
9479 }
9480
9481 /* Is the byte 'd' a legal single character identifier name? 'u' is true
9482 * iff Unicode semantics are to be used. The legal ones are any of:
9483 * a) all ASCII characters except:
9484 * 1) control and space-type ones, like NUL, SOH, \t, and SPACE;
9485 * 2) '{'
9486 * The final case currently doesn't get this far in the program, so we
9487 * don't test for it. If that were to change, it would be ok to allow it.
9488 * b) When not under Unicode rules, any upper Latin1 character
9489 * c) Otherwise, when unicode rules are used, all XIDS characters.
9490 *
9491 * Because all ASCII characters have the same representation whether
9492 * encoded in UTF-8 or not, we can use the foo_A macros below and '\0' and
9493 * '{' without knowing if is UTF-8 or not. */
9494 #define VALID_LEN_ONE_IDENT(s, e, is_utf8) \
9495 (isGRAPH_A(*(s)) || ((is_utf8) \
9496 ? isIDFIRST_utf8_safe(s, e) \
9497 : (isGRAPH_L1(*s) \
9498 && LIKELY((U8) *(s) != LATIN1_TO_NATIVE(0xAD)))))
9499
9500 STATIC char *
9501 S_scan_ident(pTHX_ char *s, char *dest, STRLEN destlen, I32 ck_uni)
9502 {
9503 I32 herelines = PL_parser->herelines;
9504 SSize_t bracket = -1;
9505 char funny = *s++;
9506 char *d = dest;
9507 char * const e = d + destlen - 3; /* two-character token, ending NUL */
9508 bool is_utf8 = cBOOL(UTF);
9509 I32 orig_copline = 0, tmp_copline = 0;
9510
9511 PERL_ARGS_ASSERT_SCAN_IDENT;
9512
9513 if (isSPACE(*s) || !*s)
9514 s = skipspace(s);
9515 if (isDIGIT(*s)) {
9516 while (isDIGIT(*s)) {
9517 if (d >= e)
9518 Perl_croak(aTHX_ "%s", ident_too_long);
9519 *d++ = *s++;
9520 }
9521 }
9522 else { /* See if it is a "normal" identifier */
9523 parse_ident(&s, &d, e, 1, is_utf8, FALSE, TRUE);
9524 }
9525 *d = '\0';
9526 d = dest;
9527 if (*d) {
9528 /* Either a digit variable, or parse_ident() found an identifier
9529 (anything valid as a bareword), so job done and return. */
9530 if (PL_lex_state != LEX_NORMAL)
9531 PL_lex_state = LEX_INTERPENDMAYBE;
9532 return s;
9533 }
9534
9535 /* Here, it is not a run-of-the-mill identifier name */
9536
9537 if (*s == '$' && s[1]
9538 && ( isIDFIRST_lazy_if_safe(s+1, PL_bufend, is_utf8)
9539 || isDIGIT_A((U8)s[1])
9540 || s[1] == '$'
9541 || s[1] == '{'
9542 || memBEGINs(s+1, (STRLEN) (PL_bufend - (s+1)), "::")) )
9543 {
9544 /* Dereferencing a value in a scalar variable.
9545 The alternatives are different syntaxes for a scalar variable.
9546 Using ' as a leading package separator isn't allowed. :: is. */
9547 return s;
9548 }
9549 /* Handle the opening { of @{...}, &{...}, *{...}, %{...}, ${...} */
9550 if (*s == '{') {
9551 bracket = s - SvPVX(PL_linestr);
9552 s++;
9553 orig_copline = CopLINE(PL_curcop);
9554 if (s < PL_bufend && isSPACE(*s)) {
9555 s = skipspace(s);
9556 }
9557 }
9558 if ((s <= PL_bufend - (is_utf8)
9559 ? UTF8SKIP(s)
9560 : 1)
9561 && VALID_LEN_ONE_IDENT(s, PL_bufend, is_utf8))
9562 {
9563 if (is_utf8) {
9564 const STRLEN skip = UTF8SKIP(s);
9565 STRLEN i;
9566 d[skip] = '\0';
9567 for ( i = 0; i < skip; i++ )
9568 d[i] = *s++;
9569 }
9570 else {
9571 *d = *s++;
9572 d[1] = '\0';
9573 }
9574 }
9575 /* Convert $^F, ${^F} and the ^F of ${^FOO} to control characters */
9576 if (*d == '^' && *s && isCONTROLVAR(*s)) {
9577 *d = toCTRL(*s);
9578 s++;
9579 }
9580 /* Warn about ambiguous code after unary operators if {...} notation isn't
9581 used. There's no difference in ambiguity; it's merely a heuristic
9582 about when not to warn. */
9583 else if (ck_uni && bracket == -1)
9584 check_uni();
9585 if (bracket != -1) {
9586 bool skip;
9587 char *s2;
9588 /* If we were processing {...} notation then... */
9589 if (isIDFIRST_lazy_if_safe(d, e, is_utf8)
9590 || (!isPRINT(*d) /* isCNTRL(d), plus all non-ASCII */
9591 && isWORDCHAR(*s))
9592 ) {
9593 /* note we have to check for a normal identifier first,
9594 * as it handles utf8 symbols, and only after that has
9595 * been ruled out can we look at the caret words */
9596 if (isIDFIRST_lazy_if_safe(d, e, is_utf8) ) {
9597 /* if it starts as a valid identifier, assume that it is one.
9598 (the later check for } being at the expected point will trap
9599 cases where this doesn't pan out.) */
9600 d += is_utf8 ? UTF8SKIP(d) : 1;
9601 parse_ident(&s, &d, e, 1, is_utf8, TRUE, TRUE);
9602 *d = '\0';
9603 }
9604 else { /* caret word: ${^Foo} ${^CAPTURE[0]} */
9605 d++;
9606 while (isWORDCHAR(*s) && d < e) {
9607 *d++ = *s++;
9608 }
9609 if (d >= e)
9610 Perl_croak(aTHX_ "%s", ident_too_long);
9611 *d = '\0';
9612 }
9613 tmp_copline = CopLINE(PL_curcop);
9614 if (s < PL_bufend && isSPACE(*s)) {
9615 s = skipspace(s);
9616 }
9617 if ((*s == '[' || (*s == '{' && strNE(dest, "sub")))) {
9618 /* ${foo[0]} and ${foo{bar}} and ${^CAPTURE[0]} notation. */
9619 if (ckWARN(WARN_AMBIGUOUS) && keyword(dest, d - dest, 0)) {
9620 const char * const brack =
9621 (const char *)
9622 ((*s == '[') ? "[...]" : "{...}");
9623 orig_copline = CopLINE(PL_curcop);
9624 CopLINE_set(PL_curcop, tmp_copline);
9625 /* diag_listed_as: Ambiguous use of %c{%s[...]} resolved to %c%s[...] */
9626 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
9627 "Ambiguous use of %c{%s%s} resolved to %c%s%s",
9628 funny, dest, brack, funny, dest, brack);
9629 CopLINE_set(PL_curcop, orig_copline);
9630 }
9631 bracket++;
9632 PL_lex_brackstack[PL_lex_brackets++] = (char)(XOPERATOR | XFAKEBRACK);
9633 PL_lex_allbrackets++;
9634 return s;
9635 }
9636 }
9637
9638 if ( !tmp_copline )
9639 tmp_copline = CopLINE(PL_curcop);
9640 if ((skip = s < PL_bufend && isSPACE(*s))) {
9641 /* Avoid incrementing line numbers or resetting PL_linestart,
9642 in case we have to back up. */
9643 STRLEN s_off = s - SvPVX(PL_linestr);
9644 s2 = peekspace(s);
9645 s = SvPVX(PL_linestr) + s_off;
9646 }
9647 else
9648 s2 = s;
9649
9650 /* Expect to find a closing } after consuming any trailing whitespace.
9651 */
9652 if (*s2 == '}') {
9653 /* Now increment line numbers if applicable. */
9654 if (skip)
9655 s = skipspace(s);
9656 s++;
9657 if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) {
9658 PL_lex_state = LEX_INTERPEND;
9659 PL_expect = XREF;
9660 }
9661 if (PL_lex_state == LEX_NORMAL) {
9662 if (ckWARN(WARN_AMBIGUOUS)
9663 && (keyword(dest, d - dest, 0)
9664 || get_cvn_flags(dest, d - dest, is_utf8
9665 ? SVf_UTF8
9666 : 0)))
9667 {
9668 SV *tmp = newSVpvn_flags( dest, d - dest,
9669 SVs_TEMP | (is_utf8 ? SVf_UTF8 : 0) );
9670 if (funny == '#')
9671 funny = '@';
9672 orig_copline = CopLINE(PL_curcop);
9673 CopLINE_set(PL_curcop, tmp_copline);
9674 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
9675 "Ambiguous use of %c{%" SVf "} resolved to %c%" SVf,
9676 funny, SVfARG(tmp), funny, SVfARG(tmp));
9677 CopLINE_set(PL_curcop, orig_copline);
9678 }
9679 }
9680 }
9681 else {
9682 /* Didn't find the closing } at the point we expected, so restore
9683 state such that the next thing to process is the opening { and */
9684 s = SvPVX(PL_linestr) + bracket; /* let the parser handle it */
9685 CopLINE_set(PL_curcop, orig_copline);
9686 PL_parser->herelines = herelines;
9687 *dest = '\0';
9688 PL_parser->sub_no_recover = TRUE;
9689 }
9690 }
9691 else if ( PL_lex_state == LEX_INTERPNORMAL
9692 && !PL_lex_brackets
9693 && !intuit_more(s, PL_bufend))
9694 PL_lex_state = LEX_INTERPEND;
9695 return s;
9696 }
9697
9698 static bool
9699 S_pmflag(pTHX_ const char* const valid_flags, U32 * pmfl, char** s, char* charset, unsigned int * x_mod_count) {
9700
9701 /* Adds, subtracts to/from 'pmfl' based on the next regex modifier flag
9702 * found in the parse starting at 's', based on the subset that are valid
9703 * in this context input to this routine in 'valid_flags'. Advances s.
9704 * Returns TRUE if the input should be treated as a valid flag, so the next
9705 * char may be as well; otherwise FALSE. 'charset' should point to a NUL
9706 * upon first call on the current regex. This routine will set it to any
9707 * charset modifier found. The caller shouldn't change it. This way,
9708 * another charset modifier encountered in the parse can be detected as an
9709 * error, as we have decided to allow only one */
9710
9711 const char c = **s;
9712 STRLEN charlen = UTF ? UTF8SKIP(*s) : 1;
9713
9714 if ( charlen != 1 || ! strchr(valid_flags, c) ) {
9715 if (isWORDCHAR_lazy_if_safe( *s, PL_bufend, UTF)) {
9716 yyerror_pv(Perl_form(aTHX_ "Unknown regexp modifier \"/%.*s\"", (int)charlen, *s),
9717 UTF ? SVf_UTF8 : 0);
9718 (*s) += charlen;
9719 /* Pretend that it worked, so will continue processing before
9720 * dieing */
9721 return TRUE;
9722 }
9723 return FALSE;
9724 }
9725
9726 switch (c) {
9727
9728 CASE_STD_PMMOD_FLAGS_PARSE_SET(pmfl, *x_mod_count);
9729 case GLOBAL_PAT_MOD: *pmfl |= PMf_GLOBAL; break;
9730 case CONTINUE_PAT_MOD: *pmfl |= PMf_CONTINUE; break;
9731 case ONCE_PAT_MOD: *pmfl |= PMf_KEEP; break;
9732 case KEEPCOPY_PAT_MOD: *pmfl |= RXf_PMf_KEEPCOPY; break;
9733 case NONDESTRUCT_PAT_MOD: *pmfl |= PMf_NONDESTRUCT; break;
9734 case LOCALE_PAT_MOD:
9735 if (*charset) {
9736 goto multiple_charsets;
9737 }
9738 set_regex_charset(pmfl, REGEX_LOCALE_CHARSET);
9739 *charset = c;
9740 break;
9741 case UNICODE_PAT_MOD:
9742 if (*charset) {
9743 goto multiple_charsets;
9744 }
9745 set_regex_charset(pmfl, REGEX_UNICODE_CHARSET);
9746 *charset = c;
9747 break;
9748 case ASCII_RESTRICT_PAT_MOD:
9749 if (! *charset) {
9750 set_regex_charset(pmfl, REGEX_ASCII_RESTRICTED_CHARSET);
9751 }
9752 else {
9753
9754 /* Error if previous modifier wasn't an 'a', but if it was, see
9755 * if, and accept, a second occurrence (only) */
9756 if (*charset != 'a'
9757 || get_regex_charset(*pmfl)
9758 != REGEX_ASCII_RESTRICTED_CHARSET)
9759 {
9760 goto multiple_charsets;
9761 }
9762 set_regex_charset(pmfl, REGEX_ASCII_MORE_RESTRICTED_CHARSET);
9763 }
9764 *charset = c;
9765 break;
9766 case DEPENDS_PAT_MOD:
9767 if (*charset) {
9768 goto multiple_charsets;
9769 }
9770 set_regex_charset(pmfl, REGEX_DEPENDS_CHARSET);
9771 *charset = c;
9772 break;
9773 }
9774
9775 (*s)++;
9776 return TRUE;
9777
9778 multiple_charsets:
9779 if (*charset != c) {
9780 yyerror(Perl_form(aTHX_ "Regexp modifiers \"/%c\" and \"/%c\" are mutually exclusive", *charset, c));
9781 }
9782 else if (c == 'a') {
9783 /* diag_listed_as: Regexp modifier "/%c" may appear a maximum of twice */
9784 yyerror("Regexp modifier \"/a\" may appear a maximum of twice");
9785 }
9786 else {
9787 yyerror(Perl_form(aTHX_ "Regexp modifier \"/%c\" may not appear twice", c));
9788 }
9789
9790 /* Pretend that it worked, so will continue processing before dieing */
9791 (*s)++;
9792 return TRUE;
9793 }
9794
9795 STATIC char *
9796 S_scan_pat(pTHX_ char *start, I32 type)
9797 {
9798 PMOP *pm;
9799 char *s;
9800 const char * const valid_flags =
9801 (const char *)((type == OP_QR) ? QR_PAT_MODS : M_PAT_MODS);
9802 char charset = '\0'; /* character set modifier */
9803 unsigned int x_mod_count = 0;
9804
9805 PERL_ARGS_ASSERT_SCAN_PAT;
9806
9807 s = scan_str(start,TRUE,FALSE, (PL_in_eval & EVAL_RE_REPARSING), NULL);
9808 if (!s)
9809 Perl_croak(aTHX_ "Search pattern not terminated");
9810
9811 pm = (PMOP*)newPMOP(type, 0);
9812 if (PL_multi_open == '?') {
9813 /* This is the only point in the code that sets PMf_ONCE: */
9814 pm->op_pmflags |= PMf_ONCE;
9815
9816 /* Hence it's safe to do this bit of PMOP book-keeping here, which
9817 allows us to restrict the list needed by reset to just the ??
9818 matches. */
9819 assert(type != OP_TRANS);
9820 if (PL_curstash) {
9821 MAGIC *mg = mg_find((const SV *)PL_curstash, PERL_MAGIC_symtab);
9822 U32 elements;
9823 if (!mg) {
9824 mg = sv_magicext(MUTABLE_SV(PL_curstash), 0, PERL_MAGIC_symtab, 0, 0,
9825 0);
9826 }
9827 elements = mg->mg_len / sizeof(PMOP**);
9828 Renewc(mg->mg_ptr, elements + 1, PMOP*, char);
9829 ((PMOP**)mg->mg_ptr) [elements++] = pm;
9830 mg->mg_len = elements * sizeof(PMOP**);
9831 PmopSTASH_set(pm,PL_curstash);
9832 }
9833 }
9834
9835 /* if qr/...(?{..}).../, then need to parse the pattern within a new
9836 * anon CV. False positives like qr/[(?{]/ are harmless */
9837
9838 if (type == OP_QR) {
9839 STRLEN len;
9840 char *e, *p = SvPV(PL_lex_stuff, len);
9841 e = p + len;
9842 for (; p < e; p++) {
9843 if (p[0] == '(' && p[1] == '?'
9844 && (p[2] == '{' || (p[2] == '?' && p[3] == '{')))
9845 {
9846 pm->op_pmflags |= PMf_HAS_CV;
9847 break;
9848 }
9849 }
9850 pm->op_pmflags |= PMf_IS_QR;
9851 }
9852
9853 while (*s && S_pmflag(aTHX_ valid_flags, &(pm->op_pmflags),
9854 &s, &charset, &x_mod_count))
9855 {};
9856 /* issue a warning if /c is specified,but /g is not */
9857 if ((pm->op_pmflags & PMf_CONTINUE) && !(pm->op_pmflags & PMf_GLOBAL))
9858 {
9859 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
9860 "Use of /c modifier is meaningless without /g" );
9861 }
9862
9863 PL_lex_op = (OP*)pm;
9864 pl_yylval.ival = OP_MATCH;
9865 return s;
9866 }
9867
9868 STATIC char *
9869 S_scan_subst(pTHX_ char *start)
9870 {
9871 char *s;
9872 PMOP *pm;
9873 I32 first_start;
9874 line_t first_line;
9875 line_t linediff = 0;
9876 I32 es = 0;
9877 char charset = '\0'; /* character set modifier */
9878 unsigned int x_mod_count = 0;
9879 char *t;
9880
9881 PERL_ARGS_ASSERT_SCAN_SUBST;
9882
9883 pl_yylval.ival = OP_NULL;
9884
9885 s = scan_str(start, TRUE, FALSE, FALSE, &t);
9886
9887 if (!s)
9888 Perl_croak(aTHX_ "Substitution pattern not terminated");
9889
9890 s = t;
9891
9892 first_start = PL_multi_start;
9893 first_line = CopLINE(PL_curcop);
9894 s = scan_str(s,FALSE,FALSE,FALSE,NULL);
9895 if (!s) {
9896 SvREFCNT_dec_NN(PL_lex_stuff);
9897 PL_lex_stuff = NULL;
9898 Perl_croak(aTHX_ "Substitution replacement not terminated");
9899 }
9900 PL_multi_start = first_start; /* so whole substitution is taken together */
9901
9902 pm = (PMOP*)newPMOP(OP_SUBST, 0);
9903
9904
9905 while (*s) {
9906 if (*s == EXEC_PAT_MOD) {
9907 s++;
9908 es++;
9909 }
9910 else if (! S_pmflag(aTHX_ S_PAT_MODS, &(pm->op_pmflags),
9911 &s, &charset, &x_mod_count))
9912 {
9913 break;
9914 }
9915 }
9916
9917 if ((pm->op_pmflags & PMf_CONTINUE)) {
9918 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), "Use of /c modifier is meaningless in s///" );
9919 }
9920
9921 if (es) {
9922 SV * const repl = newSVpvs("");
9923
9924 PL_multi_end = 0;
9925 pm->op_pmflags |= PMf_EVAL;
9926 for (; es > 1; es--) {
9927 sv_catpvs(repl, "eval ");
9928 }
9929 sv_catpvs(repl, "do {");
9930 sv_catsv(repl, PL_parser->lex_sub_repl);
9931 sv_catpvs(repl, "}");
9932 SvREFCNT_dec(PL_parser->lex_sub_repl);
9933 PL_parser->lex_sub_repl = repl;
9934 }
9935
9936
9937 linediff = CopLINE(PL_curcop) - first_line;
9938 if (linediff)
9939 CopLINE_set(PL_curcop, first_line);
9940
9941 if (linediff || es) {
9942 /* the IVX field indicates that the replacement string is a s///e;
9943 * the NVX field indicates how many src code lines the replacement
9944 * spreads over */
9945 sv_upgrade(PL_parser->lex_sub_repl, SVt_PVNV);
9946 ((XPVNV*)SvANY(PL_parser->lex_sub_repl))->xnv_u.xnv_lines = linediff;
9947 ((XPVIV*)SvANY(PL_parser->lex_sub_repl))->xiv_u.xivu_eval_seen =
9948 cBOOL(es);
9949 }
9950
9951 PL_lex_op = (OP*)pm;
9952 pl_yylval.ival = OP_SUBST;
9953 return s;
9954 }
9955
9956 STATIC char *
9957 S_scan_trans(pTHX_ char *start)
9958 {
9959 char* s;
9960 OP *o;
9961 U8 squash;
9962 U8 del;
9963 U8 complement;
9964 bool nondestruct = 0;
9965 char *t;
9966
9967 PERL_ARGS_ASSERT_SCAN_TRANS;
9968
9969 pl_yylval.ival = OP_NULL;
9970
9971 s = scan_str(start,FALSE,FALSE,FALSE,&t);
9972 if (!s)
9973 Perl_croak(aTHX_ "Transliteration pattern not terminated");
9974
9975 s = t;
9976
9977 s = scan_str(s,FALSE,FALSE,FALSE,NULL);
9978 if (!s) {
9979 SvREFCNT_dec_NN(PL_lex_stuff);
9980 PL_lex_stuff = NULL;
9981 Perl_croak(aTHX_ "Transliteration replacement not terminated");
9982 }
9983
9984 complement = del = squash = 0;
9985 while (1) {
9986 switch (*s) {
9987 case 'c':
9988 complement = OPpTRANS_COMPLEMENT;
9989 break;
9990 case 'd':
9991 del = OPpTRANS_DELETE;
9992 break;
9993 case 's':
9994 squash = OPpTRANS_SQUASH;
9995 break;
9996 case 'r':
9997 nondestruct = 1;
9998 break;
9999 default:
10000 goto no_more;
10001 }
10002 s++;
10003 }
10004 no_more:
10005
10006 o = newPVOP(nondestruct ? OP_TRANSR : OP_TRANS, 0, (char*)NULL);
10007 o->op_private &= ~OPpTRANS_ALL;
10008 o->op_private |= del|squash|complement|
10009 (DO_UTF8(PL_lex_stuff)? OPpTRANS_FROM_UTF : 0)|
10010 (DO_UTF8(PL_parser->lex_sub_repl) ? OPpTRANS_TO_UTF : 0);
10011
10012 PL_lex_op = o;
10013 pl_yylval.ival = nondestruct ? OP_TRANSR : OP_TRANS;
10014
10015
10016 return s;
10017 }
10018
10019 /* scan_heredoc
10020 Takes a pointer to the first < in <<FOO.
10021 Returns a pointer to the byte following <<FOO.
10022
10023 This function scans a heredoc, which involves different methods
10024 depending on whether we are in a string eval, quoted construct, etc.
10025 This is because PL_linestr could containing a single line of input, or
10026 a whole string being evalled, or the contents of the current quote-
10027 like operator.
10028
10029 The two basic methods are:
10030 - Steal lines from the input stream
10031 - Scan the heredoc in PL_linestr and remove it therefrom
10032
10033 In a file scope or filtered eval, the first method is used; in a
10034 string eval, the second.
10035
10036 In a quote-like operator, we have to choose between the two,
10037 depending on where we can find a newline. We peek into outer lex-
10038 ing scopes until we find one with a newline in it. If we reach the
10039 outermost lexing scope and it is a file, we use the stream method.
10040 Otherwise it is treated as an eval.
10041 */
10042
10043 STATIC char *
10044 S_scan_heredoc(pTHX_ char *s)
10045 {
10046 I32 op_type = OP_SCALAR;
10047 I32 len;
10048 SV *tmpstr;
10049 char term;
10050 char *d;
10051 char *e;
10052 char *peek;
10053 char *indent = 0;
10054 I32 indent_len = 0;
10055 bool indented = FALSE;
10056 const bool infile = PL_rsfp || PL_parser->filtered;
10057 const line_t origline = CopLINE(PL_curcop);
10058 LEXSHARED *shared = PL_parser->lex_shared;
10059
10060 PERL_ARGS_ASSERT_SCAN_HEREDOC;
10061
10062 s += 2;
10063 d = PL_tokenbuf + 1;
10064 e = PL_tokenbuf + sizeof PL_tokenbuf - 1;
10065 *PL_tokenbuf = '\n';
10066 peek = s;
10067
10068 if (*peek == '~') {
10069 indented = TRUE;
10070 peek++; s++;
10071 }
10072
10073 while (SPACE_OR_TAB(*peek))
10074 peek++;
10075
10076 if (*peek == '`' || *peek == '\'' || *peek =='"') {
10077 s = peek;
10078 term = *s++;
10079 s = delimcpy(d, e, s, PL_bufend, term, &len);
10080 if (s == PL_bufend)
10081 Perl_croak(aTHX_ "Unterminated delimiter for here document");
10082 d += len;
10083 s++;
10084 }
10085 else {
10086 if (*s == '\\')
10087 /* <<\FOO is equivalent to <<'FOO' */
10088 s++, term = '\'';
10089 else
10090 term = '"';
10091
10092 if (! isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF))
10093 Perl_croak(aTHX_ "Use of bare << to mean <<\"\" is forbidden");
10094
10095 peek = s;
10096
10097 while (isWORDCHAR_lazy_if_safe(peek, PL_bufend, UTF)) {
10098 peek += UTF ? UTF8SKIP(peek) : 1;
10099 }
10100
10101 len = (peek - s >= e - d) ? (e - d) : (peek - s);
10102 Copy(s, d, len, char);
10103 s += len;
10104 d += len;
10105 }
10106
10107 if (d >= PL_tokenbuf + sizeof PL_tokenbuf - 1)
10108 Perl_croak(aTHX_ "Delimiter for here document is too long");
10109
10110 *d++ = '\n';
10111 *d = '\0';
10112 len = d - PL_tokenbuf;
10113
10114 #ifndef PERL_STRICT_CR
10115 d = (char *) memchr(s, '\r', PL_bufend - s);
10116 if (d) {
10117 char * const olds = s;
10118 s = d;
10119 while (s < PL_bufend) {
10120 if (*s == '\r') {
10121 *d++ = '\n';
10122 if (*++s == '\n')
10123 s++;
10124 }
10125 else if (*s == '\n' && s[1] == '\r') { /* \015\013 on a mac? */
10126 *d++ = *s++;
10127 s++;
10128 }
10129 else
10130 *d++ = *s++;
10131 }
10132 *d = '\0';
10133 PL_bufend = d;
10134 SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
10135 s = olds;
10136 }
10137 #endif
10138
10139 tmpstr = newSV_type(SVt_PVIV);
10140 SvGROW(tmpstr, 80);
10141 if (term == '\'') {
10142 op_type = OP_CONST;
10143 SvIV_set(tmpstr, -1);
10144 }
10145 else if (term == '`') {
10146 op_type = OP_BACKTICK;
10147 SvIV_set(tmpstr, '\\');
10148 }
10149
10150 PL_multi_start = origline + 1 + PL_parser->herelines;
10151 PL_multi_open = PL_multi_close = '<';
10152
10153 /* inside a string eval or quote-like operator */
10154 if (!infile || PL_lex_inwhat) {
10155 SV *linestr;
10156 char *bufend;
10157 char * const olds = s;
10158 PERL_CONTEXT * const cx = CX_CUR();
10159 /* These two fields are not set until an inner lexing scope is
10160 entered. But we need them set here. */
10161 shared->ls_bufptr = s;
10162 shared->ls_linestr = PL_linestr;
10163
10164 if (PL_lex_inwhat) {
10165 /* Look for a newline. If the current buffer does not have one,
10166 peek into the line buffer of the parent lexing scope, going
10167 up as many levels as necessary to find one with a newline
10168 after bufptr.
10169 */
10170 while (!(s = (char *)memchr(
10171 (void *)shared->ls_bufptr, '\n',
10172 SvEND(shared->ls_linestr)-shared->ls_bufptr
10173 )))
10174 {
10175 shared = shared->ls_prev;
10176 /* shared is only null if we have gone beyond the outermost
10177 lexing scope. In a file, we will have broken out of the
10178 loop in the previous iteration. In an eval, the string buf-
10179 fer ends with "\n;", so the while condition above will have
10180 evaluated to false. So shared can never be null. Or so you
10181 might think. Odd syntax errors like s;@{<<; can gobble up
10182 the implicit semicolon at the end of a flie, causing the
10183 file handle to be closed even when we are not in a string
10184 eval. So shared may be null in that case.
10185 (Closing '>>}' here to balance the earlier open brace for
10186 editors that look for matched pairs.) */
10187 if (UNLIKELY(!shared))
10188 goto interminable;
10189 /* A LEXSHARED struct with a null ls_prev pointer is the outer-
10190 most lexing scope. In a file, shared->ls_linestr at that
10191 level is just one line, so there is no body to steal. */
10192 if (infile && !shared->ls_prev) {
10193 s = olds;
10194 goto streaming;
10195 }
10196 }
10197 }
10198 else { /* eval or we've already hit EOF */
10199 s = (char*)memchr((void*)s, '\n', PL_bufend - s);
10200 if (!s)
10201 goto interminable;
10202 }
10203
10204 linestr = shared->ls_linestr;
10205 bufend = SvEND(linestr);
10206 d = s;
10207 if (indented) {
10208 char *myolds = s;
10209
10210 while (s < bufend - len + 1) {
10211 if (*s++ == '\n')
10212 ++PL_parser->herelines;
10213
10214 if (memEQ(s, PL_tokenbuf + 1, len - 1)) {
10215 char *backup = s;
10216 indent_len = 0;
10217
10218 /* Only valid if it's preceded by whitespace only */
10219 while (backup != myolds && --backup >= myolds) {
10220 if (! SPACE_OR_TAB(*backup)) {
10221 break;
10222 }
10223 indent_len++;
10224 }
10225
10226 /* No whitespace or all! */
10227 if (backup == s || *backup == '\n') {
10228 Newx(indent, indent_len + 1, char);
10229 memcpy(indent, backup + 1, indent_len);
10230 indent[indent_len] = 0;
10231 s--; /* before our delimiter */
10232 PL_parser->herelines--; /* this line doesn't count */
10233 break;
10234 }
10235 }
10236 }
10237 }
10238 else {
10239 while (s < bufend - len + 1
10240 && memNE(s,PL_tokenbuf,len) )
10241 {
10242 if (*s++ == '\n')
10243 ++PL_parser->herelines;
10244 }
10245 }
10246
10247 if (s >= bufend - len + 1) {
10248 goto interminable;
10249 }
10250
10251 sv_setpvn(tmpstr,d+1,s-d);
10252 s += len - 1;
10253 /* the preceding stmt passes a newline */
10254 PL_parser->herelines++;
10255
10256 /* s now points to the newline after the heredoc terminator.
10257 d points to the newline before the body of the heredoc.
10258 */
10259
10260 /* We are going to modify linestr in place here, so set
10261 aside copies of the string if necessary for re-evals or
10262 (caller $n)[6]. */
10263 /* See the Paranoia note in case LEX_INTERPEND in yylex, for why we
10264 check shared->re_eval_str. */
10265 if (shared->re_eval_start || shared->re_eval_str) {
10266 /* Set aside the rest of the regexp */
10267 if (!shared->re_eval_str)
10268 shared->re_eval_str =
10269 newSVpvn(shared->re_eval_start,
10270 bufend - shared->re_eval_start);
10271 shared->re_eval_start -= s-d;
10272 }
10273
10274 if (cxstack_ix >= 0
10275 && CxTYPE(cx) == CXt_EVAL
10276 && CxOLD_OP_TYPE(cx) == OP_ENTEREVAL
10277 && cx->blk_eval.cur_text == linestr)
10278 {
10279 cx->blk_eval.cur_text = newSVsv(linestr);
10280 cx->blk_u16 |= 0x40; /* indicate cur_text is ref counted */
10281 }
10282
10283 /* Copy everything from s onwards back to d. */
10284 Move(s,d,bufend-s + 1,char);
10285 SvCUR_set(linestr, SvCUR(linestr) - (s-d));
10286 /* Setting PL_bufend only applies when we have not dug deeper
10287 into other scopes, because sublex_done sets PL_bufend to
10288 SvEND(PL_linestr). */
10289 if (shared == PL_parser->lex_shared)
10290 PL_bufend = SvEND(linestr);
10291 s = olds;
10292 }
10293 else {
10294 SV *linestr_save;
10295 char *oldbufptr_save;
10296 char *oldoldbufptr_save;
10297 streaming:
10298 SvPVCLEAR(tmpstr); /* avoid "uninitialized" warning */
10299 term = PL_tokenbuf[1];
10300 len--;
10301 linestr_save = PL_linestr; /* must restore this afterwards */
10302 d = s; /* and this */
10303 oldbufptr_save = PL_oldbufptr;
10304 oldoldbufptr_save = PL_oldoldbufptr;
10305 PL_linestr = newSVpvs("");
10306 PL_bufend = SvPVX(PL_linestr);
10307
10308 while (1) {
10309 PL_bufptr = PL_bufend;
10310 CopLINE_set(PL_curcop,
10311 origline + 1 + PL_parser->herelines);
10312
10313 if ( !lex_next_chunk(LEX_NO_TERM)
10314 && (!SvCUR(tmpstr) || SvEND(tmpstr)[-1] != '\n'))
10315 {
10316 /* Simply freeing linestr_save might seem simpler here, as it
10317 does not matter what PL_linestr points to, since we are
10318 about to croak; but in a quote-like op, linestr_save
10319 will have been prospectively freed already, via
10320 SAVEFREESV(PL_linestr) in sublex_push, so it’s easier to
10321 restore PL_linestr. */
10322 SvREFCNT_dec_NN(PL_linestr);
10323 PL_linestr = linestr_save;
10324 PL_oldbufptr = oldbufptr_save;
10325 PL_oldoldbufptr = oldoldbufptr_save;
10326 goto interminable;
10327 }
10328
10329 CopLINE_set(PL_curcop, origline);
10330
10331 if (!SvCUR(PL_linestr) || PL_bufend[-1] != '\n') {
10332 s = lex_grow_linestr(SvLEN(PL_linestr) + 3);
10333 /* ^That should be enough to avoid this needing to grow: */
10334 sv_catpvs(PL_linestr, "\n\0");
10335 assert(s == SvPVX(PL_linestr));
10336 PL_bufend = SvEND(PL_linestr);
10337 }
10338
10339 s = PL_bufptr;
10340 PL_parser->herelines++;
10341 PL_last_lop = PL_last_uni = NULL;
10342
10343 #ifndef PERL_STRICT_CR
10344 if (PL_bufend - PL_linestart >= 2) {
10345 if ( (PL_bufend[-2] == '\r' && PL_bufend[-1] == '\n')
10346 || (PL_bufend[-2] == '\n' && PL_bufend[-1] == '\r'))
10347 {
10348 PL_bufend[-2] = '\n';
10349 PL_bufend--;
10350 SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
10351 }
10352 else if (PL_bufend[-1] == '\r')
10353 PL_bufend[-1] = '\n';
10354 }
10355 else if (PL_bufend - PL_linestart == 1 && PL_bufend[-1] == '\r')
10356 PL_bufend[-1] = '\n';
10357 #endif
10358
10359 if (indented && (PL_bufend-s) >= len) {
10360 char * found = ninstr(s, PL_bufend, (PL_tokenbuf + 1), (PL_tokenbuf +1 + len));
10361
10362 if (found) {
10363 char *backup = found;
10364 indent_len = 0;
10365
10366 /* Only valid if it's preceded by whitespace only */
10367 while (backup != s && --backup >= s) {
10368 if (! SPACE_OR_TAB(*backup)) {
10369 break;
10370 }
10371 indent_len++;
10372 }
10373
10374 /* All whitespace or none! */
10375 if (backup == found || SPACE_OR_TAB(*backup)) {
10376 Newx(indent, indent_len + 1, char);
10377 memcpy(indent, backup, indent_len);
10378 indent[indent_len] = 0;
10379 SvREFCNT_dec(PL_linestr);
10380 PL_linestr = linestr_save;
10381 PL_linestart = SvPVX(linestr_save);
10382 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
10383 PL_oldbufptr = oldbufptr_save;
10384 PL_oldoldbufptr = oldoldbufptr_save;
10385 s = d;
10386 break;
10387 }
10388 }
10389
10390 /* Didn't find it */
10391 sv_catsv(tmpstr,PL_linestr);
10392 }
10393 else {
10394 if (*s == term && PL_bufend-s >= len
10395 && memEQ(s,PL_tokenbuf + 1,len))
10396 {
10397 SvREFCNT_dec(PL_linestr);
10398 PL_linestr = linestr_save;
10399 PL_linestart = SvPVX(linestr_save);
10400 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
10401 PL_oldbufptr = oldbufptr_save;
10402 PL_oldoldbufptr = oldoldbufptr_save;
10403 s = d;
10404 break;
10405 }
10406 else {
10407 sv_catsv(tmpstr,PL_linestr);
10408 }
10409 }
10410 } /* while (1) */
10411 }
10412
10413 PL_multi_end = origline + PL_parser->herelines;
10414
10415 if (indented && indent) {
10416 STRLEN linecount = 1;
10417 STRLEN herelen = SvCUR(tmpstr);
10418 char *ss = SvPVX(tmpstr);
10419 char *se = ss + herelen;
10420 SV *newstr = newSV(herelen+1);
10421 SvPOK_on(newstr);
10422
10423 /* Trim leading whitespace */
10424 while (ss < se) {
10425 /* newline only? Copy and move on */
10426 if (*ss == '\n') {
10427 sv_catpvs(newstr,"\n");
10428 ss++;
10429 linecount++;
10430
10431 /* Found our indentation? Strip it */
10432 }
10433 else if (se - ss >= indent_len
10434 && memEQ(ss, indent, indent_len))
10435 {
10436 STRLEN le = 0;
10437 ss += indent_len;
10438
10439 while ((ss + le) < se && *(ss + le) != '\n')
10440 le++;
10441
10442 sv_catpvn(newstr, ss, le);
10443 ss += le;
10444
10445 /* Line doesn't begin with our indentation? Croak */
10446 }
10447 else {
10448 Safefree(indent);
10449 Perl_croak(aTHX_
10450 "Indentation on line %d of here-doc doesn't match delimiter",
10451 (int)linecount
10452 );
10453 }
10454 } /* while */
10455
10456 /* avoid sv_setsv() as we dont wan't to COW here */
10457 sv_setpvn(tmpstr,SvPVX(newstr),SvCUR(newstr));
10458 Safefree(indent);
10459 SvREFCNT_dec_NN(newstr);
10460 }
10461
10462 if (SvCUR(tmpstr) + 5 < SvLEN(tmpstr)) {
10463 SvPV_shrink_to_cur(tmpstr);
10464 }
10465
10466 if (!IN_BYTES) {
10467 if (UTF && is_utf8_string((U8*)SvPVX_const(tmpstr), SvCUR(tmpstr)))
10468 SvUTF8_on(tmpstr);
10469 }
10470
10471 PL_lex_stuff = tmpstr;
10472 pl_yylval.ival = op_type;
10473 return s;
10474
10475 interminable:
10476 if (indent)
10477 Safefree(indent);
10478 SvREFCNT_dec(tmpstr);
10479 CopLINE_set(PL_curcop, origline);
10480 missingterm(PL_tokenbuf + 1, sizeof(PL_tokenbuf) - 1);
10481 }
10482
10483
10484 /* scan_inputsymbol
10485 takes: position of first '<' in input buffer
10486 returns: position of first char following the matching '>' in
10487 input buffer
10488 side-effects: pl_yylval and lex_op are set.
10489
10490 This code handles:
10491
10492 <> read from ARGV
10493 <<>> read from ARGV without magic open
10494 <FH> read from filehandle
10495 <pkg::FH> read from package qualified filehandle
10496 <pkg'FH> read from package qualified filehandle
10497 <$fh> read from filehandle in $fh
10498 <*.h> filename glob
10499
10500 */
10501
10502 STATIC char *
10503 S_scan_inputsymbol(pTHX_ char *start)
10504 {
10505 char *s = start; /* current position in buffer */
10506 char *end;
10507 I32 len;
10508 bool nomagicopen = FALSE;
10509 char *d = PL_tokenbuf; /* start of temp holding space */
10510 const char * const e = PL_tokenbuf + sizeof PL_tokenbuf; /* end of temp holding space */
10511
10512 PERL_ARGS_ASSERT_SCAN_INPUTSYMBOL;
10513
10514 end = (char *) memchr(s, '\n', PL_bufend - s);
10515 if (!end)
10516 end = PL_bufend;
10517 if (s[1] == '<' && s[2] == '>' && s[3] == '>') {
10518 nomagicopen = TRUE;
10519 *d = '\0';
10520 len = 0;
10521 s += 3;
10522 }
10523 else
10524 s = delimcpy(d, e, s + 1, end, '>', &len); /* extract until > */
10525
10526 /* die if we didn't have space for the contents of the <>,
10527 or if it didn't end, or if we see a newline
10528 */
10529
10530 if (len >= (I32)sizeof PL_tokenbuf)
10531 Perl_croak(aTHX_ "Excessively long <> operator");
10532 if (s >= end)
10533 Perl_croak(aTHX_ "Unterminated <> operator");
10534
10535 s++;
10536
10537 /* check for <$fh>
10538 Remember, only scalar variables are interpreted as filehandles by
10539 this code. Anything more complex (e.g., <$fh{$num}>) will be
10540 treated as a glob() call.
10541 This code makes use of the fact that except for the $ at the front,
10542 a scalar variable and a filehandle look the same.
10543 */
10544 if (*d == '$' && d[1]) d++;
10545
10546 /* allow <Pkg'VALUE> or <Pkg::VALUE> */
10547 while (isWORDCHAR_lazy_if_safe(d, e, UTF) || *d == '\'' || *d == ':') {
10548 d += UTF ? UTF8SKIP(d) : 1;
10549 }
10550
10551 /* If we've tried to read what we allow filehandles to look like, and
10552 there's still text left, then it must be a glob() and not a getline.
10553 Use scan_str to pull out the stuff between the <> and treat it
10554 as nothing more than a string.
10555 */
10556
10557 if (d - PL_tokenbuf != len) {
10558 pl_yylval.ival = OP_GLOB;
10559 s = scan_str(start,FALSE,FALSE,FALSE,NULL);
10560 if (!s)
10561 Perl_croak(aTHX_ "Glob not terminated");
10562 return s;
10563 }
10564 else {
10565 bool readline_overriden = FALSE;
10566 GV *gv_readline;
10567 /* we're in a filehandle read situation */
10568 d = PL_tokenbuf;
10569
10570 /* turn <> into <ARGV> */
10571 if (!len)
10572 Copy("ARGV",d,5,char);
10573
10574 /* Check whether readline() is overriden */
10575 if ((gv_readline = gv_override("readline",8)))
10576 readline_overriden = TRUE;
10577
10578 /* if <$fh>, create the ops to turn the variable into a
10579 filehandle
10580 */
10581 if (*d == '$') {
10582 /* try to find it in the pad for this block, otherwise find
10583 add symbol table ops
10584 */
10585 const PADOFFSET tmp = pad_findmy_pvn(d, len, 0);
10586 if (tmp != NOT_IN_PAD) {
10587 if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
10588 HV * const stash = PAD_COMPNAME_OURSTASH(tmp);
10589 HEK * const stashname = HvNAME_HEK(stash);
10590 SV * const sym = sv_2mortal(newSVhek(stashname));
10591 sv_catpvs(sym, "::");
10592 sv_catpv(sym, d+1);
10593 d = SvPVX(sym);
10594 goto intro_sym;
10595 }
10596 else {
10597 OP * const o = newOP(OP_PADSV, 0);
10598 o->op_targ = tmp;
10599 PL_lex_op = readline_overriden
10600 ? newUNOP(OP_ENTERSUB, OPf_STACKED,
10601 op_append_elem(OP_LIST, o,
10602 newCVREF(0, newGVOP(OP_GV,0,gv_readline))))
10603 : newUNOP(OP_READLINE, 0, o);
10604 }
10605 }
10606 else {
10607 GV *gv;
10608 ++d;
10609 intro_sym:
10610 gv = gv_fetchpv(d,
10611 GV_ADDMULTI | ( UTF ? SVf_UTF8 : 0 ),
10612 SVt_PV);
10613 PL_lex_op = readline_overriden
10614 ? newUNOP(OP_ENTERSUB, OPf_STACKED,
10615 op_append_elem(OP_LIST,
10616 newUNOP(OP_RV2SV, 0, newGVOP(OP_GV, 0, gv)),
10617 newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
10618 : newUNOP(OP_READLINE, 0,
10619 newUNOP(OP_RV2SV, 0,
10620 newGVOP(OP_GV, 0, gv)));
10621 }
10622 /* we created the ops in PL_lex_op, so make pl_yylval.ival a null op */
10623 pl_yylval.ival = OP_NULL;
10624 }
10625
10626 /* If it's none of the above, it must be a literal filehandle
10627 (<Foo::BAR> or <FOO>) so build a simple readline OP */
10628 else {
10629 GV * const gv = gv_fetchpv(d, GV_ADD | ( UTF ? SVf_UTF8 : 0 ), SVt_PVIO);
10630 PL_lex_op = readline_overriden
10631 ? newUNOP(OP_ENTERSUB, OPf_STACKED,
10632 op_append_elem(OP_LIST,
10633 newGVOP(OP_GV, 0, gv),
10634 newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
10635 : newUNOP(OP_READLINE, nomagicopen ? OPf_SPECIAL : 0, newGVOP(OP_GV, 0, gv));
10636 pl_yylval.ival = OP_NULL;
10637 }
10638 }
10639
10640 return s;
10641 }
10642
10643
10644 /* scan_str
10645 takes:
10646 start position in buffer
10647 keep_bracketed_quoted preserve \ quoting of embedded delimiters, but
10648 only if they are of the open/close form
10649 keep_delims preserve the delimiters around the string
10650 re_reparse compiling a run-time /(?{})/:
10651 collapse // to /, and skip encoding src
10652 delimp if non-null, this is set to the position of
10653 the closing delimiter, or just after it if
10654 the closing and opening delimiters differ
10655 (i.e., the opening delimiter of a substitu-
10656 tion replacement)
10657 returns: position to continue reading from buffer
10658 side-effects: multi_start, multi_close, lex_repl or lex_stuff, and
10659 updates the read buffer.
10660
10661 This subroutine pulls a string out of the input. It is called for:
10662 q single quotes q(literal text)
10663 ' single quotes 'literal text'
10664 qq double quotes qq(interpolate $here please)
10665 " double quotes "interpolate $here please"
10666 qx backticks qx(/bin/ls -l)
10667 ` backticks `/bin/ls -l`
10668 qw quote words @EXPORT_OK = qw( func() $spam )
10669 m// regexp match m/this/
10670 s/// regexp substitute s/this/that/
10671 tr/// string transliterate tr/this/that/
10672 y/// string transliterate y/this/that/
10673 ($*@) sub prototypes sub foo ($)
10674 (stuff) sub attr parameters sub foo : attr(stuff)
10675 <> readline or globs <FOO>, <>, <$fh>, or <*.c>
10676
10677 In most of these cases (all but <>, patterns and transliterate)
10678 yylex() calls scan_str(). m// makes yylex() call scan_pat() which
10679 calls scan_str(). s/// makes yylex() call scan_subst() which calls
10680 scan_str(). tr/// and y/// make yylex() call scan_trans() which
10681 calls scan_str().
10682
10683 It skips whitespace before the string starts, and treats the first
10684 character as the delimiter. If the delimiter is one of ([{< then
10685 the corresponding "close" character )]}> is used as the closing
10686 delimiter. It allows quoting of delimiters, and if the string has
10687 balanced delimiters ([{<>}]) it allows nesting.
10688
10689 On success, the SV with the resulting string is put into lex_stuff or,
10690 if that is already non-NULL, into lex_repl. The second case occurs only
10691 when parsing the RHS of the special constructs s/// and tr/// (y///).
10692 For convenience, the terminating delimiter character is stuffed into
10693 SvIVX of the SV.
10694 */
10695
10696 char *
10697 Perl_scan_str(pTHX_ char *start, int keep_bracketed_quoted, int keep_delims, int re_reparse,
10698 char **delimp
10699 )
10700 {
10701 SV *sv; /* scalar value: string */
10702 const char *tmps; /* temp string, used for delimiter matching */
10703 char *s = start; /* current position in the buffer */
10704 char term; /* terminating character */
10705 char *to; /* current position in the sv's data */
10706 I32 brackets = 1; /* bracket nesting level */
10707 bool d_is_utf8 = FALSE; /* is there any utf8 content? */
10708 IV termcode; /* terminating char. code */
10709 U8 termstr[UTF8_MAXBYTES+1]; /* terminating string */
10710 STRLEN termlen; /* length of terminating string */
10711 line_t herelines;
10712
10713 /* The delimiters that have a mirror-image closing one */
10714 const char * opening_delims = "([{<";
10715 const char * closing_delims = ")]}>";
10716
10717 /* The only non-UTF character that isn't a stand alone grapheme is
10718 * white-space, hence can't be a delimiter. */
10719 const char * non_grapheme_msg = "Use of unassigned code point or"
10720 " non-standalone grapheme for a delimiter"
10721 " is not allowed";
10722 PERL_ARGS_ASSERT_SCAN_STR;
10723
10724 /* skip space before the delimiter */
10725 if (isSPACE(*s)) {
10726 s = skipspace(s);
10727 }
10728
10729 /* mark where we are, in case we need to report errors */
10730 CLINE;
10731
10732 /* after skipping whitespace, the next character is the terminator */
10733 term = *s;
10734 if (!UTF || UTF8_IS_INVARIANT(term)) {
10735 termcode = termstr[0] = term;
10736 termlen = 1;
10737 }
10738 else {
10739 termcode = utf8_to_uvchr_buf((U8*)s, (U8*)PL_bufend, &termlen);
10740 if (UTF && UNLIKELY(! _is_grapheme((U8 *) start,
10741 (U8 *) s,
10742 (U8 *) PL_bufend,
10743 termcode)))
10744 {
10745 yyerror(non_grapheme_msg);
10746 }
10747
10748 Copy(s, termstr, termlen, U8);
10749 }
10750
10751 /* mark where we are */
10752 PL_multi_start = CopLINE(PL_curcop);
10753 PL_multi_open = termcode;
10754 herelines = PL_parser->herelines;
10755
10756 /* If the delimiter has a mirror-image closing one, get it */
10757 if (term && (tmps = strchr(opening_delims, term))) {
10758 termcode = termstr[0] = term = closing_delims[tmps - opening_delims];
10759 }
10760
10761 PL_multi_close = termcode;
10762
10763 if (PL_multi_open == PL_multi_close) {
10764 keep_bracketed_quoted = FALSE;
10765 }
10766
10767 /* create a new SV to hold the contents. 79 is the SV's initial length.
10768 What a random number. */
10769 sv = newSV_type(SVt_PVIV);
10770 SvGROW(sv, 80);
10771 SvIV_set(sv, termcode);
10772 (void)SvPOK_only(sv); /* validate pointer */
10773
10774 /* move past delimiter and try to read a complete string */
10775 if (keep_delims)
10776 sv_catpvn(sv, s, termlen);
10777 s += termlen;
10778 for (;;) {
10779 /* extend sv if need be */
10780 SvGROW(sv, SvCUR(sv) + (PL_bufend - s) + 1);
10781 /* set 'to' to the next character in the sv's string */
10782 to = SvPVX(sv)+SvCUR(sv);
10783
10784 /* if open delimiter is the close delimiter read unbridle */
10785 if (PL_multi_open == PL_multi_close) {
10786 for (; s < PL_bufend; s++,to++) {
10787 /* embedded newlines increment the current line number */
10788 if (*s == '\n' && !PL_rsfp && !PL_parser->filtered)
10789 COPLINE_INC_WITH_HERELINES;
10790 /* handle quoted delimiters */
10791 if (*s == '\\' && s+1 < PL_bufend && term != '\\') {
10792 if (!keep_bracketed_quoted
10793 && (s[1] == term
10794 || (re_reparse && s[1] == '\\'))
10795 )
10796 s++;
10797 else /* any other quotes are simply copied straight through */
10798 *to++ = *s++;
10799 }
10800 /* terminate when run out of buffer (the for() condition), or
10801 have found the terminator */
10802 else if (*s == term) { /* First byte of terminator matches */
10803 if (termlen == 1) /* If is the only byte, are done */
10804 break;
10805
10806 /* If the remainder of the terminator matches, also are
10807 * done, after checking that is a separate grapheme */
10808 if ( s + termlen <= PL_bufend
10809 && memEQ(s + 1, (char*)termstr + 1, termlen - 1))
10810 {
10811 if ( UTF
10812 && UNLIKELY(! _is_grapheme((U8 *) start,
10813 (U8 *) s,
10814 (U8 *) PL_bufend,
10815 termcode)))
10816 {
10817 yyerror(non_grapheme_msg);
10818 }
10819 break;
10820 }
10821 }
10822 else if (!d_is_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF) {
10823 d_is_utf8 = TRUE;
10824 }
10825
10826 *to = *s;
10827 }
10828 }
10829
10830 /* if the terminator isn't the same as the start character (e.g.,
10831 matched brackets), we have to allow more in the quoting, and
10832 be prepared for nested brackets.
10833 */
10834 else {
10835 /* read until we run out of string, or we find the terminator */
10836 for (; s < PL_bufend; s++,to++) {
10837 /* embedded newlines increment the line count */
10838 if (*s == '\n' && !PL_rsfp && !PL_parser->filtered)
10839 COPLINE_INC_WITH_HERELINES;
10840 /* backslashes can escape the open or closing characters */
10841 if (*s == '\\' && s+1 < PL_bufend) {
10842 if (!keep_bracketed_quoted
10843 && ( ((UV)s[1] == PL_multi_open)
10844 || ((UV)s[1] == PL_multi_close) ))
10845 {
10846 s++;
10847 }
10848 else
10849 *to++ = *s++;
10850 }
10851 /* allow nested opens and closes */
10852 else if ((UV)*s == PL_multi_close && --brackets <= 0)
10853 break;
10854 else if ((UV)*s == PL_multi_open)
10855 brackets++;
10856 else if (!d_is_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF)
10857 d_is_utf8 = TRUE;
10858 *to = *s;
10859 }
10860 }
10861 /* terminate the copied string and update the sv's end-of-string */
10862 *to = '\0';
10863 SvCUR_set(sv, to - SvPVX_const(sv));
10864
10865 /*
10866 * this next chunk reads more into the buffer if we're not done yet
10867 */
10868
10869 if (s < PL_bufend)
10870 break; /* handle case where we are done yet :-) */
10871
10872 #ifndef PERL_STRICT_CR
10873 if (to - SvPVX_const(sv) >= 2) {
10874 if ( (to[-2] == '\r' && to[-1] == '\n')
10875 || (to[-2] == '\n' && to[-1] == '\r'))
10876 {
10877 to[-2] = '\n';
10878 to--;
10879 SvCUR_set(sv, to - SvPVX_const(sv));
10880 }
10881 else if (to[-1] == '\r')
10882 to[-1] = '\n';
10883 }
10884 else if (to - SvPVX_const(sv) == 1 && to[-1] == '\r')
10885 to[-1] = '\n';
10886 #endif
10887
10888 /* if we're out of file, or a read fails, bail and reset the current
10889 line marker so we can report where the unterminated string began
10890 */
10891 COPLINE_INC_WITH_HERELINES;
10892 PL_bufptr = PL_bufend;
10893 if (!lex_next_chunk(0)) {
10894 sv_free(sv);
10895 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
10896 return NULL;
10897 }
10898 s = start = PL_bufptr;
10899 }
10900
10901 /* at this point, we have successfully read the delimited string */
10902
10903 if (keep_delims)
10904 sv_catpvn(sv, s, termlen);
10905 s += termlen;
10906
10907 if (d_is_utf8)
10908 SvUTF8_on(sv);
10909
10910 PL_multi_end = CopLINE(PL_curcop);
10911 CopLINE_set(PL_curcop, PL_multi_start);
10912 PL_parser->herelines = herelines;
10913
10914 /* if we allocated too much space, give some back */
10915 if (SvCUR(sv) + 5 < SvLEN(sv)) {
10916 SvLEN_set(sv, SvCUR(sv) + 1);
10917 SvPV_renew(sv, SvLEN(sv));
10918 }
10919
10920 /* decide whether this is the first or second quoted string we've read
10921 for this op
10922 */
10923
10924 if (PL_lex_stuff)
10925 PL_parser->lex_sub_repl = sv;
10926 else
10927 PL_lex_stuff = sv;
10928 if (delimp) *delimp = PL_multi_open == PL_multi_close ? s-termlen : s;
10929 return s;
10930 }
10931
10932 /*
10933 scan_num
10934 takes: pointer to position in buffer
10935 returns: pointer to new position in buffer
10936 side-effects: builds ops for the constant in pl_yylval.op
10937
10938 Read a number in any of the formats that Perl accepts:
10939
10940 \d(_?\d)*(\.(\d(_?\d)*)?)?[Ee][\+\-]?(\d(_?\d)*) 12 12.34 12.
10941 \.\d(_?\d)*[Ee][\+\-]?(\d(_?\d)*) .34
10942 0b[01](_?[01])* binary integers
10943 0[0-7](_?[0-7])* octal integers
10944 0x[0-9A-Fa-f](_?[0-9A-Fa-f])* hexadecimal integers
10945 0x[0-9A-Fa-f](_?[0-9A-Fa-f])*(?:\.\d*)?p[+-]?[0-9]+ hexadecimal floats
10946
10947 Like most scan_ routines, it uses the PL_tokenbuf buffer to hold the
10948 thing it reads.
10949
10950 If it reads a number without a decimal point or an exponent, it will
10951 try converting the number to an integer and see if it can do so
10952 without loss of precision.
10953 */
10954
10955 char *
10956 Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
10957 {
10958 const char *s = start; /* current position in buffer */
10959 char *d; /* destination in temp buffer */
10960 char *e; /* end of temp buffer */
10961 NV nv; /* number read, as a double */
10962 SV *sv = NULL; /* place to put the converted number */
10963 bool floatit; /* boolean: int or float? */
10964 const char *lastub = NULL; /* position of last underbar */
10965 static const char* const number_too_long = "Number too long";
10966 bool warned_about_underscore = 0;
10967 #define WARN_ABOUT_UNDERSCORE() \
10968 do { \
10969 if (!warned_about_underscore) { \
10970 warned_about_underscore = 1; \
10971 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), \
10972 "Misplaced _ in number"); \
10973 } \
10974 } while(0)
10975 /* Hexadecimal floating point.
10976 *
10977 * In many places (where we have quads and NV is IEEE 754 double)
10978 * we can fit the mantissa bits of a NV into an unsigned quad.
10979 * (Note that UVs might not be quads even when we have quads.)
10980 * This will not work everywhere, though (either no quads, or
10981 * using long doubles), in which case we have to resort to NV,
10982 * which will probably mean horrible loss of precision due to
10983 * multiple fp operations. */
10984 bool hexfp = FALSE;
10985 int total_bits = 0;
10986 int significant_bits = 0;
10987 #if NVSIZE == 8 && defined(HAS_QUAD) && defined(Uquad_t)
10988 # define HEXFP_UQUAD
10989 Uquad_t hexfp_uquad = 0;
10990 int hexfp_frac_bits = 0;
10991 #else
10992 # define HEXFP_NV
10993 NV hexfp_nv = 0.0;
10994 #endif
10995 NV hexfp_mult = 1.0;
10996 UV high_non_zero = 0; /* highest digit */
10997 int non_zero_integer_digits = 0;
10998
10999 PERL_ARGS_ASSERT_SCAN_NUM;
11000
11001 /* We use the first character to decide what type of number this is */
11002
11003 switch (*s) {
11004 default:
11005 Perl_croak(aTHX_ "panic: scan_num, *s=%d", *s);
11006
11007 /* if it starts with a 0, it could be an octal number, a decimal in
11008 0.13 disguise, or a hexadecimal number, or a binary number. */
11009 case '0':
11010 {
11011 /* variables:
11012 u holds the "number so far"
11013 shift the power of 2 of the base
11014 (hex == 4, octal == 3, binary == 1)
11015 overflowed was the number more than we can hold?
11016
11017 Shift is used when we add a digit. It also serves as an "are
11018 we in octal/hex/binary?" indicator to disallow hex characters
11019 when in octal mode.
11020 */
11021 NV n = 0.0;
11022 UV u = 0;
11023 I32 shift;
11024 bool overflowed = FALSE;
11025 bool just_zero = TRUE; /* just plain 0 or binary number? */
11026 static const NV nvshift[5] = { 1.0, 2.0, 4.0, 8.0, 16.0 };
11027 static const char* const bases[5] =
11028 { "", "binary", "", "octal", "hexadecimal" };
11029 static const char* const Bases[5] =
11030 { "", "Binary", "", "Octal", "Hexadecimal" };
11031 static const char* const maxima[5] =
11032 { "",
11033 "0b11111111111111111111111111111111",
11034 "",
11035 "037777777777",
11036 "0xffffffff" };
11037 const char *base, *Base, *max;
11038
11039 /* check for hex */
11040 if (isALPHA_FOLD_EQ(s[1], 'x')) {
11041 shift = 4;
11042 s += 2;
11043 just_zero = FALSE;
11044 } else if (isALPHA_FOLD_EQ(s[1], 'b')) {
11045 shift = 1;
11046 s += 2;
11047 just_zero = FALSE;
11048 }
11049 /* check for a decimal in disguise */
11050 else if (s[1] == '.' || isALPHA_FOLD_EQ(s[1], 'e'))
11051 goto decimal;
11052 /* so it must be octal */
11053 else {
11054 shift = 3;
11055 s++;
11056 }
11057
11058 if (*s == '_') {
11059 WARN_ABOUT_UNDERSCORE();
11060 lastub = s++;
11061 }
11062
11063 base = bases[shift];
11064 Base = Bases[shift];
11065 max = maxima[shift];
11066
11067 /* read the rest of the number */
11068 for (;;) {
11069 /* x is used in the overflow test,
11070 b is the digit we're adding on. */
11071 UV x, b;
11072
11073 switch (*s) {
11074
11075 /* if we don't mention it, we're done */
11076 default:
11077 goto out;
11078
11079 /* _ are ignored -- but warned about if consecutive */
11080 case '_':
11081 if (lastub && s == lastub + 1)
11082 WARN_ABOUT_UNDERSCORE();
11083 lastub = s++;
11084 break;
11085
11086 /* 8 and 9 are not octal */
11087 case '8': case '9':
11088 if (shift == 3)
11089 yyerror(Perl_form(aTHX_ "Illegal octal digit '%c'", *s));
11090 /* FALLTHROUGH */
11091
11092 /* octal digits */
11093 case '2': case '3': case '4':
11094 case '5': case '6': case '7':
11095 if (shift == 1)
11096 yyerror(Perl_form(aTHX_ "Illegal binary digit '%c'", *s));
11097 /* FALLTHROUGH */
11098
11099 case '0': case '1':
11100 b = *s++ & 15; /* ASCII digit -> value of digit */
11101 goto digit;
11102
11103 /* hex digits */
11104 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
11105 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
11106 /* make sure they said 0x */
11107 if (shift != 4)
11108 goto out;
11109 b = (*s++ & 7) + 9;
11110
11111 /* Prepare to put the digit we have onto the end
11112 of the number so far. We check for overflows.
11113 */
11114
11115 digit:
11116 just_zero = FALSE;
11117 if (!overflowed) {
11118 assert(shift >= 0);
11119 x = u << shift; /* make room for the digit */
11120
11121 total_bits += shift;
11122
11123 if ((x >> shift) != u
11124 && !(PL_hints & HINT_NEW_BINARY)) {
11125 overflowed = TRUE;
11126 n = (NV) u;
11127 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
11128 "Integer overflow in %s number",
11129 base);
11130 } else
11131 u = x | b; /* add the digit to the end */
11132 }
11133 if (overflowed) {
11134 n *= nvshift[shift];
11135 /* If an NV has not enough bits in its
11136 * mantissa to represent an UV this summing of
11137 * small low-order numbers is a waste of time
11138 * (because the NV cannot preserve the
11139 * low-order bits anyway): we could just
11140 * remember when did we overflow and in the
11141 * end just multiply n by the right
11142 * amount. */
11143 n += (NV) b;
11144 }
11145
11146 if (high_non_zero == 0 && b > 0)
11147 high_non_zero = b;
11148
11149 if (high_non_zero)
11150 non_zero_integer_digits++;
11151
11152 /* this could be hexfp, but peek ahead
11153 * to avoid matching ".." */
11154 if (UNLIKELY(HEXFP_PEEK(s))) {
11155 goto out;
11156 }
11157
11158 break;
11159 }
11160 }
11161
11162 /* if we get here, we had success: make a scalar value from
11163 the number.
11164 */
11165 out:
11166
11167 /* final misplaced underbar check */
11168 if (s[-1] == '_')
11169 WARN_ABOUT_UNDERSCORE();
11170
11171 if (UNLIKELY(HEXFP_PEEK(s))) {
11172 /* Do sloppy (on the underbars) but quick detection
11173 * (and value construction) for hexfp, the decimal
11174 * detection will shortly be more thorough with the
11175 * underbar checks. */
11176 const char* h = s;
11177 significant_bits = non_zero_integer_digits * shift;
11178 #ifdef HEXFP_UQUAD
11179 hexfp_uquad = u;
11180 #else /* HEXFP_NV */
11181 hexfp_nv = u;
11182 #endif
11183 /* Ignore the leading zero bits of
11184 * the high (first) non-zero digit. */
11185 if (high_non_zero) {
11186 if (high_non_zero < 0x8)
11187 significant_bits--;
11188 if (high_non_zero < 0x4)
11189 significant_bits--;
11190 if (high_non_zero < 0x2)
11191 significant_bits--;
11192 }
11193
11194 if (*h == '.') {
11195 #ifdef HEXFP_NV
11196 NV nv_mult = 1.0;
11197 #endif
11198 bool accumulate = TRUE;
11199 U8 b;
11200 int lim = 1 << shift;
11201 for (h++; ((isXDIGIT(*h) && (b = XDIGIT_VALUE(*h)) < lim) ||
11202 *h == '_'); h++) {
11203 if (isXDIGIT(*h)) {
11204 significant_bits += shift;
11205 #ifdef HEXFP_UQUAD
11206 if (accumulate) {
11207 if (significant_bits < NV_MANT_DIG) {
11208 /* We are in the long "run" of xdigits,
11209 * accumulate the full four bits. */
11210 assert(shift >= 0);
11211 hexfp_uquad <<= shift;
11212 hexfp_uquad |= b;
11213 hexfp_frac_bits += shift;
11214 } else if (significant_bits - shift < NV_MANT_DIG) {
11215 /* We are at a hexdigit either at,
11216 * or straddling, the edge of mantissa.
11217 * We will try grabbing as many as
11218 * possible bits. */
11219 int tail =
11220 significant_bits - NV_MANT_DIG;
11221 if (tail <= 0)
11222 tail += shift;
11223 assert(tail >= 0);
11224 hexfp_uquad <<= tail;
11225 assert((shift - tail) >= 0);
11226 hexfp_uquad |= b >> (shift - tail);
11227 hexfp_frac_bits += tail;
11228
11229 /* Ignore the trailing zero bits
11230 * of the last non-zero xdigit.
11231 *
11232 * The assumption here is that if
11233 * one has input of e.g. the xdigit
11234 * eight (0x8), there is only one
11235 * bit being input, not the full
11236 * four bits. Conversely, if one
11237 * specifies a zero xdigit, the
11238 * assumption is that one really
11239 * wants all those bits to be zero. */
11240 if (b) {
11241 if ((b & 0x1) == 0x0) {
11242 significant_bits--;
11243 if ((b & 0x2) == 0x0) {
11244 significant_bits--;
11245 if ((b & 0x4) == 0x0) {
11246 significant_bits--;
11247 }
11248 }
11249 }
11250 }
11251
11252 accumulate = FALSE;
11253 }
11254 } else {
11255 /* Keep skipping the xdigits, and
11256 * accumulating the significant bits,
11257 * but do not shift the uquad
11258 * (which would catastrophically drop
11259 * high-order bits) or accumulate the
11260 * xdigits anymore. */
11261 }
11262 #else /* HEXFP_NV */
11263 if (accumulate) {
11264 nv_mult /= nvshift[shift];
11265 if (nv_mult > 0.0)
11266 hexfp_nv += b * nv_mult;
11267 else
11268 accumulate = FALSE;
11269 }
11270 #endif
11271 }
11272 if (significant_bits >= NV_MANT_DIG)
11273 accumulate = FALSE;
11274 }
11275 }
11276
11277 if ((total_bits > 0 || significant_bits > 0) &&
11278 isALPHA_FOLD_EQ(*h, 'p')) {
11279 bool negexp = FALSE;
11280 h++;
11281 if (*h == '+')
11282 h++;
11283 else if (*h == '-') {
11284 negexp = TRUE;
11285 h++;
11286 }
11287 if (isDIGIT(*h)) {
11288 I32 hexfp_exp = 0;
11289 while (isDIGIT(*h) || *h == '_') {
11290 if (isDIGIT(*h)) {
11291 hexfp_exp *= 10;
11292 hexfp_exp += *h - '0';
11293 #ifdef NV_MIN_EXP
11294 if (negexp
11295 && -hexfp_exp < NV_MIN_EXP - 1) {
11296 /* NOTE: this means that the exponent
11297 * underflow warning happens for
11298 * the IEEE 754 subnormals (denormals),
11299 * because DBL_MIN_EXP etc are the lowest
11300 * possible binary (or, rather, DBL_RADIX-base)
11301 * exponent for normals, not subnormals.
11302 *
11303 * This may or may not be a good thing. */
11304 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
11305 "Hexadecimal float: exponent underflow");
11306 break;
11307 }
11308 #endif
11309 #ifdef NV_MAX_EXP
11310 if (!negexp
11311 && hexfp_exp > NV_MAX_EXP - 1) {
11312 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
11313 "Hexadecimal float: exponent overflow");
11314 break;
11315 }
11316 #endif
11317 }
11318 h++;
11319 }
11320 if (negexp)
11321 hexfp_exp = -hexfp_exp;
11322 #ifdef HEXFP_UQUAD
11323 hexfp_exp -= hexfp_frac_bits;
11324 #endif
11325 hexfp_mult = Perl_pow(2.0, hexfp_exp);
11326 hexfp = TRUE;
11327 goto decimal;
11328 }
11329 }
11330 }
11331
11332 if (overflowed) {
11333 if (n > 4294967295.0)
11334 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
11335 "%s number > %s non-portable",
11336 Base, max);
11337 sv = newSVnv(n);
11338 }
11339 else {
11340 #if UVSIZE > 4
11341 if (u > 0xffffffff)
11342 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
11343 "%s number > %s non-portable",
11344 Base, max);
11345 #endif
11346 sv = newSVuv(u);
11347 }
11348 if (just_zero && (PL_hints & HINT_NEW_INTEGER))
11349 sv = new_constant(start, s - start, "integer",
11350 sv, NULL, NULL, 0, NULL);
11351 else if (PL_hints & HINT_NEW_BINARY)
11352 sv = new_constant(start, s - start, "binary",
11353 sv, NULL, NULL, 0, NULL);
11354 }
11355 break;
11356
11357 /*
11358 handle decimal numbers.
11359 we're also sent here when we read a 0 as the first digit
11360 */
11361 case '1': case '2': case '3': case '4': case '5':
11362 case '6': case '7': case '8': case '9': case '.':
11363 decimal:
11364 d = PL_tokenbuf;
11365 e = PL_tokenbuf + sizeof PL_tokenbuf - 6; /* room for various punctuation */
11366 floatit = FALSE;
11367 if (hexfp) {
11368 floatit = TRUE;
11369 *d++ = '0';
11370 *d++ = 'x';
11371 s = start + 2;
11372 }
11373
11374 /* read next group of digits and _ and copy into d */
11375 while (isDIGIT(*s)
11376 || *s == '_'
11377 || UNLIKELY(hexfp && isXDIGIT(*s)))
11378 {
11379 /* skip underscores, checking for misplaced ones
11380 if -w is on
11381 */
11382 if (*s == '_') {
11383 if (lastub && s == lastub + 1)
11384 WARN_ABOUT_UNDERSCORE();
11385 lastub = s++;
11386 }
11387 else {
11388 /* check for end of fixed-length buffer */
11389 if (d >= e)
11390 Perl_croak(aTHX_ "%s", number_too_long);
11391 /* if we're ok, copy the character */
11392 *d++ = *s++;
11393 }
11394 }
11395
11396 /* final misplaced underbar check */
11397 if (lastub && s == lastub + 1)
11398 WARN_ABOUT_UNDERSCORE();
11399
11400 /* read a decimal portion if there is one. avoid
11401 3..5 being interpreted as the number 3. followed
11402 by .5
11403 */
11404 if (*s == '.' && s[1] != '.') {
11405 floatit = TRUE;
11406 *d++ = *s++;
11407
11408 if (*s == '_') {
11409 WARN_ABOUT_UNDERSCORE();
11410 lastub = s;
11411 }
11412
11413 /* copy, ignoring underbars, until we run out of digits.
11414 */
11415 for (; isDIGIT(*s)
11416 || *s == '_'
11417 || UNLIKELY(hexfp && isXDIGIT(*s));
11418 s++)
11419 {
11420 /* fixed length buffer check */
11421 if (d >= e)
11422 Perl_croak(aTHX_ "%s", number_too_long);
11423 if (*s == '_') {
11424 if (lastub && s == lastub + 1)
11425 WARN_ABOUT_UNDERSCORE();
11426 lastub = s;
11427 }
11428 else
11429 *d++ = *s;
11430 }
11431 /* fractional part ending in underbar? */
11432 if (s[-1] == '_')
11433 WARN_ABOUT_UNDERSCORE();
11434 if (*s == '.' && isDIGIT(s[1])) {
11435 /* oops, it's really a v-string, but without the "v" */
11436 s = start;
11437 goto vstring;
11438 }
11439 }
11440
11441 /* read exponent part, if present */
11442 if ((isALPHA_FOLD_EQ(*s, 'e')
11443 || UNLIKELY(hexfp && isALPHA_FOLD_EQ(*s, 'p')))
11444 && strchr("+-0123456789_", s[1]))
11445 {
11446 int exp_digits = 0;
11447 const char *save_s = s;
11448 char * save_d = d;
11449
11450 /* regardless of whether user said 3E5 or 3e5, use lower 'e',
11451 ditto for p (hexfloats) */
11452 if ((isALPHA_FOLD_EQ(*s, 'e'))) {
11453 /* At least some Mach atof()s don't grok 'E' */
11454 *d++ = 'e';
11455 }
11456 else if (UNLIKELY(hexfp && (isALPHA_FOLD_EQ(*s, 'p')))) {
11457 *d++ = 'p';
11458 }
11459
11460 s++;
11461
11462
11463 /* stray preinitial _ */
11464 if (*s == '_') {
11465 WARN_ABOUT_UNDERSCORE();
11466 lastub = s++;
11467 }
11468
11469 /* allow positive or negative exponent */
11470 if (*s == '+' || *s == '-')
11471 *d++ = *s++;
11472
11473 /* stray initial _ */
11474 if (*s == '_') {
11475 WARN_ABOUT_UNDERSCORE();
11476 lastub = s++;
11477 }
11478
11479 /* read digits of exponent */
11480 while (isDIGIT(*s) || *s == '_') {
11481 if (isDIGIT(*s)) {
11482 ++exp_digits;
11483 if (d >= e)
11484 Perl_croak(aTHX_ "%s", number_too_long);
11485 *d++ = *s++;
11486 }
11487 else {
11488 if (((lastub && s == lastub + 1)
11489 || (!isDIGIT(s[1]) && s[1] != '_')))
11490 WARN_ABOUT_UNDERSCORE();
11491 lastub = s++;
11492 }
11493 }
11494
11495 if (!exp_digits) {
11496 /* no exponent digits, the [eEpP] could be for something else,
11497 * though in practice we don't get here for p since that's preparsed
11498 * earlier, and results in only the 0xX being consumed, so behave similarly
11499 * for decimal floats and consume only the D.DD, leaving the [eE] to the
11500 * next token.
11501 */
11502 s = save_s;
11503 d = save_d;
11504 }
11505 else {
11506 floatit = TRUE;
11507 }
11508 }
11509
11510
11511 /*
11512 We try to do an integer conversion first if no characters
11513 indicating "float" have been found.
11514 */
11515
11516 if (!floatit) {
11517 UV uv;
11518 const int flags = grok_number (PL_tokenbuf, d - PL_tokenbuf, &uv);
11519
11520 if (flags == IS_NUMBER_IN_UV) {
11521 if (uv <= IV_MAX)
11522 sv = newSViv(uv); /* Prefer IVs over UVs. */
11523 else
11524 sv = newSVuv(uv);
11525 } else if (flags == (IS_NUMBER_IN_UV | IS_NUMBER_NEG)) {
11526 if (uv <= (UV) IV_MIN)
11527 sv = newSViv(-(IV)uv);
11528 else
11529 floatit = TRUE;
11530 } else
11531 floatit = TRUE;
11532 }
11533 if (floatit) {
11534 /* terminate the string */
11535 *d = '\0';
11536 if (UNLIKELY(hexfp)) {
11537 # ifdef NV_MANT_DIG
11538 if (significant_bits > NV_MANT_DIG)
11539 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
11540 "Hexadecimal float: mantissa overflow");
11541 # endif
11542 #ifdef HEXFP_UQUAD
11543 nv = hexfp_uquad * hexfp_mult;
11544 #else /* HEXFP_NV */
11545 nv = hexfp_nv * hexfp_mult;
11546 #endif
11547 } else {
11548 nv = Atof(PL_tokenbuf);
11549 }
11550 sv = newSVnv(nv);
11551 }
11552
11553 if ( floatit
11554 ? (PL_hints & HINT_NEW_FLOAT) : (PL_hints & HINT_NEW_INTEGER) ) {
11555 const char *const key = floatit ? "float" : "integer";
11556 const STRLEN keylen = floatit ? 5 : 7;
11557 sv = S_new_constant(aTHX_ PL_tokenbuf, d - PL_tokenbuf,
11558 key, keylen, sv, NULL, NULL, 0, NULL);
11559 }
11560 break;
11561
11562 /* if it starts with a v, it could be a v-string */
11563 case 'v':
11564 vstring:
11565 sv = newSV(5); /* preallocate storage space */
11566 ENTER_with_name("scan_vstring");
11567 SAVEFREESV(sv);
11568 s = scan_vstring(s, PL_bufend, sv);
11569 SvREFCNT_inc_simple_void_NN(sv);
11570 LEAVE_with_name("scan_vstring");
11571 break;
11572 }
11573
11574 /* make the op for the constant and return */
11575
11576 if (sv)
11577 lvalp->opval = newSVOP(OP_CONST, 0, sv);
11578 else
11579 lvalp->opval = NULL;
11580
11581 return (char *)s;
11582 }
11583
11584 STATIC char *
11585 S_scan_formline(pTHX_ char *s)
11586 {
11587 SV * const stuff = newSVpvs("");
11588 bool needargs = FALSE;
11589 bool eofmt = FALSE;
11590
11591 PERL_ARGS_ASSERT_SCAN_FORMLINE;
11592
11593 while (!needargs) {
11594 char *eol;
11595 if (*s == '.') {
11596 char *t = s+1;
11597 #ifdef PERL_STRICT_CR
11598 while (SPACE_OR_TAB(*t))
11599 t++;
11600 #else
11601 while (SPACE_OR_TAB(*t) || *t == '\r')
11602 t++;
11603 #endif
11604 if (*t == '\n' || t == PL_bufend) {
11605 eofmt = TRUE;
11606 break;
11607 }
11608 }
11609 eol = (char *) memchr(s,'\n',PL_bufend-s);
11610 if (!eol++)
11611 eol = PL_bufend;
11612 if (*s != '#') {
11613 char *t;
11614 for (t = s; t < eol; t++) {
11615 if (*t == '~' && t[1] == '~' && SvCUR(stuff)) {
11616 needargs = FALSE;
11617 goto enough; /* ~~ must be first line in formline */
11618 }
11619 if (*t == '@' || *t == '^')
11620 needargs = TRUE;
11621 }
11622 if (eol > s) {
11623 sv_catpvn(stuff, s, eol-s);
11624 #ifndef PERL_STRICT_CR
11625 if (eol-s > 1 && eol[-2] == '\r' && eol[-1] == '\n') {
11626 char *end = SvPVX(stuff) + SvCUR(stuff);
11627 end[-2] = '\n';
11628 end[-1] = '\0';
11629 SvCUR_set(stuff, SvCUR(stuff) - 1);
11630 }
11631 #endif
11632 }
11633 else
11634 break;
11635 }
11636 s = (char*)eol;
11637 if ((PL_rsfp || PL_parser->filtered)
11638 && PL_parser->form_lex_state == LEX_NORMAL) {
11639 bool got_some;
11640 PL_bufptr = PL_bufend;
11641 COPLINE_INC_WITH_HERELINES;
11642 got_some = lex_next_chunk(0);
11643 CopLINE_dec(PL_curcop);
11644 s = PL_bufptr;
11645 if (!got_some)
11646 break;
11647 }
11648 incline(s, PL_bufend);
11649 }
11650 enough:
11651 if (!SvCUR(stuff) || needargs)
11652 PL_lex_state = PL_parser->form_lex_state;
11653 if (SvCUR(stuff)) {
11654 PL_expect = XSTATE;
11655 if (needargs) {
11656 const char *s2 = s;
11657 while (isSPACE(*s2) && *s2 != '\n')
11658 s2++;
11659 if (*s2 == '{') {
11660 PL_expect = XTERMBLOCK;
11661 NEXTVAL_NEXTTOKE.ival = 0;
11662 force_next(DO);
11663 }
11664 NEXTVAL_NEXTTOKE.ival = 0;
11665 force_next(FORMLBRACK);
11666 }
11667 if (!IN_BYTES) {
11668 if (UTF && is_utf8_string((U8*)SvPVX_const(stuff), SvCUR(stuff)))
11669 SvUTF8_on(stuff);
11670 }
11671 NEXTVAL_NEXTTOKE.opval = newSVOP(OP_CONST, 0, stuff);
11672 force_next(THING);
11673 }
11674 else {
11675 SvREFCNT_dec(stuff);
11676 if (eofmt)
11677 PL_lex_formbrack = 0;
11678 }
11679 return s;
11680 }
11681
11682 I32
11683 Perl_start_subparse(pTHX_ I32 is_format, U32 flags)
11684 {
11685 const I32 oldsavestack_ix = PL_savestack_ix;
11686 CV* const outsidecv = PL_compcv;
11687
11688 SAVEI32(PL_subline);
11689 save_item(PL_subname);
11690 SAVESPTR(PL_compcv);
11691
11692 PL_compcv = MUTABLE_CV(newSV_type(is_format ? SVt_PVFM : SVt_PVCV));
11693 CvFLAGS(PL_compcv) |= flags;
11694
11695 PL_subline = CopLINE(PL_curcop);
11696 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE|padnew_SAVESUB);
11697 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outsidecv));
11698 CvOUTSIDE_SEQ(PL_compcv) = PL_cop_seqmax;
11699 if (outsidecv && CvPADLIST(outsidecv))
11700 CvPADLIST(PL_compcv)->xpadl_outid = CvPADLIST(outsidecv)->xpadl_id;
11701
11702 return oldsavestack_ix;
11703 }
11704
11705
11706 /* Do extra initialisation of a CV (typically one just created by
11707 * start_subparse()) if that CV is for a named sub
11708 */
11709
11710 void
11711 Perl_init_named_cv(pTHX_ CV *cv, OP *nameop)
11712 {
11713 PERL_ARGS_ASSERT_INIT_NAMED_CV;
11714
11715 if (nameop->op_type == OP_CONST) {
11716 const char *const name = SvPV_nolen_const(((SVOP*)nameop)->op_sv);
11717 if ( strEQ(name, "BEGIN")
11718 || strEQ(name, "END")
11719 || strEQ(name, "INIT")
11720 || strEQ(name, "CHECK")
11721 || strEQ(name, "UNITCHECK")
11722 )
11723 CvSPECIAL_on(cv);
11724 }
11725 else
11726 /* State subs inside anonymous subs need to be
11727 clonable themselves. */
11728 if ( CvANON(CvOUTSIDE(cv))
11729 || CvCLONE(CvOUTSIDE(cv))
11730 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
11731 CvOUTSIDE(cv)
11732 ))[nameop->op_targ])
11733 )
11734 CvCLONE_on(cv);
11735 }
11736
11737
11738 static int
11739 S_yywarn(pTHX_ const char *const s, U32 flags)
11740 {
11741 PERL_ARGS_ASSERT_YYWARN;
11742
11743 PL_in_eval |= EVAL_WARNONLY;
11744 yyerror_pv(s, flags);
11745 return 0;
11746 }
11747
11748 void
11749 Perl_abort_execution(pTHX_ const char * const msg, const char * const name)
11750 {
11751 PERL_ARGS_ASSERT_ABORT_EXECUTION;
11752
11753 if (PL_minus_c)
11754 Perl_croak(aTHX_ "%s%s had compilation errors.\n", msg, name);
11755 else {
11756 Perl_croak(aTHX_
11757 "%sExecution of %s aborted due to compilation errors.\n", msg, name);
11758 }
11759 NOT_REACHED; /* NOTREACHED */
11760 }
11761
11762 void
11763 Perl_yyquit(pTHX)
11764 {
11765 /* Called, after at least one error has been found, to abort the parse now,
11766 * instead of trying to forge ahead */
11767
11768 yyerror_pvn(NULL, 0, 0);
11769 }
11770
11771 int
11772 Perl_yyerror(pTHX_ const char *const s)
11773 {
11774 PERL_ARGS_ASSERT_YYERROR;
11775 return yyerror_pvn(s, strlen(s), 0);
11776 }
11777
11778 int
11779 Perl_yyerror_pv(pTHX_ const char *const s, U32 flags)
11780 {
11781 PERL_ARGS_ASSERT_YYERROR_PV;
11782 return yyerror_pvn(s, strlen(s), flags);
11783 }
11784
11785 int
11786 Perl_yyerror_pvn(pTHX_ const char *const s, STRLEN len, U32 flags)
11787 {
11788 const char *context = NULL;
11789 int contlen = -1;
11790 SV *msg;
11791 SV * const where_sv = newSVpvs_flags("", SVs_TEMP);
11792 int yychar = PL_parser->yychar;
11793
11794 /* Output error message 's' with length 'len'. 'flags' are SV flags that
11795 * apply. If the number of errors found is large enough, it abandons
11796 * parsing. If 's' is NULL, there is no message, and it abandons
11797 * processing unconditionally */
11798
11799 if (s != NULL) {
11800 if (!yychar || (yychar == ';' && !PL_rsfp))
11801 sv_catpvs(where_sv, "at EOF");
11802 else if ( PL_oldoldbufptr
11803 && PL_bufptr > PL_oldoldbufptr
11804 && PL_bufptr - PL_oldoldbufptr < 200
11805 && PL_oldoldbufptr != PL_oldbufptr
11806 && PL_oldbufptr != PL_bufptr)
11807 {
11808 /*
11809 Only for NetWare:
11810 The code below is removed for NetWare because it
11811 abends/crashes on NetWare when the script has error such as
11812 not having the closing quotes like:
11813 if ($var eq "value)
11814 Checking of white spaces is anyway done in NetWare code.
11815 */
11816 #ifndef NETWARE
11817 while (isSPACE(*PL_oldoldbufptr))
11818 PL_oldoldbufptr++;
11819 #endif
11820 context = PL_oldoldbufptr;
11821 contlen = PL_bufptr - PL_oldoldbufptr;
11822 }
11823 else if ( PL_oldbufptr
11824 && PL_bufptr > PL_oldbufptr
11825 && PL_bufptr - PL_oldbufptr < 200
11826 && PL_oldbufptr != PL_bufptr) {
11827 /*
11828 Only for NetWare:
11829 The code below is removed for NetWare because it
11830 abends/crashes on NetWare when the script has error such as
11831 not having the closing quotes like:
11832 if ($var eq "value)
11833 Checking of white spaces is anyway done in NetWare code.
11834 */
11835 #ifndef NETWARE
11836 while (isSPACE(*PL_oldbufptr))
11837 PL_oldbufptr++;
11838 #endif
11839 context = PL_oldbufptr;
11840 contlen = PL_bufptr - PL_oldbufptr;
11841 }
11842 else if (yychar > 255)
11843 sv_catpvs(where_sv, "next token ???");
11844 else if (yychar == YYEMPTY) {
11845 if (PL_lex_state == LEX_NORMAL)
11846 sv_catpvs(where_sv, "at end of line");
11847 else if (PL_lex_inpat)
11848 sv_catpvs(where_sv, "within pattern");
11849 else
11850 sv_catpvs(where_sv, "within string");
11851 }
11852 else {
11853 sv_catpvs(where_sv, "next char ");
11854 if (yychar < 32)
11855 Perl_sv_catpvf(aTHX_ where_sv, "^%c", toCTRL(yychar));
11856 else if (isPRINT_LC(yychar)) {
11857 const char string = yychar;
11858 sv_catpvn(where_sv, &string, 1);
11859 }
11860 else
11861 Perl_sv_catpvf(aTHX_ where_sv, "\\%03o", yychar & 255);
11862 }
11863 msg = newSVpvn_flags(s, len, (flags & SVf_UTF8) | SVs_TEMP);
11864 Perl_sv_catpvf(aTHX_ msg, " at %s line %" IVdf ", ",
11865 OutCopFILE(PL_curcop),
11866 (IV)(PL_parser->preambling == NOLINE
11867 ? CopLINE(PL_curcop)
11868 : PL_parser->preambling));
11869 if (context)
11870 Perl_sv_catpvf(aTHX_ msg, "near \"%" UTF8f "\"\n",
11871 UTF8fARG(UTF, contlen, context));
11872 else
11873 Perl_sv_catpvf(aTHX_ msg, "%" SVf "\n", SVfARG(where_sv));
11874 if ( PL_multi_start < PL_multi_end
11875 && (U32)(CopLINE(PL_curcop) - PL_multi_end) <= 1)
11876 {
11877 Perl_sv_catpvf(aTHX_ msg,
11878 " (Might be a runaway multi-line %c%c string starting on"
11879 " line %" IVdf ")\n",
11880 (int)PL_multi_open,(int)PL_multi_close,(IV)PL_multi_start);
11881 PL_multi_end = 0;
11882 }
11883 if (PL_in_eval & EVAL_WARNONLY) {
11884 PL_in_eval &= ~EVAL_WARNONLY;
11885 Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX), "%" SVf, SVfARG(msg));
11886 }
11887 else {
11888 qerror(msg);
11889 }
11890 }
11891 if (s == NULL || PL_error_count >= 10) {
11892 const char * msg = "";
11893 const char * const name = OutCopFILE(PL_curcop);
11894
11895 if (PL_in_eval) {
11896 SV * errsv = ERRSV;
11897 if (SvCUR(errsv)) {
11898 msg = Perl_form(aTHX_ "%" SVf, SVfARG(errsv));
11899 }
11900 }
11901
11902 if (s == NULL) {
11903 abort_execution(msg, name);
11904 }
11905 else {
11906 Perl_croak(aTHX_ "%s%s has too many errors.\n", msg, name);
11907 }
11908 }
11909 PL_in_my = 0;
11910 PL_in_my_stash = NULL;
11911 return 0;
11912 }
11913
11914 STATIC char*
11915 S_swallow_bom(pTHX_ U8 *s)
11916 {
11917 const STRLEN slen = SvCUR(PL_linestr);
11918
11919 PERL_ARGS_ASSERT_SWALLOW_BOM;
11920
11921 switch (s[0]) {
11922 case 0xFF:
11923 if (s[1] == 0xFE) {
11924 /* UTF-16 little-endian? (or UTF-32LE?) */
11925 if (s[2] == 0 && s[3] == 0) /* UTF-32 little-endian */
11926 /* diag_listed_as: Unsupported script encoding %s */
11927 Perl_croak(aTHX_ "Unsupported script encoding UTF-32LE");
11928 #ifndef PERL_NO_UTF16_FILTER
11929 #ifdef DEBUGGING
11930 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (BOM)\n");
11931 #endif
11932 s += 2;
11933 if (PL_bufend > (char*)s) {
11934 s = add_utf16_textfilter(s, TRUE);
11935 }
11936 #else
11937 /* diag_listed_as: Unsupported script encoding %s */
11938 Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
11939 #endif
11940 }
11941 break;
11942 case 0xFE:
11943 if (s[1] == 0xFF) { /* UTF-16 big-endian? */
11944 #ifndef PERL_NO_UTF16_FILTER
11945 #ifdef DEBUGGING
11946 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (BOM)\n");
11947 #endif
11948 s += 2;
11949 if (PL_bufend > (char *)s) {
11950 s = add_utf16_textfilter(s, FALSE);
11951 }
11952 #else
11953 /* diag_listed_as: Unsupported script encoding %s */
11954 Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
11955 #endif
11956 }
11957 break;
11958 case BOM_UTF8_FIRST_BYTE: {
11959 if (memBEGINs(s+1, slen - 1, BOM_UTF8_TAIL)) {
11960 #ifdef DEBUGGING
11961 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-8 script encoding (BOM)\n");
11962 #endif
11963 s += sizeof(BOM_UTF8) - 1; /* UTF-8 */
11964 }
11965 break;
11966 }
11967 case 0:
11968 if (slen > 3) {
11969 if (s[1] == 0) {
11970 if (s[2] == 0xFE && s[3] == 0xFF) {
11971 /* UTF-32 big-endian */
11972 /* diag_listed_as: Unsupported script encoding %s */
11973 Perl_croak(aTHX_ "Unsupported script encoding UTF-32BE");
11974 }
11975 }
11976 else if (s[2] == 0 && s[3] != 0) {
11977 /* Leading bytes
11978 * 00 xx 00 xx
11979 * are a good indicator of UTF-16BE. */
11980 #ifndef PERL_NO_UTF16_FILTER
11981 #ifdef DEBUGGING
11982 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (no BOM)\n");
11983 #endif
11984 s = add_utf16_textfilter(s, FALSE);
11985 #else
11986 /* diag_listed_as: Unsupported script encoding %s */
11987 Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
11988 #endif
11989 }
11990 }
11991 break;
11992
11993 default:
11994 if (slen > 3 && s[1] == 0 && s[2] != 0 && s[3] == 0) {
11995 /* Leading bytes
11996 * xx 00 xx 00
11997 * are a good indicator of UTF-16LE. */
11998 #ifndef PERL_NO_UTF16_FILTER
11999 #ifdef DEBUGGING
12000 if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (no BOM)\n");
12001 #endif
12002 s = add_utf16_textfilter(s, TRUE);
12003 #else
12004 /* diag_listed_as: Unsupported script encoding %s */
12005 Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
12006 #endif
12007 }
12008 }
12009 return (char*)s;
12010 }
12011
12012
12013 #ifndef PERL_NO_UTF16_FILTER
12014 static I32
12015 S_utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen)
12016 {
12017 SV *const filter = FILTER_DATA(idx);
12018 /* We re-use this each time round, throwing the contents away before we
12019 return. */
12020 SV *const utf16_buffer = MUTABLE_SV(IoTOP_GV(filter));
12021 SV *const utf8_buffer = filter;
12022 IV status = IoPAGE(filter);
12023 const bool reverse = cBOOL(IoLINES(filter));
12024 I32 retval;
12025
12026 PERL_ARGS_ASSERT_UTF16_TEXTFILTER;
12027
12028 /* As we're automatically added, at the lowest level, and hence only called
12029 from this file, we can be sure that we're not called in block mode. Hence
12030 don't bother writing code to deal with block mode. */
12031 if (maxlen) {
12032 Perl_croak(aTHX_ "panic: utf16_textfilter called in block mode (for %d characters)", maxlen);
12033 }
12034 if (status < 0) {
12035 Perl_croak(aTHX_ "panic: utf16_textfilter called after error (status=%" IVdf ")", status);
12036 }
12037 DEBUG_P(PerlIO_printf(Perl_debug_log,
12038 "utf16_textfilter(%p,%ce): idx=%d maxlen=%d status=%" IVdf " utf16=%" UVuf " utf8=%" UVuf "\n",
12039 FPTR2DPTR(void *, S_utf16_textfilter),
12040 reverse ? 'l' : 'b', idx, maxlen, status,
12041 (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
12042
12043 while (1) {
12044 STRLEN chars;
12045 STRLEN have;
12046 I32 newlen;
12047 U8 *end;
12048 /* First, look in our buffer of existing UTF-8 data: */
12049 char *nl = (char *)memchr(SvPVX(utf8_buffer), '\n', SvCUR(utf8_buffer));
12050
12051 if (nl) {
12052 ++nl;
12053 } else if (status == 0) {
12054 /* EOF */
12055 IoPAGE(filter) = 0;
12056 nl = SvEND(utf8_buffer);
12057 }
12058 if (nl) {
12059 STRLEN got = nl - SvPVX(utf8_buffer);
12060 /* Did we have anything to append? */
12061 retval = got != 0;
12062 sv_catpvn(sv, SvPVX(utf8_buffer), got);
12063 /* Everything else in this code works just fine if SVp_POK isn't
12064 set. This, however, needs it, and we need it to work, else
12065 we loop infinitely because the buffer is never consumed. */
12066 sv_chop(utf8_buffer, nl);
12067 break;
12068 }
12069
12070 /* OK, not a complete line there, so need to read some more UTF-16.
12071 Read an extra octect if the buffer currently has an odd number. */
12072 while (1) {
12073 if (status <= 0)
12074 break;
12075 if (SvCUR(utf16_buffer) >= 2) {
12076 /* Location of the high octet of the last complete code point.
12077 Gosh, UTF-16 is a pain. All the benefits of variable length,
12078 *coupled* with all the benefits of partial reads and
12079 endianness. */
12080 const U8 *const last_hi = (U8*)SvPVX(utf16_buffer)
12081 + ((SvCUR(utf16_buffer) & ~1) - (reverse ? 1 : 2));
12082
12083 if (*last_hi < 0xd8 || *last_hi > 0xdb) {
12084 break;
12085 }
12086
12087 /* We have the first half of a surrogate. Read more. */
12088 DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter partial surrogate detected at %p\n", last_hi));
12089 }
12090
12091 status = FILTER_READ(idx + 1, utf16_buffer,
12092 160 + (SvCUR(utf16_buffer) & 1));
12093 DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter status=%" IVdf " SvCUR(sv)=%" UVuf "\n", status, (UV)SvCUR(utf16_buffer)));
12094 DEBUG_P({ sv_dump(utf16_buffer); sv_dump(utf8_buffer);});
12095 if (status < 0) {
12096 /* Error */
12097 IoPAGE(filter) = status;
12098 return status;
12099 }
12100 }
12101
12102 /* 'chars' isn't quite the right name, as code points above 0xFFFF
12103 * require 4 bytes per char */
12104 chars = SvCUR(utf16_buffer) >> 1;
12105 have = SvCUR(utf8_buffer);
12106
12107 /* Assume the worst case size as noted by the functions: twice the
12108 * number of input bytes */
12109 SvGROW(utf8_buffer, have + chars * 4 + 1);
12110
12111 if (reverse) {
12112 end = utf16_to_utf8_reversed((U8*)SvPVX(utf16_buffer),
12113 (U8*)SvPVX_const(utf8_buffer) + have,
12114 chars * 2, &newlen);
12115 } else {
12116 end = utf16_to_utf8((U8*)SvPVX(utf16_buffer),
12117 (U8*)SvPVX_const(utf8_buffer) + have,
12118 chars * 2, &newlen);
12119 }
12120 SvCUR_set(utf8_buffer, have + newlen);
12121 *end = '\0';
12122
12123 /* No need to keep this SV "well-formed" with a '\0' after the end, as
12124 it's private to us, and utf16_to_utf8{,reversed} take a
12125 (pointer,length) pair, rather than a NUL-terminated string. */
12126 if(SvCUR(utf16_buffer) & 1) {
12127 *SvPVX(utf16_buffer) = SvEND(utf16_buffer)[-1];
12128 SvCUR_set(utf16_buffer, 1);
12129 } else {
12130 SvCUR_set(utf16_buffer, 0);
12131 }
12132 }
12133 DEBUG_P(PerlIO_printf(Perl_debug_log,
12134 "utf16_textfilter: returns, status=%" IVdf " utf16=%" UVuf " utf8=%" UVuf "\n",
12135 status,
12136 (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
12137 DEBUG_P({ sv_dump(utf8_buffer); sv_dump(sv);});
12138 return retval;
12139 }
12140
12141 static U8 *
12142 S_add_utf16_textfilter(pTHX_ U8 *const s, bool reversed)
12143 {
12144 SV *filter = filter_add(S_utf16_textfilter, NULL);
12145
12146 PERL_ARGS_ASSERT_ADD_UTF16_TEXTFILTER;
12147
12148 IoTOP_GV(filter) = MUTABLE_GV(newSVpvn((char *)s, PL_bufend - (char*)s));
12149 SvPVCLEAR(filter);
12150 IoLINES(filter) = reversed;
12151 IoPAGE(filter) = 1; /* Not EOF */
12152
12153 /* Sadly, we have to return a valid pointer, come what may, so we have to
12154 ignore any error return from this. */
12155 SvCUR_set(PL_linestr, 0);
12156 if (FILTER_READ(0, PL_linestr, 0)) {
12157 SvUTF8_on(PL_linestr);
12158 } else {
12159 SvUTF8_on(PL_linestr);
12160 }
12161 PL_bufend = SvEND(PL_linestr);
12162 return (U8*)SvPVX(PL_linestr);
12163 }
12164 #endif
12165
12166 /*
12167 Returns a pointer to the next character after the parsed
12168 vstring, as well as updating the passed in sv.
12169
12170 Function must be called like
12171
12172 sv = sv_2mortal(newSV(5));
12173 s = scan_vstring(s,e,sv);
12174
12175 where s and e are the start and end of the string.
12176 The sv should already be large enough to store the vstring
12177 passed in, for performance reasons.
12178
12179 This function may croak if fatal warnings are enabled in the
12180 calling scope, hence the sv_2mortal in the example (to prevent
12181 a leak). Make sure to do SvREFCNT_inc afterwards if you use
12182 sv_2mortal.
12183
12184 */
12185
12186 char *
12187 Perl_scan_vstring(pTHX_ const char *s, const char *const e, SV *sv)
12188 {
12189 const char *pos = s;
12190 const char *start = s;
12191
12192 PERL_ARGS_ASSERT_SCAN_VSTRING;
12193
12194 if (*pos == 'v') pos++; /* get past 'v' */
12195 while (pos < e && (isDIGIT(*pos) || *pos == '_'))
12196 pos++;
12197 if ( *pos != '.') {
12198 /* this may not be a v-string if followed by => */
12199 const char *next = pos;
12200 while (next < e && isSPACE(*next))
12201 ++next;
12202 if ((e - next) >= 2 && *next == '=' && next[1] == '>' ) {
12203 /* return string not v-string */
12204 sv_setpvn(sv,(char *)s,pos-s);
12205 return (char *)pos;
12206 }
12207 }
12208
12209 if (!isALPHA(*pos)) {
12210 U8 tmpbuf[UTF8_MAXBYTES+1];
12211
12212 if (*s == 'v')
12213 s++; /* get past 'v' */
12214
12215 SvPVCLEAR(sv);
12216
12217 for (;;) {
12218 /* this is atoi() that tolerates underscores */
12219 U8 *tmpend;
12220 UV rev = 0;
12221 const char *end = pos;
12222 UV mult = 1;
12223 while (--end >= s) {
12224 if (*end != '_') {
12225 const UV orev = rev;
12226 rev += (*end - '0') * mult;
12227 mult *= 10;
12228 if (orev > rev)
12229 /* diag_listed_as: Integer overflow in %s number */
12230 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
12231 "Integer overflow in decimal number");
12232 }
12233 }
12234
12235 /* Append native character for the rev point */
12236 tmpend = uvchr_to_utf8(tmpbuf, rev);
12237 sv_catpvn(sv, (const char*)tmpbuf, tmpend - tmpbuf);
12238 if (!UVCHR_IS_INVARIANT(rev))
12239 SvUTF8_on(sv);
12240 if (pos + 1 < e && *pos == '.' && isDIGIT(pos[1]))
12241 s = ++pos;
12242 else {
12243 s = pos;
12244 break;
12245 }
12246 while (pos < e && (isDIGIT(*pos) || *pos == '_'))
12247 pos++;
12248 }
12249 SvPOK_on(sv);
12250 sv_magic(sv,NULL,PERL_MAGIC_vstring,(const char*)start, pos-start);
12251 SvRMAGICAL_on(sv);
12252 }
12253 return (char *)s;
12254 }
12255
12256 int
12257 Perl_keyword_plugin_standard(pTHX_
12258 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
12259 {
12260 PERL_ARGS_ASSERT_KEYWORD_PLUGIN_STANDARD;
12261 PERL_UNUSED_CONTEXT;
12262 PERL_UNUSED_ARG(keyword_ptr);
12263 PERL_UNUSED_ARG(keyword_len);
12264 PERL_UNUSED_ARG(op_ptr);
12265 return KEYWORD_PLUGIN_DECLINE;
12266 }
12267
12268 /*
12269 =for apidoc Amx|void|wrap_keyword_plugin|Perl_keyword_plugin_t new_plugin|Perl_keyword_plugin_t *old_plugin_p
12270
12271 Puts a C function into the chain of keyword plugins. This is the
12272 preferred way to manipulate the L</PL_keyword_plugin> variable.
12273 C<new_plugin> is a pointer to the C function that is to be added to the
12274 keyword plugin chain, and C<old_plugin_p> points to the storage location
12275 where a pointer to the next function in the chain will be stored. The
12276 value of C<new_plugin> is written into the L</PL_keyword_plugin> variable,
12277 while the value previously stored there is written to C<*old_plugin_p>.
12278
12279 L</PL_keyword_plugin> is global to an entire process, and a module wishing
12280 to hook keyword parsing may find itself invoked more than once per
12281 process, typically in different threads. To handle that situation, this
12282 function is idempotent. The location C<*old_plugin_p> must initially
12283 (once per process) contain a null pointer. A C variable of static
12284 duration (declared at file scope, typically also marked C<static> to give
12285 it internal linkage) will be implicitly initialised appropriately, if it
12286 does not have an explicit initialiser. This function will only actually
12287 modify the plugin chain if it finds C<*old_plugin_p> to be null. This
12288 function is also thread safe on the small scale. It uses appropriate
12289 locking to avoid race conditions in accessing L</PL_keyword_plugin>.
12290
12291 When this function is called, the function referenced by C<new_plugin>
12292 must be ready to be called, except for C<*old_plugin_p> being unfilled.
12293 In a threading situation, C<new_plugin> may be called immediately, even
12294 before this function has returned. C<*old_plugin_p> will always be
12295 appropriately set before C<new_plugin> is called. If C<new_plugin>
12296 decides not to do anything special with the identifier that it is given
12297 (which is the usual case for most calls to a keyword plugin), it must
12298 chain the plugin function referenced by C<*old_plugin_p>.
12299
12300 Taken all together, XS code to install a keyword plugin should typically
12301 look something like this:
12302
12303 static Perl_keyword_plugin_t next_keyword_plugin;
12304 static OP *my_keyword_plugin(pTHX_
12305 char *keyword_plugin, STRLEN keyword_len, OP **op_ptr)
12306 {
12307 if (memEQs(keyword_ptr, keyword_len,
12308 "my_new_keyword")) {
12309 ...
12310 } else {
12311 return next_keyword_plugin(aTHX_
12312 keyword_ptr, keyword_len, op_ptr);
12313 }
12314 }
12315 BOOT:
12316 wrap_keyword_plugin(my_keyword_plugin,
12317 &next_keyword_plugin);
12318
12319 Direct access to L</PL_keyword_plugin> should be avoided.
12320
12321 =cut
12322 */
12323
12324 void
12325 Perl_wrap_keyword_plugin(pTHX_
12326 Perl_keyword_plugin_t new_plugin, Perl_keyword_plugin_t *old_plugin_p)
12327 {
12328 dVAR;
12329
12330 PERL_UNUSED_CONTEXT;
12331 PERL_ARGS_ASSERT_WRAP_KEYWORD_PLUGIN;
12332 if (*old_plugin_p) return;
12333 KEYWORD_PLUGIN_MUTEX_LOCK;
12334 if (!*old_plugin_p) {
12335 *old_plugin_p = PL_keyword_plugin;
12336 PL_keyword_plugin = new_plugin;
12337 }
12338 KEYWORD_PLUGIN_MUTEX_UNLOCK;
12339 }
12340
12341 #define parse_recdescent(g,p) S_parse_recdescent(aTHX_ g,p)
12342 static void
12343 S_parse_recdescent(pTHX_ int gramtype, I32 fakeeof)
12344 {
12345 SAVEI32(PL_lex_brackets);
12346 if (PL_lex_brackets > 100)
12347 Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
12348 PL_lex_brackstack[PL_lex_brackets++] = XFAKEEOF;
12349 SAVEI32(PL_lex_allbrackets);
12350 PL_lex_allbrackets = 0;
12351 SAVEI8(PL_lex_fakeeof);
12352 PL_lex_fakeeof = (U8)fakeeof;
12353 if(yyparse(gramtype) && !PL_parser->error_count)
12354 qerror(Perl_mess(aTHX_ "Parse error"));
12355 }
12356
12357 #define parse_recdescent_for_op(g,p) S_parse_recdescent_for_op(aTHX_ g,p)
12358 static OP *
12359 S_parse_recdescent_for_op(pTHX_ int gramtype, I32 fakeeof)
12360 {
12361 OP *o;
12362 ENTER;
12363 SAVEVPTR(PL_eval_root);
12364 PL_eval_root = NULL;
12365 parse_recdescent(gramtype, fakeeof);
12366 o = PL_eval_root;
12367 LEAVE;
12368 return o;
12369 }
12370
12371 #define parse_expr(p,f) S_parse_expr(aTHX_ p,f)
12372 static OP *
12373 S_parse_expr(pTHX_ I32 fakeeof, U32 flags)
12374 {
12375 OP *exprop;
12376 if (flags & ~PARSE_OPTIONAL)
12377 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_expr");
12378 exprop = parse_recdescent_for_op(GRAMEXPR, fakeeof);
12379 if (!exprop && !(flags & PARSE_OPTIONAL)) {
12380 if (!PL_parser->error_count)
12381 qerror(Perl_mess(aTHX_ "Parse error"));
12382 exprop = newOP(OP_NULL, 0);
12383 }
12384 return exprop;
12385 }
12386
12387 /*
12388 =for apidoc Amx|OP *|parse_arithexpr|U32 flags
12389
12390 Parse a Perl arithmetic expression. This may contain operators of precedence
12391 down to the bit shift operators. The expression must be followed (and thus
12392 terminated) either by a comparison or lower-precedence operator or by
12393 something that would normally terminate an expression such as semicolon.
12394 If C<flags> has the C<PARSE_OPTIONAL> bit set, then the expression is optional,
12395 otherwise it is mandatory. It is up to the caller to ensure that the
12396 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12397 the source of the code to be parsed and the lexical context for the
12398 expression.
12399
12400 The op tree representing the expression is returned. If an optional
12401 expression is absent, a null pointer is returned, otherwise the pointer
12402 will be non-null.
12403
12404 If an error occurs in parsing or compilation, in most cases a valid op
12405 tree is returned anyway. The error is reflected in the parser state,
12406 normally resulting in a single exception at the top level of parsing
12407 which covers all the compilation errors that occurred. Some compilation
12408 errors, however, will throw an exception immediately.
12409
12410 =cut
12411 */
12412
12413 OP *
12414 Perl_parse_arithexpr(pTHX_ U32 flags)
12415 {
12416 return parse_expr(LEX_FAKEEOF_COMPARE, flags);
12417 }
12418
12419 /*
12420 =for apidoc Amx|OP *|parse_termexpr|U32 flags
12421
12422 Parse a Perl term expression. This may contain operators of precedence
12423 down to the assignment operators. The expression must be followed (and thus
12424 terminated) either by a comma or lower-precedence operator or by
12425 something that would normally terminate an expression such as semicolon.
12426 If C<flags> has the C<PARSE_OPTIONAL> bit set, then the expression is optional,
12427 otherwise it is mandatory. It is up to the caller to ensure that the
12428 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12429 the source of the code to be parsed and the lexical context for the
12430 expression.
12431
12432 The op tree representing the expression is returned. If an optional
12433 expression is absent, a null pointer is returned, otherwise the pointer
12434 will be non-null.
12435
12436 If an error occurs in parsing or compilation, in most cases a valid op
12437 tree is returned anyway. The error is reflected in the parser state,
12438 normally resulting in a single exception at the top level of parsing
12439 which covers all the compilation errors that occurred. Some compilation
12440 errors, however, will throw an exception immediately.
12441
12442 =cut
12443 */
12444
12445 OP *
12446 Perl_parse_termexpr(pTHX_ U32 flags)
12447 {
12448 return parse_expr(LEX_FAKEEOF_COMMA, flags);
12449 }
12450
12451 /*
12452 =for apidoc Amx|OP *|parse_listexpr|U32 flags
12453
12454 Parse a Perl list expression. This may contain operators of precedence
12455 down to the comma operator. The expression must be followed (and thus
12456 terminated) either by a low-precedence logic operator such as C<or> or by
12457 something that would normally terminate an expression such as semicolon.
12458 If C<flags> has the C<PARSE_OPTIONAL> bit set, then the expression is optional,
12459 otherwise it is mandatory. It is up to the caller to ensure that the
12460 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12461 the source of the code to be parsed and the lexical context for the
12462 expression.
12463
12464 The op tree representing the expression is returned. If an optional
12465 expression is absent, a null pointer is returned, otherwise the pointer
12466 will be non-null.
12467
12468 If an error occurs in parsing or compilation, in most cases a valid op
12469 tree is returned anyway. The error is reflected in the parser state,
12470 normally resulting in a single exception at the top level of parsing
12471 which covers all the compilation errors that occurred. Some compilation
12472 errors, however, will throw an exception immediately.
12473
12474 =cut
12475 */
12476
12477 OP *
12478 Perl_parse_listexpr(pTHX_ U32 flags)
12479 {
12480 return parse_expr(LEX_FAKEEOF_LOWLOGIC, flags);
12481 }
12482
12483 /*
12484 =for apidoc Amx|OP *|parse_fullexpr|U32 flags
12485
12486 Parse a single complete Perl expression. This allows the full
12487 expression grammar, including the lowest-precedence operators such
12488 as C<or>. The expression must be followed (and thus terminated) by a
12489 token that an expression would normally be terminated by: end-of-file,
12490 closing bracketing punctuation, semicolon, or one of the keywords that
12491 signals a postfix expression-statement modifier. If C<flags> has the
12492 C<PARSE_OPTIONAL> bit set, then the expression is optional, otherwise it is
12493 mandatory. It is up to the caller to ensure that the dynamic parser
12494 state (L</PL_parser> et al) is correctly set to reflect the source of
12495 the code to be parsed and the lexical context for the expression.
12496
12497 The op tree representing the expression is returned. If an optional
12498 expression is absent, a null pointer is returned, otherwise the pointer
12499 will be non-null.
12500
12501 If an error occurs in parsing or compilation, in most cases a valid op
12502 tree is returned anyway. The error is reflected in the parser state,
12503 normally resulting in a single exception at the top level of parsing
12504 which covers all the compilation errors that occurred. Some compilation
12505 errors, however, will throw an exception immediately.
12506
12507 =cut
12508 */
12509
12510 OP *
12511 Perl_parse_fullexpr(pTHX_ U32 flags)
12512 {
12513 return parse_expr(LEX_FAKEEOF_NONEXPR, flags);
12514 }
12515
12516 /*
12517 =for apidoc Amx|OP *|parse_block|U32 flags
12518
12519 Parse a single complete Perl code block. This consists of an opening
12520 brace, a sequence of statements, and a closing brace. The block
12521 constitutes a lexical scope, so C<my> variables and various compile-time
12522 effects can be contained within it. It is up to the caller to ensure
12523 that the dynamic parser state (L</PL_parser> et al) is correctly set to
12524 reflect the source of the code to be parsed and the lexical context for
12525 the statement.
12526
12527 The op tree representing the code block is returned. This is always a
12528 real op, never a null pointer. It will normally be a C<lineseq> list,
12529 including C<nextstate> or equivalent ops. No ops to construct any kind
12530 of runtime scope are included by virtue of it being a block.
12531
12532 If an error occurs in parsing or compilation, in most cases a valid op
12533 tree (most likely null) is returned anyway. The error is reflected in
12534 the parser state, normally resulting in a single exception at the top
12535 level of parsing which covers all the compilation errors that occurred.
12536 Some compilation errors, however, will throw an exception immediately.
12537
12538 The C<flags> parameter is reserved for future use, and must always
12539 be zero.
12540
12541 =cut
12542 */
12543
12544 OP *
12545 Perl_parse_block(pTHX_ U32 flags)
12546 {
12547 if (flags)
12548 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_block");
12549 return parse_recdescent_for_op(GRAMBLOCK, LEX_FAKEEOF_NEVER);
12550 }
12551
12552 /*
12553 =for apidoc Amx|OP *|parse_barestmt|U32 flags
12554
12555 Parse a single unadorned Perl statement. This may be a normal imperative
12556 statement or a declaration that has compile-time effect. It does not
12557 include any label or other affixture. It is up to the caller to ensure
12558 that the dynamic parser state (L</PL_parser> et al) is correctly set to
12559 reflect the source of the code to be parsed and the lexical context for
12560 the statement.
12561
12562 The op tree representing the statement is returned. This may be a
12563 null pointer if the statement is null, for example if it was actually
12564 a subroutine definition (which has compile-time side effects). If not
12565 null, it will be ops directly implementing the statement, suitable to
12566 pass to L</newSTATEOP>. It will not normally include a C<nextstate> or
12567 equivalent op (except for those embedded in a scope contained entirely
12568 within the statement).
12569
12570 If an error occurs in parsing or compilation, in most cases a valid op
12571 tree (most likely null) is returned anyway. The error is reflected in
12572 the parser state, normally resulting in a single exception at the top
12573 level of parsing which covers all the compilation errors that occurred.
12574 Some compilation errors, however, will throw an exception immediately.
12575
12576 The C<flags> parameter is reserved for future use, and must always
12577 be zero.
12578
12579 =cut
12580 */
12581
12582 OP *
12583 Perl_parse_barestmt(pTHX_ U32 flags)
12584 {
12585 if (flags)
12586 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_barestmt");
12587 return parse_recdescent_for_op(GRAMBARESTMT, LEX_FAKEEOF_NEVER);
12588 }
12589
12590 /*
12591 =for apidoc Amx|SV *|parse_label|U32 flags
12592
12593 Parse a single label, possibly optional, of the type that may prefix a
12594 Perl statement. It is up to the caller to ensure that the dynamic parser
12595 state (L</PL_parser> et al) is correctly set to reflect the source of
12596 the code to be parsed. If C<flags> has the C<PARSE_OPTIONAL> bit set, then the
12597 label is optional, otherwise it is mandatory.
12598
12599 The name of the label is returned in the form of a fresh scalar. If an
12600 optional label is absent, a null pointer is returned.
12601
12602 If an error occurs in parsing, which can only occur if the label is
12603 mandatory, a valid label is returned anyway. The error is reflected in
12604 the parser state, normally resulting in a single exception at the top
12605 level of parsing which covers all the compilation errors that occurred.
12606
12607 =cut
12608 */
12609
12610 SV *
12611 Perl_parse_label(pTHX_ U32 flags)
12612 {
12613 if (flags & ~PARSE_OPTIONAL)
12614 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_label");
12615 if (PL_nexttoke) {
12616 PL_parser->yychar = yylex();
12617 if (PL_parser->yychar == LABEL) {
12618 SV * const labelsv = cSVOPx(pl_yylval.opval)->op_sv;
12619 PL_parser->yychar = YYEMPTY;
12620 cSVOPx(pl_yylval.opval)->op_sv = NULL;
12621 op_free(pl_yylval.opval);
12622 return labelsv;
12623 } else {
12624 yyunlex();
12625 goto no_label;
12626 }
12627 } else {
12628 char *s, *t;
12629 STRLEN wlen, bufptr_pos;
12630 lex_read_space(0);
12631 t = s = PL_bufptr;
12632 if (!isIDFIRST_lazy_if_safe(s, PL_bufend, UTF))
12633 goto no_label;
12634 t = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &wlen);
12635 if (word_takes_any_delimiter(s, wlen))
12636 goto no_label;
12637 bufptr_pos = s - SvPVX(PL_linestr);
12638 PL_bufptr = t;
12639 lex_read_space(LEX_KEEP_PREVIOUS);
12640 t = PL_bufptr;
12641 s = SvPVX(PL_linestr) + bufptr_pos;
12642 if (t[0] == ':' && t[1] != ':') {
12643 PL_oldoldbufptr = PL_oldbufptr;
12644 PL_oldbufptr = s;
12645 PL_bufptr = t+1;
12646 return newSVpvn_flags(s, wlen, UTF ? SVf_UTF8 : 0);
12647 } else {
12648 PL_bufptr = s;
12649 no_label:
12650 if (flags & PARSE_OPTIONAL) {
12651 return NULL;
12652 } else {
12653 qerror(Perl_mess(aTHX_ "Parse error"));
12654 return newSVpvs("x");
12655 }
12656 }
12657 }
12658 }
12659
12660 /*
12661 =for apidoc Amx|OP *|parse_fullstmt|U32 flags
12662
12663 Parse a single complete Perl statement. This may be a normal imperative
12664 statement or a declaration that has compile-time effect, and may include
12665 optional labels. It is up to the caller to ensure that the dynamic
12666 parser state (L</PL_parser> et al) is correctly set to reflect the source
12667 of the code to be parsed and the lexical context for the statement.
12668
12669 The op tree representing the statement is returned. This may be a
12670 null pointer if the statement is null, for example if it was actually
12671 a subroutine definition (which has compile-time side effects). If not
12672 null, it will be the result of a L</newSTATEOP> call, normally including
12673 a C<nextstate> or equivalent op.
12674
12675 If an error occurs in parsing or compilation, in most cases a valid op
12676 tree (most likely null) is returned anyway. The error is reflected in
12677 the parser state, normally resulting in a single exception at the top
12678 level of parsing which covers all the compilation errors that occurred.
12679 Some compilation errors, however, will throw an exception immediately.
12680
12681 The C<flags> parameter is reserved for future use, and must always
12682 be zero.
12683
12684 =cut
12685 */
12686
12687 OP *
12688 Perl_parse_fullstmt(pTHX_ U32 flags)
12689 {
12690 if (flags)
12691 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_fullstmt");
12692 return parse_recdescent_for_op(GRAMFULLSTMT, LEX_FAKEEOF_NEVER);
12693 }
12694
12695 /*
12696 =for apidoc Amx|OP *|parse_stmtseq|U32 flags
12697
12698 Parse a sequence of zero or more Perl statements. These may be normal
12699 imperative statements, including optional labels, or declarations
12700 that have compile-time effect, or any mixture thereof. The statement
12701 sequence ends when a closing brace or end-of-file is encountered in a
12702 place where a new statement could have validly started. It is up to
12703 the caller to ensure that the dynamic parser state (L</PL_parser> et al)
12704 is correctly set to reflect the source of the code to be parsed and the
12705 lexical context for the statements.
12706
12707 The op tree representing the statement sequence is returned. This may
12708 be a null pointer if the statements were all null, for example if there
12709 were no statements or if there were only subroutine definitions (which
12710 have compile-time side effects). If not null, it will be a C<lineseq>
12711 list, normally including C<nextstate> or equivalent ops.
12712
12713 If an error occurs in parsing or compilation, in most cases a valid op
12714 tree is returned anyway. The error is reflected in the parser state,
12715 normally resulting in a single exception at the top level of parsing
12716 which covers all the compilation errors that occurred. Some compilation
12717 errors, however, will throw an exception immediately.
12718
12719 The C<flags> parameter is reserved for future use, and must always
12720 be zero.
12721
12722 =cut
12723 */
12724
12725 OP *
12726 Perl_parse_stmtseq(pTHX_ U32 flags)
12727 {
12728 OP *stmtseqop;
12729 I32 c;
12730 if (flags)
12731 Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_stmtseq");
12732 stmtseqop = parse_recdescent_for_op(GRAMSTMTSEQ, LEX_FAKEEOF_CLOSING);
12733 c = lex_peek_unichar(0);
12734 if (c != -1 && c != /*{*/'}')
12735 qerror(Perl_mess(aTHX_ "Parse error"));
12736 return stmtseqop;
12737 }
12738
12739 /*
12740 * ex: set ts=8 sts=4 sw=4 et:
12741 */
12742