This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
study_chunk: avoid transforming ops while enframed
[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 AmnU|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 "invlist_inline.h"
42
43 #define new_constant(a,b,c,d,e,f,g, h)  \
44         S_new_constant(aTHX_ a,b,STR_WITH_LEN(c),d,e,f, g, h)
45
46 #define pl_yylval       (PL_parser->yylval)
47
48 /* XXX temporary backwards compatibility */
49 #define PL_lex_brackets         (PL_parser->lex_brackets)
50 #define PL_lex_allbrackets      (PL_parser->lex_allbrackets)
51 #define PL_lex_fakeeof          (PL_parser->lex_fakeeof)
52 #define PL_lex_brackstack       (PL_parser->lex_brackstack)
53 #define PL_lex_casemods         (PL_parser->lex_casemods)
54 #define PL_lex_casestack        (PL_parser->lex_casestack)
55 #define PL_lex_dojoin           (PL_parser->lex_dojoin)
56 #define PL_lex_formbrack        (PL_parser->lex_formbrack)
57 #define PL_lex_inpat            (PL_parser->lex_inpat)
58 #define PL_lex_inwhat           (PL_parser->lex_inwhat)
59 #define PL_lex_op               (PL_parser->lex_op)
60 #define PL_lex_repl             (PL_parser->lex_repl)
61 #define PL_lex_starts           (PL_parser->lex_starts)
62 #define PL_lex_stuff            (PL_parser->lex_stuff)
63 #define PL_multi_start          (PL_parser->multi_start)
64 #define PL_multi_open           (PL_parser->multi_open)
65 #define PL_multi_close          (PL_parser->multi_close)
66 #define PL_preambled            (PL_parser->preambled)
67 #define PL_linestr              (PL_parser->linestr)
68 #define PL_expect               (PL_parser->expect)
69 #define PL_copline              (PL_parser->copline)
70 #define PL_bufptr               (PL_parser->bufptr)
71 #define PL_oldbufptr            (PL_parser->oldbufptr)
72 #define PL_oldoldbufptr         (PL_parser->oldoldbufptr)
73 #define PL_linestart            (PL_parser->linestart)
74 #define PL_bufend               (PL_parser->bufend)
75 #define PL_last_uni             (PL_parser->last_uni)
76 #define PL_last_lop             (PL_parser->last_lop)
77 #define PL_last_lop_op          (PL_parser->last_lop_op)
78 #define PL_lex_state            (PL_parser->lex_state)
79 #define PL_rsfp                 (PL_parser->rsfp)
80 #define PL_rsfp_filters         (PL_parser->rsfp_filters)
81 #define PL_in_my                (PL_parser->in_my)
82 #define PL_in_my_stash          (PL_parser->in_my_stash)
83 #define PL_tokenbuf             (PL_parser->tokenbuf)
84 #define PL_multi_end            (PL_parser->multi_end)
85 #define PL_error_count          (PL_parser->error_count)
86
87 #  define PL_nexttoke           (PL_parser->nexttoke)
88 #  define PL_nexttype           (PL_parser->nexttype)
89 #  define PL_nextval            (PL_parser->nextval)
90
91
92 #define SvEVALED(sv) \
93     (SvTYPE(sv) >= SVt_PVNV \
94     && ((XPVIV*)SvANY(sv))->xiv_u.xivu_eval_seen)
95
96 static const char* const ident_too_long = "Identifier too long";
97 static const char* const ident_var_zero_multi_digit = "Numeric variables with more than one digit may not start with '0'";
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) || memCHRs("[\\]^_?", (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  * ChEop        : chaining equality-testing operator
200  * NCEop        : non-chaining comparison operator at equality precedence
201  * ChRop        : chaining relational operator <= != gt
202  * NCRop        : non-chaining relational operator isa
203  *
204  * Also see LOP and lop() below.
205  */
206
207 #ifdef DEBUGGING /* Serve -DT. */
208 #   define REPORT(retval) tokereport((I32)retval, &pl_yylval)
209 #else
210 #   define REPORT(retval) (retval)
211 #endif
212
213 #define TOKEN(retval) return ( PL_bufptr = s, REPORT(retval))
214 #define OPERATOR(retval) return (PL_expect = XTERM, PL_bufptr = s, REPORT(retval))
215 #define AOPERATOR(retval) return ao((PL_expect = XTERM, PL_bufptr = s, retval))
216 #define PREBLOCK(retval) return (PL_expect = XBLOCK,PL_bufptr = s, REPORT(retval))
217 #define PRETERMBLOCK(retval) return (PL_expect = XTERMBLOCK,PL_bufptr = s, REPORT(retval))
218 #define PREREF(retval) return (PL_expect = XREF,PL_bufptr = s, REPORT(retval))
219 #define TERM(retval) return (CLINE, PL_expect = XOPERATOR, PL_bufptr = s, REPORT(retval))
220 #define POSTDEREF(f) return (PL_bufptr = s, S_postderef(aTHX_ REPORT(f),s[1]))
221 #define LOOPX(f) return (PL_bufptr = force_word(s,BAREWORD,TRUE,FALSE), \
222                          pl_yylval.ival=f, \
223                          PL_expect = PL_nexttoke ? XOPERATOR : XTERM, \
224                          REPORT((int)LOOPEX))
225 #define FTST(f)  return (pl_yylval.ival=f, PL_expect=XTERMORDORDOR, PL_bufptr=s, REPORT((int)UNIOP))
226 #define FUN0(f)  return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0))
227 #define FUN0OP(f)  return (pl_yylval.opval=f, CLINE, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0OP))
228 #define FUN1(f)  return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC1))
229 #define BOop(f)  return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)BITOROP))
230 #define BAop(f)  return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)BITANDOP))
231 #define BCop(f) return pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr = s, \
232                        REPORT('~')
233 #define SHop(f)  return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)SHIFTOP))
234 #define PWop(f)  return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)POWOP))
235 #define PMop(f)  return(pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MATCHOP))
236 #define Aop(f)   return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)ADDOP))
237 #define AopNOASSIGN(f) return (pl_yylval.ival=f, PL_bufptr=s, REPORT((int)ADDOP))
238 #define Mop(f)   return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)MULOP))
239 #define ChEop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)CHEQOP))
240 #define NCEop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)NCEQOP))
241 #define ChRop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)CHRELOP))
242 #define NCRop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)NCRELOP))
243
244 /* This bit of chicanery makes a unary function followed by
245  * a parenthesis into a function with one argument, highest precedence.
246  * The UNIDOR macro is for unary functions that can be followed by the //
247  * operator (such as C<shift // 0>).
248  */
249 #define UNI3(f,x,have_x) { \
250         pl_yylval.ival = f; \
251         if (have_x) PL_expect = x; \
252         PL_bufptr = s; \
253         PL_last_uni = PL_oldbufptr; \
254         PL_last_lop_op = (f) < 0 ? -(f) : (f); \
255         if (*s == '(') \
256             return REPORT( (int)FUNC1 ); \
257         s = skipspace(s); \
258         return REPORT( *s=='(' ? (int)FUNC1 : (int)UNIOP ); \
259         }
260 #define UNI(f)    UNI3(f,XTERM,1)
261 #define UNIDOR(f) UNI3(f,XTERMORDORDOR,1)
262 #define UNIPROTO(f,optional) { \
263         if (optional) PL_last_uni = PL_oldbufptr; \
264         OPERATOR(f); \
265         }
266
267 #define UNIBRACK(f) UNI3(f,0,0)
268
269 /* grandfather return to old style */
270 #define OLDLOP(f) \
271         do { \
272             if (!PL_lex_allbrackets && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC) \
273                 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC; \
274             pl_yylval.ival = (f); \
275             PL_expect = XTERM; \
276             PL_bufptr = s; \
277             return (int)LSTOP; \
278         } while(0)
279
280 #define COPLINE_INC_WITH_HERELINES                  \
281     STMT_START {                                     \
282         CopLINE_inc(PL_curcop);                       \
283         if (PL_parser->herelines)                      \
284             CopLINE(PL_curcop) += PL_parser->herelines, \
285             PL_parser->herelines = 0;                    \
286     } STMT_END
287 /* Called after scan_str to update CopLINE(PL_curcop), but only when there
288  * is no sublex_push to follow. */
289 #define COPLINE_SET_FROM_MULTI_END            \
290     STMT_START {                               \
291         CopLINE_set(PL_curcop, PL_multi_end);   \
292         if (PL_multi_end != PL_multi_start)      \
293             PL_parser->herelines = 0;             \
294     } STMT_END
295
296
297 /* A file-local structure for passing around information about subroutines and
298  * related definable words */
299 struct code {
300     SV *sv;
301     CV *cv;
302     GV *gv, **gvp;
303     OP *rv2cv_op;
304     PADOFFSET off;
305     bool lex;
306 };
307
308 static const struct code no_code = { NULL, NULL, NULL, NULL, NULL, 0, FALSE };
309
310
311 #ifdef DEBUGGING
312
313 /* how to interpret the pl_yylval associated with the token */
314 enum token_type {
315     TOKENTYPE_NONE,
316     TOKENTYPE_IVAL,
317     TOKENTYPE_OPNUM, /* pl_yylval.ival contains an opcode number */
318     TOKENTYPE_PVAL,
319     TOKENTYPE_OPVAL
320 };
321
322 static struct debug_tokens {
323     const int token;
324     enum token_type type;
325     const char *name;
326 } const debug_tokens[] =
327 {
328     { ADDOP,            TOKENTYPE_OPNUM,        "ADDOP" },
329     { ANDAND,           TOKENTYPE_NONE,         "ANDAND" },
330     { ANDOP,            TOKENTYPE_NONE,         "ANDOP" },
331     { ANONSUB,          TOKENTYPE_IVAL,         "ANONSUB" },
332     { ANON_SIGSUB,      TOKENTYPE_IVAL,         "ANON_SIGSUB" },
333     { ARROW,            TOKENTYPE_NONE,         "ARROW" },
334     { ASSIGNOP,         TOKENTYPE_OPNUM,        "ASSIGNOP" },
335     { BITANDOP,         TOKENTYPE_OPNUM,        "BITANDOP" },
336     { BITOROP,          TOKENTYPE_OPNUM,        "BITOROP" },
337     { CHEQOP,           TOKENTYPE_OPNUM,        "CHEQOP" },
338     { CHRELOP,          TOKENTYPE_OPNUM,        "CHRELOP" },
339     { COLONATTR,        TOKENTYPE_NONE,         "COLONATTR" },
340     { CONTINUE,         TOKENTYPE_NONE,         "CONTINUE" },
341     { DEFAULT,          TOKENTYPE_NONE,         "DEFAULT" },
342     { DO,               TOKENTYPE_NONE,         "DO" },
343     { DOLSHARP,         TOKENTYPE_NONE,         "DOLSHARP" },
344     { DORDOR,           TOKENTYPE_NONE,         "DORDOR" },
345     { DOROP,            TOKENTYPE_OPNUM,        "DOROP" },
346     { DOTDOT,           TOKENTYPE_IVAL,         "DOTDOT" },
347     { ELSE,             TOKENTYPE_NONE,         "ELSE" },
348     { ELSIF,            TOKENTYPE_IVAL,         "ELSIF" },
349     { FOR,              TOKENTYPE_IVAL,         "FOR" },
350     { FORMAT,           TOKENTYPE_NONE,         "FORMAT" },
351     { FORMLBRACK,       TOKENTYPE_NONE,         "FORMLBRACK" },
352     { FORMRBRACK,       TOKENTYPE_NONE,         "FORMRBRACK" },
353     { FUNC,             TOKENTYPE_OPNUM,        "FUNC" },
354     { FUNC0,            TOKENTYPE_OPNUM,        "FUNC0" },
355     { FUNC0OP,          TOKENTYPE_OPVAL,        "FUNC0OP" },
356     { FUNC0SUB,         TOKENTYPE_OPVAL,        "FUNC0SUB" },
357     { FUNC1,            TOKENTYPE_OPNUM,        "FUNC1" },
358     { FUNCMETH,         TOKENTYPE_OPVAL,        "FUNCMETH" },
359     { GIVEN,            TOKENTYPE_IVAL,         "GIVEN" },
360     { HASHBRACK,        TOKENTYPE_NONE,         "HASHBRACK" },
361     { IF,               TOKENTYPE_IVAL,         "IF" },
362     { LABEL,            TOKENTYPE_OPVAL,        "LABEL" },
363     { LOCAL,            TOKENTYPE_IVAL,         "LOCAL" },
364     { LOOPEX,           TOKENTYPE_OPNUM,        "LOOPEX" },
365     { LSTOP,            TOKENTYPE_OPNUM,        "LSTOP" },
366     { LSTOPSUB,         TOKENTYPE_OPVAL,        "LSTOPSUB" },
367     { MATCHOP,          TOKENTYPE_OPNUM,        "MATCHOP" },
368     { METHOD,           TOKENTYPE_OPVAL,        "METHOD" },
369     { MULOP,            TOKENTYPE_OPNUM,        "MULOP" },
370     { MY,               TOKENTYPE_IVAL,         "MY" },
371     { NCEQOP,           TOKENTYPE_OPNUM,        "NCEQOP" },
372     { NCRELOP,          TOKENTYPE_OPNUM,        "NCRELOP" },
373     { NOAMP,            TOKENTYPE_NONE,         "NOAMP" },
374     { NOTOP,            TOKENTYPE_NONE,         "NOTOP" },
375     { OROP,             TOKENTYPE_IVAL,         "OROP" },
376     { OROR,             TOKENTYPE_NONE,         "OROR" },
377     { PACKAGE,          TOKENTYPE_NONE,         "PACKAGE" },
378     { PLUGEXPR,         TOKENTYPE_OPVAL,        "PLUGEXPR" },
379     { PLUGSTMT,         TOKENTYPE_OPVAL,        "PLUGSTMT" },
380     { PMFUNC,           TOKENTYPE_OPVAL,        "PMFUNC" },
381     { POSTJOIN,         TOKENTYPE_NONE,         "POSTJOIN" },
382     { POSTDEC,          TOKENTYPE_NONE,         "POSTDEC" },
383     { POSTINC,          TOKENTYPE_NONE,         "POSTINC" },
384     { POWOP,            TOKENTYPE_OPNUM,        "POWOP" },
385     { PREDEC,           TOKENTYPE_NONE,         "PREDEC" },
386     { PREINC,           TOKENTYPE_NONE,         "PREINC" },
387     { PRIVATEREF,       TOKENTYPE_OPVAL,        "PRIVATEREF" },
388     { QWLIST,           TOKENTYPE_OPVAL,        "QWLIST" },
389     { REFGEN,           TOKENTYPE_NONE,         "REFGEN" },
390     { REQUIRE,          TOKENTYPE_NONE,         "REQUIRE" },
391     { SHIFTOP,          TOKENTYPE_OPNUM,        "SHIFTOP" },
392     { SIGSUB,           TOKENTYPE_NONE,         "SIGSUB" },
393     { SUB,              TOKENTYPE_NONE,         "SUB" },
394     { SUBLEXEND,        TOKENTYPE_NONE,         "SUBLEXEND" },
395     { SUBLEXSTART,      TOKENTYPE_NONE,         "SUBLEXSTART" },
396     { THING,            TOKENTYPE_OPVAL,        "THING" },
397     { UMINUS,           TOKENTYPE_NONE,         "UMINUS" },
398     { UNIOP,            TOKENTYPE_OPNUM,        "UNIOP" },
399     { UNIOPSUB,         TOKENTYPE_OPVAL,        "UNIOPSUB" },
400     { UNLESS,           TOKENTYPE_IVAL,         "UNLESS" },
401     { UNTIL,            TOKENTYPE_IVAL,         "UNTIL" },
402     { USE,              TOKENTYPE_IVAL,         "USE" },
403     { WHEN,             TOKENTYPE_IVAL,         "WHEN" },
404     { WHILE,            TOKENTYPE_IVAL,         "WHILE" },
405     { BAREWORD,         TOKENTYPE_OPVAL,        "BAREWORD" },
406     { YADAYADA,         TOKENTYPE_IVAL,         "YADAYADA" },
407     { 0,                TOKENTYPE_NONE,         NULL }
408 };
409
410 /* dump the returned token in rv, plus any optional arg in pl_yylval */
411
412 STATIC int
413 S_tokereport(pTHX_ I32 rv, const YYSTYPE* lvalp)
414 {
415     PERL_ARGS_ASSERT_TOKEREPORT;
416
417     if (DEBUG_T_TEST) {
418         const char *name = NULL;
419         enum token_type type = TOKENTYPE_NONE;
420         const struct debug_tokens *p;
421         SV* const report = newSVpvs("<== ");
422
423         for (p = debug_tokens; p->token; p++) {
424             if (p->token == (int)rv) {
425                 name = p->name;
426                 type = p->type;
427                 break;
428             }
429         }
430         if (name)
431             Perl_sv_catpv(aTHX_ report, name);
432         else if (isGRAPH(rv))
433         {
434             Perl_sv_catpvf(aTHX_ report, "'%c'", (char)rv);
435             if ((char)rv == 'p')
436                 sv_catpvs(report, " (pending identifier)");
437         }
438         else if (!rv)
439             sv_catpvs(report, "EOF");
440         else
441             Perl_sv_catpvf(aTHX_ report, "?? %" IVdf, (IV)rv);
442         switch (type) {
443         case TOKENTYPE_NONE:
444             break;
445         case TOKENTYPE_IVAL:
446             Perl_sv_catpvf(aTHX_ report, "(ival=%" IVdf ")", (IV)lvalp->ival);
447             break;
448         case TOKENTYPE_OPNUM:
449             Perl_sv_catpvf(aTHX_ report, "(ival=op_%s)",
450                                     PL_op_name[lvalp->ival]);
451             break;
452         case TOKENTYPE_PVAL:
453             Perl_sv_catpvf(aTHX_ report, "(pval=\"%s\")", lvalp->pval);
454             break;
455         case TOKENTYPE_OPVAL:
456             if (lvalp->opval) {
457                 Perl_sv_catpvf(aTHX_ report, "(opval=op_%s)",
458                                     PL_op_name[lvalp->opval->op_type]);
459                 if (lvalp->opval->op_type == OP_CONST) {
460                     Perl_sv_catpvf(aTHX_ report, " %s",
461                         SvPEEK(cSVOPx_sv(lvalp->opval)));
462                 }
463
464             }
465             else
466                 sv_catpvs(report, "(opval=null)");
467             break;
468         }
469         PerlIO_printf(Perl_debug_log, "### %s\n\n", SvPV_nolen_const(report));
470     };
471     return (int)rv;
472 }
473
474
475 /* print the buffer with suitable escapes */
476
477 STATIC void
478 S_printbuf(pTHX_ const char *const fmt, const char *const s)
479 {
480     SV* const tmp = newSVpvs("");
481
482     PERL_ARGS_ASSERT_PRINTBUF;
483
484     GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral); /* fmt checked by caller */
485     PerlIO_printf(Perl_debug_log, fmt, pv_display(tmp, s, strlen(s), 0, 60));
486     GCC_DIAG_RESTORE_STMT;
487     SvREFCNT_dec(tmp);
488 }
489
490 #endif
491
492 /*
493  * S_ao
494  *
495  * This subroutine looks for an '=' next to the operator that has just been
496  * parsed and turns it into an ASSIGNOP if it finds one.
497  */
498
499 STATIC int
500 S_ao(pTHX_ int toketype)
501 {
502     if (*PL_bufptr == '=') {
503         PL_bufptr++;
504         if (toketype == ANDAND)
505             pl_yylval.ival = OP_ANDASSIGN;
506         else if (toketype == OROR)
507             pl_yylval.ival = OP_ORASSIGN;
508         else if (toketype == DORDOR)
509             pl_yylval.ival = OP_DORASSIGN;
510         toketype = ASSIGNOP;
511     }
512     return REPORT(toketype);
513 }
514
515 /*
516  * S_no_op
517  * When Perl expects an operator and finds something else, no_op
518  * prints the warning.  It always prints "<something> found where
519  * operator expected.  It prints "Missing semicolon on previous line?"
520  * if the surprise occurs at the start of the line.  "do you need to
521  * predeclare ..." is printed out for code like "sub bar; foo bar $x"
522  * where the compiler doesn't know if foo is a method call or a function.
523  * It prints "Missing operator before end of line" if there's nothing
524  * after the missing operator, or "... before <...>" if there is something
525  * after the missing operator.
526  *
527  * PL_bufptr is expected to point to the start of the thing that was found,
528  * and s after the next token or partial token.
529  */
530
531 STATIC void
532 S_no_op(pTHX_ const char *const what, char *s)
533 {
534     char * const oldbp = PL_bufptr;
535     const bool is_first = (PL_oldbufptr == PL_linestart);
536
537     PERL_ARGS_ASSERT_NO_OP;
538
539     if (!s)
540         s = oldbp;
541     else
542         PL_bufptr = s;
543     yywarn(Perl_form(aTHX_ "%s found where operator expected", what), UTF ? SVf_UTF8 : 0);
544     if (ckWARN_d(WARN_SYNTAX)) {
545         if (is_first)
546             Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
547                     "\t(Missing semicolon on previous line?)\n");
548         else if (PL_oldoldbufptr && isIDFIRST_lazy_if_safe(PL_oldoldbufptr,
549                                                            PL_bufend,
550                                                            UTF))
551         {
552             const char *t;
553             for (t = PL_oldoldbufptr;
554                  (isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF) || *t == ':');
555                  t += UTF ? UTF8SKIP(t) : 1)
556             {
557                 NOOP;
558             }
559             if (t < PL_bufptr && isSPACE(*t))
560                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
561                         "\t(Do you need to predeclare %" UTF8f "?)\n",
562                       UTF8fARG(UTF, t - PL_oldoldbufptr, PL_oldoldbufptr));
563         }
564         else {
565             assert(s >= oldbp);
566             Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
567                     "\t(Missing operator before %" UTF8f "?)\n",
568                      UTF8fARG(UTF, s - oldbp, oldbp));
569         }
570     }
571     PL_bufptr = oldbp;
572 }
573
574 /*
575  * S_missingterm
576  * Complain about missing quote/regexp/heredoc terminator.
577  * If it's called with NULL then it cauterizes the line buffer.
578  * If we're in a delimited string and the delimiter is a control
579  * character, it's reformatted into a two-char sequence like ^C.
580  * This is fatal.
581  */
582
583 STATIC void
584 S_missingterm(pTHX_ char *s, STRLEN len)
585 {
586     char tmpbuf[UTF8_MAXBYTES + 1];
587     char q;
588     bool uni = FALSE;
589     SV *sv;
590     if (s) {
591         char * const nl = (char *) my_memrchr(s, '\n', len);
592         if (nl) {
593             *nl = '\0';
594             len = nl - s;
595         }
596         uni = UTF;
597     }
598     else if (PL_multi_close < 32) {
599         *tmpbuf = '^';
600         tmpbuf[1] = (char)toCTRL(PL_multi_close);
601         tmpbuf[2] = '\0';
602         s = tmpbuf;
603         len = 2;
604     }
605     else {
606         if (LIKELY(PL_multi_close < 256)) {
607             *tmpbuf = (char)PL_multi_close;
608             tmpbuf[1] = '\0';
609             len = 1;
610         }
611         else {
612             char *end = (char *)uvchr_to_utf8((U8 *)tmpbuf, PL_multi_close);
613             *end = '\0';
614             len = end - tmpbuf;
615             uni = TRUE;
616         }
617         s = tmpbuf;
618     }
619     q = memchr(s, '"', len) ? '\'' : '"';
620     sv = sv_2mortal(newSVpvn(s, len));
621     if (uni)
622         SvUTF8_on(sv);
623     Perl_croak(aTHX_ "Can't find string terminator %c%" SVf "%c"
624                      " anywhere before EOF", q, SVfARG(sv), q);
625 }
626
627 #include "feature.h"
628
629 /*
630  * experimental text filters for win32 carriage-returns, utf16-to-utf8 and
631  * utf16-to-utf8-reversed.
632  */
633
634 #ifdef PERL_CR_FILTER
635 static void
636 strip_return(SV *sv)
637 {
638     const char *s = SvPVX_const(sv);
639     const char * const e = s + SvCUR(sv);
640
641     PERL_ARGS_ASSERT_STRIP_RETURN;
642
643     /* outer loop optimized to do nothing if there are no CR-LFs */
644     while (s < e) {
645         if (*s++ == '\r' && *s == '\n') {
646             /* hit a CR-LF, need to copy the rest */
647             char *d = s - 1;
648             *d++ = *s++;
649             while (s < e) {
650                 if (*s == '\r' && s[1] == '\n')
651                     s++;
652                 *d++ = *s++;
653             }
654             SvCUR(sv) -= s - d;
655             return;
656         }
657     }
658 }
659
660 STATIC I32
661 S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
662 {
663     const I32 count = FILTER_READ(idx+1, sv, maxlen);
664     if (count > 0 && !maxlen)
665         strip_return(sv);
666     return count;
667 }
668 #endif
669
670 /*
671 =for apidoc lex_start
672
673 Creates and initialises a new lexer/parser state object, supplying
674 a context in which to lex and parse from a new source of Perl code.
675 A pointer to the new state object is placed in L</PL_parser>.  An entry
676 is made on the save stack so that upon unwinding, the new state object
677 will be destroyed and the former value of L</PL_parser> will be restored.
678 Nothing else need be done to clean up the parsing context.
679
680 The code to be parsed comes from C<line> and C<rsfp>.  C<line>, if
681 non-null, provides a string (in SV form) containing code to be parsed.
682 A copy of the string is made, so subsequent modification of C<line>
683 does not affect parsing.  C<rsfp>, if non-null, provides an input stream
684 from which code will be read to be parsed.  If both are non-null, the
685 code in C<line> comes first and must consist of complete lines of input,
686 and C<rsfp> supplies the remainder of the source.
687
688 The C<flags> parameter is reserved for future use.  Currently it is only
689 used by perl internally, so extensions should always pass zero.
690
691 =cut
692 */
693
694 /* LEX_START_SAME_FILTER indicates that this is not a new file, so it
695    can share filters with the current parser.
696    LEX_START_DONT_CLOSE indicates that the file handle wasn't opened by the
697    caller, hence isn't owned by the parser, so shouldn't be closed on parser
698    destruction. This is used to handle the case of defaulting to reading the
699    script from the standard input because no filename was given on the command
700    line (without getting confused by situation where STDIN has been closed, so
701    the script handle is opened on fd 0)  */
702
703 void
704 Perl_lex_start(pTHX_ SV *line, PerlIO *rsfp, U32 flags)
705 {
706     const char *s = NULL;
707     yy_parser *parser, *oparser;
708
709     if (flags && flags & ~LEX_START_FLAGS)
710         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_start");
711
712     /* create and initialise a parser */
713
714     Newxz(parser, 1, yy_parser);
715     parser->old_parser = oparser = PL_parser;
716     PL_parser = parser;
717
718     parser->stack = NULL;
719     parser->stack_max1 = NULL;
720     parser->ps = NULL;
721
722     /* on scope exit, free this parser and restore any outer one */
723     SAVEPARSER(parser);
724     parser->saved_curcop = PL_curcop;
725
726     /* initialise lexer state */
727
728     parser->nexttoke = 0;
729     parser->error_count = oparser ? oparser->error_count : 0;
730     parser->copline = parser->preambling = NOLINE;
731     parser->lex_state = LEX_NORMAL;
732     parser->expect = XSTATE;
733     parser->rsfp = rsfp;
734     parser->recheck_utf8_validity = TRUE;
735     parser->rsfp_filters =
736       !(flags & LEX_START_SAME_FILTER) || !oparser
737         ? NULL
738         : MUTABLE_AV(SvREFCNT_inc(
739             oparser->rsfp_filters
740              ? oparser->rsfp_filters
741              : (oparser->rsfp_filters = newAV())
742           ));
743
744     Newx(parser->lex_brackstack, 120, char);
745     Newx(parser->lex_casestack, 12, char);
746     *parser->lex_casestack = '\0';
747     Newxz(parser->lex_shared, 1, LEXSHARED);
748
749     if (line) {
750         STRLEN len;
751         const U8* first_bad_char_loc;
752
753         s = SvPV_const(line, len);
754
755         if (   SvUTF8(line)
756             && UNLIKELY(! is_utf8_string_loc((U8 *) s,
757                                              SvCUR(line),
758                                              &first_bad_char_loc)))
759         {
760             _force_out_malformed_utf8_message(first_bad_char_loc,
761                                               (U8 *) s + SvCUR(line),
762                                               0,
763                                               1 /* 1 means die */ );
764             NOT_REACHED; /* NOTREACHED */
765         }
766
767         parser->linestr = flags & LEX_START_COPIED
768                             ? SvREFCNT_inc_simple_NN(line)
769                             : newSVpvn_flags(s, len, SvUTF8(line));
770         if (!rsfp)
771             sv_catpvs(parser->linestr, "\n;");
772     } else {
773         parser->linestr = newSVpvn("\n;", rsfp ? 1 : 2);
774     }
775
776     parser->oldoldbufptr =
777         parser->oldbufptr =
778         parser->bufptr =
779         parser->linestart = SvPVX(parser->linestr);
780     parser->bufend = parser->bufptr + SvCUR(parser->linestr);
781     parser->last_lop = parser->last_uni = NULL;
782
783     STATIC_ASSERT_STMT(FITS_IN_8_BITS(LEX_IGNORE_UTF8_HINTS|LEX_EVALBYTES
784                                                         |LEX_DONT_CLOSE_RSFP));
785     parser->lex_flags = (U8) (flags & (LEX_IGNORE_UTF8_HINTS|LEX_EVALBYTES
786                                                         |LEX_DONT_CLOSE_RSFP));
787
788     parser->in_pod = parser->filtered = 0;
789 }
790
791
792 /* delete a parser object */
793
794 void
795 Perl_parser_free(pTHX_  const yy_parser *parser)
796 {
797     PERL_ARGS_ASSERT_PARSER_FREE;
798
799     PL_curcop = parser->saved_curcop;
800     SvREFCNT_dec(parser->linestr);
801
802     if (PL_parser->lex_flags & LEX_DONT_CLOSE_RSFP)
803         PerlIO_clearerr(parser->rsfp);
804     else if (parser->rsfp && (!parser->old_parser
805           || (parser->old_parser && parser->rsfp != parser->old_parser->rsfp)))
806         PerlIO_close(parser->rsfp);
807     SvREFCNT_dec(parser->rsfp_filters);
808     SvREFCNT_dec(parser->lex_stuff);
809     SvREFCNT_dec(parser->lex_sub_repl);
810
811     Safefree(parser->lex_brackstack);
812     Safefree(parser->lex_casestack);
813     Safefree(parser->lex_shared);
814     PL_parser = parser->old_parser;
815     Safefree(parser);
816 }
817
818 void
819 Perl_parser_free_nexttoke_ops(pTHX_  yy_parser *parser, OPSLAB *slab)
820 {
821     I32 nexttoke = parser->nexttoke;
822     PERL_ARGS_ASSERT_PARSER_FREE_NEXTTOKE_OPS;
823     while (nexttoke--) {
824         if (S_is_opval_token(parser->nexttype[nexttoke] & 0xffff)
825          && parser->nextval[nexttoke].opval
826          && parser->nextval[nexttoke].opval->op_slabbed
827          && OpSLAB(parser->nextval[nexttoke].opval) == slab) {
828             op_free(parser->nextval[nexttoke].opval);
829             parser->nextval[nexttoke].opval = NULL;
830         }
831     }
832 }
833
834
835 /*
836 =for apidoc AmnxUN|SV *|PL_parser-E<gt>linestr
837
838 Buffer scalar containing the chunk currently under consideration of the
839 text currently being lexed.  This is always a plain string scalar (for
840 which C<SvPOK> is true).  It is not intended to be used as a scalar by
841 normal scalar means; instead refer to the buffer directly by the pointer
842 variables described below.
843
844 The lexer maintains various C<char*> pointers to things in the
845 C<PL_parser-E<gt>linestr> buffer.  If C<PL_parser-E<gt>linestr> is ever
846 reallocated, all of these pointers must be updated.  Don't attempt to
847 do this manually, but rather use L</lex_grow_linestr> if you need to
848 reallocate the buffer.
849
850 The content of the text chunk in the buffer is commonly exactly one
851 complete line of input, up to and including a newline terminator,
852 but there are situations where it is otherwise.  The octets of the
853 buffer may be intended to be interpreted as either UTF-8 or Latin-1.
854 The function L</lex_bufutf8> tells you which.  Do not use the C<SvUTF8>
855 flag on this scalar, which may disagree with it.
856
857 For direct examination of the buffer, the variable
858 L</PL_parser-E<gt>bufend> points to the end of the buffer.  The current
859 lexing position is pointed to by L</PL_parser-E<gt>bufptr>.  Direct use
860 of these pointers is usually preferable to examination of the scalar
861 through normal scalar means.
862
863 =for apidoc AmnxUN|char *|PL_parser-E<gt>bufend
864
865 Direct pointer to the end of the chunk of text currently being lexed, the
866 end of the lexer buffer.  This is equal to C<SvPVX(PL_parser-E<gt>linestr)
867 + SvCUR(PL_parser-E<gt>linestr)>.  A C<NUL> character (zero octet) is
868 always located at the end of the buffer, and does not count as part of
869 the buffer's contents.
870
871 =for apidoc AmnxUN|char *|PL_parser-E<gt>bufptr
872
873 Points to the current position of lexing inside the lexer buffer.
874 Characters around this point may be freely examined, within
875 the range delimited by C<SvPVX(L</PL_parser-E<gt>linestr>)> and
876 L</PL_parser-E<gt>bufend>.  The octets of the buffer may be intended to be
877 interpreted as either UTF-8 or Latin-1, as indicated by L</lex_bufutf8>.
878
879 Lexing code (whether in the Perl core or not) moves this pointer past
880 the characters that it consumes.  It is also expected to perform some
881 bookkeeping whenever a newline character is consumed.  This movement
882 can be more conveniently performed by the function L</lex_read_to>,
883 which handles newlines appropriately.
884
885 Interpretation of the buffer's octets can be abstracted out by
886 using the slightly higher-level functions L</lex_peek_unichar> and
887 L</lex_read_unichar>.
888
889 =for apidoc AmnxUN|char *|PL_parser-E<gt>linestart
890
891 Points to the start of the current line inside the lexer buffer.
892 This is useful for indicating at which column an error occurred, and
893 not much else.  This must be updated by any lexing code that consumes
894 a newline; the function L</lex_read_to> handles this detail.
895
896 =cut
897 */
898
899 /*
900 =for apidoc lex_bufutf8
901
902 Indicates whether the octets in the lexer buffer
903 (L</PL_parser-E<gt>linestr>) should be interpreted as the UTF-8 encoding
904 of Unicode characters.  If not, they should be interpreted as Latin-1
905 characters.  This is analogous to the C<SvUTF8> flag for scalars.
906
907 In UTF-8 mode, it is not guaranteed that the lexer buffer actually
908 contains valid UTF-8.  Lexing code must be robust in the face of invalid
909 encoding.
910
911 The actual C<SvUTF8> flag of the L</PL_parser-E<gt>linestr> scalar
912 is significant, but not the whole story regarding the input character
913 encoding.  Normally, when a file is being read, the scalar contains octets
914 and its C<SvUTF8> flag is off, but the octets should be interpreted as
915 UTF-8 if the C<use utf8> pragma is in effect.  During a string eval,
916 however, the scalar may have the C<SvUTF8> flag on, and in this case its
917 octets should be interpreted as UTF-8 unless the C<use bytes> pragma
918 is in effect.  This logic may change in the future; use this function
919 instead of implementing the logic yourself.
920
921 =cut
922 */
923
924 bool
925 Perl_lex_bufutf8(pTHX)
926 {
927     return UTF;
928 }
929
930 /*
931 =for apidoc lex_grow_linestr
932
933 Reallocates the lexer buffer (L</PL_parser-E<gt>linestr>) to accommodate
934 at least C<len> octets (including terminating C<NUL>).  Returns a
935 pointer to the reallocated buffer.  This is necessary before making
936 any direct modification of the buffer that would increase its length.
937 L</lex_stuff_pvn> provides a more convenient way to insert text into
938 the buffer.
939
940 Do not use C<SvGROW> or C<sv_grow> directly on C<PL_parser-E<gt>linestr>;
941 this function updates all of the lexer's variables that point directly
942 into the buffer.
943
944 =cut
945 */
946
947 char *
948 Perl_lex_grow_linestr(pTHX_ STRLEN len)
949 {
950     SV *linestr;
951     char *buf;
952     STRLEN bufend_pos, bufptr_pos, oldbufptr_pos, oldoldbufptr_pos;
953     STRLEN linestart_pos, last_uni_pos, last_lop_pos, re_eval_start_pos;
954     bool current;
955
956     linestr = PL_parser->linestr;
957     buf = SvPVX(linestr);
958     if (len <= SvLEN(linestr))
959         return buf;
960
961     /* Is the lex_shared linestr SV the same as the current linestr SV?
962      * Only in this case does re_eval_start need adjusting, since it
963      * points within lex_shared->ls_linestr's buffer */
964     current = (   !PL_parser->lex_shared->ls_linestr
965                || linestr == PL_parser->lex_shared->ls_linestr);
966
967     bufend_pos = PL_parser->bufend - buf;
968     bufptr_pos = PL_parser->bufptr - buf;
969     oldbufptr_pos = PL_parser->oldbufptr - buf;
970     oldoldbufptr_pos = PL_parser->oldoldbufptr - buf;
971     linestart_pos = PL_parser->linestart - buf;
972     last_uni_pos = PL_parser->last_uni ? PL_parser->last_uni - buf : 0;
973     last_lop_pos = PL_parser->last_lop ? PL_parser->last_lop - buf : 0;
974     re_eval_start_pos = (current && PL_parser->lex_shared->re_eval_start) ?
975                             PL_parser->lex_shared->re_eval_start - buf : 0;
976
977     buf = sv_grow(linestr, len);
978
979     PL_parser->bufend = buf + bufend_pos;
980     PL_parser->bufptr = buf + bufptr_pos;
981     PL_parser->oldbufptr = buf + oldbufptr_pos;
982     PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
983     PL_parser->linestart = buf + linestart_pos;
984     if (PL_parser->last_uni)
985         PL_parser->last_uni = buf + last_uni_pos;
986     if (PL_parser->last_lop)
987         PL_parser->last_lop = buf + last_lop_pos;
988     if (current && PL_parser->lex_shared->re_eval_start)
989         PL_parser->lex_shared->re_eval_start  = buf + re_eval_start_pos;
990     return buf;
991 }
992
993 /*
994 =for apidoc lex_stuff_pvn
995
996 Insert characters into the lexer buffer (L</PL_parser-E<gt>linestr>),
997 immediately after the current lexing point (L</PL_parser-E<gt>bufptr>),
998 reallocating the buffer if necessary.  This means that lexing code that
999 runs later will see the characters as if they had appeared in the input.
1000 It is not recommended to do this as part of normal parsing, and most
1001 uses of this facility run the risk of the inserted characters being
1002 interpreted in an unintended manner.
1003
1004 The string to be inserted is represented by C<len> octets starting
1005 at C<pv>.  These octets are interpreted as either UTF-8 or Latin-1,
1006 according to whether the C<LEX_STUFF_UTF8> flag is set in C<flags>.
1007 The characters are recoded for the lexer buffer, according to how the
1008 buffer is currently being interpreted (L</lex_bufutf8>).  If a string
1009 to be inserted is available as a Perl scalar, the L</lex_stuff_sv>
1010 function is more convenient.
1011
1012 =for apidoc Amnh||LEX_STUFF_UTF8
1013
1014 =cut
1015 */
1016
1017 void
1018 Perl_lex_stuff_pvn(pTHX_ const char *pv, STRLEN len, U32 flags)
1019 {
1020     dVAR;
1021     char *bufptr;
1022     PERL_ARGS_ASSERT_LEX_STUFF_PVN;
1023     if (flags & ~(LEX_STUFF_UTF8))
1024         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_stuff_pvn");
1025     if (UTF) {
1026         if (flags & LEX_STUFF_UTF8) {
1027             goto plain_copy;
1028         } else {
1029             STRLEN highhalf = variant_under_utf8_count((U8 *) pv,
1030                                                        (U8 *) pv + len);
1031             const char *p, *e = pv+len;;
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 lex_stuff_pv
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 lex_stuff_sv
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 lex_unstuff
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 lex_read_to
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 lex_discard_to
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 lex_next_chunk
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 =for apidoc Amnh||LEX_KEEP_PREVIOUS
1308
1309 =cut
1310 */
1311
1312 #define LEX_FAKE_EOF 0x80000000
1313 #define LEX_NO_TERM  0x40000000 /* here-doc */
1314
1315 bool
1316 Perl_lex_next_chunk(pTHX_ U32 flags)
1317 {
1318     SV *linestr;
1319     char *buf;
1320     STRLEN old_bufend_pos, new_bufend_pos;
1321     STRLEN bufptr_pos, oldbufptr_pos, oldoldbufptr_pos;
1322     STRLEN linestart_pos, last_uni_pos, last_lop_pos;
1323     bool got_some_for_debugger = 0;
1324     bool got_some;
1325
1326     if (flags & ~(LEX_KEEP_PREVIOUS|LEX_FAKE_EOF|LEX_NO_TERM))
1327         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_next_chunk");
1328     if (!(flags & LEX_NO_TERM) && PL_lex_inwhat)
1329         return FALSE;
1330     linestr = PL_parser->linestr;
1331     buf = SvPVX(linestr);
1332     if (!(flags & LEX_KEEP_PREVIOUS)
1333           && PL_parser->bufptr == PL_parser->bufend)
1334     {
1335         old_bufend_pos = bufptr_pos = oldbufptr_pos = oldoldbufptr_pos = 0;
1336         linestart_pos = 0;
1337         if (PL_parser->last_uni != PL_parser->bufend)
1338             PL_parser->last_uni = NULL;
1339         if (PL_parser->last_lop != PL_parser->bufend)
1340             PL_parser->last_lop = NULL;
1341         last_uni_pos = last_lop_pos = 0;
1342         *buf = 0;
1343         SvCUR_set(linestr, 0);
1344     } else {
1345         old_bufend_pos = PL_parser->bufend - buf;
1346         bufptr_pos = PL_parser->bufptr - buf;
1347         oldbufptr_pos = PL_parser->oldbufptr - buf;
1348         oldoldbufptr_pos = PL_parser->oldoldbufptr - buf;
1349         linestart_pos = PL_parser->linestart - buf;
1350         last_uni_pos = PL_parser->last_uni ? PL_parser->last_uni - buf : 0;
1351         last_lop_pos = PL_parser->last_lop ? PL_parser->last_lop - buf : 0;
1352     }
1353     if (flags & LEX_FAKE_EOF) {
1354         goto eof;
1355     } else if (!PL_parser->rsfp && !PL_parser->filtered) {
1356         got_some = 0;
1357     } else if (filter_gets(linestr, old_bufend_pos)) {
1358         got_some = 1;
1359         got_some_for_debugger = 1;
1360     } else if (flags & LEX_NO_TERM) {
1361         got_some = 0;
1362     } else {
1363         if (!SvPOK(linestr))   /* can get undefined by filter_gets */
1364             SvPVCLEAR(linestr);
1365         eof:
1366         /* End of real input.  Close filehandle (unless it was STDIN),
1367          * then add implicit termination.
1368          */
1369         if (PL_parser->lex_flags & LEX_DONT_CLOSE_RSFP)
1370             PerlIO_clearerr(PL_parser->rsfp);
1371         else if (PL_parser->rsfp)
1372             (void)PerlIO_close(PL_parser->rsfp);
1373         PL_parser->rsfp = NULL;
1374         PL_parser->in_pod = PL_parser->filtered = 0;
1375         if (!PL_in_eval && PL_minus_p) {
1376             sv_catpvs(linestr,
1377                 /*{*/";}continue{print or die qq(-p destination: $!\\n);}");
1378             PL_minus_n = PL_minus_p = 0;
1379         } else if (!PL_in_eval && PL_minus_n) {
1380             sv_catpvs(linestr, /*{*/";}");
1381             PL_minus_n = 0;
1382         } else
1383             sv_catpvs(linestr, ";");
1384         got_some = 1;
1385     }
1386     buf = SvPVX(linestr);
1387     new_bufend_pos = SvCUR(linestr);
1388     PL_parser->bufend = buf + new_bufend_pos;
1389     PL_parser->bufptr = buf + bufptr_pos;
1390
1391     if (UTF) {
1392         const U8* first_bad_char_loc;
1393         if (UNLIKELY(! is_utf8_string_loc(
1394                             (U8 *) PL_parser->bufptr,
1395                                    PL_parser->bufend - PL_parser->bufptr,
1396                                    &first_bad_char_loc)))
1397         {
1398             _force_out_malformed_utf8_message(first_bad_char_loc,
1399                                               (U8 *) PL_parser->bufend,
1400                                               0,
1401                                               1 /* 1 means die */ );
1402             NOT_REACHED; /* NOTREACHED */
1403         }
1404     }
1405
1406     PL_parser->oldbufptr = buf + oldbufptr_pos;
1407     PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
1408     PL_parser->linestart = buf + linestart_pos;
1409     if (PL_parser->last_uni)
1410         PL_parser->last_uni = buf + last_uni_pos;
1411     if (PL_parser->last_lop)
1412         PL_parser->last_lop = buf + last_lop_pos;
1413     if (PL_parser->preambling != NOLINE) {
1414         CopLINE_set(PL_curcop, PL_parser->preambling + 1);
1415         PL_parser->preambling = NOLINE;
1416     }
1417     if (   got_some_for_debugger
1418         && PERLDB_LINE_OR_SAVESRC
1419         && PL_curstash != PL_debstash)
1420     {
1421         /* debugger active and we're not compiling the debugger code,
1422          * so store the line into the debugger's array of lines
1423          */
1424         update_debugger_info(NULL, buf+old_bufend_pos,
1425             new_bufend_pos-old_bufend_pos);
1426     }
1427     return got_some;
1428 }
1429
1430 /*
1431 =for apidoc lex_peek_unichar
1432
1433 Looks ahead one (Unicode) character in the text currently being lexed.
1434 Returns the codepoint (unsigned integer value) of the next character,
1435 or -1 if lexing has reached the end of the input text.  To consume the
1436 peeked character, use L</lex_read_unichar>.
1437
1438 If the next character is in (or extends into) the next chunk of input
1439 text, the next chunk will be read in.  Normally the current chunk will be
1440 discarded at the same time, but if C<flags> has the C<LEX_KEEP_PREVIOUS>
1441 bit set, then the current chunk will not be discarded.
1442
1443 If the input is being interpreted as UTF-8 and a UTF-8 encoding error
1444 is encountered, an exception is generated.
1445
1446 =cut
1447 */
1448
1449 I32
1450 Perl_lex_peek_unichar(pTHX_ U32 flags)
1451 {
1452     dVAR;
1453     char *s, *bufend;
1454     if (flags & ~(LEX_KEEP_PREVIOUS))
1455         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_peek_unichar");
1456     s = PL_parser->bufptr;
1457     bufend = PL_parser->bufend;
1458     if (UTF) {
1459         U8 head;
1460         I32 unichar;
1461         STRLEN len, retlen;
1462         if (s == bufend) {
1463             if (!lex_next_chunk(flags))
1464                 return -1;
1465             s = PL_parser->bufptr;
1466             bufend = PL_parser->bufend;
1467         }
1468         head = (U8)*s;
1469         if (UTF8_IS_INVARIANT(head))
1470             return head;
1471         if (UTF8_IS_START(head)) {
1472             len = UTF8SKIP(&head);
1473             while ((STRLEN)(bufend-s) < len) {
1474                 if (!lex_next_chunk(flags | LEX_KEEP_PREVIOUS))
1475                     break;
1476                 s = PL_parser->bufptr;
1477                 bufend = PL_parser->bufend;
1478             }
1479         }
1480         unichar = utf8n_to_uvchr((U8*)s, bufend-s, &retlen, UTF8_CHECK_ONLY);
1481         if (retlen == (STRLEN)-1) {
1482             _force_out_malformed_utf8_message((U8 *) s,
1483                                               (U8 *) bufend,
1484                                               0,
1485                                               1 /* 1 means die */ );
1486             NOT_REACHED; /* NOTREACHED */
1487         }
1488         return unichar;
1489     } else {
1490         if (s == bufend) {
1491             if (!lex_next_chunk(flags))
1492                 return -1;
1493             s = PL_parser->bufptr;
1494         }
1495         return (U8)*s;
1496     }
1497 }
1498
1499 /*
1500 =for apidoc lex_read_unichar
1501
1502 Reads the next (Unicode) character in the text currently being lexed.
1503 Returns the codepoint (unsigned integer value) of the character read,
1504 and moves L</PL_parser-E<gt>bufptr> past the character, or returns -1
1505 if lexing has reached the end of the input text.  To non-destructively
1506 examine the next character, use L</lex_peek_unichar> instead.
1507
1508 If the next character is in (or extends into) the next chunk of input
1509 text, the next chunk will be read in.  Normally the current chunk will be
1510 discarded at the same time, but if C<flags> has the C<LEX_KEEP_PREVIOUS>
1511 bit set, then the current chunk will not be discarded.
1512
1513 If the input is being interpreted as UTF-8 and a UTF-8 encoding error
1514 is encountered, an exception is generated.
1515
1516 =cut
1517 */
1518
1519 I32
1520 Perl_lex_read_unichar(pTHX_ U32 flags)
1521 {
1522     I32 c;
1523     if (flags & ~(LEX_KEEP_PREVIOUS))
1524         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_unichar");
1525     c = lex_peek_unichar(flags);
1526     if (c != -1) {
1527         if (c == '\n')
1528             COPLINE_INC_WITH_HERELINES;
1529         if (UTF)
1530             PL_parser->bufptr += UTF8SKIP(PL_parser->bufptr);
1531         else
1532             ++(PL_parser->bufptr);
1533     }
1534     return c;
1535 }
1536
1537 /*
1538 =for apidoc lex_read_space
1539
1540 Reads optional spaces, in Perl style, in the text currently being
1541 lexed.  The spaces may include ordinary whitespace characters and
1542 Perl-style comments.  C<#line> directives are processed if encountered.
1543 L</PL_parser-E<gt>bufptr> is moved past the spaces, so that it points
1544 at a non-space character (or the end of the input text).
1545
1546 If spaces extend into the next chunk of input text, the next chunk will
1547 be read in.  Normally the current chunk will be discarded at the same
1548 time, but if C<flags> has the C<LEX_KEEP_PREVIOUS> bit set, then the current
1549 chunk will not be discarded.
1550
1551 =cut
1552 */
1553
1554 #define LEX_NO_INCLINE    0x40000000
1555 #define LEX_NO_NEXT_CHUNK 0x80000000
1556
1557 void
1558 Perl_lex_read_space(pTHX_ U32 flags)
1559 {
1560     char *s, *bufend;
1561     const bool can_incline = !(flags & LEX_NO_INCLINE);
1562     bool need_incline = 0;
1563     if (flags & ~(LEX_KEEP_PREVIOUS|LEX_NO_NEXT_CHUNK|LEX_NO_INCLINE))
1564         Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_read_space");
1565     s = PL_parser->bufptr;
1566     bufend = PL_parser->bufend;
1567     while (1) {
1568         char c = *s;
1569         if (c == '#') {
1570             do {
1571                 c = *++s;
1572             } while (!(c == '\n' || (c == 0 && s == bufend)));
1573         } else if (c == '\n') {
1574             s++;
1575             if (can_incline) {
1576                 PL_parser->linestart = s;
1577                 if (s == bufend)
1578                     need_incline = 1;
1579                 else
1580                     incline(s, bufend);
1581             }
1582         } else if (isSPACE(c)) {
1583             s++;
1584         } else if (c == 0 && s == bufend) {
1585             bool got_more;
1586             line_t l;
1587             if (flags & LEX_NO_NEXT_CHUNK)
1588                 break;
1589             PL_parser->bufptr = s;
1590             l = CopLINE(PL_curcop);
1591             CopLINE(PL_curcop) += PL_parser->herelines + 1;
1592             got_more = lex_next_chunk(flags);
1593             CopLINE_set(PL_curcop, l);
1594             s = PL_parser->bufptr;
1595             bufend = PL_parser->bufend;
1596             if (!got_more)
1597                 break;
1598             if (can_incline && need_incline && PL_parser->rsfp) {
1599                 incline(s, bufend);
1600                 need_incline = 0;
1601             }
1602         } else if (!c) {
1603             s++;
1604         } else {
1605             break;
1606         }
1607     }
1608     PL_parser->bufptr = s;
1609 }
1610
1611 /*
1612
1613 =for apidoc validate_proto
1614
1615 This function performs syntax checking on a prototype, C<proto>.
1616 If C<warn> is true, any illegal characters or mismatched brackets
1617 will trigger illegalproto warnings, declaring that they were
1618 detected in the prototype for C<name>.
1619
1620 The return value is C<true> if this is a valid prototype, and
1621 C<false> if it is not, regardless of whether C<warn> was C<true> or
1622 C<false>.
1623
1624 Note that C<NULL> is a valid C<proto> and will always return C<true>.
1625
1626 =cut
1627
1628  */
1629
1630 bool
1631 Perl_validate_proto(pTHX_ SV *name, SV *proto, bool warn, bool curstash)
1632 {
1633     STRLEN len, origlen;
1634     char *p;
1635     bool bad_proto = FALSE;
1636     bool in_brackets = FALSE;
1637     bool after_slash = FALSE;
1638     char greedy_proto = ' ';
1639     bool proto_after_greedy_proto = FALSE;
1640     bool must_be_last = FALSE;
1641     bool underscore = FALSE;
1642     bool bad_proto_after_underscore = FALSE;
1643
1644     PERL_ARGS_ASSERT_VALIDATE_PROTO;
1645
1646     if (!proto)
1647         return TRUE;
1648
1649     p = SvPV(proto, len);
1650     origlen = len;
1651     for (; len--; p++) {
1652         if (!isSPACE(*p)) {
1653             if (must_be_last)
1654                 proto_after_greedy_proto = TRUE;
1655             if (underscore) {
1656                 if (!memCHRs(";@%", *p))
1657                     bad_proto_after_underscore = TRUE;
1658                 underscore = FALSE;
1659             }
1660             if (!memCHRs("$@%*;[]&\\_+", *p) || *p == '\0') {
1661                 bad_proto = TRUE;
1662             }
1663             else {
1664                 if (*p == '[')
1665                     in_brackets = TRUE;
1666                 else if (*p == ']')
1667                     in_brackets = FALSE;
1668                 else if ((*p == '@' || *p == '%')
1669                          && !after_slash
1670                          && !in_brackets )
1671                 {
1672                     must_be_last = TRUE;
1673                     greedy_proto = *p;
1674                 }
1675                 else if (*p == '_')
1676                     underscore = TRUE;
1677             }
1678             if (*p == '\\')
1679                 after_slash = TRUE;
1680             else
1681                 after_slash = FALSE;
1682         }
1683     }
1684
1685     if (warn) {
1686         SV *tmpsv = newSVpvs_flags("", SVs_TEMP);
1687         p -= origlen;
1688         p = SvUTF8(proto)
1689             ? sv_uni_display(tmpsv, newSVpvn_flags(p, origlen, SVs_TEMP | SVf_UTF8),
1690                              origlen, UNI_DISPLAY_ISPRINT)
1691             : pv_pretty(tmpsv, p, origlen, 60, NULL, NULL, PERL_PV_ESCAPE_NONASCII);
1692
1693         if (curstash && !memchr(SvPVX(name), ':', SvCUR(name))) {
1694             SV *name2 = sv_2mortal(newSVsv(PL_curstname));
1695             sv_catpvs(name2, "::");
1696             sv_catsv(name2, (SV *)name);
1697             name = name2;
1698         }
1699
1700         if (proto_after_greedy_proto)
1701             Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1702                         "Prototype after '%c' for %" SVf " : %s",
1703                         greedy_proto, SVfARG(name), p);
1704         if (in_brackets)
1705             Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1706                         "Missing ']' in prototype for %" SVf " : %s",
1707                         SVfARG(name), p);
1708         if (bad_proto)
1709             Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1710                         "Illegal character in prototype for %" SVf " : %s",
1711                         SVfARG(name), p);
1712         if (bad_proto_after_underscore)
1713             Perl_warner(aTHX_ packWARN(WARN_ILLEGALPROTO),
1714                         "Illegal character after '_' in prototype for %" SVf " : %s",
1715                         SVfARG(name), p);
1716     }
1717
1718     return (! (proto_after_greedy_proto || bad_proto) );
1719 }
1720
1721 /*
1722  * S_incline
1723  * This subroutine has nothing to do with tilting, whether at windmills
1724  * or pinball tables.  Its name is short for "increment line".  It
1725  * increments the current line number in CopLINE(PL_curcop) and checks
1726  * to see whether the line starts with a comment of the form
1727  *    # line 500 "foo.pm"
1728  * If so, it sets the current line number and file to the values in the comment.
1729  */
1730
1731 STATIC void
1732 S_incline(pTHX_ const char *s, const char *end)
1733 {
1734     const char *t;
1735     const char *n;
1736     const char *e;
1737     line_t line_num;
1738     UV uv;
1739
1740     PERL_ARGS_ASSERT_INCLINE;
1741
1742     assert(end >= s);
1743
1744     COPLINE_INC_WITH_HERELINES;
1745     if (!PL_rsfp && !PL_parser->filtered && PL_lex_state == LEX_NORMAL
1746      && s+1 == PL_bufend && *s == ';') {
1747         /* fake newline in string eval */
1748         CopLINE_dec(PL_curcop);
1749         return;
1750     }
1751     if (*s++ != '#')
1752         return;
1753     while (SPACE_OR_TAB(*s))
1754         s++;
1755     if (memBEGINs(s, (STRLEN) (end - s), "line"))
1756         s += sizeof("line") - 1;
1757     else
1758         return;
1759     if (SPACE_OR_TAB(*s))
1760         s++;
1761     else
1762         return;
1763     while (SPACE_OR_TAB(*s))
1764         s++;
1765     if (!isDIGIT(*s))
1766         return;
1767
1768     n = s;
1769     while (isDIGIT(*s))
1770         s++;
1771     if (!SPACE_OR_TAB(*s) && *s != '\r' && *s != '\n' && *s != '\0')
1772         return;
1773     while (SPACE_OR_TAB(*s))
1774         s++;
1775     if (*s == '"' && (t = (char *) memchr(s+1, '"', end - s))) {
1776         s++;
1777         e = t + 1;
1778     }
1779     else {
1780         t = s;
1781         while (*t && !isSPACE(*t))
1782             t++;
1783         e = t;
1784     }
1785     while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
1786         e++;
1787     if (*e != '\n' && *e != '\0')
1788         return;         /* false alarm */
1789
1790     if (!grok_atoUV(n, &uv, &e))
1791         return;
1792     line_num = ((line_t)uv) - 1;
1793
1794     if (t - s > 0) {
1795         const STRLEN len = t - s;
1796
1797         if (!PL_rsfp && !PL_parser->filtered) {
1798             /* must copy *{"::_<(eval N)[oldfilename:L]"}
1799              * to *{"::_<newfilename"} */
1800             /* However, the long form of evals is only turned on by the
1801                debugger - usually they're "(eval %lu)" */
1802             GV * const cfgv = CopFILEGV(PL_curcop);
1803             if (cfgv) {
1804                 char smallbuf[128];
1805                 STRLEN tmplen2 = len;
1806                 char *tmpbuf2;
1807                 GV *gv2;
1808
1809                 if (tmplen2 + 2 <= sizeof smallbuf)
1810                     tmpbuf2 = smallbuf;
1811                 else
1812                     Newx(tmpbuf2, tmplen2 + 2, char);
1813
1814                 tmpbuf2[0] = '_';
1815                 tmpbuf2[1] = '<';
1816
1817                 memcpy(tmpbuf2 + 2, s, tmplen2);
1818                 tmplen2 += 2;
1819
1820                 gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE);
1821                 if (!isGV(gv2)) {
1822                     gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE);
1823                     /* adjust ${"::_<newfilename"} to store the new file name */
1824                     GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2);
1825                     /* The line number may differ. If that is the case,
1826                        alias the saved lines that are in the array.
1827                        Otherwise alias the whole array. */
1828                     if (CopLINE(PL_curcop) == line_num) {
1829                         GvHV(gv2) = MUTABLE_HV(SvREFCNT_inc(GvHV(cfgv)));
1830                         GvAV(gv2) = MUTABLE_AV(SvREFCNT_inc(GvAV(cfgv)));
1831                     }
1832                     else if (GvAV(cfgv)) {
1833                         AV * const av = GvAV(cfgv);
1834                         const line_t start = CopLINE(PL_curcop)+1;
1835                         SSize_t items = AvFILLp(av) - start;
1836                         if (items > 0) {
1837                             AV * const av2 = GvAVn(gv2);
1838                             SV **svp = AvARRAY(av) + start;
1839                             Size_t l = line_num+1;
1840                             while (items-- && l < SSize_t_MAX && l == (line_t)l)
1841                                 av_store(av2, (SSize_t)l++, SvREFCNT_inc(*svp++));
1842                         }
1843                     }
1844                 }
1845
1846                 if (tmpbuf2 != smallbuf) Safefree(tmpbuf2);
1847             }
1848         }
1849         CopFILE_free(PL_curcop);
1850         CopFILE_setn(PL_curcop, s, len);
1851     }
1852     CopLINE_set(PL_curcop, line_num);
1853 }
1854
1855 STATIC void
1856 S_update_debugger_info(pTHX_ SV *orig_sv, const char *const buf, STRLEN len)
1857 {
1858     AV *av = CopFILEAVx(PL_curcop);
1859     if (av) {
1860         SV * sv;
1861         if (PL_parser->preambling == NOLINE) sv = newSV_type(SVt_PVMG);
1862         else {
1863             sv = *av_fetch(av, 0, 1);
1864             SvUPGRADE(sv, SVt_PVMG);
1865         }
1866         if (!SvPOK(sv)) SvPVCLEAR(sv);
1867         if (orig_sv)
1868             sv_catsv(sv, orig_sv);
1869         else
1870             sv_catpvn(sv, buf, len);
1871         if (!SvIOK(sv)) {
1872             (void)SvIOK_on(sv);
1873             SvIV_set(sv, 0);
1874         }
1875         if (PL_parser->preambling == NOLINE)
1876             av_store(av, CopLINE(PL_curcop), sv);
1877     }
1878 }
1879
1880 /*
1881  * skipspace
1882  * Called to gobble the appropriate amount and type of whitespace.
1883  * Skips comments as well.
1884  * Returns the next character after the whitespace that is skipped.
1885  *
1886  * peekspace
1887  * Same thing, but look ahead without incrementing line numbers or
1888  * adjusting PL_linestart.
1889  */
1890
1891 #define skipspace(s) skipspace_flags(s, 0)
1892 #define peekspace(s) skipspace_flags(s, LEX_NO_INCLINE)
1893
1894 char *
1895 Perl_skipspace_flags(pTHX_ char *s, U32 flags)
1896 {
1897     PERL_ARGS_ASSERT_SKIPSPACE_FLAGS;
1898     if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
1899         while (s < PL_bufend && (SPACE_OR_TAB(*s) || !*s))
1900             s++;
1901     } else {
1902         STRLEN bufptr_pos = PL_bufptr - SvPVX(PL_linestr);
1903         PL_bufptr = s;
1904         lex_read_space(flags | LEX_KEEP_PREVIOUS |
1905                 (PL_lex_inwhat || PL_lex_state == LEX_FORMLINE ?
1906                     LEX_NO_NEXT_CHUNK : 0));
1907         s = PL_bufptr;
1908         PL_bufptr = SvPVX(PL_linestr) + bufptr_pos;
1909         if (PL_linestart > PL_bufptr)
1910             PL_bufptr = PL_linestart;
1911         return s;
1912     }
1913     return s;
1914 }
1915
1916 /*
1917  * S_check_uni
1918  * Check the unary operators to ensure there's no ambiguity in how they're
1919  * used.  An ambiguous piece of code would be:
1920  *     rand + 5
1921  * This doesn't mean rand() + 5.  Because rand() is a unary operator,
1922  * the +5 is its argument.
1923  */
1924
1925 STATIC void
1926 S_check_uni(pTHX)
1927 {
1928     const char *s;
1929
1930     if (PL_oldoldbufptr != PL_last_uni)
1931         return;
1932     while (isSPACE(*PL_last_uni))
1933         PL_last_uni++;
1934     s = PL_last_uni;
1935     while (isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF) || *s == '-')
1936         s += UTF ? UTF8SKIP(s) : 1;
1937     if (s < PL_bufptr && memchr(s, '(', PL_bufptr - s))
1938         return;
1939
1940     Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
1941                      "Warning: Use of \"%" UTF8f "\" without parentheses is ambiguous",
1942                      UTF8fARG(UTF, (int)(s - PL_last_uni), PL_last_uni));
1943 }
1944
1945 /*
1946  * LOP : macro to build a list operator.  Its behaviour has been replaced
1947  * with a subroutine, S_lop() for which LOP is just another name.
1948  */
1949
1950 #define LOP(f,x) return lop(f,x,s)
1951
1952 /*
1953  * S_lop
1954  * Build a list operator (or something that might be one).  The rules:
1955  *  - if we have a next token, then it's a list operator (no parens) for
1956  *    which the next token has already been parsed; e.g.,
1957  *       sort foo @args
1958  *       sort foo (@args)
1959  *  - if the next thing is an opening paren, then it's a function
1960  *  - else it's a list operator
1961  */
1962
1963 STATIC I32
1964 S_lop(pTHX_ I32 f, U8 x, char *s)
1965 {
1966     PERL_ARGS_ASSERT_LOP;
1967
1968     pl_yylval.ival = f;
1969     CLINE;
1970     PL_bufptr = s;
1971     PL_last_lop = PL_oldbufptr;
1972     PL_last_lop_op = (OPCODE)f;
1973     if (PL_nexttoke)
1974         goto lstop;
1975     PL_expect = x;
1976     if (*s == '(')
1977         return REPORT(FUNC);
1978     s = skipspace(s);
1979     if (*s == '(')
1980         return REPORT(FUNC);
1981     else {
1982         lstop:
1983         if (!PL_lex_allbrackets && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
1984             PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
1985         return REPORT(LSTOP);
1986     }
1987 }
1988
1989 /*
1990  * S_force_next
1991  * When the lexer realizes it knows the next token (for instance,
1992  * it is reordering tokens for the parser) then it can call S_force_next
1993  * to know what token to return the next time the lexer is called.  Caller
1994  * will need to set PL_nextval[] and possibly PL_expect to ensure
1995  * the lexer handles the token correctly.
1996  */
1997
1998 STATIC void
1999 S_force_next(pTHX_ I32 type)
2000 {
2001 #ifdef DEBUGGING
2002     if (DEBUG_T_TEST) {
2003         PerlIO_printf(Perl_debug_log, "### forced token:\n");
2004         tokereport(type, &NEXTVAL_NEXTTOKE);
2005     }
2006 #endif
2007     assert(PL_nexttoke < C_ARRAY_LENGTH(PL_nexttype));
2008     PL_nexttype[PL_nexttoke] = type;
2009     PL_nexttoke++;
2010 }
2011
2012 /*
2013  * S_postderef
2014  *
2015  * This subroutine handles postfix deref syntax after the arrow has already
2016  * been emitted.  @* $* etc. are emitted as two separate tokens right here.
2017  * @[ @{ %[ %{ *{ are emitted also as two tokens, but this function emits
2018  * only the first, leaving yylex to find the next.
2019  */
2020
2021 static int
2022 S_postderef(pTHX_ int const funny, char const next)
2023 {
2024     assert(funny == DOLSHARP || memCHRs("$@%&*", funny));
2025     if (next == '*') {
2026         PL_expect = XOPERATOR;
2027         if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) {
2028             assert('@' == funny || '$' == funny || DOLSHARP == funny);
2029             PL_lex_state = LEX_INTERPEND;
2030             if ('@' == funny)
2031                 force_next(POSTJOIN);
2032         }
2033         force_next(next);
2034         PL_bufptr+=2;
2035     }
2036     else {
2037         if ('@' == funny && PL_lex_state == LEX_INTERPNORMAL
2038          && !PL_lex_brackets)
2039             PL_lex_dojoin = 2;
2040         PL_expect = XOPERATOR;
2041         PL_bufptr++;
2042     }
2043     return funny;
2044 }
2045
2046 void
2047 Perl_yyunlex(pTHX)
2048 {
2049     int yyc = PL_parser->yychar;
2050     if (yyc != YYEMPTY) {
2051         if (yyc) {
2052             NEXTVAL_NEXTTOKE = PL_parser->yylval;
2053             if (yyc == '{'/*}*/ || yyc == HASHBRACK || yyc == '['/*]*/) {
2054                 PL_lex_allbrackets--;
2055                 PL_lex_brackets--;
2056                 yyc |= (3<<24) | (PL_lex_brackstack[PL_lex_brackets] << 16);
2057             } else if (yyc == '('/*)*/) {
2058                 PL_lex_allbrackets--;
2059                 yyc |= (2<<24);
2060             }
2061             force_next(yyc);
2062         }
2063         PL_parser->yychar = YYEMPTY;
2064     }
2065 }
2066
2067 STATIC SV *
2068 S_newSV_maybe_utf8(pTHX_ const char *const start, STRLEN len)
2069 {
2070     SV * const sv = newSVpvn_utf8(start, len,
2071                     ! IN_BYTES
2072                   &&  UTF
2073                   &&  len != 0
2074                   &&  is_utf8_non_invariant_string((const U8*)start, len));
2075     return sv;
2076 }
2077
2078 /*
2079  * S_force_word
2080  * When the lexer knows the next thing is a word (for instance, it has
2081  * just seen -> and it knows that the next char is a word char, then
2082  * it calls S_force_word to stick the next word into the PL_nexttoke/val
2083  * lookahead.
2084  *
2085  * Arguments:
2086  *   char *start : buffer position (must be within PL_linestr)
2087  *   int token   : PL_next* will be this type of bare word
2088  *                 (e.g., METHOD,BAREWORD)
2089  *   int check_keyword : if true, Perl checks to make sure the word isn't
2090  *       a keyword (do this if the word is a label, e.g. goto FOO)
2091  *   int allow_pack : if true, : characters will also be allowed (require,
2092  *       use, etc. do this)
2093  */
2094
2095 STATIC char *
2096 S_force_word(pTHX_ char *start, int token, int check_keyword, int allow_pack)
2097 {
2098     char *s;
2099     STRLEN len;
2100
2101     PERL_ARGS_ASSERT_FORCE_WORD;
2102
2103     start = skipspace(start);
2104     s = start;
2105     if (   isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)
2106         || (allow_pack && *s == ':' && s[1] == ':') )
2107     {
2108         s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, allow_pack, &len);
2109         if (check_keyword) {
2110           char *s2 = PL_tokenbuf;
2111           STRLEN len2 = len;
2112           if (allow_pack && memBEGINPs(s2, len, "CORE::")) {
2113             s2 += sizeof("CORE::") - 1;
2114             len2 -= sizeof("CORE::") - 1;
2115           }
2116           if (keyword(s2, len2, 0))
2117             return start;
2118         }
2119         if (token == METHOD) {
2120             s = skipspace(s);
2121             if (*s == '(')
2122                 PL_expect = XTERM;
2123             else {
2124                 PL_expect = XOPERATOR;
2125             }
2126         }
2127         NEXTVAL_NEXTTOKE.opval
2128             = newSVOP(OP_CONST,0,
2129                            S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
2130         NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
2131         force_next(token);
2132     }
2133     return s;
2134 }
2135
2136 /*
2137  * S_force_ident
2138  * Called when the lexer wants $foo *foo &foo etc, but the program
2139  * text only contains the "foo" portion.  The first argument is a pointer
2140  * to the "foo", and the second argument is the type symbol to prefix.
2141  * Forces the next token to be a "BAREWORD".
2142  * Creates the symbol if it didn't already exist (via gv_fetchpv()).
2143  */
2144
2145 STATIC void
2146 S_force_ident(pTHX_ const char *s, int kind)
2147 {
2148     PERL_ARGS_ASSERT_FORCE_IDENT;
2149
2150     if (s[0]) {
2151         const STRLEN len = s[1] ? strlen(s) : 1; /* s = "\"" see yylex */
2152         OP* const o = newSVOP(OP_CONST, 0, newSVpvn_flags(s, len,
2153                                                                 UTF ? SVf_UTF8 : 0));
2154         NEXTVAL_NEXTTOKE.opval = o;
2155         force_next(BAREWORD);
2156         if (kind) {
2157             o->op_private = OPpCONST_ENTERED;
2158             /* XXX see note in pp_entereval() for why we forgo typo
2159                warnings if the symbol must be introduced in an eval.
2160                GSAR 96-10-12 */
2161             gv_fetchpvn_flags(s, len,
2162                               (PL_in_eval ? GV_ADDMULTI
2163                               : GV_ADD) | ( UTF ? SVf_UTF8 : 0 ),
2164                               kind == '$' ? SVt_PV :
2165                               kind == '@' ? SVt_PVAV :
2166                               kind == '%' ? SVt_PVHV :
2167                               SVt_PVGV
2168                               );
2169         }
2170     }
2171 }
2172
2173 static void
2174 S_force_ident_maybe_lex(pTHX_ char pit)
2175 {
2176     NEXTVAL_NEXTTOKE.ival = pit;
2177     force_next('p');
2178 }
2179
2180 NV
2181 Perl_str_to_version(pTHX_ SV *sv)
2182 {
2183     NV retval = 0.0;
2184     NV nshift = 1.0;
2185     STRLEN len;
2186     const char *start = SvPV_const(sv,len);
2187     const char * const end = start + len;
2188     const bool utf = cBOOL(SvUTF8(sv));
2189
2190     PERL_ARGS_ASSERT_STR_TO_VERSION;
2191
2192     while (start < end) {
2193         STRLEN skip;
2194         UV n;
2195         if (utf)
2196             n = utf8n_to_uvchr((U8*)start, len, &skip, 0);
2197         else {
2198             n = *(U8*)start;
2199             skip = 1;
2200         }
2201         retval += ((NV)n)/nshift;
2202         start += skip;
2203         nshift *= 1000;
2204     }
2205     return retval;
2206 }
2207
2208 /*
2209  * S_force_version
2210  * Forces the next token to be a version number.
2211  * If the next token appears to be an invalid version number, (e.g. "v2b"),
2212  * and if "guessing" is TRUE, then no new token is created (and the caller
2213  * must use an alternative parsing method).
2214  */
2215
2216 STATIC char *
2217 S_force_version(pTHX_ char *s, int guessing)
2218 {
2219     OP *version = NULL;
2220     char *d;
2221
2222     PERL_ARGS_ASSERT_FORCE_VERSION;
2223
2224     s = skipspace(s);
2225
2226     d = s;
2227     if (*d == 'v')
2228         d++;
2229     if (isDIGIT(*d)) {
2230         while (isDIGIT(*d) || *d == '_' || *d == '.')
2231             d++;
2232         if (*d == ';' || isSPACE(*d) || *d == '{' || *d == '}' || !*d) {
2233             SV *ver;
2234             s = scan_num(s, &pl_yylval);
2235             version = pl_yylval.opval;
2236             ver = cSVOPx(version)->op_sv;
2237             if (SvPOK(ver) && !SvNIOK(ver)) {
2238                 SvUPGRADE(ver, SVt_PVNV);
2239                 SvNV_set(ver, str_to_version(ver));
2240                 SvNOK_on(ver);          /* hint that it is a version */
2241             }
2242         }
2243         else if (guessing) {
2244             return s;
2245         }
2246     }
2247
2248     /* NOTE: The parser sees the package name and the VERSION swapped */
2249     NEXTVAL_NEXTTOKE.opval = version;
2250     force_next(BAREWORD);
2251
2252     return s;
2253 }
2254
2255 /*
2256  * S_force_strict_version
2257  * Forces the next token to be a version number using strict syntax rules.
2258  */
2259
2260 STATIC char *
2261 S_force_strict_version(pTHX_ char *s)
2262 {
2263     OP *version = NULL;
2264     const char *errstr = NULL;
2265
2266     PERL_ARGS_ASSERT_FORCE_STRICT_VERSION;
2267
2268     while (isSPACE(*s)) /* leading whitespace */
2269         s++;
2270
2271     if (is_STRICT_VERSION(s,&errstr)) {
2272         SV *ver = newSV(0);
2273         s = (char *)scan_version(s, ver, 0);
2274         version = newSVOP(OP_CONST, 0, ver);
2275     }
2276     else if ((*s != ';' && *s != '{' && *s != '}' )
2277              && (s = skipspace(s), (*s != ';' && *s != '{' && *s != '}' )))
2278     {
2279         PL_bufptr = s;
2280         if (errstr)
2281             yyerror(errstr); /* version required */
2282         return s;
2283     }
2284
2285     /* NOTE: The parser sees the package name and the VERSION swapped */
2286     NEXTVAL_NEXTTOKE.opval = version;
2287     force_next(BAREWORD);
2288
2289     return s;
2290 }
2291
2292 /*
2293  * S_tokeq
2294  * Turns any \\ into \ in a quoted string passed in in 'sv', returning 'sv',
2295  * modified as necessary.  However, if HINT_NEW_STRING is on, 'sv' is
2296  * unchanged, and a new SV containing the modified input is returned.
2297  */
2298
2299 STATIC SV *
2300 S_tokeq(pTHX_ SV *sv)
2301 {
2302     char *s;
2303     char *send;
2304     char *d;
2305     SV *pv = sv;
2306
2307     PERL_ARGS_ASSERT_TOKEQ;
2308
2309     assert (SvPOK(sv));
2310     assert (SvLEN(sv));
2311     assert (!SvIsCOW(sv));
2312     if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1) /* <<'heredoc' */
2313         goto finish;
2314     s = SvPVX(sv);
2315     send = SvEND(sv);
2316     /* This is relying on the SV being "well formed" with a trailing '\0'  */
2317     while (s < send && !(*s == '\\' && s[1] == '\\'))
2318         s++;
2319     if (s == send)
2320         goto finish;
2321     d = s;
2322     if ( PL_hints & HINT_NEW_STRING ) {
2323         pv = newSVpvn_flags(SvPVX_const(pv), SvCUR(sv),
2324                             SVs_TEMP | SvUTF8(sv));
2325     }
2326     while (s < send) {
2327         if (*s == '\\') {
2328             if (s + 1 < send && (s[1] == '\\'))
2329                 s++;            /* all that, just for this */
2330         }
2331         *d++ = *s++;
2332     }
2333     *d = '\0';
2334     SvCUR_set(sv, d - SvPVX_const(sv));
2335   finish:
2336     if ( PL_hints & HINT_NEW_STRING )
2337        return new_constant(NULL, 0, "q", sv, pv, "q", 1, NULL);
2338     return sv;
2339 }
2340
2341 /*
2342  * Now come three functions related to double-quote context,
2343  * S_sublex_start, S_sublex_push, and S_sublex_done.  They're used when
2344  * converting things like "\u\Lgnat" into ucfirst(lc("gnat")).  They
2345  * interact with PL_lex_state, and create fake ( ... ) argument lists
2346  * to handle functions and concatenation.
2347  * For example,
2348  *   "foo\lbar"
2349  * is tokenised as
2350  *    stringify ( const[foo] concat lcfirst ( const[bar] ) )
2351  */
2352
2353 /*
2354  * S_sublex_start
2355  * Assumes that pl_yylval.ival is the op we're creating (e.g. OP_LCFIRST).
2356  *
2357  * Pattern matching will set PL_lex_op to the pattern-matching op to
2358  * make (we return THING if pl_yylval.ival is OP_NULL, PMFUNC otherwise).
2359  *
2360  * OP_CONST is easy--just make the new op and return.
2361  *
2362  * Everything else becomes a FUNC.
2363  *
2364  * Sets PL_lex_state to LEX_INTERPPUSH unless ival was OP_NULL or we
2365  * had an OP_CONST.  This just sets us up for a
2366  * call to S_sublex_push().
2367  */
2368
2369 STATIC I32
2370 S_sublex_start(pTHX)
2371 {
2372     const I32 op_type = pl_yylval.ival;
2373
2374     if (op_type == OP_NULL) {
2375         pl_yylval.opval = PL_lex_op;
2376         PL_lex_op = NULL;
2377         return THING;
2378     }
2379     if (op_type == OP_CONST) {
2380         SV *sv = PL_lex_stuff;
2381         PL_lex_stuff = NULL;
2382         sv = tokeq(sv);
2383
2384         if (SvTYPE(sv) == SVt_PVIV) {
2385             /* Overloaded constants, nothing fancy: Convert to SVt_PV: */
2386             STRLEN len;
2387             const char * const p = SvPV_const(sv, len);
2388             SV * const nsv = newSVpvn_flags(p, len, SvUTF8(sv));
2389             SvREFCNT_dec(sv);
2390             sv = nsv;
2391         }
2392         pl_yylval.opval = newSVOP(op_type, 0, sv);
2393         return THING;
2394     }
2395
2396     PL_parser->lex_super_state = PL_lex_state;
2397     PL_parser->lex_sub_inwhat = (U16)op_type;
2398     PL_parser->lex_sub_op = PL_lex_op;
2399     PL_parser->sub_no_recover = FALSE;
2400     PL_parser->sub_error_count = PL_error_count;
2401     PL_lex_state = LEX_INTERPPUSH;
2402
2403     PL_expect = XTERM;
2404     if (PL_lex_op) {
2405         pl_yylval.opval = PL_lex_op;
2406         PL_lex_op = NULL;
2407         return PMFUNC;
2408     }
2409     else
2410         return FUNC;
2411 }
2412
2413 /*
2414  * S_sublex_push
2415  * Create a new scope to save the lexing state.  The scope will be
2416  * ended in S_sublex_done.  Returns a '(', starting the function arguments
2417  * to the uc, lc, etc. found before.
2418  * Sets PL_lex_state to LEX_INTERPCONCAT.
2419  */
2420
2421 STATIC I32
2422 S_sublex_push(pTHX)
2423 {
2424     LEXSHARED *shared;
2425     const bool is_heredoc = PL_multi_close == '<';
2426     ENTER;
2427
2428     PL_lex_state = PL_parser->lex_super_state;
2429     SAVEI8(PL_lex_dojoin);
2430     SAVEI32(PL_lex_brackets);
2431     SAVEI32(PL_lex_allbrackets);
2432     SAVEI32(PL_lex_formbrack);
2433     SAVEI8(PL_lex_fakeeof);
2434     SAVEI32(PL_lex_casemods);
2435     SAVEI32(PL_lex_starts);
2436     SAVEI8(PL_lex_state);
2437     SAVESPTR(PL_lex_repl);
2438     SAVEVPTR(PL_lex_inpat);
2439     SAVEI16(PL_lex_inwhat);
2440     if (is_heredoc)
2441     {
2442         SAVECOPLINE(PL_curcop);
2443         SAVEI32(PL_multi_end);
2444         SAVEI32(PL_parser->herelines);
2445         PL_parser->herelines = 0;
2446     }
2447     SAVEIV(PL_multi_close);
2448     SAVEPPTR(PL_bufptr);
2449     SAVEPPTR(PL_bufend);
2450     SAVEPPTR(PL_oldbufptr);
2451     SAVEPPTR(PL_oldoldbufptr);
2452     SAVEPPTR(PL_last_lop);
2453     SAVEPPTR(PL_last_uni);
2454     SAVEPPTR(PL_linestart);
2455     SAVESPTR(PL_linestr);
2456     SAVEGENERICPV(PL_lex_brackstack);
2457     SAVEGENERICPV(PL_lex_casestack);
2458     SAVEGENERICPV(PL_parser->lex_shared);
2459     SAVEBOOL(PL_parser->lex_re_reparsing);
2460     SAVEI32(PL_copline);
2461
2462     /* The here-doc parser needs to be able to peek into outer lexing
2463        scopes to find the body of the here-doc.  So we put PL_linestr and
2464        PL_bufptr into lex_shared, to â€˜share’ those values.
2465      */
2466     PL_parser->lex_shared->ls_linestr = PL_linestr;
2467     PL_parser->lex_shared->ls_bufptr  = PL_bufptr;
2468
2469     PL_linestr = PL_lex_stuff;
2470     PL_lex_repl = PL_parser->lex_sub_repl;
2471     PL_lex_stuff = NULL;
2472     PL_parser->lex_sub_repl = NULL;
2473
2474     /* Arrange for PL_lex_stuff to be freed on scope exit, in case it gets
2475        set for an inner quote-like operator and then an error causes scope-
2476        popping.  We must not have a PL_lex_stuff value left dangling, as
2477        that breaks assumptions elsewhere.  See bug #123617.  */
2478     SAVEGENERICSV(PL_lex_stuff);
2479     SAVEGENERICSV(PL_parser->lex_sub_repl);
2480
2481     PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart
2482         = SvPVX(PL_linestr);
2483     PL_bufend += SvCUR(PL_linestr);
2484     PL_last_lop = PL_last_uni = NULL;
2485     SAVEFREESV(PL_linestr);
2486     if (PL_lex_repl) SAVEFREESV(PL_lex_repl);
2487
2488     PL_lex_dojoin = FALSE;
2489     PL_lex_brackets = PL_lex_formbrack = 0;
2490     PL_lex_allbrackets = 0;
2491     PL_lex_fakeeof = LEX_FAKEEOF_NEVER;
2492     Newx(PL_lex_brackstack, 120, char);
2493     Newx(PL_lex_casestack, 12, char);
2494     PL_lex_casemods = 0;
2495     *PL_lex_casestack = '\0';
2496     PL_lex_starts = 0;
2497     PL_lex_state = LEX_INTERPCONCAT;
2498     if (is_heredoc)
2499         CopLINE_set(PL_curcop, (line_t)PL_multi_start);
2500     PL_copline = NOLINE;
2501
2502     Newxz(shared, 1, LEXSHARED);
2503     shared->ls_prev = PL_parser->lex_shared;
2504     PL_parser->lex_shared = shared;
2505
2506     PL_lex_inwhat = PL_parser->lex_sub_inwhat;
2507     if (PL_lex_inwhat == OP_TRANSR) PL_lex_inwhat = OP_TRANS;
2508     if (PL_lex_inwhat == OP_MATCH || PL_lex_inwhat == OP_QR || PL_lex_inwhat == OP_SUBST)
2509         PL_lex_inpat = PL_parser->lex_sub_op;
2510     else
2511         PL_lex_inpat = NULL;
2512
2513     PL_parser->lex_re_reparsing = cBOOL(PL_in_eval & EVAL_RE_REPARSING);
2514     PL_in_eval &= ~EVAL_RE_REPARSING;
2515
2516     return SUBLEXSTART;
2517 }
2518
2519 /*
2520  * S_sublex_done
2521  * Restores lexer state after a S_sublex_push.
2522  */
2523
2524 STATIC I32
2525 S_sublex_done(pTHX)
2526 {
2527     if (!PL_lex_starts++) {
2528         SV * const sv = newSVpvs("");
2529         if (SvUTF8(PL_linestr))
2530             SvUTF8_on(sv);
2531         PL_expect = XOPERATOR;
2532         pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
2533         return THING;
2534     }
2535
2536     if (PL_lex_casemods) {              /* oops, we've got some unbalanced parens */
2537         PL_lex_state = LEX_INTERPCASEMOD;
2538         return yylex();
2539     }
2540
2541     /* Is there a right-hand side to take care of? (s//RHS/ or tr//RHS/) */
2542     assert(PL_lex_inwhat != OP_TRANSR);
2543     if (PL_lex_repl) {
2544         assert (PL_lex_inwhat == OP_SUBST || PL_lex_inwhat == OP_TRANS);
2545         PL_linestr = PL_lex_repl;
2546         PL_lex_inpat = 0;
2547         PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
2548         PL_bufend += SvCUR(PL_linestr);
2549         PL_last_lop = PL_last_uni = NULL;
2550         PL_lex_dojoin = FALSE;
2551         PL_lex_brackets = 0;
2552         PL_lex_allbrackets = 0;
2553         PL_lex_fakeeof = LEX_FAKEEOF_NEVER;
2554         PL_lex_casemods = 0;
2555         *PL_lex_casestack = '\0';
2556         PL_lex_starts = 0;
2557         if (SvEVALED(PL_lex_repl)) {
2558             PL_lex_state = LEX_INTERPNORMAL;
2559             PL_lex_starts++;
2560             /*  we don't clear PL_lex_repl here, so that we can check later
2561                 whether this is an evalled subst; that means we rely on the
2562                 logic to ensure sublex_done() is called again only via the
2563                 branch (in yylex()) that clears PL_lex_repl, else we'll loop */
2564         }
2565         else {
2566             PL_lex_state = LEX_INTERPCONCAT;
2567             PL_lex_repl = NULL;
2568         }
2569         if (SvTYPE(PL_linestr) >= SVt_PVNV) {
2570             CopLINE(PL_curcop) +=
2571                 ((XPVNV*)SvANY(PL_linestr))->xnv_u.xnv_lines
2572                  + PL_parser->herelines;
2573             PL_parser->herelines = 0;
2574         }
2575         return '/';
2576     }
2577     else {
2578         const line_t l = CopLINE(PL_curcop);
2579         LEAVE;
2580         if (PL_parser->sub_error_count != PL_error_count) {
2581             if (PL_parser->sub_no_recover) {
2582                 yyquit();
2583                 NOT_REACHED;
2584             }
2585         }
2586         if (PL_multi_close == '<')
2587             PL_parser->herelines += l - PL_multi_end;
2588         PL_bufend = SvPVX(PL_linestr);
2589         PL_bufend += SvCUR(PL_linestr);
2590         PL_expect = XOPERATOR;
2591         return SUBLEXEND;
2592     }
2593 }
2594
2595 HV *
2596 Perl_load_charnames(pTHX_ SV * char_name, const char * context,
2597                           const STRLEN context_len, const char ** error_msg)
2598 {
2599     /* Load the official _charnames module if not already there.  The
2600      * parameters are just to give info for any error messages generated:
2601      *  char_name   a name to look up which is the reason for loading this
2602      *  context     'char_name' in the context in the input in which it appears
2603      *  context_len how many bytes 'context' occupies
2604      *  error_msg   *error_msg will be set to any error
2605      *
2606      *  Returns the ^H table if success; otherwise NULL */
2607
2608     unsigned int i;
2609     HV * table;
2610     SV **cvp;
2611     SV * res;
2612
2613     PERL_ARGS_ASSERT_LOAD_CHARNAMES;
2614
2615     /* This loop is executed 1 1/2 times.  On the first time through, if it
2616      * isn't already loaded, try loading it, and iterate just once to see if it
2617      * worked.  */
2618     for (i = 0; i < 2; i++) {
2619         table = GvHV(PL_hintgv);                 /* ^H */
2620
2621         if (    table
2622             && (PL_hints & HINT_LOCALIZE_HH)
2623             && (cvp = hv_fetchs(table, "charnames", FALSE))
2624             &&  SvOK(*cvp))
2625         {
2626             return table;   /* Quit if already loaded */
2627         }
2628
2629         if (i == 0) {
2630             Perl_load_module(aTHX_
2631                 0,
2632                 newSVpvs("_charnames"),
2633
2634                 /* version parameter; no need to specify it, as if we get too early
2635                 * a version, will fail anyway, not being able to find 'charnames'
2636                 * */
2637                 NULL,
2638                 newSVpvs(":full"),
2639                 newSVpvs(":short"),
2640                 NULL);
2641         }
2642     }
2643
2644     /* Here, it failed; new_constant will give appropriate error messages */
2645     *error_msg = NULL;
2646     res = new_constant( NULL, 0, "charnames", char_name, NULL,
2647                         context, context_len, error_msg);
2648     SvREFCNT_dec(res);
2649
2650     return NULL;
2651 }
2652
2653 STATIC SV*
2654 S_get_and_check_backslash_N_name_wrapper(pTHX_ const char* s, const char* const e)
2655 {
2656     /* This justs wraps get_and_check_backslash_N_name() to output any error
2657      * message it returns. */
2658
2659     const char * error_msg = NULL;
2660     SV * result;
2661
2662     PERL_ARGS_ASSERT_GET_AND_CHECK_BACKSLASH_N_NAME_WRAPPER;
2663
2664     /* charnames doesn't work well if there have been errors found */
2665     if (PL_error_count > 0) {
2666         return NULL;
2667     }
2668
2669     result = get_and_check_backslash_N_name(s, e, cBOOL(UTF), &error_msg);
2670
2671     if (error_msg) {
2672         yyerror_pv(error_msg, UTF ? SVf_UTF8 : 0);
2673     }
2674
2675     return result;
2676 }
2677
2678 SV*
2679 Perl_get_and_check_backslash_N_name(pTHX_ const char* s,
2680                                           const char* const e,
2681                                           const bool is_utf8,
2682                                           const char ** error_msg)
2683 {
2684     /* <s> points to first character of interior of \N{}, <e> to one beyond the
2685      * interior, hence to the "}".  Finds what the name resolves to, returning
2686      * an SV* containing it; NULL if no valid one found.
2687      *
2688      * 'is_utf8' is TRUE if we know we want the result to be UTF-8 even if it
2689      * doesn't have to be. */
2690
2691     SV* char_name;
2692     SV* res;
2693     HV * table;
2694     SV **cvp;
2695     SV *cv;
2696     SV *rv;
2697     HV *stash;
2698
2699     /* Points to the beginning of the \N{... so that any messages include the
2700      * context of what's failing*/
2701     const char* context = s - 3;
2702     STRLEN context_len = e - context + 1; /* include all of \N{...} */
2703
2704     dVAR;
2705
2706     PERL_ARGS_ASSERT_GET_AND_CHECK_BACKSLASH_N_NAME;
2707
2708     assert(e >= s);
2709     assert(s > (char *) 3);
2710
2711     char_name = newSVpvn_flags(s, e - s, (is_utf8) ? SVf_UTF8 : 0);
2712
2713     if (!SvCUR(char_name)) {
2714         SvREFCNT_dec_NN(char_name);
2715         /* diag_listed_as: Unknown charname '%s' */
2716         *error_msg = Perl_form(aTHX_ "Unknown charname ''");
2717         return NULL;
2718     }
2719
2720     /* Autoload the charnames module */
2721
2722     table = load_charnames(char_name, context, context_len, error_msg);
2723     if (table == NULL) {
2724         return NULL;
2725     }
2726
2727     *error_msg = NULL;
2728     res = new_constant( NULL, 0, "charnames", char_name, NULL,
2729                         context, context_len, error_msg);
2730     if (*error_msg) {
2731         *error_msg = Perl_form(aTHX_ "Unknown charname '%s'", SvPVX(char_name));
2732
2733         SvREFCNT_dec(res);
2734         return NULL;
2735     }
2736
2737     /* See if the charnames handler is the Perl core's, and if so, we can skip
2738      * the validation needed for a user-supplied one, as Perl's does its own
2739      * validation. */
2740     cvp = hv_fetchs(table, "charnames", FALSE);
2741     if (cvp && (cv = *cvp) && SvROK(cv) && (rv = SvRV(cv),
2742         SvTYPE(rv) == SVt_PVCV) && ((stash = CvSTASH(rv)) != NULL))
2743     {
2744         const char * const name = HvNAME(stash);
2745          if (memEQs(name, HvNAMELEN(stash), "_charnames")) {
2746            return res;
2747        }
2748     }
2749
2750     /* Here, it isn't Perl's charname handler.  We can't rely on a
2751      * user-supplied handler to validate the input name.  For non-ut8 input,
2752      * look to see that the first character is legal.  Then loop through the
2753      * rest checking that each is a continuation */
2754
2755     /* This code makes the reasonable assumption that the only Latin1-range
2756      * characters that begin a character name alias are alphabetic, otherwise
2757      * would have to create a isCHARNAME_BEGIN macro */
2758
2759     if (! is_utf8) {
2760         if (! isALPHAU(*s)) {
2761             goto bad_charname;
2762         }
2763         s++;
2764         while (s < e) {
2765             if (! isCHARNAME_CONT(*s)) {
2766                 goto bad_charname;
2767             }
2768             if (*s == ' ' && *(s-1) == ' ') {
2769                 goto multi_spaces;
2770             }
2771             s++;
2772         }
2773     }
2774     else {
2775         /* Similarly for utf8.  For invariants can check directly; for other
2776          * Latin1, can calculate their code point and check; otherwise  use an
2777          * inversion list */
2778         if (UTF8_IS_INVARIANT(*s)) {
2779             if (! isALPHAU(*s)) {
2780                 goto bad_charname;
2781             }
2782             s++;
2783         } else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
2784             if (! isALPHAU(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s+1)))) {
2785                 goto bad_charname;
2786             }
2787             s += 2;
2788         }
2789         else {
2790             if (! _invlist_contains_cp(PL_utf8_charname_begin,
2791                                        utf8_to_uvchr_buf((U8 *) s,
2792                                                          (U8 *) e,
2793                                                          NULL)))
2794             {
2795                 goto bad_charname;
2796             }
2797             s += UTF8SKIP(s);
2798         }
2799
2800         while (s < e) {
2801             if (UTF8_IS_INVARIANT(*s)) {
2802                 if (! isCHARNAME_CONT(*s)) {
2803                     goto bad_charname;
2804                 }
2805                 if (*s == ' ' && *(s-1) == ' ') {
2806                     goto multi_spaces;
2807                 }
2808                 s++;
2809             }
2810             else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
2811                 if (! isCHARNAME_CONT(EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s+1))))
2812                 {
2813                     goto bad_charname;
2814                 }
2815                 s += 2;
2816             }
2817             else {
2818                 if (! _invlist_contains_cp(PL_utf8_charname_continue,
2819                                            utf8_to_uvchr_buf((U8 *) s,
2820                                                              (U8 *) e,
2821                                                              NULL)))
2822                 {
2823                     goto bad_charname;
2824                 }
2825                 s += UTF8SKIP(s);
2826             }
2827         }
2828     }
2829     if (*(s-1) == ' ') {
2830         /* diag_listed_as: charnames alias definitions may not contain
2831                            trailing white-space; marked by <-- HERE in %s
2832          */
2833         *error_msg = Perl_form(aTHX_
2834             "charnames alias definitions may not contain trailing "
2835             "white-space; marked by <-- HERE in %.*s<-- HERE %.*s",
2836             (int)(s - context + 1), context,
2837             (int)(e - s + 1), s + 1);
2838         return NULL;
2839     }
2840
2841     if (SvUTF8(res)) { /* Don't accept malformed charname value */
2842         const U8* first_bad_char_loc;
2843         STRLEN len;
2844         const char* const str = SvPV_const(res, len);
2845         if (UNLIKELY(! is_utf8_string_loc((U8 *) str, len,
2846                                           &first_bad_char_loc)))
2847         {
2848             _force_out_malformed_utf8_message(first_bad_char_loc,
2849                                               (U8 *) PL_parser->bufend,
2850                                               0,
2851                                               0 /* 0 means don't die */ );
2852             /* diag_listed_as: Malformed UTF-8 returned by \N{%s}
2853                                immediately after '%s' */
2854             *error_msg = Perl_form(aTHX_
2855                 "Malformed UTF-8 returned by %.*s immediately after '%.*s'",
2856                  (int) context_len, context,
2857                  (int) ((char *) first_bad_char_loc - str), str);
2858             return NULL;
2859         }
2860     }
2861
2862     return res;
2863
2864   bad_charname: {
2865
2866         /* The final %.*s makes sure that should the trailing NUL be missing
2867          * that this print won't run off the end of the string */
2868         /* diag_listed_as: Invalid character in \N{...}; marked by <-- HERE
2869                            in \N{%s} */
2870         *error_msg = Perl_form(aTHX_
2871             "Invalid character in \\N{...}; marked by <-- HERE in %.*s<-- HERE %.*s",
2872             (int)(s - context + 1), context,
2873             (int)(e - s + 1), s + 1);
2874         return NULL;
2875     }
2876
2877   multi_spaces:
2878         /* diag_listed_as: charnames alias definitions may not contain a
2879                            sequence of multiple spaces; marked by <-- HERE
2880                            in %s */
2881         *error_msg = Perl_form(aTHX_
2882             "charnames alias definitions may not contain a sequence of "
2883             "multiple spaces; marked by <-- HERE in %.*s<-- HERE %.*s",
2884             (int)(s - context + 1), context,
2885             (int)(e - s + 1), s + 1);
2886         return NULL;
2887 }
2888
2889 /*
2890   scan_const
2891
2892   Extracts the next constant part of a pattern, double-quoted string,
2893   or transliteration.  This is terrifying code.
2894
2895   For example, in parsing the double-quoted string "ab\x63$d", it would
2896   stop at the '$' and return an OP_CONST containing 'abc'.
2897
2898   It looks at PL_lex_inwhat and PL_lex_inpat to find out whether it's
2899   processing a pattern (PL_lex_inpat is true), a transliteration
2900   (PL_lex_inwhat == OP_TRANS is true), or a double-quoted string.
2901
2902   Returns a pointer to the character scanned up to. If this is
2903   advanced from the start pointer supplied (i.e. if anything was
2904   successfully parsed), will leave an OP_CONST for the substring scanned
2905   in pl_yylval. Caller must intuit reason for not parsing further
2906   by looking at the next characters herself.
2907
2908   In patterns:
2909     expand:
2910       \N{FOO}  => \N{U+hex_for_character_FOO}
2911       (if FOO expands to multiple characters, expands to \N{U+xx.XX.yy ...})
2912
2913     pass through:
2914         all other \-char, including \N and \N{ apart from \N{ABC}
2915
2916     stops on:
2917         @ and $ where it appears to be a var, but not for $ as tail anchor
2918         \l \L \u \U \Q \E
2919         (?{  or  (??{
2920
2921   In transliterations:
2922     characters are VERY literal, except for - not at the start or end
2923     of the string, which indicates a range.  However some backslash sequences
2924     are recognized: \r, \n, and the like
2925                     \007 \o{}, \x{}, \N{}
2926     If all elements in the transliteration are below 256,
2927     scan_const expands the range to the full set of intermediate
2928     characters. If the range is in utf8, the hyphen is replaced with
2929     a certain range mark which will be handled by pmtrans() in op.c.
2930
2931   In double-quoted strings:
2932     backslashes:
2933       all those recognized in transliterations
2934       deprecated backrefs: \1 (in substitution replacements)
2935       case and quoting: \U \Q \E
2936     stops on @ and $
2937
2938   scan_const does *not* construct ops to handle interpolated strings.
2939   It stops processing as soon as it finds an embedded $ or @ variable
2940   and leaves it to the caller to work out what's going on.
2941
2942   embedded arrays (whether in pattern or not) could be:
2943       @foo, @::foo, @'foo, @{foo}, @$foo, @+, @-.
2944
2945   $ in double-quoted strings must be the symbol of an embedded scalar.
2946
2947   $ in pattern could be $foo or could be tail anchor.  Assumption:
2948   it's a tail anchor if $ is the last thing in the string, or if it's
2949   followed by one of "()| \r\n\t"
2950
2951   \1 (backreferences) are turned into $1 in substitutions
2952
2953   The structure of the code is
2954       while (there's a character to process) {
2955           handle transliteration ranges
2956           skip regexp comments /(?#comment)/ and codes /(?{code})/
2957           skip #-initiated comments in //x patterns
2958           check for embedded arrays
2959           check for embedded scalars
2960           if (backslash) {
2961               deprecate \1 in substitution replacements
2962               handle string-changing backslashes \l \U \Q \E, etc.
2963               switch (what was escaped) {
2964                   handle \- in a transliteration (becomes a literal -)
2965                   if a pattern and not \N{, go treat as regular character
2966                   handle \132 (octal characters)
2967                   handle \x15 and \x{1234} (hex characters)
2968                   handle \N{name} (named characters, also \N{3,5} in a pattern)
2969                   handle \cV (control characters)
2970                   handle printf-style backslashes (\f, \r, \n, etc)
2971               } (end switch)
2972               continue
2973           } (end if backslash)
2974           handle regular character
2975     } (end while character to read)
2976
2977 */
2978
2979 STATIC char *
2980 S_scan_const(pTHX_ char *start)
2981 {
2982     char *send = PL_bufend;             /* end of the constant */
2983     SV *sv = newSV(send - start);       /* sv for the constant.  See note below
2984                                            on sizing. */
2985     char *s = start;                    /* start of the constant */
2986     char *d = SvPVX(sv);                /* destination for copies */
2987     bool dorange = FALSE;               /* are we in a translit range? */
2988     bool didrange = FALSE;              /* did we just finish a range? */
2989     bool in_charclass = FALSE;          /* within /[...]/ */
2990     bool s_is_utf8 = cBOOL(UTF);        /* Is the source string assumed to be
2991                                            UTF8?  But, this can show as true
2992                                            when the source isn't utf8, as for
2993                                            example when it is entirely composed
2994                                            of hex constants */
2995     bool d_is_utf8 = FALSE;             /* Output constant is UTF8 */
2996     STRLEN utf8_variant_count = 0;      /* When not in UTF-8, this counts the
2997                                            number of characters found so far
2998                                            that will expand (into 2 bytes)
2999                                            should we have to convert to
3000                                            UTF-8) */
3001     SV *res;                            /* result from charnames */
3002     STRLEN offset_to_max = 0;   /* The offset in the output to where the range
3003                                    high-end character is temporarily placed */
3004
3005     /* Does something require special handling in tr/// ?  This avoids extra
3006      * work in a less likely case.  As such, khw didn't feel it was worth
3007      * adding any branches to the more mainline code to handle this, which
3008      * means that this doesn't get set in some circumstances when things like
3009      * \x{100} get expanded out.  As a result there needs to be extra testing
3010      * done in the tr code */
3011     bool has_above_latin1 = FALSE;
3012
3013     /* Note on sizing:  The scanned constant is placed into sv, which is
3014      * initialized by newSV() assuming one byte of output for every byte of
3015      * input.  This routine expects newSV() to allocate an extra byte for a
3016      * trailing NUL, which this routine will append if it gets to the end of
3017      * the input.  There may be more bytes of input than output (eg., \N{LATIN
3018      * CAPITAL LETTER A}), or more output than input if the constant ends up
3019      * recoded to utf8, but each time a construct is found that might increase
3020      * the needed size, SvGROW() is called.  Its size parameter each time is
3021      * based on the best guess estimate at the time, namely the length used so
3022      * far, plus the length the current construct will occupy, plus room for
3023      * the trailing NUL, plus one byte for every input byte still unscanned */
3024
3025     UV uv = UV_MAX; /* Initialize to weird value to try to catch any uses
3026                        before set */
3027 #ifdef EBCDIC
3028     int backslash_N = 0;            /* ? was the character from \N{} */
3029     int non_portable_endpoint = 0;  /* ? In a range is an endpoint
3030                                        platform-specific like \x65 */
3031 #endif
3032
3033     PERL_ARGS_ASSERT_SCAN_CONST;
3034
3035     assert(PL_lex_inwhat != OP_TRANSR);
3036
3037     /* Protect sv from errors and fatal warnings. */
3038     ENTER_with_name("scan_const");
3039     SAVEFREESV(sv);
3040
3041     /* A bunch of code in the loop below assumes that if s[n] exists and is not
3042      * NUL, then s[n+1] exists.  This assertion makes sure that assumption is
3043      * valid */
3044     assert(*send == '\0');
3045
3046     while (s < send
3047            || dorange   /* Handle tr/// range at right edge of input */
3048     ) {
3049
3050         /* get transliterations out of the way (they're most literal) */
3051         if (PL_lex_inwhat == OP_TRANS) {
3052
3053             /* But there isn't any special handling necessary unless there is a
3054              * range, so for most cases we just drop down and handle the value
3055              * as any other.  There are two exceptions.
3056              *
3057              * 1.  A hyphen indicates that we are actually going to have a
3058              *     range.  In this case, skip the '-', set a flag, then drop
3059              *     down to handle what should be the end range value.
3060              * 2.  After we've handled that value, the next time through, that
3061              *     flag is set and we fix up the range.
3062              *
3063              * Ranges entirely within Latin1 are expanded out entirely, in
3064              * order to make the transliteration a simple table look-up.
3065              * Ranges that extend above Latin1 have to be done differently, so
3066              * there is no advantage to expanding them here, so they are
3067              * stored here as Min, RANGE_INDICATOR, Max.  'RANGE_INDICATOR' is
3068              * a byte that can't occur in legal UTF-8, and hence can signify a
3069              * hyphen without any possible ambiguity.  On EBCDIC machines, if
3070              * the range is expressed as Unicode, the Latin1 portion is
3071              * expanded out even if the range extends above Latin1.  This is
3072              * because each code point in it has to be processed here
3073              * individually to get its native translation */
3074
3075             if (! dorange) {
3076
3077                 /* Here, we don't think we're in a range.  If the new character
3078                  * is not a hyphen; or if it is a hyphen, but it's too close to
3079                  * either edge to indicate a range, or if we haven't output any
3080                  * characters yet then it's a regular character. */
3081                 if (*s != '-' || s >= send - 1 || s == start || d == SvPVX(sv))
3082                 {
3083
3084                     /* A regular character.  Process like any other, but first
3085                      * clear any flags */
3086                     didrange = FALSE;
3087                     dorange = FALSE;
3088 #ifdef EBCDIC
3089                     non_portable_endpoint = 0;
3090                     backslash_N = 0;
3091 #endif
3092                     /* The tests here for being above Latin1 and similar ones
3093                      * in the following 'else' suffice to find all such
3094                      * occurences in the constant, except those added by a
3095                      * backslash escape sequence, like \x{100}.  Mostly, those
3096                      * set 'has_above_latin1' as appropriate */
3097                     if (s_is_utf8 && UTF8_IS_ABOVE_LATIN1(*s)) {
3098                         has_above_latin1 = TRUE;
3099                     }
3100
3101                     /* Drops down to generic code to process current byte */
3102                 }
3103                 else {  /* Is a '-' in the context where it means a range */
3104                     if (didrange) { /* Something like y/A-C-Z// */
3105                         Perl_croak(aTHX_ "Ambiguous range in transliteration"
3106                                          " operator");
3107                     }
3108
3109                     dorange = TRUE;
3110
3111                     s++;    /* Skip past the hyphen */
3112
3113                     /* d now points to where the end-range character will be
3114                      * placed.  Drop down to get that character.  We'll finish
3115                      * processing the range the next time through the loop */
3116
3117                     if (s_is_utf8 && UTF8_IS_ABOVE_LATIN1(*s)) {
3118                         has_above_latin1 = TRUE;
3119                     }
3120
3121                     /* Drops down to generic code to process current byte */
3122                 }
3123             }  /* End of not a range */
3124             else {
3125                 /* Here we have parsed a range.  Now must handle it.  At this
3126                  * point:
3127                  * 'sv' is a SV* that contains the output string we are
3128                  *      constructing.  The final two characters in that string
3129                  *      are the range start and range end, in order.
3130                  * 'd'  points to just beyond the range end in the 'sv' string,
3131                  *      where we would next place something
3132                  */
3133                 char * max_ptr;
3134                 char * min_ptr;
3135                 IV range_min;
3136                 IV range_max;   /* last character in range */
3137                 STRLEN grow;
3138                 Size_t offset_to_min = 0;
3139                 Size_t extras = 0;
3140 #ifdef EBCDIC
3141                 bool convert_unicode;
3142                 IV real_range_max = 0;
3143 #endif
3144                 /* Get the code point values of the range ends. */
3145                 max_ptr = (d_is_utf8) ? (char *) utf8_hop( (U8*) d, -1) : d - 1;
3146                 offset_to_max = max_ptr - SvPVX_const(sv);
3147                 if (d_is_utf8) {
3148                     /* We know the utf8 is valid, because we just constructed
3149                      * it ourselves in previous loop iterations */
3150                     min_ptr = (char*) utf8_hop( (U8*) max_ptr, -1);
3151                     range_min = valid_utf8_to_uvchr( (U8*) min_ptr, NULL);
3152                     range_max = valid_utf8_to_uvchr( (U8*) max_ptr, NULL);
3153
3154                     /* This compensates for not all code setting
3155                      * 'has_above_latin1', so that we don't skip stuff that
3156                      * should be executed */
3157                     if (range_max > 255) {
3158                         has_above_latin1 = TRUE;
3159                     }
3160                 }
3161                 else {
3162                     min_ptr = max_ptr - 1;
3163                     range_min = * (U8*) min_ptr;
3164                     range_max = * (U8*) max_ptr;
3165                 }
3166
3167                 /* If the range is just a single code point, like tr/a-a/.../,
3168                  * that code point is already in the output, twice.  We can
3169                  * just back up over the second instance and avoid all the rest
3170                  * of the work.  But if it is a variant character, it's been
3171                  * counted twice, so decrement.  (This unlikely scenario is
3172                  * special cased, like the one for a range of 2 code points
3173                  * below, only because the main-line code below needs a range
3174                  * of 3 or more to work without special casing.  Might as well
3175                  * get it out of the way now.) */
3176                 if (UNLIKELY(range_max == range_min)) {
3177                     d = max_ptr;
3178                     if (! d_is_utf8 && ! UVCHR_IS_INVARIANT(range_max)) {
3179                         utf8_variant_count--;
3180                     }
3181                     goto range_done;
3182                 }
3183
3184 #ifdef EBCDIC
3185                 /* On EBCDIC platforms, we may have to deal with portable
3186                  * ranges.  These happen if at least one range endpoint is a
3187                  * Unicode value (\N{...}), or if the range is a subset of
3188                  * [A-Z] or [a-z], and both ends are literal characters,
3189                  * like 'A', and not like \x{C1} */
3190                 convert_unicode =
3191                                cBOOL(backslash_N)   /* \N{} forces Unicode,
3192                                                        hence portable range */
3193                     || (     ! non_portable_endpoint
3194                         && ((  isLOWER_A(range_min) && isLOWER_A(range_max))
3195                            || (isUPPER_A(range_min) && isUPPER_A(range_max))));
3196                 if (convert_unicode) {
3197
3198                     /* Special handling is needed for these portable ranges.
3199                      * They are defined to be in Unicode terms, which includes
3200                      * all the Unicode code points between the end points.
3201                      * Convert to Unicode to get the Unicode range.  Later we
3202                      * will convert each code point in the range back to
3203                      * native.  */
3204                     range_min = NATIVE_TO_UNI(range_min);
3205                     range_max = NATIVE_TO_UNI(range_max);
3206                 }
3207 #endif
3208
3209                 if (range_min > range_max) {
3210 #ifdef EBCDIC
3211                     if (convert_unicode) {
3212                         /* Need to convert back to native for meaningful
3213                          * messages for this platform */
3214                         range_min = UNI_TO_NATIVE(range_min);
3215                         range_max = UNI_TO_NATIVE(range_max);
3216                     }
3217 #endif
3218                     /* Use the characters themselves for the error message if
3219                      * ASCII printables; otherwise some visible representation
3220                      * of them */
3221                     if (isPRINT_A(range_min) && isPRINT_A(range_max)) {
3222                         Perl_croak(aTHX_
3223                          "Invalid range \"%c-%c\" in transliteration operator",
3224                          (char)range_min, (char)range_max);
3225                     }
3226 #ifdef EBCDIC
3227                     else if (convert_unicode) {
3228         /* diag_listed_as: Invalid range "%s" in transliteration operator */
3229                         Perl_croak(aTHX_
3230                            "Invalid range \"\\N{U+%04" UVXf "}-\\N{U+%04"
3231                            UVXf "}\" in transliteration operator",
3232                            range_min, range_max);
3233                     }
3234 #endif
3235                     else {
3236         /* diag_listed_as: Invalid range "%s" in transliteration operator */
3237                         Perl_croak(aTHX_
3238                            "Invalid range \"\\x{%04" UVXf "}-\\x{%04" UVXf "}\""
3239                            " in transliteration operator",
3240                            range_min, range_max);
3241                     }
3242                 }
3243
3244                 /* If the range is exactly two code points long, they are
3245                  * already both in the output */
3246                 if (UNLIKELY(range_min + 1 == range_max)) {
3247                     goto range_done;
3248                 }
3249
3250                 /* Here the range contains at least 3 code points */
3251
3252                 if (d_is_utf8) {
3253
3254                     /* If everything in the transliteration is below 256, we
3255                      * can avoid special handling later.  A translation table
3256                      * for each of those bytes is created by op.c.  So we
3257                      * expand out all ranges to their constituent code points.
3258                      * But if we've encountered something above 255, the
3259                      * expanding won't help, so skip doing that.  But if it's
3260                      * EBCDIC, we may have to look at each character below 256
3261                      * if we have to convert to/from Unicode values */
3262                     if (   has_above_latin1
3263 #ifdef EBCDIC
3264                         && (range_min > 255 || ! convert_unicode)
3265 #endif
3266                     ) {
3267                         const STRLEN off = d - SvPVX(sv);
3268                         const STRLEN extra = 1 + (send - s) + 1;
3269                         char *e;
3270
3271                         /* Move the high character one byte to the right; then
3272                          * insert between it and the range begin, an illegal
3273                          * byte which serves to indicate this is a range (using
3274                          * a '-' would be ambiguous). */
3275
3276                         if (off + extra > SvLEN(sv)) {
3277                             d = off + SvGROW(sv, off + extra);
3278                             max_ptr = d - off + offset_to_max;
3279                         }
3280
3281                         e = d++;
3282                         while (e-- > max_ptr) {
3283                             *(e + 1) = *e;
3284                         }
3285                         *(e + 1) = (char) RANGE_INDICATOR;
3286                         goto range_done;
3287                     }
3288
3289                     /* Here, we're going to expand out the range.  For EBCDIC
3290                      * the range can extend above 255 (not so in ASCII), so
3291                      * for EBCDIC, split it into the parts above and below
3292                      * 255/256 */
3293 #ifdef EBCDIC
3294                     if (range_max > 255) {
3295                         real_range_max = range_max;
3296                         range_max = 255;
3297                     }
3298 #endif
3299                 }
3300
3301                 /* Here we need to expand out the string to contain each
3302                  * character in the range.  Grow the output to handle this.
3303                  * For non-UTF8, we need a byte for each code point in the
3304                  * range, minus the three that we've already allocated for: the
3305                  * hyphen, the min, and the max.  For UTF-8, we need this
3306                  * plus an extra byte for each code point that occupies two
3307                  * bytes (is variant) when in UTF-8 (except we've already
3308                  * allocated for the end points, including if they are
3309                  * variants).  For ASCII platforms and Unicode ranges on EBCDIC
3310                  * platforms, it's easy to calculate a precise number.  To
3311                  * start, we count the variants in the range, which we need
3312                  * elsewhere in this function anyway.  (For the case where it
3313                  * isn't easy to calculate, 'extras' has been initialized to 0,
3314                  * and the calculation is done in a loop further down.) */
3315 #ifdef EBCDIC
3316                 if (convert_unicode)
3317 #endif
3318                 {
3319                     /* This is executed unconditionally on ASCII, and for
3320                      * Unicode ranges on EBCDIC.  Under these conditions, all
3321                      * code points above a certain value are variant; and none
3322                      * under that value are.  We just need to find out how much
3323                      * of the range is above that value.  We don't count the
3324                      * end points here, as they will already have been counted
3325                      * as they were parsed. */
3326                     if (range_min >= UTF_CONTINUATION_MARK) {
3327
3328                         /* The whole range is made up of variants */
3329                         extras = (range_max - 1) - (range_min + 1) + 1;
3330                     }
3331                     else if (range_max >= UTF_CONTINUATION_MARK) {
3332
3333                         /* Only the higher portion of the range is variants */
3334                         extras = (range_max - 1) - UTF_CONTINUATION_MARK + 1;
3335                     }
3336
3337                     utf8_variant_count += extras;
3338                 }
3339
3340                 /* The base growth is the number of code points in the range,
3341                  * not including the endpoints, which have already been sized
3342                  * for (and output).  We don't subtract for the hyphen, as it
3343                  * has been parsed but not output, and the SvGROW below is
3344                  * based only on what's been output plus what's left to parse.
3345                  * */
3346                 grow = (range_max - 1) - (range_min + 1) + 1;
3347
3348                 if (d_is_utf8) {
3349 #ifdef EBCDIC
3350                     /* In some cases in EBCDIC, we haven't yet calculated a
3351                      * precise amount needed for the UTF-8 variants.  Just
3352                      * assume the worst case, that everything will expand by a
3353                      * byte */
3354                     if (! convert_unicode) {
3355                         grow *= 2;
3356                     }
3357                     else
3358 #endif
3359                     {
3360                         /* Otherwise we know exactly how many variants there
3361                          * are in the range. */
3362                         grow += extras;
3363                     }
3364                 }
3365
3366                 /* Grow, but position the output to overwrite the range min end
3367                  * point, because in some cases we overwrite that */
3368                 SvCUR_set(sv, d - SvPVX_const(sv));
3369                 offset_to_min = min_ptr - SvPVX_const(sv);
3370
3371                 /* See Note on sizing above. */
3372                 d = offset_to_min + SvGROW(sv, SvCUR(sv)
3373                                              + (send - s)
3374                                              + grow
3375                                              + 1 /* Trailing NUL */ );
3376
3377                 /* Now, we can expand out the range. */
3378 #ifdef EBCDIC
3379                 if (convert_unicode) {
3380                     SSize_t i;
3381
3382                     /* Recall that the min and max are now in Unicode terms, so
3383                      * we have to convert each character to its native
3384                      * equivalent */
3385                     if (d_is_utf8) {
3386                         for (i = range_min; i <= range_max; i++) {
3387                             append_utf8_from_native_byte(
3388                                                     LATIN1_TO_NATIVE((U8) i),
3389                                                     (U8 **) &d);
3390                         }
3391                     }
3392                     else {
3393                         for (i = range_min; i <= range_max; i++) {
3394                             *d++ = (char)LATIN1_TO_NATIVE((U8) i);
3395                         }
3396                     }
3397                 }
3398                 else
3399 #endif
3400                 /* Always gets run for ASCII, and sometimes for EBCDIC. */
3401                 {
3402                     /* Here, no conversions are necessary, which means that the
3403                      * first character in the range is already in 'd' and
3404                      * valid, so we can skip overwriting it */
3405                     if (d_is_utf8) {
3406                         SSize_t i;
3407                         d += UTF8SKIP(d);
3408                         for (i = range_min + 1; i <= range_max; i++) {
3409                             append_utf8_from_native_byte((U8) i, (U8 **) &d);
3410                         }
3411                     }
3412                     else {
3413                         SSize_t i;
3414                         d++;
3415                         assert(range_min + 1 <= range_max);
3416                         for (i = range_min + 1; i < range_max; i++) {
3417 #ifdef EBCDIC
3418                             /* In this case on EBCDIC, we haven't calculated
3419                              * the variants.  Do it here, as we go along */
3420                             if (! UVCHR_IS_INVARIANT(i)) {
3421                                 utf8_variant_count++;
3422                             }
3423 #endif
3424                             *d++ = (char)i;
3425                         }
3426
3427                         /* The range_max is done outside the loop so as to
3428                          * avoid having to special case not incrementing
3429                          * 'utf8_variant_count' on EBCDIC (it's already been
3430                          * counted when originally parsed) */
3431                         *d++ = (char) range_max;
3432                     }
3433                 }
3434
3435 #ifdef EBCDIC
3436                 /* If the original range extended above 255, add in that
3437                  * portion. */
3438                 if (real_range_max) {
3439                     *d++ = (char) UTF8_TWO_BYTE_HI(0x100);
3440                     *d++ = (char) UTF8_TWO_BYTE_LO(0x100);
3441                     if (real_range_max > 0x100) {
3442                         if (real_range_max > 0x101) {
3443                             *d++ = (char) RANGE_INDICATOR;
3444                         }
3445                         d = (char*)uvchr_to_utf8((U8*)d, real_range_max);
3446                     }
3447                 }
3448 #endif
3449
3450               range_done:
3451                 /* mark the range as done, and continue */
3452                 didrange = TRUE;
3453                 dorange = FALSE;
3454 #ifdef EBCDIC
3455                 non_portable_endpoint = 0;
3456                 backslash_N = 0;
3457 #endif
3458                 continue;
3459             } /* End of is a range */
3460         } /* End of transliteration.  Joins main code after these else's */
3461         else if (*s == '[' && PL_lex_inpat && !in_charclass) {
3462             char *s1 = s-1;
3463             int esc = 0;
3464             while (s1 >= start && *s1-- == '\\')
3465                 esc = !esc;
3466             if (!esc)
3467                 in_charclass = TRUE;
3468         }
3469         else if (*s == ']' && PL_lex_inpat && in_charclass) {
3470             char *s1 = s-1;
3471             int esc = 0;
3472             while (s1 >= start && *s1-- == '\\')
3473                 esc = !esc;
3474             if (!esc)
3475                 in_charclass = FALSE;
3476         }
3477             /* skip for regexp comments /(?#comment)/, except for the last
3478              * char, which will be done separately.  Stop on (?{..}) and
3479              * friends */
3480         else if (*s == '(' && PL_lex_inpat && s[1] == '?' && !in_charclass) {
3481             if (s[2] == '#') {
3482                 if (s_is_utf8) {
3483                     PERL_UINT_FAST8_T  len = UTF8SKIP(s);
3484
3485                     while (s + len < send && *s != ')') {
3486                         Copy(s, d, len, U8);
3487                         d += len;
3488                         s += len;
3489                         len = UTF8_SAFE_SKIP(s, send);
3490                     }
3491                 }
3492                 else while (s+1 < send && *s != ')') {
3493                     *d++ = *s++;
3494                 }
3495             }
3496             else if (!PL_lex_casemods
3497                      && (    s[2] == '{' /* This should match regcomp.c */
3498                          || (s[2] == '?' && s[3] == '{')))
3499             {
3500                 break;
3501             }
3502         }
3503             /* likewise skip #-initiated comments in //x patterns */
3504         else if (*s == '#'
3505                  && PL_lex_inpat
3506                  && !in_charclass
3507                  && ((PMOP*)PL_lex_inpat)->op_pmflags & RXf_PMf_EXTENDED)
3508         {
3509             while (s < send && *s != '\n')
3510                 *d++ = *s++;
3511         }
3512             /* no further processing of single-quoted regex */
3513         else if (PL_lex_inpat && SvIVX(PL_linestr) == '\'')
3514             goto default_action;
3515
3516             /* check for embedded arrays
3517              * (@foo, @::foo, @'foo, @{foo}, @$foo, @+, @-)
3518              */
3519         else if (*s == '@' && s[1]) {
3520             if (UTF
3521                ? isIDFIRST_utf8_safe(s+1, send)
3522                : isWORDCHAR_A(s[1]))
3523             {
3524                 break;
3525             }
3526             if (memCHRs(":'{$", s[1]))
3527                 break;
3528             if (!PL_lex_inpat && (s[1] == '+' || s[1] == '-'))
3529                 break; /* in regexp, neither @+ nor @- are interpolated */
3530         }
3531             /* check for embedded scalars.  only stop if we're sure it's a
3532              * variable.  */
3533         else if (*s == '$') {
3534             if (!PL_lex_inpat)  /* not a regexp, so $ must be var */
3535                 break;
3536             if (s + 1 < send && !memCHRs("()| \r\n\t", s[1])) {
3537                 if (s[1] == '\\') {
3538                     Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
3539                                    "Possible unintended interpolation of $\\ in regex");
3540                 }
3541                 break;          /* in regexp, $ might be tail anchor */
3542             }
3543         }
3544
3545         /* End of else if chain - OP_TRANS rejoin rest */
3546
3547         if (UNLIKELY(s >= send)) {
3548             assert(s == send);
3549             break;
3550         }
3551
3552         /* backslashes */
3553         if (*s == '\\' && s+1 < send) {
3554             char* e;    /* Can be used for ending '}', etc. */
3555
3556             s++;
3557
3558             /* warn on \1 - \9 in substitution replacements, but note that \11
3559              * is an octal; and \19 is \1 followed by '9' */
3560             if (PL_lex_inwhat == OP_SUBST
3561                 && !PL_lex_inpat
3562                 && isDIGIT(*s)
3563                 && *s != '0'
3564                 && !isDIGIT(s[1]))
3565             {
3566                 /* diag_listed_as: \%d better written as $%d */
3567                 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "\\%c better written as $%c", *s, *s);
3568                 *--s = '$';
3569                 break;
3570             }
3571
3572             /* string-change backslash escapes */
3573             if (PL_lex_inwhat != OP_TRANS && *s && memCHRs("lLuUEQF", *s)) {
3574                 --s;
3575                 break;
3576             }
3577             /* In a pattern, process \N, but skip any other backslash escapes.
3578              * This is because we don't want to translate an escape sequence
3579              * into a meta symbol and have the regex compiler use the meta
3580              * symbol meaning, e.g. \x{2E} would be confused with a dot.  But
3581              * in spite of this, we do have to process \N here while the proper
3582              * charnames handler is in scope.  See bugs #56444 and #62056.
3583              *
3584              * There is a complication because \N in a pattern may also stand
3585              * for 'match a non-nl', and not mean a charname, in which case its
3586              * processing should be deferred to the regex compiler.  To be a
3587              * charname it must be followed immediately by a '{', and not look
3588              * like \N followed by a curly quantifier, i.e., not something like
3589              * \N{3,}.  regcurly returns a boolean indicating if it is a legal
3590              * quantifier */
3591             else if (PL_lex_inpat
3592                     && (*s != 'N'
3593                         || s[1] != '{'
3594                         || regcurly(s + 1)))
3595             {
3596                 *d++ = '\\';
3597                 goto default_action;
3598             }
3599
3600             switch (*s) {
3601             default:
3602                 {
3603                     if ((isALPHANUMERIC(*s)))
3604                         Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
3605                                        "Unrecognized escape \\%c passed through",
3606                                        *s);
3607                     /* default action is to copy the quoted character */
3608                     goto default_action;
3609                 }
3610
3611             /* eg. \132 indicates the octal constant 0132 */
3612             case '0': case '1': case '2': case '3':
3613             case '4': case '5': case '6': case '7':
3614                 {
3615                     I32 flags = PERL_SCAN_SILENT_ILLDIGIT
3616                               | PERL_SCAN_NOTIFY_ILLDIGIT;
3617                     STRLEN len = 3;
3618                     uv = grok_oct(s, &len, &flags, NULL);
3619                     s += len;
3620                     if (  (flags & PERL_SCAN_NOTIFY_ILLDIGIT)
3621                         && s < send
3622                         && isDIGIT(*s)  /* like \08, \178 */
3623                         && ckWARN(WARN_MISC))
3624                     {
3625                         Perl_warner(aTHX_ packWARN(WARN_MISC), "%s",
3626                             form_alien_digit_msg(8, len, s, send, UTF, FALSE));
3627                     }
3628                 }
3629                 goto NUM_ESCAPE_INSERT;
3630
3631             /* eg. \o{24} indicates the octal constant \024 */
3632             case 'o':
3633                 {
3634                     const char* error;
3635
3636                     if (! grok_bslash_o(&s, send,
3637                                                &uv, &error,
3638                                                NULL,
3639                                                FALSE, /* Not strict */
3640                                                FALSE, /* No illegal cp's */
3641                                                UTF))
3642                     {
3643                         yyerror(error);
3644                         uv = 0; /* drop through to ensure range ends are set */
3645                     }
3646                     goto NUM_ESCAPE_INSERT;
3647                 }
3648
3649             /* eg. \x24 indicates the hex constant 0x24 */
3650             case 'x':
3651                 {
3652                     const char* error;
3653
3654                     if (! grok_bslash_x(&s, send,
3655                                                &uv, &error,
3656                                                NULL,
3657                                                FALSE, /* Not strict */
3658                                                FALSE, /* No illegal cp's */
3659                                                UTF))
3660                     {
3661                         yyerror(error);
3662                         uv = 0; /* drop through to ensure range ends are set */
3663                     }
3664                 }
3665
3666               NUM_ESCAPE_INSERT:
3667                 /* Insert oct or hex escaped character. */
3668
3669                 /* Here uv is the ordinal of the next character being added */
3670                 if (UVCHR_IS_INVARIANT(uv)) {
3671                     *d++ = (char) uv;
3672                 }
3673                 else {
3674                     if (!d_is_utf8 && uv > 255) {
3675
3676                         /* Here, 'uv' won't fit unless we convert to UTF-8.
3677                          * If we've only seen invariants so far, all we have to
3678                          * do is turn on the flag */
3679                         if (utf8_variant_count == 0) {
3680                             SvUTF8_on(sv);
3681                         }
3682                         else {
3683                             SvCUR_set(sv, d - SvPVX_const(sv));
3684                             SvPOK_on(sv);
3685                             *d = '\0';
3686
3687                             sv_utf8_upgrade_flags_grow(
3688                                            sv,
3689                                            SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3690
3691                                            /* Since we're having to grow here,
3692                                             * make sure we have enough room for
3693                                             * this escape and a NUL, so the
3694                                             * code immediately below won't have
3695                                             * to actually grow again */
3696                                           UVCHR_SKIP(uv)
3697                                         + (STRLEN)(send - s) + 1);
3698                             d = SvPVX(sv) + SvCUR(sv);
3699                         }
3700
3701                         has_above_latin1 = TRUE;
3702                         d_is_utf8 = TRUE;
3703                     }
3704
3705                     if (! d_is_utf8) {
3706                         *d++ = (char)uv;
3707                         utf8_variant_count++;
3708                     }
3709                     else {
3710                        /* Usually, there will already be enough room in 'sv'
3711                         * since such escapes are likely longer than any UTF-8
3712                         * sequence they can end up as.  This isn't the case on
3713                         * EBCDIC where \x{40000000} contains 12 bytes, and the
3714                         * UTF-8 for it contains 14.  And, we have to allow for
3715                         * a trailing NUL.  It probably can't happen on ASCII
3716                         * platforms, but be safe.  See Note on sizing above. */
3717                         const STRLEN needed = d - SvPVX(sv)
3718                                             + UVCHR_SKIP(uv)
3719                                             + (send - s)
3720                                             + 1;
3721                         if (UNLIKELY(needed > SvLEN(sv))) {
3722                             SvCUR_set(sv, d - SvPVX_const(sv));
3723                             d = SvCUR(sv) + SvGROW(sv, needed);
3724                         }
3725
3726                         d = (char*) uvchr_to_utf8_flags((U8*)d, uv,
3727                                                    (ckWARN(WARN_PORTABLE))
3728                                                    ? UNICODE_WARN_PERL_EXTENDED
3729                                                    : 0);
3730                     }
3731                 }
3732 #ifdef EBCDIC
3733                 non_portable_endpoint++;
3734 #endif
3735                 continue;
3736
3737             case 'N':
3738                 /* In a non-pattern \N must be like \N{U+0041}, or it can be a
3739                  * named character, like \N{LATIN SMALL LETTER A}, or a named
3740                  * sequence, like \N{LATIN CAPITAL LETTER A WITH MACRON AND
3741                  * GRAVE} (except y/// can't handle the latter, croaking).  For
3742                  * convenience all three forms are referred to as "named
3743                  * characters" below.
3744                  *
3745                  * For patterns, \N also can mean to match a non-newline.  Code
3746                  * before this 'switch' statement should already have handled
3747                  * this situation, and hence this code only has to deal with
3748                  * the named character cases.
3749                  *
3750                  * For non-patterns, the named characters are converted to
3751                  * their string equivalents.  In patterns, named characters are
3752                  * not converted to their ultimate forms for the same reasons
3753                  * that other escapes aren't (mainly that the ultimate
3754                  * character could be considered a meta-symbol by the regex
3755                  * compiler).  Instead, they are converted to the \N{U+...}
3756                  * form to get the value from the charnames that is in effect
3757                  * right now, while preserving the fact that it was a named
3758                  * character, so that the regex compiler knows this.
3759                  *
3760                  * The structure of this section of code (besides checking for
3761                  * errors and upgrading to utf8) is:
3762                  *    If the named character is of the form \N{U+...}, pass it
3763                  *      through if a pattern; otherwise convert the code point
3764                  *      to utf8
3765                  *    Otherwise must be some \N{NAME}: convert to
3766                  *      \N{U+c1.c2...} if a pattern; otherwise convert to utf8
3767                  *
3768                  * Transliteration is an exception.  The conversion to utf8 is
3769                  * only done if the code point requires it to be representable.
3770                  *
3771                  * Here, 's' points to the 'N'; the test below is guaranteed to
3772                  * succeed if we are being called on a pattern, as we already
3773                  * know from a test above that the next character is a '{'.  A
3774                  * non-pattern \N must mean 'named character', which requires
3775                  * braces */
3776                 s++;
3777                 if (*s != '{') {
3778                     yyerror("Missing braces on \\N{}");
3779                     *d++ = '\0';
3780                     continue;
3781                 }
3782                 s++;
3783
3784                 /* If there is no matching '}', it is an error. */
3785                 if (! (e = (char *) memchr(s, '}', send - s))) {
3786                     if (! PL_lex_inpat) {
3787                         yyerror("Missing right brace on \\N{}");
3788                     } else {
3789                         yyerror("Missing right brace on \\N{} or unescaped left brace after \\N");
3790                     }
3791                     yyquit(); /* Have exhausted the input. */
3792                 }
3793
3794                 /* Here it looks like a named character */
3795
3796                 if (*s == 'U' && s[1] == '+') { /* \N{U+...} */
3797                     s += 2;         /* Skip to next char after the 'U+' */
3798                     if (PL_lex_inpat) {
3799
3800                         /* In patterns, we can have \N{U+xxxx.yyyy.zzzz...} */
3801                         /* Check the syntax.  */
3802                         const char *orig_s;
3803                         orig_s = s - 5;
3804                         if (!isXDIGIT(*s)) {
3805                           bad_NU:
3806                             yyerror(
3807                                 "Invalid hexadecimal number in \\N{U+...}"
3808                             );
3809                             s = e + 1;
3810                             *d++ = '\0';
3811                             continue;
3812                         }
3813                         while (++s < e) {
3814                             if (isXDIGIT(*s))
3815                                 continue;
3816                             else if ((*s == '.' || *s == '_')
3817                                   && isXDIGIT(s[1]))
3818                                 continue;
3819                             goto bad_NU;
3820                         }
3821
3822                         /* Pass everything through unchanged.
3823                          * +1 is for the '}' */
3824                         Copy(orig_s, d, e - orig_s + 1, char);
3825                         d += e - orig_s + 1;
3826                     }
3827                     else {  /* Not a pattern: convert the hex to string */
3828                         I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
3829                                   | PERL_SCAN_SILENT_ILLDIGIT
3830                                   | PERL_SCAN_SILENT_OVERFLOW
3831                                   | PERL_SCAN_DISALLOW_PREFIX;
3832                         STRLEN len = e - s;
3833
3834                         uv = grok_hex(s, &len, &flags, NULL);
3835                         if (len == 0 || (len != (STRLEN)(e - s)))
3836                             goto bad_NU;
3837
3838                         if (    uv > MAX_LEGAL_CP
3839                             || (flags & PERL_SCAN_GREATER_THAN_UV_MAX))
3840                         {
3841                             yyerror(form_cp_too_large_msg(16, s, len, 0));
3842                             uv = 0; /* drop through to ensure range ends are
3843                                        set */
3844                         }
3845
3846                          /* For non-tr///, if the destination is not in utf8,
3847                           * unconditionally recode it to be so.  This is
3848                           * because \N{} implies Unicode semantics, and scalars
3849                           * have to be in utf8 to guarantee those semantics.
3850                           * tr/// doesn't care about Unicode rules, so no need
3851                           * there to upgrade to UTF-8 for small enough code
3852                           * points */
3853                         if (! d_is_utf8 && (   uv > 0xFF
3854                                            || PL_lex_inwhat != OP_TRANS))
3855                         {
3856                             /* See Note on sizing above.  */
3857                             const STRLEN extra = OFFUNISKIP(uv) + (send - e) + 1;
3858
3859                             SvCUR_set(sv, d - SvPVX_const(sv));
3860                             SvPOK_on(sv);
3861                             *d = '\0';
3862
3863                             if (utf8_variant_count == 0) {
3864                                 SvUTF8_on(sv);
3865                                 d = SvCUR(sv) + SvGROW(sv, SvCUR(sv) + extra);
3866                             }
3867                             else {
3868                                 sv_utf8_upgrade_flags_grow(
3869                                                sv,
3870                                                SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3871                                                extra);
3872                                 d = SvPVX(sv) + SvCUR(sv);
3873                             }
3874
3875                             d_is_utf8 = TRUE;
3876                             has_above_latin1 = TRUE;
3877                         }
3878
3879                         /* Add the (Unicode) code point to the output. */
3880                         if (! d_is_utf8 || OFFUNI_IS_INVARIANT(uv)) {
3881                             *d++ = (char) LATIN1_TO_NATIVE(uv);
3882                         }
3883                         else {
3884                             d = (char*) uvoffuni_to_utf8_flags((U8*)d, uv,
3885                                                    (ckWARN(WARN_PORTABLE))
3886                                                    ? UNICODE_WARN_PERL_EXTENDED
3887                                                    : 0);
3888                         }
3889                     }
3890                 }
3891                 else /* Here is \N{NAME} but not \N{U+...}. */
3892                      if (! (res = get_and_check_backslash_N_name_wrapper(s, e)))
3893                 {   /* Failed.  We should die eventually, but for now use a NUL
3894                        to keep parsing */
3895                     *d++ = '\0';
3896                 }
3897                 else {  /* Successfully evaluated the name */
3898                     STRLEN len;
3899                     const char *str = SvPV_const(res, len);
3900                     if (PL_lex_inpat) {
3901
3902                         if (! len) { /* The name resolved to an empty string */
3903                             const char empty_N[] = "\\N{_}";
3904                             Copy(empty_N, d, sizeof(empty_N) - 1, char);
3905                             d += sizeof(empty_N) - 1;
3906                         }
3907                         else {
3908                             /* In order to not lose information for the regex
3909                             * compiler, pass the result in the specially made
3910                             * syntax: \N{U+c1.c2.c3...}, where c1 etc. are
3911                             * the code points in hex of each character
3912                             * returned by charnames */
3913
3914                             const char *str_end = str + len;
3915                             const STRLEN off = d - SvPVX_const(sv);
3916
3917                             if (! SvUTF8(res)) {
3918                                 /* For the non-UTF-8 case, we can determine the
3919                                  * exact length needed without having to parse
3920                                  * through the string.  Each character takes up
3921                                  * 2 hex digits plus either a trailing dot or
3922                                  * the "}" */
3923                                 const char initial_text[] = "\\N{U+";
3924                                 const STRLEN initial_len = sizeof(initial_text)
3925                                                            - 1;
3926                                 d = off + SvGROW(sv, off
3927                                                     + 3 * len
3928
3929                                                     /* +1 for trailing NUL */
3930                                                     + initial_len + 1
3931
3932                                                     + (STRLEN)(send - e));
3933                                 Copy(initial_text, d, initial_len, char);
3934                                 d += initial_len;
3935                                 while (str < str_end) {
3936                                     char hex_string[4];
3937                                     int len =
3938                                         my_snprintf(hex_string,
3939                                                   sizeof(hex_string),
3940                                                   "%02X.",
3941
3942                                                   /* The regex compiler is
3943                                                    * expecting Unicode, not
3944                                                    * native */
3945                                                   NATIVE_TO_LATIN1(*str));
3946                                     PERL_MY_SNPRINTF_POST_GUARD(len,
3947                                                            sizeof(hex_string));
3948                                     Copy(hex_string, d, 3, char);
3949                                     d += 3;
3950                                     str++;
3951                                 }
3952                                 d--;    /* Below, we will overwrite the final
3953                                            dot with a right brace */
3954                             }
3955                             else {
3956                                 STRLEN char_length; /* cur char's byte length */
3957
3958                                 /* and the number of bytes after this is
3959                                  * translated into hex digits */
3960                                 STRLEN output_length;
3961
3962                                 /* 2 hex per byte; 2 chars for '\N'; 2 chars
3963                                  * for max('U+', '.'); and 1 for NUL */
3964                                 char hex_string[2 * UTF8_MAXBYTES + 5];
3965
3966                                 /* Get the first character of the result. */
3967                                 U32 uv = utf8n_to_uvchr((U8 *) str,
3968                                                         len,
3969                                                         &char_length,
3970                                                         UTF8_ALLOW_ANYUV);
3971                                 /* Convert first code point to Unicode hex,
3972                                  * including the boiler plate before it. */
3973                                 output_length =
3974                                     my_snprintf(hex_string, sizeof(hex_string),
3975                                              "\\N{U+%X",
3976                                              (unsigned int) NATIVE_TO_UNI(uv));
3977
3978                                 /* Make sure there is enough space to hold it */
3979                                 d = off + SvGROW(sv, off
3980                                                     + output_length
3981                                                     + (STRLEN)(send - e)
3982                                                     + 2);       /* '}' + NUL */
3983                                 /* And output it */
3984                                 Copy(hex_string, d, output_length, char);
3985                                 d += output_length;
3986
3987                                 /* For each subsequent character, append dot and
3988                                 * its Unicode code point in hex */
3989                                 while ((str += char_length) < str_end) {
3990                                     const STRLEN off = d - SvPVX_const(sv);
3991                                     U32 uv = utf8n_to_uvchr((U8 *) str,
3992                                                             str_end - str,
3993                                                             &char_length,
3994                                                             UTF8_ALLOW_ANYUV);
3995                                     output_length =
3996                                         my_snprintf(hex_string,
3997                                              sizeof(hex_string),
3998                                              ".%X",
3999                                              (unsigned int) NATIVE_TO_UNI(uv));
4000
4001                                     d = off + SvGROW(sv, off
4002                                                         + output_length
4003                                                         + (STRLEN)(send - e)
4004                                                         + 2);   /* '}' +  NUL */
4005                                     Copy(hex_string, d, output_length, char);
4006                                     d += output_length;
4007                                 }
4008                             }
4009
4010                             *d++ = '}'; /* Done.  Add the trailing brace */
4011                         }
4012                     }
4013                     else { /* Here, not in a pattern.  Convert the name to a
4014                             * string. */
4015
4016                         if (PL_lex_inwhat == OP_TRANS) {
4017                             str = SvPV_const(res, len);
4018                             if (len > ((SvUTF8(res))
4019                                        ? UTF8SKIP(str)
4020                                        : 1U))
4021                             {
4022                                 yyerror(Perl_form(aTHX_
4023                                     "%.*s must not be a named sequence"
4024                                     " in transliteration operator",
4025                                         /*  +1 to include the "}" */
4026                                     (int) (e + 1 - start), start));
4027                                 *d++ = '\0';
4028                                 goto end_backslash_N;
4029                             }
4030
4031                             if (SvUTF8(res) && UTF8_IS_ABOVE_LATIN1(*str)) {
4032                                 has_above_latin1 = TRUE;
4033                             }
4034
4035                         }
4036                         else if (! SvUTF8(res)) {
4037                             /* Make sure \N{} return is UTF-8.  This is because
4038                              * \N{} implies Unicode semantics, and scalars have
4039                              * to be in utf8 to guarantee those semantics; but
4040                              * not needed in tr/// */
4041                             sv_utf8_upgrade_flags(res, 0);
4042                             str = SvPV_const(res, len);
4043                         }
4044
4045                          /* Upgrade destination to be utf8 if this new
4046                           * component is */
4047                         if (! d_is_utf8 && SvUTF8(res)) {
4048                             /* See Note on sizing above.  */
4049                             const STRLEN extra = len + (send - s) + 1;
4050
4051                             SvCUR_set(sv, d - SvPVX_const(sv));
4052                             SvPOK_on(sv);
4053                             *d = '\0';
4054
4055                             if (utf8_variant_count == 0) {
4056                                 SvUTF8_on(sv);
4057                                 d = SvCUR(sv) + SvGROW(sv, SvCUR(sv) + extra);
4058                             }
4059                             else {
4060                                 sv_utf8_upgrade_flags_grow(sv,
4061                                                 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4062                                                 extra);
4063                                 d = SvPVX(sv) + SvCUR(sv);
4064                             }
4065                             d_is_utf8 = TRUE;
4066                         } else if (len > (STRLEN)(e - s + 4)) { /* I _guess_ 4 is \N{} --jhi */
4067
4068                             /* See Note on sizing above.  (NOTE: SvCUR() is not
4069                              * set correctly here). */
4070                             const STRLEN extra = len + (send - e) + 1;
4071                             const STRLEN off = d - SvPVX_const(sv);
4072                             d = off + SvGROW(sv, off + extra);
4073                         }
4074                         Copy(str, d, len, char);
4075                         d += len;
4076                     }
4077
4078                     SvREFCNT_dec(res);
4079
4080                 } /* End \N{NAME} */
4081
4082               end_backslash_N:
4083 #ifdef EBCDIC
4084                 backslash_N++; /* \N{} is defined to be Unicode */
4085 #endif
4086                 s = e + 1;  /* Point to just after the '}' */
4087                 continue;
4088
4089             /* \c is a control character */
4090             case 'c':
4091                 s++;
4092                 if (s < send) {
4093                     const char * message;
4094
4095                     if (! grok_bslash_c(*s, (U8 *) d, &message, NULL)) {
4096                         yyerror(message);
4097                         yyquit();   /* Have always immediately croaked on
4098                                        errors in this */
4099                     }
4100                     d++;
4101                 }
4102                 else {
4103                     yyerror("Missing control char name in \\c");
4104                     yyquit();   /* Are at end of input, no sense continuing */
4105                 }
4106 #ifdef EBCDIC
4107                 non_portable_endpoint++;
4108 #endif
4109                 break;
4110
4111             /* printf-style backslashes, formfeeds, newlines, etc */
4112             case 'b':
4113                 *d++ = '\b';
4114                 break;
4115             case 'n':
4116                 *d++ = '\n';
4117                 break;
4118             case 'r':
4119                 *d++ = '\r';
4120                 break;
4121             case 'f':
4122                 *d++ = '\f';
4123                 break;
4124             case 't':
4125                 *d++ = '\t';
4126                 break;
4127             case 'e':
4128                 *d++ = ESC_NATIVE;
4129                 break;
4130             case 'a':
4131                 *d++ = '\a';
4132                 break;
4133             } /* end switch */
4134
4135             s++;
4136             continue;
4137         } /* end if (backslash) */
4138
4139     default_action:
4140         /* Just copy the input to the output, though we may have to convert
4141          * to/from UTF-8.
4142          *
4143          * If the input has the same representation in UTF-8 as not, it will be
4144          * a single byte, and we don't care about UTF8ness; just copy the byte */
4145         if (NATIVE_BYTE_IS_INVARIANT((U8)(*s))) {
4146             *d++ = *s++;
4147         }
4148         else if (! s_is_utf8 && ! d_is_utf8) {
4149             /* If neither source nor output is UTF-8, is also a single byte,
4150              * just copy it; but this byte counts should we later have to
4151              * convert to UTF-8 */
4152             *d++ = *s++;
4153             utf8_variant_count++;
4154         }
4155         else if (s_is_utf8 && d_is_utf8) {   /* Both UTF-8, can just copy */
4156             const STRLEN len = UTF8SKIP(s);
4157
4158             /* We expect the source to have already been checked for
4159              * malformedness */
4160             assert(isUTF8_CHAR((U8 *) s, (U8 *) send));
4161
4162             Copy(s, d, len, U8);
4163             d += len;
4164             s += len;
4165         }
4166         else if (s_is_utf8) { /* UTF8ness matters: convert output to utf8 */
4167             STRLEN need = send - s + 1; /* See Note on sizing above. */
4168
4169             SvCUR_set(sv, d - SvPVX_const(sv));
4170             SvPOK_on(sv);
4171             *d = '\0';
4172
4173             if (utf8_variant_count == 0) {
4174                 SvUTF8_on(sv);
4175                 d = SvCUR(sv) + SvGROW(sv, SvCUR(sv) + need);
4176             }
4177             else {
4178                 sv_utf8_upgrade_flags_grow(sv,
4179                                            SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4180                                            need);
4181                 d = SvPVX(sv) + SvCUR(sv);
4182             }
4183             d_is_utf8 = TRUE;
4184             goto default_action; /* Redo, having upgraded so both are UTF-8 */
4185         }
4186         else {  /* UTF8ness matters: convert this non-UTF8 source char to
4187                    UTF-8 for output.  It will occupy 2 bytes, but don't include
4188                    the input byte since we haven't incremented 's' yet. See
4189                    Note on sizing above. */
4190             const STRLEN off = d - SvPVX(sv);
4191             const STRLEN extra = 2 + (send - s - 1) + 1;
4192             if (off + extra > SvLEN(sv)) {
4193                 d = off + SvGROW(sv, off + extra);
4194             }
4195             *d++ = UTF8_EIGHT_BIT_HI(*s);
4196             *d++ = UTF8_EIGHT_BIT_LO(*s);
4197             s++;
4198         }
4199     } /* while loop to process each character */
4200
4201     {
4202         const STRLEN off = d - SvPVX(sv);
4203
4204         /* See if room for the terminating NUL */
4205         if (UNLIKELY(off >= SvLEN(sv))) {
4206
4207 #ifndef DEBUGGING
4208
4209             if (off > SvLEN(sv))
4210 #endif
4211                 Perl_croak(aTHX_ "panic: constant overflowed allocated space,"
4212                         " %" UVuf " >= %" UVuf, (UV)off, (UV)SvLEN(sv));
4213
4214             /* Whew!  Here we don't have room for the terminating NUL, but
4215              * everything else so far has fit.  It's not too late to grow
4216              * to fit the NUL and continue on.  But it is a bug, as the code
4217              * above was supposed to have made room for this, so under
4218              * DEBUGGING builds, we panic anyway.  */
4219             d = off + SvGROW(sv, off + 1);
4220         }
4221     }
4222
4223     /* terminate the string and set up the sv */
4224     *d = '\0';
4225     SvCUR_set(sv, d - SvPVX_const(sv));
4226
4227     SvPOK_on(sv);
4228     if (d_is_utf8) {
4229         SvUTF8_on(sv);
4230     }
4231
4232     /* shrink the sv if we allocated more than we used */
4233     if (SvCUR(sv) + 5 < SvLEN(sv)) {
4234         SvPV_shrink_to_cur(sv);
4235     }
4236
4237     /* return the substring (via pl_yylval) only if we parsed anything */
4238     if (s > start) {
4239         char *s2 = start;
4240         for (; s2 < s; s2++) {
4241             if (*s2 == '\n')
4242                 COPLINE_INC_WITH_HERELINES;
4243         }
4244         SvREFCNT_inc_simple_void_NN(sv);
4245         if (   (PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ))
4246             && ! PL_parser->lex_re_reparsing)
4247         {
4248             const char *const key = PL_lex_inpat ? "qr" : "q";
4249             const STRLEN keylen = PL_lex_inpat ? 2 : 1;
4250             const char *type;
4251             STRLEN typelen;
4252
4253             if (PL_lex_inwhat == OP_TRANS) {
4254                 type = "tr";
4255                 typelen = 2;
4256             } else if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat) {
4257                 type = "s";
4258                 typelen = 1;
4259             } else if (PL_lex_inpat && SvIVX(PL_linestr) == '\'') {
4260                 type = "q";
4261                 typelen = 1;
4262             } else {
4263                 type = "qq";
4264                 typelen = 2;
4265             }
4266
4267             sv = S_new_constant(aTHX_ start, s - start, key, keylen, sv, NULL,
4268                                 type, typelen, NULL);
4269         }
4270         pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
4271     }
4272     LEAVE_with_name("scan_const");
4273     return s;
4274 }
4275
4276 /* S_intuit_more
4277  * Returns TRUE if there's more to the expression (e.g., a subscript),
4278  * FALSE otherwise.
4279  *
4280  * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
4281  *
4282  * ->[ and ->{ return TRUE
4283  * ->$* ->$#* ->@* ->@[ ->@{ return TRUE if postderef_qq is enabled
4284  * { and [ outside a pattern are always subscripts, so return TRUE
4285  * if we're outside a pattern and it's not { or [, then return FALSE
4286  * if we're in a pattern and the first char is a {
4287  *   {4,5} (any digits around the comma) returns FALSE
4288  * if we're in a pattern and the first char is a [
4289  *   [] returns FALSE
4290  *   [SOMETHING] has a funky algorithm to decide whether it's a
4291  *      character class or not.  It has to deal with things like
4292  *      /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
4293  * anything else returns TRUE
4294  */
4295
4296 /* This is the one truly awful dwimmer necessary to conflate C and sed. */
4297
4298 STATIC int
4299 S_intuit_more(pTHX_ char *s, char *e)
4300 {
4301     PERL_ARGS_ASSERT_INTUIT_MORE;
4302
4303     if (PL_lex_brackets)
4304         return TRUE;
4305     if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
4306         return TRUE;
4307     if (*s == '-' && s[1] == '>'
4308      && FEATURE_POSTDEREF_QQ_IS_ENABLED
4309      && ( (s[2] == '$' && (s[3] == '*' || (s[3] == '#' && s[4] == '*')))
4310         ||(s[2] == '@' && memCHRs("*[{",s[3])) ))
4311         return TRUE;
4312     if (*s != '{' && *s != '[')
4313         return FALSE;
4314     PL_parser->sub_no_recover = TRUE;
4315     if (!PL_lex_inpat)
4316         return TRUE;
4317
4318     /* In a pattern, so maybe we have {n,m}. */
4319     if (*s == '{') {
4320         if (regcurly(s)) {
4321             return FALSE;
4322         }
4323         return TRUE;
4324     }
4325
4326     /* On the other hand, maybe we have a character class */
4327
4328     s++;
4329     if (*s == ']' || *s == '^')
4330         return FALSE;
4331     else {
4332         /* this is terrifying, and it works */
4333         int weight;
4334         char seen[256];
4335         const char * const send = (char *) memchr(s, ']', e - s);
4336         unsigned char un_char, last_un_char;
4337         char tmpbuf[sizeof PL_tokenbuf * 4];
4338
4339         if (!send)              /* has to be an expression */
4340             return TRUE;
4341         weight = 2;             /* let's weigh the evidence */
4342
4343         if (*s == '$')
4344             weight -= 3;
4345         else if (isDIGIT(*s)) {
4346             if (s[1] != ']') {
4347                 if (isDIGIT(s[1]) && s[2] == ']')
4348                     weight -= 10;
4349             }
4350             else
4351                 weight -= 100;
4352         }
4353         Zero(seen,256,char);
4354         un_char = 255;
4355         for (; s < send; s++) {
4356             last_un_char = un_char;
4357             un_char = (unsigned char)*s;
4358             switch (*s) {
4359             case '@':
4360             case '&':
4361             case '$':
4362                 weight -= seen[un_char] * 10;
4363                 if (isWORDCHAR_lazy_if_safe(s+1, PL_bufend, UTF)) {
4364                     int len;
4365                     scan_ident(s, tmpbuf, sizeof tmpbuf, FALSE);
4366                     len = (int)strlen(tmpbuf);
4367                     if (len > 1 && gv_fetchpvn_flags(tmpbuf, len,
4368                                                     UTF ? SVf_UTF8 : 0, SVt_PV))
4369                         weight -= 100;
4370                     else
4371                         weight -= 10;
4372                 }
4373                 else if (*s == '$'
4374                          && s[1]
4375                          && memCHRs("[#!%*<>()-=",s[1]))
4376                 {
4377                     if (/*{*/ memCHRs("])} =",s[2]))
4378                         weight -= 10;
4379                     else
4380                         weight -= 1;
4381                 }
4382                 break;
4383             case '\\':
4384                 un_char = 254;
4385                 if (s[1]) {
4386                     if (memCHRs("wds]",s[1]))
4387                         weight += 100;
4388                     else if (seen[(U8)'\''] || seen[(U8)'"'])
4389                         weight += 1;
4390                     else if (memCHRs("rnftbxcav",s[1]))
4391                         weight += 40;
4392                     else if (isDIGIT(s[1])) {
4393                         weight += 40;
4394                         while (s[1] && isDIGIT(s[1]))
4395                             s++;
4396                     }
4397                 }
4398                 else
4399                     weight += 100;
4400                 break;
4401             case '-':
4402                 if (s[1] == '\\')
4403                     weight += 50;
4404                 if (memCHRs("aA01! ",last_un_char))
4405                     weight += 30;
4406                 if (memCHRs("zZ79~",s[1]))
4407                     weight += 30;
4408                 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
4409                     weight -= 5;        /* cope with negative subscript */
4410                 break;
4411             default:
4412                 if (!isWORDCHAR(last_un_char)
4413                     && !(last_un_char == '$' || last_un_char == '@'
4414                          || last_un_char == '&')
4415                     && isALPHA(*s) && s[1] && isALPHA(s[1])) {
4416                     char *d = s;
4417                     while (isALPHA(*s))
4418                         s++;
4419                     if (keyword(d, s - d, 0))
4420                         weight -= 150;
4421                 }
4422                 if (un_char == last_un_char + 1)
4423                     weight += 5;
4424                 weight -= seen[un_char];
4425                 break;
4426             }
4427             seen[un_char]++;
4428         }
4429         if (weight >= 0)        /* probably a character class */
4430             return FALSE;
4431     }
4432
4433     return TRUE;
4434 }
4435
4436 /*
4437  * S_intuit_method
4438  *
4439  * Does all the checking to disambiguate
4440  *   foo bar
4441  * between foo(bar) and bar->foo.  Returns 0 if not a method, otherwise
4442  * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
4443  *
4444  * First argument is the stuff after the first token, e.g. "bar".
4445  *
4446  * Not a method if foo is a filehandle.
4447  * Not a method if foo is a subroutine prototyped to take a filehandle.
4448  * Not a method if it's really "Foo $bar"
4449  * Method if it's "foo $bar"
4450  * Not a method if it's really "print foo $bar"
4451  * Method if it's really "foo package::" (interpreted as package->foo)
4452  * Not a method if bar is known to be a subroutine ("sub bar; foo bar")
4453  * Not a method if bar is a filehandle or package, but is quoted with
4454  *   =>
4455  */
4456
4457 STATIC int
4458 S_intuit_method(pTHX_ char *start, SV *ioname, CV *cv)
4459 {
4460     char *s = start + (*start == '$');
4461     char tmpbuf[sizeof PL_tokenbuf];
4462     STRLEN len;
4463     GV* indirgv;
4464         /* Mustn't actually add anything to a symbol table.
4465            But also don't want to "initialise" any placeholder
4466            constants that might already be there into full
4467            blown PVGVs with attached PVCV.  */
4468     GV * const gv =
4469         ioname ? gv_fetchsv(ioname, GV_NOADD_NOINIT, SVt_PVCV) : NULL;
4470
4471     PERL_ARGS_ASSERT_INTUIT_METHOD;
4472
4473     if (!FEATURE_INDIRECT_IS_ENABLED)
4474         return 0;
4475
4476     if (gv && SvTYPE(gv) == SVt_PVGV && GvIO(gv))
4477             return 0;
4478     if (cv && SvPOK(cv)) {
4479         const char *proto = CvPROTO(cv);
4480         if (proto) {
4481             while (*proto && (isSPACE(*proto) || *proto == ';'))
4482                 proto++;
4483             if (*proto == '*')
4484                 return 0;
4485         }
4486     }
4487
4488     if (*start == '$') {
4489         SSize_t start_off = start - SvPVX(PL_linestr);
4490         if (cv || PL_last_lop_op == OP_PRINT || PL_last_lop_op == OP_SAY
4491             || isUPPER(*PL_tokenbuf))
4492             return 0;
4493         /* this could be $# */
4494         if (isSPACE(*s))
4495             s = skipspace(s);
4496         PL_bufptr = SvPVX(PL_linestr) + start_off;
4497         PL_expect = XREF;
4498         return *s == '(' ? FUNCMETH : METHOD;
4499     }
4500
4501     s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
4502     /* start is the beginning of the possible filehandle/object,
4503      * and s is the end of it
4504      * tmpbuf is a copy of it (but with single quotes as double colons)
4505      */
4506
4507     if (!keyword(tmpbuf, len, 0)) {
4508         if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
4509             len -= 2;
4510             tmpbuf[len] = '\0';
4511             goto bare_package;
4512         }
4513         indirgv = gv_fetchpvn_flags(tmpbuf, len,
4514                                     GV_NOADD_NOINIT|( UTF ? SVf_UTF8 : 0 ),
4515                                     SVt_PVCV);
4516         if (indirgv && SvTYPE(indirgv) != SVt_NULL
4517          && (!isGV(indirgv) || GvCVu(indirgv)))
4518             return 0;
4519         /* filehandle or package name makes it a method */
4520         if (!cv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, UTF ? SVf_UTF8 : 0)) {
4521             s = skipspace(s);
4522             if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
4523                 return 0;       /* no assumptions -- "=>" quotes bareword */
4524       bare_package:
4525             NEXTVAL_NEXTTOKE.opval = newSVOP(OP_CONST, 0,
4526                                                   S_newSV_maybe_utf8(aTHX_ tmpbuf, len));
4527             NEXTVAL_NEXTTOKE.opval->op_private = OPpCONST_BARE;
4528             PL_expect = XTERM;
4529             force_next(BAREWORD);
4530             PL_bufptr = s;
4531             return *s == '(' ? FUNCMETH : METHOD;
4532         }
4533     }
4534     return 0;
4535 }
4536
4537 /* Encoded script support. filter_add() effectively inserts a
4538  * 'pre-processing' function into the current source input stream.
4539  * Note that the filter function only applies to the current source file
4540  * (e.g., it will not affect files 'require'd or 'use'd by this one).
4541  *
4542  * The datasv parameter (which may be NULL) can be used to pass
4543  * private data to this instance of the filter. The filter function
4544  * can recover the SV using the FILTER_DATA macro and use it to
4545  * store private buffers and state information.
4546  *
4547  * The supplied datasv parameter is upgraded to a PVIO type
4548  * and the IoDIRP/IoANY field is used to store the function pointer,
4549  * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
4550  * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
4551  * private use must be set using malloc'd pointers.
4552  */
4553
4554 SV *
4555 Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
4556 {
4557     if (!funcp)
4558         return NULL;
4559
4560     if (!PL_parser)
4561         return NULL;
4562
4563     if (PL_parser->lex_flags & LEX_IGNORE_UTF8_HINTS)
4564         Perl_croak(aTHX_ "Source filters apply only to byte streams");
4565
4566     if (!PL_rsfp_filters)
4567         PL_rsfp_filters = newAV();
4568     if (!datasv)
4569         datasv = newSV(0);
4570     SvUPGRADE(datasv, SVt_PVIO);
4571     IoANY(datasv) = FPTR2DPTR(void *, funcp); /* stash funcp into spare field */
4572     IoFLAGS(datasv) |= IOf_FAKE_DIRP;
4573     DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
4574                           FPTR2DPTR(void *, IoANY(datasv)),
4575                           SvPV_nolen(datasv)));
4576     av_unshift(PL_rsfp_filters, 1);
4577     av_store(PL_rsfp_filters, 0, datasv) ;
4578     if (
4579         !PL_parser->filtered
4580      && PL_parser->lex_flags & LEX_EVALBYTES
4581      && PL_bufptr < PL_bufend
4582     ) {
4583         const char *s = PL_bufptr;
4584         while (s < PL_bufend) {
4585             if (*s == '\n') {
4586                 SV *linestr = PL_parser->linestr;
4587                 char *buf = SvPVX(linestr);
4588                 STRLEN const bufptr_pos = PL_parser->bufptr - buf;
4589                 STRLEN const oldbufptr_pos = PL_parser->oldbufptr - buf;
4590                 STRLEN const oldoldbufptr_pos=PL_parser->oldoldbufptr-buf;
4591                 STRLEN const linestart_pos = PL_parser->linestart - buf;
4592                 STRLEN const last_uni_pos =
4593                     PL_parser->last_uni ? PL_parser->last_uni - buf : 0;
4594                 STRLEN const last_lop_pos =
4595                     PL_parser->last_lop ? PL_parser->last_lop - buf : 0;
4596                 av_push(PL_rsfp_filters, linestr);
4597                 PL_parser->linestr =
4598                     newSVpvn(SvPVX(linestr), ++s-SvPVX(linestr));
4599                 buf = SvPVX(PL_parser->linestr);
4600                 PL_parser->bufend = buf + SvCUR(PL_parser->linestr);
4601                 PL_parser->bufptr = buf + bufptr_pos;
4602                 PL_parser->oldbufptr = buf + oldbufptr_pos;
4603                 PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
4604                 PL_parser->linestart = buf + linestart_pos;
4605                 if (PL_parser->last_uni)
4606                     PL_parser->last_uni = buf + last_uni_pos;
4607                 if (PL_parser->last_lop)
4608                     PL_parser->last_lop = buf + last_lop_pos;
4609                 SvLEN_set(linestr, SvCUR(linestr));
4610                 SvCUR_set(linestr, s - SvPVX(linestr));
4611                 PL_parser->filtered = 1;
4612                 break;
4613             }
4614             s++;
4615         }
4616     }
4617     return(datasv);
4618 }
4619
4620
4621 /* Delete most recently added instance of this filter function. */
4622 void
4623 Perl_filter_del(pTHX_ filter_t funcp)
4624 {
4625     SV *datasv;
4626
4627     PERL_ARGS_ASSERT_FILTER_DEL;
4628
4629 #ifdef DEBUGGING
4630     DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p",
4631                           FPTR2DPTR(void*, funcp)));
4632 #endif
4633     if (!PL_parser || !PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
4634         return;
4635     /* if filter is on top of stack (usual case) just pop it off */
4636     datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
4637     if (IoANY(datasv) == FPTR2DPTR(void *, funcp)) {
4638         sv_free(av_pop(PL_rsfp_filters));
4639
4640         return;
4641     }
4642     /* we need to search for the correct entry and clear it     */
4643     Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
4644 }
4645
4646
4647 /* Invoke the idxth filter function for the current rsfp.        */
4648 /* maxlen 0 = read one text line */
4649 I32
4650 Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
4651 {
4652     filter_t funcp;
4653     I32 ret;
4654     SV *datasv = NULL;
4655     /* This API is bad. It should have been using unsigned int for maxlen.
4656        Not sure if we want to change the API, but if not we should sanity
4657        check the value here.  */
4658     unsigned int correct_length = maxlen < 0 ?  PERL_INT_MAX : maxlen;
4659
4660     PERL_ARGS_ASSERT_FILTER_READ;
4661
4662     if (!PL_parser || !PL_rsfp_filters)
4663         return -1;
4664     if (idx > AvFILLp(PL_rsfp_filters)) {       /* Any more filters?    */
4665         /* Provide a default input filter to make life easy.    */
4666         /* Note that we append to the line. This is handy.      */
4667         DEBUG_P(PerlIO_printf(Perl_debug_log,
4668                               "filter_read %d: from rsfp\n", idx));
4669         if (correct_length) {
4670             /* Want a block */
4671             int len ;
4672             const int old_len = SvCUR(buf_sv);
4673
4674             /* ensure buf_sv is large enough */
4675             SvGROW(buf_sv, (STRLEN)(old_len + correct_length + 1)) ;
4676             if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len,
4677                                    correct_length)) <= 0) {
4678                 if (PerlIO_error(PL_rsfp))
4679                     return -1;          /* error */
4680                 else
4681                     return 0 ;          /* end of file */
4682             }
4683             SvCUR_set(buf_sv, old_len + len) ;
4684             SvPVX(buf_sv)[old_len + len] = '\0';
4685         } else {
4686             /* Want a line */
4687             if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
4688                 if (PerlIO_error(PL_rsfp))
4689                     return -1;          /* error */
4690                 else
4691                     return 0 ;          /* end of file */
4692             }
4693         }
4694         return SvCUR(buf_sv);
4695     }
4696     /* Skip this filter slot if filter has been deleted */
4697     if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) {
4698         DEBUG_P(PerlIO_printf(Perl_debug_log,
4699                               "filter_read %d: skipped (filter deleted)\n",
4700                               idx));
4701         return FILTER_READ(idx+1, buf_sv, correct_length); /* recurse */
4702     }
4703     if (SvTYPE(datasv) != SVt_PVIO) {
4704         if (correct_length) {
4705             /* Want a block */
4706             const STRLEN remainder = SvLEN(datasv) - SvCUR(datasv);
4707             if (!remainder) return 0; /* eof */
4708             if (correct_length > remainder) correct_length = remainder;
4709             sv_catpvn(buf_sv, SvEND(datasv), correct_length);
4710             SvCUR_set(datasv, SvCUR(datasv) + correct_length);
4711         } else {
4712             /* Want a line */
4713             const char *s = SvEND(datasv);
4714             const char *send = SvPVX(datasv) + SvLEN(datasv);
4715             while (s < send) {
4716                 if (*s == '\n') {
4717                     s++;
4718                     break;
4719                 }
4720                 s++;
4721             }
4722             if (s == send) return 0; /* eof */
4723             sv_catpvn(buf_sv, SvEND(datasv), s-SvEND(datasv));
4724             SvCUR_set(datasv, s-SvPVX(datasv));
4725         }
4726         return SvCUR(buf_sv);
4727     }
4728     /* Get function pointer hidden within datasv        */
4729     funcp = DPTR2FPTR(filter_t, IoANY(datasv));
4730     DEBUG_P(PerlIO_printf(Perl_debug_log,
4731                           "filter_read %d: via function %p (%s)\n",
4732                           idx, (void*)datasv, SvPV_nolen_const(datasv)));
4733     /* Call function. The function is expected to       */
4734     /* call "FILTER_READ(idx+1, buf_sv)" first.         */
4735     /* Return: <0:error, =0:eof, >0:not eof             */
4736     ENTER;
4737     save_scalar(PL_errgv);
4738     ret = (*funcp)(aTHX_ idx, buf_sv, correct_length);
4739     LEAVE;
4740     return ret;
4741 }
4742
4743 STATIC char *
4744 S_filter_gets(pTHX_ SV *sv, STRLEN append)
4745 {
4746     PERL_ARGS_ASSERT_FILTER_GETS;
4747
4748 #ifdef PERL_CR_FILTER
4749     if (!PL_rsfp_filters) {
4750         filter_add(S_cr_textfilter,NULL);
4751     }
4752 #endif
4753     if (PL_rsfp_filters) {
4754         if (!append)
4755             SvCUR_set(sv, 0);   /* start with empty line        */
4756         if (FILTER_READ(0, sv, 0) > 0)
4757             return ( SvPVX(sv) ) ;
4758         else
4759             return NULL ;
4760     }
4761     else
4762         return (sv_gets(sv, PL_rsfp, append));
4763 }
4764
4765 STATIC HV *
4766 S_find_in_my_stash(pTHX_ const char *pkgname, STRLEN len)
4767 {
4768     GV *gv;
4769
4770     PERL_ARGS_ASSERT_FIND_IN_MY_STASH;
4771
4772     if (memEQs(pkgname, len, "__PACKAGE__"))
4773         return PL_curstash;
4774
4775     if (len > 2
4776         && (pkgname[len - 2] == ':' && pkgname[len - 1] == ':')
4777         && (gv = gv_fetchpvn_flags(pkgname,
4778                                    len,
4779                                    ( UTF ? SVf_UTF8 : 0 ), SVt_PVHV)))
4780     {
4781         return GvHV(gv);                        /* Foo:: */
4782     }
4783
4784     /* use constant CLASS => 'MyClass' */
4785     gv = gv_fetchpvn_flags(pkgname, len, UTF ? SVf_UTF8 : 0, SVt_PVCV);
4786     if (gv && GvCV(gv)) {
4787         SV * const sv = cv_const_sv(GvCV(gv));
4788         if (sv)
4789             return gv_stashsv(sv, 0);
4790     }
4791
4792     return gv_stashpvn(pkgname, len, UTF ? SVf_UTF8 : 0);
4793 }
4794
4795
4796 STATIC char *
4797 S_tokenize_use(pTHX_ int is_use, char *s) {
4798     PERL_ARGS_ASSERT_TOKENIZE_USE;
4799
4800     if (PL_expect != XSTATE)
4801         /* diag_listed_as: "use" not allowed in expression */
4802         yyerror(Perl_form(aTHX_ "\"%s\" not allowed in expression",
4803                     is_use ? "use" : "no"));
4804     PL_expect = XTERM;
4805     s = skipspace(s);
4806     if (isDIGIT(*s) || (*s == 'v' && isDIGIT(s[1]))) {
4807         s = force_version(s, TRUE);
4808         if (*s == ';' || *s == '}'
4809                 || (s = skipspace(s), (*s == ';' || *s == '}'))) {
4810             NEXTVAL_NEXTTOKE.opval = NULL;
4811             force_next(BAREWORD);
4812         }
4813         else if (*s == 'v') {
4814             s = force_word(s,BAREWORD,FALSE,TRUE);
4815             s = force_version(s, FALSE);
4816         }
4817     }
4818     else {
4819         s = force_word(s,BAREWORD,FALSE,TRUE);
4820         s = force_version(s, FALSE);
4821     }
4822     pl_yylval.ival = is_use;
4823     return s;
4824 }
4825 #ifdef DEBUGGING
4826     static const char* const exp_name[] =
4827         { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
4828           "ATTRTERM", "TERMBLOCK", "XBLOCKTERM", "POSTDEREF",
4829           "SIGVAR", "TERMORDORDOR"
4830         };
4831 #endif
4832
4833 #define word_takes_any_delimiter(p,l) S_word_takes_any_delimiter(p,l)
4834 STATIC bool
4835 S_word_takes_any_delimiter(char *p, STRLEN len)
4836 {
4837     return (len == 1 && memCHRs("msyq", p[0]))
4838             || (len == 2
4839                 && ((p[0] == 't' && p[1] == 'r')
4840                     || (p[0] == 'q' && memCHRs("qwxr", p[1]))));
4841 }
4842
4843 static void
4844 S_check_scalar_slice(pTHX_ char *s)
4845 {
4846     s++;
4847     while (SPACE_OR_TAB(*s)) s++;
4848     if (*s == 'q' && s[1] == 'w' && !isWORDCHAR_lazy_if_safe(s+2,
4849                                                              PL_bufend,
4850                                                              UTF))
4851     {
4852         return;
4853     }
4854     while (    isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF)
4855            || (*s && memCHRs(" \t$#+-'\"", *s)))
4856     {
4857         s += UTF ? UTF8SKIP(s) : 1;
4858     }
4859     if (*s == '}' || *s == ']')
4860         pl_yylval.ival = OPpSLICEWARNING;
4861 }
4862
4863 #define lex_token_boundary() S_lex_token_boundary(aTHX)
4864 static void
4865 S_lex_token_boundary(pTHX)
4866 {
4867     PL_oldoldbufptr = PL_oldbufptr;
4868     PL_oldbufptr = PL_bufptr;
4869 }
4870
4871 #define vcs_conflict_marker(s) S_vcs_conflict_marker(aTHX_ s)
4872 static char *
4873 S_vcs_conflict_marker(pTHX_ char *s)
4874 {
4875     lex_token_boundary();
4876     PL_bufptr = s;
4877     yyerror("Version control conflict marker");
4878     while (s < PL_bufend && *s != '\n')
4879         s++;
4880     return s;
4881 }
4882
4883 static int
4884 yyl_sigvar(pTHX_ char *s)
4885 {
4886     /* we expect the sigil and optional var name part of a
4887      * signature element here. Since a '$' is not necessarily
4888      * followed by a var name, handle it specially here; the general
4889      * yylex code would otherwise try to interpret whatever follows
4890      * as a var; e.g. ($, ...) would be seen as the var '$,'
4891      */
4892
4893     U8 sigil;
4894
4895     s = skipspace(s);
4896     sigil = *s++;
4897     PL_bufptr = s; /* for error reporting */
4898     switch (sigil) {
4899     case '$':
4900     case '@':
4901     case '%':
4902         /* spot stuff that looks like an prototype */
4903         if (memCHRs("$:@%&*;\\[]", *s)) {
4904             yyerror("Illegal character following sigil in a subroutine signature");
4905             break;
4906         }
4907         /* '$#' is banned, while '$ # comment' isn't */
4908         if (*s == '#') {
4909             yyerror("'#' not allowed immediately following a sigil in a subroutine signature");
4910             break;
4911         }
4912         s = skipspace(s);
4913         if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
4914             char *dest = PL_tokenbuf + 1;
4915             /* read var name, including sigil, into PL_tokenbuf */
4916             PL_tokenbuf[0] = sigil;
4917             parse_ident(&s, &dest, dest + sizeof(PL_tokenbuf) - 1,
4918                 0, cBOOL(UTF), FALSE, FALSE);
4919             *dest = '\0';
4920             assert(PL_tokenbuf[1]); /* we have a variable name */
4921         }
4922         else {
4923             *PL_tokenbuf = 0;
4924             PL_in_my = 0;
4925         }
4926
4927         s = skipspace(s);
4928         /* parse the = for the default ourselves to avoid '+=' etc being accepted here
4929          * as the ASSIGNOP, and exclude other tokens that start with =
4930          */
4931         if (*s == '=' && (!s[1] || memCHRs("=~>", s[1]) == 0)) {
4932             /* save now to report with the same context as we did when
4933              * all ASSIGNOPS were accepted */
4934             PL_oldbufptr = s;
4935
4936             ++s;
4937             NEXTVAL_NEXTTOKE.ival = 0;
4938             force_next(ASSIGNOP);
4939             PL_expect = XTERM;
4940         }
4941         else if (*s == ',' || *s == ')') {
4942             PL_expect = XOPERATOR;
4943         }
4944         else {
4945             /* make sure the context shows the unexpected character and
4946              * hopefully a bit more */
4947             if (*s) ++s;
4948             while (*s && *s != '$' && *s != '@' && *s != '%' && *s != ')')
4949                 s++;
4950             PL_bufptr = s; /* for error reporting */
4951             yyerror("Illegal operator following parameter in a subroutine signature");
4952             PL_in_my = 0;
4953         }
4954         if (*PL_tokenbuf) {
4955             NEXTVAL_NEXTTOKE.ival = sigil;
4956             force_next('p'); /* force a signature pending identifier */
4957         }
4958         break;
4959
4960     case ')':
4961         PL_expect = XBLOCK;
4962         break;
4963     case ',': /* handle ($a,,$b) */
4964         break;
4965
4966     default:
4967         PL_in_my = 0;
4968         yyerror("A signature parameter must start with '$', '@' or '%'");
4969         /* very crude error recovery: skip to likely next signature
4970          * element */
4971         while (*s && *s != '$' && *s != '@' && *s != '%' && *s != ')')
4972             s++;
4973         break;
4974     }
4975
4976     TOKEN(sigil);
4977 }
4978
4979 static int
4980 yyl_dollar(pTHX_ char *s)
4981 {
4982     CLINE;
4983
4984     if (PL_expect == XPOSTDEREF) {
4985         if (s[1] == '#') {
4986             s++;
4987             POSTDEREF(DOLSHARP);
4988         }
4989         POSTDEREF('$');
4990     }
4991
4992     if (   s[1] == '#'
4993         && (   isIDFIRST_lazy_if_safe(s+2, PL_bufend, UTF)
4994             || memCHRs("{$:+-@", s[2])))
4995     {
4996         PL_tokenbuf[0] = '@';
4997         s = scan_ident(s + 1, PL_tokenbuf + 1,
4998                        sizeof PL_tokenbuf - 1, FALSE);
4999         if (PL_expect == XOPERATOR) {
5000             char *d = s;
5001             if (PL_bufptr > s) {
5002                 d = PL_bufptr-1;
5003                 PL_bufptr = PL_oldbufptr;
5004             }
5005             no_op("Array length", d);
5006         }
5007         if (!PL_tokenbuf[1])
5008             PREREF(DOLSHARP);
5009         PL_expect = XOPERATOR;
5010         force_ident_maybe_lex('#');
5011         TOKEN(DOLSHARP);
5012     }
5013
5014     PL_tokenbuf[0] = '$';
5015     s = scan_ident(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
5016     if (PL_expect == XOPERATOR) {
5017         char *d = s;
5018         if (PL_bufptr > s) {
5019             d = PL_bufptr-1;
5020             PL_bufptr = PL_oldbufptr;
5021         }
5022         no_op("Scalar", d);
5023     }
5024     if (!PL_tokenbuf[1]) {
5025         if (s == PL_bufend)
5026             yyerror("Final $ should be \\$ or $name");
5027         PREREF('$');
5028     }
5029
5030     {
5031         const char tmp = *s;
5032         if (PL_lex_state == LEX_NORMAL || PL_lex_brackets)
5033             s = skipspace(s);
5034
5035         if (   (PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
5036             && intuit_more(s, PL_bufend)) {
5037             if (*s == '[') {
5038                 PL_tokenbuf[0] = '@';
5039                 if (ckWARN(WARN_SYNTAX)) {
5040                     char *t = s+1;
5041
5042                     while ( t < PL_bufend ) {
5043                         if (isSPACE(*t)) {
5044                             do { t += UTF ? UTF8SKIP(t) : 1; } while (t < PL_bufend && isSPACE(*t));
5045                             /* consumed one or more space chars */
5046                         } else if (*t == '$' || *t == '@') {
5047                             /* could be more than one '$' like $$ref or @$ref */
5048                             do { t++; } while (t < PL_bufend && *t == '$');
5049
5050                             /* could be an abigail style identifier like $ foo */
5051                             while (t < PL_bufend && *t == ' ') t++;
5052
5053                             /* strip off the name of the var */
5054                             while (isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF))
5055                                 t += UTF ? UTF8SKIP(t) : 1;
5056                             /* consumed a varname */
5057                         } else if (isDIGIT(*t)) {
5058                             /* deal with hex constants like 0x11 */
5059                             if (t[0] == '0' && t[1] == 'x') {
5060                                 t += 2;
5061                                 while (t < PL_bufend && isXDIGIT(*t)) t++;
5062                             } else {
5063                                 /* deal with decimal/octal constants like 1 and 0123 */
5064                                 do { t++; } while (isDIGIT(*t));
5065                                 if (t<PL_bufend && *t == '.') {
5066                                     do { t++; } while (isDIGIT(*t));
5067                                 }
5068                             }
5069                             /* consumed a number */
5070                         } else {
5071                             /* not a var nor a space nor a number */
5072                             break;
5073                         }
5074                     }
5075                     if (t < PL_bufend && *t++ == ',') {
5076                         PL_bufptr = skipspace(PL_bufptr); /* XXX can realloc */
5077                         while (t < PL_bufend && *t != ']')
5078                             t++;
5079                         Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5080                                     "Multidimensional syntax %" UTF8f " not supported",
5081                                     UTF8fARG(UTF,(int)((t - PL_bufptr) + 1), PL_bufptr));
5082                     }
5083                 }
5084             }
5085             else if (*s == '{') {
5086                 char *t;
5087                 PL_tokenbuf[0] = '%';
5088                 if (    strEQ(PL_tokenbuf+1, "SIG")
5089                     && ckWARN(WARN_SYNTAX)
5090                     && (t = (char *) memchr(s, '}', PL_bufend - s))
5091                     && (t = (char *) memchr(t, '=', PL_bufend - t)))
5092                 {
5093                     char tmpbuf[sizeof PL_tokenbuf];
5094                     do {
5095                         t++;
5096                     } while (isSPACE(*t));
5097                     if (isIDFIRST_lazy_if_safe(t, PL_bufend, UTF)) {
5098                         STRLEN len;
5099                         t = scan_word(t, tmpbuf, sizeof tmpbuf, TRUE,
5100                                         &len);
5101                         while (isSPACE(*t))
5102                             t++;
5103                         if (  *t == ';'
5104                             && get_cvn_flags(tmpbuf, len, UTF
5105                                                             ? SVf_UTF8
5106                                                             : 0))
5107                         {
5108                             Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5109                                 "You need to quote \"%" UTF8f "\"",
5110                                     UTF8fARG(UTF, len, tmpbuf));
5111                         }
5112                     }
5113                 }
5114             }
5115         }
5116
5117         PL_expect = XOPERATOR;
5118         if ((PL_lex_state == LEX_NORMAL || PL_lex_brackets) && isSPACE((char)tmp)) {
5119             const bool islop = (PL_last_lop == PL_oldoldbufptr);
5120             if (!islop || PL_last_lop_op == OP_GREPSTART)
5121                 PL_expect = XOPERATOR;
5122             else if (memCHRs("$@\"'`q", *s))
5123                 PL_expect = XTERM;              /* e.g. print $fh "foo" */
5124             else if (   memCHRs("&*<%", *s)
5125                      && isIDFIRST_lazy_if_safe(s+1, PL_bufend, UTF))
5126             {
5127                 PL_expect = XTERM;              /* e.g. print $fh &sub */
5128             }
5129             else if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
5130                 char tmpbuf[sizeof PL_tokenbuf];
5131                 int t2;
5132                 STRLEN len;
5133                 scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
5134                 if ((t2 = keyword(tmpbuf, len, 0))) {
5135                     /* binary operators exclude handle interpretations */
5136                     switch (t2) {
5137                     case -KEY_x:
5138                     case -KEY_eq:
5139                     case -KEY_ne:
5140                     case -KEY_gt:
5141                     case -KEY_lt:
5142                     case -KEY_ge:
5143                     case -KEY_le:
5144                     case -KEY_cmp:
5145                         break;
5146                     default:
5147                         PL_expect = XTERM;      /* e.g. print $fh length() */
5148                         break;
5149                     }
5150                 }
5151                 else {
5152                     PL_expect = XTERM;  /* e.g. print $fh subr() */
5153                 }
5154             }
5155             else if (isDIGIT(*s))
5156                 PL_expect = XTERM;              /* e.g. print $fh 3 */
5157             else if (*s == '.' && isDIGIT(s[1]))
5158                 PL_expect = XTERM;              /* e.g. print $fh .3 */
5159             else if ((*s == '?' || *s == '-' || *s == '+')
5160                      && !isSPACE(s[1]) && s[1] != '=')
5161                 PL_expect = XTERM;              /* e.g. print $fh -1 */
5162             else if (*s == '/' && !isSPACE(s[1]) && s[1] != '='
5163                      && s[1] != '/')
5164                 PL_expect = XTERM;              /* e.g. print $fh /.../
5165                                                XXX except DORDOR operator
5166                                             */
5167             else if (*s == '<' && s[1] == '<' && !isSPACE(s[2])
5168                      && s[2] != '=')
5169                 PL_expect = XTERM;              /* print $fh <<"EOF" */
5170         }
5171     }
5172     force_ident_maybe_lex('$');
5173     TOKEN('$');
5174 }
5175
5176 static int
5177 yyl_sub(pTHX_ char *s, const int key)
5178 {
5179     char * const tmpbuf = PL_tokenbuf + 1;
5180     bool have_name, have_proto;
5181     STRLEN len;
5182     SV *format_name = NULL;
5183     bool is_sigsub = FEATURE_SIGNATURES_IS_ENABLED;
5184
5185     SSize_t off = s-SvPVX(PL_linestr);
5186     char *d;
5187
5188     s = skipspace(s); /* can move PL_linestr */
5189
5190     d = SvPVX(PL_linestr)+off;
5191
5192     SAVEBOOL(PL_parser->sig_seen);
5193     PL_parser->sig_seen = FALSE;
5194
5195     if (   isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)
5196         || *s == '\''
5197         || (*s == ':' && s[1] == ':'))
5198     {
5199
5200         PL_expect = XATTRBLOCK;
5201         d = scan_word(s, tmpbuf, sizeof PL_tokenbuf - 1, TRUE,
5202                       &len);
5203         if (key == KEY_format)
5204             format_name = S_newSV_maybe_utf8(aTHX_ s, d - s);
5205         *PL_tokenbuf = '&';
5206         if (memchr(tmpbuf, ':', len) || key != KEY_sub
5207          || pad_findmy_pvn(
5208                 PL_tokenbuf, len + 1, 0
5209             ) != NOT_IN_PAD)
5210             sv_setpvn(PL_subname, tmpbuf, len);
5211         else {
5212             sv_setsv(PL_subname,PL_curstname);
5213             sv_catpvs(PL_subname,"::");
5214             sv_catpvn(PL_subname,tmpbuf,len);
5215         }
5216         if (SvUTF8(PL_linestr))
5217             SvUTF8_on(PL_subname);
5218         have_name = TRUE;
5219
5220         s = skipspace(d);
5221     }
5222     else {
5223         if (key == KEY_my || key == KEY_our || key==KEY_state) {
5224             *d = '\0';
5225             /* diag_listed_as: Missing name in "%s sub" */
5226             Perl_croak(aTHX_
5227                       "Missing name in \"%s\"", PL_bufptr);
5228         }
5229         PL_expect = XATTRTERM;
5230         sv_setpvs(PL_subname,"?");
5231         have_name = FALSE;
5232     }
5233
5234     if (key == KEY_format) {
5235         if (format_name) {
5236             NEXTVAL_NEXTTOKE.opval
5237                 = newSVOP(OP_CONST,0, format_name);
5238             NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
5239             force_next(BAREWORD);
5240         }
5241         PREBLOCK(FORMAT);
5242     }
5243
5244     /* Look for a prototype */
5245     if (*s == '(' && !is_sigsub) {
5246         s = scan_str(s,FALSE,FALSE,FALSE,NULL);
5247         if (!s)
5248             Perl_croak(aTHX_ "Prototype not terminated");
5249         COPLINE_SET_FROM_MULTI_END;
5250         (void)validate_proto(PL_subname, PL_lex_stuff,
5251                              ckWARN(WARN_ILLEGALPROTO), 0);
5252         have_proto = TRUE;
5253
5254         s = skipspace(s);
5255     }
5256     else
5257         have_proto = FALSE;
5258
5259     if (  !(*s == ':' && s[1] != ':')
5260         && (*s != '{' && *s != '(') && key != KEY_format)
5261     {
5262         assert(key == KEY_sub || key == KEY_AUTOLOAD ||
5263                key == KEY_DESTROY || key == KEY_BEGIN ||
5264                key == KEY_UNITCHECK || key == KEY_CHECK ||
5265                key == KEY_INIT || key == KEY_END ||
5266                key == KEY_my || key == KEY_state ||
5267                key == KEY_our);
5268         if (!have_name)
5269             Perl_croak(aTHX_ "Illegal declaration of anonymous subroutine");
5270         else if (*s != ';' && *s != '}')
5271             Perl_croak(aTHX_ "Illegal declaration of subroutine %" SVf, SVfARG(PL_subname));
5272     }
5273
5274     if (have_proto) {
5275         NEXTVAL_NEXTTOKE.opval =
5276             newSVOP(OP_CONST, 0, PL_lex_stuff);
5277         PL_lex_stuff = NULL;
5278         force_next(THING);
5279     }
5280     if (!have_name) {
5281         if (PL_curstash)
5282             sv_setpvs(PL_subname, "__ANON__");
5283         else
5284             sv_setpvs(PL_subname, "__ANON__::__ANON__");
5285         if (is_sigsub)
5286             TOKEN(ANON_SIGSUB);
5287         else
5288             TOKEN(ANONSUB);
5289     }
5290     force_ident_maybe_lex('&');
5291     if (is_sigsub)
5292         TOKEN(SIGSUB);
5293     else
5294         TOKEN(SUB);
5295 }
5296
5297 static int
5298 yyl_interpcasemod(pTHX_ char *s)
5299 {
5300 #ifdef DEBUGGING
5301     if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
5302         Perl_croak(aTHX_
5303                    "panic: INTERPCASEMOD bufptr=%p, bufend=%p, *bufptr=%u",
5304                    PL_bufptr, PL_bufend, *PL_bufptr);
5305 #endif
5306
5307     if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
5308         /* if at a \E */
5309         if (PL_lex_casemods) {
5310             const char oldmod = PL_lex_casestack[--PL_lex_casemods];
5311             PL_lex_casestack[PL_lex_casemods] = '\0';
5312
5313             if (PL_bufptr != PL_bufend
5314                 && (oldmod == 'L' || oldmod == 'U' || oldmod == 'Q'
5315                     || oldmod == 'F')) {
5316                 PL_bufptr += 2;
5317                 PL_lex_state = LEX_INTERPCONCAT;
5318             }
5319             PL_lex_allbrackets--;
5320             return REPORT(')');
5321         }
5322         else if ( PL_bufptr != PL_bufend && PL_bufptr[1] == 'E' ) {
5323            /* Got an unpaired \E */
5324            Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
5325                     "Useless use of \\E");
5326         }
5327         if (PL_bufptr != PL_bufend)
5328             PL_bufptr += 2;
5329         PL_lex_state = LEX_INTERPCONCAT;
5330         return yylex();
5331     }
5332     else {
5333         DEBUG_T({
5334             PerlIO_printf(Perl_debug_log, "### Saw case modifier\n");
5335         });
5336         s = PL_bufptr + 1;
5337         if (s[1] == '\\' && s[2] == 'E') {
5338             PL_bufptr = s + 3;
5339             PL_lex_state = LEX_INTERPCONCAT;
5340             return yylex();
5341         }
5342         else {
5343             I32 tmp;
5344             if (   memBEGINs(s, (STRLEN) (PL_bufend - s), "L\\u")
5345                 || memBEGINs(s, (STRLEN) (PL_bufend - s), "U\\l"))
5346             {
5347                 tmp = *s, *s = s[2], s[2] = (char)tmp;  /* misordered... */
5348             }
5349             if ((*s == 'L' || *s == 'U' || *s == 'F')
5350                 && (strpbrk(PL_lex_casestack, "LUF")))
5351             {
5352                 PL_lex_casestack[--PL_lex_casemods] = '\0';
5353                 PL_lex_allbrackets--;
5354                 return REPORT(')');
5355             }
5356             if (PL_lex_casemods > 10)
5357                 Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
5358             PL_lex_casestack[PL_lex_casemods++] = *s;
5359             PL_lex_casestack[PL_lex_casemods] = '\0';
5360             PL_lex_state = LEX_INTERPCONCAT;
5361             NEXTVAL_NEXTTOKE.ival = 0;
5362             force_next((2<<24)|'(');
5363             if (*s == 'l')
5364                 NEXTVAL_NEXTTOKE.ival = OP_LCFIRST;
5365             else if (*s == 'u')
5366                 NEXTVAL_NEXTTOKE.ival = OP_UCFIRST;
5367             else if (*s == 'L')
5368                 NEXTVAL_NEXTTOKE.ival = OP_LC;
5369             else if (*s == 'U')
5370                 NEXTVAL_NEXTTOKE.ival = OP_UC;
5371             else if (*s == 'Q')
5372                 NEXTVAL_NEXTTOKE.ival = OP_QUOTEMETA;
5373             else if (*s == 'F')
5374                 NEXTVAL_NEXTTOKE.ival = OP_FC;
5375             else
5376                 Perl_croak(aTHX_ "panic: yylex, *s=%u", *s);
5377             PL_bufptr = s + 1;
5378         }
5379         force_next(FUNC);
5380         if (PL_lex_starts) {
5381             s = PL_bufptr;
5382             PL_lex_starts = 0;
5383             /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
5384             if (PL_lex_casemods == 1 && PL_lex_inpat)
5385                 TOKEN(',');
5386             else
5387                 AopNOASSIGN(OP_CONCAT);
5388         }
5389         else
5390             return yylex();
5391     }
5392 }
5393
5394 static int
5395 yyl_secondclass_keyword(pTHX_ char *s, STRLEN len, int key, I32 *orig_keyword,
5396                         GV **pgv, GV ***pgvp)
5397 {
5398     GV *ogv = NULL;     /* override (winner) */
5399     GV *hgv = NULL;     /* hidden (loser) */
5400     GV *gv = *pgv;
5401
5402     if (PL_expect != XOPERATOR && (*s != ':' || s[1] != ':')) {
5403         CV *cv;
5404         if ((gv = gv_fetchpvn_flags(PL_tokenbuf, len,
5405                                     (UTF ? SVf_UTF8 : 0)|GV_NOTQUAL,
5406                                     SVt_PVCV))
5407             && (cv = GvCVu(gv)))
5408         {
5409             if (GvIMPORTED_CV(gv))
5410                 ogv = gv;
5411             else if (! CvMETHOD(cv))
5412                 hgv = gv;
5413         }
5414         if (!ogv
5415             && (*pgvp = (GV**)hv_fetch(PL_globalstash, PL_tokenbuf, len, FALSE))
5416             && (gv = **pgvp)
5417             && (isGV_with_GP(gv)
5418                 ? GvCVu(gv) && GvIMPORTED_CV(gv)
5419                 :   SvPCS_IMPORTED(gv)
5420                 && (gv_init(gv, PL_globalstash, PL_tokenbuf,
5421                                                          len, 0), 1)))
5422         {
5423             ogv = gv;
5424         }
5425     }
5426
5427     *pgv = gv;
5428
5429     if (ogv) {
5430         *orig_keyword = key;
5431         return 0;               /* overridden by import or by GLOBAL */
5432     }
5433     else if (gv && !*pgvp
5434              && -key==KEY_lock  /* XXX generalizable kludge */
5435              && GvCVu(gv))
5436     {
5437         return 0;               /* any sub overrides "weak" keyword */
5438     }
5439     else {                      /* no override */
5440         key = -key;
5441         if (key == KEY_dump) {
5442             Perl_croak(aTHX_ "dump() must be written as CORE::dump() as of Perl 5.30");
5443         }
5444         *pgv = NULL;
5445         *pgvp = 0;
5446         if (hgv && key != KEY_x)        /* never ambiguous */
5447             Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
5448                            "Ambiguous call resolved as CORE::%s(), "
5449                            "qualify as such or use &",
5450                            GvENAME(hgv));
5451         return key;
5452     }
5453 }
5454
5455 static int
5456 yyl_qw(pTHX_ char *s, STRLEN len)
5457 {
5458     OP *words = NULL;
5459
5460     s = scan_str(s,FALSE,FALSE,FALSE,NULL);
5461     if (!s)
5462         missingterm(NULL, 0);
5463
5464     COPLINE_SET_FROM_MULTI_END;
5465     PL_expect = XOPERATOR;
5466     if (SvCUR(PL_lex_stuff)) {
5467         int warned_comma = !ckWARN(WARN_QW);
5468         int warned_comment = warned_comma;
5469         char *d = SvPV_force(PL_lex_stuff, len);
5470         while (len) {
5471             for (; isSPACE(*d) && len; --len, ++d)
5472                 /**/;
5473             if (len) {
5474                 SV *sv;
5475                 const char *b = d;
5476                 if (!warned_comma || !warned_comment) {
5477                     for (; !isSPACE(*d) && len; --len, ++d) {
5478                         if (!warned_comma && *d == ',') {
5479                             Perl_warner(aTHX_ packWARN(WARN_QW),
5480                                 "Possible attempt to separate words with commas");
5481                             ++warned_comma;
5482                         }
5483                         else if (!warned_comment && *d == '#') {
5484                             Perl_warner(aTHX_ packWARN(WARN_QW),
5485                                 "Possible attempt to put comments in qw() list");
5486                             ++warned_comment;
5487                         }
5488                     }
5489                 }
5490                 else {
5491                     for (; !isSPACE(*d) && len; --len, ++d)
5492                         /**/;
5493                 }
5494                 sv = newSVpvn_utf8(b, d-b, DO_UTF8(PL_lex_stuff));
5495                 words = op_append_elem(OP_LIST, words,
5496                                        newSVOP(OP_CONST, 0, tokeq(sv)));
5497             }
5498         }
5499     }
5500     if (!words)
5501         words = newNULLLIST();
5502     SvREFCNT_dec_NN(PL_lex_stuff);
5503     PL_lex_stuff = NULL;
5504     PL_expect = XOPERATOR;
5505     pl_yylval.opval = sawparens(words);
5506     TOKEN(QWLIST);
5507 }
5508
5509 static int
5510 yyl_hyphen(pTHX_ char *s)
5511 {
5512     if (s[1] && isALPHA(s[1]) && !isWORDCHAR(s[2])) {
5513         I32 ftst = 0;
5514         char tmp;
5515
5516         s++;
5517         PL_bufptr = s;
5518         tmp = *s++;
5519
5520         while (s < PL_bufend && SPACE_OR_TAB(*s))
5521             s++;
5522
5523         if (memBEGINs(s, (STRLEN) (PL_bufend - s), "=>")) {
5524             s = force_word(PL_bufptr,BAREWORD,FALSE,FALSE);
5525             DEBUG_T( { printbuf("### Saw unary minus before =>, forcing word %s\n", s); } );
5526             OPERATOR('-');              /* unary minus */
5527         }
5528         switch (tmp) {
5529         case 'r': ftst = OP_FTEREAD;    break;
5530         case 'w': ftst = OP_FTEWRITE;   break;
5531         case 'x': ftst = OP_FTEEXEC;    break;
5532         case 'o': ftst = OP_FTEOWNED;   break;
5533         case 'R': ftst = OP_FTRREAD;    break;
5534         case 'W': ftst = OP_FTRWRITE;   break;
5535         case 'X': ftst = OP_FTREXEC;    break;
5536         case 'O': ftst = OP_FTROWNED;   break;
5537         case 'e': ftst = OP_FTIS;       break;
5538         case 'z': ftst = OP_FTZERO;     break;
5539         case 's': ftst = OP_FTSIZE;     break;
5540         case 'f': ftst = OP_FTFILE;     break;
5541         case 'd': ftst = OP_FTDIR;      break;
5542         case 'l': ftst = OP_FTLINK;     break;
5543         case 'p': ftst = OP_FTPIPE;     break;
5544         case 'S': ftst = OP_FTSOCK;     break;
5545         case 'u': ftst = OP_FTSUID;     break;
5546         case 'g': ftst = OP_FTSGID;     break;
5547         case 'k': ftst = OP_FTSVTX;     break;
5548         case 'b': ftst = OP_FTBLK;      break;
5549         case 'c': ftst = OP_FTCHR;      break;
5550         case 't': ftst = OP_FTTTY;      break;
5551         case 'T': ftst = OP_FTTEXT;     break;
5552         case 'B': ftst = OP_FTBINARY;   break;
5553         case 'M': case 'A': case 'C':
5554             gv_fetchpvs("\024", GV_ADD|GV_NOTQUAL, SVt_PV);
5555             switch (tmp) {
5556             case 'M': ftst = OP_FTMTIME; break;
5557             case 'A': ftst = OP_FTATIME; break;
5558             case 'C': ftst = OP_FTCTIME; break;
5559             default:                     break;
5560             }
5561             break;
5562         default:
5563             break;
5564         }
5565         if (ftst) {
5566             PL_last_uni = PL_oldbufptr;
5567             PL_last_lop_op = (OPCODE)ftst;
5568             DEBUG_T( {
5569                 PerlIO_printf(Perl_debug_log, "### Saw file test %c\n", (int)tmp);
5570             } );
5571             FTST(ftst);
5572         }
5573         else {
5574             /* Assume it was a minus followed by a one-letter named
5575              * subroutine call (or a -bareword), then. */
5576             DEBUG_T( {
5577                 PerlIO_printf(Perl_debug_log,
5578                     "### '-%c' looked like a file test but was not\n",
5579                     (int) tmp);
5580             } );
5581             s = --PL_bufptr;
5582         }
5583     }
5584     {
5585         const char tmp = *s++;
5586         if (*s == tmp) {
5587             s++;
5588             if (PL_expect == XOPERATOR)
5589                 TERM(POSTDEC);
5590             else
5591                 OPERATOR(PREDEC);
5592         }
5593         else if (*s == '>') {
5594             s++;
5595             s = skipspace(s);
5596             if (((*s == '$' || *s == '&') && s[1] == '*')
5597               ||(*s == '$' && s[1] == '#' && s[2] == '*')
5598               ||((*s == '@' || *s == '%') && memCHRs("*[{", s[1]))
5599               ||(*s == '*' && (s[1] == '*' || s[1] == '{'))
5600              )
5601             {
5602                 PL_expect = XPOSTDEREF;
5603                 TOKEN(ARROW);
5604             }
5605             if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
5606                 s = force_word(s,METHOD,FALSE,TRUE);
5607                 TOKEN(ARROW);
5608             }
5609             else if (*s == '$')
5610                 OPERATOR(ARROW);
5611             else
5612                 TERM(ARROW);
5613         }
5614         if (PL_expect == XOPERATOR) {
5615             if (*s == '='
5616                 && !PL_lex_allbrackets
5617                 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5618             {
5619                 s--;
5620                 TOKEN(0);
5621             }
5622             Aop(OP_SUBTRACT);
5623         }
5624         else {
5625             if (isSPACE(*s) || !isSPACE(*PL_bufptr))
5626                 check_uni();
5627             OPERATOR('-');              /* unary minus */
5628         }
5629     }
5630 }
5631
5632 static int
5633 yyl_plus(pTHX_ char *s)
5634 {
5635     const char tmp = *s++;
5636     if (*s == tmp) {
5637         s++;
5638         if (PL_expect == XOPERATOR)
5639             TERM(POSTINC);
5640         else
5641             OPERATOR(PREINC);
5642     }
5643     if (PL_expect == XOPERATOR) {
5644         if (*s == '='
5645             && !PL_lex_allbrackets
5646             && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5647         {
5648             s--;
5649             TOKEN(0);
5650         }
5651         Aop(OP_ADD);
5652     }
5653     else {
5654         if (isSPACE(*s) || !isSPACE(*PL_bufptr))
5655             check_uni();
5656         OPERATOR('+');
5657     }
5658 }
5659
5660 static int
5661 yyl_star(pTHX_ char *s)
5662 {
5663     if (PL_expect == XPOSTDEREF)
5664         POSTDEREF('*');
5665
5666     if (PL_expect != XOPERATOR) {
5667         s = scan_ident(s, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
5668         PL_expect = XOPERATOR;
5669         force_ident(PL_tokenbuf, '*');
5670         if (!*PL_tokenbuf)
5671             PREREF('*');
5672         TERM('*');
5673     }
5674
5675     s++;
5676     if (*s == '*') {
5677         s++;
5678         if (*s == '=' && !PL_lex_allbrackets
5679             && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5680         {
5681             s -= 2;
5682             TOKEN(0);
5683         }
5684         PWop(OP_POW);
5685     }
5686
5687     if (*s == '='
5688         && !PL_lex_allbrackets
5689         && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5690     {
5691         s--;
5692         TOKEN(0);
5693     }
5694
5695     Mop(OP_MULTIPLY);
5696 }
5697
5698 static int
5699 yyl_percent(pTHX_ char *s)
5700 {
5701     if (PL_expect == XOPERATOR) {
5702         if (s[1] == '='
5703             && !PL_lex_allbrackets
5704             && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
5705         {
5706             TOKEN(0);
5707         }
5708         ++s;
5709         Mop(OP_MODULO);
5710     }
5711     else if (PL_expect == XPOSTDEREF)
5712         POSTDEREF('%');
5713
5714     PL_tokenbuf[0] = '%';
5715     s = scan_ident(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
5716     pl_yylval.ival = 0;
5717     if (!PL_tokenbuf[1]) {
5718         PREREF('%');
5719     }
5720     if (   (PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
5721         && intuit_more(s, PL_bufend)) {
5722         if (*s == '[')
5723             PL_tokenbuf[0] = '@';
5724     }
5725     PL_expect = XOPERATOR;
5726     force_ident_maybe_lex('%');
5727     TERM('%');
5728 }
5729
5730 static int
5731 yyl_caret(pTHX_ char *s)
5732 {
5733     char *d = s;
5734     const bool bof = cBOOL(FEATURE_BITWISE_IS_ENABLED);
5735     if (bof && s[1] == '.')
5736         s++;
5737     if (!PL_lex_allbrackets && PL_lex_fakeeof >=
5738             (s[1] == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_BITWISE))
5739     {
5740         s = d;
5741         TOKEN(0);
5742     }
5743     s++;
5744     BOop(bof ? d == s-2 ? OP_SBIT_XOR : OP_NBIT_XOR : OP_BIT_XOR);
5745 }
5746
5747 static int
5748 yyl_colon(pTHX_ char *s)
5749 {
5750     OP *attrs;
5751
5752     switch (PL_expect) {
5753     case XOPERATOR:
5754         if (!PL_in_my || (PL_lex_state != LEX_NORMAL && !PL_lex_brackets))
5755             break;
5756         PL_bufptr = s;  /* update in case we back off */
5757         if (*s == '=') {
5758             Perl_croak(aTHX_
5759                        "Use of := for an empty attribute list is not allowed");
5760         }
5761         goto grabattrs;
5762     case XATTRBLOCK:
5763         PL_expect = XBLOCK;
5764         goto grabattrs;
5765     case XATTRTERM:
5766         PL_expect = XTERMBLOCK;
5767      grabattrs:
5768         /* NB: as well as parsing normal attributes, we also end up
5769          * here if there is something looking like attributes
5770          * following a signature (which is illegal, but used to be
5771          * legal in 5.20..5.26). If the latter, we still parse the
5772          * attributes so that error messages(s) are less confusing,
5773          * but ignore them (parser->sig_seen).
5774          */
5775         s = skipspace(s);
5776         attrs = NULL;
5777         while (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
5778             bool sig = PL_parser->sig_seen;
5779             I32 tmp;
5780             SV *sv;
5781             STRLEN len;
5782             char *d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
5783             if (isLOWER(*s) && (tmp = keyword(PL_tokenbuf, len, 0))) {
5784                 if (tmp < 0) tmp = -tmp;
5785                 switch (tmp) {
5786                 case KEY_or:
5787                 case KEY_and:
5788                 case KEY_for:
5789                 case KEY_foreach:
5790                 case KEY_unless:
5791                 case KEY_if:
5792                 case KEY_while:
5793                 case KEY_until:
5794                     goto got_attrs;
5795                 default:
5796                     break;
5797                 }
5798             }
5799             sv = newSVpvn_flags(s, len, UTF ? SVf_UTF8 : 0);
5800             if (*d == '(') {
5801                 d = scan_str(d,TRUE,TRUE,FALSE,NULL);
5802                 if (!d) {
5803                     if (attrs)
5804                         op_free(attrs);
5805                     sv_free(sv);
5806                     Perl_croak(aTHX_ "Unterminated attribute parameter in attribute list");
5807                 }
5808                 COPLINE_SET_FROM_MULTI_END;
5809             }
5810             if (PL_lex_stuff) {
5811                 sv_catsv(sv, PL_lex_stuff);
5812                 attrs = op_append_elem(OP_LIST, attrs,
5813                                     newSVOP(OP_CONST, 0, sv));
5814                 SvREFCNT_dec_NN(PL_lex_stuff);
5815                 PL_lex_stuff = NULL;
5816             }
5817             else {
5818                 /* NOTE: any CV attrs applied here need to be part of
5819                    the CVf_BUILTIN_ATTRS define in cv.h! */
5820                 if (!PL_in_my && memEQs(SvPVX(sv), len, "lvalue")) {
5821                     sv_free(sv);
5822                     if (!sig)
5823                         CvLVALUE_on(PL_compcv);
5824                 }
5825                 else if (!PL_in_my && memEQs(SvPVX(sv), len, "method")) {
5826                     sv_free(sv);
5827                     if (!sig)
5828                         CvMETHOD_on(PL_compcv);
5829                 }
5830                 else if (!PL_in_my && memEQs(SvPVX(sv), len, "const")) {
5831                     sv_free(sv);
5832                     if (!sig) {
5833                         Perl_ck_warner_d(aTHX_
5834                             packWARN(WARN_EXPERIMENTAL__CONST_ATTR),
5835                            ":const is experimental"
5836                         );
5837                         CvANONCONST_on(PL_compcv);
5838                         if (!CvANON(PL_compcv))
5839                             yyerror(":const is not permitted on named "
5840                                     "subroutines");
5841                     }
5842                 }
5843                 /* After we've set the flags, it could be argued that
5844                    we don't need to do the attributes.pm-based setting
5845                    process, and shouldn't bother appending recognized
5846                    flags.  To experiment with that, uncomment the
5847                    following "else".  (Note that's already been
5848                    uncommented.  That keeps the above-applied built-in
5849                    attributes from being intercepted (and possibly
5850                    rejected) by a package's attribute routines, but is
5851                    justified by the performance win for the common case
5852                    of applying only built-in attributes.) */
5853                 else
5854                     attrs = op_append_elem(OP_LIST, attrs,
5855                                         newSVOP(OP_CONST, 0,
5856                                                 sv));
5857             }
5858             s = skipspace(d);
5859             if (*s == ':' && s[1] != ':')
5860                 s = skipspace(s+1);
5861             else if (s == d)
5862                 break;  /* require real whitespace or :'s */
5863             /* XXX losing whitespace on sequential attributes here */
5864         }
5865
5866         if (*s != ';'
5867             && *s != '}'
5868             && !(PL_expect == XOPERATOR
5869                  ? (*s == '=' ||  *s == ')')
5870                  : (*s == '{' ||  *s == '(')))
5871         {
5872             const char q = ((*s == '\'') ? '"' : '\'');
5873             /* If here for an expression, and parsed no attrs, back off. */
5874             if (PL_expect == XOPERATOR && !attrs) {
5875                 s = PL_bufptr;
5876                 break;
5877             }
5878             /* MUST advance bufptr here to avoid bogus "at end of line"
5879                context messages from yyerror().
5880             */
5881             PL_bufptr = s;
5882             yyerror( (const char *)
5883                      (*s
5884                       ? Perl_form(aTHX_ "Invalid separator character "
5885                                   "%c%c%c in attribute list", q, *s, q)
5886                       : "Unterminated attribute list" ) );
5887             if (attrs)
5888                 op_free(attrs);
5889             OPERATOR(':');
5890         }
5891
5892     got_attrs:
5893         if (PL_parser->sig_seen) {
5894             /* see comment about about sig_seen and parser error
5895              * handling */
5896             if (attrs)
5897                 op_free(attrs);
5898             Perl_croak(aTHX_ "Subroutine attributes must come "
5899                              "before the signature");
5900         }
5901         if (attrs) {
5902             NEXTVAL_NEXTTOKE.opval = attrs;
5903             force_next(THING);
5904         }
5905         TOKEN(COLONATTR);
5906     }
5907
5908     if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_CLOSING) {
5909         s--;
5910         TOKEN(0);
5911     }
5912
5913     PL_lex_allbrackets--;
5914     OPERATOR(':');
5915 }
5916
5917 static int
5918 yyl_subproto(pTHX_ char *s, CV *cv)
5919 {
5920     STRLEN protolen = CvPROTOLEN(cv);
5921     const char *proto = CvPROTO(cv);
5922     bool optional;
5923
5924     proto = S_strip_spaces(aTHX_ proto, &protolen);
5925     if (!protolen)
5926         TERM(FUNC0SUB);
5927     if ((optional = *proto == ';')) {
5928         do {
5929             proto++;
5930         } while (*proto == ';');
5931     }
5932
5933     if (
5934         (
5935             (
5936                 *proto == '$' || *proto == '_'
5937              || *proto == '*' || *proto == '+'
5938             )
5939          && proto[1] == '\0'
5940         )
5941      || (
5942          *proto == '\\' && proto[1] && proto[2] == '\0'
5943         )
5944     ) {
5945         UNIPROTO(UNIOPSUB,optional);
5946     }
5947
5948     if (*proto == '\\' && proto[1] == '[') {
5949         const char *p = proto + 2;
5950         while(*p && *p != ']')
5951             ++p;
5952         if(*p == ']' && !p[1])
5953             UNIPROTO(UNIOPSUB,optional);
5954     }
5955
5956     if (*proto == '&' && *s == '{') {
5957         if (PL_curstash)
5958             sv_setpvs(PL_subname, "__ANON__");
5959         else
5960             sv_setpvs(PL_subname, "__ANON__::__ANON__");
5961         if (!PL_lex_allbrackets
5962             && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
5963         {
5964             PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
5965         }
5966         PREBLOCK(LSTOPSUB);
5967     }
5968
5969     return KEY_NULL;
5970 }
5971
5972 static int
5973 yyl_leftcurly(pTHX_ char *s, const U8 formbrack)
5974 {
5975     char *d;
5976     if (PL_lex_brackets > 100) {
5977         Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
5978     }
5979
5980     switch (PL_expect) {
5981     case XTERM:
5982     case XTERMORDORDOR:
5983         PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
5984         PL_lex_allbrackets++;
5985         OPERATOR(HASHBRACK);
5986     case XOPERATOR:
5987         while (s < PL_bufend && SPACE_OR_TAB(*s))
5988             s++;
5989         d = s;
5990         PL_tokenbuf[0] = '\0';
5991         if (d < PL_bufend && *d == '-') {
5992             PL_tokenbuf[0] = '-';
5993             d++;
5994             while (d < PL_bufend && SPACE_OR_TAB(*d))
5995                 d++;
5996         }
5997         if (d < PL_bufend && isIDFIRST_lazy_if_safe(d, PL_bufend, UTF)) {
5998             STRLEN len;
5999             d = scan_word(d, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
6000                           FALSE, &len);
6001             while (d < PL_bufend && SPACE_OR_TAB(*d))
6002                 d++;
6003             if (*d == '}') {
6004                 const char minus = (PL_tokenbuf[0] == '-');
6005                 s = force_word(s + minus, BAREWORD, FALSE, TRUE);
6006                 if (minus)
6007                     force_next('-');
6008             }
6009         }
6010         /* FALLTHROUGH */
6011     case XATTRTERM:
6012     case XTERMBLOCK:
6013         PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
6014         PL_lex_allbrackets++;
6015         PL_expect = XSTATE;
6016         break;
6017     case XATTRBLOCK:
6018     case XBLOCK:
6019         PL_lex_brackstack[PL_lex_brackets++] = XSTATE;
6020         PL_lex_allbrackets++;
6021         PL_expect = XSTATE;
6022         break;
6023     case XBLOCKTERM:
6024         PL_lex_brackstack[PL_lex_brackets++] = XTERM;
6025         PL_lex_allbrackets++;
6026         PL_expect = XSTATE;
6027         break;
6028     default: {
6029             const char *t;
6030             if (PL_oldoldbufptr == PL_last_lop)
6031                 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
6032             else
6033                 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
6034             PL_lex_allbrackets++;
6035             s = skipspace(s);
6036             if (*s == '}') {
6037                 if (PL_expect == XREF && PL_lex_state == LEX_INTERPNORMAL) {
6038                     PL_expect = XTERM;
6039                     /* This hack is to get the ${} in the message. */
6040                     PL_bufptr = s+1;
6041                     yyerror("syntax error");
6042                     break;
6043                 }
6044                 OPERATOR(HASHBRACK);
6045             }
6046             if (PL_expect == XREF && PL_oldoldbufptr != PL_last_lop) {
6047                 /* ${...} or @{...} etc., but not print {...}
6048                  * Skip the disambiguation and treat this as a block.
6049                  */
6050                 goto block_expectation;
6051             }
6052             /* This hack serves to disambiguate a pair of curlies
6053              * as being a block or an anon hash.  Normally, expectation
6054              * determines that, but in cases where we're not in a
6055              * position to expect anything in particular (like inside
6056              * eval"") we have to resolve the ambiguity.  This code
6057              * covers the case where the first term in the curlies is a
6058              * quoted string.  Most other cases need to be explicitly
6059              * disambiguated by prepending a "+" before the opening
6060              * curly in order to force resolution as an anon hash.
6061              *
6062              * XXX should probably propagate the outer expectation
6063              * into eval"" to rely less on this hack, but that could
6064              * potentially break current behavior of eval"".
6065              * GSAR 97-07-21
6066              */
6067             t = s;
6068             if (*s == '\'' || *s == '"' || *s == '`') {
6069                 /* common case: get past first string, handling escapes */
6070                 for (t++; t < PL_bufend && *t != *s;)
6071                     if (*t++ == '\\')
6072                         t++;
6073                 t++;
6074             }
6075             else if (*s == 'q') {
6076                 if (++t < PL_bufend
6077                     && (!isWORDCHAR(*t)
6078                         || ((*t == 'q' || *t == 'x') && ++t < PL_bufend
6079                             && !isWORDCHAR(*t))))
6080                 {
6081                     /* skip q//-like construct */
6082                     const char *tmps;
6083                     char open, close, term;
6084                     I32 brackets = 1;
6085
6086                     while (t < PL_bufend && isSPACE(*t))
6087                         t++;
6088                     /* check for q => */
6089                     if (t+1 < PL_bufend && t[0] == '=' && t[1] == '>') {
6090                         OPERATOR(HASHBRACK);
6091                     }
6092                     term = *t;
6093                     open = term;
6094                     if (term && (tmps = memCHRs("([{< )]}> )]}>",term)))
6095                         term = tmps[5];
6096                     close = term;
6097                     if (open == close)
6098                         for (t++; t < PL_bufend; t++) {
6099                             if (*t == '\\' && t+1 < PL_bufend && open != '\\')
6100                                 t++;
6101                             else if (*t == open)
6102                                 break;
6103                         }
6104                     else {
6105                         for (t++; t < PL_bufend; t++) {
6106                             if (*t == '\\' && t+1 < PL_bufend)
6107                                 t++;
6108                             else if (*t == close && --brackets <= 0)
6109                                 break;
6110                             else if (*t == open)
6111                                 brackets++;
6112                         }
6113                     }
6114                     t++;
6115                 }
6116                 else
6117                     /* skip plain q word */
6118                     while (   t < PL_bufend
6119                            && isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF))
6120                     {
6121                         t += UTF ? UTF8SKIP(t) : 1;
6122                     }
6123             }
6124             else if (isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF)) {
6125                 t += UTF ? UTF8SKIP(t) : 1;
6126                 while (   t < PL_bufend
6127                        && isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF))
6128                 {
6129                     t += UTF ? UTF8SKIP(t) : 1;
6130                 }
6131             }
6132             while (t < PL_bufend && isSPACE(*t))
6133                 t++;
6134             /* if comma follows first term, call it an anon hash */
6135             /* XXX it could be a comma expression with loop modifiers */
6136             if (t < PL_bufend && ((*t == ',' && (*s == 'q' || !isLOWER(*s)))
6137                                || (*t == '=' && t[1] == '>')))
6138                 OPERATOR(HASHBRACK);
6139             if (PL_expect == XREF) {
6140               block_expectation:
6141                 /* If there is an opening brace or 'sub:', treat it
6142                    as a term to make ${{...}}{k} and &{sub:attr...}
6143                    dwim.  Otherwise, treat it as a statement, so
6144                    map {no strict; ...} works.
6145                  */
6146                 s = skipspace(s);
6147                 if (*s == '{') {
6148                     PL_expect = XTERM;
6149                     break;
6150                 }
6151                 if (memBEGINs(s, (STRLEN) (PL_bufend - s), "sub")) {
6152                     PL_bufptr = s;
6153                     d = s + 3;
6154                     d = skipspace(d);
6155                     s = PL_bufptr;
6156                     if (*d == ':') {
6157                         PL_expect = XTERM;
6158                         break;
6159                     }
6160                 }
6161                 PL_expect = XSTATE;
6162             }
6163             else {
6164                 PL_lex_brackstack[PL_lex_brackets-1] = XSTATE;
6165                 PL_expect = XSTATE;
6166             }
6167         }
6168         break;
6169     }
6170
6171     pl_yylval.ival = CopLINE(PL_curcop);
6172     PL_copline = NOLINE;   /* invalidate current command line number */
6173     TOKEN(formbrack ? '=' : '{');
6174 }
6175
6176 static int
6177 yyl_rightcurly(pTHX_ char *s, const U8 formbrack)
6178 {
6179     assert(s != PL_bufend);
6180     s++;
6181
6182     if (PL_lex_brackets <= 0)
6183         /* diag_listed_as: Unmatched right %s bracket */
6184         yyerror("Unmatched right curly bracket");
6185     else
6186         PL_expect = (expectation)PL_lex_brackstack[--PL_lex_brackets];
6187
6188     PL_lex_allbrackets--;
6189
6190     if (PL_lex_state == LEX_INTERPNORMAL) {
6191         if (PL_lex_brackets == 0) {
6192             if (PL_expect & XFAKEBRACK) {
6193                 PL_expect &= XENUMMASK;
6194                 PL_lex_state = LEX_INTERPEND;
6195                 PL_bufptr = s;
6196                 return yylex(); /* ignore fake brackets */
6197             }
6198             if (PL_lex_inwhat == OP_SUBST && PL_lex_repl == PL_linestr
6199              && SvEVALED(PL_lex_repl))
6200                 PL_lex_state = LEX_INTERPEND;
6201             else if (*s == '-' && s[1] == '>')
6202                 PL_lex_state = LEX_INTERPENDMAYBE;
6203             else if (*s != '[' && *s != '{')
6204                 PL_lex_state = LEX_INTERPEND;
6205         }
6206     }
6207
6208     if (PL_expect & XFAKEBRACK) {
6209         PL_expect &= XENUMMASK;
6210         PL_bufptr = s;
6211         return yylex();         /* ignore fake brackets */
6212     }
6213
6214     force_next(formbrack ? '.' : '}');
6215     if (formbrack) LEAVE_with_name("lex_format");
6216     if (formbrack == 2) { /* means . where arguments were expected */
6217         force_next(';');
6218         TOKEN(FORMRBRACK);
6219     }
6220
6221     TOKEN(';');
6222 }
6223
6224 static int
6225 yyl_ampersand(pTHX_ char *s)
6226 {
6227     if (PL_expect == XPOSTDEREF)
6228         POSTDEREF('&');
6229
6230     s++;
6231     if (*s++ == '&') {
6232         if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6233                 (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_LOGIC)) {
6234             s -= 2;
6235             TOKEN(0);
6236         }
6237         AOPERATOR(ANDAND);
6238     }
6239     s--;
6240
6241     if (PL_expect == XOPERATOR) {
6242         char *d;
6243         bool bof;
6244         if (   PL_bufptr == PL_linestart
6245             && ckWARN(WARN_SEMICOLON)
6246             && isIDFIRST_lazy_if_safe(s, PL_bufend, UTF))
6247         {
6248             CopLINE_dec(PL_curcop);
6249             Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
6250             CopLINE_inc(PL_curcop);
6251         }
6252         d = s;
6253         if ((bof = FEATURE_BITWISE_IS_ENABLED) && *s == '.')
6254             s++;
6255         if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6256                 (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_BITWISE)) {
6257             s = d;
6258             s--;
6259             TOKEN(0);
6260         }
6261         if (d == s)
6262             BAop(bof ? OP_NBIT_AND : OP_BIT_AND);
6263         else
6264             BAop(OP_SBIT_AND);
6265     }
6266
6267     PL_tokenbuf[0] = '&';
6268     s = scan_ident(s - 1, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, TRUE);
6269     pl_yylval.ival = (OPpENTERSUB_AMPER<<8);
6270
6271     if (PL_tokenbuf[1])
6272         force_ident_maybe_lex('&');
6273     else
6274         PREREF('&');
6275
6276     TERM('&');
6277 }
6278
6279 static int
6280 yyl_verticalbar(pTHX_ char *s)
6281 {
6282     char *d;
6283     bool bof;
6284
6285     s++;
6286     if (*s++ == '|') {
6287         if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6288                 (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_LOGIC)) {
6289             s -= 2;
6290             TOKEN(0);
6291         }
6292         AOPERATOR(OROR);
6293     }
6294
6295     s--;
6296     d = s;
6297     if ((bof = FEATURE_BITWISE_IS_ENABLED) && *s == '.')
6298         s++;
6299
6300     if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6301             (*s == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_BITWISE)) {
6302         s = d - 1;
6303         TOKEN(0);
6304     }
6305
6306     BOop(bof ? s == d ? OP_NBIT_OR : OP_SBIT_OR : OP_BIT_OR);
6307 }
6308
6309 static int
6310 yyl_bang(pTHX_ char *s)
6311 {
6312     const char tmp = *s++;
6313     if (tmp == '=') {
6314         /* was this !=~ where !~ was meant?
6315          * warn on m:!=~\s+([/?]|[msy]\W|tr\W): */
6316
6317         if (*s == '~' && ckWARN(WARN_SYNTAX)) {
6318             const char *t = s+1;
6319
6320             while (t < PL_bufend && isSPACE(*t))
6321                 ++t;
6322
6323             if (*t == '/' || *t == '?'
6324                 || ((*t == 'm' || *t == 's' || *t == 'y')
6325                     && !isWORDCHAR(t[1]))
6326                 || (*t == 't' && t[1] == 'r' && !isWORDCHAR(t[2])))
6327                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6328                             "!=~ should be !~");
6329         }
6330
6331         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE) {
6332             s -= 2;
6333             TOKEN(0);
6334         }
6335
6336         ChEop(OP_NE);
6337     }
6338
6339     if (tmp == '~')
6340         PMop(OP_NOT);
6341
6342     s--;
6343     OPERATOR('!');
6344 }
6345
6346 static int
6347 yyl_snail(pTHX_ char *s)
6348 {
6349     if (PL_expect == XPOSTDEREF)
6350         POSTDEREF('@');
6351     PL_tokenbuf[0] = '@';
6352     s = scan_ident(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, FALSE);
6353     if (PL_expect == XOPERATOR) {
6354         char *d = s;
6355         if (PL_bufptr > s) {
6356             d = PL_bufptr-1;
6357             PL_bufptr = PL_oldbufptr;
6358         }
6359         no_op("Array", d);
6360     }
6361     pl_yylval.ival = 0;
6362     if (!PL_tokenbuf[1]) {
6363         PREREF('@');
6364     }
6365     if (PL_lex_state == LEX_NORMAL || PL_lex_brackets)
6366         s = skipspace(s);
6367     if (   (PL_expect != XREF || PL_oldoldbufptr == PL_last_lop)
6368         && intuit_more(s, PL_bufend))
6369     {
6370         if (*s == '{')
6371             PL_tokenbuf[0] = '%';
6372
6373         /* Warn about @ where they meant $. */
6374         if (*s == '[' || *s == '{') {
6375             if (ckWARN(WARN_SYNTAX)) {
6376                 S_check_scalar_slice(aTHX_ s);
6377             }
6378         }
6379     }
6380     PL_expect = XOPERATOR;
6381     force_ident_maybe_lex('@');
6382     TERM('@');
6383 }
6384
6385 static int
6386 yyl_slash(pTHX_ char *s)
6387 {
6388     if ((PL_expect == XOPERATOR || PL_expect == XTERMORDORDOR) && s[1] == '/') {
6389         if (!PL_lex_allbrackets && PL_lex_fakeeof >=
6390                 (s[2] == '=' ? LEX_FAKEEOF_ASSIGN : LEX_FAKEEOF_LOGIC))
6391             TOKEN(0);
6392         s += 2;
6393         AOPERATOR(DORDOR);
6394     }
6395     else if (PL_expect == XOPERATOR) {
6396         s++;
6397         if (*s == '=' && !PL_lex_allbrackets
6398             && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
6399         {
6400             s--;
6401             TOKEN(0);
6402         }
6403         Mop(OP_DIVIDE);
6404     }
6405     else {
6406         /* Disable warning on "study /blah/" */
6407         if (    PL_oldoldbufptr == PL_last_uni
6408             && (   *PL_last_uni != 's' || s - PL_last_uni < 5
6409                 || memNE(PL_last_uni, "study", 5)
6410                 || isWORDCHAR_lazy_if_safe(PL_last_uni+5, PL_bufend, UTF)
6411          ))
6412             check_uni();
6413         s = scan_pat(s,OP_MATCH);
6414         TERM(sublex_start());
6415     }
6416 }
6417
6418 static int
6419 yyl_leftsquare(pTHX_ char *s)
6420 {
6421     char tmp;
6422
6423     if (PL_lex_brackets > 100)
6424         Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
6425     PL_lex_brackstack[PL_lex_brackets++] = 0;
6426     PL_lex_allbrackets++;
6427     tmp = *s++;
6428     OPERATOR(tmp);
6429 }
6430
6431 static int
6432 yyl_rightsquare(pTHX_ char *s)
6433 {
6434     if (PL_lex_brackets && PL_lex_brackstack[PL_lex_brackets-1] == XFAKEEOF)
6435         TOKEN(0);
6436     s++;
6437     if (PL_lex_brackets <= 0)
6438         /* diag_listed_as: Unmatched right %s bracket */
6439         yyerror("Unmatched right square bracket");
6440     else
6441         --PL_lex_brackets;
6442     PL_lex_allbrackets--;
6443     if (PL_lex_state == LEX_INTERPNORMAL) {
6444         if (PL_lex_brackets == 0) {
6445             if (*s == '-' && s[1] == '>')
6446                 PL_lex_state = LEX_INTERPENDMAYBE;
6447             else if (*s != '[' && *s != '{')
6448                 PL_lex_state = LEX_INTERPEND;
6449         }
6450     }
6451     TERM(']');
6452 }
6453
6454 static int
6455 yyl_tilde(pTHX_ char *s)
6456 {
6457     bool bof;
6458     if (s[1] == '~' && (PL_expect == XOPERATOR || PL_expect == XTERMORDORDOR)) {
6459         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
6460             TOKEN(0);
6461         s += 2;
6462         Perl_ck_warner_d(aTHX_
6463             packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
6464             "Smartmatch is experimental");
6465         NCEop(OP_SMARTMATCH);
6466     }
6467     s++;
6468     if ((bof = FEATURE_BITWISE_IS_ENABLED) && *s == '.') {
6469         s++;
6470         BCop(OP_SCOMPLEMENT);
6471     }
6472     BCop(bof ? OP_NCOMPLEMENT : OP_COMPLEMENT);
6473 }
6474
6475 static int
6476 yyl_leftparen(pTHX_ char *s)
6477 {
6478     if (PL_last_lop == PL_oldoldbufptr || PL_last_uni == PL_oldoldbufptr)
6479         PL_oldbufptr = PL_oldoldbufptr;         /* allow print(STDOUT 123) */
6480     else
6481         PL_expect = XTERM;
6482     s = skipspace(s);
6483     PL_lex_allbrackets++;
6484     TOKEN('(');
6485 }
6486
6487 static int
6488 yyl_rightparen(pTHX_ char *s)
6489 {
6490     if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_CLOSING)
6491         TOKEN(0);
6492     s++;
6493     PL_lex_allbrackets--;
6494     s = skipspace(s);
6495     if (*s == '{')
6496         PREBLOCK(')');
6497     TERM(')');
6498 }
6499
6500 static int
6501 yyl_leftpointy(pTHX_ char *s)
6502 {
6503     char tmp;
6504
6505     if (PL_expect != XOPERATOR) {
6506         if (s[1] != '<' && !memchr(s,'>', PL_bufend - s))
6507             check_uni();
6508         if (s[1] == '<' && s[2] != '>')
6509             s = scan_heredoc(s);
6510         else
6511             s = scan_inputsymbol(s);
6512         PL_expect = XOPERATOR;
6513         TOKEN(sublex_start());
6514     }
6515
6516     s++;
6517
6518     tmp = *s++;
6519     if (tmp == '<') {
6520         if (*s == '=' && !PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN) {
6521             s -= 2;
6522             TOKEN(0);
6523         }
6524         SHop(OP_LEFT_SHIFT);
6525     }
6526     if (tmp == '=') {
6527         tmp = *s++;
6528         if (tmp == '>') {
6529             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE) {
6530                 s -= 3;
6531                 TOKEN(0);
6532             }
6533             NCEop(OP_NCMP);
6534         }
6535         s--;
6536         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE) {
6537             s -= 2;
6538             TOKEN(0);
6539         }
6540         ChRop(OP_LE);
6541     }
6542
6543     s--;
6544     if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE) {
6545         s--;
6546         TOKEN(0);
6547     }
6548
6549     ChRop(OP_LT);
6550 }
6551
6552 static int
6553 yyl_rightpointy(pTHX_ char *s)
6554 {
6555     const char tmp = *s++;
6556
6557     if (tmp == '>') {
6558         if (*s == '=' && !PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN) {
6559             s -= 2;
6560             TOKEN(0);
6561         }
6562         SHop(OP_RIGHT_SHIFT);
6563     }
6564     else if (tmp == '=') {
6565         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE) {
6566             s -= 2;
6567             TOKEN(0);
6568         }
6569         ChRop(OP_GE);
6570     }
6571
6572     s--;
6573     if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE) {
6574         s--;
6575         TOKEN(0);
6576     }
6577
6578     ChRop(OP_GT);
6579 }
6580
6581 static int
6582 yyl_sglquote(pTHX_ char *s)
6583 {
6584     s = scan_str(s,FALSE,FALSE,FALSE,NULL);
6585     if (!s)
6586         missingterm(NULL, 0);
6587     COPLINE_SET_FROM_MULTI_END;
6588     DEBUG_T( { printbuf("### Saw string before %s\n", s); } );
6589     if (PL_expect == XOPERATOR) {
6590         no_op("String",s);
6591     }
6592     pl_yylval.ival = OP_CONST;
6593     TERM(sublex_start());
6594 }
6595
6596 static int
6597 yyl_dblquote(pTHX_ char *s, STRLEN len)
6598 {
6599     char *d;
6600     s = scan_str(s,FALSE,FALSE,FALSE,NULL);
6601     DEBUG_T( {
6602         if (s)
6603             printbuf("### Saw string before %s\n", s);
6604         else
6605             PerlIO_printf(Perl_debug_log,
6606                          "### Saw unterminated string\n");
6607     } );
6608     if (PL_expect == XOPERATOR) {
6609             no_op("String",s);
6610     }
6611     if (!s)
6612         missingterm(NULL, 0);
6613     pl_yylval.ival = OP_CONST;
6614     /* FIXME. I think that this can be const if char *d is replaced by
6615        more localised variables.  */
6616     for (d = SvPV(PL_lex_stuff, len); len; len--, d++) {
6617         if (*d == '$' || *d == '@' || *d == '\\' || !UTF8_IS_INVARIANT((U8)*d)) {
6618             pl_yylval.ival = OP_STRINGIFY;
6619             break;
6620         }
6621     }
6622     if (pl_yylval.ival == OP_CONST)
6623         COPLINE_SET_FROM_MULTI_END;
6624     TERM(sublex_start());
6625 }
6626
6627 static int
6628 yyl_backtick(pTHX_ char *s)
6629 {
6630     s = scan_str(s,FALSE,FALSE,FALSE,NULL);
6631     DEBUG_T( {
6632         if (s)
6633             printbuf("### Saw backtick string before %s\n", s);
6634         else
6635             PerlIO_printf(Perl_debug_log,
6636                          "### Saw unterminated backtick string\n");
6637     } );
6638     if (PL_expect == XOPERATOR)
6639         no_op("Backticks",s);
6640     if (!s)
6641         missingterm(NULL, 0);
6642     pl_yylval.ival = OP_BACKTICK;
6643     TERM(sublex_start());
6644 }
6645
6646 static int
6647 yyl_backslash(pTHX_ char *s)
6648 {
6649     if (PL_lex_inwhat == OP_SUBST && PL_lex_repl == PL_linestr && isDIGIT(*s))
6650         Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),"Can't use \\%c to mean $%c in expression",
6651                        *s, *s);
6652     if (PL_expect == XOPERATOR)
6653         no_op("Backslash",s);
6654     OPERATOR(REFGEN);
6655 }
6656
6657 static void
6658 yyl_data_handle(pTHX)
6659 {
6660     HV * const stash = PL_tokenbuf[2] == 'D' && PL_curstash
6661                             ? PL_curstash
6662                             : PL_defstash;
6663     GV *gv = (GV *)*hv_fetchs(stash, "DATA", 1);
6664
6665     if (!isGV(gv))
6666         gv_init(gv,stash,"DATA",4,0);
6667
6668     GvMULTI_on(gv);
6669     if (!GvIO(gv))
6670         GvIOp(gv) = newIO();
6671     IoIFP(GvIOp(gv)) = PL_rsfp;
6672
6673     /* Mark this internal pseudo-handle as clean */
6674     IoFLAGS(GvIOp(gv)) |= IOf_UNTAINT;
6675     if ((PerlIO*)PL_rsfp == PerlIO_stdin())
6676         IoTYPE(GvIOp(gv)) = IoTYPE_STD;
6677     else
6678         IoTYPE(GvIOp(gv)) = IoTYPE_RDONLY;
6679
6680 #if defined(WIN32) && !defined(PERL_TEXTMODE_SCRIPTS)
6681     /* if the script was opened in binmode, we need to revert
6682      * it to text mode for compatibility; but only iff it has CRs
6683      * XXX this is a questionable hack at best. */
6684     if (PL_bufend-PL_bufptr > 2
6685         && PL_bufend[-1] == '\n' && PL_bufend[-2] == '\r')
6686     {
6687         Off_t loc = 0;
6688         if (IoTYPE(GvIOp(gv)) == IoTYPE_RDONLY) {
6689             loc = PerlIO_tell(PL_rsfp);
6690             (void)PerlIO_seek(PL_rsfp, 0L, 0);
6691         }
6692         if (PerlLIO_setmode(RSFP_FILENO, O_TEXT) != -1) {
6693             if (loc > 0)
6694                 PerlIO_seek(PL_rsfp, loc, 0);
6695         }
6696     }
6697 #endif
6698
6699 #ifdef PERLIO_LAYERS
6700     if (!IN_BYTES) {
6701         if (UTF)
6702             PerlIO_apply_layers(aTHX_ PL_rsfp, NULL, ":utf8");
6703     }
6704 #endif
6705
6706     PL_rsfp = NULL;
6707 }
6708
6709 PERL_STATIC_NO_RET void yyl_croak_unrecognised(pTHX_ char*)
6710     __attribute__noreturn__;
6711
6712 PERL_STATIC_NO_RET void
6713 yyl_croak_unrecognised(pTHX_ char *s)
6714 {
6715     SV *dsv = newSVpvs_flags("", SVs_TEMP);
6716     const char *c;
6717     char *d;
6718     STRLEN len;
6719
6720     if (UTF) {
6721         STRLEN skiplen = UTF8SKIP(s);
6722         STRLEN stravail = PL_bufend - s;
6723         c = sv_uni_display(dsv, newSVpvn_flags(s,
6724                                                skiplen > stravail ? stravail : skiplen,
6725                                                SVs_TEMP | SVf_UTF8),
6726                            10, UNI_DISPLAY_ISPRINT);
6727     }
6728     else {
6729         c = Perl_form(aTHX_ "\\x%02X", (unsigned char)*s);
6730     }
6731
6732     if (s >= PL_linestart) {
6733         d = PL_linestart;
6734     }
6735     else {
6736         /* somehow (probably due to a parse failure), PL_linestart has advanced
6737          * pass PL_bufptr, get a reasonable beginning of line
6738          */
6739         d = s;
6740         while (d > SvPVX(PL_linestr) && d[-1] && d[-1] != '\n')
6741             --d;
6742     }
6743     len = UTF ? Perl_utf8_length(aTHX_ (U8 *) d, (U8 *) s) : (STRLEN) (s - d);
6744     if (len > UNRECOGNIZED_PRECEDE_COUNT) {
6745         d = UTF ? (char *) utf8_hop_back((U8 *) s, -UNRECOGNIZED_PRECEDE_COUNT, (U8 *)d) : s - UNRECOGNIZED_PRECEDE_COUNT;
6746     }
6747
6748     Perl_croak(aTHX_  "Unrecognized character %s; marked by <-- HERE after %" UTF8f "<-- HERE near column %d", c,
6749                       UTF8fARG(UTF, (s - d), d),
6750                      (int) len + 1);
6751 }
6752
6753 static int
6754 yyl_require(pTHX_ char *s, I32 orig_keyword)
6755 {
6756     s = skipspace(s);
6757     if (isDIGIT(*s)) {
6758         s = force_version(s, FALSE);
6759     }
6760     else if (*s != 'v' || !isDIGIT(s[1])
6761             || (s = force_version(s, TRUE), *s == 'v'))
6762     {
6763         *PL_tokenbuf = '\0';
6764         s = force_word(s,BAREWORD,TRUE,TRUE);
6765         if (isIDFIRST_lazy_if_safe(PL_tokenbuf,
6766                                    PL_tokenbuf + sizeof(PL_tokenbuf),
6767                                    UTF))
6768         {
6769             gv_stashpvn(PL_tokenbuf, strlen(PL_tokenbuf),
6770                         GV_ADD | (UTF ? SVf_UTF8 : 0));
6771         }
6772         else if (*s == '<')
6773             yyerror("<> at require-statement should be quotes");
6774     }
6775
6776     if (orig_keyword == KEY_require)
6777         pl_yylval.ival = 1;
6778     else
6779         pl_yylval.ival = 0;
6780
6781     PL_expect = PL_nexttoke ? XOPERATOR : XTERM;
6782     PL_bufptr = s;
6783     PL_last_uni = PL_oldbufptr;
6784     PL_last_lop_op = OP_REQUIRE;
6785     s = skipspace(s);
6786     return REPORT( (int)REQUIRE );
6787 }
6788
6789 static int
6790 yyl_foreach(pTHX_ char *s)
6791 {
6792     if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
6793         return REPORT(0);
6794     pl_yylval.ival = CopLINE(PL_curcop);
6795     s = skipspace(s);
6796     if (PL_expect == XSTATE && isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
6797         char *p = s;
6798         SSize_t s_off = s - SvPVX(PL_linestr);
6799         STRLEN len;
6800
6801         if (memBEGINPs(p, (STRLEN) (PL_bufend - p), "my") && isSPACE(p[2])) {
6802             p += 2;
6803         }
6804         else if (memBEGINPs(p, (STRLEN) (PL_bufend - p), "our") && isSPACE(p[3])) {
6805             p += 3;
6806         }
6807
6808         p = skipspace(p);
6809         /* skip optional package name, as in "for my abc $x (..)" */
6810         if (isIDFIRST_lazy_if_safe(p, PL_bufend, UTF)) {
6811             p = scan_word(p, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
6812             p = skipspace(p);
6813         }
6814         if (*p != '$' && *p != '\\')
6815             Perl_croak(aTHX_ "Missing $ on loop variable");
6816
6817         /* The buffer may have been reallocated, update s */
6818         s = SvPVX(PL_linestr) + s_off;
6819     }
6820     OPERATOR(FOR);
6821 }
6822
6823 static int
6824 yyl_do(pTHX_ char *s, I32 orig_keyword)
6825 {
6826     s = skipspace(s);
6827     if (*s == '{')
6828         PRETERMBLOCK(DO);
6829     if (*s != '\'') {
6830         char *d;
6831         STRLEN len;
6832         *PL_tokenbuf = '&';
6833         d = scan_word(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
6834                       1, &len);
6835         if (len && memNEs(PL_tokenbuf+1, len, "CORE")
6836          && !keyword(PL_tokenbuf + 1, len, 0)) {
6837             SSize_t off = s-SvPVX(PL_linestr);
6838             d = skipspace(d);
6839             s = SvPVX(PL_linestr)+off;
6840             if (*d == '(') {
6841                 force_ident_maybe_lex('&');
6842                 s = d;
6843             }
6844         }
6845     }
6846     if (orig_keyword == KEY_do)
6847         pl_yylval.ival = 1;
6848     else
6849         pl_yylval.ival = 0;
6850     OPERATOR(DO);
6851 }
6852
6853 static int
6854 yyl_my(pTHX_ char *s, I32 my)
6855 {
6856     if (PL_in_my) {
6857         PL_bufptr = s;
6858         yyerror(Perl_form(aTHX_
6859                           "Can't redeclare \"%s\" in \"%s\"",
6860                            my       == KEY_my    ? "my" :
6861                            my       == KEY_state ? "state" : "our",
6862                            PL_in_my == KEY_my    ? "my" :
6863                            PL_in_my == KEY_state ? "state" : "our"));
6864     }
6865     PL_in_my = (U16)my;
6866     s = skipspace(s);
6867     if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
6868         STRLEN len;
6869         s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
6870         if (memEQs(PL_tokenbuf, len, "sub"))
6871             return yyl_sub(aTHX_ s, my);
6872         PL_in_my_stash = find_in_my_stash(PL_tokenbuf, len);
6873         if (!PL_in_my_stash) {
6874             char tmpbuf[1024];
6875             int i;
6876             PL_bufptr = s;
6877             i = my_snprintf(tmpbuf, sizeof(tmpbuf), "No such class %.1000s", PL_tokenbuf);
6878             PERL_MY_SNPRINTF_POST_GUARD(i, sizeof(tmpbuf));
6879             yyerror_pv(tmpbuf, UTF ? SVf_UTF8 : 0);
6880         }
6881     }
6882     else if (*s == '\\') {
6883         if (!FEATURE_MYREF_IS_ENABLED)
6884             Perl_croak(aTHX_ "The experimental declared_refs "
6885                              "feature is not enabled");
6886         Perl_ck_warner_d(aTHX_
6887              packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
6888             "Declaring references is experimental");
6889     }
6890     OPERATOR(MY);
6891 }
6892
6893 static int yyl_try(pTHX_ char*, STRLEN);
6894
6895 static bool
6896 yyl_eol_needs_semicolon(pTHX_ char **ps)
6897 {
6898     char *s = *ps;
6899     if (PL_lex_state != LEX_NORMAL
6900         || (PL_in_eval && !PL_rsfp && !PL_parser->filtered))
6901     {
6902         const bool in_comment = *s == '#';
6903         char *d;
6904         if (*s == '#' && s == PL_linestart && PL_in_eval
6905          && !PL_rsfp && !PL_parser->filtered) {
6906             /* handle eval qq[#line 1 "foo"\n ...] */
6907             CopLINE_dec(PL_curcop);
6908             incline(s, PL_bufend);
6909         }
6910         d = s;
6911         while (d < PL_bufend && *d != '\n')
6912             d++;
6913         if (d < PL_bufend)
6914             d++;
6915         s = d;
6916         if (in_comment && d == PL_bufend
6917             && PL_lex_state == LEX_INTERPNORMAL
6918             && PL_lex_inwhat == OP_SUBST && PL_lex_repl == PL_linestr
6919             && SvEVALED(PL_lex_repl) && d[-1] == '}') s--;
6920         else
6921             incline(s, PL_bufend);
6922         if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
6923             PL_lex_state = LEX_FORMLINE;
6924             force_next(FORMRBRACK);
6925             *ps = s;
6926             return TRUE;
6927         }
6928     }
6929     else {
6930         while (s < PL_bufend && *s != '\n')
6931             s++;
6932         if (s < PL_bufend) {
6933             s++;
6934             if (s < PL_bufend)
6935                 incline(s, PL_bufend);
6936         }
6937     }
6938     *ps = s;
6939     return FALSE;
6940 }
6941
6942 static int
6943 yyl_fake_eof(pTHX_ U32 fake_eof, bool bof, char *s, STRLEN len)
6944 {
6945     char *d;
6946
6947     goto start;
6948
6949     do {
6950         fake_eof = 0;
6951         bof = cBOOL(PL_rsfp);
6952       start:
6953
6954         PL_bufptr = PL_bufend;
6955         COPLINE_INC_WITH_HERELINES;
6956         if (!lex_next_chunk(fake_eof)) {
6957             CopLINE_dec(PL_curcop);
6958             s = PL_bufptr;
6959             TOKEN(';'); /* not infinite loop because rsfp is NULL now */
6960         }
6961         CopLINE_dec(PL_curcop);
6962         s = PL_bufptr;
6963         /* If it looks like the start of a BOM or raw UTF-16,
6964          * check if it in fact is. */
6965         if (bof && PL_rsfp
6966             && (   *s == 0
6967                 || *(U8*)s == BOM_UTF8_FIRST_BYTE
6968                 || *(U8*)s >= 0xFE
6969                 || s[1] == 0))
6970         {
6971             Off_t offset = (IV)PerlIO_tell(PL_rsfp);
6972             bof = (offset == (Off_t)SvCUR(PL_linestr));
6973 #if defined(PERLIO_USING_CRLF) && defined(PERL_TEXTMODE_SCRIPTS)
6974             /* offset may include swallowed CR */
6975             if (!bof)
6976                 bof = (offset == (Off_t)SvCUR(PL_linestr)+1);
6977 #endif
6978             if (bof) {
6979                 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
6980                 s = swallow_bom((U8*)s);
6981             }
6982         }
6983         if (PL_parser->in_pod) {
6984             /* Incest with pod. */
6985             if (    memBEGINPs(s, (STRLEN) (PL_bufend - s), "=cut")
6986                 && !isALPHA(s[4]))
6987             {
6988                 SvPVCLEAR(PL_linestr);
6989                 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
6990                 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
6991                 PL_last_lop = PL_last_uni = NULL;
6992                 PL_parser->in_pod = 0;
6993             }
6994         }
6995         if (PL_rsfp || PL_parser->filtered)
6996             incline(s, PL_bufend);
6997     } while (PL_parser->in_pod);
6998
6999     PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = s;
7000     PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
7001     PL_last_lop = PL_last_uni = NULL;
7002     if (CopLINE(PL_curcop) == 1) {
7003         while (s < PL_bufend && isSPACE(*s))
7004             s++;
7005         if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
7006             s++;
7007         d = NULL;
7008         if (!PL_in_eval) {
7009             if (*s == '#' && *(s+1) == '!')
7010                 d = s + 2;
7011 #ifdef ALTERNATE_SHEBANG
7012             else {
7013                 static char const as[] = ALTERNATE_SHEBANG;
7014                 if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
7015                     d = s + (sizeof(as) - 1);
7016             }
7017 #endif /* ALTERNATE_SHEBANG */
7018         }
7019         if (d) {
7020             char *ipath;
7021             char *ipathend;
7022
7023             while (isSPACE(*d))
7024                 d++;
7025             ipath = d;
7026             while (*d && !isSPACE(*d))
7027                 d++;
7028             ipathend = d;
7029
7030 #ifdef ARG_ZERO_IS_SCRIPT
7031             if (ipathend > ipath) {
7032                 /*
7033                  * HP-UX (at least) sets argv[0] to the script name,
7034                  * which makes $^X incorrect.  And Digital UNIX and Linux,
7035                  * at least, set argv[0] to the basename of the Perl
7036                  * interpreter. So, having found "#!", we'll set it right.
7037                  */
7038                 SV* copfilesv = CopFILESV(PL_curcop);
7039                 if (copfilesv) {
7040                     SV * const x =
7041                         GvSV(gv_fetchpvs("\030", GV_ADD|GV_NOTQUAL,
7042                                          SVt_PV)); /* $^X */
7043                     assert(SvPOK(x) || SvGMAGICAL(x));
7044                     if (sv_eq(x, copfilesv)) {
7045                         sv_setpvn(x, ipath, ipathend - ipath);
7046                         SvSETMAGIC(x);
7047                     }
7048                     else {
7049                         STRLEN blen;
7050                         STRLEN llen;
7051                         const char *bstart = SvPV_const(copfilesv, blen);
7052                         const char * const lstart = SvPV_const(x, llen);
7053                         if (llen < blen) {
7054                             bstart += blen - llen;
7055                             if (strnEQ(bstart, lstart, llen) && bstart[-1] == '/') {
7056                                 sv_setpvn(x, ipath, ipathend - ipath);
7057                                 SvSETMAGIC(x);
7058                             }
7059                         }
7060                     }
7061                 }
7062                 else {
7063                     /* Anything to do if no copfilesv? */
7064                 }
7065                 TAINT_NOT;      /* $^X is always tainted, but that's OK */
7066             }
7067 #endif /* ARG_ZERO_IS_SCRIPT */
7068
7069             /*
7070              * Look for options.
7071              */
7072             d = instr(s,"perl -");
7073             if (!d) {
7074                 d = instr(s,"perl");
7075 #if defined(DOSISH)
7076                 /* avoid getting into infinite loops when shebang
7077                  * line contains "Perl" rather than "perl" */
7078                 if (!d) {
7079                     for (d = ipathend-4; d >= ipath; --d) {
7080                         if (isALPHA_FOLD_EQ(*d, 'p')
7081                             && !ibcmp(d, "perl", 4))
7082                         {
7083                             break;
7084                         }
7085                     }
7086                     if (d < ipath)
7087                         d = NULL;
7088                 }
7089 #endif
7090             }
7091 #ifdef ALTERNATE_SHEBANG
7092             /*
7093              * If the ALTERNATE_SHEBANG on this system starts with a
7094              * character that can be part of a Perl expression, then if
7095              * we see it but not "perl", we're probably looking at the
7096              * start of Perl code, not a request to hand off to some
7097              * other interpreter.  Similarly, if "perl" is there, but
7098              * not in the first 'word' of the line, we assume the line
7099              * contains the start of the Perl program.
7100              */
7101             if (d && *s != '#') {
7102                 const char *c = ipath;
7103                 while (*c && !memCHRs("; \t\r\n\f\v#", *c))
7104                     c++;
7105                 if (c < d)
7106                     d = NULL;   /* "perl" not in first word; ignore */
7107                 else
7108                     *s = '#';   /* Don't try to parse shebang line */
7109             }
7110 #endif /* ALTERNATE_SHEBANG */
7111             if (!d
7112                 && *s == '#'
7113                 && ipathend > ipath
7114                 && !PL_minus_c
7115                 && !instr(s,"indir")
7116                 && instr(PL_origargv[0],"perl"))
7117             {
7118                 dVAR;
7119                 char **newargv;
7120
7121                 *ipathend = '\0';
7122                 s = ipathend + 1;
7123                 while (s < PL_bufend && isSPACE(*s))
7124                     s++;
7125                 if (s < PL_bufend) {
7126                     Newx(newargv,PL_origargc+3,char*);
7127                     newargv[1] = s;
7128                     while (s < PL_bufend && !isSPACE(*s))
7129                         s++;
7130                     *s = '\0';
7131                     Copy(PL_origargv+1, newargv+2, PL_origargc+1, char*);
7132                 }
7133                 else
7134                     newargv = PL_origargv;
7135                 newargv[0] = ipath;
7136                 PERL_FPU_PRE_EXEC
7137                 PerlProc_execv(ipath, EXEC_ARGV_CAST(newargv));
7138                 PERL_FPU_POST_EXEC
7139                 Perl_croak(aTHX_ "Can't exec %s", ipath);
7140             }
7141             if (d) {
7142                 while (*d && !isSPACE(*d))
7143                     d++;
7144                 while (SPACE_OR_TAB(*d))
7145                     d++;
7146
7147                 if (*d++ == '-') {
7148                     const bool switches_done = PL_doswitches;
7149                     const U32 oldpdb = PL_perldb;
7150                     const bool oldn = PL_minus_n;
7151                     const bool oldp = PL_minus_p;
7152                     const char *d1 = d;
7153
7154                     do {
7155                         bool baduni = FALSE;
7156                         if (*d1 == 'C') {
7157                             const char *d2 = d1 + 1;
7158                             if (parse_unicode_opts((const char **)&d2)
7159                                 != PL_unicode)
7160                                 baduni = TRUE;
7161                         }
7162                         if (baduni || isALPHA_FOLD_EQ(*d1, 'M')) {
7163                             const char * const m = d1;
7164                             while (*d1 && !isSPACE(*d1))
7165                                 d1++;
7166                             Perl_croak(aTHX_ "Too late for \"-%.*s\" option",
7167                                   (int)(d1 - m), m);
7168                         }
7169                         d1 = moreswitches(d1);
7170                     } while (d1);
7171                     if (PL_doswitches && !switches_done) {
7172                         int argc = PL_origargc;
7173                         char **argv = PL_origargv;
7174                         do {
7175                             argc--,argv++;
7176                         } while (argc && argv[0][0] == '-' && argv[0][1]);
7177                         init_argv_symbols(argc,argv);
7178                     }
7179                     if (   (PERLDB_LINE_OR_SAVESRC && !oldpdb)
7180                         || ((PL_minus_n || PL_minus_p) && !(oldn || oldp)))
7181                           /* if we have already added "LINE: while (<>) {",
7182                              we must not do it again */
7183                     {
7184                         SvPVCLEAR(PL_linestr);
7185                         PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
7186                         PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
7187                         PL_last_lop = PL_last_uni = NULL;
7188                         PL_preambled = FALSE;
7189                         if (PERLDB_LINE_OR_SAVESRC)
7190                             (void)gv_fetchfile(PL_origfilename);
7191                         return yyl_try(aTHX_ s, len);
7192                     }
7193                 }
7194             }
7195         }
7196     }
7197
7198     if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
7199         PL_lex_state = LEX_FORMLINE;
7200         force_next(FORMRBRACK);
7201         TOKEN(';');
7202     }
7203
7204     return yyl_try(aTHX_ s, len);
7205 }
7206
7207 static int
7208 yyl_fatcomma(pTHX_ char *s, STRLEN len)
7209 {
7210     CLINE;
7211     pl_yylval.opval
7212         = newSVOP(OP_CONST, 0,
7213                        S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
7214     pl_yylval.opval->op_private = OPpCONST_BARE;
7215     TERM(BAREWORD);
7216 }
7217
7218 static int
7219 yyl_safe_bareword(pTHX_ char *s, const char lastchar)
7220 {
7221     if ((lastchar == '*' || lastchar == '%' || lastchar == '&')
7222         && PL_parser->saw_infix_sigil)
7223     {
7224         Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
7225                          "Operator or semicolon missing before %c%" UTF8f,
7226                          lastchar,
7227                          UTF8fARG(UTF, strlen(PL_tokenbuf),
7228                                   PL_tokenbuf));
7229         Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
7230                          "Ambiguous use of %c resolved as operator %c",
7231                          lastchar, lastchar);
7232     }
7233     TOKEN(BAREWORD);
7234 }
7235
7236 static int
7237 yyl_constant_op(pTHX_ char *s, SV *sv, CV *cv, OP *rv2cv_op, PADOFFSET off)
7238 {
7239     if (sv) {
7240         op_free(rv2cv_op);
7241         SvREFCNT_dec(((SVOP*)pl_yylval.opval)->op_sv);
7242         ((SVOP*)pl_yylval.opval)->op_sv = SvREFCNT_inc_simple(sv);
7243         if (SvTYPE(sv) == SVt_PVAV)
7244             pl_yylval.opval = newUNOP(OP_RV2AV, OPf_PARENS,
7245                                       pl_yylval.opval);
7246         else {
7247             pl_yylval.opval->op_private = 0;
7248             pl_yylval.opval->op_folded = 1;
7249             pl_yylval.opval->op_flags |= OPf_SPECIAL;
7250         }
7251         TOKEN(BAREWORD);
7252     }
7253
7254     op_free(pl_yylval.opval);
7255     pl_yylval.opval =
7256         off ? newCVREF(0, rv2cv_op) : rv2cv_op;
7257     pl_yylval.opval->op_private |= OPpENTERSUB_NOPAREN;
7258     PL_last_lop = PL_oldbufptr;
7259     PL_last_lop_op = OP_ENTERSUB;
7260
7261     /* Is there a prototype? */
7262     if (SvPOK(cv)) {
7263         int k = yyl_subproto(aTHX_ s, cv);
7264         if (k != KEY_NULL)
7265             return k;
7266     }
7267
7268     NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
7269     PL_expect = XTERM;
7270     force_next(off ? PRIVATEREF : BAREWORD);
7271     if (!PL_lex_allbrackets
7272         && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7273     {
7274         PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7275     }
7276
7277     TOKEN(NOAMP);
7278 }
7279
7280 /* Honour "reserved word" warnings, and enforce strict subs */
7281 static void
7282 yyl_strictwarn_bareword(pTHX_ const char lastchar)
7283 {
7284     /* after "print" and similar functions (corresponding to
7285      * "F? L" in opcode.pl), whatever wasn't already parsed as
7286      * a filehandle should be subject to "strict subs".
7287      * Likewise for the optional indirect-object argument to system
7288      * or exec, which can't be a bareword */
7289     if ((PL_last_lop_op == OP_PRINT
7290             || PL_last_lop_op == OP_PRTF
7291             || PL_last_lop_op == OP_SAY
7292             || PL_last_lop_op == OP_SYSTEM
7293             || PL_last_lop_op == OP_EXEC)
7294         && (PL_hints & HINT_STRICT_SUBS))
7295     {
7296         pl_yylval.opval->op_private |= OPpCONST_STRICT;
7297     }
7298
7299     if (lastchar != '-' && ckWARN(WARN_RESERVED)) {
7300         char *d = PL_tokenbuf;
7301         while (isLOWER(*d))
7302             d++;
7303         if (!*d && !gv_stashpv(PL_tokenbuf, UTF ? SVf_UTF8 : 0)) {
7304             /* PL_warn_reserved is constant */
7305             GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
7306             Perl_warner(aTHX_ packWARN(WARN_RESERVED), PL_warn_reserved,
7307                         PL_tokenbuf);
7308             GCC_DIAG_RESTORE_STMT;
7309         }
7310     }
7311 }
7312
7313 static int
7314 yyl_just_a_word(pTHX_ char *s, STRLEN len, I32 orig_keyword, struct code c)
7315 {
7316     int pkgname = 0;
7317     const char lastchar = (PL_bufptr == PL_oldoldbufptr ? 0 : PL_bufptr[-1]);
7318     bool safebw;
7319     bool no_op_error = FALSE;
7320     /* Use this var to track whether intuit_method has been
7321        called.  intuit_method returns 0 or > 255.  */
7322     int key = 1;
7323
7324     if (PL_expect == XOPERATOR) {
7325         if (PL_bufptr == PL_linestart) {
7326             CopLINE_dec(PL_curcop);
7327             Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
7328             CopLINE_inc(PL_curcop);
7329         }
7330         else
7331             /* We want to call no_op with s pointing after the
7332                bareword, so defer it.  But we want it to come
7333                before the Bad name croak.  */
7334             no_op_error = TRUE;
7335     }
7336
7337     /* Get the rest if it looks like a package qualifier */
7338
7339     if (*s == '\'' || (*s == ':' && s[1] == ':')) {
7340         STRLEN morelen;
7341         s = scan_word(s, PL_tokenbuf + len, sizeof PL_tokenbuf - len,
7342                       TRUE, &morelen);
7343         if (no_op_error) {
7344             no_op("Bareword",s);
7345             no_op_error = FALSE;
7346         }
7347         if (!morelen)
7348             Perl_croak(aTHX_ "Bad name after %" UTF8f "%s",
7349                     UTF8fARG(UTF, len, PL_tokenbuf),
7350                     *s == '\'' ? "'" : "::");
7351         len += morelen;
7352         pkgname = 1;
7353     }
7354
7355     if (no_op_error)
7356         no_op("Bareword",s);
7357
7358     /* See if the name is "Foo::",
7359        in which case Foo is a bareword
7360        (and a package name). */
7361
7362     if (len > 2 && PL_tokenbuf[len - 2] == ':' && PL_tokenbuf[len - 1] == ':') {
7363         if (ckWARN(WARN_BAREWORD)
7364             && ! gv_fetchpvn_flags(PL_tokenbuf, len, UTF ? SVf_UTF8 : 0, SVt_PVHV))
7365             Perl_warner(aTHX_ packWARN(WARN_BAREWORD),
7366                         "Bareword \"%" UTF8f
7367                         "\" refers to nonexistent package",
7368                         UTF8fARG(UTF, len, PL_tokenbuf));
7369         len -= 2;
7370         PL_tokenbuf[len] = '\0';
7371         c.gv = NULL;
7372         c.gvp = 0;
7373         safebw = TRUE;
7374     }
7375     else {
7376         safebw = FALSE;
7377     }
7378
7379     /* if we saw a global override before, get the right name */
7380
7381     if (!c.sv)
7382         c.sv = S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len);
7383     if (c.gvp) {
7384         SV *sv = newSVpvs("CORE::GLOBAL::");
7385         sv_catsv(sv, c.sv);
7386         SvREFCNT_dec(c.sv);
7387         c.sv = sv;
7388     }
7389
7390     /* Presume this is going to be a bareword of some sort. */
7391     CLINE;
7392     pl_yylval.opval = newSVOP(OP_CONST, 0, c.sv);
7393     pl_yylval.opval->op_private = OPpCONST_BARE;
7394
7395     /* And if "Foo::", then that's what it certainly is. */
7396     if (safebw)
7397         return yyl_safe_bareword(aTHX_ s, lastchar);
7398
7399     if (!c.off) {
7400         OP *const_op = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(c.sv));
7401         const_op->op_private = OPpCONST_BARE;
7402         c.rv2cv_op = newCVREF(OPpMAY_RETURN_CONSTANT<<8, const_op);
7403         c.cv = c.lex
7404             ? isGV(c.gv)
7405                 ? GvCV(c.gv)
7406                 : SvROK(c.gv) && SvTYPE(SvRV(c.gv)) == SVt_PVCV
7407                     ? (CV *)SvRV(c.gv)
7408                     : ((CV *)c.gv)
7409             : rv2cv_op_cv(c.rv2cv_op, RV2CVOPCV_RETURN_STUB);
7410     }
7411
7412     /* See if it's the indirect object for a list operator. */
7413
7414     if (PL_oldoldbufptr
7415         && PL_oldoldbufptr < PL_bufptr
7416         && (PL_oldoldbufptr == PL_last_lop
7417             || PL_oldoldbufptr == PL_last_uni)
7418         && /* NO SKIPSPACE BEFORE HERE! */
7419            (PL_expect == XREF
7420             || ((PL_opargs[PL_last_lop_op] >> OASHIFT)& 7)
7421                                                    == OA_FILEREF))
7422     {
7423         bool immediate_paren = *s == '(';
7424         SSize_t s_off;
7425
7426         /* (Now we can afford to cross potential line boundary.) */
7427         s = skipspace(s);
7428
7429         /* intuit_method() can indirectly call lex_next_chunk(),
7430          * invalidating s
7431          */
7432         s_off = s - SvPVX(PL_linestr);
7433         /* Two barewords in a row may indicate method call. */
7434         if (   (   isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)
7435                 || *s == '$')
7436             && (key = intuit_method(s, c.lex ? NULL : c.sv, c.cv)))
7437         {
7438             /* the code at method: doesn't use s */
7439             goto method;
7440         }
7441         s = SvPVX(PL_linestr) + s_off;
7442
7443         /* If not a declared subroutine, it's an indirect object. */
7444         /* (But it's an indir obj regardless for sort.) */
7445         /* Also, if "_" follows a filetest operator, it's a bareword */
7446
7447         if (
7448             ( !immediate_paren && (PL_last_lop_op == OP_SORT
7449              || (!c.cv
7450                  && (PL_last_lop_op != OP_MAPSTART
7451                      && PL_last_lop_op != OP_GREPSTART))))
7452            || (PL_tokenbuf[0] == '_' && PL_tokenbuf[1] == '\0'
7453                 && ((PL_opargs[PL_last_lop_op] & OA_CLASS_MASK)
7454                                                 == OA_FILESTATOP))
7455            )
7456         {
7457             PL_expect = (PL_last_lop == PL_oldoldbufptr) ? XTERM : XOPERATOR;
7458             yyl_strictwarn_bareword(aTHX_ lastchar);
7459             op_free(c.rv2cv_op);
7460             return yyl_safe_bareword(aTHX_ s, lastchar);
7461         }
7462     }
7463
7464     PL_expect = XOPERATOR;
7465     s = skipspace(s);
7466
7467     /* Is this a word before a => operator? */
7468     if (*s == '=' && s[1] == '>' && !pkgname) {
7469         op_free(c.rv2cv_op);
7470         CLINE;
7471         if (c.gvp || (c.lex && !c.off)) {
7472             assert (cSVOPx(pl_yylval.opval)->op_sv == c.sv);
7473             /* This is our own scalar, created a few lines
7474                above, so this is safe. */
7475             SvREADONLY_off(c.sv);
7476             sv_setpv(c.sv, PL_tokenbuf);
7477             if (UTF && !IN_BYTES
7478              && is_utf8_string((U8*)PL_tokenbuf, len))
7479                   SvUTF8_on(c.sv);
7480             SvREADONLY_on(c.sv);
7481         }
7482         TERM(BAREWORD);
7483     }
7484
7485     /* If followed by a paren, it's certainly a subroutine. */
7486     if (*s == '(') {
7487         CLINE;
7488         if (c.cv) {
7489             char *d = s + 1;
7490             while (SPACE_OR_TAB(*d))
7491                 d++;
7492             if (*d == ')' && (c.sv = cv_const_sv_or_av(c.cv)))
7493                 return yyl_constant_op(aTHX_ d + 1, c.sv, c.cv, c.rv2cv_op, c.off);
7494         }
7495         NEXTVAL_NEXTTOKE.opval =
7496             c.off ? c.rv2cv_op : pl_yylval.opval;
7497         if (c.off)
7498              op_free(pl_yylval.opval), force_next(PRIVATEREF);
7499         else op_free(c.rv2cv_op),      force_next(BAREWORD);
7500         pl_yylval.ival = 0;
7501         TOKEN('&');
7502     }
7503
7504     /* If followed by var or block, call it a method (unless sub) */
7505
7506     if ((*s == '$' || *s == '{') && !c.cv && FEATURE_INDIRECT_IS_ENABLED) {
7507         op_free(c.rv2cv_op);
7508         PL_last_lop = PL_oldbufptr;
7509         PL_last_lop_op = OP_METHOD;
7510         if (!PL_lex_allbrackets && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7511             PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7512         PL_expect = XBLOCKTERM;
7513         PL_bufptr = s;
7514         return REPORT(METHOD);
7515     }
7516
7517     /* If followed by a bareword, see if it looks like indir obj. */
7518
7519     if (   key == 1
7520         && !orig_keyword
7521         && (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF) || *s == '$')
7522         && (key = intuit_method(s, c.lex ? NULL : c.sv, c.cv)))
7523     {
7524       method:
7525         if (c.lex && !c.off) {
7526             assert(cSVOPx(pl_yylval.opval)->op_sv == c.sv);
7527             SvREADONLY_off(c.sv);
7528             sv_setpvn(c.sv, PL_tokenbuf, len);
7529             if (UTF && !IN_BYTES
7530              && is_utf8_string((U8*)PL_tokenbuf, len))
7531                 SvUTF8_on(c.sv);
7532             else SvUTF8_off(c.sv);
7533         }
7534         op_free(c.rv2cv_op);
7535         if (key == METHOD && !PL_lex_allbrackets
7536             && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7537         {
7538             PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7539         }
7540         return REPORT(key);
7541     }
7542
7543     /* Not a method, so call it a subroutine (if defined) */
7544
7545     if (c.cv) {
7546         /* Check for a constant sub */
7547         c.sv = cv_const_sv_or_av(c.cv);
7548         return yyl_constant_op(aTHX_ s, c.sv, c.cv, c.rv2cv_op, c.off);
7549     }
7550
7551     /* Call it a bare word */
7552
7553     if (PL_hints & HINT_STRICT_SUBS)
7554         pl_yylval.opval->op_private |= OPpCONST_STRICT;
7555     else
7556         yyl_strictwarn_bareword(aTHX_ lastchar);
7557
7558     op_free(c.rv2cv_op);
7559
7560     return yyl_safe_bareword(aTHX_ s, lastchar);
7561 }
7562
7563 static int
7564 yyl_word_or_keyword(pTHX_ char *s, STRLEN len, I32 key, I32 orig_keyword, struct code c)
7565 {
7566     switch (key) {
7567     default:                    /* not a keyword */
7568         return yyl_just_a_word(aTHX_ s, len, orig_keyword, c);
7569
7570     case KEY___FILE__:
7571         FUN0OP( newSVOP(OP_CONST, 0, newSVpv(CopFILE(PL_curcop),0)) );
7572
7573     case KEY___LINE__:
7574         FUN0OP(
7575             newSVOP(OP_CONST, 0,
7576                 Perl_newSVpvf(aTHX_ "%" IVdf, (IV)CopLINE(PL_curcop)))
7577         );
7578
7579     case KEY___PACKAGE__:
7580         FUN0OP(
7581             newSVOP(OP_CONST, 0, (PL_curstash
7582                                      ? newSVhek(HvNAME_HEK(PL_curstash))
7583                                      : &PL_sv_undef))
7584         );
7585
7586     case KEY___DATA__:
7587     case KEY___END__:
7588         if (PL_rsfp && (!PL_in_eval || PL_tokenbuf[2] == 'D'))
7589             yyl_data_handle(aTHX);
7590         return yyl_fake_eof(aTHX_ LEX_FAKE_EOF, FALSE, s, len);
7591
7592     case KEY___SUB__:
7593         FUN0OP(CvCLONE(PL_compcv)
7594                     ? newOP(OP_RUNCV, 0)
7595                     : newPVOP(OP_RUNCV,0,NULL));
7596
7597     case KEY_AUTOLOAD:
7598     case KEY_DESTROY:
7599     case KEY_BEGIN:
7600     case KEY_UNITCHECK:
7601     case KEY_CHECK:
7602     case KEY_INIT:
7603     case KEY_END:
7604         if (PL_expect == XSTATE)
7605             return yyl_sub(aTHX_ PL_bufptr, key);
7606         return yyl_just_a_word(aTHX_ s, len, orig_keyword, c);
7607
7608     case KEY_abs:
7609         UNI(OP_ABS);
7610
7611     case KEY_alarm:
7612         UNI(OP_ALARM);
7613
7614     case KEY_accept:
7615         LOP(OP_ACCEPT,XTERM);
7616
7617     case KEY_and:
7618         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
7619             return REPORT(0);
7620         OPERATOR(ANDOP);
7621
7622     case KEY_atan2:
7623         LOP(OP_ATAN2,XTERM);
7624
7625     case KEY_bind:
7626         LOP(OP_BIND,XTERM);
7627
7628     case KEY_binmode:
7629         LOP(OP_BINMODE,XTERM);
7630
7631     case KEY_bless:
7632         LOP(OP_BLESS,XTERM);
7633
7634     case KEY_break:
7635         FUN0(OP_BREAK);
7636
7637     case KEY_chop:
7638         UNI(OP_CHOP);
7639
7640     case KEY_continue:
7641         /* We have to disambiguate the two senses of
7642           "continue". If the next token is a '{' then
7643           treat it as the start of a continue block;
7644           otherwise treat it as a control operator.
7645          */
7646         s = skipspace(s);
7647         if (*s == '{')
7648             PREBLOCK(CONTINUE);
7649         else
7650             FUN0(OP_CONTINUE);
7651
7652     case KEY_chdir:
7653         /* may use HOME */
7654         (void)gv_fetchpvs("ENV", GV_ADD|GV_NOTQUAL, SVt_PVHV);
7655         UNI(OP_CHDIR);
7656
7657     case KEY_close:
7658         UNI(OP_CLOSE);
7659
7660     case KEY_closedir:
7661         UNI(OP_CLOSEDIR);
7662
7663     case KEY_cmp:
7664         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7665             return REPORT(0);
7666         NCEop(OP_SCMP);
7667
7668     case KEY_caller:
7669         UNI(OP_CALLER);
7670
7671     case KEY_crypt:
7672 #ifdef FCRYPT
7673         if (!PL_cryptseen) {
7674             PL_cryptseen = TRUE;
7675             init_des();
7676         }
7677 #endif
7678         LOP(OP_CRYPT,XTERM);
7679
7680     case KEY_chmod:
7681         LOP(OP_CHMOD,XTERM);
7682
7683     case KEY_chown:
7684         LOP(OP_CHOWN,XTERM);
7685
7686     case KEY_connect:
7687         LOP(OP_CONNECT,XTERM);
7688
7689     case KEY_chr:
7690         UNI(OP_CHR);
7691
7692     case KEY_cos:
7693         UNI(OP_COS);
7694
7695     case KEY_chroot:
7696         UNI(OP_CHROOT);
7697
7698     case KEY_default:
7699         PREBLOCK(DEFAULT);
7700
7701     case KEY_do:
7702         return yyl_do(aTHX_ s, orig_keyword);
7703
7704     case KEY_die:
7705         PL_hints |= HINT_BLOCK_SCOPE;
7706         LOP(OP_DIE,XTERM);
7707
7708     case KEY_defined:
7709         UNI(OP_DEFINED);
7710
7711     case KEY_delete:
7712         UNI(OP_DELETE);
7713
7714     case KEY_dbmopen:
7715         Perl_populate_isa(aTHX_ STR_WITH_LEN("AnyDBM_File::ISA"),
7716                           STR_WITH_LEN("NDBM_File::"),
7717                           STR_WITH_LEN("DB_File::"),
7718                           STR_WITH_LEN("GDBM_File::"),
7719                           STR_WITH_LEN("SDBM_File::"),
7720                           STR_WITH_LEN("ODBM_File::"),
7721                           NULL);
7722         LOP(OP_DBMOPEN,XTERM);
7723
7724     case KEY_dbmclose:
7725         UNI(OP_DBMCLOSE);
7726
7727     case KEY_dump:
7728         LOOPX(OP_DUMP);
7729
7730     case KEY_else:
7731         PREBLOCK(ELSE);
7732
7733     case KEY_elsif:
7734         pl_yylval.ival = CopLINE(PL_curcop);
7735         OPERATOR(ELSIF);
7736
7737     case KEY_eq:
7738         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7739             return REPORT(0);
7740         ChEop(OP_SEQ);
7741
7742     case KEY_exists:
7743         UNI(OP_EXISTS);
7744
7745     case KEY_exit:
7746         UNI(OP_EXIT);
7747
7748     case KEY_eval:
7749         s = skipspace(s);
7750         if (*s == '{') { /* block eval */
7751             PL_expect = XTERMBLOCK;
7752             UNIBRACK(OP_ENTERTRY);
7753         }
7754         else { /* string eval */
7755             PL_expect = XTERM;
7756             UNIBRACK(OP_ENTEREVAL);
7757         }
7758
7759     case KEY_evalbytes:
7760         PL_expect = XTERM;
7761         UNIBRACK(-OP_ENTEREVAL);
7762
7763     case KEY_eof:
7764         UNI(OP_EOF);
7765
7766     case KEY_exp:
7767         UNI(OP_EXP);
7768
7769     case KEY_each:
7770         UNI(OP_EACH);
7771
7772     case KEY_exec:
7773         LOP(OP_EXEC,XREF);
7774
7775     case KEY_endhostent:
7776         FUN0(OP_EHOSTENT);
7777
7778     case KEY_endnetent:
7779         FUN0(OP_ENETENT);
7780
7781     case KEY_endservent:
7782         FUN0(OP_ESERVENT);
7783
7784     case KEY_endprotoent:
7785         FUN0(OP_EPROTOENT);
7786
7787     case KEY_endpwent:
7788         FUN0(OP_EPWENT);
7789
7790     case KEY_endgrent:
7791         FUN0(OP_EGRENT);
7792
7793     case KEY_for:
7794     case KEY_foreach:
7795         return yyl_foreach(aTHX_ s);
7796
7797     case KEY_formline:
7798         LOP(OP_FORMLINE,XTERM);
7799
7800     case KEY_fork:
7801         FUN0(OP_FORK);
7802
7803     case KEY_fc:
7804         UNI(OP_FC);
7805
7806     case KEY_fcntl:
7807         LOP(OP_FCNTL,XTERM);
7808
7809     case KEY_fileno:
7810         UNI(OP_FILENO);
7811
7812     case KEY_flock:
7813         LOP(OP_FLOCK,XTERM);
7814
7815     case KEY_gt:
7816         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7817             return REPORT(0);
7818         ChRop(OP_SGT);
7819
7820     case KEY_ge:
7821         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7822             return REPORT(0);
7823         ChRop(OP_SGE);
7824
7825     case KEY_grep:
7826         LOP(OP_GREPSTART, XREF);
7827
7828     case KEY_goto:
7829         LOOPX(OP_GOTO);
7830
7831     case KEY_gmtime:
7832         UNI(OP_GMTIME);
7833
7834     case KEY_getc:
7835         UNIDOR(OP_GETC);
7836
7837     case KEY_getppid:
7838         FUN0(OP_GETPPID);
7839
7840     case KEY_getpgrp:
7841         UNI(OP_GETPGRP);
7842
7843     case KEY_getpriority:
7844         LOP(OP_GETPRIORITY,XTERM);
7845
7846     case KEY_getprotobyname:
7847         UNI(OP_GPBYNAME);
7848
7849     case KEY_getprotobynumber:
7850         LOP(OP_GPBYNUMBER,XTERM);
7851
7852     case KEY_getprotoent:
7853         FUN0(OP_GPROTOENT);
7854
7855     case KEY_getpwent:
7856         FUN0(OP_GPWENT);
7857
7858     case KEY_getpwnam:
7859         UNI(OP_GPWNAM);
7860
7861     case KEY_getpwuid:
7862         UNI(OP_GPWUID);
7863
7864     case KEY_getpeername:
7865         UNI(OP_GETPEERNAME);
7866
7867     case KEY_gethostbyname:
7868         UNI(OP_GHBYNAME);
7869
7870     case KEY_gethostbyaddr:
7871         LOP(OP_GHBYADDR,XTERM);
7872
7873     case KEY_gethostent:
7874         FUN0(OP_GHOSTENT);
7875
7876     case KEY_getnetbyname:
7877         UNI(OP_GNBYNAME);
7878
7879     case KEY_getnetbyaddr:
7880         LOP(OP_GNBYADDR,XTERM);
7881
7882     case KEY_getnetent:
7883         FUN0(OP_GNETENT);
7884
7885     case KEY_getservbyname:
7886         LOP(OP_GSBYNAME,XTERM);
7887
7888     case KEY_getservbyport:
7889         LOP(OP_GSBYPORT,XTERM);
7890
7891     case KEY_getservent:
7892         FUN0(OP_GSERVENT);
7893
7894     case KEY_getsockname:
7895         UNI(OP_GETSOCKNAME);
7896
7897     case KEY_getsockopt:
7898         LOP(OP_GSOCKOPT,XTERM);
7899
7900     case KEY_getgrent:
7901         FUN0(OP_GGRENT);
7902
7903     case KEY_getgrnam:
7904         UNI(OP_GGRNAM);
7905
7906     case KEY_getgrgid:
7907         UNI(OP_GGRGID);
7908
7909     case KEY_getlogin:
7910         FUN0(OP_GETLOGIN);
7911
7912     case KEY_given:
7913         pl_yylval.ival = CopLINE(PL_curcop);
7914         Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
7915                          "given is experimental");
7916         OPERATOR(GIVEN);
7917
7918     case KEY_glob:
7919         LOP( orig_keyword==KEY_glob ? -OP_GLOB : OP_GLOB, XTERM );
7920
7921     case KEY_hex:
7922         UNI(OP_HEX);
7923
7924     case KEY_if:
7925         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
7926             return REPORT(0);
7927         pl_yylval.ival = CopLINE(PL_curcop);
7928         OPERATOR(IF);
7929
7930     case KEY_index:
7931         LOP(OP_INDEX,XTERM);
7932
7933     case KEY_int:
7934         UNI(OP_INT);
7935
7936     case KEY_ioctl:
7937         LOP(OP_IOCTL,XTERM);
7938
7939     case KEY_isa:
7940         Perl_ck_warner_d(aTHX_
7941             packWARN(WARN_EXPERIMENTAL__ISA), "isa is experimental");
7942         NCRop(OP_ISA);
7943
7944     case KEY_join:
7945         LOP(OP_JOIN,XTERM);
7946
7947     case KEY_keys:
7948         UNI(OP_KEYS);
7949
7950     case KEY_kill:
7951         LOP(OP_KILL,XTERM);
7952
7953     case KEY_last:
7954         LOOPX(OP_LAST);
7955
7956     case KEY_lc:
7957         UNI(OP_LC);
7958
7959     case KEY_lcfirst:
7960         UNI(OP_LCFIRST);
7961
7962     case KEY_local:
7963         OPERATOR(LOCAL);
7964
7965     case KEY_length:
7966         UNI(OP_LENGTH);
7967
7968     case KEY_lt:
7969         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7970             return REPORT(0);
7971         ChRop(OP_SLT);
7972
7973     case KEY_le:
7974         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7975             return REPORT(0);
7976         ChRop(OP_SLE);
7977
7978     case KEY_localtime:
7979         UNI(OP_LOCALTIME);
7980
7981     case KEY_log:
7982         UNI(OP_LOG);
7983
7984     case KEY_link:
7985         LOP(OP_LINK,XTERM);
7986
7987     case KEY_listen:
7988         LOP(OP_LISTEN,XTERM);
7989
7990     case KEY_lock:
7991         UNI(OP_LOCK);
7992
7993     case KEY_lstat:
7994         UNI(OP_LSTAT);
7995
7996     case KEY_m:
7997         s = scan_pat(s,OP_MATCH);
7998         TERM(sublex_start());
7999
8000     case KEY_map:
8001         LOP(OP_MAPSTART, XREF);
8002
8003     case KEY_mkdir:
8004         LOP(OP_MKDIR,XTERM);
8005
8006     case KEY_msgctl:
8007         LOP(OP_MSGCTL,XTERM);
8008
8009     case KEY_msgget:
8010         LOP(OP_MSGGET,XTERM);
8011
8012     case KEY_msgrcv:
8013         LOP(OP_MSGRCV,XTERM);
8014
8015     case KEY_msgsnd:
8016         LOP(OP_MSGSND,XTERM);
8017
8018     case KEY_our:
8019     case KEY_my:
8020     case KEY_state:
8021         return yyl_my(aTHX_ s, key);
8022
8023     case KEY_next:
8024         LOOPX(OP_NEXT);
8025
8026     case KEY_ne:
8027         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8028             return REPORT(0);
8029         ChEop(OP_SNE);
8030
8031     case KEY_no:
8032         s = tokenize_use(0, s);
8033         TOKEN(USE);
8034
8035     case KEY_not:
8036         if (*s == '(' || (s = skipspace(s), *s == '('))
8037             FUN1(OP_NOT);
8038         else {
8039             if (!PL_lex_allbrackets && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
8040                 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
8041             OPERATOR(NOTOP);
8042         }
8043
8044     case KEY_open:
8045         s = skipspace(s);
8046         if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
8047             const char *t;
8048             char *d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
8049             for (t=d; isSPACE(*t);)
8050                 t++;
8051             if ( *t && memCHRs("|&*+-=!?:.", *t) && ckWARN_d(WARN_PRECEDENCE)
8052                 /* [perl #16184] */
8053                 && !(t[0] == '=' && t[1] == '>')
8054                 && !(t[0] == ':' && t[1] == ':')
8055                 && !keyword(s, d-s, 0)
8056             ) {
8057                 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
8058                    "Precedence problem: open %" UTF8f " should be open(%" UTF8f ")",
8059                     UTF8fARG(UTF, d-s, s), UTF8fARG(UTF, d-s, s));
8060             }
8061         }
8062         LOP(OP_OPEN,XTERM);
8063
8064     case KEY_or:
8065         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
8066             return REPORT(0);
8067         pl_yylval.ival = OP_OR;
8068         OPERATOR(OROP);
8069
8070     case KEY_ord:
8071         UNI(OP_ORD);
8072
8073     case KEY_oct:
8074         UNI(OP_OCT);
8075
8076     case KEY_opendir:
8077         LOP(OP_OPEN_DIR,XTERM);
8078
8079     case KEY_print:
8080         checkcomma(s,PL_tokenbuf,"filehandle");
8081         LOP(OP_PRINT,XREF);
8082
8083     case KEY_printf:
8084         checkcomma(s,PL_tokenbuf,"filehandle");
8085         LOP(OP_PRTF,XREF);
8086
8087     case KEY_prototype:
8088         UNI(OP_PROTOTYPE);
8089
8090     case KEY_push:
8091         LOP(OP_PUSH,XTERM);
8092
8093     case KEY_pop:
8094         UNIDOR(OP_POP);
8095
8096     case KEY_pos:
8097         UNIDOR(OP_POS);
8098
8099     case KEY_pack:
8100         LOP(OP_PACK,XTERM);
8101
8102     case KEY_package:
8103         s = force_word(s,BAREWORD,FALSE,TRUE);
8104         s = skipspace(s);
8105         s = force_strict_version(s);
8106         PREBLOCK(PACKAGE);
8107
8108     case KEY_pipe:
8109         LOP(OP_PIPE_OP,XTERM);
8110
8111     case KEY_q:
8112         s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8113         if (!s)
8114             missingterm(NULL, 0);
8115         COPLINE_SET_FROM_MULTI_END;
8116         pl_yylval.ival = OP_CONST;
8117         TERM(sublex_start());
8118
8119     case KEY_quotemeta:
8120         UNI(OP_QUOTEMETA);
8121
8122     case KEY_qw:
8123         return yyl_qw(aTHX_ s, len);
8124
8125     case KEY_qq:
8126         s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8127         if (!s)
8128             missingterm(NULL, 0);
8129         pl_yylval.ival = OP_STRINGIFY;
8130         if (SvIVX(PL_lex_stuff) == '\'')
8131             SvIV_set(PL_lex_stuff, 0);  /* qq'$foo' should interpolate */
8132         TERM(sublex_start());
8133
8134     case KEY_qr:
8135         s = scan_pat(s,OP_QR);
8136         TERM(sublex_start());
8137
8138     case KEY_qx:
8139         s = scan_str(s,FALSE,FALSE,FALSE,NULL);
8140         if (!s)
8141             missingterm(NULL, 0);
8142         pl_yylval.ival = OP_BACKTICK;
8143         TERM(sublex_start());
8144
8145     case KEY_return:
8146         OLDLOP(OP_RETURN);
8147
8148     case KEY_require:
8149         return yyl_require(aTHX_ s, orig_keyword);
8150
8151     case KEY_reset:
8152         UNI(OP_RESET);
8153
8154     case KEY_redo:
8155         LOOPX(OP_REDO);
8156
8157     case KEY_rename:
8158         LOP(OP_RENAME,XTERM);
8159
8160     case KEY_rand:
8161         UNI(OP_RAND);
8162
8163     case KEY_rmdir:
8164         UNI(OP_RMDIR);
8165
8166     case KEY_rindex:
8167         LOP(OP_RINDEX,XTERM);
8168
8169     case KEY_read:
8170         LOP(OP_READ,XTERM);
8171
8172     case KEY_readdir:
8173         UNI(OP_READDIR);
8174
8175     case KEY_readline:
8176         UNIDOR(OP_READLINE);
8177
8178     case KEY_readpipe:
8179         UNIDOR(OP_BACKTICK);
8180
8181     case KEY_rewinddir:
8182         UNI(OP_REWINDDIR);
8183
8184     case KEY_recv:
8185         LOP(OP_RECV,XTERM);
8186
8187     case KEY_reverse:
8188         LOP(OP_REVERSE,XTERM);
8189
8190     case KEY_readlink:
8191         UNIDOR(OP_READLINK);
8192
8193     case KEY_ref:
8194         UNI(OP_REF);
8195
8196     case KEY_s:
8197         s = scan_subst(s);
8198         if (pl_yylval.opval)
8199             TERM(sublex_start());
8200         else
8201             TOKEN(1);   /* force error */
8202
8203     case KEY_say:
8204         checkcomma(s,PL_tokenbuf,"filehandle");
8205         LOP(OP_SAY,XREF);
8206
8207     case KEY_chomp:
8208         UNI(OP_CHOMP);
8209
8210     case KEY_scalar:
8211         UNI(OP_SCALAR);
8212
8213     case KEY_select:
8214         LOP(OP_SELECT,XTERM);
8215
8216     case KEY_seek:
8217         LOP(OP_SEEK,XTERM);
8218
8219     case KEY_semctl:
8220         LOP(OP_SEMCTL,XTERM);
8221
8222     case KEY_semget:
8223         LOP(OP_SEMGET,XTERM);
8224
8225     case KEY_semop:
8226         LOP(OP_SEMOP,XTERM);
8227
8228     case KEY_send:
8229         LOP(OP_SEND,XTERM);
8230
8231     case KEY_setpgrp:
8232         LOP(OP_SETPGRP,XTERM);
8233
8234     case KEY_setpriority:
8235         LOP(OP_SETPRIORITY,XTERM);
8236
8237     case KEY_sethostent:
8238         UNI(OP_SHOSTENT);
8239
8240     case KEY_setnetent:
8241         UNI(OP_SNETENT);
8242
8243     case KEY_setservent:
8244         UNI(OP_SSERVENT);
8245
8246     case KEY_setprotoent:
8247         UNI(OP_SPROTOENT);
8248
8249     case KEY_setpwent:
8250         FUN0(OP_SPWENT);
8251
8252     case KEY_setgrent:
8253         FUN0(OP_SGRENT);
8254
8255     case KEY_seekdir:
8256         LOP(OP_SEEKDIR,XTERM);
8257
8258     case KEY_setsockopt:
8259         LOP(OP_SSOCKOPT,XTERM);
8260
8261     case KEY_shift:
8262         UNIDOR(OP_SHIFT);
8263
8264     case KEY_shmctl:
8265         LOP(OP_SHMCTL,XTERM);
8266
8267     case KEY_shmget:
8268         LOP(OP_SHMGET,XTERM);
8269
8270     case KEY_shmread:
8271         LOP(OP_SHMREAD,XTERM);
8272
8273     case KEY_shmwrite:
8274         LOP(OP_SHMWRITE,XTERM);
8275
8276     case KEY_shutdown:
8277         LOP(OP_SHUTDOWN,XTERM);
8278
8279     case KEY_sin:
8280         UNI(OP_SIN);
8281
8282     case KEY_sleep:
8283         UNI(OP_SLEEP);
8284
8285     case KEY_socket:
8286         LOP(OP_SOCKET,XTERM);
8287
8288     case KEY_socketpair:
8289         LOP(OP_SOCKPAIR,XTERM);
8290
8291     case KEY_sort:
8292         checkcomma(s,PL_tokenbuf,"subroutine name");
8293         s = skipspace(s);
8294         PL_expect = XTERM;
8295         s = force_word(s,BAREWORD,TRUE,TRUE);
8296         LOP(OP_SORT,XREF);
8297
8298     case KEY_split:
8299         LOP(OP_SPLIT,XTERM);
8300
8301     case KEY_sprintf:
8302         LOP(OP_SPRINTF,XTERM);
8303
8304     case KEY_splice:
8305         LOP(OP_SPLICE,XTERM);
8306
8307     case KEY_sqrt:
8308         UNI(OP_SQRT);
8309
8310     case KEY_srand:
8311         UNI(OP_SRAND);
8312
8313     case KEY_stat:
8314         UNI(OP_STAT);
8315
8316     case KEY_study:
8317         UNI(OP_STUDY);
8318
8319     case KEY_substr:
8320         LOP(OP_SUBSTR,XTERM);
8321
8322     case KEY_format:
8323     case KEY_sub:
8324         return yyl_sub(aTHX_ s, key);
8325
8326     case KEY_system:
8327         LOP(OP_SYSTEM,XREF);
8328
8329     case KEY_symlink:
8330         LOP(OP_SYMLINK,XTERM);
8331
8332     case KEY_syscall:
8333         LOP(OP_SYSCALL,XTERM);
8334
8335     case KEY_sysopen:
8336         LOP(OP_SYSOPEN,XTERM);
8337
8338     case KEY_sysseek:
8339         LOP(OP_SYSSEEK,XTERM);
8340
8341     case KEY_sysread:
8342         LOP(OP_SYSREAD,XTERM);
8343
8344     case KEY_syswrite:
8345         LOP(OP_SYSWRITE,XTERM);
8346
8347     case KEY_tr:
8348     case KEY_y:
8349         s = scan_trans(s);
8350         TERM(sublex_start());
8351
8352     case KEY_tell:
8353         UNI(OP_TELL);
8354
8355     case KEY_telldir:
8356         UNI(OP_TELLDIR);
8357
8358     case KEY_tie:
8359         LOP(OP_TIE,XTERM);
8360
8361     case KEY_tied:
8362         UNI(OP_TIED);
8363
8364     case KEY_time:
8365         FUN0(OP_TIME);
8366
8367     case KEY_times:
8368         FUN0(OP_TMS);
8369
8370     case KEY_truncate:
8371         LOP(OP_TRUNCATE,XTERM);
8372
8373     case KEY_uc:
8374         UNI(OP_UC);
8375
8376     case KEY_ucfirst:
8377         UNI(OP_UCFIRST);
8378
8379     case KEY_untie:
8380         UNI(OP_UNTIE);
8381
8382     case KEY_until:
8383         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8384             return REPORT(0);
8385         pl_yylval.ival = CopLINE(PL_curcop);
8386         OPERATOR(UNTIL);
8387
8388     case KEY_unless:
8389         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8390             return REPORT(0);
8391         pl_yylval.ival = CopLINE(PL_curcop);
8392         OPERATOR(UNLESS);
8393
8394     case KEY_unlink:
8395         LOP(OP_UNLINK,XTERM);
8396
8397     case KEY_undef:
8398         UNIDOR(OP_UNDEF);
8399
8400     case KEY_unpack:
8401         LOP(OP_UNPACK,XTERM);
8402
8403     case KEY_utime:
8404         LOP(OP_UTIME,XTERM);
8405
8406     case KEY_umask:
8407         UNIDOR(OP_UMASK);
8408
8409     case KEY_unshift:
8410         LOP(OP_UNSHIFT,XTERM);
8411
8412     case KEY_use:
8413         s = tokenize_use(1, s);
8414         TOKEN(USE);
8415
8416     case KEY_values:
8417         UNI(OP_VALUES);
8418
8419     case KEY_vec:
8420         LOP(OP_VEC,XTERM);
8421
8422     case KEY_when:
8423         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8424             return REPORT(0);
8425         pl_yylval.ival = CopLINE(PL_curcop);
8426         Perl_ck_warner_d(aTHX_
8427             packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
8428             "when is experimental");
8429         OPERATOR(WHEN);
8430
8431     case KEY_while:
8432         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8433             return REPORT(0);
8434         pl_yylval.ival = CopLINE(PL_curcop);
8435         OPERATOR(WHILE);
8436
8437     case KEY_warn:
8438         PL_hints |= HINT_BLOCK_SCOPE;
8439         LOP(OP_WARN,XTERM);
8440
8441     case KEY_wait:
8442         FUN0(OP_WAIT);
8443
8444     case KEY_waitpid:
8445         LOP(OP_WAITPID,XTERM);
8446
8447     case KEY_wantarray:
8448         FUN0(OP_WANTARRAY);
8449
8450     case KEY_write:
8451         /* Make sure $^L is defined. 0x0C is CTRL-L on ASCII platforms, and
8452          * we use the same number on EBCDIC */
8453         gv_fetchpvs("\x0C", GV_ADD|GV_NOTQUAL, SVt_PV);
8454         UNI(OP_ENTERWRITE);
8455
8456     case KEY_x:
8457         if (PL_expect == XOPERATOR) {
8458             if (*s == '=' && !PL_lex_allbrackets
8459                 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
8460             {
8461                 return REPORT(0);
8462             }
8463             Mop(OP_REPEAT);
8464         }
8465         check_uni();
8466         return yyl_just_a_word(aTHX_ s, len, orig_keyword, c);
8467
8468     case KEY_xor:
8469         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
8470             return REPORT(0);
8471         pl_yylval.ival = OP_XOR;
8472         OPERATOR(OROP);
8473     }
8474 }
8475
8476 static int
8477 yyl_key_core(pTHX_ char *s, STRLEN len, struct code c)
8478 {
8479     I32 key = 0;
8480     I32 orig_keyword = 0;
8481     STRLEN olen = len;
8482     char *d = s;
8483     s += 2;
8484     s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
8485     if ((*s == ':' && s[1] == ':')
8486         || (!(key = keyword(PL_tokenbuf, len, 1)) && *s == '\''))
8487     {
8488         Copy(PL_bufptr, PL_tokenbuf, olen, char);
8489         return yyl_just_a_word(aTHX_ d, olen, 0, c);
8490     }
8491     if (!key)
8492         Perl_croak(aTHX_ "CORE::%" UTF8f " is not a keyword",
8493                           UTF8fARG(UTF, len, PL_tokenbuf));
8494     if (key < 0)
8495         key = -key;
8496     else if (key == KEY_require || key == KEY_do
8497           || key == KEY_glob)
8498         /* that's a way to remember we saw "CORE::" */
8499         orig_keyword = key;
8500
8501     /* Known to be a reserved word at this point */
8502     return yyl_word_or_keyword(aTHX_ s, len, key, orig_keyword, c);
8503 }
8504
8505 static int
8506 yyl_keylookup(pTHX_ char *s, GV *gv)
8507 {
8508     dVAR;
8509     STRLEN len;
8510     bool anydelim;
8511     I32 key;
8512     struct code c = no_code;
8513     I32 orig_keyword = 0;
8514     char *d;
8515
8516     c.gv = gv;
8517
8518     PL_bufptr = s;
8519     s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
8520
8521     /* Some keywords can be followed by any delimiter, including ':' */
8522     anydelim = word_takes_any_delimiter(PL_tokenbuf, len);
8523
8524     /* x::* is just a word, unless x is "CORE" */
8525     if (!anydelim && *s == ':' && s[1] == ':') {
8526         if (memEQs(PL_tokenbuf, len, "CORE"))
8527             return yyl_key_core(aTHX_ s, len, c);
8528         return yyl_just_a_word(aTHX_ s, len, 0, c);
8529     }
8530
8531     d = s;
8532     while (d < PL_bufend && isSPACE(*d))
8533             d++;        /* no comments skipped here, or s### is misparsed */
8534
8535     /* Is this a word before a => operator? */
8536     if (*d == '=' && d[1] == '>') {
8537         return yyl_fatcomma(aTHX_ s, len);
8538     }
8539
8540     /* Check for plugged-in keyword */
8541     {
8542         OP *o;
8543         int result;
8544         char *saved_bufptr = PL_bufptr;
8545         PL_bufptr = s;
8546         result = PL_keyword_plugin(aTHX_ PL_tokenbuf, len, &o);
8547         s = PL_bufptr;
8548         if (result == KEYWORD_PLUGIN_DECLINE) {
8549             /* not a plugged-in keyword */
8550             PL_bufptr = saved_bufptr;
8551         } else if (result == KEYWORD_PLUGIN_STMT) {
8552             pl_yylval.opval = o;
8553             CLINE;
8554             if (!PL_nexttoke) PL_expect = XSTATE;
8555             return REPORT(PLUGSTMT);
8556         } else if (result == KEYWORD_PLUGIN_EXPR) {
8557             pl_yylval.opval = o;
8558             CLINE;
8559             if (!PL_nexttoke) PL_expect = XOPERATOR;
8560             return REPORT(PLUGEXPR);
8561         } else {
8562             Perl_croak(aTHX_ "Bad plugin affecting keyword '%s'", PL_tokenbuf);
8563         }
8564     }
8565
8566     /* Is this a label? */
8567     if (!anydelim && PL_expect == XSTATE
8568           && d < PL_bufend && *d == ':' && *(d + 1) != ':') {
8569         s = d + 1;
8570         pl_yylval.opval =
8571             newSVOP(OP_CONST, 0,
8572                 newSVpvn_flags(PL_tokenbuf, len, UTF ? SVf_UTF8 : 0));
8573         CLINE;
8574         TOKEN(LABEL);
8575     }
8576
8577     /* Check for lexical sub */
8578     if (PL_expect != XOPERATOR) {
8579         char tmpbuf[sizeof PL_tokenbuf + 1];
8580         *tmpbuf = '&';
8581         Copy(PL_tokenbuf, tmpbuf+1, len, char);
8582         c.off = pad_findmy_pvn(tmpbuf, len+1, 0);
8583         if (c.off != NOT_IN_PAD) {
8584             assert(c.off); /* we assume this is boolean-true below */
8585             if (PAD_COMPNAME_FLAGS_isOUR(c.off)) {
8586                 HV *  const stash = PAD_COMPNAME_OURSTASH(c.off);
8587                 HEK * const stashname = HvNAME_HEK(stash);
8588                 c.sv = newSVhek(stashname);
8589                 sv_catpvs(c.sv, "::");
8590                 sv_catpvn_flags(c.sv, PL_tokenbuf, len,
8591                                 (UTF ? SV_CATUTF8 : SV_CATBYTES));
8592                 c.gv = gv_fetchsv(c.sv, GV_NOADD_NOINIT | SvUTF8(c.sv),
8593                                   SVt_PVCV);
8594                 c.off = 0;
8595                 if (!c.gv) {
8596                     sv_free(c.sv);
8597                     c.sv = NULL;
8598                     return yyl_just_a_word(aTHX_ s, len, 0, c);
8599                 }
8600             }
8601             else {
8602                 c.rv2cv_op = newOP(OP_PADANY, 0);
8603                 c.rv2cv_op->op_targ = c.off;
8604                 c.cv = find_lexical_cv(c.off);
8605             }
8606             c.lex = TRUE;
8607             return yyl_just_a_word(aTHX_ s, len, 0, c);
8608         }
8609         c.off = 0;
8610     }
8611
8612     /* Check for built-in keyword */
8613     key = keyword(PL_tokenbuf, len, 0);
8614
8615     if (key < 0)
8616         key = yyl_secondclass_keyword(aTHX_ s, len, key, &orig_keyword, &c.gv, &c.gvp);
8617
8618     if (key && key != KEY___DATA__ && key != KEY___END__
8619      && (!anydelim || *s != '#')) {
8620         /* no override, and not s### either; skipspace is safe here
8621          * check for => on following line */
8622         bool arrow;
8623         STRLEN bufoff = PL_bufptr - SvPVX(PL_linestr);
8624         STRLEN   soff = s         - SvPVX(PL_linestr);
8625         s = peekspace(s);
8626         arrow = *s == '=' && s[1] == '>';
8627         PL_bufptr = SvPVX(PL_linestr) + bufoff;
8628         s         = SvPVX(PL_linestr) +   soff;
8629         if (arrow)
8630             return yyl_fatcomma(aTHX_ s, len);
8631     }
8632
8633     return yyl_word_or_keyword(aTHX_ s, len, key, orig_keyword, c);
8634 }
8635
8636 static int
8637 yyl_try(pTHX_ char *s, STRLEN len)
8638 {
8639     char *d;
8640     GV *gv = NULL;
8641
8642   retry:
8643     switch (*s) {
8644     default:
8645         if (UTF ? isIDFIRST_utf8_safe(s, PL_bufend) : isALNUMC(*s))
8646             return yyl_keylookup(aTHX_ s, gv);
8647         yyl_croak_unrecognised(aTHX_ s);
8648
8649     case 4:
8650     case 26:
8651         /* emulate EOF on ^D or ^Z */
8652         return yyl_fake_eof(aTHX_ LEX_FAKE_EOF, FALSE, s, len);
8653
8654     case 0:
8655         if ((!PL_rsfp || PL_lex_inwhat)
8656          && (!PL_parser->filtered || s+1 < PL_bufend)) {
8657             PL_last_uni = 0;
8658             PL_last_lop = 0;
8659             if (PL_lex_brackets
8660                 && PL_lex_brackstack[PL_lex_brackets-1] != XFAKEEOF)
8661             {
8662                 yyerror((const char *)
8663                         (PL_lex_formbrack
8664                          ? "Format not terminated"
8665                          : "Missing right curly or square bracket"));
8666             }
8667             DEBUG_T({
8668                 PerlIO_printf(Perl_debug_log, "### Tokener got EOF\n");
8669             });
8670             TOKEN(0);
8671         }
8672         if (s++ < PL_bufend)
8673             goto retry;  /* ignore stray nulls */
8674         PL_last_uni = 0;
8675         PL_last_lop = 0;
8676         if (!PL_in_eval && !PL_preambled) {
8677             PL_preambled = TRUE;
8678             if (PL_perldb) {
8679                 /* Generate a string of Perl code to load the debugger.
8680                  * If PERL5DB is set, it will return the contents of that,
8681                  * otherwise a compile-time require of perl5db.pl.  */
8682
8683                 const char * const pdb = PerlEnv_getenv("PERL5DB");
8684
8685                 if (pdb) {
8686                     sv_setpv(PL_linestr, pdb);
8687                     sv_catpvs(PL_linestr,";");
8688                 } else {
8689                     SETERRNO(0,SS_NORMAL);
8690                     sv_setpvs(PL_linestr, "BEGIN { require 'perl5db.pl' };");
8691                 }
8692                 PL_parser->preambling = CopLINE(PL_curcop);
8693             } else
8694                 SvPVCLEAR(PL_linestr);
8695             if (PL_preambleav) {
8696                 SV **svp = AvARRAY(PL_preambleav);
8697                 SV **const end = svp + AvFILLp(PL_preambleav);
8698                 while(svp <= end) {
8699                     sv_catsv(PL_linestr, *svp);
8700                     ++svp;
8701                     sv_catpvs(PL_linestr, ";");
8702                 }
8703                 sv_free(MUTABLE_SV(PL_preambleav));
8704                 PL_preambleav = NULL;
8705             }
8706             if (PL_minus_E)
8707                 sv_catpvs(PL_linestr,
8708                           "use feature ':5." STRINGIFY(PERL_VERSION) "';");
8709             if (PL_minus_n || PL_minus_p) {
8710                 sv_catpvs(PL_linestr, "LINE: while (<>) {"/*}*/);
8711                 if (PL_minus_l)
8712                     sv_catpvs(PL_linestr,"chomp;");
8713                 if (PL_minus_a) {
8714                     if (PL_minus_F) {
8715                         if (   (   *PL_splitstr == '/'
8716                                 || *PL_splitstr == '\''
8717                                 || *PL_splitstr == '"')
8718                             && strchr(PL_splitstr + 1, *PL_splitstr))
8719                         {
8720                             /* strchr is ok, because -F pattern can't contain
8721                              * embeddded NULs */
8722                             Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
8723                         }
8724                         else {
8725                             /* "q\0${splitstr}\0" is legal perl. Yes, even NUL
8726                                bytes can be used as quoting characters.  :-) */
8727                             const char *splits = PL_splitstr;
8728                             sv_catpvs(PL_linestr, "our @F=split(q\0");
8729                             do {
8730                                 /* Need to \ \s  */
8731                                 if (*splits == '\\')
8732                                     sv_catpvn(PL_linestr, splits, 1);
8733                                 sv_catpvn(PL_linestr, splits, 1);
8734                             } while (*splits++);
8735                             /* This loop will embed the trailing NUL of
8736                                PL_linestr as the last thing it does before
8737                                terminating.  */
8738                             sv_catpvs(PL_linestr, ");");
8739                         }
8740                     }
8741                     else
8742                         sv_catpvs(PL_linestr,"our @F=split(' ');");
8743                 }
8744             }
8745             sv_catpvs(PL_linestr, "\n");
8746             PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
8747             PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
8748             PL_last_lop = PL_last_uni = NULL;
8749             if (PERLDB_LINE_OR_SAVESRC && PL_curstash != PL_debstash)
8750                 update_debugger_info(PL_linestr, NULL, 0);
8751             goto retry;
8752         }
8753         return yyl_fake_eof(aTHX_ 0, cBOOL(PL_rsfp), s, len);
8754
8755     case '\r':
8756 #ifdef PERL_STRICT_CR
8757         Perl_warn(aTHX_ "Illegal character \\%03o (carriage return)", '\r');
8758         Perl_croak(aTHX_
8759       "\t(Maybe you didn't strip carriage returns after a network transfer?)\n");
8760 #endif
8761     case ' ': case '\t': case '\f': case '\v':
8762         s++;
8763         goto retry;
8764
8765     case '#':
8766     case '\n': {
8767         const bool needs_semicolon = yyl_eol_needs_semicolon(aTHX_ &s);
8768         if (needs_semicolon)
8769             TOKEN(';');
8770         else
8771             goto retry;
8772     }
8773
8774     case '-':
8775         return yyl_hyphen(aTHX_ s);
8776
8777     case '+':
8778         return yyl_plus(aTHX_ s);
8779
8780     case '*':
8781         return yyl_star(aTHX_ s);
8782
8783     case '%':
8784         return yyl_percent(aTHX_ s);
8785
8786     case '^':
8787         return yyl_caret(aTHX_ s);
8788
8789     case '[':
8790         return yyl_leftsquare(aTHX_ s);
8791
8792     case '~':
8793         return yyl_tilde(aTHX_ s);
8794
8795     case ',':
8796         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMMA)
8797             TOKEN(0);
8798         s++;
8799         OPERATOR(',');
8800     case ':':
8801         if (s[1] == ':')
8802             return yyl_just_a_word(aTHX_ s, 0, 0, no_code);
8803         return yyl_colon(aTHX_ s + 1);
8804
8805     case '(':
8806         return yyl_leftparen(aTHX_ s + 1);
8807
8808     case ';':
8809         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8810             TOKEN(0);
8811         CLINE;
8812         s++;
8813         PL_expect = XSTATE;
8814         TOKEN(';');
8815
8816     case ')':
8817         return yyl_rightparen(aTHX_ s);
8818
8819     case ']':
8820         return yyl_rightsquare(aTHX_ s);
8821
8822     case '{':
8823         return yyl_leftcurly(aTHX_ s + 1, 0);
8824
8825     case '}':
8826         if (PL_lex_brackets && PL_lex_brackstack[PL_lex_brackets-1] == XFAKEEOF)
8827             TOKEN(0);
8828         return yyl_rightcurly(aTHX_ s, 0);
8829
8830     case '&':
8831         return yyl_ampersand(aTHX_ s);
8832
8833     case '|':
8834         return yyl_verticalbar(aTHX_ s);
8835
8836     case '=':
8837         if (s[1] == '=' && (s == PL_linestart || s[-1] == '\n')
8838             && memBEGINs(s + 2, (STRLEN) (PL_bufend - (s + 2)), "====="))
8839         {
8840             s = vcs_conflict_marker(s + 7);
8841             goto retry;
8842         }
8843
8844         s++;
8845         {
8846             const char tmp = *s++;
8847             if (tmp == '=') {
8848                 if (!PL_lex_allbrackets
8849                     && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8850                 {
8851                     s -= 2;
8852                     TOKEN(0);
8853                 }
8854                 ChEop(OP_EQ);
8855             }
8856             if (tmp == '>') {
8857                 if (!PL_lex_allbrackets
8858                     && PL_lex_fakeeof >= LEX_FAKEEOF_COMMA)
8859                 {
8860                     s -= 2;
8861                     TOKEN(0);
8862                 }
8863                 OPERATOR(',');
8864             }
8865             if (tmp == '~')
8866                 PMop(OP_MATCH);
8867             if (tmp && isSPACE(*s) && ckWARN(WARN_SYNTAX)
8868                 && memCHRs("+-*/%.^&|<",tmp))
8869                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
8870                             "Reversed %c= operator",(int)tmp);
8871             s--;
8872             if (PL_expect == XSTATE
8873                 && isALPHA(tmp)
8874                 && (s == PL_linestart+1 || s[-2] == '\n') )
8875             {
8876                 if (   (PL_in_eval && !PL_rsfp && !PL_parser->filtered)
8877                     || PL_lex_state != LEX_NORMAL)
8878                 {
8879                     d = PL_bufend;
8880                     while (s < d) {
8881                         if (*s++ == '\n') {
8882                             incline(s, PL_bufend);
8883                             if (memBEGINs(s, (STRLEN) (PL_bufend - s), "=cut"))
8884                             {
8885                                 s = (char *) memchr(s,'\n', d - s);
8886                                 if (s)
8887                                     s++;
8888                                 else
8889                                     s = d;
8890                                 incline(s, PL_bufend);
8891                                 goto retry;
8892                             }
8893                         }
8894                     }
8895                     goto retry;
8896                 }
8897                 s = PL_bufend;
8898                 PL_parser->in_pod = 1;
8899                 goto retry;
8900             }
8901         }
8902         if (PL_expect == XBLOCK) {
8903             const char *t = s;
8904 #ifdef PERL_STRICT_CR
8905             while (SPACE_OR_TAB(*t))
8906 #else
8907             while (SPACE_OR_TAB(*t) || *t == '\r')
8908 #endif
8909                 t++;
8910             if (*t == '\n' || *t == '#') {
8911                 ENTER_with_name("lex_format");
8912                 SAVEI8(PL_parser->form_lex_state);
8913                 SAVEI32(PL_lex_formbrack);
8914                 PL_parser->form_lex_state = PL_lex_state;
8915                 PL_lex_formbrack = PL_lex_brackets + 1;
8916                 PL_parser->sub_error_count = PL_error_count;
8917                 return yyl_leftcurly(aTHX_ s, 1);
8918             }
8919         }
8920         if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN) {
8921             s--;
8922             TOKEN(0);
8923         }
8924         pl_yylval.ival = 0;
8925         OPERATOR(ASSIGNOP);
8926
8927     case '!':
8928         return yyl_bang(aTHX_ s + 1);
8929
8930     case '<':
8931         if (s[1] == '<' && (s == PL_linestart || s[-1] == '\n')
8932             && memBEGINs(s + 2, (STRLEN) (PL_bufend - (s + 2)), "<<<<<"))
8933         {
8934             s = vcs_conflict_marker(s + 7);
8935             goto retry;
8936         }
8937         return yyl_leftpointy(aTHX_ s);
8938
8939     case '>':
8940         if (s[1] == '>' && (s == PL_linestart || s[-1] == '\n')
8941             && memBEGINs(s + 2, (STRLEN) (PL_bufend - (s + 2)), ">>>>>"))
8942         {
8943             s = vcs_conflict_marker(s + 7);
8944             goto retry;
8945         }
8946         return yyl_rightpointy(aTHX_ s + 1);
8947
8948     case '$':
8949         return yyl_dollar(aTHX_ s);
8950
8951     case '@':
8952         return yyl_snail(aTHX_ s);
8953
8954     case '/':                   /* may be division, defined-or, or pattern */
8955         return yyl_slash(aTHX_ s);
8956
8957      case '?':                  /* conditional */
8958         s++;
8959         if (!PL_lex_allbrackets
8960             && PL_lex_fakeeof >= LEX_FAKEEOF_IFELSE)
8961         {
8962             s--;
8963             TOKEN(0);
8964         }
8965         PL_lex_allbrackets++;
8966         OPERATOR('?');
8967
8968     case '.':
8969         if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack
8970 #ifdef PERL_STRICT_CR
8971             && s[1] == '\n'
8972 #else
8973             && (s[1] == '\n' || (s[1] == '\r' && s[2] == '\n'))
8974 #endif
8975             && (s == PL_linestart || s[-1] == '\n') )
8976         {
8977             PL_expect = XSTATE;
8978             /* formbrack==2 means dot seen where arguments expected */
8979             return yyl_rightcurly(aTHX_ s, 2);
8980         }
8981         if (PL_expect == XSTATE && s[1] == '.' && s[2] == '.') {
8982             s += 3;
8983             OPERATOR(YADAYADA);
8984         }
8985         if (PL_expect == XOPERATOR || !isDIGIT(s[1])) {
8986             char tmp = *s++;
8987             if (*s == tmp) {
8988                 if (!PL_lex_allbrackets
8989                     && PL_lex_fakeeof >= LEX_FAKEEOF_RANGE)
8990                 {
8991                     s--;
8992                     TOKEN(0);
8993                 }
8994                 s++;
8995                 if (*s == tmp) {
8996                     s++;
8997                     pl_yylval.ival = OPf_SPECIAL;
8998                 }
8999                 else
9000                     pl_yylval.ival = 0;
9001                 OPERATOR(DOTDOT);
9002             }
9003             if (*s == '=' && !PL_lex_allbrackets
9004                 && PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
9005             {
9006                 s--;
9007                 TOKEN(0);
9008             }
9009             Aop(OP_CONCAT);
9010         }
9011         /* FALLTHROUGH */
9012     case '0': case '1': case '2': case '3': case '4':
9013     case '5': case '6': case '7': case '8': case '9':
9014         s = scan_num(s, &pl_yylval);
9015         DEBUG_T( { printbuf("### Saw number in %s\n", s); } );
9016         if (PL_expect == XOPERATOR)
9017             no_op("Number",s);
9018         TERM(THING);
9019
9020     case '\'':
9021         return yyl_sglquote(aTHX_ s);
9022
9023     case '"':
9024         return yyl_dblquote(aTHX_ s, len);
9025
9026     case '`':
9027         return yyl_backtick(aTHX_ s);
9028
9029     case '\\':
9030         return yyl_backslash(aTHX_ s + 1);
9031
9032     case 'v':
9033         if (isDIGIT(s[1]) && PL_expect != XOPERATOR) {
9034             char *start = s + 2;
9035             while (isDIGIT(*start) || *start == '_')
9036                 start++;
9037             if (*start == '.' && isDIGIT(start[1])) {
9038                 s = scan_num(s, &pl_yylval);
9039                 TERM(THING);
9040             }
9041             else if ((*start == ':' && start[1] == ':')
9042                   || (PL_expect == XSTATE && *start == ':'))
9043                 return yyl_keylookup(aTHX_ s, gv);
9044             else if (PL_expect == XSTATE) {
9045                 d = start;
9046                 while (d < PL_bufend && isSPACE(*d)) d++;
9047                 if (*d == ':')
9048                     return yyl_keylookup(aTHX_ s, gv);
9049             }
9050             /* avoid v123abc() or $h{v1}, allow C<print v10;> */
9051             if (!isALPHA(*start) && (PL_expect == XTERM
9052                         || PL_expect == XREF || PL_expect == XSTATE
9053                         || PL_expect == XTERMORDORDOR)) {
9054                 GV *const gv = gv_fetchpvn_flags(s, start - s,
9055                                                     UTF ? SVf_UTF8 : 0, SVt_PVCV);
9056                 if (!gv) {
9057                     s = scan_num(s, &pl_yylval);
9058                     TERM(THING);
9059                 }
9060             }
9061         }
9062         return yyl_keylookup(aTHX_ s, gv);
9063
9064     case 'x':
9065         if (isDIGIT(s[1]) && PL_expect == XOPERATOR) {
9066             s++;
9067             Mop(OP_REPEAT);
9068         }
9069         return yyl_keylookup(aTHX_ s, gv);
9070
9071     case '_':
9072     case 'a': case 'A':
9073     case 'b': case 'B':
9074     case 'c': case 'C':
9075     case 'd': case 'D':
9076     case 'e': case 'E':
9077     case 'f': case 'F':
9078     case 'g': case 'G':
9079     case 'h': case 'H':
9080     case 'i': case 'I':
9081     case 'j': case 'J':
9082     case 'k': case 'K':
9083     case 'l': case 'L':
9084     case 'm': case 'M':
9085     case 'n': case 'N':
9086     case 'o': case 'O':
9087     case 'p': case 'P':
9088     case 'q': case 'Q':
9089     case 'r': case 'R':
9090     case 's': case 'S':
9091     case 't': case 'T':
9092     case 'u': case 'U':
9093               case 'V':
9094     case 'w': case 'W':
9095               case 'X':
9096     case 'y': case 'Y':
9097     case 'z': case 'Z':
9098         return yyl_keylookup(aTHX_ s, gv);
9099     }
9100 }
9101
9102
9103 /*
9104   yylex
9105
9106   Works out what to call the token just pulled out of the input
9107   stream.  The yacc parser takes care of taking the ops we return and
9108   stitching them into a tree.
9109
9110   Returns:
9111     The type of the next token
9112
9113   Structure:
9114       Check if we have already built the token; if so, use it.
9115       Switch based on the current state:
9116           - if we have a case modifier in a string, deal with that
9117           - handle other cases of interpolation inside a string
9118           - scan the next line if we are inside a format
9119       In the normal state, switch on the next character:
9120           - default:
9121             if alphabetic, go to key lookup
9122             unrecognized character - croak
9123           - 0/4/26: handle end-of-line or EOF
9124           - cases for whitespace
9125           - \n and #: handle comments and line numbers
9126           - various operators, brackets and sigils
9127           - numbers
9128           - quotes
9129           - 'v': vstrings (or go to key lookup)
9130           - 'x' repetition operator (or go to key lookup)
9131           - other ASCII alphanumerics (key lookup begins here):
9132               word before => ?
9133               keyword plugin
9134               scan built-in keyword (but do nothing with it yet)
9135               check for statement label
9136               check for lexical subs
9137                   return yyl_just_a_word if there is one
9138               see whether built-in keyword is overridden
9139               switch on keyword number:
9140                   - default: return yyl_just_a_word:
9141                       not a built-in keyword; handle bareword lookup
9142                       disambiguate between method and sub call
9143                       fall back to bareword
9144                   - cases for built-in keywords
9145 */
9146
9147 #ifdef NETWARE
9148 #define RSFP_FILENO (PL_rsfp)
9149 #else
9150 #define RSFP_FILENO (PerlIO_fileno(PL_rsfp))
9151 #endif
9152
9153
9154 int
9155 Perl_yylex(pTHX)
9156 {
9157     dVAR;
9158     char *s = PL_bufptr;
9159
9160     if (UNLIKELY(PL_parser->recheck_utf8_validity)) {
9161         const U8* first_bad_char_loc;
9162         if (UTF && UNLIKELY(! is_utf8_string_loc((U8 *) PL_bufptr,
9163                                                         PL_bufend - PL_bufptr,
9164                                                         &first_bad_char_loc)))
9165         {
9166             _force_out_malformed_utf8_message(first_bad_char_loc,
9167                                               (U8 *) PL_bufend,
9168                                               0,
9169                                               1 /* 1 means die */ );
9170             NOT_REACHED; /* NOTREACHED */
9171         }
9172         PL_parser->recheck_utf8_validity = FALSE;
9173     }
9174     DEBUG_T( {
9175         SV* tmp = newSVpvs("");
9176         PerlIO_printf(Perl_debug_log, "### %" IVdf ":LEX_%s/X%s %s\n",
9177             (IV)CopLINE(PL_curcop),
9178             lex_state_names[PL_lex_state],
9179             exp_name[PL_expect],
9180             pv_display(tmp, s, strlen(s), 0, 60));
9181         SvREFCNT_dec(tmp);
9182     } );
9183
9184     /* when we've already built the next token, just pull it out of the queue */
9185     if (PL_nexttoke) {
9186         PL_nexttoke--;
9187         pl_yylval = PL_nextval[PL_nexttoke];
9188         {
9189             I32 next_type;
9190             next_type = PL_nexttype[PL_nexttoke];
9191             if (next_type & (7<<24)) {
9192                 if (next_type & (1<<24)) {
9193                     if (PL_lex_brackets > 100)
9194                         Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
9195                     PL_lex_brackstack[PL_lex_brackets++] =
9196                         (char) ((next_type >> 16) & 0xff);
9197                 }
9198                 if (next_type & (2<<24))
9199                     PL_lex_allbrackets++;
9200                 if (next_type & (4<<24))
9201                     PL_lex_allbrackets--;
9202                 next_type &= 0xffff;
9203             }
9204             return REPORT(next_type == 'p' ? pending_ident() : next_type);
9205         }
9206     }
9207
9208     switch (PL_lex_state) {
9209     case LEX_NORMAL:
9210     case LEX_INTERPNORMAL:
9211         break;
9212
9213     /* interpolated case modifiers like \L \U, including \Q and \E.
9214        when we get here, PL_bufptr is at the \
9215     */
9216     case LEX_INTERPCASEMOD:
9217         /* handle \E or end of string */
9218         return yyl_interpcasemod(aTHX_ s);
9219
9220     case LEX_INTERPPUSH:
9221         return REPORT(sublex_push());
9222
9223     case LEX_INTERPSTART:
9224         if (PL_bufptr == PL_bufend)
9225             return REPORT(sublex_done());
9226         DEBUG_T({
9227             if(*PL_bufptr != '(')
9228                 PerlIO_printf(Perl_debug_log, "### Interpolated variable\n");
9229         });
9230         PL_expect = XTERM;
9231         /* for /@a/, we leave the joining for the regex engine to do
9232          * (unless we're within \Q etc) */
9233         PL_lex_dojoin = (*PL_bufptr == '@'
9234                             && (!PL_lex_inpat || PL_lex_casemods));
9235         PL_lex_state = LEX_INTERPNORMAL;
9236         if (PL_lex_dojoin) {
9237             NEXTVAL_NEXTTOKE.ival = 0;
9238             force_next(',');
9239             force_ident("\"", '$');
9240             NEXTVAL_NEXTTOKE.ival = 0;
9241             force_next('$');
9242             NEXTVAL_NEXTTOKE.ival = 0;
9243             force_next((2<<24)|'(');
9244             NEXTVAL_NEXTTOKE.ival = OP_JOIN;    /* emulate join($", ...) */
9245             force_next(FUNC);
9246         }
9247         /* Convert (?{...}) and friends to 'do {...}' */
9248         if (PL_lex_inpat && *PL_bufptr == '(') {
9249             PL_parser->lex_shared->re_eval_start = PL_bufptr;
9250             PL_bufptr += 2;
9251             if (*PL_bufptr != '{')
9252                 PL_bufptr++;
9253             PL_expect = XTERMBLOCK;
9254             force_next(DO);
9255         }
9256
9257         if (PL_lex_starts++) {
9258             s = PL_bufptr;
9259             /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
9260             if (!PL_lex_casemods && PL_lex_inpat)
9261                 TOKEN(',');
9262             else
9263                 AopNOASSIGN(OP_CONCAT);
9264         }
9265         return yylex();
9266
9267     case LEX_INTERPENDMAYBE:
9268         if (intuit_more(PL_bufptr, PL_bufend)) {
9269             PL_lex_state = LEX_INTERPNORMAL;    /* false alarm, more expr */
9270             break;
9271         }
9272         /* FALLTHROUGH */
9273
9274     case LEX_INTERPEND:
9275         if (PL_lex_dojoin) {
9276             const U8 dojoin_was = PL_lex_dojoin;
9277             PL_lex_dojoin = FALSE;
9278             PL_lex_state = LEX_INTERPCONCAT;
9279             PL_lex_allbrackets--;
9280             return REPORT(dojoin_was == 1 ? (int)')' : (int)POSTJOIN);
9281         }
9282         if (PL_lex_inwhat == OP_SUBST && PL_linestr == PL_lex_repl
9283             && SvEVALED(PL_lex_repl))
9284         {
9285             if (PL_bufptr != PL_bufend)
9286                 Perl_croak(aTHX_ "Bad evalled substitution pattern");
9287             PL_lex_repl = NULL;
9288         }
9289         /* Paranoia.  re_eval_start is adjusted when S_scan_heredoc sets
9290            re_eval_str.  If the here-doc body’s length equals the previous
9291            value of re_eval_start, re_eval_start will now be null.  So
9292            check re_eval_str as well. */
9293         if (PL_parser->lex_shared->re_eval_start
9294          || PL_parser->lex_shared->re_eval_str) {
9295             SV *sv;
9296             if (*PL_bufptr != ')')
9297                 Perl_croak(aTHX_ "Sequence (?{...}) not terminated with ')'");
9298             PL_bufptr++;
9299             /* having compiled a (?{..}) expression, return the original
9300              * text too, as a const */
9301             if (PL_parser->lex_shared->re_eval_str) {
9302                 sv = PL_parser->lex_shared->re_eval_str;
9303                 PL_parser->lex_shared->re_eval_str = NULL;
9304                 SvCUR_set(sv,
9305                          PL_bufptr - PL_parser->lex_shared->re_eval_start);
9306                 SvPV_shrink_to_cur(sv);
9307             }
9308             else sv = newSVpvn(PL_parser->lex_shared->re_eval_start,
9309                          PL_bufptr - PL_parser->lex_shared->re_eval_start);
9310             NEXTVAL_NEXTTOKE.opval =
9311                     newSVOP(OP_CONST, 0,
9312                                  sv);
9313             force_next(THING);
9314             PL_parser->lex_shared->re_eval_start = NULL;
9315             PL_expect = XTERM;
9316             return REPORT(',');
9317         }
9318
9319         /* FALLTHROUGH */
9320     case LEX_INTERPCONCAT:
9321 #ifdef DEBUGGING
9322         if (PL_lex_brackets)
9323             Perl_croak(aTHX_ "panic: INTERPCONCAT, lex_brackets=%ld",
9324                        (long) PL_lex_brackets);
9325 #endif
9326         if (PL_bufptr == PL_bufend)
9327             return REPORT(sublex_done());
9328
9329         /* m'foo' still needs to be parsed for possible (?{...}) */
9330         if (SvIVX(PL_linestr) == '\'' && !PL_lex_inpat) {
9331             SV *sv = newSVsv(PL_linestr);
9332             sv = tokeq(sv);
9333             pl_yylval.opval = newSVOP(OP_CONST, 0, sv);
9334             s = PL_bufend;
9335         }
9336         else {
9337             int save_error_count = PL_error_count;
9338
9339             s = scan_const(PL_bufptr);
9340
9341             /* Set flag if this was a pattern and there were errors.  op.c will
9342              * refuse to compile a pattern with this flag set.  Otherwise, we
9343              * could get segfaults, etc. */
9344             if (PL_lex_inpat && PL_error_count > save_error_count) {
9345                 ((PMOP*)PL_lex_inpat)->op_pmflags |= PMf_HAS_ERROR;
9346             }
9347             if (*s == '\\')
9348                 PL_lex_state = LEX_INTERPCASEMOD;
9349             else
9350                 PL_lex_state = LEX_INTERPSTART;
9351         }
9352
9353         if (s != PL_bufptr) {
9354             NEXTVAL_NEXTTOKE = pl_yylval;
9355             PL_expect = XTERM;
9356             force_next(THING);
9357             if (PL_lex_starts++) {
9358                 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
9359                 if (!PL_lex_casemods && PL_lex_inpat)
9360                     TOKEN(',');
9361                 else
9362                     AopNOASSIGN(OP_CONCAT);
9363             }
9364             else {
9365                 PL_bufptr = s;
9366                 return yylex();
9367             }
9368         }
9369
9370         return yylex();
9371     case LEX_FORMLINE:
9372         if (PL_parser->sub_error_count != PL_error_count) {
9373             /* There was an error parsing a formline, which tends to
9374                mess up the parser.
9375                Unlike interpolated sub-parsing, we can't treat any of
9376                these as recoverable, so no need to check sub_no_recover.
9377             */
9378             yyquit();
9379         }
9380         assert(PL_lex_formbrack);
9381         s = scan_formline(PL_bufptr);
9382         if (!PL_lex_formbrack)
9383             return yyl_rightcurly(aTHX_ s, 1);
9384         PL_bufptr = s;
9385         return yylex();
9386     }
9387
9388     /* We really do *not* want PL_linestr ever becoming a COW. */
9389     assert (!SvIsCOW(PL_linestr));
9390     s = PL_bufptr;
9391     PL_oldoldbufptr = PL_oldbufptr;
9392     PL_oldbufptr = s;
9393
9394     if (PL_in_my == KEY_sigvar) {
9395         PL_parser->saw_infix_sigil = 0;
9396         return yyl_sigvar(aTHX_ s);
9397     }
9398
9399     {
9400         /* yyl_try() and its callees might consult PL_parser->saw_infix_sigil.
9401            On its return, we then need to set it to indicate whether the token
9402            we just encountered was an infix operator that (if we hadn't been
9403            expecting an operator) have been a sigil.
9404         */
9405         bool expected_operator = (PL_expect == XOPERATOR);
9406         int ret = yyl_try(aTHX_ s, 0);
9407         switch (pl_yylval.ival) {
9408         case OP_BIT_AND:
9409         case OP_MODULO:
9410         case OP_MULTIPLY:
9411         case OP_NBIT_AND:
9412             if (expected_operator) {
9413                 PL_parser->saw_infix_sigil = 1;
9414                 break;
9415             }
9416             /* FALLTHROUGH */
9417         default:
9418             PL_parser->saw_infix_sigil = 0;
9419         }
9420         return ret;
9421     }
9422 }
9423
9424
9425 /*
9426   S_pending_ident
9427
9428   Looks up an identifier in the pad or in a package
9429
9430   PL_in_my == KEY_sigvar indicates that this is a subroutine signature variable
9431   rather than a plain pad var.
9432
9433   Returns:
9434     PRIVATEREF if this is a lexical name.
9435     BAREWORD   if this belongs to a package.
9436
9437   Structure:
9438       if we're in a my declaration
9439           croak if they tried to say my($foo::bar)
9440           build the ops for a my() declaration
9441       if it's an access to a my() variable
9442           build ops for access to a my() variable
9443       if in a dq string, and they've said @foo and we can't find @foo
9444           warn
9445       build ops for a bareword
9446 */
9447
9448 static int
9449 S_pending_ident(pTHX)
9450 {
9451     PADOFFSET tmp = 0;
9452     const char pit = (char)pl_yylval.ival;
9453     const STRLEN tokenbuf_len = strlen(PL_tokenbuf);
9454     /* All routes through this function want to know if there is a colon.  */
9455     const char *const has_colon = (const char*) memchr (PL_tokenbuf, ':', tokenbuf_len);
9456
9457     DEBUG_T({ PerlIO_printf(Perl_debug_log,
9458           "### Pending identifier '%s'\n", PL_tokenbuf); });
9459     assert(tokenbuf_len >= 2);
9460
9461     /* if we're in a my(), we can't allow dynamics here.
9462        $foo'bar has already been turned into $foo::bar, so
9463        just check for colons.
9464
9465        if it's a legal name, the OP is a PADANY.
9466     */
9467     if (PL_in_my) {
9468         if (PL_in_my == KEY_our) {      /* "our" is merely analogous to "my" */
9469             if (has_colon)
9470                 /* diag_listed_as: No package name allowed for variable %s
9471                                    in "our" */
9472                 yyerror_pv(Perl_form(aTHX_ "No package name allowed for "
9473                                   "%s %s in \"our\"",
9474                                   *PL_tokenbuf=='&' ? "subroutine" : "variable",
9475                                   PL_tokenbuf), UTF ? SVf_UTF8 : 0);
9476             tmp = allocmy(PL_tokenbuf, tokenbuf_len, UTF ? SVf_UTF8 : 0);
9477         }
9478         else {
9479             OP *o;
9480             if (has_colon) {
9481                 /* "my" variable %s can't be in a package */
9482                 /* PL_no_myglob is constant */
9483                 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
9484                 yyerror_pv(Perl_form(aTHX_ PL_no_myglob,
9485                             PL_in_my == KEY_my ? "my" : "state",
9486                             *PL_tokenbuf == '&' ? "subroutine" : "variable",
9487                             PL_tokenbuf),
9488                             UTF ? SVf_UTF8 : 0);
9489                 GCC_DIAG_RESTORE_STMT;
9490             }
9491
9492             if (PL_in_my == KEY_sigvar) {
9493                 /* A signature 'padop' needs in addition, an op_first to
9494                  * point to a child sigdefelem, and an extra field to hold
9495                  * the signature index. We can achieve both by using an
9496                  * UNOP_AUX and (ab)using the op_aux field to hold the
9497                  * index. If we ever need more fields, use a real malloced
9498                  * aux strut instead.
9499                  */
9500                 o = newUNOP_AUX(OP_ARGELEM, 0, NULL,
9501                                     INT2PTR(UNOP_AUX_item *,
9502                                         (PL_parser->sig_elems)));
9503                 o->op_private |= (  PL_tokenbuf[0] == '$' ? OPpARGELEM_SV
9504                                   : PL_tokenbuf[0] == '@' ? OPpARGELEM_AV
9505                                   :                         OPpARGELEM_HV);
9506             }
9507             else
9508                 o = newOP(OP_PADANY, 0);
9509             o->op_targ = allocmy(PL_tokenbuf, tokenbuf_len,
9510                                                         UTF ? SVf_UTF8 : 0);
9511             if (PL_in_my == KEY_sigvar)
9512                 PL_in_my = 0;
9513
9514             pl_yylval.opval = o;
9515             return PRIVATEREF;
9516         }
9517     }
9518
9519     /*
9520        build the ops for accesses to a my() variable.
9521     */
9522
9523     if (!has_colon) {
9524         if (!PL_in_my)
9525             tmp = pad_findmy_pvn(PL_tokenbuf, tokenbuf_len,
9526                                  0);
9527         if (tmp != NOT_IN_PAD) {
9528             /* might be an "our" variable" */
9529             if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
9530                 /* build ops for a bareword */
9531                 HV *  const stash = PAD_COMPNAME_OURSTASH(tmp);
9532                 HEK * const stashname = HvNAME_HEK(stash);
9533                 SV *  const sym = newSVhek(stashname);
9534                 sv_catpvs(sym, "::");
9535                 sv_catpvn_flags(sym, PL_tokenbuf+1, tokenbuf_len > 0 ? tokenbuf_len - 1 : 0, (UTF ? SV_CATUTF8 : SV_CATBYTES ));
9536                 pl_yylval.opval = newSVOP(OP_CONST, 0, sym);
9537                 pl_yylval.opval->op_private = OPpCONST_ENTERED;
9538                 if (pit != '&')
9539                   gv_fetchsv(sym,
9540                     GV_ADDMULTI,
9541                     ((PL_tokenbuf[0] == '$') ? SVt_PV
9542                      : (PL_tokenbuf[0] == '@') ? SVt_PVAV
9543                      : SVt_PVHV));
9544                 return BAREWORD;
9545             }
9546
9547             pl_yylval.opval = newOP(OP_PADANY, 0);
9548             pl_yylval.opval->op_targ = tmp;
9549             return PRIVATEREF;
9550         }
9551     }
9552
9553     /*
9554        Whine if they've said @foo or @foo{key} in a doublequoted string,
9555        and @foo (or %foo) isn't a variable we can find in the symbol
9556        table.
9557     */
9558     if (ckWARN(WARN_AMBIGUOUS)
9559         && pit == '@'
9560         && PL_lex_state != LEX_NORMAL
9561         && !PL_lex_brackets)
9562     {
9563         GV *const gv = gv_fetchpvn_flags(PL_tokenbuf + 1, tokenbuf_len > 0 ? tokenbuf_len - 1 : 0,
9564                                          ( UTF ? SVf_UTF8 : 0 ) | GV_ADDMG,
9565                                          SVt_PVAV);
9566         if ((!gv || ((PL_tokenbuf[0] == '@') ? !GvAV(gv) : !GvHV(gv)))
9567            )
9568         {
9569             /* Downgraded from fatal to warning 20000522 mjd */
9570             Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
9571                         "Possible unintended interpolation of %" UTF8f
9572                         " in string",
9573                         UTF8fARG(UTF, tokenbuf_len, PL_tokenbuf));
9574         }
9575     }
9576
9577     /* build ops for a bareword */
9578     pl_yylval.opval = newSVOP(OP_CONST, 0,
9579                                    newSVpvn_flags(PL_tokenbuf + 1,
9580                                                       tokenbuf_len > 0 ? tokenbuf_len - 1 : 0,
9581                                                       UTF ? SVf_UTF8 : 0 ));
9582     pl_yylval.opval->op_private = OPpCONST_ENTERED;
9583     if (pit != '&')
9584         gv_fetchpvn_flags(PL_tokenbuf+1, tokenbuf_len > 0 ? tokenbuf_len - 1 : 0,
9585                      (PL_in_eval ? GV_ADDMULTI : GV_ADD)
9586                      | ( UTF ? SVf_UTF8 : 0 ),
9587                      ((PL_tokenbuf[0] == '$') ? SVt_PV
9588                       : (PL_tokenbuf[0] == '@') ? SVt_PVAV
9589                       : SVt_PVHV));
9590     return BAREWORD;
9591 }
9592
9593 STATIC void
9594 S_checkcomma(pTHX_ const char *s, const char *name, const char *what)
9595 {
9596     PERL_ARGS_ASSERT_CHECKCOMMA;
9597
9598     if (*s == ' ' && s[1] == '(') {     /* XXX gotta be a better way */
9599         if (ckWARN(WARN_SYNTAX)) {
9600             int level = 1;
9601             const char *w;
9602             for (w = s+2; *w && level; w++) {
9603                 if (*w == '(')
9604                     ++level;
9605                 else if (*w == ')')
9606                     --level;
9607             }
9608             while (isSPACE(*w))
9609                 ++w;
9610             /* the list of chars below is for end of statements or
9611              * block / parens, boolean operators (&&, ||, //) and branch
9612              * constructs (or, and, if, until, unless, while, err, for).
9613              * Not a very solid hack... */
9614             if (!*w || !memCHRs(";&/|})]oaiuwef!=", *w))
9615                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9616                             "%s (...) interpreted as function",name);
9617         }
9618     }
9619     while (s < PL_bufend && isSPACE(*s))
9620         s++;
9621     if (*s == '(')
9622         s++;
9623     while (s < PL_bufend && isSPACE(*s))
9624         s++;
9625     if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
9626         const char * const w = s;
9627         s += UTF ? UTF8SKIP(s) : 1;
9628         while (isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF))
9629             s += UTF ? UTF8SKIP(s) : 1;
9630         while (s < PL_bufend && isSPACE(*s))
9631             s++;
9632         if (*s == ',') {
9633             GV* gv;
9634             if (keyword(w, s - w, 0))
9635                 return;
9636
9637             gv = gv_fetchpvn_flags(w, s - w, ( UTF ? SVf_UTF8 : 0 ), SVt_PVCV);
9638             if (gv && GvCVu(gv))
9639                 return;
9640             if (s - w <= 254) {
9641                 PADOFFSET off;
9642                 char tmpbuf[256];
9643                 Copy(w, tmpbuf+1, s - w, char);
9644                 *tmpbuf = '&';
9645                 off = pad_findmy_pvn(tmpbuf, s-w+1, 0);
9646                 if (off != NOT_IN_PAD) return;
9647             }
9648             Perl_croak(aTHX_ "No comma allowed after %s", what);
9649         }
9650     }
9651 }
9652
9653 /* S_new_constant(): do any overload::constant lookup.
9654
9655    Either returns sv, or mortalizes/frees sv and returns a new SV*.
9656    Best used as sv=new_constant(..., sv, ...).
9657    If s, pv are NULL, calls subroutine with one argument,
9658    and <type> is used with error messages only.
9659    <type> is assumed to be well formed UTF-8.
9660
9661    If error_msg is not NULL, *error_msg will be set to any error encountered.
9662    Otherwise yyerror() will be used to output it */
9663
9664 STATIC SV *
9665 S_new_constant(pTHX_ const char *s, STRLEN len, const char *key, STRLEN keylen,
9666                SV *sv, SV *pv, const char *type, STRLEN typelen,
9667                const char ** error_msg)
9668 {
9669     dSP;
9670     HV * table = GvHV(PL_hintgv);                /* ^H */
9671     SV *res;
9672     SV *errsv = NULL;
9673     SV **cvp;
9674     SV *cv, *typesv;
9675     const char *why1 = "", *why2 = "", *why3 = "";
9676     const char * optional_colon = ":";  /* Only some messages have a colon */
9677     char *msg;
9678
9679     PERL_ARGS_ASSERT_NEW_CONSTANT;
9680     /* We assume that this is true: */
9681     assert(type || s);
9682
9683     sv_2mortal(sv);                     /* Parent created it permanently */
9684
9685     if (   ! table
9686         || ! (PL_hints & HINT_LOCALIZE_HH))
9687     {
9688         why1 = "unknown";
9689         optional_colon = "";
9690         goto report;
9691     }
9692
9693     cvp = hv_fetch(table, key, keylen, FALSE);
9694     if (!cvp || !SvOK(*cvp)) {
9695         why1 = "$^H{";
9696         why2 = key;
9697         why3 = "} is not defined";
9698         goto report;
9699     }
9700
9701     cv = *cvp;
9702     if (!pv && s)
9703         pv = newSVpvn_flags(s, len, SVs_TEMP);
9704     if (type && pv)
9705         typesv = newSVpvn_flags(type, typelen, SVs_TEMP);
9706     else
9707         typesv = &PL_sv_undef;
9708
9709     PUSHSTACKi(PERLSI_OVERLOAD);
9710     ENTER ;
9711     SAVETMPS;
9712
9713     PUSHMARK(SP) ;
9714     EXTEND(sp, 3);
9715     if (pv)
9716         PUSHs(pv);
9717     PUSHs(sv);
9718     if (pv)
9719         PUSHs(typesv);
9720     PUTBACK;
9721     call_sv(cv, G_SCALAR | ( PL_in_eval ? 0 : G_EVAL));
9722
9723     SPAGAIN ;
9724
9725     /* Check the eval first */
9726     if (!PL_in_eval && ((errsv = ERRSV), SvTRUE_NN(errsv))) {
9727         STRLEN errlen;
9728         const char * errstr;
9729         sv_catpvs(errsv, "Propagated");
9730         errstr = SvPV_const(errsv, errlen);
9731         yyerror_pvn(errstr, errlen, 0); /* Duplicates the message inside eval */
9732         (void)POPs;
9733         res = SvREFCNT_inc_simple_NN(sv);
9734     }
9735     else {
9736         res = POPs;
9737         SvREFCNT_inc_simple_void_NN(res);
9738     }
9739
9740     PUTBACK ;
9741     FREETMPS ;
9742     LEAVE ;
9743     POPSTACK;
9744
9745     if (SvOK(res)) {
9746         return res;
9747     }
9748
9749     sv = res;
9750     (void)sv_2mortal(sv);
9751
9752     why1 = "Call to &{$^H{";
9753     why2 = key;
9754     why3 = "}} did not return a defined value";
9755
9756   report:
9757
9758     msg = Perl_form(aTHX_ "Constant(%.*s)%s %s%s%s",
9759                         (int)(type ? typelen : len),
9760                         (type ? type: s),
9761                         optional_colon,
9762                         why1, why2, why3);
9763     if (error_msg) {
9764         *error_msg = msg;
9765     }
9766     else {
9767         yyerror_pv(msg, UTF ? SVf_UTF8 : 0);
9768     }
9769     return SvREFCNT_inc_simple_NN(sv);
9770 }
9771
9772 PERL_STATIC_INLINE void
9773 S_parse_ident(pTHX_ char **s, char **d, char * const e, int allow_package,
9774                     bool is_utf8, bool check_dollar, bool tick_warn)
9775 {
9776     int saw_tick = 0;
9777     const char *olds = *s;
9778     PERL_ARGS_ASSERT_PARSE_IDENT;
9779
9780     while (*s < PL_bufend) {
9781         if (*d >= e)
9782             Perl_croak(aTHX_ "%s", ident_too_long);
9783         if (is_utf8 && isIDFIRST_utf8_safe(*s, PL_bufend)) {
9784              /* The UTF-8 case must come first, otherwise things
9785              * like c\N{COMBINING TILDE} would start failing, as the
9786              * isWORDCHAR_A case below would gobble the 'c' up.
9787              */
9788
9789             char *t = *s + UTF8SKIP(*s);
9790             while (isIDCONT_utf8_safe((const U8*) t, (const U8*) PL_bufend)) {
9791                 t += UTF8SKIP(t);
9792             }
9793             if (*d + (t - *s) > e)
9794                 Perl_croak(aTHX_ "%s", ident_too_long);
9795             Copy(*s, *d, t - *s, char);
9796             *d += t - *s;
9797             *s = t;
9798         }
9799         else if ( isWORDCHAR_A(**s) ) {
9800             do {
9801                 *(*d)++ = *(*s)++;
9802             } while (isWORDCHAR_A(**s) && *d < e);
9803         }
9804         else if (   allow_package
9805                  && **s == '\''
9806                  && isIDFIRST_lazy_if_safe((*s)+1, PL_bufend, is_utf8))
9807         {
9808             *(*d)++ = ':';
9809             *(*d)++ = ':';
9810             (*s)++;
9811             saw_tick++;
9812         }
9813         else if (allow_package && **s == ':' && (*s)[1] == ':'
9814            /* Disallow things like Foo::$bar. For the curious, this is
9815             * the code path that triggers the "Bad name after" warning
9816             * when looking for barewords.
9817             */
9818            && !(check_dollar && (*s)[2] == '$')) {
9819             *(*d)++ = *(*s)++;
9820             *(*d)++ = *(*s)++;
9821         }
9822         else
9823             break;
9824     }
9825     if (UNLIKELY(tick_warn && saw_tick && PL_lex_state == LEX_INTERPNORMAL
9826               && !PL_lex_brackets && ckWARN(WARN_SYNTAX))) {
9827         char *this_d;
9828         char *d2;
9829         Newx(this_d, *s - olds + saw_tick + 2, char); /* +2 for $# */
9830         d2 = this_d;
9831         SAVEFREEPV(this_d);
9832         Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9833                          "Old package separator used in string");
9834         if (olds[-1] == '#')
9835             *d2++ = olds[-2];
9836         *d2++ = olds[-1];
9837         while (olds < *s) {
9838             if (*olds == '\'') {
9839                 *d2++ = '\\';
9840                 *d2++ = *olds++;
9841             }
9842             else
9843                 *d2++ = *olds++;
9844         }
9845         Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9846                          "\t(Did you mean \"%" UTF8f "\" instead?)\n",
9847                           UTF8fARG(is_utf8, d2-this_d, this_d));
9848     }
9849     return;
9850 }
9851
9852 /* Returns a NUL terminated string, with the length of the string written to
9853    *slp
9854    */
9855 char *
9856 Perl_scan_word(pTHX_ char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp)
9857 {
9858     char *d = dest;
9859     char * const e = d + destlen - 3;  /* two-character token, ending NUL */
9860     bool is_utf8 = cBOOL(UTF);
9861
9862     PERL_ARGS_ASSERT_SCAN_WORD;
9863
9864     parse_ident(&s, &d, e, allow_package, is_utf8, TRUE, FALSE);
9865     *d = '\0';
9866     *slp = d - dest;
9867     return s;
9868 }
9869
9870 /* Is the byte 'd' a legal single character identifier name?  'u' is true
9871  * iff Unicode semantics are to be used.  The legal ones are any of:
9872  *  a) all ASCII characters except:
9873  *          1) control and space-type ones, like NUL, SOH, \t, and SPACE;
9874  *          2) '{'
9875  *     The final case currently doesn't get this far in the program, so we
9876  *     don't test for it.  If that were to change, it would be ok to allow it.
9877  *  b) When not under Unicode rules, any upper Latin1 character
9878  *  c) Otherwise, when unicode rules are used, all XIDS characters.
9879  *
9880  *      Because all ASCII characters have the same representation whether
9881  *      encoded in UTF-8 or not, we can use the foo_A macros below and '\0' and
9882  *      '{' without knowing if is UTF-8 or not. */
9883 #define VALID_LEN_ONE_IDENT(s, e, is_utf8)                                  \
9884     (isGRAPH_A(*(s)) || ((is_utf8)                                          \
9885                          ? isIDFIRST_utf8_safe(s, e)                        \
9886                          : (isGRAPH_L1(*s)                                  \
9887                             && LIKELY((U8) *(s) != LATIN1_TO_NATIVE(0xAD)))))
9888
9889 STATIC char *
9890 S_scan_ident(pTHX_ char *s, char *dest, STRLEN destlen, I32 ck_uni)
9891 {
9892     I32 herelines = PL_parser->herelines;
9893     SSize_t bracket = -1;
9894     char funny = *s++;
9895     char *d = dest;
9896     char * const e = d + destlen - 3;    /* two-character token, ending NUL */
9897     bool is_utf8 = cBOOL(UTF);
9898     I32 orig_copline = 0, tmp_copline = 0;
9899
9900     PERL_ARGS_ASSERT_SCAN_IDENT;
9901
9902     if (isSPACE(*s) || !*s)
9903         s = skipspace(s);
9904     if (isDIGIT(*s)) { /* handle $0 and $1 $2 and $10 and etc */
9905         bool is_zero= *s == '0' ? TRUE : FALSE;
9906         char *digit_start= d;
9907         *d++ = *s++;
9908         while (s < PL_bufend && isDIGIT(*s)) {
9909             if (d >= e)
9910                 Perl_croak(aTHX_ "%s", ident_too_long);
9911             *d++ = *s++;
9912         } 
9913         if (is_zero && d - digit_start > 1)
9914             Perl_croak(aTHX_ ident_var_zero_multi_digit);
9915     }
9916     else {  /* See if it is a "normal" identifier */
9917         parse_ident(&s, &d, e, 1, is_utf8, FALSE, TRUE);
9918     }
9919     *d = '\0';
9920     d = dest;
9921     if (*d) {
9922         /* Either a digit variable, or parse_ident() found an identifier
9923            (anything valid as a bareword), so job done and return.  */
9924         if (PL_lex_state != LEX_NORMAL)
9925             PL_lex_state = LEX_INTERPENDMAYBE;
9926         return s;
9927     }
9928
9929     /* Here, it is not a run-of-the-mill identifier name */
9930
9931     if (*s == '$' && s[1]
9932         && (   isIDFIRST_lazy_if_safe(s+1, PL_bufend, is_utf8)
9933             || isDIGIT_A((U8)s[1])
9934             || s[1] == '$'
9935             || s[1] == '{'
9936             || memBEGINs(s+1, (STRLEN) (PL_bufend - (s+1)), "::")) )
9937     {
9938         /* Dereferencing a value in a scalar variable.
9939            The alternatives are different syntaxes for a scalar variable.
9940            Using ' as a leading package separator isn't allowed. :: is.   */
9941         return s;
9942     }
9943     /* Handle the opening { of @{...}, &{...}, *{...}, %{...}, ${...}  */
9944     if (*s == '{') {
9945         bracket = s - SvPVX(PL_linestr);
9946         s++;
9947         orig_copline = CopLINE(PL_curcop);
9948         if (s < PL_bufend && isSPACE(*s)) {
9949             s = skipspace(s);
9950         }
9951     }
9952     if ((s <= PL_bufend - ((is_utf8)
9953                           ? UTF8SKIP(s)
9954                           : 1))
9955         && VALID_LEN_ONE_IDENT(s, PL_bufend, is_utf8))
9956     {
9957         if (is_utf8) {
9958             const STRLEN skip = UTF8SKIP(s);
9959             STRLEN i;
9960             d[skip] = '\0';
9961             for ( i = 0; i < skip; i++ )
9962                 d[i] = *s++;
9963         }
9964         else {
9965             *d = *s++;
9966             /* special case to handle ${10}, ${11} the same way we handle ${1} etc */
9967             if (isDIGIT(*d)) {
9968                 bool is_zero= *d == '0' ? TRUE : FALSE;
9969                 char *digit_start= d;
9970                 while (s < PL_bufend && isDIGIT(*s)) {
9971                     d++;
9972                     if (d >= e)
9973                         Perl_croak(aTHX_ "%s", ident_too_long);
9974                     *d= *s++;
9975                 }
9976                 if (is_zero && d - digit_start > 1)
9977                     Perl_croak(aTHX_ ident_var_zero_multi_digit);
9978             }
9979             d[1] = '\0';
9980         }
9981     }
9982     /* Convert $^F, ${^F} and the ^F of ${^FOO} to control characters */
9983     if (*d == '^' && *s && isCONTROLVAR(*s)) {
9984         *d = toCTRL(*s);
9985         s++;
9986     }
9987     /* Warn about ambiguous code after unary operators if {...} notation isn't
9988        used.  There's no difference in ambiguity; it's merely a heuristic
9989        about when not to warn.  */
9990     else if (ck_uni && bracket == -1)
9991         check_uni();
9992     if (bracket != -1) {
9993         bool skip;
9994         char *s2;
9995         /* If we were processing {...} notation then...  */
9996         if (isIDFIRST_lazy_if_safe(d, e, is_utf8)
9997             || (!isPRINT(*d) /* isCNTRL(d), plus all non-ASCII */
9998                  && isWORDCHAR(*s))
9999         ) {
10000             /* note we have to check for a normal identifier first,
10001              * as it handles utf8 symbols, and only after that has
10002              * been ruled out can we look at the caret words */
10003             if (isIDFIRST_lazy_if_safe(d, e, is_utf8) ) {
10004                 /* if it starts as a valid identifier, assume that it is one.
10005                    (the later check for } being at the expected point will trap
10006                    cases where this doesn't pan out.)  */
10007                 d += is_utf8 ? UTF8SKIP(d) : 1;
10008                 parse_ident(&s, &d, e, 1, is_utf8, TRUE, TRUE);
10009                 *d = '\0';
10010             }
10011             else { /* caret word: ${^Foo} ${^CAPTURE[0]} */
10012                 d++;
10013                 while (isWORDCHAR(*s) && d < e) {
10014                     *d++ = *s++;
10015                 }
10016                 if (d >= e)
10017                     Perl_croak(aTHX_ "%s", ident_too_long);
10018                 *d = '\0';
10019             }
10020             tmp_copline = CopLINE(PL_curcop);
10021             if (s < PL_bufend && isSPACE(*s)) {
10022                 s = skipspace(s);
10023             }
10024             if ((*s == '[' || (*s == '{' && strNE(dest, "sub")))) {
10025                 /* ${foo[0]} and ${foo{bar}} and ${^CAPTURE[0]} notation.  */
10026                 if (ckWARN(WARN_AMBIGUOUS) && keyword(dest, d - dest, 0)) {
10027                     const char * const brack =
10028                         (const char *)
10029                         ((*s == '[') ? "[...]" : "{...}");
10030                     orig_copline = CopLINE(PL_curcop);
10031                     CopLINE_set(PL_curcop, tmp_copline);
10032    /* diag_listed_as: Ambiguous use of %c{%s[...]} resolved to %c%s[...] */
10033                     Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
10034                         "Ambiguous use of %c{%s%s} resolved to %c%s%s",
10035                         funny, dest, brack, funny, dest, brack);
10036                     CopLINE_set(PL_curcop, orig_copline);
10037                 }
10038                 bracket++;
10039                 PL_lex_brackstack[PL_lex_brackets++] = (char)(XOPERATOR | XFAKEBRACK);
10040                 PL_lex_allbrackets++;
10041                 return s;
10042             }
10043         }
10044
10045         if ( !tmp_copline )
10046             tmp_copline = CopLINE(PL_curcop);
10047         if ((skip = s < PL_bufend && isSPACE(*s))) {
10048             /* Avoid incrementing line numbers or resetting PL_linestart,
10049                in case we have to back up.  */
10050             STRLEN s_off = s - SvPVX(PL_linestr);
10051             s2 = peekspace(s);
10052             s = SvPVX(PL_linestr) + s_off;
10053         }
10054         else
10055             s2 = s;
10056
10057         /* Expect to find a closing } after consuming any trailing whitespace.
10058          */
10059         if (*s2 == '}') {
10060             /* Now increment line numbers if applicable.  */
10061             if (skip)
10062                 s = skipspace(s);
10063             s++;
10064             if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) {
10065                 PL_lex_state = LEX_INTERPEND;
10066                 PL_expect = XREF;
10067             }
10068             if (PL_lex_state == LEX_NORMAL || PL_lex_brackets) {
10069                 if (ckWARN(WARN_AMBIGUOUS)
10070                     && (keyword(dest, d - dest, 0)
10071                         || get_cvn_flags(dest, d - dest, is_utf8
10072                            ? SVf_UTF8
10073                            : 0)))
10074                 {
10075                     SV *tmp = newSVpvn_flags( dest, d - dest,
10076                                         SVs_TEMP | (is_utf8 ? SVf_UTF8 : 0) );
10077                     if (funny == '#')
10078                         funny = '@';
10079                     orig_copline = CopLINE(PL_curcop);
10080                     CopLINE_set(PL_curcop, tmp_copline);
10081                     Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
10082                         "Ambiguous use of %c{%" SVf "} resolved to %c%" SVf,
10083                         funny, SVfARG(tmp), funny, SVfARG(tmp));
10084                     CopLINE_set(PL_curcop, orig_copline);
10085                 }
10086             }
10087         }
10088         else {
10089             /* Didn't find the closing } at the point we expected, so restore
10090                state such that the next thing to process is the opening { and */
10091             s = SvPVX(PL_linestr) + bracket; /* let the parser handle it */
10092             CopLINE_set(PL_curcop, orig_copline);
10093             PL_parser->herelines = herelines;
10094             *dest = '\0';
10095             PL_parser->sub_no_recover = TRUE;
10096         }
10097     }
10098     else if (   PL_lex_state == LEX_INTERPNORMAL
10099              && !PL_lex_brackets
10100              && !intuit_more(s, PL_bufend))
10101         PL_lex_state = LEX_INTERPEND;
10102     return s;
10103 }
10104
10105 static bool
10106 S_pmflag(pTHX_ const char* const valid_flags, U32 * pmfl, char** s, char* charset, unsigned int * x_mod_count) {
10107
10108     /* Adds, subtracts to/from 'pmfl' based on the next regex modifier flag
10109      * found in the parse starting at 's', based on the subset that are valid
10110      * in this context input to this routine in 'valid_flags'. Advances s.
10111      * Returns TRUE if the input should be treated as a valid flag, so the next
10112      * char may be as well; otherwise FALSE. 'charset' should point to a NUL
10113      * upon first call on the current regex.  This routine will set it to any
10114      * charset modifier found.  The caller shouldn't change it.  This way,
10115      * another charset modifier encountered in the parse can be detected as an
10116      * error, as we have decided to allow only one */
10117
10118     const char c = **s;
10119     STRLEN charlen = UTF ? UTF8SKIP(*s) : 1;
10120
10121     if ( charlen != 1 || ! strchr(valid_flags, c) ) {
10122         if (isWORDCHAR_lazy_if_safe( *s, PL_bufend, UTF)) {
10123             yyerror_pv(Perl_form(aTHX_ "Unknown regexp modifier \"/%.*s\"", (int)charlen, *s),
10124                        UTF ? SVf_UTF8 : 0);
10125             (*s) += charlen;
10126             /* Pretend that it worked, so will continue processing before
10127              * dieing */
10128             return TRUE;
10129         }
10130         return FALSE;
10131     }
10132
10133     switch (c) {
10134
10135         CASE_STD_PMMOD_FLAGS_PARSE_SET(pmfl, *x_mod_count);
10136         case GLOBAL_PAT_MOD:      *pmfl |= PMf_GLOBAL; break;
10137         case CONTINUE_PAT_MOD:    *pmfl |= PMf_CONTINUE; break;
10138         case ONCE_PAT_MOD:        *pmfl |= PMf_KEEP; break;
10139         case KEEPCOPY_PAT_MOD:    *pmfl |= RXf_PMf_KEEPCOPY; break;
10140         case NONDESTRUCT_PAT_MOD: *pmfl |= PMf_NONDESTRUCT; break;
10141         case LOCALE_PAT_MOD:
10142             if (*charset) {
10143                 goto multiple_charsets;
10144             }
10145             set_regex_charset(pmfl, REGEX_LOCALE_CHARSET);
10146             *charset = c;
10147             break;
10148         case UNICODE_PAT_MOD:
10149             if (*charset) {
10150                 goto multiple_charsets;
10151             }
10152             set_regex_charset(pmfl, REGEX_UNICODE_CHARSET);
10153             *charset = c;
10154             break;
10155         case ASCII_RESTRICT_PAT_MOD:
10156             if (! *charset) {
10157                 set_regex_charset(pmfl, REGEX_ASCII_RESTRICTED_CHARSET);
10158             }
10159             else {
10160
10161                 /* Error if previous modifier wasn't an 'a', but if it was, see
10162                  * if, and accept, a second occurrence (only) */
10163                 if (*charset != 'a'
10164                     || get_regex_charset(*pmfl)
10165                         != REGEX_ASCII_RESTRICTED_CHARSET)
10166                 {
10167                         goto multiple_charsets;
10168                 }
10169                 set_regex_charset(pmfl, REGEX_ASCII_MORE_RESTRICTED_CHARSET);
10170             }
10171             *charset = c;
10172             break;
10173         case DEPENDS_PAT_MOD:
10174             if (*charset) {
10175                 goto multiple_charsets;
10176             }
10177             set_regex_charset(pmfl, REGEX_DEPENDS_CHARSET);
10178             *charset = c;
10179             break;
10180     }
10181
10182     (*s)++;
10183     return TRUE;
10184
10185     multiple_charsets:
10186         if (*charset != c) {
10187             yyerror(Perl_form(aTHX_ "Regexp modifiers \"/%c\" and \"/%c\" are mutually exclusive", *charset, c));
10188         }
10189         else if (c == 'a') {
10190   /* diag_listed_as: Regexp modifier "/%c" may appear a maximum of twice */
10191             yyerror("Regexp modifier \"/a\" may appear a maximum of twice");
10192         }
10193         else {
10194             yyerror(Perl_form(aTHX_ "Regexp modifier \"/%c\" may not appear twice", c));
10195         }
10196
10197         /* Pretend that it worked, so will continue processing before dieing */
10198         (*s)++;
10199         return TRUE;
10200 }
10201
10202 STATIC char *
10203 S_scan_pat(pTHX_ char *start, I32 type)
10204 {
10205     PMOP *pm;
10206     char *s;
10207     const char * const valid_flags =
10208         (const char *)((type == OP_QR) ? QR_PAT_MODS : M_PAT_MODS);
10209     char charset = '\0';    /* character set modifier */
10210     unsigned int x_mod_count = 0;
10211
10212     PERL_ARGS_ASSERT_SCAN_PAT;
10213
10214     s = scan_str(start,TRUE,FALSE, (PL_in_eval & EVAL_RE_REPARSING), NULL);
10215     if (!s)
10216         Perl_croak(aTHX_ "Search pattern not terminated");
10217
10218     pm = (PMOP*)newPMOP(type, 0);
10219     if (PL_multi_open == '?') {
10220         /* This is the only point in the code that sets PMf_ONCE:  */
10221         pm->op_pmflags |= PMf_ONCE;
10222
10223         /* Hence it's safe to do this bit of PMOP book-keeping here, which
10224            allows us to restrict the list needed by reset to just the ??
10225            matches.  */
10226         assert(type != OP_TRANS);
10227         if (PL_curstash) {
10228             MAGIC *mg = mg_find((const SV *)PL_curstash, PERL_MAGIC_symtab);
10229             U32 elements;
10230             if (!mg) {
10231                 mg = sv_magicext(MUTABLE_SV(PL_curstash), 0, PERL_MAGIC_symtab, 0, 0,
10232                                  0);
10233             }
10234             elements = mg->mg_len / sizeof(PMOP**);
10235             Renewc(mg->mg_ptr, elements + 1, PMOP*, char);
10236             ((PMOP**)mg->mg_ptr) [elements++] = pm;
10237             mg->mg_len = elements * sizeof(PMOP**);
10238             PmopSTASH_set(pm,PL_curstash);
10239         }
10240     }
10241
10242     /* if qr/...(?{..}).../, then need to parse the pattern within a new
10243      * anon CV. False positives like qr/[(?{]/ are harmless */
10244
10245     if (type == OP_QR) {
10246         STRLEN len;
10247         char *e, *p = SvPV(PL_lex_stuff, len);
10248         e = p + len;
10249         for (; p < e; p++) {
10250             if (p[0] == '(' && p[1] == '?'
10251                 && (p[2] == '{' || (p[2] == '?' && p[3] == '{')))
10252             {
10253                 pm->op_pmflags |= PMf_HAS_CV;
10254                 break;
10255             }
10256         }
10257         pm->op_pmflags |= PMf_IS_QR;
10258     }
10259
10260     while (*s && S_pmflag(aTHX_ valid_flags, &(pm->op_pmflags),
10261                                 &s, &charset, &x_mod_count))
10262     {};
10263     /* issue a warning if /c is specified,but /g is not */
10264     if ((pm->op_pmflags & PMf_CONTINUE) && !(pm->op_pmflags & PMf_GLOBAL))
10265     {
10266         Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
10267                        "Use of /c modifier is meaningless without /g" );
10268     }
10269
10270     PL_lex_op = (OP*)pm;
10271     pl_yylval.ival = OP_MATCH;
10272     return s;
10273 }
10274
10275 STATIC char *
10276 S_scan_subst(pTHX_ char *start)
10277 {
10278     char *s;
10279     PMOP *pm;
10280     I32 first_start;
10281     line_t first_line;
10282     line_t linediff = 0;
10283     I32 es = 0;
10284     char charset = '\0';    /* character set modifier */
10285     unsigned int x_mod_count = 0;
10286     char *t;
10287
10288     PERL_ARGS_ASSERT_SCAN_SUBST;
10289
10290     pl_yylval.ival = OP_NULL;
10291
10292     s = scan_str(start, TRUE, FALSE, FALSE, &t);
10293
10294     if (!s)
10295         Perl_croak(aTHX_ "Substitution pattern not terminated");
10296
10297     s = t;
10298
10299     first_start = PL_multi_start;
10300     first_line = CopLINE(PL_curcop);
10301     s = scan_str(s,FALSE,FALSE,FALSE,NULL);
10302     if (!s) {
10303         SvREFCNT_dec_NN(PL_lex_stuff);
10304         PL_lex_stuff = NULL;
10305         Perl_croak(aTHX_ "Substitution replacement not terminated");
10306     }
10307     PL_multi_start = first_start;       /* so whole substitution is taken together */
10308
10309     pm = (PMOP*)newPMOP(OP_SUBST, 0);
10310
10311
10312     while (*s) {
10313         if (*s == EXEC_PAT_MOD) {
10314             s++;
10315             es++;
10316         }
10317         else if (! S_pmflag(aTHX_ S_PAT_MODS, &(pm->op_pmflags),
10318                                   &s, &charset, &x_mod_count))
10319         {
10320             break;
10321         }
10322     }
10323
10324     if ((pm->op_pmflags & PMf_CONTINUE)) {
10325         Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), "Use of /c modifier is meaningless in s///" );
10326     }
10327
10328     if (es) {
10329         SV * const repl = newSVpvs("");
10330
10331         PL_multi_end = 0;
10332         pm->op_pmflags |= PMf_EVAL;
10333         for (; es > 1; es--) {
10334             sv_catpvs(repl, "eval ");
10335         }
10336         sv_catpvs(repl, "do {");
10337         sv_catsv(repl, PL_parser->lex_sub_repl);
10338         sv_catpvs(repl, "}");
10339         SvREFCNT_dec(PL_parser->lex_sub_repl);
10340         PL_parser->lex_sub_repl = repl;
10341     }
10342
10343
10344     linediff = CopLINE(PL_curcop) - first_line;
10345     if (linediff)
10346         CopLINE_set(PL_curcop, first_line);
10347
10348     if (linediff || es) {
10349         /* the IVX field indicates that the replacement string is a s///e;
10350          * the NVX field indicates how many src code lines the replacement
10351          * spreads over */
10352         sv_upgrade(PL_parser->lex_sub_repl, SVt_PVNV);
10353         ((XPVNV*)SvANY(PL_parser->lex_sub_repl))->xnv_u.xnv_lines = linediff;
10354         ((XPVIV*)SvANY(PL_parser->lex_sub_repl))->xiv_u.xivu_eval_seen =
10355                                                                     cBOOL(es);
10356     }
10357
10358     PL_lex_op = (OP*)pm;
10359     pl_yylval.ival = OP_SUBST;
10360     return s;
10361 }
10362
10363 STATIC char *
10364 S_scan_trans(pTHX_ char *start)
10365 {
10366     char* s;
10367     OP *o;
10368     U8 squash;
10369     U8 del;
10370     U8 complement;
10371     bool nondestruct = 0;
10372     char *t;
10373
10374     PERL_ARGS_ASSERT_SCAN_TRANS;
10375
10376     pl_yylval.ival = OP_NULL;
10377
10378     s = scan_str(start,FALSE,FALSE,FALSE,&t);
10379     if (!s)
10380         Perl_croak(aTHX_ "Transliteration pattern not terminated");
10381
10382     s = t;
10383
10384     s = scan_str(s,FALSE,FALSE,FALSE,NULL);
10385     if (!s) {
10386         SvREFCNT_dec_NN(PL_lex_stuff);
10387         PL_lex_stuff = NULL;
10388         Perl_croak(aTHX_ "Transliteration replacement not terminated");
10389     }
10390
10391     complement = del = squash = 0;
10392     while (1) {
10393         switch (*s) {
10394         case 'c':
10395             complement = OPpTRANS_COMPLEMENT;
10396             break;
10397         case 'd':
10398             del = OPpTRANS_DELETE;
10399             break;
10400         case 's':
10401             squash = OPpTRANS_SQUASH;
10402             break;
10403         case 'r':
10404             nondestruct = 1;
10405             break;
10406         default:
10407             goto no_more;
10408         }
10409         s++;
10410     }
10411   no_more:
10412
10413     o = newPVOP(nondestruct ? OP_TRANSR : OP_TRANS, 0, (char*)NULL);
10414     o->op_private &= ~OPpTRANS_ALL;
10415     o->op_private |= del|squash|complement;
10416
10417     PL_lex_op = o;
10418     pl_yylval.ival = nondestruct ? OP_TRANSR : OP_TRANS;
10419
10420
10421     return s;
10422 }
10423
10424 /* scan_heredoc
10425    Takes a pointer to the first < in <<FOO.
10426    Returns a pointer to the byte following <<FOO.
10427
10428    This function scans a heredoc, which involves different methods
10429    depending on whether we are in a string eval, quoted construct, etc.
10430    This is because PL_linestr could containing a single line of input, or
10431    a whole string being evalled, or the contents of the current quote-
10432    like operator.
10433
10434    The two basic methods are:
10435     - Steal lines from the input stream
10436     - Scan the heredoc in PL_linestr and remove it therefrom
10437
10438    In a file scope or filtered eval, the first method is used; in a
10439    string eval, the second.
10440
10441    In a quote-like operator, we have to choose between the two,
10442    depending on where we can find a newline.  We peek into outer lex-
10443    ing scopes until we find one with a newline in it.  If we reach the
10444    outermost lexing scope and it is a file, we use the stream method.
10445    Otherwise it is treated as an eval.
10446 */
10447
10448 STATIC char *
10449 S_scan_heredoc(pTHX_ char *s)
10450 {
10451     I32 op_type = OP_SCALAR;
10452     I32 len;
10453     SV *tmpstr;
10454     char term;
10455     char *d;
10456     char *e;
10457     char *peek;
10458     char *indent = 0;
10459     I32 indent_len = 0;
10460     bool indented = FALSE;
10461     const bool infile = PL_rsfp || PL_parser->filtered;
10462     const line_t origline = CopLINE(PL_curcop);
10463     LEXSHARED *shared = PL_parser->lex_shared;
10464
10465     PERL_ARGS_ASSERT_SCAN_HEREDOC;
10466
10467     s += 2;
10468     d = PL_tokenbuf + 1;
10469     e = PL_tokenbuf + sizeof PL_tokenbuf - 1;
10470     *PL_tokenbuf = '\n';
10471     peek = s;
10472
10473     if (*peek == '~') {
10474         indented = TRUE;
10475         peek++; s++;
10476     }
10477
10478     while (SPACE_OR_TAB(*peek))
10479         peek++;
10480
10481     if (*peek == '`' || *peek == '\'' || *peek =='"') {
10482         s = peek;
10483         term = *s++;
10484         s = delimcpy(d, e, s, PL_bufend, term, &len);
10485         if (s == PL_bufend)
10486             Perl_croak(aTHX_ "Unterminated delimiter for here document");
10487         d += len;
10488         s++;
10489     }
10490     else {
10491         if (*s == '\\')
10492             /* <<\FOO is equivalent to <<'FOO' */
10493             s++, term = '\'';
10494         else
10495             term = '"';
10496
10497         if (! isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF))
10498             Perl_croak(aTHX_ "Use of bare << to mean <<\"\" is forbidden");
10499
10500         peek = s;
10501
10502         while (isWORDCHAR_lazy_if_safe(peek, PL_bufend, UTF)) {
10503             peek += UTF ? UTF8SKIP(peek) : 1;
10504         }
10505
10506         len = (peek - s >= e - d) ? (e - d) : (peek - s);
10507         Copy(s, d, len, char);
10508         s += len;
10509         d += len;
10510     }
10511
10512     if (d >= PL_tokenbuf + sizeof PL_tokenbuf - 1)
10513         Perl_croak(aTHX_ "Delimiter for here document is too long");
10514
10515     *d++ = '\n';
10516     *d = '\0';
10517     len = d - PL_tokenbuf;
10518
10519 #ifndef PERL_STRICT_CR
10520     d = (char *) memchr(s, '\r', PL_bufend - s);
10521     if (d) {
10522         char * const olds = s;
10523         s = d;
10524         while (s < PL_bufend) {
10525             if (*s == '\r') {
10526                 *d++ = '\n';
10527                 if (*++s == '\n')
10528                     s++;
10529             }
10530             else if (*s == '\n' && s[1] == '\r') {      /* \015\013 on a mac? */
10531                 *d++ = *s++;
10532                 s++;
10533             }
10534             else
10535                 *d++ = *s++;
10536         }
10537         *d = '\0';
10538         PL_bufend = d;
10539         SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
10540         s = olds;
10541     }
10542 #endif
10543
10544     tmpstr = newSV_type(SVt_PVIV);
10545     SvGROW(tmpstr, 80);
10546     if (term == '\'') {
10547         op_type = OP_CONST;
10548         SvIV_set(tmpstr, -1);
10549     }
10550     else if (term == '`') {
10551         op_type = OP_BACKTICK;
10552         SvIV_set(tmpstr, '\\');
10553     }
10554
10555     PL_multi_start = origline + 1 + PL_parser->herelines;
10556     PL_multi_open = PL_multi_close = '<';
10557
10558     /* inside a string eval or quote-like operator */
10559     if (!infile || PL_lex_inwhat) {
10560         SV *linestr;
10561         char *bufend;
10562         char * const olds = s;
10563         PERL_CONTEXT * const cx = CX_CUR();
10564         /* These two fields are not set until an inner lexing scope is
10565            entered.  But we need them set here. */
10566         shared->ls_bufptr  = s;
10567         shared->ls_linestr = PL_linestr;
10568
10569         if (PL_lex_inwhat) {
10570             /* Look for a newline.  If the current buffer does not have one,
10571              peek into the line buffer of the parent lexing scope, going
10572              up as many levels as necessary to find one with a newline
10573              after bufptr.
10574             */
10575             while (!(s = (char *)memchr(
10576                                 (void *)shared->ls_bufptr, '\n',
10577                                 SvEND(shared->ls_linestr)-shared->ls_bufptr
10578                 )))
10579             {
10580                 shared = shared->ls_prev;
10581                 /* shared is only null if we have gone beyond the outermost
10582                    lexing scope.  In a file, we will have broken out of the
10583                    loop in the previous iteration.  In an eval, the string buf-
10584                    fer ends with "\n;", so the while condition above will have
10585                    evaluated to false.  So shared can never be null.  Or so you
10586                    might think.  Odd syntax errors like s;@{<<; can gobble up
10587                    the implicit semicolon at the end of a flie, causing the
10588                    file handle to be closed even when we are not in a string
10589                    eval.  So shared may be null in that case.
10590                    (Closing '>>}' here to balance the earlier open brace for
10591                    editors that look for matched pairs.) */
10592                 if (UNLIKELY(!shared))
10593                     goto interminable;
10594                 /* A LEXSHARED struct with a null ls_prev pointer is the outer-
10595                    most lexing scope.  In a file, shared->ls_linestr at that
10596                    level is just one line, so there is no body to steal. */
10597                 if (infile && !shared->ls_prev) {
10598                     s = olds;
10599                     goto streaming;
10600                 }
10601             }
10602         }
10603         else {  /* eval or we've already hit EOF */
10604             s = (char*)memchr((void*)s, '\n', PL_bufend - s);
10605             if (!s)
10606                 goto interminable;
10607         }
10608
10609         linestr = shared->ls_linestr;
10610         bufend = SvEND(linestr);
10611         d = s;
10612         if (indented) {
10613             char *myolds = s;
10614
10615             while (s < bufend - len + 1) {
10616                 if (*s++ == '\n')
10617                     ++PL_parser->herelines;
10618
10619                 if (memEQ(s, PL_tokenbuf + 1, len - 1)) {
10620                     char *backup = s;
10621                     indent_len = 0;
10622
10623                     /* Only valid if it's preceded by whitespace only */
10624                     while (backup != myolds && --backup >= myolds) {
10625                         if (! SPACE_OR_TAB(*backup)) {
10626                             break;
10627                         }
10628                         indent_len++;
10629                     }
10630
10631                     /* No whitespace or all! */
10632                     if (backup == s || *backup == '\n') {
10633                         Newx(indent, indent_len + 1, char);
10634                         memcpy(indent, backup + 1, indent_len);
10635                         indent[indent_len] = 0;
10636                         s--; /* before our delimiter */
10637                         PL_parser->herelines--; /* this line doesn't count */
10638                         break;
10639                     }
10640                 }
10641             }
10642         }
10643         else {
10644             while (s < bufend - len + 1
10645                    && memNE(s,PL_tokenbuf,len) )
10646             {
10647                 if (*s++ == '\n')
10648                     ++PL_parser->herelines;
10649             }
10650         }
10651
10652         if (s >= bufend - len + 1) {
10653             goto interminable;
10654         }
10655
10656         sv_setpvn(tmpstr,d+1,s-d);
10657         s += len - 1;
10658         /* the preceding stmt passes a newline */
10659         PL_parser->herelines++;
10660
10661         /* s now points to the newline after the heredoc terminator.
10662            d points to the newline before the body of the heredoc.
10663          */
10664
10665         /* We are going to modify linestr in place here, so set
10666            aside copies of the string if necessary for re-evals or
10667            (caller $n)[6]. */
10668         /* See the Paranoia note in case LEX_INTERPEND in yylex, for why we
10669            check shared->re_eval_str. */
10670         if (shared->re_eval_start || shared->re_eval_str) {
10671             /* Set aside the rest of the regexp */
10672             if (!shared->re_eval_str)
10673                 shared->re_eval_str =
10674                        newSVpvn(shared->re_eval_start,
10675                                 bufend - shared->re_eval_start);
10676             shared->re_eval_start -= s-d;
10677         }
10678
10679         if (cxstack_ix >= 0
10680             && CxTYPE(cx) == CXt_EVAL
10681             && CxOLD_OP_TYPE(cx) == OP_ENTEREVAL
10682             && cx->blk_eval.cur_text == linestr)
10683         {
10684             cx->blk_eval.cur_text = newSVsv(linestr);
10685             cx->blk_u16 |= 0x40; /* indicate cur_text is ref counted */
10686         }
10687
10688         /* Copy everything from s onwards back to d. */
10689         Move(s,d,bufend-s + 1,char);
10690         SvCUR_set(linestr, SvCUR(linestr) - (s-d));
10691         /* Setting PL_bufend only applies when we have not dug deeper
10692            into other scopes, because sublex_done sets PL_bufend to
10693            SvEND(PL_linestr). */
10694         if (shared == PL_parser->lex_shared)
10695             PL_bufend = SvEND(linestr);
10696         s = olds;
10697     }
10698     else {
10699         SV *linestr_save;
10700         char *oldbufptr_save;
10701         char *oldoldbufptr_save;
10702       streaming:
10703         SvPVCLEAR(tmpstr);   /* avoid "uninitialized" warning */
10704         term = PL_tokenbuf[1];
10705         len--;
10706         linestr_save = PL_linestr; /* must restore this afterwards */
10707         d = s;                   /* and this */
10708         oldbufptr_save = PL_oldbufptr;
10709         oldoldbufptr_save = PL_oldoldbufptr;
10710         PL_linestr = newSVpvs("");
10711         PL_bufend = SvPVX(PL_linestr);
10712
10713         while (1) {
10714             PL_bufptr = PL_bufend;
10715             CopLINE_set(PL_curcop,
10716                         origline + 1 + PL_parser->herelines);
10717
10718             if (   !lex_next_chunk(LEX_NO_TERM)
10719                 && (!SvCUR(tmpstr) || SvEND(tmpstr)[-1] != '\n'))
10720             {
10721                 /* Simply freeing linestr_save might seem simpler here, as it
10722                    does not matter what PL_linestr points to, since we are
10723                    about to croak; but in a quote-like op, linestr_save
10724                    will have been prospectively freed already, via
10725                    SAVEFREESV(PL_linestr) in sublex_push, so it’s easier to
10726                    restore PL_linestr. */
10727                 SvREFCNT_dec_NN(PL_linestr);
10728                 PL_linestr = linestr_save;
10729                 PL_oldbufptr = oldbufptr_save;
10730                 PL_oldoldbufptr = oldoldbufptr_save;
10731                 goto interminable;
10732             }
10733
10734             CopLINE_set(PL_curcop, origline);
10735
10736             if (!SvCUR(PL_linestr) || PL_bufend[-1] != '\n') {
10737                 s = lex_grow_linestr(SvLEN(PL_linestr) + 3);
10738                 /* ^That should be enough to avoid this needing to grow:  */
10739                 sv_catpvs(PL_linestr, "\n\0");
10740                 assert(s == SvPVX(PL_linestr));
10741                 PL_bufend = SvEND(PL_linestr);
10742             }
10743
10744             s = PL_bufptr;
10745             PL_parser->herelines++;
10746             PL_last_lop = PL_last_uni = NULL;
10747
10748 #ifndef PERL_STRICT_CR
10749             if (PL_bufend - PL_linestart >= 2) {
10750                 if (   (PL_bufend[-2] == '\r' && PL_bufend[-1] == '\n')
10751                     || (PL_bufend[-2] == '\n' && PL_bufend[-1] == '\r'))
10752                 {
10753                     PL_bufend[-2] = '\n';
10754                     PL_bufend--;
10755                     SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
10756                 }
10757                 else if (PL_bufend[-1] == '\r')
10758                     PL_bufend[-1] = '\n';
10759             }
10760             else if (PL_bufend - PL_linestart == 1 && PL_bufend[-1] == '\r')
10761                 PL_bufend[-1] = '\n';
10762 #endif
10763
10764             if (indented && (PL_bufend-s) >= len) {
10765                 char * found = ninstr(s, PL_bufend, (PL_tokenbuf + 1), (PL_tokenbuf +1 + len));
10766
10767                 if (found) {
10768                     char *backup = found;
10769                     indent_len = 0;
10770
10771                     /* Only valid if it's preceded by whitespace only */
10772                     while (backup != s && --backup >= s) {
10773                         if (! SPACE_OR_TAB(*backup)) {
10774                             break;
10775                         }
10776                         indent_len++;
10777                     }
10778
10779                     /* All whitespace or none! */
10780                     if (backup == found || SPACE_OR_TAB(*backup)) {
10781                         Newx(indent, indent_len + 1, char);
10782                         memcpy(indent, backup, indent_len);
10783                         indent[indent_len] = 0;
10784                         SvREFCNT_dec(PL_linestr);
10785                         PL_linestr = linestr_save;
10786                         PL_linestart = SvPVX(linestr_save);
10787                         PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
10788                         PL_oldbufptr = oldbufptr_save;
10789                         PL_oldoldbufptr = oldoldbufptr_save;
10790                         s = d;
10791                         break;
10792                     }
10793                 }
10794
10795                 /* Didn't find it */
10796                 sv_catsv(tmpstr,PL_linestr);
10797             }
10798             else {
10799                 if (*s == term && PL_bufend-s >= len
10800                     && memEQ(s,PL_tokenbuf + 1,len))
10801                 {
10802                     SvREFCNT_dec(PL_linestr);
10803                     PL_linestr = linestr_save;
10804                     PL_linestart = SvPVX(linestr_save);
10805                     PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
10806                     PL_oldbufptr = oldbufptr_save;
10807                     PL_oldoldbufptr = oldoldbufptr_save;
10808                     s = d;
10809                     break;
10810                 }
10811                 else {
10812                     sv_catsv(tmpstr,PL_linestr);
10813                 }
10814             }
10815         } /* while (1) */
10816     }
10817
10818     PL_multi_end = origline + PL_parser->herelines;
10819
10820     if (indented && indent) {
10821         STRLEN linecount = 1;
10822         STRLEN herelen = SvCUR(tmpstr);
10823         char *ss = SvPVX(tmpstr);
10824         char *se = ss + herelen;
10825         SV *newstr = newSV(herelen+1);
10826         SvPOK_on(newstr);
10827
10828         /* Trim leading whitespace */
10829         while (ss < se) {
10830             /* newline only? Copy and move on */
10831             if (*ss == '\n') {
10832                 sv_catpvs(newstr,"\n");
10833                 ss++;
10834                 linecount++;
10835
10836             /* Found our indentation? Strip it */
10837             }
10838             else if (se - ss >= indent_len
10839                        && memEQ(ss, indent, indent_len))
10840             {
10841                 STRLEN le = 0;
10842                 ss += indent_len;
10843
10844                 while ((ss + le) < se && *(ss + le) != '\n')
10845                     le++;
10846
10847                 sv_catpvn(newstr, ss, le);
10848                 ss += le;
10849
10850             /* Line doesn't begin with our indentation? Croak */
10851             }
10852             else {
10853                 Safefree(indent);
10854                 Perl_croak(aTHX_
10855                     "Indentation on line %d of here-doc doesn't match delimiter",
10856                     (int)linecount
10857                 );
10858             }
10859         } /* while */
10860
10861         /* avoid sv_setsv() as we dont wan't to COW here */
10862         sv_setpvn(tmpstr,SvPVX(newstr),SvCUR(newstr));
10863         Safefree(indent);
10864         SvREFCNT_dec_NN(newstr);
10865     }
10866
10867     if (SvCUR(tmpstr) + 5 < SvLEN(tmpstr)) {
10868         SvPV_shrink_to_cur(tmpstr);
10869     }
10870
10871     if (!IN_BYTES) {
10872         if (UTF && is_utf8_string((U8*)SvPVX_const(tmpstr), SvCUR(tmpstr)))
10873             SvUTF8_on(tmpstr);
10874     }
10875
10876     PL_lex_stuff = tmpstr;
10877     pl_yylval.ival = op_type;
10878     return s;
10879
10880   interminable:
10881     if (indent)
10882         Safefree(indent);
10883     SvREFCNT_dec(tmpstr);
10884     CopLINE_set(PL_curcop, origline);
10885     missingterm(PL_tokenbuf + 1, sizeof(PL_tokenbuf) - 1);
10886 }
10887
10888
10889 /* scan_inputsymbol
10890    takes: position of first '<' in input buffer
10891    returns: position of first char following the matching '>' in
10892             input buffer
10893    side-effects: pl_yylval and lex_op are set.
10894
10895    This code handles:
10896
10897    <>           read from ARGV
10898    <<>>         read from ARGV without magic open
10899    <FH>         read from filehandle
10900    <pkg::FH>    read from package qualified filehandle
10901    <pkg'FH>     read from package qualified filehandle
10902    <$fh>        read from filehandle in $fh
10903    <*.h>        filename glob
10904
10905 */
10906
10907 STATIC char *
10908 S_scan_inputsymbol(pTHX_ char *start)
10909 {
10910     char *s = start;            /* current position in buffer */
10911     char *end;
10912     I32 len;
10913     bool nomagicopen = FALSE;
10914     char *d = PL_tokenbuf;                                      /* start of temp holding space */
10915     const char * const e = PL_tokenbuf + sizeof PL_tokenbuf;    /* end of temp holding space */
10916
10917     PERL_ARGS_ASSERT_SCAN_INPUTSYMBOL;
10918
10919     end = (char *) memchr(s, '\n', PL_bufend - s);
10920     if (!end)
10921         end = PL_bufend;
10922     if (s[1] == '<' && s[2] == '>' && s[3] == '>') {
10923         nomagicopen = TRUE;
10924         *d = '\0';
10925         len = 0;
10926         s += 3;
10927     }
10928     else
10929         s = delimcpy(d, e, s + 1, end, '>', &len);      /* extract until > */
10930
10931     /* die if we didn't have space for the contents of the <>,
10932        or if it didn't end, or if we see a newline
10933     */
10934
10935     if (len >= (I32)sizeof PL_tokenbuf)
10936         Perl_croak(aTHX_ "Excessively long <> operator");
10937     if (s >= end)
10938         Perl_croak(aTHX_ "Unterminated <> operator");
10939
10940     s++;
10941
10942     /* check for <$fh>
10943        Remember, only scalar variables are interpreted as filehandles by
10944        this code.  Anything more complex (e.g., <$fh{$num}>) will be
10945        treated as a glob() call.
10946        This code makes use of the fact that except for the $ at the front,
10947        a scalar variable and a filehandle look the same.
10948     */
10949     if (*d == '$' && d[1]) d++;
10950
10951     /* allow <Pkg'VALUE> or <Pkg::VALUE> */
10952     while (isWORDCHAR_lazy_if_safe(d, e, UTF) || *d == '\'' || *d == ':') {
10953         d += UTF ? UTF8SKIP(d) : 1;
10954     }
10955
10956     /* If we've tried to read what we allow filehandles to look like, and
10957        there's still text left, then it must be a glob() and not a getline.
10958        Use scan_str to pull out the stuff between the <> and treat it
10959        as nothing more than a string.
10960     */
10961
10962     if (d - PL_tokenbuf != len) {
10963         pl_yylval.ival = OP_GLOB;
10964         s = scan_str(start,FALSE,FALSE,FALSE,NULL);
10965         if (!s)
10966            Perl_croak(aTHX_ "Glob not terminated");
10967         return s;
10968     }
10969     else {
10970         bool readline_overriden = FALSE;
10971         GV *gv_readline;
10972         /* we're in a filehandle read situation */
10973         d = PL_tokenbuf;
10974
10975         /* turn <> into <ARGV> */
10976         if (!len)
10977             Copy("ARGV",d,5,char);
10978
10979         /* Check whether readline() is overriden */
10980         if ((gv_readline = gv_override("readline",8)))
10981             readline_overriden = TRUE;
10982
10983         /* if <$fh>, create the ops to turn the variable into a
10984            filehandle
10985         */
10986         if (*d == '$') {
10987             /* try to find it in the pad for this block, otherwise find
10988                add symbol table ops
10989             */
10990             const PADOFFSET tmp = pad_findmy_pvn(d, len, 0);
10991             if (tmp != NOT_IN_PAD) {
10992                 if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
10993                     HV * const stash = PAD_COMPNAME_OURSTASH(tmp);
10994                     HEK * const stashname = HvNAME_HEK(stash);
10995                     SV * const sym = sv_2mortal(newSVhek(stashname));
10996                     sv_catpvs(sym, "::");
10997                     sv_catpv(sym, d+1);
10998                     d = SvPVX(sym);
10999                     goto intro_sym;
11000                 }
11001                 else {
11002                     OP * const o = newOP(OP_PADSV, 0);
11003                     o->op_targ = tmp;
11004                     PL_lex_op = readline_overriden
11005                         ? newUNOP(OP_ENTERSUB, OPf_STACKED,
11006                                 op_append_elem(OP_LIST, o,
11007                                     newCVREF(0, newGVOP(OP_GV,0,gv_readline))))
11008                         : newUNOP(OP_READLINE, 0, o);
11009                 }
11010             }
11011             else {
11012                 GV *gv;
11013                 ++d;
11014               intro_sym:
11015                 gv = gv_fetchpv(d,
11016                                 GV_ADDMULTI | ( UTF ? SVf_UTF8 : 0 ),
11017                                 SVt_PV);
11018                 PL_lex_op = readline_overriden
11019                     ? newUNOP(OP_ENTERSUB, OPf_STACKED,
11020                             op_append_elem(OP_LIST,
11021                                 newUNOP(OP_RV2SV, 0, newGVOP(OP_GV, 0, gv)),
11022                                 newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
11023                     : newUNOP(OP_READLINE, 0,
11024                             newUNOP(OP_RV2SV, 0,
11025                                 newGVOP(OP_GV, 0, gv)));
11026             }
11027             /* we created the ops in PL_lex_op, so make pl_yylval.ival a null op */
11028             pl_yylval.ival = OP_NULL;
11029         }
11030
11031         /* If it's none of the above, it must be a literal filehandle
11032            (<Foo::BAR> or <FOO>) so build a simple readline OP */
11033         else {
11034             GV * const gv = gv_fetchpv(d, GV_ADD | ( UTF ? SVf_UTF8 : 0 ), SVt_PVIO);
11035             PL_lex_op = readline_overriden
11036                 ? newUNOP(OP_ENTERSUB, OPf_STACKED,
11037                         op_append_elem(OP_LIST,
11038                             newGVOP(OP_GV, 0, gv),
11039                             newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
11040                 : newUNOP(OP_READLINE, nomagicopen ? OPf_SPECIAL : 0, newGVOP(OP_GV, 0, gv));
11041             pl_yylval.ival = OP_NULL;
11042         }
11043     }
11044
11045     return s;
11046 }
11047
11048
11049 /* scan_str
11050    takes:
11051         start                   position in buffer
11052         keep_bracketed_quoted   preserve \ quoting of embedded delimiters, but
11053                                 only if they are of the open/close form
11054         keep_delims             preserve the delimiters around the string
11055         re_reparse              compiling a run-time /(?{})/:
11056                                    collapse // to /,  and skip encoding src
11057         delimp                  if non-null, this is set to the position of
11058                                 the closing delimiter, or just after it if
11059                                 the closing and opening delimiters differ
11060                                 (i.e., the opening delimiter of a substitu-
11061                                 tion replacement)
11062    returns: position to continue reading from buffer
11063    side-effects: multi_start, multi_close, lex_repl or lex_stuff, and
11064         updates the read buffer.
11065
11066    This subroutine pulls a string out of the input.  It is called for:
11067         q               single quotes           q(literal text)
11068         '               single quotes           'literal text'
11069         qq              double quotes           qq(interpolate $here please)
11070         "               double quotes           "interpolate $here please"
11071         qx              backticks               qx(/bin/ls -l)
11072         `               backticks               `/bin/ls -l`
11073         qw              quote words             @EXPORT_OK = qw( func() $spam )
11074         m//             regexp match            m/this/
11075         s///            regexp substitute       s/this/that/
11076         tr///           string transliterate    tr/this/that/
11077         y///            string transliterate    y/this/that/
11078         ($*@)           sub prototypes          sub foo ($)
11079         (stuff)         sub attr parameters     sub foo : attr(stuff)
11080         <>              readline or globs       <FOO>, <>, <$fh>, or <*.c>
11081
11082    In most of these cases (all but <>, patterns and transliterate)
11083    yylex() calls scan_str().  m// makes yylex() call scan_pat() which
11084    calls scan_str().  s/// makes yylex() call scan_subst() which calls
11085    scan_str().  tr/// and y/// make yylex() call scan_trans() which
11086    calls scan_str().
11087
11088    It skips whitespace before the string starts, and treats the first
11089    character as the delimiter.  If the delimiter is one of ([{< then
11090    the corresponding "close" character )]}> is used as the closing
11091    delimiter.  It allows quoting of delimiters, and if the string has
11092    balanced delimiters ([{<>}]) it allows nesting.
11093
11094    On success, the SV with the resulting string is put into lex_stuff or,
11095    if that is already non-NULL, into lex_repl. The second case occurs only
11096    when parsing the RHS of the special constructs s/// and tr/// (y///).
11097    For convenience, the terminating delimiter character is stuffed into
11098    SvIVX of the SV.
11099 */
11100
11101 char *
11102 Perl_scan_str(pTHX_ char *start, int keep_bracketed_quoted, int keep_delims, int re_reparse,
11103                  char **delimp
11104     )
11105 {
11106     SV *sv;                     /* scalar value: string */
11107     const char *tmps;           /* temp string, used for delimiter matching */
11108     char *s = start;            /* current position in the buffer */
11109     char term;                  /* terminating character */
11110     char *to;                   /* current position in the sv's data */
11111     I32 brackets = 1;           /* bracket nesting level */
11112     bool d_is_utf8 = FALSE;     /* is there any utf8 content? */
11113     IV termcode;                /* terminating char. code */
11114     U8 termstr[UTF8_MAXBYTES+1]; /* terminating string */
11115     STRLEN termlen;             /* length of terminating string */
11116     line_t herelines;
11117
11118     /* The delimiters that have a mirror-image closing one */
11119     const char * opening_delims = "([{<";
11120     const char * closing_delims = ")]}>";
11121
11122     /* The only non-UTF character that isn't a stand alone grapheme is
11123      * white-space, hence can't be a delimiter. */
11124     const char * non_grapheme_msg = "Use of unassigned code point or"
11125                                     " non-standalone grapheme for a delimiter"
11126                                     " is not allowed";
11127     PERL_ARGS_ASSERT_SCAN_STR;
11128
11129     /* skip space before the delimiter */
11130     if (isSPACE(*s)) {
11131         s = skipspace(s);
11132     }
11133
11134     /* mark where we are, in case we need to report errors */
11135     CLINE;
11136
11137     /* after skipping whitespace, the next character is the terminator */
11138     term = *s;
11139     if (!UTF || UTF8_IS_INVARIANT(term)) {
11140         termcode = termstr[0] = term;
11141         termlen = 1;
11142     }
11143     else {
11144         termcode = utf8_to_uvchr_buf((U8*)s, (U8*)PL_bufend, &termlen);
11145         if (UTF && UNLIKELY(! is_grapheme((U8 *) start,
11146                                            (U8 *) s,
11147                                            (U8 *) PL_bufend,
11148                                                   termcode)))
11149         {
11150             yyerror(non_grapheme_msg);
11151         }
11152
11153         Copy(s, termstr, termlen, U8);
11154     }
11155
11156     /* mark where we are */
11157     PL_multi_start = CopLINE(PL_curcop);
11158     PL_multi_open = termcode;
11159     herelines = PL_parser->herelines;
11160
11161     /* If the delimiter has a mirror-image closing one, get it */
11162     if (term && (tmps = strchr(opening_delims, term))) {
11163         termcode = termstr[0] = term = closing_delims[tmps - opening_delims];
11164     }
11165
11166     PL_multi_close = termcode;
11167
11168     if (PL_multi_open == PL_multi_close) {
11169         keep_bracketed_quoted = FALSE;
11170     }
11171
11172     /* create a new SV to hold the contents.  79 is the SV's initial length.
11173        What a random number. */
11174     sv = newSV_type(SVt_PVIV);
11175     SvGROW(sv, 80);
11176     SvIV_set(sv, termcode);
11177     (void)SvPOK_only(sv);               /* validate pointer */
11178
11179     /* move past delimiter and try to read a complete string */
11180     if (keep_delims)
11181         sv_catpvn(sv, s, termlen);
11182     s += termlen;
11183     for (;;) {
11184         /* extend sv if need be */
11185         SvGROW(sv, SvCUR(sv) + (PL_bufend - s) + 1);
11186         /* set 'to' to the next character in the sv's string */
11187         to = SvPVX(sv)+SvCUR(sv);
11188
11189         /* if open delimiter is the close delimiter read unbridle */
11190         if (PL_multi_open == PL_multi_close) {
11191             for (; s < PL_bufend; s++,to++) {
11192                 /* embedded newlines increment the current line number */
11193                 if (*s == '\n' && !PL_rsfp && !PL_parser->filtered)
11194                     COPLINE_INC_WITH_HERELINES;
11195                 /* handle quoted delimiters */
11196                 if (*s == '\\' && s+1 < PL_bufend && term != '\\') {
11197                     if (!keep_bracketed_quoted
11198                         && (s[1] == term
11199                             || (re_reparse && s[1] == '\\'))
11200                     )
11201                         s++;
11202                     else /* any other quotes are simply copied straight through */
11203                         *to++ = *s++;
11204                 }
11205                 /* terminate when run out of buffer (the for() condition), or
11206                    have found the terminator */
11207                 else if (*s == term) {  /* First byte of terminator matches */
11208                     if (termlen == 1)   /* If is the only byte, are done */
11209                         break;
11210
11211                     /* If the remainder of the terminator matches, also are
11212                      * done, after checking that is a separate grapheme */
11213                     if (   s + termlen <= PL_bufend
11214                         && memEQ(s + 1, (char*)termstr + 1, termlen - 1))
11215                     {
11216                         if (   UTF
11217                             && UNLIKELY(! is_grapheme((U8 *) start,
11218                                                        (U8 *) s,
11219                                                        (U8 *) PL_bufend,
11220                                                               termcode)))
11221                         {
11222                             yyerror(non_grapheme_msg);
11223                         }
11224                         break;
11225                     }
11226                 }
11227                 else if (!d_is_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF) {
11228                     d_is_utf8 = TRUE;
11229                 }
11230
11231                 *to = *s;
11232             }
11233         }
11234
11235         /* if the terminator isn't the same as the start character (e.g.,
11236            matched brackets), we have to allow more in the quoting, and
11237            be prepared for nested brackets.
11238         */
11239         else {
11240             /* read until we run out of string, or we find the terminator */
11241             for (; s < PL_bufend; s++,to++) {
11242                 /* embedded newlines increment the line count */
11243                 if (*s == '\n' && !PL_rsfp && !PL_parser->filtered)
11244                     COPLINE_INC_WITH_HERELINES;
11245                 /* backslashes can escape the open or closing characters */
11246                 if (*s == '\\' && s+1 < PL_bufend) {
11247                     if (!keep_bracketed_quoted
11248                        && ( ((UV)s[1] == PL_multi_open)
11249                          || ((UV)s[1] == PL_multi_close) ))
11250                     {
11251                         s++;
11252                     }
11253                     else
11254                         *to++ = *s++;
11255                 }
11256                 /* allow nested opens and closes */
11257                 else if ((UV)*s == PL_multi_close && --brackets <= 0)
11258                     break;
11259                 else if ((UV)*s == PL_multi_open)
11260                     brackets++;
11261                 else if (!d_is_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF)
11262                     d_is_utf8 = TRUE;
11263                 *to = *s;
11264             }
11265         }
11266         /* terminate the copied string and update the sv's end-of-string */
11267         *to = '\0';
11268         SvCUR_set(sv, to - SvPVX_const(sv));
11269
11270         /*
11271          * this next chunk reads more into the buffer if we're not done yet
11272          */
11273
11274         if (s < PL_bufend)
11275             break;              /* handle case where we are done yet :-) */
11276
11277 #ifndef PERL_STRICT_CR
11278         if (to - SvPVX_const(sv) >= 2) {
11279             if (   (to[-2] == '\r' && to[-1] == '\n')
11280                 || (to[-2] == '\n' && to[-1] == '\r'))
11281             {
11282                 to[-2] = '\n';
11283                 to--;
11284                 SvCUR_set(sv, to - SvPVX_const(sv));
11285             }
11286             else if (to[-1] == '\r')
11287                 to[-1] = '\n';
11288         }
11289         else if (to - SvPVX_const(sv) == 1 && to[-1] == '\r')
11290             to[-1] = '\n';
11291 #endif
11292
11293         /* if we're out of file, or a read fails, bail and reset the current
11294            line marker so we can report where the unterminated string began
11295         */
11296         COPLINE_INC_WITH_HERELINES;
11297         PL_bufptr = PL_bufend;
11298         if (!lex_next_chunk(0)) {
11299             sv_free(sv);
11300             CopLINE_set(PL_curcop, (line_t)PL_multi_start);
11301             return NULL;
11302         }
11303         s = start = PL_bufptr;
11304     }
11305
11306     /* at this point, we have successfully read the delimited string */
11307
11308     if (keep_delims)
11309             sv_catpvn(sv, s, termlen);
11310     s += termlen;
11311
11312     if (d_is_utf8)
11313         SvUTF8_on(sv);
11314
11315     PL_multi_end = CopLINE(PL_curcop);
11316     CopLINE_set(PL_curcop, PL_multi_start);
11317     PL_parser->herelines = herelines;
11318
11319     /* if we allocated too much space, give some back */
11320     if (SvCUR(sv) + 5 < SvLEN(sv)) {
11321         SvLEN_set(sv, SvCUR(sv) + 1);
11322         SvPV_renew(sv, SvLEN(sv));
11323     }
11324
11325     /* decide whether this is the first or second quoted string we've read
11326        for this op
11327     */
11328
11329     if (PL_lex_stuff)
11330         PL_parser->lex_sub_repl = sv;
11331     else
11332         PL_lex_stuff = sv;
11333     if (delimp) *delimp = PL_multi_open == PL_multi_close ? s-termlen : s;
11334     return s;
11335 }
11336
11337 /*
11338   scan_num
11339   takes: pointer to position in buffer
11340   returns: pointer to new position in buffer
11341   side-effects: builds ops for the constant in pl_yylval.op
11342
11343   Read a number in any of the formats that Perl accepts:
11344
11345   \d(_?\d)*(\.(\d(_?\d)*)?)?[Ee][\+\-]?(\d(_?\d)*)      12 12.34 12.
11346   \.\d(_?\d)*[Ee][\+\-]?(\d(_?\d)*)                     .34
11347   0b[01](_?[01])*                                       binary integers
11348   0[0-7](_?[0-7])*                                      octal integers
11349   0x[0-9A-Fa-f](_?[0-9A-Fa-f])*                         hexadecimal integers
11350   0x[0-9A-Fa-f](_?[0-9A-Fa-f])*(?:\.\d*)?p[+-]?[0-9]+   hexadecimal floats
11351
11352   Like most scan_ routines, it uses the PL_tokenbuf buffer to hold the
11353   thing it reads.
11354
11355   If it reads a number without a decimal point or an exponent, it will
11356   try converting the number to an integer and see if it can do so
11357   without loss of precision.
11358 */
11359
11360 char *
11361 Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
11362 {
11363     const char *s = start;      /* current position in buffer */
11364     char *d;                    /* destination in temp buffer */
11365     char *e;                    /* end of temp buffer */
11366     NV nv;                              /* number read, as a double */
11367     SV *sv = NULL;                      /* place to put the converted number */
11368     bool floatit;                       /* boolean: int or float? */
11369     const char *lastub = NULL;          /* position of last underbar */
11370     static const char* const number_too_long = "Number too long";
11371     bool warned_about_underscore = 0;
11372     I32 shift; /* shift per digit for hex/oct/bin, hoisted here for fp */
11373 #define WARN_ABOUT_UNDERSCORE() \
11374         do { \
11375             if (!warned_about_underscore) { \
11376                 warned_about_underscore = 1; \
11377                 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), \
11378                                "Misplaced _ in number"); \
11379             } \
11380         } while(0)
11381     /* Hexadecimal floating point.
11382      *
11383      * In many places (where we have quads and NV is IEEE 754 double)
11384      * we can fit the mantissa bits of a NV into an unsigned quad.
11385      * (Note that UVs might not be quads even when we have quads.)
11386      * This will not work everywhere, though (either no quads, or
11387      * using long doubles), in which case we have to resort to NV,
11388      * which will probably mean horrible loss of precision due to
11389      * multiple fp operations. */
11390     bool hexfp = FALSE;
11391     int total_bits = 0;
11392     int significant_bits = 0;
11393 #if NVSIZE == 8 && defined(HAS_QUAD) && defined(Uquad_t)
11394 #  define HEXFP_UQUAD
11395     Uquad_t hexfp_uquad = 0;
11396     int hexfp_frac_bits = 0;
11397 #else
11398 #  define HEXFP_NV
11399     NV hexfp_nv = 0.0;
11400 #endif
11401     NV hexfp_mult = 1.0;
11402     UV high_non_zero = 0; /* highest digit */
11403     int non_zero_integer_digits = 0;
11404
11405     PERL_ARGS_ASSERT_SCAN_NUM;
11406
11407     /* We use the first character to decide what type of number this is */
11408
11409     switch (*s) {
11410     default:
11411         Perl_croak(aTHX_ "panic: scan_num, *s=%d", *s);
11412
11413     /* if it starts with a 0, it could be an octal number, a decimal in
11414        0.13 disguise, or a hexadecimal number, or a binary number. */
11415     case '0':
11416         {
11417           /* variables:
11418              u          holds the "number so far"
11419              overflowed was the number more than we can hold?
11420
11421              Shift is used when we add a digit.  It also serves as an "are
11422              we in octal/hex/binary?" indicator to disallow hex characters
11423              when in octal mode.
11424            */
11425             NV n = 0.0;
11426             UV u = 0;
11427             bool overflowed = FALSE;
11428             bool just_zero  = TRUE;     /* just plain 0 or binary number? */
11429             bool has_digs = FALSE;
11430             static const NV nvshift[5] = { 1.0, 2.0, 4.0, 8.0, 16.0 };
11431             static const char* const bases[5] =
11432               { "", "binary", "", "octal", "hexadecimal" };
11433             static const char* const Bases[5] =
11434               { "", "Binary", "", "Octal", "Hexadecimal" };
11435             static const char* const maxima[5] =
11436               { "",
11437                 "0b11111111111111111111111111111111",
11438                 "",
11439                 "037777777777",
11440                 "0xffffffff" };
11441             const char *base, *Base, *max;
11442
11443             /* check for hex */
11444             if (isALPHA_FOLD_EQ(s[1], 'x')) {
11445                 shift = 4;
11446                 s += 2;
11447                 just_zero = FALSE;
11448             } else if (isALPHA_FOLD_EQ(s[1], 'b')) {
11449                 shift = 1;
11450                 s += 2;
11451                 just_zero = FALSE;
11452             }
11453             /* check for a decimal in disguise */
11454             else if (s[1] == '.' || isALPHA_FOLD_EQ(s[1], 'e'))
11455                 goto decimal;
11456             /* so it must be octal */
11457             else {
11458                 shift = 3;
11459                 s++;
11460             }
11461
11462             if (*s == '_') {
11463                 WARN_ABOUT_UNDERSCORE();
11464                lastub = s++;
11465             }
11466
11467             base = bases[shift];
11468             Base = Bases[shift];
11469             max  = maxima[shift];
11470
11471             /* read the rest of the number */
11472             for (;;) {
11473                 /* x is used in the overflow test,
11474                    b is the digit we're adding on. */
11475                 UV x, b;
11476
11477                 switch (*s) {
11478
11479                 /* if we don't mention it, we're done */
11480                 default:
11481                     goto out;
11482
11483                 /* _ are ignored -- but warned about if consecutive */
11484                 case '_':
11485                     if (lastub && s == lastub + 1)
11486                         WARN_ABOUT_UNDERSCORE();
11487                     lastub = s++;
11488                     break;
11489
11490                 /* 8 and 9 are not octal */
11491                 case '8': case '9':
11492                     if (shift == 3)
11493                         yyerror(Perl_form(aTHX_ "Illegal octal digit '%c'", *s));
11494                     /* FALLTHROUGH */
11495
11496                 /* octal digits */
11497                 case '2': case '3': case '4':
11498                 case '5': case '6': case '7':
11499                     if (shift == 1)
11500                         yyerror(Perl_form(aTHX_ "Illegal binary digit '%c'", *s));
11501                     /* FALLTHROUGH */
11502
11503                 case '0': case '1':
11504                     b = *s++ & 15;              /* ASCII digit -> value of digit */
11505                     goto digit;
11506
11507                 /* hex digits */
11508                 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
11509                 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
11510                     /* make sure they said 0x */
11511                     if (shift != 4)
11512                         goto out;
11513                     b = (*s++ & 7) + 9;
11514
11515                     /* Prepare to put the digit we have onto the end
11516                        of the number so far.  We check for overflows.
11517                     */
11518
11519                   digit:
11520                     just_zero = FALSE;
11521                     has_digs = TRUE;
11522                     if (!overflowed) {
11523                         assert(shift >= 0);
11524                         x = u << shift; /* make room for the digit */
11525
11526                         total_bits += shift;
11527
11528                         if ((x >> shift) != u
11529                             && !(PL_hints & HINT_NEW_BINARY)) {
11530                             overflowed = TRUE;
11531                             n = (NV) u;
11532                             Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
11533                                              "Integer overflow in %s number",
11534                                              base);
11535                         } else
11536                             u = x | b;          /* add the digit to the end */
11537                     }
11538                     if (overflowed) {
11539                         n *= nvshift[shift];
11540                         /* If an NV has not enough bits in its
11541                          * mantissa to represent an UV this summing of
11542                          * small low-order numbers is a waste of time
11543                          * (because the NV cannot preserve the
11544                          * low-order bits anyway): we could just
11545                          * remember when did we overflow and in the
11546                          * end just multiply n by the right
11547                          * amount. */
11548                         n += (NV) b;
11549                     }
11550
11551                     if (high_non_zero == 0 && b > 0)
11552                         high_non_zero = b;
11553
11554                     if (high_non_zero)
11555                         non_zero_integer_digits++;
11556
11557                     /* this could be hexfp, but peek ahead
11558                      * to avoid matching ".." */
11559                     if (UNLIKELY(HEXFP_PEEK(s))) {
11560                         goto out;
11561                     }
11562
11563                     break;
11564                 }
11565             }
11566
11567           /* if we get here, we had success: make a scalar value from
11568              the number.
11569           */
11570           out:
11571
11572             /* final misplaced underbar check */
11573             if (s[-1] == '_')
11574                 WARN_ABOUT_UNDERSCORE();
11575
11576             if (UNLIKELY(HEXFP_PEEK(s))) {
11577                 /* Do sloppy (on the underbars) but quick detection
11578                  * (and value construction) for hexfp, the decimal
11579                  * detection will shortly be more thorough with the
11580                  * underbar checks. */
11581                 const char* h = s;
11582                 significant_bits = non_zero_integer_digits * shift;
11583 #ifdef HEXFP_UQUAD
11584                 hexfp_uquad = u;
11585 #else /* HEXFP_NV */
11586                 hexfp_nv = u;
11587 #endif
11588                 /* Ignore the leading zero bits of
11589                  * the high (first) non-zero digit. */
11590                 if (high_non_zero) {
11591                     if (high_non_zero < 0x8)
11592                         significant_bits--;
11593                     if (high_non_zero < 0x4)
11594                         significant_bits--;
11595                     if (high_non_zero < 0x2)
11596                         significant_bits--;
11597                 }
11598
11599                 if (*h == '.') {
11600 #ifdef HEXFP_NV
11601                     NV nv_mult = 1.0;
11602 #endif
11603                     bool accumulate = TRUE;
11604                     U8 b;
11605                     int lim = 1 << shift;
11606                     for (h++; ((isXDIGIT(*h) && (b = XDIGIT_VALUE(*h)) < lim) ||
11607                                *h == '_'); h++) {
11608                         if (isXDIGIT(*h)) {
11609                             significant_bits += shift;
11610 #ifdef HEXFP_UQUAD
11611                             if (accumulate) {
11612                                 if (significant_bits < NV_MANT_DIG) {
11613                                     /* We are in the long "run" of xdigits,
11614                                      * accumulate the full four bits. */
11615                                     assert(shift >= 0);
11616                                     hexfp_uquad <<= shift;
11617                                     hexfp_uquad |= b;
11618                                     hexfp_frac_bits += shift;
11619                                 } else if (significant_bits - shift < NV_MANT_DIG) {
11620                                     /* We are at a hexdigit either at,
11621                                      * or straddling, the edge of mantissa.
11622                                      * We will try grabbing as many as
11623                                      * possible bits. */
11624                                     int tail =
11625                                       significant_bits - NV_MANT_DIG;
11626                                     if (tail <= 0)
11627                                        tail += shift;
11628                                     assert(tail >= 0);
11629                                     hexfp_uquad <<= tail;
11630                                     assert((shift - tail) >= 0);
11631                                     hexfp_uquad |= b >> (shift - tail);
11632                                     hexfp_frac_bits += tail;
11633
11634                                     /* Ignore the trailing zero bits
11635                                      * of the last non-zero xdigit.
11636                                      *
11637                                      * The assumption here is that if
11638                                      * one has input of e.g. the xdigit
11639                                      * eight (0x8), there is only one
11640                                      * bit being input, not the full
11641                                      * four bits.  Conversely, if one
11642                                      * specifies a zero xdigit, the
11643                                      * assumption is that one really
11644                                      * wants all those bits to be zero. */
11645                                     if (b) {
11646                                         if ((b & 0x1) == 0x0) {
11647                                             significant_bits--;
11648                                             if ((b & 0x2) == 0x0) {
11649                                                 significant_bits--;
11650                                                 if ((b & 0x4) == 0x0) {
11651                                                     significant_bits--;
11652                                                 }
11653                                             }
11654                                         }
11655                                     }
11656
11657                                     accumulate = FALSE;
11658                                 }
11659                             } else {
11660                                 /* Keep skipping the xdigits, and
11661                                  * accumulating the significant bits,
11662                                  * but do not shift the uquad
11663                                  * (which would catastrophically drop
11664                                  * high-order bits) or accumulate the
11665                                  * xdigits anymore. */
11666                             }
11667 #else /* HEXFP_NV */
11668                             if (accumulate) {
11669                                 nv_mult /= nvshift[shift];
11670                                 if (nv_mult > 0.0)
11671                                     hexfp_nv += b * nv_mult;
11672                                 else
11673                                     accumulate = FALSE;
11674                             }
11675 #endif
11676                         }
11677                         if (significant_bits >= NV_MANT_DIG)
11678                             accumulate = FALSE;
11679                     }
11680                 }
11681
11682                 if ((total_bits > 0 || significant_bits > 0) &&
11683                     isALPHA_FOLD_EQ(*h, 'p')) {
11684                     bool negexp = FALSE;
11685                     h++;
11686                     if (*h == '+')
11687                         h++;
11688                     else if (*h == '-') {
11689                         negexp = TRUE;
11690                         h++;
11691                     }
11692                     if (isDIGIT(*h)) {
11693                         I32 hexfp_exp = 0;
11694                         while (isDIGIT(*h) || *h == '_') {
11695                             if (isDIGIT(*h)) {
11696                                 hexfp_exp *= 10;
11697                                 hexfp_exp += *h - '0';
11698 #ifdef NV_MIN_EXP
11699                                 if (negexp
11700                                     && -hexfp_exp < NV_MIN_EXP - 1) {
11701                                     /* NOTE: this means that the exponent
11702                                      * underflow warning happens for
11703                                      * the IEEE 754 subnormals (denormals),
11704                                      * because DBL_MIN_EXP etc are the lowest
11705                                      * possible binary (or, rather, DBL_RADIX-base)
11706                                      * exponent for normals, not subnormals.
11707                                      *
11708                                      * This may or may not be a good thing. */
11709                                     Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
11710                                                    "Hexadecimal float: exponent underflow");
11711                                     break;
11712                                 }
11713 #endif
11714 #ifdef NV_MAX_EXP
11715                                 if (!negexp
11716                                     && hexfp_exp > NV_MAX_EXP - 1) {
11717                                     Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
11718                                                    "Hexadecimal float: exponent overflow");
11719                                     break;
11720                                 }
11721 #endif
11722                             }
11723                             h++;
11724                         }
11725                         if (negexp)
11726                             hexfp_exp = -hexfp_exp;
11727 #ifdef HEXFP_UQUAD
11728                         hexfp_exp -= hexfp_frac_bits;
11729 #endif
11730                         hexfp_mult = Perl_pow(2.0, hexfp_exp);
11731                         hexfp = TRUE;
11732                         goto decimal;
11733                     }
11734                 }
11735             }
11736
11737             if (shift != 3 && !has_digs) {
11738                 /* 0x or 0b with no digits, treat it as an error.
11739                    Originally this backed up the parse before the b or
11740                    x, but that has the potential for silent changes in
11741                    behaviour, like for: "0x.3" and "0x+$foo".
11742                 */
11743                 const char *d = s;
11744                 char *oldbp = PL_bufptr;
11745                 if (*d) ++d; /* so the user sees the bad non-digit */
11746                 PL_bufptr = (char *)d; /* so yyerror reports the context */
11747                 yyerror(Perl_form(aTHX_ "No digits found for %s literal",
11748                                   shift == 4 ? "hexadecimal" : "binary"));
11749                 PL_bufptr = oldbp;
11750             }
11751
11752             if (overflowed) {
11753                 if (n > 4294967295.0)
11754                     Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
11755                                    "%s number > %s non-portable",
11756                                    Base, max);
11757                 sv = newSVnv(n);
11758             }
11759             else {
11760 #if UVSIZE > 4
11761                 if (u > 0xffffffff)
11762                     Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
11763                                    "%s number > %s non-portable",
11764                                    Base, max);
11765 #endif
11766                 sv = newSVuv(u);
11767             }
11768             if (just_zero && (PL_hints & HINT_NEW_INTEGER))
11769                 sv = new_constant(start, s - start, "integer",
11770                                   sv, NULL, NULL, 0, NULL);
11771             else if (PL_hints & HINT_NEW_BINARY)
11772                 sv = new_constant(start, s - start, "binary",
11773                                   sv, NULL, NULL, 0, NULL);
11774         }
11775         break;
11776
11777     /*
11778       handle decimal numbers.
11779       we're also sent here when we read a 0 as the first digit
11780     */
11781     case '1': case '2': case '3': case '4': case '5':
11782     case '6': case '7': case '8': case '9': case '.':
11783       decimal:
11784         d = PL_tokenbuf;
11785         e = PL_tokenbuf + sizeof PL_tokenbuf - 6; /* room for various punctuation */
11786         floatit = FALSE;
11787         if (hexfp) {
11788             floatit = TRUE;
11789             *d++ = '0';
11790             switch (shift) {
11791             case 4:
11792                 *d++ = 'x';
11793                 s = start + 2;
11794                 break;
11795             case 3:
11796                 s = start + 1;
11797                 break;
11798             case 1:
11799                 *d++ = 'b';
11800                 s = start + 2;
11801                 break;
11802             default:
11803                 NOT_REACHED; /* NOTREACHED */
11804             }
11805         }
11806
11807         /* read next group of digits and _ and copy into d */
11808         while (isDIGIT(*s)
11809                || *s == '_'
11810                || UNLIKELY(hexfp && isXDIGIT(*s)))
11811         {
11812             /* skip underscores, checking for misplaced ones
11813                if -w is on
11814             */
11815             if (*s == '_') {
11816                 if (lastub && s == lastub + 1)
11817                     WARN_ABOUT_UNDERSCORE();
11818                 lastub = s++;
11819             }
11820             else {
11821                 /* check for end of fixed-length buffer */
11822                 if (d >= e)
11823                     Perl_croak(aTHX_ "%s", number_too_long);
11824                 /* if we're ok, copy the character */
11825                 *d++ = *s++;
11826             }
11827         }
11828
11829         /* final misplaced underbar check */
11830         if (lastub && s == lastub + 1)
11831             WARN_ABOUT_UNDERSCORE();
11832
11833         /* read a decimal portion if there is one.  avoid
11834            3..5 being interpreted as the number 3. followed
11835            by .5
11836         */
11837         if (*s == '.' && s[1] != '.') {
11838             floatit = TRUE;
11839             *d++ = *s++;
11840
11841             if (*s == '_') {
11842                 WARN_ABOUT_UNDERSCORE();
11843                 lastub = s;
11844             }
11845
11846             /* copy, ignoring underbars, until we run out of digits.
11847             */
11848             for (; isDIGIT(*s)
11849                    || *s == '_'
11850                    || UNLIKELY(hexfp && isXDIGIT(*s));
11851                  s++)
11852             {
11853                 /* fixed length buffer check */
11854                 if (d >= e)
11855                     Perl_croak(aTHX_ "%s", number_too_long);
11856                 if (*s == '_') {
11857                    if (lastub && s == lastub + 1)
11858                         WARN_ABOUT_UNDERSCORE();
11859                    lastub = s;
11860                 }
11861                 else
11862                     *d++ = *s;
11863             }
11864             /* fractional part ending in underbar? */
11865             if (s[-1] == '_')
11866                 WARN_ABOUT_UNDERSCORE();
11867             if (*s == '.' && isDIGIT(s[1])) {
11868                 /* oops, it's really a v-string, but without the "v" */
11869                 s = start;
11870                 goto vstring;
11871             }
11872         }
11873
11874         /* read exponent part, if present */
11875         if ((isALPHA_FOLD_EQ(*s, 'e')
11876               || UNLIKELY(hexfp && isALPHA_FOLD_EQ(*s, 'p')))
11877             && memCHRs("+-0123456789_", s[1]))
11878         {
11879             int exp_digits = 0;
11880             const char *save_s = s;
11881             char * save_d = d;
11882
11883             /* regardless of whether user said 3E5 or 3e5, use lower 'e',
11884                ditto for p (hexfloats) */
11885             if ((isALPHA_FOLD_EQ(*s, 'e'))) {
11886                 /* At least some Mach atof()s don't grok 'E' */
11887                 *d++ = 'e';
11888             }
11889             else if (UNLIKELY(hexfp && (isALPHA_FOLD_EQ(*s, 'p')))) {
11890                 *d++ = 'p';
11891             }
11892
11893             s++;
11894
11895
11896             /* stray preinitial _ */
11897             if (*s == '_') {
11898                 WARN_ABOUT_UNDERSCORE();
11899                 lastub = s++;
11900             }
11901
11902             /* allow positive or negative exponent */
11903             if (*s == '+' || *s == '-')
11904                 *d++ = *s++;
11905
11906             /* stray initial _ */
11907             if (*s == '_') {
11908                 WARN_ABOUT_UNDERSCORE();
11909                 lastub = s++;
11910             }
11911
11912             /* read digits of exponent */
11913             while (isDIGIT(*s) || *s == '_') {
11914                 if (isDIGIT(*s)) {
11915                     ++exp_digits;
11916                     if (d >= e)
11917                         Perl_croak(aTHX_ "%s", number_too_long);
11918                     *d++ = *s++;
11919                 }
11920                 else {
11921                    if (((lastub && s == lastub + 1)
11922                         || (!isDIGIT(s[1]) && s[1] != '_')))
11923                         WARN_ABOUT_UNDERSCORE();
11924                    lastub = s++;
11925                 }
11926             }
11927
11928             if (!exp_digits) {
11929                 /* no exponent digits, the [eEpP] could be for something else,
11930                  * though in practice we don't get here for p since that's preparsed
11931                  * earlier, and results in only the 0xX being consumed, so behave similarly
11932                  * for decimal floats and consume only the D.DD, leaving the [eE] to the
11933                  * next token.
11934                  */
11935                 s = save_s;
11936                 d = save_d;
11937             }
11938             else {
11939                 floatit = TRUE;
11940             }
11941         }
11942
11943
11944         /*
11945            We try to do an integer conversion first if no characters
11946            indicating "float" have been found.
11947          */
11948
11949         if (!floatit) {
11950             UV uv;
11951             const int flags = grok_number (PL_tokenbuf, d - PL_tokenbuf, &uv);
11952
11953             if (flags == IS_NUMBER_IN_UV) {
11954               if (uv <= IV_MAX)
11955                 sv = newSViv(uv); /* Prefer IVs over UVs. */
11956               else
11957                 sv = newSVuv(uv);
11958             } else if (flags == (IS_NUMBER_IN_UV | IS_NUMBER_NEG)) {
11959               if (uv <= (UV) IV_MIN)
11960                 sv = newSViv(-(IV)uv);
11961               else
11962                 floatit = TRUE;
11963             } else
11964               floatit = TRUE;
11965         }
11966         if (floatit) {
11967             /* terminate the string */
11968             *d = '\0';
11969             if (UNLIKELY(hexfp)) {
11970 #  ifdef NV_MANT_DIG
11971                 if (significant_bits > NV_MANT_DIG)
11972                     Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
11973                                    "Hexadecimal float: mantissa overflow");
11974 #  endif
11975 #ifdef HEXFP_UQUAD
11976                 nv = hexfp_uquad * hexfp_mult;
11977 #else /* HEXFP_NV */
11978                 nv = hexfp_nv * hexfp_mult;
11979 #endif
11980             } else {
11981                 nv = Atof(PL_tokenbuf);
11982             }
11983             sv = newSVnv(nv);
11984         }
11985
11986         if ( floatit
11987              ? (PL_hints & HINT_NEW_FLOAT) : (PL_hints & HINT_NEW_INTEGER) ) {
11988             const char *const key = floatit ? "float" : "integer";
11989             const STRLEN keylen = floatit ? 5 : 7;
11990             sv = S_new_constant(aTHX_ PL_tokenbuf, d - PL_tokenbuf,
11991                                 key, keylen, sv, NULL, NULL, 0, NULL);
11992         }
11993         break;
11994
11995     /* if it starts with a v, it could be a v-string */
11996     case 'v':
11997     vstring:
11998                 sv = newSV(5); /* preallocate storage space */
11999                 ENTER_with_name("scan_vstring");
12000                 SAVEFREESV(sv);
12001                 s = scan_vstring(s, PL_bufend, sv);
12002                 SvREFCNT_inc_simple_void_NN(sv);
12003                 LEAVE_with_name("scan_vstring");
12004         break;
12005     }
12006
12007     /* make the op for the constant and return */
12008
12009     if (sv)
12010         lvalp->opval = newSVOP(OP_CONST, 0, sv);
12011     else
12012         lvalp->opval = NULL;
12013
12014     return (char *)s;
12015 }
12016
12017 STATIC char *
12018 S_scan_formline(pTHX_ char *s)
12019 {
12020     SV * const stuff = newSVpvs("");
12021     bool needargs = FALSE;
12022     bool eofmt = FALSE;
12023
12024     PERL_ARGS_ASSERT_SCAN_FORMLINE;
12025
12026     while (!needargs) {
12027         char *eol;
12028         if (*s == '.') {
12029             char *t = s+1;
12030 #ifdef PERL_STRICT_CR
12031             while (SPACE_OR_TAB(*t))
12032                 t++;
12033 #else
12034             while (SPACE_OR_TAB(*t) || *t == '\r')
12035                 t++;
12036 #endif
12037             if (*t == '\n' || t == PL_bufend) {
12038                 eofmt = TRUE;
12039                 break;
12040             }
12041         }
12042         eol = (char *) memchr(s,'\n',PL_bufend-s);
12043         if (!eol++)
12044                 eol = PL_bufend;
12045         if (*s != '#') {
12046             char *t;
12047             for (t = s; t < eol; t++) {
12048                 if (*t == '~' && t[1] == '~' && SvCUR(stuff)) {
12049                     needargs = FALSE;
12050                     goto enough;        /* ~~ must be first line in formline */
12051                 }
12052                 if (*t == '@' || *t == '^')
12053                     needargs = TRUE;
12054             }
12055             if (eol > s) {
12056                 sv_catpvn(stuff, s, eol-s);
12057 #ifndef PERL_STRICT_CR
12058                 if (eol-s > 1 && eol[-2] == '\r' && eol[-1] == '\n') {
12059                     char *end = SvPVX(stuff) + SvCUR(stuff);
12060                     end[-2] = '\n';
12061                     end[-1] = '\0';
12062                     SvCUR_set(stuff, SvCUR(stuff) - 1);
12063                 }
12064 #endif
12065             }
12066             else
12067               break;
12068         }
12069         s = (char*)eol;
12070         if ((PL_rsfp || PL_parser->filtered)
12071          && PL_parser->form_lex_state == LEX_NORMAL) {
12072             bool got_some;
12073             PL_bufptr = PL_bufend;
12074             COPLINE_INC_WITH_HERELINES;
12075             got_some = lex_next_chunk(0);
12076             CopLINE_dec(PL_curcop);
12077             s = PL_bufptr;
12078             if (!got_some)
12079                 break;
12080         }
12081         incline(s, PL_bufend);
12082     }
12083   enough:
12084     if (!SvCUR(stuff) || needargs)
12085         PL_lex_state = PL_parser->form_lex_state;
12086     if (SvCUR(stuff)) {
12087         PL_expect = XSTATE;
12088         if (needargs) {
12089             const char *s2 = s;
12090             while (isSPACE(*s2) && *s2 != '\n')
12091                 s2++;
12092             if (*s2 == '{') {
12093                 PL_expect = XTERMBLOCK;
12094                 NEXTVAL_NEXTTOKE.ival = 0;
12095                 force_next(DO);
12096             }
12097             NEXTVAL_NEXTTOKE.ival = 0;
12098             force_next(FORMLBRACK);
12099         }
12100         if (!IN_BYTES) {
12101             if (UTF && is_utf8_string((U8*)SvPVX_const(stuff), SvCUR(stuff)))
12102                 SvUTF8_on(stuff);
12103         }
12104         NEXTVAL_NEXTTOKE.opval = newSVOP(OP_CONST, 0, stuff);
12105         force_next(THING);
12106     }
12107     else {
12108         SvREFCNT_dec(stuff);
12109         if (eofmt)
12110             PL_lex_formbrack = 0;
12111     }
12112     return s;
12113 }
12114
12115 I32
12116 Perl_start_subparse(pTHX_ I32 is_format, U32 flags)
12117 {
12118     const I32 oldsavestack_ix = PL_savestack_ix;
12119     CV* const outsidecv = PL_compcv;
12120
12121     SAVEI32(PL_subline);
12122     save_item(PL_subname);
12123     SAVESPTR(PL_compcv);
12124
12125     PL_compcv = MUTABLE_CV(newSV_type(is_format ? SVt_PVFM : SVt_PVCV));
12126     CvFLAGS(PL_compcv) |= flags;
12127
12128     PL_subline = CopLINE(PL_curcop);
12129     CvPADLIST(PL_compcv) = pad_new(padnew_SAVE|padnew_SAVESUB);
12130     CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outsidecv));
12131     CvOUTSIDE_SEQ(PL_compcv) = PL_cop_seqmax;
12132     if (outsidecv && CvPADLIST(outsidecv))
12133         CvPADLIST(PL_compcv)->xpadl_outid = CvPADLIST(outsidecv)->xpadl_id;
12134
12135     return oldsavestack_ix;
12136 }
12137
12138
12139 /* Do extra initialisation of a CV (typically one just created by
12140  * start_subparse()) if that CV is for a named sub
12141  */
12142
12143 void
12144 Perl_init_named_cv(pTHX_ CV *cv, OP *nameop)
12145 {
12146     PERL_ARGS_ASSERT_INIT_NAMED_CV;
12147
12148     if (nameop->op_type == OP_CONST) {
12149         const char *const name = SvPV_nolen_const(((SVOP*)nameop)->op_sv);
12150         if (   strEQ(name, "BEGIN")
12151             || strEQ(name, "END")
12152             || strEQ(name, "INIT")
12153             || strEQ(name, "CHECK")
12154             || strEQ(name, "UNITCHECK")
12155         )
12156           CvSPECIAL_on(cv);
12157     }
12158     else
12159     /* State subs inside anonymous subs need to be
12160      clonable themselves. */
12161     if (   CvANON(CvOUTSIDE(cv))
12162         || CvCLONE(CvOUTSIDE(cv))
12163         || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
12164                         CvOUTSIDE(cv)
12165                      ))[nameop->op_targ])
12166     )
12167       CvCLONE_on(cv);
12168 }
12169
12170
12171 static int
12172 S_yywarn(pTHX_ const char *const s, U32 flags)
12173 {
12174     PERL_ARGS_ASSERT_YYWARN;
12175
12176     PL_in_eval |= EVAL_WARNONLY;
12177     yyerror_pv(s, flags);
12178     return 0;
12179 }
12180
12181 void
12182 Perl_abort_execution(pTHX_ const char * const msg, const char * const name)
12183 {
12184     PERL_ARGS_ASSERT_ABORT_EXECUTION;
12185
12186     if (PL_minus_c)
12187         Perl_croak(aTHX_ "%s%s had compilation errors.\n", msg, name);
12188     else {
12189         Perl_croak(aTHX_
12190                 "%sExecution of %s aborted due to compilation errors.\n", msg, name);
12191     }
12192     NOT_REACHED; /* NOTREACHED */
12193 }
12194
12195 void
12196 Perl_yyquit(pTHX)
12197 {
12198     /* Called, after at least one error has been found, to abort the parse now,
12199      * instead of trying to forge ahead */
12200
12201     yyerror_pvn(NULL, 0, 0);
12202 }
12203
12204 int
12205 Perl_yyerror(pTHX_ const char *const s)
12206 {
12207     PERL_ARGS_ASSERT_YYERROR;
12208     return yyerror_pvn(s, strlen(s), 0);
12209 }
12210
12211 int
12212 Perl_yyerror_pv(pTHX_ const char *const s, U32 flags)
12213 {
12214     PERL_ARGS_ASSERT_YYERROR_PV;
12215     return yyerror_pvn(s, strlen(s), flags);
12216 }
12217
12218 int
12219 Perl_yyerror_pvn(pTHX_ const char *const s, STRLEN len, U32 flags)
12220 {
12221     const char *context = NULL;
12222     int contlen = -1;
12223     SV *msg;
12224     SV * const where_sv = newSVpvs_flags("", SVs_TEMP);
12225     int yychar  = PL_parser->yychar;
12226
12227     /* Output error message 's' with length 'len'.  'flags' are SV flags that
12228      * apply.  If the number of errors found is large enough, it abandons
12229      * parsing.  If 's' is NULL, there is no message, and it abandons
12230      * processing unconditionally */
12231
12232     if (s != NULL) {
12233         if (!yychar || (yychar == ';' && !PL_rsfp))
12234             sv_catpvs(where_sv, "at EOF");
12235         else if (   PL_oldoldbufptr
12236                  && PL_bufptr > PL_oldoldbufptr
12237                  && PL_bufptr - PL_oldoldbufptr < 200
12238                  && PL_oldoldbufptr != PL_oldbufptr
12239                  && PL_oldbufptr != PL_bufptr)
12240         {
12241             /*
12242                     Only for NetWare:
12243                     The code below is removed for NetWare because it
12244                     abends/crashes on NetWare when the script has error such as
12245                     not having the closing quotes like:
12246                         if ($var eq "value)
12247                     Checking of white spaces is anyway done in NetWare code.
12248             */
12249 #ifndef NETWARE
12250             while (isSPACE(*PL_oldoldbufptr))
12251                 PL_oldoldbufptr++;
12252 #endif
12253             context = PL_oldoldbufptr;
12254             contlen = PL_bufptr - PL_oldoldbufptr;
12255         }
12256         else if (  PL_oldbufptr
12257                 && PL_bufptr > PL_oldbufptr
12258                 && PL_bufptr - PL_oldbufptr < 200
12259                 && PL_oldbufptr != PL_bufptr) {
12260             /*
12261                     Only for NetWare:
12262                     The code below is removed for NetWare because it
12263                     abends/crashes on NetWare when the script has error such as
12264                     not having the closing quotes like:
12265                         if ($var eq "value)
12266                     Checking of white spaces is anyway done in NetWare code.
12267             */
12268 #ifndef NETWARE
12269             while (isSPACE(*PL_oldbufptr))
12270                 PL_oldbufptr++;
12271 #endif
12272             context = PL_oldbufptr;
12273             contlen = PL_bufptr - PL_oldbufptr;
12274         }
12275         else if (yychar > 255)
12276             sv_catpvs(where_sv, "next token ???");
12277         else if (yychar == YYEMPTY) {
12278             if (PL_lex_state == LEX_NORMAL)
12279                 sv_catpvs(where_sv, "at end of line");
12280             else if (PL_lex_inpat)
12281                 sv_catpvs(where_sv, "within pattern");
12282             else
12283                 sv_catpvs(where_sv, "within string");
12284         }
12285         else {
12286             sv_catpvs(where_sv, "next char ");
12287             if (yychar < 32)
12288                 Perl_sv_catpvf(aTHX_ where_sv, "^%c", toCTRL(yychar));
12289             else if (isPRINT_LC(yychar)) {
12290                 const char string = yychar;
12291                 sv_catpvn(where_sv, &string, 1);
12292             }
12293             else
12294                 Perl_sv_catpvf(aTHX_ where_sv, "\\%03o", yychar & 255);
12295         }
12296         msg = newSVpvn_flags(s, len, (flags & SVf_UTF8) | SVs_TEMP);
12297         Perl_sv_catpvf(aTHX_ msg, " at %s line %" IVdf ", ",
12298             OutCopFILE(PL_curcop),
12299             (IV)(PL_parser->preambling == NOLINE
12300                    ? CopLINE(PL_curcop)
12301                    : PL_parser->preambling));
12302         if (context)
12303             Perl_sv_catpvf(aTHX_ msg, "near \"%" UTF8f "\"\n",
12304                                  UTF8fARG(UTF, contlen, context));
12305         else
12306             Perl_sv_catpvf(aTHX_ msg, "%" SVf "\n", SVfARG(where_sv));
12307         if (   PL_multi_start < PL_multi_end
12308             && (U32)(CopLINE(PL_curcop) - PL_multi_end) <= 1)
12309         {
12310             Perl_sv_catpvf(aTHX_ msg,
12311             "  (Might be a runaway multi-line %c%c string starting on"
12312             " line %" IVdf ")\n",
12313                     (int)PL_multi_open,(int)PL_multi_close,(IV)PL_multi_start);
12314             PL_multi_end = 0;
12315         }
12316         if (PL_in_eval & EVAL_WARNONLY) {
12317             PL_in_eval &= ~EVAL_WARNONLY;
12318             Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX), "%" SVf, SVfARG(msg));
12319         }
12320         else {
12321             qerror(msg);
12322         }
12323     }
12324     if (s == NULL || PL_error_count >= 10) {
12325         const char * msg = "";
12326         const char * const name = OutCopFILE(PL_curcop);
12327
12328         if (PL_in_eval) {
12329             SV * errsv = ERRSV;
12330             if (SvCUR(errsv)) {
12331                 msg = Perl_form(aTHX_ "%" SVf, SVfARG(errsv));
12332             }
12333         }
12334
12335         if (s == NULL) {
12336             abort_execution(msg, name);
12337         }
12338         else {
12339             Perl_croak(aTHX_ "%s%s has too many errors.\n", msg, name);
12340         }
12341     }
12342     PL_in_my = 0;
12343     PL_in_my_stash = NULL;
12344     return 0;
12345 }
12346
12347 STATIC char*
12348 S_swallow_bom(pTHX_ U8 *s)
12349 {
12350     const STRLEN slen = SvCUR(PL_linestr);
12351
12352     PERL_ARGS_ASSERT_SWALLOW_BOM;
12353
12354     switch (s[0]) {
12355     case 0xFF:
12356         if (s[1] == 0xFE) {
12357             /* UTF-16 little-endian? (or UTF-32LE?) */
12358             if (s[2] == 0 && s[3] == 0)  /* UTF-32 little-endian */
12359                 /* diag_listed_as: Unsupported script encoding %s */
12360                 Perl_croak(aTHX_ "Unsupported script encoding UTF-32LE");
12361 #ifndef PERL_NO_UTF16_FILTER
12362 #ifdef DEBUGGING
12363             if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (BOM)\n");
12364 #endif
12365             s += 2;
12366             if (PL_bufend > (char*)s) {
12367                 s = add_utf16_textfilter(s, TRUE);
12368             }
12369 #else
12370             /* diag_listed_as: Unsupported script encoding %s */
12371             Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
12372 #endif
12373         }
12374         break;
12375     case 0xFE:
12376         if (s[1] == 0xFF) {   /* UTF-16 big-endian? */
12377 #ifndef PERL_NO_UTF16_FILTER
12378 #ifdef DEBUGGING
12379             if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (BOM)\n");
12380 #endif
12381             s += 2;
12382             if (PL_bufend > (char *)s) {
12383                 s = add_utf16_textfilter(s, FALSE);
12384             }
12385 #else
12386             /* diag_listed_as: Unsupported script encoding %s */
12387             Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
12388 #endif
12389         }
12390         break;
12391     case BOM_UTF8_FIRST_BYTE: {
12392         if (memBEGINs(s+1, slen - 1, BOM_UTF8_TAIL)) {
12393 #ifdef DEBUGGING
12394             if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-8 script encoding (BOM)\n");
12395 #endif
12396             s += sizeof(BOM_UTF8) - 1;                     /* UTF-8 */
12397         }
12398         break;
12399     }
12400     case 0:
12401         if (slen > 3) {
12402              if (s[1] == 0) {
12403                   if (s[2] == 0xFE && s[3] == 0xFF) {
12404                        /* UTF-32 big-endian */
12405                        /* diag_listed_as: Unsupported script encoding %s */
12406                        Perl_croak(aTHX_ "Unsupported script encoding UTF-32BE");
12407                   }
12408              }
12409              else if (s[2] == 0 && s[3] != 0) {
12410                   /* Leading bytes
12411                    * 00 xx 00 xx
12412                    * are a good indicator of UTF-16BE. */
12413 #ifndef PERL_NO_UTF16_FILTER
12414 #ifdef DEBUGGING
12415                   if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (no BOM)\n");
12416 #endif
12417                   s = add_utf16_textfilter(s, FALSE);
12418 #else
12419                   /* diag_listed_as: Unsupported script encoding %s */
12420                   Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
12421 #endif
12422              }
12423         }
12424         break;
12425
12426     default:
12427          if (slen > 3 && s[1] == 0 && s[2] != 0 && s[3] == 0) {
12428                   /* Leading bytes
12429                    * xx 00 xx 00
12430                    * are a good indicator of UTF-16LE. */
12431 #ifndef PERL_NO_UTF16_FILTER
12432 #ifdef DEBUGGING
12433               if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (no BOM)\n");
12434 #endif
12435               s = add_utf16_textfilter(s, TRUE);
12436 #else
12437               /* diag_listed_as: Unsupported script encoding %s */
12438               Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
12439 #endif
12440          }
12441     }
12442     return (char*)s;
12443 }
12444
12445
12446 #ifndef PERL_NO_UTF16_FILTER
12447 static I32
12448 S_utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen)
12449 {
12450     SV *const filter = FILTER_DATA(idx);
12451     /* We re-use this each time round, throwing the contents away before we
12452        return.  */
12453     SV *const utf16_buffer = MUTABLE_SV(IoTOP_GV(filter));
12454     SV *const utf8_buffer = filter;
12455     IV status = IoPAGE(filter);
12456     const bool reverse = cBOOL(IoLINES(filter));
12457     I32 retval;
12458
12459     PERL_ARGS_ASSERT_UTF16_TEXTFILTER;
12460
12461     /* As we're automatically added, at the lowest level, and hence only called
12462        from this file, we can be sure that we're not called in block mode. Hence
12463        don't bother writing code to deal with block mode.  */
12464     if (maxlen) {
12465         Perl_croak(aTHX_ "panic: utf16_textfilter called in block mode (for %d characters)", maxlen);
12466     }
12467     if (status < 0) {
12468         Perl_croak(aTHX_ "panic: utf16_textfilter called after error (status=%" IVdf ")", status);
12469     }
12470     DEBUG_P(PerlIO_printf(Perl_debug_log,
12471                           "utf16_textfilter(%p,%ce): idx=%d maxlen=%d status=%" IVdf " utf16=%" UVuf " utf8=%" UVuf "\n",
12472                           FPTR2DPTR(void *, S_utf16_textfilter),
12473                           reverse ? 'l' : 'b', idx, maxlen, status,
12474                           (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
12475
12476     while (1) {
12477         STRLEN chars;
12478         STRLEN have;
12479         Size_t newlen;
12480         U8 *end;
12481         /* First, look in our buffer of existing UTF-8 data:  */
12482         char *nl = (char *)memchr(SvPVX(utf8_buffer), '\n', SvCUR(utf8_buffer));
12483
12484         if (nl) {
12485             ++nl;
12486         } else if (status == 0) {
12487             /* EOF */
12488             IoPAGE(filter) = 0;
12489             nl = SvEND(utf8_buffer);
12490         }
12491         if (nl) {
12492             STRLEN got = nl - SvPVX(utf8_buffer);
12493             /* Did we have anything to append?  */
12494             retval = got != 0;
12495             sv_catpvn(sv, SvPVX(utf8_buffer), got);
12496             /* Everything else in this code works just fine if SVp_POK isn't
12497                set.  This, however, needs it, and we need it to work, else
12498                we loop infinitely because the buffer is never consumed.  */
12499             sv_chop(utf8_buffer, nl);
12500             break;
12501         }
12502
12503         /* OK, not a complete line there, so need to read some more UTF-16.
12504            Read an extra octect if the buffer currently has an odd number. */
12505         while (1) {
12506             if (status <= 0)
12507                 break;
12508             if (SvCUR(utf16_buffer) >= 2) {
12509                 /* Location of the high octet of the last complete code point.
12510                    Gosh, UTF-16 is a pain. All the benefits of variable length,
12511                    *coupled* with all the benefits of partial reads and
12512                    endianness.  */
12513                 const U8 *const last_hi = (U8*)SvPVX(utf16_buffer)
12514                     + ((SvCUR(utf16_buffer) & ~1) - (reverse ? 1 : 2));
12515
12516                 if (*last_hi < 0xd8 || *last_hi > 0xdb) {
12517                     break;
12518                 }
12519
12520                 /* We have the first half of a surrogate. Read more.  */
12521                 DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter partial surrogate detected at %p\n", last_hi));
12522             }
12523
12524             status = FILTER_READ(idx + 1, utf16_buffer,
12525                                  160 + (SvCUR(utf16_buffer) & 1));
12526             DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter status=%" IVdf " SvCUR(sv)=%" UVuf "\n", status, (UV)SvCUR(utf16_buffer)));
12527             DEBUG_P({ sv_dump(utf16_buffer); sv_dump(utf8_buffer);});
12528             if (status < 0) {
12529                 /* Error */
12530                 IoPAGE(filter) = status;
12531                 return status;
12532             }
12533         }
12534
12535         /* 'chars' isn't quite the right name, as code points above 0xFFFF
12536          * require 4 bytes per char */
12537         chars = SvCUR(utf16_buffer) >> 1;
12538         have = SvCUR(utf8_buffer);
12539
12540         /* Assume the worst case size as noted by the functions: twice the
12541          * number of input bytes */
12542         SvGROW(utf8_buffer, have + chars * 4 + 1);
12543
12544         if (reverse) {
12545             end = utf16_to_utf8_reversed((U8*)SvPVX(utf16_buffer),
12546                                          (U8*)SvPVX_const(utf8_buffer) + have,
12547                                          chars * 2, &newlen);
12548         } else {
12549             end = utf16_to_utf8((U8*)SvPVX(utf16_buffer),
12550                                 (U8*)SvPVX_const(utf8_buffer) + have,
12551                                 chars * 2, &newlen);
12552         }
12553         SvCUR_set(utf8_buffer, have + newlen);
12554         *end = '\0';
12555
12556         /* No need to keep this SV "well-formed" with a '\0' after the end, as
12557            it's private to us, and utf16_to_utf8{,reversed} take a
12558            (pointer,length) pair, rather than a NUL-terminated string.  */
12559         if(SvCUR(utf16_buffer) & 1) {
12560             *SvPVX(utf16_buffer) = SvEND(utf16_buffer)[-1];
12561             SvCUR_set(utf16_buffer, 1);
12562         } else {
12563             SvCUR_set(utf16_buffer, 0);
12564         }
12565     }
12566     DEBUG_P(PerlIO_printf(Perl_debug_log,
12567                           "utf16_textfilter: returns, status=%" IVdf " utf16=%" UVuf " utf8=%" UVuf "\n",
12568                           status,
12569                           (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
12570     DEBUG_P({ sv_dump(utf8_buffer); sv_dump(sv);});
12571     return retval;
12572 }
12573
12574 static U8 *
12575 S_add_utf16_textfilter(pTHX_ U8 *const s, bool reversed)
12576 {
12577     SV *filter = filter_add(S_utf16_textfilter, NULL);
12578
12579     PERL_ARGS_ASSERT_ADD_UTF16_TEXTFILTER;
12580
12581     IoTOP_GV(filter) = MUTABLE_GV(newSVpvn((char *)s, PL_bufend - (char*)s));
12582     SvPVCLEAR(filter);
12583     IoLINES(filter) = reversed;
12584     IoPAGE(filter) = 1; /* Not EOF */
12585
12586     /* Sadly, we have to return a valid pointer, come what may, so we have to
12587        ignore any error return from this.  */
12588     SvCUR_set(PL_linestr, 0);
12589     if (FILTER_READ(0, PL_linestr, 0)) {
12590         SvUTF8_on(PL_linestr);
12591     } else {
12592         SvUTF8_on(PL_linestr);
12593     }
12594     PL_bufend = SvEND(PL_linestr);
12595     return (U8*)SvPVX(PL_linestr);
12596 }
12597 #endif
12598
12599 /*
12600 Returns a pointer to the next character after the parsed
12601 vstring, as well as updating the passed in sv.
12602
12603 Function must be called like
12604
12605         sv = sv_2mortal(newSV(5));
12606         s = scan_vstring(s,e,sv);
12607
12608 where s and e are the start and end of the string.
12609 The sv should already be large enough to store the vstring
12610 passed in, for performance reasons.
12611
12612 This function may croak if fatal warnings are enabled in the
12613 calling scope, hence the sv_2mortal in the example (to prevent
12614 a leak).  Make sure to do SvREFCNT_inc afterwards if you use
12615 sv_2mortal.
12616
12617 */
12618
12619 char *
12620 Perl_scan_vstring(pTHX_ const char *s, const char *const e, SV *sv)
12621 {
12622     const char *pos = s;
12623     const char *start = s;
12624
12625     PERL_ARGS_ASSERT_SCAN_VSTRING;
12626
12627     if (*pos == 'v') pos++;  /* get past 'v' */
12628     while (pos < e && (isDIGIT(*pos) || *pos == '_'))
12629         pos++;
12630     if ( *pos != '.') {
12631         /* this may not be a v-string if followed by => */
12632         const char *next = pos;
12633         while (next < e && isSPACE(*next))
12634             ++next;
12635         if ((e - next) >= 2 && *next == '=' && next[1] == '>' ) {
12636             /* return string not v-string */
12637             sv_setpvn(sv,(char *)s,pos-s);
12638             return (char *)pos;
12639         }
12640     }
12641
12642     if (!isALPHA(*pos)) {
12643         U8 tmpbuf[UTF8_MAXBYTES+1];
12644
12645         if (*s == 'v')
12646             s++;  /* get past 'v' */
12647
12648         SvPVCLEAR(sv);
12649
12650         for (;;) {
12651             /* this is atoi() that tolerates underscores */
12652             U8 *tmpend;
12653             UV rev = 0;
12654             const char *end = pos;
12655             UV mult = 1;
12656             while (--end >= s) {
12657                 if (*end != '_') {
12658                     const UV orev = rev;
12659                     rev += (*end - '0') * mult;
12660                     mult *= 10;
12661                     if (orev > rev)
12662                         /* diag_listed_as: Integer overflow in %s number */
12663                         Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
12664                                          "Integer overflow in decimal number");
12665                 }
12666             }
12667
12668             /* Append native character for the rev point */
12669             tmpend = uvchr_to_utf8(tmpbuf, rev);
12670             sv_catpvn(sv, (const char*)tmpbuf, tmpend - tmpbuf);
12671             if (!UVCHR_IS_INVARIANT(rev))
12672                  SvUTF8_on(sv);
12673             if (pos + 1 < e && *pos == '.' && isDIGIT(pos[1]))
12674                  s = ++pos;
12675             else {
12676                  s = pos;
12677                  break;
12678             }
12679             while (pos < e && (isDIGIT(*pos) || *pos == '_'))
12680                  pos++;
12681         }
12682         SvPOK_on(sv);
12683         sv_magic(sv,NULL,PERL_MAGIC_vstring,(const char*)start, pos-start);
12684         SvRMAGICAL_on(sv);
12685     }
12686     return (char *)s;
12687 }
12688
12689 int
12690 Perl_keyword_plugin_standard(pTHX_
12691         char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
12692 {
12693     PERL_ARGS_ASSERT_KEYWORD_PLUGIN_STANDARD;
12694     PERL_UNUSED_CONTEXT;
12695     PERL_UNUSED_ARG(keyword_ptr);
12696     PERL_UNUSED_ARG(keyword_len);
12697     PERL_UNUSED_ARG(op_ptr);
12698     return KEYWORD_PLUGIN_DECLINE;
12699 }
12700
12701 /*
12702 =for apidoc wrap_keyword_plugin
12703
12704 Puts a C function into the chain of keyword plugins.  This is the
12705 preferred way to manipulate the L</PL_keyword_plugin> variable.
12706 C<new_plugin> is a pointer to the C function that is to be added to the
12707 keyword plugin chain, and C<old_plugin_p> points to the storage location
12708 where a pointer to the next function in the chain will be stored.  The
12709 value of C<new_plugin> is written into the L</PL_keyword_plugin> variable,
12710 while the value previously stored there is written to C<*old_plugin_p>.
12711
12712 L</PL_keyword_plugin> is global to an entire process, and a module wishing
12713 to hook keyword parsing may find itself invoked more than once per
12714 process, typically in different threads.  To handle that situation, this
12715 function is idempotent.  The location C<*old_plugin_p> must initially
12716 (once per process) contain a null pointer.  A C variable of static
12717 duration (declared at file scope, typically also marked C<static> to give
12718 it internal linkage) will be implicitly initialised appropriately, if it
12719 does not have an explicit initialiser.  This function will only actually
12720 modify the plugin chain if it finds C<*old_plugin_p> to be null.  This
12721 function is also thread safe on the small scale.  It uses appropriate
12722 locking to avoid race conditions in accessing L</PL_keyword_plugin>.
12723
12724 When this function is called, the function referenced by C<new_plugin>
12725 must be ready to be called, except for C<*old_plugin_p> being unfilled.
12726 In a threading situation, C<new_plugin> may be called immediately, even
12727 before this function has returned.  C<*old_plugin_p> will always be
12728 appropriately set before C<new_plugin> is called.  If C<new_plugin>
12729 decides not to do anything special with the identifier that it is given
12730 (which is the usual case for most calls to a keyword plugin), it must
12731 chain the plugin function referenced by C<*old_plugin_p>.
12732
12733 Taken all together, XS code to install a keyword plugin should typically
12734 look something like this:
12735
12736     static Perl_keyword_plugin_t next_keyword_plugin;
12737     static OP *my_keyword_plugin(pTHX_
12738         char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
12739     {
12740         if (memEQs(keyword_ptr, keyword_len,
12741                    "my_new_keyword")) {
12742             ...
12743         } else {
12744             return next_keyword_plugin(aTHX_
12745                 keyword_ptr, keyword_len, op_ptr);
12746         }
12747     }
12748     BOOT:
12749         wrap_keyword_plugin(my_keyword_plugin,
12750                             &next_keyword_plugin);
12751
12752 Direct access to L</PL_keyword_plugin> should be avoided.
12753
12754 =cut
12755 */
12756
12757 void
12758 Perl_wrap_keyword_plugin(pTHX_
12759     Perl_keyword_plugin_t new_plugin, Perl_keyword_plugin_t *old_plugin_p)
12760 {
12761     dVAR;
12762
12763     PERL_UNUSED_CONTEXT;
12764     PERL_ARGS_ASSERT_WRAP_KEYWORD_PLUGIN;
12765     if (*old_plugin_p) return;
12766     KEYWORD_PLUGIN_MUTEX_LOCK;
12767     if (!*old_plugin_p) {
12768         *old_plugin_p = PL_keyword_plugin;
12769         PL_keyword_plugin = new_plugin;
12770     }
12771     KEYWORD_PLUGIN_MUTEX_UNLOCK;
12772 }
12773
12774 #define parse_recdescent(g,p) S_parse_recdescent(aTHX_ g,p)
12775 static void
12776 S_parse_recdescent(pTHX_ int gramtype, I32 fakeeof)
12777 {
12778     SAVEI32(PL_lex_brackets);
12779     if (PL_lex_brackets > 100)
12780         Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
12781     PL_lex_brackstack[PL_lex_brackets++] = XFAKEEOF;
12782     SAVEI32(PL_lex_allbrackets);
12783     PL_lex_allbrackets = 0;
12784     SAVEI8(PL_lex_fakeeof);
12785     PL_lex_fakeeof = (U8)fakeeof;
12786     if(yyparse(gramtype) && !PL_parser->error_count)
12787         qerror(Perl_mess(aTHX_ "Parse error"));
12788 }
12789
12790 #define parse_recdescent_for_op(g,p) S_parse_recdescent_for_op(aTHX_ g,p)
12791 static OP *
12792 S_parse_recdescent_for_op(pTHX_ int gramtype, I32 fakeeof)
12793 {
12794     OP *o;
12795     ENTER;
12796     SAVEVPTR(PL_eval_root);
12797     PL_eval_root = NULL;
12798     parse_recdescent(gramtype, fakeeof);
12799     o = PL_eval_root;
12800     LEAVE;
12801     return o;
12802 }
12803
12804 #define parse_expr(p,f) S_parse_expr(aTHX_ p,f)
12805 static OP *
12806 S_parse_expr(pTHX_ I32 fakeeof, U32 flags)
12807 {
12808     OP *exprop;
12809     if (flags & ~PARSE_OPTIONAL)
12810         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_expr");
12811     exprop = parse_recdescent_for_op(GRAMEXPR, fakeeof);
12812     if (!exprop && !(flags & PARSE_OPTIONAL)) {
12813         if (!PL_parser->error_count)
12814             qerror(Perl_mess(aTHX_ "Parse error"));
12815         exprop = newOP(OP_NULL, 0);
12816     }
12817     return exprop;
12818 }
12819
12820 /*
12821 =for apidoc parse_arithexpr
12822
12823 Parse a Perl arithmetic expression.  This may contain operators of precedence
12824 down to the bit shift operators.  The expression must be followed (and thus
12825 terminated) either by a comparison or lower-precedence operator or by
12826 something that would normally terminate an expression such as semicolon.
12827 If C<flags> has the C<PARSE_OPTIONAL> bit set, then the expression is optional,
12828 otherwise it is mandatory.  It is up to the caller to ensure that the
12829 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12830 the source of the code to be parsed and the lexical context for the
12831 expression.
12832
12833 The op tree representing the expression is returned.  If an optional
12834 expression is absent, a null pointer is returned, otherwise the pointer
12835 will be non-null.
12836
12837 If an error occurs in parsing or compilation, in most cases a valid op
12838 tree is returned anyway.  The error is reflected in the parser state,
12839 normally resulting in a single exception at the top level of parsing
12840 which covers all the compilation errors that occurred.  Some compilation
12841 errors, however, will throw an exception immediately.
12842
12843 =for apidoc Amnh||PARSE_OPTIONAL
12844
12845 =cut
12846
12847 */
12848
12849 OP *
12850 Perl_parse_arithexpr(pTHX_ U32 flags)
12851 {
12852     return parse_expr(LEX_FAKEEOF_COMPARE, flags);
12853 }
12854
12855 /*
12856 =for apidoc parse_termexpr
12857
12858 Parse a Perl term expression.  This may contain operators of precedence
12859 down to the assignment operators.  The expression must be followed (and thus
12860 terminated) either by a comma or lower-precedence operator or by
12861 something that would normally terminate an expression such as semicolon.
12862 If C<flags> has the C<PARSE_OPTIONAL> bit set, then the expression is optional,
12863 otherwise it is mandatory.  It is up to the caller to ensure that the
12864 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12865 the source of the code to be parsed and the lexical context for the
12866 expression.
12867
12868 The op tree representing the expression is returned.  If an optional
12869 expression is absent, a null pointer is returned, otherwise the pointer
12870 will be non-null.
12871
12872 If an error occurs in parsing or compilation, in most cases a valid op
12873 tree is returned anyway.  The error is reflected in the parser state,
12874 normally resulting in a single exception at the top level of parsing
12875 which covers all the compilation errors that occurred.  Some compilation
12876 errors, however, will throw an exception immediately.
12877
12878 =cut
12879 */
12880
12881 OP *
12882 Perl_parse_termexpr(pTHX_ U32 flags)
12883 {
12884     return parse_expr(LEX_FAKEEOF_COMMA, flags);
12885 }
12886
12887 /*
12888 =for apidoc parse_listexpr
12889
12890 Parse a Perl list expression.  This may contain operators of precedence
12891 down to the comma operator.  The expression must be followed (and thus
12892 terminated) either by a low-precedence logic operator such as C<or> or by
12893 something that would normally terminate an expression such as semicolon.
12894 If C<flags> has the C<PARSE_OPTIONAL> bit set, then the expression is optional,
12895 otherwise it is mandatory.  It is up to the caller to ensure that the
12896 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12897 the source of the code to be parsed and the lexical context for the
12898 expression.
12899
12900 The op tree representing the expression is returned.  If an optional
12901 expression is absent, a null pointer is returned, otherwise the pointer
12902 will be non-null.
12903
12904 If an error occurs in parsing or compilation, in most cases a valid op
12905 tree is returned anyway.  The error is reflected in the parser state,
12906 normally resulting in a single exception at the top level of parsing
12907 which covers all the compilation errors that occurred.  Some compilation
12908 errors, however, will throw an exception immediately.
12909
12910 =cut
12911 */
12912
12913 OP *
12914 Perl_parse_listexpr(pTHX_ U32 flags)
12915 {
12916     return parse_expr(LEX_FAKEEOF_LOWLOGIC, flags);
12917 }
12918
12919 /*
12920 =for apidoc parse_fullexpr
12921
12922 Parse a single complete Perl expression.  This allows the full
12923 expression grammar, including the lowest-precedence operators such
12924 as C<or>.  The expression must be followed (and thus terminated) by a
12925 token that an expression would normally be terminated by: end-of-file,
12926 closing bracketing punctuation, semicolon, or one of the keywords that
12927 signals a postfix expression-statement modifier.  If C<flags> has the
12928 C<PARSE_OPTIONAL> bit set, then the expression is optional, otherwise it is
12929 mandatory.  It is up to the caller to ensure that the dynamic parser
12930 state (L</PL_parser> et al) is correctly set to reflect the source of
12931 the code to be parsed and the lexical context for the expression.
12932
12933 The op tree representing the expression is returned.  If an optional
12934 expression is absent, a null pointer is returned, otherwise the pointer
12935 will be non-null.
12936
12937 If an error occurs in parsing or compilation, in most cases a valid op
12938 tree is returned anyway.  The error is reflected in the parser state,
12939 normally resulting in a single exception at the top level of parsing
12940 which covers all the compilation errors that occurred.  Some compilation
12941 errors, however, will throw an exception immediately.
12942
12943 =cut
12944 */
12945
12946 OP *
12947 Perl_parse_fullexpr(pTHX_ U32 flags)
12948 {
12949     return parse_expr(LEX_FAKEEOF_NONEXPR, flags);
12950 }
12951
12952 /*
12953 =for apidoc parse_block
12954
12955 Parse a single complete Perl code block.  This consists of an opening
12956 brace, a sequence of statements, and a closing brace.  The block
12957 constitutes a lexical scope, so C<my> variables and various compile-time
12958 effects can be contained within it.  It is up to the caller to ensure
12959 that the dynamic parser state (L</PL_parser> et al) is correctly set to
12960 reflect the source of the code to be parsed and the lexical context for
12961 the statement.
12962
12963 The op tree representing the code block is returned.  This is always a
12964 real op, never a null pointer.  It will normally be a C<lineseq> list,
12965 including C<nextstate> or equivalent ops.  No ops to construct any kind
12966 of runtime scope are included by virtue of it being a block.
12967
12968 If an error occurs in parsing or compilation, in most cases a valid op
12969 tree (most likely null) is returned anyway.  The error is reflected in
12970 the parser state, normally resulting in a single exception at the top
12971 level of parsing which covers all the compilation errors that occurred.
12972 Some compilation errors, however, will throw an exception immediately.
12973
12974 The C<flags> parameter is reserved for future use, and must always
12975 be zero.
12976
12977 =cut
12978 */
12979
12980 OP *
12981 Perl_parse_block(pTHX_ U32 flags)
12982 {
12983     if (flags)
12984         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_block");
12985     return parse_recdescent_for_op(GRAMBLOCK, LEX_FAKEEOF_NEVER);
12986 }
12987
12988 /*
12989 =for apidoc parse_barestmt
12990
12991 Parse a single unadorned Perl statement.  This may be a normal imperative
12992 statement or a declaration that has compile-time effect.  It does not
12993 include any label or other affixture.  It is up to the caller to ensure
12994 that the dynamic parser state (L</PL_parser> et al) is correctly set to
12995 reflect the source of the code to be parsed and the lexical context for
12996 the statement.
12997
12998 The op tree representing the statement is returned.  This may be a
12999 null pointer if the statement is null, for example if it was actually
13000 a subroutine definition (which has compile-time side effects).  If not
13001 null, it will be ops directly implementing the statement, suitable to
13002 pass to L</newSTATEOP>.  It will not normally include a C<nextstate> or
13003 equivalent op (except for those embedded in a scope contained entirely
13004 within the statement).
13005
13006 If an error occurs in parsing or compilation, in most cases a valid op
13007 tree (most likely null) is returned anyway.  The error is reflected in
13008 the parser state, normally resulting in a single exception at the top
13009 level of parsing which covers all the compilation errors that occurred.
13010 Some compilation errors, however, will throw an exception immediately.
13011
13012 The C<flags> parameter is reserved for future use, and must always
13013 be zero.
13014
13015 =cut
13016 */
13017
13018 OP *
13019 Perl_parse_barestmt(pTHX_ U32 flags)
13020 {
13021     if (flags)
13022         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_barestmt");
13023     return parse_recdescent_for_op(GRAMBARESTMT, LEX_FAKEEOF_NEVER);
13024 }
13025
13026 /*
13027 =for apidoc parse_label
13028
13029 Parse a single label, possibly optional, of the type that may prefix a
13030 Perl statement.  It is up to the caller to ensure that the dynamic parser
13031 state (L</PL_parser> et al) is correctly set to reflect the source of
13032 the code to be parsed.  If C<flags> has the C<PARSE_OPTIONAL> bit set, then the
13033 label is optional, otherwise it is mandatory.
13034
13035 The name of the label is returned in the form of a fresh scalar.  If an
13036 optional label is absent, a null pointer is returned.
13037
13038 If an error occurs in parsing, which can only occur if the label is
13039 mandatory, a valid label is returned anyway.  The error is reflected in
13040 the parser state, normally resulting in a single exception at the top
13041 level of parsing which covers all the compilation errors that occurred.
13042
13043 =cut
13044 */
13045
13046 SV *
13047 Perl_parse_label(pTHX_ U32 flags)
13048 {
13049     if (flags & ~PARSE_OPTIONAL)
13050         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_label");
13051     if (PL_nexttoke) {
13052         PL_parser->yychar = yylex();
13053         if (PL_parser->yychar == LABEL) {
13054             SV * const labelsv = cSVOPx(pl_yylval.opval)->op_sv;
13055             PL_parser->yychar = YYEMPTY;
13056             cSVOPx(pl_yylval.opval)->op_sv = NULL;
13057             op_free(pl_yylval.opval);
13058             return labelsv;
13059         } else {
13060             yyunlex();
13061             goto no_label;
13062         }
13063     } else {
13064         char *s, *t;
13065         STRLEN wlen, bufptr_pos;
13066         lex_read_space(0);
13067         t = s = PL_bufptr;
13068         if (!isIDFIRST_lazy_if_safe(s, PL_bufend, UTF))
13069             goto no_label;
13070         t = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &wlen);
13071         if (word_takes_any_delimiter(s, wlen))
13072             goto no_label;
13073         bufptr_pos = s - SvPVX(PL_linestr);
13074         PL_bufptr = t;
13075         lex_read_space(LEX_KEEP_PREVIOUS);
13076         t = PL_bufptr;
13077         s = SvPVX(PL_linestr) + bufptr_pos;
13078         if (t[0] == ':' && t[1] != ':') {
13079             PL_oldoldbufptr = PL_oldbufptr;
13080             PL_oldbufptr = s;
13081             PL_bufptr = t+1;
13082             return newSVpvn_flags(s, wlen, UTF ? SVf_UTF8 : 0);
13083         } else {
13084             PL_bufptr = s;
13085             no_label:
13086             if (flags & PARSE_OPTIONAL) {
13087                 return NULL;
13088             } else {
13089                 qerror(Perl_mess(aTHX_ "Parse error"));
13090                 return newSVpvs("x");
13091             }
13092         }
13093     }
13094 }
13095
13096 /*
13097 =for apidoc parse_fullstmt
13098
13099 Parse a single complete Perl statement.  This may be a normal imperative
13100 statement or a declaration that has compile-time effect, and may include
13101 optional labels.  It is up to the caller to ensure that the dynamic
13102 parser state (L</PL_parser> et al) is correctly set to reflect the source
13103 of the code to be parsed and the lexical context for the statement.
13104
13105 The op tree representing the statement is returned.  This may be a
13106 null pointer if the statement is null, for example if it was actually
13107 a subroutine definition (which has compile-time side effects).  If not
13108 null, it will be the result of a L</newSTATEOP> call, normally including
13109 a C<nextstate> or equivalent op.
13110
13111 If an error occurs in parsing or compilation, in most cases a valid op
13112 tree (most likely null) is returned anyway.  The error is reflected in
13113 the parser state, normally resulting in a single exception at the top
13114 level of parsing which covers all the compilation errors that occurred.
13115 Some compilation errors, however, will throw an exception immediately.
13116
13117 The C<flags> parameter is reserved for future use, and must always
13118 be zero.
13119
13120 =cut
13121 */
13122
13123 OP *
13124 Perl_parse_fullstmt(pTHX_ U32 flags)
13125 {
13126     if (flags)
13127         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_fullstmt");
13128     return parse_recdescent_for_op(GRAMFULLSTMT, LEX_FAKEEOF_NEVER);
13129 }
13130
13131 /*
13132 =for apidoc parse_stmtseq
13133
13134 Parse a sequence of zero or more Perl statements.  These may be normal
13135 imperative statements, including optional labels, or declarations
13136 that have compile-time effect, or any mixture thereof.  The statement
13137 sequence ends when a closing brace or end-of-file is encountered in a
13138 place where a new statement could have validly started.  It is up to
13139 the caller to ensure that the dynamic parser state (L</PL_parser> et al)
13140 is correctly set to reflect the source of the code to be parsed and the
13141 lexical context for the statements.
13142
13143 The op tree representing the statement sequence is returned.  This may
13144 be a null pointer if the statements were all null, for example if there
13145 were no statements or if there were only subroutine definitions (which
13146 have compile-time side effects).  If not null, it will be a C<lineseq>
13147 list, normally including C<nextstate> or equivalent ops.
13148
13149 If an error occurs in parsing or compilation, in most cases a valid op
13150 tree is returned anyway.  The error is reflected in the parser state,
13151 normally resulting in a single exception at the top level of parsing
13152 which covers all the compilation errors that occurred.  Some compilation
13153 errors, however, will throw an exception immediately.
13154
13155 The C<flags> parameter is reserved for future use, and must always
13156 be zero.
13157
13158 =cut
13159 */
13160
13161 OP *
13162 Perl_parse_stmtseq(pTHX_ U32 flags)
13163 {
13164     OP *stmtseqop;
13165     I32 c;
13166     if (flags)
13167         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_stmtseq");
13168     stmtseqop = parse_recdescent_for_op(GRAMSTMTSEQ, LEX_FAKEEOF_CLOSING);
13169     c = lex_peek_unichar(0);
13170     if (c != -1 && c != /*{*/'}')
13171         qerror(Perl_mess(aTHX_ "Parse error"));
13172     return stmtseqop;
13173 }
13174
13175 /*
13176 =for apidoc parse_subsignature
13177
13178 Parse a subroutine signature declaration. This is the contents of the
13179 parentheses following a named or anonymous subroutine declaration when the
13180 C<signatures> feature is enabled. Note that this function neither expects
13181 nor consumes the opening and closing parentheses around the signature; it
13182 is the caller's job to handle these.
13183
13184 This function must only be called during parsing of a subroutine; after
13185 L</start_subparse> has been called. It might allocate lexical variables on
13186 the pad for the current subroutine.
13187
13188 The op tree to unpack the arguments from the stack at runtime is returned.
13189 This op tree should appear at the beginning of the compiled function. The
13190 caller may wish to use L</op_append_list> to build their function body
13191 after it, or splice it together with the body before calling L</newATTRSUB>.
13192
13193 The C<flags> parameter is reserved for future use, and must always
13194 be zero.
13195
13196 =cut
13197 */
13198
13199 OP *
13200 Perl_parse_subsignature(pTHX_ U32 flags)
13201 {
13202     if (flags)
13203         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_subsignature");
13204     return parse_recdescent_for_op(GRAMSUBSIGNATURE, LEX_FAKEEOF_NONEXPR);
13205 }
13206
13207 /*
13208  * ex: set ts=8 sts=4 sw=4 et:
13209  */