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