This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regexec.c: Improve comments for a function
[perl5.git] / toke.c
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)     \
45         S_new_constant(aTHX_ a,b,STR_WITH_LEN(c),d,e,f, g)
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_PVAL,         "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
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
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
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
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
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
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
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
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
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 = FALSE;
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
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
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
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 *
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
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 = 0;    /* Count of variants */
1026             const char *p, *e = pv+len;
1027             for (p = pv; p != e; p++) {
1028                 if (! UTF8_IS_INVARIANT(*p)) {
1029                     highhalf++;
1030                 }
1031             }
1032             if (!highhalf)
1033                 goto plain_copy;
1034             lex_grow_linestr(SvCUR(PL_parser->linestr)+1+len+highhalf);
1035             bufptr = PL_parser->bufptr;
1036             Move(bufptr, bufptr+len+highhalf, PL_parser->bufend+1-bufptr, char);
1037             SvCUR_set(PL_parser->linestr,
1038                 SvCUR(PL_parser->linestr) + len+highhalf);
1039             PL_parser->bufend += len+highhalf;
1040             for (p = pv; p != e; p++) {
1041                 append_utf8_from_native_byte(*p, (U8 **) &bufptr);
1042             }
1043         }
1044     } else {
1045         if (flags & LEX_STUFF_UTF8) {
1046             STRLEN highhalf = 0;
1047             const char *p, *e = pv+len;
1048             for (p = pv; p != e; p++) {
1049                 U8 c = (U8)*p;
1050                 if (UTF8_IS_ABOVE_LATIN1(c)) {
1051                     Perl_croak(aTHX_ "Lexing code attempted to stuff "
1052                                 "non-Latin-1 character into Latin-1 input");
1053                 } else if (UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(p, e)) {
1054                     p++;
1055                     highhalf++;
1056                 } else assert(UTF8_IS_INVARIANT(c));
1057             }
1058             if (!highhalf)
1059                 goto plain_copy;
1060             lex_grow_linestr(SvCUR(PL_parser->linestr)+1+len-highhalf);
1061             bufptr = PL_parser->bufptr;
1062             Move(bufptr, bufptr+len-highhalf, PL_parser->bufend+1-bufptr, char);
1063             SvCUR_set(PL_parser->linestr,
1064                 SvCUR(PL_parser->linestr) + len-highhalf);
1065             PL_parser->bufend += len-highhalf;
1066             p = pv;
1067             while (p < e) {
1068                 if (UTF8_IS_INVARIANT(*p)) {
1069                     *bufptr++ = *p;
1070                     p++;
1071                 }
1072                 else {
1073                     assert(p < e -1 );
1074                     *bufptr++ = EIGHT_BIT_UTF8_TO_NATIVE(*p, *(p+1));
1075                     p += 2;
1076                 }
1077             }
1078         } else {
1079           plain_copy:
1080             lex_grow_linestr(SvCUR(PL_parser->linestr)+1+len);
1081             bufptr = PL_parser->bufptr;
1082             Move(bufptr, bufptr+len, PL_parser->bufend+1-bufptr, char);
1083             SvCUR_set(PL_parser->linestr, SvCUR(PL_parser->linestr) + len);
1084             PL_parser->bufend += len;
1085             Copy(pv, bufptr, len, char);
1086         }
1087     }
1088 }
1089
1090 /*
1091 =for apidoc Amx|void|lex_stuff_pv|const char *pv|U32 flags
1092
1093 Insert characters into the lexer buffer (L</PL_parser-E<gt>linestr>),
1094 immediately after the current lexing point (L</PL_parser-E<gt>bufptr>),
1095 reallocating the buffer if necessary.  This means that lexing code that
1096 runs later will see the characters as if they had appeared in the input.
1097 It is not recommended to do this as part of normal parsing, and most
1098 uses of this facility run the risk of the inserted characters being
1099 interpreted in an unintended manner.
1100
1101 The string to be inserted is represented by octets starting at C<pv>
1102 and continuing to the first nul.  These octets are interpreted as either
1103 UTF-8 or Latin-1, according to whether the C<LEX_STUFF_UTF8> flag is set
1104 in C<flags>.  The characters are recoded for the lexer buffer, according
1105 to how the buffer is currently being interpreted (L</lex_bufutf8>).
1106 If it is not convenient to nul-terminate a string to be inserted, the
1107 L</lex_stuff_pvn> function is more appropriate.
1108
1109 =cut
1110 */
1111
1112 void
1113 Perl_lex_stuff_pv(pTHX_ const char *pv, U32 flags)
1114 {
1115     PERL_ARGS_ASSERT_LEX_STUFF_PV;
1116     lex_stuff_pvn(pv, strlen(pv), flags);
1117 }
1118
1119 /*
1120 =for apidoc Amx|void|lex_stuff_sv|SV *sv|U32 flags
1121
1122 Insert characters into the lexer buffer (L</PL_parser-E<gt>linestr>),
1123 immediately after the current lexing point (L</PL_parser-E<gt>bufptr>),
1124 reallocating the buffer if necessary.  This means that lexing code that
1125 runs later will see the characters as if they had appeared in the input.
1126 It is not recommended to do this as part of normal parsing, and most
1127 uses of this facility run the risk of the inserted characters being
1128 interpreted in an unintended manner.
1129
1130 The string to be inserted is the string value of C<sv>.  The characters
1131 are recoded for the lexer buffer, according to how the buffer is currently
1132 being interpreted (L</lex_bufutf8>).  If a string to be inserted is
1133 not already a Perl scalar, the L</lex_stuff_pvn> function avoids the
1134 need to construct a scalar.
1135
1136 =cut
1137 */
1138
1139 void
1140 Perl_lex_stuff_sv(pTHX_ SV *sv, U32 flags)
1141 {
1142     char *pv;
1143     STRLEN len;
1144     PERL_ARGS_ASSERT_LEX_STUFF_SV;
1145     if (flags)
1146         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_stuff_sv");
1147     pv = SvPV(sv, len);
1148     lex_stuff_pvn(pv, len, flags | (SvUTF8(sv) ? LEX_STUFF_UTF8 : 0));
1149 }
1150
1151 /*
1152 =for apidoc Amx|void|lex_unstuff|char *ptr
1153
1154 Discards text about to be lexed, from L</PL_parser-E<gt>bufptr> up to
1155 C<ptr>.  Text following C<ptr> will be moved, and the buffer shortened.
1156 This hides the discarded text from any lexing code that runs later,
1157 as if the text had never appeared.
1158
1159 This is not the normal way to consume lexed text.  For that, use
1160 L</lex_read_to>.
1161
1162 =cut
1163 */
1164
1165 void
1166 Perl_lex_unstuff(pTHX_ char *ptr)
1167 {
1168     char *buf, *bufend;
1169     STRLEN unstuff_len;
1170     PERL_ARGS_ASSERT_LEX_UNSTUFF;
1171     buf = PL_parser->bufptr;
1172     if (ptr < buf)
1173         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_unstuff");
1174     if (ptr == buf)
1175         return;
1176     bufend = PL_parser->bufend;
1177     if (ptr > bufend)
1178         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_unstuff");
1179     unstuff_len = ptr - buf;
1180     Move(ptr, buf, bufend+1-ptr, char);
1181     SvCUR_set(PL_parser->linestr, SvCUR(PL_parser->linestr) - unstuff_len);
1182     PL_parser->bufend = bufend - unstuff_len;
1183 }
1184
1185 /*
1186 =for apidoc Amx|void|lex_read_to|char *ptr
1187
1188 Consume text in the lexer buffer, from L</PL_parser-E<gt>bufptr> up
1189 to C<ptr>.  This advances L</PL_parser-E<gt>bufptr> to match C<ptr>,
1190 performing the correct bookkeeping whenever a newline character is passed.
1191 This is the normal way to consume lexed text.
1192
1193 Interpretation of the buffer's octets can be abstracted out by
1194 using the slightly higher-level functions L</lex_peek_unichar> and
1195 L</lex_read_unichar>.
1196
1197 =cut
1198 */
1199
1200 void
1201 Perl_lex_read_to(pTHX_ char *ptr)
1202 {
1203     char *s;
1204     PERL_ARGS_ASSERT_LEX_READ_TO;
1205     s = PL_parser->bufptr;
1206     if (ptr < s || ptr > PL_parser->bufend)
1207         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_to");
1208     for (; s != ptr; s++)
1209         if (*s == '\n') {
1210             COPLINE_INC_WITH_HERELINES;
1211             PL_parser->linestart = s+1;
1212         }
1213     PL_parser->bufptr = ptr;
1214 }
1215
1216 /*
1217 =for apidoc Amx|void|lex_discard_to|char *ptr
1218
1219 Discards the first part of the L</PL_parser-E<gt>linestr> buffer,
1220 up to C<ptr>.  The remaining content of the buffer will be moved, and
1221 all pointers into the buffer updated appropriately.  C<ptr> must not
1222 be later in the buffer than the position of L</PL_parser-E<gt>bufptr>:
1223 it is not permitted to discard text that has yet to be lexed.
1224
1225 Normally it is not necessarily to do this directly, because it suffices to
1226 use the implicit discarding behaviour of L</lex_next_chunk> and things
1227 based on it.  However, if a token stretches across multiple lines,
1228 and the lexing code has kept multiple lines of text in the buffer for
1229 that purpose, then after completion of the token it would be wise to
1230 explicitly discard the now-unneeded earlier lines, to avoid future
1231 multi-line tokens growing the buffer without bound.
1232
1233 =cut
1234 */
1235
1236 void
1237 Perl_lex_discard_to(pTHX_ char *ptr)
1238 {
1239     char *buf;
1240     STRLEN discard_len;
1241     PERL_ARGS_ASSERT_LEX_DISCARD_TO;
1242     buf = SvPVX(PL_parser->linestr);
1243     if (ptr < buf)
1244         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_discard_to");
1245     if (ptr == buf)
1246         return;
1247     if (ptr > PL_parser->bufptr)
1248         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_discard_to");
1249     discard_len = ptr - buf;
1250     if (PL_parser->oldbufptr < ptr)
1251         PL_parser->oldbufptr = ptr;
1252     if (PL_parser->oldoldbufptr < ptr)
1253         PL_parser->oldoldbufptr = ptr;
1254     if (PL_parser->last_uni && PL_parser->last_uni < ptr)
1255         PL_parser->last_uni = NULL;
1256     if (PL_parser->last_lop && PL_parser->last_lop < ptr)
1257         PL_parser->last_lop = NULL;
1258     Move(ptr, buf, PL_parser->bufend+1-ptr, char);
1259     SvCUR_set(PL_parser->linestr, SvCUR(PL_parser->linestr) - discard_len);
1260     PL_parser->bufend -= discard_len;
1261     PL_parser->bufptr -= discard_len;
1262     PL_parser->oldbufptr -= discard_len;
1263     PL_parser->oldoldbufptr -= discard_len;
1264     if (PL_parser->last_uni)
1265         PL_parser->last_uni -= discard_len;
1266     if (PL_parser->last_lop)
1267         PL_parser->last_lop -= discard_len;
1268 }
1269
1270 void
1271 Perl_notify_parser_that_changed_to_utf8(pTHX)
1272 {
1273     /* Called when $^H is changed to indicate that HINT_UTF8 has changed from
1274      * off to on.  At compile time, this has the effect of entering a 'use
1275      * utf8' section.  This means that any input was not previously checked for
1276      * UTF-8 (because it was off), but now we do need to check it, or our
1277      * assumptions about the input being sane could be wrong, and we could
1278      * segfault.  This routine just sets a flag so that the next time we look
1279      * at the input we do the well-formed UTF-8 check.  If we aren't in the
1280      * proper phase, there may not be a parser object, but if there is, setting
1281      * the flag is harmless */
1282
1283     if (PL_parser) {
1284         PL_parser->recheck_utf8_validity = TRUE;
1285     }
1286 }
1287
1288 /*
1289 =for apidoc Amx|bool|lex_next_chunk|U32 flags
1290
1291 Reads in the next chunk of text to be lexed, appending it to
1292 L</PL_parser-E<gt>linestr>.  This should be called when lexing code has
1293 looked to the end of the current chunk and wants to know more.  It is
1294 usual, but not necessary, for lexing to have consumed the entirety of
1295 the current chunk at this time.
1296
1297 If L</PL_parser-E<gt>bufptr> is pointing to the very end of the current
1298 chunk (i.e., the current chunk has been entirely consumed), normally the
1299 current chunk will be discarded at the same time that the new chunk is
1300 read in.  If C<flags> has the C<LEX_KEEP_PREVIOUS> bit set, the current chunk
1301 will not be discarded.  If the current chunk has not been entirely
1302 consumed, then it will not be discarded regardless of the flag.
1303
1304 Returns true if some new text was added to the buffer, or false if the
1305 buffer has reached the end of the input text.
1306
1307 =cut
1308 */
1309
1310 #define LEX_FAKE_EOF 0x80000000
1311 #define LEX_NO_TERM  0x40000000 /* here-doc */
1312
1313 bool
1314 Perl_lex_next_chunk(pTHX_ U32 flags)
1315 {
1316     SV *linestr;
1317     char *buf;
1318     STRLEN old_bufend_pos, new_bufend_pos;
1319     STRLEN bufptr_pos, oldbufptr_pos, oldoldbufptr_pos;
1320     STRLEN linestart_pos, last_uni_pos, last_lop_pos;
1321     bool got_some_for_debugger = 0;
1322     bool got_some;
1323
1324     if (flags & ~(LEX_KEEP_PREVIOUS|LEX_FAKE_EOF|LEX_NO_TERM))
1325         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_next_chunk");
1326     if (!(flags & LEX_NO_TERM) && PL_lex_inwhat)
1327         return FALSE;
1328     linestr = PL_parser->linestr;
1329     buf = SvPVX(linestr);
1330     if (!(flags & LEX_KEEP_PREVIOUS)
1331           && PL_parser->bufptr == PL_parser->bufend)
1332     {
1333         old_bufend_pos = bufptr_pos = oldbufptr_pos = oldoldbufptr_pos = 0;
1334         linestart_pos = 0;
1335         if (PL_parser->last_uni != PL_parser->bufend)
1336             PL_parser->last_uni = NULL;
1337         if (PL_parser->last_lop != PL_parser->bufend)
1338             PL_parser->last_lop = NULL;
1339         last_uni_pos = last_lop_pos = 0;
1340         *buf = 0;
1341         SvCUR(linestr) = 0;
1342     } else {
1343         old_bufend_pos = PL_parser->bufend - buf;
1344         bufptr_pos = PL_parser->bufptr - buf;
1345         oldbufptr_pos = PL_parser->oldbufptr - buf;
1346         oldoldbufptr_pos = PL_parser->oldoldbufptr - buf;
1347         linestart_pos = PL_parser->linestart - buf;
1348         last_uni_pos = PL_parser->last_uni ? PL_parser->last_uni - buf : 0;
1349         last_lop_pos = PL_parser->last_lop ? PL_parser->last_lop - buf : 0;
1350     }
1351     if (flags & LEX_FAKE_EOF) {
1352         goto eof;
1353     } else if (!PL_parser->rsfp && !PL_parser->filtered) {
1354         got_some = 0;
1355     } else if (filter_gets(linestr, old_bufend_pos)) {
1356         got_some = 1;
1357         got_some_for_debugger = 1;
1358     } else if (flags & LEX_NO_TERM) {
1359         got_some = 0;
1360     } else {
1361         if (!SvPOK(linestr))   /* can get undefined by filter_gets */
1362             SvPVCLEAR(linestr);
1363         eof:
1364         /* End of real input.  Close filehandle (unless it was STDIN),
1365          * then add implicit termination.
1366          */
1367         if (PL_parser->lex_flags & LEX_DONT_CLOSE_RSFP)
1368             PerlIO_clearerr(PL_parser->rsfp);
1369         else if (PL_parser->rsfp)
1370             (void)PerlIO_close(PL_parser->rsfp);
1371         PL_parser->rsfp = NULL;
1372         PL_parser->in_pod = PL_parser->filtered = 0;
1373         if (!PL_in_eval && PL_minus_p) {
1374             sv_catpvs(linestr,
1375                 /*{*/";}continue{print or die qq(-p destination: $!\\n);}");
1376             PL_minus_n = PL_minus_p = 0;
1377         } else if (!PL_in_eval && PL_minus_n) {
1378             sv_catpvs(linestr, /*{*/";}");
1379             PL_minus_n = 0;
1380         } else
1381             sv_catpvs(linestr, ";");
1382         got_some = 1;
1383     }
1384     buf = SvPVX(linestr);
1385     new_bufend_pos = SvCUR(linestr);
1386     PL_parser->bufend = buf + new_bufend_pos;
1387     PL_parser->bufptr = buf + bufptr_pos;
1388
1389     if (UTF) {
1390         const U8* first_bad_char_loc;
1391         if (UNLIKELY(! is_utf8_string_loc(
1392                             (U8 *) PL_parser->bufptr,
1393                                    PL_parser->bufend - PL_parser->bufptr,
1394                                    &first_bad_char_loc)))
1395         {
1396             _force_out_malformed_utf8_message(first_bad_char_loc,
1397                                               (U8 *) PL_parser->bufend,
1398                                               0,
1399                                               1 /* 1 means die */ );
1400             NOT_REACHED; /* NOTREACHED */
1401         }
1402     }
1403
1404     PL_parser->oldbufptr = buf + oldbufptr_pos;
1405     PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
1406     PL_parser->linestart = buf + linestart_pos;
1407     if (PL_parser->last_uni)
1408         PL_parser->last_uni = buf + last_uni_pos;
1409     if (PL_parser->last_lop)
1410         PL_parser->last_lop = buf + last_lop_pos;
1411     if (PL_parser->preambling != NOLINE) {
1412         CopLINE_set(PL_curcop, PL_parser->preambling + 1);
1413         PL_parser->preambling = NOLINE;
1414     }
1415     if (   got_some_for_debugger
1416         && PERLDB_LINE_OR_SAVESRC
1417         && PL_curstash != PL_debstash)
1418     {
1419         /* debugger active and we're not compiling the debugger code,
1420          * so store the line into the debugger's array of lines
1421          */
1422         update_debugger_info(NULL, buf+old_bufend_pos,
1423             new_bufend_pos-old_bufend_pos);
1424     }
1425     return got_some;
1426 }
1427
1428 /*
1429 =for apidoc Amx|I32|lex_peek_unichar|U32 flags
1430
1431 Looks ahead one (Unicode) character in the text currently being lexed.
1432 Returns the codepoint (unsigned integer value) of the next character,
1433 or -1 if lexing has reached the end of the input text.  To consume the
1434 peeked character, use L</lex_read_unichar>.
1435
1436 If the next character is in (or extends into) the next chunk of input
1437 text, the next chunk will be read in.  Normally the current chunk will be
1438 discarded at the same time, but if C<flags> has the C<LEX_KEEP_PREVIOUS>
1439 bit set, then the current chunk will not be discarded.
1440
1441 If the input is being interpreted as UTF-8 and a UTF-8 encoding error
1442 is encountered, an exception is generated.
1443
1444 =cut
1445 */
1446
1447 I32
1448 Perl_lex_peek_unichar(pTHX_ U32 flags)
1449 {
1450     dVAR;
1451     char *s, *bufend;
1452     if (flags & ~(LEX_KEEP_PREVIOUS))
1453         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_peek_unichar");
1454     s = PL_parser->bufptr;
1455     bufend = PL_parser->bufend;
1456     if (UTF) {
1457         U8 head;
1458         I32 unichar;
1459         STRLEN len, retlen;
1460         if (s == bufend) {
1461             if (!lex_next_chunk(flags))
1462                 return -1;
1463             s = PL_parser->bufptr;
1464             bufend = PL_parser->bufend;
1465         }
1466         head = (U8)*s;
1467         if (UTF8_IS_INVARIANT(head))
1468             return head;
1469         if (UTF8_IS_START(head)) {
1470             len = UTF8SKIP(&head);
1471             while ((STRLEN)(bufend-s) < len) {
1472                 if (!lex_next_chunk(flags | LEX_KEEP_PREVIOUS))
1473                     break;
1474                 s = PL_parser->bufptr;
1475                 bufend = PL_parser->bufend;
1476             }
1477         }
1478         unichar = utf8n_to_uvchr((U8*)s, bufend-s, &retlen, UTF8_CHECK_ONLY);
1479         if (retlen == (STRLEN)-1) {
1480             _force_out_malformed_utf8_message((U8 *) s,
1481                                               (U8 *) bufend,
1482                                               0,
1483                                               1 /* 1 means die */ );
1484             NOT_REACHED; /* NOTREACHED */
1485         }
1486         return unichar;
1487     } else {
1488         if (s == bufend) {
1489             if (!lex_next_chunk(flags))
1490                 return -1;
1491             s = PL_parser->bufptr;
1492         }
1493         return (U8)*s;
1494     }
1495 }
1496
1497 /*
1498 =for apidoc Amx|I32|lex_read_unichar|U32 flags
1499
1500 Reads the next (Unicode) character in the text currently being lexed.
1501 Returns the codepoint (unsigned integer value) of the character read,
1502 and moves L</PL_parser-E<gt>bufptr> past the character, or returns -1
1503 if lexing has reached the end of the input text.  To non-destructively
1504 examine the next character, use L</lex_peek_unichar> instead.
1505
1506 If the next character is in (or extends into) the next chunk of input
1507 text, the next chunk will be read in.  Normally the current chunk will be
1508 discarded at the same time, but if C<flags> has the C<LEX_KEEP_PREVIOUS>
1509 bit set, then the current chunk will not be discarded.
1510
1511 If the input is being interpreted as UTF-8 and a UTF-8 encoding error
1512 is encountered, an exception is generated.
1513
1514 =cut
1515 */
1516
1517 I32
1518 Perl_lex_read_unichar(pTHX_ U32 flags)
1519 {
1520     I32 c;
1521     if (flags & ~(LEX_KEEP_PREVIOUS))
1522         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_unichar");
1523     c = lex_peek_unichar(flags);
1524     if (c != -1) {
1525         if (c == '\n')
1526             COPLINE_INC_WITH_HERELINES;
1527         if (UTF)
1528             PL_parser->bufptr += UTF8SKIP(PL_parser->bufptr);
1529         else
1530             ++(PL_parser->bufptr);
1531     }
1532     return c;
1533 }
1534
1535 /*
1536 =for apidoc Amx|void|lex_read_space|U32 flags
1537
1538 Reads optional spaces, in Perl style, in the text currently being
1539 lexed.  The spaces may include ordinary whitespace characters and
1540 Perl-style comments.  C<#line> directives are processed if encountered.
1541 L</PL_parser-E<gt>bufptr> is moved past the spaces, so that it points
1542 at a non-space character (or the end of the input text).
1543
1544 If spaces extend into the next chunk of input text, the next chunk will
1545 be read in.  Normally the current chunk will be discarded at the same
1546 time, but if C<flags> has the C<LEX_KEEP_PREVIOUS> bit set, then the current
1547 chunk will not be discarded.
1548
1549 =cut
1550 */
1551
1552 #define LEX_NO_INCLINE    0x40000000
1553 #define LEX_NO_NEXT_CHUNK 0x80000000
1554
1555 void
1556 Perl_lex_read_space(pTHX_ U32 flags)
1557 {
1558     char *s, *bufend;
1559     const bool can_incline = !(flags & LEX_NO_INCLINE);
1560     bool need_incline = 0;
1561     if (flags & ~(LEX_KEEP_PREVIOUS|LEX_NO_NEXT_CHUNK|LEX_NO_INCLINE))
1562         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_space");
1563     s = PL_parser->bufptr;
1564     bufend = PL_parser->bufend;
1565     while (1) {
1566         char c = *s;
1567         if (c == '#') {
1568             do {
1569                 c = *++s;
1570             } while (!(c == '\n' || (c == 0 && s == bufend)));
1571         } else if (c == '\n') {
1572             s++;
1573             if (can_incline) {
1574                 PL_parser->linestart = s;
1575                 if (s == bufend)
1576                     need_incline = 1;
1577                 else
1578                     incline(s, bufend);
1579             }
1580         } else if (isSPACE(c)) {
1581             s++;
1582         } else if (c == 0 && s == bufend) {
1583             bool got_more;
1584             line_t l;
1585             if (flags & LEX_NO_NEXT_CHUNK)
1586                 break;
1587             PL_parser->bufptr = s;
1588             l = CopLINE(PL_curcop);
1589             CopLINE(PL_curcop) += PL_parser->herelines + 1;
1590             got_more = lex_next_chunk(flags);
1591             CopLINE_set(PL_curcop, l);
1592             s = PL_parser->bufptr;
1593             bufend = PL_parser->bufend;
1594             if (!got_more)
1595                 break;
1596             if (can_incline && need_incline && PL_parser->rsfp) {
1597                 incline(s, bufend);
1598                 need_incline = 0;
1599             }
1600         } else if (!c) {
1601             s++;
1602         } else {
1603             break;
1604         }
1605     }
1606     PL_parser->bufptr = s;
1607 }
1608
1609 /*
1610
1611 =for apidoc EXMp|bool|validate_proto|SV *name|SV *proto|bool warn
1612
1613 This function performs syntax checking on a prototype, C<proto>.
1614 If C<warn> is true, any illegal characters or mismatched brackets
1615 will trigger illegalproto warnings, declaring that they were
1616 detected in the prototype for C<name>.
1617
1618 The return value is C<true> if this is a valid prototype, and
1619 C<false> if it is not, regardless of whether C<warn> was C<true> or
1620 C<false>.
1621
1622 Note that C<NULL> is a valid C<proto> and will always return C<true>.
1623
1624 =cut
1625
1626  */
1627
1628 bool
1629 Perl_validate_proto(pTHX_ SV *name, SV *proto, bool warn, bool curstash)
1630 {
1631     STRLEN len, origlen;
1632     char *p;
1633     bool bad_proto = FALSE;
1634     bool in_brackets = FALSE;
1635     bool after_slash = FALSE;
1636     char greedy_proto = ' ';
1637     bool proto_after_greedy_proto = FALSE;
1638     bool must_be_last = FALSE;
1639     bool underscore = FALSE;
1640     bool bad_proto_after_underscore = FALSE;
1641
1642     PERL_ARGS_ASSERT_VALIDATE_PROTO;
1643
1644     if (!proto)
1645         return TRUE;
1646
1647     p = SvPV(proto, len);
1648     origlen = len;
1649     for (; len--; p++) {
1650         if (!isSPACE(*p)) {
1651             if (must_be_last)
1652                 proto_after_greedy_proto = TRUE;
1653             if (underscore) {
1654                 if (!strchr(";@%", *p))
1655                     bad_proto_after_underscore = TRUE;
1656                 underscore = FALSE;
1657             }
1658             if (!strchr("$@%*;[]&\\_+", *p) || *p == '\0') {
1659                 bad_proto = TRUE;
1660             }
1661             else {
1662                 if (*p == '[')
1663                     in_brackets = TRUE;
1664                 else if (*p == ']')
1665                     in_brackets = FALSE;
1666                 else if ((*p == '@' || *p == '%')
1667                          && !after_slash
1668                          && !in_brackets )
1669                 {
1670                     must_be_last = TRUE;
1671                     greedy_proto = *p;
1672                 }
1673                 else if (*p == '_')
1674                     underscore = TRUE;
1675             }
1676             if (*p == '\\')
1677                 after_slash = TRUE;
1678             else
1679                 after_slash = FALSE;
1680         }
1681     }
1682
1683     if (warn) {
1684         SV *tmpsv = newSVpvs_flags("", SVs_TEMP);
1685         p -= origlen;
1686         p = SvUTF8(proto)
1687             ? sv_uni_display(tmpsv, newSVpvn_flags(p, origlen, SVs_TEMP | SVf_UTF8),
1688                              origlen, UNI_DISPLAY_ISPRINT)
1689             : pv_pretty(tmpsv, p, origlen, 60, NULL, NULL, PERL_PV_ESCAPE_NONASCII);
1690
1691         if (curstash && !memchr(SvPVX(name), ':', SvCUR(name))) {
1692             SV *name2 = sv_2mortal(newSVsv(PL_curstname));
1693             sv_catpvs(name2, "::");
1694             sv_catsv(name2, (SV *)name);
1695             name = name2;
1696         }
1697
1698         if (proto_after_greedy_proto)
1699             Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1700                         "Prototype after '%c' for %" SVf " : %s",
1701                         greedy_proto, SVfARG(name), p);
1702         if (in_brackets)
1703             Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1704                         "Missing ']' in prototype for %" SVf " : %s",
1705                         SVfARG(name), p);
1706         if (bad_proto)
1707             Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1708                         "Illegal character in prototype for %" SVf " : %s",
1709                         SVfARG(name), p);
1710         if (bad_proto_after_underscore)
1711             Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1712                         "Illegal character after '_' in prototype for %" SVf " : %s",
1713                         SVfARG(name), p);
1714     }
1715
1716     return (! (proto_after_greedy_proto || bad_proto) );
1717 }
1718
1719 /*
1720  * S_incline
1721  * This subroutine has nothing to do with tilting, whether at windmills
1722  * or pinball tables.  Its name is short for "increment line".  It
1723  * increments the current line number in CopLINE(PL_curcop) and checks
1724  * to see whether the line starts with a comment of the form
1725  *    # line 500 "foo.pm"
1726  * If so, it sets the current line number and file to the values in the comment.
1727  */
1728
1729 STATIC void
1730 S_incline(pTHX_ const char *s, const char *end)
1731 {
1732     const char *t;
1733     const char *n;
1734     const char *e;
1735     line_t line_num;
1736     UV uv;
1737
1738     PERL_ARGS_ASSERT_INCLINE;
1739
1740     assert(end >= s);
1741
1742     COPLINE_INC_WITH_HERELINES;
1743     if (!PL_rsfp && !PL_parser->filtered && PL_lex_state == LEX_NORMAL
1744      && s+1 == PL_bufend && *s == ';') {
1745         /* fake newline in string eval */
1746         CopLINE_dec(PL_curcop);
1747         return;
1748     }
1749     if (*s++ != '#')
1750         return;
1751     while (SPACE_OR_TAB(*s))
1752         s++;
1753     if (memBEGINs(s, (STRLEN) (end - s), "line"))
1754         s += sizeof("line") - 1;
1755     else
1756         return;
1757     if (SPACE_OR_TAB(*s))
1758         s++;
1759     else
1760         return;
1761     while (SPACE_OR_TAB(*s))
1762         s++;
1763     if (!isDIGIT(*s))
1764         return;
1765
1766     n = s;
1767     while (isDIGIT(*s))
1768         s++;
1769     if (!SPACE_OR_TAB(*s) && *s != '\r' && *s != '\n' && *s != '\0')
1770         return;
1771     while (SPACE_OR_TAB(*s))
1772         s++;
1773     if (*s == '"' && (t = (char *) memchr(s+1, '"', end - s))) {
1774         s++;
1775         e = t + 1;
1776     }
1777     else {
1778         t = s;
1779         while (*t && !isSPACE(*t))
1780             t++;
1781         e = t;
1782     }
1783     while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
1784         e++;
1785     if (*e != '\n' && *e != '\0')
1786         return;         /* false alarm */
1787
1788     if (!grok_atoUV(n, &uv, &e))
1789         return;
1790     line_num = ((line_t)uv) - 1;
1791
1792     if (t - s > 0) {
1793         const STRLEN len = t - s;
1794
1795         if (!PL_rsfp && !PL_parser->filtered) {
1796             /* must copy *{"::_<(eval N)[oldfilename:L]"}
1797              * to *{"::_<newfilename"} */
1798             /* However, the long form of evals is only turned on by the
1799                debugger - usually they're "(eval %lu)" */
1800             GV * const cfgv = CopFILEGV(PL_curcop);
1801             if (cfgv) {
1802                 char smallbuf[128];
1803                 STRLEN tmplen2 = len;
1804                 char *tmpbuf2;
1805                 GV *gv2;
1806
1807                 if (tmplen2 + 2 <= sizeof smallbuf)
1808                     tmpbuf2 = smallbuf;
1809                 else
1810                     Newx(tmpbuf2, tmplen2 + 2, char);
1811
1812                 tmpbuf2[0] = '_';
1813                 tmpbuf2[1] = '<';
1814
1815                 memcpy(tmpbuf2 + 2, s, tmplen2);
1816                 tmplen2 += 2;
1817
1818                 gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE);
1819                 if (!isGV(gv2)) {
1820                     gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE);
1821                     /* adjust ${"::_<newfilename"} to store the new file name */
1822                     GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2);
1823                     /* The line number may differ. If that is the case,
1824                        alias the saved lines that are in the array.
1825                        Otherwise alias the whole array. */
1826                     if (CopLINE(PL_curcop) == line_num) {
1827                         GvHV(gv2) = MUTABLE_HV(SvREFCNT_inc(GvHV(cfgv)));
1828                         GvAV(gv2) = MUTABLE_AV(SvREFCNT_inc(GvAV(cfgv)));
1829                     }
1830                     else if (GvAV(cfgv)) {
1831                         AV * const av = GvAV(cfgv);
1832                         const line_t start = CopLINE(PL_curcop)+1;
1833                         SSize_t items = AvFILLp(av) - start;
1834                         if (items > 0) {
1835                             AV * const av2 = GvAVn(gv2);
1836                             SV **svp = AvARRAY(av) + start;
1837                             Size_t l = line_num+1;
1838                             while (items-- && l < SSize_t_MAX && l == (line_t)l)
1839                                 av_store(av2, (SSize_t)l++, SvREFCNT_inc(*svp++));
1840                         }
1841                     }
1842                 }
1843
1844                 if (tmpbuf2 != smallbuf) Safefree(tmpbuf2);
1845             }
1846         }
1847         CopFILE_free(PL_curcop);
1848         CopFILE_setn(PL_curcop, s, len);
1849     }
1850     CopLINE_set(PL_curcop, line_num);
1851 }
1852
1853 STATIC void
1854 S_update_debugger_info(pTHX_ SV *orig_sv, const char *const buf, STRLEN len)
1855 {
1856     AV *av = CopFILEAVx(PL_curcop);
1857     if (av) {
1858         SV * sv;
1859         if (PL_parser->preambling == NOLINE) sv = newSV_type(SVt_PVMG);
1860         else {
1861             sv = *av_fetch(av, 0, 1);
1862             SvUPGRADE(sv, SVt_PVMG);
1863         }
1864         if (!SvPOK(sv)) SvPVCLEAR(sv);
1865         if (orig_sv)
1866             sv_catsv(sv, orig_sv);
1867         else
1868             sv_catpvn(sv, buf, len);
1869         if (!SvIOK(sv)) {
1870             (void)SvIOK_on(sv);
1871             SvIV_set(sv, 0);
1872         }
1873         if (PL_parser->preambling == NOLINE)
1874             av_store(av, CopLINE(PL_curcop), sv);
1875     }
1876 }
1877
1878 /*
1879  * skipspace
1880  * Called to gobble the appropriate amount and type of whitespace.
1881  * Skips comments as well.
1882  * Returns the next character after the whitespace that is skipped.
1883  *
1884  * peekspace
1885  * Same thing, but look ahead without incrementing line numbers or
1886  * adjusting PL_linestart.
1887  */
1888
1889 #define skipspace(s) skipspace_flags(s, 0)
1890 #define peekspace(s) skipspace_flags(s, LEX_NO_INCLINE)
1891
1892 STATIC char *
1893 S_skipspace_flags(pTHX_ char *s, U32 flags)
1894 {
1895     PERL_ARGS_ASSERT_SKIPSPACE_FLAGS;
1896     if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
1897         while (s < PL_bufend && (SPACE_OR_TAB(*s) || !*s))
1898             s++;
1899     } else {
1900         STRLEN bufptr_pos = PL_bufptr - SvPVX(PL_linestr);
1901         PL_bufptr = s;
1902         lex_read_space(flags | LEX_KEEP_PREVIOUS |
1903                 (PL_lex_inwhat || PL_lex_state == LEX_FORMLINE ?
1904                     LEX_NO_NEXT_CHUNK : 0));
1905         s = PL_bufptr;
1906         PL_bufptr = SvPVX(PL_linestr) + bufptr_pos;
1907         if (PL_linestart > PL_bufptr)
1908             PL_bufptr = PL_linestart;
1909         return s;
1910     }
1911     return s;
1912 }
1913
1914 /*
1915  * S_check_uni
1916  * Check the unary operators to ensure there's no ambiguity in how they're
1917  * used.  An ambiguous piece of code would be:
1918  *     rand + 5
1919  * This doesn't mean rand() + 5.  Because rand() is a unary operator,
1920  * the +5 is its argument.
1921  */
1922
1923 STATIC void
1924 S_check_uni(pTHX)
1925 {
1926     const char *s;
1927
1928     if (PL_oldoldbufptr != PL_last_uni)
1929         return;
1930     while (isSPACE(*PL_last_uni))
1931         PL_last_uni++;
1932     s = PL_last_uni;
1933     while (isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF) || *s == '-')
1934         s += UTF ? UTF8SKIP(s) : 1;
1935     if (s < PL_bufptr && memchr(s, '(', PL_bufptr - s))
1936         return;
1937
1938     Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
1939                      "Warning: Use of \"%" UTF8f "\" without parentheses is ambiguous",
1940                      UTF8fARG(UTF, (int)(s - PL_last_uni), PL_last_uni));
1941 }
1942
1943 /*
1944  * LOP : macro to build a list operator.  Its behaviour has been replaced
1945  * with a subroutine, S_lop() for which LOP is just another name.
1946  */
1947
1948 #define LOP(f,x) return lop(f,x,s)
1949
1950 /*
1951  * S_lop
1952  * Build a list operator (or something that might be one).  The rules:
1953  *  - if we have a next token, then it's a list operator (no parens) for
1954  *    which the next token has already been parsed; e.g.,
1955  *       sort foo @args
1956  *       sort foo (@args)
1957  *  - if the next thing is an opening paren, then it's a function
1958  *  - else it's a list operator
1959  */
1960
1961 STATIC I32
1962 S_lop(pTHX_ I32 f, U8 x, char *s)
1963 {
1964     PERL_ARGS_ASSERT_LOP;
1965
1966     pl_yylval.ival = f;
1967     CLINE;
1968     PL_bufptr = s;
1969     PL_last_lop = PL_oldbufptr;
1970     PL_last_lop_op = (OPCODE)f;
1971     if (PL_nexttoke)
1972         goto lstop;
1973     PL_expect = x;
1974     if (*s == '(')
1975         return REPORT(FUNC);
1976     s = skipspace(s);
1977     if (*s == '(')
1978         return REPORT(FUNC);
1979     else {
1980         lstop:
1981         if (!PL_lex_allbrackets && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
1982             PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
1983         return REPORT(LSTOP);
1984     }
1985 }
1986
1987 /*
1988  * S_force_next
1989  * When the lexer realizes it knows the next token (for instance,
1990  * it is reordering tokens for the parser) then it can call S_force_next
1991  * to know what token to return the next time the lexer is called.  Caller
1992  * will need to set PL_nextval[] and possibly PL_expect to ensure
1993  * the lexer handles the token correctly.
1994  */
1995
1996 STATIC void
1997 S_force_next(pTHX_ I32 type)
1998 {
1999 #ifdef DEBUGGING
2000     if (DEBUG_T_TEST) {
2001         PerlIO_printf(Perl_debug_log, "### forced token:\n");
2002         tokereport(type, &NEXTVAL_NEXTTOKE);
2003     }
2004 #endif
2005     assert(PL_nexttoke < C_ARRAY_LENGTH(PL_nexttype));
2006     PL_nexttype[PL_nexttoke] = type;
2007     PL_nexttoke++;
2008 }
2009
2010 /*
2011  * S_postderef
2012  *
2013  * This subroutine handles postfix deref syntax after the arrow has already
2014  * been emitted.  @* $* etc. are emitted as two separate tokens right here.
2015  * @[ @{ %[ %{ *{ are emitted also as two tokens, but this function emits
2016  * only the first, leaving yylex to find the next.
2017  */
2018
2019 static int
2020 S_postderef(pTHX_ int const funny, char const next)
2021 {
2022     assert(funny == DOLSHARP || strchr("$@%&*", funny));
2023     if (next == '*') {
2024         PL_expect = XOPERATOR;
2025         if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) {
2026             assert('@' == funny || '$' == funny || DOLSHARP == funny);
2027             PL_lex_state = LEX_INTERPEND;
2028             if ('@' == funny)
2029                 force_next(POSTJOIN);
2030         }
2031         force_next(next);
2032         PL_bufptr+=2;
2033     }
2034     else {
2035         if ('@' == funny && PL_lex_state == LEX_INTERPNORMAL
2036          && !PL_lex_brackets)
2037             PL_lex_dojoin = 2;
2038         PL_expect = XOPERATOR;
2039         PL_bufptr++;
2040     }
2041     return funny;
2042 }
2043
2044 void
2045 Perl_yyunlex(pTHX)
2046 {
2047     int yyc = PL_parser->yychar;
2048     if (yyc != YYEMPTY) {
2049         if (yyc) {
2050             NEXTVAL_NEXTTOKE = PL_parser->yylval;
2051             if (yyc == '{'/*}*/ || yyc == HASHBRACK || yyc == '['/*]*/) {
2052                 PL_lex_allbrackets--;
2053                 PL_lex_brackets--;
2054                 yyc |= (3<<24) | (PL_lex_brackstack[PL_lex_brackets] << 16);
2055             } else if (yyc == '('/*)*/) {
2056                 PL_lex_allbrackets--;
2057                 yyc |= (2<<24);
2058             }
2059             force_next(yyc);
2060         }
2061         PL_parser->yychar = YYEMPTY;
2062     }
2063 }
2064
2065 STATIC SV *
2066 S_newSV_maybe_utf8(pTHX_ const char *const start, STRLEN len)
2067 {
2068     SV * const sv = newSVpvn_utf8(start, len,
2069                     ! IN_BYTES
2070                   &&  UTF
2071                   &&  is_utf8_non_invariant_string((const U8*)start, len));
2072     return sv;
2073 }
2074
2075 /*
2076  * S_force_word
2077  * When the lexer knows the next thing is a word (for instance, it has
2078  * just seen -> and it knows that the next char is a word char, then
2079  * it calls S_force_word to stick the next word into the PL_nexttoke/val
2080  * lookahead.
2081  *
2082  * Arguments:
2083  *   char *start : buffer position (must be within PL_linestr)
2084  *   int token   : PL_next* will be this type of bare word
2085  *                 (e.g., METHOD,BAREWORD)
2086  *   int check_keyword : if true, Perl checks to make sure the word isn't
2087  *       a keyword (do this if the word is a label, e.g. goto FOO)
2088  *   int allow_pack : if true, : characters will also be allowed (require,
2089  *       use, etc. do this)
2090  */
2091
2092 STATIC char *
2093 S_force_word(pTHX_ char *start, int token, int check_keyword, int allow_pack)
2094 {
2095     char *s;
2096     STRLEN len;
2097
2098     PERL_ARGS_ASSERT_FORCE_WORD;
2099
2100     start = skipspace(start);
2101     s = start;
2102     if (   isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)
2103         || (allow_pack && *s == ':' && s[1] == ':') )
2104     {
2105         s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, allow_pack, &len);
2106         if (check_keyword) {
2107           char *s2 = PL_tokenbuf;
2108           STRLEN len2 = len;
2109           if (allow_pack && memBEGINPs(s2, len, "CORE::")) {
2110             s2 += sizeof("CORE::") - 1;
2111             len2 -= sizeof("CORE::") - 1;
2112           }
2113           if (keyword(s2, len2, 0))
2114             return start;
2115         }
2116         if (token == METHOD) {
2117             s = skipspace(s);
2118             if (*s == '(')
2119                 PL_expect = XTERM;
2120             else {
2121                 PL_expect = XOPERATOR;
2122             }
2123         }
2124         NEXTVAL_NEXTTOKE.opval
2125             = newSVOP(OP_CONST,0,
2126                            S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
2127         NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
2128         force_next(token);
2129     }
2130     return s;
2131 }
2132
2133 /*
2134  * S_force_ident
2135  * Called when the lexer wants $foo *foo &foo etc, but the program
2136  * text only contains the "foo" portion.  The first argument is a pointer
2137  * to the "foo", and the second argument is the type symbol to prefix.
2138  * Forces the next token to be a "BAREWORD".
2139  * Creates the symbol if it didn't already exist (via gv_fetchpv()).
2140  */
2141
2142 STATIC void
2143 S_force_ident(pTHX_ const char *s, int kind)
2144 {
2145     PERL_ARGS_ASSERT_FORCE_IDENT;
2146
2147     if (s[0]) {
2148         const STRLEN len = s[1] ? strlen(s) : 1; /* s = "\"" see yylex */
2149         OP* const o = newSVOP(OP_CONST, 0, newSVpvn_flags(s, len,
2150                                                                 UTF ? SVf_UTF8 : 0));
2151         NEXTVAL_NEXTTOKE.opval = o;
2152         force_next(BAREWORD);
2153         if (kind) {
2154             o->op_private = OPpCONST_ENTERED;
2155             /* XXX see note in pp_entereval() for why we forgo typo
2156                warnings if the symbol must be introduced in an eval.
2157                GSAR 96-10-12 */
2158             gv_fetchpvn_flags(s, len,
2159                               (PL_in_eval ? GV_ADDMULTI
2160                               : GV_ADD) | ( UTF ? SVf_UTF8 : 0 ),
2161                               kind == '$' ? SVt_PV :
2162                               kind == '@' ? SVt_PVAV :
2163                               kind == '%' ? SVt_PVHV :
2164                               SVt_PVGV
2165                               );
2166         }
2167     }
2168 }
2169
2170 static void
2171 S_force_ident_maybe_lex(pTHX_ char pit)
2172 {
2173     NEXTVAL_NEXTTOKE.ival = pit;
2174     force_next('p');
2175 }
2176
2177 NV
2178 Perl_str_to_version(pTHX_ SV *sv)
2179 {
2180     NV retval = 0.0;
2181     NV nshift = 1.0;
2182     STRLEN len;
2183     const char *start = SvPV_const(sv,len);
2184     const char * const end = start + len;
2185     const bool utf = cBOOL(SvUTF8(sv));
2186
2187     PERL_ARGS_ASSERT_STR_TO_VERSION;
2188
2189     while (start < end) {
2190         STRLEN skip;
2191         UV n;
2192         if (utf)
2193             n = utf8n_to_uvchr((U8*)start, len, &skip, 0);
2194         else {
2195             n = *(U8*)start;
2196             skip = 1;
2197         }
2198         retval += ((NV)n)/nshift;
2199         start += skip;
2200         nshift *= 1000;
2201     }
2202     return retval;
2203 }
2204
2205 /*
2206  * S_force_version
2207  * Forces the next token to be a version number.
2208  * If the next token appears to be an invalid version number, (e.g. "v2b"),
2209  * and if "guessing" is TRUE, then no new token is created (and the caller
2210  * must use an alternative parsing method).
2211  */
2212
2213 STATIC char *
2214 S_force_version(pTHX_ char *s, int guessing)
2215 {
2216     OP *version = NULL;
2217     char *d;
2218
2219     PERL_ARGS_ASSERT_FORCE_VERSION;
2220
2221     s = skipspace(s);
2222
2223     d = s;
2224     if (*d == 'v')
2225         d++;
2226     if (isDIGIT(*d)) {
2227         while (isDIGIT(*d) || *d == '_' || *d == '.')
2228             d++;
2229         if (*d == ';' || isSPACE(*d) || *d == '{' || *d == '}' || !*d) {
2230             SV *ver;
2231             s = scan_num(s, &pl_yylval);
2232             version = pl_yylval.opval;
2233             ver = cSVOPx(version)->op_sv;
2234             if (SvPOK(ver) && !SvNIOK(ver)) {
2235                 SvUPGRADE(ver, SVt_PVNV);
2236                 SvNV_set(ver, str_to_version(ver));
2237                 SvNOK_on(ver);          /* hint that it is a version */
2238             }
2239         }
2240         else if (guessing) {
2241             return s;
2242         }
2243     }
2244
2245     /* NOTE: The parser sees the package name and the VERSION swapped */
2246     NEXTVAL_NEXTTOKE.opval = version;
2247     force_next(BAREWORD);
2248
2249     return s;
2250 }
2251
2252 /*
2253  * S_force_strict_version
2254  * Forces the next token to be a version number using strict syntax rules.
2255  */
2256
2257 STATIC char *
2258 S_force_strict_version(pTHX_ char *s)
2259 {
2260     OP *version = NULL;
2261     const char *errstr = NULL;
2262
2263     PERL_ARGS_ASSERT_FORCE_STRICT_VERSION;
2264
2265     while (isSPACE(*s)) /* leading whitespace */
2266         s++;
2267
2268     if (is_STRICT_VERSION(s,&errstr)) {
2269         SV *ver = newSV(0);
2270         s = (char *)scan_version(s, ver, 0);
2271         version = newSVOP(OP_CONST, 0, ver);
2272     }
2273     else if ((*s != ';' && *s != '{' && *s != '}' )
2274              && (s = skipspace(s), (*s != ';' && *s != '{' && *s != '}' )))
2275     {
2276         PL_bufptr = s;
2277         if (errstr)
2278             yyerror(errstr); /* version required */
2279         return s;
2280     }
2281
2282     /* NOTE: The parser sees the package name and the VERSION swapped */
2283     NEXTVAL_NEXTTOKE.opval = version;
2284     force_next(BAREWORD);
2285
2286     return s;
2287 }
2288
2289 /*
2290  * S_tokeq
2291  * Turns any \\ into \ in a quoted string passed in in 'sv', returning 'sv',
2292  * modified as necessary.  However, if HINT_NEW_STRING is on, 'sv' is
2293  * unchanged, and a new SV containing the modified input is returned.
2294  */
2295
2296 STATIC SV *
2297 S_tokeq(pTHX_ SV *sv)
2298 {
2299     char *s;
2300     char *send;
2301     char *d;
2302     SV *pv = sv;
2303
2304     PERL_ARGS_ASSERT_TOKEQ;
2305
2306     assert (SvPOK(sv));
2307     assert (SvLEN(sv));
2308     assert (!SvIsCOW(sv));
2309     if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1) /* <<'heredoc' */
2310         goto finish;
2311     s = SvPVX(sv);
2312     send = SvEND(sv);
2313     /* This is relying on the SV being "well formed" with a trailing '\0'  */
2314     while (s < send && !(*s == '\\' && s[1] == '\\'))
2315         s++;
2316     if (s == send)
2317         goto finish;
2318     d = s;
2319     if ( PL_hints & HINT_NEW_STRING ) {
2320         pv = newSVpvn_flags(SvPVX_const(pv), SvCUR(sv),
2321                             SVs_TEMP | SvUTF8(sv));
2322     }
2323     while (s < send) {
2324         if (*s == '\\') {
2325             if (s + 1 < send && (s[1] == '\\'))
2326                 s++;            /* all that, just for this */
2327         }
2328         *d++ = *s++;
2329     }
2330     *d = '\0';
2331     SvCUR_set(sv, d - SvPVX_const(sv));
2332   finish:
2333     if ( PL_hints & HINT_NEW_STRING )
2334        return new_constant(NULL, 0, "q", sv, pv, "q", 1);
2335     return sv;
2336 }
2337
2338 /*
2339  * Now come three functions related to double-quote context,
2340  * S_sublex_start, S_sublex_push, and S_sublex_done.  They're used when
2341  * converting things like "\u\Lgnat" into ucfirst(lc("gnat")).  They
2342  * interact with PL_lex_state, and create fake ( ... ) argument lists
2343  * to handle functions and concatenation.
2344  * For example,
2345  *   "foo\lbar"
2346  * is tokenised as
2347  *    stringify ( const[foo] concat lcfirst ( const[bar] ) )
2348  */
2349
2350 /*
2351  * S_sublex_start
2352  * Assumes that pl_yylval.ival is the op we're creating (e.g. OP_LCFIRST).
2353  *
2354  * Pattern matching will set PL_lex_op to the pattern-matching op to
2355  * make (we return THING if pl_yylval.ival is OP_NULL, PMFUNC otherwise).
2356  *
2357  * OP_CONST is easy--just make the new op and return.
2358  *
2359  * Everything else becomes a FUNC.
2360  *
2361  * Sets PL_lex_state to LEX_INTERPPUSH unless ival was OP_NULL or we
2362  * had an OP_CONST.  This just sets us up for a
2363  * call to S_sublex_push().
2364  */
2365
2366 STATIC I32
2367 S_sublex_start(pTHX)
2368 {
2369     const I32 op_type = pl_yylval.ival;
2370
2371     if (op_type == OP_NULL) {
2372         pl_yylval.opval = PL_lex_op;
2373         PL_lex_op = NULL;
2374         return THING;
2375     }
2376     if (op_type == OP_CONST) {
2377         SV *sv = PL_lex_stuff;
2378         PL_lex_stuff = NULL;
2379         sv = tokeq(sv);
2380
2381         if (SvTYPE(sv) == SVt_PVIV) {
2382             /* Overloaded constants, nothing fancy: Convert to SVt_PV: */
2383             STRLEN len;
2384             const char * const p = SvPV_const(sv, len);
2385             SV * const nsv = newSVpvn_flags(p, len, SvUTF8(sv));
2386             SvREFCNT_dec(sv);
2387             sv = nsv;
2388         }
2389         pl_yylval.opval = newSVOP(op_type, 0, sv);
2390         return THING;
2391     }
2392
2393     PL_parser->lex_super_state = PL_lex_state;
2394     PL_parser->lex_sub_inwhat = (U16)op_type;
2395     PL_parser->lex_sub_op = PL_lex_op;
2396     PL_parser->sub_no_recover = FALSE;
2397     PL_parser->sub_error_count = PL_error_count;
2398     PL_lex_state = LEX_INTERPPUSH;
2399
2400     PL_expect = XTERM;
2401     if (PL_lex_op) {
2402         pl_yylval.opval = PL_lex_op;
2403         PL_lex_op = NULL;
2404         return PMFUNC;
2405     }
2406     else
2407         return FUNC;
2408 }
2409
2410 /*
2411  * S_sublex_push
2412  * Create a new scope to save the lexing state.  The scope will be
2413  * ended in S_sublex_done.  Returns a '(', starting the function arguments
2414  * to the uc, lc, etc. found before.
2415  * Sets PL_lex_state to LEX_INTERPCONCAT.
2416  */
2417
2418 STATIC I32
2419 S_sublex_push(pTHX)
2420 {
2421     LEXSHARED *shared;
2422     const bool is_heredoc = PL_multi_close == '<';
2423     ENTER;
2424
2425     PL_lex_state = PL_parser->lex_super_state;
2426     SAVEI8(PL_lex_dojoin);
2427     SAVEI32(PL_lex_brackets);
2428     SAVEI32(PL_lex_allbrackets);
2429     SAVEI32(PL_lex_formbrack);
2430     SAVEI8(PL_lex_fakeeof);
2431     SAVEI32(PL_lex_casemods);
2432     SAVEI32(PL_lex_starts);
2433     SAVEI8(PL_lex_state);
2434     SAVESPTR(PL_lex_repl);
2435     SAVEVPTR(PL_lex_inpat);
2436     SAVEI16(PL_lex_inwhat);
2437     if (is_heredoc)
2438     {
2439         SAVECOPLINE(PL_curcop);
2440         SAVEI32(PL_multi_end);
2441         SAVEI32(PL_parser->herelines);
2442         PL_parser->herelines = 0;
2443     }
2444     SAVEIV(PL_multi_close);
2445     SAVEPPTR(PL_bufptr);
2446     SAVEPPTR(PL_bufend);
2447     SAVEPPTR(PL_oldbufptr);
2448     SAVEPPTR(PL_oldoldbufptr);
2449     SAVEPPTR(PL_last_lop);
2450     SAVEPPTR(PL_last_uni);
2451     SAVEPPTR(PL_linestart);
2452     SAVESPTR(PL_linestr);
2453     SAVEGENERICPV(PL_lex_brackstack);
2454     SAVEGENERICPV(PL_lex_casestack);
2455     SAVEGENERICPV(PL_parser->lex_shared);
2456     SAVEBOOL(PL_parser->lex_re_reparsing);
2457     SAVEI32(PL_copline);
2458
2459     /* The here-doc parser needs to be able to peek into outer lexing
2460        scopes to find the body of the here-doc.  So we put PL_linestr and
2461        PL_bufptr into lex_shared, to â€˜share’ those values.
2462      */
2463     PL_parser->lex_shared->ls_linestr = PL_linestr;
2464     PL_parser->lex_shared->ls_bufptr  = PL_bufptr;
2465
2466     PL_linestr = PL_lex_stuff;
2467     PL_lex_repl = PL_parser->lex_sub_repl;
2468     PL_lex_stuff = NULL;
2469     PL_parser->lex_sub_repl = NULL;
2470
2471     /* Arrange for PL_lex_stuff to be freed on scope exit, in case it gets
2472        set for an inner quote-like operator and then an error causes scope-
2473        popping.  We must not have a PL_lex_stuff value left dangling, as
2474        that breaks assumptions elsewhere.  See bug #123617.  */
2475     SAVEGENERICSV(PL_lex_stuff);
2476     SAVEGENERICSV(PL_parser->lex_sub_repl);
2477
2478     PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart
2479         = SvPVX(PL_linestr);
2480     PL_bufend += SvCUR(PL_linestr);
2481     PL_last_lop = PL_last_uni = NULL;
2482     SAVEFREESV(PL_linestr);
2483     if (PL_lex_repl) SAVEFREESV(PL_lex_repl);
2484
2485     PL_lex_dojoin = FALSE;
2486     PL_lex_brackets = PL_lex_formbrack = 0;
2487     PL_lex_allbrackets = 0;
2488     PL_lex_fakeeof = LEX_FAKEEOF_NEVER;
2489     Newx(PL_lex_brackstack, 120, char);
2490     Newx(PL_lex_casestack, 12, char);
2491     PL_lex_casemods = 0;
2492     *PL_lex_casestack = '\0';
2493     PL_lex_starts = 0;
2494     PL_lex_state = LEX_INTERPCONCAT;
2495     if (is_heredoc)
2496         CopLINE_set(PL_curcop, (line_t)PL_multi_start);
2497     PL_copline = NOLINE;
2498
2499     Newxz(shared, 1, LEXSHARED);
2500     shared->ls_prev = PL_parser->lex_shared;
2501     PL_parser->lex_shared = shared;
2502
2503     PL_lex_inwhat = PL_parser->lex_sub_inwhat;
2504     if (PL_lex_inwhat == OP_TRANSR) PL_lex_inwhat = OP_TRANS;
2505     if (PL_lex_inwhat == OP_MATCH || PL_lex_inwhat == OP_QR || PL_lex_inwhat == OP_SUBST)
2506         PL_lex_inpat = PL_parser->lex_sub_op;
2507     else
2508         PL_lex_inpat = NULL;
2509
2510     PL_parser->lex_re_reparsing = cBOOL(PL_in_eval & EVAL_RE_REPARSING);
2511     PL_in_eval &= ~EVAL_RE_REPARSING;
2512
2513     return '(';
2514 }
2515
2516 /*
2517  * S_sublex_done
2518  * Restores lexer state after a S_sublex_push.
2519  */
2520
2521 STATIC I32
2522 S_sublex_done(pTHX)
2523 {
2524     if (!PL_lex_starts++) {
2525         SV * const sv = newSVpvs("");
2526         if (SvUTF8(PL_linestr))
2527             SvUTF8_on(sv);
2528         PL_expect = XOPERATOR;
2529         pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
2530         return THING;
2531     }
2532
2533     if (PL_lex_casemods) {              /* oops, we've got some unbalanced parens */
2534         PL_lex_state = LEX_INTERPCASEMOD;
2535         return yylex();
2536     }
2537
2538     /* Is there a right-hand side to take care of? (s//RHS/ or tr//RHS/) */
2539     assert(PL_lex_inwhat != OP_TRANSR);
2540     if (PL_lex_repl) {
2541         assert (PL_lex_inwhat == OP_SUBST || PL_lex_inwhat == OP_TRANS);
2542         PL_linestr = PL_lex_repl;
2543         PL_lex_inpat = 0;
2544         PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
2545         PL_bufend += SvCUR(PL_linestr);
2546         PL_last_lop = PL_last_uni = NULL;
2547         PL_lex_dojoin = FALSE;
2548         PL_lex_brackets = 0;
2549         PL_lex_allbrackets = 0;
2550         PL_lex_fakeeof = LEX_FAKEEOF_NEVER;
2551         PL_lex_casemods = 0;
2552         *PL_lex_casestack = '\0';
2553         PL_lex_starts = 0;
2554         if (SvEVALED(PL_lex_repl)) {
2555             PL_lex_state = LEX_INTERPNORMAL;
2556             PL_lex_starts++;
2557             /*  we don't clear PL_lex_repl here, so that we can check later
2558                 whether this is an evalled subst; that means we rely on the
2559                 logic to ensure sublex_done() is called again only via the
2560                 branch (in yylex()) that clears PL_lex_repl, else we'll loop */
2561         }
2562         else {
2563             PL_lex_state = LEX_INTERPCONCAT;
2564             PL_lex_repl = NULL;
2565         }
2566         if (SvTYPE(PL_linestr) >= SVt_PVNV) {
2567             CopLINE(PL_curcop) +=
2568                 ((XPVNV*)SvANY(PL_linestr))->xnv_u.xnv_lines
2569                  + PL_parser->herelines;
2570             PL_parser->herelines = 0;
2571         }
2572         return '/';
2573     }
2574     else {
2575         const line_t l = CopLINE(PL_curcop);
2576         LEAVE;
2577         if (PL_parser->sub_error_count != PL_error_count) {
2578             if (PL_parser->sub_no_recover) {
2579                 yyquit();
2580                 NOT_REACHED;
2581             }
2582         }
2583         if (PL_multi_close == '<')
2584             PL_parser->herelines += l - PL_multi_end;
2585         PL_bufend = SvPVX(PL_linestr);
2586         PL_bufend += SvCUR(PL_linestr);
2587         PL_expect = XOPERATOR;
2588         return ')';
2589     }
2590 }
2591
2592 STATIC SV*
2593 S_get_and_check_backslash_N_name(pTHX_ const char* s, const char* const e)
2594 {
2595     /* <s> points to first character of interior of \N{}, <e> to one beyond the
2596      * interior, hence to the "}".  Finds what the name resolves to, returning
2597      * an SV* containing it; NULL if no valid one found */
2598
2599     dVAR;
2600     SV* res = newSVpvn_flags(s, e - s, UTF ? SVf_UTF8 : 0);
2601
2602     HV * table;
2603     SV **cvp;
2604     SV *cv;
2605     SV *rv;
2606     HV *stash;
2607     const char* backslash_ptr = s - 3; /* Points to the <\> of \N{... */
2608
2609     PERL_ARGS_ASSERT_GET_AND_CHECK_BACKSLASH_N_NAME;
2610
2611     if (!SvCUR(res)) {
2612         SvREFCNT_dec_NN(res);
2613         /* diag_listed_as: Unknown charname '%s' */
2614         yyerror("Unknown charname ''");
2615         return NULL;
2616     }
2617
2618     res = new_constant( NULL, 0, "charnames", res, NULL, backslash_ptr,
2619                         /* include the <}> */
2620                         e - backslash_ptr + 1);
2621     if (! SvPOK(res)) {
2622         SvREFCNT_dec_NN(res);
2623         return NULL;
2624     }
2625
2626     /* See if the charnames handler is the Perl core's, and if so, we can skip
2627      * the validation needed for a user-supplied one, as Perl's does its own
2628      * validation. */
2629     table = GvHV(PL_hintgv);             /* ^H */
2630     cvp = hv_fetchs(table, "charnames", FALSE);
2631     if (cvp && (cv = *cvp) && SvROK(cv) && (rv = SvRV(cv),
2632         SvTYPE(rv) == SVt_PVCV) && ((stash = CvSTASH(rv)) != NULL))
2633     {
2634         const char * const name = HvNAME(stash);
2635          if (memEQs(name, HvNAMELEN(stash), "_charnames")) {
2636            return res;
2637        }
2638     }
2639
2640     /* Here, it isn't Perl's charname handler.  We can't rely on a
2641      * user-supplied handler to validate the input name.  For non-ut8 input,
2642      * look to see that the first character is legal.  Then loop through the
2643      * rest checking that each is a continuation */
2644
2645     /* This code makes the reasonable assumption that the only Latin1-range
2646      * characters that begin a character name alias are alphabetic, otherwise
2647      * would have to create a isCHARNAME_BEGIN macro */
2648
2649     if (! UTF) {
2650         if (! isALPHAU(*s)) {
2651             goto bad_charname;
2652         }
2653         s++;
2654         while (s < e) {
2655             if (! isCHARNAME_CONT(*s)) {
2656                 goto bad_charname;
2657             }
2658             if (*s == ' ' && *(s-1) == ' ') {
2659                 goto multi_spaces;
2660             }
2661             s++;
2662         }
2663     }
2664     else {
2665         /* Similarly for utf8.  For invariants can check directly; for other
2666          * Latin1, can calculate their code point and check; otherwise  use a
2667          * swash */
2668         if (UTF8_IS_INVARIANT(*s)) {
2669             if (! isALPHAU(*s)) {
2670                 goto bad_charname;
2671             }
2672             s++;
2673         } else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
2674             if (! isALPHAU(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s+1)))) {
2675                 goto bad_charname;
2676             }
2677             s += 2;
2678         }
2679         else {
2680             if (! _invlist_contains_cp(PL_utf8_charname_begin,
2681                                        utf8_to_uvchr_buf((U8 *) s,
2682                                                          (U8 *) e,
2683                                                          NULL)))
2684             {
2685                 goto bad_charname;
2686             }
2687             s += UTF8SKIP(s);
2688         }
2689
2690         while (s < e) {
2691             if (UTF8_IS_INVARIANT(*s)) {
2692                 if (! isCHARNAME_CONT(*s)) {
2693                     goto bad_charname;
2694                 }
2695                 if (*s == ' ' && *(s-1) == ' ') {
2696                     goto multi_spaces;
2697                 }
2698                 s++;
2699             }
2700             else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
2701                 if (! isCHARNAME_CONT(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s+1))))
2702                 {
2703                     goto bad_charname;
2704                 }
2705                 s += 2;
2706             }
2707             else {
2708                 if (! _invlist_contains_cp(PL_utf8_charname_continue,
2709                                            utf8_to_uvchr_buf((U8 *) s,
2710                                                              (U8 *) e,
2711                                                              NULL)))
2712                 {
2713                     goto bad_charname;
2714                 }
2715                 s += UTF8SKIP(s);
2716             }
2717         }
2718     }
2719     if (*(s-1) == ' ') {
2720         /* diag_listed_as: charnames alias definitions may not contain
2721                            trailing white-space; marked by <-- HERE in %s
2722          */
2723         yyerror_pv(
2724             Perl_form(aTHX_
2725             "charnames alias definitions may not contain trailing "
2726             "white-space; marked by <-- HERE in %.*s<-- HERE %.*s",
2727             (int)(s - backslash_ptr + 1), backslash_ptr,
2728             (int)(e - s + 1), s + 1
2729             ),
2730         UTF ? SVf_UTF8 : 0);
2731         return NULL;
2732     }
2733
2734     if (SvUTF8(res)) { /* Don't accept malformed input */
2735         const U8* first_bad_char_loc;
2736         STRLEN len;
2737         const char* const str = SvPV_const(res, len);
2738         if (UNLIKELY(! is_utf8_string_loc((U8 *) str, len,
2739                                           &first_bad_char_loc)))
2740         {
2741             _force_out_malformed_utf8_message(first_bad_char_loc,
2742                                               (U8 *) PL_parser->bufend,
2743                                               0,
2744                                               0 /* 0 means don't die */ );
2745             /* diag_listed_as: Malformed UTF-8 returned by \N{%s}
2746                                immediately after '%s' */
2747             yyerror_pv(
2748               Perl_form(aTHX_
2749                 "Malformed UTF-8 returned by %.*s immediately after '%.*s'",
2750                  (int) (e - backslash_ptr + 1), backslash_ptr,
2751                  (int) ((char *) first_bad_char_loc - str), str
2752               ),
2753               SVf_UTF8);
2754             return NULL;
2755         }
2756     }
2757
2758     return res;
2759
2760   bad_charname: {
2761
2762         /* The final %.*s makes sure that should the trailing NUL be missing
2763          * that this print won't run off the end of the string */
2764         /* diag_listed_as: Invalid character in \N{...}; marked by <-- HERE
2765                            in \N{%s} */
2766         yyerror_pv(
2767           Perl_form(aTHX_
2768             "Invalid character in \\N{...}; marked by <-- HERE in %.*s<-- HERE %.*s",
2769             (int)(s - backslash_ptr + 1), backslash_ptr,
2770             (int)(e - s + 1), s + 1
2771           ),
2772           UTF ? SVf_UTF8 : 0);
2773         return NULL;
2774     }
2775
2776   multi_spaces:
2777         /* diag_listed_as: charnames alias definitions may not contain a
2778                            sequence of multiple spaces; marked by <-- HERE
2779                            in %s */
2780         yyerror_pv(
2781           Perl_form(aTHX_
2782             "charnames alias definitions may not contain a sequence of "
2783             "multiple spaces; marked by <-- HERE in %.*s<-- HERE %.*s",
2784             (int)(s - backslash_ptr + 1), backslash_ptr,
2785             (int)(e - s + 1), s + 1
2786           ),
2787           UTF ? SVf_UTF8 : 0);
2788         return NULL;
2789 }
2790
2791 /*
2792   scan_const
2793
2794   Extracts the next constant part of a pattern, double-quoted string,
2795   or transliteration.  This is terrifying code.
2796
2797   For example, in parsing the double-quoted string "ab\x63$d", it would
2798   stop at the '$' and return an OP_CONST containing 'abc'.
2799
2800   It looks at PL_lex_inwhat and PL_lex_inpat to find out whether it's
2801   processing a pattern (PL_lex_inpat is true), a transliteration
2802   (PL_lex_inwhat == OP_TRANS is true), or a double-quoted string.
2803
2804   Returns a pointer to the character scanned up to. If this is
2805   advanced from the start pointer supplied (i.e. if anything was
2806   successfully parsed), will leave an OP_CONST for the substring scanned
2807   in pl_yylval. Caller must intuit reason for not parsing further
2808   by looking at the next characters herself.
2809
2810   In patterns:
2811     expand:
2812       \N{FOO}  => \N{U+hex_for_character_FOO}
2813       (if FOO expands to multiple characters, expands to \N{U+xx.XX.yy ...})
2814
2815     pass through:
2816         all other \-char, including \N and \N{ apart from \N{ABC}
2817
2818     stops on:
2819         @ and $ where it appears to be a var, but not for $ as tail anchor
2820         \l \L \u \U \Q \E
2821         (?{  or  (??{
2822
2823   In transliterations:
2824     characters are VERY literal, except for - not at the start or end
2825     of the string, which indicates a range.  However some backslash sequences
2826     are recognized: \r, \n, and the like
2827                     \007 \o{}, \x{}, \N{}
2828     If all elements in the transliteration are below 256,
2829     scan_const expands the range to the full set of intermediate
2830     characters. If the range is in utf8, the hyphen is replaced with
2831     a certain range mark which will be handled by pmtrans() in op.c.
2832
2833   In double-quoted strings:
2834     backslashes:
2835       all those recognized in transliterations
2836       deprecated backrefs: \1 (in substitution replacements)
2837       case and quoting: \U \Q \E
2838     stops on @ and $
2839
2840   scan_const does *not* construct ops to handle interpolated strings.
2841   It stops processing as soon as it finds an embedded $ or @ variable
2842   and leaves it to the caller to work out what's going on.
2843
2844   embedded arrays (whether in pattern or not) could be:
2845       @foo, @::foo, @'foo, @{foo}, @$foo, @+, @-.
2846
2847   $ in double-quoted strings must be the symbol of an embedded scalar.
2848
2849   $ in pattern could be $foo or could be tail anchor.  Assumption:
2850   it's a tail anchor if $ is the last thing in the string, or if it's
2851   followed by one of "()| \r\n\t"
2852
2853   \1 (backreferences) are turned into $1 in substitutions
2854
2855   The structure of the code is
2856       while (there's a character to process) {
2857           handle transliteration ranges
2858           skip regexp comments /(?#comment)/ and codes /(?{code})/
2859           skip #-initiated comments in //x patterns
2860           check for embedded arrays
2861           check for embedded scalars
2862           if (backslash) {
2863               deprecate \1 in substitution replacements
2864               handle string-changing backslashes \l \U \Q \E, etc.
2865               switch (what was escaped) {
2866                   handle \- in a transliteration (becomes a literal -)
2867                   if a pattern and not \N{, go treat as regular character
2868                   handle \132 (octal characters)
2869                   handle \x15 and \x{1234} (hex characters)
2870                   handle \N{name} (named characters, also \N{3,5} in a pattern)
2871                   handle \cV (control characters)
2872                   handle printf-style backslashes (\f, \r, \n, etc)
2873               } (end switch)
2874               continue
2875           } (end if backslash)
2876           handle regular character
2877     } (end while character to read)
2878
2879 */
2880
2881 STATIC char *
2882 S_scan_const(pTHX_ char *start)
2883 {
2884     char *send = PL_bufend;             /* end of the constant */
2885     SV *sv = newSV(send - start);       /* sv for the constant.  See note below
2886                                            on sizing. */
2887     char *s = start;                    /* start of the constant */
2888     char *d = SvPVX(sv);                /* destination for copies */
2889     bool dorange = FALSE;               /* are we in a translit range? */
2890     bool didrange = FALSE;              /* did we just finish a range? */
2891     bool in_charclass = FALSE;          /* within /[...]/ */
2892     bool has_utf8 = FALSE;              /* Output constant is UTF8 */
2893     bool  this_utf8 = cBOOL(UTF);       /* Is the source string assumed to be
2894                                            UTF8?  But, this can show as true
2895                                            when the source isn't utf8, as for
2896                                            example when it is entirely composed
2897                                            of hex constants */
2898     STRLEN utf8_variant_count = 0;      /* When not in UTF-8, this counts the
2899                                            number of characters found so far
2900                                            that will expand (into 2 bytes)
2901                                            should we have to convert to
2902                                            UTF-8) */
2903     SV *res;                            /* result from charnames */
2904     STRLEN offset_to_max = 0;   /* The offset in the output to where the range
2905                                    high-end character is temporarily placed */
2906
2907     /* Does something require special handling in tr/// ?  This avoids extra
2908      * work in a less likely case.  As such, khw didn't feel it was worth
2909      * adding any branches to the more mainline code to handle this, which
2910      * means that this doesn't get set in some circumstances when things like
2911      * \x{100} get expanded out.  As a result there needs to be extra testing
2912      * done in the tr code */
2913     bool has_above_latin1 = FALSE;
2914
2915     /* Note on sizing:  The scanned constant is placed into sv, which is
2916      * initialized by newSV() assuming one byte of output for every byte of
2917      * input.  This routine expects newSV() to allocate an extra byte for a
2918      * trailing NUL, which this routine will append if it gets to the end of
2919      * the input.  There may be more bytes of input than output (eg., \N{LATIN
2920      * CAPITAL LETTER A}), or more output than input if the constant ends up
2921      * recoded to utf8, but each time a construct is found that might increase
2922      * the needed size, SvGROW() is called.  Its size parameter each time is
2923      * based on the best guess estimate at the time, namely the length used so
2924      * far, plus the length the current construct will occupy, plus room for
2925      * the trailing NUL, plus one byte for every input byte still unscanned */
2926
2927     UV uv = UV_MAX; /* Initialize to weird value to try to catch any uses
2928                        before set */
2929 #ifdef EBCDIC
2930     int backslash_N = 0;            /* ? was the character from \N{} */
2931     int non_portable_endpoint = 0;  /* ? In a range is an endpoint
2932                                        platform-specific like \x65 */
2933 #endif
2934
2935     PERL_ARGS_ASSERT_SCAN_CONST;
2936
2937     assert(PL_lex_inwhat != OP_TRANSR);
2938     if (PL_lex_inwhat == OP_TRANS && PL_parser->lex_sub_op) {
2939         /* If we are doing a trans and we know we want UTF8 set expectation */
2940         has_utf8   = PL_parser->lex_sub_op->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF);
2941         this_utf8  = PL_parser->lex_sub_op->op_private & (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
2942     }
2943
2944     /* Protect sv from errors and fatal warnings. */
2945     ENTER_with_name("scan_const");
2946     SAVEFREESV(sv);
2947
2948     while (s < send
2949            || dorange   /* Handle tr/// range at right edge of input */
2950     ) {
2951
2952         /* get transliterations out of the way (they're most literal) */
2953         if (PL_lex_inwhat == OP_TRANS) {
2954
2955             /* But there isn't any special handling necessary unless there is a
2956              * range, so for most cases we just drop down and handle the value
2957              * as any other.  There are two exceptions.
2958              *
2959              * 1.  A hyphen indicates that we are actually going to have a
2960              *     range.  In this case, skip the '-', set a flag, then drop
2961              *     down to handle what should be the end range value.
2962              * 2.  After we've handled that value, the next time through, that
2963              *     flag is set and we fix up the range.
2964              *
2965              * Ranges entirely within Latin1 are expanded out entirely, in
2966              * order to make the transliteration a simple table look-up.
2967              * Ranges that extend above Latin1 have to be done differently, so
2968              * there is no advantage to expanding them here, so they are
2969              * stored here as Min, ILLEGAL_UTF8_BYTE, Max.  The illegal byte
2970              * signifies a hyphen without any possible ambiguity.  On EBCDIC
2971              * machines, if the range is expressed as Unicode, the Latin1
2972              * portion is expanded out even if the range extends above
2973              * Latin1.  This is because each code point in it has to be
2974              * processed here individually to get its native translation */
2975
2976             if (! dorange) {
2977
2978                 /* Here, we don't think we're in a range.  If the new character
2979                  * is not a hyphen; or if it is a hyphen, but it's too close to
2980                  * either edge to indicate a range, or if we haven't output any
2981                  * characters yet then it's a regular character. */
2982                 if (*s != '-' || s >= send - 1 || s == start || d == SvPVX(sv)) {
2983
2984                     /* A regular character.  Process like any other, but first
2985                      * clear any flags */
2986                     didrange = FALSE;
2987                     dorange = FALSE;
2988 #ifdef EBCDIC
2989                     non_portable_endpoint = 0;
2990                     backslash_N = 0;
2991 #endif
2992                     /* The tests here for being above Latin1 and similar ones
2993                      * in the following 'else' suffice to find all such
2994                      * occurences in the constant, except those added by a
2995                      * backslash escape sequence, like \x{100}.  Mostly, those
2996                      * set 'has_above_latin1' as appropriate */
2997                     if (this_utf8 && UTF8_IS_ABOVE_LATIN1(*s)) {
2998                         has_above_latin1 = TRUE;
2999                     }
3000
3001                     /* Drops down to generic code to process current byte */
3002                 }
3003                 else {  /* Is a '-' in the context where it means a range */
3004                     if (didrange) { /* Something like y/A-C-Z// */
3005                         Perl_croak(aTHX_ "Ambiguous range in transliteration"
3006                                          " operator");
3007                     }
3008
3009                     dorange = TRUE;
3010
3011                     s++;    /* Skip past the hyphen */
3012
3013                     /* d now points to where the end-range character will be
3014                      * placed.  Save it so won't have to go finding it later,
3015                      * and drop down to get that character.  (Actually we
3016                      * instead save the offset, to handle the case where a
3017                      * realloc in the meantime could change the actual
3018                      * pointer).  We'll finish processing the range the next
3019                      * time through the loop */
3020                     offset_to_max = d - SvPVX_const(sv);
3021
3022                     if (this_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             }  /* End of not a range */
3029             else {
3030                 /* Here we have parsed a range.  Now must handle it.  At this
3031                  * point:
3032                  * 'sv' is a SV* that contains the output string we are
3033                  *      constructing.  The final two characters in that string
3034                  *      are the range start and range end, in order.
3035                  * 'd'  points to just beyond the range end in the 'sv' string,
3036                  *      where we would next place something
3037                  * 'offset_to_max' is the offset in 'sv' at which the character
3038                  *      (the range's maximum end point) before 'd'  begins.
3039                  */
3040                 char * max_ptr = SvPVX(sv) + offset_to_max;
3041                 char * min_ptr;
3042                 IV range_min;
3043                 IV range_max;   /* last character in range */
3044                 STRLEN grow;
3045                 Size_t offset_to_min = 0;
3046                 Size_t extras = 0;
3047 #ifdef EBCDIC
3048                 bool convert_unicode;
3049                 IV real_range_max = 0;
3050 #endif
3051                 /* Get the code point values of the range ends. */
3052                 if (has_utf8) {
3053                     /* We know the utf8 is valid, because we just constructed
3054                      * it ourselves in previous loop iterations */
3055                     min_ptr = (char*) utf8_hop( (U8*) max_ptr, -1);
3056                     range_min = valid_utf8_to_uvchr( (U8*) min_ptr, NULL);
3057                     range_max = valid_utf8_to_uvchr( (U8*) max_ptr, NULL);
3058
3059                     /* This compensates for not all code setting
3060                      * 'has_above_latin1', so that we don't skip stuff that
3061                      * should be executed */
3062                     if (range_max > 255) {
3063                         has_above_latin1 = TRUE;
3064                     }
3065                 }
3066                 else {
3067                     min_ptr = max_ptr - 1;
3068                     range_min = * (U8*) min_ptr;
3069                     range_max = * (U8*) max_ptr;
3070                 }
3071
3072                 /* If the range is just a single code point, like tr/a-a/.../,
3073                  * that code point is already in the output, twice.  We can
3074                  * just back up over the second instance and avoid all the rest
3075                  * of the work.  But if it is a variant character, it's been
3076                  * counted twice, so decrement.  (This unlikely scenario is
3077                  * special cased, like the one for a range of 2 code points
3078                  * below, only because the main-line code below needs a range
3079                  * of 3 or more to work without special casing.  Might as well
3080                  * get it out of the way now.) */
3081                 if (UNLIKELY(range_max == range_min)) {
3082                     d = max_ptr;
3083                     if (! has_utf8 && ! UVCHR_IS_INVARIANT(range_max)) {
3084                         utf8_variant_count--;
3085                     }
3086                     goto range_done;
3087                 }
3088
3089 #ifdef EBCDIC
3090                 /* On EBCDIC platforms, we may have to deal with portable
3091                  * ranges.  These happen if at least one range endpoint is a
3092                  * Unicode value (\N{...}), or if the range is a subset of
3093                  * [A-Z] or [a-z], and both ends are literal characters,
3094                  * like 'A', and not like \x{C1} */
3095                 convert_unicode =
3096                                cBOOL(backslash_N)   /* \N{} forces Unicode,
3097                                                        hence portable range */
3098                     || (     ! non_portable_endpoint
3099                         && ((  isLOWER_A(range_min) && isLOWER_A(range_max))
3100                            || (isUPPER_A(range_min) && isUPPER_A(range_max))));
3101                 if (convert_unicode) {
3102
3103                     /* Special handling is needed for these portable ranges.
3104                      * They are defined to be in Unicode terms, which includes
3105                      * all the Unicode code points between the end points.
3106                      * Convert to Unicode to get the Unicode range.  Later we
3107                      * will convert each code point in the range back to
3108                      * native.  */
3109                     range_min = NATIVE_TO_UNI(range_min);
3110                     range_max = NATIVE_TO_UNI(range_max);
3111                 }
3112 #endif
3113
3114                 if (range_min > range_max) {
3115 #ifdef EBCDIC
3116                     if (convert_unicode) {
3117                         /* Need to convert back to native for meaningful
3118                          * messages for this platform */
3119                         range_min = UNI_TO_NATIVE(range_min);
3120                         range_max = UNI_TO_NATIVE(range_max);
3121                     }
3122 #endif
3123                     /* Use the characters themselves for the error message if
3124                      * ASCII printables; otherwise some visible representation
3125                      * of them */
3126                     if (isPRINT_A(range_min) && isPRINT_A(range_max)) {
3127                         Perl_croak(aTHX_
3128                          "Invalid range \"%c-%c\" in transliteration operator",
3129                          (char)range_min, (char)range_max);
3130                     }
3131 #ifdef EBCDIC
3132                     else if (convert_unicode) {
3133         /* diag_listed_as: Invalid range "%s" in transliteration operator */
3134                         Perl_croak(aTHX_
3135                            "Invalid range \"\\N{U+%04" UVXf "}-\\N{U+%04"
3136                            UVXf "}\" in transliteration operator",
3137                            range_min, range_max);
3138                     }
3139 #endif
3140                     else {
3141         /* diag_listed_as: Invalid range "%s" in transliteration operator */
3142                         Perl_croak(aTHX_
3143                            "Invalid range \"\\x{%04" UVXf "}-\\x{%04" UVXf "}\""
3144                            " in transliteration operator",
3145                            range_min, range_max);
3146                     }
3147                 }
3148
3149                 /* If the range is exactly two code points long, they are
3150                  * already both in the output */
3151                 if (UNLIKELY(range_min + 1 == range_max)) {
3152                     goto range_done;
3153                 }
3154
3155                 /* Here the range contains at least 3 code points */
3156
3157                 if (has_utf8) {
3158
3159                     /* If everything in the transliteration is below 256, we
3160                      * can avoid special handling later.  A translation table
3161                      * for each of those bytes is created by op.c.  So we
3162                      * expand out all ranges to their constituent code points.
3163                      * But if we've encountered something above 255, the
3164                      * expanding won't help, so skip doing that.  But if it's
3165                      * EBCDIC, we may have to look at each character below 256
3166                      * if we have to convert to/from Unicode values */
3167                     if (   has_above_latin1
3168 #ifdef EBCDIC
3169                         && (range_min > 255 || ! convert_unicode)
3170 #endif
3171                     ) {
3172                         /* Move the high character one byte to the right; then
3173                          * insert between it and the range begin, an illegal
3174                          * byte which serves to indicate this is a range (using
3175                          * a '-' would be ambiguous). */
3176                         char *e = d++;
3177                         while (e-- > max_ptr) {
3178                             *(e + 1) = *e;
3179                         }
3180                         *(e + 1) = (char) ILLEGAL_UTF8_BYTE;
3181                         goto range_done;
3182                     }
3183
3184                     /* Here, we're going to expand out the range.  For EBCDIC
3185                      * the range can extend above 255 (not so in ASCII), so
3186                      * for EBCDIC, split it into the parts above and below
3187                      * 255/256 */
3188 #ifdef EBCDIC
3189                     if (range_max > 255) {
3190                         real_range_max = range_max;
3191                         range_max = 255;
3192                     }
3193 #endif
3194                 }
3195
3196                 /* Here we need to expand out the string to contain each
3197                  * character in the range.  Grow the output to handle this.
3198                  * For non-UTF8, we need a byte for each code point in the
3199                  * range, minus the three that we've already allocated for: the
3200                  * hyphen, the min, and the max.  For UTF-8, we need this
3201                  * plus an extra byte for each code point that occupies two
3202                  * bytes (is variant) when in UTF-8 (except we've already
3203                  * allocated for the end points, including if they are
3204                  * variants).  For ASCII platforms and Unicode ranges on EBCDIC
3205                  * platforms, it's easy to calculate a precise number.  To
3206                  * start, we count the variants in the range, which we need
3207                  * elsewhere in this function anyway.  (For the case where it
3208                  * isn't easy to calculate, 'extras' has been initialized to 0,
3209                  * and the calculation is done in a loop further down.) */
3210 #ifdef EBCDIC
3211                 if (convert_unicode)
3212 #endif
3213                 {
3214                     /* This is executed unconditionally on ASCII, and for
3215                      * Unicode ranges on EBCDIC.  Under these conditions, all
3216                      * code points above a certain value are variant; and none
3217                      * under that value are.  We just need to find out how much
3218                      * of the range is above that value.  We don't count the
3219                      * end points here, as they will already have been counted
3220                      * as they were parsed. */
3221                     if (range_min >= UTF_CONTINUATION_MARK) {
3222
3223                         /* The whole range is made up of variants */
3224                         extras = (range_max - 1) - (range_min + 1) + 1;
3225                     }
3226                     else if (range_max >= UTF_CONTINUATION_MARK) {
3227
3228                         /* Only the higher portion of the range is variants */
3229                         extras = (range_max - 1) - UTF_CONTINUATION_MARK + 1;
3230                     }
3231
3232                     utf8_variant_count += extras;
3233                 }
3234
3235                 /* The base growth is the number of code points in the range,
3236                  * not including the endpoints, which have already been sized
3237                  * for (and output).  We don't subtract for the hyphen, as it
3238                  * has been parsed but not output, and the SvGROW below is
3239                  * based only on what's been output plus what's left to parse.
3240                  * */
3241                 grow = (range_max - 1) - (range_min + 1) + 1;
3242
3243                 if (has_utf8) {
3244 #ifdef EBCDIC
3245                     /* In some cases in EBCDIC, we haven't yet calculated a
3246                      * precise amount needed for the UTF-8 variants.  Just
3247                      * assume the worst case, that everything will expand by a
3248                      * byte */
3249                     if (! convert_unicode) {
3250                         grow *= 2;
3251                     }
3252                     else
3253 #endif
3254                     {
3255                         /* Otherwise we know exactly how many variants there
3256                          * are in the range. */
3257                         grow += extras;
3258                     }
3259                 }
3260
3261                 /* Grow, but position the output to overwrite the range min end
3262                  * point, because in some cases we overwrite that */
3263                 SvCUR_set(sv, d - SvPVX_const(sv));
3264                 offset_to_min = min_ptr - SvPVX_const(sv);
3265
3266                 /* See Note on sizing above. */
3267                 d = offset_to_min + SvGROW(sv, SvCUR(sv)
3268                                              + (send - s)
3269                                              + grow
3270                                              + 1 /* Trailing NUL */ );
3271
3272                 /* Now, we can expand out the range. */
3273 #ifdef EBCDIC
3274                 if (convert_unicode) {
3275                     SSize_t i;
3276
3277                     /* Recall that the min and max are now in Unicode terms, so
3278                      * we have to convert each character to its native
3279                      * equivalent */
3280                     if (has_utf8) {
3281                         for (i = range_min; i <= range_max; i++) {
3282                             append_utf8_from_native_byte(
3283                                                     LATIN1_TO_NATIVE((U8) i),
3284                                                     (U8 **) &d);
3285                         }
3286                     }
3287                     else {
3288                         for (i = range_min; i <= range_max; i++) {
3289                             *d++ = (char)LATIN1_TO_NATIVE((U8) i);
3290                         }
3291                     }
3292                 }
3293                 else
3294 #endif
3295                 /* Always gets run for ASCII, and sometimes for EBCDIC. */
3296                 {
3297                     /* Here, no conversions are necessary, which means that the
3298                      * first character in the range is already in 'd' and
3299                      * valid, so we can skip overwriting it */
3300                     if (has_utf8) {
3301                         SSize_t i;
3302                         d += UTF8SKIP(d);
3303                         for (i = range_min + 1; i <= range_max; i++) {
3304                             append_utf8_from_native_byte((U8) i, (U8 **) &d);
3305                         }
3306                     }
3307                     else {
3308                         SSize_t i;
3309                         d++;
3310                         assert(range_min + 1 <= range_max);
3311                         for (i = range_min + 1; i < range_max; i++) {
3312 #ifdef EBCDIC
3313                             /* In this case on EBCDIC, we haven't calculated
3314                              * the variants.  Do it here, as we go along */
3315                             if (! UVCHR_IS_INVARIANT(i)) {
3316                                 utf8_variant_count++;
3317                             }
3318 #endif
3319                             *d++ = (char)i;
3320                         }
3321
3322                         /* The range_max is done outside the loop so as to
3323                          * avoid having to special case not incrementing
3324                          * 'utf8_variant_count' on EBCDIC (it's already been
3325                          * counted when originally parsed) */
3326                         *d++ = (char) range_max;
3327                     }
3328                 }
3329
3330 #ifdef EBCDIC
3331                 /* If the original range extended above 255, add in that
3332                  * portion. */
3333                 if (real_range_max) {
3334                     *d++ = (char) UTF8_TWO_BYTE_HI(0x100);
3335                     *d++ = (char) UTF8_TWO_BYTE_LO(0x100);
3336                     if (real_range_max > 0x100) {
3337                         if (real_range_max > 0x101) {
3338                             *d++ = (char) ILLEGAL_UTF8_BYTE;
3339                         }
3340                         d = (char*)uvchr_to_utf8((U8*)d, real_range_max);
3341                     }
3342                 }
3343 #endif
3344
3345               range_done:
3346                 /* mark the range as done, and continue */
3347                 didrange = TRUE;
3348                 dorange = FALSE;
3349 #ifdef EBCDIC
3350                 non_portable_endpoint = 0;
3351                 backslash_N = 0;
3352 #endif
3353                 continue;
3354             } /* End of is a range */
3355         } /* End of transliteration.  Joins main code after these else's */
3356         else if (*s == '[' && PL_lex_inpat && !in_charclass) {
3357             char *s1 = s-1;
3358             int esc = 0;
3359             while (s1 >= start && *s1-- == '\\')
3360                 esc = !esc;
3361             if (!esc)
3362                 in_charclass = TRUE;
3363         }
3364         else if (*s == ']' && PL_lex_inpat && in_charclass) {
3365             char *s1 = s-1;
3366             int esc = 0;
3367             while (s1 >= start && *s1-- == '\\')
3368                 esc = !esc;
3369             if (!esc)
3370                 in_charclass = FALSE;
3371         }
3372             /* skip for regexp comments /(?#comment)/, except for the last
3373              * char, which will be done separately.  Stop on (?{..}) and
3374              * friends */
3375         else if (*s == '(' && PL_lex_inpat && s[1] == '?' && !in_charclass) {
3376             if (s[2] == '#') {
3377                 while (s+1 < send && *s != ')')
3378                     *d++ = *s++;
3379             }
3380             else if (!PL_lex_casemods
3381                      && (    s[2] == '{' /* This should match regcomp.c */
3382                          || (s[2] == '?' && s[3] == '{')))
3383             {
3384                 break;
3385             }
3386         }
3387             /* likewise skip #-initiated comments in //x patterns */
3388         else if (*s == '#'
3389                  && PL_lex_inpat
3390                  && !in_charclass
3391                  && ((PMOP*)PL_lex_inpat)->op_pmflags & RXf_PMf_EXTENDED)
3392         {
3393             while (s < send && *s != '\n')
3394                 *d++ = *s++;
3395         }
3396             /* no further processing of single-quoted regex */
3397         else if (PL_lex_inpat && SvIVX(PL_linestr) == '\'')
3398             goto default_action;
3399
3400             /* check for embedded arrays
3401              * (@foo, @::foo, @'foo, @{foo}, @$foo, @+, @-)
3402              */
3403         else if (*s == '@' && s[1]) {
3404             if (UTF
3405                ? isIDFIRST_utf8_safe(s+1, send)
3406                : isWORDCHAR_A(s[1]))
3407             {
3408                 break;
3409             }
3410             if (strchr(":'{$", s[1]))
3411                 break;
3412             if (!PL_lex_inpat && (s[1] == '+' || s[1] == '-'))
3413                 break; /* in regexp, neither @+ nor @- are interpolated */
3414         }
3415             /* check for embedded scalars.  only stop if we're sure it's a
3416              * variable.  */
3417         else if (*s == '$') {
3418             if (!PL_lex_inpat)  /* not a regexp, so $ must be var */
3419                 break;
3420             if (s + 1 < send && !strchr("()| \r\n\t", s[1])) {
3421                 if (s[1] == '\\') {
3422                     Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
3423                                    "Possible unintended interpolation of $\\ in regex");
3424                 }
3425                 break;          /* in regexp, $ might be tail anchor */
3426             }
3427         }
3428
3429         /* End of else if chain - OP_TRANS rejoin rest */
3430
3431         if (UNLIKELY(s >= send)) {
3432             assert(s == send);
3433             break;
3434         }
3435
3436         /* backslashes */
3437         if (*s == '\\' && s+1 < send) {
3438             char* e;    /* Can be used for ending '}', etc. */
3439
3440             s++;
3441
3442             /* warn on \1 - \9 in substitution replacements, but note that \11
3443              * is an octal; and \19 is \1 followed by '9' */
3444             if (PL_lex_inwhat == OP_SUBST
3445                 && !PL_lex_inpat
3446                 && isDIGIT(*s)
3447                 && *s != '0'
3448                 && !isDIGIT(s[1]))
3449             {
3450                 /* diag_listed_as: \%d better written as $%d */
3451                 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "\\%c better written as $%c", *s, *s);
3452                 *--s = '$';
3453                 break;
3454             }
3455
3456             /* string-change backslash escapes */
3457             if (PL_lex_inwhat != OP_TRANS && *s && strchr("lLuUEQF", *s)) {
3458                 --s;
3459                 break;
3460             }
3461             /* In a pattern, process \N, but skip any other backslash escapes.
3462              * This is because we don't want to translate an escape sequence
3463              * into a meta symbol and have the regex compiler use the meta
3464              * symbol meaning, e.g. \x{2E} would be confused with a dot.  But
3465              * in spite of this, we do have to process \N here while the proper
3466              * charnames handler is in scope.  See bugs #56444 and #62056.
3467              *
3468              * There is a complication because \N in a pattern may also stand
3469              * for 'match a non-nl', and not mean a charname, in which case its
3470              * processing should be deferred to the regex compiler.  To be a
3471              * charname it must be followed immediately by a '{', and not look
3472              * like \N followed by a curly quantifier, i.e., not something like
3473              * \N{3,}.  regcurly returns a boolean indicating if it is a legal
3474              * quantifier */
3475             else if (PL_lex_inpat
3476                     && (*s != 'N'
3477                         || s[1] != '{'
3478                         || regcurly(s + 1)))
3479             {
3480                 *d++ = '\\';
3481                 goto default_action;
3482             }
3483
3484             switch (*s) {
3485             default:
3486                 {
3487                     if ((isALPHANUMERIC(*s)))
3488                         Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
3489                                        "Unrecognized escape \\%c passed through",
3490                                        *s);
3491                     /* default action is to copy the quoted character */
3492                     goto default_action;
3493                 }
3494
3495             /* eg. \132 indicates the octal constant 0132 */
3496             case '0': case '1': case '2': case '3':
3497             case '4': case '5': case '6': case '7':
3498                 {
3499                     I32 flags = PERL_SCAN_SILENT_ILLDIGIT;
3500                     STRLEN len = 3;
3501                     uv = grok_oct(s, &len, &flags, NULL);
3502                     s += len;
3503                     if (len < 3 && s < send && isDIGIT(*s)
3504                         && ckWARN(WARN_MISC))
3505                     {
3506                         Perl_warner(aTHX_ packWARN(WARN_MISC),
3507                                     "%s", form_short_octal_warning(s, len));
3508                     }
3509                 }
3510                 goto NUM_ESCAPE_INSERT;
3511
3512             /* eg. \o{24} indicates the octal constant \024 */
3513             case 'o':
3514                 {
3515                     const char* error;
3516
3517                     bool valid = grok_bslash_o(&s, PL_bufend,
3518                                                &uv, &error,
3519                                                TRUE, /* Output warning */
3520                                                FALSE, /* Not strict */
3521                                                TRUE, /* Output warnings for
3522                                                          non-portables */
3523                                                UTF);
3524                     if (! valid) {
3525                         yyerror(error);
3526                         uv = 0; /* drop through to ensure range ends are set */
3527                     }
3528                     goto NUM_ESCAPE_INSERT;
3529                 }
3530
3531             /* eg. \x24 indicates the hex constant 0x24 */
3532             case 'x':
3533                 {
3534                     const char* error;
3535
3536                     bool valid = grok_bslash_x(&s, PL_bufend,
3537                                                &uv, &error,
3538                                                TRUE, /* Output warning */
3539                                                FALSE, /* Not strict */
3540                                                TRUE,  /* Output warnings for
3541                                                          non-portables */
3542                                                UTF);
3543                     if (! valid) {
3544                         yyerror(error);
3545                         uv = 0; /* drop through to ensure range ends are set */
3546                     }
3547                 }
3548
3549               NUM_ESCAPE_INSERT:
3550                 /* Insert oct or hex escaped character. */
3551
3552                 /* Here uv is the ordinal of the next character being added */
3553                 if (UVCHR_IS_INVARIANT(uv)) {
3554                     *d++ = (char) uv;
3555                 }
3556                 else {
3557                     if (!has_utf8 && uv > 255) {
3558
3559                         /* Here, 'uv' won't fit unless we convert to UTF-8.
3560                          * If we've only seen invariants so far, all we have to
3561                          * do is turn on the flag */
3562                         if (utf8_variant_count == 0) {
3563                             SvUTF8_on(sv);
3564                         }
3565                         else {
3566                             SvCUR_set(sv, d - SvPVX_const(sv));
3567                             SvPOK_on(sv);
3568                             *d = '\0';
3569
3570                             sv_utf8_upgrade_flags_grow(
3571                                            sv,
3572                                            SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3573
3574                                            /* Since we're having to grow here,
3575                                             * make sure we have enough room for
3576                                             * this escape and a NUL, so the
3577                                             * code immediately below won't have
3578                                             * to actually grow again */
3579                                           UVCHR_SKIP(uv)
3580                                         + (STRLEN)(send - s) + 1);
3581                             d = SvPVX(sv) + SvCUR(sv);
3582                         }
3583
3584                         has_above_latin1 = TRUE;
3585                         has_utf8 = TRUE;
3586                     }
3587
3588                     if (! has_utf8) {
3589                         *d++ = (char)uv;
3590                         utf8_variant_count++;
3591                     }
3592                     else {
3593                        /* Usually, there will already be enough room in 'sv'
3594                         * since such escapes are likely longer than any UTF-8
3595                         * sequence they can end up as.  This isn't the case on
3596                         * EBCDIC where \x{40000000} contains 12 bytes, and the
3597                         * UTF-8 for it contains 14.  And, we have to allow for
3598                         * a trailing NUL.  It probably can't happen on ASCII
3599                         * platforms, but be safe.  See Note on sizing above. */
3600                         const STRLEN needed = d - SvPVX(sv)
3601                                             + UVCHR_SKIP(uv)
3602                                             + (send - s)
3603                                             + 1;
3604                         if (UNLIKELY(needed > SvLEN(sv))) {
3605                             SvCUR_set(sv, d - SvPVX_const(sv));
3606                             d = SvCUR(sv) + SvGROW(sv, needed);
3607                         }
3608
3609                         d = (char*)uvchr_to_utf8((U8*)d, uv);
3610                         if (PL_lex_inwhat == OP_TRANS
3611                             && PL_parser->lex_sub_op)
3612                         {
3613                             PL_parser->lex_sub_op->op_private |=
3614                                 (PL_lex_repl ? OPpTRANS_FROM_UTF
3615                                              : OPpTRANS_TO_UTF);
3616                         }
3617                     }
3618                 }
3619 #ifdef EBCDIC
3620                 non_portable_endpoint++;
3621 #endif
3622                 continue;
3623
3624             case 'N':
3625                 /* In a non-pattern \N must be like \N{U+0041}, or it can be a
3626                  * named character, like \N{LATIN SMALL LETTER A}, or a named
3627                  * sequence, like \N{LATIN CAPITAL LETTER A WITH MACRON AND
3628                  * GRAVE} (except y/// can't handle the latter, croaking).  For
3629                  * convenience all three forms are referred to as "named
3630                  * characters" below.
3631                  *
3632                  * For patterns, \N also can mean to match a non-newline.  Code
3633                  * before this 'switch' statement should already have handled
3634                  * this situation, and hence this code only has to deal with
3635                  * the named character cases.
3636                  *
3637                  * For non-patterns, the named characters are converted to
3638                  * their string equivalents.  In patterns, named characters are
3639                  * not converted to their ultimate forms for the same reasons
3640                  * that other escapes aren't (mainly that the ultimate
3641                  * character could be considered a meta-symbol by the regex
3642                  * compiler).  Instead, they are converted to the \N{U+...}
3643                  * form to get the value from the charnames that is in effect
3644                  * right now, while preserving the fact that it was a named
3645                  * character, so that the regex compiler knows this.
3646                  *
3647                  * The structure of this section of code (besides checking for
3648                  * errors and upgrading to utf8) is:
3649                  *    If the named character is of the form \N{U+...}, pass it
3650                  *      through if a pattern; otherwise convert the code point
3651                  *      to utf8
3652                  *    Otherwise must be some \N{NAME}: convert to
3653                  *      \N{U+c1.c2...} if a pattern; otherwise convert to utf8
3654                  *
3655                  * Transliteration is an exception.  The conversion to utf8 is
3656                  * only done if the code point requires it to be representable.
3657                  *
3658                  * Here, 's' points to the 'N'; the test below is guaranteed to
3659                  * succeed if we are being called on a pattern, as we already
3660                  * know from a test above that the next character is a '{'.  A
3661                  * non-pattern \N must mean 'named character', which requires
3662                  * braces */
3663                 s++;
3664                 if (*s != '{') {
3665                     yyerror("Missing braces on \\N{}");
3666                     *d++ = '\0';
3667                     continue;
3668                 }
3669                 s++;
3670
3671                 /* If there is no matching '}', it is an error. */
3672                 if (! (e = (char *) memchr(s, '}', send - s))) {
3673                     if (! PL_lex_inpat) {
3674                         yyerror("Missing right brace on \\N{}");
3675                     } else {
3676                         yyerror("Missing right brace on \\N{} or unescaped left brace after \\N");
3677                     }
3678                     yyquit(); /* Have exhausted the input. */
3679                 }
3680
3681                 /* Here it looks like a named character */
3682
3683                 if (*s == 'U' && s[1] == '+') { /* \N{U+...} */
3684                     s += 2;         /* Skip to next char after the 'U+' */
3685                     if (PL_lex_inpat) {
3686
3687                         /* In patterns, we can have \N{U+xxxx.yyyy.zzzz...} */
3688                         /* Check the syntax.  */
3689                         const char *orig_s;
3690                         orig_s = s - 5;
3691                         if (!isXDIGIT(*s)) {
3692                           bad_NU:
3693                             yyerror(
3694                                 "Invalid hexadecimal number in \\N{U+...}"
3695                             );
3696                             s = e + 1;
3697                             *d++ = '\0';
3698                             continue;
3699                         }
3700                         while (++s < e) {
3701                             if (isXDIGIT(*s))
3702                                 continue;
3703                             else if ((*s == '.' || *s == '_')
3704                                   && isXDIGIT(s[1]))
3705                                 continue;
3706                             goto bad_NU;
3707                         }
3708
3709                         /* Pass everything through unchanged.
3710                          * +1 is for the '}' */
3711                         Copy(orig_s, d, e - orig_s + 1, char);
3712                         d += e - orig_s + 1;
3713                     }
3714                     else {  /* Not a pattern: convert the hex to string */
3715                         I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
3716                                 | PERL_SCAN_SILENT_ILLDIGIT
3717                                 | PERL_SCAN_DISALLOW_PREFIX;
3718                         STRLEN len = e - s;
3719                         uv = grok_hex(s, &len, &flags, NULL);
3720                         if (len == 0 || (len != (STRLEN)(e - s)))
3721                             goto bad_NU;
3722
3723                          /* For non-tr///, if the destination is not in utf8,
3724                           * unconditionally recode it to be so.  This is
3725                           * because \N{} implies Unicode semantics, and scalars
3726                           * have to be in utf8 to guarantee those semantics.
3727                           * tr/// doesn't care about Unicode rules, so no need
3728                           * there to upgrade to UTF-8 for small enough code
3729                           * points */
3730                         if (! has_utf8 && (   uv > 0xFF
3731                                            || PL_lex_inwhat != OP_TRANS))
3732                         {
3733                             /* See Note on sizing above.  */
3734                             const STRLEN extra = OFFUNISKIP(uv) + (send - e) + 1;
3735
3736                             SvCUR_set(sv, d - SvPVX_const(sv));
3737                             SvPOK_on(sv);
3738                             *d = '\0';
3739
3740                             if (utf8_variant_count == 0) {
3741                                 SvUTF8_on(sv);
3742                                 d = SvCUR(sv) + SvGROW(sv, SvCUR(sv) + extra);
3743                             }
3744                             else {
3745                                 sv_utf8_upgrade_flags_grow(
3746                                                sv,
3747                                                SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3748                                                extra);
3749                                 d = SvPVX(sv) + SvCUR(sv);
3750                             }
3751
3752                             has_utf8 = TRUE;
3753                             has_above_latin1 = TRUE;
3754                         }
3755
3756                         /* Add the (Unicode) code point to the output. */
3757                         if (! has_utf8 || OFFUNI_IS_INVARIANT(uv)) {
3758                             *d++ = (char) LATIN1_TO_NATIVE(uv);
3759                         }
3760                         else {
3761                             d = (char*) uvoffuni_to_utf8_flags((U8*)d, uv, 0);
3762                         }
3763                     }
3764                 }
3765                 else /* Here is \N{NAME} but not \N{U+...}. */
3766                      if ((res = get_and_check_backslash_N_name(s, e)))
3767                 {
3768                     STRLEN len;
3769                     const char *str = SvPV_const(res, len);
3770                     if (PL_lex_inpat) {
3771
3772                         if (! len) { /* The name resolved to an empty string */
3773                             Copy("\\N{}", d, 4, char);
3774                             d += 4;
3775                         }
3776                         else {
3777                             /* In order to not lose information for the regex
3778                             * compiler, pass the result in the specially made
3779                             * syntax: \N{U+c1.c2.c3...}, where c1 etc. are
3780                             * the code points in hex of each character
3781                             * returned by charnames */
3782
3783                             const char *str_end = str + len;
3784                             const STRLEN off = d - SvPVX_const(sv);
3785
3786                             if (! SvUTF8(res)) {
3787                                 /* For the non-UTF-8 case, we can determine the
3788                                  * exact length needed without having to parse
3789                                  * through the string.  Each character takes up
3790                                  * 2 hex digits plus either a trailing dot or
3791                                  * the "}" */
3792                                 const char initial_text[] = "\\N{U+";
3793                                 const STRLEN initial_len = sizeof(initial_text)
3794                                                            - 1;
3795                                 d = off + SvGROW(sv, off
3796                                                     + 3 * len
3797
3798                                                     /* +1 for trailing NUL */
3799                                                     + initial_len + 1
3800
3801                                                     + (STRLEN)(send - e));
3802                                 Copy(initial_text, d, initial_len, char);
3803                                 d += initial_len;
3804                                 while (str < str_end) {
3805                                     char hex_string[4];
3806                                     int len =
3807                                         my_snprintf(hex_string,
3808                                                   sizeof(hex_string),
3809                                                   "%02X.",
3810
3811                                                   /* The regex compiler is
3812                                                    * expecting Unicode, not
3813                                                    * native */
3814                                                   NATIVE_TO_LATIN1(*str));
3815                                     PERL_MY_SNPRINTF_POST_GUARD(len,
3816                                                            sizeof(hex_string));
3817                                     Copy(hex_string, d, 3, char);
3818                                     d += 3;
3819                                     str++;
3820                                 }
3821                                 d--;    /* Below, we will overwrite the final
3822                                            dot with a right brace */
3823                             }
3824                             else {
3825                                 STRLEN char_length; /* cur char's byte length */
3826
3827                                 /* and the number of bytes after this is
3828                                  * translated into hex digits */
3829                                 STRLEN output_length;
3830
3831                                 /* 2 hex per byte; 2 chars for '\N'; 2 chars
3832                                  * for max('U+', '.'); and 1 for NUL */
3833                                 char hex_string[2 * UTF8_MAXBYTES + 5];
3834
3835                                 /* Get the first character of the result. */
3836                                 U32 uv = utf8n_to_uvchr((U8 *) str,
3837                                                         len,
3838                                                         &char_length,
3839                                                         UTF8_ALLOW_ANYUV);
3840                                 /* Convert first code point to Unicode hex,
3841                                  * including the boiler plate before it. */
3842                                 output_length =
3843                                     my_snprintf(hex_string, sizeof(hex_string),
3844                                              "\\N{U+%X",
3845                                              (unsigned int) NATIVE_TO_UNI(uv));
3846
3847                                 /* Make sure there is enough space to hold it */
3848                                 d = off + SvGROW(sv, off
3849                                                     + output_length
3850                                                     + (STRLEN)(send - e)
3851                                                     + 2);       /* '}' + NUL */
3852                                 /* And output it */
3853                                 Copy(hex_string, d, output_length, char);
3854                                 d += output_length;
3855
3856                                 /* For each subsequent character, append dot and
3857                                 * its Unicode code point in hex */
3858                                 while ((str += char_length) < str_end) {
3859                                     const STRLEN off = d - SvPVX_const(sv);
3860                                     U32 uv = utf8n_to_uvchr((U8 *) str,
3861                                                             str_end - str,
3862                                                             &char_length,
3863                                                             UTF8_ALLOW_ANYUV);
3864                                     output_length =
3865                                         my_snprintf(hex_string,
3866                                              sizeof(hex_string),
3867                                              ".%X",
3868                                              (unsigned int) NATIVE_TO_UNI(uv));
3869
3870                                     d = off + SvGROW(sv, off
3871                                                         + output_length
3872                                                         + (STRLEN)(send - e)
3873                                                         + 2);   /* '}' +  NUL */
3874                                     Copy(hex_string, d, output_length, char);
3875                                     d += output_length;
3876                                 }
3877                             }
3878
3879                             *d++ = '}'; /* Done.  Add the trailing brace */
3880                         }
3881                     }
3882                     else { /* Here, not in a pattern.  Convert the name to a
3883                             * string. */
3884
3885                         if (PL_lex_inwhat == OP_TRANS) {
3886                             str = SvPV_const(res, len);
3887                             if (len > ((SvUTF8(res))
3888                                        ? UTF8SKIP(str)
3889                                        : 1U))
3890                             {
3891                                 yyerror(Perl_form(aTHX_
3892                                     "%.*s must not be a named sequence"
3893                                     " in transliteration operator",
3894                                         /*  +1 to include the "}" */
3895                                     (int) (e + 1 - start), start));
3896                                 *d++ = '\0';
3897                                 goto end_backslash_N;
3898                             }
3899
3900                             if (SvUTF8(res) && UTF8_IS_ABOVE_LATIN1(*str)) {
3901                                 has_above_latin1 = TRUE;
3902                             }
3903
3904                         }
3905                         else if (! SvUTF8(res)) {
3906                             /* Make sure \N{} return is UTF-8.  This is because
3907                              * \N{} implies Unicode semantics, and scalars have
3908                              * to be in utf8 to guarantee those semantics; but
3909                              * not needed in tr/// */
3910                             sv_utf8_upgrade_flags(res, 0);
3911                             str = SvPV_const(res, len);
3912                         }
3913
3914                          /* Upgrade destination to be utf8 if this new
3915                           * component is */
3916                         if (! has_utf8 && SvUTF8(res)) {
3917                             /* See Note on sizing above.  */
3918                             const STRLEN extra = len + (send - s) + 1;
3919
3920                             SvCUR_set(sv, d - SvPVX_const(sv));
3921                             SvPOK_on(sv);
3922                             *d = '\0';
3923
3924                             if (utf8_variant_count == 0) {
3925                                 SvUTF8_on(sv);
3926                                 d = SvCUR(sv) + SvGROW(sv, SvCUR(sv) + extra);
3927                             }
3928                             else {
3929                                 sv_utf8_upgrade_flags_grow(sv,
3930                                                 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3931                                                 extra);
3932                                 d = SvPVX(sv) + SvCUR(sv);
3933                             }
3934                             has_utf8 = TRUE;
3935                         } else if (len > (STRLEN)(e - s + 4)) { /* I _guess_ 4 is \N{} --jhi */
3936
3937                             /* See Note on sizing above.  (NOTE: SvCUR() is not
3938                              * set correctly here). */
3939                             const STRLEN extra = len + (send - e) + 1;
3940                             const STRLEN off = d - SvPVX_const(sv);
3941                             d = off + SvGROW(sv, off + extra);
3942                         }
3943                         Copy(str, d, len, char);
3944                         d += len;
3945                     }
3946
3947                     SvREFCNT_dec(res);
3948
3949                 } /* End \N{NAME} */
3950
3951               end_backslash_N:
3952 #ifdef EBCDIC
3953                 backslash_N++; /* \N{} is defined to be Unicode */
3954 #endif
3955                 s = e + 1;  /* Point to just after the '}' */
3956                 continue;
3957
3958             /* \c is a control character */
3959             case 'c':
3960                 s++;
3961                 if (s < send) {
3962                     *d++ = grok_bslash_c(*s, 1);
3963                 }
3964                 else {
3965                     yyerror("Missing control char name in \\c");
3966                     yyquit();   /* Are at end of input, no sense continuing */
3967                 }
3968 #ifdef EBCDIC
3969                 non_portable_endpoint++;
3970 #endif
3971                 break;
3972
3973             /* printf-style backslashes, formfeeds, newlines, etc */
3974             case 'b':
3975                 *d++ = '\b';
3976                 break;
3977             case 'n':
3978                 *d++ = '\n';
3979                 break;
3980             case 'r':
3981                 *d++ = '\r';
3982                 break;
3983             case 'f':
3984                 *d++ = '\f';
3985                 break;
3986             case 't':
3987                 *d++ = '\t';
3988                 break;
3989             case 'e':
3990                 *d++ = ESC_NATIVE;
3991                 break;
3992             case 'a':
3993                 *d++ = '\a';
3994                 break;
3995             } /* end switch */
3996
3997             s++;
3998             continue;
3999         } /* end if (backslash) */
4000
4001     default_action:
4002         /* Just copy the input to the output, though we may have to convert
4003          * to/from UTF-8.
4004          *
4005          * If the input has the same representation in UTF-8 as not, it will be
4006          * a single byte, and we don't care about UTF8ness; just copy the byte */
4007         if (NATIVE_BYTE_IS_INVARIANT((U8)(*s))) {
4008             *d++ = *s++;
4009         }
4010         else if (! this_utf8 && ! has_utf8) {
4011             /* If neither source nor output is UTF-8, is also a single byte,
4012              * just copy it; but this byte counts should we later have to
4013              * convert to UTF-8 */
4014             *d++ = *s++;
4015             utf8_variant_count++;
4016         }
4017         else if (this_utf8 && has_utf8) {   /* Both UTF-8, can just copy */
4018             const STRLEN len = UTF8SKIP(s);
4019
4020             /* We expect the source to have already been checked for
4021              * malformedness */
4022             assert(isUTF8_CHAR((U8 *) s, (U8 *) send));
4023
4024             Copy(s, d, len, U8);
4025             d += len;
4026             s += len;
4027         }
4028         else { /* UTF8ness matters and doesn't match, need to convert */
4029             STRLEN len = 1;
4030             const UV nextuv   = (this_utf8)
4031                                 ? utf8n_to_uvchr((U8*)s, send - s, &len, 0)
4032                                 : (UV) ((U8) *s);
4033             STRLEN need = UVCHR_SKIP(nextuv);
4034
4035             if (!has_utf8) {
4036                 SvCUR_set(sv, d - SvPVX_const(sv));
4037                 SvPOK_on(sv);
4038                 *d = '\0';
4039
4040                 /* See Note on sizing above. */
4041                 need += (STRLEN)(send - s) + 1;
4042
4043                 if (utf8_variant_count == 0) {
4044                     SvUTF8_on(sv);
4045                     d = SvCUR(sv) + SvGROW(sv, SvCUR(sv) + need);
4046                 }
4047                 else {
4048                     sv_utf8_upgrade_flags_grow(sv,
4049                                                SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4050                                                need);
4051                     d = SvPVX(sv) + SvCUR(sv);
4052                 }
4053                 has_utf8 = TRUE;
4054             } else if (need > len) {
4055                 /* encoded value larger than old, may need extra space (NOTE:
4056                  * SvCUR() is not set correctly here).   See Note on sizing
4057                  * above.  */
4058                 const STRLEN extra = need + (send - s) + 1;
4059                 const STRLEN off = d - SvPVX_const(sv);
4060                 d = off + SvGROW(sv, off + extra);
4061             }
4062             s += len;
4063
4064             d = (char*)uvchr_to_utf8((U8*)d, nextuv);
4065         }
4066     } /* while loop to process each character */
4067
4068     /* terminate the string and set up the sv */
4069     *d = '\0';
4070     SvCUR_set(sv, d - SvPVX_const(sv));
4071     if (SvCUR(sv) >= SvLEN(sv))
4072         Perl_croak(aTHX_ "panic: constant overflowed allocated space, %" UVuf
4073                    " >= %" UVuf, (UV)SvCUR(sv), (UV)SvLEN(sv));
4074
4075     SvPOK_on(sv);
4076     if (has_utf8) {
4077         SvUTF8_on(sv);
4078         if (PL_lex_inwhat == OP_TRANS && PL_parser->lex_sub_op) {
4079             PL_parser->lex_sub_op->op_private |=
4080                     (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
4081         }
4082     }
4083
4084     /* shrink the sv if we allocated more than we used */
4085     if (SvCUR(sv) + 5 < SvLEN(sv)) {
4086         SvPV_shrink_to_cur(sv);
4087     }
4088
4089     /* return the substring (via pl_yylval) only if we parsed anything */
4090     if (s > start) {
4091         char *s2 = start;
4092         for (; s2 < s; s2++) {
4093             if (*s2 == '\n')
4094                 COPLINE_INC_WITH_HERELINES;
4095         }
4096         SvREFCNT_inc_simple_void_NN(sv);
4097         if (   (PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ))
4098             && ! PL_parser->lex_re_reparsing)
4099         {
4100             const char *const key = PL_lex_inpat ? "qr" : "q";
4101             const STRLEN keylen = PL_lex_inpat ? 2 : 1;
4102             const char *type;
4103             STRLEN typelen;
4104
4105             if (PL_lex_inwhat == OP_TRANS) {
4106                 type = "tr";
4107                 typelen = 2;
4108             } else if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat) {
4109                 type = "s";
4110                 typelen = 1;
4111             } else if (PL_lex_inpat && SvIVX(PL_linestr) == '\'') {
4112                 type = "q";
4113                 typelen = 1;
4114             } else  {
4115                 type = "qq";
4116                 typelen = 2;
4117             }
4118
4119             sv = S_new_constant(aTHX_ start, s - start, key, keylen, sv, NULL,
4120                                 type, typelen);
4121         }
4122         pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
4123     }
4124     LEAVE_with_name("scan_const");
4125     return s;
4126 }
4127
4128 /* S_intuit_more
4129  * Returns TRUE if there's more to the expression (e.g., a subscript),
4130  * FALSE otherwise.
4131  *
4132  * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
4133  *
4134  * ->[ and ->{ return TRUE
4135  * ->$* ->$#* ->@* ->@[ ->@{ return TRUE if postderef_qq is enabled
4136  * { and [ outside a pattern are always subscripts, so return TRUE
4137  * if we're outside a pattern and it's not { or [, then return FALSE
4138  * if we're in a pattern and the first char is a {
4139  *   {4,5} (any digits around the comma) returns FALSE
4140  * if we're in a pattern and the first char is a [
4141  *   [] returns FALSE
4142  *   [SOMETHING] has a funky algorithm to decide whether it's a
4143  *      character class or not.  It has to deal with things like
4144  *      /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
4145  * anything else returns TRUE
4146  */
4147
4148 /* This is the one truly awful dwimmer necessary to conflate C and sed. */
4149
4150 STATIC int
4151 S_intuit_more(pTHX_ char *s, char *e)
4152 {
4153     PERL_ARGS_ASSERT_INTUIT_MORE;
4154
4155     if (PL_lex_brackets)
4156         return TRUE;
4157     if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
4158         return TRUE;
4159     if (*s == '-' && s[1] == '>'
4160      && FEATURE_POSTDEREF_QQ_IS_ENABLED
4161      && ( (s[2] == '$' && (s[3] == '*' || (s[3] == '#' && s[4] == '*')))
4162         ||(s[2] == '@' && strchr("*[{",s[3])) ))
4163         return TRUE;
4164     if (*s != '{' && *s != '[')
4165         return FALSE;
4166     PL_parser->sub_no_recover = TRUE;
4167     if (!PL_lex_inpat)
4168         return TRUE;
4169
4170     /* In a pattern, so maybe we have {n,m}. */
4171     if (*s == '{') {
4172         if (regcurly(s)) {
4173             return FALSE;
4174         }
4175         return TRUE;
4176     }
4177
4178     /* On the other hand, maybe we have a character class */
4179
4180     s++;
4181     if (*s == ']' || *s == '^')
4182         return FALSE;
4183     else {
4184         /* this is terrifying, and it works */
4185         int weight;
4186         char seen[256];
4187         const char * const send = (char *) memchr(s, ']', e - s);
4188         unsigned char un_char, last_un_char;
4189         char tmpbuf[sizeof PL_tokenbuf * 4];
4190
4191         if (!send)              /* has to be an expression */
4192             return TRUE;
4193         weight = 2;             /* let's weigh the evidence */
4194
4195         if (*s == '$')
4196             weight -= 3;
4197         else if (isDIGIT(*s)) {
4198             if (s[1] != ']') {
4199                 if (isDIGIT(s[1]) && s[2] == ']')
4200                     weight -= 10;
4201             }
4202             else
4203                 weight -= 100;
4204         }
4205         Zero(seen,256,char);
4206         un_char = 255;
4207         for (; s < send; s++) {
4208             last_un_char = un_char;
4209             un_char = (unsigned char)*s;
4210             switch (*s) {
4211             case '@':
4212             case '&':
4213             case '$':
4214                 weight -= seen[un_char] * 10;
4215                 if (isWORDCHAR_lazy_if_safe(s+1, PL_bufend, UTF)) {
4216                     int len;
4217                     scan_ident(s, tmpbuf, sizeof tmpbuf, FALSE);
4218                     len = (int)strlen(tmpbuf);
4219                     if (len > 1 && gv_fetchpvn_flags(tmpbuf, len,
4220                                                     UTF ? SVf_UTF8 : 0, SVt_PV))
4221                         weight -= 100;
4222                     else
4223                         weight -= 10;
4224                 }
4225                 else if (*s == '$'
4226                          && s[1]
4227                          && strchr("[#!%*<>()-=",s[1]))
4228                 {
4229                     if (/*{*/ strchr("])} =",s[2]))
4230                         weight -= 10;
4231                     else
4232                         weight -= 1;
4233                 }
4234                 break;
4235             case '\\':
4236                 un_char = 254;
4237                 if (s[1]) {
4238                     if (strchr("wds]",s[1]))
4239                         weight += 100;
4240                     else if (seen[(U8)'\''] || seen[(U8)'"'])
4241                         weight += 1;
4242                     else if (strchr("rnftbxcav",s[1]))
4243                         weight += 40;
4244                     else if (isDIGIT(s[1])) {
4245                         weight += 40;
4246                         while (s[1] && isDIGIT(s[1]))
4247                             s++;
4248                     }
4249                 }
4250                 else
4251                     weight += 100;
4252                 break;
4253             case '-':
4254                 if (s[1] == '\\')
4255                     weight += 50;
4256                 if (strchr("aA01! ",last_un_char))
4257                     weight += 30;
4258                 if (strchr("zZ79~",s[1]))
4259                     weight += 30;
4260                 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
4261                     weight -= 5;        /* cope with negative subscript */
4262                 break;
4263             default:
4264                 if (!isWORDCHAR(last_un_char)
4265                     && !(last_un_char == '$' || last_un_char == '@'
4266                          || last_un_char == '&')
4267                     && isALPHA(*s) && s[1] && isALPHA(s[1])) {
4268                     char *d = s;
4269                     while (isALPHA(*s))
4270                         s++;
4271                     if (keyword(d, s - d, 0))
4272                         weight -= 150;
4273                 }
4274                 if (un_char == last_un_char + 1)
4275                     weight += 5;
4276                 weight -= seen[un_char];
4277                 break;
4278             }
4279             seen[un_char]++;
4280         }
4281         if (weight >= 0)        /* probably a character class */
4282             return FALSE;
4283     }
4284
4285     return TRUE;
4286 }
4287
4288 /*
4289  * S_intuit_method
4290  *
4291  * Does all the checking to disambiguate
4292  *   foo bar
4293  * between foo(bar) and bar->foo.  Returns 0 if not a method, otherwise
4294  * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
4295  *
4296  * First argument is the stuff after the first token, e.g. "bar".
4297  *
4298  * Not a method if foo is a filehandle.
4299  * Not a method if foo is a subroutine prototyped to take a filehandle.
4300  * Not a method if it's really "Foo $bar"
4301  * Method if it's "foo $bar"
4302  * Not a method if it's really "print foo $bar"
4303  * Method if it's really "foo package::" (interpreted as package->foo)
4304  * Not a method if bar is known to be a subroutine ("sub bar; foo bar")
4305  * Not a method if bar is a filehandle or package, but is quoted with
4306  *   =>
4307  */
4308
4309 STATIC int
4310 S_intuit_method(pTHX_ char *start, SV *ioname, CV *cv)
4311 {
4312     char *s = start + (*start == '$');
4313     char tmpbuf[sizeof PL_tokenbuf];
4314     STRLEN len;
4315     GV* indirgv;
4316         /* Mustn't actually add anything to a symbol table.
4317            But also don't want to "initialise" any placeholder
4318            constants that might already be there into full
4319            blown PVGVs with attached PVCV.  */
4320     GV * const gv =
4321         ioname ? gv_fetchsv(ioname, GV_NOADD_NOINIT, SVt_PVCV) : NULL;
4322
4323     PERL_ARGS_ASSERT_INTUIT_METHOD;
4324
4325     if (gv && SvTYPE(gv) == SVt_PVGV && GvIO(gv))
4326             return 0;
4327     if (cv && SvPOK(cv)) {
4328         const char *proto = CvPROTO(cv);
4329         if (proto) {
4330             while (*proto && (isSPACE(*proto) || *proto == ';'))
4331                 proto++;
4332             if (*proto == '*')
4333                 return 0;
4334         }
4335     }
4336
4337     if (*start == '$') {
4338         SSize_t start_off = start - SvPVX(PL_linestr);
4339         if (cv || PL_last_lop_op == OP_PRINT || PL_last_lop_op == OP_SAY
4340             || isUPPER(*PL_tokenbuf))
4341             return 0;
4342         /* this could be $# */
4343         if (isSPACE(*s))
4344             s = skipspace(s);
4345         PL_bufptr = SvPVX(PL_linestr) + start_off;
4346         PL_expect = XREF;
4347         return *s == '(' ? FUNCMETH : METHOD;
4348     }
4349
4350     s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
4351     /* start is the beginning of the possible filehandle/object,
4352      * and s is the end of it
4353      * tmpbuf is a copy of it (but with single quotes as double colons)
4354      */
4355
4356     if (!keyword(tmpbuf, len, 0)) {
4357         if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
4358             len -= 2;
4359             tmpbuf[len] = '\0';
4360             goto bare_package;
4361         }
4362         indirgv = gv_fetchpvn_flags(tmpbuf, len,
4363                                     GV_NOADD_NOINIT|( UTF ? SVf_UTF8 : 0 ),
4364                                     SVt_PVCV);
4365         if (indirgv && SvTYPE(indirgv) != SVt_NULL
4366          && (!isGV(indirgv) || GvCVu(indirgv)))
4367             return 0;
4368         /* filehandle or package name makes it a method */
4369         if (!cv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, UTF ? SVf_UTF8 : 0)) {
4370             s = skipspace(s);
4371             if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
4372                 return 0;       /* no assumptions -- "=>" quotes bareword */
4373       bare_package:
4374             NEXTVAL_NEXTTOKE.opval = newSVOP(OP_CONST, 0,
4375                                                   S_newSV_maybe_utf8(aTHX_ tmpbuf, len));
4376             NEXTVAL_NEXTTOKE.opval->op_private = OPpCONST_BARE;
4377             PL_expect = XTERM;
4378             force_next(BAREWORD);
4379             PL_bufptr = s;
4380             return *s == '(' ? FUNCMETH : METHOD;
4381         }
4382     }
4383     return 0;
4384 }
4385
4386 /* Encoded script support. filter_add() effectively inserts a
4387  * 'pre-processing' function into the current source input stream.
4388  * Note that the filter function only applies to the current source file
4389  * (e.g., it will not affect files 'require'd or 'use'd by this one).
4390  *
4391  * The datasv parameter (which may be NULL) can be used to pass
4392  * private data to this instance of the filter. The filter function
4393  * can recover the SV using the FILTER_DATA macro and use it to
4394  * store private buffers and state information.
4395  *
4396  * The supplied datasv parameter is upgraded to a PVIO type
4397  * and the IoDIRP/IoANY field is used to store the function pointer,
4398  * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
4399  * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
4400  * private use must be set using malloc'd pointers.
4401  */
4402
4403 SV *
4404 Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
4405 {
4406     if (!funcp)
4407         return NULL;
4408
4409     if (!PL_parser)
4410         return NULL;
4411
4412     if (PL_parser->lex_flags & LEX_IGNORE_UTF8_HINTS)
4413         Perl_croak(aTHX_ "Source filters apply only to byte streams");
4414
4415     if (!PL_rsfp_filters)
4416         PL_rsfp_filters = newAV();
4417     if (!datasv)
4418         datasv = newSV(0);
4419     SvUPGRADE(datasv, SVt_PVIO);
4420     IoANY(datasv) = FPTR2DPTR(void *, funcp); /* stash funcp into spare field */
4421     IoFLAGS(datasv) |= IOf_FAKE_DIRP;
4422     DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
4423                           FPTR2DPTR(void *, IoANY(datasv)),
4424                           SvPV_nolen(datasv)));
4425     av_unshift(PL_rsfp_filters, 1);
4426     av_store(PL_rsfp_filters, 0, datasv) ;
4427     if (
4428         !PL_parser->filtered
4429      && PL_parser->lex_flags & LEX_EVALBYTES
4430      && PL_bufptr < PL_bufend
4431     ) {
4432         const char *s = PL_bufptr;
4433         while (s < PL_bufend) {
4434             if (*s == '\n') {
4435                 SV *linestr = PL_parser->linestr;
4436                 char *buf = SvPVX(linestr);
4437                 STRLEN const bufptr_pos = PL_parser->bufptr - buf;
4438                 STRLEN const oldbufptr_pos = PL_parser->oldbufptr - buf;
4439                 STRLEN const oldoldbufptr_pos=PL_parser->oldoldbufptr-buf;
4440                 STRLEN const linestart_pos = PL_parser->linestart - buf;
4441                 STRLEN const last_uni_pos =
4442                     PL_parser->last_uni ? PL_parser->last_uni - buf : 0;
4443                 STRLEN const last_lop_pos =
4444                     PL_parser->last_lop ? PL_parser->last_lop - buf : 0;
4445                 av_push(PL_rsfp_filters, linestr);
4446                 PL_parser->linestr =
4447                     newSVpvn(SvPVX(linestr), ++s-SvPVX(linestr));
4448                 buf = SvPVX(PL_parser->linestr);
4449                 PL_parser->bufend = buf + SvCUR(PL_parser->linestr);
4450                 PL_parser->bufptr = buf + bufptr_pos;
4451                 PL_parser->oldbufptr = buf + oldbufptr_pos;
4452                 PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
4453                 PL_parser->linestart = buf + linestart_pos;
4454                 if (PL_parser->last_uni)
4455                     PL_parser->last_uni = buf + last_uni_pos;
4456                 if (PL_parser->last_lop)
4457                     PL_parser->last_lop = buf + last_lop_pos;
4458                 SvLEN_set(linestr, SvCUR(linestr));
4459                 SvCUR_set(linestr, s - SvPVX(linestr));
4460                 PL_parser->filtered = 1;
4461                 break;
4462             }
4463             s++;
4464         }
4465     }
4466     return(datasv);
4467 }
4468
4469
4470 /* Delete most recently added instance of this filter function. */
4471 void
4472 Perl_filter_del(pTHX_ filter_t funcp)
4473 {
4474     SV *datasv;
4475
4476     PERL_ARGS_ASSERT_FILTER_DEL;
4477
4478 #ifdef DEBUGGING
4479     DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p",
4480                           FPTR2DPTR(void*, funcp)));
4481 #endif
4482     if (!PL_parser || !PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
4483         return;
4484     /* if filter is on top of stack (usual case) just pop it off */
4485     datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
4486     if (IoANY(datasv) == FPTR2DPTR(void *, funcp)) {
4487         sv_free(av_pop(PL_rsfp_filters));
4488
4489         return;
4490     }
4491     /* we need to search for the correct entry and clear it     */
4492     Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
4493 }
4494
4495
4496 /* Invoke the idxth filter function for the current rsfp.        */
4497 /* maxlen 0 = read one text line */
4498 I32
4499 Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
4500 {
4501     filter_t funcp;
4502     I32 ret;
4503     SV *datasv = NULL;
4504     /* This API is bad. It should have been using unsigned int for maxlen.
4505        Not sure if we want to change the API, but if not we should sanity
4506        check the value here.  */
4507     unsigned int correct_length = maxlen < 0 ?  PERL_INT_MAX : maxlen;
4508
4509     PERL_ARGS_ASSERT_FILTER_READ;
4510
4511     if (!PL_parser || !PL_rsfp_filters)
4512         return -1;
4513     if (idx > AvFILLp(PL_rsfp_filters)) {       /* Any more filters?    */
4514         /* Provide a default input filter to make life easy.    */
4515         /* Note that we append to the line. This is handy.      */
4516         DEBUG_P(PerlIO_printf(Perl_debug_log,
4517                               "filter_read %d: from rsfp\n", idx));
4518         if (correct_length) {
4519             /* Want a block */
4520             int len ;
4521             const int old_len = SvCUR(buf_sv);
4522
4523             /* ensure buf_sv is large enough */
4524             SvGROW(buf_sv, (STRLEN)(old_len + correct_length + 1)) ;
4525             if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len,
4526                                    correct_length)) <= 0) {
4527                 if (PerlIO_error(PL_rsfp))
4528                     return -1;          /* error */
4529                 else
4530                     return 0 ;          /* end of file */
4531             }
4532             SvCUR_set(buf_sv, old_len + len) ;
4533             SvPVX(buf_sv)[old_len + len] = '\0';
4534         } else {
4535             /* Want a line */
4536             if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
4537                 if (PerlIO_error(PL_rsfp))
4538                     return -1;          /* error */
4539                 else
4540                     return 0 ;          /* end of file */
4541             }
4542         }
4543         return SvCUR(buf_sv);
4544     }
4545     /* Skip this filter slot if filter has been deleted */
4546     if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) {
4547         DEBUG_P(PerlIO_printf(Perl_debug_log,
4548                               "filter_read %d: skipped (filter deleted)\n",
4549                               idx));
4550         return FILTER_READ(idx+1, buf_sv, correct_length); /* recurse */
4551     }
4552     if (SvTYPE(datasv) != SVt_PVIO) {
4553         if (correct_length) {
4554             /* Want a block */
4555             const STRLEN remainder = SvLEN(datasv) - SvCUR(datasv);
4556             if (!remainder) return 0; /* eof */
4557             if (correct_length > remainder) correct_length = remainder;
4558             sv_catpvn(buf_sv, SvEND(datasv), correct_length);
4559             SvCUR_set(datasv, SvCUR(datasv) + correct_length);
4560         } else {
4561             /* Want a line */
4562             const char *s = SvEND(datasv);
4563             const char *send = SvPVX(datasv) + SvLEN(datasv);
4564             while (s < send) {
4565                 if (*s == '\n') {
4566                     s++;
4567                     break;
4568                 }
4569                 s++;
4570             }
4571             if (s == send) return 0; /* eof */
4572             sv_catpvn(buf_sv, SvEND(datasv), s-SvEND(datasv));
4573             SvCUR_set(datasv, s-SvPVX(datasv));
4574         }
4575         return SvCUR(buf_sv);
4576     }
4577     /* Get function pointer hidden within datasv        */
4578     funcp = DPTR2FPTR(filter_t, IoANY(datasv));
4579     DEBUG_P(PerlIO_printf(Perl_debug_log,
4580                           "filter_read %d: via function %p (%s)\n",
4581                           idx, (void*)datasv, SvPV_nolen_const(datasv)));
4582     /* Call function. The function is expected to       */
4583     /* call "FILTER_READ(idx+1, buf_sv)" first.         */
4584     /* Return: <0:error, =0:eof, >0:not eof             */
4585     ENTER;
4586     save_scalar(PL_errgv);
4587     ret = (*funcp)(aTHX_ idx, buf_sv, correct_length);
4588     LEAVE;
4589     return ret;
4590 }
4591
4592 STATIC char *
4593 S_filter_gets(pTHX_ SV *sv, STRLEN append)
4594 {
4595     PERL_ARGS_ASSERT_FILTER_GETS;
4596
4597 #ifdef PERL_CR_FILTER
4598     if (!PL_rsfp_filters) {
4599         filter_add(S_cr_textfilter,NULL);
4600     }
4601 #endif
4602     if (PL_rsfp_filters) {
4603         if (!append)
4604             SvCUR_set(sv, 0);   /* start with empty line        */
4605         if (FILTER_READ(0, sv, 0) > 0)
4606             return ( SvPVX(sv) ) ;
4607         else
4608             return NULL ;
4609     }
4610     else
4611         return (sv_gets(sv, PL_rsfp, append));
4612 }
4613
4614 STATIC HV *
4615 S_find_in_my_stash(pTHX_ const char *pkgname, STRLEN len)
4616 {
4617     GV *gv;
4618
4619     PERL_ARGS_ASSERT_FIND_IN_MY_STASH;
4620
4621     if (memEQs(pkgname, len, "__PACKAGE__"))
4622         return PL_curstash;
4623
4624     if (len > 2
4625         && (pkgname[len - 2] == ':' && pkgname[len - 1] == ':')
4626         && (gv = gv_fetchpvn_flags(pkgname,
4627                                    len,
4628                                    ( UTF ? SVf_UTF8 : 0 ), SVt_PVHV)))
4629     {
4630         return GvHV(gv);                        /* Foo:: */
4631     }
4632
4633     /* use constant CLASS => 'MyClass' */
4634     gv = gv_fetchpvn_flags(pkgname, len, UTF ? SVf_UTF8 : 0, SVt_PVCV);
4635     if (gv && GvCV(gv)) {
4636         SV * const sv = cv_const_sv(GvCV(gv));
4637         if (sv)
4638             return gv_stashsv(sv, 0);
4639     }
4640
4641     return gv_stashpvn(pkgname, len, UTF ? SVf_UTF8 : 0);
4642 }
4643
4644
4645 STATIC char *
4646 S_tokenize_use(pTHX_ int is_use, char *s) {
4647     PERL_ARGS_ASSERT_TOKENIZE_USE;
4648
4649     if (PL_expect != XSTATE)
4650         /* diag_listed_as: "use" not allowed in expression */
4651         yyerror(Perl_form(aTHX_ "\"%s\" not allowed in expression",
4652                     is_use ? "use" : "no"));
4653     PL_expect = XTERM;
4654     s = skipspace(s);
4655     if (isDIGIT(*s) || (*s == 'v' && isDIGIT(s[1]))) {
4656         s = force_version(s, TRUE);
4657         if (*s == ';' || *s == '}'
4658                 || (s = skipspace(s), (*s == ';' || *s == '}'))) {
4659             NEXTVAL_NEXTTOKE.opval = NULL;
4660             force_next(BAREWORD);
4661         }
4662         else if (*s == 'v') {
4663             s = force_word(s,BAREWORD,FALSE,TRUE);
4664             s = force_version(s, FALSE);
4665         }
4666     }
4667     else {
4668         s = force_word(s,BAREWORD,FALSE,TRUE);
4669         s = force_version(s, FALSE);
4670     }
4671     pl_yylval.ival = is_use;
4672     return s;
4673 }
4674 #ifdef DEBUGGING
4675     static const char* const exp_name[] =
4676         { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
4677           "ATTRTERM", "TERMBLOCK", "XBLOCKTERM", "POSTDEREF",
4678           "SIGVAR", "TERMORDORDOR"
4679         };
4680 #endif
4681
4682 #define word_takes_any_delimiter(p,l) S_word_takes_any_delimiter(p,l)
4683 STATIC bool
4684 S_word_takes_any_delimiter(char *p, STRLEN len)
4685 {
4686     return (len == 1 && strchr("msyq", p[0]))
4687             || (len == 2
4688                 && ((p[0] == 't' && p[1] == 'r')
4689                     || (p[0] == 'q' && strchr("qwxr", p[1]))));
4690 }
4691
4692 static void
4693 S_check_scalar_slice(pTHX_ char *s)
4694 {
4695     s++;
4696     while (SPACE_OR_TAB(*s)) s++;
4697     if (*s == 'q' && s[1] == 'w' && !isWORDCHAR_lazy_if_safe(s+2,
4698                                                              PL_bufend,
4699                                                              UTF))
4700     {
4701         return;
4702     }
4703     while (    isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF)
4704            || (*s && strchr(" \t$#+-'\"", *s)))
4705     {
4706         s += UTF ? UTF8SKIP(s) : 1;
4707     }
4708     if (*s == '}' || *s == ']')
4709         pl_yylval.ival = OPpSLICEWARNING;
4710 }
4711
4712 #define lex_token_boundary() S_lex_token_boundary(aTHX)
4713 static void
4714 S_lex_token_boundary(pTHX)
4715 {
4716     PL_oldoldbufptr = PL_oldbufptr;
4717     PL_oldbufptr = PL_bufptr;
4718 }
4719
4720 #define vcs_conflict_marker(s) S_vcs_conflict_marker(aTHX_ s)
4721 static char *
4722 S_vcs_conflict_marker(pTHX_ char *s)
4723 {
4724     lex_token_boundary();
4725     PL_bufptr = s;
4726     yyerror("Version control conflict marker");
4727     while (s < PL_bufend && *s != '\n')
4728         s++;
4729     return s;
4730 }
4731
4732 /*
4733   yylex
4734
4735   Works out what to call the token just pulled out of the input
4736   stream.  The yacc parser takes care of taking the ops we return and
4737   stitching them into a tree.
4738
4739   Returns:
4740     The type of the next token
4741
4742   Structure:
4743       Check if we have already built the token; if so, use it.
4744       Switch based on the current state:
4745           - if we have a case modifier in a string, deal with that
4746           - handle other cases of interpolation inside a string
4747           - scan the next line if we are inside a format
4748       In the normal state, switch on the next character:
4749           - default:
4750             if alphabetic, go to key lookup
4751             unrecognized character - croak
4752           - 0/4/26: handle end-of-line or EOF
4753           - cases for whitespace
4754           - \n and #: handle comments and line numbers
4755           - various operators, brackets and sigils
4756           - numbers
4757           - quotes
4758           - 'v': vstrings (or go to key lookup)
4759           - 'x' repetition operator (or go to key lookup)
4760           - other ASCII alphanumerics (key lookup begins here):
4761               word before => ?
4762               keyword plugin
4763               scan built-in keyword (but do nothing with it yet)
4764               check for statement label
4765               check for lexical subs
4766                   goto just_a_word if there is one
4767               see whether built-in keyword is overridden
4768               switch on keyword number:
4769                   - default: just_a_word:
4770                       not a built-in keyword; handle bareword lookup
4771                       disambiguate between method and sub call
4772                       fall back to bareword
4773                   - cases for built-in keywords
4774 */
4775
4776
4777 int
4778 Perl_yylex(pTHX)
4779 {
4780     dVAR;
4781     char *s = PL_bufptr;
4782     char *d;
4783     STRLEN len;
4784     bool bof = FALSE;
4785     const bool saw_infix_sigil = cBOOL(PL_parser->saw_infix_sigil);
4786     U8 formbrack = 0;
4787     U32 fake_eof = 0;
4788
4789     /* orig_keyword, gvp, and gv are initialized here because
4790      * jump to the label just_a_word_zero can bypass their
4791      * initialization later. */
4792     I32 orig_keyword = 0;
4793     GV *gv = NULL;
4794     GV **gvp = NULL;
4795
4796     if (UNLIKELY(PL_parser->recheck_utf8_validity)) {
4797         const U8* first_bad_char_loc;
4798         if (UTF && UNLIKELY(! is_utf8_string_loc((U8 *) PL_bufptr,
4799                                                         PL_bufend - PL_bufptr,
4800                                                         &first_bad_char_loc)))
4801         {
4802             _force_out_malformed_utf8_message(first_bad_char_loc,
4803                                               (U8 *) PL_bufend,
4804                                               0,
4805                                               1 /* 1 means die */ );
4806             NOT_REACHED; /* NOTREACHED */
4807         }
4808         PL_parser->recheck_utf8_validity = FALSE;
4809     }
4810     DEBUG_T( {
4811         SV* tmp = newSVpvs("");
4812         PerlIO_printf(Perl_debug_log, "### %" IVdf ":LEX_%s/X%s %s\n",
4813             (IV)CopLINE(PL_curcop),
4814             lex_state_names[PL_lex_state],
4815             exp_name[PL_expect],
4816             pv_display(tmp, s, strlen(s), 0, 60));
4817         SvREFCNT_dec(tmp);
4818     } );
4819
4820     /* when we've already built the next token, just pull it out of the queue */
4821     if (PL_nexttoke) {
4822         PL_nexttoke--;
4823         pl_yylval = PL_nextval[PL_nexttoke];
4824         {
4825             I32 next_type;
4826             next_type = PL_nexttype[PL_nexttoke];
4827             if (next_type & (7<<24)) {
4828                 if (next_type & (1<<24)) {
4829                     if (PL_lex_brackets > 100)
4830                         Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
4831                     PL_lex_brackstack[PL_lex_brackets++] =
4832                         (char) ((next_type >> 16) & 0xff);
4833                 }
4834                 if (next_type & (2<<24))
4835                     PL_lex_allbrackets++;
4836                 if (next_type & (4<<24))
4837                     PL_lex_allbrackets--;
4838                 next_type &= 0xffff;
4839             }
4840             return REPORT(next_type == 'p' ? pending_ident() : next_type);
4841         }
4842     }
4843
4844     switch (PL_lex_state) {
4845     case LEX_NORMAL:
4846     case LEX_INTERPNORMAL:
4847         break;
4848
4849     /* interpolated case modifiers like \L \U, including \Q and \E.
4850        when we get here, PL_bufptr is at the \
4851     */
4852     case LEX_INTERPCASEMOD:
4853 #ifdef DEBUGGING
4854         if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
4855             Perl_croak(aTHX_
4856                        "panic: INTERPCASEMOD bufptr=%p, bufend=%p, *bufptr=%u",
4857                        PL_bufptr, PL_bufend, *PL_bufptr);
4858 #endif
4859         /* handle \E or end of string */
4860         if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
4861             /* if at a \E */
4862             if (PL_lex_casemods) {
4863                 const char oldmod = PL_lex_casestack[--PL_lex_casemods];
4864                 PL_lex_casestack[PL_lex_casemods] = '\0';
4865
4866                 if (PL_bufptr != PL_bufend
4867                     && (oldmod == 'L' || oldmod == 'U' || oldmod == 'Q'
4868                         || oldmod == 'F')) {
4869                     PL_bufptr += 2;
4870                     PL_lex_state = LEX_INTERPCONCAT;
4871                 }
4872                 PL_lex_allbrackets--;
4873                 return REPORT(')');
4874             }
4875             else if ( PL_bufptr != PL_bufend && PL_bufptr[1] == 'E' ) {
4876                /* Got an unpaired \E */
4877                Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4878                         "Useless use of \\E");
4879             }
4880             if (PL_bufptr != PL_bufend)
4881                 PL_bufptr += 2;
4882             PL_lex_state = LEX_INTERPCONCAT;
4883             return yylex();
4884         }
4885         else {
4886             DEBUG_T({ PerlIO_printf(Perl_debug_log,
4887               "### Saw case modifier\n"); });
4888             s = PL_bufptr + 1;
4889             if (s[1] == '\\' && s[2] == 'E') {
4890                 PL_bufptr = s + 3;
4891                 PL_lex_state = LEX_INTERPCONCAT;
4892                 return yylex();
4893             }
4894             else {
4895                 I32 tmp;
4896                 if (   memBEGINs(s, (STRLEN) (PL_bufend - s), "L\\u")
4897                     || memBEGINs(s, (STRLEN) (PL_bufend - s), "U\\l"))
4898                 {
4899                     tmp = *s, *s = s[2], s[2] = (char)tmp;      /* misordered... */
4900                 }
4901                 if ((*s == 'L' || *s == 'U' || *s == 'F')
4902                     && (strpbrk(PL_lex_casestack, "LUF")))
4903                 {
4904                     PL_lex_casestack[--PL_lex_casemods] = '\0';
4905                     PL_lex_allbrackets--;
4906                     return REPORT(')');
4907                 }
4908                 if (PL_lex_casemods > 10)
4909                     Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
4910                 PL_lex_casestack[PL_lex_casemods++] = *s;
4911                 PL_lex_casestack[PL_lex_casemods] = '\0';
4912                 PL_lex_state = LEX_INTERPCONCAT;
4913                 NEXTVAL_NEXTTOKE.ival = 0;
4914                 force_next((2<<24)|'(');
4915                 if (*s == 'l')
4916                     NEXTVAL_NEXTTOKE.ival = OP_LCFIRST;
4917                 else if (*s == 'u')
4918                     NEXTVAL_NEXTTOKE.ival = OP_UCFIRST;
4919                 else if (*s == 'L')
4920                     NEXTVAL_NEXTTOKE.ival = OP_LC;
4921                 else if (*s == 'U')
4922                     NEXTVAL_NEXTTOKE.ival = OP_UC;
4923                 else if (*s == 'Q')
4924                     NEXTVAL_NEXTTOKE.ival = OP_QUOTEMETA;
4925                 else if (*s == 'F')
4926                     NEXTVAL_NEXTTOKE.ival = OP_FC;
4927                 else
4928                     Perl_croak(aTHX_ "panic: yylex, *s=%u", *s);
4929                 PL_bufptr = s + 1;
4930             }
4931             force_next(FUNC);
4932             if (PL_lex_starts) {
4933                 s = PL_bufptr;
4934                 PL_lex_starts = 0;
4935                 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
4936                 if (PL_lex_casemods == 1 && PL_lex_inpat)
4937                     TOKEN(',');
4938                 else
4939                     AopNOASSIGN(OP_CONCAT);
4940             }
4941             else
4942                 return yylex();
4943         }
4944
4945     case LEX_INTERPPUSH:
4946         return REPORT(sublex_push());
4947
4948     case LEX_INTERPSTART:
4949         if (PL_bufptr == PL_bufend)
4950             return REPORT(sublex_done());
4951         DEBUG_T({ if(*PL_bufptr != '(') PerlIO_printf(Perl_debug_log,
4952               "### Interpolated variable\n"); });
4953         PL_expect = XTERM;
4954         /* for /@a/, we leave the joining for the regex engine to do
4955          * (unless we're within \Q etc) */
4956         PL_lex_dojoin = (*PL_bufptr == '@'
4957                             && (!PL_lex_inpat || PL_lex_casemods));
4958         PL_lex_state = LEX_INTERPNORMAL;
4959         if (PL_lex_dojoin) {
4960             NEXTVAL_NEXTTOKE.ival = 0;
4961             force_next(',');
4962             force_ident("\"", '$');
4963             NEXTVAL_NEXTTOKE.ival = 0;
4964             force_next('$');
4965             NEXTVAL_NEXTTOKE.ival = 0;
4966             force_next((2<<24)|'(');
4967             NEXTVAL_NEXTTOKE.ival = OP_JOIN;    /* emulate join($", ...) */
4968             force_next(FUNC);
4969         }
4970         /* Convert (?{...}) and friends to 'do {...}' */
4971         if (PL_lex_inpat && *PL_bufptr == '(') {
4972             PL_parser->lex_shared->re_eval_start = PL_bufptr;
4973             PL_bufptr += 2;
4974             if (*PL_bufptr != '{')
4975                 PL_bufptr++;
4976             PL_expect = XTERMBLOCK;
4977             force_next(DO);
4978         }
4979
4980         if (PL_lex_starts++) {
4981             s = PL_bufptr;
4982             /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
4983             if (!PL_lex_casemods && PL_lex_inpat)
4984                 TOKEN(',');
4985             else
4986                 AopNOASSIGN(OP_CONCAT);
4987         }
4988         return yylex();
4989
4990     case LEX_INTERPENDMAYBE:
4991         if (intuit_more(PL_bufptr, PL_bufend)) {
4992             PL_lex_state = LEX_INTERPNORMAL;    /* false alarm, more expr */
4993             break;
4994         }
4995         /* FALLTHROUGH */
4996
4997     case LEX_INTERPEND:
4998         if (PL_lex_dojoin) {
4999             const U8 dojoin_was = PL_lex_dojoin;
5000             PL_lex_dojoin = FALSE;
5001             PL_lex_state = LEX_INTERPCONCAT;
5002             PL_lex_allbrackets--;
5003             return REPORT(dojoin_was == 1 ? (int)')' : (int)POSTJOIN);
5004         }
5005         if (PL_lex_inwhat == OP_SUBST && PL_linestr == PL_lex_repl
5006             && SvEVALED(PL_lex_repl))
5007         {
5008             if (PL_bufptr != PL_bufend)
5009                 Perl_croak(aTHX_ "Bad evalled substitution pattern");
5010             PL_lex_repl = NULL;
5011         }
5012         /* Paranoia.  re_eval_start is adjusted when S_scan_heredoc sets
5013            re_eval_str.  If the here-doc body’s length equals the previous
5014            value of re_eval_start, re_eval_start will now be null.  So
5015            check re_eval_str as well. */
5016         if (PL_parser->lex_shared->re_eval_start
5017          || PL_parser->lex_shared->re_eval_str) {
5018             SV *sv;
5019             if (*PL_bufptr != ')')
5020                 Perl_croak(aTHX_ "Sequence (?{...}) not terminated with ')'");
5021             PL_bufptr++;
5022             /* having compiled a (?{..}) expression, return the original
5023              * text too, as a const */
5024             if (PL_parser->lex_shared->re_eval_str) {
5025                 sv = PL_parser->lex_shared->re_eval_str;
5026                 PL_parser->lex_shared->re_eval_str = NULL;
5027                 SvCUR_set(sv,
5028                          PL_bufptr - PL_parser->lex_shared->re_eval_start);
5029                 SvPV_shrink_to_cur(sv);
5030             }
5031             else sv = newSVpvn(PL_parser->lex_shared->re_eval_start,
5032                          PL_bufptr - PL_parser->lex_shared->re_eval_start);
5033             NEXTVAL_NEXTTOKE.opval =
5034                     newSVOP(OP_CONST, 0,
5035                                  sv);
5036             force_next(THING);
5037             PL_parser->lex_shared->re_eval_start = NULL;
5038             PL_expect = XTERM;
5039             return REPORT(',');
5040         }
5041
5042         /* FALLTHROUGH */
5043     case LEX_INTERPCONCAT:
5044 #ifdef DEBUGGING
5045         if (PL_lex_brackets)
5046             Perl_croak(aTHX_ "panic: INTERPCONCAT, lex_brackets=%ld",
5047                        (long) PL_lex_brackets);
5048 #endif
5049         if (PL_bufptr == PL_bufend)
5050             return REPORT(sublex_done());
5051
5052         /* m'foo' still needs to be parsed for possible (?{...}) */
5053         if (SvIVX(PL_linestr) == '\'' && !PL_lex_inpat) {
5054             SV *sv = newSVsv(PL_linestr);
5055             sv = tokeq(sv);
5056             pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
5057             s = PL_bufend;
5058         }
5059         else {
5060             int save_error_count = PL_error_count;
5061
5062             s = scan_const(PL_bufptr);
5063
5064             /* Set flag if this was a pattern and there were errors.  op.c will
5065              * refuse to compile a pattern with this flag set.  Otherwise, we
5066              * could get segfaults, etc. */
5067             if (PL_lex_inpat && PL_error_count > save_error_count) {
5068                 ((PMOP*)PL_lex_inpat)->op_pmflags |= PMf_HAS_ERROR;
5069             }
5070             if (*s == '\\')
5071                 PL_lex_state = LEX_INTERPCASEMOD;
5072             else
5073                 PL_lex_state = LEX_INTERPSTART;
5074         }
5075
5076         if (s != PL_bufptr) {
5077             NEXTVAL_NEXTTOKE = pl_yylval;
5078             PL_expect = XTERM;
5079             force_next(THING);
5080             if (PL_lex_starts++) {
5081                 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
5082                 if (!PL_lex_casemods && PL_lex_inpat)
5083                     TOKEN(',');
5084                 else
5085                     AopNOASSIGN(OP_CONCAT);
5086             }
5087             else {
5088                 PL_bufptr = s;
5089                 return yylex();
5090             }
5091         }
5092
5093         return yylex();
5094     case LEX_FORMLINE:
5095         if (PL_parser->sub_error_count != PL_error_count) {
5096             /* There was an error parsing a formline, which tends to
5097                mess up the parser.
5098                Unlike interpolated sub-parsing, we can't treat any of
5099                these as recoverable, so no need to check sub_no_recover.
5100             */
5101             yyquit();
5102         }
5103         assert(PL_lex_formbrack);
5104         s = scan_formline(PL_bufptr);
5105         if (!PL_lex_formbrack)
5106         {
5107             formbrack = 1;
5108             goto rightbracket;
5109         }
5110         PL_bufptr = s;
5111         return yylex();
5112     }
5113
5114     /* We really do *not* want PL_linestr ever becoming a COW. */
5115     assert (!SvIsCOW(PL_linestr));
5116     s = PL_bufptr;
5117     PL_oldoldbufptr = PL_oldbufptr;
5118     PL_oldbufptr = s;
5119     PL_parser->saw_infix_sigil = 0;
5120
5121     if (PL_in_my == KEY_sigvar) {
5122         /* we expect the sigil and optional var name part of a
5123          * signature element here. Since a '$' is not necessarily
5124          * followed by a var name, handle it specially here; the general
5125          * yylex code would otherwise try to interpret whatever follows
5126          * as a var; e.g. ($, ...) would be seen as the var '$,'
5127          */
5128
5129         U8 sigil;
5130
5131         s = skipspace(s);
5132         sigil = *s++;
5133         PL_bufptr = s; /* for error reporting */
5134         switch (sigil) {
5135         case '$':
5136         case '@':
5137         case '%':
5138             /* spot stuff that looks like an prototype */
5139             if (strchr("$:@%&*;\\[]", *s)) {
5140                 yyerror("Illegal character following sigil in a subroutine signature");
5141                 break;
5142             }
5143             /* '$#' is banned, while '$ # comment' isn't */
5144             if (*s == '#') {
5145                 yyerror("'#' not allowed immediately following a sigil in a subroutine signature");
5146                 break;
5147             }
5148             s = skipspace(s);
5149             if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
5150                 char *dest = PL_tokenbuf + 1;
5151                 /* read var name, including sigil, into PL_tokenbuf */
5152                 PL_tokenbuf[0] = sigil;
5153                 parse_ident(&s, &dest, dest + sizeof(PL_tokenbuf) - 1,
5154                     0, cBOOL(UTF), FALSE, FALSE);
5155                 *dest = '\0';
5156                 assert(PL_tokenbuf[1]); /* we have a variable name */
5157             }
5158             else {
5159                 *PL_tokenbuf = 0;
5160                 PL_in_my = 0;
5161             }
5162
5163             s = skipspace(s);
5164             /* parse the = for the default ourselves to avoid '+=' etc being accepted here
5165              * as the ASSIGNOP, and exclude other tokens that start with =
5166              */
5167             if (*s == '=' && (!s[1] || strchr("=~>", s[1]) == 0)) {
5168                 /* save now to report with the same context as we did when
5169                  * all ASSIGNOPS were accepted */
5170                 PL_oldbufptr = s;
5171
5172                 ++s;
5173                 NEXTVAL_NEXTTOKE.ival = 0;
5174                 force_next(ASSIGNOP);
5175                 PL_expect = XTERM;
5176             }
5177             else if (*s == ',' || *s == ')') {
5178                 PL_expect = XOPERATOR;
5179             }
5180             else {
5181                 /* make sure the context shows the unexpected character and
5182                  * hopefully a bit more */
5183                 if (*s) ++s;
5184                 while (*s && *s != '$' && *s != '@' && *s != '%' && *s != ')')
5185                     s++;
5186                 PL_bufptr = s; /* for error reporting */
5187                 yyerror("Illegal operator following parameter in a subroutine signature");
5188                 PL_in_my = 0;
5189             }
5190             if (*PL_tokenbuf) {
5191                 NEXTVAL_NEXTTOKE.ival = sigil;
5192                 force_next('p'); /* force a signature pending identifier */
5193             }
5194             break;
5195
5196         case ')':
5197             PL_expect = XBLOCK;
5198             break;
5199         case ',': /* handle ($a,,$b) */
5200             break;
5201
5202         default:
5203             PL_in_my = 0;
5204             yyerror("A signature parameter must start with '$', '@' or '%'");
5205             /* very crude error recovery: skip to likely next signature
5206              * element */
5207             while (*s && *s != '$' && *s != '@' && *s != '%' && *s != ')')
5208                 s++;
5209             break;
5210         }
5211         TOKEN(sigil);
5212     }
5213
5214   retry:
5215     switch (*s) {
5216     default:
5217         if (UTF) {
5218             if (isIDFIRST_utf8_safe(s, PL_bufend)) {
5219                 goto keylookup;
5220             }
5221         }
5222         else if (isALNUMC(*s)) {
5223             goto keylookup;
5224         }
5225     {
5226         SV *dsv = newSVpvs_flags("", SVs_TEMP);
5227         const char *c;
5228         if (UTF) {
5229             STRLEN skiplen = UTF8SKIP(s);
5230             STRLEN stravail = PL_bufend - s;
5231             c = sv_uni_display(dsv, newSVpvn_flags(s,
5232                                                    skiplen > stravail ? stravail : skiplen,
5233                                                    SVs_TEMP | SVf_UTF8),
5234                                10, UNI_DISPLAY_ISPRINT);
5235         }
5236         else {
5237             c = Perl_form(aTHX_ "\\x%02X", (unsigned char)*s);
5238         }
5239
5240         if (s >= PL_linestart) {
5241             d = PL_linestart;
5242         }
5243         else {
5244             /* somehow (probably due to a parse failure), PL_linestart has advanced
5245              * pass PL_bufptr, get a reasonable beginning of line
5246              */
5247             d = s;
5248             while (d > SvPVX(PL_linestr) && d[-1] && d[-1] != '\n')
5249                 --d;
5250         }
5251         len = UTF ? Perl_utf8_length(aTHX_ (U8 *) d, (U8 *) s) : (STRLEN) (s - d);
5252         if (len > UNRECOGNIZED_PRECEDE_COUNT) {
5253             d = UTF ? (char *) utf8_hop_back((U8 *) s, -UNRECOGNIZED_PRECEDE_COUNT, (U8 *)d) : s - UNRECOGNIZED_PRECEDE_COUNT;
5254         }
5255
5256         Perl_croak(aTHX_  "Unrecognized character %s; marked by <-- HERE after %" UTF8f "<-- HERE near column %d", c,
5257                           UTF8fARG(UTF, (s - d), d),
5258                          (int) len + 1);
5259     }
5260     case 4:
5261     case 26:
5262         goto fake_eof;                  /* emulate EOF on ^D or ^Z */
5263     case 0:
5264         if ((!PL_rsfp || PL_lex_inwhat)
5265          && (!PL_parser->filtered || s+1 < PL_bufend)) {
5266             PL_last_uni = 0;
5267             PL_last_lop = 0;
5268             if (PL_lex_brackets
5269                 && PL_lex_brackstack[PL_lex_brackets-1] != XFAKEEOF)
5270             {
5271                 yyerror((const char *)
5272                         (PL_lex_formbrack
5273                          ? "Format not terminated"
5274                          : "Missing right curly or square bracket"));
5275             }
5276             DEBUG_T( { PerlIO_printf(Perl_debug_log,
5277                         "### Tokener got EOF\n");
5278             } );
5279             TOKEN(0);
5280         }
5281         if (s++ < PL_bufend)
5282             goto retry;                 /* ignore stray nulls */
5283         PL_last_uni = 0;
5284         PL_last_lop = 0;
5285         if (!PL_in_eval && !PL_preambled) {
5286             PL_preambled = TRUE;
5287             if (PL_perldb) {
5288                 /* Generate a string of Perl code to load the debugger.
5289                  * If PERL5DB is set, it will return the contents of that,
5290                  * otherwise a compile-time require of perl5db.pl.  */
5291
5292                 const char * const pdb = PerlEnv_getenv("PERL5DB");
5293
5294                 if (pdb) {
5295                     sv_setpv(PL_linestr, pdb);
5296                     sv_catpvs(PL_linestr,";");
5297                 } else {
5298                     SETERRNO(0,SS_NORMAL);
5299                     sv_setpvs(PL_linestr, "BEGIN { require 'perl5db.pl' };");
5300                 }
5301                 PL_parser->preambling = CopLINE(PL_curcop);
5302             } else
5303                 SvPVCLEAR(PL_linestr);
5304             if (PL_preambleav) {
5305                 SV **svp = AvARRAY(PL_preambleav);
5306                 SV **const end = svp + AvFILLp(PL_preambleav);
5307                 while(svp <= end) {
5308                     sv_catsv(PL_linestr, *svp);
5309                     ++svp;
5310                     sv_catpvs(PL_linestr, ";");
5311                 }
5312                 sv_free(MUTABLE_SV(PL_preambleav));
5313                 PL_preambleav = NULL;
5314             }
5315             if (PL_minus_E)
5316                 sv_catpvs(PL_linestr,
5317                           "use feature ':5." STRINGIFY(PERL_VERSION) "';");
5318             if (PL_minus_n || PL_minus_p) {
5319                 sv_catpvs(PL_linestr, "LINE: while (<>) {"/*}*/);
5320                 if (PL_minus_l)
5321                     sv_catpvs(PL_linestr,"chomp;");
5322                 if (PL_minus_a) {
5323                     if (PL_minus_F) {
5324                         if (   (   *PL_splitstr == '/'
5325                                 || *PL_splitstr == '\''
5326                                 || *PL_splitstr == '"')
5327                             && strchr(PL_splitstr + 1, *PL_splitstr))
5328                         {
5329                             /* strchr is ok, because -F pattern can't contain
5330                              * embeddded NULs */
5331                             Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
5332                         }
5333                         else {
5334                             /* "q\0${splitstr}\0" is legal perl. Yes, even NUL
5335                                bytes can be used as quoting characters.  :-) */
5336                             const char *splits = PL_splitstr;
5337                             sv_catpvs(PL_linestr, "our @F=split(q\0");
5338                             do {
5339                                 /* Need to \ \s  */
5340                                 if (*splits == '\\')
5341                                     sv_catpvn(PL_linestr, splits, 1);
5342                                 sv_catpvn(PL_linestr, splits, 1);
5343                             } while (*splits++);
5344                             /* This loop will embed the trailing NUL of
5345                                PL_linestr as the last thing it does before
5346                                terminating.  */
5347                             sv_catpvs(PL_linestr, ");");
5348                         }
5349                     }
5350                     else
5351                         sv_catpvs(PL_linestr,"our @F=split(' ');");
5352                 }
5353             }
5354             sv_catpvs(PL_linestr, "\n");
5355             PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
5356             PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
5357             PL_last_lop = PL_last_uni = NULL;
5358             if (PERLDB_LINE_OR_SAVESRC && PL_curstash != PL_debstash)
5359                 update_debugger_info(PL_linestr, NULL, 0);
5360             goto retry;
5361         }
5362         do {
5363             fake_eof = 0;
5364             bof = cBOOL(PL_rsfp);
5365             if (0) {
5366               fake_eof:
5367                 fake_eof = LEX_FAKE_EOF;
5368             }
5369             PL_bufptr = PL_bufend;
5370             COPLINE_INC_WITH_HERELINES;
5371             if (!lex_next_chunk(fake_eof)) {
5372                 CopLINE_dec(PL_curcop);
5373                 s = PL_bufptr;
5374                 TOKEN(';');     /* not infinite loop because rsfp is NULL now */
5375             }
5376             CopLINE_dec(PL_curcop);
5377             s = PL_bufptr;
5378             /* If it looks like the start of a BOM or raw UTF-16,
5379              * check if it in fact is. */
5380             if (bof && PL_rsfp
5381                 && (   *s == 0
5382                     || *(U8*)s == BOM_UTF8_FIRST_BYTE
5383                     || *(U8*)s >= 0xFE
5384                     || s[1] == 0))
5385             {
5386                 Off_t offset = (IV)PerlIO_tell(PL_rsfp);
5387                 bof = (offset == (Off_t)SvCUR(PL_linestr));
5388 #if defined(PERLIO_USING_CRLF) && defined(PERL_TEXTMODE_SCRIPTS)
5389                 /* offset may include swallowed CR */
5390                 if (!bof)
5391                     bof = (offset == (Off_t)SvCUR(PL_linestr)+1);
5392 #endif
5393                 if (bof) {
5394                     PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
5395                     s = swallow_bom((U8*)s);
5396                 }
5397             }
5398             if (PL_parser->in_pod) {
5399                 /* Incest with pod. */
5400                 if (    memBEGINPs(s, (STRLEN) (PL_bufend - s), "=cut")
5401                     && !isALPHA(s[4]))
5402                 {
5403                     SvPVCLEAR(PL_linestr);
5404                     PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
5405                     PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
5406                     PL_last_lop = PL_last_uni = NULL;
5407                     PL_parser->in_pod = 0;
5408                 }
5409             }
5410             if (PL_rsfp || PL_parser->filtered)
5411                 incline(s, PL_bufend);
5412         } while (PL_parser->in_pod);
5413         PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = s;
5414         PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
5415         PL_last_lop = PL_last_uni = NULL;
5416         if (CopLINE(PL_curcop) == 1) {
5417             while (s < PL_bufend && isSPACE(*s))
5418                 s++;
5419             if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
5420                 s++;
5421             d = NULL;
5422             if (!PL_in_eval) {
5423                 if (*s == '#' && *(s+1) == '!')
5424                     d = s + 2;
5425 #ifdef ALTERNATE_SHEBANG
5426                 else {
5427                     static char const as[] = ALTERNATE_SHEBANG;
5428                     if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
5429                         d = s + (sizeof(as) - 1);
5430                 }
5431 #endif /* ALTERNATE_SHEBANG */
5432             }
5433             if (d) {
5434                 char *ipath;
5435                 char *ipathend;
5436
5437                 while (isSPACE(*d))
5438                     d++;
5439                 ipath = d;
5440                 while (*d && !isSPACE(*d))
5441                     d++;
5442                 ipathend = d;
5443
5444 #ifdef ARG_ZERO_IS_SCRIPT
5445                 if (ipathend > ipath) {
5446                     /*
5447                      * HP-UX (at least) sets argv[0] to the script name,
5448                      * which makes $^X incorrect.  And Digital UNIX and Linux,
5449                      * at least, set argv[0] to the basename of the Perl
5450                      * interpreter. So, having found "#!", we'll set it right.
5451                      */
5452                     SV* copfilesv = CopFILESV(PL_curcop);
5453                     if (copfilesv) {
5454                         SV * const x =
5455                             GvSV(gv_fetchpvs("\030", GV_ADD|GV_NOTQUAL,
5456                                              SVt_PV)); /* $^X */
5457                         assert(SvPOK(x) || SvGMAGICAL(x));
5458                         if (sv_eq(x, copfilesv)) {
5459                             sv_setpvn(x, ipath, ipathend - ipath);
5460                             SvSETMAGIC(x);
5461                         }
5462                         else {
5463                             STRLEN blen;
5464                             STRLEN llen;
5465                             const char *bstart = SvPV_const(copfilesv, blen);
5466                             const char * const lstart = SvPV_const(x, llen);
5467                             if (llen < blen) {
5468                                 bstart += blen - llen;
5469                                 if (strnEQ(bstart, lstart, llen) &&     bstart[-1] == '/') {
5470                                     sv_setpvn(x, ipath, ipathend - ipath);
5471                                     SvSETMAGIC(x);
5472                                 }
5473                             }
5474                         }
5475                     }
5476                     else {
5477                         /* Anything to do if no copfilesv? */
5478                     }
5479                     TAINT_NOT;  /* $^X is always tainted, but that's OK */
5480                 }
5481 #endif /* ARG_ZERO_IS_SCRIPT */
5482
5483                 /*
5484                  * Look for options.
5485                  */
5486                 d = instr(s,"perl -");
5487                 if (!d) {
5488                     d = instr(s,"perl");
5489 #if defined(DOSISH)
5490                     /* avoid getting into infinite loops when shebang
5491                      * line contains "Perl" rather than "perl" */
5492                     if (!d) {
5493                         for (d = ipathend-4; d >= ipath; --d) {
5494                             if (isALPHA_FOLD_EQ(*d, 'p')
5495                                 && !ibcmp(d, "perl", 4))
5496                             {
5497                                 break;
5498                             }
5499                         }
5500                         if (d < ipath)
5501                             d = NULL;
5502                     }
5503 #endif
5504                 }
5505 #ifdef ALTERNATE_SHEBANG
5506                 /*
5507                  * If the ALTERNATE_SHEBANG on this system starts with a
5508                  * character that can be part of a Perl expression, then if
5509                  * we see it but not "perl", we're probably looking at the
5510                  * start of Perl code, not a request to hand off to some
5511                  * other interpreter.  Similarly, if "perl" is there, but
5512                  * not in the first 'word' of the line, we assume the line
5513                  * contains the start of the Perl program.
5514                  */
5515                 if (d && *s != '#') {
5516                     const char *c = ipath;
5517                     while (*c && !strchr("; \t\r\n\f\v#", *c))
5518                         c++;
5519                     if (c < d)
5520                         d = NULL;       /* "perl" not in first word; ignore */
5521                     else
5522                         *s = '#';       /* Don't try to parse shebang line */
5523                 }
5524 #endif /* ALTERNATE_SHEBANG */
5525                 if (!d
5526                     && *s == '#'
5527                     && ipathend > ipath
5528                     && !PL_minus_c
5529                     && !instr(s,"indir")
5530                     && instr(PL_origargv[0],"perl"))
5531                 {
5532                     dVAR;
5533                     char **newargv;
5534
5535                     *ipathend = '\0';
5536                     s = ipathend + 1;
5537                     while (s < PL_bufend && isSPACE(*s))
5538                         s++;
5539                     if (s < PL_bufend) {
5540                         Newx(newargv,PL_origargc+3,char*);
5541                         newargv[1] = s;
5542                         while (s < PL_bufend && !isSPACE(*s))
5543                             s++;
5544                         *s = '\0';
5545                         Copy(PL_origargv+1, newargv+2, PL_origargc+1, char*);
5546                     }
5547                     else
5548                         newargv = PL_origargv;
5549                     newargv[0] = ipath;
5550                     PERL_FPU_PRE_EXEC
5551                     PerlProc_execv(ipath, EXEC_ARGV_CAST(newargv));
5552                     PERL_FPU_POST_EXEC
5553                     Perl_croak(aTHX_ "Can't exec %s", ipath);
5554                 }
5555                 if (d) {
5556                     while (*d && !isSPACE(*d))
5557                         d++;
5558                     while (SPACE_OR_TAB(*d))
5559                         d++;
5560
5561                     if (*d++ == '-') {
5562                         const bool switches_done = PL_doswitches;
5563                         const U32 oldpdb = PL_perldb;
5564                         const bool oldn = PL_minus_n;
5565                         const bool oldp = PL_minus_p;
5566                         const char *d1 = d;
5567
5568                         do {
5569                             bool baduni = FALSE;
5570                             if (*d1 == 'C') {
5571                                 const char *d2 = d1 + 1;
5572                                 if (parse_unicode_opts((const char **)&d2)
5573                                     != PL_unicode)
5574                                     baduni = TRUE;
5575                             }
5576                             if (baduni || isALPHA_FOLD_EQ(*d1, 'M')) {
5577                                 const char * const m = d1;
5578                                 while (*d1 && !isSPACE(*d1))
5579                                     d1++;
5580                                 Perl_croak(aTHX_ "Too late for \"-%.*s\" option",
5581                                       (int)(d1 - m), m);
5582                             }
5583                             d1 = moreswitches(d1);
5584                         } while (d1);
5585                         if (PL_doswitches && !switches_done) {
5586                             int argc = PL_origargc;
5587                             char **argv = PL_origargv;
5588                             do {
5589                                 argc--,argv++;
5590                             } while (argc && argv[0][0] == '-' && argv[0][1]);
5591                             init_argv_symbols(argc,argv);
5592                         }
5593                         if (   (PERLDB_LINE_OR_SAVESRC && !oldpdb)
5594                             || ((PL_minus_n || PL_minus_p) && !(oldn || oldp)))
5595                               /* if we have already added "LINE: while (<>) {",
5596                                  we must not do it again */
5597                         {
5598                             SvPVCLEAR(PL_linestr);
5599                             PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
5600                             PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
5601                             PL_last_lop = PL_last_uni = NULL;
5602                             PL_preambled = FALSE;
5603                             if (PERLDB_LINE_OR_SAVESRC)
5604                                 (void)gv_fetchfile(PL_origfilename);
5605                             goto retry;
5606                         }
5607                     }
5608                 }
5609             }
5610         }
5611         if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
5612             PL_lex_state = LEX_FORMLINE;
5613             force_next(FORMRBRACK);
5614             TOKEN(';');
5615         }
5616         goto retry;
5617     case '\r':
5618 #ifdef PERL_STRICT_CR
5619         Perl_warn(aTHX_ "Illegal character \\%03o (carriage return)", '\r');
5620         Perl_croak(aTHX_
5621       "\t(Maybe you didn't strip carriage returns after a network transfer?)\n");
5622 #endif
5623     case ' ': case '\t': case '\f': case '\v':
5624         s++;
5625         goto retry;
5626     case '#':
5627     case '\n':
5628         if (PL_lex_state != LEX_NORMAL
5629             || (PL_in_eval && !PL_rsfp && !PL_parser->filtered))
5630         {
5631             const bool in_comment = *s == '#';
5632             if (*s == '#' && s == PL_linestart && PL_in_eval
5633              && !PL_rsfp && !PL_parser->filtered) {
5634                 /* handle eval qq[#line 1 "foo"\n ...] */
5635                 CopLINE_dec(PL_curcop);
5636                 incline(s, PL_bufend);
5637             }
5638             d = s;
5639             while (d < PL_bufend && *d != '\n')
5640                 d++;
5641             if (d < PL_bufend)
5642                 d++;
5643             s = d;
5644             if (in_comment && d == PL_bufend
5645                 && PL_lex_state == LEX_INTERPNORMAL
5646                 && PL_lex_inwhat == OP_SUBST && PL_lex_repl == PL_linestr
5647                 && SvEVALED(PL_lex_repl) && d[-1] == '}') s--;
5648             else
5649                 incline(s, PL_bufend);
5650             if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
5651                 PL_lex_state = LEX_FORMLINE;
5652                 force_next(FORMRBRACK);
5653                 TOKEN(';');
5654             }
5655         }
5656         else {
5657             while (s < PL_bufend && *s != '\n')
5658                 s++;
5659             if (s < PL_bufend)
5660                 {
5661                     s++;
5662                     if (s < PL_bufend)
5663                         incline(s, PL_bufend);
5664                 }
5665         }
5666         goto retry;
5667     case '-':
5668         if (s[1] && isALPHA(s[1]) && !isWORDCHAR(s[2])) {
5669             I32 ftst = 0;
5670             char tmp;
5671
5672             s++;
5673             PL_bufptr = s;
5674             tmp = *s++;
5675
5676             while (s < PL_bufend && SPACE_OR_TAB(*s))
5677                 s++;
5678
5679             if (memBEGINs(s, (STRLEN) (PL_bufend - s), "=>")) {
5680                 s = force_word(PL_bufptr,BAREWORD,FALSE,FALSE);
5681                 DEBUG_T( { printbuf("### Saw unary minus before =>, forcing word %s\n", s); } );
5682                 OPERATOR('-');          /* unary minus */
5683             }
5684             switch (tmp) {
5685             case 'r': ftst = OP_FTEREAD;        break;
5686             case 'w': ftst = OP_FTEWRITE;       break;
5687             case 'x': ftst = OP_FTEEXEC;        break;
5688             case 'o': ftst = OP_FTEOWNED;       break;
5689             case 'R': ftst = OP_FTRREAD;        break;
5690             case 'W': ftst = OP_FTRWRITE;       break;
5691             case 'X': ftst = OP_FTREXEC;        break;
5692             case 'O': ftst = OP_FTROWNED;       break;
5693             case 'e': ftst = OP_FTIS;           break;
5694             case 'z': ftst = OP_FTZERO;         break;
5695             case 's': ftst = OP_FTSIZE;         break;
5696             case 'f': ftst = OP_FTFILE;         break;
5697             case 'd': ftst = OP_FTDIR;          break;
5698             case 'l': ftst = OP_FTLINK;         break;
5699             case 'p': ftst = OP_FTPIPE;         break;
5700             case 'S': ftst = OP_FTSOCK;         break;
5701             case 'u': ftst = OP_FTSUID;         break;
5702             case 'g': ftst = OP_FTSGID;         break;
5703             case 'k': ftst = OP_FTSVTX;         break;
5704             case 'b': ftst = OP_FTBLK;          break;
5705             case 'c': ftst = OP_FTCHR;          break;
5706             case 't': ftst = OP_FTTTY;          break;
5707             case 'T': ftst = OP_FTTEXT;         break;
5708             case 'B': ftst = OP_FTBINARY;       break;
5709             case 'M': case 'A': case 'C':
5710                 gv_fetchpvs("\024", GV_ADD|GV_NOTQUAL, SVt_PV);
5711                 switch (tmp) {
5712                 case 'M': ftst = OP_FTMTIME;    break;
5713                 case 'A': ftst = OP_FTATIME;    break;
5714                 case 'C': ftst = OP_FTCTIME;    break;
5715                 default:                        break;
5716                 }
5717                 break;
5718             default:
5719                 break;
5720             }
5721             if (ftst) {
5722                 PL_last_uni = PL_oldbufptr;
5723                 PL_last_lop_op = (OPCODE)ftst;
5724                 DEBUG_T( { PerlIO_printf(Perl_debug_log,
5725                         "### Saw file test %c\n", (int)tmp);
5726                 } );
5727                 FTST(ftst);
5728             }
5729             else {
5730                 /* Assume it was a minus followed by a one-letter named
5731                  * subroutine call (or a -bareword), then. */
5732                 DEBUG_T( { PerlIO_printf(Perl_debug_log,
5733                         "### '-%c' looked like a file test but was not\n",
5734                         (int) tmp);
5735                 } );
5736                 s = --PL_bufptr;
5737             }
5738         }
5739         {
5740             const char tmp = *s++;
5741             if (*s == tmp) {
5742                 s++;
5743                 if (PL_expect == XOPERATOR)
5744                     TERM(POSTDEC);
5745                 else
5746                     OPERATOR(PREDEC);
5747             }
5748             else if (*s == '>') {
5749                 s++;
5750                 s = skipspace(s);
5751                 if (((*s == '$' || *s == '&') && s[1] == '*')
5752                   ||(*s == '$' && s[1] == '#' && s[2] == '*')
5753                   ||((*s == '@' || *s == '%') && strchr("*[{", s[1]))
5754                   ||(*s == '*' && (s[1] == '*' || s[1] == '{'))
5755                  )
5756                 {
5757                     PL_expect = XPOSTDEREF;
5758                     TOKEN(ARROW);
5759                 }
5760                 if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
5761                     s = force_word(s,METHOD,FALSE,TRUE);
5762                     TOKEN(ARROW);
5763                 }
5764                 else if (*s == '$')
5765                     OPERATOR(ARROW);
5766                 else
5767                     TERM(ARROW);
5768             }
5769             if (PL_expect == XOPERATOR) {
5770                 if (*s == '='
5771                     && !PL_lex_allbrackets
5772                     && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5773                 {
5774                     s--;
5775                     TOKEN(0);
5776                 }
5777                 Aop(OP_SUBTRACT);
5778             }
5779             else {
5780                 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
5781                     check_uni();
5782                 OPERATOR('-');          /* unary minus */
5783             }
5784         }
5785
5786     case '+':
5787         {
5788             const char tmp = *s++;
5789             if (*s == tmp) {
5790                 s++;
5791                 if (PL_expect == XOPERATOR)
5792                     TERM(POSTINC);
5793                 else
5794                     OPERATOR(PREINC);
5795             }
5796             if (PL_expect == XOPERATOR) {
5797                 if (*s == '='
5798                     && !PL_lex_allbrackets
5799                     && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5800                 {
5801                     s--;
5802                     TOKEN(0);
5803                 }
5804                 Aop(OP_ADD);
5805             }
5806             else {
5807                 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
5808                     check_uni();
5809                 OPERATOR('+');
5810             }
5811         }
5812
5813     case '*':
5814         if (PL_expect == XPOSTDEREF) POSTDEREF('*');
5815         if (PL_expect != XOPERATOR) {
5816             s = scan_ident(s, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
5817             PL_expect = XOPERATOR;
5818             force_ident(PL_tokenbuf, '*');
5819             if (!*PL_tokenbuf)
5820                 PREREF('*');
5821             TERM('*');
5822         }
5823         s++;
5824         if (*s == '*') {
5825             s++;
5826             if (*s == '=' && !PL_lex_allbrackets
5827                 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5828             {
5829                 s -= 2;
5830                 TOKEN(0);
5831             }
5832             PWop(OP_POW);
5833         }
5834         if (*s == '='
5835             && !PL_lex_allbrackets
5836             && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5837         {
5838             s--;
5839             TOKEN(0);
5840         }
5841         PL_parser->saw_infix_sigil = 1;
5842         Mop(OP_MULTIPLY);
5843
5844     case '%':
5845     {
5846         if (PL_expect == XOPERATOR) {
5847             if (s[1] == '='
5848                 && !PL_lex_allbrackets
5849                 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5850             {
5851                 TOKEN(0);
5852             }
5853             ++s;
5854             PL_parser->saw_infix_sigil = 1;
5855             Mop(OP_MODULO);
5856         }
5857         else if (PL_expect == XPOSTDEREF) POSTDEREF('%');
5858         PL_tokenbuf[0] = '%';
5859         s = scan_ident(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
5860         pl_yylval.ival = 0;
5861         if (!PL_tokenbuf[1]) {
5862             PREREF('%');
5863         }
5864         if (   (PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
5865             && intuit_more(s, PL_bufend)) {
5866             if (*s == '[')
5867                 PL_tokenbuf[0] = '@';
5868         }
5869         PL_expect = XOPERATOR;
5870         force_ident_maybe_lex('%');
5871         TERM('%');
5872     }
5873     case '^':
5874         d = s;
5875         bof = FEATURE_BITWISE_IS_ENABLED;
5876         if (bof && s[1] == '.')
5877             s++;
5878         if (!PL_lex_allbrackets && PL_lex_fakeeof >=
5879                 (s[1] == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_BITWISE))
5880         {
5881             s = d;
5882             TOKEN(0);
5883         }
5884         s++;
5885         BOop(bof ? d == s-2 ? OP_SBIT_XOR : OP_NBIT_XOR : OP_BIT_XOR);
5886     case '[':
5887         if (PL_lex_brackets > 100)
5888             Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
5889         PL_lex_brackstack[PL_lex_brackets++] = 0;
5890         PL_lex_allbrackets++;
5891         {
5892             const char tmp = *s++;
5893             OPERATOR(tmp);
5894         }
5895     case '~':
5896         if (s[1] == '~'
5897             && (PL_expect == XOPERATOR || PL_expect == XTERMORDORDOR))
5898         {
5899             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
5900                 TOKEN(0);
5901             s += 2;
5902             Perl_ck_warner_d(aTHX_
5903                 packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
5904                 "Smartmatch is experimental");
5905             Eop(OP_SMARTMATCH);
5906         }
5907         s++;
5908         if ((bof = FEATURE_BITWISE_IS_ENABLED) && *s == '.') {
5909             s++;
5910             BCop(OP_SCOMPLEMENT);
5911         }
5912         BCop(bof ? OP_NCOMPLEMENT : OP_COMPLEMENT);
5913     case ',':
5914         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMMA)
5915             TOKEN(0);
5916         s++;
5917         OPERATOR(',');
5918     case ':':
5919         if (s[1] == ':') {
5920             len = 0;
5921             goto just_a_word_zero_gv;
5922         }
5923         s++;
5924         {
5925         OP *attrs;
5926
5927         switch (PL_expect) {
5928         case XOPERATOR:
5929             if (!PL_in_my || PL_lex_state != LEX_NORMAL)
5930                 break;
5931             PL_bufptr = s;      /* update in case we back off */
5932             if (*s == '=') {
5933                 Perl_croak(aTHX_
5934                            "Use of := for an empty attribute list is not allowed");
5935             }
5936             goto grabattrs;
5937         case XATTRBLOCK:
5938             PL_expect = XBLOCK;
5939             goto grabattrs;
5940         case XATTRTERM:
5941             PL_expect = XTERMBLOCK;
5942          grabattrs:
5943             /* NB: as well as parsing normal attributes, we also end up
5944              * here if there is something looking like attributes
5945              * following a signature (which is illegal, but used to be
5946              * legal in 5.20..5.26). If the latter, we still parse the
5947              * attributes so that error messages(s) are less confusing,
5948              * but ignore them (parser->sig_seen).
5949              */
5950             s = skipspace(s);
5951             attrs = NULL;
5952             while (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
5953                 bool sig = PL_parser->sig_seen;
5954                 I32 tmp;
5955                 SV *sv;
5956                 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
5957                 if (isLOWER(*s) && (tmp = keyword(PL_tokenbuf, len, 0))) {
5958                     if (tmp < 0) tmp = -tmp;
5959                     switch (tmp) {
5960                     case KEY_or:
5961                     case KEY_and:
5962                     case KEY_for:
5963                     case KEY_foreach:
5964                     case KEY_unless:
5965                     case KEY_if:
5966                     case KEY_while:
5967                     case KEY_until:
5968                         goto got_attrs;
5969                     default:
5970                         break;
5971                     }
5972                 }
5973                 sv = newSVpvn_flags(s, len, UTF ? SVf_UTF8 : 0);
5974                 if (*d == '(') {
5975                     d = scan_str(d,TRUE,TRUE,FALSE,NULL);
5976                     if (!d) {
5977                         if (attrs)
5978                             op_free(attrs);
5979                         sv_free(sv);
5980                         Perl_croak(aTHX_ "Unterminated attribute parameter in attribute list");
5981                     }
5982                     COPLINE_SET_FROM_MULTI_END;
5983                 }
5984                 if (PL_lex_stuff) {
5985                     sv_catsv(sv, PL_lex_stuff);
5986                     attrs = op_append_elem(OP_LIST, attrs,
5987                                         newSVOP(OP_CONST, 0, sv));
5988                     SvREFCNT_dec_NN(PL_lex_stuff);
5989                     PL_lex_stuff = NULL;
5990                 }
5991                 else {
5992                     /* NOTE: any CV attrs applied here need to be part of
5993                        the CVf_BUILTIN_ATTRS define in cv.h! */
5994                     if (!PL_in_my && memEQs(SvPVX(sv), len, "lvalue")) {
5995                         sv_free(sv);
5996                         if (!sig)
5997                             CvLVALUE_on(PL_compcv);
5998                     }
5999                     else if (!PL_in_my && memEQs(SvPVX(sv), len, "method")) {
6000                         sv_free(sv);
6001                         if (!sig)
6002                             CvMETHOD_on(PL_compcv);
6003                     }
6004                     else if (!PL_in_my && memEQs(SvPVX(sv), len, "const"))
6005                     {
6006                         sv_free(sv);
6007                         if (!sig) {
6008                             Perl_ck_warner_d(aTHX_
6009                                 packWARN(WARN_EXPERIMENTAL__CONST_ATTR),
6010                                ":const is experimental"
6011                             );
6012                             CvANONCONST_on(PL_compcv);
6013                             if (!CvANON(PL_compcv))
6014                                 yyerror(":const is not permitted on named "
6015                                         "subroutines");
6016                         }
6017                     }
6018                     /* After we've set the flags, it could be argued that
6019                        we don't need to do the attributes.pm-based setting
6020                        process, and shouldn't bother appending recognized
6021                        flags.  To experiment with that, uncomment the
6022                        following "else".  (Note that's already been
6023                        uncommented.  That keeps the above-applied built-in
6024                        attributes from being intercepted (and possibly
6025                        rejected) by a package's attribute routines, but is
6026                        justified by the performance win for the common case
6027                        of applying only built-in attributes.) */
6028                     else
6029                         attrs = op_append_elem(OP_LIST, attrs,
6030                                             newSVOP(OP_CONST, 0,
6031                                                     sv));
6032                 }
6033                 s = skipspace(d);
6034                 if (*s == ':' && s[1] != ':')
6035                     s = skipspace(s+1);
6036                 else if (s == d)
6037                     break;      /* require real whitespace or :'s */
6038                 /* XXX losing whitespace on sequential attributes here */
6039             }
6040             {
6041                 if (*s != ';'
6042                     && *s != '}'
6043                     && !(PL_expect == XOPERATOR
6044                          ? (*s == '=' ||  *s == ')')
6045                          : (*s == '{' ||  *s == '(')))
6046                 {
6047                     const char q = ((*s == '\'') ? '"' : '\'');
6048                     /* If here for an expression, and parsed no attrs, back
6049                        off. */
6050                     if (PL_expect == XOPERATOR && !attrs) {
6051                         s = PL_bufptr;
6052                         break;
6053                     }
6054                     /* MUST advance bufptr here to avoid bogus "at end of line"
6055                        context messages from yyerror().
6056                     */
6057                     PL_bufptr = s;
6058                     yyerror( (const char *)
6059                              (*s
6060                               ? Perl_form(aTHX_ "Invalid separator character "
6061                                           "%c%c%c in attribute list", q, *s, q)
6062                               : "Unterminated attribute list" ) );
6063                     if (attrs)
6064                         op_free(attrs);
6065                     OPERATOR(':');
6066                 }
6067             }
6068         got_attrs:
6069             if (PL_parser->sig_seen) {
6070                 /* see comment about about sig_seen and parser error
6071                  * handling */
6072                 if (attrs)
6073                     op_free(attrs);
6074                 Perl_croak(aTHX_ "Subroutine attributes must come "
6075                                  "before the signature");
6076                 }
6077             if (attrs) {
6078                 NEXTVAL_NEXTTOKE.opval = attrs;
6079                 force_next(THING);
6080             }
6081             TOKEN(COLONATTR);
6082         }
6083         }
6084         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_CLOSING) {
6085             s--;
6086             TOKEN(0);
6087         }
6088         PL_lex_allbrackets--;
6089         OPERATOR(':');
6090     case '(':
6091         s++;
6092         if (PL_last_lop == PL_oldoldbufptr || PL_last_uni == PL_oldoldbufptr)
6093             PL_oldbufptr = PL_oldoldbufptr;             /* allow print(STDOUT 123) */
6094         else
6095             PL_expect = XTERM;
6096         s = skipspace(s);
6097         PL_lex_allbrackets++;
6098         TOKEN('(');
6099     case ';':
6100         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
6101             TOKEN(0);
6102         CLINE;
6103         s++;
6104         PL_expect = XSTATE;
6105         TOKEN(';');
6106     case ')':
6107         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_CLOSING)
6108             TOKEN(0);
6109         s++;
6110         PL_lex_allbrackets--;
6111         s = skipspace(s);
6112         if (*s == '{')
6113             PREBLOCK(')');
6114         TERM(')');
6115     case ']':
6116         if (PL_lex_brackets && PL_lex_brackstack[PL_lex_brackets-1] == XFAKEEOF)
6117             TOKEN(0);
6118         s++;
6119         if (PL_lex_brackets <= 0)
6120             /* diag_listed_as: Unmatched right %s bracket */
6121             yyerror("Unmatched right square bracket");
6122         else
6123             --PL_lex_brackets;
6124         PL_lex_allbrackets--;
6125         if (PL_lex_state == LEX_INTERPNORMAL) {
6126             if (PL_lex_brackets == 0) {
6127                 if (*s == '-' && s[1] == '>')
6128                     PL_lex_state = LEX_INTERPENDMAYBE;
6129                 else if (*s != '[' && *s != '{')
6130                     PL_lex_state = LEX_INTERPEND;
6131             }
6132         }
6133         TERM(']');
6134     case '{':
6135         s++;
6136       leftbracket:
6137         if (PL_lex_brackets > 100) {
6138             Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
6139         }
6140         switch (PL_expect) {
6141         case XTERM:
6142         case XTERMORDORDOR:
6143             PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
6144             PL_lex_allbrackets++;
6145             OPERATOR(HASHBRACK);
6146         case XOPERATOR:
6147             while (s < PL_bufend && SPACE_OR_TAB(*s))
6148                 s++;
6149             d = s;
6150             PL_tokenbuf[0] = '\0';
6151             if (d < PL_bufend && *d == '-') {
6152                 PL_tokenbuf[0] = '-';
6153                 d++;
6154                 while (d < PL_bufend && SPACE_OR_TAB(*d))
6155                     d++;
6156             }
6157             if (d < PL_bufend && isIDFIRST_lazy_if_safe(d, PL_bufend, UTF)) {
6158                 d = scan_word(d, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
6159                               FALSE, &len);
6160                 while (d < PL_bufend && SPACE_OR_TAB(*d))
6161                     d++;
6162                 if (*d == '}') {
6163                     const char minus = (PL_tokenbuf[0] == '-');
6164                     s = force_word(s + minus, BAREWORD, FALSE, TRUE);
6165                     if (minus)
6166                         force_next('-');
6167                 }
6168             }
6169             /* FALLTHROUGH */
6170         case XATTRTERM:
6171         case XTERMBLOCK:
6172             PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
6173             PL_lex_allbrackets++;
6174             PL_expect = XSTATE;
6175             break;
6176         case XATTRBLOCK:
6177         case XBLOCK:
6178             PL_lex_brackstack[PL_lex_brackets++] = XSTATE;
6179             PL_lex_allbrackets++;
6180             PL_expect = XSTATE;
6181             break;
6182         case XBLOCKTERM:
6183             PL_lex_brackstack[PL_lex_brackets++] = XTERM;
6184             PL_lex_allbrackets++;
6185             PL_expect = XSTATE;
6186             break;
6187         default: {
6188                 const char *t;
6189                 if (PL_oldoldbufptr == PL_last_lop)
6190                     PL_lex_brackstack[PL_lex_brackets++] = XTERM;
6191                 else
6192                     PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
6193                 PL_lex_allbrackets++;
6194                 s = skipspace(s);
6195                 if (*s == '}') {
6196                     if (PL_expect == XREF && PL_lex_state == LEX_INTERPNORMAL) {
6197                         PL_expect = XTERM;
6198                         /* This hack is to get the ${} in the message. */
6199                         PL_bufptr = s+1;
6200                         yyerror("syntax error");
6201                         break;
6202                     }
6203                     OPERATOR(HASHBRACK);
6204                 }
6205                 if (PL_expect == XREF && PL_oldoldbufptr != PL_last_lop) {
6206                     /* ${...} or @{...} etc., but not print {...}
6207                      * Skip the disambiguation and treat this as a block.
6208                      */
6209                     goto block_expectation;
6210                 }
6211                 /* This hack serves to disambiguate a pair of curlies
6212                  * as being a block or an anon hash.  Normally, expectation
6213                  * determines that, but in cases where we're not in a
6214                  * position to expect anything in particular (like inside
6215                  * eval"") we have to resolve the ambiguity.  This code
6216                  * covers the case where the first term in the curlies is a
6217                  * quoted string.  Most other cases need to be explicitly
6218                  * disambiguated by prepending a "+" before the opening
6219                  * curly in order to force resolution as an anon hash.
6220                  *
6221                  * XXX should probably propagate the outer expectation
6222                  * into eval"" to rely less on this hack, but that could
6223                  * potentially break current behavior of eval"".
6224                  * GSAR 97-07-21
6225                  */
6226                 t = s;
6227                 if (*s == '\'' || *s == '"' || *s == '`') {
6228                     /* common case: get past first string, handling escapes */
6229                     for (t++; t < PL_bufend && *t != *s;)
6230                         if (*t++ == '\\')
6231                             t++;
6232                     t++;
6233                 }
6234                 else if (*s == 'q') {
6235                     if (++t < PL_bufend
6236                         && (!isWORDCHAR(*t)
6237                             || ((*t == 'q' || *t == 'x') && ++t < PL_bufend
6238                                 && !isWORDCHAR(*t))))
6239                     {
6240                         /* skip q//-like construct */
6241                         const char *tmps;
6242                         char open, close, term;
6243                         I32 brackets = 1;
6244
6245                         while (t < PL_bufend && isSPACE(*t))
6246                             t++;
6247                         /* check for q => */
6248                         if (t+1 < PL_bufend && t[0] == '=' && t[1] == '>') {
6249                             OPERATOR(HASHBRACK);
6250                         }
6251                         term = *t;
6252                         open = term;
6253                         if (term && (tmps = strchr("([{< )]}> )]}>",term)))
6254                             term = tmps[5];
6255                         close = term;
6256                         if (open == close)
6257                             for (t++; t < PL_bufend; t++) {
6258                                 if (*t == '\\' && t+1 < PL_bufend && open != '\\')
6259                                     t++;
6260                                 else if (*t == open)
6261                                     break;
6262                             }
6263                         else {
6264                             for (t++; t < PL_bufend; t++) {
6265                                 if (*t == '\\' && t+1 < PL_bufend)
6266                                     t++;
6267                                 else if (*t == close && --brackets <= 0)
6268                                     break;
6269                                 else if (*t == open)
6270                                     brackets++;
6271                             }
6272                         }
6273                         t++;
6274                     }
6275                     else
6276                         /* skip plain q word */
6277                         while (   t < PL_bufend
6278                                && isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF))
6279                         {
6280                             t += UTF ? UTF8SKIP(t) : 1;
6281                         }
6282                 }
6283                 else if (isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF)) {
6284                     t += UTF ? UTF8SKIP(t) : 1;
6285                     while (   t < PL_bufend
6286                            && isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF))
6287                     {
6288                         t += UTF ? UTF8SKIP(t) : 1;
6289                     }
6290                 }
6291                 while (t < PL_bufend && isSPACE(*t))
6292                     t++;
6293                 /* if comma follows first term, call it an anon hash */
6294                 /* XXX it could be a comma expression with loop modifiers */
6295                 if (t < PL_bufend && ((*t == ',' && (*s == 'q' || !isLOWER(*s)))
6296                                    || (*t == '=' && t[1] == '>')))
6297                     OPERATOR(HASHBRACK);
6298                 if (PL_expect == XREF)
6299                 {
6300                   block_expectation:
6301                     /* If there is an opening brace or 'sub:', treat it
6302                        as a term to make ${{...}}{k} and &{sub:attr...}
6303                        dwim.  Otherwise, treat it as a statement, so
6304                        map {no strict; ...} works.
6305                      */
6306                     s = skipspace(s);
6307                     if (*s == '{') {
6308                         PL_expect = XTERM;
6309                         break;
6310                     }
6311                     if (memBEGINs(s, (STRLEN) (PL_bufend - s), "sub")) {
6312                         PL_bufptr = s;
6313                         d = s + 3;
6314                         d = skipspace(d);
6315                         s = PL_bufptr;
6316                         if (*d == ':') {
6317                             PL_expect = XTERM;
6318                             break;
6319                         }
6320                     }
6321                     PL_expect = XSTATE;
6322                 }
6323                 else {
6324                     PL_lex_brackstack[PL_lex_brackets-1] = XSTATE;
6325                     PL_expect = XSTATE;
6326                 }
6327             }
6328             break;
6329         }
6330         pl_yylval.ival = CopLINE(PL_curcop);
6331         PL_copline = NOLINE;   /* invalidate current command line number */
6332         TOKEN(formbrack ? '=' : '{');
6333     case '}':
6334         if (PL_lex_brackets && PL_lex_brackstack[PL_lex_brackets-1] == XFAKEEOF)
6335             TOKEN(0);
6336       rightbracket:
6337         assert(s != PL_bufend);
6338         s++;
6339         if (PL_lex_brackets <= 0)
6340             /* diag_listed_as: Unmatched right %s bracket */
6341             yyerror("Unmatched right curly bracket");
6342         else
6343             PL_expect = (expectation)PL_lex_brackstack[--PL_lex_brackets];
6344         PL_lex_allbrackets--;
6345         if (PL_lex_state == LEX_INTERPNORMAL) {
6346             if (PL_lex_brackets == 0) {
6347                 if (PL_expect & XFAKEBRACK) {
6348                     PL_expect &= XENUMMASK;
6349                     PL_lex_state = LEX_INTERPEND;
6350                     PL_bufptr = s;
6351                     return yylex();     /* ignore fake brackets */
6352                 }
6353                 if (PL_lex_inwhat == OP_SUBST && PL_lex_repl == PL_linestr
6354                  && SvEVALED(PL_lex_repl))
6355                     PL_lex_state = LEX_INTERPEND;
6356                 else if (*s == '-' && s[1] == '>')
6357                     PL_lex_state = LEX_INTERPENDMAYBE;
6358                 else if (*s != '[' && *s != '{')
6359                     PL_lex_state = LEX_INTERPEND;
6360             }
6361         }
6362         if (PL_expect & XFAKEBRACK) {
6363             PL_expect &= XENUMMASK;
6364             PL_bufptr = s;
6365             return yylex();             /* ignore fake brackets */
6366         }
6367         force_next(formbrack ? '.' : '}');
6368         if (formbrack) LEAVE_with_name("lex_format");
6369         if (formbrack == 2) { /* means . where arguments were expected */
6370             force_next(';');
6371             TOKEN(FORMRBRACK);
6372         }
6373         TOKEN(';');
6374     case '&':
6375         if (PL_expect == XPOSTDEREF) POSTDEREF('&');
6376         s++;
6377         if (*s++ == '&') {
6378             if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6379                     (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_LOGIC)) {
6380                 s -= 2;
6381                 TOKEN(0);
6382             }
6383             AOPERATOR(ANDAND);
6384         }
6385         s--;
6386         if (PL_expect == XOPERATOR) {
6387             if (   PL_bufptr == PL_linestart
6388                 && ckWARN(WARN_SEMICOLON)
6389                 && isIDFIRST_lazy_if_safe(s, PL_bufend, UTF))
6390             {
6391                 CopLINE_dec(PL_curcop);
6392                 Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
6393                 CopLINE_inc(PL_curcop);
6394             }
6395             d = s;
6396             if ((bof = FEATURE_BITWISE_IS_ENABLED) && *s == '.')
6397                 s++;
6398             if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6399                     (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_BITWISE)) {
6400                 s = d;
6401                 s--;
6402                 TOKEN(0);
6403             }
6404             if (d == s) {
6405                 PL_parser->saw_infix_sigil = 1;
6406                 BAop(bof ? OP_NBIT_AND : OP_BIT_AND);
6407             }
6408             else
6409                 BAop(OP_SBIT_AND);
6410         }
6411
6412         PL_tokenbuf[0] = '&';
6413         s = scan_ident(s - 1, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, TRUE);
6414         pl_yylval.ival = (OPpENTERSUB_AMPER<<8);
6415         if (PL_tokenbuf[1]) {
6416             force_ident_maybe_lex('&');
6417         }
6418         else
6419             PREREF('&');
6420         TERM('&');
6421
6422     case '|':
6423         s++;
6424         if (*s++ == '|') {
6425             if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6426                     (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_LOGIC)) {
6427                 s -= 2;
6428                 TOKEN(0);
6429             }
6430             AOPERATOR(OROR);
6431         }
6432         s--;
6433         d = s;
6434         if ((bof = FEATURE_BITWISE_IS_ENABLED) && *s == '.')
6435             s++;
6436         if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6437                 (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_BITWISE)) {
6438             s = d - 1;
6439             TOKEN(0);
6440         }
6441         BOop(bof ? s == d ? OP_NBIT_OR : OP_SBIT_OR : OP_BIT_OR);
6442     case '=':
6443         s++;
6444         {
6445             const char tmp = *s++;
6446             if (tmp == '=') {
6447                 if (   (s == PL_linestart+2 || s[-3] == '\n')
6448                     && memBEGINs(s, (STRLEN) (PL_bufend - s), "====="))
6449                 {
6450                     s = vcs_conflict_marker(s + 5);
6451                     goto retry;
6452                 }
6453                 if (!PL_lex_allbrackets
6454                     && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6455                 {
6456                     s -= 2;
6457                     TOKEN(0);
6458                 }
6459                 Eop(OP_EQ);
6460             }
6461             if (tmp == '>') {
6462                 if (!PL_lex_allbrackets
6463                     && PL_lex_fakeeof >= LEX_FAKEEOF_COMMA)
6464                 {
6465                     s -= 2;
6466                     TOKEN(0);
6467                 }
6468                 OPERATOR(',');
6469             }
6470             if (tmp == '~')
6471                 PMop(OP_MATCH);
6472             if (tmp && isSPACE(*s) && ckWARN(WARN_SYNTAX)
6473                 && strchr("+-*/%.^&|<",tmp))
6474                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6475                             "Reversed %c= operator",(int)tmp);
6476             s--;
6477             if (PL_expect == XSTATE
6478                 && isALPHA(tmp)
6479                 && (s == PL_linestart+1 || s[-2] == '\n') )
6480             {
6481                 if (   (PL_in_eval && !PL_rsfp && !PL_parser->filtered)
6482                     || PL_lex_state != LEX_NORMAL)
6483                 {
6484                     d = PL_bufend;
6485                     while (s < d) {
6486                         if (*s++ == '\n') {
6487                             incline(s, PL_bufend);
6488                             if (memBEGINs(s, (STRLEN) (PL_bufend - s), "=cut"))
6489                             {
6490                                 s = (char *) memchr(s,'\n', d - s);
6491                                 if (s)
6492                                     s++;
6493                                 else
6494                                     s = d;
6495                                 incline(s, PL_bufend);
6496                                 goto retry;
6497                             }
6498                         }
6499                     }
6500                     goto retry;
6501                 }
6502                 s = PL_bufend;
6503                 PL_parser->in_pod = 1;
6504                 goto retry;
6505             }
6506         }
6507         if (PL_expect == XBLOCK) {
6508             const char *t = s;
6509 #ifdef PERL_STRICT_CR
6510             while (SPACE_OR_TAB(*t))
6511 #else
6512             while (SPACE_OR_TAB(*t) || *t == '\r')
6513 #endif
6514                 t++;
6515             if (*t == '\n' || *t == '#') {
6516                 formbrack = 1;
6517                 ENTER_with_name("lex_format");
6518                 SAVEI8(PL_parser->form_lex_state);
6519                 SAVEI32(PL_lex_formbrack);
6520                 PL_parser->form_lex_state = PL_lex_state;
6521                 PL_lex_formbrack = PL_lex_brackets + 1;
6522                 PL_parser->sub_error_count = PL_error_count;
6523                 goto leftbracket;
6524             }
6525         }
6526         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN) {
6527             s--;
6528             TOKEN(0);
6529         }
6530         pl_yylval.ival = 0;
6531         OPERATOR(ASSIGNOP);
6532     case '!':
6533         s++;
6534         {
6535             const char tmp = *s++;
6536             if (tmp == '=') {
6537                 /* was this !=~ where !~ was meant?
6538                  * warn on m:!=~\s+([/?]|[msy]\W|tr\W): */
6539
6540                 if (*s == '~' && ckWARN(WARN_SYNTAX)) {
6541                     const char *t = s+1;
6542
6543                     while (t < PL_bufend && isSPACE(*t))
6544                         ++t;
6545
6546                     if (*t == '/' || *t == '?'
6547                         || ((*t == 'm' || *t == 's' || *t == 'y')
6548                             && !isWORDCHAR(t[1]))
6549                         || (*t == 't' && t[1] == 'r' && !isWORDCHAR(t[2])))
6550                         Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6551                                     "!=~ should be !~");
6552                 }
6553                 if (!PL_lex_allbrackets
6554                     && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6555                 {
6556                     s -= 2;
6557                     TOKEN(0);
6558                 }
6559                 Eop(OP_NE);
6560             }
6561             if (tmp == '~')
6562                 PMop(OP_NOT);
6563         }
6564         s--;
6565         OPERATOR('!');
6566     case '<':
6567         if (PL_expect != XOPERATOR) {
6568             if (s[1] != '<' && !memchr(s,'>', PL_bufend - s))
6569                 check_uni();
6570             if (s[1] == '<' && s[2] != '>') {
6571                 if (   (s == PL_linestart || s[-1] == '\n')
6572                     && memBEGINs(s+2, (STRLEN) (PL_bufend - (s+2)), "<<<<<"))
6573                 {
6574                     s = vcs_conflict_marker(s + 7);
6575                     goto retry;
6576                 }
6577                 s = scan_heredoc(s);
6578             }
6579             else
6580                 s = scan_inputsymbol(s);
6581             PL_expect = XOPERATOR;
6582             TOKEN(sublex_start());
6583         }
6584         s++;
6585         {
6586             char tmp = *s++;
6587             if (tmp == '<') {
6588                 if (   (s == PL_linestart+2 || s[-3] == '\n')
6589                     && memBEGINs(s, (STRLEN) (PL_bufend - s), "<<<<<"))
6590                 {
6591                     s = vcs_conflict_marker(s + 5);
6592                     goto retry;
6593                 }
6594                 if (*s == '=' && !PL_lex_allbrackets
6595                     && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
6596                 {
6597                     s -= 2;
6598                     TOKEN(0);
6599                 }
6600                 SHop(OP_LEFT_SHIFT);
6601             }
6602             if (tmp == '=') {
6603                 tmp = *s++;
6604                 if (tmp == '>') {
6605                     if (!PL_lex_allbrackets
6606                         && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6607                     {
6608                         s -= 3;
6609                         TOKEN(0);
6610                     }
6611                     Eop(OP_NCMP);
6612                 }
6613                 s--;
6614                 if (!PL_lex_allbrackets
6615                     && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6616                 {
6617                     s -= 2;
6618                     TOKEN(0);
6619                 }
6620                 Rop(OP_LE);
6621             }
6622         }
6623         s--;
6624         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE) {
6625             s--;
6626             TOKEN(0);
6627         }
6628         Rop(OP_LT);
6629     case '>':
6630         s++;
6631         {
6632             const char tmp = *s++;
6633             if (tmp == '>') {
6634                 if (   (s == PL_linestart+2 || s[-3] == '\n')
6635                     && memBEGINs(s, (STRLEN) (PL_bufend - s), ">>>>>"))
6636                 {
6637                     s = vcs_conflict_marker(s + 5);
6638                     goto retry;
6639                 }
6640                 if (*s == '=' && !PL_lex_allbrackets
6641                     && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
6642                 {
6643                     s -= 2;
6644                     TOKEN(0);
6645                 }
6646                 SHop(OP_RIGHT_SHIFT);
6647             }
6648             else if (tmp == '=') {
6649                 if (!PL_lex_allbrackets
6650                     && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6651                 {
6652                     s -= 2;
6653                     TOKEN(0);
6654                 }
6655                 Rop(OP_GE);
6656             }
6657         }
6658         s--;
6659         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE) {
6660             s--;
6661             TOKEN(0);
6662         }
6663         Rop(OP_GT);
6664
6665     case '$':
6666         CLINE;
6667
6668         if (PL_expect == XPOSTDEREF) {
6669             if (s[1] == '#') {
6670                 s++;
6671                 POSTDEREF(DOLSHARP);
6672             }
6673             POSTDEREF('$');
6674         }
6675
6676         if (   s[1] == '#'
6677             && (   isIDFIRST_lazy_if_safe(s+2, PL_bufend, UTF)
6678                 || strchr("{$:+-@", s[2])))
6679         {
6680             PL_tokenbuf[0] = '@';
6681             s = scan_ident(s + 1, PL_tokenbuf + 1,
6682                            sizeof PL_tokenbuf - 1, FALSE);
6683             if (PL_expect == XOPERATOR) {
6684                 d = s;
6685                 if (PL_bufptr > s) {
6686                     d = PL_bufptr-1;
6687                     PL_bufptr = PL_oldbufptr;
6688                 }
6689                 no_op("Array length", d);
6690             }
6691             if (!PL_tokenbuf[1])
6692                 PREREF(DOLSHARP);
6693             PL_expect = XOPERATOR;
6694             force_ident_maybe_lex('#');
6695             TOKEN(DOLSHARP);
6696         }
6697
6698         PL_tokenbuf[0] = '$';
6699         s = scan_ident(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
6700         if (PL_expect == XOPERATOR) {
6701             d = s;
6702             if (PL_bufptr > s) {
6703                 d = PL_bufptr-1;
6704                 PL_bufptr = PL_oldbufptr;
6705             }
6706             no_op("Scalar", d);
6707         }
6708         if (!PL_tokenbuf[1]) {
6709             if (s == PL_bufend)
6710                 yyerror("Final $ should be \\$ or $name");
6711             PREREF('$');
6712         }
6713
6714         d = s;
6715         {
6716             const char tmp = *s;
6717             if (PL_lex_state == LEX_NORMAL || PL_lex_brackets)
6718                 s = skipspace(s);
6719
6720             if (   (PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
6721                 && intuit_more(s, PL_bufend)) {
6722                 if (*s == '[') {
6723                     PL_tokenbuf[0] = '@';
6724                     if (ckWARN(WARN_SYNTAX)) {
6725                         char *t = s+1;
6726
6727                         while (   isSPACE(*t)
6728                                || isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF)
6729                                || *t == '$')
6730                         {
6731                             t += UTF ? UTF8SKIP(t) : 1;
6732                         }
6733                         if (*t++ == ',') {
6734                             PL_bufptr = skipspace(PL_bufptr); /* XXX can realloc */
6735                             while (t < PL_bufend && *t != ']')
6736                                 t++;
6737                             Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6738                                         "Multidimensional syntax %" UTF8f " not supported",
6739                                         UTF8fARG(UTF,(int)((t - PL_bufptr) + 1), PL_bufptr));
6740                         }
6741                     }
6742                 }
6743                 else if (*s == '{') {
6744                     char *t;
6745                     PL_tokenbuf[0] = '%';
6746                     if (    strEQ(PL_tokenbuf+1, "SIG")
6747                         && ckWARN(WARN_SYNTAX)
6748                         && (t = (char *) memchr(s, '}', PL_bufend - s))
6749                         && (t = (char *) memchr(t, '=', PL_bufend - t)))
6750                     {
6751                         char tmpbuf[sizeof PL_tokenbuf];
6752                         do {
6753                             t++;
6754                         } while (isSPACE(*t));
6755                         if (isIDFIRST_lazy_if_safe(t, PL_bufend, UTF)) {
6756                             STRLEN len;
6757                             t = scan_word(t, tmpbuf, sizeof tmpbuf, TRUE,
6758                                             &len);
6759                             while (isSPACE(*t))
6760                                 t++;
6761                             if (  *t == ';'
6762                                 && get_cvn_flags(tmpbuf, len, UTF
6763                                                                 ? SVf_UTF8
6764                                                                 : 0))
6765                             {
6766                                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6767                                     "You need to quote \"%" UTF8f "\"",
6768                                         UTF8fARG(UTF, len, tmpbuf));
6769                             }
6770                         }
6771                     }
6772                 }
6773             }
6774
6775             PL_expect = XOPERATOR;
6776             if (PL_lex_state == LEX_NORMAL && isSPACE((char)tmp)) {
6777                 const bool islop = (PL_last_lop == PL_oldoldbufptr);
6778                 if (!islop || PL_last_lop_op == OP_GREPSTART)
6779                     PL_expect = XOPERATOR;
6780                 else if (strchr("$@\"'`q", *s))
6781                     PL_expect = XTERM;          /* e.g. print $fh "foo" */
6782                 else if (   strchr("&*<%", *s)
6783                          && isIDFIRST_lazy_if_safe(s+1, PL_bufend, UTF))
6784                 {
6785                     PL_expect = XTERM;          /* e.g. print $fh &sub */
6786                 }
6787                 else if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
6788                     char tmpbuf[sizeof PL_tokenbuf];
6789                     int t2;
6790                     scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
6791                     if ((t2 = keyword(tmpbuf, len, 0))) {
6792                         /* binary operators exclude handle interpretations */
6793                         switch (t2) {
6794                         case -KEY_x:
6795                         case -KEY_eq:
6796                         case -KEY_ne:
6797                         case -KEY_gt:
6798                         case -KEY_lt:
6799                         case -KEY_ge:
6800                         case -KEY_le:
6801                         case -KEY_cmp:
6802                             break;
6803                         default:
6804                             PL_expect = XTERM;  /* e.g. print $fh length() */
6805                             break;
6806                         }
6807                     }
6808                     else {
6809                         PL_expect = XTERM;      /* e.g. print $fh subr() */
6810                     }
6811                 }
6812                 else if (isDIGIT(*s))
6813                     PL_expect = XTERM;          /* e.g. print $fh 3 */
6814                 else if (*s == '.' && isDIGIT(s[1]))
6815                     PL_expect = XTERM;          /* e.g. print $fh .3 */
6816                 else if ((*s == '?' || *s == '-' || *s == '+')
6817                          && !isSPACE(s[1]) && s[1] != '=')
6818                     PL_expect = XTERM;          /* e.g. print $fh -1 */
6819                 else if (*s == '/' && !isSPACE(s[1]) && s[1] != '='
6820                          && s[1] != '/')
6821                     PL_expect = XTERM;          /* e.g. print $fh /.../
6822                                                    XXX except DORDOR operator
6823                                                 */
6824                 else if (*s == '<' && s[1] == '<' && !isSPACE(s[2])
6825                          && s[2] != '=')
6826                     PL_expect = XTERM;          /* print $fh <<"EOF" */
6827             }
6828         }
6829         force_ident_maybe_lex('$');
6830         TOKEN('$');
6831
6832     case '@':
6833         if (PL_expect == XPOSTDEREF)
6834             POSTDEREF('@');
6835         PL_tokenbuf[0] = '@';
6836         s = scan_ident(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
6837         if (PL_expect == XOPERATOR) {
6838             d = s;
6839             if (PL_bufptr > s) {
6840                 d = PL_bufptr-1;
6841                 PL_bufptr = PL_oldbufptr;
6842             }
6843             no_op("Array", d);
6844         }
6845         pl_yylval.ival = 0;
6846         if (!PL_tokenbuf[1]) {
6847             PREREF('@');
6848         }
6849         if (PL_lex_state == LEX_NORMAL)
6850             s = skipspace(s);
6851         if (   (PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
6852             && intuit_more(s, PL_bufend))
6853         {
6854             if (*s == '{')
6855                 PL_tokenbuf[0] = '%';
6856
6857             /* Warn about @ where they meant $. */
6858             if (*s == '[' || *s == '{') {
6859                 if (ckWARN(WARN_SYNTAX)) {
6860                     S_check_scalar_slice(aTHX_ s);
6861                 }
6862             }
6863         }
6864         PL_expect = XOPERATOR;
6865         force_ident_maybe_lex('@');
6866         TERM('@');
6867
6868      case '/':                  /* may be division, defined-or, or pattern */
6869         if ((PL_expect == XOPERATOR || PL_expect == XTERMORDORDOR) && s[1] == '/') {
6870             if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6871                     (s[2] == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_LOGIC))
6872                 TOKEN(0);
6873             s += 2;
6874             AOPERATOR(DORDOR);
6875         }
6876         else if (PL_expect == XOPERATOR) {
6877             s++;
6878             if (*s == '=' && !PL_lex_allbrackets
6879                 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
6880             {
6881                 s--;
6882                 TOKEN(0);
6883             }
6884             Mop(OP_DIVIDE);
6885         }
6886         else {
6887             /* Disable warning on "study /blah/" */
6888             if (    PL_oldoldbufptr == PL_last_uni
6889                 && (   *PL_last_uni != 's' || s - PL_last_uni < 5
6890                     || memNE(PL_last_uni, "study", 5)
6891                     || isWORDCHAR_lazy_if_safe(PL_last_uni+5, PL_bufend, UTF)
6892              ))
6893                 check_uni();
6894             s = scan_pat(s,OP_MATCH);
6895             TERM(sublex_start());
6896         }
6897
6898      case '?':                  /* conditional */
6899         s++;
6900         if (!PL_lex_allbrackets
6901             && PL_lex_fakeeof >= LEX_FAKEEOF_IFELSE)
6902         {
6903             s--;
6904             TOKEN(0);
6905         }
6906         PL_lex_allbrackets++;
6907         OPERATOR('?');
6908
6909     case '.':
6910         if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack
6911 #ifdef PERL_STRICT_CR
6912             && s[1] == '\n'
6913 #else
6914             && (s[1] == '\n' || (s[1] == '\r' && s[2] == '\n'))
6915 #endif
6916             && (s == PL_linestart || s[-1] == '\n') )
6917         {
6918             PL_expect = XSTATE;
6919             formbrack = 2; /* dot seen where arguments expected */
6920             goto rightbracket;
6921         }
6922         if (PL_expect == XSTATE && s[1] == '.' && s[2] == '.') {
6923             s += 3;
6924             OPERATOR(YADAYADA);
6925         }
6926         if (PL_expect == XOPERATOR || !isDIGIT(s[1])) {
6927             char tmp = *s++;
6928             if (*s == tmp) {
6929                 if (!PL_lex_allbrackets
6930                     && PL_lex_fakeeof >= LEX_FAKEEOF_RANGE)
6931                 {
6932                     s--;
6933                     TOKEN(0);
6934                 }
6935                 s++;
6936                 if (*s == tmp) {
6937                     s++;
6938                     pl_yylval.ival = OPf_SPECIAL;
6939                 }
6940                 else
6941                     pl_yylval.ival = 0;
6942                 OPERATOR(DOTDOT);
6943             }
6944             if (*s == '=' && !PL_lex_allbrackets
6945                 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
6946             {
6947                 s--;
6948                 TOKEN(0);
6949             }
6950             Aop(OP_CONCAT);
6951         }
6952         /* FALLTHROUGH */
6953     case '0': case '1': case '2': case '3': case '4':
6954     case '5': case '6': case '7': case '8': case '9':
6955         s = scan_num(s, &pl_yylval);
6956         DEBUG_T( { printbuf("### Saw number in %s\n", s); } );
6957         if (PL_expect == XOPERATOR)
6958             no_op("Number",s);
6959         TERM(THING);
6960
6961     case '\'':
6962         s = scan_str(s,FALSE,FALSE,FALSE,NULL);
6963         if (!s)
6964             missingterm(NULL, 0);
6965         COPLINE_SET_FROM_MULTI_END;
6966         DEBUG_T( { printbuf("### Saw string before %s\n", s); } );
6967         if (PL_expect == XOPERATOR) {
6968             no_op("String",s);
6969         }
6970         pl_yylval.ival = OP_CONST;
6971         TERM(sublex_start());
6972
6973     case '"':
6974         s = scan_str(s,FALSE,FALSE,FALSE,NULL);
6975         DEBUG_T( {
6976             if (s)
6977                 printbuf("### Saw string before %s\n", s);
6978             else
6979                 PerlIO_printf(Perl_debug_log,
6980                              "### Saw unterminated string\n");
6981         } );
6982         if (PL_expect == XOPERATOR) {
6983                 no_op("String",s);
6984         }
6985         if (!s)
6986             missingterm(NULL, 0);
6987         pl_yylval.ival = OP_CONST;
6988         /* FIXME. I think that this can be const if char *d is replaced by
6989            more localised variables.  */
6990         for (d = SvPV(PL_lex_stuff, len); len; len--, d++) {
6991             if (*d == '$' || *d == '@' || *d == '\\' || !UTF8_IS_INVARIANT((U8)*d)) {
6992                 pl_yylval.ival = OP_STRINGIFY;
6993                 break;
6994             }
6995         }
6996         if (pl_yylval.ival == OP_CONST)
6997             COPLINE_SET_FROM_MULTI_END;
6998         TERM(sublex_start());
6999
7000     case '`':
7001         s = scan_str(s,FALSE,FALSE,FALSE,NULL);
7002         DEBUG_T( {
7003             if (s)
7004                 printbuf("### Saw backtick string before %s\n", s);
7005             else
7006                 PerlIO_printf(Perl_debug_log,
7007                              "### Saw unterminated backtick string\n");
7008         } );
7009         if (PL_expect == XOPERATOR)
7010             no_op("Backticks",s);
7011         if (!s)
7012             missingterm(NULL, 0);
7013         pl_yylval.ival = OP_BACKTICK;
7014         TERM(sublex_start());
7015
7016     case '\\':
7017         s++;
7018         if (PL_lex_inwhat == OP_SUBST && PL_lex_repl == PL_linestr
7019          && isDIGIT(*s))
7020             Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),"Can't use \\%c to mean $%c in expression",
7021                            *s, *s);
7022         if (PL_expect == XOPERATOR)
7023             no_op("Backslash",s);
7024         OPERATOR(REFGEN);
7025
7026     case 'v':
7027         if (isDIGIT(s[1]) && PL_expect != XOPERATOR) {
7028             char *start = s + 2;
7029             while (isDIGIT(*start) || *start == '_')
7030                 start++;
7031             if (*start == '.' && isDIGIT(start[1])) {
7032                 s = scan_num(s, &pl_yylval);
7033                 TERM(THING);
7034             }
7035             else if ((*start == ':' && start[1] == ':')
7036                   || (PL_expect == XSTATE && *start == ':'))
7037                 goto keylookup;
7038             else if (PL_expect == XSTATE) {
7039                 d = start;
7040                 while (d < PL_bufend && isSPACE(*d)) d++;
7041                 if (*d == ':') goto keylookup;
7042             }
7043             /* avoid v123abc() or $h{v1}, allow C<print v10;> */
7044             if (!isALPHA(*start) && (PL_expect == XTERM
7045                         || PL_expect == XREF || PL_expect == XSTATE
7046                         || PL_expect == XTERMORDORDOR)) {
7047                 GV *const gv = gv_fetchpvn_flags(s, start - s,
7048                                                     UTF ? SVf_UTF8 : 0, SVt_PVCV);
7049                 if (!gv) {
7050                     s = scan_num(s, &pl_yylval);
7051                     TERM(THING);
7052                 }
7053             }
7054         }
7055         goto keylookup;
7056     case 'x':
7057         if (isDIGIT(s[1]) && PL_expect == XOPERATOR) {
7058             s++;
7059             Mop(OP_REPEAT);
7060         }
7061         goto keylookup;
7062
7063     case '_':
7064     case 'a': case 'A':
7065     case 'b': case 'B':
7066     case 'c': case 'C':
7067     case 'd': case 'D':
7068     case 'e': case 'E':
7069     case 'f': case 'F':
7070     case 'g': case 'G':
7071     case 'h': case 'H':
7072     case 'i': case 'I':
7073     case 'j': case 'J':
7074     case 'k': case 'K':
7075     case 'l': case 'L':
7076     case 'm': case 'M':
7077     case 'n': case 'N':
7078     case 'o': case 'O':
7079     case 'p': case 'P':
7080     case 'q': case 'Q':
7081     case 'r': case 'R':
7082     case 's': case 'S':
7083     case 't': case 'T':
7084     case 'u': case 'U':
7085               case 'V':
7086     case 'w': case 'W':
7087               case 'X':
7088     case 'y': case 'Y':
7089     case 'z': case 'Z':
7090
7091       keylookup: {
7092         bool anydelim;
7093         bool lex;
7094         I32 tmp;
7095         SV *sv;
7096         CV *cv;
7097         PADOFFSET off;
7098         OP *rv2cv_op;
7099
7100         lex = FALSE;
7101         orig_keyword = 0;
7102         off = 0;
7103         sv = NULL;
7104         cv = NULL;
7105         gv = NULL;
7106         gvp = NULL;
7107         rv2cv_op = NULL;
7108
7109         PL_bufptr = s;
7110         s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
7111
7112         /* Some keywords can be followed by any delimiter, including ':' */
7113         anydelim = word_takes_any_delimiter(PL_tokenbuf, len);
7114
7115         /* x::* is just a word, unless x is "CORE" */
7116         if (!anydelim && *s == ':' && s[1] == ':') {
7117             if (memEQs(PL_tokenbuf, len, "CORE")) goto case_KEY_CORE;
7118             goto just_a_word;
7119         }
7120
7121         d = s;
7122         while (d < PL_bufend && isSPACE(*d))
7123                 d++;    /* no comments skipped here, or s### is misparsed */
7124
7125         /* Is this a word before a => operator? */
7126         if (*d == '=' && d[1] == '>') {
7127           fat_arrow:
7128             CLINE;
7129             pl_yylval.opval
7130                 = newSVOP(OP_CONST, 0,
7131                                S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
7132             pl_yylval.opval->op_private = OPpCONST_BARE;
7133             TERM(BAREWORD);
7134         }
7135
7136         /* Check for plugged-in keyword */
7137         {
7138             OP *o;
7139             int result;
7140             char *saved_bufptr = PL_bufptr;
7141             PL_bufptr = s;
7142             result = PL_keyword_plugin(aTHX_ PL_tokenbuf, len, &o);
7143             s = PL_bufptr;
7144             if (result == KEYWORD_PLUGIN_DECLINE) {
7145                 /* not a plugged-in keyword */
7146                 PL_bufptr = saved_bufptr;
7147             } else if (result == KEYWORD_PLUGIN_STMT) {
7148                 pl_yylval.opval = o;
7149                 CLINE;
7150                 if (!PL_nexttoke) PL_expect = XSTATE;
7151                 return REPORT(PLUGSTMT);
7152             } else if (result == KEYWORD_PLUGIN_EXPR) {
7153                 pl_yylval.opval = o;
7154                 CLINE;
7155                 if (!PL_nexttoke) PL_expect = XOPERATOR;
7156                 return REPORT(PLUGEXPR);
7157             } else {
7158                 Perl_croak(aTHX_ "Bad plugin affecting keyword '%s'",
7159                                         PL_tokenbuf);
7160             }
7161         }
7162
7163         /* Check for built-in keyword */
7164         tmp = keyword(PL_tokenbuf, len, 0);
7165
7166         /* Is this a label? */
7167         if (!anydelim && PL_expect == XSTATE
7168               && d < PL_bufend && *d == ':' && *(d + 1) != ':') {
7169             s = d + 1;
7170             pl_yylval.pval = savepvn(PL_tokenbuf, len+1);
7171             pl_yylval.pval[len] = '\0';
7172             pl_yylval.pval[len+1] = UTF ? 1 : 0;
7173             CLINE;
7174             TOKEN(LABEL);
7175         }
7176
7177         /* Check for lexical sub */
7178         if (PL_expect != XOPERATOR) {
7179             char tmpbuf[sizeof PL_tokenbuf + 1];
7180             *tmpbuf = '&';
7181             Copy(PL_tokenbuf, tmpbuf+1, len, char);
7182             off = pad_findmy_pvn(tmpbuf, len+1, 0);
7183             if (off != NOT_IN_PAD) {
7184                 assert(off); /* we assume this is boolean-true below */
7185                 if (PAD_COMPNAME_FLAGS_isOUR(off)) {
7186                     HV *  const stash = PAD_COMPNAME_OURSTASH(off);
7187                     HEK * const stashname = HvNAME_HEK(stash);
7188                     sv = newSVhek(stashname);
7189                     sv_catpvs(sv, "::");
7190                     sv_catpvn_flags(sv, PL_tokenbuf, len,
7191                                     (UTF ? SV_CATUTF8 : SV_CATBYTES));
7192                     gv = gv_fetchsv(sv, GV_NOADD_NOINIT | SvUTF8(sv),
7193                                     SVt_PVCV);
7194                     off = 0;
7195                     if (!gv) {
7196                         sv_free(sv);
7197                         sv = NULL;
7198                         goto just_a_word;
7199                     }
7200                 }
7201                 else {
7202                     rv2cv_op = newOP(OP_PADANY, 0);
7203                     rv2cv_op->op_targ = off;
7204                     cv = find_lexical_cv(off);
7205                 }
7206                 lex = TRUE;
7207                 goto just_a_word;
7208             }
7209             off = 0;
7210         }
7211
7212         if (tmp < 0) {                  /* second-class keyword? */
7213             GV *ogv = NULL;     /* override (winner) */
7214             GV *hgv = NULL;     /* hidden (loser) */
7215             if (PL_expect != XOPERATOR && (*s != ':' || s[1] != ':')) {
7216                 CV *cv;
7217                 if ((gv = gv_fetchpvn_flags(PL_tokenbuf, len,
7218                                             (UTF ? SVf_UTF8 : 0)|GV_NOTQUAL,
7219                                             SVt_PVCV))
7220                     && (cv = GvCVu(gv)))
7221                 {
7222                     if (GvIMPORTED_CV(gv))
7223                         ogv = gv;
7224                     else if (! CvMETHOD(cv))
7225                         hgv = gv;
7226                 }
7227                 if (!ogv
7228                     && (gvp = (GV**)hv_fetch(PL_globalstash, PL_tokenbuf,
7229                                                               len, FALSE))
7230                     && (gv = *gvp)
7231                     && (isGV_with_GP(gv)
7232                         ? GvCVu(gv) && GvIMPORTED_CV(gv)
7233                         :   SvPCS_IMPORTED(gv)
7234                         && (gv_init(gv, PL_globalstash, PL_tokenbuf,
7235                                                                  len, 0), 1)))
7236                 {
7237                     ogv = gv;
7238                 }
7239             }
7240             if (ogv) {
7241                 orig_keyword = tmp;
7242                 tmp = 0;                /* overridden by import or by GLOBAL */
7243             }
7244             else if (gv && !gvp
7245                      && -tmp==KEY_lock  /* XXX generalizable kludge */
7246                      && GvCVu(gv))
7247             {
7248                 tmp = 0;                /* any sub overrides "weak" keyword */
7249             }
7250             else {                      /* no override */
7251                 tmp = -tmp;
7252                 if (tmp == KEY_dump) {
7253                     Perl_croak(aTHX_ "dump() must be written as CORE::dump() as of Perl 5.30");
7254                 }
7255                 gv = NULL;
7256                 gvp = 0;
7257                 if (hgv && tmp != KEY_x)        /* never ambiguous */
7258                     Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
7259                                    "Ambiguous call resolved as CORE::%s(), "
7260                                    "qualify as such or use &",
7261                                    GvENAME(hgv));
7262             }
7263         }
7264
7265         if (tmp && tmp != KEY___DATA__ && tmp != KEY___END__
7266          && (!anydelim || *s != '#')) {
7267             /* no override, and not s### either; skipspace is safe here
7268              * check for => on following line */
7269             bool arrow;
7270             STRLEN bufoff = PL_bufptr - SvPVX(PL_linestr);
7271             STRLEN   soff = s         - SvPVX(PL_linestr);
7272             s = peekspace(s);
7273             arrow = *s == '=' && s[1] == '>';
7274             PL_bufptr = SvPVX(PL_linestr) + bufoff;
7275             s         = SvPVX(PL_linestr) +   soff;
7276             if (arrow)
7277                 goto fat_arrow;
7278         }
7279
7280       reserved_word:
7281         switch (tmp) {
7282
7283             /* Trade off - by using this evil construction we can pull the
7284                variable gv into the block labelled keylookup. If not, then
7285                we have to give it function scope so that the goto from the
7286                earlier ':' case doesn't bypass the initialisation.  */
7287             just_a_word_zero_gv:
7288                 sv = NULL;
7289                 cv = NULL;
7290                 gv = NULL;
7291                 gvp = NULL;
7292                 rv2cv_op = NULL;
7293                 orig_keyword = 0;
7294                 lex = 0;
7295                 off = 0;
7296             /* FALLTHROUGH */
7297         default:                        /* not a keyword */
7298           just_a_word: {
7299                 int pkgname = 0;
7300                 const char lastchar = (PL_bufptr == PL_oldoldbufptr ? 0 : PL_bufptr[-1]);
7301                 bool safebw;
7302                 bool no_op_error = FALSE;
7303
7304                 if (PL_expect == XOPERATOR) {
7305                     if (PL_bufptr == PL_linestart) {
7306                         CopLINE_dec(PL_curcop);
7307                         Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
7308                         CopLINE_inc(PL_curcop);
7309                     }
7310                     else
7311                         /* We want to call no_op with s pointing after the
7312                            bareword, so defer it.  But we want it to come
7313                            before the Bad name croak.  */
7314                         no_op_error = TRUE;
7315                 }
7316
7317                 /* Get the rest if it looks like a package qualifier */
7318
7319                 if (*s == '\'' || (*s == ':' && s[1] == ':')) {
7320                     STRLEN morelen;
7321                     s = scan_word(s, PL_tokenbuf + len, sizeof PL_tokenbuf - len,
7322                                   TRUE, &morelen);
7323                     if (no_op_error) {
7324                         no_op("Bareword",s);
7325                         no_op_error = FALSE;
7326                     }
7327                     if (!morelen)
7328                         Perl_croak(aTHX_ "Bad name after %" UTF8f "%s",
7329                                 UTF8fARG(UTF, len, PL_tokenbuf),
7330                                 *s == '\'' ? "'" : "::");
7331                     len += morelen;
7332                     pkgname = 1;
7333                 }
7334
7335                 if (no_op_error)
7336                         no_op("Bareword",s);
7337
7338                 /* See if the name is "Foo::",
7339                    in which case Foo is a bareword
7340                    (and a package name). */
7341
7342                 if (len > 2
7343                     && PL_tokenbuf[len - 2] == ':'
7344                     && PL_tokenbuf[len - 1] == ':')
7345                 {
7346                     if (ckWARN(WARN_BAREWORD)
7347                         && ! gv_fetchpvn_flags(PL_tokenbuf, len, UTF ? SVf_UTF8 : 0, SVt_PVHV))
7348                         Perl_warner(aTHX_ packWARN(WARN_BAREWORD),
7349                                     "Bareword \"%" UTF8f
7350                                     "\" refers to nonexistent package",
7351                                     UTF8fARG(UTF, len, PL_tokenbuf));
7352                     len -= 2;
7353                     PL_tokenbuf[len] = '\0';
7354                     gv = NULL;
7355                     gvp = 0;
7356                     safebw = TRUE;
7357                 }
7358                 else {
7359                     safebw = FALSE;
7360                 }
7361
7362                 /* if we saw a global override before, get the right name */
7363
7364                 if (!sv)
7365                   sv = S_newSV_maybe_utf8(aTHX_ PL_tokenbuf,
7366                                                 len);
7367                 if (gvp) {
7368                     SV * const tmp_sv = sv;
7369                     sv = newSVpvs("CORE::GLOBAL::");
7370                     sv_catsv(sv, tmp_sv);
7371                     SvREFCNT_dec(tmp_sv);
7372                 }
7373
7374
7375                 /* Presume this is going to be a bareword of some sort. */
7376                 CLINE;
7377                 pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
7378                 pl_yylval.opval->op_private = OPpCONST_BARE;
7379
7380                 /* And if "Foo::", then that's what it certainly is. */
7381                 if (safebw)
7382                     goto safe_bareword;
7383
7384                 if (!off)
7385                 {
7386                     OP *const_op = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(sv));
7387                     const_op->op_private = OPpCONST_BARE;
7388                     rv2cv_op =
7389                         newCVREF(OPpMAY_RETURN_CONSTANT<<8, const_op);
7390                     cv = lex
7391                         ? isGV(gv)
7392                             ? GvCV(gv)
7393                             : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
7394                                 ? (CV *)SvRV(gv)
7395                                 : ((CV *)gv)
7396                         : rv2cv_op_cv(rv2cv_op, RV2CVOPCV_RETURN_STUB);
7397                 }
7398
7399                 /* Use this var to track whether intuit_method has been
7400                    called.  intuit_method returns 0 or > 255.  */
7401                 tmp = 1;
7402
7403                 /* See if it's the indirect object for a list operator. */
7404
7405                 if (PL_oldoldbufptr
7406                     && PL_oldoldbufptr < PL_bufptr
7407                     && (PL_oldoldbufptr == PL_last_lop
7408                         || PL_oldoldbufptr == PL_last_uni)
7409                     && /* NO SKIPSPACE BEFORE HERE! */
7410                        (PL_expect == XREF
7411                         || ((PL_opargs[PL_last_lop_op] >> OASHIFT)& 7)
7412                                                                == OA_FILEREF))
7413                 {
7414                     bool immediate_paren = *s == '(';
7415                     SSize_t s_off;
7416
7417                     /* (Now we can afford to cross potential line boundary.) */
7418                     s = skipspace(s);
7419
7420                     /* intuit_method() can indirectly call lex_next_chunk(),
7421                      * invalidating s
7422                      */
7423                     s_off = s - SvPVX(PL_linestr);
7424                     /* Two barewords in a row may indicate method call. */
7425                     if (   (   isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)
7426                             || *s == '$')
7427                         && (tmp = intuit_method(s, lex ? NULL : sv, cv)))
7428                     {
7429                         /* the code at method: doesn't use s */
7430                         goto method;
7431                     }
7432                     s = SvPVX(PL_linestr) + s_off;
7433
7434                     /* If not a declared subroutine, it's an indirect object. */
7435                     /* (But it's an indir obj regardless for sort.) */
7436                     /* Also, if "_" follows a filetest operator, it's a bareword */
7437
7438                     if (
7439                         ( !immediate_paren && (PL_last_lop_op == OP_SORT
7440                          || (!cv
7441                              && (PL_last_lop_op != OP_MAPSTART
7442                                  && PL_last_lop_op != OP_GREPSTART))))
7443                        || (PL_tokenbuf[0] == '_' && PL_tokenbuf[1] == '\0'
7444                             && ((PL_opargs[PL_last_lop_op] & OA_CLASS_MASK)
7445                                                             == OA_FILESTATOP))
7446                        )
7447                     {
7448                         PL_expect = (PL_last_lop == PL_oldoldbufptr) ? XTERM : XOPERATOR;
7449                         goto bareword;
7450                     }
7451                 }
7452
7453                 PL_expect = XOPERATOR;
7454                 s = skipspace(s);
7455
7456                 /* Is this a word before a => operator? */
7457                 if (*s == '=' && s[1] == '>' && !pkgname) {
7458                     op_free(rv2cv_op);
7459                     CLINE;
7460                     if (gvp || (lex && !off)) {
7461                         assert (cSVOPx(pl_yylval.opval)->op_sv == sv);
7462                         /* This is our own scalar, created a few lines
7463                            above, so this is safe. */
7464                         SvREADONLY_off(sv);
7465                         sv_setpv(sv, PL_tokenbuf);
7466                         if (UTF && !IN_BYTES
7467                          && is_utf8_string((U8*)PL_tokenbuf, len))
7468                               SvUTF8_on(sv);
7469                         SvREADONLY_on(sv);
7470                     }
7471                     TERM(BAREWORD);
7472                 }
7473
7474                 /* If followed by a paren, it's certainly a subroutine. */
7475                 if (*s == '(') {
7476                     CLINE;
7477                     if (cv) {
7478                         d = s + 1;
7479                         while (SPACE_OR_TAB(*d))
7480                             d++;
7481                         if (*d == ')' && (sv = cv_const_sv_or_av(cv))) {
7482                             s = d + 1;
7483                             goto its_constant;
7484                         }
7485                     }
7486                     NEXTVAL_NEXTTOKE.opval =
7487                         off ? rv2cv_op : pl_yylval.opval;
7488                     if (off)
7489                          op_free(pl_yylval.opval), force_next(PRIVATEREF);
7490                     else op_free(rv2cv_op),        force_next(BAREWORD);
7491                     pl_yylval.ival = 0;
7492                     TOKEN('&');
7493                 }
7494
7495                 /* If followed by var or block, call it a method (unless sub) */
7496
7497                 if ((*s == '$' || *s == '{') && !cv) {
7498                     op_free(rv2cv_op);
7499                     PL_last_lop = PL_oldbufptr;
7500                     PL_last_lop_op = OP_METHOD;
7501                     if (!PL_lex_allbrackets
7502                         && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7503                     {
7504                         PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7505                     }
7506                     PL_expect = XBLOCKTERM;
7507                     PL_bufptr = s;
7508                     return REPORT(METHOD);
7509                 }
7510
7511                 /* If followed by a bareword, see if it looks like indir obj. */
7512
7513                 if (   tmp == 1
7514                     && !orig_keyword
7515                     && (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF) || *s == '$')
7516                     && (tmp = intuit_method(s, lex ? NULL : sv, cv)))
7517                 {
7518                   method:
7519                     if (lex && !off) {
7520                         assert(cSVOPx(pl_yylval.opval)->op_sv == sv);
7521                         SvREADONLY_off(sv);
7522                         sv_setpvn(sv, PL_tokenbuf, len);
7523                         if (UTF && !IN_BYTES
7524                          && is_utf8_string((U8*)PL_tokenbuf, len))
7525                             SvUTF8_on (sv);
7526                         else SvUTF8_off(sv);
7527                     }
7528                     op_free(rv2cv_op);
7529                     if (tmp == METHOD && !PL_lex_allbrackets
7530                         && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7531                     {
7532                         PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7533                     }
7534                     return REPORT(tmp);
7535                 }
7536
7537                 /* Not a method, so call it a subroutine (if defined) */
7538
7539                 if (cv) {
7540                     /* Check for a constant sub */
7541                     if ((sv = cv_const_sv_or_av(cv))) {
7542                   its_constant:
7543                         op_free(rv2cv_op);
7544                         SvREFCNT_dec(((SVOP*)pl_yylval.opval)->op_sv);
7545                         ((SVOP*)pl_yylval.opval)->op_sv = SvREFCNT_inc_simple(sv);
7546                         if (SvTYPE(sv) == SVt_PVAV)
7547                             pl_yylval.opval = newUNOP(OP_RV2AV, OPf_PARENS,
7548                                                       pl_yylval.opval);
7549                         else {
7550                             pl_yylval.opval->op_private = 0;
7551                             pl_yylval.opval->op_folded = 1;
7552                             pl_yylval.opval->op_flags |= OPf_SPECIAL;
7553                         }
7554                         TOKEN(BAREWORD);
7555                     }
7556
7557                     op_free(pl_yylval.opval);
7558                     pl_yylval.opval =
7559                         off ? newCVREF(0, rv2cv_op) : rv2cv_op;
7560                     pl_yylval.opval->op_private |= OPpENTERSUB_NOPAREN;
7561                     PL_last_lop = PL_oldbufptr;
7562                     PL_last_lop_op = OP_ENTERSUB;
7563                     /* Is there a prototype? */
7564                     if (
7565                         SvPOK(cv))
7566                     {
7567                         STRLEN protolen = CvPROTOLEN(cv);
7568                         const char *proto = CvPROTO(cv);
7569                         bool optional;
7570                         proto = S_strip_spaces(aTHX_ proto, &protolen);
7571                         if (!protolen)
7572                             TERM(FUNC0SUB);
7573                         if ((optional = *proto == ';'))
7574                           do
7575                             proto++;
7576                           while (*proto == ';');
7577                         if (
7578                             (
7579                                 (
7580                                     *proto == '$' || *proto == '_'
7581                                  || *proto == '*' || *proto == '+'
7582                                 )
7583                              && proto[1] == '\0'
7584                             )
7585                          || (
7586                              *proto == '\\' && proto[1] && proto[2] == '\0'
7587                             )
7588                         )
7589                             UNIPROTO(UNIOPSUB,optional);
7590                         if (*proto == '\\' && proto[1] == '[') {
7591                             const char *p = proto + 2;
7592                             while(*p && *p != ']')
7593                                 ++p;
7594                             if(*p == ']' && !p[1])
7595                                 UNIPROTO(UNIOPSUB,optional);
7596                         }
7597                         if (*proto == '&' && *s == '{') {
7598                             if (PL_curstash)
7599                                 sv_setpvs(PL_subname, "__ANON__");
7600                             else
7601                                 sv_setpvs(PL_subname, "__ANON__::__ANON__");
7602                             if (!PL_lex_allbrackets
7603                                 && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7604                             {
7605                                 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7606                             }
7607                             PREBLOCK(LSTOPSUB);
7608                         }
7609                     }
7610                     NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
7611                     PL_expect = XTERM;
7612                     force_next(off ? PRIVATEREF : BAREWORD);
7613                     if (!PL_lex_allbrackets
7614                         && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7615                     {
7616                         PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7617                     }
7618                     TOKEN(NOAMP);
7619                 }
7620
7621                 /* Call it a bare word */
7622
7623                 if (PL_hints & HINT_STRICT_SUBS)
7624                     pl_yylval.opval->op_private |= OPpCONST_STRICT;
7625                 else {
7626                 bareword:
7627                     /* after "print" and similar functions (corresponding to
7628                      * "F? L" in opcode.pl), whatever wasn't already parsed as
7629                      * a filehandle should be subject to "strict subs".
7630                      * Likewise for the optional indirect-object argument to system
7631                      * or exec, which can't be a bareword */
7632                     if ((PL_last_lop_op == OP_PRINT
7633                             || PL_last_lop_op == OP_PRTF
7634                             || PL_last_lop_op == OP_SAY
7635                             || PL_last_lop_op == OP_SYSTEM
7636                             || PL_last_lop_op == OP_EXEC)
7637                             && (PL_hints & HINT_STRICT_SUBS))
7638                         pl_yylval.opval->op_private |= OPpCONST_STRICT;
7639                     if (lastchar != '-') {
7640                         if (ckWARN(WARN_RESERVED)) {
7641                             d = PL_tokenbuf;
7642                             while (isLOWER(*d))
7643                                 d++;
7644                             if (!*d && !gv_stashpv(PL_tokenbuf, UTF ? SVf_UTF8 : 0))
7645                             {
7646                                 /* PL_warn_reserved is constant */
7647                                 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
7648                                 Perl_warner(aTHX_ packWARN(WARN_RESERVED), PL_warn_reserved,
7649                                        PL_tokenbuf);
7650                                 GCC_DIAG_RESTORE_STMT;
7651                             }
7652                         }
7653                     }
7654                 }
7655                 op_free(rv2cv_op);
7656
7657             safe_bareword:
7658                 if ((lastchar == '*' || lastchar == '%' || lastchar == '&')
7659                  && saw_infix_sigil) {
7660                     Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
7661                                      "Operator or semicolon missing before %c%" UTF8f,
7662                                      lastchar,
7663                                      UTF8fARG(UTF, strlen(PL_tokenbuf),
7664                                               PL_tokenbuf));
7665                     Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
7666                                      "Ambiguous use of %c resolved as operator %c",
7667                                      lastchar, lastchar);
7668                 }
7669                 TOKEN(BAREWORD);
7670             }
7671
7672         case KEY___FILE__:
7673             FUN0OP(
7674                 newSVOP(OP_CONST, 0, newSVpv(CopFILE(PL_curcop),0))
7675             );
7676
7677         case KEY___LINE__:
7678             FUN0OP(
7679                 newSVOP(OP_CONST, 0,
7680                     Perl_newSVpvf(aTHX_ "%" IVdf, (IV)CopLINE(PL_curcop)))
7681             );
7682
7683         case KEY___PACKAGE__:
7684             FUN0OP(
7685                 newSVOP(OP_CONST, 0,
7686                                         (PL_curstash
7687                                          ? newSVhek(HvNAME_HEK(PL_curstash))
7688                                          : &PL_sv_undef))
7689             );
7690
7691         case KEY___DATA__:
7692         case KEY___END__: {
7693             GV *gv;
7694             if (PL_rsfp && (!PL_in_eval || PL_tokenbuf[2] == 'D')) {
7695                 HV * const stash = PL_tokenbuf[2] == 'D' && PL_curstash
7696                                         ? PL_curstash
7697                                         : PL_defstash;
7698                 gv = (GV *)*hv_fetchs(stash, "DATA", 1);
7699                 if (!isGV(gv))
7700                     gv_init(gv,stash,"DATA",4,0);
7701                 GvMULTI_on(gv);
7702                 if (!GvIO(gv))
7703                     GvIOp(gv) = newIO();
7704                 IoIFP(GvIOp(gv)) = PL_rsfp;
7705                 /* Mark this internal pseudo-handle as clean */
7706                 IoFLAGS(GvIOp(gv)) |= IOf_UNTAINT;
7707                 if ((PerlIO*)PL_rsfp == PerlIO_stdin())
7708                     IoTYPE(GvIOp(gv)) = IoTYPE_STD;
7709                 else
7710                     IoTYPE(GvIOp(gv)) = IoTYPE_RDONLY;
7711 #if defined(WIN32) && !defined(PERL_TEXTMODE_SCRIPTS)
7712                 /* if the script was opened in binmode, we need to revert
7713                  * it to text mode for compatibility; but only iff it has CRs
7714                  * XXX this is a questionable hack at best. */
7715                 if (PL_bufend-PL_bufptr > 2
7716                     && PL_bufend[-1] == '\n' && PL_bufend[-2] == '\r')
7717                 {
7718                     Off_t loc = 0;
7719                     if (IoTYPE(GvIOp(gv)) == IoTYPE_RDONLY) {
7720                         loc = PerlIO_tell(PL_rsfp);
7721                         (void)PerlIO_seek(PL_rsfp, 0L, 0);
7722                     }
7723 #ifdef NETWARE
7724                         if (PerlLIO_setmode(PL_rsfp, O_TEXT) != -1) {
7725 #else
7726                     if (PerlLIO_setmode(PerlIO_fileno(PL_rsfp), O_TEXT) != -1) {
7727 #endif  /* NETWARE */
7728                         if (loc > 0)
7729                             PerlIO_seek(PL_rsfp, loc, 0);
7730                     }
7731                 }
7732 #endif
7733 #ifdef PERLIO_LAYERS
7734                 if (!IN_BYTES) {
7735                     if (UTF)
7736                         PerlIO_apply_layers(aTHX_ PL_rsfp, NULL, ":utf8");
7737                 }
7738 #endif
7739                 PL_rsfp = NULL;
7740             }
7741             goto fake_eof;
7742         }
7743
7744         case KEY___SUB__:
7745             FUN0OP(CvCLONE(PL_compcv)
7746                         ? newOP(OP_RUNCV, 0)
7747                         : newPVOP(OP_RUNCV,0,NULL));
7748
7749         case KEY_AUTOLOAD:
7750         case KEY_DESTROY:
7751         case KEY_BEGIN:
7752         case KEY_UNITCHECK:
7753         case KEY_CHECK:
7754         case KEY_INIT:
7755         case KEY_END:
7756             if (PL_expect == XSTATE) {
7757                 s = PL_bufptr;
7758                 goto really_sub;
7759             }
7760             goto just_a_word;
7761
7762         case_KEY_CORE:
7763             {
7764                 STRLEN olen = len;
7765                 d = s;
7766                 s += 2;
7767                 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
7768                 if ((*s == ':' && s[1] == ':')
7769                  || (!(tmp = keyword(PL_tokenbuf, len, 1)) && *s == '\''))
7770                 {
7771                     s = d;
7772                     len = olen;
7773                     Copy(PL_bufptr, PL_tokenbuf, olen, char);
7774                     goto just_a_word;
7775                 }
7776                 if (!tmp)
7777                     Perl_croak(aTHX_ "CORE::%" UTF8f " is not a keyword",
7778                                       UTF8fARG(UTF, len, PL_tokenbuf));
7779                 if (tmp < 0)
7780                     tmp = -tmp;
7781                 else if (tmp == KEY_require || tmp == KEY_do
7782                       || tmp == KEY_glob)
7783                     /* that's a way to remember we saw "CORE::" */
7784                     orig_keyword = tmp;
7785                 goto reserved_word;
7786             }
7787
7788         case KEY_abs:
7789             UNI(OP_ABS);
7790
7791         case KEY_alarm:
7792             UNI(OP_ALARM);
7793
7794         case KEY_accept:
7795             LOP(OP_ACCEPT,XTERM);
7796
7797         case KEY_and:
7798             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
7799                 return REPORT(0);
7800             OPERATOR(ANDOP);
7801
7802         case KEY_atan2:
7803             LOP(OP_ATAN2,XTERM);
7804
7805         case KEY_bind:
7806             LOP(OP_BIND,XTERM);
7807
7808         case KEY_binmode:
7809             LOP(OP_BINMODE,XTERM);
7810
7811         case KEY_bless:
7812             LOP(OP_BLESS,XTERM);
7813
7814         case KEY_break:
7815             FUN0(OP_BREAK);
7816
7817         case KEY_chop:
7818             UNI(OP_CHOP);
7819
7820         case KEY_continue:
7821                     /* We have to disambiguate the two senses of
7822                       "continue". If the next token is a '{' then
7823                       treat it as the start of a continue block;
7824                       otherwise treat it as a control operator.
7825                      */
7826                     s = skipspace(s);
7827                     if (*s == '{')
7828             PREBLOCK(CONTINUE);
7829                     else
7830                         FUN0(OP_CONTINUE);
7831
7832         case KEY_chdir:
7833             /* may use HOME */
7834             (void)gv_fetchpvs("ENV", GV_ADD|GV_NOTQUAL, SVt_PVHV);
7835             UNI(OP_CHDIR);
7836
7837         case KEY_close:
7838             UNI(OP_CLOSE);
7839
7840         case KEY_closedir:
7841             UNI(OP_CLOSEDIR);
7842
7843         case KEY_cmp:
7844             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7845                 return REPORT(0);
7846             Eop(OP_SCMP);
7847
7848         case KEY_caller:
7849             UNI(OP_CALLER);
7850
7851         case KEY_crypt:
7852 #ifdef FCRYPT
7853             if (!PL_cryptseen) {
7854                 PL_cryptseen = TRUE;
7855                 init_des();
7856             }
7857 #endif
7858             LOP(OP_CRYPT,XTERM);
7859
7860         case KEY_chmod:
7861             LOP(OP_CHMOD,XTERM);
7862
7863         case KEY_chown:
7864             LOP(OP_CHOWN,XTERM);
7865
7866         case KEY_connect:
7867             LOP(OP_CONNECT,XTERM);
7868
7869         case KEY_chr:
7870             UNI(OP_CHR);
7871
7872         case KEY_cos:
7873             UNI(OP_COS);
7874
7875         case KEY_chroot:
7876             UNI(OP_CHROOT);
7877
7878         case KEY_default:
7879             PREBLOCK(DEFAULT);
7880
7881         case KEY_do:
7882             s = skipspace(s);
7883             if (*s == '{')
7884                 PRETERMBLOCK(DO);
7885             if (*s != '\'') {
7886                 *PL_tokenbuf = '&';
7887                 d = scan_word(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
7888                               1, &len);
7889                 if (len && memNEs(PL_tokenbuf+1, len, "CORE")
7890                  && !keyword(PL_tokenbuf + 1, len, 0)) {
7891                     SSize_t off = s-SvPVX(PL_linestr);
7892                     d = skipspace(d);
7893                     s = SvPVX(PL_linestr)+off;
7894                     if (*d == '(') {
7895                         force_ident_maybe_lex('&');
7896                         s = d;
7897                     }
7898                 }
7899             }
7900             if (orig_keyword == KEY_do) {
7901                 orig_keyword = 0;
7902                 pl_yylval.ival = 1;
7903             }
7904             else
7905                 pl_yylval.ival = 0;
7906             OPERATOR(DO);
7907
7908         case KEY_die:
7909             PL_hints |= HINT_BLOCK_SCOPE;
7910             LOP(OP_DIE,XTERM);
7911
7912         case KEY_defined:
7913             UNI(OP_DEFINED);
7914
7915         case KEY_delete:
7916             UNI(OP_DELETE);
7917
7918         case KEY_dbmopen:
7919             Perl_populate_isa(aTHX_ STR_WITH_LEN("AnyDBM_File::ISA"),
7920                               STR_WITH_LEN("NDBM_File::"),
7921                               STR_WITH_LEN("DB_File::"),
7922                               STR_WITH_LEN("GDBM_File::"),
7923                               STR_WITH_LEN("SDBM_File::"),
7924                               STR_WITH_LEN("ODBM_File::"),
7925                               NULL);
7926             LOP(OP_DBMOPEN,XTERM);
7927
7928         case KEY_dbmclose:
7929             UNI(OP_DBMCLOSE);
7930
7931         case KEY_dump:
7932             LOOPX(OP_DUMP);
7933
7934         case KEY_else:
7935             PREBLOCK(ELSE);
7936
7937         case KEY_elsif:
7938             pl_yylval.ival = CopLINE(PL_curcop);
7939             OPERATOR(ELSIF);
7940
7941         case KEY_eq:
7942             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7943                 return REPORT(0);
7944             Eop(OP_SEQ);
7945
7946         case KEY_exists:
7947             UNI(OP_EXISTS);
7948
7949         case KEY_exit:
7950             UNI(OP_EXIT);
7951
7952         case KEY_eval:
7953             s = skipspace(s);
7954             if (*s == '{') { /* block eval */
7955                 PL_expect = XTERMBLOCK;
7956                 UNIBRACK(OP_ENTERTRY);
7957             }
7958             else { /* string eval */
7959                 PL_expect = XTERM;
7960                 UNIBRACK(OP_ENTEREVAL);
7961             }
7962
7963         case KEY_evalbytes:
7964             PL_expect = XTERM;
7965             UNIBRACK(-OP_ENTEREVAL);
7966
7967         case KEY_eof:
7968             UNI(OP_EOF);
7969
7970         case KEY_exp:
7971             UNI(OP_EXP);
7972
7973         case KEY_each:
7974             UNI(OP_EACH);
7975
7976         case KEY_exec:
7977             LOP(OP_EXEC,XREF);
7978
7979         case KEY_endhostent:
7980             FUN0(OP_EHOSTENT);
7981
7982         case KEY_endnetent:
7983             FUN0(OP_ENETENT);
7984
7985         case KEY_endservent:
7986             FUN0(OP_ESERVENT);
7987
7988         case KEY_endprotoent:
7989             FUN0(OP_EPROTOENT);
7990
7991         case KEY_endpwent:
7992             FUN0(OP_EPWENT);
7993
7994         case KEY_endgrent:
7995             FUN0(OP_EGRENT);
7996
7997         case KEY_for:
7998         case KEY_foreach:
7999             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8000                 return REPORT(0);
8001             pl_yylval.ival = CopLINE(PL_curcop);
8002             s = skipspace(s);
8003             if (   PL_expect == XSTATE
8004                 && isIDFIRST_lazy_if_safe(s, PL_bufend, UTF))
8005             {
8006                 char *p = s;
8007                 SSize_t s_off = s - SvPVX(PL_linestr);
8008
8009                 if (   memBEGINPs(p, (STRLEN) (PL_bufend - p), "my")
8010                     && isSPACE(*(p + 2)))
8011                 {
8012                     p += 2;
8013                 }
8014                 else if (   memBEGINPs(p, (STRLEN) (PL_bufend - p), "our")
8015                          && isSPACE(*(p + 3)))
8016                 {
8017                     p += 3;
8018                 }
8019
8020                 p = skipspace(p);
8021                 /* skip optional package name, as in "for my abc $x (..)" */
8022                 if (isIDFIRST_lazy_if_safe(p, PL_bufend, UTF)) {
8023                     p = scan_word(p, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
8024                     p = skipspace(p);
8025                 }
8026                 if (*p != '$' && *p != '\\')
8027                     Perl_croak(aTHX_ "Missing $ on loop variable");
8028
8029                 /* The buffer may have been reallocated, update s */
8030                 s = SvPVX(PL_linestr) + s_off;
8031             }
8032             OPERATOR(FOR);
8033
8034         case KEY_formline:
8035             LOP(OP_FORMLINE,XTERM);
8036
8037         case KEY_fork:
8038             FUN0(OP_FORK);
8039
8040         case KEY_fc:
8041             UNI(OP_FC);
8042
8043         case KEY_fcntl:
8044             LOP(OP_FCNTL,XTERM);
8045
8046         case KEY_fileno:
8047             UNI(OP_FILENO);
8048
8049         case KEY_flock:
8050             LOP(OP_FLOCK,XTERM);
8051
8052         case KEY_gt:
8053             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8054                 return REPORT(0);
8055             Rop(OP_SGT);
8056
8057         case KEY_ge:
8058             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8059                 return REPORT(0);
8060             Rop(OP_SGE);
8061
8062         case KEY_grep:
8063             LOP(OP_GREPSTART, XREF);
8064
8065         case KEY_goto:
8066             LOOPX(OP_GOTO);
8067
8068         case KEY_gmtime:
8069             UNI(OP_GMTIME);
8070
8071         case KEY_getc:
8072             UNIDOR(OP_GETC);
8073
8074         case KEY_getppid:
8075             FUN0(OP_GETPPID);
8076
8077         case KEY_getpgrp:
8078             UNI(OP_GETPGRP);
8079
8080         case KEY_getpriority:
8081             LOP(OP_GETPRIORITY,XTERM);
8082
8083         case KEY_getprotobyname:
8084             UNI(OP_GPBYNAME);
8085
8086         case KEY_getprotobynumber:
8087             LOP(OP_GPBYNUMBER,XTERM);
8088
8089         case KEY_getprotoent:
8090             FUN0(OP_GPROTOENT);
8091
8092         case KEY_getpwent:
8093             FUN0(OP_GPWENT);
8094
8095         case KEY_getpwnam:
8096             UNI(OP_GPWNAM);
8097
8098         case KEY_getpwuid:
8099             UNI(OP_GPWUID);
8100
8101         case KEY_getpeername:
8102             UNI(OP_GETPEERNAME);
8103
8104         case KEY_gethostbyname:
8105             UNI(OP_GHBYNAME);
8106
8107         case KEY_gethostbyaddr:
8108             LOP(OP_GHBYADDR,XTERM);
8109
8110         case KEY_gethostent:
8111             FUN0(OP_GHOSTENT);
8112
8113         case KEY_getnetbyname:
8114             UNI(OP_GNBYNAME);
8115
8116         case KEY_getnetbyaddr:
8117             LOP(OP_GNBYADDR,XTERM);
8118
8119         case KEY_getnetent:
8120             FUN0(OP_GNETENT);
8121
8122         case KEY_getservbyname:
8123             LOP(OP_GSBYNAME,XTERM);
8124
8125         case KEY_getservbyport:
8126             LOP(OP_GSBYPORT,XTERM);
8127
8128         case KEY_getservent:
8129             FUN0(OP_GSERVENT);
8130
8131         case KEY_getsockname:
8132             UNI(OP_GETSOCKNAME);
8133
8134         case KEY_getsockopt:
8135             LOP(OP_GSOCKOPT,XTERM);
8136
8137         case KEY_getgrent:
8138             FUN0(OP_GGRENT);
8139
8140         case KEY_getgrnam:
8141             UNI(OP_GGRNAM);
8142
8143         case KEY_getgrgid:
8144             UNI(OP_GGRGID);
8145
8146         case KEY_getlogin:
8147             FUN0(OP_GETLOGIN);
8148
8149         case KEY_given:
8150             pl_yylval.ival = CopLINE(PL_curcop);
8151             Perl_ck_warner_d(aTHX_
8152                 packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
8153                 "given is experimental");
8154             OPERATOR(GIVEN);
8155
8156         case KEY_glob:
8157             LOP(
8158              orig_keyword==KEY_glob ? -OP_GLOB : OP_GLOB,
8159              XTERM
8160             );
8161
8162         case KEY_hex:
8163             UNI(OP_HEX);
8164
8165         case KEY_if:
8166             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8167                 return REPORT(0);
8168             pl_yylval.ival = CopLINE(PL_curcop);
8169             OPERATOR(IF);
8170
8171         case KEY_index:
8172             LOP(OP_INDEX,XTERM);
8173
8174         case KEY_int:
8175             UNI(OP_INT);
8176
8177         case KEY_ioctl:
8178             LOP(OP_IOCTL,XTERM);
8179
8180         case KEY_join:
8181             LOP(OP_JOIN,XTERM);
8182
8183         case KEY_keys:
8184             UNI(OP_KEYS);
8185
8186         case KEY_kill:
8187             LOP(OP_KILL,XTERM);
8188
8189         case KEY_last:
8190             LOOPX(OP_LAST);
8191
8192         case KEY_lc:
8193             UNI(OP_LC);
8194
8195         case KEY_lcfirst:
8196             UNI(OP_LCFIRST);
8197
8198         case KEY_local:
8199             OPERATOR(LOCAL);
8200
8201         case KEY_length:
8202             UNI(OP_LENGTH);
8203
8204         case KEY_lt:
8205             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8206                 return REPORT(0);
8207             Rop(OP_SLT);
8208
8209         case KEY_le:
8210             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8211                 return REPORT(0);
8212             Rop(OP_SLE);
8213
8214         case KEY_localtime:
8215             UNI(OP_LOCALTIME);
8216
8217         case KEY_log:
8218             UNI(OP_LOG);
8219
8220         case KEY_link:
8221             LOP(OP_LINK,XTERM);
8222
8223         case KEY_listen:
8224             LOP(OP_LISTEN,XTERM);
8225
8226         case KEY_lock:
8227             UNI(OP_LOCK);
8228
8229         case KEY_lstat:
8230             UNI(OP_LSTAT);
8231
8232         case KEY_m:
8233             s = scan_pat(s,OP_MATCH);
8234             TERM(sublex_start());
8235
8236         case KEY_map:
8237             LOP(OP_MAPSTART, XREF);
8238
8239         case KEY_mkdir:
8240             LOP(OP_MKDIR,XTERM);
8241
8242         case KEY_msgctl:
8243             LOP(OP_MSGCTL,XTERM);
8244
8245         case KEY_msgget:
8246             LOP(OP_MSGGET,XTERM);
8247
8248         case KEY_msgrcv:
8249             LOP(OP_MSGRCV,XTERM);
8250
8251         case KEY_msgsnd:
8252             LOP(OP_MSGSND,XTERM);
8253
8254         case KEY_our:
8255         case KEY_my:
8256         case KEY_state:
8257             if (PL_in_my) {
8258                 PL_bufptr = s;
8259                 yyerror(Perl_form(aTHX_
8260                                   "Can't redeclare \"%s\" in \"%s\"",
8261                                    tmp      == KEY_my    ? "my" :
8262                                    tmp      == KEY_state ? "state" : "our",
8263                                    PL_in_my == KEY_my    ? "my" :
8264                                    PL_in_my == KEY_state ? "state" : "our"));
8265             }
8266             PL_in_my = (U16)tmp;
8267             s = skipspace(s);
8268             if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
8269                 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
8270                 if (memEQs(PL_tokenbuf, len, "sub"))
8271                     goto really_sub;
8272                 PL_in_my_stash = find_in_my_stash(PL_tokenbuf, len);
8273                 if (!PL_in_my_stash) {
8274                     char tmpbuf[1024];
8275                     int len;
8276                     PL_bufptr = s;
8277                     len = my_snprintf(tmpbuf, sizeof(tmpbuf), "No such class %.1000s", PL_tokenbuf);
8278                     PERL_MY_SNPRINTF_POST_GUARD(len, sizeof(tmpbuf));
8279                     yyerror_pv(tmpbuf, UTF ? SVf_UTF8 : 0);
8280                 }
8281             }
8282             else if (*s == '\\') {
8283                 if (!FEATURE_MYREF_IS_ENABLED)
8284                     Perl_croak(aTHX_ "The experimental declared_refs "
8285                                      "feature is not enabled");
8286                 Perl_ck_warner_d(aTHX_
8287                      packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
8288                     "Declaring references is experimental");
8289             }
8290             OPERATOR(MY);
8291
8292         case KEY_next:
8293             LOOPX(OP_NEXT);
8294
8295         case KEY_ne:
8296             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8297                 return REPORT(0);
8298             Eop(OP_SNE);
8299
8300         case KEY_no:
8301             s = tokenize_use(0, s);
8302             TOKEN(USE);
8303
8304         case KEY_not:
8305             if (*s == '(' || (s = skipspace(s), *s == '('))
8306                 FUN1(OP_NOT);
8307             else {
8308                 if (!PL_lex_allbrackets
8309                     && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
8310                 {
8311                     PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
8312                 }
8313                 OPERATOR(NOTOP);
8314             }
8315
8316         case KEY_open:
8317             s = skipspace(s);
8318             if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
8319                 const char *t;
8320                 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE,
8321                               &len);
8322                 for (t=d; isSPACE(*t);)
8323                     t++;
8324                 if ( *t && strchr("|&*+-=!?:.", *t) && ckWARN_d(WARN_PRECEDENCE)
8325                     /* [perl #16184] */
8326                     && !(t[0] == '=' && t[1] == '>')
8327                     && !(t[0] == ':' && t[1] == ':')
8328                     && !keyword(s, d-s, 0)
8329                 ) {
8330                     Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
8331                        "Precedence problem: open %" UTF8f " should be open(%" UTF8f ")",
8332                         UTF8fARG(UTF, d-s, s), UTF8fARG(UTF, d-s, s));
8333                 }
8334             }
8335             LOP(OP_OPEN,XTERM);
8336
8337         case KEY_or:
8338             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
8339                 return REPORT(0);
8340             pl_yylval.ival = OP_OR;
8341             OPERATOR(OROP);
8342
8343         case KEY_ord:
8344             UNI(OP_ORD);
8345
8346         case KEY_oct:
8347             UNI(OP_OCT);
8348
8349         case KEY_opendir:
8350             LOP(OP_OPEN_DIR,XTERM);
8351
8352         case KEY_print:
8353             checkcomma(s,PL_tokenbuf,"filehandle");
8354             LOP(OP_PRINT,XREF);
8355
8356         case KEY_printf:
8357             checkcomma(s,PL_tokenbuf,"filehandle");
8358             LOP(OP_PRTF,XREF);
8359
8360         case KEY_prototype:
8361             UNI(OP_PROTOTYPE);
8362
8363         case KEY_push:
8364             LOP(OP_PUSH,XTERM);
8365
8366         case KEY_pop:
8367             UNIDOR(OP_POP);
8368
8369         case KEY_pos:
8370             UNIDOR(OP_POS);
8371
8372         case KEY_pack:
8373             LOP(OP_PACK,XTERM);
8374
8375         case KEY_package:
8376             s = force_word(s,BAREWORD,FALSE,TRUE);
8377             s = skipspace(s);
8378             s = force_strict_version(s);
8379             PREBLOCK(PACKAGE);
8380
8381         case KEY_pipe:
8382             LOP(OP_PIPE_OP,XTERM);
8383
8384         case KEY_q:
8385             s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8386             if (!s)
8387                 missingterm(NULL, 0);
8388             COPLINE_SET_FROM_MULTI_END;
8389             pl_yylval.ival = OP_CONST;
8390             TERM(sublex_start());
8391
8392         case KEY_quotemeta:
8393             UNI(OP_QUOTEMETA);
8394
8395         case KEY_qw: {
8396             OP *words = NULL;
8397             s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8398             if (!s)
8399                 missingterm(NULL, 0);
8400             COPLINE_SET_FROM_MULTI_END;
8401             PL_expect = XOPERATOR;
8402             if (SvCUR(PL_lex_stuff)) {
8403                 int warned_comma = !ckWARN(WARN_QW);
8404                 int warned_comment = warned_comma;
8405                 d = SvPV_force(PL_lex_stuff, len);
8406                 while (len) {
8407                     for (; isSPACE(*d) && len; --len, ++d)
8408                         /**/;
8409                     if (len) {
8410                         SV *sv;
8411                         const char *b = d;
8412                         if (!warned_comma || !warned_comment) {
8413                             for (; !isSPACE(*d) && len; --len, ++d) {
8414                                 if (!warned_comma && *d == ',') {
8415                                     Perl_warner(aTHX_ packWARN(WARN_QW),
8416                                         "Possible attempt to separate words with commas");
8417                                     ++warned_comma;
8418                                 }
8419                                 else if (!warned_comment && *d == '#') {
8420                                     Perl_warner(aTHX_ packWARN(WARN_QW),
8421                                         "Possible attempt to put comments in qw() list");
8422                                     ++warned_comment;
8423                                 }
8424                             }
8425                         }
8426                         else {
8427                             for (; !isSPACE(*d) && len; --len, ++d)
8428                                 /**/;
8429                         }
8430                         sv = newSVpvn_utf8(b, d-b, DO_UTF8(PL_lex_stuff));
8431                         words = op_append_elem(OP_LIST, words,
8432                                             newSVOP(OP_CONST, 0, tokeq(sv)));
8433                     }
8434                 }
8435             }
8436             if (!words)
8437                 words = newNULLLIST();
8438             SvREFCNT_dec_NN(PL_lex_stuff);
8439             PL_lex_stuff = NULL;
8440             PL_expect = XOPERATOR;
8441             pl_yylval.opval = sawparens(words);
8442             TOKEN(QWLIST);
8443         }
8444
8445         case KEY_qq:
8446             s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8447             if (!s)
8448                 missingterm(NULL, 0);
8449             pl_yylval.ival = OP_STRINGIFY;
8450             if (SvIVX(PL_lex_stuff) == '\'')
8451                 SvIV_set(PL_lex_stuff, 0);      /* qq'$foo' should interpolate */
8452             TERM(sublex_start());
8453
8454         case KEY_qr:
8455             s = scan_pat(s,OP_QR);
8456             TERM(sublex_start());
8457
8458         case KEY_qx:
8459             s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8460             if (!s)
8461                 missingterm(NULL, 0);
8462             pl_yylval.ival = OP_BACKTICK;
8463             TERM(sublex_start());
8464
8465         case KEY_return:
8466             OLDLOP(OP_RETURN);
8467
8468         case KEY_require:
8469             s = skipspace(s);
8470             if (isDIGIT(*s)) {
8471                 s = force_version(s, FALSE);
8472             }
8473             else if (*s != 'v' || !isDIGIT(s[1])
8474                     || (s = force_version(s, TRUE), *s == 'v'))
8475             {
8476                 *PL_tokenbuf = '\0';
8477                 s = force_word(s,BAREWORD,TRUE,TRUE);
8478                 if (isIDFIRST_lazy_if_safe(PL_tokenbuf,
8479                                            PL_tokenbuf + sizeof(PL_tokenbuf),
8480                                            UTF))
8481                 {
8482                     gv_stashpvn(PL_tokenbuf, strlen(PL_tokenbuf),
8483                                 GV_ADD | (UTF ? SVf_UTF8 : 0));
8484                 }
8485                 else if (*s == '<')
8486                     yyerror("<> at require-statement should be quotes");
8487             }
8488             if (orig_keyword == KEY_require) {
8489                 orig_keyword = 0;
8490                 pl_yylval.ival = 1;
8491             }
8492             else
8493                 pl_yylval.ival = 0;
8494             PL_expect = PL_nexttoke ? XOPERATOR : XTERM;
8495             PL_bufptr = s;
8496             PL_last_uni = PL_oldbufptr;
8497             PL_last_lop_op = OP_REQUIRE;
8498             s = skipspace(s);
8499             return REPORT( (int)REQUIRE );
8500
8501         case KEY_reset:
8502             UNI(OP_RESET);
8503
8504         case KEY_redo:
8505             LOOPX(OP_REDO);
8506
8507         case KEY_rename:
8508             LOP(OP_RENAME,XTERM);
8509
8510         case KEY_rand:
8511             UNI(OP_RAND);
8512
8513         case KEY_rmdir:
8514             UNI(OP_RMDIR);
8515
8516         case KEY_rindex:
8517             LOP(OP_RINDEX,XTERM);
8518
8519         case KEY_read:
8520             LOP(OP_READ,XTERM);
8521
8522         case KEY_readdir:
8523             UNI(OP_READDIR);
8524
8525         case KEY_readline:
8526             UNIDOR(OP_READLINE);
8527
8528         case KEY_readpipe:
8529             UNIDOR(OP_BACKTICK);
8530
8531         case KEY_rewinddir:
8532             UNI(OP_REWINDDIR);
8533
8534         case KEY_recv:
8535             LOP(OP_RECV,XTERM);
8536
8537         case KEY_reverse:
8538             LOP(OP_REVERSE,XTERM);
8539
8540         case KEY_readlink:
8541             UNIDOR(OP_READLINK);
8542
8543         case KEY_ref:
8544             UNI(OP_REF);
8545
8546         case KEY_s:
8547             s = scan_subst(s);
8548             if (pl_yylval.opval)
8549                 TERM(sublex_start());
8550             else
8551                 TOKEN(1);       /* force error */
8552
8553         case KEY_say:
8554             checkcomma(s,PL_tokenbuf,"filehandle");
8555             LOP(OP_SAY,XREF);
8556
8557         case KEY_chomp:
8558             UNI(OP_CHOMP);
8559
8560         case KEY_scalar:
8561             UNI(OP_SCALAR);
8562
8563         case KEY_select:
8564             LOP(OP_SELECT,XTERM);
8565
8566         case KEY_seek:
8567             LOP(OP_SEEK,XTERM);
8568
8569         case KEY_semctl:
8570             LOP(OP_SEMCTL,XTERM);
8571
8572         case KEY_semget:
8573             LOP(OP_SEMGET,XTERM);
8574
8575         case KEY_semop:
8576             LOP(OP_SEMOP,XTERM);
8577
8578         case KEY_send:
8579             LOP(OP_SEND,XTERM);
8580
8581         case KEY_setpgrp:
8582             LOP(OP_SETPGRP,XTERM);
8583
8584         case KEY_setpriority:
8585             LOP(OP_SETPRIORITY,XTERM);
8586
8587         case KEY_sethostent:
8588             UNI(OP_SHOSTENT);
8589
8590         case KEY_setnetent:
8591             UNI(OP_SNETENT);
8592
8593         case KEY_setservent:
8594             UNI(OP_SSERVENT);
8595
8596         case KEY_setprotoent:
8597             UNI(OP_SPROTOENT);
8598
8599         case KEY_setpwent:
8600             FUN0(OP_SPWENT);
8601
8602         case KEY_setgrent:
8603             FUN0(OP_SGRENT);
8604
8605         case KEY_seekdir:
8606             LOP(OP_SEEKDIR,XTERM);
8607
8608         case KEY_setsockopt:
8609             LOP(OP_SSOCKOPT,XTERM);
8610
8611         case KEY_shift:
8612             UNIDOR(OP_SHIFT);
8613
8614         case KEY_shmctl:
8615             LOP(OP_SHMCTL,XTERM);
8616
8617         case KEY_shmget:
8618             LOP(OP_SHMGET,XTERM);
8619
8620         case KEY_shmread:
8621             LOP(OP_SHMREAD,XTERM);
8622
8623         case KEY_shmwrite:
8624             LOP(OP_SHMWRITE,XTERM);
8625
8626         case KEY_shutdown:
8627             LOP(OP_SHUTDOWN,XTERM);
8628
8629         case KEY_sin:
8630             UNI(OP_SIN);
8631
8632         case KEY_sleep:
8633             UNI(OP_SLEEP);
8634
8635         case KEY_socket:
8636             LOP(OP_SOCKET,XTERM);
8637
8638         case KEY_socketpair:
8639             LOP(OP_SOCKPAIR,XTERM);
8640
8641         case KEY_sort:
8642             checkcomma(s,PL_tokenbuf,"subroutine name");
8643             s = skipspace(s);
8644             PL_expect = XTERM;
8645             s = force_word(s,BAREWORD,TRUE,TRUE);
8646             LOP(OP_SORT,XREF);
8647
8648         case KEY_split:
8649             LOP(OP_SPLIT,XTERM);
8650
8651         case KEY_sprintf:
8652             LOP(OP_SPRINTF,XTERM);
8653
8654         case KEY_splice:
8655             LOP(OP_SPLICE,XTERM);
8656
8657         case KEY_sqrt:
8658             UNI(OP_SQRT);
8659
8660         case KEY_srand:
8661             UNI(OP_SRAND);
8662
8663         case KEY_stat:
8664             UNI(OP_STAT);
8665
8666         case KEY_study:
8667             UNI(OP_STUDY);
8668
8669         case KEY_substr:
8670             LOP(OP_SUBSTR,XTERM);
8671
8672         case KEY_format:
8673         case KEY_sub:
8674           really_sub:
8675             {
8676                 char * const tmpbuf = PL_tokenbuf + 1;
8677                 bool have_name, have_proto;
8678                 const int key = tmp;
8679                 SV *format_name = NULL;
8680                 bool is_sigsub = FEATURE_SIGNATURES_IS_ENABLED;
8681
8682                 SSize_t off = s-SvPVX(PL_linestr);
8683                 s = skipspace(s);
8684                 d = SvPVX(PL_linestr)+off;
8685
8686                 SAVEBOOL(PL_parser->sig_seen);
8687                 PL_parser->sig_seen = FALSE;
8688
8689                 if (   isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)
8690                     || *s == '\''
8691                     || (*s == ':' && s[1] == ':'))
8692                 {
8693
8694                     PL_expect = XATTRBLOCK;
8695                     d = scan_word(s, tmpbuf, sizeof PL_tokenbuf - 1, TRUE,
8696                                   &len);
8697                     if (key == KEY_format)
8698                         format_name = S_newSV_maybe_utf8(aTHX_ s, d - s);
8699                     *PL_tokenbuf = '&';
8700                     if (memchr(tmpbuf, ':', len) || key != KEY_sub
8701                      || pad_findmy_pvn(
8702                             PL_tokenbuf, len + 1, 0
8703                         ) != NOT_IN_PAD)
8704                         sv_setpvn(PL_subname, tmpbuf, len);
8705                     else {
8706                         sv_setsv(PL_subname,PL_curstname);
8707                         sv_catpvs(PL_subname,"::");
8708                         sv_catpvn(PL_subname,tmpbuf,len);
8709                     }
8710                     if (SvUTF8(PL_linestr))
8711                         SvUTF8_on(PL_subname);
8712                     have_name = TRUE;
8713
8714
8715                     s = skipspace(d);
8716                 }
8717                 else {
8718                     if (key == KEY_my || key == KEY_our || key==KEY_state)
8719                     {
8720                         *d = '\0';
8721                         /* diag_listed_as: Missing name in "%s sub" */
8722                         Perl_croak(aTHX_
8723                                   "Missing name in \"%s\"", PL_bufptr);
8724                     }
8725                     PL_expect = XATTRTERM;
8726                     sv_setpvs(PL_subname,"?");
8727                     have_name = FALSE;
8728                 }
8729
8730                 if (key == KEY_format) {
8731                     if (format_name) {
8732                         NEXTVAL_NEXTTOKE.opval
8733                             = newSVOP(OP_CONST,0, format_name);
8734                         NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
8735                         force_next(BAREWORD);
8736                     }
8737                     PREBLOCK(FORMAT);
8738                 }
8739
8740                 /* Look for a prototype */
8741                 if (*s == '(' && !is_sigsub) {
8742                     s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8743                     if (!s)
8744                         Perl_croak(aTHX_ "Prototype not terminated");
8745                     COPLINE_SET_FROM_MULTI_END;
8746                     (void)validate_proto(PL_subname, PL_lex_stuff,
8747                                          ckWARN(WARN_ILLEGALPROTO), 0);
8748                     have_proto = TRUE;
8749
8750                     s = skipspace(s);
8751                 }
8752                 else
8753                     have_proto = FALSE;
8754
8755                 if (  !(*s == ':' && s[1] != ':')
8756                     && (*s != '{' && *s != '(') && key != KEY_format)
8757                 {
8758                     assert(key == KEY_sub || key == KEY_AUTOLOAD ||
8759                            key == KEY_DESTROY || key == KEY_BEGIN ||
8760                            key == KEY_UNITCHECK || key == KEY_CHECK ||
8761                            key == KEY_INIT || key == KEY_END ||
8762                            key == KEY_my || key == KEY_state ||
8763                            key == KEY_our);
8764                     if (!have_name)
8765                         Perl_croak(aTHX_ "Illegal declaration of anonymous subroutine");
8766                     else if (*s != ';' && *s != '}')
8767                         Perl_croak(aTHX_ "Illegal declaration of subroutine %" SVf, SVfARG(PL_subname));
8768                 }
8769
8770                 if (have_proto) {
8771                     NEXTVAL_NEXTTOKE.opval =
8772                         newSVOP(OP_CONST, 0, PL_lex_stuff);
8773                     PL_lex_stuff = NULL;
8774                     force_next(THING);
8775                 }
8776                 if (!have_name) {
8777                     if (PL_curstash)
8778                         sv_setpvs(PL_subname, "__ANON__");
8779                     else
8780                         sv_setpvs(PL_subname, "__ANON__::__ANON__");
8781                     if (is_sigsub)
8782                         TOKEN(ANON_SIGSUB);
8783                     else
8784                         TOKEN(ANONSUB);
8785                 }
8786                 force_ident_maybe_lex('&');
8787                 if (is_sigsub)
8788                     TOKEN(SIGSUB);
8789                 else
8790                     TOKEN(SUB);
8791             }
8792
8793         case KEY_system:
8794             LOP(OP_SYSTEM,XREF);
8795
8796         case KEY_symlink:
8797             LOP(OP_SYMLINK,XTERM);
8798
8799         case KEY_syscall:
8800             LOP(OP_SYSCALL,XTERM);
8801
8802         case KEY_sysopen:
8803             LOP(OP_SYSOPEN,XTERM);
8804
8805         case KEY_sysseek:
8806             LOP(OP_SYSSEEK,XTERM);
8807
8808         case KEY_sysread:
8809             LOP(OP_SYSREAD,XTERM);
8810
8811         case KEY_syswrite:
8812             LOP(OP_SYSWRITE,XTERM);
8813
8814         case KEY_tr:
8815         case KEY_y:
8816             s = scan_trans(s);
8817             TERM(sublex_start());
8818
8819         case KEY_tell:
8820             UNI(OP_TELL);
8821
8822         case KEY_telldir:
8823             UNI(OP_TELLDIR);
8824
8825         case KEY_tie:
8826             LOP(OP_TIE,XTERM);
8827
8828         case KEY_tied:
8829             UNI(OP_TIED);
8830
8831         case KEY_time:
8832             FUN0(OP_TIME);
8833
8834         case KEY_times:
8835             FUN0(OP_TMS);
8836
8837         case KEY_truncate:
8838             LOP(OP_TRUNCATE,XTERM);
8839
8840         case KEY_uc:
8841             UNI(OP_UC);
8842
8843         case KEY_ucfirst:
8844             UNI(OP_UCFIRST);
8845
8846         case KEY_untie:
8847             UNI(OP_UNTIE);
8848
8849         case KEY_until:
8850             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8851                 return REPORT(0);
8852             pl_yylval.ival = CopLINE(PL_curcop);
8853             OPERATOR(UNTIL);
8854
8855         case KEY_unless:
8856             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8857                 return REPORT(0);
8858             pl_yylval.ival = CopLINE(PL_curcop);
8859             OPERATOR(UNLESS);
8860
8861         case KEY_unlink:
8862             LOP(OP_UNLINK,XTERM);
8863
8864         case KEY_undef:
8865             UNIDOR(OP_UNDEF);
8866
8867         case KEY_unpack:
8868             LOP(OP_UNPACK,XTERM);
8869
8870         case KEY_utime:
8871             LOP(OP_UTIME,XTERM);
8872
8873         case KEY_umask:
8874             UNIDOR(OP_UMASK);
8875
8876         case KEY_unshift:
8877             LOP(OP_UNSHIFT,XTERM);
8878
8879         case KEY_use:
8880             s = tokenize_use(1, s);
8881             TOKEN(USE);
8882
8883         case KEY_values:
8884             UNI(OP_VALUES);
8885
8886         case KEY_vec:
8887             LOP(OP_VEC,XTERM);
8888
8889         case KEY_when:
8890             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8891                 return REPORT(0);
8892             pl_yylval.ival = CopLINE(PL_curcop);
8893             Perl_ck_warner_d(aTHX_
8894                 packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
8895                 "when is experimental");
8896             OPERATOR(WHEN);
8897
8898         case KEY_while:
8899             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8900                 return REPORT(0);
8901             pl_yylval.ival = CopLINE(PL_curcop);
8902             OPERATOR(WHILE);
8903
8904         case KEY_warn:
8905             PL_hints |= HINT_BLOCK_SCOPE;
8906             LOP(OP_WARN,XTERM);
8907
8908         case KEY_wait:
8909             FUN0(OP_WAIT);
8910
8911         case KEY_waitpid:
8912             LOP(OP_WAITPID,XTERM);
8913
8914         case KEY_wantarray:
8915             FUN0(OP_WANTARRAY);
8916
8917         case KEY_write:
8918             /* Make sure $^L is defined. 0x0C is CTRL-L on ASCII platforms, and
8919              * we use the same number on EBCDIC */
8920             gv_fetchpvs("\x0C", GV_ADD|GV_NOTQUAL, SVt_PV);
8921             UNI(OP_ENTERWRITE);
8922
8923         case KEY_x:
8924             if (PL_expect == XOPERATOR) {
8925                 if (*s == '=' && !PL_lex_allbrackets
8926                     && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
8927                 {
8928                     return REPORT(0);
8929                 }
8930                 Mop(OP_REPEAT);
8931             }
8932             check_uni();
8933             goto just_a_word;
8934
8935         case KEY_xor:
8936             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
8937                 return REPORT(0);
8938             pl_yylval.ival = OP_XOR;
8939             OPERATOR(OROP);
8940         }
8941     }}
8942 }
8943
8944 /*
8945   S_pending_ident
8946
8947   Looks up an identifier in the pad or in a package
8948
8949   is_sig indicates that this is a subroutine signature variable
8950   rather than a plain pad var.
8951
8952   Returns:
8953     PRIVATEREF if this is a lexical name.
8954     BAREWORD   if this belongs to a package.
8955
8956   Structure:
8957       if we're in a my declaration
8958           croak if they tried to say my($foo::bar)
8959           build the ops for a my() declaration
8960       if it's an access to a my() variable
8961           build ops for access to a my() variable
8962       if in a dq string, and they've said @foo and we can't find @foo
8963           warn
8964       build ops for a bareword
8965 */
8966
8967 static int
8968 S_pending_ident(pTHX)
8969 {
8970     PADOFFSET tmp = 0;
8971     const char pit = (char)pl_yylval.ival;
8972     const STRLEN tokenbuf_len = strlen(PL_tokenbuf);
8973     /* All routes through this function want to know if there is a colon.  */
8974     const char *const has_colon = (const char*) memchr (PL_tokenbuf, ':', tokenbuf_len);
8975
8976     DEBUG_T({ PerlIO_printf(Perl_debug_log,
8977           "### Pending identifier '%s'\n", PL_tokenbuf); });
8978     assert(tokenbuf_len >= 2);
8979
8980     /* if we're in a my(), we can't allow dynamics here.
8981        $foo'bar has already been turned into $foo::bar, so
8982        just check for colons.
8983
8984        if it's a legal name, the OP is a PADANY.
8985     */
8986     if (PL_in_my) {
8987         if (PL_in_my == KEY_our) {      /* "our" is merely analogous to "my" */
8988             if (has_colon)
8989                 /* diag_listed_as: No package name allowed for variable %s
8990                                    in "our" */
8991                 yyerror_pv(Perl_form(aTHX_ "No package name allowed for "
8992                                   "%se %s in \"our\"",
8993                                   *PL_tokenbuf=='&' ?"subroutin":"variabl",
8994                                   PL_tokenbuf), UTF ? SVf_UTF8 : 0);
8995             tmp = allocmy(PL_tokenbuf, tokenbuf_len, UTF ? SVf_UTF8 : 0);
8996         }
8997         else {
8998             OP *o;
8999             if (has_colon) {
9000                 /* "my" variable %s can't be in a package */
9001                 /* PL_no_myglob is constant */
9002                 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
9003                 yyerror_pv(Perl_form(aTHX_ PL_no_myglob,
9004                             PL_in_my == KEY_my ? "my" : "state",
9005                             *PL_tokenbuf == '&' ? "subroutin" : "variabl",
9006                             PL_tokenbuf),
9007                             UTF ? SVf_UTF8 : 0);
9008                 GCC_DIAG_RESTORE_STMT;
9009             }
9010
9011             if (PL_in_my == KEY_sigvar) {
9012                 /* A signature 'padop' needs in addition, an op_first to
9013                  * point to a child sigdefelem, and an extra field to hold
9014                  * the signature index. We can achieve both by using an
9015                  * UNOP_AUX and (ab)using the op_aux field to hold the
9016                  * index. If we ever need more fields, use a real malloced
9017                  * aux strut instead.
9018                  */
9019                 o = newUNOP_AUX(OP_ARGELEM, 0, NULL,
9020                                     INT2PTR(UNOP_AUX_item *,
9021                                         (PL_parser->sig_elems)));
9022                 o->op_private |= (  PL_tokenbuf[0] == '$' ? OPpARGELEM_SV
9023                                   : PL_tokenbuf[0] == '@' ? OPpARGELEM_AV
9024                                   :                         OPpARGELEM_HV);
9025             }
9026             else
9027                 o = newOP(OP_PADANY, 0);
9028             o->op_targ = allocmy(PL_tokenbuf, tokenbuf_len,
9029                                                         UTF ? SVf_UTF8 : 0);
9030             if (PL_in_my == KEY_sigvar)
9031                 PL_in_my = 0;
9032
9033             pl_yylval.opval = o;
9034             return PRIVATEREF;
9035         }
9036     }
9037
9038     /*
9039        build the ops for accesses to a my() variable.
9040     */
9041
9042     if (!has_colon) {
9043         if (!PL_in_my)
9044             tmp = pad_findmy_pvn(PL_tokenbuf, tokenbuf_len,
9045                                  0);
9046         if (tmp != NOT_IN_PAD) {
9047             /* might be an "our" variable" */
9048             if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
9049                 /* build ops for a bareword */
9050                 HV *  const stash = PAD_COMPNAME_OURSTASH(tmp);
9051                 HEK * const stashname = HvNAME_HEK(stash);
9052                 SV *  const sym = newSVhek(stashname);
9053                 sv_catpvs(sym, "::");
9054                 sv_catpvn_flags(sym, PL_tokenbuf+1, tokenbuf_len > 0 ? tokenbuf_len - 1 : 0, (UTF ? SV_CATUTF8 : SV_CATBYTES ));
9055                 pl_yylval.opval = newSVOP(OP_CONST, 0, sym);
9056                 pl_yylval.opval->op_private = OPpCONST_ENTERED;
9057                 if (pit != '&')
9058                   gv_fetchsv(sym,
9059                     GV_ADDMULTI,
9060                     ((PL_tokenbuf[0] == '$') ? SVt_PV
9061                      : (PL_tokenbuf[0] == '@') ? SVt_PVAV
9062                      : SVt_PVHV));
9063                 return BAREWORD;
9064             }
9065
9066             pl_yylval.opval = newOP(OP_PADANY, 0);
9067             pl_yylval.opval->op_targ = tmp;
9068             return PRIVATEREF;
9069         }
9070     }
9071
9072     /*
9073        Whine if they've said @foo or @foo{key} in a doublequoted string,
9074        and @foo (or %foo) isn't a variable we can find in the symbol
9075        table.
9076     */
9077     if (ckWARN(WARN_AMBIGUOUS)
9078         && pit == '@'
9079         && PL_lex_state != LEX_NORMAL
9080         && !PL_lex_brackets)
9081     {
9082         GV *const gv = gv_fetchpvn_flags(PL_tokenbuf + 1, tokenbuf_len > 0 ? tokenbuf_len - 1 : 0,
9083                                          ( UTF ? SVf_UTF8 : 0 ) | GV_ADDMG,
9084                                          SVt_PVAV);
9085         if ((!gv || ((PL_tokenbuf[0] == '@') ? !GvAV(gv) : !GvHV(gv)))
9086            )
9087         {
9088             /* Downgraded from fatal to warning 20000522 mjd */
9089             Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
9090                         "Possible unintended interpolation of %" UTF8f
9091                         " in string",
9092                         UTF8fARG(UTF, tokenbuf_len, PL_tokenbuf));
9093         }
9094     }
9095
9096     /* build ops for a bareword */
9097     pl_yylval.opval = newSVOP(OP_CONST, 0,
9098                                    newSVpvn_flags(PL_tokenbuf + 1,
9099                                                       tokenbuf_len > 0 ? tokenbuf_len - 1 : 0,
9100                                                       UTF ? SVf_UTF8 : 0 ));
9101     pl_yylval.opval->op_private = OPpCONST_ENTERED;
9102     if (pit != '&')
9103         gv_fetchpvn_flags(PL_tokenbuf+1, tokenbuf_len > 0 ? tokenbuf_len - 1 : 0,
9104                      (PL_in_eval ? GV_ADDMULTI : GV_ADD)
9105                      | ( UTF ? SVf_UTF8 : 0 ),
9106                      ((PL_tokenbuf[0] == '$') ? SVt_PV
9107                       : (PL_tokenbuf[0] == '@') ? SVt_PVAV
9108                       : SVt_PVHV));
9109     return BAREWORD;
9110 }
9111
9112 STATIC void
9113 S_checkcomma(pTHX_ const char *s, const char *name, const char *what)
9114 {
9115     PERL_ARGS_ASSERT_CHECKCOMMA;
9116
9117     if (*s == ' ' && s[1] == '(') {     /* XXX gotta be a better way */
9118         if (ckWARN(WARN_SYNTAX)) {
9119             int level = 1;
9120             const char *w;
9121             for (w = s+2; *w && level; w++) {
9122                 if (*w == '(')
9123                     ++level;
9124                 else if (*w == ')')
9125                     --level;
9126             }
9127             while (isSPACE(*w))
9128                 ++w;
9129             /* the list of chars below is for end of statements or
9130              * block / parens, boolean operators (&&, ||, //) and branch
9131              * constructs (or, and, if, until, unless, while, err, for).
9132              * Not a very solid hack... */
9133             if (!*w || !strchr(";&/|})]oaiuwef!=", *w))
9134                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9135                             "%s (...) interpreted as function",name);
9136         }
9137     }
9138     while (s < PL_bufend && isSPACE(*s))
9139         s++;
9140     if (*s == '(')
9141         s++;
9142     while (s < PL_bufend && isSPACE(*s))
9143         s++;
9144     if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
9145         const char * const w = s;
9146         s += UTF ? UTF8SKIP(s) : 1;
9147         while (isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF))
9148             s += UTF ? UTF8SKIP(s) : 1;
9149         while (s < PL_bufend && isSPACE(*s))
9150             s++;
9151         if (*s == ',') {
9152             GV* gv;
9153             if (keyword(w, s - w, 0))
9154                 return;
9155
9156             gv = gv_fetchpvn_flags(w, s - w, ( UTF ? SVf_UTF8 : 0 ), SVt_PVCV);
9157             if (gv && GvCVu(gv))
9158                 return;
9159             if (s - w <= 254) {
9160                 PADOFFSET off;
9161                 char tmpbuf[256];
9162                 Copy(w, tmpbuf+1, s - w, char);
9163                 *tmpbuf = '&';
9164                 off = pad_findmy_pvn(tmpbuf, s-w+1, 0);
9165                 if (off != NOT_IN_PAD) return;
9166             }
9167             Perl_croak(aTHX_ "No comma allowed after %s", what);
9168         }
9169     }
9170 }
9171
9172 /* S_new_constant(): do any overload::constant lookup.
9173
9174    Either returns sv, or mortalizes/frees sv and returns a new SV*.
9175    Best used as sv=new_constant(..., sv, ...).
9176    If s, pv are NULL, calls subroutine with one argument,
9177    and <type> is used with error messages only.
9178    <type> is assumed to be well formed UTF-8 */
9179
9180 STATIC SV *
9181 S_new_constant(pTHX_ const char *s, STRLEN len, const char *key, STRLEN keylen,
9182                SV *sv, SV *pv, const char *type, STRLEN typelen)
9183 {
9184     dSP;
9185     HV * table = GvHV(PL_hintgv);                /* ^H */
9186     SV *res;
9187     SV *errsv = NULL;
9188     SV **cvp;
9189     SV *cv, *typesv;
9190     const char *why1 = "", *why2 = "", *why3 = "";
9191
9192     PERL_ARGS_ASSERT_NEW_CONSTANT;
9193     /* We assume that this is true: */
9194     if (*key == 'c') { assert (strEQ(key, "charnames")); }
9195     assert(type || s);
9196
9197     /* charnames doesn't work well if there have been errors found */
9198     if (PL_error_count > 0 && *key == 'c')
9199     {
9200         SvREFCNT_dec_NN(sv);
9201         return &PL_sv_undef;
9202     }
9203
9204     sv_2mortal(sv);                     /* Parent created it permanently */
9205     if (!table
9206         || ! (PL_hints & HINT_LOCALIZE_HH)
9207         || ! (cvp = hv_fetch(table, key, keylen, FALSE))
9208         || ! SvOK(*cvp))
9209     {
9210         char *msg;
9211
9212         /* Here haven't found what we're looking for.  If it is charnames,
9213          * perhaps it needs to be loaded.  Try doing that before giving up */
9214         if (*key == 'c') {
9215             Perl_load_module(aTHX_
9216                             0,
9217                             newSVpvs("_charnames"),
9218                              /* version parameter; no need to specify it, as if
9219                               * we get too early a version, will fail anyway,
9220                               * not being able to find '_charnames' */
9221                             NULL,
9222                             newSVpvs(":full"),
9223                             newSVpvs(":short"),
9224                             NULL);
9225             assert(sp == PL_stack_sp);
9226             table = GvHV(PL_hintgv);
9227             if (table
9228                 && (PL_hints & HINT_LOCALIZE_HH)
9229                 && (cvp = hv_fetch(table, key, keylen, FALSE))
9230                 && SvOK(*cvp))
9231             {
9232                 goto now_ok;
9233             }
9234         }
9235         if (!table || !(PL_hints & HINT_LOCALIZE_HH)) {
9236             msg = Perl_form(aTHX_
9237                                "Constant(%.*s) unknown",
9238                                 (int)(type ? typelen : len),
9239                                 (type ? type: s));
9240         }
9241         else {
9242             why1 = "$^H{";
9243             why2 = key;
9244             why3 = "} is not defined";
9245         report:
9246             if (*key == 'c') {
9247                 msg = Perl_form(aTHX_
9248                             /* The +3 is for '\N{'; -4 for that, plus '}' */
9249                             "Unknown charname '%.*s'", (int)typelen - 4, type + 3
9250                       );
9251             }
9252             else {
9253                 msg = Perl_form(aTHX_ "Constant(%.*s): %s%s%s",
9254                                     (int)(type ? typelen : len),
9255                                     (type ? type: s), why1, why2, why3);
9256             }
9257         }
9258         yyerror_pv(msg, UTF ? SVf_UTF8 : 0);
9259         return SvREFCNT_inc_simple_NN(sv);
9260     }
9261   now_ok:
9262     cv = *cvp;
9263     if (!pv && s)
9264         pv = newSVpvn_flags(s, len, SVs_TEMP);
9265     if (type && pv)
9266         typesv = newSVpvn_flags(type, typelen, SVs_TEMP);
9267     else
9268         typesv = &PL_sv_undef;
9269
9270     PUSHSTACKi(PERLSI_OVERLOAD);
9271     ENTER ;
9272     SAVETMPS;
9273
9274     PUSHMARK(SP) ;
9275     EXTEND(sp, 3);
9276     if (pv)
9277         PUSHs(pv);
9278     PUSHs(sv);
9279     if (pv)
9280         PUSHs(typesv);
9281     PUTBACK;
9282     call_sv(cv, G_SCALAR | ( PL_in_eval ? 0 : G_EVAL));
9283
9284     SPAGAIN ;
9285
9286     /* Check the eval first */
9287     if (!PL_in_eval && ((errsv = ERRSV), SvTRUE_NN(errsv))) {
9288         STRLEN errlen;
9289         const char * errstr;
9290         sv_catpvs(errsv, "Propagated");
9291         errstr = SvPV_const(errsv, errlen);
9292         yyerror_pvn(errstr, errlen, 0); /* Duplicates the message inside eval */
9293         (void)POPs;
9294         res = SvREFCNT_inc_simple_NN(sv);
9295     }
9296     else {
9297         res = POPs;
9298         SvREFCNT_inc_simple_void_NN(res);
9299     }
9300
9301     PUTBACK ;
9302     FREETMPS ;
9303     LEAVE ;
9304     POPSTACK;
9305
9306     if (!SvOK(res)) {
9307         why1 = "Call to &{$^H{";
9308         why2 = key;
9309         why3 = "}} did not return a defined value";
9310         sv = res;
9311         (void)sv_2mortal(sv);
9312         goto report;
9313     }
9314
9315     return res;
9316 }
9317
9318 PERL_STATIC_INLINE void
9319 S_parse_ident(pTHX_ char **s, char **d, char * const e, int allow_package,
9320                     bool is_utf8, bool check_dollar, bool tick_warn)
9321 {
9322     int saw_tick = 0;
9323     const char *olds = *s;
9324     PERL_ARGS_ASSERT_PARSE_IDENT;
9325
9326     while (*s < PL_bufend) {
9327         if (*d >= e)
9328             Perl_croak(aTHX_ "%s", ident_too_long);
9329         if (is_utf8 && isIDFIRST_utf8_safe(*s, PL_bufend)) {
9330              /* The UTF-8 case must come first, otherwise things
9331              * like c\N{COMBINING TILDE} would start failing, as the
9332              * isWORDCHAR_A case below would gobble the 'c' up.
9333              */
9334
9335             char *t = *s + UTF8SKIP(*s);
9336             while (isIDCONT_utf8_safe((const U8*) t, (const U8*) PL_bufend)) {
9337                 t += UTF8SKIP(t);
9338             }
9339             if (*d + (t - *s) > e)
9340                 Perl_croak(aTHX_ "%s", ident_too_long);
9341             Copy(*s, *d, t - *s, char);
9342             *d += t - *s;
9343             *s = t;
9344         }
9345         else if ( isWORDCHAR_A(**s) ) {
9346             do {
9347                 *(*d)++ = *(*s)++;
9348             } while (isWORDCHAR_A(**s) && *d < e);
9349         }
9350         else if (   allow_package
9351                  && **s == '\''
9352                  && isIDFIRST_lazy_if_safe((*s)+1, PL_bufend, is_utf8))
9353         {
9354             *(*d)++ = ':';
9355             *(*d)++ = ':';
9356             (*s)++;
9357             saw_tick++;
9358         }
9359         else if (allow_package && **s == ':' && (*s)[1] == ':'
9360            /* Disallow things like Foo::$bar. For the curious, this is
9361             * the code path that triggers the "Bad name after" warning
9362             * when looking for barewords.
9363             */
9364            && !(check_dollar && (*s)[2] == '$')) {
9365             *(*d)++ = *(*s)++;
9366             *(*d)++ = *(*s)++;
9367         }
9368         else
9369             break;
9370     }
9371     if (UNLIKELY(tick_warn && saw_tick && PL_lex_state == LEX_INTERPNORMAL
9372               && !PL_lex_brackets && ckWARN(WARN_SYNTAX))) {
9373         char *d;
9374         char *d2;
9375         Newx(d, *s - olds + saw_tick + 2, char); /* +2 for $# */
9376         d2 = d;
9377         SAVEFREEPV(d);
9378         Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9379                          "Old package separator used in string");
9380         if (olds[-1] == '#')
9381             *d2++ = olds[-2];
9382         *d2++ = olds[-1];
9383         while (olds < *s) {
9384             if (*olds == '\'') {
9385                 *d2++ = '\\';
9386                 *d2++ = *olds++;
9387             }
9388             else
9389                 *d2++ = *olds++;
9390         }
9391         Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9392                          "\t(Did you mean \"%" UTF8f "\" instead?)\n",
9393                           UTF8fARG(is_utf8, d2-d, d));
9394     }
9395     return;
9396 }
9397
9398 /* Returns a NUL terminated string, with the length of the string written to
9399    *slp
9400    */
9401 STATIC char *
9402 S_scan_word(pTHX_ char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp)
9403 {
9404     char *d = dest;
9405     char * const e = d + destlen - 3;  /* two-character token, ending NUL */
9406     bool is_utf8 = cBOOL(UTF);
9407
9408     PERL_ARGS_ASSERT_SCAN_WORD;
9409
9410     parse_ident(&s, &d, e, allow_package, is_utf8, TRUE, FALSE);
9411     *d = '\0';
9412     *slp = d - dest;
9413     return s;
9414 }
9415
9416 /* Is the byte 'd' a legal single character identifier name?  'u' is true
9417  * iff Unicode semantics are to be used.  The legal ones are any of:
9418  *  a) all ASCII characters except:
9419  *          1) control and space-type ones, like NUL, SOH, \t, and SPACE;
9420  *          2) '{'
9421  *     The final case currently doesn't get this far in the program, so we
9422  *     don't test for it.  If that were to change, it would be ok to allow it.
9423  *  b) When not under Unicode rules, any upper Latin1 character
9424  *  c) Otherwise, when unicode rules are used, all XIDS characters.
9425  *
9426  *      Because all ASCII characters have the same representation whether
9427  *      encoded in UTF-8 or not, we can use the foo_A macros below and '\0' and
9428  *      '{' without knowing if is UTF-8 or not. */
9429 #define VALID_LEN_ONE_IDENT(s, e, is_utf8)                                  \
9430     (isGRAPH_A(*(s)) || ((is_utf8)                                          \
9431                          ? isIDFIRST_utf8_safe(s, e)                        \
9432                          : (isGRAPH_L1(*s)                                  \
9433                             && LIKELY((U8) *(s) != LATIN1_TO_NATIVE(0xAD)))))
9434
9435 STATIC char *
9436 S_scan_ident(pTHX_ char *s, char *dest, STRLEN destlen, I32 ck_uni)
9437 {
9438     I32 herelines = PL_parser->herelines;
9439     SSize_t bracket = -1;
9440     char funny = *s++;
9441     char *d = dest;
9442     char * const e = d + destlen - 3;    /* two-character token, ending NUL */
9443     bool is_utf8 = cBOOL(UTF);
9444     I32 orig_copline = 0, tmp_copline = 0;
9445
9446     PERL_ARGS_ASSERT_SCAN_IDENT;
9447
9448     if (isSPACE(*s) || !*s)
9449         s = skipspace(s);
9450     if (isDIGIT(*s)) {
9451         while (isDIGIT(*s)) {
9452             if (d >= e)
9453                 Perl_croak(aTHX_ "%s", ident_too_long);
9454             *d++ = *s++;
9455         }
9456     }
9457     else {  /* See if it is a "normal" identifier */
9458         parse_ident(&s, &d, e, 1, is_utf8, FALSE, TRUE);
9459     }
9460     *d = '\0';
9461     d = dest;
9462     if (*d) {
9463         /* Either a digit variable, or parse_ident() found an identifier
9464            (anything valid as a bareword), so job done and return.  */
9465         if (PL_lex_state != LEX_NORMAL)
9466             PL_lex_state = LEX_INTERPENDMAYBE;
9467         return s;
9468     }
9469
9470     /* Here, it is not a run-of-the-mill identifier name */
9471
9472     if (*s == '$' && s[1]
9473         && (   isIDFIRST_lazy_if_safe(s+1, PL_bufend, is_utf8)
9474             || isDIGIT_A((U8)s[1])
9475             || s[1] == '$'
9476             || s[1] == '{'
9477             || memBEGINs(s+1, (STRLEN) (PL_bufend - (s+1)), "::")) )
9478     {
9479         /* Dereferencing a value in a scalar variable.
9480            The alternatives are different syntaxes for a scalar variable.
9481            Using ' as a leading package separator isn't allowed. :: is.   */
9482         return s;
9483     }
9484     /* Handle the opening { of @{...}, &{...}, *{...}, %{...}, ${...}  */
9485     if (*s == '{') {
9486         bracket = s - SvPVX(PL_linestr);
9487         s++;
9488         orig_copline = CopLINE(PL_curcop);
9489         if (s < PL_bufend && isSPACE(*s)) {
9490             s = skipspace(s);
9491         }
9492     }
9493     if ((s <= PL_bufend - (is_utf8)
9494                           ? UTF8SKIP(s)
9495                           : 1)
9496         && VALID_LEN_ONE_IDENT(s, PL_bufend, is_utf8))
9497     {
9498         if (is_utf8) {
9499             const STRLEN skip = UTF8SKIP(s);
9500             STRLEN i;
9501             d[skip] = '\0';
9502             for ( i = 0; i < skip; i++ )
9503                 d[i] = *s++;
9504         }
9505         else {
9506             *d = *s++;
9507             d[1] = '\0';
9508         }
9509     }
9510     /* Convert $^F, ${^F} and the ^F of ${^FOO} to control characters */
9511     if (*d == '^' && *s && isCONTROLVAR(*s)) {
9512         *d = toCTRL(*s);
9513         s++;
9514     }
9515     /* Warn about ambiguous code after unary operators if {...} notation isn't
9516        used.  There's no difference in ambiguity; it's merely a heuristic
9517        about when not to warn.  */
9518     else if (ck_uni && bracket == -1)
9519         check_uni();
9520     if (bracket != -1) {
9521         bool skip;
9522         char *s2;
9523         /* If we were processing {...} notation then...  */
9524         if (isIDFIRST_lazy_if_safe(d, e, is_utf8)
9525             || (!isPRINT(*d) /* isCNTRL(d), plus all non-ASCII */
9526                  && isWORDCHAR(*s))
9527         ) {
9528             /* note we have to check for a normal identifier first,
9529              * as it handles utf8 symbols, and only after that has
9530              * been ruled out can we look at the caret words */
9531             if (isIDFIRST_lazy_if_safe(d, e, is_utf8) ) {
9532                 /* if it starts as a valid identifier, assume that it is one.
9533                    (the later check for } being at the expected point will trap
9534                    cases where this doesn't pan out.)  */
9535                 d += is_utf8 ? UTF8SKIP(d) : 1;
9536                 parse_ident(&s, &d, e, 1, is_utf8, TRUE, TRUE);
9537                 *d = '\0';
9538             }
9539             else { /* caret word: ${^Foo} ${^CAPTURE[0]} */
9540                 d++;
9541                 while (isWORDCHAR(*s) && d < e) {
9542                     *d++ = *s++;
9543                 }
9544                 if (d >= e)
9545                     Perl_croak(aTHX_ "%s", ident_too_long);
9546                 *d = '\0';
9547             }
9548             tmp_copline = CopLINE(PL_curcop);
9549             if (s < PL_bufend && isSPACE(*s)) {
9550                 s = skipspace(s);
9551             }
9552             if ((*s == '[' || (*s == '{' && strNE(dest, "sub")))) {
9553                 /* ${foo[0]} and ${foo{bar}} and ${^CAPTURE[0]} notation.  */
9554                 if (ckWARN(WARN_AMBIGUOUS) && keyword(dest, d - dest, 0)) {
9555                     const char * const brack =
9556                         (const char *)
9557                         ((*s == '[') ? "[...]" : "{...}");
9558                     orig_copline = CopLINE(PL_curcop);
9559                     CopLINE_set(PL_curcop, tmp_copline);
9560    /* diag_listed_as: Ambiguous use of %c{%s[...]} resolved to %c%s[...] */
9561                     Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
9562                         "Ambiguous use of %c{%s%s} resolved to %c%s%s",
9563                         funny, dest, brack, funny, dest, brack);
9564                     CopLINE_set(PL_curcop, orig_copline);
9565                 }
9566                 bracket++;
9567                 PL_lex_brackstack[PL_lex_brackets++] = (char)(XOPERATOR | XFAKEBRACK);
9568                 PL_lex_allbrackets++;
9569                 return s;
9570             }
9571         }
9572
9573         if ( !tmp_copline )
9574             tmp_copline = CopLINE(PL_curcop);
9575         if ((skip = s < PL_bufend && isSPACE(*s))) {
9576             /* Avoid incrementing line numbers or resetting PL_linestart,
9577                in case we have to back up.  */
9578             STRLEN s_off = s - SvPVX(PL_linestr);
9579             s2 = peekspace(s);
9580             s = SvPVX(PL_linestr) + s_off;
9581         }
9582         else
9583             s2 = s;
9584
9585         /* Expect to find a closing } after consuming any trailing whitespace.
9586          */
9587         if (*s2 == '}') {
9588             /* Now increment line numbers if applicable.  */
9589             if (skip)
9590                 s = skipspace(s);
9591             s++;
9592             if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) {
9593                 PL_lex_state = LEX_INTERPEND;
9594                 PL_expect = XREF;
9595             }
9596             if (PL_lex_state == LEX_NORMAL) {
9597                 if (ckWARN(WARN_AMBIGUOUS)
9598                     && (keyword(dest, d - dest, 0)
9599                         || get_cvn_flags(dest, d - dest, is_utf8
9600                            ? SVf_UTF8
9601                            : 0)))
9602                 {
9603                     SV *tmp = newSVpvn_flags( dest, d - dest,
9604                                         SVs_TEMP | (is_utf8 ? SVf_UTF8 : 0) );
9605                     if (funny == '#')
9606                         funny = '@';
9607                     orig_copline = CopLINE(PL_curcop);
9608                     CopLINE_set(PL_curcop, tmp_copline);
9609                     Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
9610                         "Ambiguous use of %c{%" SVf "} resolved to %c%" SVf,
9611                         funny, SVfARG(tmp), funny, SVfARG(tmp));
9612                     CopLINE_set(PL_curcop, orig_copline);
9613                 }
9614             }
9615         }
9616         else {
9617             /* Didn't find the closing } at the point we expected, so restore
9618                state such that the next thing to process is the opening { and */
9619             s = SvPVX(PL_linestr) + bracket; /* let the parser handle it */
9620             CopLINE_set(PL_curcop, orig_copline);
9621             PL_parser->herelines = herelines;
9622             *dest = '\0';
9623             PL_parser->sub_no_recover = TRUE;
9624         }
9625     }
9626     else if (   PL_lex_state == LEX_INTERPNORMAL
9627              && !PL_lex_brackets
9628              && !intuit_more(s, PL_bufend))
9629         PL_lex_state = LEX_INTERPEND;
9630     return s;
9631 }
9632
9633 static bool
9634 S_pmflag(pTHX_ const char* const valid_flags, U32 * pmfl, char** s, char* charset, unsigned int * x_mod_count) {
9635
9636     /* Adds, subtracts to/from 'pmfl' based on the next regex modifier flag
9637      * found in the parse starting at 's', based on the subset that are valid
9638      * in this context input to this routine in 'valid_flags'. Advances s.
9639      * Returns TRUE if the input should be treated as a valid flag, so the next
9640      * char may be as well; otherwise FALSE. 'charset' should point to a NUL
9641      * upon first call on the current regex.  This routine will set it to any
9642      * charset modifier found.  The caller shouldn't change it.  This way,
9643      * another charset modifier encountered in the parse can be detected as an
9644      * error, as we have decided to allow only one */
9645
9646     const char c = **s;
9647     STRLEN charlen = UTF ? UTF8SKIP(*s) : 1;
9648
9649     if ( charlen != 1 || ! strchr(valid_flags, c) ) {
9650         if (isWORDCHAR_lazy_if_safe( *s, PL_bufend, UTF)) {
9651             yyerror_pv(Perl_form(aTHX_ "Unknown regexp modifier \"/%.*s\"", (int)charlen, *s),
9652                        UTF ? SVf_UTF8 : 0);
9653             (*s) += charlen;
9654             /* Pretend that it worked, so will continue processing before
9655              * dieing */
9656             return TRUE;
9657         }
9658         return FALSE;
9659     }
9660
9661     switch (c) {
9662
9663         CASE_STD_PMMOD_FLAGS_PARSE_SET(pmfl, *x_mod_count);
9664         case GLOBAL_PAT_MOD:      *pmfl |= PMf_GLOBAL; break;
9665         case CONTINUE_PAT_MOD:    *pmfl |= PMf_CONTINUE; break;
9666         case ONCE_PAT_MOD:        *pmfl |= PMf_KEEP; break;
9667         case KEEPCOPY_PAT_MOD:    *pmfl |= RXf_PMf_KEEPCOPY; break;
9668         case NONDESTRUCT_PAT_MOD: *pmfl |= PMf_NONDESTRUCT; break;
9669         case LOCALE_PAT_MOD:
9670             if (*charset) {
9671                 goto multiple_charsets;
9672             }
9673             set_regex_charset(pmfl, REGEX_LOCALE_CHARSET);
9674             *charset = c;
9675             break;
9676         case UNICODE_PAT_MOD:
9677             if (*charset) {
9678                 goto multiple_charsets;
9679             }
9680             set_regex_charset(pmfl, REGEX_UNICODE_CHARSET);
9681             *charset = c;
9682             break;
9683         case ASCII_RESTRICT_PAT_MOD:
9684             if (! *charset) {
9685                 set_regex_charset(pmfl, REGEX_ASCII_RESTRICTED_CHARSET);
9686             }
9687             else {
9688
9689                 /* Error if previous modifier wasn't an 'a', but if it was, see
9690                  * if, and accept, a second occurrence (only) */
9691                 if (*charset != 'a'
9692                     || get_regex_charset(*pmfl)
9693                         != REGEX_ASCII_RESTRICTED_CHARSET)
9694                 {
9695                         goto multiple_charsets;
9696                 }
9697                 set_regex_charset(pmfl, REGEX_ASCII_MORE_RESTRICTED_CHARSET);
9698             }
9699             *charset = c;
9700             break;
9701         case DEPENDS_PAT_MOD:
9702             if (*charset) {
9703                 goto multiple_charsets;
9704             }
9705             set_regex_charset(pmfl, REGEX_DEPENDS_CHARSET);
9706             *charset = c;
9707             break;
9708     }
9709
9710     (*s)++;
9711     return TRUE;
9712
9713     multiple_charsets:
9714         if (*charset != c) {
9715             yyerror(Perl_form(aTHX_ "Regexp modifiers \"/%c\" and \"/%c\" are mutually exclusive", *charset, c));
9716         }
9717         else if (c == 'a') {
9718   /* diag_listed_as: Regexp modifier "/%c" may appear a maximum of twice */
9719             yyerror("Regexp modifier \"/a\" may appear a maximum of twice");
9720         }
9721         else {
9722             yyerror(Perl_form(aTHX_ "Regexp modifier \"/%c\" may not appear twice", c));
9723         }
9724
9725         /* Pretend that it worked, so will continue processing before dieing */
9726         (*s)++;
9727         return TRUE;
9728 }
9729
9730 STATIC char *
9731 S_scan_pat(pTHX_ char *start, I32 type)
9732 {
9733     PMOP *pm;
9734     char *s;
9735     const char * const valid_flags =
9736         (const char *)((type == OP_QR) ? QR_PAT_MODS : M_PAT_MODS);
9737     char charset = '\0';    /* character set modifier */
9738     unsigned int x_mod_count = 0;
9739
9740     PERL_ARGS_ASSERT_SCAN_PAT;
9741
9742     s = scan_str(start,TRUE,FALSE, (PL_in_eval & EVAL_RE_REPARSING), NULL);
9743     if (!s)
9744         Perl_croak(aTHX_ "Search pattern not terminated");
9745
9746     pm = (PMOP*)newPMOP(type, 0);
9747     if (PL_multi_open == '?') {
9748         /* This is the only point in the code that sets PMf_ONCE:  */
9749         pm->op_pmflags |= PMf_ONCE;
9750
9751         /* Hence it's safe to do this bit of PMOP book-keeping here, which
9752            allows us to restrict the list needed by reset to just the ??
9753            matches.  */
9754         assert(type != OP_TRANS);
9755         if (PL_curstash) {
9756             MAGIC *mg = mg_find((const SV *)PL_curstash, PERL_MAGIC_symtab);
9757             U32 elements;
9758             if (!mg) {
9759                 mg = sv_magicext(MUTABLE_SV(PL_curstash), 0, PERL_MAGIC_symtab, 0, 0,
9760                                  0);
9761             }
9762             elements = mg->mg_len / sizeof(PMOP**);
9763             Renewc(mg->mg_ptr, elements + 1, PMOP*, char);
9764             ((PMOP**)mg->mg_ptr) [elements++] = pm;
9765             mg->mg_len = elements * sizeof(PMOP**);
9766             PmopSTASH_set(pm,PL_curstash);
9767         }
9768     }
9769
9770     /* if qr/...(?{..}).../, then need to parse the pattern within a new
9771      * anon CV. False positives like qr/[(?{]/ are harmless */
9772
9773     if (type == OP_QR) {
9774         STRLEN len;
9775         char *e, *p = SvPV(PL_lex_stuff, len);
9776         e = p + len;
9777         for (; p < e; p++) {
9778             if (p[0] == '(' && p[1] == '?'
9779                 && (p[2] == '{' || (p[2] == '?' && p[3] == '{')))
9780             {
9781                 pm->op_pmflags |= PMf_HAS_CV;
9782                 break;
9783             }
9784         }
9785         pm->op_pmflags |= PMf_IS_QR;
9786     }
9787
9788     while (*s && S_pmflag(aTHX_ valid_flags, &(pm->op_pmflags),
9789                                 &s, &charset, &x_mod_count))
9790     {};
9791     /* issue a warning if /c is specified,but /g is not */
9792     if ((pm->op_pmflags & PMf_CONTINUE) && !(pm->op_pmflags & PMf_GLOBAL))
9793     {
9794         Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
9795                        "Use of /c modifier is meaningless without /g" );
9796     }
9797
9798     PL_lex_op = (OP*)pm;
9799     pl_yylval.ival = OP_MATCH;
9800     return s;
9801 }
9802
9803 STATIC char *
9804 S_scan_subst(pTHX_ char *start)
9805 {
9806     char *s;
9807     PMOP *pm;
9808     I32 first_start;
9809     line_t first_line;
9810     line_t linediff = 0;
9811     I32 es = 0;
9812     char charset = '\0';    /* character set modifier */
9813     unsigned int x_mod_count = 0;
9814     char *t;
9815
9816     PERL_ARGS_ASSERT_SCAN_SUBST;
9817
9818     pl_yylval.ival = OP_NULL;
9819
9820     s = scan_str(start, TRUE, FALSE, FALSE, &t);
9821
9822     if (!s)
9823         Perl_croak(aTHX_ "Substitution pattern not terminated");
9824
9825     s = t;
9826
9827     first_start = PL_multi_start;
9828     first_line = CopLINE(PL_curcop);
9829     s = scan_str(s,FALSE,FALSE,FALSE,NULL);
9830     if (!s) {
9831         SvREFCNT_dec_NN(PL_lex_stuff);
9832         PL_lex_stuff = NULL;
9833         Perl_croak(aTHX_ "Substitution replacement not terminated");
9834     }
9835     PL_multi_start = first_start;       /* so whole substitution is taken together */
9836
9837     pm = (PMOP*)newPMOP(OP_SUBST, 0);
9838
9839
9840     while (*s) {
9841         if (*s == EXEC_PAT_MOD) {
9842             s++;
9843             es++;
9844         }
9845         else if (! S_pmflag(aTHX_ S_PAT_MODS, &(pm->op_pmflags),
9846                                   &s, &charset, &x_mod_count))
9847         {
9848             break;
9849         }
9850     }
9851
9852     if ((pm->op_pmflags & PMf_CONTINUE)) {
9853         Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), "Use of /c modifier is meaningless in s///" );
9854     }
9855
9856     if (es) {
9857         SV * const repl = newSVpvs("");
9858
9859         PL_multi_end = 0;
9860         pm->op_pmflags |= PMf_EVAL;
9861         for (; es > 1; es--) {
9862             sv_catpvs(repl, "eval ");
9863         }
9864         sv_catpvs(repl, "do {");
9865         sv_catsv(repl, PL_parser->lex_sub_repl);
9866         sv_catpvs(repl, "}");
9867         SvREFCNT_dec(PL_parser->lex_sub_repl);
9868         PL_parser->lex_sub_repl = repl;
9869     }
9870
9871
9872     linediff = CopLINE(PL_curcop) - first_line;
9873     if (linediff)
9874         CopLINE_set(PL_curcop, first_line);
9875
9876     if (linediff || es) {
9877         /* the IVX field indicates that the replacement string is a s///e;
9878          * the NVX field indicates how many src code lines the replacement
9879          * spreads over */
9880         sv_upgrade(PL_parser->lex_sub_repl, SVt_PVNV);
9881         ((XPVNV*)SvANY(PL_parser->lex_sub_repl))->xnv_u.xnv_lines = linediff;
9882         ((XPVIV*)SvANY(PL_parser->lex_sub_repl))->xiv_u.xivu_eval_seen =
9883                                                                     cBOOL(es);
9884     }
9885
9886     PL_lex_op = (OP*)pm;
9887     pl_yylval.ival = OP_SUBST;
9888     return s;
9889 }
9890
9891 STATIC char *
9892 S_scan_trans(pTHX_ char *start)
9893 {
9894     char* s;
9895     OP *o;
9896     U8 squash;
9897     U8 del;
9898     U8 complement;
9899     bool nondestruct = 0;
9900     char *t;
9901
9902     PERL_ARGS_ASSERT_SCAN_TRANS;
9903
9904     pl_yylval.ival = OP_NULL;
9905
9906     s = scan_str(start,FALSE,FALSE,FALSE,&t);
9907     if (!s)
9908         Perl_croak(aTHX_ "Transliteration pattern not terminated");
9909
9910     s = t;
9911
9912     s = scan_str(s,FALSE,FALSE,FALSE,NULL);
9913     if (!s) {
9914         SvREFCNT_dec_NN(PL_lex_stuff);
9915         PL_lex_stuff = NULL;
9916         Perl_croak(aTHX_ "Transliteration replacement not terminated");
9917     }
9918
9919     complement = del = squash = 0;
9920     while (1) {
9921         switch (*s) {
9922         case 'c':
9923             complement = OPpTRANS_COMPLEMENT;
9924             break;
9925         case 'd':
9926             del = OPpTRANS_DELETE;
9927             break;
9928         case 's':
9929             squash = OPpTRANS_SQUASH;
9930             break;
9931         case 'r':
9932             nondestruct = 1;
9933             break;
9934         default:
9935             goto no_more;
9936         }
9937         s++;
9938     }
9939   no_more:
9940
9941     o = newPVOP(nondestruct ? OP_TRANSR : OP_TRANS, 0, (char*)NULL);
9942     o->op_private &= ~OPpTRANS_ALL;
9943     o->op_private |= del|squash|complement|
9944       (DO_UTF8(PL_lex_stuff)? OPpTRANS_FROM_UTF : 0)|
9945       (DO_UTF8(PL_parser->lex_sub_repl) ? OPpTRANS_TO_UTF   : 0);
9946
9947     PL_lex_op = o;
9948     pl_yylval.ival = nondestruct ? OP_TRANSR : OP_TRANS;
9949
9950
9951     return s;
9952 }
9953
9954 /* scan_heredoc
9955    Takes a pointer to the first < in <<FOO.
9956    Returns a pointer to the byte following <<FOO.
9957
9958    This function scans a heredoc, which involves different methods
9959    depending on whether we are in a string eval, quoted construct, etc.
9960    This is because PL_linestr could containing a single line of input, or
9961    a whole string being evalled, or the contents of the current quote-
9962    like operator.
9963
9964    The two basic methods are:
9965     - Steal lines from the input stream
9966     - Scan the heredoc in PL_linestr and remove it therefrom
9967
9968    In a file scope or filtered eval, the first method is used; in a
9969    string eval, the second.
9970
9971    In a quote-like operator, we have to choose between the two,
9972    depending on where we can find a newline.  We peek into outer lex-
9973    ing scopes until we find one with a newline in it.  If we reach the
9974    outermost lexing scope and it is a file, we use the stream method.
9975    Otherwise it is treated as an eval.
9976 */
9977
9978 STATIC char *
9979 S_scan_heredoc(pTHX_ char *s)
9980 {
9981     I32 op_type = OP_SCALAR;
9982     I32 len;
9983     SV *tmpstr;
9984     char term;
9985     char *d;
9986     char *e;
9987     char *peek;
9988     char *indent = 0;
9989     I32 indent_len = 0;
9990     bool indented = FALSE;
9991     const bool infile = PL_rsfp || PL_parser->filtered;
9992     const line_t origline = CopLINE(PL_curcop);
9993     LEXSHARED *shared = PL_parser->lex_shared;
9994
9995     PERL_ARGS_ASSERT_SCAN_HEREDOC;
9996
9997     s += 2;
9998     d = PL_tokenbuf + 1;
9999     e = PL_tokenbuf + sizeof PL_tokenbuf - 1;
10000     *PL_tokenbuf = '\n';
10001     peek = s;
10002     if (*peek == '~') {
10003         indented = TRUE;
10004         peek++; s++;
10005     }
10006     while (SPACE_OR_TAB(*peek))
10007         peek++;
10008     if (*peek == '`' || *peek == '\'' || *peek =='"') {
10009         s = peek;
10010         term = *s++;
10011         s = delimcpy(d, e, s, PL_bufend, term, &len);
10012         if (s == PL_bufend)
10013             Perl_croak(aTHX_ "Unterminated delimiter for here document");
10014         d += len;
10015         s++;
10016     }
10017     else {
10018         if (*s == '\\')
10019             /* <<\FOO is equivalent to <<'FOO' */
10020             s++, term = '\'';
10021         else
10022             term = '"';
10023         if (! isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF))
10024             Perl_croak(aTHX_ "Use of bare << to mean <<\"\" is forbidden");
10025         peek = s;
10026         while (isWORDCHAR_lazy_if_safe(peek, PL_bufend, UTF)) {
10027             peek += UTF ? UTF8SKIP(peek) : 1;
10028         }
10029         len = (peek - s >= e - d) ? (e - d) : (peek - s);
10030         Copy(s, d, len, char);
10031         s += len;
10032         d += len;
10033     }
10034     if (d >= PL_tokenbuf + sizeof PL_tokenbuf - 1)
10035         Perl_croak(aTHX_ "Delimiter for here document is too long");
10036     *d++ = '\n';
10037     *d = '\0';
10038     len = d - PL_tokenbuf;
10039
10040 #ifndef PERL_STRICT_CR
10041     d = (char *) memchr(s, '\r', PL_bufend - s);
10042     if (d) {
10043         char * const olds = s;
10044         s = d;
10045         while (s < PL_bufend) {
10046             if (*s == '\r') {
10047                 *d++ = '\n';
10048                 if (*++s == '\n')
10049                     s++;
10050             }
10051             else if (*s == '\n' && s[1] == '\r') {      /* \015\013 on a mac? */
10052                 *d++ = *s++;
10053                 s++;
10054             }
10055             else
10056                 *d++ = *s++;
10057         }
10058         *d = '\0';
10059         PL_bufend = d;
10060         SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
10061         s = olds;
10062     }
10063 #endif
10064
10065     tmpstr = newSV_type(SVt_PVIV);
10066     SvGROW(tmpstr, 80);
10067     if (term == '\'') {
10068         op_type = OP_CONST;
10069         SvIV_set(tmpstr, -1);
10070     }
10071     else if (term == '`') {
10072         op_type = OP_BACKTICK;
10073         SvIV_set(tmpstr, '\\');
10074     }
10075
10076     PL_multi_start = origline + 1 + PL_parser->herelines;
10077     PL_multi_open = PL_multi_close = '<';
10078     /* inside a string eval or quote-like operator */
10079     if (!infile || PL_lex_inwhat) {
10080         SV *linestr;
10081         char *bufend;
10082         char * const olds = s;
10083         PERL_CONTEXT * const cx = CX_CUR();
10084         /* These two fields are not set until an inner lexing scope is
10085            entered.  But we need them set here. */
10086         shared->ls_bufptr  = s;
10087         shared->ls_linestr = PL_linestr;
10088         if (PL_lex_inwhat)
10089           /* Look for a newline.  If the current buffer does not have one,
10090              peek into the line buffer of the parent lexing scope, going
10091              up as many levels as necessary to find one with a newline
10092              after bufptr.
10093            */
10094           while (!(s = (char *)memchr(
10095                     (void *)shared->ls_bufptr, '\n',
10096                     SvEND(shared->ls_linestr)-shared->ls_bufptr
10097                 ))) {
10098             shared = shared->ls_prev;
10099             /* shared is only null if we have gone beyond the outermost
10100                lexing scope.  In a file, we will have broken out of the
10101                loop in the previous iteration.  In an eval, the string buf-
10102                fer ends with "\n;", so the while condition above will have
10103                evaluated to false.  So shared can never be null.  Or so you
10104                might think.  Odd syntax errors like s;@{<<; can gobble up
10105                the implicit semicolon at the end of a flie, causing the
10106                file handle to be closed even when we are not in a string
10107                eval.  So shared may be null in that case.
10108                (Closing '}' here to balance the earlier open brace for
10109                editors that look for matched pairs.) */
10110             if (UNLIKELY(!shared))
10111                 goto interminable;
10112             /* A LEXSHARED struct with a null ls_prev pointer is the outer-
10113                most lexing scope.  In a file, shared->ls_linestr at that
10114                level is just one line, so there is no body to steal. */
10115             if (infile && !shared->ls_prev) {
10116                 s = olds;
10117                 goto streaming;
10118             }
10119           }
10120         else {  /* eval or we've already hit EOF */
10121             s = (char*)memchr((void*)s, '\n', PL_bufend - s);
10122             if (!s)
10123                 goto interminable;
10124         }
10125         linestr = shared->ls_linestr;
10126         bufend = SvEND(linestr);
10127         d = s;
10128         if (indented) {
10129             char *myolds = s;
10130
10131             while (s < bufend - len + 1) {
10132                 if (*s++ == '\n')
10133                     ++PL_parser->herelines;
10134
10135                 if (memEQ(s, PL_tokenbuf + 1, len - 1)) {
10136                     char *backup = s;
10137                     indent_len = 0;
10138
10139                     /* Only valid if it's preceded by whitespace only */
10140                     while (backup != myolds && --backup >= myolds) {
10141                         if (! SPACE_OR_TAB(*backup)) {
10142                             break;
10143                         }
10144
10145                         indent_len++;
10146                     }
10147
10148                     /* No whitespace or all! */
10149                     if (backup == s || *backup == '\n') {
10150                         Newx(indent, indent_len + 1, char);
10151                         memcpy(indent, backup + 1, indent_len);
10152                         indent[indent_len] = 0;
10153                         s--; /* before our delimiter */
10154                         PL_parser->herelines--; /* this line doesn't count */
10155                         break;
10156                     }
10157                 }
10158             }
10159         } else {
10160             while (s < bufend - len + 1
10161                    && memNE(s,PL_tokenbuf,len) )
10162             {
10163                 if (*s++ == '\n')
10164                     ++PL_parser->herelines;
10165             }
10166         }
10167
10168         if (s >= bufend - len + 1) {
10169             goto interminable;
10170         }
10171         sv_setpvn(tmpstr,d+1,s-d);
10172         s += len - 1;
10173         /* the preceding stmt passes a newline */
10174         PL_parser->herelines++;
10175
10176         /* s now points to the newline after the heredoc terminator.
10177            d points to the newline before the body of the heredoc.
10178          */
10179
10180         /* We are going to modify linestr in place here, so set
10181            aside copies of the string if necessary for re-evals or
10182            (caller $n)[6]. */
10183         /* See the Paranoia note in case LEX_INTERPEND in yylex, for why we
10184            check shared->re_eval_str. */
10185         if (shared->re_eval_start || shared->re_eval_str) {
10186             /* Set aside the rest of the regexp */
10187             if (!shared->re_eval_str)
10188                 shared->re_eval_str =
10189                        newSVpvn(shared->re_eval_start,
10190                                 bufend - shared->re_eval_start);
10191             shared->re_eval_start -= s-d;
10192         }
10193         if (cxstack_ix >= 0
10194             && CxTYPE(cx) == CXt_EVAL
10195             && CxOLD_OP_TYPE(cx) == OP_ENTEREVAL
10196             && cx->blk_eval.cur_text == linestr)
10197         {
10198             cx->blk_eval.cur_text = newSVsv(linestr);
10199             cx->blk_u16 |= 0x40; /* indicate cur_text is ref counted */
10200         }
10201         /* Copy everything from s onwards back to d. */
10202         Move(s,d,bufend-s + 1,char);
10203         SvCUR_set(linestr, SvCUR(linestr) - (s-d));
10204         /* Setting PL_bufend only applies when we have not dug deeper
10205            into other scopes, because sublex_done sets PL_bufend to
10206            SvEND(PL_linestr). */
10207         if (shared == PL_parser->lex_shared) PL_bufend = SvEND(linestr);
10208         s = olds;
10209     }
10210     else
10211     {
10212       SV *linestr_save;
10213       char *oldbufptr_save;
10214       char *oldoldbufptr_save;
10215      streaming:
10216       SvPVCLEAR(tmpstr);   /* avoid "uninitialized" warning */
10217       term = PL_tokenbuf[1];
10218       len--;
10219       linestr_save = PL_linestr; /* must restore this afterwards */
10220       d = s;                     /* and this */
10221       oldbufptr_save = PL_oldbufptr;
10222       oldoldbufptr_save = PL_oldoldbufptr;
10223       PL_linestr = newSVpvs("");
10224       PL_bufend = SvPVX(PL_linestr);
10225       while (1) {
10226         PL_bufptr = PL_bufend;
10227         CopLINE_set(PL_curcop,
10228                     origline + 1 + PL_parser->herelines);
10229         if (!lex_next_chunk(LEX_NO_TERM)
10230          && (!SvCUR(tmpstr) || SvEND(tmpstr)[-1] != '\n')) {
10231             /* Simply freeing linestr_save might seem simpler here, as it
10232                does not matter what PL_linestr points to, since we are
10233                about to croak; but in a quote-like op, linestr_save
10234                will have been prospectively freed already, via
10235                SAVEFREESV(PL_linestr) in sublex_push, so it’s easier to
10236                restore PL_linestr. */
10237             SvREFCNT_dec_NN(PL_linestr);
10238             PL_linestr = linestr_save;
10239             PL_oldbufptr = oldbufptr_save;
10240             PL_oldoldbufptr = oldoldbufptr_save;
10241             goto interminable;
10242         }
10243         CopLINE_set(PL_curcop, origline);
10244         if (!SvCUR(PL_linestr) || PL_bufend[-1] != '\n') {
10245             s = lex_grow_linestr(SvLEN(PL_linestr) + 3);
10246             /* ^That should be enough to avoid this needing to grow:  */
10247             sv_catpvs(PL_linestr, "\n\0");
10248             assert(s == SvPVX(PL_linestr));
10249             PL_bufend = SvEND(PL_linestr);
10250         }
10251         s = PL_bufptr;
10252         PL_parser->herelines++;
10253         PL_last_lop = PL_last_uni = NULL;
10254 #ifndef PERL_STRICT_CR
10255         if (PL_bufend - PL_linestart >= 2) {
10256             if (   (PL_bufend[-2] == '\r' && PL_bufend[-1] == '\n')
10257                 || (PL_bufend[-2] == '\n' && PL_bufend[-1] == '\r'))
10258             {
10259                 PL_bufend[-2] = '\n';
10260                 PL_bufend--;
10261                 SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
10262             }
10263             else if (PL_bufend[-1] == '\r')
10264                 PL_bufend[-1] = '\n';
10265         }
10266         else if (PL_bufend - PL_linestart == 1 && PL_bufend[-1] == '\r')
10267             PL_bufend[-1] = '\n';
10268 #endif
10269         if (indented && (PL_bufend-s) >= len) {
10270             char * found = ninstr(s, PL_bufend, (PL_tokenbuf + 1), (PL_tokenbuf +1 + len));
10271
10272             if (found) {
10273                 char *backup = found;
10274                 indent_len = 0;
10275
10276                 /* Only valid if it's preceded by whitespace only */
10277                 while (backup != s && --backup >= s) {
10278                     if (! SPACE_OR_TAB(*backup)) {
10279                         break;
10280                     }
10281                     indent_len++;
10282                 }
10283
10284                 /* All whitespace or none! */
10285                 if (backup == found || SPACE_OR_TAB(*backup)) {
10286                     Newx(indent, indent_len + 1, char);
10287                     memcpy(indent, backup, indent_len);
10288                     indent[indent_len] = 0;
10289                     SvREFCNT_dec(PL_linestr);
10290                     PL_linestr = linestr_save;
10291                     PL_linestart = SvPVX(linestr_save);
10292                     PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
10293                     PL_oldbufptr = oldbufptr_save;
10294                     PL_oldoldbufptr = oldoldbufptr_save;
10295                     s = d;
10296                     break;
10297                 }
10298             }
10299
10300             /* Didn't find it */
10301             sv_catsv(tmpstr,PL_linestr);
10302         } else {
10303             if (*s == term && PL_bufend-s >= len
10304                 && memEQ(s,PL_tokenbuf + 1,len))
10305             {
10306                 SvREFCNT_dec(PL_linestr);
10307                 PL_linestr = linestr_save;
10308                 PL_linestart = SvPVX(linestr_save);
10309                 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
10310                 PL_oldbufptr = oldbufptr_save;
10311                 PL_oldoldbufptr = oldoldbufptr_save;
10312                 s = d;
10313                 break;
10314             } else {
10315                 sv_catsv(tmpstr,PL_linestr);
10316             }
10317         }
10318       }
10319     }
10320     PL_multi_end = origline + PL_parser->herelines;
10321     if (indented && indent) {
10322         STRLEN linecount = 1;
10323         STRLEN herelen = SvCUR(tmpstr);
10324         char *ss = SvPVX(tmpstr);
10325         char *se = ss + herelen;
10326         SV *newstr = newSV(herelen+1);
10327         SvPOK_on(newstr);
10328
10329         /* Trim leading whitespace */
10330         while (ss < se) {
10331             /* newline only? Copy and move on */
10332             if (*ss == '\n') {
10333                 sv_catpvs(newstr,"\n");
10334                 ss++;
10335                 linecount++;
10336
10337             /* Found our indentation? Strip it */
10338             } else if (se - ss >= indent_len
10339                        && memEQ(ss, indent, indent_len))
10340             {
10341                 STRLEN le = 0;
10342
10343                 ss += indent_len;
10344
10345                 while ((ss + le) < se && *(ss + le) != '\n')
10346                     le++;
10347
10348                 sv_catpvn(newstr, ss, le);
10349
10350                 ss += le;
10351
10352             /* Line doesn't begin with our indentation? Croak */
10353             } else {
10354                 Perl_croak(aTHX_
10355                     "Indentation on line %d of here-doc doesn't match delimiter",
10356                     (int)linecount
10357                 );
10358             }
10359         }
10360         /* avoid sv_setsv() as we dont wan't to COW here */
10361         sv_setpvn(tmpstr,SvPVX(newstr),SvCUR(newstr));
10362         Safefree(indent);
10363         SvREFCNT_dec_NN(newstr);
10364     }
10365     if (SvCUR(tmpstr) + 5 < SvLEN(tmpstr)) {
10366         SvPV_shrink_to_cur(tmpstr);
10367     }
10368     if (!IN_BYTES) {
10369         if (UTF && is_utf8_string((U8*)SvPVX_const(tmpstr), SvCUR(tmpstr)))
10370             SvUTF8_on(tmpstr);
10371     }
10372     PL_lex_stuff = tmpstr;
10373     pl_yylval.ival = op_type;
10374     return s;
10375
10376   interminable:
10377     SvREFCNT_dec(tmpstr);
10378     CopLINE_set(PL_curcop, origline);
10379     missingterm(PL_tokenbuf + 1, sizeof(PL_tokenbuf) - 1);
10380 }
10381
10382 /* scan_inputsymbol
10383    takes: position of first '<' in input buffer
10384    returns: position of first char following the matching '>' in
10385             input buffer
10386    side-effects: pl_yylval and lex_op are set.
10387
10388    This code handles:
10389
10390    <>           read from ARGV
10391    <<>>         read from ARGV without magic open
10392    <FH>         read from filehandle
10393    <pkg::FH>    read from package qualified filehandle
10394    <pkg'FH>     read from package qualified filehandle
10395    <$fh>        read from filehandle in $fh
10396    <*.h>        filename glob
10397
10398 */
10399
10400 STATIC char *
10401 S_scan_inputsymbol(pTHX_ char *start)
10402 {
10403     char *s = start;            /* current position in buffer */
10404     char *end;
10405     I32 len;
10406     bool nomagicopen = FALSE;
10407     char *d = PL_tokenbuf;                                      /* start of temp holding space */
10408     const char * const e = PL_tokenbuf + sizeof PL_tokenbuf;    /* end of temp holding space */
10409
10410     PERL_ARGS_ASSERT_SCAN_INPUTSYMBOL;
10411
10412     end = (char *) memchr(s, '\n', PL_bufend - s);
10413     if (!end)
10414         end = PL_bufend;
10415     if (s[1] == '<' && s[2] == '>' && s[3] == '>') {
10416         nomagicopen = TRUE;
10417         *d = '\0';
10418         len = 0;
10419         s += 3;
10420     }
10421     else
10422         s = delimcpy(d, e, s + 1, end, '>', &len);      /* extract until > */
10423
10424     /* die if we didn't have space for the contents of the <>,
10425        or if it didn't end, or if we see a newline
10426     */
10427
10428     if (len >= (I32)sizeof PL_tokenbuf)
10429         Perl_croak(aTHX_ "Excessively long <> operator");
10430     if (s >= end)
10431         Perl_croak(aTHX_ "Unterminated <> operator");
10432
10433     s++;
10434
10435     /* check for <$fh>
10436        Remember, only scalar variables are interpreted as filehandles by
10437        this code.  Anything more complex (e.g., <$fh{$num}>) will be
10438        treated as a glob() call.
10439        This code makes use of the fact that except for the $ at the front,
10440        a scalar variable and a filehandle look the same.
10441     */
10442     if (*d == '$' && d[1]) d++;
10443
10444     /* allow <Pkg'VALUE> or <Pkg::VALUE> */
10445     while (isWORDCHAR_lazy_if_safe(d, e, UTF) || *d == '\'' || *d == ':') {
10446         d += UTF ? UTF8SKIP(d) : 1;
10447     }
10448
10449     /* If we've tried to read what we allow filehandles to look like, and
10450        there's still text left, then it must be a glob() and not a getline.
10451        Use scan_str to pull out the stuff between the <> and treat it
10452        as nothing more than a string.
10453     */
10454
10455     if (d - PL_tokenbuf != len) {
10456         pl_yylval.ival = OP_GLOB;
10457         s = scan_str(start,FALSE,FALSE,FALSE,NULL);
10458         if (!s)
10459            Perl_croak(aTHX_ "Glob not terminated");
10460         return s;
10461     }
10462     else {
10463         bool readline_overriden = FALSE;
10464         GV *gv_readline;
10465         /* we're in a filehandle read situation */
10466         d = PL_tokenbuf;
10467
10468         /* turn <> into <ARGV> */
10469         if (!len)
10470             Copy("ARGV",d,5,char);
10471
10472         /* Check whether readline() is overriden */
10473         if ((gv_readline = gv_override("readline",8)))
10474             readline_overriden = TRUE;
10475
10476         /* if <$fh>, create the ops to turn the variable into a
10477            filehandle
10478         */
10479         if (*d == '$') {
10480             /* try to find it in the pad for this block, otherwise find
10481                add symbol table ops
10482             */
10483             const PADOFFSET tmp = pad_findmy_pvn(d, len, 0);
10484             if (tmp != NOT_IN_PAD) {
10485                 if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
10486                     HV * const stash = PAD_COMPNAME_OURSTASH(tmp);
10487                     HEK * const stashname = HvNAME_HEK(stash);
10488                     SV * const sym = sv_2mortal(newSVhek(stashname));
10489                     sv_catpvs(sym, "::");
10490                     sv_catpv(sym, d+1);
10491                     d = SvPVX(sym);
10492                     goto intro_sym;
10493                 }
10494                 else {
10495                     OP * const o = newOP(OP_PADSV, 0);
10496                     o->op_targ = tmp;
10497                     PL_lex_op = readline_overriden
10498                         ? newUNOP(OP_ENTERSUB, OPf_STACKED,
10499                                 op_append_elem(OP_LIST, o,
10500                                     newCVREF(0, newGVOP(OP_GV,0,gv_readline))))
10501                         : newUNOP(OP_READLINE, 0, o);
10502                 }
10503             }
10504             else {
10505                 GV *gv;
10506                 ++d;
10507               intro_sym:
10508                 gv = gv_fetchpv(d,
10509                                 GV_ADDMULTI | ( UTF ? SVf_UTF8 : 0 ),
10510                                 SVt_PV);
10511                 PL_lex_op = readline_overriden
10512                     ? newUNOP(OP_ENTERSUB, OPf_STACKED,
10513                             op_append_elem(OP_LIST,
10514                                 newUNOP(OP_RV2SV, 0, newGVOP(OP_GV, 0, gv)),
10515                                 newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
10516                     : newUNOP(OP_READLINE, 0,
10517                             newUNOP(OP_RV2SV, 0,
10518                                 newGVOP(OP_GV, 0, gv)));
10519             }
10520             /* we created the ops in PL_lex_op, so make pl_yylval.ival a null op */
10521             pl_yylval.ival = OP_NULL;
10522         }
10523
10524         /* If it's none of the above, it must be a literal filehandle
10525            (<Foo::BAR> or <FOO>) so build a simple readline OP */
10526         else {
10527             GV * const gv = gv_fetchpv(d, GV_ADD | ( UTF ? SVf_UTF8 : 0 ), SVt_PVIO);
10528             PL_lex_op = readline_overriden
10529                 ? newUNOP(OP_ENTERSUB, OPf_STACKED,
10530                         op_append_elem(OP_LIST,
10531                             newGVOP(OP_GV, 0, gv),
10532                             newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
10533                 : newUNOP(OP_READLINE, nomagicopen ? OPf_SPECIAL : 0, newGVOP(OP_GV, 0, gv));
10534             pl_yylval.ival = OP_NULL;
10535         }
10536     }
10537
10538     return s;
10539 }
10540
10541
10542 /* scan_str
10543    takes:
10544         start                   position in buffer
10545         keep_bracketed_quoted   preserve \ quoting of embedded delimiters, but
10546                                 only if they are of the open/close form
10547         keep_delims             preserve the delimiters around the string
10548         re_reparse              compiling a run-time /(?{})/:
10549                                    collapse // to /,  and skip encoding src
10550         delimp                  if non-null, this is set to the position of
10551                                 the closing delimiter, or just after it if
10552                                 the closing and opening delimiters differ
10553                                 (i.e., the opening delimiter of a substitu-
10554                                 tion replacement)
10555    returns: position to continue reading from buffer
10556    side-effects: multi_start, multi_close, lex_repl or lex_stuff, and
10557         updates the read buffer.
10558
10559    This subroutine pulls a string out of the input.  It is called for:
10560         q               single quotes           q(literal text)
10561         '               single quotes           'literal text'
10562         qq              double quotes           qq(interpolate $here please)
10563         "               double quotes           "interpolate $here please"
10564         qx              backticks               qx(/bin/ls -l)
10565         `               backticks               `/bin/ls -l`
10566         qw              quote words             @EXPORT_OK = qw( func() $spam )
10567         m//             regexp match            m/this/
10568         s///            regexp substitute       s/this/that/
10569         tr///           string transliterate    tr/this/that/
10570         y///            string transliterate    y/this/that/
10571         ($*@)           sub prototypes          sub foo ($)
10572         (stuff)         sub attr parameters     sub foo : attr(stuff)
10573         <>              readline or globs       <FOO>, <>, <$fh>, or <*.c>
10574
10575    In most of these cases (all but <>, patterns and transliterate)
10576    yylex() calls scan_str().  m// makes yylex() call scan_pat() which
10577    calls scan_str().  s/// makes yylex() call scan_subst() which calls
10578    scan_str().  tr/// and y/// make yylex() call scan_trans() which
10579    calls scan_str().
10580
10581    It skips whitespace before the string starts, and treats the first
10582    character as the delimiter.  If the delimiter is one of ([{< then
10583    the corresponding "close" character )]}> is used as the closing
10584    delimiter.  It allows quoting of delimiters, and if the string has
10585    balanced delimiters ([{<>}]) it allows nesting.
10586
10587    On success, the SV with the resulting string is put into lex_stuff or,
10588    if that is already non-NULL, into lex_repl. The second case occurs only
10589    when parsing the RHS of the special constructs s/// and tr/// (y///).
10590    For convenience, the terminating delimiter character is stuffed into
10591    SvIVX of the SV.
10592 */
10593
10594 STATIC char *
10595 S_scan_str(pTHX_ char *start, int keep_bracketed_quoted, int keep_delims, int re_reparse,
10596                  char **delimp
10597     )
10598 {
10599     SV *sv;                     /* scalar value: string */
10600     const char *tmps;           /* temp string, used for delimiter matching */
10601     char *s = start;            /* current position in the buffer */
10602     char term;                  /* terminating character */
10603     char *to;                   /* current position in the sv's data */
10604     I32 brackets = 1;           /* bracket nesting level */
10605     bool has_utf8 = FALSE;      /* is there any utf8 content? */
10606     IV termcode;                /* terminating char. code */
10607     U8 termstr[UTF8_MAXBYTES+1]; /* terminating string */
10608     STRLEN termlen;             /* length of terminating string */
10609     line_t herelines;
10610
10611     /* The delimiters that have a mirror-image closing one */
10612     const char * opening_delims = "([{<";
10613     const char * closing_delims = ")]}>";
10614
10615     /* The only non-UTF character that isn't a stand alone grapheme is
10616      * white-space, hence can't be a delimiter. */
10617     const char * non_grapheme_msg = "Use of unassigned code point or"
10618                                     " non-standalone grapheme for a delimiter"
10619                                     " is not allowed";
10620     PERL_ARGS_ASSERT_SCAN_STR;
10621
10622     /* skip space before the delimiter */
10623     if (isSPACE(*s)) {
10624         s = skipspace(s);
10625     }
10626
10627     /* mark where we are, in case we need to report errors */
10628     CLINE;
10629
10630     /* after skipping whitespace, the next character is the terminator */
10631     term = *s;
10632     if (!UTF || UTF8_IS_INVARIANT(term)) {
10633         termcode = termstr[0] = term;
10634         termlen = 1;
10635     }
10636     else {
10637         termcode = utf8_to_uvchr_buf((U8*)s, (U8*)PL_bufend, &termlen);
10638         if (UTF && UNLIKELY(! _is_grapheme((U8 *) start,
10639                                            (U8 *) s,
10640                                            (U8 *) PL_bufend,
10641                                                   termcode)))
10642         {
10643             yyerror(non_grapheme_msg);
10644         }
10645
10646         Copy(s, termstr, termlen, U8);
10647     }
10648
10649     /* mark where we are */
10650     PL_multi_start = CopLINE(PL_curcop);
10651     PL_multi_open = termcode;
10652     herelines = PL_parser->herelines;
10653
10654     /* If the delimiter has a mirror-image closing one, get it */
10655     if (term && (tmps = strchr(opening_delims, term))) {
10656         termcode = termstr[0] = term = closing_delims[tmps - opening_delims];
10657     }
10658
10659     PL_multi_close = termcode;
10660
10661     if (PL_multi_open == PL_multi_close) {
10662         keep_bracketed_quoted = FALSE;
10663     }
10664
10665     /* create a new SV to hold the contents.  79 is the SV's initial length.
10666        What a random number. */
10667     sv = newSV_type(SVt_PVIV);
10668     SvGROW(sv, 80);
10669     SvIV_set(sv, termcode);
10670     (void)SvPOK_only(sv);               /* validate pointer */
10671
10672     /* move past delimiter and try to read a complete string */
10673     if (keep_delims)
10674         sv_catpvn(sv, s, termlen);
10675     s += termlen;
10676     for (;;) {
10677         /* extend sv if need be */
10678         SvGROW(sv, SvCUR(sv) + (PL_bufend - s) + 1);
10679         /* set 'to' to the next character in the sv's string */
10680         to = SvPVX(sv)+SvCUR(sv);
10681
10682         /* if open delimiter is the close delimiter read unbridle */
10683         if (PL_multi_open == PL_multi_close) {
10684             for (; s < PL_bufend; s++,to++) {
10685                 /* embedded newlines increment the current line number */
10686                 if (*s == '\n' && !PL_rsfp && !PL_parser->filtered)
10687                     COPLINE_INC_WITH_HERELINES;
10688                 /* handle quoted delimiters */
10689                 if (*s == '\\' && s+1 < PL_bufend && term != '\\') {
10690                     if (!keep_bracketed_quoted
10691                         && (s[1] == term
10692                             || (re_reparse && s[1] == '\\'))
10693                     )
10694                         s++;
10695                     else /* any other quotes are simply copied straight through */
10696                         *to++ = *s++;
10697                 }
10698                 /* terminate when run out of buffer (the for() condition), or
10699                    have found the terminator */
10700                 else if (*s == term) {  /* First byte of terminator matches */
10701                     if (termlen == 1)   /* If is the only byte, are done */
10702                         break;
10703
10704                     /* If the remainder of the terminator matches, also are
10705                      * done, after checking that is a separate grapheme */
10706                     if (   s + termlen <= PL_bufend
10707                         && memEQ(s + 1, (char*)termstr + 1, termlen - 1))
10708                     {
10709                         if (   UTF
10710                             && UNLIKELY(! _is_grapheme((U8 *) start,
10711                                                        (U8 *) s,
10712                                                        (U8 *) PL_bufend,
10713                                                               termcode)))
10714                         {
10715                             yyerror(non_grapheme_msg);
10716                         }
10717                         break;
10718                     }
10719                 }
10720                 else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF) {
10721                     has_utf8 = TRUE;
10722                 }
10723
10724                 *to = *s;
10725             }
10726         }
10727
10728         /* if the terminator isn't the same as the start character (e.g.,
10729            matched brackets), we have to allow more in the quoting, and
10730            be prepared for nested brackets.
10731         */
10732         else {
10733             /* read until we run out of string, or we find the terminator */
10734             for (; s < PL_bufend; s++,to++) {
10735                 /* embedded newlines increment the line count */
10736                 if (*s == '\n' && !PL_rsfp && !PL_parser->filtered)
10737                     COPLINE_INC_WITH_HERELINES;
10738                 /* backslashes can escape the open or closing characters */
10739                 if (*s == '\\' && s+1 < PL_bufend) {
10740                     if (!keep_bracketed_quoted
10741                        && ( ((UV)s[1] == PL_multi_open)
10742                          || ((UV)s[1] == PL_multi_close) ))
10743                     {
10744                         s++;
10745                     }
10746                     else
10747                         *to++ = *s++;
10748                 }
10749                 /* allow nested opens and closes */
10750                 else if ((UV)*s == PL_multi_close && --brackets <= 0)
10751                     break;
10752                 else if ((UV)*s == PL_multi_open)
10753                     brackets++;
10754                 else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF)
10755                     has_utf8 = TRUE;
10756                 *to = *s;
10757             }
10758         }
10759         /* terminate the copied string and update the sv's end-of-string */
10760         *to = '\0';
10761         SvCUR_set(sv, to - SvPVX_const(sv));
10762
10763         /*
10764          * this next chunk reads more into the buffer if we're not done yet
10765          */
10766
10767         if (s < PL_bufend)
10768             break;              /* handle case where we are done yet :-) */
10769
10770 #ifndef PERL_STRICT_CR
10771         if (to - SvPVX_const(sv) >= 2) {
10772             if (   (to[-2] == '\r' && to[-1] == '\n')
10773                 || (to[-2] == '\n' && to[-1] == '\r'))
10774             {
10775                 to[-2] = '\n';
10776                 to--;
10777                 SvCUR_set(sv, to - SvPVX_const(sv));
10778             }
10779             else if (to[-1] == '\r')
10780                 to[-1] = '\n';
10781         }
10782         else if (to - SvPVX_const(sv) == 1 && to[-1] == '\r')
10783             to[-1] = '\n';
10784 #endif
10785
10786         /* if we're out of file, or a read fails, bail and reset the current
10787            line marker so we can report where the unterminated string began
10788         */
10789         COPLINE_INC_WITH_HERELINES;
10790         PL_bufptr = PL_bufend;
10791         if (!lex_next_chunk(0)) {
10792             sv_free(sv);
10793             CopLINE_set(PL_curcop, (line_t)PL_multi_start);
10794             return NULL;
10795         }
10796         s = start = PL_bufptr;
10797     }
10798
10799     /* at this point, we have successfully read the delimited string */
10800
10801     if (keep_delims)
10802             sv_catpvn(sv, s, termlen);
10803     s += termlen;
10804
10805     if (has_utf8)
10806         SvUTF8_on(sv);
10807
10808     PL_multi_end = CopLINE(PL_curcop);
10809     CopLINE_set(PL_curcop, PL_multi_start);
10810     PL_parser->herelines = herelines;
10811
10812     /* if we allocated too much space, give some back */
10813     if (SvCUR(sv) + 5 < SvLEN(sv)) {
10814         SvLEN_set(sv, SvCUR(sv) + 1);
10815         SvPV_renew(sv, SvLEN(sv));
10816     }
10817
10818     /* decide whether this is the first or second quoted string we've read
10819        for this op
10820     */
10821
10822     if (PL_lex_stuff)
10823         PL_parser->lex_sub_repl = sv;
10824     else
10825         PL_lex_stuff = sv;
10826     if (delimp) *delimp = PL_multi_open == PL_multi_close ? s-termlen : s;
10827     return s;
10828 }
10829
10830 /*
10831   scan_num
10832   takes: pointer to position in buffer
10833   returns: pointer to new position in buffer
10834   side-effects: builds ops for the constant in pl_yylval.op
10835
10836   Read a number in any of the formats that Perl accepts:
10837
10838   \d(_?\d)*(\.(\d(_?\d)*)?)?[Ee][\+\-]?(\d(_?\d)*)      12 12.34 12.
10839   \.\d(_?\d)*[Ee][\+\-]?(\d(_?\d)*)                     .34
10840   0b[01](_?[01])*                                       binary integers
10841   0[0-7](_?[0-7])*                                      octal integers
10842   0x[0-9A-Fa-f](_?[0-9A-Fa-f])*                         hexadecimal integers
10843   0x[0-9A-Fa-f](_?[0-9A-Fa-f])*(?:\.\d*)?p[+-]?[0-9]+   hexadecimal floats
10844
10845   Like most scan_ routines, it uses the PL_tokenbuf buffer to hold the
10846   thing it reads.
10847
10848   If it reads a number without a decimal point or an exponent, it will
10849   try converting the number to an integer and see if it can do so
10850   without loss of precision.
10851 */
10852
10853 char *
10854 Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
10855 {
10856     const char *s = start;      /* current position in buffer */
10857     char *d;                    /* destination in temp buffer */
10858     char *e;                    /* end of temp buffer */
10859     NV nv;                              /* number read, as a double */
10860     SV *sv = NULL;                      /* place to put the converted number */
10861     bool floatit;                       /* boolean: int or float? */
10862     const char *lastub = NULL;          /* position of last underbar */
10863     static const char* const number_too_long = "Number too long";
10864     bool warned_about_underscore = 0;
10865 #define WARN_ABOUT_UNDERSCORE() \
10866         do { \
10867             if (!warned_about_underscore) { \
10868                 warned_about_underscore = 1; \
10869                 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), \
10870                                "Misplaced _ in number"); \
10871             } \
10872         } while(0)
10873     /* Hexadecimal floating point.
10874      *
10875      * In many places (where we have quads and NV is IEEE 754 double)
10876      * we can fit the mantissa bits of a NV into an unsigned quad.
10877      * (Note that UVs might not be quads even when we have quads.)
10878      * This will not work everywhere, though (either no quads, or
10879      * using long doubles), in which case we have to resort to NV,
10880      * which will probably mean horrible loss of precision due to
10881      * multiple fp operations. */
10882     bool hexfp = FALSE;
10883     int total_bits = 0;
10884     int significant_bits = 0;
10885 #if NVSIZE == 8 && defined(HAS_QUAD) && defined(Uquad_t)
10886 #  define HEXFP_UQUAD
10887     Uquad_t hexfp_uquad = 0;
10888     int hexfp_frac_bits = 0;
10889 #else
10890 #  define HEXFP_NV
10891     NV hexfp_nv = 0.0;
10892 #endif
10893     NV hexfp_mult = 1.0;
10894     UV high_non_zero = 0; /* highest digit */
10895     int non_zero_integer_digits = 0;
10896
10897     PERL_ARGS_ASSERT_SCAN_NUM;
10898
10899     /* We use the first character to decide what type of number this is */
10900
10901     switch (*s) {
10902     default:
10903         Perl_croak(aTHX_ "panic: scan_num, *s=%d", *s);
10904
10905     /* if it starts with a 0, it could be an octal number, a decimal in
10906        0.13 disguise, or a hexadecimal number, or a binary number. */
10907     case '0':
10908         {
10909           /* variables:
10910              u          holds the "number so far"
10911              shift      the power of 2 of the base
10912                         (hex == 4, octal == 3, binary == 1)
10913              overflowed was the number more than we can hold?
10914
10915              Shift is used when we add a digit.  It also serves as an "are
10916              we in octal/hex/binary?" indicator to disallow hex characters
10917              when in octal mode.
10918            */
10919             NV n = 0.0;
10920             UV u = 0;
10921             I32 shift;
10922             bool overflowed = FALSE;
10923             bool just_zero  = TRUE;     /* just plain 0 or binary number? */
10924             static const NV nvshift[5] = { 1.0, 2.0, 4.0, 8.0, 16.0 };
10925             static const char* const bases[5] =
10926               { "", "binary", "", "octal", "hexadecimal" };
10927             static const char* const Bases[5] =
10928               { "", "Binary", "", "Octal", "Hexadecimal" };
10929             static const char* const maxima[5] =
10930               { "",
10931                 "0b11111111111111111111111111111111",
10932                 "",
10933                 "037777777777",
10934                 "0xffffffff" };
10935             const char *base, *Base, *max;
10936
10937             /* check for hex */
10938             if (isALPHA_FOLD_EQ(s[1], 'x')) {
10939                 shift = 4;
10940                 s += 2;
10941                 just_zero = FALSE;
10942             } else if (isALPHA_FOLD_EQ(s[1], 'b')) {
10943                 shift = 1;
10944                 s += 2;
10945                 just_zero = FALSE;
10946             }
10947             /* check for a decimal in disguise */
10948             else if (s[1] == '.' || isALPHA_FOLD_EQ(s[1], 'e'))
10949                 goto decimal;
10950             /* so it must be octal */
10951             else {
10952                 shift = 3;
10953                 s++;
10954             }
10955
10956             if (*s == '_') {
10957                 WARN_ABOUT_UNDERSCORE();
10958                lastub = s++;
10959             }
10960
10961             base = bases[shift];
10962             Base = Bases[shift];
10963             max  = maxima[shift];
10964
10965             /* read the rest of the number */
10966             for (;;) {
10967                 /* x is used in the overflow test,
10968                    b is the digit we're adding on. */
10969                 UV x, b;
10970
10971                 switch (*s) {
10972
10973                 /* if we don't mention it, we're done */
10974                 default:
10975                     goto out;
10976
10977                 /* _ are ignored -- but warned about if consecutive */
10978                 case '_':
10979                     if (lastub && s == lastub + 1)
10980                         WARN_ABOUT_UNDERSCORE();
10981                     lastub = s++;
10982                     break;
10983
10984                 /* 8 and 9 are not octal */
10985                 case '8': case '9':
10986                     if (shift == 3)
10987                         yyerror(Perl_form(aTHX_ "Illegal octal digit '%c'", *s));
10988                     /* FALLTHROUGH */
10989
10990                 /* octal digits */
10991                 case '2': case '3': case '4':
10992                 case '5': case '6': case '7':
10993                     if (shift == 1)
10994                         yyerror(Perl_form(aTHX_ "Illegal binary digit '%c'", *s));
10995                     /* FALLTHROUGH */
10996
10997                 case '0': case '1':
10998                     b = *s++ & 15;              /* ASCII digit -> value of digit */
10999                     goto digit;
11000
11001                 /* hex digits */
11002                 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
11003                 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
11004                     /* make sure they said 0x */
11005                     if (shift != 4)
11006                         goto out;
11007                     b = (*s++ & 7) + 9;
11008
11009                     /* Prepare to put the digit we have onto the end
11010                        of the number so far.  We check for overflows.
11011                     */
11012
11013                   digit:
11014                     just_zero = FALSE;
11015                     if (!overflowed) {
11016                         assert(shift >= 0);
11017                         x = u << shift; /* make room for the digit */
11018
11019                         total_bits += shift;
11020
11021                         if ((x >> shift) != u
11022                             && !(PL_hints & HINT_NEW_BINARY)) {
11023                             overflowed = TRUE;
11024                             n = (NV) u;
11025                             Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
11026                                              "Integer overflow in %s number",
11027                                              base);
11028                         } else
11029                             u = x | b;          /* add the digit to the end */
11030                     }
11031                     if (overflowed) {
11032                         n *= nvshift[shift];
11033                         /* If an NV has not enough bits in its
11034                          * mantissa to represent an UV this summing of
11035                          * small low-order numbers is a waste of time
11036                          * (because the NV cannot preserve the
11037                          * low-order bits anyway): we could just
11038                          * remember when did we overflow and in the
11039                          * end just multiply n by the right
11040                          * amount. */
11041                         n += (NV) b;
11042                     }
11043
11044                     if (high_non_zero == 0 && b > 0)
11045                         high_non_zero = b;
11046
11047                     if (high_non_zero)
11048                         non_zero_integer_digits++;
11049
11050                     /* this could be hexfp, but peek ahead
11051                      * to avoid matching ".." */
11052                     if (UNLIKELY(HEXFP_PEEK(s))) {
11053                         goto out;
11054                     }
11055
11056                     break;
11057                 }
11058             }
11059
11060           /* if we get here, we had success: make a scalar value from
11061              the number.
11062           */
11063           out:
11064
11065             /* final misplaced underbar check */
11066             if (s[-1] == '_')
11067                 WARN_ABOUT_UNDERSCORE();
11068
11069             if (UNLIKELY(HEXFP_PEEK(s))) {
11070                 /* Do sloppy (on the underbars) but quick detection
11071                  * (and value construction) for hexfp, the decimal
11072                  * detection will shortly be more thorough with the
11073                  * underbar checks. */
11074                 const char* h = s;
11075                 significant_bits = non_zero_integer_digits * shift;
11076 #ifdef HEXFP_UQUAD
11077                 hexfp_uquad = u;
11078 #else /* HEXFP_NV */
11079                 hexfp_nv = u;
11080 #endif
11081                 /* Ignore the leading zero bits of
11082                  * the high (first) non-zero digit. */
11083                 if (high_non_zero) {
11084                     if (high_non_zero < 0x8)
11085                         significant_bits--;
11086                     if (high_non_zero < 0x4)
11087                         significant_bits--;
11088                     if (high_non_zero < 0x2)
11089                         significant_bits--;
11090                 }
11091
11092                 if (*h == '.') {
11093 #ifdef HEXFP_NV
11094                     NV nv_mult = 1.0;
11095 #endif
11096                     bool accumulate = TRUE;
11097                     U8 b;
11098                     int lim = 1 << shift;
11099                     for (h++; ((isXDIGIT(*h) && (b = XDIGIT_VALUE(*h)) < lim) ||
11100                                *h == '_'); h++) {
11101                         if (isXDIGIT(*h)) {
11102                             significant_bits += shift;
11103 #ifdef HEXFP_UQUAD
11104                             if (accumulate) {
11105                                 if (significant_bits < NV_MANT_DIG) {
11106                                     /* We are in the long "run" of xdigits,
11107                                      * accumulate the full four bits. */
11108                                     assert(shift >= 0);
11109                                     hexfp_uquad <<= shift;
11110                                     hexfp_uquad |= b;
11111                                     hexfp_frac_bits += shift;
11112                                 } else if (significant_bits - shift < NV_MANT_DIG) {
11113                                     /* We are at a hexdigit either at,
11114                                      * or straddling, the edge of mantissa.
11115                                      * We will try grabbing as many as
11116                                      * possible bits. */
11117                                     int tail =
11118                                       significant_bits - NV_MANT_DIG;
11119                                     if (tail <= 0)
11120                                        tail += shift;
11121                                     assert(tail >= 0);
11122                                     hexfp_uquad <<= tail;
11123                                     assert((shift - tail) >= 0);
11124                                     hexfp_uquad |= b >> (shift - tail);
11125                                     hexfp_frac_bits += tail;
11126
11127                                     /* Ignore the trailing zero bits
11128                                      * of the last non-zero xdigit.
11129                                      *
11130                                      * The assumption here is that if
11131                                      * one has input of e.g. the xdigit
11132                                      * eight (0x8), there is only one
11133                                      * bit being input, not the full
11134                                      * four bits.  Conversely, if one
11135                                      * specifies a zero xdigit, the
11136                                      * assumption is that one really
11137                                      * wants all those bits to be zero. */
11138                                     if (b) {
11139                                         if ((b & 0x1) == 0x0) {
11140                                             significant_bits--;
11141                                             if ((b & 0x2) == 0x0) {
11142                                                 significant_bits--;
11143                                                 if ((b & 0x4) == 0x0) {
11144                                                     significant_bits--;
11145                                                 }
11146                                             }
11147                                         }
11148                                     }
11149
11150                                     accumulate = FALSE;
11151                                 }
11152                             } else {
11153                                 /* Keep skipping the xdigits, and
11154                                  * accumulating the significant bits,
11155                                  * but do not shift the uquad
11156                                  * (which would catastrophically drop
11157                                  * high-order bits) or accumulate the
11158                                  * xdigits anymore. */
11159                             }
11160 #else /* HEXFP_NV */
11161                             if (accumulate) {
11162                                 nv_mult /= nvshift[shift];
11163                                 if (nv_mult > 0.0)
11164                                     hexfp_nv += b * nv_mult;
11165                                 else
11166                                     accumulate = FALSE;
11167                             }
11168 #endif
11169                         }
11170                         if (significant_bits >= NV_MANT_DIG)
11171                             accumulate = FALSE;
11172                     }
11173                 }
11174
11175                 if ((total_bits > 0 || significant_bits > 0) &&
11176                     isALPHA_FOLD_EQ(*h, 'p')) {
11177                     bool negexp = FALSE;
11178                     h++;
11179                     if (*h == '+')
11180                         h++;
11181                     else if (*h == '-') {
11182                         negexp = TRUE;
11183                         h++;
11184                     }
11185                     if (isDIGIT(*h)) {
11186                         I32 hexfp_exp = 0;
11187                         while (isDIGIT(*h) || *h == '_') {
11188                             if (isDIGIT(*h)) {
11189                                 hexfp_exp *= 10;
11190                                 hexfp_exp += *h - '0';
11191 #ifdef NV_MIN_EXP
11192                                 if (negexp
11193                                     && -hexfp_exp < NV_MIN_EXP - 1) {
11194                                     /* NOTE: this means that the exponent
11195                                      * underflow warning happens for
11196                                      * the IEEE 754 subnormals (denormals),
11197                                      * because DBL_MIN_EXP etc are the lowest
11198                                      * possible binary (or, rather, DBL_RADIX-base)
11199                                      * exponent for normals, not subnormals.
11200                                      *
11201                                      * This may or may not be a good thing. */
11202                                     Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
11203                                                    "Hexadecimal float: exponent underflow");
11204                                     break;
11205                                 }
11206 #endif
11207 #ifdef NV_MAX_EXP
11208                                 if (!negexp
11209                                     && hexfp_exp > NV_MAX_EXP - 1) {
11210                                     Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
11211                                                    "Hexadecimal float: exponent overflow");
11212                                     break;
11213                                 }
11214 #endif
11215                             }
11216                             h++;
11217                         }
11218                         if (negexp)
11219                             hexfp_exp = -hexfp_exp;
11220 #ifdef HEXFP_UQUAD
11221                         hexfp_exp -= hexfp_frac_bits;
11222 #endif
11223                         hexfp_mult = Perl_pow(2.0, hexfp_exp);
11224                         hexfp = TRUE;
11225                         goto decimal;
11226                     }
11227                 }
11228             }
11229
11230             if (overflowed) {
11231                 if (n > 4294967295.0)
11232                     Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
11233                                    "%s number > %s non-portable",
11234                                    Base, max);
11235                 sv = newSVnv(n);
11236             }
11237             else {
11238 #if UVSIZE > 4
11239                 if (u > 0xffffffff)
11240                     Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
11241                                    "%s number > %s non-portable",
11242                                    Base, max);
11243 #endif
11244                 sv = newSVuv(u);
11245             }
11246             if (just_zero && (PL_hints & HINT_NEW_INTEGER))
11247                 sv = new_constant(start, s - start, "integer",
11248                                   sv, NULL, NULL, 0);
11249             else if (PL_hints & HINT_NEW_BINARY)
11250                 sv = new_constant(start, s - start, "binary", sv, NULL, NULL, 0);
11251         }
11252         break;
11253
11254     /*
11255       handle decimal numbers.
11256       we're also sent here when we read a 0 as the first digit
11257     */
11258     case '1': case '2': case '3': case '4': case '5':
11259     case '6': case '7': case '8': case '9': case '.':
11260       decimal:
11261         d = PL_tokenbuf;
11262         e = PL_tokenbuf + sizeof PL_tokenbuf - 6; /* room for various punctuation */
11263         floatit = FALSE;
11264         if (hexfp) {
11265             floatit = TRUE;
11266             *d++ = '0';
11267             *d++ = 'x';
11268             s = start + 2;
11269         }
11270
11271         /* read next group of digits and _ and copy into d */
11272         while (isDIGIT(*s)
11273                || *s == '_'
11274                || UNLIKELY(hexfp && isXDIGIT(*s)))
11275         {
11276             /* skip underscores, checking for misplaced ones
11277                if -w is on
11278             */
11279             if (*s == '_') {
11280                 if (lastub && s == lastub + 1)
11281                     WARN_ABOUT_UNDERSCORE();
11282                 lastub = s++;
11283             }
11284             else {
11285                 /* check for end of fixed-length buffer */
11286                 if (d >= e)
11287                     Perl_croak(aTHX_ "%s", number_too_long);
11288                 /* if we're ok, copy the character */
11289                 *d++ = *s++;
11290             }
11291         }
11292
11293         /* final misplaced underbar check */
11294         if (lastub && s == lastub + 1)
11295             WARN_ABOUT_UNDERSCORE();
11296
11297         /* read a decimal portion if there is one.  avoid
11298            3..5 being interpreted as the number 3. followed
11299            by .5
11300         */
11301         if (*s == '.' && s[1] != '.') {
11302             floatit = TRUE;
11303             *d++ = *s++;
11304
11305             if (*s == '_') {
11306                 WARN_ABOUT_UNDERSCORE();
11307                 lastub = s;
11308             }
11309
11310             /* copy, ignoring underbars, until we run out of digits.
11311             */
11312             for (; isDIGIT(*s)
11313                    || *s == '_'
11314                    || UNLIKELY(hexfp && isXDIGIT(*s));
11315                  s++)
11316             {
11317                 /* fixed length buffer check */
11318                 if (d >= e)
11319                     Perl_croak(aTHX_ "%s", number_too_long);
11320                 if (*s == '_') {
11321                    if (lastub && s == lastub + 1)
11322                         WARN_ABOUT_UNDERSCORE();
11323                    lastub = s;
11324                 }
11325                 else
11326                     *d++ = *s;
11327             }
11328             /* fractional part ending in underbar? */
11329             if (s[-1] == '_')
11330                 WARN_ABOUT_UNDERSCORE();
11331             if (*s == '.' && isDIGIT(s[1])) {
11332                 /* oops, it's really a v-string, but without the "v" */
11333                 s = start;
11334                 goto vstring;
11335             }
11336         }
11337
11338         /* read exponent part, if present */
11339         if ((isALPHA_FOLD_EQ(*s, 'e')
11340               || UNLIKELY(hexfp && isALPHA_FOLD_EQ(*s, 'p')))
11341             && strchr("+-0123456789_", s[1]))
11342         {
11343             int exp_digits = 0;
11344             const char *save_s = s;
11345             char * save_d = d;
11346
11347             /* regardless of whether user said 3E5 or 3e5, use lower 'e',
11348                ditto for p (hexfloats) */
11349             if ((isALPHA_FOLD_EQ(*s, 'e'))) {
11350                 /* At least some Mach atof()s don't grok 'E' */
11351                 *d++ = 'e';
11352             }
11353             else if (UNLIKELY(hexfp && (isALPHA_FOLD_EQ(*s, 'p')))) {
11354                 *d++ = 'p';
11355             }
11356
11357             s++;
11358
11359
11360             /* stray preinitial _ */
11361             if (*s == '_') {
11362                 WARN_ABOUT_UNDERSCORE();
11363                 lastub = s++;
11364             }
11365
11366             /* allow positive or negative exponent */
11367             if (*s == '+' || *s == '-')
11368                 *d++ = *s++;
11369
11370             /* stray initial _ */
11371             if (*s == '_') {
11372                 WARN_ABOUT_UNDERSCORE();
11373                 lastub = s++;
11374             }
11375
11376             /* read digits of exponent */
11377             while (isDIGIT(*s) || *s == '_') {
11378                 if (isDIGIT(*s)) {
11379                     ++exp_digits;
11380                     if (d >= e)
11381                         Perl_croak(aTHX_ "%s", number_too_long);
11382                     *d++ = *s++;
11383                 }
11384                 else {
11385                    if (((lastub && s == lastub + 1)
11386                         || (!isDIGIT(s[1]) && s[1] != '_')))
11387                         WARN_ABOUT_UNDERSCORE();
11388                    lastub = s++;
11389                 }
11390             }
11391
11392             if (!exp_digits) {
11393                 /* no exponent digits, the [eEpP] could be for something else,
11394                  * though in practice we don't get here for p since that's preparsed
11395                  * earlier, and results in only the 0xX being consumed, so behave similarly
11396                  * for decimal floats and consume only the D.DD, leaving the [eE] to the
11397                  * next token.
11398                  */
11399                 s = save_s;
11400                 d = save_d;
11401             }
11402             else {
11403                 floatit = TRUE;
11404             }
11405         }
11406
11407
11408         /*
11409            We try to do an integer conversion first if no characters
11410            indicating "float" have been found.
11411          */
11412
11413         if (!floatit) {
11414             UV uv;
11415             const int flags = grok_number (PL_tokenbuf, d - PL_tokenbuf, &uv);
11416
11417             if (flags == IS_NUMBER_IN_UV) {
11418               if (uv <= IV_MAX)
11419                 sv = newSViv(uv); /* Prefer IVs over UVs. */
11420               else
11421                 sv = newSVuv(uv);
11422             } else if (flags == (IS_NUMBER_IN_UV | IS_NUMBER_NEG)) {
11423               if (uv <= (UV) IV_MIN)
11424                 sv = newSViv(-(IV)uv);
11425               else
11426                 floatit = TRUE;
11427             } else
11428               floatit = TRUE;
11429         }
11430         if (floatit) {
11431             /* terminate the string */
11432             *d = '\0';
11433             if (UNLIKELY(hexfp)) {
11434 #  ifdef NV_MANT_DIG
11435                 if (significant_bits > NV_MANT_DIG)
11436                     Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
11437                                    "Hexadecimal float: mantissa overflow");
11438 #  endif
11439 #ifdef HEXFP_UQUAD
11440                 nv = hexfp_uquad * hexfp_mult;
11441 #else /* HEXFP_NV */
11442                 nv = hexfp_nv * hexfp_mult;
11443 #endif
11444             } else {
11445                 nv = Atof(PL_tokenbuf);
11446             }
11447             sv = newSVnv(nv);
11448         }
11449
11450         if ( floatit
11451              ? (PL_hints & HINT_NEW_FLOAT) : (PL_hints & HINT_NEW_INTEGER) ) {
11452             const char *const key = floatit ? "float" : "integer";
11453             const STRLEN keylen = floatit ? 5 : 7;
11454             sv = S_new_constant(aTHX_ PL_tokenbuf, d - PL_tokenbuf,
11455                                 key, keylen, sv, NULL, NULL, 0);
11456         }
11457         break;
11458
11459     /* if it starts with a v, it could be a v-string */
11460     case 'v':
11461     vstring:
11462                 sv = newSV(5); /* preallocate storage space */
11463                 ENTER_with_name("scan_vstring");
11464                 SAVEFREESV(sv);
11465                 s = scan_vstring(s, PL_bufend, sv);
11466                 SvREFCNT_inc_simple_void_NN(sv);
11467                 LEAVE_with_name("scan_vstring");
11468         break;
11469     }
11470
11471     /* make the op for the constant and return */
11472
11473     if (sv)
11474         lvalp->opval = newSVOP(OP_CONST, 0, sv);
11475     else
11476         lvalp->opval = NULL;
11477
11478     return (char *)s;
11479 }
11480
11481 STATIC char *
11482 S_scan_formline(pTHX_ char *s)
11483 {
11484     SV * const stuff = newSVpvs("");
11485     bool needargs = FALSE;
11486     bool eofmt = FALSE;
11487
11488     PERL_ARGS_ASSERT_SCAN_FORMLINE;
11489
11490     while (!needargs) {
11491         char *eol;
11492         if (*s == '.') {
11493             char *t = s+1;
11494 #ifdef PERL_STRICT_CR
11495             while (SPACE_OR_TAB(*t))
11496                 t++;
11497 #else
11498             while (SPACE_OR_TAB(*t) || *t == '\r')
11499                 t++;
11500 #endif
11501             if (*t == '\n' || t == PL_bufend) {
11502                 eofmt = TRUE;
11503                 break;
11504             }
11505         }
11506         eol = (char *) memchr(s,'\n',PL_bufend-s);
11507         if (!eol++)
11508                 eol = PL_bufend;
11509         if (*s != '#') {
11510             char *t;
11511             for (t = s; t < eol; t++) {
11512                 if (*t == '~' && t[1] == '~' && SvCUR(stuff)) {
11513                     needargs = FALSE;
11514                     goto enough;        /* ~~ must be first line in formline */
11515                 }
11516                 if (*t == '@' || *t == '^')
11517                     needargs = TRUE;
11518             }
11519             if (eol > s) {
11520                 sv_catpvn(stuff, s, eol-s);
11521 #ifndef PERL_STRICT_CR
11522                 if (eol-s > 1 && eol[-2] == '\r' && eol[-1] == '\n') {
11523                     char *end = SvPVX(stuff) + SvCUR(stuff);
11524                     end[-2] = '\n';
11525                     end[-1] = '\0';
11526                     SvCUR_set(stuff, SvCUR(stuff) - 1);
11527                 }
11528 #endif
11529             }
11530             else
11531               break;
11532         }
11533         s = (char*)eol;
11534         if ((PL_rsfp || PL_parser->filtered)
11535          && PL_parser->form_lex_state == LEX_NORMAL) {
11536             bool got_some;
11537             PL_bufptr = PL_bufend;
11538             COPLINE_INC_WITH_HERELINES;
11539             got_some = lex_next_chunk(0);
11540             CopLINE_dec(PL_curcop);
11541             s = PL_bufptr;
11542             if (!got_some)
11543                 break;
11544         }
11545         incline(s, PL_bufend);
11546     }
11547   enough:
11548     if (!SvCUR(stuff) || needargs)
11549         PL_lex_state = PL_parser->form_lex_state;
11550     if (SvCUR(stuff)) {
11551         PL_expect = XSTATE;
11552         if (needargs) {
11553             const char *s2 = s;
11554             while (isSPACE(*s2) && *s2 != '\n')
11555                 s2++;
11556             if (*s2 == '{') {
11557                 PL_expect = XTERMBLOCK;
11558                 NEXTVAL_NEXTTOKE.ival = 0;
11559                 force_next(DO);
11560             }
11561             NEXTVAL_NEXTTOKE.ival = 0;
11562             force_next(FORMLBRACK);
11563         }
11564         if (!IN_BYTES) {
11565             if (UTF && is_utf8_string((U8*)SvPVX_const(stuff), SvCUR(stuff)))
11566                 SvUTF8_on(stuff);
11567         }
11568         NEXTVAL_NEXTTOKE.opval = newSVOP(OP_CONST, 0, stuff);
11569         force_next(THING);
11570     }
11571     else {
11572         SvREFCNT_dec(stuff);
11573         if (eofmt)
11574             PL_lex_formbrack = 0;
11575     }
11576     return s;
11577 }
11578
11579 I32
11580 Perl_start_subparse(pTHX_ I32 is_format, U32 flags)
11581 {
11582     const I32 oldsavestack_ix = PL_savestack_ix;
11583     CV* const outsidecv = PL_compcv;
11584
11585     SAVEI32(PL_subline);
11586     save_item(PL_subname);
11587     SAVESPTR(PL_compcv);
11588
11589     PL_compcv = MUTABLE_CV(newSV_type(is_format ? SVt_PVFM : SVt_PVCV));
11590     CvFLAGS(PL_compcv) |= flags;
11591
11592     PL_subline = CopLINE(PL_curcop);
11593     CvPADLIST(PL_compcv) = pad_new(padnew_SAVE|padnew_SAVESUB);
11594     CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outsidecv));
11595     CvOUTSIDE_SEQ(PL_compcv) = PL_cop_seqmax;
11596     if (outsidecv && CvPADLIST(outsidecv))
11597         CvPADLIST(PL_compcv)->xpadl_outid = CvPADLIST(outsidecv)->xpadl_id;
11598
11599     return oldsavestack_ix;
11600 }
11601
11602
11603 /* Do extra initialisation of a CV (typically one just created by
11604  * start_subparse()) if that CV is for a named sub
11605  */
11606
11607 void
11608 Perl_init_named_cv(pTHX_ CV *cv, OP *nameop)
11609 {
11610     PERL_ARGS_ASSERT_INIT_NAMED_CV;
11611
11612     if (nameop->op_type == OP_CONST) {
11613         const char *const name = SvPV_nolen_const(((SVOP*)nameop)->op_sv);
11614         if (   strEQ(name, "BEGIN")
11615             || strEQ(name, "END")
11616             || strEQ(name, "INIT")
11617             || strEQ(name, "CHECK")
11618             || strEQ(name, "UNITCHECK")
11619         )
11620           CvSPECIAL_on(cv);
11621     }
11622     else
11623     /* State subs inside anonymous subs need to be
11624      clonable themselves. */
11625     if (   CvANON(CvOUTSIDE(cv))
11626         || CvCLONE(CvOUTSIDE(cv))
11627         || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
11628                         CvOUTSIDE(cv)
11629                      ))[nameop->op_targ])
11630     )
11631       CvCLONE_on(cv);
11632 }
11633
11634
11635 static int
11636 S_yywarn(pTHX_ const char *const s, U32 flags)
11637 {
11638     PERL_ARGS_ASSERT_YYWARN;
11639
11640     PL_in_eval |= EVAL_WARNONLY;
11641     yyerror_pv(s, flags);
11642     return 0;
11643 }
11644
11645 void
11646 Perl_abort_execution(pTHX_ const char * const msg, const char * const name)
11647 {
11648     PERL_ARGS_ASSERT_ABORT_EXECUTION;
11649
11650     if (PL_minus_c)
11651         Perl_croak(aTHX_ "%s%s had compilation errors.\n", msg, name);
11652     else {
11653         Perl_croak(aTHX_
11654                 "%sExecution of %s aborted due to compilation errors.\n", msg, name);
11655     }
11656     NOT_REACHED; /* NOTREACHED */
11657 }
11658
11659 void
11660 Perl_yyquit(pTHX)
11661 {
11662     /* Called, after at least one error has been found, to abort the parse now,
11663      * instead of trying to forge ahead */
11664
11665     yyerror_pvn(NULL, 0, 0);
11666 }
11667
11668 int
11669 Perl_yyerror(pTHX_ const char *const s)
11670 {
11671     PERL_ARGS_ASSERT_YYERROR;
11672     return yyerror_pvn(s, strlen(s), 0);
11673 }
11674
11675 int
11676 Perl_yyerror_pv(pTHX_ const char *const s, U32 flags)
11677 {
11678     PERL_ARGS_ASSERT_YYERROR_PV;
11679     return yyerror_pvn(s, strlen(s), flags);
11680 }
11681
11682 int
11683 Perl_yyerror_pvn(pTHX_ const char *const s, STRLEN len, U32 flags)
11684 {
11685     const char *context = NULL;
11686     int contlen = -1;
11687     SV *msg;
11688     SV * const where_sv = newSVpvs_flags("", SVs_TEMP);
11689     int yychar  = PL_parser->yychar;
11690
11691     /* Output error message 's' with length 'len'.  'flags' are SV flags that
11692      * apply.  If the number of errors found is large enough, it abandons
11693      * parsing.  If 's' is NULL, there is no message, and it abandons
11694      * processing unconditionally */
11695
11696     if (s != NULL) {
11697         if (!yychar || (yychar == ';' && !PL_rsfp))
11698             sv_catpvs(where_sv, "at EOF");
11699         else if (   PL_oldoldbufptr
11700                  && PL_bufptr > PL_oldoldbufptr
11701                  && PL_bufptr - PL_oldoldbufptr < 200
11702                  && PL_oldoldbufptr != PL_oldbufptr
11703                  && PL_oldbufptr != PL_bufptr)
11704         {
11705             /*
11706                     Only for NetWare:
11707                     The code below is removed for NetWare because it
11708                     abends/crashes on NetWare when the script has error such as
11709                     not having the closing quotes like:
11710                         if ($var eq "value)
11711                     Checking of white spaces is anyway done in NetWare code.
11712             */
11713 #ifndef NETWARE
11714             while (isSPACE(*PL_oldoldbufptr))
11715                 PL_oldoldbufptr++;
11716 #endif
11717             context = PL_oldoldbufptr;
11718             contlen = PL_bufptr - PL_oldoldbufptr;
11719         }
11720         else if (  PL_oldbufptr
11721                 && PL_bufptr > PL_oldbufptr
11722                 && PL_bufptr - PL_oldbufptr < 200
11723                 && PL_oldbufptr != PL_bufptr) {
11724             /*
11725                     Only for NetWare:
11726                     The code below is removed for NetWare because it
11727                     abends/crashes on NetWare when the script has error such as
11728                     not having the closing quotes like:
11729                         if ($var eq "value)
11730                     Checking of white spaces is anyway done in NetWare code.
11731             */
11732 #ifndef NETWARE
11733             while (isSPACE(*PL_oldbufptr))
11734                 PL_oldbufptr++;
11735 #endif
11736             context = PL_oldbufptr;
11737             contlen = PL_bufptr - PL_oldbufptr;
11738         }
11739         else if (yychar > 255)
11740             sv_catpvs(where_sv, "next token ???");
11741         else if (yychar == YYEMPTY) {
11742             if (PL_lex_state == LEX_NORMAL)
11743                 sv_catpvs(where_sv, "at end of line");
11744             else if (PL_lex_inpat)
11745                 sv_catpvs(where_sv, "within pattern");
11746             else
11747                 sv_catpvs(where_sv, "within string");
11748         }
11749         else {
11750             sv_catpvs(where_sv, "next char ");
11751             if (yychar < 32)
11752                 Perl_sv_catpvf(aTHX_ where_sv, "^%c", toCTRL(yychar));
11753             else if (isPRINT_LC(yychar)) {
11754                 const char string = yychar;
11755                 sv_catpvn(where_sv, &string, 1);
11756             }
11757             else
11758                 Perl_sv_catpvf(aTHX_ where_sv, "\\%03o", yychar & 255);
11759         }
11760         msg = newSVpvn_flags(s, len, (flags & SVf_UTF8) | SVs_TEMP);
11761         Perl_sv_catpvf(aTHX_ msg, " at %s line %" IVdf ", ",
11762             OutCopFILE(PL_curcop),
11763             (IV)(PL_parser->preambling == NOLINE
11764                    ? CopLINE(PL_curcop)
11765                    : PL_parser->preambling));
11766         if (context)
11767             Perl_sv_catpvf(aTHX_ msg, "near \"%" UTF8f "\"\n",
11768                                  UTF8fARG(UTF, contlen, context));
11769         else
11770             Perl_sv_catpvf(aTHX_ msg, "%" SVf "\n", SVfARG(where_sv));
11771         if (   PL_multi_start < PL_multi_end
11772             && (U32)(CopLINE(PL_curcop) - PL_multi_end) <= 1)
11773         {
11774             Perl_sv_catpvf(aTHX_ msg,
11775             "  (Might be a runaway multi-line %c%c string starting on"
11776             " line %" IVdf ")\n",
11777                     (int)PL_multi_open,(int)PL_multi_close,(IV)PL_multi_start);
11778             PL_multi_end = 0;
11779         }
11780         if (PL_in_eval & EVAL_WARNONLY) {
11781             PL_in_eval &= ~EVAL_WARNONLY;
11782             Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX), "%" SVf, SVfARG(msg));
11783         }
11784         else {
11785             qerror(msg);
11786         }
11787     }
11788     if (s == NULL || PL_error_count >= 10) {
11789         const char * msg = "";
11790         const char * const name = OutCopFILE(PL_curcop);
11791
11792         if (PL_in_eval) {
11793             SV * errsv = ERRSV;
11794             if (SvCUR(errsv)) {
11795                 msg = Perl_form(aTHX_ "%" SVf, SVfARG(errsv));
11796             }
11797         }
11798
11799         if (s == NULL) {
11800             abort_execution(msg, name);
11801         }
11802         else {
11803             Perl_croak(aTHX_ "%s%s has too many errors.\n", msg, name);
11804         }
11805     }
11806     PL_in_my = 0;
11807     PL_in_my_stash = NULL;
11808     return 0;
11809 }
11810
11811 STATIC char*
11812 S_swallow_bom(pTHX_ U8 *s)
11813 {
11814     const STRLEN slen = SvCUR(PL_linestr);
11815
11816     PERL_ARGS_ASSERT_SWALLOW_BOM;
11817
11818     switch (s[0]) {
11819     case 0xFF:
11820         if (s[1] == 0xFE) {
11821             /* UTF-16 little-endian? (or UTF-32LE?) */
11822             if (s[2] == 0 && s[3] == 0)  /* UTF-32 little-endian */
11823                 /* diag_listed_as: Unsupported script encoding %s */
11824                 Perl_croak(aTHX_ "Unsupported script encoding UTF-32LE");
11825 #ifndef PERL_NO_UTF16_FILTER
11826 #ifdef DEBUGGING
11827             if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (BOM)\n");
11828 #endif
11829             s += 2;
11830             if (PL_bufend > (char*)s) {
11831                 s = add_utf16_textfilter(s, TRUE);
11832             }
11833 #else
11834             /* diag_listed_as: Unsupported script encoding %s */
11835             Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
11836 #endif
11837         }
11838         break;
11839     case 0xFE:
11840         if (s[1] == 0xFF) {   /* UTF-16 big-endian? */
11841 #ifndef PERL_NO_UTF16_FILTER
11842 #ifdef DEBUGGING
11843             if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (BOM)\n");
11844 #endif
11845             s += 2;
11846             if (PL_bufend > (char *)s) {
11847                 s = add_utf16_textfilter(s, FALSE);
11848             }
11849 #else
11850             /* diag_listed_as: Unsupported script encoding %s */
11851             Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
11852 #endif
11853         }
11854         break;
11855     case BOM_UTF8_FIRST_BYTE: {
11856         if (memBEGINs(s+1, slen - 1, BOM_UTF8_TAIL)) {
11857 #ifdef DEBUGGING
11858             if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-8 script encoding (BOM)\n");
11859 #endif
11860             s += sizeof(BOM_UTF8) - 1;                     /* UTF-8 */
11861         }
11862         break;
11863     }
11864     case 0:
11865         if (slen > 3) {
11866              if (s[1] == 0) {
11867                   if (s[2] == 0xFE && s[3] == 0xFF) {
11868                        /* UTF-32 big-endian */
11869                        /* diag_listed_as: Unsupported script encoding %s */
11870                        Perl_croak(aTHX_ "Unsupported script encoding UTF-32BE");
11871                   }
11872              }
11873              else if (s[2] == 0 && s[3] != 0) {
11874                   /* Leading bytes
11875                    * 00 xx 00 xx
11876                    * are a good indicator of UTF-16BE. */
11877 #ifndef PERL_NO_UTF16_FILTER
11878 #ifdef DEBUGGING
11879                   if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (no BOM)\n");
11880 #endif
11881                   s = add_utf16_textfilter(s, FALSE);
11882 #else
11883                   /* diag_listed_as: Unsupported script encoding %s */
11884                   Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
11885 #endif
11886              }
11887         }
11888         break;
11889
11890     default:
11891          if (slen > 3 && s[1] == 0 && s[2] != 0 && s[3] == 0) {
11892                   /* Leading bytes
11893                    * xx 00 xx 00
11894                    * are a good indicator of UTF-16LE. */
11895 #ifndef PERL_NO_UTF16_FILTER
11896 #ifdef DEBUGGING
11897               if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (no BOM)\n");
11898 #endif
11899               s = add_utf16_textfilter(s, TRUE);
11900 #else
11901               /* diag_listed_as: Unsupported script encoding %s */
11902               Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
11903 #endif
11904          }
11905     }
11906     return (char*)s;
11907 }
11908
11909
11910 #ifndef PERL_NO_UTF16_FILTER
11911 static I32
11912 S_utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen)
11913 {
11914     SV *const filter = FILTER_DATA(idx);
11915     /* We re-use this each time round, throwing the contents away before we
11916        return.  */
11917     SV *const utf16_buffer = MUTABLE_SV(IoTOP_GV(filter));
11918     SV *const utf8_buffer = filter;
11919     IV status = IoPAGE(filter);
11920     const bool reverse = cBOOL(IoLINES(filter));
11921     I32 retval;
11922
11923     PERL_ARGS_ASSERT_UTF16_TEXTFILTER;
11924
11925     /* As we're automatically added, at the lowest level, and hence only called
11926        from this file, we can be sure that we're not called in block mode. Hence
11927        don't bother writing code to deal with block mode.  */
11928     if (maxlen) {
11929         Perl_croak(aTHX_ "panic: utf16_textfilter called in block mode (for %d characters)", maxlen);
11930     }
11931     if (status < 0) {
11932         Perl_croak(aTHX_ "panic: utf16_textfilter called after error (status=%" IVdf ")", status);
11933     }
11934     DEBUG_P(PerlIO_printf(Perl_debug_log,
11935                           "utf16_textfilter(%p,%ce): idx=%d maxlen=%d status=%" IVdf " utf16=%" UVuf " utf8=%" UVuf "\n",
11936                           FPTR2DPTR(void *, S_utf16_textfilter),
11937                           reverse ? 'l' : 'b', idx, maxlen, status,
11938                           (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
11939
11940     while (1) {
11941         STRLEN chars;
11942         STRLEN have;
11943         I32 newlen;
11944         U8 *end;
11945         /* First, look in our buffer of existing UTF-8 data:  */
11946         char *nl = (char *)memchr(SvPVX(utf8_buffer), '\n', SvCUR(utf8_buffer));
11947
11948         if (nl) {
11949             ++nl;
11950         } else if (status == 0) {
11951             /* EOF */
11952             IoPAGE(filter) = 0;
11953             nl = SvEND(utf8_buffer);
11954         }
11955         if (nl) {
11956             STRLEN got = nl - SvPVX(utf8_buffer);
11957             /* Did we have anything to append?  */
11958             retval = got != 0;
11959             sv_catpvn(sv, SvPVX(utf8_buffer), got);
11960             /* Everything else in this code works just fine if SVp_POK isn't
11961                set.  This, however, needs it, and we need it to work, else
11962                we loop infinitely because the buffer is never consumed.  */
11963             sv_chop(utf8_buffer, nl);
11964             break;
11965         }
11966
11967         /* OK, not a complete line there, so need to read some more UTF-16.
11968            Read an extra octect if the buffer currently has an odd number. */
11969         while (1) {
11970             if (status <= 0)
11971                 break;
11972             if (SvCUR(utf16_buffer) >= 2) {
11973                 /* Location of the high octet of the last complete code point.
11974                    Gosh, UTF-16 is a pain. All the benefits of variable length,
11975                    *coupled* with all the benefits of partial reads and
11976                    endianness.  */
11977                 const U8 *const last_hi = (U8*)SvPVX(utf16_buffer)
11978                     + ((SvCUR(utf16_buffer) & ~1) - (reverse ? 1 : 2));
11979
11980                 if (*last_hi < 0xd8 || *last_hi > 0xdb) {
11981                     break;
11982                 }
11983
11984                 /* We have the first half of a surrogate. Read more.  */
11985                 DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter partial surrogate detected at %p\n", last_hi));
11986             }
11987
11988             status = FILTER_READ(idx + 1, utf16_buffer,
11989                                  160 + (SvCUR(utf16_buffer) & 1));
11990             DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter status=%" IVdf " SvCUR(sv)=%" UVuf "\n", status, (UV)SvCUR(utf16_buffer)));
11991             DEBUG_P({ sv_dump(utf16_buffer); sv_dump(utf8_buffer);});
11992             if (status < 0) {
11993                 /* Error */
11994                 IoPAGE(filter) = status;
11995                 return status;
11996             }
11997         }
11998
11999         /* 'chars' isn't quite the right name, as code points above 0xFFFF
12000          * require 4 bytes per char */
12001         chars = SvCUR(utf16_buffer) >> 1;
12002         have = SvCUR(utf8_buffer);
12003
12004         /* Assume the worst case size as noted by the functions: twice the
12005          * number of input bytes */
12006         SvGROW(utf8_buffer, have + chars * 4 + 1);
12007
12008         if (reverse) {
12009             end = utf16_to_utf8_reversed((U8*)SvPVX(utf16_buffer),
12010                                          (U8*)SvPVX_const(utf8_buffer) + have,
12011                                          chars * 2, &newlen);
12012         } else {
12013             end = utf16_to_utf8((U8*)SvPVX(utf16_buffer),
12014                                 (U8*)SvPVX_const(utf8_buffer) + have,
12015                                 chars * 2, &newlen);
12016         }
12017         SvCUR_set(utf8_buffer, have + newlen);
12018         *end = '\0';
12019
12020         /* No need to keep this SV "well-formed" with a '\0' after the end, as
12021            it's private to us, and utf16_to_utf8{,reversed} take a
12022            (pointer,length) pair, rather than a NUL-terminated string.  */
12023         if(SvCUR(utf16_buffer) & 1) {
12024             *SvPVX(utf16_buffer) = SvEND(utf16_buffer)[-1];
12025             SvCUR_set(utf16_buffer, 1);
12026         } else {
12027             SvCUR_set(utf16_buffer, 0);
12028         }
12029     }
12030     DEBUG_P(PerlIO_printf(Perl_debug_log,
12031                           "utf16_textfilter: returns, status=%" IVdf " utf16=%" UVuf " utf8=%" UVuf "\n",
12032                           status,
12033                           (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
12034     DEBUG_P({ sv_dump(utf8_buffer); sv_dump(sv);});
12035     return retval;
12036 }
12037
12038 static U8 *
12039 S_add_utf16_textfilter(pTHX_ U8 *const s, bool reversed)
12040 {
12041     SV *filter = filter_add(S_utf16_textfilter, NULL);
12042
12043     PERL_ARGS_ASSERT_ADD_UTF16_TEXTFILTER;
12044
12045     IoTOP_GV(filter) = MUTABLE_GV(newSVpvn((char *)s, PL_bufend - (char*)s));
12046     SvPVCLEAR(filter);
12047     IoLINES(filter) = reversed;
12048     IoPAGE(filter) = 1; /* Not EOF */
12049
12050     /* Sadly, we have to return a valid pointer, come what may, so we have to
12051        ignore any error return from this.  */
12052     SvCUR_set(PL_linestr, 0);
12053     if (FILTER_READ(0, PL_linestr, 0)) {
12054         SvUTF8_on(PL_linestr);
12055     } else {
12056         SvUTF8_on(PL_linestr);
12057     }
12058     PL_bufend = SvEND(PL_linestr);
12059     return (U8*)SvPVX(PL_linestr);
12060 }
12061 #endif
12062
12063 /*
12064 Returns a pointer to the next character after the parsed
12065 vstring, as well as updating the passed in sv.
12066
12067 Function must be called like
12068
12069         sv = sv_2mortal(newSV(5));
12070         s = scan_vstring(s,e,sv);
12071
12072 where s and e are the start and end of the string.
12073 The sv should already be large enough to store the vstring
12074 passed in, for performance reasons.
12075
12076 This function may croak if fatal warnings are enabled in the
12077 calling scope, hence the sv_2mortal in the example (to prevent
12078 a leak).  Make sure to do SvREFCNT_inc afterwards if you use
12079 sv_2mortal.
12080
12081 */
12082
12083 char *
12084 Perl_scan_vstring(pTHX_ const char *s, const char *const e, SV *sv)
12085 {
12086     const char *pos = s;
12087     const char *start = s;
12088
12089     PERL_ARGS_ASSERT_SCAN_VSTRING;
12090
12091     if (*pos == 'v') pos++;  /* get past 'v' */
12092     while (pos < e && (isDIGIT(*pos) || *pos == '_'))
12093         pos++;
12094     if ( *pos != '.') {
12095         /* this may not be a v-string if followed by => */
12096         const char *next = pos;
12097         while (next < e && isSPACE(*next))
12098             ++next;
12099         if ((e - next) >= 2 && *next == '=' && next[1] == '>' ) {
12100             /* return string not v-string */
12101             sv_setpvn(sv,(char *)s,pos-s);
12102             return (char *)pos;
12103         }
12104     }
12105
12106     if (!isALPHA(*pos)) {
12107         U8 tmpbuf[UTF8_MAXBYTES+1];
12108
12109         if (*s == 'v')
12110             s++;  /* get past 'v' */
12111
12112         SvPVCLEAR(sv);
12113
12114         for (;;) {
12115             /* this is atoi() that tolerates underscores */
12116             U8 *tmpend;
12117             UV rev = 0;
12118             const char *end = pos;
12119             UV mult = 1;
12120             while (--end >= s) {
12121                 if (*end != '_') {
12122                     const UV orev = rev;
12123                     rev += (*end - '0') * mult;
12124                     mult *= 10;
12125                     if (orev > rev)
12126                         /* diag_listed_as: Integer overflow in %s number */
12127                         Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
12128                                          "Integer overflow in decimal number");
12129                 }
12130             }
12131
12132             /* Append native character for the rev point */
12133             tmpend = uvchr_to_utf8(tmpbuf, rev);
12134             sv_catpvn(sv, (const char*)tmpbuf, tmpend - tmpbuf);
12135             if (!UVCHR_IS_INVARIANT(rev))
12136                  SvUTF8_on(sv);
12137             if (pos + 1 < e && *pos == '.' && isDIGIT(pos[1]))
12138                  s = ++pos;
12139             else {
12140                  s = pos;
12141                  break;
12142             }
12143             while (pos < e && (isDIGIT(*pos) || *pos == '_'))
12144                  pos++;
12145         }
12146         SvPOK_on(sv);
12147         sv_magic(sv,NULL,PERL_MAGIC_vstring,(const char*)start, pos-start);
12148         SvRMAGICAL_on(sv);
12149     }
12150     return (char *)s;
12151 }
12152
12153 int
12154 Perl_keyword_plugin_standard(pTHX_
12155         char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
12156 {
12157     PERL_ARGS_ASSERT_KEYWORD_PLUGIN_STANDARD;
12158     PERL_UNUSED_CONTEXT;
12159     PERL_UNUSED_ARG(keyword_ptr);
12160     PERL_UNUSED_ARG(keyword_len);
12161     PERL_UNUSED_ARG(op_ptr);
12162     return KEYWORD_PLUGIN_DECLINE;
12163 }
12164
12165 /*
12166 =for apidoc Amx|void|wrap_keyword_plugin|Perl_keyword_plugin_t new_plugin|Perl_keyword_plugin_t *old_plugin_p
12167
12168 Puts a C function into the chain of keyword plugins.  This is the
12169 preferred way to manipulate the L</PL_keyword_plugin> variable.
12170 C<new_plugin> is a pointer to the C function that is to be added to the
12171 keyword plugin chain, and C<old_plugin_p> points to the storage location
12172 where a pointer to the next function in the chain will be stored.  The
12173 value of C<new_plugin> is written into the L</PL_keyword_plugin> variable,
12174 while the value previously stored there is written to C<*old_plugin_p>.
12175
12176 L</PL_keyword_plugin> is global to an entire process, and a module wishing
12177 to hook keyword parsing may find itself invoked more than once per
12178 process, typically in different threads.  To handle that situation, this
12179 function is idempotent.  The location C<*old_plugin_p> must initially
12180 (once per process) contain a null pointer.  A C variable of static
12181 duration (declared at file scope, typically also marked C<static> to give
12182 it internal linkage) will be implicitly initialised appropriately, if it
12183 does not have an explicit initialiser.  This function will only actually
12184 modify the plugin chain if it finds C<*old_plugin_p> to be null.  This
12185 function is also thread safe on the small scale.  It uses appropriate
12186 locking to avoid race conditions in accessing L</PL_keyword_plugin>.
12187
12188 When this function is called, the function referenced by C<new_plugin>
12189 must be ready to be called, except for C<*old_plugin_p> being unfilled.
12190 In a threading situation, C<new_plugin> may be called immediately, even
12191 before this function has returned.  C<*old_plugin_p> will always be
12192 appropriately set before C<new_plugin> is called.  If C<new_plugin>
12193 decides not to do anything special with the identifier that it is given
12194 (which is the usual case for most calls to a keyword plugin), it must
12195 chain the plugin function referenced by C<*old_plugin_p>.
12196
12197 Taken all together, XS code to install a keyword plugin should typically
12198 look something like this:
12199
12200     static Perl_keyword_plugin_t next_keyword_plugin;
12201     static OP *my_keyword_plugin(pTHX_
12202         char *keyword_plugin, STRLEN keyword_len, OP **op_ptr)
12203     {
12204         if (memEQs(keyword_ptr, keyword_len,
12205                    "my_new_keyword")) {
12206             ...
12207         } else {
12208             return next_keyword_plugin(aTHX_
12209                 keyword_ptr, keyword_len, op_ptr);
12210         }
12211     }
12212     BOOT:
12213         wrap_keyword_plugin(my_keyword_plugin,
12214                             &next_keyword_plugin);
12215
12216 Direct access to L</PL_keyword_plugin> should be avoided.
12217
12218 =cut
12219 */
12220
12221 void
12222 Perl_wrap_keyword_plugin(pTHX_
12223     Perl_keyword_plugin_t new_plugin, Perl_keyword_plugin_t *old_plugin_p)
12224 {
12225     dVAR;
12226
12227     PERL_UNUSED_CONTEXT;
12228     PERL_ARGS_ASSERT_WRAP_KEYWORD_PLUGIN;
12229     if (*old_plugin_p) return;
12230     KEYWORD_PLUGIN_MUTEX_LOCK;
12231     if (!*old_plugin_p) {
12232         *old_plugin_p = PL_keyword_plugin;
12233         PL_keyword_plugin = new_plugin;
12234     }
12235     KEYWORD_PLUGIN_MUTEX_UNLOCK;
12236 }
12237
12238 #define parse_recdescent(g,p) S_parse_recdescent(aTHX_ g,p)
12239 static void
12240 S_parse_recdescent(pTHX_ int gramtype, I32 fakeeof)
12241 {
12242     SAVEI32(PL_lex_brackets);
12243     if (PL_lex_brackets > 100)
12244         Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
12245     PL_lex_brackstack[PL_lex_brackets++] = XFAKEEOF;
12246     SAVEI32(PL_lex_allbrackets);
12247     PL_lex_allbrackets = 0;
12248     SAVEI8(PL_lex_fakeeof);
12249     PL_lex_fakeeof = (U8)fakeeof;
12250     if(yyparse(gramtype) && !PL_parser->error_count)
12251         qerror(Perl_mess(aTHX_ "Parse error"));
12252 }
12253
12254 #define parse_recdescent_for_op(g,p) S_parse_recdescent_for_op(aTHX_ g,p)
12255 static OP *
12256 S_parse_recdescent_for_op(pTHX_ int gramtype, I32 fakeeof)
12257 {
12258     OP *o;
12259     ENTER;
12260     SAVEVPTR(PL_eval_root);
12261     PL_eval_root = NULL;
12262     parse_recdescent(gramtype, fakeeof);
12263     o = PL_eval_root;
12264     LEAVE;
12265     return o;
12266 }
12267
12268 #define parse_expr(p,f) S_parse_expr(aTHX_ p,f)
12269 static OP *
12270 S_parse_expr(pTHX_ I32 fakeeof, U32 flags)
12271 {
12272     OP *exprop;
12273     if (flags & ~PARSE_OPTIONAL)
12274         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_expr");
12275     exprop = parse_recdescent_for_op(GRAMEXPR, fakeeof);
12276     if (!exprop && !(flags & PARSE_OPTIONAL)) {
12277         if (!PL_parser->error_count)
12278             qerror(Perl_mess(aTHX_ "Parse error"));
12279         exprop = newOP(OP_NULL, 0);
12280     }
12281     return exprop;
12282 }
12283
12284 /*
12285 =for apidoc Amx|OP *|parse_arithexpr|U32 flags
12286
12287 Parse a Perl arithmetic expression.  This may contain operators of precedence
12288 down to the bit shift operators.  The expression must be followed (and thus
12289 terminated) either by a comparison or lower-precedence operator or by
12290 something that would normally terminate an expression such as semicolon.
12291 If C<flags> has the C<PARSE_OPTIONAL> bit set, then the expression is optional,
12292 otherwise it is mandatory.  It is up to the caller to ensure that the
12293 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12294 the source of the code to be parsed and the lexical context for the
12295 expression.
12296
12297 The op tree representing the expression is returned.  If an optional
12298 expression is absent, a null pointer is returned, otherwise the pointer
12299 will be non-null.
12300
12301 If an error occurs in parsing or compilation, in most cases a valid op
12302 tree is returned anyway.  The error is reflected in the parser state,
12303 normally resulting in a single exception at the top level of parsing
12304 which covers all the compilation errors that occurred.  Some compilation
12305 errors, however, will throw an exception immediately.
12306
12307 =cut
12308 */
12309
12310 OP *
12311 Perl_parse_arithexpr(pTHX_ U32 flags)
12312 {
12313     return parse_expr(LEX_FAKEEOF_COMPARE, flags);
12314 }
12315
12316 /*
12317 =for apidoc Amx|OP *|parse_termexpr|U32 flags
12318
12319 Parse a Perl term expression.  This may contain operators of precedence
12320 down to the assignment operators.  The expression must be followed (and thus
12321 terminated) either by a comma or lower-precedence operator or by
12322 something that would normally terminate an expression such as semicolon.
12323 If C<flags> has the C<PARSE_OPTIONAL> bit set, then the expression is optional,
12324 otherwise it is mandatory.  It is up to the caller to ensure that the
12325 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12326 the source of the code to be parsed and the lexical context for the
12327 expression.
12328
12329 The op tree representing the expression is returned.  If an optional
12330 expression is absent, a null pointer is returned, otherwise the pointer
12331 will be non-null.
12332
12333 If an error occurs in parsing or compilation, in most cases a valid op
12334 tree is returned anyway.  The error is reflected in the parser state,
12335 normally resulting in a single exception at the top level of parsing
12336 which covers all the compilation errors that occurred.  Some compilation
12337 errors, however, will throw an exception immediately.
12338
12339 =cut
12340 */
12341
12342 OP *
12343 Perl_parse_termexpr(pTHX_ U32 flags)
12344 {
12345     return parse_expr(LEX_FAKEEOF_COMMA, flags);
12346 }
12347
12348 /*
12349 =for apidoc Amx|OP *|parse_listexpr|U32 flags
12350
12351 Parse a Perl list expression.  This may contain operators of precedence
12352 down to the comma operator.  The expression must be followed (and thus
12353 terminated) either by a low-precedence logic operator such as C<or> or by
12354 something that would normally terminate an expression such as semicolon.
12355 If C<flags> has the C<PARSE_OPTIONAL> bit set, then the expression is optional,
12356 otherwise it is mandatory.  It is up to the caller to ensure that the
12357 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12358 the source of the code to be parsed and the lexical context for the
12359 expression.
12360
12361 The op tree representing the expression is returned.  If an optional
12362 expression is absent, a null pointer is returned, otherwise the pointer
12363 will be non-null.
12364
12365 If an error occurs in parsing or compilation, in most cases a valid op
12366 tree is returned anyway.  The error is reflected in the parser state,
12367 normally resulting in a single exception at the top level of parsing
12368 which covers all the compilation errors that occurred.  Some compilation
12369 errors, however, will throw an exception immediately.
12370
12371 =cut
12372 */
12373
12374 OP *
12375 Perl_parse_listexpr(pTHX_ U32 flags)
12376 {
12377     return parse_expr(LEX_FAKEEOF_LOWLOGIC, flags);
12378 }
12379
12380 /*
12381 =for apidoc Amx|OP *|parse_fullexpr|U32 flags
12382
12383 Parse a single complete Perl expression.  This allows the full
12384 expression grammar, including the lowest-precedence operators such
12385 as C<or>.  The expression must be followed (and thus terminated) by a
12386 token that an expression would normally be terminated by: end-of-file,
12387 closing bracketing punctuation, semicolon, or one of the keywords that
12388 signals a postfix expression-statement modifier.  If C<flags> has the
12389 C<PARSE_OPTIONAL> bit set, then the expression is optional, otherwise it is
12390 mandatory.  It is up to the caller to ensure that the dynamic parser
12391 state (L</PL_parser> et al) is correctly set to reflect the source of
12392 the code to be parsed and the lexical context for the expression.
12393
12394 The op tree representing the expression is returned.  If an optional
12395 expression is absent, a null pointer is returned, otherwise the pointer
12396 will be non-null.
12397
12398 If an error occurs in parsing or compilation, in most cases a valid op
12399 tree is returned anyway.  The error is reflected in the parser state,
12400 normally resulting in a single exception at the top level of parsing
12401 which covers all the compilation errors that occurred.  Some compilation
12402 errors, however, will throw an exception immediately.
12403
12404 =cut
12405 */
12406
12407 OP *
12408 Perl_parse_fullexpr(pTHX_ U32 flags)
12409 {
12410     return parse_expr(LEX_FAKEEOF_NONEXPR, flags);
12411 }
12412
12413 /*
12414 =for apidoc Amx|OP *|parse_block|U32 flags
12415
12416 Parse a single complete Perl code block.  This consists of an opening
12417 brace, a sequence of statements, and a closing brace.  The block
12418 constitutes a lexical scope, so C<my> variables and various compile-time
12419 effects can be contained within it.  It is up to the caller to ensure
12420 that the dynamic parser state (L</PL_parser> et al) is correctly set to
12421 reflect the source of the code to be parsed and the lexical context for
12422 the statement.
12423
12424 The op tree representing the code block is returned.  This is always a
12425 real op, never a null pointer.  It will normally be a C<lineseq> list,
12426 including C<nextstate> or equivalent ops.  No ops to construct any kind
12427 of runtime scope are included by virtue of it being a block.
12428
12429 If an error occurs in parsing or compilation, in most cases a valid op
12430 tree (most likely null) is returned anyway.  The error is reflected in
12431 the parser state, normally resulting in a single exception at the top
12432 level of parsing which covers all the compilation errors that occurred.
12433 Some compilation errors, however, will throw an exception immediately.
12434
12435 The C<flags> parameter is reserved for future use, and must always
12436 be zero.
12437
12438 =cut
12439 */
12440
12441 OP *
12442 Perl_parse_block(pTHX_ U32 flags)
12443 {
12444     if (flags)
12445         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_block");
12446     return parse_recdescent_for_op(GRAMBLOCK, LEX_FAKEEOF_NEVER);
12447 }
12448
12449 /*
12450 =for apidoc Amx|OP *|parse_barestmt|U32 flags
12451
12452 Parse a single unadorned Perl statement.  This may be a normal imperative
12453 statement or a declaration that has compile-time effect.  It does not
12454 include any label or other affixture.  It is up to the caller to ensure
12455 that the dynamic parser state (L</PL_parser> et al) is correctly set to
12456 reflect the source of the code to be parsed and the lexical context for
12457 the statement.
12458
12459 The op tree representing the statement is returned.  This may be a
12460 null pointer if the statement is null, for example if it was actually
12461 a subroutine definition (which has compile-time side effects).  If not
12462 null, it will be ops directly implementing the statement, suitable to
12463 pass to L</newSTATEOP>.  It will not normally include a C<nextstate> or
12464 equivalent op (except for those embedded in a scope contained entirely
12465 within the statement).
12466
12467 If an error occurs in parsing or compilation, in most cases a valid op
12468 tree (most likely null) is returned anyway.  The error is reflected in
12469 the parser state, normally resulting in a single exception at the top
12470 level of parsing which covers all the compilation errors that occurred.
12471 Some compilation errors, however, will throw an exception immediately.
12472
12473 The C<flags> parameter is reserved for future use, and must always
12474 be zero.
12475
12476 =cut
12477 */
12478
12479 OP *
12480 Perl_parse_barestmt(pTHX_ U32 flags)
12481 {
12482     if (flags)
12483         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_barestmt");
12484     return parse_recdescent_for_op(GRAMBARESTMT, LEX_FAKEEOF_NEVER);
12485 }
12486
12487 /*
12488 =for apidoc Amx|SV *|parse_label|U32 flags
12489
12490 Parse a single label, possibly optional, of the type that may prefix a
12491 Perl statement.  It is up to the caller to ensure that the dynamic parser
12492 state (L</PL_parser> et al) is correctly set to reflect the source of
12493 the code to be parsed.  If C<flags> has the C<PARSE_OPTIONAL> bit set, then the
12494 label is optional, otherwise it is mandatory.
12495
12496 The name of the label is returned in the form of a fresh scalar.  If an
12497 optional label is absent, a null pointer is returned.
12498
12499 If an error occurs in parsing, which can only occur if the label is
12500 mandatory, a valid label is returned anyway.  The error is reflected in
12501 the parser state, normally resulting in a single exception at the top
12502 level of parsing which covers all the compilation errors that occurred.
12503
12504 =cut
12505 */
12506
12507 SV *
12508 Perl_parse_label(pTHX_ U32 flags)
12509 {
12510     if (flags & ~PARSE_OPTIONAL)
12511         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_label");
12512     if (PL_nexttoke) {
12513         PL_parser->yychar = yylex();
12514         if (PL_parser->yychar == LABEL) {
12515             char * const lpv = pl_yylval.pval;
12516             STRLEN llen = strlen(lpv);
12517             PL_parser->yychar = YYEMPTY;
12518             return newSVpvn_flags(lpv, llen, lpv[llen+1] ? SVf_UTF8 : 0);
12519         } else {
12520             yyunlex();
12521             goto no_label;
12522         }
12523     } else {
12524         char *s, *t;
12525         STRLEN wlen, bufptr_pos;
12526         lex_read_space(0);
12527         t = s = PL_bufptr;
12528         if (!isIDFIRST_lazy_if_safe(s, PL_bufend, UTF))
12529             goto no_label;
12530         t = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &wlen);
12531         if (word_takes_any_delimiter(s, wlen))
12532             goto no_label;
12533         bufptr_pos = s - SvPVX(PL_linestr);
12534         PL_bufptr = t;
12535         lex_read_space(LEX_KEEP_PREVIOUS);
12536         t = PL_bufptr;
12537         s = SvPVX(PL_linestr) + bufptr_pos;
12538         if (t[0] == ':' && t[1] != ':') {
12539             PL_oldoldbufptr = PL_oldbufptr;
12540             PL_oldbufptr = s;
12541             PL_bufptr = t+1;
12542             return newSVpvn_flags(s, wlen, UTF ? SVf_UTF8 : 0);
12543         } else {
12544             PL_bufptr = s;
12545             no_label:
12546             if (flags & PARSE_OPTIONAL) {
12547                 return NULL;
12548             } else {
12549                 qerror(Perl_mess(aTHX_ "Parse error"));
12550                 return newSVpvs("x");
12551             }
12552         }
12553     }
12554 }
12555
12556 /*
12557 =for apidoc Amx|OP *|parse_fullstmt|U32 flags
12558
12559 Parse a single complete Perl statement.  This may be a normal imperative
12560 statement or a declaration that has compile-time effect, and may include
12561 optional labels.  It is up to the caller to ensure that the dynamic
12562 parser state (L</PL_parser> et al) is correctly set to reflect the source
12563 of the code to be parsed and the lexical context for the statement.
12564
12565 The op tree representing the statement is returned.  This may be a
12566 null pointer if the statement is null, for example if it was actually
12567 a subroutine definition (which has compile-time side effects).  If not
12568 null, it will be the result of a L</newSTATEOP> call, normally including
12569 a C<nextstate> or equivalent op.
12570
12571 If an error occurs in parsing or compilation, in most cases a valid op
12572 tree (most likely null) is returned anyway.  The error is reflected in
12573 the parser state, normally resulting in a single exception at the top
12574 level of parsing which covers all the compilation errors that occurred.
12575 Some compilation errors, however, will throw an exception immediately.
12576
12577 The C<flags> parameter is reserved for future use, and must always
12578 be zero.
12579
12580 =cut
12581 */
12582
12583 OP *
12584 Perl_parse_fullstmt(pTHX_ U32 flags)
12585 {
12586     if (flags)
12587         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_fullstmt");
12588     return parse_recdescent_for_op(GRAMFULLSTMT, LEX_FAKEEOF_NEVER);
12589 }
12590
12591 /*
12592 =for apidoc Amx|OP *|parse_stmtseq|U32 flags
12593
12594 Parse a sequence of zero or more Perl statements.  These may be normal
12595 imperative statements, including optional labels, or declarations
12596 that have compile-time effect, or any mixture thereof.  The statement
12597 sequence ends when a closing brace or end-of-file is encountered in a
12598 place where a new statement could have validly started.  It is up to
12599 the caller to ensure that the dynamic parser state (L</PL_parser> et al)
12600 is correctly set to reflect the source of the code to be parsed and the
12601 lexical context for the statements.
12602
12603 The op tree representing the statement sequence is returned.  This may
12604 be a null pointer if the statements were all null, for example if there
12605 were no statements or if there were only subroutine definitions (which
12606 have compile-time side effects).  If not null, it will be a C<lineseq>
12607 list, normally including C<nextstate> or equivalent ops.
12608
12609 If an error occurs in parsing or compilation, in most cases a valid op
12610 tree is returned anyway.  The error is reflected in the parser state,
12611 normally resulting in a single exception at the top level of parsing
12612 which covers all the compilation errors that occurred.  Some compilation
12613 errors, however, will throw an exception immediately.
12614
12615 The C<flags> parameter is reserved for future use, and must always
12616 be zero.
12617
12618 =cut
12619 */
12620
12621 OP *
12622 Perl_parse_stmtseq(pTHX_ U32 flags)
12623 {
12624     OP *stmtseqop;
12625     I32 c;
12626     if (flags)
12627         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_stmtseq");
12628     stmtseqop = parse_recdescent_for_op(GRAMSTMTSEQ, LEX_FAKEEOF_CLOSING);
12629     c = lex_peek_unichar(0);
12630     if (c != -1 && c != /*{*/'}')
12631         qerror(Perl_mess(aTHX_ "Parse error"));
12632     return stmtseqop;
12633 }
12634
12635 /*
12636  * ex: set ts=8 sts=4 sw=4 et:
12637  */