This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
op.c: Abstract common override code
[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     SV *pv = sv;
2517
2518     PERL_ARGS_ASSERT_TOKEQ;
2519
2520     assert (SvPOK(sv));
2521     assert (SvLEN(sv));
2522     assert (!SvIsCOW(sv));
2523     if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1) /* <<'heredoc' */
2524         goto finish;
2525     s = SvPVX(sv);
2526     send = SvEND(sv);
2527     /* This is relying on the SV being "well formed" with a trailing '\0'  */
2528     while (s < send && !(*s == '\\' && s[1] == '\\'))
2529         s++;
2530     if (s == send)
2531         goto finish;
2532     d = s;
2533     if ( PL_hints & HINT_NEW_STRING ) {
2534         pv = newSVpvn_flags(SvPVX_const(pv), SvCUR(sv),
2535                             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 == OP_SUBST && PL_lex_repl == PL_linestr
6947          && isDIGIT(*s))
6948             Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),"Can't use \\%c to mean $%c in expression",
6949                            *s, *s);
6950         if (PL_expect == XOPERATOR)
6951             no_op("Backslash",s);
6952         OPERATOR(REFGEN);
6953
6954     case 'v':
6955         if (isDIGIT(s[1]) && PL_expect != XOPERATOR) {
6956             char *start = s + 2;
6957             while (isDIGIT(*start) || *start == '_')
6958                 start++;
6959             if (*start == '.' && isDIGIT(start[1])) {
6960                 s = scan_num(s, &pl_yylval);
6961                 TERM(THING);
6962             }
6963             else if ((*start == ':' && start[1] == ':')
6964                   || (PL_expect == XSTATE && *start == ':'))
6965                 goto keylookup;
6966             else if (PL_expect == XSTATE) {
6967                 d = start;
6968                 while (d < PL_bufend && isSPACE(*d)) d++;
6969                 if (*d == ':') goto keylookup;
6970             }
6971             /* avoid v123abc() or $h{v1}, allow C<print v10;> */
6972             if (!isALPHA(*start) && (PL_expect == XTERM
6973                         || PL_expect == XREF || PL_expect == XSTATE
6974                         || PL_expect == XTERMORDORDOR)) {
6975                 GV *const gv = gv_fetchpvn_flags(s, start - s,
6976                                                     UTF ? SVf_UTF8 : 0, SVt_PVCV);
6977                 if (!gv) {
6978                     s = scan_num(s, &pl_yylval);
6979                     TERM(THING);
6980                 }
6981             }
6982         }
6983         goto keylookup;
6984     case 'x':
6985         if (isDIGIT(s[1]) && PL_expect == XOPERATOR) {
6986             s++;
6987             Mop(OP_REPEAT);
6988         }
6989         goto keylookup;
6990
6991     case '_':
6992     case 'a': case 'A':
6993     case 'b': case 'B':
6994     case 'c': case 'C':
6995     case 'd': case 'D':
6996     case 'e': case 'E':
6997     case 'f': case 'F':
6998     case 'g': case 'G':
6999     case 'h': case 'H':
7000     case 'i': case 'I':
7001     case 'j': case 'J':
7002     case 'k': case 'K':
7003     case 'l': case 'L':
7004     case 'm': case 'M':
7005     case 'n': case 'N':
7006     case 'o': case 'O':
7007     case 'p': case 'P':
7008     case 'q': case 'Q':
7009     case 'r': case 'R':
7010     case 's': case 'S':
7011     case 't': case 'T':
7012     case 'u': case 'U':
7013               case 'V':
7014     case 'w': case 'W':
7015               case 'X':
7016     case 'y': case 'Y':
7017     case 'z': case 'Z':
7018
7019       keylookup: {
7020         bool anydelim;
7021         bool lex;
7022         I32 tmp;
7023         SV *sv;
7024         CV *cv;
7025         PADOFFSET off;
7026         OP *rv2cv_op;
7027
7028         lex = FALSE;
7029         orig_keyword = 0;
7030         off = 0;
7031         sv = NULL;
7032         cv = NULL;
7033         gv = NULL;
7034         gvp = NULL;
7035         rv2cv_op = NULL;
7036
7037         PL_bufptr = s;
7038         s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
7039
7040         /* Some keywords can be followed by any delimiter, including ':' */
7041         anydelim = word_takes_any_delimeter(PL_tokenbuf, len);
7042
7043         /* x::* is just a word, unless x is "CORE" */
7044         if (!anydelim && *s == ':' && s[1] == ':' && strNE(PL_tokenbuf, "CORE"))
7045             goto just_a_word;
7046
7047         d = s;
7048         while (d < PL_bufend && isSPACE(*d))
7049                 d++;    /* no comments skipped here, or s### is misparsed */
7050
7051         /* Is this a word before a => operator? */
7052         if (*d == '=' && d[1] == '>') {
7053           fat_arrow:
7054             CLINE;
7055             pl_yylval.opval
7056                 = (OP*)newSVOP(OP_CONST, 0,
7057                                S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
7058             pl_yylval.opval->op_private = OPpCONST_BARE;
7059             TERM(WORD);
7060         }
7061
7062         /* Check for plugged-in keyword */
7063         {
7064             OP *o;
7065             int result;
7066             char *saved_bufptr = PL_bufptr;
7067             PL_bufptr = s;
7068             result = PL_keyword_plugin(aTHX_ PL_tokenbuf, len, &o);
7069             s = PL_bufptr;
7070             if (result == KEYWORD_PLUGIN_DECLINE) {
7071                 /* not a plugged-in keyword */
7072                 PL_bufptr = saved_bufptr;
7073             } else if (result == KEYWORD_PLUGIN_STMT) {
7074                 pl_yylval.opval = o;
7075                 CLINE;
7076                 PL_expect = XSTATE;
7077                 return REPORT(PLUGSTMT);
7078             } else if (result == KEYWORD_PLUGIN_EXPR) {
7079                 pl_yylval.opval = o;
7080                 CLINE;
7081                 PL_expect = XOPERATOR;
7082                 return REPORT(PLUGEXPR);
7083             } else {
7084                 Perl_croak(aTHX_ "Bad plugin affecting keyword '%s'",
7085                                         PL_tokenbuf);
7086             }
7087         }
7088
7089         /* Check for built-in keyword */
7090         tmp = keyword(PL_tokenbuf, len, 0);
7091
7092         /* Is this a label? */
7093         if (!anydelim && PL_expect == XSTATE
7094               && d < PL_bufend && *d == ':' && *(d + 1) != ':') {
7095             s = d + 1;
7096             pl_yylval.pval = savepvn(PL_tokenbuf, len+1);
7097             pl_yylval.pval[len] = '\0';
7098             pl_yylval.pval[len+1] = UTF ? 1 : 0;
7099             CLINE;
7100             TOKEN(LABEL);
7101         }
7102
7103         /* Check for lexical sub */
7104         if (PL_expect != XOPERATOR) {
7105             char tmpbuf[sizeof PL_tokenbuf + 1];
7106             *tmpbuf = '&';
7107             Copy(PL_tokenbuf, tmpbuf+1, len, char);
7108             off = pad_findmy_pvn(tmpbuf, len+1, UTF ? SVf_UTF8 : 0);
7109             if (off != NOT_IN_PAD) {
7110                 assert(off); /* we assume this is boolean-true below */
7111                 if (PAD_COMPNAME_FLAGS_isOUR(off)) {
7112                     HV *  const stash = PAD_COMPNAME_OURSTASH(off);
7113                     HEK * const stashname = HvNAME_HEK(stash);
7114                     sv = newSVhek(stashname);
7115                     sv_catpvs(sv, "::");
7116                     sv_catpvn_flags(sv, PL_tokenbuf, len,
7117                                     (UTF ? SV_CATUTF8 : SV_CATBYTES));
7118                     gv = gv_fetchsv(sv, GV_NOADD_NOINIT | SvUTF8(sv),
7119                                     SVt_PVCV);
7120                     off = 0;
7121                     if (!gv) {
7122                         sv_free(sv);
7123                         sv = NULL;
7124                         goto just_a_word;
7125                     }
7126                 }
7127                 else {
7128                     rv2cv_op = newOP(OP_PADANY, 0);
7129                     rv2cv_op->op_targ = off;
7130                     cv = find_lexical_cv(off);
7131                 }
7132                 lex = TRUE;
7133                 goto just_a_word;
7134             }
7135             off = 0;
7136         }
7137
7138         if (tmp < 0) {                  /* second-class keyword? */
7139             GV *ogv = NULL;     /* override (winner) */
7140             GV *hgv = NULL;     /* hidden (loser) */
7141             if (PL_expect != XOPERATOR && (*s != ':' || s[1] != ':')) {
7142                 CV *cv;
7143                 if ((gv = gv_fetchpvn_flags(PL_tokenbuf, len,
7144                                             UTF ? SVf_UTF8 : 0, SVt_PVCV)) &&
7145                     (cv = GvCVu(gv)))
7146                 {
7147                     if (GvIMPORTED_CV(gv))
7148                         ogv = gv;
7149                     else if (! CvMETHOD(cv))
7150                         hgv = gv;
7151                 }
7152                 if (!ogv &&
7153                     (gvp = (GV**)hv_fetch(PL_globalstash, PL_tokenbuf,
7154                                             UTF ? -(I32)len : (I32)len, FALSE)) &&
7155                     (gv = *gvp) && isGV_with_GP(gv) &&
7156                     GvCVu(gv) && GvIMPORTED_CV(gv))
7157                 {
7158                     ogv = gv;
7159                 }
7160             }
7161             if (ogv) {
7162                 orig_keyword = tmp;
7163                 tmp = 0;                /* overridden by import or by GLOBAL */
7164             }
7165             else if (gv && !gvp
7166                      && -tmp==KEY_lock  /* XXX generalizable kludge */
7167                      && GvCVu(gv))
7168             {
7169                 tmp = 0;                /* any sub overrides "weak" keyword */
7170             }
7171             else {                      /* no override */
7172                 tmp = -tmp;
7173                 if (tmp == KEY_dump) {
7174                     Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
7175                                    "dump() better written as CORE::dump()");
7176                 }
7177                 gv = NULL;
7178                 gvp = 0;
7179                 if (hgv && tmp != KEY_x && tmp != KEY_CORE)     /* never ambiguous */
7180                     Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
7181                                    "Ambiguous call resolved as CORE::%s(), "
7182                                    "qualify as such or use &",
7183                                    GvENAME(hgv));
7184             }
7185         }
7186
7187         if (tmp && tmp != KEY___DATA__ && tmp != KEY___END__
7188          && (!anydelim || *s != '#')) {
7189             /* no override, and not s### either; skipspace is safe here
7190              * check for => on following line */
7191             bool arrow;
7192             STRLEN bufoff = PL_bufptr - SvPVX(PL_linestr);
7193             STRLEN   soff = s         - SvPVX(PL_linestr);
7194             s = skipspace_flags(s, LEX_NO_INCLINE);
7195             arrow = *s == '=' && s[1] == '>';
7196             PL_bufptr = SvPVX(PL_linestr) + bufoff;
7197             s         = SvPVX(PL_linestr) +   soff;
7198             if (arrow)
7199                 goto fat_arrow;
7200         }
7201
7202       reserved_word:
7203         switch (tmp) {
7204
7205         default:                        /* not a keyword */
7206             /* Trade off - by using this evil construction we can pull the
7207                variable gv into the block labelled keylookup. If not, then
7208                we have to give it function scope so that the goto from the
7209                earlier ':' case doesn't bypass the initialisation.  */
7210             if (0) {
7211             just_a_word_zero_gv:
7212                 sv = NULL;
7213                 cv = NULL;
7214                 gv = NULL;
7215                 gvp = NULL;
7216                 rv2cv_op = NULL;
7217                 orig_keyword = 0;
7218                 lex = 0;
7219                 off = 0;
7220             }
7221           just_a_word: {
7222                 int pkgname = 0;
7223                 const char lastchar = (PL_bufptr == PL_oldoldbufptr ? 0 : PL_bufptr[-1]);
7224                 const char penultchar =
7225                     lastchar && PL_bufptr - 2 >= PL_linestart
7226                          ? PL_bufptr[-2]
7227                          : 0;
7228 #ifdef PERL_MAD
7229                 SV *nextPL_nextwhite = 0;
7230 #endif
7231
7232
7233                 /* Get the rest if it looks like a package qualifier */
7234
7235                 if (*s == '\'' || (*s == ':' && s[1] == ':')) {
7236                     STRLEN morelen;
7237                     s = scan_word(s, PL_tokenbuf + len, sizeof PL_tokenbuf - len,
7238                                   TRUE, &morelen);
7239                     if (!morelen)
7240                         Perl_croak(aTHX_ "Bad name after %"UTF8f"%s",
7241                                 UTF8fARG(UTF, len, PL_tokenbuf),
7242                                 *s == '\'' ? "'" : "::");
7243                     len += morelen;
7244                     pkgname = 1;
7245                 }
7246
7247                 if (PL_expect == XOPERATOR) {
7248                     if (PL_bufptr == PL_linestart) {
7249                         CopLINE_dec(PL_curcop);
7250                         Perl_warner(aTHX_ packWARN(WARN_SEMICOLON), "%s", PL_warn_nosemi);
7251                         CopLINE_inc(PL_curcop);
7252                     }
7253                     else
7254                         no_op("Bareword",s);
7255                 }
7256
7257                 /* Look for a subroutine with this name in current package,
7258                    unless this is a lexical sub, or name is "Foo::",
7259                    in which case Foo is a bareword
7260                    (and a package name). */
7261
7262                 if (len > 2 && !PL_madskills &&
7263                     PL_tokenbuf[len - 2] == ':' && PL_tokenbuf[len - 1] == ':')
7264                 {
7265                     if (ckWARN(WARN_BAREWORD)
7266                         && ! gv_fetchpvn_flags(PL_tokenbuf, len, UTF ? SVf_UTF8 : 0, SVt_PVHV))
7267                         Perl_warner(aTHX_ packWARN(WARN_BAREWORD),
7268                           "Bareword \"%"UTF8f"\" refers to nonexistent package",
7269                            UTF8fARG(UTF, len, PL_tokenbuf));
7270                     len -= 2;
7271                     PL_tokenbuf[len] = '\0';
7272                     gv = NULL;
7273                     gvp = 0;
7274                 }
7275                 else {
7276                     if (!lex && !gv) {
7277                         /* Mustn't actually add anything to a symbol table.
7278                            But also don't want to "initialise" any placeholder
7279                            constants that might already be there into full
7280                            blown PVGVs with attached PVCV.  */
7281                         gv = gv_fetchpvn_flags(PL_tokenbuf, len,
7282                                                GV_NOADD_NOINIT | ( UTF ? SVf_UTF8 : 0 ),
7283                                                SVt_PVCV);
7284                     }
7285                     len = 0;
7286                 }
7287
7288                 /* if we saw a global override before, get the right name */
7289
7290                 if (!sv)
7291                   sv = S_newSV_maybe_utf8(aTHX_ PL_tokenbuf,
7292                     len ? len : strlen(PL_tokenbuf));
7293                 if (gvp) {
7294                     SV * const tmp_sv = sv;
7295                     sv = newSVpvs("CORE::GLOBAL::");
7296                     sv_catsv(sv, tmp_sv);
7297                     SvREFCNT_dec(tmp_sv);
7298                 }
7299
7300 #ifdef PERL_MAD
7301                 if (PL_madskills && !PL_thistoken) {
7302                     char *start = SvPVX(PL_linestr) + PL_realtokenstart;
7303                     PL_thistoken = newSVpvn(start,s - start);
7304                     PL_realtokenstart = s - SvPVX(PL_linestr);
7305                 }
7306 #endif
7307
7308                 /* Presume this is going to be a bareword of some sort. */
7309                 CLINE;
7310                 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
7311                 pl_yylval.opval->op_private = OPpCONST_BARE;
7312
7313                 /* And if "Foo::", then that's what it certainly is. */
7314                 if (len)
7315                     goto safe_bareword;
7316
7317                 if (!off)
7318                 {
7319                     OP *const_op = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(sv));
7320                     const_op->op_private = OPpCONST_BARE;
7321                     rv2cv_op = newCVREF(0, const_op);
7322                     cv = lex ? GvCV(gv) : rv2cv_op_cv(rv2cv_op, 0);
7323                 }
7324
7325                 /* See if it's the indirect object for a list operator. */
7326
7327                 if (PL_oldoldbufptr &&
7328                     PL_oldoldbufptr < PL_bufptr &&
7329                     (PL_oldoldbufptr == PL_last_lop
7330                      || PL_oldoldbufptr == PL_last_uni) &&
7331                     /* NO SKIPSPACE BEFORE HERE! */
7332                     (PL_expect == XREF ||
7333                      ((PL_opargs[PL_last_lop_op] >> OASHIFT)& 7) == OA_FILEREF))
7334                 {
7335                     bool immediate_paren = *s == '(';
7336
7337                     /* (Now we can afford to cross potential line boundary.) */
7338                     s = SKIPSPACE2(s,nextPL_nextwhite);
7339 #ifdef PERL_MAD
7340                     PL_nextwhite = nextPL_nextwhite;    /* assume no & deception */
7341 #endif
7342
7343                     /* Two barewords in a row may indicate method call. */
7344
7345                     if ((isIDFIRST_lazy_if(s,UTF) || *s == '$') &&
7346                         (tmp = intuit_method(s, gv, cv))) {
7347                         op_free(rv2cv_op);
7348                         if (tmp == METHOD && !PL_lex_allbrackets &&
7349                                 PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7350                             PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7351                         return REPORT(tmp);
7352                     }
7353
7354                     /* If not a declared subroutine, it's an indirect object. */
7355                     /* (But it's an indir obj regardless for sort.) */
7356                     /* Also, if "_" follows a filetest operator, it's a bareword */
7357
7358                     if (
7359                         ( !immediate_paren && (PL_last_lop_op == OP_SORT ||
7360                          (!cv &&
7361                         (PL_last_lop_op != OP_MAPSTART &&
7362                          PL_last_lop_op != OP_GREPSTART))))
7363                        || (PL_tokenbuf[0] == '_' && PL_tokenbuf[1] == '\0'
7364                             && ((PL_opargs[PL_last_lop_op] & OA_CLASS_MASK) == OA_FILESTATOP))
7365                        )
7366                     {
7367                         PL_expect = (PL_last_lop == PL_oldoldbufptr) ? XTERM : XOPERATOR;
7368                         goto bareword;
7369                     }
7370                 }
7371
7372                 PL_expect = XOPERATOR;
7373 #ifdef PERL_MAD
7374                 if (isSPACE(*s))
7375                     s = SKIPSPACE2(s,nextPL_nextwhite);
7376                 PL_nextwhite = nextPL_nextwhite;
7377 #else
7378                 s = skipspace(s);
7379 #endif
7380
7381                 /* Is this a word before a => operator? */
7382                 if (*s == '=' && s[1] == '>' && !pkgname) {
7383                     op_free(rv2cv_op);
7384                     CLINE;
7385                     /* This is our own scalar, created a few lines above,
7386                        so this is safe. */
7387                     SvREADONLY_off(cSVOPx(pl_yylval.opval)->op_sv);
7388                     sv_setpv(((SVOP*)pl_yylval.opval)->op_sv, PL_tokenbuf);
7389                     if (UTF && !IN_BYTES && is_utf8_string((U8*)PL_tokenbuf, len))
7390                       SvUTF8_on(((SVOP*)pl_yylval.opval)->op_sv);
7391                     SvREADONLY_on(cSVOPx(pl_yylval.opval)->op_sv);
7392                     TERM(WORD);
7393                 }
7394
7395                 /* If followed by a paren, it's certainly a subroutine. */
7396                 if (*s == '(') {
7397                     CLINE;
7398                     if (cv) {
7399                         d = s + 1;
7400                         while (SPACE_OR_TAB(*d))
7401                             d++;
7402                         if (*d == ')' && (sv = cv_const_sv_or_av(cv))) {
7403                             s = d + 1;
7404                             goto its_constant;
7405                         }
7406                     }
7407 #ifdef PERL_MAD
7408                     if (PL_madskills) {
7409                         PL_nextwhite = PL_thiswhite;
7410                         PL_thiswhite = 0;
7411                     }
7412                     start_force(PL_curforce);
7413 #endif
7414                     NEXTVAL_NEXTTOKE.opval =
7415                         off ? rv2cv_op : pl_yylval.opval;
7416                     PL_expect = XOPERATOR;
7417 #ifdef PERL_MAD
7418                     if (PL_madskills) {
7419                         PL_nextwhite = nextPL_nextwhite;
7420                         curmad('X', PL_thistoken);
7421                         PL_thistoken = newSVpvs("");
7422                     }
7423 #endif
7424                     if (off)
7425                          op_free(pl_yylval.opval), force_next(PRIVATEREF);
7426                     else op_free(rv2cv_op),        force_next(WORD);
7427                     pl_yylval.ival = 0;
7428                     TOKEN('&');
7429                 }
7430
7431                 /* If followed by var or block, call it a method (unless sub) */
7432
7433                 if ((*s == '$' || *s == '{') && !cv) {
7434                     op_free(rv2cv_op);
7435                     PL_last_lop = PL_oldbufptr;
7436                     PL_last_lop_op = OP_METHOD;
7437                     if (!PL_lex_allbrackets &&
7438                             PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7439                         PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7440                     PREBLOCK(METHOD);
7441                 }
7442
7443                 /* If followed by a bareword, see if it looks like indir obj. */
7444
7445                 if (!orig_keyword
7446                         && (isIDFIRST_lazy_if(s,UTF) || *s == '$')
7447                         && (tmp = intuit_method(s, gv, cv))) {
7448                     op_free(rv2cv_op);
7449                     if (tmp == METHOD && !PL_lex_allbrackets &&
7450                             PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7451                         PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7452                     return REPORT(tmp);
7453                 }
7454
7455                 /* Not a method, so call it a subroutine (if defined) */
7456
7457                 if (cv) {
7458                     if (lastchar == '-' && penultchar != '-') {
7459                         const STRLEN l = len ? len : strlen(PL_tokenbuf);
7460                         Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
7461                             "Ambiguous use of -%"UTF8f" resolved as -&%"UTF8f"()",
7462                              UTF8fARG(UTF, l, PL_tokenbuf),
7463                              UTF8fARG(UTF, l, PL_tokenbuf));
7464                     }
7465                     /* Check for a constant sub */
7466                     if ((sv = cv_const_sv_or_av(cv))) {
7467                   its_constant:
7468                         op_free(rv2cv_op);
7469                         SvREFCNT_dec(((SVOP*)pl_yylval.opval)->op_sv);
7470                         ((SVOP*)pl_yylval.opval)->op_sv = SvREFCNT_inc_simple(sv);
7471                         if (SvTYPE(sv) == SVt_PVAV)
7472                             pl_yylval.opval = newUNOP(OP_RV2AV, OPf_PARENS,
7473                                                       pl_yylval.opval);
7474                         else {
7475                             pl_yylval.opval->op_private = 0;
7476                             pl_yylval.opval->op_folded = 1;
7477                             pl_yylval.opval->op_flags |= OPf_SPECIAL;
7478                         }
7479                         TOKEN(WORD);
7480                     }
7481
7482                     op_free(pl_yylval.opval);
7483                     pl_yylval.opval =
7484                         off ? (OP *)newCVREF(0, rv2cv_op) : rv2cv_op;
7485                     pl_yylval.opval->op_private |= OPpENTERSUB_NOPAREN;
7486                     PL_last_lop = PL_oldbufptr;
7487                     PL_last_lop_op = OP_ENTERSUB;
7488                     /* Is there a prototype? */
7489                     if (
7490 #ifdef PERL_MAD
7491                         cv &&
7492 #endif
7493                         SvPOK(cv))
7494                     {
7495                         STRLEN protolen = CvPROTOLEN(cv);
7496                         const char *proto = CvPROTO(cv);
7497                         bool optional;
7498                         proto = S_strip_spaces(aTHX_ proto, &protolen);
7499                         if (!protolen)
7500                             TERM(FUNC0SUB);
7501                         if ((optional = *proto == ';'))
7502                           do
7503                             proto++;
7504                           while (*proto == ';');
7505                         if (
7506                             (
7507                                 (
7508                                     *proto == '$' || *proto == '_'
7509                                  || *proto == '*' || *proto == '+'
7510                                 )
7511                              && proto[1] == '\0'
7512                             )
7513                          || (
7514                              *proto == '\\' && proto[1] && proto[2] == '\0'
7515                             )
7516                         )
7517                             UNIPROTO(UNIOPSUB,optional);
7518                         if (*proto == '\\' && proto[1] == '[') {
7519                             const char *p = proto + 2;
7520                             while(*p && *p != ']')
7521                                 ++p;
7522                             if(*p == ']' && !p[1])
7523                                 UNIPROTO(UNIOPSUB,optional);
7524                         }
7525                         if (*proto == '&' && *s == '{') {
7526                             if (PL_curstash)
7527                                 sv_setpvs(PL_subname, "__ANON__");
7528                             else
7529                                 sv_setpvs(PL_subname, "__ANON__::__ANON__");
7530                             if (!PL_lex_allbrackets &&
7531                                     PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7532                                 PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7533                             PREBLOCK(LSTOPSUB);
7534                         }
7535                     }
7536 #ifdef PERL_MAD
7537                     {
7538                         if (PL_madskills) {
7539                             PL_nextwhite = PL_thiswhite;
7540                             PL_thiswhite = 0;
7541                         }
7542                         start_force(PL_curforce);
7543                         NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
7544                         PL_expect = XTERM;
7545                         if (PL_madskills) {
7546                             PL_nextwhite = nextPL_nextwhite;
7547                             curmad('X', PL_thistoken);
7548                             PL_thistoken = newSVpvs("");
7549                         }
7550                         force_next(off ? PRIVATEREF : WORD);
7551                         if (!PL_lex_allbrackets &&
7552                                 PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7553                             PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7554                         TOKEN(NOAMP);
7555                     }
7556                 }
7557
7558                 /* Guess harder when madskills require "best effort". */
7559                 if (PL_madskills && (!gv || !GvCVu(gv))) {
7560                     int probable_sub = 0;
7561                     if (strchr("\"'`$@%0123456789!*+{[<", *s))
7562                         probable_sub = 1;
7563                     else if (isALPHA(*s)) {
7564                         char tmpbuf[1024];
7565                         STRLEN tmplen;
7566                         d = s;
7567                         d = scan_word(d, tmpbuf, sizeof tmpbuf, TRUE, &tmplen);
7568                         if (!keyword(tmpbuf, tmplen, 0))
7569                             probable_sub = 1;
7570                         else {
7571                             while (d < PL_bufend && isSPACE(*d))
7572                                 d++;
7573                             if (*d == '=' && d[1] == '>')
7574                                 probable_sub = 1;
7575                         }
7576                     }
7577                     if (probable_sub) {
7578                         gv = gv_fetchpv(PL_tokenbuf, GV_ADD | ( UTF ? SVf_UTF8 : 0 ),
7579                                         SVt_PVCV);
7580                         op_free(pl_yylval.opval);
7581                         pl_yylval.opval =
7582                             off ? (OP *)newCVREF(0, rv2cv_op) : rv2cv_op;
7583                         pl_yylval.opval->op_private |= OPpENTERSUB_NOPAREN;
7584                         PL_last_lop = PL_oldbufptr;
7585                         PL_last_lop_op = OP_ENTERSUB;
7586                         PL_nextwhite = PL_thiswhite;
7587                         PL_thiswhite = 0;
7588                         start_force(PL_curforce);
7589                         NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
7590                         PL_expect = XTERM;
7591                         PL_nextwhite = nextPL_nextwhite;
7592                         curmad('X', PL_thistoken);
7593                         PL_thistoken = newSVpvs("");
7594                         force_next(off ? PRIVATEREF : WORD);
7595                         if (!PL_lex_allbrackets &&
7596                                 PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7597                             PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7598                         TOKEN(NOAMP);
7599                     }
7600 #else
7601                     NEXTVAL_NEXTTOKE.opval = pl_yylval.opval;
7602                     PL_expect = XTERM;
7603                     force_next(off ? PRIVATEREF : WORD);
7604                     if (!PL_lex_allbrackets &&
7605                             PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
7606                         PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
7607                     TOKEN(NOAMP);
7608 #endif
7609                 }
7610
7611                 /* Call it a bare word */
7612
7613                 if (PL_hints & HINT_STRICT_SUBS)
7614                     pl_yylval.opval->op_private |= OPpCONST_STRICT;
7615                 else {
7616                 bareword:
7617                     /* after "print" and similar functions (corresponding to
7618                      * "F? L" in opcode.pl), whatever wasn't already parsed as
7619                      * a filehandle should be subject to "strict subs".
7620                      * Likewise for the optional indirect-object argument to system
7621                      * or exec, which can't be a bareword */
7622                     if ((PL_last_lop_op == OP_PRINT
7623                             || PL_last_lop_op == OP_PRTF
7624                             || PL_last_lop_op == OP_SAY
7625                             || PL_last_lop_op == OP_SYSTEM
7626                             || PL_last_lop_op == OP_EXEC)
7627                             && (PL_hints & HINT_STRICT_SUBS))
7628                         pl_yylval.opval->op_private |= OPpCONST_STRICT;
7629                     if (lastchar != '-') {
7630                         if (ckWARN(WARN_RESERVED)) {
7631                             d = PL_tokenbuf;
7632                             while (isLOWER(*d))
7633                                 d++;
7634                             if (!*d && !gv_stashpv(PL_tokenbuf, UTF ? SVf_UTF8 : 0))
7635                                 Perl_warner(aTHX_ packWARN(WARN_RESERVED), PL_warn_reserved,
7636                                        PL_tokenbuf);
7637                         }
7638                     }
7639                 }
7640                 op_free(rv2cv_op);
7641
7642             safe_bareword:
7643                 if ((lastchar == '*' || lastchar == '%' || lastchar == '&')
7644                  && saw_infix_sigil) {
7645                     Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
7646                                      "Operator or semicolon missing before %c%"UTF8f,
7647                                      lastchar,
7648                                      UTF8fARG(UTF, strlen(PL_tokenbuf),
7649                                               PL_tokenbuf));
7650                     Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
7651                                      "Ambiguous use of %c resolved as operator %c",
7652                                      lastchar, lastchar);
7653                 }
7654                 TOKEN(WORD);
7655             }
7656
7657         case KEY___FILE__:
7658             FUN0OP(
7659                 (OP*)newSVOP(OP_CONST, 0, newSVpv(CopFILE(PL_curcop),0))
7660             );
7661
7662         case KEY___LINE__:
7663             FUN0OP(
7664                 (OP*)newSVOP(OP_CONST, 0,
7665                     Perl_newSVpvf(aTHX_ "%"IVdf, (IV)CopLINE(PL_curcop)))
7666             );
7667
7668         case KEY___PACKAGE__:
7669             FUN0OP(
7670                 (OP*)newSVOP(OP_CONST, 0,
7671                                         (PL_curstash
7672                                          ? newSVhek(HvNAME_HEK(PL_curstash))
7673                                          : &PL_sv_undef))
7674             );
7675
7676         case KEY___DATA__:
7677         case KEY___END__: {
7678             GV *gv;
7679             if (PL_rsfp && (!PL_in_eval || PL_tokenbuf[2] == 'D')) {
7680                 HV * const stash = PL_tokenbuf[2] == 'D' && PL_curstash
7681                                         ? PL_curstash
7682                                         : PL_defstash;
7683                 gv = (GV *)*hv_fetchs(stash, "DATA", 1);
7684                 if (!isGV(gv))
7685                     gv_init(gv,stash,"DATA",4,0);
7686                 GvMULTI_on(gv);
7687                 if (!GvIO(gv))
7688                     GvIOp(gv) = newIO();
7689                 IoIFP(GvIOp(gv)) = PL_rsfp;
7690 #if defined(HAS_FCNTL) && defined(F_SETFD)
7691                 {
7692                     const int fd = PerlIO_fileno(PL_rsfp);
7693                     fcntl(fd,F_SETFD,fd >= 3);
7694                 }
7695 #endif
7696                 /* Mark this internal pseudo-handle as clean */
7697                 IoFLAGS(GvIOp(gv)) |= IOf_UNTAINT;
7698                 if ((PerlIO*)PL_rsfp == PerlIO_stdin())
7699                     IoTYPE(GvIOp(gv)) = IoTYPE_STD;
7700                 else
7701                     IoTYPE(GvIOp(gv)) = IoTYPE_RDONLY;
7702 #if defined(WIN32) && !defined(PERL_TEXTMODE_SCRIPTS)
7703                 /* if the script was opened in binmode, we need to revert
7704                  * it to text mode for compatibility; but only iff it has CRs
7705                  * XXX this is a questionable hack at best. */
7706                 if (PL_bufend-PL_bufptr > 2
7707                     && PL_bufend[-1] == '\n' && PL_bufend[-2] == '\r')
7708                 {
7709                     Off_t loc = 0;
7710                     if (IoTYPE(GvIOp(gv)) == IoTYPE_RDONLY) {
7711                         loc = PerlIO_tell(PL_rsfp);
7712                         (void)PerlIO_seek(PL_rsfp, 0L, 0);
7713                     }
7714 #ifdef NETWARE
7715                         if (PerlLIO_setmode(PL_rsfp, O_TEXT) != -1) {
7716 #else
7717                     if (PerlLIO_setmode(PerlIO_fileno(PL_rsfp), O_TEXT) != -1) {
7718 #endif  /* NETWARE */
7719                         if (loc > 0)
7720                             PerlIO_seek(PL_rsfp, loc, 0);
7721                     }
7722                 }
7723 #endif
7724 #ifdef PERLIO_LAYERS
7725                 if (!IN_BYTES) {
7726                     if (UTF)
7727                         PerlIO_apply_layers(aTHX_ PL_rsfp, NULL, ":utf8");
7728                     else if (PL_encoding) {
7729                         SV *name;
7730                         dSP;
7731                         ENTER;
7732                         SAVETMPS;
7733                         PUSHMARK(sp);
7734                         EXTEND(SP, 1);
7735                         XPUSHs(PL_encoding);
7736                         PUTBACK;
7737                         call_method("name", G_SCALAR);
7738                         SPAGAIN;
7739                         name = POPs;
7740                         PUTBACK;
7741                         PerlIO_apply_layers(aTHX_ PL_rsfp, NULL,
7742                                             Perl_form(aTHX_ ":encoding(%"SVf")",
7743                                                       SVfARG(name)));
7744                         FREETMPS;
7745                         LEAVE;
7746                     }
7747                 }
7748 #endif
7749 #ifdef PERL_MAD
7750                 if (PL_madskills) {
7751                     if (PL_realtokenstart >= 0) {
7752                         char *tstart = SvPVX(PL_linestr) + PL_realtokenstart;
7753                         if (!PL_endwhite)
7754                             PL_endwhite = newSVpvs("");
7755                         sv_catsv(PL_endwhite, PL_thiswhite);
7756                         PL_thiswhite = 0;
7757                         sv_catpvn(PL_endwhite, tstart, PL_bufend - tstart);
7758                         PL_realtokenstart = -1;
7759                     }
7760                     while ((s = filter_gets(PL_endwhite, SvCUR(PL_endwhite)))
7761                            != NULL) ;
7762                 }
7763 #endif
7764                 PL_rsfp = NULL;
7765             }
7766             goto fake_eof;
7767         }
7768
7769         case KEY___SUB__:
7770             FUN0OP(newPVOP(OP_RUNCV,0,NULL));
7771
7772         case KEY_AUTOLOAD:
7773         case KEY_DESTROY:
7774         case KEY_BEGIN:
7775         case KEY_UNITCHECK:
7776         case KEY_CHECK:
7777         case KEY_INIT:
7778         case KEY_END:
7779             if (PL_expect == XSTATE) {
7780                 s = PL_bufptr;
7781                 goto really_sub;
7782             }
7783             goto just_a_word;
7784
7785         case KEY_CORE:
7786             if (*s == ':' && s[1] == ':') {
7787                 STRLEN olen = len;
7788                 d = s;
7789                 s += 2;
7790                 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
7791                 if ((*s == ':' && s[1] == ':')
7792                  || (!(tmp = keyword(PL_tokenbuf, len, 1)) && *s == '\''))
7793                 {
7794                     s = d;
7795                     len = olen;
7796                     Copy(PL_bufptr, PL_tokenbuf, olen, char);
7797                     goto just_a_word;
7798                 }
7799                 if (!tmp)
7800                     Perl_croak(aTHX_ "CORE::%"UTF8f" is not a keyword",
7801                                       UTF8fARG(UTF, len, PL_tokenbuf));
7802                 if (tmp < 0)
7803                     tmp = -tmp;
7804                 else if (tmp == KEY_require || tmp == KEY_do
7805                       || tmp == KEY_glob)
7806                     /* that's a way to remember we saw "CORE::" */
7807                     orig_keyword = tmp;
7808                 goto reserved_word;
7809             }
7810             goto just_a_word;
7811
7812         case KEY_abs:
7813             UNI(OP_ABS);
7814
7815         case KEY_alarm:
7816             UNI(OP_ALARM);
7817
7818         case KEY_accept:
7819             LOP(OP_ACCEPT,XTERM);
7820
7821         case KEY_and:
7822             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
7823                 return REPORT(0);
7824             OPERATOR(ANDOP);
7825
7826         case KEY_atan2:
7827             LOP(OP_ATAN2,XTERM);
7828
7829         case KEY_bind:
7830             LOP(OP_BIND,XTERM);
7831
7832         case KEY_binmode:
7833             LOP(OP_BINMODE,XTERM);
7834
7835         case KEY_bless:
7836             LOP(OP_BLESS,XTERM);
7837
7838         case KEY_break:
7839             FUN0(OP_BREAK);
7840
7841         case KEY_chop:
7842             UNI(OP_CHOP);
7843
7844         case KEY_continue:
7845                     /* We have to disambiguate the two senses of
7846                       "continue". If the next token is a '{' then
7847                       treat it as the start of a continue block;
7848                       otherwise treat it as a control operator.
7849                      */
7850                     s = skipspace(s);
7851                     if (*s == '{')
7852             PREBLOCK(CONTINUE);
7853                     else
7854                         FUN0(OP_CONTINUE);
7855
7856         case KEY_chdir:
7857             /* may use HOME */
7858             (void)gv_fetchpvs("ENV", GV_ADD|GV_NOTQUAL, SVt_PVHV);
7859             UNI(OP_CHDIR);
7860
7861         case KEY_close:
7862             UNI(OP_CLOSE);
7863
7864         case KEY_closedir:
7865             UNI(OP_CLOSEDIR);
7866
7867         case KEY_cmp:
7868             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7869                 return REPORT(0);
7870             Eop(OP_SCMP);
7871
7872         case KEY_caller:
7873             UNI(OP_CALLER);
7874
7875         case KEY_crypt:
7876 #ifdef FCRYPT
7877             if (!PL_cryptseen) {
7878                 PL_cryptseen = TRUE;
7879                 init_des();
7880             }
7881 #endif
7882             LOP(OP_CRYPT,XTERM);
7883
7884         case KEY_chmod:
7885             LOP(OP_CHMOD,XTERM);
7886
7887         case KEY_chown:
7888             LOP(OP_CHOWN,XTERM);
7889
7890         case KEY_connect:
7891             LOP(OP_CONNECT,XTERM);
7892
7893         case KEY_chr:
7894             UNI(OP_CHR);
7895
7896         case KEY_cos:
7897             UNI(OP_COS);
7898
7899         case KEY_chroot:
7900             UNI(OP_CHROOT);
7901
7902         case KEY_default:
7903             PREBLOCK(DEFAULT);
7904
7905         case KEY_do:
7906             s = SKIPSPACE1(s);
7907             if (*s == '{')
7908                 PRETERMBLOCK(DO);
7909             if (*s != '\'') {
7910                 *PL_tokenbuf = '&';
7911                 d = scan_word(s, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
7912                               1, &len);
7913                 if (len && !keyword(PL_tokenbuf + 1, len, 0)) {
7914                     d = SKIPSPACE1(d);
7915                     if (*d == '(') {
7916                         force_ident_maybe_lex('&');
7917                         s = d;
7918                     }
7919                 }
7920             }
7921             if (orig_keyword == KEY_do) {
7922                 orig_keyword = 0;
7923                 pl_yylval.ival = 1;
7924             }
7925             else
7926                 pl_yylval.ival = 0;
7927             OPERATOR(DO);
7928
7929         case KEY_die:
7930             PL_hints |= HINT_BLOCK_SCOPE;
7931             LOP(OP_DIE,XTERM);
7932
7933         case KEY_defined:
7934             UNI(OP_DEFINED);
7935
7936         case KEY_delete:
7937             UNI(OP_DELETE);
7938
7939         case KEY_dbmopen:
7940             Perl_populate_isa(aTHX_ STR_WITH_LEN("AnyDBM_File::ISA"),
7941                               STR_WITH_LEN("NDBM_File::"),
7942                               STR_WITH_LEN("DB_File::"),
7943                               STR_WITH_LEN("GDBM_File::"),
7944                               STR_WITH_LEN("SDBM_File::"),
7945                               STR_WITH_LEN("ODBM_File::"),
7946                               NULL);
7947             LOP(OP_DBMOPEN,XTERM);
7948
7949         case KEY_dbmclose:
7950             UNI(OP_DBMCLOSE);
7951
7952         case KEY_dump:
7953             PL_expect = XOPERATOR;
7954             s = force_word(s,WORD,TRUE,FALSE);
7955             LOOPX(OP_DUMP);
7956
7957         case KEY_else:
7958             PREBLOCK(ELSE);
7959
7960         case KEY_elsif:
7961             pl_yylval.ival = CopLINE(PL_curcop);
7962             OPERATOR(ELSIF);
7963
7964         case KEY_eq:
7965             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
7966                 return REPORT(0);
7967             Eop(OP_SEQ);
7968
7969         case KEY_exists:
7970             UNI(OP_EXISTS);
7971         
7972         case KEY_exit:
7973             if (PL_madskills)
7974                 UNI(OP_INT);
7975             UNI(OP_EXIT);
7976
7977         case KEY_eval:
7978             s = SKIPSPACE1(s);
7979             if (*s == '{') { /* block eval */
7980                 PL_expect = XTERMBLOCK;
7981                 UNIBRACK(OP_ENTERTRY);
7982             }
7983             else { /* string eval */
7984                 PL_expect = XTERM;
7985                 UNIBRACK(OP_ENTEREVAL);
7986             }
7987
7988         case KEY_evalbytes:
7989             PL_expect = XTERM;
7990             UNIBRACK(-OP_ENTEREVAL);
7991
7992         case KEY_eof:
7993             UNI(OP_EOF);
7994
7995         case KEY_exp:
7996             UNI(OP_EXP);
7997
7998         case KEY_each:
7999             UNI(OP_EACH);
8000
8001         case KEY_exec:
8002             LOP(OP_EXEC,XREF);
8003
8004         case KEY_endhostent:
8005             FUN0(OP_EHOSTENT);
8006
8007         case KEY_endnetent:
8008             FUN0(OP_ENETENT);
8009
8010         case KEY_endservent:
8011             FUN0(OP_ESERVENT);
8012
8013         case KEY_endprotoent:
8014             FUN0(OP_EPROTOENT);
8015
8016         case KEY_endpwent:
8017             FUN0(OP_EPWENT);
8018
8019         case KEY_endgrent:
8020             FUN0(OP_EGRENT);
8021
8022         case KEY_for:
8023         case KEY_foreach:
8024             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8025                 return REPORT(0);
8026             pl_yylval.ival = CopLINE(PL_curcop);
8027             s = SKIPSPACE1(s);
8028             if (PL_expect == XSTATE && isIDFIRST_lazy_if(s,UTF)) {
8029                 char *p = s;
8030 #ifdef PERL_MAD
8031                 int soff = s - SvPVX(PL_linestr); /* for skipspace realloc */
8032 #endif
8033
8034                 if ((PL_bufend - p) >= 3 &&
8035                     strnEQ(p, "my", 2) && isSPACE(*(p + 2)))
8036                     p += 2;
8037                 else if ((PL_bufend - p) >= 4 &&
8038                     strnEQ(p, "our", 3) && isSPACE(*(p + 3)))
8039                     p += 3;
8040                 p = PEEKSPACE(p);
8041                 /* skip optional package name, as in "for my abc $x (..)" */
8042                 if (isIDFIRST_lazy_if(p,UTF)) {
8043                     p = scan_word(p, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
8044                     p = PEEKSPACE(p);
8045                 }
8046                 if (*p != '$')
8047                     Perl_croak(aTHX_ "Missing $ on loop variable");
8048 #ifdef PERL_MAD
8049                 s = SvPVX(PL_linestr) + soff;
8050 #endif
8051             }
8052             OPERATOR(FOR);
8053
8054         case KEY_formline:
8055             LOP(OP_FORMLINE,XTERM);
8056
8057         case KEY_fork:
8058             FUN0(OP_FORK);
8059
8060         case KEY_fc:
8061             UNI(OP_FC);
8062
8063         case KEY_fcntl:
8064             LOP(OP_FCNTL,XTERM);
8065
8066         case KEY_fileno:
8067             UNI(OP_FILENO);
8068
8069         case KEY_flock:
8070             LOP(OP_FLOCK,XTERM);
8071
8072         case KEY_gt:
8073             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8074                 return REPORT(0);
8075             Rop(OP_SGT);
8076
8077         case KEY_ge:
8078             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8079                 return REPORT(0);
8080             Rop(OP_SGE);
8081
8082         case KEY_grep:
8083             LOP(OP_GREPSTART, XREF);
8084
8085         case KEY_goto:
8086             PL_expect = XOPERATOR;
8087             s = force_word(s,WORD,TRUE,FALSE);
8088             LOOPX(OP_GOTO);
8089
8090         case KEY_gmtime:
8091             UNI(OP_GMTIME);
8092
8093         case KEY_getc:
8094             UNIDOR(OP_GETC);
8095
8096         case KEY_getppid:
8097             FUN0(OP_GETPPID);
8098
8099         case KEY_getpgrp:
8100             UNI(OP_GETPGRP);
8101
8102         case KEY_getpriority:
8103             LOP(OP_GETPRIORITY,XTERM);
8104
8105         case KEY_getprotobyname:
8106             UNI(OP_GPBYNAME);
8107
8108         case KEY_getprotobynumber:
8109             LOP(OP_GPBYNUMBER,XTERM);
8110
8111         case KEY_getprotoent:
8112             FUN0(OP_GPROTOENT);
8113
8114         case KEY_getpwent:
8115             FUN0(OP_GPWENT);
8116
8117         case KEY_getpwnam:
8118             UNI(OP_GPWNAM);
8119
8120         case KEY_getpwuid:
8121             UNI(OP_GPWUID);
8122
8123         case KEY_getpeername:
8124             UNI(OP_GETPEERNAME);
8125
8126         case KEY_gethostbyname:
8127             UNI(OP_GHBYNAME);
8128
8129         case KEY_gethostbyaddr:
8130             LOP(OP_GHBYADDR,XTERM);
8131
8132         case KEY_gethostent:
8133             FUN0(OP_GHOSTENT);
8134
8135         case KEY_getnetbyname:
8136             UNI(OP_GNBYNAME);
8137
8138         case KEY_getnetbyaddr:
8139             LOP(OP_GNBYADDR,XTERM);
8140
8141         case KEY_getnetent:
8142             FUN0(OP_GNETENT);
8143
8144         case KEY_getservbyname:
8145             LOP(OP_GSBYNAME,XTERM);
8146
8147         case KEY_getservbyport:
8148             LOP(OP_GSBYPORT,XTERM);
8149
8150         case KEY_getservent:
8151             FUN0(OP_GSERVENT);
8152
8153         case KEY_getsockname:
8154             UNI(OP_GETSOCKNAME);
8155
8156         case KEY_getsockopt:
8157             LOP(OP_GSOCKOPT,XTERM);
8158
8159         case KEY_getgrent:
8160             FUN0(OP_GGRENT);
8161
8162         case KEY_getgrnam:
8163             UNI(OP_GGRNAM);
8164
8165         case KEY_getgrgid:
8166             UNI(OP_GGRGID);
8167
8168         case KEY_getlogin:
8169             FUN0(OP_GETLOGIN);
8170
8171         case KEY_given:
8172             pl_yylval.ival = CopLINE(PL_curcop);
8173             Perl_ck_warner_d(aTHX_
8174                 packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
8175                 "given is experimental");
8176             OPERATOR(GIVEN);
8177
8178         case KEY_glob:
8179             LOP(
8180              orig_keyword==KEY_glob ? (orig_keyword=0, -OP_GLOB) : OP_GLOB,
8181              XTERM
8182             );
8183
8184         case KEY_hex:
8185             UNI(OP_HEX);
8186
8187         case KEY_if:
8188             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8189                 return REPORT(0);
8190             pl_yylval.ival = CopLINE(PL_curcop);
8191             OPERATOR(IF);
8192
8193         case KEY_index:
8194             LOP(OP_INDEX,XTERM);
8195
8196         case KEY_int:
8197             UNI(OP_INT);
8198
8199         case KEY_ioctl:
8200             LOP(OP_IOCTL,XTERM);
8201
8202         case KEY_join:
8203             LOP(OP_JOIN,XTERM);
8204
8205         case KEY_keys:
8206             UNI(OP_KEYS);
8207
8208         case KEY_kill:
8209             LOP(OP_KILL,XTERM);
8210
8211         case KEY_last:
8212             PL_expect = XOPERATOR;
8213             s = force_word(s,WORD,TRUE,FALSE);
8214             LOOPX(OP_LAST);
8215         
8216         case KEY_lc:
8217             UNI(OP_LC);
8218
8219         case KEY_lcfirst:
8220             UNI(OP_LCFIRST);
8221
8222         case KEY_local:
8223             pl_yylval.ival = 0;
8224             OPERATOR(LOCAL);
8225
8226         case KEY_length:
8227             UNI(OP_LENGTH);
8228
8229         case KEY_lt:
8230             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8231                 return REPORT(0);
8232             Rop(OP_SLT);
8233
8234         case KEY_le:
8235             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8236                 return REPORT(0);
8237             Rop(OP_SLE);
8238
8239         case KEY_localtime:
8240             UNI(OP_LOCALTIME);
8241
8242         case KEY_log:
8243             UNI(OP_LOG);
8244
8245         case KEY_link:
8246             LOP(OP_LINK,XTERM);
8247
8248         case KEY_listen:
8249             LOP(OP_LISTEN,XTERM);
8250
8251         case KEY_lock:
8252             UNI(OP_LOCK);
8253
8254         case KEY_lstat:
8255             UNI(OP_LSTAT);
8256
8257         case KEY_m:
8258             s = scan_pat(s,OP_MATCH);
8259             TERM(sublex_start());
8260
8261         case KEY_map:
8262             LOP(OP_MAPSTART, XREF);
8263
8264         case KEY_mkdir:
8265             LOP(OP_MKDIR,XTERM);
8266
8267         case KEY_msgctl:
8268             LOP(OP_MSGCTL,XTERM);
8269
8270         case KEY_msgget:
8271             LOP(OP_MSGGET,XTERM);
8272
8273         case KEY_msgrcv:
8274             LOP(OP_MSGRCV,XTERM);
8275
8276         case KEY_msgsnd:
8277             LOP(OP_MSGSND,XTERM);
8278
8279         case KEY_our:
8280         case KEY_my:
8281         case KEY_state:
8282             PL_in_my = (U16)tmp;
8283             s = SKIPSPACE1(s);
8284             if (isIDFIRST_lazy_if(s,UTF)) {
8285 #ifdef PERL_MAD
8286                 char* start = s;
8287 #endif
8288                 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
8289                 if (len == 3 && strnEQ(PL_tokenbuf, "sub", 3))
8290                 {
8291                     if (!FEATURE_LEXSUBS_IS_ENABLED)
8292                         Perl_croak(aTHX_
8293                                   "Experimental \"%s\" subs not enabled",
8294                                    tmp == KEY_my    ? "my"    :
8295                                    tmp == KEY_state ? "state" : "our");
8296                     Perl_ck_warner_d(aTHX_
8297                         packWARN(WARN_EXPERIMENTAL__LEXICAL_SUBS),
8298                         "The lexical_subs feature is experimental");
8299                     goto really_sub;
8300                 }
8301                 PL_in_my_stash = find_in_my_stash(PL_tokenbuf, len);
8302                 if (!PL_in_my_stash) {
8303                     char tmpbuf[1024];
8304                     PL_bufptr = s;
8305                     my_snprintf(tmpbuf, sizeof(tmpbuf), "No such class %.1000s", PL_tokenbuf);
8306                     yyerror_pv(tmpbuf, UTF ? SVf_UTF8 : 0);
8307                 }
8308 #ifdef PERL_MAD
8309                 if (PL_madskills) {     /* just add type to declarator token */
8310                     sv_catsv(PL_thistoken, PL_nextwhite);
8311                     PL_nextwhite = 0;
8312                     sv_catpvn(PL_thistoken, start, s - start);
8313                 }
8314 #endif
8315             }
8316             pl_yylval.ival = 1;
8317             OPERATOR(MY);
8318
8319         case KEY_next:
8320             PL_expect = XOPERATOR;
8321             s = force_word(s,WORD,TRUE,FALSE);
8322             LOOPX(OP_NEXT);
8323
8324         case KEY_ne:
8325             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_COMPARE)
8326                 return REPORT(0);
8327             Eop(OP_SNE);
8328
8329         case KEY_no:
8330             s = tokenize_use(0, s);
8331             TERM(USE);
8332
8333         case KEY_not:
8334             if (*s == '(' || (s = SKIPSPACE1(s), *s == '('))
8335                 FUN1(OP_NOT);
8336             else {
8337                 if (!PL_lex_allbrackets &&
8338                         PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC)
8339                     PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC;
8340                 OPERATOR(NOTOP);
8341             }
8342
8343         case KEY_open:
8344             s = SKIPSPACE1(s);
8345             if (isIDFIRST_lazy_if(s,UTF)) {
8346           const char *t;
8347           d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE,
8348               &len);
8349                 for (t=d; isSPACE(*t);)
8350                     t++;
8351                 if ( *t && strchr("|&*+-=!?:.", *t) && ckWARN_d(WARN_PRECEDENCE)
8352                     /* [perl #16184] */
8353                     && !(t[0] == '=' && t[1] == '>')
8354                     && !(t[0] == ':' && t[1] == ':')
8355                     && !keyword(s, d-s, 0)
8356                 ) {
8357                     Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
8358                        "Precedence problem: open %"UTF8f" should be open(%"UTF8f")",
8359                         UTF8fARG(UTF, d-s, s), UTF8fARG(UTF, d-s, s));
8360                 }
8361             }
8362             LOP(OP_OPEN,XTERM);
8363
8364         case KEY_or:
8365             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
8366                 return REPORT(0);
8367             pl_yylval.ival = OP_OR;
8368             OPERATOR(OROP);
8369
8370         case KEY_ord:
8371             UNI(OP_ORD);
8372
8373         case KEY_oct:
8374             UNI(OP_OCT);
8375
8376         case KEY_opendir:
8377             LOP(OP_OPEN_DIR,XTERM);
8378
8379         case KEY_print:
8380             checkcomma(s,PL_tokenbuf,"filehandle");
8381             LOP(OP_PRINT,XREF);
8382
8383         case KEY_printf:
8384             checkcomma(s,PL_tokenbuf,"filehandle");
8385             LOP(OP_PRTF,XREF);
8386
8387         case KEY_prototype:
8388             UNI(OP_PROTOTYPE);
8389
8390         case KEY_push:
8391             LOP(OP_PUSH,XTERM);
8392
8393         case KEY_pop:
8394             UNIDOR(OP_POP);
8395
8396         case KEY_pos:
8397             UNIDOR(OP_POS);
8398         
8399         case KEY_pack:
8400             LOP(OP_PACK,XTERM);
8401
8402         case KEY_package:
8403             s = force_word(s,WORD,FALSE,TRUE);
8404             s = SKIPSPACE1(s);
8405             s = force_strict_version(s);
8406             PL_lex_expect = XBLOCK;
8407             OPERATOR(PACKAGE);
8408
8409         case KEY_pipe:
8410             LOP(OP_PIPE_OP,XTERM);
8411
8412         case KEY_q:
8413             s = scan_str(s,!!PL_madskills,FALSE,FALSE, FALSE);
8414             COPLINE_SET_FROM_MULTI_END;
8415             if (!s)
8416                 missingterm(NULL);
8417             pl_yylval.ival = OP_CONST;
8418             TERM(sublex_start());
8419
8420         case KEY_quotemeta:
8421             UNI(OP_QUOTEMETA);
8422
8423         case KEY_qw: {
8424             OP *words = NULL;
8425             s = scan_str(s,!!PL_madskills,FALSE,FALSE, FALSE);
8426             COPLINE_SET_FROM_MULTI_END;
8427             if (!s)
8428                 missingterm(NULL);
8429             PL_expect = XOPERATOR;
8430             if (SvCUR(PL_lex_stuff)) {
8431                 int warned_comma = !ckWARN(WARN_QW);
8432                 int warned_comment = warned_comma;
8433                 d = SvPV_force(PL_lex_stuff, len);
8434                 while (len) {
8435                     for (; isSPACE(*d) && len; --len, ++d)
8436                         /**/;
8437                     if (len) {
8438                         SV *sv;
8439                         const char *b = d;
8440                         if (!warned_comma || !warned_comment) {
8441                             for (; !isSPACE(*d) && len; --len, ++d) {
8442                                 if (!warned_comma && *d == ',') {
8443                                     Perl_warner(aTHX_ packWARN(WARN_QW),
8444                                         "Possible attempt to separate words with commas");
8445                                     ++warned_comma;
8446                                 }
8447                                 else if (!warned_comment && *d == '#') {
8448                                     Perl_warner(aTHX_ packWARN(WARN_QW),
8449                                         "Possible attempt to put comments in qw() list");
8450                                     ++warned_comment;
8451                                 }
8452                             }
8453                         }
8454                         else {
8455                             for (; !isSPACE(*d) && len; --len, ++d)
8456                                 /**/;
8457                         }
8458                         sv = newSVpvn_utf8(b, d-b, DO_UTF8(PL_lex_stuff));
8459                         words = op_append_elem(OP_LIST, words,
8460                                             newSVOP(OP_CONST, 0, tokeq(sv)));
8461                     }
8462                 }
8463             }
8464             if (!words)
8465                 words = newNULLLIST();
8466             if (PL_lex_stuff) {
8467                 SvREFCNT_dec(PL_lex_stuff);
8468                 PL_lex_stuff = NULL;
8469             }
8470             PL_expect = XOPERATOR;
8471             pl_yylval.opval = sawparens(words);
8472             TOKEN(QWLIST);
8473         }
8474
8475         case KEY_qq:
8476             s = scan_str(s,!!PL_madskills,FALSE,FALSE, FALSE);
8477             if (!s)
8478                 missingterm(NULL);
8479             pl_yylval.ival = OP_STRINGIFY;
8480             if (SvIVX(PL_lex_stuff) == '\'')
8481                 SvIV_set(PL_lex_stuff, 0);      /* qq'$foo' should interpolate */
8482             TERM(sublex_start());
8483
8484         case KEY_qr:
8485             s = scan_pat(s,OP_QR);
8486             TERM(sublex_start());
8487
8488         case KEY_qx:
8489             s = scan_str(s,!!PL_madskills,FALSE,FALSE, FALSE);
8490             if (!s)
8491                 missingterm(NULL);
8492             readpipe_override();
8493             TERM(sublex_start());
8494
8495         case KEY_return:
8496             OLDLOP(OP_RETURN);
8497
8498         case KEY_require:
8499             s = SKIPSPACE1(s);
8500             PL_expect = XOPERATOR;
8501             if (isDIGIT(*s)) {
8502                 s = force_version(s, FALSE);
8503             }
8504             else if (*s != 'v' || !isDIGIT(s[1])
8505                     || (s = force_version(s, TRUE), *s == 'v'))
8506             {
8507                 *PL_tokenbuf = '\0';
8508                 s = force_word(s,WORD,TRUE,TRUE);
8509                 if (isIDFIRST_lazy_if(PL_tokenbuf,UTF))
8510                     gv_stashpvn(PL_tokenbuf, strlen(PL_tokenbuf),
8511                                 GV_ADD | (UTF ? SVf_UTF8 : 0));
8512                 else if (*s == '<')
8513                     yyerror("<> should be quotes");
8514             }
8515             if (orig_keyword == KEY_require) {
8516                 orig_keyword = 0;
8517                 pl_yylval.ival = 1;
8518             }
8519             else 
8520                 pl_yylval.ival = 0;
8521             PL_expect = XTERM;
8522             PL_bufptr = s;
8523             PL_last_uni = PL_oldbufptr;
8524             PL_last_lop_op = OP_REQUIRE;
8525             s = skipspace(s);
8526             return REPORT( (int)REQUIRE );
8527
8528         case KEY_reset:
8529             UNI(OP_RESET);
8530
8531         case KEY_redo:
8532             PL_expect = XOPERATOR;
8533             s = force_word(s,WORD,TRUE,FALSE);
8534             LOOPX(OP_REDO);
8535
8536         case KEY_rename:
8537             LOP(OP_RENAME,XTERM);
8538
8539         case KEY_rand:
8540             UNI(OP_RAND);
8541
8542         case KEY_rmdir:
8543             UNI(OP_RMDIR);
8544
8545         case KEY_rindex:
8546             LOP(OP_RINDEX,XTERM);
8547
8548         case KEY_read:
8549             LOP(OP_READ,XTERM);
8550
8551         case KEY_readdir:
8552             UNI(OP_READDIR);
8553
8554         case KEY_readline:
8555             UNIDOR(OP_READLINE);
8556
8557         case KEY_readpipe:
8558             UNIDOR(OP_BACKTICK);
8559
8560         case KEY_rewinddir:
8561             UNI(OP_REWINDDIR);
8562
8563         case KEY_recv:
8564             LOP(OP_RECV,XTERM);
8565
8566         case KEY_reverse:
8567             LOP(OP_REVERSE,XTERM);
8568
8569         case KEY_readlink:
8570             UNIDOR(OP_READLINK);
8571
8572         case KEY_ref:
8573             UNI(OP_REF);
8574
8575         case KEY_s:
8576             s = scan_subst(s);
8577             if (pl_yylval.opval)
8578                 TERM(sublex_start());
8579             else
8580                 TOKEN(1);       /* force error */
8581
8582         case KEY_say:
8583             checkcomma(s,PL_tokenbuf,"filehandle");
8584             LOP(OP_SAY,XREF);
8585
8586         case KEY_chomp:
8587             UNI(OP_CHOMP);
8588         
8589         case KEY_scalar:
8590             UNI(OP_SCALAR);
8591
8592         case KEY_select:
8593             LOP(OP_SELECT,XTERM);
8594
8595         case KEY_seek:
8596             LOP(OP_SEEK,XTERM);
8597
8598         case KEY_semctl:
8599             LOP(OP_SEMCTL,XTERM);
8600
8601         case KEY_semget:
8602             LOP(OP_SEMGET,XTERM);
8603
8604         case KEY_semop:
8605             LOP(OP_SEMOP,XTERM);
8606
8607         case KEY_send:
8608             LOP(OP_SEND,XTERM);
8609
8610         case KEY_setpgrp:
8611             LOP(OP_SETPGRP,XTERM);
8612
8613         case KEY_setpriority:
8614             LOP(OP_SETPRIORITY,XTERM);
8615
8616         case KEY_sethostent:
8617             UNI(OP_SHOSTENT);
8618
8619         case KEY_setnetent:
8620             UNI(OP_SNETENT);
8621
8622         case KEY_setservent:
8623             UNI(OP_SSERVENT);
8624
8625         case KEY_setprotoent:
8626             UNI(OP_SPROTOENT);
8627
8628         case KEY_setpwent:
8629             FUN0(OP_SPWENT);
8630
8631         case KEY_setgrent:
8632             FUN0(OP_SGRENT);
8633
8634         case KEY_seekdir:
8635             LOP(OP_SEEKDIR,XTERM);
8636
8637         case KEY_setsockopt:
8638             LOP(OP_SSOCKOPT,XTERM);
8639
8640         case KEY_shift:
8641             UNIDOR(OP_SHIFT);
8642
8643         case KEY_shmctl:
8644             LOP(OP_SHMCTL,XTERM);
8645
8646         case KEY_shmget:
8647             LOP(OP_SHMGET,XTERM);
8648
8649         case KEY_shmread:
8650             LOP(OP_SHMREAD,XTERM);
8651
8652         case KEY_shmwrite:
8653             LOP(OP_SHMWRITE,XTERM);
8654
8655         case KEY_shutdown:
8656             LOP(OP_SHUTDOWN,XTERM);
8657
8658         case KEY_sin:
8659             UNI(OP_SIN);
8660
8661         case KEY_sleep:
8662             UNI(OP_SLEEP);
8663
8664         case KEY_socket:
8665             LOP(OP_SOCKET,XTERM);
8666
8667         case KEY_socketpair:
8668             LOP(OP_SOCKPAIR,XTERM);
8669
8670         case KEY_sort:
8671             checkcomma(s,PL_tokenbuf,"subroutine name");
8672             s = SKIPSPACE1(s);
8673             PL_expect = XTERM;
8674             s = force_word(s,WORD,TRUE,TRUE);
8675             LOP(OP_SORT,XREF);
8676
8677         case KEY_split:
8678             LOP(OP_SPLIT,XTERM);
8679
8680         case KEY_sprintf:
8681             LOP(OP_SPRINTF,XTERM);
8682
8683         case KEY_splice:
8684             LOP(OP_SPLICE,XTERM);
8685
8686         case KEY_sqrt:
8687             UNI(OP_SQRT);
8688
8689         case KEY_srand:
8690             UNI(OP_SRAND);
8691
8692         case KEY_stat:
8693             UNI(OP_STAT);
8694
8695         case KEY_study:
8696             UNI(OP_STUDY);
8697
8698         case KEY_substr:
8699             LOP(OP_SUBSTR,XTERM);
8700
8701         case KEY_format:
8702         case KEY_sub:
8703           really_sub:
8704             {
8705                 char * const tmpbuf = PL_tokenbuf + 1;
8706                 expectation attrful;
8707                 bool have_name, have_proto;
8708                 const int key = tmp;
8709 #ifndef PERL_MAD
8710                 SV *format_name = NULL;
8711 #endif
8712
8713 #ifdef PERL_MAD
8714                 SV *tmpwhite = 0;
8715
8716                 char *tstart = SvPVX(PL_linestr) + PL_realtokenstart;
8717                 SV *subtoken = PL_madskills
8718                    ? newSVpvn_flags(tstart, s - tstart, SvUTF8(PL_linestr))
8719                    : NULL;
8720                 PL_thistoken = 0;
8721
8722                 d = s;
8723                 s = SKIPSPACE2(s,tmpwhite);
8724 #else
8725                 d = s;
8726                 s = skipspace(s);
8727 #endif
8728
8729                 if (isIDFIRST_lazy_if(s,UTF) || *s == '\'' ||
8730                     (*s == ':' && s[1] == ':'))
8731                 {
8732 #ifdef PERL_MAD
8733                     SV *nametoke = NULL;
8734 #endif
8735
8736                     PL_expect = XBLOCK;
8737                     attrful = XATTRBLOCK;
8738                     d = scan_word(s, tmpbuf, sizeof PL_tokenbuf - 1, TRUE,
8739                                   &len);
8740 #ifdef PERL_MAD
8741                     if (PL_madskills)
8742                         nametoke = newSVpvn_flags(s, d - s, SvUTF8(PL_linestr));
8743 #else
8744                     if (key == KEY_format)
8745                         format_name = S_newSV_maybe_utf8(aTHX_ s, d - s);
8746 #endif
8747                     *PL_tokenbuf = '&';
8748                     if (memchr(tmpbuf, ':', len) || key != KEY_sub
8749                      || pad_findmy_pvn(
8750                             PL_tokenbuf, len + 1, UTF ? SVf_UTF8 : 0
8751                         ) != NOT_IN_PAD)
8752                         sv_setpvn(PL_subname, tmpbuf, len);
8753                     else {
8754                         sv_setsv(PL_subname,PL_curstname);
8755                         sv_catpvs(PL_subname,"::");
8756                         sv_catpvn(PL_subname,tmpbuf,len);
8757                     }
8758                     if (SvUTF8(PL_linestr))
8759                         SvUTF8_on(PL_subname);
8760                     have_name = TRUE;
8761
8762
8763 #ifdef PERL_MAD
8764                     start_force(0);
8765                     CURMAD('X', nametoke);
8766                     CURMAD('_', tmpwhite);
8767                     force_ident_maybe_lex('&');
8768
8769                     s = SKIPSPACE2(d,tmpwhite);
8770 #else
8771                     s = skipspace(d);
8772 #endif
8773                 }
8774                 else {
8775                     if (key == KEY_my || key == KEY_our || key==KEY_state)
8776                     {
8777                         *d = '\0';
8778                         /* diag_listed_as: Missing name in "%s sub" */
8779                         Perl_croak(aTHX_
8780                                   "Missing name in \"%s\"", PL_bufptr);
8781                     }
8782                     PL_expect = XTERMBLOCK;
8783                     attrful = XATTRTERM;
8784                     sv_setpvs(PL_subname,"?");
8785                     have_name = FALSE;
8786                 }
8787
8788                 if (key == KEY_format) {
8789 #ifdef PERL_MAD
8790                     PL_thistoken = subtoken;
8791                     s = d;
8792 #else
8793                     if (format_name) {
8794                         start_force(PL_curforce);
8795                         NEXTVAL_NEXTTOKE.opval
8796                             = (OP*)newSVOP(OP_CONST,0, format_name);
8797                         NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
8798                         force_next(WORD);
8799                     }
8800 #endif
8801                     PREBLOCK(FORMAT);
8802                 }
8803
8804                 /* Look for a prototype */
8805                 if (*s == '(') {
8806                     s = scan_str(s,!!PL_madskills,FALSE,FALSE, FALSE);
8807                     COPLINE_SET_FROM_MULTI_END;
8808                     if (!s)
8809                         Perl_croak(aTHX_ "Prototype not terminated");
8810                     (void)validate_proto(PL_subname, PL_lex_stuff, ckWARN(WARN_ILLEGALPROTO));
8811                     have_proto = TRUE;
8812
8813 #ifdef PERL_MAD
8814                     start_force(0);
8815                     CURMAD('q', PL_thisopen);
8816                     CURMAD('_', tmpwhite);
8817                     CURMAD('=', PL_thisstuff);
8818                     CURMAD('Q', PL_thisclose);
8819                     NEXTVAL_NEXTTOKE.opval =
8820                         (OP*)newSVOP(OP_CONST, 0, PL_lex_stuff);
8821                     PL_lex_stuff = NULL;
8822                     force_next(THING);
8823
8824                     s = SKIPSPACE2(s,tmpwhite);
8825 #else
8826                     s = skipspace(s);
8827 #endif
8828                 }
8829                 else
8830                     have_proto = FALSE;
8831
8832                 if (*s == ':' && s[1] != ':')
8833                     PL_expect = attrful;
8834                 else if (*s != '{' && key == KEY_sub) {
8835                     if (!have_name)
8836                         Perl_croak(aTHX_ "Illegal declaration of anonymous subroutine");
8837                     else if (*s != ';' && *s != '}')
8838                         Perl_croak(aTHX_ "Illegal declaration of subroutine %"SVf, SVfARG(PL_subname));
8839                 }
8840
8841 #ifdef PERL_MAD
8842                 start_force(0);
8843                 if (tmpwhite) {
8844                     if (PL_madskills)
8845                         curmad('^', newSVpvs(""));
8846                     CURMAD('_', tmpwhite);
8847                 }
8848                 force_next(0);
8849
8850                 PL_thistoken = subtoken;
8851                 PERL_UNUSED_VAR(have_proto);
8852 #else
8853                 if (have_proto) {
8854                     NEXTVAL_NEXTTOKE.opval =
8855                         (OP*)newSVOP(OP_CONST, 0, PL_lex_stuff);
8856                     PL_lex_stuff = NULL;
8857                     force_next(THING);
8858                 }
8859 #endif
8860                 if (!have_name) {
8861                     if (PL_curstash)
8862                         sv_setpvs(PL_subname, "__ANON__");
8863                     else
8864                         sv_setpvs(PL_subname, "__ANON__::__ANON__");
8865                     TOKEN(ANONSUB);
8866                 }
8867 #ifndef PERL_MAD
8868                 force_ident_maybe_lex('&');
8869 #endif
8870                 TOKEN(SUB);
8871             }
8872
8873         case KEY_system:
8874             LOP(OP_SYSTEM,XREF);
8875
8876         case KEY_symlink:
8877             LOP(OP_SYMLINK,XTERM);
8878
8879         case KEY_syscall:
8880             LOP(OP_SYSCALL,XTERM);
8881
8882         case KEY_sysopen:
8883             LOP(OP_SYSOPEN,XTERM);
8884
8885         case KEY_sysseek:
8886             LOP(OP_SYSSEEK,XTERM);
8887
8888         case KEY_sysread:
8889             LOP(OP_SYSREAD,XTERM);
8890
8891         case KEY_syswrite:
8892             LOP(OP_SYSWRITE,XTERM);
8893
8894         case KEY_tr:
8895         case KEY_y:
8896             s = scan_trans(s);
8897             TERM(sublex_start());
8898
8899         case KEY_tell:
8900             UNI(OP_TELL);
8901
8902         case KEY_telldir:
8903             UNI(OP_TELLDIR);
8904
8905         case KEY_tie:
8906             LOP(OP_TIE,XTERM);
8907
8908         case KEY_tied:
8909             UNI(OP_TIED);
8910
8911         case KEY_time:
8912             FUN0(OP_TIME);
8913
8914         case KEY_times:
8915             FUN0(OP_TMS);
8916
8917         case KEY_truncate:
8918             LOP(OP_TRUNCATE,XTERM);
8919
8920         case KEY_uc:
8921             UNI(OP_UC);
8922
8923         case KEY_ucfirst:
8924             UNI(OP_UCFIRST);
8925
8926         case KEY_untie:
8927             UNI(OP_UNTIE);
8928
8929         case KEY_until:
8930             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8931                 return REPORT(0);
8932             pl_yylval.ival = CopLINE(PL_curcop);
8933             OPERATOR(UNTIL);
8934
8935         case KEY_unless:
8936             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8937                 return REPORT(0);
8938             pl_yylval.ival = CopLINE(PL_curcop);
8939             OPERATOR(UNLESS);
8940
8941         case KEY_unlink:
8942             LOP(OP_UNLINK,XTERM);
8943
8944         case KEY_undef:
8945             UNIDOR(OP_UNDEF);
8946
8947         case KEY_unpack:
8948             LOP(OP_UNPACK,XTERM);
8949
8950         case KEY_utime:
8951             LOP(OP_UTIME,XTERM);
8952
8953         case KEY_umask:
8954             UNIDOR(OP_UMASK);
8955
8956         case KEY_unshift:
8957             LOP(OP_UNSHIFT,XTERM);
8958
8959         case KEY_use:
8960             s = tokenize_use(1, s);
8961             OPERATOR(USE);
8962
8963         case KEY_values:
8964             UNI(OP_VALUES);
8965
8966         case KEY_vec:
8967             LOP(OP_VEC,XTERM);
8968
8969         case KEY_when:
8970             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8971                 return REPORT(0);
8972             pl_yylval.ival = CopLINE(PL_curcop);
8973             Perl_ck_warner_d(aTHX_
8974                 packWARN(WARN_EXPERIMENTAL__SMARTMATCH),
8975                 "when is experimental");
8976             OPERATOR(WHEN);
8977
8978         case KEY_while:
8979             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_NONEXPR)
8980                 return REPORT(0);
8981             pl_yylval.ival = CopLINE(PL_curcop);
8982             OPERATOR(WHILE);
8983
8984         case KEY_warn:
8985             PL_hints |= HINT_BLOCK_SCOPE;
8986             LOP(OP_WARN,XTERM);
8987
8988         case KEY_wait:
8989             FUN0(OP_WAIT);
8990
8991         case KEY_waitpid:
8992             LOP(OP_WAITPID,XTERM);
8993
8994         case KEY_wantarray:
8995             FUN0(OP_WANTARRAY);
8996
8997         case KEY_write:
8998             /* Make sure $^L is defined. 0x0C is CTRL-L on ASCII platforms, and
8999              * we use the same number on EBCDIC */
9000             gv_fetchpvs("\x0C", GV_ADD|GV_NOTQUAL, SVt_PV);
9001             UNI(OP_ENTERWRITE);
9002
9003         case KEY_x:
9004             if (PL_expect == XOPERATOR) {
9005                 if (*s == '=' && !PL_lex_allbrackets &&
9006                         PL_lex_fakeeof >= LEX_FAKEEOF_ASSIGN)
9007                     return REPORT(0);
9008                 Mop(OP_REPEAT);
9009             }
9010             check_uni();
9011             goto just_a_word;
9012
9013         case KEY_xor:
9014             if (!PL_lex_allbrackets && PL_lex_fakeeof >= LEX_FAKEEOF_LOWLOGIC)
9015                 return REPORT(0);
9016             pl_yylval.ival = OP_XOR;
9017             OPERATOR(OROP);
9018         }
9019     }}
9020 }
9021
9022 /*
9023   S_pending_ident
9024
9025   Looks up an identifier in the pad or in a package
9026
9027   Returns:
9028     PRIVATEREF if this is a lexical name.
9029     WORD       if this belongs to a package.
9030
9031   Structure:
9032       if we're in a my declaration
9033           croak if they tried to say my($foo::bar)
9034           build the ops for a my() declaration
9035       if it's an access to a my() variable
9036           build ops for access to a my() variable
9037       if in a dq string, and they've said @foo and we can't find @foo
9038           warn
9039       build ops for a bareword
9040 */
9041
9042 static int
9043 S_pending_ident(pTHX)
9044 {
9045     dVAR;
9046     PADOFFSET tmp = 0;
9047     const char pit = (char)pl_yylval.ival;
9048     const STRLEN tokenbuf_len = strlen(PL_tokenbuf);
9049     /* All routes through this function want to know if there is a colon.  */
9050     const char *const has_colon = (const char*) memchr (PL_tokenbuf, ':', tokenbuf_len);
9051
9052     DEBUG_T({ PerlIO_printf(Perl_debug_log,
9053           "### Pending identifier '%s'\n", PL_tokenbuf); });
9054
9055     /* if we're in a my(), we can't allow dynamics here.
9056        $foo'bar has already been turned into $foo::bar, so
9057        just check for colons.
9058
9059        if it's a legal name, the OP is a PADANY.
9060     */
9061     if (PL_in_my) {
9062         if (PL_in_my == KEY_our) {      /* "our" is merely analogous to "my" */
9063             if (has_colon)
9064                 yyerror_pv(Perl_form(aTHX_ "No package name allowed for "
9065                                   "variable %s in \"our\"",
9066                                   PL_tokenbuf), UTF ? SVf_UTF8 : 0);
9067             tmp = allocmy(PL_tokenbuf, tokenbuf_len, UTF ? SVf_UTF8 : 0);
9068         }
9069         else {
9070             if (has_colon)
9071                 yyerror_pv(Perl_form(aTHX_ PL_no_myglob,
9072                             PL_in_my == KEY_my ? "my" : "state", PL_tokenbuf),
9073                             UTF ? SVf_UTF8 : 0);
9074
9075             pl_yylval.opval = newOP(OP_PADANY, 0);
9076             pl_yylval.opval->op_targ = allocmy(PL_tokenbuf, tokenbuf_len,
9077                                                         UTF ? SVf_UTF8 : 0);
9078             return PRIVATEREF;
9079         }
9080     }
9081
9082     /*
9083        build the ops for accesses to a my() variable.
9084     */
9085
9086     if (!has_colon) {
9087         if (!PL_in_my)
9088             tmp = pad_findmy_pvn(PL_tokenbuf, tokenbuf_len,
9089                                     UTF ? SVf_UTF8 : 0);
9090         if (tmp != NOT_IN_PAD) {
9091             /* might be an "our" variable" */
9092             if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
9093                 /* build ops for a bareword */
9094                 HV *  const stash = PAD_COMPNAME_OURSTASH(tmp);
9095                 HEK * const stashname = HvNAME_HEK(stash);
9096                 SV *  const sym = newSVhek(stashname);
9097                 sv_catpvs(sym, "::");
9098                 sv_catpvn_flags(sym, PL_tokenbuf+1, tokenbuf_len - 1, (UTF ? SV_CATUTF8 : SV_CATBYTES ));
9099                 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sym);
9100                 pl_yylval.opval->op_private = OPpCONST_ENTERED;
9101                 if (pit != '&')
9102                   gv_fetchsv(sym,
9103                     (PL_in_eval
9104                         ? (GV_ADDMULTI | GV_ADDINEVAL)
9105                         : GV_ADDMULTI
9106                     ),
9107                     ((PL_tokenbuf[0] == '$') ? SVt_PV
9108                      : (PL_tokenbuf[0] == '@') ? SVt_PVAV
9109                      : SVt_PVHV));
9110                 return WORD;
9111             }
9112
9113             pl_yylval.opval = newOP(OP_PADANY, 0);
9114             pl_yylval.opval->op_targ = tmp;
9115             return PRIVATEREF;
9116         }
9117     }
9118
9119     /*
9120        Whine if they've said @foo in a doublequoted string,
9121        and @foo isn't a variable we can find in the symbol
9122        table.
9123     */
9124     if (ckWARN(WARN_AMBIGUOUS) &&
9125         pit == '@' && PL_lex_state != LEX_NORMAL && !PL_lex_brackets) {
9126         GV *const gv = gv_fetchpvn_flags(PL_tokenbuf + 1, tokenbuf_len - 1,
9127                                         ( UTF ? SVf_UTF8 : 0 ), SVt_PVAV);
9128         if ((!gv || ((PL_tokenbuf[0] == '@') ? !GvAV(gv) : !GvHV(gv)))
9129                 /* DO NOT warn for @- and @+ */
9130                 && !( PL_tokenbuf[2] == '\0' &&
9131                     ( PL_tokenbuf[1] == '-' || PL_tokenbuf[1] == '+' ))
9132            )
9133         {
9134             /* Downgraded from fatal to warning 20000522 mjd */
9135             Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
9136                         "Possible unintended interpolation of %"UTF8f
9137                         " in string",
9138                         UTF8fARG(UTF, tokenbuf_len, PL_tokenbuf));
9139         }
9140     }
9141
9142     /* build ops for a bareword */
9143     pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0,
9144                                    newSVpvn_flags(PL_tokenbuf + 1,
9145                                                       tokenbuf_len - 1,
9146                                                       UTF ? SVf_UTF8 : 0 ));
9147     pl_yylval.opval->op_private = OPpCONST_ENTERED;
9148     if (pit != '&')
9149         gv_fetchpvn_flags(PL_tokenbuf+1, tokenbuf_len - 1,
9150                      (PL_in_eval ? (GV_ADDMULTI | GV_ADDINEVAL) : GV_ADD)
9151                      | ( UTF ? SVf_UTF8 : 0 ),
9152                      ((PL_tokenbuf[0] == '$') ? SVt_PV
9153                       : (PL_tokenbuf[0] == '@') ? SVt_PVAV
9154                       : SVt_PVHV));
9155     return WORD;
9156 }
9157
9158 STATIC void
9159 S_checkcomma(pTHX_ const char *s, const char *name, const char *what)
9160 {
9161     dVAR;
9162
9163     PERL_ARGS_ASSERT_CHECKCOMMA;
9164
9165     if (*s == ' ' && s[1] == '(') {     /* XXX gotta be a better way */
9166         if (ckWARN(WARN_SYNTAX)) {
9167             int level = 1;
9168             const char *w;
9169             for (w = s+2; *w && level; w++) {
9170                 if (*w == '(')
9171                     ++level;
9172                 else if (*w == ')')
9173                     --level;
9174             }
9175             while (isSPACE(*w))
9176                 ++w;
9177             /* the list of chars below is for end of statements or
9178              * block / parens, boolean operators (&&, ||, //) and branch
9179              * constructs (or, and, if, until, unless, while, err, for).
9180              * Not a very solid hack... */
9181             if (!*w || !strchr(";&/|})]oaiuwef!=", *w))
9182                 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9183                             "%s (...) interpreted as function",name);
9184         }
9185     }
9186     while (s < PL_bufend && isSPACE(*s))
9187         s++;
9188     if (*s == '(')
9189         s++;
9190     while (s < PL_bufend && isSPACE(*s))
9191         s++;
9192     if (isIDFIRST_lazy_if(s,UTF)) {
9193         const char * const w = s;
9194         s += UTF ? UTF8SKIP(s) : 1;
9195         while (isWORDCHAR_lazy_if(s,UTF))
9196             s += UTF ? UTF8SKIP(s) : 1;
9197         while (s < PL_bufend && isSPACE(*s))
9198             s++;
9199         if (*s == ',') {
9200             GV* gv;
9201             if (keyword(w, s - w, 0))
9202                 return;
9203
9204             gv = gv_fetchpvn_flags(w, s - w, ( UTF ? SVf_UTF8 : 0 ), SVt_PVCV);
9205             if (gv && GvCVu(gv))
9206                 return;
9207             Perl_croak(aTHX_ "No comma allowed after %s", what);
9208         }
9209     }
9210 }
9211
9212 /* S_new_constant(): do any overload::constant lookup.
9213
9214    Either returns sv, or mortalizes/frees sv and returns a new SV*.
9215    Best used as sv=new_constant(..., sv, ...).
9216    If s, pv are NULL, calls subroutine with one argument,
9217    and <type> is used with error messages only.
9218    <type> is assumed to be well formed UTF-8 */
9219
9220 STATIC SV *
9221 S_new_constant(pTHX_ const char *s, STRLEN len, const char *key, STRLEN keylen,
9222                SV *sv, SV *pv, const char *type, STRLEN typelen)
9223 {
9224     dVAR; dSP;
9225     HV * table = GvHV(PL_hintgv);                /* ^H */
9226     SV *res;
9227     SV *errsv = NULL;
9228     SV **cvp;
9229     SV *cv, *typesv;
9230     const char *why1 = "", *why2 = "", *why3 = "";
9231
9232     PERL_ARGS_ASSERT_NEW_CONSTANT;
9233     /* We assume that this is true: */
9234     if (*key == 'c') { assert (strEQ(key, "charnames")); }
9235     assert(type || s);
9236
9237     /* charnames doesn't work well if there have been errors found */
9238     if (PL_error_count > 0 && *key == 'c')
9239     {
9240         SvREFCNT_dec_NN(sv);
9241         return &PL_sv_undef;
9242     }
9243
9244     sv_2mortal(sv);                     /* Parent created it permanently */
9245     if (!table
9246         || ! (PL_hints & HINT_LOCALIZE_HH)
9247         || ! (cvp = hv_fetch(table, key, keylen, FALSE))
9248         || ! SvOK(*cvp))
9249     {
9250         char *msg;
9251         
9252         /* Here haven't found what we're looking for.  If it is charnames,
9253          * perhaps it needs to be loaded.  Try doing that before giving up */
9254         if (*key == 'c') {
9255             Perl_load_module(aTHX_
9256                             0,
9257                             newSVpvs("_charnames"),
9258                              /* version parameter; no need to specify it, as if
9259                               * we get too early a version, will fail anyway,
9260                               * not being able to find '_charnames' */
9261                             NULL,
9262                             newSVpvs(":full"),
9263                             newSVpvs(":short"),
9264                             NULL);
9265             SPAGAIN;
9266             table = GvHV(PL_hintgv);
9267             if (table
9268                 && (PL_hints & HINT_LOCALIZE_HH)
9269                 && (cvp = hv_fetch(table, key, keylen, FALSE))
9270                 && SvOK(*cvp))
9271             {
9272                 goto now_ok;
9273             }
9274         }
9275         if (!table || !(PL_hints & HINT_LOCALIZE_HH)) {
9276             msg = Perl_form(aTHX_
9277                                "Constant(%.*s) unknown",
9278                                 (int)(type ? typelen : len),
9279                                 (type ? type: s));
9280         }
9281         else {
9282             why1 = "$^H{";
9283             why2 = key;
9284             why3 = "} is not defined";
9285         report:
9286             if (*key == 'c') {
9287                 msg = Perl_form(aTHX_
9288                             /* The +3 is for '\N{'; -4 for that, plus '}' */
9289                             "Unknown charname '%.*s'", (int)typelen - 4, type + 3
9290                       );
9291             }
9292             else {
9293                 msg = Perl_form(aTHX_ "Constant(%.*s): %s%s%s",
9294                                     (int)(type ? typelen : len),
9295                                     (type ? type: s), why1, why2, why3);
9296             }
9297         }
9298         yyerror_pv(msg, UTF ? SVf_UTF8 : 0);
9299         return SvREFCNT_inc_simple_NN(sv);
9300     }
9301 now_ok:
9302     cv = *cvp;
9303     if (!pv && s)
9304         pv = newSVpvn_flags(s, len, SVs_TEMP);
9305     if (type && pv)
9306         typesv = newSVpvn_flags(type, typelen, SVs_TEMP);
9307     else
9308         typesv = &PL_sv_undef;
9309
9310     PUSHSTACKi(PERLSI_OVERLOAD);
9311     ENTER ;
9312     SAVETMPS;
9313
9314     PUSHMARK(SP) ;
9315     EXTEND(sp, 3);
9316     if (pv)
9317         PUSHs(pv);
9318     PUSHs(sv);
9319     if (pv)
9320         PUSHs(typesv);
9321     PUTBACK;
9322     call_sv(cv, G_SCALAR | ( PL_in_eval ? 0 : G_EVAL));
9323
9324     SPAGAIN ;
9325
9326     /* Check the eval first */
9327     if (!PL_in_eval && ((errsv = ERRSV), SvTRUE_NN(errsv))) {
9328         STRLEN errlen;
9329         const char * errstr;
9330         sv_catpvs(errsv, "Propagated");
9331         errstr = SvPV_const(errsv, errlen);
9332         yyerror_pvn(errstr, errlen, 0); /* Duplicates the message inside eval */
9333         (void)POPs;
9334         res = SvREFCNT_inc_simple_NN(sv);
9335     }
9336     else {
9337         res = POPs;
9338         SvREFCNT_inc_simple_void_NN(res);
9339     }
9340
9341     PUTBACK ;
9342     FREETMPS ;
9343     LEAVE ;
9344     POPSTACK;
9345
9346     if (!SvOK(res)) {
9347         why1 = "Call to &{$^H{";
9348         why2 = key;
9349         why3 = "}} did not return a defined value";
9350         sv = res;
9351         (void)sv_2mortal(sv);
9352         goto report;
9353     }
9354
9355     return res;
9356 }
9357
9358 PERL_STATIC_INLINE void
9359 S_parse_ident(pTHX_ char **s, char **d, char * const e, int allow_package, bool is_utf8) {
9360     dVAR;
9361     PERL_ARGS_ASSERT_PARSE_IDENT;
9362
9363     for (;;) {
9364         if (*d >= e)
9365             Perl_croak(aTHX_ "%s", ident_too_long);
9366         if (is_utf8 && isIDFIRST_utf8((U8*)*s)) {
9367              /* The UTF-8 case must come first, otherwise things
9368              * like c\N{COMBINING TILDE} would start failing, as the
9369              * isWORDCHAR_A case below would gobble the 'c' up.
9370              */
9371
9372             char *t = *s + UTF8SKIP(*s);
9373             while (isIDCONT_utf8((U8*)t))
9374                 t += UTF8SKIP(t);
9375             if (*d + (t - *s) > e)
9376                 Perl_croak(aTHX_ "%s", ident_too_long);
9377             Copy(*s, *d, t - *s, char);
9378             *d += t - *s;
9379             *s = t;
9380         }
9381         else if ( isWORDCHAR_A(**s) ) {
9382             do {
9383                 *(*d)++ = *(*s)++;
9384             } while (isWORDCHAR_A(**s) && *d < e);
9385         }
9386         else if (allow_package && **s == '\'' && isIDFIRST_lazy_if(*s+1,is_utf8)) {
9387             *(*d)++ = ':';
9388             *(*d)++ = ':';
9389             (*s)++;
9390         }
9391         else if (allow_package && **s == ':' && (*s)[1] == ':'
9392            /* Disallow things like Foo::$bar. For the curious, this is
9393             * the code path that triggers the "Bad name after" warning
9394             * when looking for barewords.
9395             */
9396            && (*s)[2] != '$') {
9397             *(*d)++ = *(*s)++;
9398             *(*d)++ = *(*s)++;
9399         }
9400         else
9401             break;
9402     }
9403     return;
9404 }
9405
9406 /* Returns a NUL terminated string, with the length of the string written to
9407    *slp
9408    */
9409 STATIC char *
9410 S_scan_word(pTHX_ char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp)
9411 {
9412     dVAR;
9413     char *d = dest;
9414     char * const e = d + destlen - 3;  /* two-character token, ending NUL */
9415     bool is_utf8 = cBOOL(UTF);
9416
9417     PERL_ARGS_ASSERT_SCAN_WORD;
9418
9419     parse_ident(&s, &d, e, allow_package, is_utf8);
9420     *d = '\0';
9421     *slp = d - dest;
9422     return s;
9423 }
9424
9425 STATIC char *
9426 S_scan_ident(pTHX_ char *s, char *dest, STRLEN destlen, I32 ck_uni)
9427 {
9428     dVAR;
9429     I32 herelines = PL_parser->herelines;
9430     SSize_t bracket = -1;
9431     char funny = *s++;
9432     char *d = dest;
9433     char * const e = d + destlen - 3;    /* two-character token, ending NUL */
9434     bool is_utf8 = cBOOL(UTF);
9435     I32 orig_copline = 0, tmp_copline = 0;
9436
9437     PERL_ARGS_ASSERT_SCAN_IDENT;
9438
9439     if (isSPACE(*s))
9440         s = PEEKSPACE(s);
9441     if (isDIGIT(*s)) {
9442         while (isDIGIT(*s)) {
9443             if (d >= e)
9444                 Perl_croak(aTHX_ "%s", ident_too_long);
9445             *d++ = *s++;
9446         }
9447     }
9448     else {
9449         parse_ident(&s, &d, e, 1, is_utf8);
9450     }
9451     *d = '\0';
9452     d = dest;
9453     if (*d) {
9454         /* Either a digit variable, or parse_ident() found an identifier
9455            (anything valid as a bareword), so job done and return.  */
9456         if (PL_lex_state != LEX_NORMAL)
9457             PL_lex_state = LEX_INTERPENDMAYBE;
9458         return s;
9459     }
9460     if (*s == '$' && s[1] &&
9461       (isIDFIRST_lazy_if(s+1,is_utf8)
9462          || isDIGIT_A((U8)s[1])
9463          || s[1] == '$'
9464          || s[1] == '{'
9465          || strnEQ(s+1,"::",2)) )
9466     {
9467         /* Dereferencing a value in a scalar variable.
9468            The alternatives are different syntaxes for a scalar variable.
9469            Using ' as a leading package separator isn't allowed. :: is.   */
9470         return s;
9471     }
9472     /* Handle the opening { of @{...}, &{...}, *{...}, %{...}, ${...}  */
9473     if (*s == '{') {
9474         bracket = s - SvPVX(PL_linestr);
9475         s++;
9476         orig_copline = CopLINE(PL_curcop);
9477         if (s < PL_bufend && isSPACE(*s)) {
9478             s = PEEKSPACE(s);
9479         }
9480     }
9481
9482 /* Is the byte 'd' a legal single character identifier name?  'u' is true
9483  * iff Unicode semantics are to be used.  The legal ones are any of:
9484  *  a) ASCII digits
9485  *  b) ASCII punctuation
9486  *  c) When not under Unicode rules, any upper Latin1 character
9487  *  d) \c?, \c\, \c^, \c_, and \cA..\cZ, minus the ones that have traditionally
9488  *     been matched by \s on ASCII platforms.  That is: \c?, plus 1-32, minus
9489  *     the \s ones. */
9490 #define VALID_LEN_ONE_IDENT(d, u) (isPUNCT_A((U8)(d))                       \
9491                                    || isDIGIT_A((U8)(d))                    \
9492                                    || (!(u) && !isASCII((U8)(d)))           \
9493                                    || ((((U8)(d)) < 32)                     \
9494                                        && (((((U8)(d)) >= 14)               \
9495                                            || (((U8)(d)) <= 8 && (d) != 0) \
9496                                            || (((U8)(d)) == 13))))          \
9497                                    || (((U8)(d)) == toCTRL('?')))
9498     if (s < PL_bufend
9499         && (isIDFIRST_lazy_if(s, is_utf8) || VALID_LEN_ONE_IDENT(*s, is_utf8)))
9500     {
9501         if ( isCNTRL_A((U8)*s) ) {
9502             deprecate("literal control characters in variable names");
9503         }
9504         
9505         if (is_utf8) {
9506             const STRLEN skip = UTF8SKIP(s);
9507             STRLEN i;
9508             d[skip] = '\0';
9509             for ( i = 0; i < skip; i++ )
9510                 d[i] = *s++;
9511         }
9512         else {
9513             *d = *s++;
9514             d[1] = '\0';
9515         }
9516     }
9517     /* Convert $^F, ${^F} and the ^F of ${^FOO} to control characters */
9518     if (*d == '^' && *s && isCONTROLVAR(*s)) {
9519         *d = toCTRL(*s);
9520         s++;
9521     }
9522     /* Warn about ambiguous code after unary operators if {...} notation isn't
9523        used.  There's no difference in ambiguity; it's merely a heuristic
9524        about when not to warn.  */
9525     else if (ck_uni && bracket == -1)
9526         check_uni();
9527     if (bracket != -1) {
9528         /* If we were processing {...} notation then...  */
9529         if (isIDFIRST_lazy_if(d,is_utf8)) {
9530             /* if it starts as a valid identifier, assume that it is one.
9531                (the later check for } being at the expected point will trap
9532                cases where this doesn't pan out.)  */
9533         d += is_utf8 ? UTF8SKIP(d) : 1;
9534         parse_ident(&s, &d, e, 1, is_utf8);
9535             *d = '\0';
9536             tmp_copline = CopLINE(PL_curcop);
9537             if (s < PL_bufend && isSPACE(*s)) {
9538                 s = PEEKSPACE(s);
9539             }
9540             if ((*s == '[' || (*s == '{' && strNE(dest, "sub")))) {
9541                 /* ${foo[0]} and ${foo{bar}} notation.  */
9542                 if (ckWARN(WARN_AMBIGUOUS) && keyword(dest, d - dest, 0)) {
9543                     const char * const brack =
9544                         (const char *)
9545                         ((*s == '[') ? "[...]" : "{...}");
9546                     orig_copline = CopLINE(PL_curcop);
9547                     CopLINE_set(PL_curcop, tmp_copline);
9548    /* diag_listed_as: Ambiguous use of %c{%s[...]} resolved to %c%s[...] */
9549                     Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
9550                         "Ambiguous use of %c{%s%s} resolved to %c%s%s",
9551                         funny, dest, brack, funny, dest, brack);
9552                     CopLINE_set(PL_curcop, orig_copline);
9553                 }
9554                 bracket++;
9555                 PL_lex_brackstack[PL_lex_brackets++] = (char)(XOPERATOR | XFAKEBRACK);
9556                 PL_lex_allbrackets++;
9557                 return s;
9558             }
9559         }
9560         /* Handle extended ${^Foo} variables
9561          * 1999-02-27 mjd-perl-patch@plover.com */
9562         else if (! isPRINT(*d) /* isCNTRL(d), plus all non-ASCII */
9563                  && isWORDCHAR(*s))
9564         {
9565             d++;
9566             while (isWORDCHAR(*s) && d < e) {
9567                 *d++ = *s++;
9568             }
9569             if (d >= e)
9570                 Perl_croak(aTHX_ "%s", ident_too_long);
9571             *d = '\0';
9572         }
9573
9574         if ( !tmp_copline )
9575             tmp_copline = CopLINE(PL_curcop);
9576         if (s < PL_bufend && isSPACE(*s)) {
9577             s = PEEKSPACE(s);
9578         }
9579             
9580         /* Expect to find a closing } after consuming any trailing whitespace.
9581          */
9582         if (*s == '}') {
9583             s++;
9584             if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) {
9585                 PL_lex_state = LEX_INTERPEND;
9586                 PL_expect = XREF;
9587             }
9588             if (PL_lex_state == LEX_NORMAL) {
9589                 if (ckWARN(WARN_AMBIGUOUS) &&
9590                     (keyword(dest, d - dest, 0)
9591                      || get_cvn_flags(dest, d - dest, is_utf8 ? SVf_UTF8 : 0)))
9592                 {
9593                     SV *tmp = newSVpvn_flags( dest, d - dest,
9594                                             SVs_TEMP | (is_utf8 ? SVf_UTF8 : 0) );
9595                     if (funny == '#')
9596                         funny = '@';
9597                     orig_copline = CopLINE(PL_curcop);
9598                     CopLINE_set(PL_curcop, tmp_copline);
9599                     Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
9600                         "Ambiguous use of %c{%"SVf"} resolved to %c%"SVf,
9601                         funny, tmp, funny, tmp);
9602                     CopLINE_set(PL_curcop, orig_copline);
9603                 }
9604             }
9605         }
9606         else {
9607             /* Didn't find the closing } at the point we expected, so restore
9608                state such that the next thing to process is the opening { and */
9609             s = SvPVX(PL_linestr) + bracket; /* let the parser handle it */
9610             CopLINE_set(PL_curcop, orig_copline);
9611             PL_parser->herelines = herelines;
9612             *dest = '\0';
9613         }
9614     }
9615     else if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets && !intuit_more(s))
9616         PL_lex_state = LEX_INTERPEND;
9617     return s;
9618 }
9619
9620 static bool
9621 S_pmflag(pTHX_ const char* const valid_flags, U32 * pmfl, char** s, char* charset) {
9622
9623     /* Adds, subtracts to/from 'pmfl' based on regex modifier flags found in
9624      * the parse starting at 's', based on the subset that are valid in this
9625      * context input to this routine in 'valid_flags'. Advances s.  Returns
9626      * TRUE if the input should be treated as a valid flag, so the next char
9627      * may be as well; otherwise FALSE. 'charset' should point to a NUL upon
9628      * first call on the current regex.  This routine will set it to any
9629      * charset modifier found.  The caller shouldn't change it.  This way,
9630      * another charset modifier encountered in the parse can be detected as an
9631      * error, as we have decided to allow only one */
9632
9633     const char c = **s;
9634     STRLEN charlen = UTF ? UTF8SKIP(*s) : 1;
9635
9636     if ( charlen != 1 || ! strchr(valid_flags, c) ) {
9637         if (isWORDCHAR_lazy_if(*s, UTF)) {
9638             yyerror_pv(Perl_form(aTHX_ "Unknown regexp modifier \"/%.*s\"", (int)charlen, *s),
9639                        UTF ? SVf_UTF8 : 0);
9640             (*s) += charlen;
9641             /* Pretend that it worked, so will continue processing before
9642              * dieing */
9643             return TRUE;
9644         }
9645         return FALSE;
9646     }
9647
9648     switch (c) {
9649
9650         CASE_STD_PMMOD_FLAGS_PARSE_SET(pmfl);
9651         case GLOBAL_PAT_MOD:      *pmfl |= PMf_GLOBAL; break;
9652         case CONTINUE_PAT_MOD:    *pmfl |= PMf_CONTINUE; break;
9653         case ONCE_PAT_MOD:        *pmfl |= PMf_KEEP; break;
9654         case KEEPCOPY_PAT_MOD:    *pmfl |= RXf_PMf_KEEPCOPY; break;
9655         case NONDESTRUCT_PAT_MOD: *pmfl |= PMf_NONDESTRUCT; break;
9656         case LOCALE_PAT_MOD:
9657             if (*charset) {
9658                 goto multiple_charsets;
9659             }
9660             set_regex_charset(pmfl, REGEX_LOCALE_CHARSET);
9661             *charset = c;
9662             break;
9663         case UNICODE_PAT_MOD:
9664             if (*charset) {
9665                 goto multiple_charsets;
9666             }
9667             set_regex_charset(pmfl, REGEX_UNICODE_CHARSET);
9668             *charset = c;
9669             break;
9670         case ASCII_RESTRICT_PAT_MOD:
9671             if (! *charset) {
9672                 set_regex_charset(pmfl, REGEX_ASCII_RESTRICTED_CHARSET);
9673             }
9674             else {
9675
9676                 /* Error if previous modifier wasn't an 'a', but if it was, see
9677                  * if, and accept, a second occurrence (only) */
9678                 if (*charset != 'a'
9679                     || get_regex_charset(*pmfl)
9680                         != REGEX_ASCII_RESTRICTED_CHARSET)
9681                 {
9682                         goto multiple_charsets;
9683                 }
9684                 set_regex_charset(pmfl, REGEX_ASCII_MORE_RESTRICTED_CHARSET);
9685             }
9686             *charset = c;
9687             break;
9688         case DEPENDS_PAT_MOD:
9689             if (*charset) {
9690                 goto multiple_charsets;
9691             }
9692             set_regex_charset(pmfl, REGEX_DEPENDS_CHARSET);
9693             *charset = c;
9694             break;
9695     }
9696
9697     (*s)++;
9698     return TRUE;
9699
9700     multiple_charsets:
9701         if (*charset != c) {
9702             yyerror(Perl_form(aTHX_ "Regexp modifiers \"/%c\" and \"/%c\" are mutually exclusive", *charset, c));
9703         }
9704         else if (c == 'a') {
9705             yyerror("Regexp modifier \"/a\" may appear a maximum of twice");
9706         }
9707         else {
9708             yyerror(Perl_form(aTHX_ "Regexp modifier \"/%c\" may not appear twice", c));
9709         }
9710
9711         /* Pretend that it worked, so will continue processing before dieing */
9712         (*s)++;
9713         return TRUE;
9714 }
9715
9716 STATIC char *
9717 S_scan_pat(pTHX_ char *start, I32 type)
9718 {
9719     dVAR;
9720     PMOP *pm;
9721     char *s;
9722     const char * const valid_flags =
9723         (const char *)((type == OP_QR) ? QR_PAT_MODS : M_PAT_MODS);
9724     char charset = '\0';    /* character set modifier */
9725 #ifdef PERL_MAD
9726     char *modstart;
9727 #endif
9728
9729     PERL_ARGS_ASSERT_SCAN_PAT;
9730
9731     s = scan_str(start,!!PL_madskills,FALSE, (PL_in_eval & EVAL_RE_REPARSING),
9732                        TRUE /* look for escaped bracketed metas */ );
9733
9734     if (!s) {
9735         const char * const delimiter = skipspace(start);
9736         Perl_croak(aTHX_
9737                    (const char *)
9738                    (*delimiter == '?'
9739                     ? "Search pattern not terminated or ternary operator parsed as search pattern"
9740                     : "Search pattern not terminated" ));
9741     }
9742
9743     pm = (PMOP*)newPMOP(type, 0);
9744     if (PL_multi_open == '?') {
9745         /* This is the only point in the code that sets PMf_ONCE:  */
9746         pm->op_pmflags |= PMf_ONCE;
9747
9748         /* Hence it's safe to do this bit of PMOP book-keeping here, which
9749            allows us to restrict the list needed by reset to just the ??
9750            matches.  */
9751         assert(type != OP_TRANS);
9752         if (PL_curstash) {
9753             MAGIC *mg = mg_find((const SV *)PL_curstash, PERL_MAGIC_symtab);
9754             U32 elements;
9755             if (!mg) {
9756                 mg = sv_magicext(MUTABLE_SV(PL_curstash), 0, PERL_MAGIC_symtab, 0, 0,
9757                                  0);
9758             }
9759             elements = mg->mg_len / sizeof(PMOP**);
9760             Renewc(mg->mg_ptr, elements + 1, PMOP*, char);
9761             ((PMOP**)mg->mg_ptr) [elements++] = pm;
9762             mg->mg_len = elements * sizeof(PMOP**);
9763             PmopSTASH_set(pm,PL_curstash);
9764         }
9765     }
9766 #ifdef PERL_MAD
9767     modstart = s;
9768 #endif
9769
9770     /* if qr/...(?{..}).../, then need to parse the pattern within a new
9771      * anon CV. False positives like qr/[(?{]/ are harmless */
9772
9773     if (type == OP_QR) {
9774         STRLEN len;
9775         char *e, *p = SvPV(PL_lex_stuff, len);
9776         e = p + len;
9777         for (; p < e; p++) {
9778             if (p[0] == '(' && p[1] == '?'
9779                 && (p[2] == '{' || (p[2] == '?' && p[3] == '{')))
9780             {
9781                 pm->op_pmflags |= PMf_HAS_CV;
9782                 break;
9783             }
9784         }
9785         pm->op_pmflags |= PMf_IS_QR;
9786     }
9787
9788     while (*s && S_pmflag(aTHX_ valid_flags, &(pm->op_pmflags), &s, &charset)) {};
9789 #ifdef PERL_MAD
9790     if (PL_madskills && modstart != s) {
9791         SV* tmptoken = newSVpvn(modstart, s - modstart);
9792         append_madprops(newMADPROP('m', MAD_SV, tmptoken, 0), (OP*)pm, 0);
9793     }
9794 #endif
9795     /* issue a warning if /c is specified,but /g is not */
9796     if ((pm->op_pmflags & PMf_CONTINUE) && !(pm->op_pmflags & PMf_GLOBAL))
9797     {
9798         Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), 
9799                        "Use of /c modifier is meaningless without /g" );
9800     }
9801
9802     PL_lex_op = (OP*)pm;
9803     pl_yylval.ival = OP_MATCH;
9804     return s;
9805 }
9806
9807 STATIC char *
9808 S_scan_subst(pTHX_ char *start)
9809 {
9810     dVAR;
9811     char *s;
9812     PMOP *pm;
9813     I32 first_start;
9814     line_t first_line;
9815     I32 es = 0;
9816     char charset = '\0';    /* character set modifier */
9817 #ifdef PERL_MAD
9818     char *modstart;
9819 #endif
9820
9821     PERL_ARGS_ASSERT_SCAN_SUBST;
9822
9823     pl_yylval.ival = OP_NULL;
9824
9825     s = scan_str(start,!!PL_madskills,FALSE,FALSE,
9826                  TRUE /* look for escaped bracketed metas */ );
9827
9828     if (!s)
9829         Perl_croak(aTHX_ "Substitution pattern not terminated");
9830
9831     if (s[-1] == PL_multi_open)
9832         s--;
9833 #ifdef PERL_MAD
9834     if (PL_madskills) {
9835         CURMAD('q', PL_thisopen);
9836         CURMAD('_', PL_thiswhite);
9837         CURMAD('E', PL_thisstuff);
9838         CURMAD('Q', PL_thisclose);
9839         PL_realtokenstart = s - SvPVX(PL_linestr);
9840     }
9841 #endif
9842
9843     first_start = PL_multi_start;
9844     first_line = CopLINE(PL_curcop);
9845     s = scan_str(s,!!PL_madskills,FALSE,FALSE, FALSE);
9846     if (!s) {
9847         if (PL_lex_stuff) {
9848             SvREFCNT_dec(PL_lex_stuff);
9849             PL_lex_stuff = NULL;
9850         }
9851         Perl_croak(aTHX_ "Substitution replacement not terminated");
9852     }
9853     PL_multi_start = first_start;       /* so whole substitution is taken together */
9854
9855     pm = (PMOP*)newPMOP(OP_SUBST, 0);
9856
9857 #ifdef PERL_MAD
9858     if (PL_madskills) {
9859         CURMAD('z', PL_thisopen);
9860         CURMAD('R', PL_thisstuff);
9861         CURMAD('Z', PL_thisclose);
9862     }
9863     modstart = s;
9864 #endif
9865
9866     while (*s) {
9867         if (*s == EXEC_PAT_MOD) {
9868             s++;
9869             es++;
9870         }
9871         else if (! S_pmflag(aTHX_ S_PAT_MODS, &(pm->op_pmflags), &s, &charset))
9872         {
9873             break;
9874         }
9875     }
9876
9877 #ifdef PERL_MAD
9878     if (PL_madskills) {
9879         if (modstart != s)
9880             curmad('m', newSVpvn(modstart, s - modstart));
9881         append_madprops(PL_thismad, (OP*)pm, 0);
9882         PL_thismad = 0;
9883     }
9884 #endif
9885     if ((pm->op_pmflags & PMf_CONTINUE)) {
9886         Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), "Use of /c modifier is meaningless in s///" );
9887     }
9888
9889     if (es) {
9890         SV * const repl = newSVpvs("");
9891
9892         PL_multi_end = 0;
9893         pm->op_pmflags |= PMf_EVAL;
9894         while (es-- > 0) {
9895             if (es)
9896                 sv_catpvs(repl, "eval ");
9897             else
9898                 sv_catpvs(repl, "do ");
9899         }
9900         sv_catpvs(repl, "{");
9901         sv_catsv(repl, PL_sublex_info.repl);
9902         sv_catpvs(repl, "}");
9903         SvEVALED_on(repl);
9904         SvREFCNT_dec(PL_sublex_info.repl);
9905         PL_sublex_info.repl = repl;
9906     }
9907     if (CopLINE(PL_curcop) != first_line) {
9908         sv_upgrade(PL_sublex_info.repl, SVt_PVNV);
9909         ((XPVNV*)SvANY(PL_sublex_info.repl))->xnv_u.xpad_cop_seq.xlow =
9910             CopLINE(PL_curcop) - first_line;
9911         CopLINE_set(PL_curcop, first_line);
9912     }
9913
9914     PL_lex_op = (OP*)pm;
9915     pl_yylval.ival = OP_SUBST;
9916     return s;
9917 }
9918
9919 STATIC char *
9920 S_scan_trans(pTHX_ char *start)
9921 {
9922     dVAR;
9923     char* s;
9924     OP *o;
9925     U8 squash;
9926     U8 del;
9927     U8 complement;
9928     bool nondestruct = 0;
9929 #ifdef PERL_MAD
9930     char *modstart;
9931 #endif
9932
9933     PERL_ARGS_ASSERT_SCAN_TRANS;
9934
9935     pl_yylval.ival = OP_NULL;
9936
9937     s = scan_str(start,!!PL_madskills,FALSE,FALSE, FALSE);
9938     if (!s)
9939         Perl_croak(aTHX_ "Transliteration pattern not terminated");
9940
9941     if (s[-1] == PL_multi_open)
9942         s--;
9943 #ifdef PERL_MAD
9944     if (PL_madskills) {
9945         CURMAD('q', PL_thisopen);
9946         CURMAD('_', PL_thiswhite);
9947         CURMAD('E', PL_thisstuff);
9948         CURMAD('Q', PL_thisclose);
9949         PL_realtokenstart = s - SvPVX(PL_linestr);
9950     }
9951 #endif
9952
9953     s = scan_str(s,!!PL_madskills,FALSE,FALSE, FALSE);
9954     if (!s) {
9955         if (PL_lex_stuff) {
9956             SvREFCNT_dec(PL_lex_stuff);
9957             PL_lex_stuff = NULL;
9958         }
9959         Perl_croak(aTHX_ "Transliteration replacement not terminated");
9960     }
9961     if (PL_madskills) {
9962         CURMAD('z', PL_thisopen);
9963         CURMAD('R', PL_thisstuff);
9964         CURMAD('Z', PL_thisclose);
9965     }
9966
9967     complement = del = squash = 0;
9968 #ifdef PERL_MAD
9969     modstart = s;
9970 #endif
9971     while (1) {
9972         switch (*s) {
9973         case 'c':
9974             complement = OPpTRANS_COMPLEMENT;
9975             break;
9976         case 'd':
9977             del = OPpTRANS_DELETE;
9978             break;
9979         case 's':
9980             squash = OPpTRANS_SQUASH;
9981             break;
9982         case 'r':
9983             nondestruct = 1;
9984             break;
9985         default:
9986             goto no_more;
9987         }
9988         s++;
9989     }
9990   no_more:
9991
9992     o = newPVOP(nondestruct ? OP_TRANSR : OP_TRANS, 0, (char*)NULL);
9993     o->op_private &= ~OPpTRANS_ALL;
9994     o->op_private |= del|squash|complement|
9995       (DO_UTF8(PL_lex_stuff)? OPpTRANS_FROM_UTF : 0)|
9996       (DO_UTF8(PL_sublex_info.repl) ? OPpTRANS_TO_UTF   : 0);
9997
9998     PL_lex_op = o;
9999     pl_yylval.ival = nondestruct ? OP_TRANSR : OP_TRANS;
10000
10001 #ifdef PERL_MAD
10002     if (PL_madskills) {
10003         if (modstart != s)
10004             curmad('m', newSVpvn(modstart, s - modstart));
10005         append_madprops(PL_thismad, o, 0);
10006         PL_thismad = 0;
10007     }
10008 #endif
10009
10010     return s;
10011 }
10012
10013 /* scan_heredoc
10014    Takes a pointer to the first < in <<FOO.
10015    Returns a pointer to the byte following <<FOO.
10016
10017    This function scans a heredoc, which involves different methods
10018    depending on whether we are in a string eval, quoted construct, etc.
10019    This is because PL_linestr could containing a single line of input, or
10020    a whole string being evalled, or the contents of the current quote-
10021    like operator.
10022
10023    The two basic methods are:
10024     - Steal lines from the input stream
10025     - Scan the heredoc in PL_linestr and remove it therefrom
10026
10027    In a file scope or filtered eval, the first method is used; in a
10028    string eval, the second.
10029
10030    In a quote-like operator, we have to choose between the two,
10031    depending on where we can find a newline.  We peek into outer lex-
10032    ing scopes until we find one with a newline in it.  If we reach the
10033    outermost lexing scope and it is a file, we use the stream method.
10034    Otherwise it is treated as an eval.
10035 */
10036
10037 STATIC char *
10038 S_scan_heredoc(pTHX_ char *s)
10039 {
10040     dVAR;
10041     I32 op_type = OP_SCALAR;
10042     I32 len;
10043     SV *tmpstr;
10044     char term;
10045     char *d;
10046     char *e;
10047     char *peek;
10048     const bool infile = PL_rsfp || PL_parser->filtered;
10049     const line_t origline = CopLINE(PL_curcop);
10050     LEXSHARED *shared = PL_parser->lex_shared;
10051 #ifdef PERL_MAD
10052     I32 stuffstart = s - SvPVX(PL_linestr);
10053     char *tstart;
10054  
10055     PL_realtokenstart = -1;
10056 #endif
10057
10058     PERL_ARGS_ASSERT_SCAN_HEREDOC;
10059
10060     s += 2;
10061     d = PL_tokenbuf + 1;
10062     e = PL_tokenbuf + sizeof PL_tokenbuf - 1;
10063     *PL_tokenbuf = '\n';
10064     peek = s;
10065     while (SPACE_OR_TAB(*peek))
10066         peek++;
10067     if (*peek == '`' || *peek == '\'' || *peek =='"') {
10068         s = peek;
10069         term = *s++;
10070         s = delimcpy(d, e, s, PL_bufend, term, &len);
10071         if (s == PL_bufend)
10072             Perl_croak(aTHX_ "Unterminated delimiter for here document");
10073         d += len;
10074         s++;
10075     }
10076     else {
10077         if (*s == '\\')
10078             /* <<\FOO is equivalent to <<'FOO' */
10079             s++, term = '\'';
10080         else
10081             term = '"';
10082         if (!isWORDCHAR_lazy_if(s,UTF))
10083             deprecate("bare << to mean <<\"\"");
10084         for (; isWORDCHAR_lazy_if(s,UTF); s++) {
10085             if (d < e)
10086                 *d++ = *s;
10087         }
10088     }
10089     if (d >= PL_tokenbuf + sizeof PL_tokenbuf - 1)
10090         Perl_croak(aTHX_ "Delimiter for here document is too long");
10091     *d++ = '\n';
10092     *d = '\0';
10093     len = d - PL_tokenbuf;
10094
10095 #ifdef PERL_MAD
10096     if (PL_madskills) {
10097         tstart = PL_tokenbuf + 1;
10098         PL_thisclose = newSVpvn(tstart, len - 1);
10099         tstart = SvPVX(PL_linestr) + stuffstart;
10100         PL_thisopen = newSVpvn(tstart, s - tstart);
10101         stuffstart = s - SvPVX(PL_linestr);
10102     }
10103 #endif
10104 #ifndef PERL_STRICT_CR
10105     d = strchr(s, '\r');
10106     if (d) {
10107         char * const olds = s;
10108         s = d;
10109         while (s < PL_bufend) {
10110             if (*s == '\r') {
10111                 *d++ = '\n';
10112                 if (*++s == '\n')
10113                     s++;
10114             }
10115             else if (*s == '\n' && s[1] == '\r') {      /* \015\013 on a mac? */
10116                 *d++ = *s++;
10117                 s++;
10118             }
10119             else
10120                 *d++ = *s++;
10121         }
10122         *d = '\0';
10123         PL_bufend = d;
10124         SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
10125         s = olds;
10126     }
10127 #endif
10128 #ifdef PERL_MAD
10129     if (PL_madskills) {
10130         tstart = SvPVX(PL_linestr) + stuffstart;
10131         if (PL_thisstuff)
10132             sv_catpvn(PL_thisstuff, tstart, s - tstart);
10133         else
10134             PL_thisstuff = newSVpvn(tstart, s - tstart);
10135     }
10136
10137     stuffstart = s - SvPVX(PL_linestr);
10138 #endif
10139
10140     tmpstr = newSV_type(SVt_PVIV);
10141     SvGROW(tmpstr, 80);
10142     if (term == '\'') {
10143         op_type = OP_CONST;
10144         SvIV_set(tmpstr, -1);
10145     }
10146     else if (term == '`') {
10147         op_type = OP_BACKTICK;
10148         SvIV_set(tmpstr, '\\');
10149     }
10150
10151     PL_multi_start = origline + 1 + PL_parser->herelines;
10152     PL_multi_open = PL_multi_close = '<';
10153     /* inside a string eval or quote-like operator */
10154     if (!infile || PL_lex_inwhat) {
10155         SV *linestr;
10156         char *bufend;
10157         char * const olds = s;
10158         PERL_CONTEXT * const cx = &cxstack[cxstack_ix];
10159         /* These two fields are not set until an inner lexing scope is
10160            entered.  But we need them set here. */
10161         shared->ls_bufptr  = s;
10162         shared->ls_linestr = PL_linestr;
10163         if (PL_lex_inwhat)
10164           /* Look for a newline.  If the current buffer does not have one,
10165              peek into the line buffer of the parent lexing scope, going
10166              up as many levels as necessary to find one with a newline
10167              after bufptr.
10168            */
10169           while (!(s = (char *)memchr(
10170                     (void *)shared->ls_bufptr, '\n',
10171                     SvEND(shared->ls_linestr)-shared->ls_bufptr
10172                 ))) {
10173             shared = shared->ls_prev;
10174             /* shared is only null if we have gone beyond the outermost
10175                lexing scope.  In a file, we will have broken out of the
10176                loop in the previous iteration.  In an eval, the string buf-
10177                fer ends with "\n;", so the while condition above will have
10178                evaluated to false.  So shared can never be null. */
10179             assert(shared);
10180             /* A LEXSHARED struct with a null ls_prev pointer is the outer-
10181                most lexing scope.  In a file, shared->ls_linestr at that
10182                level is just one line, so there is no body to steal. */
10183             if (infile && !shared->ls_prev) {
10184                 s = olds;
10185                 goto streaming;
10186             }
10187           }
10188         else {  /* eval */
10189             s = (char*)memchr((void*)s, '\n', PL_bufend - s);
10190             assert(s);
10191         }
10192         linestr = shared->ls_linestr;
10193         bufend = SvEND(linestr);
10194         d = s;
10195         while (s < bufend - len + 1 &&
10196           memNE(s,PL_tokenbuf,len) ) {
10197             if (*s++ == '\n')
10198                 ++PL_parser->herelines;
10199         }
10200         if (s >= bufend - len + 1) {
10201             goto interminable;
10202         }
10203         sv_setpvn(tmpstr,d+1,s-d);
10204 #ifdef PERL_MAD
10205         if (PL_madskills) {
10206             if (PL_thisstuff)
10207                 sv_catpvn(PL_thisstuff, d + 1, s - d);
10208             else
10209                 PL_thisstuff = newSVpvn(d + 1, s - d);
10210             stuffstart = s - SvPVX(PL_linestr);
10211         }
10212 #endif
10213         s += len - 1;
10214         /* the preceding stmt passes a newline */
10215         PL_parser->herelines++;
10216
10217         /* s now points to the newline after the heredoc terminator.
10218            d points to the newline before the body of the heredoc.
10219          */
10220
10221         /* We are going to modify linestr in place here, so set
10222            aside copies of the string if necessary for re-evals or
10223            (caller $n)[6]. */
10224         /* See the Paranoia note in case LEX_INTERPEND in yylex, for why we
10225            check shared->re_eval_str. */
10226         if (shared->re_eval_start || shared->re_eval_str) {
10227             /* Set aside the rest of the regexp */
10228             if (!shared->re_eval_str)
10229                 shared->re_eval_str =
10230                        newSVpvn(shared->re_eval_start,
10231                                 bufend - shared->re_eval_start);
10232             shared->re_eval_start -= s-d;
10233         }
10234         if (cxstack_ix >= 0 && CxTYPE(cx) == CXt_EVAL &&
10235             CxOLD_OP_TYPE(cx) == OP_ENTEREVAL &&
10236             cx->blk_eval.cur_text == linestr)
10237         {
10238             cx->blk_eval.cur_text = newSVsv(linestr);
10239             SvSCREAM_on(cx->blk_eval.cur_text);
10240         }
10241         /* Copy everything from s onwards back to d. */
10242         Move(s,d,bufend-s + 1,char);
10243         SvCUR_set(linestr, SvCUR(linestr) - (s-d));
10244         /* Setting PL_bufend only applies when we have not dug deeper
10245            into other scopes, because sublex_done sets PL_bufend to
10246            SvEND(PL_linestr). */
10247         if (shared == PL_parser->lex_shared) PL_bufend = SvEND(linestr);
10248         s = olds;
10249     }
10250     else
10251     {
10252       SV *linestr_save;
10253      streaming:
10254       sv_setpvs(tmpstr,"");   /* avoid "uninitialized" warning */
10255       term = PL_tokenbuf[1];
10256       len--;
10257       linestr_save = PL_linestr; /* must restore this afterwards */
10258       d = s;                     /* and this */
10259       PL_linestr = newSVpvs("");
10260       PL_bufend = SvPVX(PL_linestr);
10261       while (1) {
10262 #ifdef PERL_MAD
10263         if (PL_madskills) {
10264             tstart = SvPVX(PL_linestr) + stuffstart;
10265             if (PL_thisstuff)
10266                 sv_catpvn(PL_thisstuff, tstart, PL_bufend - tstart);
10267             else
10268                 PL_thisstuff = newSVpvn(tstart, PL_bufend - tstart);
10269         }
10270 #endif
10271         PL_bufptr = PL_bufend;
10272         CopLINE_set(PL_curcop,
10273                     origline + 1 + PL_parser->herelines);
10274         if (!lex_next_chunk(LEX_NO_TERM)
10275          && (!SvCUR(tmpstr) || SvEND(tmpstr)[-1] != '\n')) {
10276             SvREFCNT_dec(linestr_save);
10277             goto interminable;
10278         }
10279         CopLINE_set(PL_curcop, origline);
10280         if (!SvCUR(PL_linestr) || PL_bufend[-1] != '\n') {
10281             s = lex_grow_linestr(SvLEN(PL_linestr) + 3);
10282             /* ^That should be enough to avoid this needing to grow:  */
10283             sv_catpvs(PL_linestr, "\n\0");
10284             assert(s == SvPVX(PL_linestr));
10285             PL_bufend = SvEND(PL_linestr);
10286         }
10287         s = PL_bufptr;
10288 #ifdef PERL_MAD
10289         stuffstart = s - SvPVX(PL_linestr);
10290 #endif
10291         PL_parser->herelines++;
10292         PL_last_lop = PL_last_uni = NULL;
10293 #ifndef PERL_STRICT_CR
10294         if (PL_bufend - PL_linestart >= 2) {
10295             if ((PL_bufend[-2] == '\r' && PL_bufend[-1] == '\n') ||
10296                 (PL_bufend[-2] == '\n' && PL_bufend[-1] == '\r'))
10297             {
10298                 PL_bufend[-2] = '\n';
10299                 PL_bufend--;
10300                 SvCUR_set(PL_linestr, PL_bufend - SvPVX_const(PL_linestr));
10301             }
10302             else if (PL_bufend[-1] == '\r')
10303                 PL_bufend[-1] = '\n';
10304         }
10305         else if (PL_bufend - PL_linestart == 1 && PL_bufend[-1] == '\r')
10306             PL_bufend[-1] = '\n';
10307 #endif
10308         if (*s == term && memEQ(s,PL_tokenbuf + 1,len)) {
10309             SvREFCNT_dec(PL_linestr);
10310             PL_linestr = linestr_save;
10311             PL_linestart = SvPVX(linestr_save);
10312             PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
10313             s = d;
10314             break;
10315         }
10316         else {
10317             sv_catsv(tmpstr,PL_linestr);
10318         }
10319       }
10320     }
10321     PL_multi_end = origline + PL_parser->herelines;
10322     if (SvCUR(tmpstr) + 5 < SvLEN(tmpstr)) {
10323         SvPV_shrink_to_cur(tmpstr);
10324     }
10325     if (!IN_BYTES) {
10326         if (UTF && is_utf8_string((U8*)SvPVX_const(tmpstr), SvCUR(tmpstr)))
10327             SvUTF8_on(tmpstr);
10328         else if (PL_encoding)
10329             sv_recode_to_utf8(tmpstr, PL_encoding);
10330     }
10331     PL_lex_stuff = tmpstr;
10332     pl_yylval.ival = op_type;
10333     return s;
10334
10335   interminable:
10336     SvREFCNT_dec(tmpstr);
10337     CopLINE_set(PL_curcop, origline);
10338     missingterm(PL_tokenbuf + 1);
10339 }
10340
10341 /* scan_inputsymbol
10342    takes: current position in input buffer
10343    returns: new position in input buffer
10344    side-effects: pl_yylval and lex_op are set.
10345
10346    This code handles:
10347
10348    <>           read from ARGV
10349    <FH>         read from filehandle
10350    <pkg::FH>    read from package qualified filehandle
10351    <pkg'FH>     read from package qualified filehandle
10352    <$fh>        read from filehandle in $fh
10353    <*.h>        filename glob
10354
10355 */
10356
10357 STATIC char *
10358 S_scan_inputsymbol(pTHX_ char *start)
10359 {
10360     dVAR;
10361     char *s = start;            /* current position in buffer */
10362     char *end;
10363     I32 len;
10364     char *d = PL_tokenbuf;                                      /* start of temp holding space */
10365     const char * const e = PL_tokenbuf + sizeof PL_tokenbuf;    /* end of temp holding space */
10366
10367     PERL_ARGS_ASSERT_SCAN_INPUTSYMBOL;
10368
10369     end = strchr(s, '\n');
10370     if (!end)
10371         end = PL_bufend;
10372     s = delimcpy(d, e, s + 1, end, '>', &len);  /* extract until > */
10373
10374     /* die if we didn't have space for the contents of the <>,
10375        or if it didn't end, or if we see a newline
10376     */
10377
10378     if (len >= (I32)sizeof PL_tokenbuf)
10379         Perl_croak(aTHX_ "Excessively long <> operator");
10380     if (s >= end)
10381         Perl_croak(aTHX_ "Unterminated <> operator");
10382
10383     s++;
10384
10385     /* check for <$fh>
10386        Remember, only scalar variables are interpreted as filehandles by
10387        this code.  Anything more complex (e.g., <$fh{$num}>) will be
10388        treated as a glob() call.
10389        This code makes use of the fact that except for the $ at the front,
10390        a scalar variable and a filehandle look the same.
10391     */
10392     if (*d == '$' && d[1]) d++;
10393
10394     /* allow <Pkg'VALUE> or <Pkg::VALUE> */
10395     while (*d && (isWORDCHAR_lazy_if(d,UTF) || *d == '\'' || *d == ':'))
10396         d += UTF ? UTF8SKIP(d) : 1;
10397
10398     /* If we've tried to read what we allow filehandles to look like, and
10399        there's still text left, then it must be a glob() and not a getline.
10400        Use scan_str to pull out the stuff between the <> and treat it
10401        as nothing more than a string.
10402     */
10403
10404     if (d - PL_tokenbuf != len) {
10405         pl_yylval.ival = OP_GLOB;
10406         s = scan_str(start,!!PL_madskills,FALSE,FALSE, FALSE);
10407         if (!s)
10408            Perl_croak(aTHX_ "Glob not terminated");
10409         return s;
10410     }
10411     else {
10412         bool readline_overriden = FALSE;
10413         GV *gv_readline;
10414         GV **gvp;
10415         /* we're in a filehandle read situation */
10416         d = PL_tokenbuf;
10417
10418         /* turn <> into <ARGV> */
10419         if (!len)
10420             Copy("ARGV",d,5,char);
10421
10422         /* Check whether readline() is overriden */
10423         gv_readline = gv_fetchpvs("readline", GV_NOTQUAL, SVt_PVCV);
10424         if ((gv_readline
10425                 && GvCVu(gv_readline) && GvIMPORTED_CV(gv_readline))
10426                 ||
10427                 ((gvp = (GV**)hv_fetchs(PL_globalstash, "readline", FALSE))
10428                  && (gv_readline = *gvp) && isGV_with_GP(gv_readline)
10429                 && GvCVu(gv_readline) && GvIMPORTED_CV(gv_readline)))
10430             readline_overriden = TRUE;
10431
10432         /* if <$fh>, create the ops to turn the variable into a
10433            filehandle
10434         */
10435         if (*d == '$') {
10436             /* try to find it in the pad for this block, otherwise find
10437                add symbol table ops
10438             */
10439             const PADOFFSET tmp = pad_findmy_pvn(d, len, UTF ? SVf_UTF8 : 0);
10440             if (tmp != NOT_IN_PAD) {
10441                 if (PAD_COMPNAME_FLAGS_isOUR(tmp)) {
10442                     HV * const stash = PAD_COMPNAME_OURSTASH(tmp);
10443                     HEK * const stashname = HvNAME_HEK(stash);
10444                     SV * const sym = sv_2mortal(newSVhek(stashname));
10445                     sv_catpvs(sym, "::");
10446                     sv_catpv(sym, d+1);
10447                     d = SvPVX(sym);
10448                     goto intro_sym;
10449                 }
10450                 else {
10451                     OP * const o = newOP(OP_PADSV, 0);
10452                     o->op_targ = tmp;
10453                     PL_lex_op = readline_overriden
10454                         ? (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
10455                                 op_append_elem(OP_LIST, o,
10456                                     newCVREF(0, newGVOP(OP_GV,0,gv_readline))))
10457                         : (OP*)newUNOP(OP_READLINE, 0, o);
10458                 }
10459             }
10460             else {
10461                 GV *gv;
10462                 ++d;
10463 intro_sym:
10464                 gv = gv_fetchpv(d,
10465                                 (PL_in_eval
10466                                  ? (GV_ADDMULTI | GV_ADDINEVAL)
10467                                  : GV_ADDMULTI) | ( UTF ? SVf_UTF8 : 0 ),
10468                                 SVt_PV);
10469                 PL_lex_op = readline_overriden
10470                     ? (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
10471                             op_append_elem(OP_LIST,
10472                                 newUNOP(OP_RV2SV, 0, newGVOP(OP_GV, 0, gv)),
10473                                 newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
10474                     : (OP*)newUNOP(OP_READLINE, 0,
10475                             newUNOP(OP_RV2SV, 0,
10476                                 newGVOP(OP_GV, 0, gv)));
10477             }
10478             if (!readline_overriden)
10479                 PL_lex_op->op_flags |= OPf_SPECIAL;
10480             /* we created the ops in PL_lex_op, so make pl_yylval.ival a null op */
10481             pl_yylval.ival = OP_NULL;
10482         }
10483
10484         /* If it's none of the above, it must be a literal filehandle
10485            (<Foo::BAR> or <FOO>) so build a simple readline OP */
10486         else {
10487             GV * const gv = gv_fetchpv(d, GV_ADD | ( UTF ? SVf_UTF8 : 0 ), SVt_PVIO);
10488             PL_lex_op = readline_overriden
10489                 ? (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
10490                         op_append_elem(OP_LIST,
10491                             newGVOP(OP_GV, 0, gv),
10492                             newCVREF(0, newGVOP(OP_GV, 0, gv_readline))))
10493                 : (OP*)newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, gv));
10494             pl_yylval.ival = OP_NULL;
10495         }
10496     }
10497
10498     return s;
10499 }
10500
10501
10502 /* scan_str
10503    takes:
10504         start                   position in buffer
10505         keep_quoted             preserve \ on the embedded delimiter(s)
10506         keep_delims             preserve the delimiters around the string
10507         re_reparse              compiling a run-time /(?{})/:
10508                                    collapse // to /,  and skip encoding src
10509         deprecate_escaped_meta  issue a deprecation warning for cer-
10510                                 tain paired metacharacters that appear
10511                                 escaped within it
10512    returns: position to continue reading from buffer
10513    side-effects: multi_start, multi_close, lex_repl or lex_stuff, and
10514         updates the read buffer.
10515
10516    This subroutine pulls a string out of the input.  It is called for:
10517         q               single quotes           q(literal text)
10518         '               single quotes           'literal text'
10519         qq              double quotes           qq(interpolate $here please)
10520         "               double quotes           "interpolate $here please"
10521         qx              backticks               qx(/bin/ls -l)
10522         `               backticks               `/bin/ls -l`
10523         qw              quote words             @EXPORT_OK = qw( func() $spam )
10524         m//             regexp match            m/this/
10525         s///            regexp substitute       s/this/that/
10526         tr///           string transliterate    tr/this/that/
10527         y///            string transliterate    y/this/that/
10528         ($*@)           sub prototypes          sub foo ($)
10529         (stuff)         sub attr parameters     sub foo : attr(stuff)
10530         <>              readline or globs       <FOO>, <>, <$fh>, or <*.c>
10531         
10532    In most of these cases (all but <>, patterns and transliterate)
10533    yylex() calls scan_str().  m// makes yylex() call scan_pat() which
10534    calls scan_str().  s/// makes yylex() call scan_subst() which calls
10535    scan_str().  tr/// and y/// make yylex() call scan_trans() which
10536    calls scan_str().
10537
10538    It skips whitespace before the string starts, and treats the first
10539    character as the delimiter.  If the delimiter is one of ([{< then
10540    the corresponding "close" character )]}> is used as the closing
10541    delimiter.  It allows quoting of delimiters, and if the string has
10542    balanced delimiters ([{<>}]) it allows nesting.
10543
10544    On success, the SV with the resulting string is put into lex_stuff or,
10545    if that is already non-NULL, into lex_repl. The second case occurs only
10546    when parsing the RHS of the special constructs s/// and tr/// (y///).
10547    For convenience, the terminating delimiter character is stuffed into
10548    SvIVX of the SV.
10549 */
10550
10551 STATIC char *
10552 S_scan_str(pTHX_ char *start, int keep_quoted, int keep_delims, int re_reparse,
10553                  bool deprecate_escaped_meta
10554     )
10555 {
10556     dVAR;
10557     SV *sv;                     /* scalar value: string */
10558     const char *tmps;           /* temp string, used for delimiter matching */
10559     char *s = start;            /* current position in the buffer */
10560     char term;                  /* terminating character */
10561     char *to;                   /* current position in the sv's data */
10562     I32 brackets = 1;           /* bracket nesting level */
10563     bool has_utf8 = FALSE;      /* is there any utf8 content? */
10564     I32 termcode;               /* terminating char. code */
10565     U8 termstr[UTF8_MAXBYTES];  /* terminating string */
10566     STRLEN termlen;             /* length of terminating string */
10567     int last_off = 0;           /* last position for nesting bracket */
10568     char *escaped_open = NULL;
10569     line_t herelines;
10570 #ifdef PERL_MAD
10571     int stuffstart;
10572     char *tstart;
10573 #endif
10574
10575     PERL_ARGS_ASSERT_SCAN_STR;
10576
10577     /* skip space before the delimiter */
10578     if (isSPACE(*s)) {
10579         s = PEEKSPACE(s);
10580     }
10581
10582 #ifdef PERL_MAD
10583     if (PL_realtokenstart >= 0) {
10584         stuffstart = PL_realtokenstart;
10585         PL_realtokenstart = -1;
10586     }
10587     else
10588         stuffstart = start - SvPVX(PL_linestr);
10589 #endif
10590     /* mark where we are, in case we need to report errors */
10591     CLINE;
10592
10593     /* after skipping whitespace, the next character is the terminator */
10594     term = *s;
10595     if (!UTF) {
10596         termcode = termstr[0] = term;
10597         termlen = 1;
10598     }
10599     else {
10600         termcode = utf8_to_uvchr_buf((U8*)s, (U8*)PL_bufend, &termlen);
10601         Copy(s, termstr, termlen, U8);
10602         if (!UTF8_IS_INVARIANT(term))
10603             has_utf8 = TRUE;
10604     }
10605
10606     /* mark where we are */
10607     PL_multi_start = CopLINE(PL_curcop);
10608     PL_multi_open = term;
10609     herelines = PL_parser->herelines;
10610
10611     /* find corresponding closing delimiter */
10612     if (term && (tmps = strchr("([{< )]}> )]}>",term)))
10613         termcode = termstr[0] = term = tmps[5];
10614
10615     PL_multi_close = term;
10616
10617     /* A warning is raised if the input parameter requires it for escaped (by a
10618      * backslash) paired metacharacters {} [] and () when the delimiters are
10619      * those same characters, and the backslash is ineffective.  This doesn't
10620      * happen for <>, as they aren't metas. */
10621     if (deprecate_escaped_meta
10622         && (PL_multi_open == PL_multi_close
10623             || PL_multi_open == '<'
10624             || ! ckWARN_d(WARN_DEPRECATED)))
10625     {
10626         deprecate_escaped_meta = FALSE;
10627     }
10628
10629     /* create a new SV to hold the contents.  79 is the SV's initial length.
10630        What a random number. */
10631     sv = newSV_type(SVt_PVIV);
10632     SvGROW(sv, 80);
10633     SvIV_set(sv, termcode);
10634     (void)SvPOK_only(sv);               /* validate pointer */
10635
10636     /* move past delimiter and try to read a complete string */
10637     if (keep_delims)
10638         sv_catpvn(sv, s, termlen);
10639     s += termlen;
10640 #ifdef PERL_MAD
10641     tstart = SvPVX(PL_linestr) + stuffstart;
10642     if (PL_madskills && !PL_thisopen && !keep_delims) {
10643         PL_thisopen = newSVpvn(tstart, s - tstart);
10644         stuffstart = s - SvPVX(PL_linestr);
10645     }
10646 #endif
10647     for (;;) {
10648         if (PL_encoding && !UTF && !re_reparse) {
10649             bool cont = TRUE;
10650
10651             while (cont) {
10652                 int offset = s - SvPVX_const(PL_linestr);
10653                 const bool found = sv_cat_decode(sv, PL_encoding, PL_linestr,
10654                                            &offset, (char*)termstr, termlen);
10655                 const char *ns;
10656                 char *svlast;
10657
10658                 if (SvIsCOW(PL_linestr)) {
10659                     STRLEN bufend_pos, bufptr_pos, oldbufptr_pos;
10660                     STRLEN oldoldbufptr_pos, linestart_pos, last_uni_pos;
10661                     STRLEN last_lop_pos, re_eval_start_pos, s_pos;
10662                     char *buf = SvPVX(PL_linestr);
10663                     bufend_pos = PL_parser->bufend - buf;
10664                     bufptr_pos = PL_parser->bufptr - buf;
10665                     oldbufptr_pos = PL_parser->oldbufptr - buf;
10666                     oldoldbufptr_pos = PL_parser->oldoldbufptr - buf;
10667                     linestart_pos = PL_parser->linestart - buf;
10668                     last_uni_pos = PL_parser->last_uni
10669                         ? PL_parser->last_uni - buf
10670                         : 0;
10671                     last_lop_pos = PL_parser->last_lop
10672                         ? PL_parser->last_lop - buf
10673                         : 0;
10674                     re_eval_start_pos =
10675                         PL_parser->lex_shared->re_eval_start ?
10676                             PL_parser->lex_shared->re_eval_start - buf : 0;
10677                     s_pos = s - buf;
10678
10679                     sv_force_normal(PL_linestr);
10680
10681                     buf = SvPVX(PL_linestr);
10682                     PL_parser->bufend = buf + bufend_pos;
10683                     PL_parser->bufptr = buf + bufptr_pos;
10684                     PL_parser->oldbufptr = buf + oldbufptr_pos;
10685                     PL_parser->oldoldbufptr = buf + oldoldbufptr_pos;
10686                     PL_parser->linestart = buf + linestart_pos;
10687                     if (PL_parser->last_uni)
10688                         PL_parser->last_uni = buf + last_uni_pos;
10689                     if (PL_parser->last_lop)
10690                         PL_parser->last_lop = buf + last_lop_pos;
10691                     if (PL_parser->lex_shared->re_eval_start)
10692                         PL_parser->lex_shared->re_eval_start  =
10693                             buf + re_eval_start_pos;
10694                     s = buf + s_pos;
10695                 }
10696                 ns = SvPVX_const(PL_linestr) + offset;
10697                 svlast = SvEND(sv) - 1;
10698
10699                 for (; s < ns; s++) {
10700                     if (*s == '\n' && !PL_rsfp && !PL_parser->filtered)
10701                         COPLINE_INC_WITH_HERELINES;
10702                 }
10703                 if (!found)
10704                     goto read_more_line;
10705                 else {
10706                     /* handle quoted delimiters */
10707                     if (SvCUR(sv) > 1 && *(svlast-1) == '\\') {
10708                         const char *t;
10709                         for (t = svlast-2; t >= SvPVX_const(sv) && *t == '\\';)
10710                             t--;
10711                         if ((svlast-1 - t) % 2) {
10712                             if (!keep_quoted) {
10713                                 *(svlast-1) = term;
10714                                 *svlast = '\0';
10715                                 SvCUR_set(sv, SvCUR(sv) - 1);
10716                             }
10717                             continue;
10718                         }
10719                     }
10720                     if (PL_multi_open == PL_multi_close) {
10721                         cont = FALSE;
10722                     }
10723                     else {
10724                         const char *t;
10725                         char *w;
10726                         for (t = w = SvPVX(sv)+last_off; t < svlast; w++, t++) {
10727                             /* At here, all closes are "was quoted" one,
10728                                so we don't check PL_multi_close. */
10729                             if (*t == '\\') {
10730                                 if (!keep_quoted && *(t+1) == PL_multi_open)
10731                                     t++;
10732                                 else
10733                                     *w++ = *t++;
10734                             }
10735                             else if (*t == PL_multi_open)
10736                                 brackets++;
10737
10738                             *w = *t;
10739                         }
10740                         if (w < t) {
10741                             *w++ = term;
10742                             *w = '\0';
10743                             SvCUR_set(sv, w - SvPVX_const(sv));
10744                         }
10745                         last_off = w - SvPVX(sv);
10746                         if (--brackets <= 0)
10747                             cont = FALSE;
10748                     }
10749                 }
10750             }
10751             if (!keep_delims) {
10752                 SvCUR_set(sv, SvCUR(sv) - 1);
10753                 *SvEND(sv) = '\0';
10754             }
10755             break;
10756         }
10757
10758         /* extend sv if need be */
10759         SvGROW(sv, SvCUR(sv) + (PL_bufend - s) + 1);
10760         /* set 'to' to the next character in the sv's string */
10761         to = SvPVX(sv)+SvCUR(sv);
10762
10763         /* if open delimiter is the close delimiter read unbridle */
10764         if (PL_multi_open == PL_multi_close) {
10765             for (; s < PL_bufend; s++,to++) {
10766                 /* embedded newlines increment the current line number */
10767                 if (*s == '\n' && !PL_rsfp && !PL_parser->filtered)
10768                     COPLINE_INC_WITH_HERELINES;
10769                 /* handle quoted delimiters */
10770                 if (*s == '\\' && s+1 < PL_bufend && term != '\\') {
10771                     if (!keep_quoted
10772                         && (s[1] == term
10773                             || (re_reparse && s[1] == '\\'))
10774                     )
10775                         s++;
10776                     /* any other quotes are simply copied straight through */
10777                     else
10778                         *to++ = *s++;
10779                 }
10780                 /* terminate when run out of buffer (the for() condition), or
10781                    have found the terminator */
10782                 else if (*s == term) {
10783                     if (termlen == 1)
10784                         break;
10785                     if (s+termlen <= PL_bufend && memEQ(s, (char*)termstr, termlen))
10786                         break;
10787                 }
10788                 else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF)
10789                     has_utf8 = TRUE;
10790                 *to = *s;
10791             }
10792         }
10793         
10794         /* if the terminator isn't the same as the start character (e.g.,
10795            matched brackets), we have to allow more in the quoting, and
10796            be prepared for nested brackets.
10797         */
10798         else {
10799             /* read until we run out of string, or we find the terminator */
10800             for (; s < PL_bufend; s++,to++) {
10801                 /* embedded newlines increment the line count */
10802                 if (*s == '\n' && !PL_rsfp && !PL_parser->filtered)
10803                     COPLINE_INC_WITH_HERELINES;
10804                 /* backslashes can escape the open or closing characters */
10805                 if (*s == '\\' && s+1 < PL_bufend) {
10806                     if (!keep_quoted &&
10807                         ((s[1] == PL_multi_open) || (s[1] == PL_multi_close)))
10808                     {
10809                         s++;
10810
10811                         /* Here, 'deprecate_escaped_meta' is true iff the
10812                          * delimiters are paired metacharacters, and 's' points
10813                          * to an occurrence of one of them within the string,
10814                          * which was preceded by a backslash.  If this is a
10815                          * context where the delimiter is also a metacharacter,
10816                          * the backslash is useless, and deprecated.  () and []
10817                          * are meta in any context. {} are meta only when
10818                          * appearing in a quantifier or in things like '\p{'
10819                          * (but '\\p{' isn't meta).  They also aren't meta
10820                          * unless there is a matching closed, escaped char
10821                          * later on within the string.  If 's' points to an
10822                          * open, set a flag; if to a close, test that flag, and
10823                          * raise a warning if it was set */
10824
10825                         if (deprecate_escaped_meta) {
10826                             if (*s == PL_multi_open) {
10827                                 if (*s != '{') {
10828                                     escaped_open = s;
10829                                 }
10830                                      /* Look for a closing '\}' */
10831                                 else if (regcurly(s, TRUE)) {
10832                                     escaped_open = s;
10833                                 }
10834                                      /* Look for e.g.  '\x{' */
10835                                 else if (s - start > 2
10836                                          && _generic_isCC(*(s-2),
10837                                              _CC_BACKSLASH_FOO_LBRACE_IS_META))
10838                                 { /* Exclude '\\x', '\\\\x', etc. */
10839                                     char *lookbehind = s - 4;
10840                                     bool is_meta = TRUE;
10841                                     while (lookbehind >= start
10842                                            && *lookbehind == '\\')
10843                                     {
10844                                         is_meta = ! is_meta;
10845                                         lookbehind--;
10846                                     }
10847                                     if (is_meta) {
10848                                         escaped_open = s;
10849                                     }
10850                                 }
10851                             }
10852                             else if (escaped_open) {
10853                                 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
10854                                     "Useless use of '\\'; doesn't escape metacharacter '%c'", PL_multi_open);
10855                                 escaped_open = NULL;
10856                             }
10857                         }
10858                     }
10859                     else
10860                         *to++ = *s++;
10861                 }
10862                 /* allow nested opens and closes */
10863                 else if (*s == PL_multi_close && --brackets <= 0)
10864                     break;
10865                 else if (*s == PL_multi_open)
10866                     brackets++;
10867                 else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF)
10868                     has_utf8 = TRUE;
10869                 *to = *s;
10870             }
10871         }
10872         /* terminate the copied string and update the sv's end-of-string */
10873         *to = '\0';
10874         SvCUR_set(sv, to - SvPVX_const(sv));
10875
10876         /*
10877          * this next chunk reads more into the buffer if we're not done yet
10878          */
10879
10880         if (s < PL_bufend)
10881             break;              /* handle case where we are done yet :-) */
10882
10883 #ifndef PERL_STRICT_CR
10884         if (to - SvPVX_const(sv) >= 2) {
10885             if ((to[-2] == '\r' && to[-1] == '\n') ||
10886                 (to[-2] == '\n' && to[-1] == '\r'))
10887             {
10888                 to[-2] = '\n';
10889                 to--;
10890                 SvCUR_set(sv, to - SvPVX_const(sv));
10891             }
10892             else if (to[-1] == '\r')
10893                 to[-1] = '\n';
10894         }
10895         else if (to - SvPVX_const(sv) == 1 && to[-1] == '\r')
10896             to[-1] = '\n';
10897 #endif
10898         
10899      read_more_line:
10900         /* if we're out of file, or a read fails, bail and reset the current
10901            line marker so we can report where the unterminated string began
10902         */
10903 #ifdef PERL_MAD
10904         if (PL_madskills) {
10905             char * const tstart = SvPVX(PL_linestr) + stuffstart;
10906             if (PL_thisstuff)
10907                 sv_catpvn(PL_thisstuff, tstart, PL_bufend - tstart);
10908             else
10909                 PL_thisstuff = newSVpvn(tstart, PL_bufend - tstart);
10910         }
10911 #endif
10912         COPLINE_INC_WITH_HERELINES;
10913         PL_bufptr = PL_bufend;
10914         if (!lex_next_chunk(0)) {
10915             sv_free(sv);
10916             CopLINE_set(PL_curcop, (line_t)PL_multi_start);
10917             return NULL;
10918         }
10919         s = PL_bufptr;
10920 #ifdef PERL_MAD
10921         stuffstart = 0;
10922 #endif
10923     }
10924
10925     /* at this point, we have successfully read the delimited string */
10926
10927     if (!PL_encoding || UTF || re_reparse) {
10928 #ifdef PERL_MAD
10929         if (PL_madskills) {
10930             char * const tstart = SvPVX(PL_linestr) + stuffstart;
10931             const int len = s - tstart;
10932             if (PL_thisstuff)
10933                 sv_catpvn(PL_thisstuff, tstart, len);
10934             else
10935                 PL_thisstuff = newSVpvn(tstart, len);
10936             if (!PL_thisclose && !keep_delims)
10937                 PL_thisclose = newSVpvn(s,termlen);
10938         }
10939 #endif
10940
10941         if (keep_delims)
10942             sv_catpvn(sv, s, termlen);
10943         s += termlen;
10944     }
10945 #ifdef PERL_MAD
10946     else {
10947         if (PL_madskills) {
10948             char * const tstart = SvPVX(PL_linestr) + stuffstart;
10949             const int len = s - tstart - termlen;
10950             if (PL_thisstuff)
10951                 sv_catpvn(PL_thisstuff, tstart, len);
10952             else
10953                 PL_thisstuff = newSVpvn(tstart, len);
10954             if (!PL_thisclose && !keep_delims)
10955                 PL_thisclose = newSVpvn(s - termlen,termlen);
10956         }
10957     }
10958 #endif
10959     if (has_utf8 || (PL_encoding && !re_reparse))
10960         SvUTF8_on(sv);
10961
10962     PL_multi_end = CopLINE(PL_curcop);
10963     CopLINE_set(PL_curcop, PL_multi_start);
10964     PL_parser->herelines = herelines;
10965
10966     /* if we allocated too much space, give some back */
10967     if (SvCUR(sv) + 5 < SvLEN(sv)) {
10968         SvLEN_set(sv, SvCUR(sv) + 1);
10969         SvPV_renew(sv, SvLEN(sv));
10970     }
10971
10972     /* decide whether this is the first or second quoted string we've read
10973        for this op
10974     */
10975
10976     if (PL_lex_stuff)
10977         PL_sublex_info.repl = sv;
10978     else
10979         PL_lex_stuff = sv;
10980     return s;
10981 }
10982
10983 /*
10984   scan_num
10985   takes: pointer to position in buffer
10986   returns: pointer to new position in buffer
10987   side-effects: builds ops for the constant in pl_yylval.op
10988
10989   Read a number in any of the formats that Perl accepts:
10990
10991   \d(_?\d)*(\.(\d(_?\d)*)?)?[Ee][\+\-]?(\d(_?\d)*)      12 12.34 12.
10992   \.\d(_?\d)*[Ee][\+\-]?(\d(_?\d)*)                     .34
10993   0b[01](_?[01])*
10994   0[0-7](_?[0-7])*
10995   0x[0-9A-Fa-f](_?[0-9A-Fa-f])*
10996
10997   Like most scan_ routines, it uses the PL_tokenbuf buffer to hold the
10998   thing it reads.
10999
11000   If it reads a number without a decimal point or an exponent, it will
11001   try converting the number to an integer and see if it can do so
11002   without loss of precision.
11003 */
11004
11005 char *
11006 Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
11007 {
11008     dVAR;
11009     const char *s = start;      /* current position in buffer */
11010     char *d;                    /* destination in temp buffer */
11011     char *e;                    /* end of temp buffer */
11012     NV nv;                              /* number read, as a double */
11013     SV *sv = NULL;                      /* place to put the converted number */
11014     bool floatit;                       /* boolean: int or float? */
11015     const char *lastub = NULL;          /* position of last underbar */
11016     static const char* const number_too_long = "Number too long";
11017
11018     PERL_ARGS_ASSERT_SCAN_NUM;
11019
11020     /* We use the first character to decide what type of number this is */
11021
11022     switch (*s) {
11023     default:
11024         Perl_croak(aTHX_ "panic: scan_num, *s=%d", *s);
11025
11026     /* if it starts with a 0, it could be an octal number, a decimal in
11027        0.13 disguise, or a hexadecimal number, or a binary number. */
11028     case '0':
11029         {
11030           /* variables:
11031              u          holds the "number so far"
11032              shift      the power of 2 of the base
11033                         (hex == 4, octal == 3, binary == 1)
11034              overflowed was the number more than we can hold?
11035
11036              Shift is used when we add a digit.  It also serves as an "are
11037              we in octal/hex/binary?" indicator to disallow hex characters
11038              when in octal mode.
11039            */
11040             NV n = 0.0;
11041             UV u = 0;
11042             I32 shift;
11043             bool overflowed = FALSE;
11044             bool just_zero  = TRUE;     /* just plain 0 or binary number? */
11045             static const NV nvshift[5] = { 1.0, 2.0, 4.0, 8.0, 16.0 };
11046             static const char* const bases[5] =
11047               { "", "binary", "", "octal", "hexadecimal" };
11048             static const char* const Bases[5] =
11049               { "", "Binary", "", "Octal", "Hexadecimal" };
11050             static const char* const maxima[5] =
11051               { "",
11052                 "0b11111111111111111111111111111111",
11053                 "",
11054                 "037777777777",
11055                 "0xffffffff" };
11056             const char *base, *Base, *max;
11057
11058             /* check for hex */
11059             if (s[1] == 'x' || s[1] == 'X') {
11060                 shift = 4;
11061                 s += 2;
11062                 just_zero = FALSE;
11063             } else if (s[1] == 'b' || s[1] == 'B') {
11064                 shift = 1;
11065                 s += 2;
11066                 just_zero = FALSE;
11067             }
11068             /* check for a decimal in disguise */
11069             else if (s[1] == '.' || s[1] == 'e' || s[1] == 'E')
11070                 goto decimal;
11071             /* so it must be octal */
11072             else {
11073                 shift = 3;
11074                 s++;
11075             }
11076
11077             if (*s == '_') {
11078                 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
11079                                "Misplaced _ in number");
11080                lastub = s++;
11081             }
11082
11083             base = bases[shift];
11084             Base = Bases[shift];
11085             max  = maxima[shift];
11086
11087             /* read the rest of the number */
11088             for (;;) {
11089                 /* x is used in the overflow test,
11090                    b is the digit we're adding on. */
11091                 UV x, b;
11092
11093                 switch (*s) {
11094
11095                 /* if we don't mention it, we're done */
11096                 default:
11097                     goto out;
11098
11099                 /* _ are ignored -- but warned about if consecutive */
11100                 case '_':
11101                     if (lastub && s == lastub + 1)
11102                         Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
11103                                        "Misplaced _ in number");
11104                     lastub = s++;
11105                     break;
11106
11107                 /* 8 and 9 are not octal */
11108                 case '8': case '9':
11109                     if (shift == 3)
11110                         yyerror(Perl_form(aTHX_ "Illegal octal digit '%c'", *s));
11111                     /* FALL THROUGH */
11112
11113                 /* octal digits */
11114                 case '2': case '3': case '4':
11115                 case '5': case '6': case '7':
11116                     if (shift == 1)
11117                         yyerror(Perl_form(aTHX_ "Illegal binary digit '%c'", *s));
11118                     /* FALL THROUGH */
11119
11120                 case '0': case '1':
11121                     b = *s++ & 15;              /* ASCII digit -> value of digit */
11122                     goto digit;
11123
11124                 /* hex digits */
11125                 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
11126                 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
11127                     /* make sure they said 0x */
11128                     if (shift != 4)
11129                         goto out;
11130                     b = (*s++ & 7) + 9;
11131
11132                     /* Prepare to put the digit we have onto the end
11133                        of the number so far.  We check for overflows.
11134                     */
11135
11136                   digit:
11137                     just_zero = FALSE;
11138                     if (!overflowed) {
11139                         x = u << shift; /* make room for the digit */
11140
11141                         if ((x >> shift) != u
11142                             && !(PL_hints & HINT_NEW_BINARY)) {
11143                             overflowed = TRUE;
11144                             n = (NV) u;
11145                             Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
11146                                              "Integer overflow in %s number",
11147                                              base);
11148                         } else
11149                             u = x | b;          /* add the digit to the end */
11150                     }
11151                     if (overflowed) {
11152                         n *= nvshift[shift];
11153                         /* If an NV has not enough bits in its
11154                          * mantissa to represent an UV this summing of
11155                          * small low-order numbers is a waste of time
11156                          * (because the NV cannot preserve the
11157                          * low-order bits anyway): we could just
11158                          * remember when did we overflow and in the
11159                          * end just multiply n by the right
11160                          * amount. */
11161                         n += (NV) b;
11162                     }
11163                     break;
11164                 }
11165             }
11166
11167           /* if we get here, we had success: make a scalar value from
11168              the number.
11169           */
11170           out:
11171
11172             /* final misplaced underbar check */
11173             if (s[-1] == '_') {
11174                 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Misplaced _ in number");
11175             }
11176
11177             if (overflowed) {
11178                 if (n > 4294967295.0)
11179                     Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
11180                                    "%s number > %s non-portable",
11181                                    Base, max);
11182                 sv = newSVnv(n);
11183             }
11184             else {
11185 #if UVSIZE > 4
11186                 if (u > 0xffffffff)
11187                     Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
11188                                    "%s number > %s non-portable",
11189                                    Base, max);
11190 #endif
11191                 sv = newSVuv(u);
11192             }
11193             if (just_zero && (PL_hints & HINT_NEW_INTEGER))
11194                 sv = new_constant(start, s - start, "integer",
11195                                   sv, NULL, NULL, 0);
11196             else if (PL_hints & HINT_NEW_BINARY)
11197                 sv = new_constant(start, s - start, "binary", sv, NULL, NULL, 0);
11198         }
11199         break;
11200
11201     /*
11202       handle decimal numbers.
11203       we're also sent here when we read a 0 as the first digit
11204     */
11205     case '1': case '2': case '3': case '4': case '5':
11206     case '6': case '7': case '8': case '9': case '.':
11207       decimal:
11208         d = PL_tokenbuf;
11209         e = PL_tokenbuf + sizeof PL_tokenbuf - 6; /* room for various punctuation */
11210         floatit = FALSE;
11211
11212         /* read next group of digits and _ and copy into d */
11213         while (isDIGIT(*s) || *s == '_') {
11214             /* skip underscores, checking for misplaced ones
11215                if -w is on
11216             */
11217             if (*s == '_') {
11218                 if (lastub && s == lastub + 1)
11219                     Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
11220                                    "Misplaced _ in number");
11221                 lastub = s++;
11222             }
11223             else {
11224                 /* check for end of fixed-length buffer */
11225                 if (d >= e)
11226                     Perl_croak(aTHX_ "%s", number_too_long);
11227                 /* if we're ok, copy the character */
11228                 *d++ = *s++;
11229             }
11230         }
11231
11232         /* final misplaced underbar check */
11233         if (lastub && s == lastub + 1) {
11234             Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Misplaced _ in number");
11235         }
11236
11237         /* read a decimal portion if there is one.  avoid
11238            3..5 being interpreted as the number 3. followed
11239            by .5
11240         */
11241         if (*s == '.' && s[1] != '.') {
11242             floatit = TRUE;
11243             *d++ = *s++;
11244
11245             if (*s == '_') {
11246                 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
11247                                "Misplaced _ in number");
11248                 lastub = s;
11249             }
11250
11251             /* copy, ignoring underbars, until we run out of digits.
11252             */
11253             for (; isDIGIT(*s) || *s == '_'; s++) {
11254                 /* fixed length buffer check */
11255                 if (d >= e)
11256                     Perl_croak(aTHX_ "%s", number_too_long);
11257                 if (*s == '_') {
11258                    if (lastub && s == lastub + 1)
11259                        Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
11260                                       "Misplaced _ in number");
11261                    lastub = s;
11262                 }
11263                 else
11264                     *d++ = *s;
11265             }
11266             /* fractional part ending in underbar? */
11267             if (s[-1] == '_') {
11268                 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
11269                                "Misplaced _ in number");
11270             }
11271             if (*s == '.' && isDIGIT(s[1])) {
11272                 /* oops, it's really a v-string, but without the "v" */
11273                 s = start;
11274                 goto vstring;
11275             }
11276         }
11277
11278         /* read exponent part, if present */
11279         if ((*s == 'e' || *s == 'E') && strchr("+-0123456789_", s[1])) {
11280             floatit = TRUE;
11281             s++;
11282
11283             /* regardless of whether user said 3E5 or 3e5, use lower 'e' */
11284             *d++ = 'e';         /* At least some Mach atof()s don't grok 'E' */
11285
11286             /* stray preinitial _ */
11287             if (*s == '_') {
11288                 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
11289                                "Misplaced _ in number");
11290                 lastub = s++;
11291             }
11292
11293             /* allow positive or negative exponent */
11294             if (*s == '+' || *s == '-')
11295                 *d++ = *s++;
11296
11297             /* stray initial _ */
11298             if (*s == '_') {
11299                 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
11300                                "Misplaced _ in number");
11301                 lastub = s++;
11302             }
11303
11304             /* read digits of exponent */
11305             while (isDIGIT(*s) || *s == '_') {
11306                 if (isDIGIT(*s)) {
11307                     if (d >= e)
11308                         Perl_croak(aTHX_ "%s", number_too_long);
11309                     *d++ = *s++;
11310                 }
11311                 else {
11312                    if (((lastub && s == lastub + 1) ||
11313                         (!isDIGIT(s[1]) && s[1] != '_')))
11314                        Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
11315                                       "Misplaced _ in number");
11316                    lastub = s++;
11317                 }
11318             }
11319         }
11320
11321
11322         /*
11323            We try to do an integer conversion first if no characters
11324            indicating "float" have been found.
11325          */
11326
11327         if (!floatit) {
11328             UV uv;
11329             const int flags = grok_number (PL_tokenbuf, d - PL_tokenbuf, &uv);
11330
11331             if (flags == IS_NUMBER_IN_UV) {
11332               if (uv <= IV_MAX)
11333                 sv = newSViv(uv); /* Prefer IVs over UVs. */
11334               else
11335                 sv = newSVuv(uv);
11336             } else if (flags == (IS_NUMBER_IN_UV | IS_NUMBER_NEG)) {
11337               if (uv <= (UV) IV_MIN)
11338                 sv = newSViv(-(IV)uv);
11339               else
11340                 floatit = TRUE;
11341             } else
11342               floatit = TRUE;
11343         }
11344         if (floatit) {
11345             /* terminate the string */
11346             *d = '\0';
11347             nv = Atof(PL_tokenbuf);
11348             sv = newSVnv(nv);
11349         }
11350
11351         if ( floatit
11352              ? (PL_hints & HINT_NEW_FLOAT) : (PL_hints & HINT_NEW_INTEGER) ) {
11353             const char *const key = floatit ? "float" : "integer";
11354             const STRLEN keylen = floatit ? 5 : 7;
11355             sv = S_new_constant(aTHX_ PL_tokenbuf, d - PL_tokenbuf,
11356                                 key, keylen, sv, NULL, NULL, 0);
11357         }
11358         break;
11359
11360     /* if it starts with a v, it could be a v-string */
11361     case 'v':
11362 vstring:
11363                 sv = newSV(5); /* preallocate storage space */
11364                 ENTER_with_name("scan_vstring");
11365                 SAVEFREESV(sv);
11366                 s = scan_vstring(s, PL_bufend, sv);
11367                 SvREFCNT_inc_simple_void_NN(sv);
11368                 LEAVE_with_name("scan_vstring");
11369         break;
11370     }
11371
11372     /* make the op for the constant and return */
11373
11374     if (sv)
11375         lvalp->opval = newSVOP(OP_CONST, 0, sv);
11376     else
11377         lvalp->opval = NULL;
11378
11379     return (char *)s;
11380 }
11381
11382 STATIC char *
11383 S_scan_formline(pTHX_ char *s)
11384 {
11385     dVAR;
11386     char *eol;
11387     char *t;
11388     SV * const stuff = newSVpvs("");
11389     bool needargs = FALSE;
11390     bool eofmt = FALSE;
11391 #ifdef PERL_MAD
11392     char *tokenstart = s;
11393     SV* savewhite = NULL;
11394
11395     if (PL_madskills) {
11396         savewhite = PL_thiswhite;
11397         PL_thiswhite = 0;
11398     }
11399 #endif
11400
11401     PERL_ARGS_ASSERT_SCAN_FORMLINE;
11402
11403     while (!needargs) {
11404         if (*s == '.') {
11405             t = s+1;
11406 #ifdef PERL_STRICT_CR
11407             while (SPACE_OR_TAB(*t))
11408                 t++;
11409 #else
11410             while (SPACE_OR_TAB(*t) || *t == '\r')
11411                 t++;
11412 #endif
11413             if (*t == '\n' || t == PL_bufend) {
11414                 eofmt = TRUE;
11415                 break;
11416             }
11417         }
11418         eol = (char *) memchr(s,'\n',PL_bufend-s);
11419         if (!eol++)
11420                 eol = PL_bufend;
11421         if (*s != '#') {
11422             for (t = s; t < eol; t++) {
11423                 if (*t == '~' && t[1] == '~' && SvCUR(stuff)) {
11424                     needargs = FALSE;
11425                     goto enough;        /* ~~ must be first line in formline */
11426                 }
11427                 if (*t == '@' || *t == '^')
11428                     needargs = TRUE;
11429             }
11430             if (eol > s) {
11431                 sv_catpvn(stuff, s, eol-s);
11432 #ifndef PERL_STRICT_CR
11433                 if (eol-s > 1 && eol[-2] == '\r' && eol[-1] == '\n') {
11434                     char *end = SvPVX(stuff) + SvCUR(stuff);
11435                     end[-2] = '\n';
11436                     end[-1] = '\0';
11437                     SvCUR_set(stuff, SvCUR(stuff) - 1);
11438                 }
11439 #endif
11440             }
11441             else
11442               break;
11443         }
11444         s = (char*)eol;
11445         if ((PL_rsfp || PL_parser->filtered)
11446          && PL_parser->form_lex_state == LEX_NORMAL) {
11447             bool got_some;
11448 #ifdef PERL_MAD
11449             if (PL_madskills) {
11450                 if (PL_thistoken)
11451                     sv_catpvn(PL_thistoken, tokenstart, PL_bufend - tokenstart);
11452                 else
11453                     PL_thistoken = newSVpvn(tokenstart, PL_bufend - tokenstart);
11454             }
11455 #endif
11456             PL_bufptr = PL_bufend;
11457             COPLINE_INC_WITH_HERELINES;
11458             got_some = lex_next_chunk(0);
11459             CopLINE_dec(PL_curcop);
11460             s = PL_bufptr;
11461 #ifdef PERL_MAD
11462             tokenstart = PL_bufptr;
11463 #endif
11464             if (!got_some)
11465                 break;
11466         }
11467         incline(s);
11468     }
11469   enough:
11470     if (!SvCUR(stuff) || needargs)
11471         PL_lex_state = PL_parser->form_lex_state;
11472     if (SvCUR(stuff)) {
11473         PL_expect = XSTATE;
11474         if (needargs) {
11475             start_force(PL_curforce);
11476             NEXTVAL_NEXTTOKE.ival = 0;
11477             force_next(FORMLBRACK);
11478         }
11479         if (!IN_BYTES) {
11480             if (UTF && is_utf8_string((U8*)SvPVX_const(stuff), SvCUR(stuff)))
11481                 SvUTF8_on(stuff);
11482             else if (PL_encoding)
11483                 sv_recode_to_utf8(stuff, PL_encoding);
11484         }
11485         start_force(PL_curforce);
11486         NEXTVAL_NEXTTOKE.opval = (OP*)newSVOP(OP_CONST, 0, stuff);
11487         force_next(THING);
11488     }
11489     else {
11490         SvREFCNT_dec(stuff);
11491         if (eofmt)
11492             PL_lex_formbrack = 0;
11493     }
11494 #ifdef PERL_MAD
11495     if (PL_madskills) {
11496         if (PL_thistoken)
11497             sv_catpvn(PL_thistoken, tokenstart, s - tokenstart);
11498         else
11499             PL_thistoken = newSVpvn(tokenstart, s - tokenstart);
11500         PL_thiswhite = savewhite;
11501     }
11502 #endif
11503     return s;
11504 }
11505
11506 I32
11507 Perl_start_subparse(pTHX_ I32 is_format, U32 flags)
11508 {
11509     dVAR;
11510     const I32 oldsavestack_ix = PL_savestack_ix;
11511     CV* const outsidecv = PL_compcv;
11512
11513     SAVEI32(PL_subline);
11514     save_item(PL_subname);
11515     SAVESPTR(PL_compcv);
11516
11517     PL_compcv = MUTABLE_CV(newSV_type(is_format ? SVt_PVFM : SVt_PVCV));
11518     CvFLAGS(PL_compcv) |= flags;
11519
11520     PL_subline = CopLINE(PL_curcop);
11521     CvPADLIST(PL_compcv) = pad_new(padnew_SAVE|padnew_SAVESUB);
11522     CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outsidecv));
11523     CvOUTSIDE_SEQ(PL_compcv) = PL_cop_seqmax;
11524     if (outsidecv && CvPADLIST(outsidecv))
11525         CvPADLIST(PL_compcv)->xpadl_outid =
11526             PadlistNAMES(CvPADLIST(outsidecv));
11527
11528     return oldsavestack_ix;
11529 }
11530
11531 static int
11532 S_yywarn(pTHX_ const char *const s, U32 flags)
11533 {
11534     dVAR;
11535
11536     PERL_ARGS_ASSERT_YYWARN;
11537
11538     PL_in_eval |= EVAL_WARNONLY;
11539     yyerror_pv(s, flags);
11540     PL_in_eval &= ~EVAL_WARNONLY;
11541     return 0;
11542 }
11543
11544 int
11545 Perl_yyerror(pTHX_ const char *const s)
11546 {
11547     PERL_ARGS_ASSERT_YYERROR;
11548     return yyerror_pvn(s, strlen(s), 0);
11549 }
11550
11551 int
11552 Perl_yyerror_pv(pTHX_ const char *const s, U32 flags)
11553 {
11554     PERL_ARGS_ASSERT_YYERROR_PV;
11555     return yyerror_pvn(s, strlen(s), flags);
11556 }
11557
11558 int
11559 Perl_yyerror_pvn(pTHX_ const char *const s, STRLEN len, U32 flags)
11560 {
11561     dVAR;
11562     const char *context = NULL;
11563     int contlen = -1;
11564     SV *msg;
11565     SV * const where_sv = newSVpvs_flags("", SVs_TEMP);
11566     int yychar  = PL_parser->yychar;
11567
11568     PERL_ARGS_ASSERT_YYERROR_PVN;
11569
11570     if (!yychar || (yychar == ';' && !PL_rsfp))
11571         sv_catpvs(where_sv, "at EOF");
11572     else if (PL_oldoldbufptr && PL_bufptr > PL_oldoldbufptr &&
11573       PL_bufptr - PL_oldoldbufptr < 200 && PL_oldoldbufptr != PL_oldbufptr &&
11574       PL_oldbufptr != PL_bufptr) {
11575         /*
11576                 Only for NetWare:
11577                 The code below is removed for NetWare because it abends/crashes on NetWare
11578                 when the script has error such as not having the closing quotes like:
11579                     if ($var eq "value)
11580                 Checking of white spaces is anyway done in NetWare code.
11581         */
11582 #ifndef NETWARE
11583         while (isSPACE(*PL_oldoldbufptr))
11584             PL_oldoldbufptr++;
11585 #endif
11586         context = PL_oldoldbufptr;
11587         contlen = PL_bufptr - PL_oldoldbufptr;
11588     }
11589     else if (PL_oldbufptr && PL_bufptr > PL_oldbufptr &&
11590       PL_bufptr - PL_oldbufptr < 200 && PL_oldbufptr != PL_bufptr) {
11591         /*
11592                 Only for NetWare:
11593                 The code below is removed for NetWare because it abends/crashes on NetWare
11594                 when the script has error such as not having the closing quotes like:
11595                     if ($var eq "value)
11596                 Checking of white spaces is anyway done in NetWare code.
11597         */
11598 #ifndef NETWARE
11599         while (isSPACE(*PL_oldbufptr))
11600             PL_oldbufptr++;
11601 #endif
11602         context = PL_oldbufptr;
11603         contlen = PL_bufptr - PL_oldbufptr;
11604     }
11605     else if (yychar > 255)
11606         sv_catpvs(where_sv, "next token ???");
11607     else if (yychar == -2) { /* YYEMPTY */
11608         if (PL_lex_state == LEX_NORMAL ||
11609            (PL_lex_state == LEX_KNOWNEXT && PL_lex_defer == LEX_NORMAL))
11610             sv_catpvs(where_sv, "at end of line");
11611         else if (PL_lex_inpat)
11612             sv_catpvs(where_sv, "within pattern");
11613         else
11614             sv_catpvs(where_sv, "within string");
11615     }
11616     else {
11617         sv_catpvs(where_sv, "next char ");
11618         if (yychar < 32)
11619             Perl_sv_catpvf(aTHX_ where_sv, "^%c", toCTRL(yychar));
11620         else if (isPRINT_LC(yychar)) {
11621             const char string = yychar;
11622             sv_catpvn(where_sv, &string, 1);
11623         }
11624         else
11625             Perl_sv_catpvf(aTHX_ where_sv, "\\%03o", yychar & 255);
11626     }
11627     msg = newSVpvn_flags(s, len, (flags & SVf_UTF8) | SVs_TEMP);
11628     Perl_sv_catpvf(aTHX_ msg, " at %s line %"IVdf", ",
11629         OutCopFILE(PL_curcop),
11630         (IV)(PL_parser->preambling == NOLINE
11631                ? CopLINE(PL_curcop)
11632                : PL_parser->preambling));
11633     if (context)
11634         Perl_sv_catpvf(aTHX_ msg, "near \"%"UTF8f"\"\n",
11635                              UTF8fARG(UTF, contlen, context));
11636     else
11637         Perl_sv_catpvf(aTHX_ msg, "%"SVf"\n", SVfARG(where_sv));
11638     if (PL_multi_start < PL_multi_end && (U32)(CopLINE(PL_curcop) - PL_multi_end) <= 1) {
11639         Perl_sv_catpvf(aTHX_ msg,
11640         "  (Might be a runaway multi-line %c%c string starting on line %"IVdf")\n",
11641                 (int)PL_multi_open,(int)PL_multi_close,(IV)PL_multi_start);
11642         PL_multi_end = 0;
11643     }
11644     if (PL_in_eval & EVAL_WARNONLY) {
11645         Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX), "%"SVf, SVfARG(msg));
11646     }
11647     else
11648         qerror(msg);
11649     if (PL_error_count >= 10) {
11650         SV * errsv;
11651         if (PL_in_eval && ((errsv = ERRSV), SvCUR(errsv)))
11652             Perl_croak(aTHX_ "%"SVf"%s has too many errors.\n",
11653                        SVfARG(errsv), OutCopFILE(PL_curcop));
11654         else
11655             Perl_croak(aTHX_ "%s has too many errors.\n",
11656             OutCopFILE(PL_curcop));
11657     }
11658     PL_in_my = 0;
11659     PL_in_my_stash = NULL;
11660     return 0;
11661 }
11662
11663 STATIC char*
11664 S_swallow_bom(pTHX_ U8 *s)
11665 {
11666     dVAR;
11667     const STRLEN slen = SvCUR(PL_linestr);
11668
11669     PERL_ARGS_ASSERT_SWALLOW_BOM;
11670
11671     switch (s[0]) {
11672     case 0xFF:
11673         if (s[1] == 0xFE) {
11674             /* UTF-16 little-endian? (or UTF-32LE?) */
11675             if (s[2] == 0 && s[3] == 0)  /* UTF-32 little-endian */
11676                 /* diag_listed_as: Unsupported script encoding %s */
11677                 Perl_croak(aTHX_ "Unsupported script encoding UTF-32LE");
11678 #ifndef PERL_NO_UTF16_FILTER
11679             if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (BOM)\n");
11680             s += 2;
11681             if (PL_bufend > (char*)s) {
11682                 s = add_utf16_textfilter(s, TRUE);
11683             }
11684 #else
11685             /* diag_listed_as: Unsupported script encoding %s */
11686             Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
11687 #endif
11688         }
11689         break;
11690     case 0xFE:
11691         if (s[1] == 0xFF) {   /* UTF-16 big-endian? */
11692 #ifndef PERL_NO_UTF16_FILTER
11693             if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (BOM)\n");
11694             s += 2;
11695             if (PL_bufend > (char *)s) {
11696                 s = add_utf16_textfilter(s, FALSE);
11697             }
11698 #else
11699             /* diag_listed_as: Unsupported script encoding %s */
11700             Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
11701 #endif
11702         }
11703         break;
11704     case BOM_UTF8_FIRST_BYTE: {
11705         const STRLEN len = sizeof(BOM_UTF8_TAIL) - 1; /* Exclude trailing NUL */
11706         if (slen > len && memEQ(s+1, BOM_UTF8_TAIL, len)) {
11707             if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-8 script encoding (BOM)\n");
11708             s += len + 1;                      /* UTF-8 */
11709         }
11710         break;
11711     }
11712     case 0:
11713         if (slen > 3) {
11714              if (s[1] == 0) {
11715                   if (s[2] == 0xFE && s[3] == 0xFF) {
11716                        /* UTF-32 big-endian */
11717                        /* diag_listed_as: Unsupported script encoding %s */
11718                        Perl_croak(aTHX_ "Unsupported script encoding UTF-32BE");
11719                   }
11720              }
11721              else if (s[2] == 0 && s[3] != 0) {
11722                   /* Leading bytes
11723                    * 00 xx 00 xx
11724                    * are a good indicator of UTF-16BE. */
11725 #ifndef PERL_NO_UTF16_FILTER
11726                   if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (no BOM)\n");
11727                   s = add_utf16_textfilter(s, FALSE);
11728 #else
11729                   /* diag_listed_as: Unsupported script encoding %s */
11730                   Perl_croak(aTHX_ "Unsupported script encoding UTF-16BE");
11731 #endif
11732              }
11733         }
11734
11735     default:
11736          if (slen > 3 && s[1] == 0 && s[2] != 0 && s[3] == 0) {
11737                   /* Leading bytes
11738                    * xx 00 xx 00
11739                    * are a good indicator of UTF-16LE. */
11740 #ifndef PERL_NO_UTF16_FILTER
11741               if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (no BOM)\n");
11742               s = add_utf16_textfilter(s, TRUE);
11743 #else
11744               /* diag_listed_as: Unsupported script encoding %s */
11745               Perl_croak(aTHX_ "Unsupported script encoding UTF-16LE");
11746 #endif
11747          }
11748     }
11749     return (char*)s;
11750 }
11751
11752
11753 #ifndef PERL_NO_UTF16_FILTER
11754 static I32
11755 S_utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen)
11756 {
11757     dVAR;
11758     SV *const filter = FILTER_DATA(idx);
11759     /* We re-use this each time round, throwing the contents away before we
11760        return.  */
11761     SV *const utf16_buffer = MUTABLE_SV(IoTOP_GV(filter));
11762     SV *const utf8_buffer = filter;
11763     IV status = IoPAGE(filter);
11764     const bool reverse = cBOOL(IoLINES(filter));
11765     I32 retval;
11766
11767     PERL_ARGS_ASSERT_UTF16_TEXTFILTER;
11768
11769     /* As we're automatically added, at the lowest level, and hence only called
11770        from this file, we can be sure that we're not called in block mode. Hence
11771        don't bother writing code to deal with block mode.  */
11772     if (maxlen) {
11773         Perl_croak(aTHX_ "panic: utf16_textfilter called in block mode (for %d characters)", maxlen);
11774     }
11775     if (status < 0) {
11776         Perl_croak(aTHX_ "panic: utf16_textfilter called after error (status=%"IVdf")", status);
11777     }
11778     DEBUG_P(PerlIO_printf(Perl_debug_log,
11779                           "utf16_textfilter(%p,%ce): idx=%d maxlen=%d status=%"IVdf" utf16=%"UVuf" utf8=%"UVuf"\n",
11780                           FPTR2DPTR(void *, S_utf16_textfilter),
11781                           reverse ? 'l' : 'b', idx, maxlen, status,
11782                           (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
11783
11784     while (1) {
11785         STRLEN chars;
11786         STRLEN have;
11787         I32 newlen;
11788         U8 *end;
11789         /* First, look in our buffer of existing UTF-8 data:  */
11790         char *nl = (char *)memchr(SvPVX(utf8_buffer), '\n', SvCUR(utf8_buffer));
11791
11792         if (nl) {
11793             ++nl;
11794         } else if (status == 0) {
11795             /* EOF */
11796             IoPAGE(filter) = 0;
11797             nl = SvEND(utf8_buffer);
11798         }
11799         if (nl) {
11800             STRLEN got = nl - SvPVX(utf8_buffer);
11801             /* Did we have anything to append?  */
11802             retval = got != 0;
11803             sv_catpvn(sv, SvPVX(utf8_buffer), got);
11804             /* Everything else in this code works just fine if SVp_POK isn't
11805                set.  This, however, needs it, and we need it to work, else
11806                we loop infinitely because the buffer is never consumed.  */
11807             sv_chop(utf8_buffer, nl);
11808             break;
11809         }
11810
11811         /* OK, not a complete line there, so need to read some more UTF-16.
11812            Read an extra octect if the buffer currently has an odd number. */
11813         while (1) {
11814             if (status <= 0)
11815                 break;
11816             if (SvCUR(utf16_buffer) >= 2) {
11817                 /* Location of the high octet of the last complete code point.
11818                    Gosh, UTF-16 is a pain. All the benefits of variable length,
11819                    *coupled* with all the benefits of partial reads and
11820                    endianness.  */
11821                 const U8 *const last_hi = (U8*)SvPVX(utf16_buffer)
11822                     + ((SvCUR(utf16_buffer) & ~1) - (reverse ? 1 : 2));
11823
11824                 if (*last_hi < 0xd8 || *last_hi > 0xdb) {
11825                     break;
11826                 }
11827
11828                 /* We have the first half of a surrogate. Read more.  */
11829                 DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter partial surrogate detected at %p\n", last_hi));
11830             }
11831
11832             status = FILTER_READ(idx + 1, utf16_buffer,
11833                                  160 + (SvCUR(utf16_buffer) & 1));
11834             DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16_textfilter status=%"IVdf" SvCUR(sv)=%"UVuf"\n", status, (UV)SvCUR(utf16_buffer)));
11835             DEBUG_P({ sv_dump(utf16_buffer); sv_dump(utf8_buffer);});
11836             if (status < 0) {
11837                 /* Error */
11838                 IoPAGE(filter) = status;
11839                 return status;
11840             }
11841         }
11842
11843         chars = SvCUR(utf16_buffer) >> 1;
11844         have = SvCUR(utf8_buffer);
11845         SvGROW(utf8_buffer, have + chars * 3 + 1);
11846
11847         if (reverse) {
11848             end = utf16_to_utf8_reversed((U8*)SvPVX(utf16_buffer),
11849                                          (U8*)SvPVX_const(utf8_buffer) + have,
11850                                          chars * 2, &newlen);
11851         } else {
11852             end = utf16_to_utf8((U8*)SvPVX(utf16_buffer),
11853                                 (U8*)SvPVX_const(utf8_buffer) + have,
11854                                 chars * 2, &newlen);
11855         }
11856         SvCUR_set(utf8_buffer, have + newlen);
11857         *end = '\0';
11858
11859         /* No need to keep this SV "well-formed" with a '\0' after the end, as
11860            it's private to us, and utf16_to_utf8{,reversed} take a
11861            (pointer,length) pair, rather than a NUL-terminated string.  */
11862         if(SvCUR(utf16_buffer) & 1) {
11863             *SvPVX(utf16_buffer) = SvEND(utf16_buffer)[-1];
11864             SvCUR_set(utf16_buffer, 1);
11865         } else {
11866             SvCUR_set(utf16_buffer, 0);
11867         }
11868     }
11869     DEBUG_P(PerlIO_printf(Perl_debug_log,
11870                           "utf16_textfilter: returns, status=%"IVdf" utf16=%"UVuf" utf8=%"UVuf"\n",
11871                           status,
11872                           (UV)SvCUR(utf16_buffer), (UV)SvCUR(utf8_buffer)));
11873     DEBUG_P({ sv_dump(utf8_buffer); sv_dump(sv);});
11874     return retval;
11875 }
11876
11877 static U8 *
11878 S_add_utf16_textfilter(pTHX_ U8 *const s, bool reversed)
11879 {
11880     SV *filter = filter_add(S_utf16_textfilter, NULL);
11881
11882     PERL_ARGS_ASSERT_ADD_UTF16_TEXTFILTER;
11883
11884     IoTOP_GV(filter) = MUTABLE_GV(newSVpvn((char *)s, PL_bufend - (char*)s));
11885     sv_setpvs(filter, "");
11886     IoLINES(filter) = reversed;
11887     IoPAGE(filter) = 1; /* Not EOF */
11888
11889     /* Sadly, we have to return a valid pointer, come what may, so we have to
11890        ignore any error return from this.  */
11891     SvCUR_set(PL_linestr, 0);
11892     if (FILTER_READ(0, PL_linestr, 0)) {
11893         SvUTF8_on(PL_linestr);
11894     } else {
11895         SvUTF8_on(PL_linestr);
11896     }
11897     PL_bufend = SvEND(PL_linestr);
11898     return (U8*)SvPVX(PL_linestr);
11899 }
11900 #endif
11901
11902 /*
11903 Returns a pointer to the next character after the parsed
11904 vstring, as well as updating the passed in sv.
11905
11906 Function must be called like
11907
11908         sv = sv_2mortal(newSV(5));
11909         s = scan_vstring(s,e,sv);
11910
11911 where s and e are the start and end of the string.
11912 The sv should already be large enough to store the vstring
11913 passed in, for performance reasons.
11914
11915 This function may croak if fatal warnings are enabled in the
11916 calling scope, hence the sv_2mortal in the example (to prevent
11917 a leak).  Make sure to do SvREFCNT_inc afterwards if you use
11918 sv_2mortal.
11919
11920 */
11921
11922 char *
11923 Perl_scan_vstring(pTHX_ const char *s, const char *const e, SV *sv)
11924 {
11925     dVAR;
11926     const char *pos = s;
11927     const char *start = s;
11928
11929     PERL_ARGS_ASSERT_SCAN_VSTRING;
11930
11931     if (*pos == 'v') pos++;  /* get past 'v' */
11932     while (pos < e && (isDIGIT(*pos) || *pos == '_'))
11933         pos++;
11934     if ( *pos != '.') {
11935         /* this may not be a v-string if followed by => */
11936         const char *next = pos;
11937         while (next < e && isSPACE(*next))
11938             ++next;
11939         if ((e - next) >= 2 && *next == '=' && next[1] == '>' ) {
11940             /* return string not v-string */
11941             sv_setpvn(sv,(char *)s,pos-s);
11942             return (char *)pos;
11943         }
11944     }
11945
11946     if (!isALPHA(*pos)) {
11947         U8 tmpbuf[UTF8_MAXBYTES+1];
11948
11949         if (*s == 'v')
11950             s++;  /* get past 'v' */
11951
11952         sv_setpvs(sv, "");
11953
11954         for (;;) {
11955             /* this is atoi() that tolerates underscores */
11956             U8 *tmpend;
11957             UV rev = 0;
11958             const char *end = pos;
11959             UV mult = 1;
11960             while (--end >= s) {
11961                 if (*end != '_') {
11962                     const UV orev = rev;
11963                     rev += (*end - '0') * mult;
11964                     mult *= 10;
11965                     if (orev > rev)
11966                         /* diag_listed_as: Integer overflow in %s number */
11967                         Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
11968                                          "Integer overflow in decimal number");
11969                 }
11970             }
11971 #ifdef EBCDIC
11972             if (rev > 0x7FFFFFFF)
11973                  Perl_croak(aTHX_ "In EBCDIC the v-string components cannot exceed 2147483647");
11974 #endif
11975             /* Append native character for the rev point */
11976             tmpend = uvchr_to_utf8(tmpbuf, rev);
11977             sv_catpvn(sv, (const char*)tmpbuf, tmpend - tmpbuf);
11978             if (!UVCHR_IS_INVARIANT(rev))
11979                  SvUTF8_on(sv);
11980             if (pos + 1 < e && *pos == '.' && isDIGIT(pos[1]))
11981                  s = ++pos;
11982             else {
11983                  s = pos;
11984                  break;
11985             }
11986             while (pos < e && (isDIGIT(*pos) || *pos == '_'))
11987                  pos++;
11988         }
11989         SvPOK_on(sv);
11990         sv_magic(sv,NULL,PERL_MAGIC_vstring,(const char*)start, pos-start);
11991         SvRMAGICAL_on(sv);
11992     }
11993     return (char *)s;
11994 }
11995
11996 int
11997 Perl_keyword_plugin_standard(pTHX_
11998         char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
11999 {
12000     PERL_ARGS_ASSERT_KEYWORD_PLUGIN_STANDARD;
12001     PERL_UNUSED_CONTEXT;
12002     PERL_UNUSED_ARG(keyword_ptr);
12003     PERL_UNUSED_ARG(keyword_len);
12004     PERL_UNUSED_ARG(op_ptr);
12005     return KEYWORD_PLUGIN_DECLINE;
12006 }
12007
12008 #define parse_recdescent(g,p) S_parse_recdescent(aTHX_ g,p)
12009 static void
12010 S_parse_recdescent(pTHX_ int gramtype, I32 fakeeof)
12011 {
12012     SAVEI32(PL_lex_brackets);
12013     if (PL_lex_brackets > 100)
12014         Renew(PL_lex_brackstack, PL_lex_brackets + 10, char);
12015     PL_lex_brackstack[PL_lex_brackets++] = XFAKEEOF;
12016     SAVEI32(PL_lex_allbrackets);
12017     PL_lex_allbrackets = 0;
12018     SAVEI8(PL_lex_fakeeof);
12019     PL_lex_fakeeof = (U8)fakeeof;
12020     if(yyparse(gramtype) && !PL_parser->error_count)
12021         qerror(Perl_mess(aTHX_ "Parse error"));
12022 }
12023
12024 #define parse_recdescent_for_op(g,p) S_parse_recdescent_for_op(aTHX_ g,p)
12025 static OP *
12026 S_parse_recdescent_for_op(pTHX_ int gramtype, I32 fakeeof)
12027 {
12028     OP *o;
12029     ENTER;
12030     SAVEVPTR(PL_eval_root);
12031     PL_eval_root = NULL;
12032     parse_recdescent(gramtype, fakeeof);
12033     o = PL_eval_root;
12034     LEAVE;
12035     return o;
12036 }
12037
12038 #define parse_expr(p,f) S_parse_expr(aTHX_ p,f)
12039 static OP *
12040 S_parse_expr(pTHX_ I32 fakeeof, U32 flags)
12041 {
12042     OP *exprop;
12043     if (flags & ~PARSE_OPTIONAL)
12044         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_expr");
12045     exprop = parse_recdescent_for_op(GRAMEXPR, fakeeof);
12046     if (!exprop && !(flags & PARSE_OPTIONAL)) {
12047         if (!PL_parser->error_count)
12048             qerror(Perl_mess(aTHX_ "Parse error"));
12049         exprop = newOP(OP_NULL, 0);
12050     }
12051     return exprop;
12052 }
12053
12054 /*
12055 =for apidoc Amx|OP *|parse_arithexpr|U32 flags
12056
12057 Parse a Perl arithmetic expression.  This may contain operators of precedence
12058 down to the bit shift operators.  The expression must be followed (and thus
12059 terminated) either by a comparison or lower-precedence operator or by
12060 something that would normally terminate an expression such as semicolon.
12061 If I<flags> includes C<PARSE_OPTIONAL> then the expression is optional,
12062 otherwise it is mandatory.  It is up to the caller to ensure that the
12063 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12064 the source of the code to be parsed and the lexical context for the
12065 expression.
12066
12067 The op tree representing the expression is returned.  If an optional
12068 expression is absent, a null pointer is returned, otherwise the pointer
12069 will be non-null.
12070
12071 If an error occurs in parsing or compilation, in most cases a valid op
12072 tree is returned anyway.  The error is reflected in the parser state,
12073 normally resulting in a single exception at the top level of parsing
12074 which covers all the compilation errors that occurred.  Some compilation
12075 errors, however, will throw an exception immediately.
12076
12077 =cut
12078 */
12079
12080 OP *
12081 Perl_parse_arithexpr(pTHX_ U32 flags)
12082 {
12083     return parse_expr(LEX_FAKEEOF_COMPARE, flags);
12084 }
12085
12086 /*
12087 =for apidoc Amx|OP *|parse_termexpr|U32 flags
12088
12089 Parse a Perl term expression.  This may contain operators of precedence
12090 down to the assignment operators.  The expression must be followed (and thus
12091 terminated) either by a comma or lower-precedence operator or by
12092 something that would normally terminate an expression such as semicolon.
12093 If I<flags> includes C<PARSE_OPTIONAL> then the expression is optional,
12094 otherwise it is mandatory.  It is up to the caller to ensure that the
12095 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12096 the source of the code to be parsed and the lexical context for the
12097 expression.
12098
12099 The op tree representing the expression is returned.  If an optional
12100 expression is absent, a null pointer is returned, otherwise the pointer
12101 will be non-null.
12102
12103 If an error occurs in parsing or compilation, in most cases a valid op
12104 tree is returned anyway.  The error is reflected in the parser state,
12105 normally resulting in a single exception at the top level of parsing
12106 which covers all the compilation errors that occurred.  Some compilation
12107 errors, however, will throw an exception immediately.
12108
12109 =cut
12110 */
12111
12112 OP *
12113 Perl_parse_termexpr(pTHX_ U32 flags)
12114 {
12115     return parse_expr(LEX_FAKEEOF_COMMA, flags);
12116 }
12117
12118 /*
12119 =for apidoc Amx|OP *|parse_listexpr|U32 flags
12120
12121 Parse a Perl list expression.  This may contain operators of precedence
12122 down to the comma operator.  The expression must be followed (and thus
12123 terminated) either by a low-precedence logic operator such as C<or> or by
12124 something that would normally terminate an expression such as semicolon.
12125 If I<flags> includes C<PARSE_OPTIONAL> then the expression is optional,
12126 otherwise it is mandatory.  It is up to the caller to ensure that the
12127 dynamic parser state (L</PL_parser> et al) is correctly set to reflect
12128 the source of the code to be parsed and the lexical context for the
12129 expression.
12130
12131 The op tree representing the expression is returned.  If an optional
12132 expression is absent, a null pointer is returned, otherwise the pointer
12133 will be non-null.
12134
12135 If an error occurs in parsing or compilation, in most cases a valid op
12136 tree is returned anyway.  The error is reflected in the parser state,
12137 normally resulting in a single exception at the top level of parsing
12138 which covers all the compilation errors that occurred.  Some compilation
12139 errors, however, will throw an exception immediately.
12140
12141 =cut
12142 */
12143
12144 OP *
12145 Perl_parse_listexpr(pTHX_ U32 flags)
12146 {
12147     return parse_expr(LEX_FAKEEOF_LOWLOGIC, flags);
12148 }
12149
12150 /*
12151 =for apidoc Amx|OP *|parse_fullexpr|U32 flags
12152
12153 Parse a single complete Perl expression.  This allows the full
12154 expression grammar, including the lowest-precedence operators such
12155 as C<or>.  The expression must be followed (and thus terminated) by a
12156 token that an expression would normally be terminated by: end-of-file,
12157 closing bracketing punctuation, semicolon, or one of the keywords that
12158 signals a postfix expression-statement modifier.  If I<flags> includes
12159 C<PARSE_OPTIONAL> then the expression is optional, otherwise it is
12160 mandatory.  It is up to the caller to ensure that the dynamic parser
12161 state (L</PL_parser> et al) is correctly set to reflect the source of
12162 the code to be parsed and the lexical context for the expression.
12163
12164 The op tree representing the expression is returned.  If an optional
12165 expression is absent, a null pointer is returned, otherwise the pointer
12166 will be non-null.
12167
12168 If an error occurs in parsing or compilation, in most cases a valid op
12169 tree is returned anyway.  The error is reflected in the parser state,
12170 normally resulting in a single exception at the top level of parsing
12171 which covers all the compilation errors that occurred.  Some compilation
12172 errors, however, will throw an exception immediately.
12173
12174 =cut
12175 */
12176
12177 OP *
12178 Perl_parse_fullexpr(pTHX_ U32 flags)
12179 {
12180     return parse_expr(LEX_FAKEEOF_NONEXPR, flags);
12181 }
12182
12183 /*
12184 =for apidoc Amx|OP *|parse_block|U32 flags
12185
12186 Parse a single complete Perl code block.  This consists of an opening
12187 brace, a sequence of statements, and a closing brace.  The block
12188 constitutes a lexical scope, so C<my> variables and various compile-time
12189 effects can be contained within it.  It is up to the caller to ensure
12190 that the dynamic parser state (L</PL_parser> et al) is correctly set to
12191 reflect the source of the code to be parsed and the lexical context for
12192 the statement.
12193
12194 The op tree representing the code block is returned.  This is always a
12195 real op, never a null pointer.  It will normally be a C<lineseq> list,
12196 including C<nextstate> or equivalent ops.  No ops to construct any kind
12197 of runtime scope are included by virtue of it being a block.
12198
12199 If an error occurs in parsing or compilation, in most cases a valid op
12200 tree (most likely null) is returned anyway.  The error is reflected in
12201 the parser state, normally resulting in a single exception at the top
12202 level of parsing which covers all the compilation errors that occurred.
12203 Some compilation errors, however, will throw an exception immediately.
12204
12205 The I<flags> parameter is reserved for future use, and must always
12206 be zero.
12207
12208 =cut
12209 */
12210
12211 OP *
12212 Perl_parse_block(pTHX_ U32 flags)
12213 {
12214     if (flags)
12215         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_block");
12216     return parse_recdescent_for_op(GRAMBLOCK, LEX_FAKEEOF_NEVER);
12217 }
12218
12219 /*
12220 =for apidoc Amx|OP *|parse_barestmt|U32 flags
12221
12222 Parse a single unadorned Perl statement.  This may be a normal imperative
12223 statement or a declaration that has compile-time effect.  It does not
12224 include any label or other affixture.  It is up to the caller to ensure
12225 that the dynamic parser state (L</PL_parser> et al) is correctly set to
12226 reflect the source of the code to be parsed and the lexical context for
12227 the statement.
12228
12229 The op tree representing the statement is returned.  This may be a
12230 null pointer if the statement is null, for example if it was actually
12231 a subroutine definition (which has compile-time side effects).  If not
12232 null, it will be ops directly implementing the statement, suitable to
12233 pass to L</newSTATEOP>.  It will not normally include a C<nextstate> or
12234 equivalent op (except for those embedded in a scope contained entirely
12235 within the statement).
12236
12237 If an error occurs in parsing or compilation, in most cases a valid op
12238 tree (most likely null) is returned anyway.  The error is reflected in
12239 the parser state, normally resulting in a single exception at the top
12240 level of parsing which covers all the compilation errors that occurred.
12241 Some compilation errors, however, will throw an exception immediately.
12242
12243 The I<flags> parameter is reserved for future use, and must always
12244 be zero.
12245
12246 =cut
12247 */
12248
12249 OP *
12250 Perl_parse_barestmt(pTHX_ U32 flags)
12251 {
12252     if (flags)
12253         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_barestmt");
12254     return parse_recdescent_for_op(GRAMBARESTMT, LEX_FAKEEOF_NEVER);
12255 }
12256
12257 /*
12258 =for apidoc Amx|SV *|parse_label|U32 flags
12259
12260 Parse a single label, possibly optional, of the type that may prefix a
12261 Perl statement.  It is up to the caller to ensure that the dynamic parser
12262 state (L</PL_parser> et al) is correctly set to reflect the source of
12263 the code to be parsed.  If I<flags> includes C<PARSE_OPTIONAL> then the
12264 label is optional, otherwise it is mandatory.
12265
12266 The name of the label is returned in the form of a fresh scalar.  If an
12267 optional label is absent, a null pointer is returned.
12268
12269 If an error occurs in parsing, which can only occur if the label is
12270 mandatory, a valid label is returned anyway.  The error is reflected in
12271 the parser state, normally resulting in a single exception at the top
12272 level of parsing which covers all the compilation errors that occurred.
12273
12274 =cut
12275 */
12276
12277 SV *
12278 Perl_parse_label(pTHX_ U32 flags)
12279 {
12280     if (flags & ~PARSE_OPTIONAL)
12281         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_label");
12282     if (PL_lex_state == LEX_KNOWNEXT) {
12283         PL_parser->yychar = yylex();
12284         if (PL_parser->yychar == LABEL) {
12285             char * const lpv = pl_yylval.pval;
12286             STRLEN llen = strlen(lpv);
12287             PL_parser->yychar = YYEMPTY;
12288             return newSVpvn_flags(lpv, llen, lpv[llen+1] ? SVf_UTF8 : 0);
12289         } else {
12290             yyunlex();
12291             goto no_label;
12292         }
12293     } else {
12294         char *s, *t;
12295         STRLEN wlen, bufptr_pos;
12296         lex_read_space(0);
12297         t = s = PL_bufptr;
12298         if (!isIDFIRST_lazy_if(s, UTF))
12299             goto no_label;
12300         t = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &wlen);
12301         if (word_takes_any_delimeter(s, wlen))
12302             goto no_label;
12303         bufptr_pos = s - SvPVX(PL_linestr);
12304         PL_bufptr = t;
12305         lex_read_space(LEX_KEEP_PREVIOUS);
12306         t = PL_bufptr;
12307         s = SvPVX(PL_linestr) + bufptr_pos;
12308         if (t[0] == ':' && t[1] != ':') {
12309             PL_oldoldbufptr = PL_oldbufptr;
12310             PL_oldbufptr = s;
12311             PL_bufptr = t+1;
12312             return newSVpvn_flags(s, wlen, UTF ? SVf_UTF8 : 0);
12313         } else {
12314             PL_bufptr = s;
12315             no_label:
12316             if (flags & PARSE_OPTIONAL) {
12317                 return NULL;
12318             } else {
12319                 qerror(Perl_mess(aTHX_ "Parse error"));
12320                 return newSVpvs("x");
12321             }
12322         }
12323     }
12324 }
12325
12326 /*
12327 =for apidoc Amx|OP *|parse_fullstmt|U32 flags
12328
12329 Parse a single complete Perl statement.  This may be a normal imperative
12330 statement or a declaration that has compile-time effect, and may include
12331 optional labels.  It is up to the caller to ensure that the dynamic
12332 parser state (L</PL_parser> et al) is correctly set to reflect the source
12333 of the code to be parsed and the lexical context for the statement.
12334
12335 The op tree representing the statement is returned.  This may be a
12336 null pointer if the statement is null, for example if it was actually
12337 a subroutine definition (which has compile-time side effects).  If not
12338 null, it will be the result of a L</newSTATEOP> call, normally including
12339 a C<nextstate> or equivalent op.
12340
12341 If an error occurs in parsing or compilation, in most cases a valid op
12342 tree (most likely null) is returned anyway.  The error is reflected in
12343 the parser state, normally resulting in a single exception at the top
12344 level of parsing which covers all the compilation errors that occurred.
12345 Some compilation errors, however, will throw an exception immediately.
12346
12347 The I<flags> parameter is reserved for future use, and must always
12348 be zero.
12349
12350 =cut
12351 */
12352
12353 OP *
12354 Perl_parse_fullstmt(pTHX_ U32 flags)
12355 {
12356     if (flags)
12357         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_fullstmt");
12358     return parse_recdescent_for_op(GRAMFULLSTMT, LEX_FAKEEOF_NEVER);
12359 }
12360
12361 /*
12362 =for apidoc Amx|OP *|parse_stmtseq|U32 flags
12363
12364 Parse a sequence of zero or more Perl statements.  These may be normal
12365 imperative statements, including optional labels, or declarations
12366 that have compile-time effect, or any mixture thereof.  The statement
12367 sequence ends when a closing brace or end-of-file is encountered in a
12368 place where a new statement could have validly started.  It is up to
12369 the caller to ensure that the dynamic parser state (L</PL_parser> et al)
12370 is correctly set to reflect the source of the code to be parsed and the
12371 lexical context for the statements.
12372
12373 The op tree representing the statement sequence is returned.  This may
12374 be a null pointer if the statements were all null, for example if there
12375 were no statements or if there were only subroutine definitions (which
12376 have compile-time side effects).  If not null, it will be a C<lineseq>
12377 list, normally including C<nextstate> or equivalent ops.
12378
12379 If an error occurs in parsing or compilation, in most cases a valid op
12380 tree is returned anyway.  The error is reflected in the parser state,
12381 normally resulting in a single exception at the top level of parsing
12382 which covers all the compilation errors that occurred.  Some compilation
12383 errors, however, will throw an exception immediately.
12384
12385 The I<flags> parameter is reserved for future use, and must always
12386 be zero.
12387
12388 =cut
12389 */
12390
12391 OP *
12392 Perl_parse_stmtseq(pTHX_ U32 flags)
12393 {
12394     OP *stmtseqop;
12395     I32 c;
12396     if (flags)
12397         Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_stmtseq");
12398     stmtseqop = parse_recdescent_for_op(GRAMSTMTSEQ, LEX_FAKEEOF_CLOSING);
12399     c = lex_peek_unichar(0);
12400     if (c != -1 && c != /*{*/'}')
12401         qerror(Perl_mess(aTHX_ "Parse error"));
12402     return stmtseqop;
12403 }
12404
12405 /*
12406  * Local variables:
12407  * c-indentation-style: bsd
12408  * c-basic-offset: 4
12409  * indent-tabs-mode: nil
12410  * End:
12411  *
12412  * ex: set ts=8 sts=4 sw=4 et:
12413  */