This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #59908] \x, \0, and \N{} not ok in double-quotish when followed by > \x100
[perl5.git] / toke.c
CommitLineData
a0d0e21e 1/* toke.c
a687059c 2 *
1129b882
NC
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
a687059c 5 *
d48672a2
LW
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.
378cc40b 8 *
a0d0e21e
LW
9 */
10
11/*
4ac71550
TC
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"]
378cc40b
LW
15 */
16
9cbb5ea2
GS
17/*
18 * This file is the lexer for Perl. It's closely linked to the
4e553d73 19 * parser, perly.y.
ffb4593c
NT
20 *
21 * The main routine is yylex(), which returns the next token.
22 */
23
378cc40b 24#include "EXTERN.h"
864dbfa3 25#define PERL_IN_TOKE_C
378cc40b 26#include "perl.h"
378cc40b 27
eb0d8d16
NC
28#define new_constant(a,b,c,d,e,f,g) \
29 S_new_constant(aTHX_ a,b,STR_WITH_LEN(c),d,e,f, g)
30
6154021b 31#define pl_yylval (PL_parser->yylval)
d3b6f988 32
acdf0a21
DM
33/* YYINITDEPTH -- initial size of the parser's stacks. */
34#define YYINITDEPTH 200
35
199e78b7
DM
36/* XXX temporary backwards compatibility */
37#define PL_lex_brackets (PL_parser->lex_brackets)
38#define PL_lex_brackstack (PL_parser->lex_brackstack)
39#define PL_lex_casemods (PL_parser->lex_casemods)
40#define PL_lex_casestack (PL_parser->lex_casestack)
41#define PL_lex_defer (PL_parser->lex_defer)
42#define PL_lex_dojoin (PL_parser->lex_dojoin)
43#define PL_lex_expect (PL_parser->lex_expect)
44#define PL_lex_formbrack (PL_parser->lex_formbrack)
45#define PL_lex_inpat (PL_parser->lex_inpat)
46#define PL_lex_inwhat (PL_parser->lex_inwhat)
47#define PL_lex_op (PL_parser->lex_op)
48#define PL_lex_repl (PL_parser->lex_repl)
49#define PL_lex_starts (PL_parser->lex_starts)
50#define PL_lex_stuff (PL_parser->lex_stuff)
51#define PL_multi_start (PL_parser->multi_start)
52#define PL_multi_open (PL_parser->multi_open)
53#define PL_multi_close (PL_parser->multi_close)
54#define PL_pending_ident (PL_parser->pending_ident)
55#define PL_preambled (PL_parser->preambled)
56#define PL_sublex_info (PL_parser->sublex_info)
bdc0bf6f 57#define PL_linestr (PL_parser->linestr)
c2598295
DM
58#define PL_expect (PL_parser->expect)
59#define PL_copline (PL_parser->copline)
f06b5848
DM
60#define PL_bufptr (PL_parser->bufptr)
61#define PL_oldbufptr (PL_parser->oldbufptr)
62#define PL_oldoldbufptr (PL_parser->oldoldbufptr)
63#define PL_linestart (PL_parser->linestart)
64#define PL_bufend (PL_parser->bufend)
65#define PL_last_uni (PL_parser->last_uni)
66#define PL_last_lop (PL_parser->last_lop)
67#define PL_last_lop_op (PL_parser->last_lop_op)
bc177e6b 68#define PL_lex_state (PL_parser->lex_state)
2f9285f8 69#define PL_rsfp (PL_parser->rsfp)
5486870f 70#define PL_rsfp_filters (PL_parser->rsfp_filters)
12bd6ede
DM
71#define PL_in_my (PL_parser->in_my)
72#define PL_in_my_stash (PL_parser->in_my_stash)
14047fc9 73#define PL_tokenbuf (PL_parser->tokenbuf)
670a9cb2 74#define PL_multi_end (PL_parser->multi_end)
13765c85 75#define PL_error_count (PL_parser->error_count)
199e78b7
DM
76
77#ifdef PERL_MAD
78# define PL_endwhite (PL_parser->endwhite)
79# define PL_faketokens (PL_parser->faketokens)
80# define PL_lasttoke (PL_parser->lasttoke)
81# define PL_nextwhite (PL_parser->nextwhite)
82# define PL_realtokenstart (PL_parser->realtokenstart)
83# define PL_skipwhite (PL_parser->skipwhite)
84# define PL_thisclose (PL_parser->thisclose)
85# define PL_thismad (PL_parser->thismad)
86# define PL_thisopen (PL_parser->thisopen)
87# define PL_thisstuff (PL_parser->thisstuff)
88# define PL_thistoken (PL_parser->thistoken)
89# define PL_thiswhite (PL_parser->thiswhite)
fb205e7a
DM
90# define PL_thiswhite (PL_parser->thiswhite)
91# define PL_nexttoke (PL_parser->nexttoke)
92# define PL_curforce (PL_parser->curforce)
93#else
94# define PL_nexttoke (PL_parser->nexttoke)
95# define PL_nexttype (PL_parser->nexttype)
96# define PL_nextval (PL_parser->nextval)
199e78b7
DM
97#endif
98
3cbf51f5
DM
99static int
100S_pending_ident(pTHX);
199e78b7 101
0bd48802 102static const char ident_too_long[] = "Identifier too long";
c445ea15 103static const char commaless_variable_list[] = "comma-less variable list";
8903cb82 104
6e3aabd6 105#ifndef PERL_NO_UTF16_FILTER
acfe0abc
GS
106static I32 utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen);
107static I32 utf16rev_textfilter(pTHX_ int idx, SV *sv, int maxlen);
6e3aabd6 108#endif
51371543 109
29595ff2 110#ifdef PERL_MAD
29595ff2 111# define CURMAD(slot,sv) if (PL_madskills) { curmad(slot,sv); sv = 0; }
cd81e915 112# define NEXTVAL_NEXTTOKE PL_nexttoke[PL_curforce].next_val
9ded7720 113#else
5db06880 114# define CURMAD(slot,sv)
9ded7720 115# define NEXTVAL_NEXTTOKE PL_nextval[PL_nexttoke]
29595ff2
NC
116#endif
117
9059aa12
LW
118#define XFAKEBRACK 128
119#define XENUMMASK 127
120
39e02b42
JH
121#ifdef USE_UTF8_SCRIPTS
122# define UTF (!IN_BYTES)
2b9d42f0 123#else
746b446a 124# define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8))
2b9d42f0 125#endif
a0ed51b3 126
61f0cdd9 127/* In variables named $^X, these are the legal values for X.
2b92dfce
GS
128 * 1999-02-27 mjd-perl-patch@plover.com */
129#define isCONTROLVAR(x) (isUPPER(x) || strchr("[\\]^_?", (x)))
130
bf4acbe4
GS
131/* On MacOS, respect nonbreaking spaces */
132#ifdef MACOS_TRADITIONAL
133#define SPACE_OR_TAB(c) ((c)==' '||(c)=='\312'||(c)=='\t')
134#else
135#define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t')
136#endif
137
ffb4593c
NT
138/* LEX_* are values for PL_lex_state, the state of the lexer.
139 * They are arranged oddly so that the guard on the switch statement
79072805
LW
140 * can get by with a single comparison (if the compiler is smart enough).
141 */
142
fb73857a
PP
143/* #define LEX_NOTPARSING 11 is done in perl.h. */
144
b6007c36
DM
145#define LEX_NORMAL 10 /* normal code (ie not within "...") */
146#define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */
147#define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */
148#define LEX_INTERPPUSH 7 /* starting a new sublex parse level */
149#define LEX_INTERPSTART 6 /* expecting the start of a $var */
150
151 /* at end of code, eg "$x" followed by: */
152#define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */
153#define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */
154
155#define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of
156 string or after \E, $foo, etc */
157#define LEX_INTERPCONST 2 /* NOT USED */
158#define LEX_FORMLINE 1 /* expecting a format line */
159#define LEX_KNOWNEXT 0 /* next token known; just return it */
160
79072805 161
bbf60fe6 162#ifdef DEBUGGING
27da23d5 163static const char* const lex_state_names[] = {
bbf60fe6
DM
164 "KNOWNEXT",
165 "FORMLINE",
166 "INTERPCONST",
167 "INTERPCONCAT",
168 "INTERPENDMAYBE",
169 "INTERPEND",
170 "INTERPSTART",
171 "INTERPPUSH",
172 "INTERPCASEMOD",
173 "INTERPNORMAL",
174 "NORMAL"
175};
176#endif
177
79072805
LW
178#ifdef ff_next
179#undef ff_next
d48672a2
LW
180#endif
181
79072805 182#include "keywords.h"
fe14fcc3 183
ffb4593c
NT
184/* CLINE is a macro that ensures PL_copline has a sane value */
185
ae986130
LW
186#ifdef CLINE
187#undef CLINE
188#endif
57843af0 189#define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline))
3280af22 190
5db06880 191#ifdef PERL_MAD
29595ff2
NC
192# define SKIPSPACE0(s) skipspace0(s)
193# define SKIPSPACE1(s) skipspace1(s)
194# define SKIPSPACE2(s,tsv) skipspace2(s,&tsv)
195# define PEEKSPACE(s) skipspace2(s,0)
196#else
197# define SKIPSPACE0(s) skipspace(s)
198# define SKIPSPACE1(s) skipspace(s)
199# define SKIPSPACE2(s,tsv) skipspace(s)
200# define PEEKSPACE(s) skipspace(s)
201#endif
202
ffb4593c
NT
203/*
204 * Convenience functions to return different tokens and prime the
9cbb5ea2 205 * lexer for the next token. They all take an argument.
ffb4593c
NT
206 *
207 * TOKEN : generic token (used for '(', DOLSHARP, etc)
208 * OPERATOR : generic operator
209 * AOPERATOR : assignment operator
210 * PREBLOCK : beginning the block after an if, while, foreach, ...
211 * PRETERMBLOCK : beginning a non-code-defining {} block (eg, hash ref)
212 * PREREF : *EXPR where EXPR is not a simple identifier
213 * TERM : expression term
214 * LOOPX : loop exiting command (goto, last, dump, etc)
215 * FTST : file test operator
216 * FUN0 : zero-argument function
2d2e263d 217 * FUN1 : not used, except for not, which isn't a UNIOP
ffb4593c
NT
218 * BOop : bitwise or or xor
219 * BAop : bitwise and
220 * SHop : shift operator
221 * PWop : power operator
9cbb5ea2 222 * PMop : pattern-matching operator
ffb4593c
NT
223 * Aop : addition-level operator
224 * Mop : multiplication-level operator
225 * Eop : equality-testing operator
e5edeb50 226 * Rop : relational operator <= != gt
ffb4593c
NT
227 *
228 * Also see LOP and lop() below.
229 */
230
998054bd 231#ifdef DEBUGGING /* Serve -DT. */
704d4215 232# define REPORT(retval) tokereport((I32)retval, &pl_yylval)
998054bd 233#else
bbf60fe6 234# define REPORT(retval) (retval)
998054bd
SC
235#endif
236
bbf60fe6
DM
237#define TOKEN(retval) return ( PL_bufptr = s, REPORT(retval))
238#define OPERATOR(retval) return (PL_expect = XTERM, PL_bufptr = s, REPORT(retval))
239#define AOPERATOR(retval) return ao((PL_expect = XTERM, PL_bufptr = s, REPORT(retval)))
240#define PREBLOCK(retval) return (PL_expect = XBLOCK,PL_bufptr = s, REPORT(retval))
241#define PRETERMBLOCK(retval) return (PL_expect = XTERMBLOCK,PL_bufptr = s, REPORT(retval))
242#define PREREF(retval) return (PL_expect = XREF,PL_bufptr = s, REPORT(retval))
243#define TERM(retval) return (CLINE, PL_expect = XOPERATOR, PL_bufptr = s, REPORT(retval))
6154021b
RGS
244#define LOOPX(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)LOOPEX))
245#define FTST(f) return (pl_yylval.ival=f, PL_expect=XTERMORDORDOR, PL_bufptr=s, REPORT((int)UNIOP))
246#define FUN0(f) return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0))
247#define FUN1(f) return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC1))
248#define BOop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITOROP)))
249#define BAop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITANDOP)))
250#define SHop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)SHIFTOP)))
251#define PWop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)POWOP)))
252#define PMop(f) return(pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MATCHOP))
253#define Aop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)ADDOP)))
254#define Mop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MULOP)))
255#define Eop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)EQOP))
256#define Rop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)RELOP))
2f3197b3 257
a687059c
LW
258/* This bit of chicanery makes a unary function followed by
259 * a parenthesis into a function with one argument, highest precedence.
6f33ba73
RGS
260 * The UNIDOR macro is for unary functions that can be followed by the //
261 * operator (such as C<shift // 0>).
a687059c 262 */
376fcdbf 263#define UNI2(f,x) { \
6154021b 264 pl_yylval.ival = f; \
376fcdbf
AL
265 PL_expect = x; \
266 PL_bufptr = s; \
267 PL_last_uni = PL_oldbufptr; \
268 PL_last_lop_op = f; \
269 if (*s == '(') \
270 return REPORT( (int)FUNC1 ); \
29595ff2 271 s = PEEKSPACE(s); \
376fcdbf
AL
272 return REPORT( *s=='(' ? (int)FUNC1 : (int)UNIOP ); \
273 }
6f33ba73
RGS
274#define UNI(f) UNI2(f,XTERM)
275#define UNIDOR(f) UNI2(f,XTERMORDORDOR)
a687059c 276
376fcdbf 277#define UNIBRACK(f) { \
6154021b 278 pl_yylval.ival = f; \
376fcdbf
AL
279 PL_bufptr = s; \
280 PL_last_uni = PL_oldbufptr; \
281 if (*s == '(') \
282 return REPORT( (int)FUNC1 ); \
29595ff2 283 s = PEEKSPACE(s); \
376fcdbf
AL
284 return REPORT( (*s == '(') ? (int)FUNC1 : (int)UNIOP ); \
285 }
79072805 286
9f68db38 287/* grandfather return to old style */
6154021b 288#define OLDLOP(f) return(pl_yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)LSTOP)
79072805 289
8fa7f367
JH
290#ifdef DEBUGGING
291
6154021b 292/* how to interpret the pl_yylval associated with the token */
bbf60fe6
DM
293enum token_type {
294 TOKENTYPE_NONE,
295 TOKENTYPE_IVAL,
6154021b 296 TOKENTYPE_OPNUM, /* pl_yylval.ival contains an opcode number */
bbf60fe6
DM
297 TOKENTYPE_PVAL,
298 TOKENTYPE_OPVAL,
299 TOKENTYPE_GVVAL
300};
301
6d4a66ac
NC
302static struct debug_tokens {
303 const int token;
304 enum token_type type;
305 const char *name;
306} const debug_tokens[] =
9041c2e3 307{
bbf60fe6
DM
308 { ADDOP, TOKENTYPE_OPNUM, "ADDOP" },
309 { ANDAND, TOKENTYPE_NONE, "ANDAND" },
310 { ANDOP, TOKENTYPE_NONE, "ANDOP" },
311 { ANONSUB, TOKENTYPE_IVAL, "ANONSUB" },
312 { ARROW, TOKENTYPE_NONE, "ARROW" },
313 { ASSIGNOP, TOKENTYPE_OPNUM, "ASSIGNOP" },
314 { BITANDOP, TOKENTYPE_OPNUM, "BITANDOP" },
315 { BITOROP, TOKENTYPE_OPNUM, "BITOROP" },
316 { COLONATTR, TOKENTYPE_NONE, "COLONATTR" },
317 { CONTINUE, TOKENTYPE_NONE, "CONTINUE" },
0d863452 318 { DEFAULT, TOKENTYPE_NONE, "DEFAULT" },
bbf60fe6
DM
319 { DO, TOKENTYPE_NONE, "DO" },
320 { DOLSHARP, TOKENTYPE_NONE, "DOLSHARP" },
321 { DORDOR, TOKENTYPE_NONE, "DORDOR" },
322 { DOROP, TOKENTYPE_OPNUM, "DOROP" },
323 { DOTDOT, TOKENTYPE_IVAL, "DOTDOT" },
324 { ELSE, TOKENTYPE_NONE, "ELSE" },
325 { ELSIF, TOKENTYPE_IVAL, "ELSIF" },
326 { EQOP, TOKENTYPE_OPNUM, "EQOP" },
327 { FOR, TOKENTYPE_IVAL, "FOR" },
328 { FORMAT, TOKENTYPE_NONE, "FORMAT" },
329 { FUNC, TOKENTYPE_OPNUM, "FUNC" },
330 { FUNC0, TOKENTYPE_OPNUM, "FUNC0" },
331 { FUNC0SUB, TOKENTYPE_OPVAL, "FUNC0SUB" },
332 { FUNC1, TOKENTYPE_OPNUM, "FUNC1" },
333 { FUNCMETH, TOKENTYPE_OPVAL, "FUNCMETH" },
0d863452 334 { GIVEN, TOKENTYPE_IVAL, "GIVEN" },
bbf60fe6
DM
335 { HASHBRACK, TOKENTYPE_NONE, "HASHBRACK" },
336 { IF, TOKENTYPE_IVAL, "IF" },
337 { LABEL, TOKENTYPE_PVAL, "LABEL" },
338 { LOCAL, TOKENTYPE_IVAL, "LOCAL" },
339 { LOOPEX, TOKENTYPE_OPNUM, "LOOPEX" },
340 { LSTOP, TOKENTYPE_OPNUM, "LSTOP" },
341 { LSTOPSUB, TOKENTYPE_OPVAL, "LSTOPSUB" },
342 { MATCHOP, TOKENTYPE_OPNUM, "MATCHOP" },
343 { METHOD, TOKENTYPE_OPVAL, "METHOD" },
344 { MULOP, TOKENTYPE_OPNUM, "MULOP" },
345 { MY, TOKENTYPE_IVAL, "MY" },
346 { MYSUB, TOKENTYPE_NONE, "MYSUB" },
347 { NOAMP, TOKENTYPE_NONE, "NOAMP" },
348 { NOTOP, TOKENTYPE_NONE, "NOTOP" },
349 { OROP, TOKENTYPE_IVAL, "OROP" },
350 { OROR, TOKENTYPE_NONE, "OROR" },
351 { PACKAGE, TOKENTYPE_NONE, "PACKAGE" },
352 { PMFUNC, TOKENTYPE_OPVAL, "PMFUNC" },
353 { POSTDEC, TOKENTYPE_NONE, "POSTDEC" },
354 { POSTINC, TOKENTYPE_NONE, "POSTINC" },
355 { POWOP, TOKENTYPE_OPNUM, "POWOP" },
356 { PREDEC, TOKENTYPE_NONE, "PREDEC" },
357 { PREINC, TOKENTYPE_NONE, "PREINC" },
358 { PRIVATEREF, TOKENTYPE_OPVAL, "PRIVATEREF" },
359 { REFGEN, TOKENTYPE_NONE, "REFGEN" },
360 { RELOP, TOKENTYPE_OPNUM, "RELOP" },
361 { SHIFTOP, TOKENTYPE_OPNUM, "SHIFTOP" },
362 { SUB, TOKENTYPE_NONE, "SUB" },
363 { THING, TOKENTYPE_OPVAL, "THING" },
364 { UMINUS, TOKENTYPE_NONE, "UMINUS" },
365 { UNIOP, TOKENTYPE_OPNUM, "UNIOP" },
366 { UNIOPSUB, TOKENTYPE_OPVAL, "UNIOPSUB" },
367 { UNLESS, TOKENTYPE_IVAL, "UNLESS" },
368 { UNTIL, TOKENTYPE_IVAL, "UNTIL" },
369 { USE, TOKENTYPE_IVAL, "USE" },
0d863452 370 { WHEN, TOKENTYPE_IVAL, "WHEN" },
bbf60fe6
DM
371 { WHILE, TOKENTYPE_IVAL, "WHILE" },
372 { WORD, TOKENTYPE_OPVAL, "WORD" },
be25f609 373 { YADAYADA, TOKENTYPE_IVAL, "YADAYADA" },
c35e046a 374 { 0, TOKENTYPE_NONE, NULL }
bbf60fe6
DM
375};
376
6154021b 377/* dump the returned token in rv, plus any optional arg in pl_yylval */
998054bd 378
bbf60fe6 379STATIC int
704d4215 380S_tokereport(pTHX_ I32 rv, const YYSTYPE* lvalp)
bbf60fe6 381{
97aff369 382 dVAR;
7918f24d
NC
383
384 PERL_ARGS_ASSERT_TOKEREPORT;
385
bbf60fe6 386 if (DEBUG_T_TEST) {
bd61b366 387 const char *name = NULL;
bbf60fe6 388 enum token_type type = TOKENTYPE_NONE;
f54cb97a 389 const struct debug_tokens *p;
396482e1 390 SV* const report = newSVpvs("<== ");
bbf60fe6 391
f54cb97a 392 for (p = debug_tokens; p->token; p++) {
bbf60fe6
DM
393 if (p->token == (int)rv) {
394 name = p->name;
395 type = p->type;
396 break;
397 }
398 }
399 if (name)
54667de8 400 Perl_sv_catpv(aTHX_ report, name);
bbf60fe6
DM
401 else if ((char)rv > ' ' && (char)rv < '~')
402 Perl_sv_catpvf(aTHX_ report, "'%c'", (char)rv);
403 else if (!rv)
396482e1 404 sv_catpvs(report, "EOF");
bbf60fe6
DM
405 else
406 Perl_sv_catpvf(aTHX_ report, "?? %"IVdf, (IV)rv);
407 switch (type) {
408 case TOKENTYPE_NONE:
409 case TOKENTYPE_GVVAL: /* doesn't appear to be used */
410 break;
411 case TOKENTYPE_IVAL:
704d4215 412 Perl_sv_catpvf(aTHX_ report, "(ival=%"IVdf")", (IV)lvalp->ival);
bbf60fe6
DM
413 break;
414 case TOKENTYPE_OPNUM:
415 Perl_sv_catpvf(aTHX_ report, "(ival=op_%s)",
704d4215 416 PL_op_name[lvalp->ival]);
bbf60fe6
DM
417 break;
418 case TOKENTYPE_PVAL:
704d4215 419 Perl_sv_catpvf(aTHX_ report, "(pval=\"%s\")", lvalp->pval);
bbf60fe6
DM
420 break;
421 case TOKENTYPE_OPVAL:
704d4215 422 if (lvalp->opval) {
401441c0 423 Perl_sv_catpvf(aTHX_ report, "(opval=op_%s)",
704d4215
GG
424 PL_op_name[lvalp->opval->op_type]);
425 if (lvalp->opval->op_type == OP_CONST) {
b6007c36 426 Perl_sv_catpvf(aTHX_ report, " %s",
704d4215 427 SvPEEK(cSVOPx_sv(lvalp->opval)));
b6007c36
DM
428 }
429
430 }
401441c0 431 else
396482e1 432 sv_catpvs(report, "(opval=null)");
bbf60fe6
DM
433 break;
434 }
b6007c36 435 PerlIO_printf(Perl_debug_log, "### %s\n\n", SvPV_nolen_const(report));
bbf60fe6
DM
436 };
437 return (int)rv;
998054bd
SC
438}
439
b6007c36
DM
440
441/* print the buffer with suitable escapes */
442
443STATIC void
15f169a1 444S_printbuf(pTHX_ const char *const fmt, const char *const s)
b6007c36 445{
396482e1 446 SV* const tmp = newSVpvs("");
7918f24d
NC
447
448 PERL_ARGS_ASSERT_PRINTBUF;
449
b6007c36
DM
450 PerlIO_printf(Perl_debug_log, fmt, pv_display(tmp, s, strlen(s), 0, 60));
451 SvREFCNT_dec(tmp);
452}
453
8fa7f367
JH
454#endif
455
ffb4593c
NT
456/*
457 * S_ao
458 *
c963b151
BD
459 * This subroutine detects &&=, ||=, and //= and turns an ANDAND, OROR or DORDOR
460 * into an OP_ANDASSIGN, OP_ORASSIGN, or OP_DORASSIGN
ffb4593c
NT
461 */
462
76e3520e 463STATIC int
cea2e8a9 464S_ao(pTHX_ int toketype)
a0d0e21e 465{
97aff369 466 dVAR;
3280af22
NIS
467 if (*PL_bufptr == '=') {
468 PL_bufptr++;
a0d0e21e 469 if (toketype == ANDAND)
6154021b 470 pl_yylval.ival = OP_ANDASSIGN;
a0d0e21e 471 else if (toketype == OROR)
6154021b 472 pl_yylval.ival = OP_ORASSIGN;
c963b151 473 else if (toketype == DORDOR)
6154021b 474 pl_yylval.ival = OP_DORASSIGN;
a0d0e21e
LW
475 toketype = ASSIGNOP;
476 }
477 return toketype;
478}
479
ffb4593c
NT
480/*
481 * S_no_op
482 * When Perl expects an operator and finds something else, no_op
483 * prints the warning. It always prints "<something> found where
484 * operator expected. It prints "Missing semicolon on previous line?"
485 * if the surprise occurs at the start of the line. "do you need to
486 * predeclare ..." is printed out for code like "sub bar; foo bar $x"
487 * where the compiler doesn't know if foo is a method call or a function.
488 * It prints "Missing operator before end of line" if there's nothing
489 * after the missing operator, or "... before <...>" if there is something
490 * after the missing operator.
491 */
492
76e3520e 493STATIC void
15f169a1 494S_no_op(pTHX_ const char *const what, char *s)
463ee0b2 495{
97aff369 496 dVAR;
9d4ba2ae
AL
497 char * const oldbp = PL_bufptr;
498 const bool is_first = (PL_oldbufptr == PL_linestart);
68dc0745 499
7918f24d
NC
500 PERL_ARGS_ASSERT_NO_OP;
501
1189a94a
GS
502 if (!s)
503 s = oldbp;
07c798fb 504 else
1189a94a 505 PL_bufptr = s;
cea2e8a9 506 yywarn(Perl_form(aTHX_ "%s found where operator expected", what));
56da5a46
RGS
507 if (ckWARN_d(WARN_SYNTAX)) {
508 if (is_first)
509 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
510 "\t(Missing semicolon on previous line?)\n");
511 else if (PL_oldoldbufptr && isIDFIRST_lazy_if(PL_oldoldbufptr,UTF)) {
f54cb97a 512 const char *t;
c35e046a
AL
513 for (t = PL_oldoldbufptr; (isALNUM_lazy_if(t,UTF) || *t == ':'); t++)
514 NOOP;
56da5a46
RGS
515 if (t < PL_bufptr && isSPACE(*t))
516 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
517 "\t(Do you need to predeclare %.*s?)\n",
551405c4 518 (int)(t - PL_oldoldbufptr), PL_oldoldbufptr);
56da5a46
RGS
519 }
520 else {
521 assert(s >= oldbp);
522 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
551405c4 523 "\t(Missing operator before %.*s?)\n", (int)(s - oldbp), oldbp);
56da5a46 524 }
07c798fb 525 }
3280af22 526 PL_bufptr = oldbp;
8990e307
LW
527}
528
ffb4593c
NT
529/*
530 * S_missingterm
531 * Complain about missing quote/regexp/heredoc terminator.
d4c19fe8 532 * If it's called with NULL then it cauterizes the line buffer.
ffb4593c
NT
533 * If we're in a delimited string and the delimiter is a control
534 * character, it's reformatted into a two-char sequence like ^C.
535 * This is fatal.
536 */
537
76e3520e 538STATIC void
cea2e8a9 539S_missingterm(pTHX_ char *s)
8990e307 540{
97aff369 541 dVAR;
8990e307
LW
542 char tmpbuf[3];
543 char q;
544 if (s) {
9d4ba2ae 545 char * const nl = strrchr(s,'\n');
d2719217 546 if (nl)
8990e307
LW
547 *nl = '\0';
548 }
463559e7 549 else if (isCNTRL(PL_multi_close)) {
8990e307 550 *tmpbuf = '^';
585ec06d 551 tmpbuf[1] = (char)toCTRL(PL_multi_close);
8990e307
LW
552 tmpbuf[2] = '\0';
553 s = tmpbuf;
554 }
555 else {
eb160463 556 *tmpbuf = (char)PL_multi_close;
8990e307
LW
557 tmpbuf[1] = '\0';
558 s = tmpbuf;
559 }
560 q = strchr(s,'"') ? '\'' : '"';
cea2e8a9 561 Perl_croak(aTHX_ "Can't find string terminator %c%s%c anywhere before EOF",q,s,q);
463ee0b2 562}
79072805 563
ef89dcc3 564#define FEATURE_IS_ENABLED(name) \
0d863452 565 ((0 != (PL_hints & HINT_LOCALIZE_HH)) \
89529cee 566 && S_feature_is_enabled(aTHX_ STR_WITH_LEN(name)))
4a731d7b
NC
567/* The longest string we pass in. */
568#define MAX_FEATURE_LEN (sizeof("switch")-1)
569
0d863452
RH
570/*
571 * S_feature_is_enabled
572 * Check whether the named feature is enabled.
573 */
574STATIC bool
15f169a1 575S_feature_is_enabled(pTHX_ const char *const name, STRLEN namelen)
0d863452 576{
97aff369 577 dVAR;
0d863452 578 HV * const hinthv = GvHV(PL_hintgv);
4a731d7b 579 char he_name[8 + MAX_FEATURE_LEN] = "feature_";
7918f24d
NC
580
581 PERL_ARGS_ASSERT_FEATURE_IS_ENABLED;
582
4a731d7b
NC
583 assert(namelen <= MAX_FEATURE_LEN);
584 memcpy(&he_name[8], name, namelen);
d4c19fe8 585
7b9ef140 586 return (hinthv && hv_exists(hinthv, he_name, 8 + namelen));
0d863452
RH
587}
588
ffb4593c
NT
589/*
590 * Perl_deprecate
ffb4593c
NT
591 */
592
79072805 593void
15f169a1 594Perl_deprecate(pTHX_ const char *const s)
a0d0e21e 595{
7918f24d
NC
596 PERL_ARGS_ASSERT_DEPRECATE;
597
599cee73 598 if (ckWARN(WARN_DEPRECATED))
9014280d 599 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED), "Use of %s is deprecated", s);
a0d0e21e
LW
600}
601
12bcd1a6 602void
15f169a1 603Perl_deprecate_old(pTHX_ const char *const s)
12bcd1a6
PM
604{
605 /* This function should NOT be called for any new deprecated warnings */
606 /* Use Perl_deprecate instead */
607 /* */
608 /* It is here to maintain backward compatibility with the pre-5.8 */
609 /* warnings category hierarchy. The "deprecated" category used to */
610 /* live under the "syntax" category. It is now a top-level category */
611 /* in its own right. */
612
7918f24d
NC
613 PERL_ARGS_ASSERT_DEPRECATE_OLD;
614
12bcd1a6 615 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
bfed75c6 616 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
12bcd1a6
PM
617 "Use of %s is deprecated", s);
618}
619
ffb4593c 620/*
9cbb5ea2
GS
621 * experimental text filters for win32 carriage-returns, utf16-to-utf8 and
622 * utf16-to-utf8-reversed.
ffb4593c
NT
623 */
624
c39cd008
GS
625#ifdef PERL_CR_FILTER
626static void
627strip_return(SV *sv)
628{
95a20fc0 629 register const char *s = SvPVX_const(sv);
9d4ba2ae 630 register const char * const e = s + SvCUR(sv);
7918f24d
NC
631
632 PERL_ARGS_ASSERT_STRIP_RETURN;
633
c39cd008
GS
634 /* outer loop optimized to do nothing if there are no CR-LFs */
635 while (s < e) {
636 if (*s++ == '\r' && *s == '\n') {
637 /* hit a CR-LF, need to copy the rest */
638 register char *d = s - 1;
639 *d++ = *s++;
640 while (s < e) {
641 if (*s == '\r' && s[1] == '\n')
642 s++;
643 *d++ = *s++;
644 }
645 SvCUR(sv) -= s - d;
646 return;
647 }
648 }
649}
a868473f 650
76e3520e 651STATIC I32
c39cd008 652S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
a868473f 653{
f54cb97a 654 const I32 count = FILTER_READ(idx+1, sv, maxlen);
c39cd008
GS
655 if (count > 0 && !maxlen)
656 strip_return(sv);
657 return count;
a868473f
NIS
658}
659#endif
660
199e78b7
DM
661
662
ffb4593c
NT
663/*
664 * Perl_lex_start
5486870f 665 *
e3abe207 666 * Create a parser object and initialise its parser and lexer fields
5486870f
DM
667 *
668 * rsfp is the opened file handle to read from (if any),
669 *
670 * line holds any initial content already read from the file (or in
671 * the case of no file, such as an eval, the whole contents);
672 *
673 * new_filter indicates that this is a new file and it shouldn't inherit
674 * the filters from the current parser (ie require).
ffb4593c
NT
675 */
676
a0d0e21e 677void
5486870f 678Perl_lex_start(pTHX_ SV *line, PerlIO *rsfp, bool new_filter)
79072805 679{
97aff369 680 dVAR;
6ef55633 681 const char *s = NULL;
8990e307 682 STRLEN len;
5486870f 683 yy_parser *parser, *oparser;
acdf0a21
DM
684
685 /* create and initialise a parser */
686
199e78b7 687 Newxz(parser, 1, yy_parser);
5486870f 688 parser->old_parser = oparser = PL_parser;
acdf0a21
DM
689 PL_parser = parser;
690
691 Newx(parser->stack, YYINITDEPTH, yy_stack_frame);
692 parser->ps = parser->stack;
693 parser->stack_size = YYINITDEPTH;
694
695 parser->stack->state = 0;
696 parser->yyerrstatus = 0;
697 parser->yychar = YYEMPTY; /* Cause a token to be read. */
698
e3abe207
DM
699 /* on scope exit, free this parser and restore any outer one */
700 SAVEPARSER(parser);
7c4baf47 701 parser->saved_curcop = PL_curcop;
e3abe207 702
acdf0a21 703 /* initialise lexer state */
8990e307 704
fb205e7a
DM
705#ifdef PERL_MAD
706 parser->curforce = -1;
707#else
708 parser->nexttoke = 0;
709#endif
ca4cfd28 710 parser->error_count = oparser ? oparser->error_count : 0;
c2598295 711 parser->copline = NOLINE;
5afb0a62 712 parser->lex_state = LEX_NORMAL;
c2598295 713 parser->expect = XSTATE;
2f9285f8 714 parser->rsfp = rsfp;
56b27c9a 715 parser->rsfp_filters = (new_filter || !oparser) ? newAV()
502c6561 716 : MUTABLE_AV(SvREFCNT_inc(oparser->rsfp_filters));
2f9285f8 717
199e78b7
DM
718 Newx(parser->lex_brackstack, 120, char);
719 Newx(parser->lex_casestack, 12, char);
720 *parser->lex_casestack = '\0';
02b34bbe 721
10efb74f
NC
722 if (line) {
723 s = SvPV_const(line, len);
724 } else {
725 len = 0;
726 }
bdc0bf6f 727
10efb74f 728 if (!len) {
bdc0bf6f 729 parser->linestr = newSVpvs("\n;");
10efb74f 730 } else if (SvREADONLY(line) || s[len-1] != ';') {
bdc0bf6f 731 parser->linestr = newSVsv(line);
10efb74f 732 if (s[len-1] != ';')
bdc0bf6f 733 sv_catpvs(parser->linestr, "\n;");
6c5ce11d
NC
734 } else {
735 SvTEMP_off(line);
736 SvREFCNT_inc_simple_void_NN(line);
bdc0bf6f 737 parser->linestr = line;
8990e307 738 }
f06b5848
DM
739 parser->oldoldbufptr =
740 parser->oldbufptr =
741 parser->bufptr =
742 parser->linestart = SvPVX(parser->linestr);
743 parser->bufend = parser->bufptr + SvCUR(parser->linestr);
744 parser->last_lop = parser->last_uni = NULL;
79072805 745}
a687059c 746
e3abe207
DM
747
748/* delete a parser object */
749
750void
751Perl_parser_free(pTHX_ const yy_parser *parser)
752{
7918f24d
NC
753 PERL_ARGS_ASSERT_PARSER_FREE;
754
7c4baf47 755 PL_curcop = parser->saved_curcop;
bdc0bf6f
DM
756 SvREFCNT_dec(parser->linestr);
757
2f9285f8
DM
758 if (parser->rsfp == PerlIO_stdin())
759 PerlIO_clearerr(parser->rsfp);
799361c3
SH
760 else if (parser->rsfp && (!parser->old_parser ||
761 (parser->old_parser && parser->rsfp != parser->old_parser->rsfp)))
2f9285f8 762 PerlIO_close(parser->rsfp);
5486870f 763 SvREFCNT_dec(parser->rsfp_filters);
2f9285f8 764
e3abe207
DM
765 Safefree(parser->stack);
766 Safefree(parser->lex_brackstack);
767 Safefree(parser->lex_casestack);
768 PL_parser = parser->old_parser;
769 Safefree(parser);
770}
771
772
ffb4593c
NT
773/*
774 * Perl_lex_end
9cbb5ea2
GS
775 * Finalizer for lexing operations. Must be called when the parser is
776 * done with the lexer.
ffb4593c
NT
777 */
778
463ee0b2 779void
864dbfa3 780Perl_lex_end(pTHX)
463ee0b2 781{
97aff369 782 dVAR;
3280af22 783 PL_doextract = FALSE;
463ee0b2
LW
784}
785
ffb4593c
NT
786/*
787 * S_incline
788 * This subroutine has nothing to do with tilting, whether at windmills
789 * or pinball tables. Its name is short for "increment line". It
57843af0 790 * increments the current line number in CopLINE(PL_curcop) and checks
ffb4593c 791 * to see whether the line starts with a comment of the form
9cbb5ea2
GS
792 * # line 500 "foo.pm"
793 * If so, it sets the current line number and file to the values in the comment.
ffb4593c
NT
794 */
795
76e3520e 796STATIC void
d9095cec 797S_incline(pTHX_ const char *s)
463ee0b2 798{
97aff369 799 dVAR;
d9095cec
NC
800 const char *t;
801 const char *n;
802 const char *e;
463ee0b2 803
7918f24d
NC
804 PERL_ARGS_ASSERT_INCLINE;
805
57843af0 806 CopLINE_inc(PL_curcop);
463ee0b2
LW
807 if (*s++ != '#')
808 return;
d4c19fe8
AL
809 while (SPACE_OR_TAB(*s))
810 s++;
73659bf1
GS
811 if (strnEQ(s, "line", 4))
812 s += 4;
813 else
814 return;
084592ab 815 if (SPACE_OR_TAB(*s))
73659bf1 816 s++;
4e553d73 817 else
73659bf1 818 return;
d4c19fe8
AL
819 while (SPACE_OR_TAB(*s))
820 s++;
463ee0b2
LW
821 if (!isDIGIT(*s))
822 return;
d4c19fe8 823
463ee0b2
LW
824 n = s;
825 while (isDIGIT(*s))
826 s++;
07714eb4 827 if (!SPACE_OR_TAB(*s) && *s != '\r' && *s != '\n' && *s != '\0')
26b6dc3f 828 return;
bf4acbe4 829 while (SPACE_OR_TAB(*s))
463ee0b2 830 s++;
73659bf1 831 if (*s == '"' && (t = strchr(s+1, '"'))) {
463ee0b2 832 s++;
73659bf1
GS
833 e = t + 1;
834 }
463ee0b2 835 else {
c35e046a
AL
836 t = s;
837 while (!isSPACE(*t))
838 t++;
73659bf1 839 e = t;
463ee0b2 840 }
bf4acbe4 841 while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
73659bf1
GS
842 e++;
843 if (*e != '\n' && *e != '\0')
844 return; /* false alarm */
845
f4dd75d9 846 if (t - s > 0) {
d9095cec 847 const STRLEN len = t - s;
8a5ee598 848#ifndef USE_ITHREADS
19bad673
NC
849 SV *const temp_sv = CopFILESV(PL_curcop);
850 const char *cf;
851 STRLEN tmplen;
852
853 if (temp_sv) {
854 cf = SvPVX(temp_sv);
855 tmplen = SvCUR(temp_sv);
856 } else {
857 cf = NULL;
858 tmplen = 0;
859 }
860
42d9b98d 861 if (tmplen > 7 && strnEQ(cf, "(eval ", 6)) {
e66cf94c
RGS
862 /* must copy *{"::_<(eval N)[oldfilename:L]"}
863 * to *{"::_<newfilename"} */
44867030
NC
864 /* However, the long form of evals is only turned on by the
865 debugger - usually they're "(eval %lu)" */
866 char smallbuf[128];
867 char *tmpbuf;
868 GV **gvp;
d9095cec 869 STRLEN tmplen2 = len;
798b63bc 870 if (tmplen + 2 <= sizeof smallbuf)
e66cf94c
RGS
871 tmpbuf = smallbuf;
872 else
2ae0db35 873 Newx(tmpbuf, tmplen + 2, char);
44867030
NC
874 tmpbuf[0] = '_';
875 tmpbuf[1] = '<';
2ae0db35 876 memcpy(tmpbuf + 2, cf, tmplen);
44867030 877 tmplen += 2;
8a5ee598
RGS
878 gvp = (GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, FALSE);
879 if (gvp) {
44867030
NC
880 char *tmpbuf2;
881 GV *gv2;
882
883 if (tmplen2 + 2 <= sizeof smallbuf)
884 tmpbuf2 = smallbuf;
885 else
886 Newx(tmpbuf2, tmplen2 + 2, char);
887
888 if (tmpbuf2 != smallbuf || tmpbuf != smallbuf) {
889 /* Either they malloc'd it, or we malloc'd it,
890 so no prefix is present in ours. */
891 tmpbuf2[0] = '_';
892 tmpbuf2[1] = '<';
893 }
894
895 memcpy(tmpbuf2 + 2, s, tmplen2);
896 tmplen2 += 2;
897
8a5ee598 898 gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE);
e5527e4b 899 if (!isGV(gv2)) {
8a5ee598 900 gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE);
e5527e4b
RGS
901 /* adjust ${"::_<newfilename"} to store the new file name */
902 GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2);
3cb1dbc6
NC
903 GvHV(gv2) = MUTABLE_HV(SvREFCNT_inc(GvHV(*gvp)));
904 GvAV(gv2) = MUTABLE_AV(SvREFCNT_inc(GvAV(*gvp)));
e5527e4b 905 }
44867030
NC
906
907 if (tmpbuf2 != smallbuf) Safefree(tmpbuf2);
8a5ee598 908 }
e66cf94c 909 if (tmpbuf != smallbuf) Safefree(tmpbuf);
e66cf94c 910 }
8a5ee598 911#endif
05ec9bb3 912 CopFILE_free(PL_curcop);
d9095cec 913 CopFILE_setn(PL_curcop, s, len);
f4dd75d9 914 }
57843af0 915 CopLINE_set(PL_curcop, atoi(n)-1);
463ee0b2
LW
916}
917
29595ff2 918#ifdef PERL_MAD
cd81e915 919/* skip space before PL_thistoken */
29595ff2
NC
920
921STATIC char *
922S_skipspace0(pTHX_ register char *s)
923{
7918f24d
NC
924 PERL_ARGS_ASSERT_SKIPSPACE0;
925
29595ff2
NC
926 s = skipspace(s);
927 if (!PL_madskills)
928 return s;
cd81e915
NC
929 if (PL_skipwhite) {
930 if (!PL_thiswhite)
6b29d1f5 931 PL_thiswhite = newSVpvs("");
cd81e915
NC
932 sv_catsv(PL_thiswhite, PL_skipwhite);
933 sv_free(PL_skipwhite);
934 PL_skipwhite = 0;
935 }
936 PL_realtokenstart = s - SvPVX(PL_linestr);
29595ff2
NC
937 return s;
938}
939
cd81e915 940/* skip space after PL_thistoken */
29595ff2
NC
941
942STATIC char *
943S_skipspace1(pTHX_ register char *s)
944{
d4c19fe8 945 const char *start = s;
29595ff2
NC
946 I32 startoff = start - SvPVX(PL_linestr);
947
7918f24d
NC
948 PERL_ARGS_ASSERT_SKIPSPACE1;
949
29595ff2
NC
950 s = skipspace(s);
951 if (!PL_madskills)
952 return s;
953 start = SvPVX(PL_linestr) + startoff;
cd81e915 954 if (!PL_thistoken && PL_realtokenstart >= 0) {
d4c19fe8 955 const char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
cd81e915
NC
956 PL_thistoken = newSVpvn(tstart, start - tstart);
957 }
958 PL_realtokenstart = -1;
959 if (PL_skipwhite) {
960 if (!PL_nextwhite)
6b29d1f5 961 PL_nextwhite = newSVpvs("");
cd81e915
NC
962 sv_catsv(PL_nextwhite, PL_skipwhite);
963 sv_free(PL_skipwhite);
964 PL_skipwhite = 0;
29595ff2
NC
965 }
966 return s;
967}
968
969STATIC char *
970S_skipspace2(pTHX_ register char *s, SV **svp)
971{
c35e046a
AL
972 char *start;
973 const I32 bufptroff = PL_bufptr - SvPVX(PL_linestr);
974 const I32 startoff = s - SvPVX(PL_linestr);
975
7918f24d
NC
976 PERL_ARGS_ASSERT_SKIPSPACE2;
977
29595ff2
NC
978 s = skipspace(s);
979 PL_bufptr = SvPVX(PL_linestr) + bufptroff;
980 if (!PL_madskills || !svp)
981 return s;
982 start = SvPVX(PL_linestr) + startoff;
cd81e915 983 if (!PL_thistoken && PL_realtokenstart >= 0) {
d4c19fe8 984 char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
cd81e915
NC
985 PL_thistoken = newSVpvn(tstart, start - tstart);
986 PL_realtokenstart = -1;
29595ff2 987 }
cd81e915 988 if (PL_skipwhite) {
29595ff2 989 if (!*svp)
6b29d1f5 990 *svp = newSVpvs("");
cd81e915
NC
991 sv_setsv(*svp, PL_skipwhite);
992 sv_free(PL_skipwhite);
993 PL_skipwhite = 0;
29595ff2
NC
994 }
995
996 return s;
997}
998#endif
999
80a702cd 1000STATIC void
15f169a1 1001S_update_debugger_info(pTHX_ SV *orig_sv, const char *const buf, STRLEN len)
80a702cd
RGS
1002{
1003 AV *av = CopFILEAVx(PL_curcop);
1004 if (av) {
b9f83d2f 1005 SV * const sv = newSV_type(SVt_PVMG);
5fa550fb
NC
1006 if (orig_sv)
1007 sv_setsv(sv, orig_sv);
1008 else
1009 sv_setpvn(sv, buf, len);
80a702cd
RGS
1010 (void)SvIOK_on(sv);
1011 SvIV_set(sv, 0);
1012 av_store(av, (I32)CopLINE(PL_curcop), sv);
1013 }
1014}
1015
ffb4593c
NT
1016/*
1017 * S_skipspace
1018 * Called to gobble the appropriate amount and type of whitespace.
1019 * Skips comments as well.
1020 */
1021
76e3520e 1022STATIC char *
cea2e8a9 1023S_skipspace(pTHX_ register char *s)
a687059c 1024{
97aff369 1025 dVAR;
5db06880
NC
1026#ifdef PERL_MAD
1027 int curoff;
1028 int startoff = s - SvPVX(PL_linestr);
1029
7918f24d
NC
1030 PERL_ARGS_ASSERT_SKIPSPACE;
1031
cd81e915
NC
1032 if (PL_skipwhite) {
1033 sv_free(PL_skipwhite);
1034 PL_skipwhite = 0;
5db06880
NC
1035 }
1036#endif
7918f24d 1037 PERL_ARGS_ASSERT_SKIPSPACE;
5db06880 1038
3280af22 1039 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
bf4acbe4 1040 while (s < PL_bufend && SPACE_OR_TAB(*s))
463ee0b2 1041 s++;
5db06880
NC
1042#ifdef PERL_MAD
1043 goto done;
1044#else
463ee0b2 1045 return s;
5db06880 1046#endif
463ee0b2
LW
1047 }
1048 for (;;) {
fd049845 1049 STRLEN prevlen;
09bef843 1050 SSize_t oldprevlen, oldoldprevlen;
9c5ffd7c 1051 SSize_t oldloplen = 0, oldunilen = 0;
60e6418e
GS
1052 while (s < PL_bufend && isSPACE(*s)) {
1053 if (*s++ == '\n' && PL_in_eval && !PL_rsfp)
1054 incline(s);
1055 }
ffb4593c
NT
1056
1057 /* comment */
3280af22
NIS
1058 if (s < PL_bufend && *s == '#') {
1059 while (s < PL_bufend && *s != '\n')
463ee0b2 1060 s++;
60e6418e 1061 if (s < PL_bufend) {
463ee0b2 1062 s++;
60e6418e
GS
1063 if (PL_in_eval && !PL_rsfp) {
1064 incline(s);
1065 continue;
1066 }
1067 }
463ee0b2 1068 }
ffb4593c
NT
1069
1070 /* only continue to recharge the buffer if we're at the end
1071 * of the buffer, we're not reading from a source filter, and
1072 * we're in normal lexing mode
1073 */
09bef843
SB
1074 if (s < PL_bufend || !PL_rsfp || PL_sublex_info.sub_inwhat ||
1075 PL_lex_state == LEX_FORMLINE)
5db06880
NC
1076#ifdef PERL_MAD
1077 goto done;
1078#else
463ee0b2 1079 return s;
5db06880 1080#endif
ffb4593c
NT
1081
1082 /* try to recharge the buffer */
5db06880
NC
1083#ifdef PERL_MAD
1084 curoff = s - SvPVX(PL_linestr);
1085#endif
1086
9cbb5ea2 1087 if ((s = filter_gets(PL_linestr, PL_rsfp,
bd61b366 1088 (prevlen = SvCUR(PL_linestr)))) == NULL)
9cbb5ea2 1089 {
5db06880
NC
1090#ifdef PERL_MAD
1091 if (PL_madskills && curoff != startoff) {
cd81e915 1092 if (!PL_skipwhite)
6b29d1f5 1093 PL_skipwhite = newSVpvs("");
cd81e915 1094 sv_catpvn(PL_skipwhite, SvPVX(PL_linestr) + startoff,
5db06880
NC
1095 curoff - startoff);
1096 }
1097
1098 /* mustn't throw out old stuff yet if madpropping */
1099 SvCUR(PL_linestr) = curoff;
1100 s = SvPVX(PL_linestr) + curoff;
1101 *s = 0;
1102 if (curoff && s[-1] == '\n')
1103 s[-1] = ' ';
1104#endif
1105
9cbb5ea2 1106 /* end of file. Add on the -p or -n magic */
cd81e915 1107 /* XXX these shouldn't really be added here, can't set PL_faketokens */
01a19ab0 1108 if (PL_minus_p) {
5db06880 1109#ifdef PERL_MAD
6502358f 1110 sv_catpvs(PL_linestr,
5db06880
NC
1111 ";}continue{print or die qq(-p destination: $!\\n);}");
1112#else
6502358f 1113 sv_setpvs(PL_linestr,
01a19ab0 1114 ";}continue{print or die qq(-p destination: $!\\n);}");
5db06880 1115#endif
3280af22 1116 PL_minus_n = PL_minus_p = 0;
a0d0e21e 1117 }
01a19ab0 1118 else if (PL_minus_n) {
5db06880 1119#ifdef PERL_MAD
76f68e9b 1120 sv_catpvs(PL_linestr, ";}");
5db06880 1121#else
76f68e9b 1122 sv_setpvs(PL_linestr, ";}");
5db06880 1123#endif
01a19ab0
NC
1124 PL_minus_n = 0;
1125 }
a0d0e21e 1126 else
5db06880 1127#ifdef PERL_MAD
76f68e9b 1128 sv_catpvs(PL_linestr,";");
5db06880 1129#else
76f68e9b 1130 sv_setpvs(PL_linestr,";");
5db06880 1131#endif
ffb4593c
NT
1132
1133 /* reset variables for next time we lex */
9cbb5ea2 1134 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart
89122651
NC
1135 = SvPVX(PL_linestr)
1136#ifdef PERL_MAD
1137 + curoff
1138#endif
1139 ;
3280af22 1140 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
bd61b366 1141 PL_last_lop = PL_last_uni = NULL;
ffb4593c 1142
4c84d7f2 1143 /* Close the filehandle. Could be from
ffb4593c
NT
1144 * STDIN, or a regular file. If we were reading code from
1145 * STDIN (because the commandline held no -e or filename)
1146 * then we don't close it, we reset it so the code can
1147 * read from STDIN too.
1148 */
1149
4c84d7f2 1150 if ((PerlIO*)PL_rsfp == PerlIO_stdin())
3280af22 1151 PerlIO_clearerr(PL_rsfp);
8990e307 1152 else
3280af22 1153 (void)PerlIO_close(PL_rsfp);
4608196e 1154 PL_rsfp = NULL;
463ee0b2
LW
1155 return s;
1156 }
ffb4593c
NT
1157
1158 /* not at end of file, so we only read another line */
09bef843
SB
1159 /* make corresponding updates to old pointers, for yyerror() */
1160 oldprevlen = PL_oldbufptr - PL_bufend;
1161 oldoldprevlen = PL_oldoldbufptr - PL_bufend;
1162 if (PL_last_uni)
1163 oldunilen = PL_last_uni - PL_bufend;
1164 if (PL_last_lop)
1165 oldloplen = PL_last_lop - PL_bufend;
3280af22
NIS
1166 PL_linestart = PL_bufptr = s + prevlen;
1167 PL_bufend = s + SvCUR(PL_linestr);
1168 s = PL_bufptr;
09bef843
SB
1169 PL_oldbufptr = s + oldprevlen;
1170 PL_oldoldbufptr = s + oldoldprevlen;
1171 if (PL_last_uni)
1172 PL_last_uni = s + oldunilen;
1173 if (PL_last_lop)
1174 PL_last_lop = s + oldloplen;
a0d0e21e 1175 incline(s);
ffb4593c
NT
1176
1177 /* debugger active and we're not compiling the debugger code,
1178 * so store the line into the debugger's array of lines
1179 */
65269a95 1180 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
5fa550fb 1181 update_debugger_info(NULL, PL_bufptr, PL_bufend - PL_bufptr);
463ee0b2 1182 }
5db06880
NC
1183
1184#ifdef PERL_MAD
1185 done:
1186 if (PL_madskills) {
cd81e915 1187 if (!PL_skipwhite)
6b29d1f5 1188 PL_skipwhite = newSVpvs("");
5db06880
NC
1189 curoff = s - SvPVX(PL_linestr);
1190 if (curoff - startoff)
cd81e915 1191 sv_catpvn(PL_skipwhite, SvPVX(PL_linestr) + startoff,
5db06880
NC
1192 curoff - startoff);
1193 }
1194 return s;
1195#endif
a687059c 1196}
378cc40b 1197
ffb4593c
NT
1198/*
1199 * S_check_uni
1200 * Check the unary operators to ensure there's no ambiguity in how they're
1201 * used. An ambiguous piece of code would be:
1202 * rand + 5
1203 * This doesn't mean rand() + 5. Because rand() is a unary operator,
1204 * the +5 is its argument.
1205 */
1206
76e3520e 1207STATIC void
cea2e8a9 1208S_check_uni(pTHX)
ba106d47 1209{
97aff369 1210 dVAR;
d4c19fe8
AL
1211 const char *s;
1212 const char *t;
2f3197b3 1213
3280af22 1214 if (PL_oldoldbufptr != PL_last_uni)
2f3197b3 1215 return;
3280af22
NIS
1216 while (isSPACE(*PL_last_uni))
1217 PL_last_uni++;
c35e046a
AL
1218 s = PL_last_uni;
1219 while (isALNUM_lazy_if(s,UTF) || *s == '-')
1220 s++;
3280af22 1221 if ((t = strchr(s, '(')) && t < PL_bufptr)
a0d0e21e 1222 return;
6136c704 1223
0453d815 1224 if (ckWARN_d(WARN_AMBIGUOUS)){
9014280d 1225 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
32d45c1d
NC
1226 "Warning: Use of \"%.*s\" without parentheses is ambiguous",
1227 (int)(s - PL_last_uni), PL_last_uni);
0453d815 1228 }
2f3197b3
LW
1229}
1230
ffb4593c
NT
1231/*
1232 * LOP : macro to build a list operator. Its behaviour has been replaced
1233 * with a subroutine, S_lop() for which LOP is just another name.
1234 */
1235
a0d0e21e
LW
1236#define LOP(f,x) return lop(f,x,s)
1237
ffb4593c
NT
1238/*
1239 * S_lop
1240 * Build a list operator (or something that might be one). The rules:
1241 * - if we have a next token, then it's a list operator [why?]
1242 * - if the next thing is an opening paren, then it's a function
1243 * - else it's a list operator
1244 */
1245
76e3520e 1246STATIC I32
a0be28da 1247S_lop(pTHX_ I32 f, int x, char *s)
ffed7fef 1248{
97aff369 1249 dVAR;
7918f24d
NC
1250
1251 PERL_ARGS_ASSERT_LOP;
1252
6154021b 1253 pl_yylval.ival = f;
35c8bce7 1254 CLINE;
3280af22
NIS
1255 PL_expect = x;
1256 PL_bufptr = s;
1257 PL_last_lop = PL_oldbufptr;
eb160463 1258 PL_last_lop_op = (OPCODE)f;
5db06880
NC
1259#ifdef PERL_MAD
1260 if (PL_lasttoke)
1261 return REPORT(LSTOP);
1262#else
3280af22 1263 if (PL_nexttoke)
bbf60fe6 1264 return REPORT(LSTOP);
5db06880 1265#endif
79072805 1266 if (*s == '(')
bbf60fe6 1267 return REPORT(FUNC);
29595ff2 1268 s = PEEKSPACE(s);
79072805 1269 if (*s == '(')
bbf60fe6 1270 return REPORT(FUNC);
79072805 1271 else
bbf60fe6 1272 return REPORT(LSTOP);
79072805
LW
1273}
1274
5db06880
NC
1275#ifdef PERL_MAD
1276 /*
1277 * S_start_force
1278 * Sets up for an eventual force_next(). start_force(0) basically does
1279 * an unshift, while start_force(-1) does a push. yylex removes items
1280 * on the "pop" end.
1281 */
1282
1283STATIC void
1284S_start_force(pTHX_ int where)
1285{
1286 int i;
1287
cd81e915 1288 if (where < 0) /* so people can duplicate start_force(PL_curforce) */
5db06880 1289 where = PL_lasttoke;
cd81e915
NC
1290 assert(PL_curforce < 0 || PL_curforce == where);
1291 if (PL_curforce != where) {
5db06880
NC
1292 for (i = PL_lasttoke; i > where; --i) {
1293 PL_nexttoke[i] = PL_nexttoke[i-1];
1294 }
1295 PL_lasttoke++;
1296 }
cd81e915 1297 if (PL_curforce < 0) /* in case of duplicate start_force() */
5db06880 1298 Zero(&PL_nexttoke[where], 1, NEXTTOKE);
cd81e915
NC
1299 PL_curforce = where;
1300 if (PL_nextwhite) {
5db06880 1301 if (PL_madskills)
6b29d1f5 1302 curmad('^', newSVpvs(""));
cd81e915 1303 CURMAD('_', PL_nextwhite);
5db06880
NC
1304 }
1305}
1306
1307STATIC void
1308S_curmad(pTHX_ char slot, SV *sv)
1309{
1310 MADPROP **where;
1311
1312 if (!sv)
1313 return;
cd81e915
NC
1314 if (PL_curforce < 0)
1315 where = &PL_thismad;
5db06880 1316 else
cd81e915 1317 where = &PL_nexttoke[PL_curforce].next_mad;
5db06880 1318
cd81e915 1319 if (PL_faketokens)
76f68e9b 1320 sv_setpvs(sv, "");
5db06880
NC
1321 else {
1322 if (!IN_BYTES) {
1323 if (UTF && is_utf8_string((U8*)SvPVX(sv), SvCUR(sv)))
1324 SvUTF8_on(sv);
1325 else if (PL_encoding) {
1326 sv_recode_to_utf8(sv, PL_encoding);
1327 }
1328 }
1329 }
1330
1331 /* keep a slot open for the head of the list? */
1332 if (slot != '_' && *where && (*where)->mad_key == '^') {
1333 (*where)->mad_key = slot;
daba3364 1334 sv_free(MUTABLE_SV(((*where)->mad_val)));
5db06880
NC
1335 (*where)->mad_val = (void*)sv;
1336 }
1337 else
1338 addmad(newMADsv(slot, sv), where, 0);
1339}
1340#else
b3f24c00
MHM
1341# define start_force(where) NOOP
1342# define curmad(slot, sv) NOOP
5db06880
NC
1343#endif
1344
ffb4593c
NT
1345/*
1346 * S_force_next
9cbb5ea2 1347 * When the lexer realizes it knows the next token (for instance,
ffb4593c 1348 * it is reordering tokens for the parser) then it can call S_force_next
9cbb5ea2 1349 * to know what token to return the next time the lexer is called. Caller
5db06880
NC
1350 * will need to set PL_nextval[] (or PL_nexttoke[].next_val with PERL_MAD),
1351 * and possibly PL_expect to ensure the lexer handles the token correctly.
ffb4593c
NT
1352 */
1353
4e553d73 1354STATIC void
cea2e8a9 1355S_force_next(pTHX_ I32 type)
79072805 1356{
97aff369 1357 dVAR;
704d4215
GG
1358#ifdef DEBUGGING
1359 if (DEBUG_T_TEST) {
1360 PerlIO_printf(Perl_debug_log, "### forced token:\n");
f05d7009 1361 tokereport(type, &NEXTVAL_NEXTTOKE);
704d4215
GG
1362 }
1363#endif
5db06880 1364#ifdef PERL_MAD
cd81e915 1365 if (PL_curforce < 0)
5db06880 1366 start_force(PL_lasttoke);
cd81e915 1367 PL_nexttoke[PL_curforce].next_type = type;
5db06880
NC
1368 if (PL_lex_state != LEX_KNOWNEXT)
1369 PL_lex_defer = PL_lex_state;
1370 PL_lex_state = LEX_KNOWNEXT;
1371 PL_lex_expect = PL_expect;
cd81e915 1372 PL_curforce = -1;
5db06880 1373#else
3280af22
NIS
1374 PL_nexttype[PL_nexttoke] = type;
1375 PL_nexttoke++;
1376 if (PL_lex_state != LEX_KNOWNEXT) {
1377 PL_lex_defer = PL_lex_state;
1378 PL_lex_expect = PL_expect;
1379 PL_lex_state = LEX_KNOWNEXT;
79072805 1380 }
5db06880 1381#endif
79072805
LW
1382}
1383
d0a148a6 1384STATIC SV *
15f169a1 1385S_newSV_maybe_utf8(pTHX_ const char *const start, STRLEN len)
d0a148a6 1386{
97aff369 1387 dVAR;
740cce10
NC
1388 SV * const sv = newSVpvn_utf8(start, len,
1389 UTF && !IN_BYTES
1390 && is_utf8_string((const U8*)start, len));
d0a148a6
NC
1391 return sv;
1392}
1393
ffb4593c
NT
1394/*
1395 * S_force_word
1396 * When the lexer knows the next thing is a word (for instance, it has
1397 * just seen -> and it knows that the next char is a word char, then
02b34bbe
DM
1398 * it calls S_force_word to stick the next word into the PL_nexttoke/val
1399 * lookahead.
ffb4593c
NT
1400 *
1401 * Arguments:
b1b65b59 1402 * char *start : buffer position (must be within PL_linestr)
02b34bbe 1403 * int token : PL_next* will be this type of bare word (e.g., METHOD,WORD)
ffb4593c
NT
1404 * int check_keyword : if true, Perl checks to make sure the word isn't
1405 * a keyword (do this if the word is a label, e.g. goto FOO)
1406 * int allow_pack : if true, : characters will also be allowed (require,
1407 * use, etc. do this)
9cbb5ea2 1408 * int allow_initial_tick : used by the "sub" lexer only.
ffb4593c
NT
1409 */
1410
76e3520e 1411STATIC char *
cea2e8a9 1412S_force_word(pTHX_ register char *start, int token, int check_keyword, int allow_pack, int allow_initial_tick)
79072805 1413{
97aff369 1414 dVAR;
463ee0b2
LW
1415 register char *s;
1416 STRLEN len;
4e553d73 1417
7918f24d
NC
1418 PERL_ARGS_ASSERT_FORCE_WORD;
1419
29595ff2 1420 start = SKIPSPACE1(start);
463ee0b2 1421 s = start;
7e2040f0 1422 if (isIDFIRST_lazy_if(s,UTF) ||
a0d0e21e 1423 (allow_pack && *s == ':') ||
15f0808c 1424 (allow_initial_tick && *s == '\'') )
a0d0e21e 1425 {
3280af22 1426 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, allow_pack, &len);
5458a98a 1427 if (check_keyword && keyword(PL_tokenbuf, len, 0))
463ee0b2 1428 return start;
cd81e915 1429 start_force(PL_curforce);
5db06880
NC
1430 if (PL_madskills)
1431 curmad('X', newSVpvn(start,s-start));
463ee0b2 1432 if (token == METHOD) {
29595ff2 1433 s = SKIPSPACE1(s);
463ee0b2 1434 if (*s == '(')
3280af22 1435 PL_expect = XTERM;
463ee0b2 1436 else {
3280af22 1437 PL_expect = XOPERATOR;
463ee0b2 1438 }
79072805 1439 }
e74e6b3d 1440 if (PL_madskills)
63575281 1441 curmad('g', newSVpvs( "forced" ));
9ded7720 1442 NEXTVAL_NEXTTOKE.opval
d0a148a6
NC
1443 = (OP*)newSVOP(OP_CONST,0,
1444 S_newSV_maybe_utf8(aTHX_ PL_tokenbuf, len));
9ded7720 1445 NEXTVAL_NEXTTOKE.opval->op_private |= OPpCONST_BARE;
79072805
LW
1446 force_next(token);
1447 }
1448 return s;
1449}
1450
ffb4593c
NT
1451/*
1452 * S_force_ident
9cbb5ea2 1453 * Called when the lexer wants $foo *foo &foo etc, but the program
ffb4593c
NT
1454 * text only contains the "foo" portion. The first argument is a pointer
1455 * to the "foo", and the second argument is the type symbol to prefix.
1456 * Forces the next token to be a "WORD".
9cbb5ea2 1457 * Creates the symbol if it didn't already exist (via gv_fetchpv()).
ffb4593c
NT
1458 */
1459
76e3520e 1460STATIC void
bfed75c6 1461S_force_ident(pTHX_ register const char *s, int kind)
79072805 1462{
97aff369 1463 dVAR;
7918f24d
NC
1464
1465 PERL_ARGS_ASSERT_FORCE_IDENT;
1466
c35e046a 1467 if (*s) {
90e5519e
NC
1468 const STRLEN len = strlen(s);
1469 OP* const o = (OP*)newSVOP(OP_CONST, 0, newSVpvn(s, len));
cd81e915 1470 start_force(PL_curforce);
9ded7720 1471 NEXTVAL_NEXTTOKE.opval = o;
79072805 1472 force_next(WORD);
748a9306 1473 if (kind) {
11343788 1474 o->op_private = OPpCONST_ENTERED;
55497cff
PP
1475 /* XXX see note in pp_entereval() for why we forgo typo
1476 warnings if the symbol must be introduced in an eval.
1477 GSAR 96-10-12 */
90e5519e
NC
1478 gv_fetchpvn_flags(s, len,
1479 PL_in_eval ? (GV_ADDMULTI | GV_ADDINEVAL)
1480 : GV_ADD,
1481 kind == '$' ? SVt_PV :
1482 kind == '@' ? SVt_PVAV :
1483 kind == '%' ? SVt_PVHV :
a0d0e21e 1484 SVt_PVGV
90e5519e 1485 );
748a9306 1486 }
79072805
LW
1487 }
1488}
1489
1571675a
GS
1490NV
1491Perl_str_to_version(pTHX_ SV *sv)
1492{
1493 NV retval = 0.0;
1494 NV nshift = 1.0;
1495 STRLEN len;
cfd0369c 1496 const char *start = SvPV_const(sv,len);
9d4ba2ae 1497 const char * const end = start + len;
504618e9 1498 const bool utf = SvUTF8(sv) ? TRUE : FALSE;
7918f24d
NC
1499
1500 PERL_ARGS_ASSERT_STR_TO_VERSION;
1501
1571675a 1502 while (start < end) {
ba210ebe 1503 STRLEN skip;
1571675a
GS
1504 UV n;
1505 if (utf)
9041c2e3 1506 n = utf8n_to_uvchr((U8*)start, len, &skip, 0);
1571675a
GS
1507 else {
1508 n = *(U8*)start;
1509 skip = 1;
1510 }
1511 retval += ((NV)n)/nshift;
1512 start += skip;
1513 nshift *= 1000;
1514 }
1515 return retval;
1516}
1517
4e553d73 1518/*
ffb4593c
NT
1519 * S_force_version
1520 * Forces the next token to be a version number.
e759cc13
RGS
1521 * If the next token appears to be an invalid version number, (e.g. "v2b"),
1522 * and if "guessing" is TRUE, then no new token is created (and the caller
1523 * must use an alternative parsing method).
ffb4593c
NT
1524 */
1525
76e3520e 1526STATIC char *
e759cc13 1527S_force_version(pTHX_ char *s, int guessing)
89bfa8cd 1528{
97aff369 1529 dVAR;
5f66b61c 1530 OP *version = NULL;
44dcb63b 1531 char *d;
5db06880
NC
1532#ifdef PERL_MAD
1533 I32 startoff = s - SvPVX(PL_linestr);
1534#endif
89bfa8cd 1535
7918f24d
NC
1536 PERL_ARGS_ASSERT_FORCE_VERSION;
1537
29595ff2 1538 s = SKIPSPACE1(s);
89bfa8cd 1539
44dcb63b 1540 d = s;
dd629d5b 1541 if (*d == 'v')
44dcb63b 1542 d++;
44dcb63b 1543 if (isDIGIT(*d)) {
e759cc13
RGS
1544 while (isDIGIT(*d) || *d == '_' || *d == '.')
1545 d++;
5db06880
NC
1546#ifdef PERL_MAD
1547 if (PL_madskills) {
cd81e915 1548 start_force(PL_curforce);
5db06880
NC
1549 curmad('X', newSVpvn(s,d-s));
1550 }
1551#endif
9f3d182e 1552 if (*d == ';' || isSPACE(*d) || *d == '}' || !*d) {
dd629d5b 1553 SV *ver;
6154021b
RGS
1554 s = scan_num(s, &pl_yylval);
1555 version = pl_yylval.opval;
dd629d5b
GS
1556 ver = cSVOPx(version)->op_sv;
1557 if (SvPOK(ver) && !SvNIOK(ver)) {
862a34c6 1558 SvUPGRADE(ver, SVt_PVNV);
9d6ce603 1559 SvNV_set(ver, str_to_version(ver));
1571675a 1560 SvNOK_on(ver); /* hint that it is a version */
44dcb63b 1561 }
89bfa8cd 1562 }
5db06880
NC
1563 else if (guessing) {
1564#ifdef PERL_MAD
1565 if (PL_madskills) {
cd81e915
NC
1566 sv_free(PL_nextwhite); /* let next token collect whitespace */
1567 PL_nextwhite = 0;
5db06880
NC
1568 s = SvPVX(PL_linestr) + startoff;
1569 }
1570#endif
e759cc13 1571 return s;
5db06880 1572 }
89bfa8cd
PP
1573 }
1574
5db06880
NC
1575#ifdef PERL_MAD
1576 if (PL_madskills && !version) {
cd81e915
NC
1577 sv_free(PL_nextwhite); /* let next token collect whitespace */
1578 PL_nextwhite = 0;
5db06880
NC
1579 s = SvPVX(PL_linestr) + startoff;
1580 }
1581#endif
89bfa8cd 1582 /* NOTE: The parser sees the package name and the VERSION swapped */
cd81e915 1583 start_force(PL_curforce);
9ded7720 1584 NEXTVAL_NEXTTOKE.opval = version;
4e553d73 1585 force_next(WORD);
89bfa8cd 1586
e759cc13 1587 return s;
89bfa8cd
PP
1588}
1589
ffb4593c
NT
1590/*
1591 * S_tokeq
1592 * Tokenize a quoted string passed in as an SV. It finds the next
1593 * chunk, up to end of string or a backslash. It may make a new
1594 * SV containing that chunk (if HINT_NEW_STRING is on). It also
1595 * turns \\ into \.
1596 */
1597
76e3520e 1598STATIC SV *
cea2e8a9 1599S_tokeq(pTHX_ SV *sv)
79072805 1600{
97aff369 1601 dVAR;
79072805
LW
1602 register char *s;
1603 register char *send;
1604 register char *d;
b3ac6de7
IZ
1605 STRLEN len = 0;
1606 SV *pv = sv;
79072805 1607
7918f24d
NC
1608 PERL_ARGS_ASSERT_TOKEQ;
1609
79072805 1610 if (!SvLEN(sv))
b3ac6de7 1611 goto finish;
79072805 1612
a0d0e21e 1613 s = SvPV_force(sv, len);
21a311ee 1614 if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1)
b3ac6de7 1615 goto finish;
463ee0b2 1616 send = s + len;
79072805
LW
1617 while (s < send && *s != '\\')
1618 s++;
1619 if (s == send)
b3ac6de7 1620 goto finish;
79072805 1621 d = s;
be4731d2 1622 if ( PL_hints & HINT_NEW_STRING ) {
59cd0e26 1623 pv = newSVpvn_flags(SvPVX_const(pv), len, SVs_TEMP | SvUTF8(sv));
be4731d2 1624 }
79072805
LW
1625 while (s < send) {
1626 if (*s == '\\') {
a0d0e21e 1627 if (s + 1 < send && (s[1] == '\\'))
79072805
LW
1628 s++; /* all that, just for this */
1629 }
1630 *d++ = *s++;
1631 }
1632 *d = '\0';
95a20fc0 1633 SvCUR_set(sv, d - SvPVX_const(sv));
b3ac6de7 1634 finish:
3280af22 1635 if ( PL_hints & HINT_NEW_STRING )
eb0d8d16 1636 return new_constant(NULL, 0, "q", sv, pv, "q", 1);
79072805
LW
1637 return sv;
1638}
1639
ffb4593c
NT
1640/*
1641 * Now come three functions related to double-quote context,
1642 * S_sublex_start, S_sublex_push, and S_sublex_done. They're used when
1643 * converting things like "\u\Lgnat" into ucfirst(lc("gnat")). They
1644 * interact with PL_lex_state, and create fake ( ... ) argument lists
1645 * to handle functions and concatenation.
1646 * They assume that whoever calls them will be setting up a fake
1647 * join call, because each subthing puts a ',' after it. This lets
1648 * "lower \luPpEr"
1649 * become
1650 * join($, , 'lower ', lcfirst( 'uPpEr', ) ,)
1651 *
1652 * (I'm not sure whether the spurious commas at the end of lcfirst's
1653 * arguments and join's arguments are created or not).
1654 */
1655
1656/*
1657 * S_sublex_start
6154021b 1658 * Assumes that pl_yylval.ival is the op we're creating (e.g. OP_LCFIRST).
ffb4593c
NT
1659 *
1660 * Pattern matching will set PL_lex_op to the pattern-matching op to
6154021b 1661 * make (we return THING if pl_yylval.ival is OP_NULL, PMFUNC otherwise).
ffb4593c
NT
1662 *
1663 * OP_CONST and OP_READLINE are easy--just make the new op and return.
1664 *
1665 * Everything else becomes a FUNC.
1666 *
1667 * Sets PL_lex_state to LEX_INTERPPUSH unless (ival was OP_NULL or we
1668 * had an OP_CONST or OP_READLINE). This just sets us up for a
1669 * call to S_sublex_push().
1670 */
1671
76e3520e 1672STATIC I32
cea2e8a9 1673S_sublex_start(pTHX)
79072805 1674{
97aff369 1675 dVAR;
6154021b 1676 register const I32 op_type = pl_yylval.ival;
79072805
LW
1677
1678 if (op_type == OP_NULL) {
6154021b 1679 pl_yylval.opval = PL_lex_op;
5f66b61c 1680 PL_lex_op = NULL;
79072805
LW
1681 return THING;
1682 }
1683 if (op_type == OP_CONST || op_type == OP_READLINE) {
3280af22 1684 SV *sv = tokeq(PL_lex_stuff);
b3ac6de7
IZ
1685
1686 if (SvTYPE(sv) == SVt_PVIV) {
1687 /* Overloaded constants, nothing fancy: Convert to SVt_PV: */
1688 STRLEN len;
96a5add6 1689 const char * const p = SvPV_const(sv, len);
740cce10 1690 SV * const nsv = newSVpvn_flags(p, len, SvUTF8(sv));
b3ac6de7
IZ
1691 SvREFCNT_dec(sv);
1692 sv = nsv;
4e553d73 1693 }
6154021b 1694 pl_yylval.opval = (OP*)newSVOP(op_type, 0, sv);
a0714e2c 1695 PL_lex_stuff = NULL;
6f33ba73
RGS
1696 /* Allow <FH> // "foo" */
1697 if (op_type == OP_READLINE)
1698 PL_expect = XTERMORDORDOR;
79072805
LW
1699 return THING;
1700 }
e3f73d4e
RGS
1701 else if (op_type == OP_BACKTICK && PL_lex_op) {
1702 /* readpipe() vas overriden */
1703 cSVOPx(cLISTOPx(cUNOPx(PL_lex_op)->op_first)->op_first->op_sibling)->op_sv = tokeq(PL_lex_stuff);
6154021b 1704 pl_yylval.opval = PL_lex_op;
9b201d7d 1705 PL_lex_op = NULL;
e3f73d4e
RGS
1706 PL_lex_stuff = NULL;
1707 return THING;
1708 }
79072805 1709
3280af22 1710 PL_sublex_info.super_state = PL_lex_state;
eac04b2e 1711 PL_sublex_info.sub_inwhat = (U16)op_type;
3280af22
NIS
1712 PL_sublex_info.sub_op = PL_lex_op;
1713 PL_lex_state = LEX_INTERPPUSH;
55497cff 1714
3280af22
NIS
1715 PL_expect = XTERM;
1716 if (PL_lex_op) {
6154021b 1717 pl_yylval.opval = PL_lex_op;
5f66b61c 1718 PL_lex_op = NULL;
55497cff
PP
1719 return PMFUNC;
1720 }
1721 else
1722 return FUNC;
1723}
1724
ffb4593c
NT
1725/*
1726 * S_sublex_push
1727 * Create a new scope to save the lexing state. The scope will be
1728 * ended in S_sublex_done. Returns a '(', starting the function arguments
1729 * to the uc, lc, etc. found before.
1730 * Sets PL_lex_state to LEX_INTERPCONCAT.
1731 */
1732
76e3520e 1733STATIC I32
cea2e8a9 1734S_sublex_push(pTHX)
55497cff 1735{
27da23d5 1736 dVAR;
f46d017c 1737 ENTER;
55497cff 1738
3280af22 1739 PL_lex_state = PL_sublex_info.super_state;
651b5b28 1740 SAVEBOOL(PL_lex_dojoin);
3280af22 1741 SAVEI32(PL_lex_brackets);
3280af22
NIS
1742 SAVEI32(PL_lex_casemods);
1743 SAVEI32(PL_lex_starts);
651b5b28 1744 SAVEI8(PL_lex_state);
7766f137 1745 SAVEVPTR(PL_lex_inpat);
98246f1e 1746 SAVEI16(PL_lex_inwhat);
57843af0 1747 SAVECOPLINE(PL_curcop);
3280af22 1748 SAVEPPTR(PL_bufptr);
8452ff4b 1749 SAVEPPTR(PL_bufend);
3280af22
NIS
1750 SAVEPPTR(PL_oldbufptr);
1751 SAVEPPTR(PL_oldoldbufptr);
207e3d1a
JH
1752 SAVEPPTR(PL_last_lop);
1753 SAVEPPTR(PL_last_uni);
3280af22
NIS
1754 SAVEPPTR(PL_linestart);
1755 SAVESPTR(PL_linestr);
8edd5f42
RGS
1756 SAVEGENERICPV(PL_lex_brackstack);
1757 SAVEGENERICPV(PL_lex_casestack);
3280af22
NIS
1758
1759 PL_linestr = PL_lex_stuff;
a0714e2c 1760 PL_lex_stuff = NULL;
3280af22 1761
9cbb5ea2
GS
1762 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart
1763 = SvPVX(PL_linestr);
3280af22 1764 PL_bufend += SvCUR(PL_linestr);
bd61b366 1765 PL_last_lop = PL_last_uni = NULL;
3280af22
NIS
1766 SAVEFREESV(PL_linestr);
1767
1768 PL_lex_dojoin = FALSE;
1769 PL_lex_brackets = 0;
a02a5408
JC
1770 Newx(PL_lex_brackstack, 120, char);
1771 Newx(PL_lex_casestack, 12, char);
3280af22
NIS
1772 PL_lex_casemods = 0;
1773 *PL_lex_casestack = '\0';
1774 PL_lex_starts = 0;
1775 PL_lex_state = LEX_INTERPCONCAT;
eb160463 1776 CopLINE_set(PL_curcop, (line_t)PL_multi_start);
3280af22
NIS
1777
1778 PL_lex_inwhat = PL_sublex_info.sub_inwhat;
1779 if (PL_lex_inwhat == OP_MATCH || PL_lex_inwhat == OP_QR || PL_lex_inwhat == OP_SUBST)
1780 PL_lex_inpat = PL_sublex_info.sub_op;
79072805 1781 else
5f66b61c 1782 PL_lex_inpat = NULL;
79072805 1783
55497cff 1784 return '(';
79072805
LW
1785}
1786
ffb4593c
NT
1787/*
1788 * S_sublex_done
1789 * Restores lexer state after a S_sublex_push.
1790 */
1791
76e3520e 1792STATIC I32
cea2e8a9 1793S_sublex_done(pTHX)
79072805 1794{
27da23d5 1795 dVAR;
3280af22 1796 if (!PL_lex_starts++) {
396482e1 1797 SV * const sv = newSVpvs("");
9aa983d2
JH
1798 if (SvUTF8(PL_linestr))
1799 SvUTF8_on(sv);
3280af22 1800 PL_expect = XOPERATOR;
6154021b 1801 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
79072805
LW
1802 return THING;
1803 }
1804
3280af22
NIS
1805 if (PL_lex_casemods) { /* oops, we've got some unbalanced parens */
1806 PL_lex_state = LEX_INTERPCASEMOD;
cea2e8a9 1807 return yylex();
79072805
LW
1808 }
1809
ffb4593c 1810 /* Is there a right-hand side to take care of? (s//RHS/ or tr//RHS/) */
3280af22
NIS
1811 if (PL_lex_repl && (PL_lex_inwhat == OP_SUBST || PL_lex_inwhat == OP_TRANS)) {
1812 PL_linestr = PL_lex_repl;
1813 PL_lex_inpat = 0;
1814 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
1815 PL_bufend += SvCUR(PL_linestr);
bd61b366 1816 PL_last_lop = PL_last_uni = NULL;
3280af22
NIS
1817 SAVEFREESV(PL_linestr);
1818 PL_lex_dojoin = FALSE;
1819 PL_lex_brackets = 0;
3280af22
NIS
1820 PL_lex_casemods = 0;
1821 *PL_lex_casestack = '\0';
1822 PL_lex_starts = 0;
25da4f38 1823 if (SvEVALED(PL_lex_repl)) {
3280af22
NIS
1824 PL_lex_state = LEX_INTERPNORMAL;
1825 PL_lex_starts++;
e9fa98b2
HS
1826 /* we don't clear PL_lex_repl here, so that we can check later
1827 whether this is an evalled subst; that means we rely on the
1828 logic to ensure sublex_done() is called again only via the
1829 branch (in yylex()) that clears PL_lex_repl, else we'll loop */
79072805 1830 }
e9fa98b2 1831 else {
3280af22 1832 PL_lex_state = LEX_INTERPCONCAT;
a0714e2c 1833 PL_lex_repl = NULL;
e9fa98b2 1834 }
79072805 1835 return ',';
ffed7fef
LW
1836 }
1837 else {
5db06880
NC
1838#ifdef PERL_MAD
1839 if (PL_madskills) {
cd81e915
NC
1840 if (PL_thiswhite) {
1841 if (!PL_endwhite)
6b29d1f5 1842 PL_endwhite = newSVpvs("");
cd81e915
NC
1843 sv_catsv(PL_endwhite, PL_thiswhite);
1844 PL_thiswhite = 0;
1845 }
1846 if (PL_thistoken)
76f68e9b 1847 sv_setpvs(PL_thistoken,"");
5db06880 1848 else
cd81e915 1849 PL_realtokenstart = -1;
5db06880
NC
1850 }
1851#endif
f46d017c 1852 LEAVE;
3280af22
NIS
1853 PL_bufend = SvPVX(PL_linestr);
1854 PL_bufend += SvCUR(PL_linestr);
1855 PL_expect = XOPERATOR;
09bef843 1856 PL_sublex_info.sub_inwhat = 0;
79072805 1857 return ')';
ffed7fef
LW
1858 }
1859}
1860
02aa26ce
NT
1861/*
1862 scan_const
1863
1864 Extracts a pattern, double-quoted string, or transliteration. This
1865 is terrifying code.
1866
94def140 1867 It looks at PL_lex_inwhat and PL_lex_inpat to find out whether it's
3280af22 1868 processing a pattern (PL_lex_inpat is true), a transliteration
94def140 1869 (PL_lex_inwhat == OP_TRANS is true), or a double-quoted string.
02aa26ce 1870
94def140
ST
1871 Returns a pointer to the character scanned up to. If this is
1872 advanced from the start pointer supplied (i.e. if anything was
9b599b2a 1873 successfully parsed), will leave an OP for the substring scanned
6154021b 1874 in pl_yylval. Caller must intuit reason for not parsing further
9b599b2a
GS
1875 by looking at the next characters herself.
1876
02aa26ce
NT
1877 In patterns:
1878 backslashes:
1879 double-quoted style: \r and \n
1880 regexp special ones: \D \s
94def140
ST
1881 constants: \x31
1882 backrefs: \1
02aa26ce
NT
1883 case and quoting: \U \Q \E
1884 stops on @ and $, but not for $ as tail anchor
1885
1886 In transliterations:
1887 characters are VERY literal, except for - not at the start or end
94def140
ST
1888 of the string, which indicates a range. If the range is in bytes,
1889 scan_const expands the range to the full set of intermediate
1890 characters. If the range is in utf8, the hyphen is replaced with
1891 a certain range mark which will be handled by pmtrans() in op.c.
02aa26ce
NT
1892
1893 In double-quoted strings:
1894 backslashes:
1895 double-quoted style: \r and \n
94def140
ST
1896 constants: \x31
1897 deprecated backrefs: \1 (in substitution replacements)
02aa26ce
NT
1898 case and quoting: \U \Q \E
1899 stops on @ and $
1900
1901 scan_const does *not* construct ops to handle interpolated strings.
1902 It stops processing as soon as it finds an embedded $ or @ variable
1903 and leaves it to the caller to work out what's going on.
1904
94def140
ST
1905 embedded arrays (whether in pattern or not) could be:
1906 @foo, @::foo, @'foo, @{foo}, @$foo, @+, @-.
1907
1908 $ in double-quoted strings must be the symbol of an embedded scalar.
02aa26ce
NT
1909
1910 $ in pattern could be $foo or could be tail anchor. Assumption:
1911 it's a tail anchor if $ is the last thing in the string, or if it's
94def140 1912 followed by one of "()| \r\n\t"
02aa26ce
NT
1913
1914 \1 (backreferences) are turned into $1
1915
1916 The structure of the code is
1917 while (there's a character to process) {
94def140
ST
1918 handle transliteration ranges
1919 skip regexp comments /(?#comment)/ and codes /(?{code})/
1920 skip #-initiated comments in //x patterns
1921 check for embedded arrays
02aa26ce
NT
1922 check for embedded scalars
1923 if (backslash) {
94def140
ST
1924 leave intact backslashes from leaveit (below)
1925 deprecate \1 in substitution replacements
02aa26ce
NT
1926 handle string-changing backslashes \l \U \Q \E, etc.
1927 switch (what was escaped) {
94def140
ST
1928 handle \- in a transliteration (becomes a literal -)
1929 handle \132 (octal characters)
1930 handle \x15 and \x{1234} (hex characters)
1931 handle \N{name} (named characters)
1932 handle \cV (control characters)
1933 handle printf-style backslashes (\f, \r, \n, etc)
02aa26ce 1934 } (end switch)
77a135fe 1935 continue
02aa26ce 1936 } (end if backslash)
77a135fe 1937 handle regular character
02aa26ce 1938 } (end while character to read)
4e553d73 1939
02aa26ce
NT
1940*/
1941
76e3520e 1942STATIC char *
cea2e8a9 1943S_scan_const(pTHX_ char *start)
79072805 1944{
97aff369 1945 dVAR;
3280af22 1946 register char *send = PL_bufend; /* end of the constant */
77a135fe
KW
1947 SV *sv = newSV(send - start); /* sv for the constant. See
1948 note below on sizing. */
02aa26ce
NT
1949 register char *s = start; /* start of the constant */
1950 register char *d = SvPVX(sv); /* destination for copies */
1951 bool dorange = FALSE; /* are we in a translit range? */
c2e66d9e 1952 bool didrange = FALSE; /* did we just finish a range? */
2b9d42f0 1953 I32 has_utf8 = FALSE; /* Output constant is UTF8 */
77a135fe
KW
1954 I32 this_utf8 = UTF; /* Is the source string assumed
1955 to be UTF8? But, this can
1956 show as true when the source
1957 isn't utf8, as for example
1958 when it is entirely composed
1959 of hex constants */
1960
1961 /* Note on sizing: The scanned constant is placed into sv, which is
1962 * initialized by newSV() assuming one byte of output for every byte of
1963 * input. This routine expects newSV() to allocate an extra byte for a
1964 * trailing NUL, which this routine will append if it gets to the end of
1965 * the input. There may be more bytes of input than output (eg., \N{LATIN
1966 * CAPITAL LETTER A}), or more output than input if the constant ends up
1967 * recoded to utf8, but each time a construct is found that might increase
1968 * the needed size, SvGROW() is called. Its size parameter each time is
1969 * based on the best guess estimate at the time, namely the length used so
1970 * far, plus the length the current construct will occupy, plus room for
1971 * the trailing NUL, plus one byte for every input byte still unscanned */
1972
012bcf8d 1973 UV uv;
4c3a8340
ST
1974#ifdef EBCDIC
1975 UV literal_endpoint = 0;
e294cc5d 1976 bool native_range = TRUE; /* turned to FALSE if the first endpoint is Unicode. */
4c3a8340 1977#endif
012bcf8d 1978
7918f24d
NC
1979 PERL_ARGS_ASSERT_SCAN_CONST;
1980
2b9d42f0
NIS
1981 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
1982 /* If we are doing a trans and we know we want UTF8 set expectation */
1983 has_utf8 = PL_sublex_info.sub_op->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF);
1984 this_utf8 = PL_sublex_info.sub_op->op_private & (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
1985 }
1986
1987
79072805 1988 while (s < send || dorange) {
02aa26ce 1989 /* get transliterations out of the way (they're most literal) */
3280af22 1990 if (PL_lex_inwhat == OP_TRANS) {
02aa26ce 1991 /* expand a range A-Z to the full set of characters. AIE! */
79072805 1992 if (dorange) {
1ba5c669
JH
1993 I32 i; /* current expanded character */
1994 I32 min; /* first character in range */
1995 I32 max; /* last character in range */
02aa26ce 1996
e294cc5d
JH
1997#ifdef EBCDIC
1998 UV uvmax = 0;
1999#endif
2000
2001 if (has_utf8
2002#ifdef EBCDIC
2003 && !native_range
2004#endif
2005 ) {
9d4ba2ae 2006 char * const c = (char*)utf8_hop((U8*)d, -1);
8973db79
JH
2007 char *e = d++;
2008 while (e-- > c)
2009 *(e + 1) = *e;
25716404 2010 *c = (char)UTF_TO_NATIVE(0xff);
8973db79
JH
2011 /* mark the range as done, and continue */
2012 dorange = FALSE;
2013 didrange = TRUE;
2014 continue;
2015 }
2b9d42f0 2016
95a20fc0 2017 i = d - SvPVX_const(sv); /* remember current offset */
e294cc5d
JH
2018#ifdef EBCDIC
2019 SvGROW(sv,
2020 SvLEN(sv) + (has_utf8 ?
2021 (512 - UTF_CONTINUATION_MARK +
2022 UNISKIP(0x100))
2023 : 256));
2024 /* How many two-byte within 0..255: 128 in UTF-8,
2025 * 96 in UTF-8-mod. */
2026#else
9cbb5ea2 2027 SvGROW(sv, SvLEN(sv) + 256); /* never more than 256 chars in a range */
e294cc5d 2028#endif
9cbb5ea2 2029 d = SvPVX(sv) + i; /* refresh d after realloc */
e294cc5d
JH
2030#ifdef EBCDIC
2031 if (has_utf8) {
2032 int j;
2033 for (j = 0; j <= 1; j++) {
2034 char * const c = (char*)utf8_hop((U8*)d, -1);
2035 const UV uv = utf8n_to_uvchr((U8*)c, d - c, NULL, 0);
2036 if (j)
2037 min = (U8)uv;
2038 else if (uv < 256)
2039 max = (U8)uv;
2040 else {
2041 max = (U8)0xff; /* only to \xff */
2042 uvmax = uv; /* \x{100} to uvmax */
2043 }
2044 d = c; /* eat endpoint chars */
2045 }
2046 }
2047 else {
2048#endif
2049 d -= 2; /* eat the first char and the - */
2050 min = (U8)*d; /* first char in range */
2051 max = (U8)d[1]; /* last char in range */
2052#ifdef EBCDIC
2053 }
2054#endif
8ada0baa 2055
c2e66d9e 2056 if (min > max) {
01ec43d0 2057 Perl_croak(aTHX_
d1573ac7 2058 "Invalid range \"%c-%c\" in transliteration operator",
1ba5c669 2059 (char)min, (char)max);
c2e66d9e
GS
2060 }
2061
c7f1f016 2062#ifdef EBCDIC
4c3a8340
ST
2063 if (literal_endpoint == 2 &&
2064 ((isLOWER(min) && isLOWER(max)) ||
2065 (isUPPER(min) && isUPPER(max)))) {
8ada0baa
JH
2066 if (isLOWER(min)) {
2067 for (i = min; i <= max; i++)
2068 if (isLOWER(i))
db42d148 2069 *d++ = NATIVE_TO_NEED(has_utf8,i);
8ada0baa
JH
2070 } else {
2071 for (i = min; i <= max; i++)
2072 if (isUPPER(i))
db42d148 2073 *d++ = NATIVE_TO_NEED(has_utf8,i);
8ada0baa
JH
2074 }
2075 }
2076 else
2077#endif
2078 for (i = min; i <= max; i++)
e294cc5d
JH
2079#ifdef EBCDIC
2080 if (has_utf8) {
2081 const U8 ch = (U8)NATIVE_TO_UTF(i);
2082 if (UNI_IS_INVARIANT(ch))
2083 *d++ = (U8)i;
2084 else {
2085 *d++ = (U8)UTF8_EIGHT_BIT_HI(ch);
2086 *d++ = (U8)UTF8_EIGHT_BIT_LO(ch);
2087 }
2088 }
2089 else
2090#endif
2091 *d++ = (char)i;
2092
2093#ifdef EBCDIC
2094 if (uvmax) {
2095 d = (char*)uvchr_to_utf8((U8*)d, 0x100);
2096 if (uvmax > 0x101)
2097 *d++ = (char)UTF_TO_NATIVE(0xff);
2098 if (uvmax > 0x100)
2099 d = (char*)uvchr_to_utf8((U8*)d, uvmax);
2100 }
2101#endif
02aa26ce
NT
2102
2103 /* mark the range as done, and continue */
79072805 2104 dorange = FALSE;
01ec43d0 2105 didrange = TRUE;
4c3a8340
ST
2106#ifdef EBCDIC
2107 literal_endpoint = 0;
2108#endif
79072805 2109 continue;
4e553d73 2110 }
02aa26ce
NT
2111
2112 /* range begins (ignore - as first or last char) */
79072805 2113 else if (*s == '-' && s+1 < send && s != start) {
4e553d73 2114 if (didrange) {
1fafa243 2115 Perl_croak(aTHX_ "Ambiguous range in transliteration operator");
01ec43d0 2116 }
e294cc5d
JH
2117 if (has_utf8
2118#ifdef EBCDIC
2119 && !native_range
2120#endif
2121 ) {
25716404 2122 *d++ = (char)UTF_TO_NATIVE(0xff); /* use illegal utf8 byte--see pmtrans */
a0ed51b3
LW
2123 s++;
2124 continue;
2125 }
79072805
LW
2126 dorange = TRUE;
2127 s++;
01ec43d0
GS
2128 }
2129 else {
2130 didrange = FALSE;
4c3a8340
ST
2131#ifdef EBCDIC
2132 literal_endpoint = 0;
e294cc5d 2133 native_range = TRUE;
4c3a8340 2134#endif
01ec43d0 2135 }
79072805 2136 }
02aa26ce
NT
2137
2138 /* if we get here, we're not doing a transliteration */
2139
0f5d15d6
IZ
2140 /* skip for regexp comments /(?#comment)/ and code /(?{code})/,
2141 except for the last char, which will be done separately. */
3280af22 2142 else if (*s == '(' && PL_lex_inpat && s[1] == '?') {
cc6b7395 2143 if (s[2] == '#') {
e994fd66 2144 while (s+1 < send && *s != ')')
db42d148 2145 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
155aba94
GS
2146 }
2147 else if (s[2] == '{' /* This should match regcomp.c */
67edc0c9 2148 || (s[2] == '?' && s[3] == '{'))
155aba94 2149 {
cc6b7395 2150 I32 count = 1;
0f5d15d6 2151 char *regparse = s + (s[2] == '{' ? 3 : 4);
cc6b7395
IZ
2152 char c;
2153
d9f97599
GS
2154 while (count && (c = *regparse)) {
2155 if (c == '\\' && regparse[1])
2156 regparse++;
4e553d73 2157 else if (c == '{')
cc6b7395 2158 count++;
4e553d73 2159 else if (c == '}')
cc6b7395 2160 count--;
d9f97599 2161 regparse++;
cc6b7395 2162 }
e994fd66 2163 if (*regparse != ')')
5bdf89e7 2164 regparse--; /* Leave one char for continuation. */
0f5d15d6 2165 while (s < regparse)
db42d148 2166 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
cc6b7395 2167 }
748a9306 2168 }
02aa26ce
NT
2169
2170 /* likewise skip #-initiated comments in //x patterns */
3280af22
NIS
2171 else if (*s == '#' && PL_lex_inpat &&
2172 ((PMOP*)PL_lex_inpat)->op_pmflags & PMf_EXTENDED) {
748a9306 2173 while (s+1 < send && *s != '\n')
db42d148 2174 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
748a9306 2175 }
02aa26ce 2176
5d1d4326 2177 /* check for embedded arrays
da6eedaa 2178 (@foo, @::foo, @'foo, @{foo}, @$foo, @+, @-)
5d1d4326 2179 */
1749ea0d
ST
2180 else if (*s == '@' && s[1]) {
2181 if (isALNUM_lazy_if(s+1,UTF))
2182 break;
2183 if (strchr(":'{$", s[1]))
2184 break;
2185 if (!PL_lex_inpat && (s[1] == '+' || s[1] == '-'))
2186 break; /* in regexp, neither @+ nor @- are interpolated */
2187 }
02aa26ce
NT
2188
2189 /* check for embedded scalars. only stop if we're sure it's a
2190 variable.
2191 */
79072805 2192 else if (*s == '$') {
3280af22 2193 if (!PL_lex_inpat) /* not a regexp, so $ must be var */
79072805 2194 break;
77772344
B
2195 if (s + 1 < send && !strchr("()| \r\n\t", s[1])) {
2196 if (s[1] == '\\' && ckWARN(WARN_AMBIGUOUS)) {
2197 Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS),
2198 "Possible unintended interpolation of $\\ in regex");
2199 }
79072805 2200 break; /* in regexp, $ might be tail anchor */
77772344 2201 }
79072805 2202 }
02aa26ce 2203
2b9d42f0
NIS
2204 /* End of else if chain - OP_TRANS rejoin rest */
2205
02aa26ce 2206 /* backslashes */
79072805
LW
2207 if (*s == '\\' && s+1 < send) {
2208 s++;
02aa26ce 2209
02aa26ce 2210 /* deprecate \1 in strings and substitution replacements */
3280af22 2211 if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat &&
a0d0e21e 2212 isDIGIT(*s) && *s != '0' && !isDIGIT(s[1]))
79072805 2213 {
599cee73 2214 if (ckWARN(WARN_SYNTAX))
9014280d 2215 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "\\%c better written as $%c", *s, *s);
79072805
LW
2216 *--s = '$';
2217 break;
2218 }
02aa26ce
NT
2219
2220 /* string-change backslash escapes */
3280af22 2221 if (PL_lex_inwhat != OP_TRANS && *s && strchr("lLuUEQ", *s)) {
79072805
LW
2222 --s;
2223 break;
2224 }
cc74c5bd
ST
2225 /* skip any other backslash escapes in a pattern */
2226 else if (PL_lex_inpat) {
2227 *d++ = NATIVE_TO_NEED(has_utf8,'\\');
2228 goto default_action;
2229 }
02aa26ce
NT
2230
2231 /* if we get here, it's either a quoted -, or a digit */
79072805 2232 switch (*s) {
02aa26ce
NT
2233
2234 /* quoted - in transliterations */
79072805 2235 case '-':
3280af22 2236 if (PL_lex_inwhat == OP_TRANS) {
79072805
LW
2237 *d++ = *s++;
2238 continue;
2239 }
2240 /* FALL THROUGH */
2241 default:
11b8faa4 2242 {
86f97054 2243 if ((isALPHA(*s) || isDIGIT(*s)) &&
041457d9 2244 ckWARN(WARN_MISC))
9014280d 2245 Perl_warner(aTHX_ packWARN(WARN_MISC),
e294cc5d
JH
2246 "Unrecognized escape \\%c passed through",
2247 *s);
11b8faa4 2248 /* default action is to copy the quoted character */
f9a63242 2249 goto default_action;
11b8faa4 2250 }
02aa26ce 2251
77a135fe 2252 /* eg. \132 indicates the octal constant 0x132 */
79072805
LW
2253 case '0': case '1': case '2': case '3':
2254 case '4': case '5': case '6': case '7':
ba210ebe 2255 {
53305cf1
NC
2256 I32 flags = 0;
2257 STRLEN len = 3;
77a135fe 2258 uv = NATIVE_TO_UNI(grok_oct(s, &len, &flags, NULL));
ba210ebe
JH
2259 s += len;
2260 }
012bcf8d 2261 goto NUM_ESCAPE_INSERT;
02aa26ce 2262
77a135fe 2263 /* eg. \x24 indicates the hex constant 0x24 */
79072805 2264 case 'x':
a0ed51b3
LW
2265 ++s;
2266 if (*s == '{') {
9d4ba2ae 2267 char* const e = strchr(s, '}');
a4c04bdc
NC
2268 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
2269 PERL_SCAN_DISALLOW_PREFIX;
53305cf1 2270 STRLEN len;
355860ce 2271
53305cf1 2272 ++s;
adaeee49 2273 if (!e) {
a0ed51b3 2274 yyerror("Missing right brace on \\x{}");
355860ce 2275 continue;
ba210ebe 2276 }
53305cf1 2277 len = e - s;
77a135fe 2278 uv = NATIVE_TO_UNI(grok_hex(s, &len, &flags, NULL));
ba210ebe 2279 s = e + 1;
a0ed51b3
LW
2280 }
2281 else {
ba210ebe 2282 {
53305cf1 2283 STRLEN len = 2;
a4c04bdc 2284 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
77a135fe 2285 uv = NATIVE_TO_UNI(grok_hex(s, &len, &flags, NULL));
ba210ebe
JH
2286 s += len;
2287 }
012bcf8d
GS
2288 }
2289
2290 NUM_ESCAPE_INSERT:
77a135fe
KW
2291 /* Insert oct, hex, or \N{U+...} escaped character. There will
2292 * always be enough room in sv since such escapes will be
2293 * longer than any UTF-8 sequence they can end up as, except if
2294 * they force us to recode the rest of the string into utf8 */
ba7cea30 2295
77a135fe
KW
2296 /* Here uv is the ordinal of the next character being added in
2297 * unicode (converted from native). (It has to be done before
2298 * here because \N is interpreted as unicode, and oct and hex
2299 * as native.) */
2300 if (!UNI_IS_INVARIANT(uv)) {
9aa983d2 2301 if (!has_utf8 && uv > 255) {
77a135fe
KW
2302 /* Might need to recode whatever we have accumulated so
2303 * far if it contains any chars variant in utf8 or
2304 * utf-ebcdic. */
2305
2306 SvCUR_set(sv, d - SvPVX_const(sv));
2307 SvPOK_on(sv);
2308 *d = '\0';
2309 sv_utf8_upgrade(sv);
2310 /* See Note on sizing above. */
2311 SvGROW(sv, SvCUR(sv) + UNISKIP(uv) + (STRLEN)(send - s) + 1);
2312 d = SvPVX(sv) + SvCUR(sv);
2313 has_utf8 = TRUE;
012bcf8d
GS
2314 }
2315
77a135fe
KW
2316 if (has_utf8) {
2317 d = (char*)uvuni_to_utf8((U8*)d, uv);
f9a63242
JH
2318 if (PL_lex_inwhat == OP_TRANS &&
2319 PL_sublex_info.sub_op) {
2320 PL_sublex_info.sub_op->op_private |=
2321 (PL_lex_repl ? OPpTRANS_FROM_UTF
2322 : OPpTRANS_TO_UTF);
f9a63242 2323 }
e294cc5d
JH
2324#ifdef EBCDIC
2325 if (uv > 255 && !dorange)
2326 native_range = FALSE;
2327#endif
012bcf8d 2328 }
a0ed51b3 2329 else {
012bcf8d 2330 *d++ = (char)uv;
a0ed51b3 2331 }
012bcf8d
GS
2332 }
2333 else {
c4d5f83a 2334 *d++ = (char) uv;
a0ed51b3 2335 }
79072805 2336 continue;
02aa26ce 2337
77a135fe
KW
2338 /* \N{LATIN SMALL LETTER A} is a named character, and so is
2339 * \N{U+0041} */
4a2d328f 2340 case 'N':
55eda711 2341 ++s;
423cee85
JH
2342 if (*s == '{') {
2343 char* e = strchr(s, '}');
155aba94 2344 SV *res;
423cee85 2345 STRLEN len;
cfd0369c 2346 const char *str;
4e553d73 2347
423cee85 2348 if (!e) {
5777a3f7 2349 yyerror("Missing right brace on \\N{}");
423cee85
JH
2350 e = s - 1;
2351 goto cont_scan;
2352 }
dbc0d4f2 2353 if (e > s + 2 && s[1] == 'U' && s[2] == '+') {
77a135fe
KW
2354 /* \N{U+...} The ... is a unicode value even on EBCDIC
2355 * machines */
dbc0d4f2
JH
2356 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
2357 PERL_SCAN_DISALLOW_PREFIX;
2358 s += 3;
2359 len = e - s;
2360 uv = grok_hex(s, &len, &flags, NULL);
b57a0404
JH
2361 if ( e > s && len != (STRLEN)(e - s) ) {
2362 uv = 0xFFFD;
fc8cd66c 2363 }
dbc0d4f2
JH
2364 s = e + 1;
2365 goto NUM_ESCAPE_INSERT;
2366 }
55eda711 2367 res = newSVpvn(s + 1, e - s - 1);
bd61b366 2368 res = new_constant( NULL, 0, "charnames",
eb0d8d16 2369 res, NULL, s - 2, e - s + 3 );
f9a63242
JH
2370 if (has_utf8)
2371 sv_utf8_upgrade(res);
cfd0369c 2372 str = SvPV_const(res,len);
1c47067b
JH
2373#ifdef EBCDIC_NEVER_MIND
2374 /* charnames uses pack U and that has been
2375 * recently changed to do the below uni->native
2376 * mapping, so this would be redundant (and wrong,
2377 * the code point would be doubly converted).
2378 * But leave this in just in case the pack U change
2379 * gets revoked, but the semantics is still
2380 * desireable for charnames. --jhi */
cddc7ef4 2381 {
cfd0369c 2382 UV uv = utf8_to_uvchr((const U8*)str, 0);
cddc7ef4
JH
2383
2384 if (uv < 0x100) {
89ebb4a3 2385 U8 tmpbuf[UTF8_MAXBYTES+1], *d;
cddc7ef4
JH
2386
2387 d = uvchr_to_utf8(tmpbuf, UNI_TO_NATIVE(uv));
2388 sv_setpvn(res, (char *)tmpbuf, d - tmpbuf);
cfd0369c 2389 str = SvPV_const(res, len);
cddc7ef4
JH
2390 }
2391 }
2392#endif
77a135fe
KW
2393 /* If destination is not in utf8 but this new character is,
2394 * recode the dest to utf8 */
89491803 2395 if (!has_utf8 && SvUTF8(res)) {
77a135fe 2396 SvCUR_set(sv, d - SvPVX_const(sv));
f08d6ad9 2397 SvPOK_on(sv);
e4f3eed8 2398 *d = '\0';
f08d6ad9 2399 sv_utf8_upgrade(sv);
77a135fe
KW
2400 /* See Note on sizing above. */
2401 SvGROW(sv, SvCUR(sv) + len + (STRLEN)(send - s) + 1);
f08d6ad9 2402 d = SvPVX(sv) + SvCUR(sv);
89491803 2403 has_utf8 = TRUE;
77a135fe 2404 } else if (len > (STRLEN)(e - s + 4)) { /* I _guess_ 4 is \N{} --jhi */
423cee85 2405
77a135fe
KW
2406 /* See Note on sizing above. (NOTE: SvCUR() is not set
2407 * correctly here). */
2408 const STRLEN off = d - SvPVX_const(sv);
2409 d = SvGROW(sv, off + len + (STRLEN)(send - s) + 1) + off;
423cee85 2410 }
e294cc5d
JH
2411#ifdef EBCDIC
2412 if (!dorange)
2413 native_range = FALSE; /* \N{} is guessed to be Unicode */
2414#endif
423cee85
JH
2415 Copy(str, d, len, char);
2416 d += len;
2417 SvREFCNT_dec(res);
2418 cont_scan:
2419 s = e + 1;
2420 }
2421 else
5777a3f7 2422 yyerror("Missing braces on \\N{}");
423cee85
JH
2423 continue;
2424
02aa26ce 2425 /* \c is a control character */
79072805
LW
2426 case 'c':
2427 s++;
961ce445 2428 if (s < send) {
ba210ebe 2429 U8 c = *s++;
c7f1f016
NIS
2430#ifdef EBCDIC
2431 if (isLOWER(c))
2432 c = toUPPER(c);
2433#endif
db42d148 2434 *d++ = NATIVE_TO_NEED(has_utf8,toCTRL(c));
ba210ebe 2435 }
961ce445
RGS
2436 else {
2437 yyerror("Missing control char name in \\c");
2438 }
79072805 2439 continue;
02aa26ce
NT
2440
2441 /* printf-style backslashes, formfeeds, newlines, etc */
79072805 2442 case 'b':
db42d148 2443 *d++ = NATIVE_TO_NEED(has_utf8,'\b');
79072805
LW
2444 break;
2445 case 'n':
db42d148 2446 *d++ = NATIVE_TO_NEED(has_utf8,'\n');
79072805
LW
2447 break;
2448 case 'r':
db42d148 2449 *d++ = NATIVE_TO_NEED(has_utf8,'\r');
79072805
LW
2450 break;
2451 case 'f':
db42d148 2452 *d++ = NATIVE_TO_NEED(has_utf8,'\f');
79072805
LW
2453 break;
2454 case 't':
db42d148 2455 *d++ = NATIVE_TO_NEED(has_utf8,'\t');
79072805 2456 break;
34a3fe2a 2457 case 'e':
db42d148 2458 *d++ = ASCII_TO_NEED(has_utf8,'\033');
34a3fe2a
PP
2459 break;
2460 case 'a':
db42d148 2461 *d++ = ASCII_TO_NEED(has_utf8,'\007');
79072805 2462 break;
02aa26ce
NT
2463 } /* end switch */
2464
79072805
LW
2465 s++;
2466 continue;
02aa26ce 2467 } /* end if (backslash) */
4c3a8340
ST
2468#ifdef EBCDIC
2469 else
2470 literal_endpoint++;
2471#endif
02aa26ce 2472
f9a63242 2473 default_action:
77a135fe
KW
2474 /* If we started with encoded form, or already know we want it,
2475 then encode the next character */
2476 if (! NATIVE_IS_INVARIANT((U8)(*s)) && (this_utf8 || has_utf8)) {
2b9d42f0 2477 STRLEN len = 1;
77a135fe
KW
2478
2479
2480 /* One might think that it is wasted effort in the case of the
2481 * source being utf8 (this_utf8 == TRUE) to take the next character
2482 * in the source, convert it to an unsigned value, and then convert
2483 * it back again. But the source has not been validated here. The
2484 * routine that does the conversion checks for errors like
2485 * malformed utf8 */
2486
5f66b61c
AL
2487 const UV nextuv = (this_utf8) ? utf8n_to_uvchr((U8*)s, send - s, &len, 0) : (UV) ((U8) *s);
2488 const STRLEN need = UNISKIP(NATIVE_TO_UNI(nextuv));
77a135fe
KW
2489 if (!has_utf8) {
2490 SvCUR_set(sv, d - SvPVX_const(sv));
2491 SvPOK_on(sv);
2492 *d = '\0';
2493 sv_utf8_upgrade(sv);
2494
2495 /* See Note on sizing above. */
2496 SvGROW(sv, SvCUR(sv) + need + (STRLEN)(send - s) + 1);
2497 d = SvPVX(sv) + SvCUR(sv);
2498 has_utf8 = TRUE;
2499 } else if (need > len) {
2500 /* encoded value larger than old, may need extra space (NOTE:
2501 * SvCUR() is not set correctly here). See Note on sizing
2502 * above. */
9d4ba2ae 2503 const STRLEN off = d - SvPVX_const(sv);
77a135fe 2504 d = SvGROW(sv, off + need + (STRLEN)(send - s) + 1) + off;
2b9d42f0 2505 }
77a135fe
KW
2506 s += len;
2507
5f66b61c 2508 d = (char*)uvchr_to_utf8((U8*)d, nextuv);
e294cc5d
JH
2509#ifdef EBCDIC
2510 if (uv > 255 && !dorange)
2511 native_range = FALSE;
2512#endif
2b9d42f0
NIS
2513 }
2514 else {
2515 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
2516 }
02aa26ce
NT
2517 } /* while loop to process each character */
2518
2519 /* terminate the string and set up the sv */
79072805 2520 *d = '\0';
95a20fc0 2521 SvCUR_set(sv, d - SvPVX_const(sv));
2b9d42f0 2522 if (SvCUR(sv) >= SvLEN(sv))
d0063567 2523 Perl_croak(aTHX_ "panic: constant overflowed allocated space");
2b9d42f0 2524
79072805 2525 SvPOK_on(sv);
9f4817db 2526 if (PL_encoding && !has_utf8) {
d0063567
DK
2527 sv_recode_to_utf8(sv, PL_encoding);
2528 if (SvUTF8(sv))
2529 has_utf8 = TRUE;
9f4817db 2530 }
2b9d42f0 2531 if (has_utf8) {
7e2040f0 2532 SvUTF8_on(sv);
2b9d42f0 2533 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
d0063567 2534 PL_sublex_info.sub_op->op_private |=
2b9d42f0
NIS
2535 (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
2536 }
2537 }
79072805 2538
02aa26ce 2539 /* shrink the sv if we allocated more than we used */
79072805 2540 if (SvCUR(sv) + 5 < SvLEN(sv)) {
1da4ca5f 2541 SvPV_shrink_to_cur(sv);
79072805 2542 }
02aa26ce 2543
6154021b 2544 /* return the substring (via pl_yylval) only if we parsed anything */
3280af22 2545 if (s > PL_bufptr) {
eb0d8d16
NC
2546 if ( PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ) ) {
2547 const char *const key = PL_lex_inpat ? "qr" : "q";
2548 const STRLEN keylen = PL_lex_inpat ? 2 : 1;
2549 const char *type;
2550 STRLEN typelen;
2551
2552 if (PL_lex_inwhat == OP_TRANS) {
2553 type = "tr";
2554 typelen = 2;
2555 } else if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat) {
2556 type = "s";
2557 typelen = 1;
2558 } else {
2559 type = "qq";
2560 typelen = 2;
2561 }
2562
2563 sv = S_new_constant(aTHX_ start, s - start, key, keylen, sv, NULL,
2564 type, typelen);
2565 }
6154021b 2566 pl_yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
b3ac6de7 2567 } else
8990e307 2568 SvREFCNT_dec(sv);
79072805
LW
2569 return s;
2570}
2571
ffb4593c
NT
2572/* S_intuit_more
2573 * Returns TRUE if there's more to the expression (e.g., a subscript),
2574 * FALSE otherwise.
ffb4593c
NT
2575 *
2576 * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
2577 *
2578 * ->[ and ->{ return TRUE
2579 * { and [ outside a pattern are always subscripts, so return TRUE
2580 * if we're outside a pattern and it's not { or [, then return FALSE
2581 * if we're in a pattern and the first char is a {
2582 * {4,5} (any digits around the comma) returns FALSE
2583 * if we're in a pattern and the first char is a [
2584 * [] returns FALSE
2585 * [SOMETHING] has a funky algorithm to decide whether it's a
2586 * character class or not. It has to deal with things like
2587 * /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
2588 * anything else returns TRUE
2589 */
2590
9cbb5ea2
GS
2591/* This is the one truly awful dwimmer necessary to conflate C and sed. */
2592
76e3520e 2593STATIC int
cea2e8a9 2594S_intuit_more(pTHX_ register char *s)
79072805 2595{
97aff369 2596 dVAR;
7918f24d
NC
2597
2598 PERL_ARGS_ASSERT_INTUIT_MORE;
2599
3280af22 2600 if (PL_lex_brackets)
79072805
LW
2601 return TRUE;
2602 if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
2603 return TRUE;
2604 if (*s != '{' && *s != '[')
2605 return FALSE;
3280af22 2606 if (!PL_lex_inpat)
79072805
LW
2607 return TRUE;
2608
2609 /* In a pattern, so maybe we have {n,m}. */
2610 if (*s == '{') {
2611 s++;
2612 if (!isDIGIT(*s))
2613 return TRUE;
2614 while (isDIGIT(*s))
2615 s++;
2616 if (*s == ',')
2617 s++;
2618 while (isDIGIT(*s))
2619 s++;
2620 if (*s == '}')
2621 return FALSE;
2622 return TRUE;
2623
2624 }
2625
2626 /* On the other hand, maybe we have a character class */
2627
2628 s++;
2629 if (*s == ']' || *s == '^')
2630 return FALSE;
2631 else {
ffb4593c 2632 /* this is terrifying, and it works */
79072805
LW
2633 int weight = 2; /* let's weigh the evidence */
2634 char seen[256];
f27ffc4a 2635 unsigned char un_char = 255, last_un_char;
9d4ba2ae 2636 const char * const send = strchr(s,']');
3280af22 2637 char tmpbuf[sizeof PL_tokenbuf * 4];
79072805
LW
2638
2639 if (!send) /* has to be an expression */
2640 return TRUE;
2641
2642 Zero(seen,256,char);
2643 if (*s == '$')
2644 weight -= 3;
2645 else if (isDIGIT(*s)) {
2646 if (s[1] != ']') {
2647 if (isDIGIT(s[1]) && s[2] == ']')
2648 weight -= 10;
2649 }
2650 else
2651 weight -= 100;
2652 }
2653 for (; s < send; s++) {
2654 last_un_char = un_char;
2655 un_char = (unsigned char)*s;
2656 switch (*s) {
2657 case '@':
2658 case '&':
2659 case '$':
2660 weight -= seen[un_char] * 10;
7e2040f0 2661 if (isALNUM_lazy_if(s+1,UTF)) {
90e5519e 2662 int len;
8903cb82 2663 scan_ident(s, send, tmpbuf, sizeof tmpbuf, FALSE);
90e5519e
NC
2664 len = (int)strlen(tmpbuf);
2665 if (len > 1 && gv_fetchpvn_flags(tmpbuf, len, 0, SVt_PV))
79072805
LW
2666 weight -= 100;
2667 else
2668 weight -= 10;
2669 }
2670 else if (*s == '$' && s[1] &&
93a17b20
LW
2671 strchr("[#!%*<>()-=",s[1])) {
2672 if (/*{*/ strchr("])} =",s[2]))
79072805
LW
2673 weight -= 10;
2674 else
2675 weight -= 1;
2676 }
2677 break;
2678 case '\\':
2679 un_char = 254;
2680 if (s[1]) {
93a17b20 2681 if (strchr("wds]",s[1]))
79072805 2682 weight += 100;
10edeb5d 2683 else if (seen[(U8)'\''] || seen[(U8)'"'])
79072805 2684 weight += 1;
93a17b20 2685 else if (strchr("rnftbxcav",s[1]))
79072805
LW
2686 weight += 40;
2687 else if (isDIGIT(s[1])) {
2688 weight += 40;
2689 while (s[1] && isDIGIT(s[1]))
2690 s++;
2691 }
2692 }
2693 else
2694 weight += 100;
2695 break;
2696 case '-':
2697 if (s[1] == '\\')
2698 weight += 50;
93a17b20 2699 if (strchr("aA01! ",last_un_char))
79072805 2700 weight += 30;
93a17b20 2701 if (strchr("zZ79~",s[1]))
79072805 2702 weight += 30;
f27ffc4a
GS
2703 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
2704 weight -= 5; /* cope with negative subscript */
79072805
LW
2705 break;
2706 default:
3792a11b
NC
2707 if (!isALNUM(last_un_char)
2708 && !(last_un_char == '$' || last_un_char == '@'
2709 || last_un_char == '&')
2710 && isALPHA(*s) && s[1] && isALPHA(s[1])) {
79072805
LW
2711 char *d = tmpbuf;
2712 while (isALPHA(*s))
2713 *d++ = *s++;
2714 *d = '\0';
5458a98a 2715 if (keyword(tmpbuf, d - tmpbuf, 0))
79072805
LW
2716 weight -= 150;
2717 }
2718 if (un_char == last_un_char + 1)
2719 weight += 5;
2720 weight -= seen[un_char];
2721 break;
2722 }
2723 seen[un_char]++;
2724 }
2725 if (weight >= 0) /* probably a character class */
2726 return FALSE;
2727 }
2728
2729 return TRUE;
2730}
ffed7fef 2731
ffb4593c
NT
2732/*
2733 * S_intuit_method
2734 *
2735 * Does all the checking to disambiguate
2736 * foo bar
2737 * between foo(bar) and bar->foo. Returns 0 if not a method, otherwise
2738 * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
2739 *
2740 * First argument is the stuff after the first token, e.g. "bar".
2741 *
2742 * Not a method if bar is a filehandle.
2743 * Not a method if foo is a subroutine prototyped to take a filehandle.
2744 * Not a method if it's really "Foo $bar"
2745 * Method if it's "foo $bar"
2746 * Not a method if it's really "print foo $bar"
2747 * Method if it's really "foo package::" (interpreted as package->foo)
8f8cf39c 2748 * Not a method if bar is known to be a subroutine ("sub bar; foo bar")
3cb0bbe5 2749 * Not a method if bar is a filehandle or package, but is quoted with
ffb4593c
NT
2750 * =>
2751 */
2752
76e3520e 2753STATIC int
62d55b22 2754S_intuit_method(pTHX_ char *start, GV *gv, CV *cv)
a0d0e21e 2755{
97aff369 2756 dVAR;
a0d0e21e 2757 char *s = start + (*start == '$');
3280af22 2758 char tmpbuf[sizeof PL_tokenbuf];
a0d0e21e
LW
2759 STRLEN len;
2760 GV* indirgv;
5db06880
NC
2761#ifdef PERL_MAD
2762 int soff;
2763#endif
a0d0e21e 2764
7918f24d
NC
2765 PERL_ARGS_ASSERT_INTUIT_METHOD;
2766
a0d0e21e 2767 if (gv) {
62d55b22 2768 if (SvTYPE(gv) == SVt_PVGV && GvIO(gv))
a0d0e21e 2769 return 0;
62d55b22
NC
2770 if (cv) {
2771 if (SvPOK(cv)) {
2772 const char *proto = SvPVX_const(cv);
2773 if (proto) {
2774 if (*proto == ';')
2775 proto++;
2776 if (*proto == '*')
2777 return 0;
2778 }
b6c543e3
IZ
2779 }
2780 } else
c35e046a 2781 gv = NULL;
a0d0e21e 2782 }
8903cb82 2783 s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
ffb4593c
NT
2784 /* start is the beginning of the possible filehandle/object,
2785 * and s is the end of it
2786 * tmpbuf is a copy of it
2787 */
2788
a0d0e21e 2789 if (*start == '$') {
3ef1310e
RGS
2790 if (gv || PL_last_lop_op == OP_PRINT || PL_last_lop_op == OP_SAY ||
2791 isUPPER(*PL_tokenbuf))
a0d0e21e 2792 return 0;
5db06880
NC
2793#ifdef PERL_MAD
2794 len = start - SvPVX(PL_linestr);
2795#endif
29595ff2 2796 s = PEEKSPACE(s);
f0092767 2797#ifdef PERL_MAD
5db06880
NC
2798 start = SvPVX(PL_linestr) + len;
2799#endif
3280af22
NIS
2800 PL_bufptr = start;
2801 PL_expect = XREF;
a0d0e21e
LW
2802 return *s == '(' ? FUNCMETH : METHOD;
2803 }
5458a98a 2804 if (!keyword(tmpbuf, len, 0)) {
c3e0f903
GS
2805 if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
2806 len -= 2;
2807 tmpbuf[len] = '\0';
5db06880
NC
2808#ifdef PERL_MAD
2809 soff = s - SvPVX(PL_linestr);
2810#endif
c3e0f903
GS
2811 goto bare_package;
2812 }
90e5519e 2813 indirgv = gv_fetchpvn_flags(tmpbuf, len, 0, SVt_PVCV);
8ebc5c01 2814 if (indirgv && GvCVu(indirgv))
a0d0e21e
LW
2815 return 0;
2816 /* filehandle or package name makes it a method */
da51bb9b 2817 if (!gv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, 0)) {
5db06880
NC
2818#ifdef PERL_MAD
2819 soff = s - SvPVX(PL_linestr);
2820#endif
29595ff2 2821 s = PEEKSPACE(s);
3280af22 2822 if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
55497cff 2823 return 0; /* no assumptions -- "=>" quotes bearword */
c3e0f903 2824 bare_package:
cd81e915 2825 start_force(PL_curforce);
9ded7720 2826 NEXTVAL_NEXTTOKE.opval = (OP*)newSVOP(OP_CONST, 0,
79cb57f6 2827 newSVpvn(tmpbuf,len));
9ded7720 2828 NEXTVAL_NEXTTOKE.opval->op_private = OPpCONST_BARE;
5db06880
NC
2829 if (PL_madskills)
2830 curmad('X', newSVpvn(start,SvPVX(PL_linestr) + soff - start));
3280af22 2831 PL_expect = XTERM;
a0d0e21e 2832 force_next(WORD);
3280af22 2833 PL_bufptr = s;
5db06880
NC
2834#ifdef PERL_MAD
2835 PL_bufptr = SvPVX(PL_linestr) + soff; /* restart before space */
2836#endif
a0d0e21e
LW
2837 return *s == '(' ? FUNCMETH : METHOD;
2838 }
2839 }
2840 return 0;
2841}
2842
16d20bd9 2843/* Encoded script support. filter_add() effectively inserts a
4e553d73 2844 * 'pre-processing' function into the current source input stream.
16d20bd9
AD
2845 * Note that the filter function only applies to the current source file
2846 * (e.g., it will not affect files 'require'd or 'use'd by this one).
2847 *
2848 * The datasv parameter (which may be NULL) can be used to pass
2849 * private data to this instance of the filter. The filter function
2850 * can recover the SV using the FILTER_DATA macro and use it to
2851 * store private buffers and state information.
2852 *
2853 * The supplied datasv parameter is upgraded to a PVIO type
4755096e 2854 * and the IoDIRP/IoANY field is used to store the function pointer,
e0c19803 2855 * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
16d20bd9
AD
2856 * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
2857 * private use must be set using malloc'd pointers.
2858 */
16d20bd9
AD
2859
2860SV *
864dbfa3 2861Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
16d20bd9 2862{
97aff369 2863 dVAR;
f4c556ac 2864 if (!funcp)
a0714e2c 2865 return NULL;
f4c556ac 2866
5486870f
DM
2867 if (!PL_parser)
2868 return NULL;
2869
3280af22
NIS
2870 if (!PL_rsfp_filters)
2871 PL_rsfp_filters = newAV();
16d20bd9 2872 if (!datasv)
561b68a9 2873 datasv = newSV(0);
862a34c6 2874 SvUPGRADE(datasv, SVt_PVIO);
8141890a 2875 IoANY(datasv) = FPTR2DPTR(void *, funcp); /* stash funcp into spare field */
e0c19803 2876 IoFLAGS(datasv) |= IOf_FAKE_DIRP;
f4c556ac 2877 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
55662e27
JH
2878 FPTR2DPTR(void *, IoANY(datasv)),
2879 SvPV_nolen(datasv)));
3280af22
NIS
2880 av_unshift(PL_rsfp_filters, 1);
2881 av_store(PL_rsfp_filters, 0, datasv) ;
16d20bd9
AD
2882 return(datasv);
2883}
4e553d73 2884
16d20bd9
AD
2885
2886/* Delete most recently added instance of this filter function. */
a0d0e21e 2887void
864dbfa3 2888Perl_filter_del(pTHX_ filter_t funcp)
16d20bd9 2889{
97aff369 2890 dVAR;
e0c19803 2891 SV *datasv;
24801a4b 2892
7918f24d
NC
2893 PERL_ARGS_ASSERT_FILTER_DEL;
2894
33073adb 2895#ifdef DEBUGGING
55662e27
JH
2896 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p",
2897 FPTR2DPTR(void*, funcp)));
33073adb 2898#endif
5486870f 2899 if (!PL_parser || !PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
16d20bd9
AD
2900 return;
2901 /* if filter is on top of stack (usual case) just pop it off */
e0c19803 2902 datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
8141890a 2903 if (IoANY(datasv) == FPTR2DPTR(void *, funcp)) {
e0c19803 2904 IoFLAGS(datasv) &= ~IOf_FAKE_DIRP;
4755096e 2905 IoANY(datasv) = (void *)NULL;
3280af22 2906 sv_free(av_pop(PL_rsfp_filters));
e50aee73 2907
16d20bd9
AD
2908 return;
2909 }
2910 /* we need to search for the correct entry and clear it */
cea2e8a9 2911 Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
16d20bd9
AD
2912}
2913
2914
1de9afcd
RGS
2915/* Invoke the idxth filter function for the current rsfp. */
2916/* maxlen 0 = read one text line */
16d20bd9 2917I32
864dbfa3 2918Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
a0d0e21e 2919{
97aff369 2920 dVAR;
16d20bd9
AD
2921 filter_t funcp;
2922 SV *datasv = NULL;
f482118e
NC
2923 /* This API is bad. It should have been using unsigned int for maxlen.
2924 Not sure if we want to change the API, but if not we should sanity
2925 check the value here. */
39cd7a59
NC
2926 const unsigned int correct_length
2927 = maxlen < 0 ?
2928#ifdef PERL_MICRO
2929 0x7FFFFFFF
2930#else
2931 INT_MAX
2932#endif
2933 : maxlen;
e50aee73 2934
7918f24d
NC
2935 PERL_ARGS_ASSERT_FILTER_READ;
2936
5486870f 2937 if (!PL_parser || !PL_rsfp_filters)
16d20bd9 2938 return -1;
1de9afcd 2939 if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */
16d20bd9
AD
2940 /* Provide a default input filter to make life easy. */
2941 /* Note that we append to the line. This is handy. */
f4c556ac
GS
2942 DEBUG_P(PerlIO_printf(Perl_debug_log,
2943 "filter_read %d: from rsfp\n", idx));
f482118e 2944 if (correct_length) {
16d20bd9
AD
2945 /* Want a block */
2946 int len ;
f54cb97a 2947 const int old_len = SvCUR(buf_sv);
16d20bd9
AD
2948
2949 /* ensure buf_sv is large enough */
f482118e
NC
2950 SvGROW(buf_sv, (STRLEN)(old_len + correct_length)) ;
2951 if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len,
2952 correct_length)) <= 0) {
3280af22 2953 if (PerlIO_error(PL_rsfp))
37120919
AD
2954 return -1; /* error */
2955 else
2956 return 0 ; /* end of file */
2957 }
16d20bd9
AD
2958 SvCUR_set(buf_sv, old_len + len) ;
2959 } else {
2960 /* Want a line */
3280af22
NIS
2961 if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
2962 if (PerlIO_error(PL_rsfp))
37120919
AD
2963 return -1; /* error */
2964 else
2965 return 0 ; /* end of file */
2966 }
16d20bd9
AD
2967 }
2968 return SvCUR(buf_sv);
2969 }
2970 /* Skip this filter slot if filter has been deleted */
1de9afcd 2971 if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) {
f4c556ac
GS
2972 DEBUG_P(PerlIO_printf(Perl_debug_log,
2973 "filter_read %d: skipped (filter deleted)\n",
2974 idx));
f482118e 2975 return FILTER_READ(idx+1, buf_sv, correct_length); /* recurse */
16d20bd9
AD
2976 }
2977 /* Get function pointer hidden within datasv */
8141890a 2978 funcp = DPTR2FPTR(filter_t, IoANY(datasv));
f4c556ac
GS
2979 DEBUG_P(PerlIO_printf(Perl_debug_log,
2980 "filter_read %d: via function %p (%s)\n",
ca0270c4 2981 idx, (void*)datasv, SvPV_nolen_const(datasv)));
16d20bd9
AD
2982 /* Call function. The function is expected to */
2983 /* call "FILTER_READ(idx+1, buf_sv)" first. */
37120919 2984 /* Return: <0:error, =0:eof, >0:not eof */
f482118e 2985 return (*funcp)(aTHX_ idx, buf_sv, correct_length);
16d20bd9
AD
2986}
2987
76e3520e 2988STATIC char *
cea2e8a9 2989S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append)
16d20bd9 2990{
97aff369 2991 dVAR;
7918f24d
NC
2992
2993 PERL_ARGS_ASSERT_FILTER_GETS;
2994
c39cd008 2995#ifdef PERL_CR_FILTER
3280af22 2996 if (!PL_rsfp_filters) {
c39cd008 2997 filter_add(S_cr_textfilter,NULL);
a868473f
NIS
2998 }
2999#endif
3280af22 3000 if (PL_rsfp_filters) {
55497cff
PP
3001 if (!append)
3002 SvCUR_set(sv, 0); /* start with empty line */
16d20bd9
AD
3003 if (FILTER_READ(0, sv, 0) > 0)
3004 return ( SvPVX(sv) ) ;
3005 else
bd61b366 3006 return NULL ;
16d20bd9 3007 }
9d116dd7 3008 else
fd049845 3009 return (sv_gets(sv, fp, append));
a0d0e21e
LW
3010}
3011
01ec43d0 3012STATIC HV *
9bde8eb0 3013S_find_in_my_stash(pTHX_ const char *pkgname, STRLEN len)
def3634b 3014{
97aff369 3015 dVAR;
def3634b
GS
3016 GV *gv;
3017
7918f24d
NC
3018 PERL_ARGS_ASSERT_FIND_IN_MY_STASH;
3019
01ec43d0 3020 if (len == 11 && *pkgname == '_' && strEQ(pkgname, "__PACKAGE__"))
def3634b
GS
3021 return PL_curstash;
3022
3023 if (len > 2 &&
3024 (pkgname[len - 2] == ':' && pkgname[len - 1] == ':') &&
90e5519e 3025 (gv = gv_fetchpvn_flags(pkgname, len, 0, SVt_PVHV)))
01ec43d0
GS
3026 {
3027 return GvHV(gv); /* Foo:: */
def3634b
GS
3028 }
3029
3030 /* use constant CLASS => 'MyClass' */
c35e046a
AL
3031 gv = gv_fetchpvn_flags(pkgname, len, 0, SVt_PVCV);
3032 if (gv && GvCV(gv)) {
3033 SV * const sv = cv_const_sv(GvCV(gv));
3034 if (sv)
9bde8eb0 3035 pkgname = SvPV_const(sv, len);
def3634b
GS
3036 }
3037
9bde8eb0 3038 return gv_stashpvn(pkgname, len, 0);
def3634b 3039}
a0d0e21e 3040
e3f73d4e
RGS
3041/*
3042 * S_readpipe_override
3043 * Check whether readpipe() is overriden, and generates the appropriate
3044 * optree, provided sublex_start() is called afterwards.
3045 */
3046STATIC void
1d51329b 3047S_readpipe_override(pTHX)
e3f73d4e
RGS
3048{
3049 GV **gvp;
3050 GV *gv_readpipe = gv_fetchpvs("readpipe", GV_NOTQUAL, SVt_PVCV);
6154021b 3051 pl_yylval.ival = OP_BACKTICK;
e3f73d4e
RGS
3052 if ((gv_readpipe
3053 && GvCVu(gv_readpipe) && GvIMPORTED_CV(gv_readpipe))
3054 ||
3055 ((gvp = (GV**)hv_fetchs(PL_globalstash, "readpipe", FALSE))
d5e716f5 3056 && (gv_readpipe = *gvp) && isGV_with_GP(gv_readpipe)
e3f73d4e
RGS
3057 && GvCVu(gv_readpipe) && GvIMPORTED_CV(gv_readpipe)))
3058 {
3059 PL_lex_op = (OP*)newUNOP(OP_ENTERSUB, OPf_STACKED,
3060 append_elem(OP_LIST,
3061 newSVOP(OP_CONST, 0, &PL_sv_undef), /* value will be read later */
3062 newCVREF(0, newGVOP(OP_GV, 0, gv_readpipe))));
3063 }
e3f73d4e
RGS
3064}
3065
5db06880
NC
3066#ifdef PERL_MAD
3067 /*
3068 * Perl_madlex
3069 * The intent of this yylex wrapper is to minimize the changes to the
3070 * tokener when we aren't interested in collecting madprops. It remains
3071 * to be seen how successful this strategy will be...
3072 */
3073
3074int
3075Perl_madlex(pTHX)
3076{
3077 int optype;
3078 char *s = PL_bufptr;
3079
cd81e915
NC
3080 /* make sure PL_thiswhite is initialized */
3081 PL_thiswhite = 0;
3082 PL_thismad = 0;
5db06880 3083
cd81e915 3084 /* just do what yylex would do on pending identifier; leave PL_thiswhite alone */
5db06880
NC
3085 if (PL_pending_ident)
3086 return S_pending_ident(aTHX);
3087
3088 /* previous token ate up our whitespace? */
cd81e915
NC
3089 if (!PL_lasttoke && PL_nextwhite) {
3090 PL_thiswhite = PL_nextwhite;
3091 PL_nextwhite = 0;
5db06880
NC
3092 }
3093
3094 /* isolate the token, and figure out where it is without whitespace */
cd81e915
NC
3095 PL_realtokenstart = -1;
3096 PL_thistoken = 0;
5db06880
NC
3097 optype = yylex();
3098 s = PL_bufptr;
cd81e915 3099 assert(PL_curforce < 0);
5db06880 3100
cd81e915
NC
3101 if (!PL_thismad || PL_thismad->mad_key == '^') { /* not forced already? */
3102 if (!PL_thistoken) {
3103 if (PL_realtokenstart < 0 || !CopLINE(PL_curcop))
6b29d1f5 3104 PL_thistoken = newSVpvs("");
5db06880 3105 else {
c35e046a 3106 char * const tstart = SvPVX(PL_linestr) + PL_realtokenstart;
cd81e915 3107 PL_thistoken = newSVpvn(tstart, s - tstart);
5db06880
NC
3108 }
3109 }
cd81e915
NC
3110 if (PL_thismad) /* install head */
3111 CURMAD('X', PL_thistoken);
5db06880
NC
3112 }
3113
3114 /* last whitespace of a sublex? */
cd81e915
NC
3115 if (optype == ')' && PL_endwhite) {
3116 CURMAD('X', PL_endwhite);
5db06880
NC
3117 }
3118
cd81e915 3119 if (!PL_thismad) {
5db06880
NC
3120
3121 /* if no whitespace and we're at EOF, bail. Otherwise fake EOF below. */
cd81e915
NC
3122 if (!PL_thiswhite && !PL_endwhite && !optype) {
3123 sv_free(PL_thistoken);
3124 PL_thistoken = 0;
5db06880
NC
3125 return 0;
3126 }
3127
3128 /* put off final whitespace till peg */
3129 if (optype == ';' && !PL_rsfp) {
cd81e915
NC
3130 PL_nextwhite = PL_thiswhite;
3131 PL_thiswhite = 0;
5db06880 3132 }
cd81e915
NC
3133 else if (PL_thisopen) {
3134 CURMAD('q', PL_thisopen);
3135 if (PL_thistoken)
3136 sv_free(PL_thistoken);
3137 PL_thistoken = 0;
5db06880
NC
3138 }
3139 else {
3140 /* Store actual token text as madprop X */
cd81e915 3141 CURMAD('X', PL_thistoken);
5db06880
NC
3142 }
3143
cd81e915 3144 if (PL_thiswhite) {
5db06880 3145 /* add preceding whitespace as madprop _ */
cd81e915 3146 CURMAD('_', PL_thiswhite);
5db06880
NC
3147 }
3148
cd81e915 3149 if (PL_thisstuff) {
5db06880 3150 /* add quoted material as madprop = */
cd81e915 3151 CURMAD('=', PL_thisstuff);
5db06880
NC
3152 }
3153
cd81e915 3154 if (PL_thisclose) {
5db06880 3155 /* add terminating quote as madprop Q */
cd81e915 3156 CURMAD('Q', PL_thisclose);
5db06880
NC
3157 }
3158 }
3159
3160 /* special processing based on optype */
3161
3162 switch (optype) {
3163
3164 /* opval doesn't need a TOKEN since it can already store mp */
3165 case WORD:
3166 case METHOD:
3167 case FUNCMETH:
3168 case THING:
3169 case PMFUNC:
3170 case PRIVATEREF:
3171 case FUNC0SUB:
3172 case UNIOPSUB:
3173 case LSTOPSUB:
6154021b
RGS
3174 if (pl_yylval.opval)
3175 append_madprops(PL_thismad, pl_yylval.opval, 0);
cd81e915 3176 PL_thismad = 0;
5db06880
NC
3177 return optype;
3178
3179 /* fake EOF */
3180 case 0:
3181 optype = PEG;
cd81e915
NC
3182 if (PL_endwhite) {
3183 addmad(newMADsv('p', PL_endwhite), &PL_thismad, 0);
3184 PL_endwhite = 0;
5db06880
NC
3185 }
3186 break;
3187
3188 case ']':
3189 case '}':
cd81e915 3190 if (PL_faketokens)
5db06880
NC
3191 break;
3192 /* remember any fake bracket that lexer is about to discard */
3193 if (PL_lex_brackets == 1 &&
3194 ((expectation)PL_lex_brackstack[0] & XFAKEBRACK))
3195 {
3196 s = PL_bufptr;
3197 while (s < PL_bufend && (*s == ' ' || *s == '\t'))
3198 s++;
3199 if (*s == '}') {
cd81e915
NC
3200 PL_thiswhite = newSVpvn(PL_bufptr, ++s - PL_bufptr);
3201 addmad(newMADsv('#', PL_thiswhite), &PL_thismad, 0);
3202 PL_thiswhite = 0;
5db06880
NC
3203 PL_bufptr = s - 1;
3204 break; /* don't bother looking for trailing comment */
3205 }
3206 else
3207 s = PL_bufptr;
3208 }
3209 if (optype == ']')
3210 break;
3211 /* FALLTHROUGH */
3212
3213 /* attach a trailing comment to its statement instead of next token */
3214 case ';':
cd81e915 3215 if (PL_faketokens)
5db06880
NC
3216 break;
3217 if (PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == optype) {
3218 s = PL_bufptr;
3219 while (s < PL_bufend && (*s == ' ' || *s == '\t'))
3220 s++;
3221 if (*s == '\n' || *s == '#') {
3222 while (s < PL_bufend && *s != '\n')
3223 s++;
3224 if (s < PL_bufend)
3225 s++;
cd81e915
NC
3226 PL_thiswhite = newSVpvn(PL_bufptr, s - PL_bufptr);
3227 addmad(newMADsv('#', PL_thiswhite), &PL_thismad, 0);
3228 PL_thiswhite = 0;
5db06880
NC
3229 PL_bufptr = s;
3230 }
3231 }
3232 break;
3233
3234 /* pval */
3235 case LABEL:
3236 break;
3237
3238 /* ival */
3239 default:
3240 break;
3241
3242 }
3243
3244 /* Create new token struct. Note: opvals return early above. */
6154021b 3245 pl_yylval.tkval = newTOKEN(optype, pl_yylval, PL_thismad);
cd81e915 3246 PL_thismad = 0;
5db06880
NC
3247 return optype;
3248}
3249#endif
3250
468aa647 3251STATIC char *
cc6ed77d 3252S_tokenize_use(pTHX_ int is_use, char *s) {
97aff369 3253 dVAR;
7918f24d
NC
3254
3255 PERL_ARGS_ASSERT_TOKENIZE_USE;
3256
468aa647
RGS
3257 if (PL_expect != XSTATE)
3258 yyerror(Perl_form(aTHX_ "\"%s\" not allowed in expression",
3259 is_use ? "use" : "no"));
29595ff2 3260 s = SKIPSPACE1(s);
468aa647
RGS
3261 if (isDIGIT(*s) || (*s == 'v' && isDIGIT(s[1]))) {
3262 s = force_version(s, TRUE);
29595ff2 3263 if (*s == ';' || (s = SKIPSPACE1(s), *s == ';')) {
cd81e915 3264 start_force(PL_curforce);
9ded7720 3265 NEXTVAL_NEXTTOKE.opval = NULL;
468aa647
RGS
3266 force_next(WORD);
3267 }
3268 else if (*s == 'v') {
3269 s = force_word(s,WORD,FALSE,TRUE,FALSE);
3270 s = force_version(s, FALSE);
3271 }
3272 }
3273 else {
3274 s = force_word(s,WORD,FALSE,TRUE,FALSE);
3275 s = force_version(s, FALSE);
3276 }
6154021b 3277 pl_yylval.ival = is_use;
468aa647
RGS
3278 return s;
3279}
748a9306 3280#ifdef DEBUGGING
27da23d5 3281 static const char* const exp_name[] =
09bef843 3282 { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
27308ded 3283 "ATTRTERM", "TERMBLOCK", "TERMORDORDOR"
09bef843 3284 };
748a9306 3285#endif
463ee0b2 3286
02aa26ce
NT
3287/*
3288 yylex
3289
3290 Works out what to call the token just pulled out of the input
3291 stream. The yacc parser takes care of taking the ops we return and
3292 stitching them into a tree.
3293
3294 Returns:
3295 PRIVATEREF
3296
3297 Structure:
3298 if read an identifier
3299 if we're in a my declaration
3300 croak if they tried to say my($foo::bar)
3301 build the ops for a my() declaration
3302 if it's an access to a my() variable
3303 are we in a sort block?
3304 croak if my($a); $a <=> $b
3305 build ops for access to a my() variable
3306 if in a dq string, and they've said @foo and we can't find @foo
3307 croak
3308 build ops for a bareword
3309 if we already built the token before, use it.
3310*/
3311
20141f0e 3312
dba4d153
JH
3313#ifdef __SC__
3314#pragma segment Perl_yylex
3315#endif
dba4d153 3316int
dba4d153 3317Perl_yylex(pTHX)
20141f0e 3318{
97aff369 3319 dVAR;
3afc138a 3320 register char *s = PL_bufptr;
378cc40b 3321 register char *d;
463ee0b2 3322 STRLEN len;
aa7440fb 3323 bool bof = FALSE;
a687059c 3324
10edeb5d
JH
3325 /* orig_keyword, gvp, and gv are initialized here because
3326 * jump to the label just_a_word_zero can bypass their
3327 * initialization later. */
3328 I32 orig_keyword = 0;
3329 GV *gv = NULL;
3330 GV **gvp = NULL;
3331
bbf60fe6 3332 DEBUG_T( {
396482e1 3333 SV* tmp = newSVpvs("");
b6007c36
DM
3334 PerlIO_printf(Perl_debug_log, "### %"IVdf":LEX_%s/X%s %s\n",
3335 (IV)CopLINE(PL_curcop),
3336 lex_state_names[PL_lex_state],
3337 exp_name[PL_expect],
3338 pv_display(tmp, s, strlen(s), 0, 60));
3339 SvREFCNT_dec(tmp);
bbf60fe6 3340 } );
02aa26ce 3341 /* check if there's an identifier for us to look at */
ba979b31 3342 if (PL_pending_ident)
bbf60fe6 3343 return REPORT(S_pending_ident(aTHX));
bbce6d69 3344
02aa26ce
NT
3345 /* no identifier pending identification */
3346
3280af22 3347 switch (PL_lex_state) {
79072805
LW
3348#ifdef COMMENTARY
3349 case LEX_NORMAL: /* Some compilers will produce faster */
3350 case LEX_INTERPNORMAL: /* code if we comment these out. */
3351 break;
3352#endif
3353
09bef843 3354 /* when we've already built the next token, just pull it out of the queue */
79072805 3355 case LEX_KNOWNEXT:
5db06880
NC
3356#ifdef PERL_MAD
3357 PL_lasttoke--;
6154021b 3358 pl_yylval = PL_nexttoke[PL_lasttoke].next_val;
5db06880 3359 if (PL_madskills) {
cd81e915 3360 PL_thismad = PL_nexttoke[PL_lasttoke].next_mad;
5db06880 3361 PL_nexttoke[PL_lasttoke].next_mad = 0;
cd81e915 3362 if (PL_thismad && PL_thismad->mad_key == '_') {
daba3364 3363 PL_thiswhite = MUTABLE_SV(PL_thismad->mad_val);
cd81e915
NC
3364 PL_thismad->mad_val = 0;
3365 mad_free(PL_thismad);
3366 PL_thismad = 0;
5db06880
NC
3367 }
3368 }
3369 if (!PL_lasttoke) {
3370 PL_lex_state = PL_lex_defer;
3371 PL_expect = PL_lex_expect;
3372 PL_lex_defer = LEX_NORMAL;
3373 if (!PL_nexttoke[PL_lasttoke].next_type)
3374 return yylex();
3375 }
3376#else
3280af22 3377 PL_nexttoke--;
6154021b 3378 pl_yylval = PL_nextval[PL_nexttoke];
3280af22
NIS
3379 if (!PL_nexttoke) {
3380 PL_lex_state = PL_lex_defer;
3381 PL_expect = PL_lex_expect;
3382 PL_lex_defer = LEX_NORMAL;
463ee0b2 3383 }
5db06880
NC
3384#endif
3385#ifdef PERL_MAD
3386 /* FIXME - can these be merged? */
3387 return(PL_nexttoke[PL_lasttoke].next_type);
3388#else
bbf60fe6 3389 return REPORT(PL_nexttype[PL_nexttoke]);
5db06880 3390#endif
79072805 3391
02aa26ce 3392 /* interpolated case modifiers like \L \U, including \Q and \E.
3280af22 3393 when we get here, PL_bufptr is at the \
02aa26ce 3394 */
79072805
LW
3395 case LEX_INTERPCASEMOD:
3396#ifdef DEBUGGING
3280af22 3397 if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
cea2e8a9 3398 Perl_croak(aTHX_ "panic: INTERPCASEMOD");
79072805 3399#endif
02aa26ce 3400 /* handle \E or end of string */
3280af22 3401 if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
02aa26ce 3402 /* if at a \E */
3280af22 3403 if (PL_lex_casemods) {
f54cb97a 3404 const char oldmod = PL_lex_casestack[--PL_lex_casemods];
3280af22 3405 PL_lex_casestack[PL_lex_casemods] = '\0';
02aa26ce 3406
3792a11b
NC
3407 if (PL_bufptr != PL_bufend
3408 && (oldmod == 'L' || oldmod == 'U' || oldmod == 'Q')) {
3280af22
NIS
3409 PL_bufptr += 2;
3410 PL_lex_state = LEX_INTERPCONCAT;
5db06880
NC
3411#ifdef PERL_MAD
3412 if (PL_madskills)
6b29d1f5 3413 PL_thistoken = newSVpvs("\\E");
5db06880 3414#endif
a0d0e21e 3415 }
bbf60fe6 3416 return REPORT(')');
79072805 3417 }
5db06880
NC
3418#ifdef PERL_MAD
3419 while (PL_bufptr != PL_bufend &&
3420 PL_bufptr[0] == '\\' && PL_bufptr[1] == 'E') {
cd81e915 3421 if (!PL_thiswhite)
6b29d1f5 3422 PL_thiswhite = newSVpvs("");
cd81e915 3423 sv_catpvn(PL_thiswhite, PL_bufptr, 2);
5db06880
NC
3424 PL_bufptr += 2;
3425 }
3426#else
3280af22
NIS
3427 if (PL_bufptr != PL_bufend)
3428 PL_bufptr += 2;
5db06880 3429#endif
3280af22 3430 PL_lex_state = LEX_INTERPCONCAT;
cea2e8a9 3431 return yylex();
79072805
LW
3432 }
3433 else {
607df283 3434 DEBUG_T({ PerlIO_printf(Perl_debug_log,
b6007c36 3435 "### Saw case modifier\n"); });
3280af22 3436 s = PL_bufptr + 1;
6e909404 3437 if (s[1] == '\\' && s[2] == 'E') {
5db06880 3438#ifdef PERL_MAD
cd81e915 3439 if (!PL_thiswhite)
6b29d1f5 3440 PL_thiswhite = newSVpvs("");
cd81e915 3441 sv_catpvn(PL_thiswhite, PL_bufptr, 4);
5db06880 3442#endif
89122651 3443 PL_bufptr = s + 3;
6e909404
JH
3444 PL_lex_state = LEX_INTERPCONCAT;
3445 return yylex();
a0d0e21e 3446 }
6e909404 3447 else {
90771dc0 3448 I32 tmp;
5db06880
NC
3449 if (!PL_madskills) /* when just compiling don't need correct */
3450 if (strnEQ(s, "L\\u", 3) || strnEQ(s, "U\\l", 3))
3451 tmp = *s, *s = s[2], s[2] = (char)tmp; /* misordered... */
3792a11b 3452 if ((*s == 'L' || *s == 'U') &&
6e909404
JH
3453 (strchr(PL_lex_casestack, 'L') || strchr(PL_lex_casestack, 'U'))) {
3454 PL_lex_casestack[--PL_lex_casemods] = '\0';
bbf60fe6 3455 return REPORT(')');
6e909404
JH
3456 }
3457 if (PL_lex_casemods > 10)
3458 Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
3459 PL_lex_casestack[PL_lex_casemods++] = *s;
3460 PL_lex_casestack[PL_lex_casemods] = '\0';
3461 PL_lex_state = LEX_INTERPCONCAT;
cd81e915 3462 start_force(PL_curforce);
9ded7720 3463 NEXTVAL_NEXTTOKE.ival = 0;
6e909404 3464 force_next('(');
cd81e915 3465 start_force(PL_curforce);
6e909404 3466 if (*s == 'l')
9ded7720 3467 NEXTVAL_NEXTTOKE.ival = OP_LCFIRST;
6e909404 3468 else if (*s == 'u')
9ded7720 3469 NEXTVAL_NEXTTOKE.ival = OP_UCFIRST;
6e909404 3470 else if (*s == 'L')
9ded7720 3471 NEXTVAL_NEXTTOKE.ival = OP_LC;
6e909404 3472 else if (*s == 'U')
9ded7720 3473 NEXTVAL_NEXTTOKE.ival = OP_UC;
6e909404 3474 else if (*s == 'Q')
9ded7720 3475 NEXTVAL_NEXTTOKE.ival = OP_QUOTEMETA;
6e909404
JH
3476 else
3477 Perl_croak(aTHX_ "panic: yylex");
5db06880 3478 if (PL_madskills) {
a5849ce5
NC
3479 SV* const tmpsv = newSVpvs("\\ ");
3480 /* replace the space with the character we want to escape
3481 */
3482 SvPVX(tmpsv)[1] = *s;
5db06880
NC
3483 curmad('_', tmpsv);
3484 }
6e909404 3485 PL_bufptr = s + 1;
a0d0e21e 3486 }
79072805 3487 force_next(FUNC);
3280af22
NIS
3488 if (PL_lex_starts) {
3489 s = PL_bufptr;
3490 PL_lex_starts = 0;
5db06880
NC
3491#ifdef PERL_MAD
3492 if (PL_madskills) {
cd81e915
NC
3493 if (PL_thistoken)
3494 sv_free(PL_thistoken);
6b29d1f5 3495 PL_thistoken = newSVpvs("");
5db06880
NC
3496 }
3497#endif
131b3ad0
DM
3498 /* commas only at base level: /$a\Ub$c/ => ($a,uc(b.$c)) */
3499 if (PL_lex_casemods == 1 && PL_lex_inpat)
3500 OPERATOR(',');
3501 else
3502 Aop(OP_CONCAT);
79072805
LW
3503 }
3504 else
cea2e8a9 3505 return yylex();
79072805
LW
3506 }
3507
55497cff 3508 case LEX_INTERPPUSH:
bbf60fe6 3509 return REPORT(sublex_push());
55497cff 3510
79072805 3511 case LEX_INTERPSTART:
3280af22 3512 if (PL_bufptr == PL_bufend)
bbf60fe6 3513 return REPORT(sublex_done());
607df283 3514 DEBUG_T({ PerlIO_printf(Perl_debug_log,
b6007c36 3515 "### Interpolated variable\n"); });
3280af22
NIS
3516 PL_expect = XTERM;
3517 PL_lex_dojoin = (*PL_bufptr == '@');
3518 PL_lex_state = LEX_INTERPNORMAL;
3519 if (PL_lex_dojoin) {
cd81e915 3520 start_force(PL_curforce);
9ded7720 3521 NEXTVAL_NEXTTOKE.ival = 0;
79072805 3522 force_next(',');
cd81e915 3523 start_force(PL_curforce);
a0d0e21e 3524 force_ident("\"", '$');
cd81e915 3525 start_force(PL_curforce);
9ded7720 3526 NEXTVAL_NEXTTOKE.ival = 0;
79072805 3527 force_next('$');
cd81e915 3528 start_force(PL_curforce);
9ded7720 3529 NEXTVAL_NEXTTOKE.ival = 0;
79072805 3530 force_next('(');
cd81e915 3531 start_force(PL_curforce);
9ded7720 3532 NEXTVAL_NEXTTOKE.ival = OP_JOIN; /* emulate join($", ...) */
79072805
LW
3533 force_next(FUNC);
3534