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