This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use PerlMemShared for CopSTASHPV and CopFILE. MUCH harder than it sounds!
[perl5.git] / toke.c
CommitLineData
a0d0e21e 1/* toke.c
a687059c 2 *
bc89e66f 3 * Copyright (c) 1991-2001, Larry Wall
a687059c 4 *
d48672a2
LW
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
378cc40b 7 *
a0d0e21e
LW
8 */
9
10/*
11 * "It all comes from here, the stench and the peril." --Frodo
378cc40b
LW
12 */
13
9cbb5ea2
GS
14/*
15 * This file is the lexer for Perl. It's closely linked to the
4e553d73 16 * parser, perly.y.
ffb4593c
NT
17 *
18 * The main routine is yylex(), which returns the next token.
19 */
20
378cc40b 21#include "EXTERN.h"
864dbfa3 22#define PERL_IN_TOKE_C
378cc40b 23#include "perl.h"
378cc40b 24
d3b6f988
GS
25#define yychar PL_yychar
26#define yylval PL_yylval
27
fc36a67e 28static char ident_too_long[] = "Identifier too long";
8903cb82 29
acfe0abc 30static void restore_rsfp(pTHX_ void *f);
6e3aabd6 31#ifndef PERL_NO_UTF16_FILTER
acfe0abc
GS
32static I32 utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen);
33static I32 utf16rev_textfilter(pTHX_ int idx, SV *sv, int maxlen);
6e3aabd6 34#endif
51371543 35
9059aa12
LW
36#define XFAKEBRACK 128
37#define XENUMMASK 127
38
39e02b42
JH
39#ifdef USE_UTF8_SCRIPTS
40# define UTF (!IN_BYTES)
2b9d42f0 41#else
39e02b42
JH
42# ifdef EBCDIC /* For now 'use utf8' does not affect tokenizer on EBCDIC */
43# define UTF (PL_linestr && DO_UTF8(PL_linestr))
44# else
45# define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8))
46# endif
2b9d42f0 47#endif
a0ed51b3 48
61f0cdd9 49/* In variables named $^X, these are the legal values for X.
2b92dfce
GS
50 * 1999-02-27 mjd-perl-patch@plover.com */
51#define isCONTROLVAR(x) (isUPPER(x) || strchr("[\\]^_?", (x)))
52
bf4acbe4
GS
53/* On MacOS, respect nonbreaking spaces */
54#ifdef MACOS_TRADITIONAL
55#define SPACE_OR_TAB(c) ((c)==' '||(c)=='\312'||(c)=='\t')
56#else
57#define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t')
58#endif
59
ffb4593c
NT
60/* LEX_* are values for PL_lex_state, the state of the lexer.
61 * They are arranged oddly so that the guard on the switch statement
79072805
LW
62 * can get by with a single comparison (if the compiler is smart enough).
63 */
64
fb73857a
PP
65/* #define LEX_NOTPARSING 11 is done in perl.h. */
66
55497cff
PP
67#define LEX_NORMAL 10
68#define LEX_INTERPNORMAL 9
69#define LEX_INTERPCASEMOD 8
70#define LEX_INTERPPUSH 7
71#define LEX_INTERPSTART 6
72#define LEX_INTERPEND 5
73#define LEX_INTERPENDMAYBE 4
74#define LEX_INTERPCONCAT 3
75#define LEX_INTERPCONST 2
76#define LEX_FORMLINE 1
77#define LEX_KNOWNEXT 0
79072805 78
79072805
LW
79#ifdef ff_next
80#undef ff_next
d48672a2
LW
81#endif
82
a1a0e61e 83#ifdef USE_PURE_BISON
dba4d153
JH
84# ifndef YYMAXLEVEL
85# define YYMAXLEVEL 100
86# endif
20141f0e
RI
87YYSTYPE* yylval_pointer[YYMAXLEVEL];
88int* yychar_pointer[YYMAXLEVEL];
6f202aea 89int yyactlevel = -1;
22c35a8c
GS
90# undef yylval
91# undef yychar
20141f0e
RI
92# define yylval (*yylval_pointer[yyactlevel])
93# define yychar (*yychar_pointer[yyactlevel])
94# define PERL_YYLEX_PARAM yylval_pointer[yyactlevel],yychar_pointer[yyactlevel]
4e553d73 95# undef yylex
dba4d153 96# define yylex() Perl_yylex_r(aTHX_ yylval_pointer[yyactlevel],yychar_pointer[yyactlevel])
a1a0e61e
TD
97#endif
98
79072805 99#include "keywords.h"
fe14fcc3 100
ffb4593c
NT
101/* CLINE is a macro that ensures PL_copline has a sane value */
102
ae986130
LW
103#ifdef CLINE
104#undef CLINE
105#endif
57843af0 106#define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline))
3280af22 107
ffb4593c
NT
108/*
109 * Convenience functions to return different tokens and prime the
9cbb5ea2 110 * lexer for the next token. They all take an argument.
ffb4593c
NT
111 *
112 * TOKEN : generic token (used for '(', DOLSHARP, etc)
113 * OPERATOR : generic operator
114 * AOPERATOR : assignment operator
115 * PREBLOCK : beginning the block after an if, while, foreach, ...
116 * PRETERMBLOCK : beginning a non-code-defining {} block (eg, hash ref)
117 * PREREF : *EXPR where EXPR is not a simple identifier
118 * TERM : expression term
119 * LOOPX : loop exiting command (goto, last, dump, etc)
120 * FTST : file test operator
121 * FUN0 : zero-argument function
2d2e263d 122 * FUN1 : not used, except for not, which isn't a UNIOP
ffb4593c
NT
123 * BOop : bitwise or or xor
124 * BAop : bitwise and
125 * SHop : shift operator
126 * PWop : power operator
9cbb5ea2 127 * PMop : pattern-matching operator
ffb4593c
NT
128 * Aop : addition-level operator
129 * Mop : multiplication-level operator
130 * Eop : equality-testing operator
e5edeb50 131 * Rop : relational operator <= != gt
ffb4593c
NT
132 *
133 * Also see LOP and lop() below.
134 */
135
075953c3
JH
136/* Note that REPORT() and REPORT2() will be expressions that supply
137 * their own trailing comma, not suitable for statements as such. */
998054bd 138#ifdef DEBUGGING /* Serve -DT. */
075953c3
JH
139# define REPORT(x,retval) tokereport(x,s,(int)retval),
140# define REPORT2(x,retval) tokereport(x,s, yylval.ival),
998054bd 141#else
075953c3
JH
142# define REPORT(x,retval)
143# define REPORT2(x,retval)
998054bd
SC
144#endif
145
075953c3
JH
146#define TOKEN(retval) return (REPORT2("token",retval) PL_bufptr = s,(int)retval)
147#define OPERATOR(retval) return (REPORT2("operator",retval) PL_expect = XTERM, PL_bufptr = s,(int)retval)
148#define AOPERATOR(retval) return ao((REPORT2("aop",retval) PL_expect = XTERM, PL_bufptr = s,(int)retval))
149#define PREBLOCK(retval) return (REPORT2("preblock",retval) PL_expect = XBLOCK,PL_bufptr = s,(int)retval)
150#define PRETERMBLOCK(retval) return (REPORT2("pretermblock",retval) PL_expect = XTERMBLOCK,PL_bufptr = s,(int)retval)
151#define PREREF(retval) return (REPORT2("preref",retval) PL_expect = XREF,PL_bufptr = s,(int)retval)
152#define TERM(retval) return (CLINE, REPORT2("term",retval) PL_expect = XOPERATOR, PL_bufptr = s,(int)retval)
153#define LOOPX(f) return(yylval.ival=f, REPORT("loopx",f) PL_expect = XTERM,PL_bufptr = s,(int)LOOPEX)
154#define FTST(f) return(yylval.ival=f, REPORT("ftst",f) PL_expect = XTERM,PL_bufptr = s,(int)UNIOP)
155#define FUN0(f) return(yylval.ival = f, REPORT("fun0",f) PL_expect = XOPERATOR,PL_bufptr = s,(int)FUNC0)
156#define FUN1(f) return(yylval.ival = f, REPORT("fun1",f) PL_expect = XOPERATOR,PL_bufptr = s,(int)FUNC1)
157#define BOop(f) return ao((yylval.ival=f, REPORT("bitorop",f) PL_expect = XTERM,PL_bufptr = s,(int)BITOROP))
158#define BAop(f) return ao((yylval.ival=f, REPORT("bitandop",f) PL_expect = XTERM,PL_bufptr = s,(int)BITANDOP))
159#define SHop(f) return ao((yylval.ival=f, REPORT("shiftop",f) PL_expect = XTERM,PL_bufptr = s,(int)SHIFTOP))
160#define PWop(f) return ao((yylval.ival=f, REPORT("powop",f) PL_expect = XTERM,PL_bufptr = s,(int)POWOP))
161#define PMop(f) return(yylval.ival=f, REPORT("matchop",f) PL_expect = XTERM,PL_bufptr = s,(int)MATCHOP)
162#define Aop(f) return ao((yylval.ival=f, REPORT("add",f) PL_expect = XTERM,PL_bufptr = s,(int)ADDOP))
163#define Mop(f) return ao((yylval.ival=f, REPORT("mul",f) PL_expect = XTERM,PL_bufptr = s,(int)MULOP))
164#define Eop(f) return(yylval.ival=f, REPORT("eq",f) PL_expect = XTERM,PL_bufptr = s,(int)EQOP)
165#define Rop(f) return(yylval.ival=f, REPORT("rel",f) PL_expect = XTERM,PL_bufptr = s,(int)RELOP)
2f3197b3 166
a687059c
LW
167/* This bit of chicanery makes a unary function followed by
168 * a parenthesis into a function with one argument, highest precedence.
169 */
2f3197b3 170#define UNI(f) return(yylval.ival = f, \
075953c3 171 REPORT("uni",f) \
3280af22
NIS
172 PL_expect = XTERM, \
173 PL_bufptr = s, \
174 PL_last_uni = PL_oldbufptr, \
175 PL_last_lop_op = f, \
a687059c
LW
176 (*s == '(' || (s = skipspace(s), *s == '(') ? (int)FUNC1 : (int)UNIOP) )
177
79072805 178#define UNIBRACK(f) return(yylval.ival = f, \
075953c3 179 REPORT("uni",f) \
3280af22
NIS
180 PL_bufptr = s, \
181 PL_last_uni = PL_oldbufptr, \
79072805
LW
182 (*s == '(' || (s = skipspace(s), *s == '(') ? (int)FUNC1 : (int)UNIOP) )
183
9f68db38 184/* grandfather return to old style */
3280af22 185#define OLDLOP(f) return(yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)LSTOP)
79072805 186
8fa7f367
JH
187#ifdef DEBUGGING
188
2d00ba3b 189STATIC void
61b2116b 190S_tokereport(pTHX_ char *thing, char* s, I32 rv)
9041c2e3 191{
998054bd 192 DEBUG_T({
9c5ffd7c 193 SV* report = newSVpv(thing, 0);
29b291f7
RB
194 Perl_sv_catpvf(aTHX_ report, ":line %d:%"IVdf":", CopLINE(PL_curcop),
195 (IV)rv);
998054bd
SC
196
197 if (s - PL_bufptr > 0)
198 sv_catpvn(report, PL_bufptr, s - PL_bufptr);
199 else {
200 if (PL_oldbufptr && *PL_oldbufptr)
201 sv_catpv(report, PL_tokenbuf);
202 }
203 PerlIO_printf(Perl_debug_log, "### %s\n", SvPV_nolen(report));
5f80b19c 204 });
998054bd
SC
205}
206
8fa7f367
JH
207#endif
208
ffb4593c
NT
209/*
210 * S_ao
211 *
212 * This subroutine detects &&= and ||= and turns an ANDAND or OROR
213 * into an OP_ANDASSIGN or OP_ORASSIGN
214 */
215
76e3520e 216STATIC int
cea2e8a9 217S_ao(pTHX_ int toketype)
a0d0e21e 218{
3280af22
NIS
219 if (*PL_bufptr == '=') {
220 PL_bufptr++;
a0d0e21e
LW
221 if (toketype == ANDAND)
222 yylval.ival = OP_ANDASSIGN;
223 else if (toketype == OROR)
224 yylval.ival = OP_ORASSIGN;
225 toketype = ASSIGNOP;
226 }
227 return toketype;
228}
229
ffb4593c
NT
230/*
231 * S_no_op
232 * When Perl expects an operator and finds something else, no_op
233 * prints the warning. It always prints "<something> found where
234 * operator expected. It prints "Missing semicolon on previous line?"
235 * if the surprise occurs at the start of the line. "do you need to
236 * predeclare ..." is printed out for code like "sub bar; foo bar $x"
237 * where the compiler doesn't know if foo is a method call or a function.
238 * It prints "Missing operator before end of line" if there's nothing
239 * after the missing operator, or "... before <...>" if there is something
240 * after the missing operator.
241 */
242
76e3520e 243STATIC void
cea2e8a9 244S_no_op(pTHX_ char *what, char *s)
463ee0b2 245{
3280af22
NIS
246 char *oldbp = PL_bufptr;
247 bool is_first = (PL_oldbufptr == PL_linestart);
68dc0745 248
1189a94a
GS
249 if (!s)
250 s = oldbp;
07c798fb 251 else
1189a94a 252 PL_bufptr = s;
cea2e8a9 253 yywarn(Perl_form(aTHX_ "%s found where operator expected", what));
748a9306 254 if (is_first)
cea2e8a9 255 Perl_warn(aTHX_ "\t(Missing semicolon on previous line?)\n");
7e2040f0 256 else if (PL_oldoldbufptr && isIDFIRST_lazy_if(PL_oldoldbufptr,UTF)) {
748a9306 257 char *t;
7e2040f0 258 for (t = PL_oldoldbufptr; *t && (isALNUM_lazy_if(t,UTF) || *t == ':'); t++) ;
3280af22 259 if (t < PL_bufptr && isSPACE(*t))
cea2e8a9 260 Perl_warn(aTHX_ "\t(Do you need to predeclare %.*s?)\n",
3280af22 261 t - PL_oldoldbufptr, PL_oldoldbufptr);
748a9306 262 }
07c798fb
HS
263 else {
264 assert(s >= oldbp);
cea2e8a9 265 Perl_warn(aTHX_ "\t(Missing operator before %.*s?)\n", s - oldbp, oldbp);
07c798fb 266 }
3280af22 267 PL_bufptr = oldbp;
8990e307
LW
268}
269
ffb4593c
NT
270/*
271 * S_missingterm
272 * Complain about missing quote/regexp/heredoc terminator.
273 * If it's called with (char *)NULL then it cauterizes the line buffer.
274 * If we're in a delimited string and the delimiter is a control
275 * character, it's reformatted into a two-char sequence like ^C.
276 * This is fatal.
277 */
278
76e3520e 279STATIC void
cea2e8a9 280S_missingterm(pTHX_ char *s)
8990e307
LW
281{
282 char tmpbuf[3];
283 char q;
284 if (s) {
285 char *nl = strrchr(s,'\n');
d2719217 286 if (nl)
8990e307
LW
287 *nl = '\0';
288 }
9d116dd7
JH
289 else if (
290#ifdef EBCDIC
291 iscntrl(PL_multi_close)
292#else
293 PL_multi_close < 32 || PL_multi_close == 127
294#endif
295 ) {
8990e307 296 *tmpbuf = '^';
3280af22 297 tmpbuf[1] = toCTRL(PL_multi_close);
8990e307
LW
298 s = "\\n";
299 tmpbuf[2] = '\0';
300 s = tmpbuf;
301 }
302 else {
3280af22 303 *tmpbuf = PL_multi_close;
8990e307
LW
304 tmpbuf[1] = '\0';
305 s = tmpbuf;
306 }
307 q = strchr(s,'"') ? '\'' : '"';
cea2e8a9 308 Perl_croak(aTHX_ "Can't find string terminator %c%s%c anywhere before EOF",q,s,q);
463ee0b2 309}
79072805 310
ffb4593c
NT
311/*
312 * Perl_deprecate
ffb4593c
NT
313 */
314
79072805 315void
864dbfa3 316Perl_deprecate(pTHX_ char *s)
a0d0e21e 317{
599cee73 318 if (ckWARN(WARN_DEPRECATED))
cea2e8a9 319 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of %s is deprecated", s);
a0d0e21e
LW
320}
321
ffb4593c
NT
322/*
323 * depcom
9cbb5ea2 324 * Deprecate a comma-less variable list.
ffb4593c
NT
325 */
326
76e3520e 327STATIC void
cea2e8a9 328S_depcom(pTHX)
a0d0e21e
LW
329{
330 deprecate("comma-less variable list");
331}
332
ffb4593c 333/*
9cbb5ea2
GS
334 * experimental text filters for win32 carriage-returns, utf16-to-utf8 and
335 * utf16-to-utf8-reversed.
ffb4593c
NT
336 */
337
c39cd008
GS
338#ifdef PERL_CR_FILTER
339static void
340strip_return(SV *sv)
341{
342 register char *s = SvPVX(sv);
343 register char *e = s + SvCUR(sv);
344 /* outer loop optimized to do nothing if there are no CR-LFs */
345 while (s < e) {
346 if (*s++ == '\r' && *s == '\n') {
347 /* hit a CR-LF, need to copy the rest */
348 register char *d = s - 1;
349 *d++ = *s++;
350 while (s < e) {
351 if (*s == '\r' && s[1] == '\n')
352 s++;
353 *d++ = *s++;
354 }
355 SvCUR(sv) -= s - d;
356 return;
357 }
358 }
359}
a868473f 360
76e3520e 361STATIC I32
c39cd008 362S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
a868473f 363{
c39cd008
GS
364 I32 count = FILTER_READ(idx+1, sv, maxlen);
365 if (count > 0 && !maxlen)
366 strip_return(sv);
367 return count;
a868473f
NIS
368}
369#endif
370
ffb4593c
NT
371/*
372 * Perl_lex_start
9cbb5ea2
GS
373 * Initialize variables. Uses the Perl save_stack to save its state (for
374 * recursive calls to the parser).
ffb4593c
NT
375 */
376
a0d0e21e 377void
864dbfa3 378Perl_lex_start(pTHX_ SV *line)
79072805 379{
8990e307
LW
380 char *s;
381 STRLEN len;
382
3280af22
NIS
383 SAVEI32(PL_lex_dojoin);
384 SAVEI32(PL_lex_brackets);
3280af22
NIS
385 SAVEI32(PL_lex_casemods);
386 SAVEI32(PL_lex_starts);
387 SAVEI32(PL_lex_state);
7766f137 388 SAVEVPTR(PL_lex_inpat);
3280af22 389 SAVEI32(PL_lex_inwhat);
18b09519
GS
390 if (PL_lex_state == LEX_KNOWNEXT) {
391 I32 toke = PL_nexttoke;
392 while (--toke >= 0) {
393 SAVEI32(PL_nexttype[toke]);
394 SAVEVPTR(PL_nextval[toke]);
395 }
396 SAVEI32(PL_nexttoke);
18b09519 397 }
57843af0 398 SAVECOPLINE(PL_curcop);
3280af22
NIS
399 SAVEPPTR(PL_bufptr);
400 SAVEPPTR(PL_bufend);
401 SAVEPPTR(PL_oldbufptr);
402 SAVEPPTR(PL_oldoldbufptr);
207e3d1a
JH
403 SAVEPPTR(PL_last_lop);
404 SAVEPPTR(PL_last_uni);
3280af22
NIS
405 SAVEPPTR(PL_linestart);
406 SAVESPTR(PL_linestr);
407 SAVEPPTR(PL_lex_brackstack);
408 SAVEPPTR(PL_lex_casestack);
c76ac1ee 409 SAVEDESTRUCTOR_X(restore_rsfp, PL_rsfp);
3280af22
NIS
410 SAVESPTR(PL_lex_stuff);
411 SAVEI32(PL_lex_defer);
09bef843 412 SAVEI32(PL_sublex_info.sub_inwhat);
3280af22 413 SAVESPTR(PL_lex_repl);
bebdddfc
GS
414 SAVEINT(PL_expect);
415 SAVEINT(PL_lex_expect);
3280af22
NIS
416
417 PL_lex_state = LEX_NORMAL;
418 PL_lex_defer = 0;
419 PL_expect = XSTATE;
420 PL_lex_brackets = 0;
3280af22
NIS
421 New(899, PL_lex_brackstack, 120, char);
422 New(899, PL_lex_casestack, 12, char);
423 SAVEFREEPV(PL_lex_brackstack);
424 SAVEFREEPV(PL_lex_casestack);
425 PL_lex_casemods = 0;
426 *PL_lex_casestack = '\0';
427 PL_lex_dojoin = 0;
428 PL_lex_starts = 0;
429 PL_lex_stuff = Nullsv;
430 PL_lex_repl = Nullsv;
431 PL_lex_inpat = 0;
76be56bc 432 PL_nexttoke = 0;
3280af22 433 PL_lex_inwhat = 0;
09bef843 434 PL_sublex_info.sub_inwhat = 0;
3280af22
NIS
435 PL_linestr = line;
436 if (SvREADONLY(PL_linestr))
437 PL_linestr = sv_2mortal(newSVsv(PL_linestr));
438 s = SvPV(PL_linestr, len);
8990e307 439 if (len && s[len-1] != ';') {
3280af22
NIS
440 if (!(SvFLAGS(PL_linestr) & SVs_TEMP))
441 PL_linestr = sv_2mortal(newSVsv(PL_linestr));
442 sv_catpvn(PL_linestr, "\n;", 2);
8990e307 443 }
3280af22
NIS
444 SvTEMP_off(PL_linestr);
445 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = SvPVX(PL_linestr);
446 PL_bufend = PL_bufptr + SvCUR(PL_linestr);
207e3d1a 447 PL_last_lop = PL_last_uni = Nullch;
3280af22 448 PL_rsfp = 0;
79072805 449}
a687059c 450
ffb4593c
NT
451/*
452 * Perl_lex_end
9cbb5ea2
GS
453 * Finalizer for lexing operations. Must be called when the parser is
454 * done with the lexer.
ffb4593c
NT
455 */
456
463ee0b2 457void
864dbfa3 458Perl_lex_end(pTHX)
463ee0b2 459{
3280af22 460 PL_doextract = FALSE;
463ee0b2
LW
461}
462
ffb4593c
NT
463/*
464 * S_incline
465 * This subroutine has nothing to do with tilting, whether at windmills
466 * or pinball tables. Its name is short for "increment line". It
57843af0 467 * increments the current line number in CopLINE(PL_curcop) and checks
ffb4593c 468 * to see whether the line starts with a comment of the form
9cbb5ea2
GS
469 * # line 500 "foo.pm"
470 * If so, it sets the current line number and file to the values in the comment.
ffb4593c
NT
471 */
472
76e3520e 473STATIC void
cea2e8a9 474S_incline(pTHX_ char *s)
463ee0b2
LW
475{
476 char *t;
477 char *n;
73659bf1 478 char *e;
463ee0b2 479 char ch;
463ee0b2 480
57843af0 481 CopLINE_inc(PL_curcop);
463ee0b2
LW
482 if (*s++ != '#')
483 return;
bf4acbe4 484 while (SPACE_OR_TAB(*s)) s++;
73659bf1
GS
485 if (strnEQ(s, "line", 4))
486 s += 4;
487 else
488 return;
084592ab 489 if (SPACE_OR_TAB(*s))
73659bf1 490 s++;
4e553d73 491 else
73659bf1 492 return;
bf4acbe4 493 while (SPACE_OR_TAB(*s)) s++;
463ee0b2
LW
494 if (!isDIGIT(*s))
495 return;
496 n = s;
497 while (isDIGIT(*s))
498 s++;
bf4acbe4 499 while (SPACE_OR_TAB(*s))
463ee0b2 500 s++;
73659bf1 501 if (*s == '"' && (t = strchr(s+1, '"'))) {
463ee0b2 502 s++;
73659bf1
GS
503 e = t + 1;
504 }
463ee0b2 505 else {
463ee0b2 506 for (t = s; !isSPACE(*t); t++) ;
73659bf1 507 e = t;
463ee0b2 508 }
bf4acbe4 509 while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
73659bf1
GS
510 e++;
511 if (*e != '\n' && *e != '\0')
512 return; /* false alarm */
513
463ee0b2
LW
514 ch = *t;
515 *t = '\0';
f4dd75d9 516 if (t - s > 0) {
05ec9bb3 517 CopFILE_free(PL_curcop);
57843af0 518 CopFILE_set(PL_curcop, s);
f4dd75d9 519 }
463ee0b2 520 *t = ch;
57843af0 521 CopLINE_set(PL_curcop, atoi(n)-1);
463ee0b2
LW
522}
523
ffb4593c
NT
524/*
525 * S_skipspace
526 * Called to gobble the appropriate amount and type of whitespace.
527 * Skips comments as well.
528 */
529
76e3520e 530STATIC char *
cea2e8a9 531S_skipspace(pTHX_ register char *s)
a687059c 532{
3280af22 533 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
bf4acbe4 534 while (s < PL_bufend && SPACE_OR_TAB(*s))
463ee0b2
LW
535 s++;
536 return s;
537 }
538 for (;;) {
fd049845 539 STRLEN prevlen;
09bef843 540 SSize_t oldprevlen, oldoldprevlen;
9c5ffd7c 541 SSize_t oldloplen = 0, oldunilen = 0;
60e6418e
GS
542 while (s < PL_bufend && isSPACE(*s)) {
543 if (*s++ == '\n' && PL_in_eval && !PL_rsfp)
544 incline(s);
545 }
ffb4593c
NT
546
547 /* comment */
3280af22
NIS
548 if (s < PL_bufend && *s == '#') {
549 while (s < PL_bufend && *s != '\n')
463ee0b2 550 s++;
60e6418e 551 if (s < PL_bufend) {
463ee0b2 552 s++;
60e6418e
GS
553 if (PL_in_eval && !PL_rsfp) {
554 incline(s);
555 continue;
556 }
557 }
463ee0b2 558 }
ffb4593c
NT
559
560 /* only continue to recharge the buffer if we're at the end
561 * of the buffer, we're not reading from a source filter, and
562 * we're in normal lexing mode
563 */
09bef843
SB
564 if (s < PL_bufend || !PL_rsfp || PL_sublex_info.sub_inwhat ||
565 PL_lex_state == LEX_FORMLINE)
463ee0b2 566 return s;
ffb4593c
NT
567
568 /* try to recharge the buffer */
9cbb5ea2
GS
569 if ((s = filter_gets(PL_linestr, PL_rsfp,
570 (prevlen = SvCUR(PL_linestr)))) == Nullch)
571 {
572 /* end of file. Add on the -p or -n magic */
3280af22
NIS
573 if (PL_minus_n || PL_minus_p) {
574 sv_setpv(PL_linestr,PL_minus_p ?
08e9d68e
DD
575 ";}continue{print or die qq(-p destination: $!\\n)" :
576 "");
3280af22
NIS
577 sv_catpv(PL_linestr,";}");
578 PL_minus_n = PL_minus_p = 0;
a0d0e21e
LW
579 }
580 else
3280af22 581 sv_setpv(PL_linestr,";");
ffb4593c
NT
582
583 /* reset variables for next time we lex */
9cbb5ea2
GS
584 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart
585 = SvPVX(PL_linestr);
3280af22 586 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 587 PL_last_lop = PL_last_uni = Nullch;
ffb4593c
NT
588
589 /* Close the filehandle. Could be from -P preprocessor,
590 * STDIN, or a regular file. If we were reading code from
591 * STDIN (because the commandline held no -e or filename)
592 * then we don't close it, we reset it so the code can
593 * read from STDIN too.
594 */
595
3280af22
NIS
596 if (PL_preprocess && !PL_in_eval)
597 (void)PerlProc_pclose(PL_rsfp);
598 else if ((PerlIO*)PL_rsfp == PerlIO_stdin())
599 PerlIO_clearerr(PL_rsfp);
8990e307 600 else
3280af22
NIS
601 (void)PerlIO_close(PL_rsfp);
602 PL_rsfp = Nullfp;
463ee0b2
LW
603 return s;
604 }
ffb4593c
NT
605
606 /* not at end of file, so we only read another line */
09bef843
SB
607 /* make corresponding updates to old pointers, for yyerror() */
608 oldprevlen = PL_oldbufptr - PL_bufend;
609 oldoldprevlen = PL_oldoldbufptr - PL_bufend;
610 if (PL_last_uni)
611 oldunilen = PL_last_uni - PL_bufend;
612 if (PL_last_lop)
613 oldloplen = PL_last_lop - PL_bufend;
3280af22
NIS
614 PL_linestart = PL_bufptr = s + prevlen;
615 PL_bufend = s + SvCUR(PL_linestr);
616 s = PL_bufptr;
09bef843
SB
617 PL_oldbufptr = s + oldprevlen;
618 PL_oldoldbufptr = s + oldoldprevlen;
619 if (PL_last_uni)
620 PL_last_uni = s + oldunilen;
621 if (PL_last_lop)
622 PL_last_lop = s + oldloplen;
a0d0e21e 623 incline(s);
ffb4593c
NT
624
625 /* debugger active and we're not compiling the debugger code,
626 * so store the line into the debugger's array of lines
627 */
3280af22 628 if (PERLDB_LINE && PL_curstash != PL_debstash) {
8990e307
LW
629 SV *sv = NEWSV(85,0);
630
631 sv_upgrade(sv, SVt_PVMG);
3280af22 632 sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
0ac0412a
MJD
633 (void)SvIOK_on(sv);
634 SvIVX(sv) = 0;
57843af0 635 av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv);
8990e307 636 }
463ee0b2 637 }
a687059c 638}
378cc40b 639
ffb4593c
NT
640/*
641 * S_check_uni
642 * Check the unary operators to ensure there's no ambiguity in how they're
643 * used. An ambiguous piece of code would be:
644 * rand + 5
645 * This doesn't mean rand() + 5. Because rand() is a unary operator,
646 * the +5 is its argument.
647 */
648
76e3520e 649STATIC void
cea2e8a9 650S_check_uni(pTHX)
ba106d47 651{
2f3197b3 652 char *s;
a0d0e21e 653 char *t;
2f3197b3 654
3280af22 655 if (PL_oldoldbufptr != PL_last_uni)
2f3197b3 656 return;
3280af22
NIS
657 while (isSPACE(*PL_last_uni))
658 PL_last_uni++;
7e2040f0 659 for (s = PL_last_uni; isALNUM_lazy_if(s,UTF) || *s == '-'; s++) ;
3280af22 660 if ((t = strchr(s, '(')) && t < PL_bufptr)
a0d0e21e 661 return;
0453d815 662 if (ckWARN_d(WARN_AMBIGUOUS)){
f248d071 663 char ch = *s;
0453d815 664 *s = '\0';
4e553d73
NIS
665 Perl_warner(aTHX_ WARN_AMBIGUOUS,
666 "Warning: Use of \"%s\" without parens is ambiguous",
0453d815
PM
667 PL_last_uni);
668 *s = ch;
669 }
2f3197b3
LW
670}
671
ffb4593c
NT
672/* workaround to replace the UNI() macro with a function. Only the
673 * hints/uts.sh file mentions this. Other comments elsewhere in the
674 * source indicate Microport Unix might need it too.
675 */
676
ffed7fef
LW
677#ifdef CRIPPLED_CC
678
679#undef UNI
ffed7fef 680#define UNI(f) return uni(f,s)
ffed7fef 681
76e3520e 682STATIC int
cea2e8a9 683S_uni(pTHX_ I32 f, char *s)
ffed7fef
LW
684{
685 yylval.ival = f;
3280af22
NIS
686 PL_expect = XTERM;
687 PL_bufptr = s;
8f872242
NIS
688 PL_last_uni = PL_oldbufptr;
689 PL_last_lop_op = f;
ffed7fef
LW
690 if (*s == '(')
691 return FUNC1;
692 s = skipspace(s);
693 if (*s == '(')
694 return FUNC1;
695 else
696 return UNIOP;
697}
698
a0d0e21e
LW
699#endif /* CRIPPLED_CC */
700
ffb4593c
NT
701/*
702 * LOP : macro to build a list operator. Its behaviour has been replaced
703 * with a subroutine, S_lop() for which LOP is just another name.
704 */
705
a0d0e21e
LW
706#define LOP(f,x) return lop(f,x,s)
707
ffb4593c
NT
708/*
709 * S_lop
710 * Build a list operator (or something that might be one). The rules:
711 * - if we have a next token, then it's a list operator [why?]
712 * - if the next thing is an opening paren, then it's a function
713 * - else it's a list operator
714 */
715
76e3520e 716STATIC I32
a0be28da 717S_lop(pTHX_ I32 f, int x, char *s)
ffed7fef 718{
79072805 719 yylval.ival = f;
35c8bce7 720 CLINE;
075953c3 721 REPORT("lop", f)
3280af22
NIS
722 PL_expect = x;
723 PL_bufptr = s;
724 PL_last_lop = PL_oldbufptr;
725 PL_last_lop_op = f;
726 if (PL_nexttoke)
a0d0e21e 727 return LSTOP;
79072805
LW
728 if (*s == '(')
729 return FUNC;
730 s = skipspace(s);
731 if (*s == '(')
732 return FUNC;
733 else
734 return LSTOP;
735}
736
ffb4593c
NT
737/*
738 * S_force_next
9cbb5ea2 739 * When the lexer realizes it knows the next token (for instance,
ffb4593c 740 * it is reordering tokens for the parser) then it can call S_force_next
9cbb5ea2
GS
741 * to know what token to return the next time the lexer is called. Caller
742 * will need to set PL_nextval[], and possibly PL_expect to ensure the lexer
743 * handles the token correctly.
ffb4593c
NT
744 */
745
4e553d73 746STATIC void
cea2e8a9 747S_force_next(pTHX_ I32 type)
79072805 748{
3280af22
NIS
749 PL_nexttype[PL_nexttoke] = type;
750 PL_nexttoke++;
751 if (PL_lex_state != LEX_KNOWNEXT) {
752 PL_lex_defer = PL_lex_state;
753 PL_lex_expect = PL_expect;
754 PL_lex_state = LEX_KNOWNEXT;
79072805
LW
755 }
756}
757
ffb4593c
NT
758/*
759 * S_force_word
760 * When the lexer knows the next thing is a word (for instance, it has
761 * just seen -> and it knows that the next char is a word char, then
762 * it calls S_force_word to stick the next word into the PL_next lookahead.
763 *
764 * Arguments:
b1b65b59 765 * char *start : buffer position (must be within PL_linestr)
ffb4593c
NT
766 * int token : PL_next will be this type of bare word (e.g., METHOD,WORD)
767 * int check_keyword : if true, Perl checks to make sure the word isn't
768 * a keyword (do this if the word is a label, e.g. goto FOO)
769 * int allow_pack : if true, : characters will also be allowed (require,
770 * use, etc. do this)
9cbb5ea2 771 * int allow_initial_tick : used by the "sub" lexer only.
ffb4593c
NT
772 */
773
76e3520e 774STATIC char *
cea2e8a9 775S_force_word(pTHX_ register char *start, int token, int check_keyword, int allow_pack, int allow_initial_tick)
79072805 776{
463ee0b2
LW
777 register char *s;
778 STRLEN len;
4e553d73 779
463ee0b2
LW
780 start = skipspace(start);
781 s = start;
7e2040f0 782 if (isIDFIRST_lazy_if(s,UTF) ||
a0d0e21e 783 (allow_pack && *s == ':') ||
15f0808c 784 (allow_initial_tick && *s == '\'') )
a0d0e21e 785 {
3280af22
NIS
786 s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, allow_pack, &len);
787 if (check_keyword && keyword(PL_tokenbuf, len))
463ee0b2
LW
788 return start;
789 if (token == METHOD) {
790 s = skipspace(s);
791 if (*s == '(')
3280af22 792 PL_expect = XTERM;
463ee0b2 793 else {
3280af22 794 PL_expect = XOPERATOR;
463ee0b2 795 }
79072805 796 }
3280af22
NIS
797 PL_nextval[PL_nexttoke].opval = (OP*)newSVOP(OP_CONST,0, newSVpv(PL_tokenbuf,0));
798 PL_nextval[PL_nexttoke].opval->op_private |= OPpCONST_BARE;
79072805
LW
799 force_next(token);
800 }
801 return s;
802}
803
ffb4593c
NT
804/*
805 * S_force_ident
9cbb5ea2 806 * Called when the lexer wants $foo *foo &foo etc, but the program
ffb4593c
NT
807 * text only contains the "foo" portion. The first argument is a pointer
808 * to the "foo", and the second argument is the type symbol to prefix.
809 * Forces the next token to be a "WORD".
9cbb5ea2 810 * Creates the symbol if it didn't already exist (via gv_fetchpv()).
ffb4593c
NT
811 */
812
76e3520e 813STATIC void
cea2e8a9 814S_force_ident(pTHX_ register char *s, int kind)
79072805
LW
815{
816 if (s && *s) {
11343788 817 OP* o = (OP*)newSVOP(OP_CONST, 0, newSVpv(s,0));
3280af22 818 PL_nextval[PL_nexttoke].opval = o;
79072805 819 force_next(WORD);
748a9306 820 if (kind) {
11343788 821 o->op_private = OPpCONST_ENTERED;
55497cff
PP
822 /* XXX see note in pp_entereval() for why we forgo typo
823 warnings if the symbol must be introduced in an eval.
824 GSAR 96-10-12 */
3280af22 825 gv_fetchpv(s, PL_in_eval ? (GV_ADDMULTI | GV_ADDINEVAL) : TRUE,
a0d0e21e
LW
826 kind == '$' ? SVt_PV :
827 kind == '@' ? SVt_PVAV :
828 kind == '%' ? SVt_PVHV :
829 SVt_PVGV
830 );
748a9306 831 }
79072805
LW
832 }
833}
834
1571675a
GS
835NV
836Perl_str_to_version(pTHX_ SV *sv)
837{
838 NV retval = 0.0;
839 NV nshift = 1.0;
840 STRLEN len;
841 char *start = SvPVx(sv,len);
3aa33fe5 842 bool utf = SvUTF8(sv) ? TRUE : FALSE;
1571675a
GS
843 char *end = start + len;
844 while (start < end) {
ba210ebe 845 STRLEN skip;
1571675a
GS
846 UV n;
847 if (utf)
9041c2e3 848 n = utf8n_to_uvchr((U8*)start, len, &skip, 0);
1571675a
GS
849 else {
850 n = *(U8*)start;
851 skip = 1;
852 }
853 retval += ((NV)n)/nshift;
854 start += skip;
855 nshift *= 1000;
856 }
857 return retval;
858}
859
4e553d73 860/*
ffb4593c
NT
861 * S_force_version
862 * Forces the next token to be a version number.
e759cc13
RGS
863 * If the next token appears to be an invalid version number, (e.g. "v2b"),
864 * and if "guessing" is TRUE, then no new token is created (and the caller
865 * must use an alternative parsing method).
ffb4593c
NT
866 */
867
76e3520e 868STATIC char *
e759cc13 869S_force_version(pTHX_ char *s, int guessing)
89bfa8cd
PP
870{
871 OP *version = Nullop;
44dcb63b 872 char *d;
89bfa8cd
PP
873
874 s = skipspace(s);
875
44dcb63b 876 d = s;
dd629d5b 877 if (*d == 'v')
44dcb63b 878 d++;
44dcb63b 879 if (isDIGIT(*d)) {
e759cc13
RGS
880 while (isDIGIT(*d) || *d == '_' || *d == '.')
881 d++;
9f3d182e 882 if (*d == ';' || isSPACE(*d) || *d == '}' || !*d) {
dd629d5b 883 SV *ver;
b73d6f50 884 s = scan_num(s, &yylval);
89bfa8cd 885 version = yylval.opval;
dd629d5b
GS
886 ver = cSVOPx(version)->op_sv;
887 if (SvPOK(ver) && !SvNIOK(ver)) {
155aba94 888 (void)SvUPGRADE(ver, SVt_PVNV);
1571675a
GS
889 SvNVX(ver) = str_to_version(ver);
890 SvNOK_on(ver); /* hint that it is a version */
44dcb63b 891 }
89bfa8cd 892 }
e759cc13
RGS
893 else if (guessing)
894 return s;
89bfa8cd
PP
895 }
896
897 /* NOTE: The parser sees the package name and the VERSION swapped */
3280af22 898 PL_nextval[PL_nexttoke].opval = version;
4e553d73 899 force_next(WORD);
89bfa8cd 900
e759cc13 901 return s;
89bfa8cd
PP
902}
903
ffb4593c
NT
904/*
905 * S_tokeq
906 * Tokenize a quoted string passed in as an SV. It finds the next
907 * chunk, up to end of string or a backslash. It may make a new
908 * SV containing that chunk (if HINT_NEW_STRING is on). It also
909 * turns \\ into \.
910 */
911
76e3520e 912STATIC SV *
cea2e8a9 913S_tokeq(pTHX_ SV *sv)
79072805
LW
914{
915 register char *s;
916 register char *send;
917 register char *d;
b3ac6de7
IZ
918 STRLEN len = 0;
919 SV *pv = sv;
79072805
LW
920
921 if (!SvLEN(sv))
b3ac6de7 922 goto finish;
79072805 923
a0d0e21e 924 s = SvPV_force(sv, len);
21a311ee 925 if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1)
b3ac6de7 926 goto finish;
463ee0b2 927 send = s + len;
79072805
LW
928 while (s < send && *s != '\\')
929 s++;
930 if (s == send)
b3ac6de7 931 goto finish;
79072805 932 d = s;
be4731d2 933 if ( PL_hints & HINT_NEW_STRING ) {
79cb57f6 934 pv = sv_2mortal(newSVpvn(SvPVX(pv), len));
be4731d2
NIS
935 if (SvUTF8(sv))
936 SvUTF8_on(pv);
937 }
79072805
LW
938 while (s < send) {
939 if (*s == '\\') {
a0d0e21e 940 if (s + 1 < send && (s[1] == '\\'))
79072805
LW
941 s++; /* all that, just for this */
942 }
943 *d++ = *s++;
944 }
945 *d = '\0';
463ee0b2 946 SvCUR_set(sv, d - SvPVX(sv));
b3ac6de7 947 finish:
3280af22 948 if ( PL_hints & HINT_NEW_STRING )
b3ac6de7 949 return new_constant(NULL, 0, "q", sv, pv, "q");
79072805
LW
950 return sv;
951}
952
ffb4593c
NT
953/*
954 * Now come three functions related to double-quote context,
955 * S_sublex_start, S_sublex_push, and S_sublex_done. They're used when
956 * converting things like "\u\Lgnat" into ucfirst(lc("gnat")). They
957 * interact with PL_lex_state, and create fake ( ... ) argument lists
958 * to handle functions and concatenation.
959 * They assume that whoever calls them will be setting up a fake
960 * join call, because each subthing puts a ',' after it. This lets
961 * "lower \luPpEr"
962 * become
963 * join($, , 'lower ', lcfirst( 'uPpEr', ) ,)
964 *
965 * (I'm not sure whether the spurious commas at the end of lcfirst's
966 * arguments and join's arguments are created or not).
967 */
968
969/*
970 * S_sublex_start
971 * Assumes that yylval.ival is the op we're creating (e.g. OP_LCFIRST).
972 *
973 * Pattern matching will set PL_lex_op to the pattern-matching op to
974 * make (we return THING if yylval.ival is OP_NULL, PMFUNC otherwise).
975 *
976 * OP_CONST and OP_READLINE are easy--just make the new op and return.
977 *
978 * Everything else becomes a FUNC.
979 *
980 * Sets PL_lex_state to LEX_INTERPPUSH unless (ival was OP_NULL or we
981 * had an OP_CONST or OP_READLINE). This just sets us up for a
982 * call to S_sublex_push().
983 */
984
76e3520e 985STATIC I32
cea2e8a9 986S_sublex_start(pTHX)
79072805
LW
987{
988 register I32 op_type = yylval.ival;
79072805
LW
989
990 if (op_type == OP_NULL) {
3280af22
NIS
991 yylval.opval = PL_lex_op;
992 PL_lex_op = Nullop;
79072805
LW
993 return THING;
994 }
995 if (op_type == OP_CONST || op_type == OP_READLINE) {
3280af22 996 SV *sv = tokeq(PL_lex_stuff);
b3ac6de7
IZ
997
998 if (SvTYPE(sv) == SVt_PVIV) {
999 /* Overloaded constants, nothing fancy: Convert to SVt_PV: */
1000 STRLEN len;
1001 char *p;
1002 SV *nsv;
1003
1004 p = SvPV(sv, len);
79cb57f6 1005 nsv = newSVpvn(p, len);
01ec43d0
GS
1006 if (SvUTF8(sv))
1007 SvUTF8_on(nsv);
b3ac6de7
IZ
1008 SvREFCNT_dec(sv);
1009 sv = nsv;
4e553d73 1010 }
b3ac6de7 1011 yylval.opval = (OP*)newSVOP(op_type, 0, sv);
3280af22 1012 PL_lex_stuff = Nullsv;
79072805
LW
1013 return THING;
1014 }
1015
3280af22
NIS
1016 PL_sublex_info.super_state = PL_lex_state;
1017 PL_sublex_info.sub_inwhat = op_type;
1018 PL_sublex_info.sub_op = PL_lex_op;
1019 PL_lex_state = LEX_INTERPPUSH;
55497cff 1020
3280af22
NIS
1021 PL_expect = XTERM;
1022 if (PL_lex_op) {
1023 yylval.opval = PL_lex_op;
1024 PL_lex_op = Nullop;
55497cff
PP
1025 return PMFUNC;
1026 }
1027 else
1028 return FUNC;
1029}
1030
ffb4593c
NT
1031/*
1032 * S_sublex_push
1033 * Create a new scope to save the lexing state. The scope will be
1034 * ended in S_sublex_done. Returns a '(', starting the function arguments
1035 * to the uc, lc, etc. found before.
1036 * Sets PL_lex_state to LEX_INTERPCONCAT.
1037 */
1038
76e3520e 1039STATIC I32
cea2e8a9 1040S_sublex_push(pTHX)
55497cff 1041{
f46d017c 1042 ENTER;
55497cff 1043
3280af22
NIS
1044 PL_lex_state = PL_sublex_info.super_state;
1045 SAVEI32(PL_lex_dojoin);
1046 SAVEI32(PL_lex_brackets);
3280af22
NIS
1047 SAVEI32(PL_lex_casemods);
1048 SAVEI32(PL_lex_starts);
1049 SAVEI32(PL_lex_state);
7766f137 1050 SAVEVPTR(PL_lex_inpat);
3280af22 1051 SAVEI32(PL_lex_inwhat);
57843af0 1052 SAVECOPLINE(PL_curcop);
3280af22 1053 SAVEPPTR(PL_bufptr);
8452ff4b 1054 SAVEPPTR(PL_bufend);
3280af22
NIS
1055 SAVEPPTR(PL_oldbufptr);
1056 SAVEPPTR(PL_oldoldbufptr);
207e3d1a
JH
1057 SAVEPPTR(PL_last_lop);
1058 SAVEPPTR(PL_last_uni);
3280af22
NIS
1059 SAVEPPTR(PL_linestart);
1060 SAVESPTR(PL_linestr);
1061 SAVEPPTR(PL_lex_brackstack);
1062 SAVEPPTR(PL_lex_casestack);
1063
1064 PL_linestr = PL_lex_stuff;
1065 PL_lex_stuff = Nullsv;
1066
9cbb5ea2
GS
1067 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart
1068 = SvPVX(PL_linestr);
3280af22 1069 PL_bufend += SvCUR(PL_linestr);
207e3d1a 1070 PL_last_lop = PL_last_uni = Nullch;
3280af22
NIS
1071 SAVEFREESV(PL_linestr);
1072
1073 PL_lex_dojoin = FALSE;
1074 PL_lex_brackets = 0;
3280af22
NIS
1075 New(899, PL_lex_brackstack, 120, char);
1076 New(899, PL_lex_casestack, 12, char);
1077 SAVEFREEPV(PL_lex_brackstack);
1078 SAVEFREEPV(PL_lex_casestack);
1079 PL_lex_casemods = 0;
1080 *PL_lex_casestack = '\0';
1081 PL_lex_starts = 0;
1082 PL_lex_state = LEX_INTERPCONCAT;
57843af0 1083 CopLINE_set(PL_curcop, PL_multi_start);
3280af22
NIS
1084
1085 PL_lex_inwhat = PL_sublex_info.sub_inwhat;
1086 if (PL_lex_inwhat == OP_MATCH || PL_lex_inwhat == OP_QR || PL_lex_inwhat == OP_SUBST)
1087 PL_lex_inpat = PL_sublex_info.sub_op;
79072805 1088 else
3280af22 1089 PL_lex_inpat = Nullop;
79072805 1090
55497cff 1091 return '(';
79072805
LW
1092}
1093
ffb4593c
NT
1094/*
1095 * S_sublex_done
1096 * Restores lexer state after a S_sublex_push.
1097 */
1098
76e3520e 1099STATIC I32
cea2e8a9 1100S_sublex_done(pTHX)
79072805 1101{
3280af22 1102 if (!PL_lex_starts++) {
9aa983d2
JH
1103 SV *sv = newSVpvn("",0);
1104 if (SvUTF8(PL_linestr))
1105 SvUTF8_on(sv);
3280af22 1106 PL_expect = XOPERATOR;
9aa983d2 1107 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
79072805
LW
1108 return THING;
1109 }
1110
3280af22
NIS
1111 if (PL_lex_casemods) { /* oops, we've got some unbalanced parens */
1112 PL_lex_state = LEX_INTERPCASEMOD;
cea2e8a9 1113 return yylex();
79072805
LW
1114 }
1115
ffb4593c 1116 /* Is there a right-hand side to take care of? (s//RHS/ or tr//RHS/) */
3280af22
NIS
1117 if (PL_lex_repl && (PL_lex_inwhat == OP_SUBST || PL_lex_inwhat == OP_TRANS)) {
1118 PL_linestr = PL_lex_repl;
1119 PL_lex_inpat = 0;
1120 PL_bufend = PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
1121 PL_bufend += SvCUR(PL_linestr);
207e3d1a 1122 PL_last_lop = PL_last_uni = Nullch;
3280af22
NIS
1123 SAVEFREESV(PL_linestr);
1124 PL_lex_dojoin = FALSE;
1125 PL_lex_brackets = 0;
3280af22
NIS
1126 PL_lex_casemods = 0;
1127 *PL_lex_casestack = '\0';
1128 PL_lex_starts = 0;
25da4f38 1129 if (SvEVALED(PL_lex_repl)) {
3280af22
NIS
1130 PL_lex_state = LEX_INTERPNORMAL;
1131 PL_lex_starts++;
e9fa98b2
HS
1132 /* we don't clear PL_lex_repl here, so that we can check later
1133 whether this is an evalled subst; that means we rely on the
1134 logic to ensure sublex_done() is called again only via the
1135 branch (in yylex()) that clears PL_lex_repl, else we'll loop */
79072805 1136 }
e9fa98b2 1137 else {
3280af22 1138 PL_lex_state = LEX_INTERPCONCAT;
e9fa98b2
HS
1139 PL_lex_repl = Nullsv;
1140 }
79072805 1141 return ',';
ffed7fef
LW
1142 }
1143 else {
f46d017c 1144 LEAVE;
3280af22
NIS
1145 PL_bufend = SvPVX(PL_linestr);
1146 PL_bufend += SvCUR(PL_linestr);
1147 PL_expect = XOPERATOR;
09bef843 1148 PL_sublex_info.sub_inwhat = 0;
79072805 1149 return ')';
ffed7fef
LW
1150 }
1151}
1152
02aa26ce
NT
1153/*
1154 scan_const
1155
1156 Extracts a pattern, double-quoted string, or transliteration. This
1157 is terrifying code.
1158
3280af22
NIS
1159 It looks at lex_inwhat and PL_lex_inpat to find out whether it's
1160 processing a pattern (PL_lex_inpat is true), a transliteration
02aa26ce
NT
1161 (lex_inwhat & OP_TRANS is true), or a double-quoted string.
1162
9b599b2a
GS
1163 Returns a pointer to the character scanned up to. Iff this is
1164 advanced from the start pointer supplied (ie if anything was
1165 successfully parsed), will leave an OP for the substring scanned
1166 in yylval. Caller must intuit reason for not parsing further
1167 by looking at the next characters herself.
1168
02aa26ce
NT
1169 In patterns:
1170 backslashes:
1171 double-quoted style: \r and \n
1172 regexp special ones: \D \s
1173 constants: \x3
1174 backrefs: \1 (deprecated in substitution replacements)
1175 case and quoting: \U \Q \E
1176 stops on @ and $, but not for $ as tail anchor
1177
1178 In transliterations:
1179 characters are VERY literal, except for - not at the start or end
1180 of the string, which indicates a range. scan_const expands the
1181 range to the full set of intermediate characters.
1182
1183 In double-quoted strings:
1184 backslashes:
1185 double-quoted style: \r and \n
1186 constants: \x3
1187 backrefs: \1 (deprecated)
1188 case and quoting: \U \Q \E
1189 stops on @ and $
1190
1191 scan_const does *not* construct ops to handle interpolated strings.
1192 It stops processing as soon as it finds an embedded $ or @ variable
1193 and leaves it to the caller to work out what's going on.
1194
1195 @ in pattern could be: @foo, @{foo}, @$foo, @'foo, @:foo.
1196
1197 $ in pattern could be $foo or could be tail anchor. Assumption:
1198 it's a tail anchor if $ is the last thing in the string, or if it's
1199 followed by one of ")| \n\t"
1200
1201 \1 (backreferences) are turned into $1
1202
1203 The structure of the code is
1204 while (there's a character to process) {
1205 handle transliteration ranges
1206 skip regexp comments
1207 skip # initiated comments in //x patterns
1208 check for embedded @foo
1209 check for embedded scalars
1210 if (backslash) {
1211 leave intact backslashes from leave (below)
1212 deprecate \1 in strings and sub replacements
1213 handle string-changing backslashes \l \U \Q \E, etc.
1214 switch (what was escaped) {
1215 handle - in a transliteration (becomes a literal -)
1216 handle \132 octal characters
1217 handle 0x15 hex characters
1218 handle \cV (control V)
1219 handle printf backslashes (\f, \r, \n, etc)
1220 } (end switch)
1221 } (end if backslash)
1222 } (end while character to read)
4e553d73 1223
02aa26ce
NT
1224*/
1225
76e3520e 1226STATIC char *
cea2e8a9 1227S_scan_const(pTHX_ char *start)
79072805 1228{
3280af22 1229 register char *send = PL_bufend; /* end of the constant */
02aa26ce
NT
1230 SV *sv = NEWSV(93, send - start); /* sv for the constant */
1231 register char *s = start; /* start of the constant */
1232 register char *d = SvPVX(sv); /* destination for copies */
1233 bool dorange = FALSE; /* are we in a translit range? */
c2e66d9e 1234 bool didrange = FALSE; /* did we just finish a range? */
2b9d42f0
NIS
1235 I32 has_utf8 = FALSE; /* Output constant is UTF8 */
1236 I32 this_utf8 = UTF; /* The source string is assumed to be UTF8 */
012bcf8d
GS
1237 UV uv;
1238
dff6d3cd 1239 const char *leaveit = /* set of acceptably-backslashed characters */
3280af22 1240 PL_lex_inpat
4a2d328f 1241 ? "\\.^$@AGZdDwWsSbBpPXC+*?|()-nrtfeaxcz0123456789[{]} \t\n\r\f\v#"
9b599b2a 1242 : "";
79072805 1243
2b9d42f0
NIS
1244 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
1245 /* If we are doing a trans and we know we want UTF8 set expectation */
1246 has_utf8 = PL_sublex_info.sub_op->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF);
1247 this_utf8 = PL_sublex_info.sub_op->op_private & (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
1248 }
1249
1250
79072805 1251 while (s < send || dorange) {
02aa26ce 1252 /* get transliterations out of the way (they're most literal) */
3280af22 1253 if (PL_lex_inwhat == OP_TRANS) {
02aa26ce 1254 /* expand a range A-Z to the full set of characters. AIE! */
79072805 1255 if (dorange) {
1ba5c669
JH
1256 I32 i; /* current expanded character */
1257 I32 min; /* first character in range */
1258 I32 max; /* last character in range */
02aa26ce 1259
2b9d42f0 1260 if (has_utf8) {
8973db79
JH
1261 char *c = (char*)utf8_hop((U8*)d, -1);
1262 char *e = d++;
1263 while (e-- > c)
1264 *(e + 1) = *e;
25716404 1265 *c = (char)UTF_TO_NATIVE(0xff);
8973db79
JH
1266 /* mark the range as done, and continue */
1267 dorange = FALSE;
1268 didrange = TRUE;
1269 continue;
1270 }
2b9d42f0 1271
02aa26ce 1272 i = d - SvPVX(sv); /* remember current offset */
9cbb5ea2
GS
1273 SvGROW(sv, SvLEN(sv) + 256); /* never more than 256 chars in a range */
1274 d = SvPVX(sv) + i; /* refresh d after realloc */
02aa26ce
NT
1275 d -= 2; /* eat the first char and the - */
1276
8ada0baa
JH
1277 min = (U8)*d; /* first char in range */
1278 max = (U8)d[1]; /* last char in range */
1279
c2e66d9e 1280 if (min > max) {
01ec43d0 1281 Perl_croak(aTHX_
1ba5c669
JH
1282 "Invalid [] range \"%c-%c\" in transliteration operator",
1283 (char)min, (char)max);
c2e66d9e
GS
1284 }
1285
c7f1f016 1286#ifdef EBCDIC
8ada0baa
JH
1287 if ((isLOWER(min) && isLOWER(max)) ||
1288 (isUPPER(min) && isUPPER(max))) {
1289 if (isLOWER(min)) {
1290 for (i = min; i <= max; i++)
1291 if (isLOWER(i))
db42d148 1292 *d++ = NATIVE_TO_NEED(has_utf8,i);
8ada0baa
JH
1293 } else {
1294 for (i = min; i <= max; i++)
1295 if (isUPPER(i))
db42d148 1296 *d++ = NATIVE_TO_NEED(has_utf8,i);
8ada0baa
JH
1297 }
1298 }
1299 else
1300#endif
1301 for (i = min; i <= max; i++)
1302 *d++ = i;
02aa26ce
NT
1303
1304 /* mark the range as done, and continue */
79072805 1305 dorange = FALSE;
01ec43d0 1306 didrange = TRUE;
79072805 1307 continue;
4e553d73 1308 }
02aa26ce
NT
1309
1310 /* range begins (ignore - as first or last char) */
79072805 1311 else if (*s == '-' && s+1 < send && s != start) {
4e553d73 1312 if (didrange) {
1fafa243 1313 Perl_croak(aTHX_ "Ambiguous range in transliteration operator");
01ec43d0 1314 }
2b9d42f0 1315 if (has_utf8) {
25716404 1316 *d++ = (char)UTF_TO_NATIVE(0xff); /* use illegal utf8 byte--see pmtrans */
a0ed51b3
LW
1317 s++;
1318 continue;
1319 }
79072805
LW
1320 dorange = TRUE;
1321 s++;
01ec43d0
GS
1322 }
1323 else {
1324 didrange = FALSE;
1325 }
79072805 1326 }
02aa26ce
NT
1327
1328 /* if we get here, we're not doing a transliteration */
1329
0f5d15d6
IZ
1330 /* skip for regexp comments /(?#comment)/ and code /(?{code})/,
1331 except for the last char, which will be done separately. */
3280af22 1332 else if (*s == '(' && PL_lex_inpat && s[1] == '?') {
cc6b7395
IZ
1333 if (s[2] == '#') {
1334 while (s < send && *s != ')')
db42d148 1335 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
155aba94
GS
1336 }
1337 else if (s[2] == '{' /* This should match regcomp.c */
1338 || ((s[2] == 'p' || s[2] == '?') && s[3] == '{'))
1339 {
cc6b7395 1340 I32 count = 1;
0f5d15d6 1341 char *regparse = s + (s[2] == '{' ? 3 : 4);
cc6b7395
IZ
1342 char c;
1343
d9f97599
GS
1344 while (count && (c = *regparse)) {
1345 if (c == '\\' && regparse[1])
1346 regparse++;
4e553d73 1347 else if (c == '{')
cc6b7395 1348 count++;
4e553d73 1349 else if (c == '}')
cc6b7395 1350 count--;
d9f97599 1351 regparse++;
cc6b7395 1352 }
5bdf89e7
IZ
1353 if (*regparse != ')') {
1354 regparse--; /* Leave one char for continuation. */
cc6b7395 1355 yyerror("Sequence (?{...}) not terminated or not {}-balanced");
5bdf89e7 1356 }
0f5d15d6 1357 while (s < regparse)
db42d148 1358 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
cc6b7395 1359 }
748a9306 1360 }
02aa26ce
NT
1361
1362 /* likewise skip #-initiated comments in //x patterns */
3280af22
NIS
1363 else if (*s == '#' && PL_lex_inpat &&
1364 ((PMOP*)PL_lex_inpat)->op_pmflags & PMf_EXTENDED) {
748a9306 1365 while (s+1 < send && *s != '\n')
db42d148 1366 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
748a9306 1367 }
02aa26ce 1368
5d1d4326
JH
1369 /* check for embedded arrays
1370 (@foo, @:foo, @'foo, @{foo}, @$foo, @+, @-)
1371 */
7e2040f0 1372 else if (*s == '@' && s[1]
5d1d4326 1373 && (isALNUM_lazy_if(s+1,UTF) || strchr(":'{$+-", s[1])))
79072805 1374 break;
02aa26ce
NT
1375
1376 /* check for embedded scalars. only stop if we're sure it's a
1377 variable.
1378 */
79072805 1379 else if (*s == '$') {
3280af22 1380 if (!PL_lex_inpat) /* not a regexp, so $ must be var */
79072805 1381 break;
6002328a 1382 if (s + 1 < send && !strchr("()| \r\n\t", s[1]))
79072805
LW
1383 break; /* in regexp, $ might be tail anchor */
1384 }
02aa26ce 1385
2b9d42f0
NIS
1386 /* End of else if chain - OP_TRANS rejoin rest */
1387
02aa26ce 1388 /* backslashes */
79072805
LW
1389 if (*s == '\\' && s+1 < send) {
1390 s++;
02aa26ce
NT
1391
1392 /* some backslashes we leave behind */
c9f97d15 1393 if (*leaveit && *s && strchr(leaveit, *s)) {
db42d148
NIS
1394 *d++ = NATIVE_TO_NEED(has_utf8,'\\');
1395 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
79072805
LW
1396 continue;
1397 }
02aa26ce
NT
1398
1399 /* deprecate \1 in strings and substitution replacements */
3280af22 1400 if (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat &&
a0d0e21e 1401 isDIGIT(*s) && *s != '0' && !isDIGIT(s[1]))
79072805 1402 {
599cee73 1403 if (ckWARN(WARN_SYNTAX))
cea2e8a9 1404 Perl_warner(aTHX_ WARN_SYNTAX, "\\%c better written as $%c", *s, *s);
79072805
LW
1405 *--s = '$';
1406 break;
1407 }
02aa26ce
NT
1408
1409 /* string-change backslash escapes */
3280af22 1410 if (PL_lex_inwhat != OP_TRANS && *s && strchr("lLuUEQ", *s)) {
79072805
LW
1411 --s;
1412 break;
1413 }
02aa26ce
NT
1414
1415 /* if we get here, it's either a quoted -, or a digit */
79072805 1416 switch (*s) {
02aa26ce
NT
1417
1418 /* quoted - in transliterations */
79072805 1419 case '-':
3280af22 1420 if (PL_lex_inwhat == OP_TRANS) {
79072805
LW
1421 *d++ = *s++;
1422 continue;
1423 }
1424 /* FALL THROUGH */
1425 default:
11b8faa4 1426 {
7e84c16c 1427 if (ckWARN(WARN_MISC) && isALNUM(*s))
4e553d73 1428 Perl_warner(aTHX_ WARN_MISC,
11b8faa4
JH
1429 "Unrecognized escape \\%c passed through",
1430 *s);
1431 /* default action is to copy the quoted character */
f9a63242 1432 goto default_action;
11b8faa4 1433 }
02aa26ce
NT
1434
1435 /* \132 indicates an octal constant */
79072805
LW
1436 case '0': case '1': case '2': case '3':
1437 case '4': case '5': case '6': case '7':
ba210ebe 1438 {
53305cf1
NC
1439 I32 flags = 0;
1440 STRLEN len = 3;
1441 uv = grok_oct(s, &len, &flags, NULL);
ba210ebe
JH
1442 s += len;
1443 }
012bcf8d 1444 goto NUM_ESCAPE_INSERT;
02aa26ce
NT
1445
1446 /* \x24 indicates a hex constant */
79072805 1447 case 'x':
a0ed51b3
LW
1448 ++s;
1449 if (*s == '{') {
1450 char* e = strchr(s, '}');
a4c04bdc
NC
1451 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
1452 PERL_SCAN_DISALLOW_PREFIX;
53305cf1 1453 STRLEN len;
355860ce 1454
53305cf1 1455 ++s;
adaeee49 1456 if (!e) {
a0ed51b3 1457 yyerror("Missing right brace on \\x{}");
355860ce 1458 continue;
ba210ebe 1459 }
53305cf1
NC
1460 len = e - s;
1461 uv = grok_hex(s, &len, &flags, NULL);
ba210ebe 1462 s = e + 1;
a0ed51b3
LW
1463 }
1464 else {
ba210ebe 1465 {
53305cf1 1466 STRLEN len = 2;
a4c04bdc 1467 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
53305cf1 1468 uv = grok_hex(s, &len, &flags, NULL);
ba210ebe
JH
1469 s += len;
1470 }
012bcf8d
GS
1471 }
1472
1473 NUM_ESCAPE_INSERT:
1474 /* Insert oct or hex escaped character.
301d3d20 1475 * There will always enough room in sv since such
db42d148 1476 * escapes will be longer than any UTF-8 sequence
301d3d20 1477 * they can end up as. */
ba7cea30 1478
c7f1f016
NIS
1479 /* We need to map to chars to ASCII before doing the tests
1480 to cover EBCDIC
1481 */
c4d5f83a 1482 if (!UNI_IS_INVARIANT(NATIVE_TO_UNI(uv))) {
9aa983d2 1483 if (!has_utf8 && uv > 255) {
301d3d20
JH
1484 /* Might need to recode whatever we have
1485 * accumulated so far if it contains any
1486 * hibit chars.
1487 *
1488 * (Can't we keep track of that and avoid
1489 * this rescan? --jhi)
012bcf8d 1490 */
c7f1f016 1491 int hicount = 0;
63cd0674
NIS
1492 U8 *c;
1493 for (c = (U8 *) SvPVX(sv); c < (U8 *)d; c++) {
c4d5f83a 1494 if (!NATIVE_IS_INVARIANT(*c)) {
012bcf8d 1495 hicount++;
db42d148 1496 }
012bcf8d 1497 }
63cd0674 1498 if (hicount) {
db42d148
NIS
1499 STRLEN offset = d - SvPVX(sv);
1500 U8 *src, *dst;
1501 d = SvGROW(sv, SvLEN(sv) + hicount + 1) + offset;
1502 src = (U8 *)d - 1;
1503 dst = src+hicount;
1504 d += hicount;
1505 while (src >= (U8 *)SvPVX(sv)) {
c4d5f83a 1506 if (!NATIVE_IS_INVARIANT(*src)) {
63cd0674 1507 U8 ch = NATIVE_TO_ASCII(*src);
db42d148
NIS
1508 *dst-- = UTF8_EIGHT_BIT_LO(ch);
1509 *dst-- = UTF8_EIGHT_BIT_HI(ch);
012bcf8d
GS
1510 }
1511 else {
63cd0674 1512 *dst-- = *src;
012bcf8d 1513 }
c7f1f016 1514 src--;
012bcf8d
GS
1515 }
1516 }
1517 }
1518
9aa983d2 1519 if (has_utf8 || uv > 255) {
9041c2e3 1520 d = (char*)uvchr_to_utf8((U8*)d, uv);
4e553d73 1521 has_utf8 = TRUE;
f9a63242
JH
1522 if (PL_lex_inwhat == OP_TRANS &&
1523 PL_sublex_info.sub_op) {
1524 PL_sublex_info.sub_op->op_private |=
1525 (PL_lex_repl ? OPpTRANS_FROM_UTF
1526 : OPpTRANS_TO_UTF);
f9a63242 1527 }
012bcf8d 1528 }
a0ed51b3 1529 else {
012bcf8d 1530 *d++ = (char)uv;
a0ed51b3 1531 }
012bcf8d
GS
1532 }
1533 else {
c4d5f83a 1534 *d++ = (char) uv;
a0ed51b3 1535 }
79072805 1536 continue;
02aa26ce 1537
b239daa5 1538 /* \N{LATIN SMALL LETTER A} is a named character */
4a2d328f 1539 case 'N':
55eda711 1540 ++s;
423cee85
JH
1541 if (*s == '{') {
1542 char* e = strchr(s, '}');
155aba94 1543 SV *res;
423cee85
JH
1544 STRLEN len;
1545 char *str;
4e553d73 1546
423cee85 1547 if (!e) {
5777a3f7 1548 yyerror("Missing right brace on \\N{}");
423cee85
JH
1549 e = s - 1;
1550 goto cont_scan;
1551 }
55eda711
JH
1552 res = newSVpvn(s + 1, e - s - 1);
1553 res = new_constant( Nullch, 0, "charnames",
1554 res, Nullsv, "\\N{...}" );
f9a63242
JH
1555 if (has_utf8)
1556 sv_utf8_upgrade(res);
423cee85 1557 str = SvPV(res,len);
89491803 1558 if (!has_utf8 && SvUTF8(res)) {
f08d6ad9
GS
1559 char *ostart = SvPVX(sv);
1560 SvCUR_set(sv, d - ostart);
1561 SvPOK_on(sv);
e4f3eed8 1562 *d = '\0';
f08d6ad9 1563 sv_utf8_upgrade(sv);
d2f449dd
SB
1564 /* this just broke our allocation above... */
1565 SvGROW(sv, send - start);
f08d6ad9 1566 d = SvPVX(sv) + SvCUR(sv);
89491803 1567 has_utf8 = TRUE;
f08d6ad9 1568 }
b239daa5 1569 if (len > e - s + 4) { /* I _guess_ 4 is \N{} --jhi */
423cee85
JH
1570 char *odest = SvPVX(sv);
1571
8973db79 1572 SvGROW(sv, (SvLEN(sv) + len - (e - s + 4)));
423cee85
JH
1573 d = SvPVX(sv) + (d - odest);
1574 }
1575 Copy(str, d, len, char);
1576 d += len;
1577 SvREFCNT_dec(res);
1578 cont_scan:
1579 s = e + 1;
1580 }
1581 else
5777a3f7 1582 yyerror("Missing braces on \\N{}");
423cee85
JH
1583 continue;
1584
02aa26ce 1585 /* \c is a control character */
79072805
LW
1586 case 'c':
1587 s++;
ba210ebe
JH
1588 {
1589 U8 c = *s++;
c7f1f016
NIS
1590#ifdef EBCDIC
1591 if (isLOWER(c))
1592 c = toUPPER(c);
1593#endif
db42d148 1594 *d++ = NATIVE_TO_NEED(has_utf8,toCTRL(c));
ba210ebe 1595 }
79072805 1596 continue;
02aa26ce
NT
1597
1598 /* printf-style backslashes, formfeeds, newlines, etc */
79072805 1599 case 'b':
db42d148 1600 *d++ = NATIVE_TO_NEED(has_utf8,'\b');
79072805
LW
1601 break;
1602 case 'n':
db42d148 1603 *d++ = NATIVE_TO_NEED(has_utf8,'\n');
79072805
LW
1604 break;
1605 case 'r':
db42d148 1606 *d++ = NATIVE_TO_NEED(has_utf8,'\r');
79072805
LW
1607 break;
1608 case 'f':
db42d148 1609 *d++ = NATIVE_TO_NEED(has_utf8,'\f');
79072805
LW
1610 break;
1611 case 't':
db42d148 1612 *d++ = NATIVE_TO_NEED(has_utf8,'\t');
79072805 1613 break;
34a3fe2a 1614 case 'e':
db42d148 1615 *d++ = ASCII_TO_NEED(has_utf8,'\033');
34a3fe2a
PP
1616 break;
1617 case 'a':
db42d148 1618 *d++ = ASCII_TO_NEED(has_utf8,'\007');
79072805 1619 break;
02aa26ce
NT
1620 } /* end switch */
1621
79072805
LW
1622 s++;
1623 continue;
02aa26ce
NT
1624 } /* end if (backslash) */
1625
f9a63242 1626 default_action:
2b9d42f0
NIS
1627 /* If we started with encoded form, or already know we want it
1628 and then encode the next character */
1629 if ((has_utf8 || this_utf8) && !NATIVE_IS_INVARIANT((U8)(*s))) {
1630 STRLEN len = 1;
1631 UV uv = (this_utf8) ? utf8n_to_uvchr((U8*)s, send - s, &len, 0) : (UV) ((U8) *s);
1632 STRLEN need = UNISKIP(NATIVE_TO_UNI(uv));
1633 s += len;
1634 if (need > len) {
1635 /* encoded value larger than old, need extra space (NOTE: SvCUR() not set here) */
1636 STRLEN off = d - SvPVX(sv);
1637 d = SvGROW(sv, SvLEN(sv) + (need-len)) + off;
1638 }
1639 d = (char*)uvchr_to_utf8((U8*)d, uv);
1640 has_utf8 = TRUE;
1641 }
1642 else {
1643 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1644 }
02aa26ce
NT
1645 } /* while loop to process each character */
1646
1647 /* terminate the string and set up the sv */
79072805 1648 *d = '\0';
463ee0b2 1649 SvCUR_set(sv, d - SvPVX(sv));
2b9d42f0 1650 if (SvCUR(sv) >= SvLEN(sv))
585602fa 1651 Perl_croak(aTHX_ "panic: constant overflowed allocated space");
2b9d42f0 1652
79072805 1653 SvPOK_on(sv);
9f4817db 1654 if (PL_encoding && !has_utf8) {
5d170f3a 1655 Perl_sv_recode_to_utf8(aTHX_ sv, PL_encoding);
9f4817db
JH
1656 has_utf8 = TRUE;
1657 }
2b9d42f0 1658 if (has_utf8) {
7e2040f0 1659 SvUTF8_on(sv);
2b9d42f0
NIS
1660 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
1661 PL_sublex_info.sub_op->op_private |=
1662 (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
1663 }
1664 }
79072805 1665
02aa26ce 1666 /* shrink the sv if we allocated more than we used */
79072805
LW
1667 if (SvCUR(sv) + 5 < SvLEN(sv)) {
1668 SvLEN_set(sv, SvCUR(sv) + 1);
463ee0b2 1669 Renew(SvPVX(sv), SvLEN(sv), char);
79072805 1670 }
02aa26ce 1671
9b599b2a 1672 /* return the substring (via yylval) only if we parsed anything */
3280af22
NIS
1673 if (s > PL_bufptr) {
1674 if ( PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ) )
4e553d73 1675 sv = new_constant(start, s - start, (PL_lex_inpat ? "qr" : "q"),
b3ac6de7 1676 sv, Nullsv,
4e553d73 1677 ( PL_lex_inwhat == OP_TRANS
b3ac6de7 1678 ? "tr"
3280af22 1679 : ( (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat)
b3ac6de7
IZ
1680 ? "s"
1681 : "qq")));
79072805 1682 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
b3ac6de7 1683 } else
8990e307 1684 SvREFCNT_dec(sv);
79072805
LW
1685 return s;
1686}
1687
ffb4593c
NT
1688/* S_intuit_more
1689 * Returns TRUE if there's more to the expression (e.g., a subscript),
1690 * FALSE otherwise.
ffb4593c
NT
1691 *
1692 * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
1693 *
1694 * ->[ and ->{ return TRUE
1695 * { and [ outside a pattern are always subscripts, so return TRUE
1696 * if we're outside a pattern and it's not { or [, then return FALSE
1697 * if we're in a pattern and the first char is a {
1698 * {4,5} (any digits around the comma) returns FALSE
1699 * if we're in a pattern and the first char is a [
1700 * [] returns FALSE
1701 * [SOMETHING] has a funky algorithm to decide whether it's a
1702 * character class or not. It has to deal with things like
1703 * /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
1704 * anything else returns TRUE
1705 */
1706
9cbb5ea2
GS
1707/* This is the one truly awful dwimmer necessary to conflate C and sed. */
1708
76e3520e 1709STATIC int
cea2e8a9 1710S_intuit_more(pTHX_ register char *s)
79072805 1711{
3280af22 1712 if (PL_lex_brackets)
79072805
LW
1713 return TRUE;
1714 if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
1715 return TRUE;
1716 if (*s != '{' && *s != '[')
1717 return FALSE;
3280af22 1718 if (!PL_lex_inpat)
79072805
LW
1719 return TRUE;
1720
1721 /* In a pattern, so maybe we have {n,m}. */
1722 if (*s == '{') {
1723 s++;
1724 if (!isDIGIT(*s))
1725 return TRUE;
1726 while (isDIGIT(*s))
1727 s++;
1728 if (*s == ',')
1729 s++;
1730 while (isDIGIT(*s))
1731 s++;
1732 if (*s == '}')
1733 return FALSE;
1734 return TRUE;
1735
1736 }
1737
1738 /* On the other hand, maybe we have a character class */
1739
1740 s++;
1741 if (*s == ']' || *s == '^')
1742 return FALSE;
1743 else {
ffb4593c 1744 /* this is terrifying, and it works */
79072805
LW
1745 int weight = 2; /* let's weigh the evidence */
1746 char seen[256];
f27ffc4a 1747 unsigned char un_char = 255, last_un_char;
93a17b20 1748 char *send = strchr(s,']');
3280af22 1749 char tmpbuf[sizeof PL_tokenbuf * 4];
79072805
LW
1750
1751 if (!send) /* has to be an expression */
1752 return TRUE;
1753
1754 Zero(seen,256,char);
1755 if (*s == '$')
1756 weight -= 3;
1757 else if (isDIGIT(*s)) {
1758 if (s[1] != ']') {
1759 if (isDIGIT(s[1]) && s[2] == ']')
1760 weight -= 10;
1761 }
1762 else
1763 weight -= 100;
1764 }
1765 for (; s < send; s++) {
1766 last_un_char = un_char;
1767 un_char = (unsigned char)*s;
1768 switch (*s) {
1769 case '@':
1770 case '&':
1771 case '$':
1772 weight -= seen[un_char] * 10;
7e2040f0 1773 if (isALNUM_lazy_if(s+1,UTF)) {
8903cb82 1774 scan_ident(s, send, tmpbuf, sizeof tmpbuf, FALSE);
a0d0e21e 1775 if ((int)strlen(tmpbuf) > 1 && gv_fetchpv(tmpbuf,FALSE, SVt_PV))
79072805
LW
1776 weight -= 100;
1777 else
1778 weight -= 10;
1779 }
1780 else if (*s == '$' && s[1] &&
93a17b20
LW
1781 strchr("[#!%*<>()-=",s[1])) {
1782 if (/*{*/ strchr("])} =",s[2]))
79072805
LW
1783 weight -= 10;
1784 else
1785 weight -= 1;
1786 }
1787 break;
1788 case '\\':
1789 un_char = 254;
1790 if (s[1]) {
93a17b20 1791 if (strchr("wds]",s[1]))
79072805
LW
1792 weight += 100;
1793 else if (seen['\''] || seen['"'])
1794 weight += 1;
93a17b20 1795 else if (strchr("rnftbxcav",s[1]))
79072805
LW
1796 weight += 40;
1797 else if (isDIGIT(s[1])) {
1798 weight += 40;
1799 while (s[1] && isDIGIT(s[1]))
1800 s++;
1801 }
1802 }
1803 else
1804 weight += 100;
1805 break;
1806 case '-':
1807 if (s[1] == '\\')
1808 weight += 50;
93a17b20 1809 if (strchr("aA01! ",last_un_char))
79072805 1810 weight += 30;
93a17b20 1811 if (strchr("zZ79~",s[1]))
79072805 1812 weight += 30;
f27ffc4a
GS
1813 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
1814 weight -= 5; /* cope with negative subscript */
79072805
LW
1815 break;
1816 default:
93a17b20 1817 if (!isALNUM(last_un_char) && !strchr("$@&",last_un_char) &&
79072805
LW
1818 isALPHA(*s) && s[1] && isALPHA(s[1])) {
1819 char *d = tmpbuf;
1820 while (isALPHA(*s))
1821 *d++ = *s++;
1822 *d = '\0';
1823 if (keyword(tmpbuf, d - tmpbuf))
1824 weight -= 150;
1825 }
1826 if (un_char == last_un_char + 1)
1827 weight += 5;
1828 weight -= seen[un_char];
1829 break;
1830 }
1831 seen[un_char]++;
1832 }
1833 if (weight >= 0) /* probably a character class */
1834 return FALSE;
1835 }
1836
1837 return TRUE;
1838}
ffed7fef 1839
ffb4593c
NT
1840/*
1841 * S_intuit_method
1842 *
1843 * Does all the checking to disambiguate
1844 * foo bar
1845 * between foo(bar) and bar->foo. Returns 0 if not a method, otherwise
1846 * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
1847 *
1848 * First argument is the stuff after the first token, e.g. "bar".
1849 *
1850 * Not a method if bar is a filehandle.
1851 * Not a method if foo is a subroutine prototyped to take a filehandle.
1852 * Not a method if it's really "Foo $bar"
1853 * Method if it's "foo $bar"
1854 * Not a method if it's really "print foo $bar"
1855 * Method if it's really "foo package::" (interpreted as package->foo)
1856 * Not a method if bar is known to be a subroutne ("sub bar; foo bar")
3cb0bbe5 1857 * Not a method if bar is a filehandle or package, but is quoted with
ffb4593c
NT
1858 * =>
1859 */
1860
76e3520e 1861STATIC int
cea2e8a9 1862S_intuit_method(pTHX_ char *start, GV *gv)
a0d0e21e
LW
1863{
1864 char *s = start + (*start == '$');
3280af22 1865 char tmpbuf[sizeof PL_tokenbuf];
a0d0e21e
LW
1866 STRLEN len;
1867 GV* indirgv;
1868
1869 if (gv) {
b6c543e3 1870 CV *cv;
a0d0e21e
LW
1871 if (GvIO(gv))
1872 return 0;
b6c543e3
IZ
1873 if ((cv = GvCVu(gv))) {
1874 char *proto = SvPVX(cv);
1875 if (proto) {
1876 if (*proto == ';')
1877 proto++;
1878 if (*proto == '*')
1879 return 0;
1880 }
1881 } else
a0d0e21e
LW
1882 gv = 0;
1883 }
8903cb82 1884 s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
ffb4593c
NT
1885 /* start is the beginning of the possible filehandle/object,
1886 * and s is the end of it
1887 * tmpbuf is a copy of it
1888 */
1889
a0d0e21e 1890 if (*start == '$') {
3280af22 1891 if (gv || PL_last_lop_op == OP_PRINT || isUPPER(*PL_tokenbuf))
a0d0e21e
LW
1892 return 0;
1893 s = skipspace(s);
3280af22
NIS
1894 PL_bufptr = start;
1895 PL_expect = XREF;
a0d0e21e
LW
1896 return *s == '(' ? FUNCMETH : METHOD;
1897 }
1898 if (!keyword(tmpbuf, len)) {
c3e0f903
GS
1899 if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
1900 len -= 2;
1901 tmpbuf[len] = '\0';
1902 goto bare_package;
1903 }
1904 indirgv = gv_fetchpv(tmpbuf, FALSE, SVt_PVCV);
8ebc5c01 1905 if (indirgv && GvCVu(indirgv))
a0d0e21e
LW
1906 return 0;
1907 /* filehandle or package name makes it a method */
89bfa8cd 1908 if (!gv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, FALSE)) {
a0d0e21e 1909 s = skipspace(s);
3280af22 1910 if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
55497cff 1911 return 0; /* no assumptions -- "=>" quotes bearword */
c3e0f903 1912 bare_package:
3280af22 1913 PL_nextval[PL_nexttoke].opval = (OP*)newSVOP(OP_CONST, 0,
79cb57f6 1914 newSVpvn(tmpbuf,len));
3280af22
NIS
1915 PL_nextval[PL_nexttoke].opval->op_private = OPpCONST_BARE;
1916 PL_expect = XTERM;
a0d0e21e 1917 force_next(WORD);
3280af22 1918 PL_bufptr = s;
a0d0e21e
LW
1919 return *s == '(' ? FUNCMETH : METHOD;
1920 }
1921 }
1922 return 0;
1923}
1924
ffb4593c
NT
1925/*
1926 * S_incl_perldb
1927 * Return a string of Perl code to load the debugger. If PERL5DB
1928 * is set, it will return the contents of that, otherwise a
1929 * compile-time require of perl5db.pl.
1930 */
1931
76e3520e 1932STATIC char*
cea2e8a9 1933S_incl_perldb(pTHX)
a0d0e21e 1934{
3280af22 1935 if (PL_perldb) {
76e3520e 1936 char *pdb = PerlEnv_getenv("PERL5DB");
a0d0e21e
LW
1937
1938 if (pdb)
1939 return pdb;
91487cfc 1940 SETERRNO(0,SS$_NORMAL);
a0d0e21e
LW
1941 return "BEGIN { require 'perl5db.pl' }";
1942 }
1943 return "";
1944}
1945
1946
16d20bd9 1947/* Encoded script support. filter_add() effectively inserts a
4e553d73 1948 * 'pre-processing' function into the current source input stream.
16d20bd9
AD
1949 * Note that the filter function only applies to the current source file
1950 * (e.g., it will not affect files 'require'd or 'use'd by this one).
1951 *
1952 * The datasv parameter (which may be NULL) can be used to pass
1953 * private data to this instance of the filter. The filter function
1954 * can recover the SV using the FILTER_DATA macro and use it to
1955 * store private buffers and state information.
1956 *
1957 * The supplied datasv parameter is upgraded to a PVIO type
4755096e 1958 * and the IoDIRP/IoANY field is used to store the function pointer,
e0c19803 1959 * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
16d20bd9
AD
1960 * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
1961 * private use must be set using malloc'd pointers.
1962 */
16d20bd9
AD
1963
1964SV *
864dbfa3 1965Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
16d20bd9 1966{
f4c556ac
GS
1967 if (!funcp)
1968 return Nullsv;
1969
3280af22
NIS
1970 if (!PL_rsfp_filters)
1971 PL_rsfp_filters = newAV();
16d20bd9 1972 if (!datasv)
8c52afec 1973 datasv = NEWSV(255,0);
16d20bd9 1974 if (!SvUPGRADE(datasv, SVt_PVIO))
cea2e8a9 1975 Perl_die(aTHX_ "Can't upgrade filter_add data to SVt_PVIO");
4755096e 1976 IoANY(datasv) = (void *)funcp; /* stash funcp into spare field */
e0c19803 1977 IoFLAGS(datasv) |= IOf_FAKE_DIRP;
f4c556ac 1978 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
fe5a182c 1979 (void*)funcp, SvPV_nolen(datasv)));
3280af22
NIS
1980 av_unshift(PL_rsfp_filters, 1);
1981 av_store(PL_rsfp_filters, 0, datasv) ;
16d20bd9
AD
1982 return(datasv);
1983}
4e553d73 1984
16d20bd9
AD
1985
1986/* Delete most recently added instance of this filter function. */
a0d0e21e 1987void
864dbfa3 1988Perl_filter_del(pTHX_ filter_t funcp)
16d20bd9 1989{
e0c19803 1990 SV *datasv;
fe5a182c 1991 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p", (void*)funcp));
3280af22 1992 if (!PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
16d20bd9
AD
1993 return;
1994 /* if filter is on top of stack (usual case) just pop it off */
e0c19803 1995 datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
4755096e 1996 if (IoANY(datasv) == (void *)funcp) {
e0c19803 1997 IoFLAGS(datasv) &= ~IOf_FAKE_DIRP;
4755096e 1998 IoANY(datasv) = (void *)NULL;
3280af22 1999 sv_free(av_pop(PL_rsfp_filters));
e50aee73 2000
16d20bd9
AD
2001 return;
2002 }
2003 /* we need to search for the correct entry and clear it */
cea2e8a9 2004 Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
16d20bd9
AD
2005}
2006
2007
2008/* Invoke the n'th filter function for the current rsfp. */
2009I32
864dbfa3 2010Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
4e553d73
NIS
2011
2012
8ac85365 2013 /* 0 = read one text line */
a0d0e21e 2014{
16d20bd9
AD
2015 filter_t funcp;
2016 SV *datasv = NULL;
e50aee73 2017
3280af22 2018 if (!PL_rsfp_filters)
16d20bd9 2019 return -1;
3280af22 2020 if (idx > AvFILLp(PL_rsfp_filters)){ /* Any more filters? */
16d20bd9
AD
2021 /* Provide a default input filter to make life easy. */
2022 /* Note that we append to the line. This is handy. */
f4c556ac
GS
2023 DEBUG_P(PerlIO_printf(Perl_debug_log,
2024 "filter_read %d: from rsfp\n", idx));
4e553d73 2025 if (maxlen) {
16d20bd9
AD
2026 /* Want a block */
2027 int len ;
2028 int old_len = SvCUR(buf_sv) ;
2029
2030 /* ensure buf_sv is large enough */
2031 SvGROW(buf_sv, old_len + maxlen) ;
3280af22
NIS
2032 if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len, maxlen)) <= 0){
2033 if (PerlIO_error(PL_rsfp))
37120919
AD
2034 return -1; /* error */
2035 else
2036 return 0 ; /* end of file */
2037 }
16d20bd9
AD
2038 SvCUR_set(buf_sv, old_len + len) ;
2039 } else {
2040 /* Want a line */
3280af22
NIS
2041 if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
2042 if (PerlIO_error(PL_rsfp))
37120919
AD
2043 return -1; /* error */
2044 else
2045 return 0 ; /* end of file */
2046 }
16d20bd9
AD
2047 }
2048 return SvCUR(buf_sv);
2049 }
2050 /* Skip this filter slot if filter has been deleted */
3280af22 2051 if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef){
f4c556ac
GS
2052 DEBUG_P(PerlIO_printf(Perl_debug_log,
2053 "filter_read %d: skipped (filter deleted)\n",
2054 idx));
16d20bd9
AD
2055 return FILTER_READ(idx+1, buf_sv, maxlen); /* recurse */
2056 }
2057 /* Get function pointer hidden within datasv */
4755096e 2058 funcp = (filter_t)IoANY(datasv);
f4c556ac
GS
2059 DEBUG_P(PerlIO_printf(Perl_debug_log,
2060 "filter_read %d: via function %p (%s)\n",
fe5a182c 2061 idx, (void*)funcp, SvPV_nolen(datasv)));
16d20bd9
AD
2062 /* Call function. The function is expected to */
2063 /* call "FILTER_READ(idx+1, buf_sv)" first. */
37120919 2064 /* Return: <0:error, =0:eof, >0:not eof */
acfe0abc 2065 return (*funcp)(aTHX_ idx, buf_sv, maxlen);
16d20bd9
AD
2066}
2067
76e3520e 2068STATIC char *
cea2e8a9 2069S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append)
16d20bd9 2070{
c39cd008 2071#ifdef PERL_CR_FILTER
3280af22 2072 if (!PL_rsfp_filters) {
c39cd008 2073 filter_add(S_cr_textfilter,NULL);
a868473f
NIS
2074 }
2075#endif
3280af22 2076 if (PL_rsfp_filters) {
16d20bd9 2077
55497cff
PP
2078 if (!append)
2079 SvCUR_set(sv, 0); /* start with empty line */
16d20bd9
AD
2080 if (FILTER_READ(0, sv, 0) > 0)
2081 return ( SvPVX(sv) ) ;
2082 else
2083 return Nullch ;
2084 }
9d116dd7 2085 else
fd049845 2086 return (sv_gets(sv, fp, append));
a0d0e21e
LW
2087}
2088
01ec43d0
GS
2089STATIC HV *
2090S_find_in_my_stash(pTHX_ char *pkgname, I32 len)
def3634b
GS
2091{
2092 GV *gv;
2093
01ec43d0 2094 if (len == 11 && *pkgname == '_' && strEQ(pkgname, "__PACKAGE__"))
def3634b
GS
2095 return PL_curstash;
2096
2097 if (len > 2 &&
2098 (pkgname[len - 2] == ':' && pkgname[len - 1] == ':') &&
01ec43d0
GS
2099 (gv = gv_fetchpv(pkgname, FALSE, SVt_PVHV)))
2100 {
2101 return GvHV(gv); /* Foo:: */
def3634b
GS
2102 }
2103
2104 /* use constant CLASS => 'MyClass' */
2105 if ((gv = gv_fetchpv(pkgname, FALSE, SVt_PVCV))) {
2106 SV *sv;
2107 if (GvCV(gv) && (sv = cv_const_sv(GvCV(gv)))) {
2108 pkgname = SvPV_nolen(sv);
2109 }
2110 }
2111
2112 return gv_stashpv(pkgname, FALSE);
2113}
a0d0e21e 2114
748a9306
LW
2115#ifdef DEBUGGING
2116 static char* exp_name[] =
09bef843
SB
2117 { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
2118 "ATTRTERM", "TERMBLOCK"
2119 };
748a9306 2120#endif
463ee0b2 2121
02aa26ce
NT
2122/*
2123 yylex
2124
2125 Works out what to call the token just pulled out of the input
2126 stream. The yacc parser takes care of taking the ops we return and
2127 stitching them into a tree.
2128
2129 Returns:
2130 PRIVATEREF
2131
2132 Structure:
2133 if read an identifier
2134 if we're in a my declaration
2135 croak if they tried to say my($foo::bar)
2136 build the ops for a my() declaration
2137 if it's an access to a my() variable
2138 are we in a sort block?
2139 croak if my($a); $a <=> $b
2140 build ops for access to a my() variable
2141 if in a dq string, and they've said @foo and we can't find @foo
2142 croak
2143 build ops for a bareword
2144 if we already built the token before, use it.
2145*/
2146
dba4d153 2147#ifdef USE_PURE_BISON
864dbfa3 2148int
dba4d153 2149Perl_yylex_r(pTHX_ YYSTYPE *lvalp, int *lcharp)
378cc40b 2150{
20141f0e
RI
2151 int r;
2152
6f202aea 2153 yyactlevel++;
20141f0e
RI
2154 yylval_pointer[yyactlevel] = lvalp;
2155 yychar_pointer[yyactlevel] = lcharp;
b73d6f50
RI
2156 if (yyactlevel >= YYMAXLEVEL)
2157 Perl_croak(aTHX_ "panic: YYMAXLEVEL");
20141f0e 2158
dba4d153 2159 r = Perl_yylex(aTHX);
20141f0e 2160
d8ae6756
RI
2161 if (yyactlevel > 0)
2162 yyactlevel--;
20141f0e
RI
2163
2164 return r;
2165}
dba4d153 2166#endif
20141f0e 2167
dba4d153
JH
2168#ifdef __SC__
2169#pragma segment Perl_yylex
2170#endif
dba4d153 2171int
dba4d153 2172Perl_yylex(pTHX)
20141f0e 2173{
79072805 2174 register char *s;
378cc40b 2175 register char *d;
79072805 2176 register I32 tmp;
463ee0b2 2177 STRLEN len;
161b471a
NIS
2178 GV *gv = Nullgv;
2179 GV **gvp = 0;
aa7440fb 2180 bool bof = FALSE;
a687059c 2181
02aa26ce 2182 /* check if there's an identifier for us to look at */
8eceec63 2183 if (PL_pending_ident)
e930465f 2184 return S_pending_ident(aTHX);
bbce6d69 2185
02aa26ce
NT
2186 /* no identifier pending identification */
2187
3280af22 2188 switch (PL_lex_state) {
79072805
LW
2189#ifdef COMMENTARY
2190 case LEX_NORMAL: /* Some compilers will produce faster */
2191 case LEX_INTERPNORMAL: /* code if we comment these out. */
2192 break;
2193#endif
2194
09bef843 2195 /* when we've already built the next token, just pull it out of the queue */
79072805 2196 case LEX_KNOWNEXT:
3280af22
NIS
2197 PL_nexttoke--;
2198 yylval = PL_nextval[PL_nexttoke];
2199 if (!PL_nexttoke) {
2200 PL_lex_state = PL_lex_defer;
2201 PL_expect = PL_lex_expect;
2202 PL_lex_defer = LEX_NORMAL;
463ee0b2 2203 }
607df283 2204 DEBUG_T({ PerlIO_printf(Perl_debug_log,
4659c93f 2205 "### Next token after '%s' was known, type %"IVdf"\n", PL_bufptr,
5f80b19c 2206 (IV)PL_nexttype[PL_nexttoke]); });
607df283 2207
3280af22 2208 return(PL_nexttype[PL_nexttoke]);
79072805 2209
02aa26ce 2210 /* interpolated case modifiers like \L \U, including \Q and \E.
3280af22 2211 when we get here, PL_bufptr is at the \
02aa26ce 2212 */
79072805
LW
2213 case LEX_INTERPCASEMOD:
2214#ifdef DEBUGGING
3280af22 2215 if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
cea2e8a9 2216 Perl_croak(aTHX_ "panic: INTERPCASEMOD");
79072805 2217#endif
02aa26ce 2218 /* handle \E or end of string */
3280af22 2219 if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
a0d0e21e 2220 char oldmod;
02aa26ce
NT
2221
2222 /* if at a \E */
3280af22
NIS
2223 if (PL_lex_casemods) {
2224 oldmod = PL_lex_casestack[--PL_lex_casemods];
2225 PL_lex_casestack[PL_lex_casemods] = '\0';
02aa26ce 2226
3280af22
NIS
2227 if (PL_bufptr != PL_bufend && strchr("LUQ", oldmod)) {
2228 PL_bufptr += 2;
2229 PL_lex_state = LEX_INTERPCONCAT;
a0d0e21e 2230 }
79072805
LW
2231 return ')';
2232 }
3280af22
NIS
2233 if (PL_bufptr != PL_bufend)
2234 PL_bufptr += 2;
2235 PL_lex_state = LEX_INTERPCONCAT;
cea2e8a9 2236 return yylex();
79072805
LW
2237 }
2238 else {
607df283 2239 DEBUG_T({ PerlIO_printf(Perl_debug_log,
5f80b19c 2240 "### Saw case modifier at '%s'\n", PL_bufptr); });
3280af22 2241 s = PL_bufptr + 1;
79072805
LW
2242 if (strnEQ(s, "L\\u", 3) || strnEQ(s, "U\\l", 3))
2243 tmp = *s, *s = s[2], s[2] = tmp; /* misordered... */
a0d0e21e 2244 if (strchr("LU", *s) &&
3280af22 2245 (strchr(PL_lex_casestack, 'L') || strchr(PL_lex_casestack, 'U')))
a0d0e21e 2246 {
3280af22 2247 PL_lex_casestack[--PL_lex_casemods] = '\0';
a0d0e21e
LW
2248 return ')';
2249 }
3280af22
NIS
2250 if (PL_lex_casemods > 10) {
2251 char* newlb = Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
2252 if (newlb != PL_lex_casestack) {
a0d0e21e 2253 SAVEFREEPV(newlb);
3280af22 2254 PL_lex_casestack = newlb;
a0d0e21e
LW
2255 }
2256 }
3280af22
NIS
2257 PL_lex_casestack[PL_lex_casemods++] = *s;
2258 PL_lex_casestack[PL_lex_casemods] = '\0';
2259 PL_lex_state = LEX_INTERPCONCAT;
2260 PL_nextval[PL_nexttoke].ival = 0;
79072805
LW
2261 force_next('(');
2262 if (*s == 'l')
3280af22 2263 PL_nextval[PL_nexttoke].ival = OP_LCFIRST;
79072805 2264 else if (*s == 'u')
3280af22 2265 PL_nextval[PL_nexttoke].ival = OP_UCFIRST;
79072805 2266 else if (*s == 'L')
3280af22 2267 PL_nextval[PL_nexttoke].ival = OP_LC;
79072805 2268 else if (*s == 'U')
3280af22 2269 PL_nextval[PL_nexttoke].ival = OP_UC;
a0d0e21e 2270 else if (*s == 'Q')
3280af22 2271 PL_nextval[PL_nexttoke].ival = OP_QUOTEMETA;
79072805 2272 else
cea2e8a9 2273 Perl_croak(aTHX_ "panic: yylex");
3280af22 2274 PL_bufptr = s + 1;
79072805 2275 force_next(FUNC);
3280af22
NIS
2276 if (PL_lex_starts) {
2277 s = PL_bufptr;
2278 PL_lex_starts = 0;
79072805
LW
2279 Aop(OP_CONCAT);
2280 }
2281 else
cea2e8a9 2282 return yylex();
79072805
LW
2283 }
2284
55497cff
PP
2285 case LEX_INTERPPUSH:
2286 return sublex_push();
2287
79072805 2288 case LEX_INTERPSTART:
3280af22 2289 if (PL_bufptr == PL_bufend)
79072805 2290 return sublex_done();
607df283 2291 DEBUG_T({ PerlIO_printf(Perl_debug_log,
5f80b19c 2292 "### Interpolated variable at '%s'\n", PL_bufptr); });
3280af22
NIS
2293 PL_expect = XTERM;
2294 PL_lex_dojoin = (*PL_bufptr == '@');
2295 PL_lex_state = LEX_INTERPNORMAL;
2296 if (PL_lex_dojoin) {
2297 PL_nextval[PL_nexttoke].ival = 0;
79072805 2298 force_next(',');
4d1ff10f 2299#ifdef USE_5005THREADS
533c011a
NIS
2300 PL_nextval[PL_nexttoke].opval = newOP(OP_THREADSV, 0);
2301 PL_nextval[PL_nexttoke].opval->op_targ = find_threadsv("\"");
554b3eca
MB
2302 force_next(PRIVATEREF);
2303#else
a0d0e21e 2304 force_ident("\"", '$');
4d1ff10f 2305#endif /* USE_5005THREADS */
3280af22 2306 PL_nextval[PL_nexttoke].ival = 0;
79072805 2307 force_next('$');
3280af22 2308 PL_nextval[PL_nexttoke].ival = 0;
79072805 2309 force_next('(');
3280af22 2310 PL_nextval[PL_nexttoke].ival = OP_JOIN; /* emulate join($", ...) */
79072805
LW
2311 force_next(FUNC);
2312 }
3280af22
NIS
2313 if (PL_lex_starts++) {
2314 s = PL_bufptr;
79072805
LW
2315 Aop(OP_CONCAT);
2316 }
cea2e8a9 2317 return yylex();
79072805
LW
2318
2319 case LEX_INTERPENDMAYBE:
3280af22
NIS
2320 if (intuit_more(PL_bufptr)) {
2321 PL_lex_state = LEX_INTERPNORMAL; /* false alarm, more expr */
79072805
LW
2322 break;
2323 }
2324 /* FALL THROUGH */
2325
2326 case LEX_INTERPEND:
3280af22
NIS
2327 if (PL_lex_dojoin) {
2328 PL_lex_dojoin = FALSE;
2329 PL_lex_state = LEX_INTERPCONCAT;
79072805
LW
2330 return ')';
2331 }
43a16006 2332 if (PL_lex_inwhat == OP_SUBST && PL_linestr == PL_lex_repl
25da4f38 2333 && SvEVALED(PL_lex_repl))
43a16006 2334 {
e9fa98b2 2335 if (PL_bufptr != PL_bufend)
cea2e8a9 2336 Perl_croak(aTHX_ "Bad evalled substitution pattern");
e9fa98b2
HS
2337 PL_lex_repl = Nullsv;
2338 }
79072805
LW
2339 /* FALLTHROUGH */
2340 case LEX_INTERPCONCAT:
2341#ifdef DEBUGGING
3280af22 2342 if (PL_lex_brackets)
cea2e8a9 2343 Perl_croak(aTHX_ "panic: INTERPCONCAT");
79072805 2344#endif
3280af22 2345 if (PL_bufptr == PL_bufend)
79072805
LW
2346 return sublex_done();
2347
3280af22
NIS
2348 if (SvIVX(PL_linestr) == '\'') {
2349 SV *sv = newSVsv(PL_linestr);
2350 if (!PL_lex_inpat)
76e3520e 2351 sv = tokeq(sv);
3280af22 2352 else if ( PL_hints & HINT_NEW_RE )
b3ac6de7 2353 sv = new_constant(NULL, 0, "qr", sv, sv, "q");
79072805 2354 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
3280af22 2355 s = PL_bufend;
79072805
LW
2356 }
2357 else {
3280af22 2358 s = scan_const(PL_bufptr);
79072805 2359 if (*s == '\\')
3280af22 2360 PL_lex_state = LEX_INTERPCASEMOD;
79072805 2361 else
3280af22 2362 PL_lex_state = LEX_INTERPSTART;
79072805
LW
2363 }
2364
3280af22
NIS
2365 if (s != PL_bufptr) {
2366 PL_nextval[PL_nexttoke] = yylval;
2367 PL_expect = XTERM;
79072805 2368 force_next(THING);
3280af22 2369 if (PL_lex_starts++)
79072805
LW
2370 Aop(OP_CONCAT);
2371 else {
3280af22 2372 PL_bufptr = s;
cea2e8a9 2373 return yylex();
79072805
LW
2374 }
2375 }
2376
cea2e8a9 2377 return yylex();
a0d0e21e 2378 case LEX_FORMLINE:
3280af22
NIS
2379 PL_lex_state = LEX_NORMAL;
2380 s = scan_formline(PL_bufptr);
2381 if (!PL_lex_formbrack)
a0d0e21e
LW
2382 goto rightbracket;
2383 OPERATOR(';');
79072805
LW
2384 }
2385
3280af22
NIS
2386 s = PL_bufptr;
2387 PL_oldoldbufptr = PL_oldbufptr;
2388 PL_oldbufptr = s;
607df283 2389 DEBUG_T( {
bf49b057
GS
2390 PerlIO_printf(Perl_debug_log, "### Tokener expecting %s at %s\n",
2391 exp_name[PL_expect], s);
5f80b19c 2392 } );
463ee0b2
LW
2393
2394 retry:
378cc40b
LW
2395 switch (*s) {
2396 default:
7e2040f0 2397 if (isIDFIRST_lazy_if(s,UTF))
834a4ddd 2398 goto keylookup;
cea2e8a9 2399 Perl_croak(aTHX_ "Unrecognized character \\x%02X", *s & 255);
e929a76b
LW
2400 case 4:
2401 case 26:
2402 goto fake_eof; /* emulate EOF on ^D or ^Z */
378cc40b 2403 case 0:
3280af22
NIS
2404 if (!PL_rsfp) {
2405 PL_last_uni = 0;
2406 PL_last_lop = 0;
2407 if (PL_lex_brackets)
d98d5fff 2408 yyerror("Missing right curly or square bracket");
4e553d73 2409 DEBUG_T( { PerlIO_printf(Perl_debug_log,
607df283 2410 "### Tokener got EOF\n");
5f80b19c 2411 } );
79072805 2412 TOKEN(0);
463ee0b2 2413 }
3280af22 2414 if (s++ < PL_bufend)
a687059c 2415 goto retry; /* ignore stray nulls */
3280af22
NIS
2416 PL_last_uni = 0;
2417 PL_last_lop = 0;
2418 if (!PL_in_eval && !PL_preambled) {
2419 PL_preambled = TRUE;
2420 sv_setpv(PL_linestr,incl_perldb());
2421 if (SvCUR(PL_linestr))
2422 sv_catpv(PL_linestr,";");
2423 if (PL_preambleav){
2424 while(AvFILLp(PL_preambleav) >= 0) {
2425 SV *tmpsv = av_shift(PL_preambleav);
2426 sv_catsv(PL_linestr, tmpsv);
2427 sv_catpv(PL_linestr, ";");
91b7def8
PP
2428 sv_free(tmpsv);
2429 }
3280af22
NIS
2430 sv_free((SV*)PL_preambleav);
2431 PL_preambleav = NULL;
91b7def8 2432 }
3280af22
NIS
2433 if (PL_minus_n || PL_minus_p) {
2434 sv_catpv(PL_linestr, "LINE: while (<>) {");
2435 if (PL_minus_l)
2436 sv_catpv(PL_linestr,"chomp;");
2437 if (PL_minus_a) {
3280af22
NIS
2438 if (PL_minus_F) {
2439 if (strchr("/'\"", *PL_splitstr)
2440 && strchr(PL_splitstr + 1, *PL_splitstr))
3db68c4c 2441 Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
54310121
PP
2442 else {
2443 char delim;
2444 s = "'~#\200\1'"; /* surely one char is unused...*/
3280af22 2445 while (s[1] && strchr(PL_splitstr, *s)) s++;
54310121 2446 delim = *s;
75c72d73 2447 Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s%c",
46fc3d4c 2448 "q" + (delim == '\''), delim);
3280af22 2449 for (s = PL_splitstr; *s; s++) {
54310121 2450 if (*s == '\\')
3280af22
NIS
2451 sv_catpvn(PL_linestr, "\\", 1);
2452 sv_catpvn(PL_linestr, s, 1);
54310121 2453 }
cea2e8a9 2454 Perl_sv_catpvf(aTHX_ PL_linestr, "%c);", delim);
54310121 2455 }
2304df62
AD
2456 }
2457 else
75c72d73 2458 sv_catpv(PL_linestr,"our @F=split(' ');");
2304df62 2459 }
79072805 2460 }
3280af22
NIS
2461 sv_catpv(PL_linestr, "\n");
2462 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2463 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 2464 PL_last_lop = PL_last_uni = Nullch;
3280af22 2465 if (PERLDB_LINE && PL_curstash != PL_debstash) {
a0d0e21e
LW
2466 SV *sv = NEWSV(85,0);
2467
2468 sv_upgrade(sv, SVt_PVMG);
3280af22 2469 sv_setsv(sv,PL_linestr);
0ac0412a
MJD
2470 (void)SvIOK_on(sv);
2471 SvIVX(sv) = 0;
57843af0 2472 av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv);
a0d0e21e 2473 }
79072805 2474 goto retry;
a687059c 2475 }
e929a76b 2476 do {
aa7440fb 2477 bof = PL_rsfp ? TRUE : FALSE;
7e28d3af
JH
2478 if ((s = filter_gets(PL_linestr, PL_rsfp, 0)) == Nullch) {
2479 fake_eof:
2480 if (PL_rsfp) {
2481 if (PL_preprocess && !PL_in_eval)
2482 (void)PerlProc_pclose(PL_rsfp);
2483 else if ((PerlIO *)PL_rsfp == PerlIO_stdin())
2484 PerlIO_clearerr(PL_rsfp);
2485 else
2486 (void)PerlIO_close(PL_rsfp);
2487 PL_rsfp = Nullfp;
2488 PL_doextract = FALSE;
2489 }
2490 if (!PL_in_eval && (PL_minus_n || PL_minus_p)) {
2491 sv_setpv(PL_linestr,PL_minus_p ? ";}continue{print" : "");
2492 sv_catpv(PL_linestr,";}");
2493 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2494 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 2495 PL_last_lop = PL_last_uni = Nullch;
7e28d3af
JH
2496 PL_minus_n = PL_minus_p = 0;
2497 goto retry;
2498 }
2499 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
207e3d1a 2500 PL_last_lop = PL_last_uni = Nullch;
7e28d3af
JH
2501 sv_setpv(PL_linestr,"");
2502 TOKEN(';'); /* not infinite loop because rsfp is NULL now */
2503 }
2504 /* if it looks like the start of a BOM, check if it in fact is */
2505 else if (bof && (!*s || *(U8*)s == 0xEF || *(U8*)s >= 0xFE)) {
226017aa 2506#ifdef PERLIO_IS_STDIO
e3f494f1
JH
2507# ifdef __GNU_LIBRARY__
2508# if __GNU_LIBRARY__ == 1 /* Linux glibc5 */
226017aa
DD
2509# define FTELL_FOR_PIPE_IS_BROKEN
2510# endif
e3f494f1
JH
2511# else
2512# ifdef __GLIBC__
2513# if __GLIBC__ == 1 /* maybe some glibc5 release had it like this? */
2514# define FTELL_FOR_PIPE_IS_BROKEN
2515# endif
2516# endif
226017aa
DD
2517# endif
2518#endif
2519#ifdef FTELL_FOR_PIPE_IS_BROKEN
2520 /* This loses the possibility to detect the bof
2521 * situation on perl -P when the libc5 is being used.
2522 * Workaround? Maybe attach some extra state to PL_rsfp?
2523 */
2524 if (!PL_preprocess)
7e28d3af 2525 bof = PerlIO_tell(PL_rsfp) == SvCUR(PL_linestr);
226017aa 2526#else
7e28d3af 2527 bof = PerlIO_tell(PL_rsfp) == SvCUR(PL_linestr);
226017aa 2528#endif
7e28d3af 2529 if (bof) {
3280af22 2530 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
7e28d3af 2531 s = swallow_bom((U8*)s);
e929a76b 2532 }
378cc40b 2533 }
3280af22 2534 if (PL_doextract) {
a0d0e21e 2535 if (*s == '#' && s[1] == '!' && instr(s,"perl"))
3280af22 2536 PL_doextract = FALSE;
a0d0e21e
LW
2537
2538 /* Incest with pod. */
2539 if (*s == '=' && strnEQ(s, "=cut", 4)) {
3280af22
NIS
2540 sv_setpv(PL_linestr, "");
2541 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2542 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 2543 PL_last_lop = PL_last_uni = Nullch;
3280af22 2544 PL_doextract = FALSE;
a0d0e21e 2545 }
4e553d73 2546 }
463ee0b2 2547 incline(s);
3280af22
NIS
2548 } while (PL_doextract);
2549 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = s;
2550 if (PERLDB_LINE && PL_curstash != PL_debstash) {
79072805 2551 SV *sv = NEWSV(85,0);
a687059c 2552
93a17b20 2553 sv_upgrade(sv, SVt_PVMG);
3280af22 2554 sv_setsv(sv,PL_linestr);
0ac0412a
MJD
2555 (void)SvIOK_on(sv);
2556 SvIVX(sv) = 0;
57843af0 2557 av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv);
a687059c 2558 }
3280af22 2559 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 2560 PL_last_lop = PL_last_uni = Nullch;
57843af0 2561 if (CopLINE(PL_curcop) == 1) {
3280af22 2562 while (s < PL_bufend && isSPACE(*s))
79072805 2563 s++;
a0d0e21e 2564 if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
79072805 2565 s++;
44a8e56a 2566 d = Nullch;
3280af22 2567 if (!PL_in_eval) {
44a8e56a
PP
2568 if (*s == '#' && *(s+1) == '!')
2569 d = s + 2;
2570#ifdef ALTERNATE_SHEBANG
2571 else {
2572 static char as[] = ALTERNATE_SHEBANG;
2573 if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
2574 d = s + (sizeof(as) - 1);
2575 }
2576#endif /* ALTERNATE_SHEBANG */
2577 }
2578 if (d) {
b8378b72 2579 char *ipath;
774d564b 2580 char *ipathend;
b8378b72 2581
774d564b 2582 while (isSPACE(*d))
b8378b72
CS
2583 d++;
2584 ipath = d;
774d564b
PP
2585 while (*d && !isSPACE(*d))
2586 d++;
2587 ipathend = d;
2588
2589#ifdef ARG_ZERO_IS_SCRIPT
2590 if (ipathend > ipath) {
2591 /*
2592 * HP-UX (at least) sets argv[0] to the script name,
2593 * which makes $^X incorrect. And Digital UNIX and Linux,
2594 * at least, set argv[0] to the basename of the Perl
2595 * interpreter. So, having found "#!", we'll set it right.
2596 */
ee2f7564 2597 SV *x = GvSV(gv_fetchpv("\030", TRUE, SVt_PV)); /* $^X */
774d564b 2598 assert(SvPOK(x) || SvGMAGICAL(x));
cc49e20b 2599 if (sv_eq(x, CopFILESV(PL_curcop))) {
774d564b 2600 sv_setpvn(x, ipath, ipathend - ipath);
9607fc9c
PP
2601 SvSETMAGIC(x);
2602 }
774d564b 2603 TAINT_NOT; /* $^X is always tainted, but that's OK */
8ebc5c01 2604 }
774d564b 2605#endif /* ARG_ZERO_IS_SCRIPT */
b8378b72
CS
2606
2607 /*
2608 * Look for options.
2609 */
748a9306 2610 d = instr(s,"perl -");
84e30d1a 2611 if (!d) {
748a9306 2612 d = instr(s,"perl");
84e30d1a
GS
2613#if defined(DOSISH)
2614 /* avoid getting into infinite loops when shebang
2615 * line contains "Perl" rather than "perl" */
2616 if (!d) {
2617 for (d = ipathend-4; d >= ipath; --d) {
2618 if ((*d == 'p' || *d == 'P')
2619 && !ibcmp(d, "perl", 4))
2620 {
2621 break;
2622 }
2623 }
2624 if (d < ipath)
2625 d = Nullch;
2626 }
2627#endif
2628 }
44a8e56a
PP
2629#ifdef ALTERNATE_SHEBANG
2630 /*
2631 * If the ALTERNATE_SHEBANG on this system starts with a
2632 * character that can be part of a Perl expression, then if
2633 * we see it but not "perl", we're probably looking at the
2634 * start of Perl code, not a request to hand off to some
2635 * other interpreter. Similarly, if "perl" is there, but
2636 * not in the first 'word' of the line, we assume the line
2637 * contains the start of the Perl program.
44a8e56a
PP
2638 */
2639 if (d && *s != '#') {
774d564b 2640 char *c = ipath;
44a8e56a
PP
2641 while (*c && !strchr("; \t\r\n\f\v#", *c))
2642 c++;
2643 if (c < d)
2644 d = Nullch; /* "perl" not in first word; ignore */
2645 else
2646 *s = '#'; /* Don't try to parse shebang line */
2647 }
774d564b 2648#endif /* ALTERNATE_SHEBANG */
bf4acbe4 2649#ifndef MACOS_TRADITIONAL
748a9306 2650 if (!d &&
44a8e56a 2651 *s == '#' &&
774d564b 2652 ipathend > ipath &&
3280af22 2653 !PL_minus_c &&
748a9306 2654 !instr(s,"indir") &&
3280af22 2655 instr(PL_origargv[0],"perl"))
748a9306 2656 {
9f68db38 2657 char **newargv;
9f68db38 2658
774d564b
PP
2659 *ipathend = '\0';
2660 s = ipathend + 1;
3280af22 2661 while (s < PL_bufend && isSPACE(*s))
9f68db38 2662 s++;
3280af22
NIS
2663 if (s < PL_bufend) {
2664 Newz(899,newargv,PL_origargc+3,char*);
9f68db38 2665 newargv[1] = s;
3280af22 2666 while (s < PL_bufend && !isSPACE(*s))
9f68db38
LW
2667 s++;
2668 *s = '\0';
3280af22 2669 Copy(PL_origargv+1, newargv+2, PL_origargc+1, char*);
9f68db38
LW
2670 }
2671 else
3280af22 2672 newargv = PL_origargv;
774d564b 2673 newargv[0] = ipath;
b4748376 2674 PerlProc_execv(ipath, EXEC_ARGV_CAST(newargv));
cea2e8a9 2675 Perl_croak(aTHX_ "Can't exec %s", ipath);
9f68db38 2676 }
bf4acbe4 2677#endif
748a9306 2678 if (d) {
3280af22
NIS
2679 U32 oldpdb = PL_perldb;
2680 bool oldn = PL_minus_n;
2681 bool oldp = PL_minus_p;
748a9306
LW
2682
2683 while (*d && !isSPACE(*d)) d++;
bf4acbe4 2684 while (SPACE_OR_TAB(*d)) d++;
748a9306
LW
2685
2686 if (*d++ == '-') {
a11ec5a9 2687 bool switches_done = PL_doswitches;
8cc95fdb
PP
2688 do {
2689 if (*d == 'M' || *d == 'm') {
2690 char *m = d;
2691 while (*d && !isSPACE(*d)) d++;
cea2e8a9 2692 Perl_croak(aTHX_ "Too late for \"-%.*s\" option",
8cc95fdb
PP
2693 (int)(d - m), m);
2694 }
2695 d = moreswitches(d);
2696 } while (d);
155aba94
GS
2697 if ((PERLDB_LINE && !oldpdb) ||
2698 ((PL_minus_n || PL_minus_p) && !(oldn || oldp)))
b084f20b
PP
2699 /* if we have already added "LINE: while (<>) {",
2700 we must not do it again */
748a9306 2701 {
3280af22
NIS
2702 sv_setpv(PL_linestr, "");
2703 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2704 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 2705 PL_last_lop = PL_last_uni = Nullch;
3280af22 2706 PL_preambled = FALSE;
84902520 2707 if (PERLDB_LINE)
3280af22 2708 (void)gv_fetchfile(PL_origfilename);
748a9306
LW
2709 goto retry;
2710 }
a11ec5a9
RGS
2711 if (PL_doswitches && !switches_done) {
2712 int argc = PL_origargc;
2713 char **argv = PL_origargv;
2714 do {
2715 argc--,argv++;
2716 } while (argc && argv[0][0] == '-' && argv[0][1]);
2717 init_argv_symbols(argc,argv);
2718 }
a0d0e21e 2719 }
79072805 2720 }
9f68db38 2721 }
79072805 2722 }
3280af22
NIS
2723 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
2724 PL_bufptr = s;
2725 PL_lex_state = LEX_FORMLINE;
cea2e8a9 2726 return yylex();
ae986130 2727 }
378cc40b 2728 goto retry;
4fdae800 2729 case '\r':
6a27c188 2730#ifdef PERL_STRICT_CR
cea2e8a9 2731 Perl_warn(aTHX_ "Illegal character \\%03o (carriage return)", '\r');
4e553d73 2732 Perl_croak(aTHX_
cc507455 2733 "\t(Maybe you didn't strip carriage returns after a network transfer?)\n");
a868473f 2734#endif
4fdae800 2735 case ' ': case '\t': case '\f': case 013:
bf4acbe4
GS
2736#ifdef MACOS_TRADITIONAL
2737 case '\312':
2738#endif
378cc40b
LW
2739 s++;
2740 goto retry;
378cc40b 2741 case '#':
e929a76b 2742 case '\n':
3280af22 2743 if (PL_lex_state != LEX_NORMAL || (PL_in_eval && !PL_rsfp)) {
df0deb90
GS
2744 if (*s == '#' && s == PL_linestart && PL_in_eval && !PL_rsfp) {
2745 /* handle eval qq[#line 1 "foo"\n ...] */
2746 CopLINE_dec(PL_curcop);
2747 incline(s);
2748 }
3280af22 2749 d = PL_bufend;
a687059c 2750 while (s < d && *s != '\n')
378cc40b 2751 s++;
0f85fab0 2752 if (s < d)
378cc40b 2753 s++;
78c267c1 2754 else if (s > d) /* Found by Ilya: feed random input to Perl. */
a8406387 2755 Perl_croak(aTHX_ "panic: input overflow");
463ee0b2 2756 incline(s);
3280af22
NIS
2757 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
2758 PL_bufptr = s;
2759 PL_lex_state = LEX_FORMLINE;
cea2e8a9 2760 return yylex();
a687059c 2761 }
378cc40b 2762 }
a687059c 2763 else {
378cc40b 2764 *s = '\0';
3280af22 2765 PL_bufend = s;
a687059c 2766 }
378cc40b
LW
2767 goto retry;
2768 case '-':
79072805 2769 if (s[1] && isALPHA(s[1]) && !isALNUM(s[2])) {
e5edeb50
JH
2770 I32 ftst = 0;
2771
378cc40b 2772 s++;
3280af22 2773 PL_bufptr = s;
748a9306
LW
2774 tmp = *s++;
2775
bf4acbe4 2776 while (s < PL_bufend && SPACE_OR_TAB(*s))
748a9306
LW
2777 s++;
2778
2779 if (strnEQ(s,"=>",2)) {
3280af22 2780 s = force_word(PL_bufptr,WORD,FALSE,FALSE,FALSE);
4e553d73 2781 DEBUG_T( { PerlIO_printf(Perl_debug_log,
607df283 2782 "### Saw unary minus before =>, forcing word '%s'\n", s);
5f80b19c 2783 } );
748a9306
LW
2784 OPERATOR('-'); /* unary minus */
2785 }
3280af22 2786 PL_last_uni = PL_oldbufptr;
748a9306 2787 switch (tmp) {
e5edeb50
JH
2788 case 'r': ftst = OP_FTEREAD; break;
2789 case 'w': ftst = OP_FTEWRITE; break;
2790 case 'x': ftst = OP_FTEEXEC; break;
2791 case 'o': ftst = OP_FTEOWNED; break;
2792 case 'R': ftst = OP_FTRREAD; break;
2793 case 'W': ftst = OP_FTRWRITE; break;
2794 case 'X': ftst = OP_FTREXEC; break;
2795 case 'O': ftst = OP_FTROWNED; break;
2796 case 'e': ftst = OP_FTIS; break;
2797 case 'z': ftst = OP_FTZERO; break;
2798 case 's': ftst = OP_FTSIZE; break;
2799 case 'f': ftst = OP_FTFILE; break;
2800 case 'd': ftst = OP_FTDIR; break;
2801 case 'l': ftst = OP_FTLINK; break;
2802 case 'p': ftst = OP_FTPIPE; break;
2803 case 'S': ftst = OP_FTSOCK; break;
2804 case 'u': ftst = OP_FTSUID; break;
2805 case 'g': ftst = OP_FTSGID; break;
2806 case 'k': ftst = OP_FTSVTX; break;
2807 case 'b': ftst = OP_FTBLK; break;
2808 case 'c': ftst = OP_FTCHR; break;
2809 case 't': ftst = OP_FTTTY; break;
2810 case 'T': ftst = OP_FTTEXT; break;
2811 case 'B': ftst = OP_FTBINARY; break;
2812 case 'M': case 'A': case 'C':
2813 gv_fetchpv("\024",TRUE, SVt_PV);
2814 switch (tmp) {
2815 case 'M': ftst = OP_FTMTIME; break;
2816 case 'A': ftst = OP_FTATIME; break;
2817 case 'C': ftst = OP_FTCTIME; break;
2818 default: break;
2819 }
2820 break;
378cc40b 2821 default:
378cc40b
LW
2822 break;
2823 }
e5edeb50
JH
2824 if (ftst) {
2825 PL_last_lop_op = ftst;
4e553d73 2826 DEBUG_T( { PerlIO_printf(Perl_debug_log,
0844c848 2827 "### Saw file test %c\n", (int)ftst);
5f80b19c 2828 } );
e5edeb50
JH
2829 FTST(ftst);
2830 }
2831 else {
2832 /* Assume it was a minus followed by a one-letter named
2833 * subroutine call (or a -bareword), then. */
95c31fe3 2834 DEBUG_T( { PerlIO_printf(Perl_debug_log,
0844c848
RB
2835 "### %c looked like a file test but was not\n",
2836 (int)ftst);
5f80b19c 2837 } );
e5edeb50
JH
2838 s -= 2;
2839 }
378cc40b 2840 }
a687059c
LW
2841 tmp = *s++;
2842 if (*s == tmp) {
2843 s++;
3280af22 2844 if (PL_expect == XOPERATOR)
79072805
LW
2845 TERM(POSTDEC);
2846 else
2847 OPERATOR(PREDEC);
2848 }
2849 else if (*s == '>') {
2850 s++;
2851 s = skipspace(s);
7e2040f0 2852 if (isIDFIRST_lazy_if(s,UTF)) {
a0d0e21e 2853 s = force_word(s,METHOD,FALSE,TRUE,FALSE);
463ee0b2 2854 TOKEN(ARROW);
79072805 2855 }
748a9306
LW
2856 else if (*s == '$')
2857 OPERATOR(ARROW);
463ee0b2 2858 else
748a9306 2859 TERM(ARROW);
a687059c 2860 }
3280af22 2861 if (PL_expect == XOPERATOR)
79072805
LW
2862 Aop(OP_SUBTRACT);
2863 else {
3280af22 2864 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
2f3197b3 2865 check_uni();
79072805 2866 OPERATOR('-'); /* unary minus */
2f3197b3 2867 }
79072805 2868
378cc40b 2869 case '+':
a687059c
LW
2870 tmp = *s++;
2871 if (*s == tmp) {
378cc40b 2872 s++;
3280af22 2873 if (PL_expect == XOPERATOR)
79072805
LW
2874 TERM(POSTINC);
2875 else
2876 OPERATOR(PREINC);
378cc40b 2877 }
3280af22 2878 if (PL_expect == XOPERATOR)
79072805
LW
2879 Aop(OP_ADD);
2880 else {
3280af22 2881 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
2f3197b3 2882 check_uni();
a687059c 2883 OPERATOR('+');
2f3197b3 2884 }
a687059c 2885
378cc40b 2886 case '*':
3280af22
NIS
2887 if (PL_expect != XOPERATOR) {
2888 s = scan_ident(s, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
2889 PL_expect = XOPERATOR;
2890 force_ident(PL_tokenbuf, '*');
2891 if (!*PL_tokenbuf)
a0d0e21e 2892 PREREF('*');
79072805 2893 TERM('*');
a687059c 2894 }
79072805
LW
2895 s++;
2896 if (*s == '*') {
a687059c 2897 s++;
79072805 2898 PWop(OP_POW);
a687059c 2899 }
79072805
LW
2900 Mop(OP_MULTIPLY);
2901
378cc40b 2902 case '%':
3280af22 2903 if (PL_expect == XOPERATOR) {
bbce6d69
PP
2904 ++s;
2905 Mop(OP_MODULO);
a687059c 2906 }
3280af22
NIS
2907 PL_tokenbuf[0] = '%';
2908 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, TRUE);
2909 if (!PL_tokenbuf[1]) {
2910 if (s == PL_bufend)
bbce6d69
PP
2911 yyerror("Final % should be \\% or %name");
2912 PREREF('%');
a687059c 2913 }
3280af22 2914 PL_pending_ident = '%';
bbce6d69 2915 TERM('%');
a687059c 2916
378cc40b 2917 case '^':
79072805 2918 s++;
a0d0e21e 2919 BOop(OP_BIT_XOR);
79072805 2920 case '[':
3280af22 2921 PL_lex_brackets++;
79072805 2922 /* FALL THROUGH */
378cc40b 2923 case '~':
378cc40b 2924 case ',':
378cc40b
LW
2925 tmp = *s++;
2926 OPERATOR(tmp);
a0d0e21e
LW
2927 case ':':
2928 if (s[1] == ':') {
2929 len = 0;
2930 goto just_a_word;
2931 }
2932 s++;
09bef843
SB
2933 switch (PL_expect) {
2934 OP *attrs;
2935 case XOPERATOR:
2936 if (!PL_in_my || PL_lex_state != LEX_NORMAL)
2937 break;
2938 PL_bufptr = s; /* update in case we back off */
2939 goto grabattrs;
2940 case XATTRBLOCK:
2941 PL_expect = XBLOCK;
2942 goto grabattrs;
2943 case XATTRTERM:
2944 PL_expect = XTERMBLOCK;
2945 grabattrs:
2946 s = skipspace(s);
2947 attrs = Nullop;
7e2040f0 2948 while (isIDFIRST_lazy_if(s,UTF)) {
09bef843 2949 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
f9829d6b
GS
2950 if (isLOWER(*s) && (tmp = keyword(PL_tokenbuf, len))) {
2951 if (tmp < 0) tmp = -tmp;
2952 switch (tmp) {
2953 case KEY_or:
2954 case KEY_and:
2955 case KEY_for:
2956 case KEY_unless:
2957 case KEY_if:
2958 case KEY_while:
2959 case KEY_until:
2960 goto got_attrs;
2961 default:
2962 break;
2963 }
2964 }
09bef843
SB
2965 if (*d == '(') {
2966 d = scan_str(d,TRUE,TRUE);
2967 if (!d) {
09bef843
SB
2968 /* MUST advance bufptr here to avoid bogus
2969 "at end of line" context messages from yyerror().
2970 */
2971 PL_bufptr = s + len;
2972 yyerror("Unterminated attribute parameter in attribute list");
2973 if (attrs)
2974 op_free(attrs);
2975 return 0; /* EOF indicator */
2976 }
2977 }
2978 if (PL_lex_stuff) {
2979 SV *sv = newSVpvn(s, len);
2980 sv_catsv(sv, PL_lex_stuff);
2981 attrs = append_elem(OP_LIST, attrs,
2982 newSVOP(OP_CONST, 0, sv));
2983 SvREFCNT_dec(PL_lex_stuff);
2984 PL_lex_stuff = Nullsv;
2985 }
2986 else {
78f9721b
SM
2987 if (!PL_in_my && len == 6 && strnEQ(s, "lvalue", len))
2988 CvLVALUE_on(PL_compcv);
2989 else if (!PL_in_my && len == 6 && strnEQ(s, "locked", len))
2990 CvLOCKED_on(PL_compcv);
2991 else if (!PL_in_my && len == 6 && strnEQ(s, "method", len))
2992 CvMETHOD_on(PL_compcv);
87ecf892 2993#ifdef USE_ITHREADS
c8a3bf85 2994 else if (PL_in_my == KEY_our && len == 6 && strnEQ(s, "unique", len))
7fb37951 2995 GvUNIQUE_on(cGVOPx_gv(yylval.opval));
87ecf892 2996#endif
78f9721b
SM
2997 /* After we've set the flags, it could be argued that
2998 we don't need to do the attributes.pm-based setting
2999 process, and shouldn't bother appending recognized
3000 flags. To experiment with that, uncomment the
3001 following "else": */
0256094b 3002 else
78f9721b
SM
3003 attrs = append_elem(OP_LIST, attrs,
3004 newSVOP(OP_CONST, 0,
3005 newSVpvn(s, len)));
09bef843
SB
3006 }
3007 s = skipspace(d);
0120eecf 3008 if (*s == ':' && s[1] != ':')
09bef843 3009 s = skipspace(s+1);
0120eecf
GS
3010 else if (s == d)
3011 break; /* require real whitespace or :'s */
09bef843 3012 }
f9829d6b
GS
3013 tmp = (PL_expect == XOPERATOR ? '=' : '{'); /*'}(' for vi */
3014 if (*s != ';' && *s != tmp && (tmp != '=' || *s != ')')) {
09bef843
SB
3015 char q = ((*s == '\'') ? '"' : '\'');
3016 /* If here for an expression, and parsed no attrs, back off. */
3017 if (tmp == '=' && !attrs) {
3018 s = PL_bufptr;
3019 break;
3020 }
3021 /* MUST advance bufptr here to avoid bogus "at end of line"
3022 context messages from yyerror().
3023 */
3024 PL_bufptr = s;
3025 if (!*s)
3026 yyerror("Unterminated attribute list");
3027 else
3028 yyerror(Perl_form(aTHX_ "Invalid separator character %c%c%c in attribute list",
3029 q, *s, q));
3030 if (attrs)
3031 op_free(attrs);
3032 OPERATOR(':');
3033 }
f9829d6b 3034 got_attrs:
09bef843
SB
3035 if (attrs) {
3036 PL_nextval[PL_nexttoke].opval = attrs;
3037 force_next(THING);
3038 }
3039 TOKEN(COLONATTR);
3040 }
a0d0e21e 3041 OPERATOR(':');
8990e307
LW
3042 case '(':
3043 s++;
3280af22
NIS
3044 if (PL_last_lop == PL_oldoldbufptr || PL_last_uni == PL_oldoldbufptr)
3045 PL_oldbufptr = PL_oldoldbufptr; /* allow print(STDOUT 123) */
a0d0e21e 3046 else
3280af22 3047 PL_expect = XTERM;
a0d0e21e 3048 TOKEN('(');
378cc40b 3049 case ';':
f4dd75d9 3050 CLINE;
378cc40b
LW
3051 tmp = *s++;
3052 OPERATOR(tmp);
3053 case ')':
378cc40b 3054 tmp = *s++;
16d20bd9
AD
3055 s = skipspace(s);
3056 if (*s == '{')
3057 PREBLOCK(tmp);
378cc40b 3058 TERM(tmp);
79072805
LW
3059 case ']':
3060 s++;
3280af22 3061 if (PL_lex_brackets <= 0)
d98d5fff 3062 yyerror("Unmatched right square bracket");
463ee0b2 3063 else
3280af22
NIS
3064 --PL_lex_brackets;
3065 if (PL_lex_state == LEX_INTERPNORMAL) {
3066 if (PL_lex_brackets == 0) {
a0d0e21e 3067 if (*s != '[' && *s != '{' && (*s != '-' || s[1] != '>'))
3280af22 3068 PL_lex_state = LEX_INTERPEND;
79072805
LW
3069 }
3070 }
4633a7c4 3071 TERM(']');
79072805
LW
3072 case '{':
3073 leftbracket:
79072805 3074 s++;
3280af22
NIS
3075 if (PL_lex_brackets > 100) {
3076 char* newlb = Renew(PL_lex_brackstack, PL_lex_brackets + 1, char);
3077 if (newlb != PL_lex_brackstack) {
8990e307 3078 SAVEFREEPV(newlb);
3280af22 3079 PL_lex_brackstack = newlb;
8990e307
LW
3080 }
3081 }
3280af22 3082 switch (PL_expect) {
a0d0e21e 3083 case XTERM:
3280af22 3084 if (PL_lex_formbrack) {
a0d0e21e
LW
3085 s--;
3086 PRETERMBLOCK(DO);
3087 }
3280af22
NIS
3088 if (PL_oldoldbufptr == PL_last_lop)
3089 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
a0d0e21e 3090 else
3280af22 3091 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
79072805 3092 OPERATOR(HASHBRACK);
a0d0e21e 3093 case XOPERATOR:
bf4acbe4 3094 while (s < PL_bufend && SPACE_OR_TAB(*s))
748a9306 3095 s++;
44a8e56a 3096 d = s;
3280af22
NIS
3097 PL_tokenbuf[0] = '\0';
3098 if (d < PL_bufend && *d == '-') {
3099 PL_tokenbuf[0] = '-';
44a8e56a 3100 d++;
bf4acbe4 3101 while (d < PL_bufend && SPACE_OR_TAB(*d))
44a8e56a
PP
3102 d++;
3103 }
7e2040f0 3104 if (d < PL_bufend && isIDFIRST_lazy_if(d,UTF)) {
3280af22 3105 d = scan_word(d, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
8903cb82 3106 FALSE, &len);
bf4acbe4 3107 while (d < PL_bufend && SPACE_OR_TAB(*d))
748a9306
LW
3108 d++;
3109 if (*d == '}') {
3280af22 3110 char minus = (PL_tokenbuf[0] == '-');
44a8e56a
PP
3111 s = force_word(s + minus, WORD, FALSE, TRUE, FALSE);
3112 if (minus)
3113 force_next('-');
748a9306
LW
3114 }
3115 }
3116 /* FALL THROUGH */
09bef843 3117 case XATTRBLOCK:
748a9306 3118 case XBLOCK:
3280af22
NIS
3119 PL_lex_brackstack[PL_lex_brackets++] = XSTATE;
3120 PL_expect = XSTATE;
a0d0e21e 3121 break;
09bef843 3122 case XATTRTERM:
a0d0e21e 3123 case XTERMBLOCK:
3280af22
NIS
3124 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
3125 PL_expect = XSTATE;
a0d0e21e
LW
3126 break;
3127 default: {
3128 char *t;
3280af22
NIS
3129 if (PL_oldoldbufptr == PL_last_lop)
3130 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
a0d0e21e 3131 else
3280af22 3132 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
a0d0e21e 3133 s = skipspace(s);
8452ff4b
SB
3134 if (*s == '}') {
3135 if (PL_expect == XREF && PL_lex_state == LEX_INTERPNORMAL) {
3136 PL_expect = XTERM;
3137 /* This hack is to get the ${} in the message. */
3138 PL_bufptr = s+1;
3139 yyerror("syntax error");
3140 break;
3141 }
a0d0e21e 3142 OPERATOR(HASHBRACK);
8452ff4b 3143 }
b8a4b1be
GS
3144 /* This hack serves to disambiguate a pair of curlies
3145 * as being a block or an anon hash. Normally, expectation
3146 * determines that, but in cases where we're not in a
3147 * position to expect anything in particular (like inside
3148 * eval"") we have to resolve the ambiguity. This code
3149 * covers the case where the first term in the curlies is a
3150 * quoted string. Most other cases need to be explicitly
3151 * disambiguated by prepending a `+' before the opening
3152 * curly in order to force resolution as an anon hash.
3153 *
3154 * XXX should probably propagate the outer expectation
3155 * into eval"" to rely less on this hack, but that could
3156 * potentially break current behavior of eval"".
3157 * GSAR 97-07-21
3158 */
3159 t = s;
3160 if (*s == '\'' || *s == '"' || *s == '`') {
3161 /* common case: get past first string, handling escapes */
3280af22 3162 for (t++; t < PL_bufend && *t != *s;)
b8a4b1be
GS
3163 if (*t++ == '\\' && (*t == '\\' || *t == *s))
3164 t++;
3165 t++;
a0d0e21e 3166 }
b8a4b1be 3167 else if (*s == 'q') {
3280af22 3168 if (++t < PL_bufend
b8a4b1be 3169 && (!isALNUM(*t)
3280af22 3170 || ((*t == 'q' || *t == 'x') && ++t < PL_bufend
0505442f
GS
3171 && !isALNUM(*t))))
3172 {
b8a4b1be
GS
3173 char *tmps;
3174 char open, close, term;
3175 I32 brackets = 1;
3176
3280af22 3177 while (t < PL_bufend && isSPACE(*t))
b8a4b1be
GS
3178 t++;
3179 term = *t;
3180 open = term;
3181 if (term && (tmps = strchr("([{< )]}> )]}>",term)))
3182 term = tmps[5];
3183 close = term;
3184 if (open == close)
3280af22
NIS
3185 for (t++; t < PL_bufend; t++) {
3186 if (*t == '\\' && t+1 < PL_bufend && open != '\\')
b8a4b1be 3187 t++;
6d07e5e9 3188 else if (*t == open)
b8a4b1be
GS
3189 break;
3190 }
3191 else
3280af22
NIS
3192 for (t++; t < PL_bufend; t++) {
3193 if (*t == '\\' && t+1 < PL_bufend)
b8a4b1be 3194 t++;
6d07e5e9 3195 else if (*t == close && --brackets <= 0)
b8a4b1be
GS
3196 break;
3197 else if (*t == open)
3198 brackets++;
3199 }
3200 }
3201 t++;
a0d0e21e 3202 }
7e2040f0 3203 else if (isALNUM_lazy_if(t,UTF)) {
0505442f 3204 t += UTF8SKIP(t);
7e2040f0 3205 while (t < PL_bufend && isALNUM_lazy_if(t,UTF))
0505442f 3206 t += UTF8SKIP(t);
a0d0e21e 3207 }
3280af22 3208 while (t < PL_bufend && isSPACE(*t))
a0d0e21e 3209 t++;
b8a4b1be
GS
3210 /* if comma follows first term, call it an anon hash */
3211 /* XXX it could be a comma expression with loop modifiers */
3280af22 3212 if (t < PL_bufend && ((*t == ',' && (*s == 'q' || !isLOWER(*s)))
b8a4b1be 3213 || (*t == '=' && t[1] == '>')))
a0d0e21e 3214 OPERATOR(HASHBRACK);
3280af22 3215 if (PL_expect == XREF)
4e4e412b 3216 PL_expect = XTERM;
a0d0e21e 3217 else {
3280af22
NIS
3218 PL_lex_brackstack[PL_lex_brackets-1] = XSTATE;
3219 PL_expect = XSTATE;
a0d0e21e 3220 }
8990e307 3221 }
a0d0e21e 3222 break;
463ee0b2 3223 }
57843af0 3224 yylval.ival = CopLINE(PL_curcop);
79072805 3225 if (isSPACE(*s) || *s == '#')
3280af22 3226 PL_copline = NOLINE; /* invalidate current command line number */
79072805 3227 TOKEN('{');
378cc40b 3228 case '}':
79072805
LW
3229 rightbracket:
3230 s++;
3280af22 3231 if (PL_lex_brackets <= 0)
d98d5fff 3232 yyerror("Unmatched right curly bracket");
463ee0b2 3233 else
3280af22 3234 PL_expect = (expectation)PL_lex_brackstack[--PL_lex_brackets];
c2e66d9e 3235 if (PL_lex_brackets < PL_lex_formbrack && PL_lex_state != LEX_INTERPNORMAL)
3280af22
NIS
3236 PL_lex_formbrack = 0;
3237 if (PL_lex_state == LEX_INTERPNORMAL) {
3238 if (PL_lex_brackets == 0) {
9059aa12
LW
3239 if (PL_expect & XFAKEBRACK) {
3240 PL_expect &= XENUMMASK;
3280af22
NIS
3241 PL_lex_state = LEX_INTERPEND;
3242 PL_bufptr = s;
cea2e8a9 3243 return yylex(); /* ignore fake brackets */
79072805 3244 }
fa83b5b6 3245 if (*s == '-' && s[1] == '>')
3280af22 3246 PL_lex_state = LEX_INTERPENDMAYBE;
fa83b5b6 3247 else if (*s != '[' && *s != '{')
3280af22 3248 PL_lex_state = LEX_INTERPEND;
79072805
LW
3249 }
3250 }
9059aa12
LW
3251 if (PL_expect & XFAKEBRACK) {
3252 PL_expect &= XENUMMASK;
3280af22 3253 PL_bufptr = s;
cea2e8a9 3254 return yylex(); /* ignore fake brackets */
748a9306 3255 }
79072805
LW
3256 force_next('}');
3257 TOKEN(';');
378cc40b
LW
3258 case '&':
3259 s++;
3260 tmp = *s++;
3261 if (tmp == '&')
a0d0e21e 3262 AOPERATOR(ANDAND);
378cc40b 3263 s--;
3280af22 3264 if (PL_expect == XOPERATOR) {
7e2040f0
GS
3265 if (ckWARN(WARN_SEMICOLON)
3266 && isIDFIRST_lazy_if(s,UTF) && PL_bufptr == PL_linestart)
3267 {
57843af0 3268 CopLINE_dec(PL_curcop);
cea2e8a9 3269 Perl_warner(aTHX_ WARN_SEMICOLON, PL_warn_nosemi);
57843af0 3270 CopLINE_inc(PL_curcop);
463ee0b2 3271 }
79072805 3272 BAop(OP_BIT_AND);
463ee0b2 3273 }
79072805 3274
3280af22
NIS
3275 s = scan_ident(s - 1, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
3276 if (*PL_tokenbuf) {
3277 PL_expect = XOPERATOR;
3278 force_ident(PL_tokenbuf, '&');
463ee0b2 3279 }
79072805
LW
3280 else
3281 PREREF('&');
c07a80fd 3282 yylval.ival = (OPpENTERSUB_AMPER<<8);
79072805
LW
3283 TERM('&');
3284
378cc40b
LW
3285 case '|':
3286 s++;
3287 tmp = *s++;
3288 if (tmp == '|')
a0d0e21e 3289 AOPERATOR(OROR);
378cc40b 3290 s--;
79072805 3291 BOop(OP_BIT_OR);
378cc40b
LW
3292 case '=':
3293 s++;
3294 tmp = *s++;
3295 if (tmp == '=')
79072805
LW
3296 Eop(OP_EQ);
3297 if (tmp == '>')
3298 OPERATOR(',');
378cc40b 3299 if (tmp == '~')
79072805 3300 PMop(OP_MATCH);
599cee73 3301 if (ckWARN(WARN_SYNTAX) && tmp && isSPACE(*s) && strchr("+-*/%.^&|<",tmp))
cea2e8a9 3302 Perl_warner(aTHX_ WARN_SYNTAX, "Reversed %c= operator",(int)tmp);
378cc40b 3303 s--;
3280af22
NIS
3304 if (PL_expect == XSTATE && isALPHA(tmp) &&
3305 (s == PL_linestart+1 || s[-2] == '\n') )
748a9306 3306 {
3280af22
NIS
3307 if (PL_in_eval && !PL_rsfp) {
3308 d = PL_bufend;
a5f75d66
AD
3309 while (s < d) {
3310 if (*s++ == '\n') {
3311 incline(s);
3312 if (strnEQ(s,"=cut",4)) {
3313 s = strchr(s,'\n');
3314 if (s)
3315 s++;
3316 else
3317 s = d;
3318 incline(s);
3319 goto retry;
3320 }
3321 }
3322 }
3323 goto retry;
3324 }
3280af22
NIS
3325 s = PL_bufend;
3326 PL_doextract = TRUE;
a0d0e21e
LW
3327 goto retry;
3328 }
3280af22 3329 if (PL_lex_brackets < PL_lex_formbrack) {
a0d0e21e 3330 char *t;
51882d45 3331#ifdef PERL_STRICT_CR
bf4acbe4 3332 for (t = s; SPACE_OR_TAB(*t); t++) ;
51882d45 3333#else
bf4acbe4 3334 for (t = s; SPACE_OR_TAB(*t) || *t == '\r'; t++) ;
51882d45 3335#endif
a0d0e21e
LW
3336 if (*t == '\n' || *t == '#') {
3337 s--;
3280af22 3338 PL_expect = XBLOCK;
a0d0e21e
LW
3339 goto leftbracket;
3340 }
79072805 3341 }
a0d0e21e
LW
3342 yylval.ival = 0;
3343 OPERATOR(ASSIGNOP);
378cc40b
LW
3344 case '!':
3345 s++;
3346 tmp = *s++;
3347 if (tmp == '=')
79072805 3348 Eop(OP_NE);
378cc40b 3349 if (tmp == '~')
79072805 3350 PMop(OP_NOT);
378cc40b
LW
3351 s--;
3352 OPERATOR('!');
3353 case '<':
3280af22 3354 if (PL_expect != XOPERATOR) {
93a17b20 3355 if (s[1] != '<' && !strchr(s,'>'))
2f3197b3 3356 check_uni();
79072805
LW
3357 if (s[1] == '<')
3358 s = scan_heredoc(s);
3359 else
3360 s = scan_inputsymbol(s);
3361 TERM(sublex_start());
378cc40b
LW
3362 }
3363 s++;
3364 tmp = *s++;
3365 if (tmp == '<')
79072805 3366 SHop(OP_LEFT_SHIFT);
395c3793
LW
3367 if (tmp == '=') {
3368 tmp = *s++;
3369 if (tmp == '>')
79072805 3370 Eop(OP_NCMP);
395c3793 3371 s--;
79072805 3372 Rop(OP_LE);
395c3793 3373 }
378cc40b 3374 s--;
79072805 3375 Rop(OP_LT);
378cc40b
LW
3376 case '>':
3377 s++;
3378 tmp = *s++;
3379 if (tmp == '>')
79072805 3380 SHop(OP_RIGHT_SHIFT);
378cc40b 3381 if (tmp == '=')
79072805 3382 Rop(OP_GE);
378cc40b 3383 s--;
79072805 3384 Rop(OP_GT);
378cc40b
LW
3385
3386 case '$':
bbce6d69
PP
3387 CLINE;
3388
3280af22
NIS
3389 if (PL_expect == XOPERATOR) {
3390 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
3391 PL_expect = XTERM;
a0d0e21e 3392 depcom();
bbce6d69 3393 return ','; /* grandfather non-comma-format format */
a0d0e21e 3394 }
8990e307 3395 }
a0d0e21e 3396
7e2040f0 3397 if (s[1] == '#' && (isIDFIRST_lazy_if(s+2,UTF) || strchr("{$:+-", s[2]))) {
3280af22 3398 PL_tokenbuf[0] = '@';
376b8730
SM
3399 s = scan_ident(s + 1, PL_bufend, PL_tokenbuf + 1,
3400 sizeof PL_tokenbuf - 1, FALSE);
3401 if (PL_expect == XOPERATOR)
3402 no_op("Array length", s);
3280af22 3403 if (!PL_tokenbuf[1])
a0d0e21e 3404 PREREF(DOLSHARP);
3280af22
NIS
3405 PL_expect = XOPERATOR;
3406 PL_pending_ident = '#';
463ee0b2 3407 TOKEN(DOLSHARP);
79072805 3408 }
bbce6d69 3409
3280af22 3410 PL_tokenbuf[0] = '$';
376b8730
SM
3411 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1,
3412 sizeof PL_tokenbuf - 1, FALSE);
3413 if (PL_expect == XOPERATOR)
3414 no_op("Scalar", s);
3280af22
NIS
3415 if (!PL_tokenbuf[1]) {
3416 if (s == PL_bufend)
bbce6d69
PP
3417 yyerror("Final $ should be \\$ or $name");
3418 PREREF('$');
8990e307 3419 }
a0d0e21e 3420
bbce6d69 3421 /* This kludge not intended to be bulletproof. */
3280af22 3422 if (PL_tokenbuf[1] == '[' && !PL_tokenbuf[2]) {
bbce6d69 3423 yylval.opval = newSVOP(OP_CONST, 0,
b448e4fe 3424 newSViv(PL_compiling.cop_arybase));
bbce6d69
PP
3425 yylval.opval->op_private = OPpCONST_ARYBASE;
3426 TERM(THING);
3427 }
3428
ff68c719 3429 d = s;
69d2bceb 3430 tmp = (I32)*s;
3280af22 3431 if (PL_lex_state == LEX_NORMAL)
ff68c719
PP
3432 s = skipspace(s);
3433
3280af22 3434 if ((PL_expect != XREF || PL_oldoldbufptr == PL_last_lop) && intuit_more(s)) {
bbce6d69
PP
3435 char *t;
3436 if (*s == '[') {
3280af22 3437 PL_tokenbuf[0] = '@';
599cee73 3438 if (ckWARN(WARN_SYNTAX)) {
bbce6d69 3439 for(t = s + 1;
7e2040f0 3440 isSPACE(*t) || isALNUM_lazy_if(t,UTF) || *t == '$';
bbce6d69 3441 t++) ;
a0d0e21e 3442 if (*t++ == ',') {
3280af22
NIS
3443 PL_bufptr = skipspace(PL_bufptr);
3444 while (t < PL_bufend && *t != ']')
bbce6d69 3445 t++;
cea2e8a9 3446 Perl_warner(aTHX_ WARN_SYNTAX,
599cee73
PM
3447 "Multidimensional syntax %.*s not supported",
3448 (t - PL_bufptr) + 1, PL_bufptr);
a0d0e21e
LW
3449 }
3450 }
bbce6d69
PP
3451 }
3452 else if (*s == '{') {
3280af22 3453 PL_tokenbuf[0] = '%';
599cee73 3454 if (ckWARN(WARN_SYNTAX) && strEQ(PL_tokenbuf+1, "SIG") &&
bbce6d69
PP
3455 (t = strchr(s, '}')) && (t = strchr(t, '=')))
3456 {
3280af22 3457 char tmpbuf[sizeof PL_tokenbuf];
a0d0e21e
LW
3458 STRLEN len;
3459 for (t++; isSPACE(*t); t++) ;
7e2040f0 3460 if (isIDFIRST_lazy_if(t,UTF)) {
8903cb82 3461 t = scan_word(t, tmpbuf, sizeof tmpbuf, TRUE, &len);
59a6d928 3462 for (; isSPACE(*t); t++) ;
864dbfa3 3463 if (*t == ';' && get_cv(tmpbuf, FALSE))
cea2e8a9 3464 Perl_warner(aTHX_ WARN_SYNTAX,
599cee73 3465 "You need to quote \"%s\"", tmpbuf);
748a9306 3466 }
93a17b20
LW
3467 }
3468 }
2f3197b3 3469 }
bbce6d69 3470
3280af22 3471 PL_expect = XOPERATOR;
69d2bceb 3472 if (PL_lex_state == LEX_NORMAL && isSPACE((char)tmp)) {
3280af22
NIS
3473 bool islop = (PL_last_lop == PL_oldoldbufptr);
3474 if (!islop || PL_last_lop_op == OP_GREPSTART)
3475 PL_expect = XOPERATOR;
bbce6d69 3476 else if (strchr("$@\"'`q", *s))
3280af22 3477 PL_expect = XTERM; /* e.g. print $fh "foo" */
7e2040f0 3478 else if (strchr("&*<%", *s) && isIDFIRST_lazy_if(s+1,UTF))
3280af22 3479 PL_expect = XTERM; /* e.g. print $fh &sub */
7e2040f0 3480 else if (isIDFIRST_lazy_if(s,UTF)) {
3280af22 3481 char tmpbuf[sizeof PL_tokenbuf];
8903cb82 3482 scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
155aba94 3483 if ((tmp = keyword(tmpbuf, len))) {
84902520
TB
3484 /* binary operators exclude handle interpretations */
3485 switch (tmp) {
3486 case -KEY_x:
3487 case -KEY_eq:
3488 case -KEY_ne:
3489 case -KEY_gt:
3490 case -KEY_lt:
3491 case -KEY_ge:
3492 case -KEY_le:
3493 case -KEY_cmp:
3494 break;
3495 default:
3280af22 3496 PL_expect = XTERM; /* e.g. print $fh length() */
84902520
TB
3497 break;
3498 }
3499 }
68dc0745
PP
3500 else {
3501 GV *gv = gv_fetchpv(tmpbuf, FALSE, SVt_PVCV);
3502 if (gv && GvCVu(gv))
3280af22 3503 PL_expect = XTERM; /* e.g. print $fh subr() */
93a17b20 3504 }
93a17b20 3505 }
bbce6d69 3506 else if (isDIGIT(*s))
3280af22 3507 PL_expect = XTERM; /* e.g. print $fh 3 */
bbce6d69 3508 else if (*s == '.' && isDIGIT(s[1]))
3280af22 3509