This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate perlio:
[perl5.git] / toke.c
CommitLineData
a0d0e21e 1/* toke.c
a687059c 2 *
be3c0a43 3 * Copyright (c) 1991-2002, 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 65/* #define LEX_NOTPARSING 11 is done in perl.h. */
66
55497cff 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
IRC
87YYSTYPE* yylval_pointer[YYMAXLEVEL];
88int* yychar_pointer[YYMAXLEVEL];
6f202aea 89int yyactlevel = -1;
22c35a8c
GS
90# undef yylval
91# undef yychar
20141f0e
IRC
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 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 870{
871 OP *version = Nullop;
44dcb63b 872 char *d;
89bfa8cd 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 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 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 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 {
707afd92
MS
1427 if (ckWARN(WARN_MISC) &&
1428 isALNUM(*s) &&
1429 *s != '_')
4e553d73 1430 Perl_warner(aTHX_ WARN_MISC,
11b8faa4
JH
1431 "Unrecognized escape \\%c passed through",
1432 *s);
1433 /* default action is to copy the quoted character */
f9a63242 1434 goto default_action;
11b8faa4 1435 }
02aa26ce
NT
1436
1437 /* \132 indicates an octal constant */
79072805
LW
1438 case '0': case '1': case '2': case '3':
1439 case '4': case '5': case '6': case '7':
ba210ebe 1440 {
53305cf1
NC
1441 I32 flags = 0;
1442 STRLEN len = 3;
1443 uv = grok_oct(s, &len, &flags, NULL);
ba210ebe
JH
1444 s += len;
1445 }
012bcf8d 1446 goto NUM_ESCAPE_INSERT;
02aa26ce
NT
1447
1448 /* \x24 indicates a hex constant */
79072805 1449 case 'x':
a0ed51b3
LW
1450 ++s;
1451 if (*s == '{') {
1452 char* e = strchr(s, '}');
a4c04bdc
NC
1453 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
1454 PERL_SCAN_DISALLOW_PREFIX;
53305cf1 1455 STRLEN len;
355860ce 1456
53305cf1 1457 ++s;
adaeee49 1458 if (!e) {
a0ed51b3 1459 yyerror("Missing right brace on \\x{}");
355860ce 1460 continue;
ba210ebe 1461 }
53305cf1
NC
1462 len = e - s;
1463 uv = grok_hex(s, &len, &flags, NULL);
ba210ebe 1464 s = e + 1;
a0ed51b3
LW
1465 }
1466 else {
ba210ebe 1467 {
53305cf1 1468 STRLEN len = 2;
a4c04bdc 1469 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
53305cf1 1470 uv = grok_hex(s, &len, &flags, NULL);
ba210ebe
JH
1471 s += len;
1472 }
012bcf8d
GS
1473 }
1474
1475 NUM_ESCAPE_INSERT:
1476 /* Insert oct or hex escaped character.
301d3d20 1477 * There will always enough room in sv since such
db42d148 1478 * escapes will be longer than any UTF-8 sequence
301d3d20 1479 * they can end up as. */
ba7cea30 1480
c7f1f016
NIS
1481 /* We need to map to chars to ASCII before doing the tests
1482 to cover EBCDIC
1483 */
c4d5f83a 1484 if (!UNI_IS_INVARIANT(NATIVE_TO_UNI(uv))) {
9aa983d2 1485 if (!has_utf8 && uv > 255) {
301d3d20
JH
1486 /* Might need to recode whatever we have
1487 * accumulated so far if it contains any
1488 * hibit chars.
1489 *
1490 * (Can't we keep track of that and avoid
1491 * this rescan? --jhi)
012bcf8d 1492 */
c7f1f016 1493 int hicount = 0;
63cd0674
NIS
1494 U8 *c;
1495 for (c = (U8 *) SvPVX(sv); c < (U8 *)d; c++) {
c4d5f83a 1496 if (!NATIVE_IS_INVARIANT(*c)) {
012bcf8d 1497 hicount++;
db42d148 1498 }
012bcf8d 1499 }
63cd0674 1500 if (hicount) {
db42d148
NIS
1501 STRLEN offset = d - SvPVX(sv);
1502 U8 *src, *dst;
1503 d = SvGROW(sv, SvLEN(sv) + hicount + 1) + offset;
1504 src = (U8 *)d - 1;
1505 dst = src+hicount;
1506 d += hicount;
1507 while (src >= (U8 *)SvPVX(sv)) {
c4d5f83a 1508 if (!NATIVE_IS_INVARIANT(*src)) {
63cd0674 1509 U8 ch = NATIVE_TO_ASCII(*src);
db42d148
NIS
1510 *dst-- = UTF8_EIGHT_BIT_LO(ch);
1511 *dst-- = UTF8_EIGHT_BIT_HI(ch);
012bcf8d
GS
1512 }
1513 else {
63cd0674 1514 *dst-- = *src;
012bcf8d 1515 }
c7f1f016 1516 src--;
012bcf8d
GS
1517 }
1518 }
1519 }
1520
9aa983d2 1521 if (has_utf8 || uv > 255) {
9041c2e3 1522 d = (char*)uvchr_to_utf8((U8*)d, uv);
4e553d73 1523 has_utf8 = TRUE;
f9a63242
JH
1524 if (PL_lex_inwhat == OP_TRANS &&
1525 PL_sublex_info.sub_op) {
1526 PL_sublex_info.sub_op->op_private |=
1527 (PL_lex_repl ? OPpTRANS_FROM_UTF
1528 : OPpTRANS_TO_UTF);
f9a63242 1529 }
012bcf8d 1530 }
a0ed51b3 1531 else {
012bcf8d 1532 *d++ = (char)uv;
a0ed51b3 1533 }
012bcf8d
GS
1534 }
1535 else {
c4d5f83a 1536 *d++ = (char) uv;
a0ed51b3 1537 }
79072805 1538 continue;
02aa26ce 1539
b239daa5 1540 /* \N{LATIN SMALL LETTER A} is a named character */
4a2d328f 1541 case 'N':
55eda711 1542 ++s;
423cee85
JH
1543 if (*s == '{') {
1544 char* e = strchr(s, '}');
155aba94 1545 SV *res;
423cee85
JH
1546 STRLEN len;
1547 char *str;
4e553d73 1548
423cee85 1549 if (!e) {
5777a3f7 1550 yyerror("Missing right brace on \\N{}");
423cee85
JH
1551 e = s - 1;
1552 goto cont_scan;
1553 }
55eda711
JH
1554 res = newSVpvn(s + 1, e - s - 1);
1555 res = new_constant( Nullch, 0, "charnames",
1556 res, Nullsv, "\\N{...}" );
f9a63242
JH
1557 if (has_utf8)
1558 sv_utf8_upgrade(res);
423cee85 1559 str = SvPV(res,len);
89491803 1560 if (!has_utf8 && SvUTF8(res)) {
f08d6ad9
GS
1561 char *ostart = SvPVX(sv);
1562 SvCUR_set(sv, d - ostart);
1563 SvPOK_on(sv);
e4f3eed8 1564 *d = '\0';
f08d6ad9 1565 sv_utf8_upgrade(sv);
d2f449dd
SB
1566 /* this just broke our allocation above... */
1567 SvGROW(sv, send - start);
f08d6ad9 1568 d = SvPVX(sv) + SvCUR(sv);
89491803 1569 has_utf8 = TRUE;
f08d6ad9 1570 }
b239daa5 1571 if (len > e - s + 4) { /* I _guess_ 4 is \N{} --jhi */
423cee85
JH
1572 char *odest = SvPVX(sv);
1573
8973db79 1574 SvGROW(sv, (SvLEN(sv) + len - (e - s + 4)));
423cee85
JH
1575 d = SvPVX(sv) + (d - odest);
1576 }
1577 Copy(str, d, len, char);
1578 d += len;
1579 SvREFCNT_dec(res);
1580 cont_scan:
1581 s = e + 1;
1582 }
1583 else
5777a3f7 1584 yyerror("Missing braces on \\N{}");
423cee85
JH
1585 continue;
1586
02aa26ce 1587 /* \c is a control character */
79072805
LW
1588 case 'c':
1589 s++;
ba210ebe
JH
1590 {
1591 U8 c = *s++;
c7f1f016
NIS
1592#ifdef EBCDIC
1593 if (isLOWER(c))
1594 c = toUPPER(c);
1595#endif
db42d148 1596 *d++ = NATIVE_TO_NEED(has_utf8,toCTRL(c));
ba210ebe 1597 }
79072805 1598 continue;
02aa26ce
NT
1599
1600 /* printf-style backslashes, formfeeds, newlines, etc */
79072805 1601 case 'b':
db42d148 1602 *d++ = NATIVE_TO_NEED(has_utf8,'\b');
79072805
LW
1603 break;
1604 case 'n':
db42d148 1605 *d++ = NATIVE_TO_NEED(has_utf8,'\n');
79072805
LW
1606 break;
1607 case 'r':
db42d148 1608 *d++ = NATIVE_TO_NEED(has_utf8,'\r');
79072805
LW
1609 break;
1610 case 'f':
db42d148 1611 *d++ = NATIVE_TO_NEED(has_utf8,'\f');
79072805
LW
1612 break;
1613 case 't':
db42d148 1614 *d++ = NATIVE_TO_NEED(has_utf8,'\t');
79072805 1615 break;
34a3fe2a 1616 case 'e':
db42d148 1617 *d++ = ASCII_TO_NEED(has_utf8,'\033');
34a3fe2a
PP
1618 break;
1619 case 'a':
db42d148 1620 *d++ = ASCII_TO_NEED(has_utf8,'\007');
79072805 1621 break;
02aa26ce
NT
1622 } /* end switch */
1623
79072805
LW
1624 s++;
1625 continue;
02aa26ce
NT
1626 } /* end if (backslash) */
1627
f9a63242 1628 default_action:
2b9d42f0
NIS
1629 /* If we started with encoded form, or already know we want it
1630 and then encode the next character */
1631 if ((has_utf8 || this_utf8) && !NATIVE_IS_INVARIANT((U8)(*s))) {
1632 STRLEN len = 1;
1633 UV uv = (this_utf8) ? utf8n_to_uvchr((U8*)s, send - s, &len, 0) : (UV) ((U8) *s);
1634 STRLEN need = UNISKIP(NATIVE_TO_UNI(uv));
1635 s += len;
1636 if (need > len) {
1637 /* encoded value larger than old, need extra space (NOTE: SvCUR() not set here) */
1638 STRLEN off = d - SvPVX(sv);
1639 d = SvGROW(sv, SvLEN(sv) + (need-len)) + off;
1640 }
1641 d = (char*)uvchr_to_utf8((U8*)d, uv);
1642 has_utf8 = TRUE;
1643 }
1644 else {
1645 *d++ = NATIVE_TO_NEED(has_utf8,*s++);
1646 }
02aa26ce
NT
1647 } /* while loop to process each character */
1648
1649 /* terminate the string and set up the sv */
79072805 1650 *d = '\0';
463ee0b2 1651 SvCUR_set(sv, d - SvPVX(sv));
2b9d42f0 1652 if (SvCUR(sv) >= SvLEN(sv))
585602fa 1653 Perl_croak(aTHX_ "panic: constant overflowed allocated space");
2b9d42f0 1654
79072805 1655 SvPOK_on(sv);
9f4817db 1656 if (PL_encoding && !has_utf8) {
5d170f3a 1657 Perl_sv_recode_to_utf8(aTHX_ sv, PL_encoding);
9f4817db
JH
1658 has_utf8 = TRUE;
1659 }
2b9d42f0 1660 if (has_utf8) {
7e2040f0 1661 SvUTF8_on(sv);
2b9d42f0
NIS
1662 if (PL_lex_inwhat == OP_TRANS && PL_sublex_info.sub_op) {
1663 PL_sublex_info.sub_op->op_private |=
1664 (PL_lex_repl ? OPpTRANS_FROM_UTF : OPpTRANS_TO_UTF);
1665 }
1666 }
79072805 1667
02aa26ce 1668 /* shrink the sv if we allocated more than we used */
79072805
LW
1669 if (SvCUR(sv) + 5 < SvLEN(sv)) {
1670 SvLEN_set(sv, SvCUR(sv) + 1);
463ee0b2 1671 Renew(SvPVX(sv), SvLEN(sv), char);
79072805 1672 }
02aa26ce 1673
9b599b2a 1674 /* return the substring (via yylval) only if we parsed anything */
3280af22
NIS
1675 if (s > PL_bufptr) {
1676 if ( PL_hints & ( PL_lex_inpat ? HINT_NEW_RE : HINT_NEW_STRING ) )
4e553d73 1677 sv = new_constant(start, s - start, (PL_lex_inpat ? "qr" : "q"),
b3ac6de7 1678 sv, Nullsv,
4e553d73 1679 ( PL_lex_inwhat == OP_TRANS
b3ac6de7 1680 ? "tr"
3280af22 1681 : ( (PL_lex_inwhat == OP_SUBST && !PL_lex_inpat)
b3ac6de7
IZ
1682 ? "s"
1683 : "qq")));
79072805 1684 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
b3ac6de7 1685 } else
8990e307 1686 SvREFCNT_dec(sv);
79072805
LW
1687 return s;
1688}
1689
ffb4593c
NT
1690/* S_intuit_more
1691 * Returns TRUE if there's more to the expression (e.g., a subscript),
1692 * FALSE otherwise.
ffb4593c
NT
1693 *
1694 * It deals with "$foo[3]" and /$foo[3]/ and /$foo[0123456789$]+/
1695 *
1696 * ->[ and ->{ return TRUE
1697 * { and [ outside a pattern are always subscripts, so return TRUE
1698 * if we're outside a pattern and it's not { or [, then return FALSE
1699 * if we're in a pattern and the first char is a {
1700 * {4,5} (any digits around the comma) returns FALSE
1701 * if we're in a pattern and the first char is a [
1702 * [] returns FALSE
1703 * [SOMETHING] has a funky algorithm to decide whether it's a
1704 * character class or not. It has to deal with things like
1705 * /$foo[-3]/ and /$foo[$bar]/ as well as /$foo[$\d]+/
1706 * anything else returns TRUE
1707 */
1708
9cbb5ea2
GS
1709/* This is the one truly awful dwimmer necessary to conflate C and sed. */
1710
76e3520e 1711STATIC int
cea2e8a9 1712S_intuit_more(pTHX_ register char *s)
79072805 1713{
3280af22 1714 if (PL_lex_brackets)
79072805
LW
1715 return TRUE;
1716 if (*s == '-' && s[1] == '>' && (s[2] == '[' || s[2] == '{'))
1717 return TRUE;
1718 if (*s != '{' && *s != '[')
1719 return FALSE;
3280af22 1720 if (!PL_lex_inpat)
79072805
LW
1721 return TRUE;
1722
1723 /* In a pattern, so maybe we have {n,m}. */
1724 if (*s == '{') {
1725 s++;
1726 if (!isDIGIT(*s))
1727 return TRUE;
1728 while (isDIGIT(*s))
1729 s++;
1730 if (*s == ',')
1731 s++;
1732 while (isDIGIT(*s))
1733 s++;
1734 if (*s == '}')
1735 return FALSE;
1736 return TRUE;
1737
1738 }
1739
1740 /* On the other hand, maybe we have a character class */
1741
1742 s++;
1743 if (*s == ']' || *s == '^')
1744 return FALSE;
1745 else {
ffb4593c 1746 /* this is terrifying, and it works */
79072805
LW
1747 int weight = 2; /* let's weigh the evidence */
1748 char seen[256];
f27ffc4a 1749 unsigned char un_char = 255, last_un_char;
93a17b20 1750 char *send = strchr(s,']');
3280af22 1751 char tmpbuf[sizeof PL_tokenbuf * 4];
79072805
LW
1752
1753 if (!send) /* has to be an expression */
1754 return TRUE;
1755
1756 Zero(seen,256,char);
1757 if (*s == '$')
1758 weight -= 3;
1759 else if (isDIGIT(*s)) {
1760 if (s[1] != ']') {
1761 if (isDIGIT(s[1]) && s[2] == ']')
1762 weight -= 10;
1763 }
1764 else
1765 weight -= 100;
1766 }
1767 for (; s < send; s++) {
1768 last_un_char = un_char;
1769 un_char = (unsigned char)*s;
1770 switch (*s) {
1771 case '@':
1772 case '&':
1773 case '$':
1774 weight -= seen[un_char] * 10;
7e2040f0 1775 if (isALNUM_lazy_if(s+1,UTF)) {
8903cb82 1776 scan_ident(s, send, tmpbuf, sizeof tmpbuf, FALSE);
a0d0e21e 1777 if ((int)strlen(tmpbuf) > 1 && gv_fetchpv(tmpbuf,FALSE, SVt_PV))
79072805
LW
1778 weight -= 100;
1779 else
1780 weight -= 10;
1781 }
1782 else if (*s == '$' && s[1] &&
93a17b20
LW
1783 strchr("[#!%*<>()-=",s[1])) {
1784 if (/*{*/ strchr("])} =",s[2]))
79072805
LW
1785 weight -= 10;
1786 else
1787 weight -= 1;
1788 }
1789 break;
1790 case '\\':
1791 un_char = 254;
1792 if (s[1]) {
93a17b20 1793 if (strchr("wds]",s[1]))
79072805
LW
1794 weight += 100;
1795 else if (seen['\''] || seen['"'])
1796 weight += 1;
93a17b20 1797 else if (strchr("rnftbxcav",s[1]))
79072805
LW
1798 weight += 40;
1799 else if (isDIGIT(s[1])) {
1800 weight += 40;
1801 while (s[1] && isDIGIT(s[1]))
1802 s++;
1803 }
1804 }
1805 else
1806 weight += 100;
1807 break;
1808 case '-':
1809 if (s[1] == '\\')
1810 weight += 50;
93a17b20 1811 if (strchr("aA01! ",last_un_char))
79072805 1812 weight += 30;
93a17b20 1813 if (strchr("zZ79~",s[1]))
79072805 1814 weight += 30;
f27ffc4a
GS
1815 if (last_un_char == 255 && (isDIGIT(s[1]) || s[1] == '$'))
1816 weight -= 5; /* cope with negative subscript */
79072805
LW
1817 break;
1818 default:
93a17b20 1819 if (!isALNUM(last_un_char) && !strchr("$@&",last_un_char) &&
79072805
LW
1820 isALPHA(*s) && s[1] && isALPHA(s[1])) {
1821 char *d = tmpbuf;
1822 while (isALPHA(*s))
1823 *d++ = *s++;
1824 *d = '\0';
1825 if (keyword(tmpbuf, d - tmpbuf))
1826 weight -= 150;
1827 }
1828 if (un_char == last_un_char + 1)
1829 weight += 5;
1830 weight -= seen[un_char];
1831 break;
1832 }
1833 seen[un_char]++;
1834 }
1835 if (weight >= 0) /* probably a character class */
1836 return FALSE;
1837 }
1838
1839 return TRUE;
1840}
ffed7fef 1841
ffb4593c
NT
1842/*
1843 * S_intuit_method
1844 *
1845 * Does all the checking to disambiguate
1846 * foo bar
1847 * between foo(bar) and bar->foo. Returns 0 if not a method, otherwise
1848 * FUNCMETH (bar->foo(args)) or METHOD (bar->foo args).
1849 *
1850 * First argument is the stuff after the first token, e.g. "bar".
1851 *
1852 * Not a method if bar is a filehandle.
1853 * Not a method if foo is a subroutine prototyped to take a filehandle.
1854 * Not a method if it's really "Foo $bar"
1855 * Method if it's "foo $bar"
1856 * Not a method if it's really "print foo $bar"
1857 * Method if it's really "foo package::" (interpreted as package->foo)
1858 * Not a method if bar is known to be a subroutne ("sub bar; foo bar")
3cb0bbe5 1859 * Not a method if bar is a filehandle or package, but is quoted with
ffb4593c
NT
1860 * =>
1861 */
1862
76e3520e 1863STATIC int
cea2e8a9 1864S_intuit_method(pTHX_ char *start, GV *gv)
a0d0e21e
LW
1865{
1866 char *s = start + (*start == '$');
3280af22 1867 char tmpbuf[sizeof PL_tokenbuf];
a0d0e21e
LW
1868 STRLEN len;
1869 GV* indirgv;
1870
1871 if (gv) {
b6c543e3 1872 CV *cv;
a0d0e21e
LW
1873 if (GvIO(gv))
1874 return 0;
b6c543e3
IZ
1875 if ((cv = GvCVu(gv))) {
1876 char *proto = SvPVX(cv);
1877 if (proto) {
1878 if (*proto == ';')
1879 proto++;
1880 if (*proto == '*')
1881 return 0;
1882 }
1883 } else
a0d0e21e
LW
1884 gv = 0;
1885 }
8903cb82 1886 s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
ffb4593c
NT
1887 /* start is the beginning of the possible filehandle/object,
1888 * and s is the end of it
1889 * tmpbuf is a copy of it
1890 */
1891
a0d0e21e 1892 if (*start == '$') {
3280af22 1893 if (gv || PL_last_lop_op == OP_PRINT || isUPPER(*PL_tokenbuf))
a0d0e21e
LW
1894 return 0;
1895 s = skipspace(s);
3280af22
NIS
1896 PL_bufptr = start;
1897 PL_expect = XREF;
a0d0e21e
LW
1898 return *s == '(' ? FUNCMETH : METHOD;
1899 }
1900 if (!keyword(tmpbuf, len)) {
c3e0f903
GS
1901 if (len > 2 && tmpbuf[len - 2] == ':' && tmpbuf[len - 1] == ':') {
1902 len -= 2;
1903 tmpbuf[len] = '\0';
1904 goto bare_package;
1905 }
1906 indirgv = gv_fetchpv(tmpbuf, FALSE, SVt_PVCV);
8ebc5c01 1907 if (indirgv && GvCVu(indirgv))
a0d0e21e
LW
1908 return 0;
1909 /* filehandle or package name makes it a method */
89bfa8cd 1910 if (!gv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, FALSE)) {
a0d0e21e 1911 s = skipspace(s);
3280af22 1912 if ((PL_bufend - s) >= 2 && *s == '=' && *(s+1) == '>')
55497cff 1913 return 0; /* no assumptions -- "=>" quotes bearword */
c3e0f903 1914 bare_package:
3280af22 1915 PL_nextval[PL_nexttoke].opval = (OP*)newSVOP(OP_CONST, 0,
79cb57f6 1916 newSVpvn(tmpbuf,len));
3280af22
NIS
1917 PL_nextval[PL_nexttoke].opval->op_private = OPpCONST_BARE;
1918 PL_expect = XTERM;
a0d0e21e 1919 force_next(WORD);
3280af22 1920 PL_bufptr = s;
a0d0e21e
LW
1921 return *s == '(' ? FUNCMETH : METHOD;
1922 }
1923 }
1924 return 0;
1925}
1926
ffb4593c
NT
1927/*
1928 * S_incl_perldb
1929 * Return a string of Perl code to load the debugger. If PERL5DB
1930 * is set, it will return the contents of that, otherwise a
1931 * compile-time require of perl5db.pl.
1932 */
1933
76e3520e 1934STATIC char*
cea2e8a9 1935S_incl_perldb(pTHX)
a0d0e21e 1936{
3280af22 1937 if (PL_perldb) {
76e3520e 1938 char *pdb = PerlEnv_getenv("PERL5DB");
a0d0e21e
LW
1939
1940 if (pdb)
1941 return pdb;
91487cfc 1942 SETERRNO(0,SS$_NORMAL);
a0d0e21e
LW
1943 return "BEGIN { require 'perl5db.pl' }";
1944 }
1945 return "";
1946}
1947
1948
16d20bd9 1949/* Encoded script support. filter_add() effectively inserts a
4e553d73 1950 * 'pre-processing' function into the current source input stream.
16d20bd9
AD
1951 * Note that the filter function only applies to the current source file
1952 * (e.g., it will not affect files 'require'd or 'use'd by this one).
1953 *
1954 * The datasv parameter (which may be NULL) can be used to pass
1955 * private data to this instance of the filter. The filter function
1956 * can recover the SV using the FILTER_DATA macro and use it to
1957 * store private buffers and state information.
1958 *
1959 * The supplied datasv parameter is upgraded to a PVIO type
4755096e 1960 * and the IoDIRP/IoANY field is used to store the function pointer,
e0c19803 1961 * and IOf_FAKE_DIRP is enabled on datasv to mark this as such.
16d20bd9
AD
1962 * Note that IoTOP_NAME, IoFMT_NAME, IoBOTTOM_NAME, if set for
1963 * private use must be set using malloc'd pointers.
1964 */
16d20bd9
AD
1965
1966SV *
864dbfa3 1967Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
16d20bd9 1968{
f4c556ac
GS
1969 if (!funcp)
1970 return Nullsv;
1971
3280af22
NIS
1972 if (!PL_rsfp_filters)
1973 PL_rsfp_filters = newAV();
16d20bd9 1974 if (!datasv)
8c52afec 1975 datasv = NEWSV(255,0);
16d20bd9 1976 if (!SvUPGRADE(datasv, SVt_PVIO))
cea2e8a9 1977 Perl_die(aTHX_ "Can't upgrade filter_add data to SVt_PVIO");
4755096e 1978 IoANY(datasv) = (void *)funcp; /* stash funcp into spare field */
e0c19803 1979 IoFLAGS(datasv) |= IOf_FAKE_DIRP;
f4c556ac 1980 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_add func %p (%s)\n",
fe5a182c 1981 (void*)funcp, SvPV_nolen(datasv)));
3280af22
NIS
1982 av_unshift(PL_rsfp_filters, 1);
1983 av_store(PL_rsfp_filters, 0, datasv) ;
16d20bd9
AD
1984 return(datasv);
1985}
4e553d73 1986
16d20bd9
AD
1987
1988/* Delete most recently added instance of this filter function. */
a0d0e21e 1989void
864dbfa3 1990Perl_filter_del(pTHX_ filter_t funcp)
16d20bd9 1991{
e0c19803 1992 SV *datasv;
fe5a182c 1993 DEBUG_P(PerlIO_printf(Perl_debug_log, "filter_del func %p", (void*)funcp));
3280af22 1994 if (!PL_rsfp_filters || AvFILLp(PL_rsfp_filters)<0)
16d20bd9
AD
1995 return;
1996 /* if filter is on top of stack (usual case) just pop it off */
e0c19803 1997 datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters));
4755096e 1998 if (IoANY(datasv) == (void *)funcp) {
e0c19803 1999 IoFLAGS(datasv) &= ~IOf_FAKE_DIRP;
4755096e 2000 IoANY(datasv) = (void *)NULL;
3280af22 2001 sv_free(av_pop(PL_rsfp_filters));
e50aee73 2002
16d20bd9
AD
2003 return;
2004 }
2005 /* we need to search for the correct entry and clear it */
cea2e8a9 2006 Perl_die(aTHX_ "filter_del can only delete in reverse order (currently)");
16d20bd9
AD
2007}
2008
2009
2010/* Invoke the n'th filter function for the current rsfp. */
2011I32
864dbfa3 2012Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
4e553d73
NIS
2013
2014
8ac85365 2015 /* 0 = read one text line */
a0d0e21e 2016{
16d20bd9
AD
2017 filter_t funcp;
2018 SV *datasv = NULL;
e50aee73 2019
3280af22 2020 if (!PL_rsfp_filters)
16d20bd9 2021 return -1;
3280af22 2022 if (idx > AvFILLp(PL_rsfp_filters)){ /* Any more filters? */
16d20bd9
AD
2023 /* Provide a default input filter to make life easy. */
2024 /* Note that we append to the line. This is handy. */
f4c556ac
GS
2025 DEBUG_P(PerlIO_printf(Perl_debug_log,
2026 "filter_read %d: from rsfp\n", idx));
4e553d73 2027 if (maxlen) {
16d20bd9
AD
2028 /* Want a block */
2029 int len ;
2030 int old_len = SvCUR(buf_sv) ;
2031
2032 /* ensure buf_sv is large enough */
2033 SvGROW(buf_sv, old_len + maxlen) ;
3280af22
NIS
2034 if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len, maxlen)) <= 0){
2035 if (PerlIO_error(PL_rsfp))
37120919
AD
2036 return -1; /* error */
2037 else
2038 return 0 ; /* end of file */
2039 }
16d20bd9
AD
2040 SvCUR_set(buf_sv, old_len + len) ;
2041 } else {
2042 /* Want a line */
3280af22
NIS
2043 if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
2044 if (PerlIO_error(PL_rsfp))
37120919
AD
2045 return -1; /* error */
2046 else
2047 return 0 ; /* end of file */
2048 }
16d20bd9
AD
2049 }
2050 return SvCUR(buf_sv);
2051 }
2052 /* Skip this filter slot if filter has been deleted */
3280af22 2053 if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef){
f4c556ac
GS
2054 DEBUG_P(PerlIO_printf(Perl_debug_log,
2055 "filter_read %d: skipped (filter deleted)\n",
2056 idx));
16d20bd9
AD
2057 return FILTER_READ(idx+1, buf_sv, maxlen); /* recurse */
2058 }
2059 /* Get function pointer hidden within datasv */
4755096e 2060 funcp = (filter_t)IoANY(datasv);
f4c556ac
GS
2061 DEBUG_P(PerlIO_printf(Perl_debug_log,
2062 "filter_read %d: via function %p (%s)\n",
fe5a182c 2063 idx, (void*)funcp, SvPV_nolen(datasv)));
16d20bd9
AD
2064 /* Call function. The function is expected to */
2065 /* call "FILTER_READ(idx+1, buf_sv)" first. */
37120919 2066 /* Return: <0:error, =0:eof, >0:not eof */
acfe0abc 2067 return (*funcp)(aTHX_ idx, buf_sv, maxlen);
16d20bd9
AD
2068}
2069
76e3520e 2070STATIC char *
cea2e8a9 2071S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append)
16d20bd9 2072{
c39cd008 2073#ifdef PERL_CR_FILTER
3280af22 2074 if (!PL_rsfp_filters) {
c39cd008 2075 filter_add(S_cr_textfilter,NULL);
a868473f
NIS
2076 }
2077#endif
3280af22 2078 if (PL_rsfp_filters) {
16d20bd9 2079
55497cff 2080 if (!append)
2081 SvCUR_set(sv, 0); /* start with empty line */
16d20bd9
AD
2082 if (FILTER_READ(0, sv, 0) > 0)
2083 return ( SvPVX(sv) ) ;
2084 else
2085 return Nullch ;
2086 }
9d116dd7 2087 else
fd049845 2088 return (sv_gets(sv, fp, append));
a0d0e21e
LW
2089}
2090
01ec43d0
GS
2091STATIC HV *
2092S_find_in_my_stash(pTHX_ char *pkgname, I32 len)
def3634b
GS
2093{
2094 GV *gv;
2095
01ec43d0 2096 if (len == 11 && *pkgname == '_' && strEQ(pkgname, "__PACKAGE__"))
def3634b
GS
2097 return PL_curstash;
2098
2099 if (len > 2 &&
2100 (pkgname[len - 2] == ':' && pkgname[len - 1] == ':') &&
01ec43d0
GS
2101 (gv = gv_fetchpv(pkgname, FALSE, SVt_PVHV)))
2102 {
2103 return GvHV(gv); /* Foo:: */
def3634b
GS
2104 }
2105
2106 /* use constant CLASS => 'MyClass' */
2107 if ((gv = gv_fetchpv(pkgname, FALSE, SVt_PVCV))) {
2108 SV *sv;
2109 if (GvCV(gv) && (sv = cv_const_sv(GvCV(gv)))) {
2110 pkgname = SvPV_nolen(sv);
2111 }
2112 }
2113
2114 return gv_stashpv(pkgname, FALSE);
2115}
a0d0e21e 2116
748a9306
LW
2117#ifdef DEBUGGING
2118 static char* exp_name[] =
09bef843
SB
2119 { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "ATTRBLOCK",
2120 "ATTRTERM", "TERMBLOCK"
2121 };
748a9306 2122#endif
463ee0b2 2123
02aa26ce
NT
2124/*
2125 yylex
2126
2127 Works out what to call the token just pulled out of the input
2128 stream. The yacc parser takes care of taking the ops we return and
2129 stitching them into a tree.
2130
2131 Returns:
2132 PRIVATEREF
2133
2134 Structure:
2135 if read an identifier
2136 if we're in a my declaration
2137 croak if they tried to say my($foo::bar)
2138 build the ops for a my() declaration
2139 if it's an access to a my() variable
2140 are we in a sort block?
2141 croak if my($a); $a <=> $b
2142 build ops for access to a my() variable
2143 if in a dq string, and they've said @foo and we can't find @foo
2144 croak
2145 build ops for a bareword
2146 if we already built the token before, use it.
2147*/
2148
dba4d153 2149#ifdef USE_PURE_BISON
864dbfa3 2150int
dba4d153 2151Perl_yylex_r(pTHX_ YYSTYPE *lvalp, int *lcharp)
378cc40b 2152{
20141f0e
IRC
2153 int r;
2154
6f202aea 2155 yyactlevel++;
20141f0e
IRC
2156 yylval_pointer[yyactlevel] = lvalp;
2157 yychar_pointer[yyactlevel] = lcharp;
b73d6f50
IRC
2158 if (yyactlevel >= YYMAXLEVEL)
2159 Perl_croak(aTHX_ "panic: YYMAXLEVEL");
20141f0e 2160
dba4d153 2161 r = Perl_yylex(aTHX);
20141f0e 2162
d8ae6756
IRC
2163 if (yyactlevel > 0)
2164 yyactlevel--;
20141f0e
IRC
2165
2166 return r;
2167}
dba4d153 2168#endif
20141f0e 2169
dba4d153
JH
2170#ifdef __SC__
2171#pragma segment Perl_yylex
2172#endif
dba4d153 2173int
dba4d153 2174Perl_yylex(pTHX)
20141f0e 2175{
79072805 2176 register char *s;
378cc40b 2177 register char *d;
79072805 2178 register I32 tmp;
463ee0b2 2179 STRLEN len;
161b471a
NIS
2180 GV *gv = Nullgv;
2181 GV **gvp = 0;
aa7440fb 2182 bool bof = FALSE;
a687059c 2183
02aa26ce 2184 /* check if there's an identifier for us to look at */
ba979b31 2185 if (PL_pending_ident)
e930465f 2186 return S_pending_ident(aTHX);
bbce6d69 2187
02aa26ce
NT
2188 /* no identifier pending identification */
2189
3280af22 2190 switch (PL_lex_state) {
79072805
LW
2191#ifdef COMMENTARY
2192 case LEX_NORMAL: /* Some compilers will produce faster */
2193 case LEX_INTERPNORMAL: /* code if we comment these out. */
2194 break;
2195#endif
2196
09bef843 2197 /* when we've already built the next token, just pull it out of the queue */
79072805 2198 case LEX_KNOWNEXT:
3280af22
NIS
2199 PL_nexttoke--;
2200 yylval = PL_nextval[PL_nexttoke];
2201 if (!PL_nexttoke) {
2202 PL_lex_state = PL_lex_defer;
2203 PL_expect = PL_lex_expect;
2204 PL_lex_defer = LEX_NORMAL;
463ee0b2 2205 }
607df283 2206 DEBUG_T({ PerlIO_printf(Perl_debug_log,
4659c93f 2207 "### Next token after '%s' was known, type %"IVdf"\n", PL_bufptr,
5f80b19c 2208 (IV)PL_nexttype[PL_nexttoke]); });
607df283 2209
3280af22 2210 return(PL_nexttype[PL_nexttoke]);
79072805 2211
02aa26ce 2212 /* interpolated case modifiers like \L \U, including \Q and \E.
3280af22 2213 when we get here, PL_bufptr is at the \
02aa26ce 2214 */
79072805
LW
2215 case LEX_INTERPCASEMOD:
2216#ifdef DEBUGGING
3280af22 2217 if (PL_bufptr != PL_bufend && *PL_bufptr != '\\')
cea2e8a9 2218 Perl_croak(aTHX_ "panic: INTERPCASEMOD");
79072805 2219#endif
02aa26ce 2220 /* handle \E or end of string */
3280af22 2221 if (PL_bufptr == PL_bufend || PL_bufptr[1] == 'E') {
a0d0e21e 2222 char oldmod;
02aa26ce
NT
2223
2224 /* if at a \E */
3280af22
NIS
2225 if (PL_lex_casemods) {
2226 oldmod = PL_lex_casestack[--PL_lex_casemods];
2227 PL_lex_casestack[PL_lex_casemods] = '\0';
02aa26ce 2228
3280af22
NIS
2229 if (PL_bufptr != PL_bufend && strchr("LUQ", oldmod)) {
2230 PL_bufptr += 2;
2231 PL_lex_state = LEX_INTERPCONCAT;
a0d0e21e 2232 }
79072805
LW
2233 return ')';
2234 }
3280af22
NIS
2235 if (PL_bufptr != PL_bufend)
2236 PL_bufptr += 2;
2237 PL_lex_state = LEX_INTERPCONCAT;
cea2e8a9 2238 return yylex();
79072805
LW
2239 }
2240 else {
607df283 2241 DEBUG_T({ PerlIO_printf(Perl_debug_log,
5f80b19c 2242 "### Saw case modifier at '%s'\n", PL_bufptr); });
3280af22 2243 s = PL_bufptr + 1;
79072805
LW
2244 if (strnEQ(s, "L\\u", 3) || strnEQ(s, "U\\l", 3))
2245 tmp = *s, *s = s[2], s[2] = tmp; /* misordered... */
a0d0e21e 2246 if (strchr("LU", *s) &&
3280af22 2247 (strchr(PL_lex_casestack, 'L') || strchr(PL_lex_casestack, 'U')))
a0d0e21e 2248 {
3280af22 2249 PL_lex_casestack[--PL_lex_casemods] = '\0';
a0d0e21e
LW
2250 return ')';
2251 }
3280af22
NIS
2252 if (PL_lex_casemods > 10) {
2253 char* newlb = Renew(PL_lex_casestack, PL_lex_casemods + 2, char);
2254 if (newlb != PL_lex_casestack) {
a0d0e21e 2255 SAVEFREEPV(newlb);
3280af22 2256 PL_lex_casestack = newlb;
a0d0e21e
LW
2257 }
2258 }
3280af22
NIS
2259 PL_lex_casestack[PL_lex_casemods++] = *s;
2260 PL_lex_casestack[PL_lex_casemods] = '\0';
2261 PL_lex_state = LEX_INTERPCONCAT;
2262 PL_nextval[PL_nexttoke].ival = 0;
79072805
LW
2263 force_next('(');
2264 if (*s == 'l')
3280af22 2265 PL_nextval[PL_nexttoke].ival = OP_LCFIRST;
79072805 2266 else if (*s == 'u')
3280af22 2267 PL_nextval[PL_nexttoke].ival = OP_UCFIRST;
79072805 2268 else if (*s == 'L')
3280af22 2269 PL_nextval[PL_nexttoke].ival = OP_LC;
79072805 2270 else if (*s == 'U')
3280af22 2271 PL_nextval[PL_nexttoke].ival = OP_UC;
a0d0e21e 2272 else if (*s == 'Q')
3280af22 2273 PL_nextval[PL_nexttoke].ival = OP_QUOTEMETA;
79072805 2274 else
cea2e8a9 2275 Perl_croak(aTHX_ "panic: yylex");
3280af22 2276 PL_bufptr = s + 1;
79072805 2277 force_next(FUNC);
3280af22
NIS
2278 if (PL_lex_starts) {
2279 s = PL_bufptr;
2280 PL_lex_starts = 0;
79072805
LW
2281 Aop(OP_CONCAT);
2282 }
2283 else
cea2e8a9 2284 return yylex();
79072805
LW
2285 }
2286
55497cff 2287 case LEX_INTERPPUSH:
2288 return sublex_push();
2289
79072805 2290 case LEX_INTERPSTART:
3280af22 2291 if (PL_bufptr == PL_bufend)
79072805 2292 return sublex_done();
607df283 2293 DEBUG_T({ PerlIO_printf(Perl_debug_log,
5f80b19c 2294 "### Interpolated variable at '%s'\n", PL_bufptr); });
3280af22
NIS
2295 PL_expect = XTERM;
2296 PL_lex_dojoin = (*PL_bufptr == '@');
2297 PL_lex_state = LEX_INTERPNORMAL;
2298 if (PL_lex_dojoin) {
2299 PL_nextval[PL_nexttoke].ival = 0;
79072805 2300 force_next(',');
4d1ff10f 2301#ifdef USE_5005THREADS
533c011a
NIS
2302 PL_nextval[PL_nexttoke].opval = newOP(OP_THREADSV, 0);
2303 PL_nextval[PL_nexttoke].opval->op_targ = find_threadsv("\"");
554b3eca
MB
2304 force_next(PRIVATEREF);
2305#else
a0d0e21e 2306 force_ident("\"", '$');
4d1ff10f 2307#endif /* USE_5005THREADS */
3280af22 2308 PL_nextval[PL_nexttoke].ival = 0;
79072805 2309 force_next('$');
3280af22 2310 PL_nextval[PL_nexttoke].ival = 0;
79072805 2311 force_next('(');
3280af22 2312 PL_nextval[PL_nexttoke].ival = OP_JOIN; /* emulate join($", ...) */
79072805
LW
2313 force_next(FUNC);
2314 }
3280af22
NIS
2315 if (PL_lex_starts++) {
2316 s = PL_bufptr;
79072805
LW
2317 Aop(OP_CONCAT);
2318 }
cea2e8a9 2319 return yylex();
79072805
LW
2320
2321 case LEX_INTERPENDMAYBE:
3280af22
NIS
2322 if (intuit_more(PL_bufptr)) {
2323 PL_lex_state = LEX_INTERPNORMAL; /* false alarm, more expr */
79072805
LW
2324 break;
2325 }
2326 /* FALL THROUGH */
2327
2328 case LEX_INTERPEND:
3280af22
NIS
2329 if (PL_lex_dojoin) {
2330 PL_lex_dojoin = FALSE;
2331 PL_lex_state = LEX_INTERPCONCAT;
79072805
LW
2332 return ')';
2333 }
43a16006 2334 if (PL_lex_inwhat == OP_SUBST && PL_linestr == PL_lex_repl
25da4f38 2335 && SvEVALED(PL_lex_repl))
43a16006 2336 {
e9fa98b2 2337 if (PL_bufptr != PL_bufend)
cea2e8a9 2338 Perl_croak(aTHX_ "Bad evalled substitution pattern");
e9fa98b2
HS
2339 PL_lex_repl = Nullsv;
2340 }
79072805
LW
2341 /* FALLTHROUGH */
2342 case LEX_INTERPCONCAT:
2343#ifdef DEBUGGING
3280af22 2344 if (PL_lex_brackets)
cea2e8a9 2345 Perl_croak(aTHX_ "panic: INTERPCONCAT");
79072805 2346#endif
3280af22 2347 if (PL_bufptr == PL_bufend)
79072805
LW
2348 return sublex_done();
2349
3280af22
NIS
2350 if (SvIVX(PL_linestr) == '\'') {
2351 SV *sv = newSVsv(PL_linestr);
2352 if (!PL_lex_inpat)
76e3520e 2353 sv = tokeq(sv);
3280af22 2354 else if ( PL_hints & HINT_NEW_RE )
b3ac6de7 2355 sv = new_constant(NULL, 0, "qr", sv, sv, "q");
79072805 2356 yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
3280af22 2357 s = PL_bufend;
79072805
LW
2358 }
2359 else {
3280af22 2360 s = scan_const(PL_bufptr);
79072805 2361 if (*s == '\\')
3280af22 2362 PL_lex_state = LEX_INTERPCASEMOD;
79072805 2363 else
3280af22 2364 PL_lex_state = LEX_INTERPSTART;
79072805
LW
2365 }
2366
3280af22
NIS
2367 if (s != PL_bufptr) {
2368 PL_nextval[PL_nexttoke] = yylval;
2369 PL_expect = XTERM;
79072805 2370 force_next(THING);
3280af22 2371 if (PL_lex_starts++)
79072805
LW
2372 Aop(OP_CONCAT);
2373 else {
3280af22 2374 PL_bufptr = s;
cea2e8a9 2375 return yylex();
79072805
LW
2376 }
2377 }
2378
cea2e8a9 2379 return yylex();
a0d0e21e 2380 case LEX_FORMLINE:
3280af22
NIS
2381 PL_lex_state = LEX_NORMAL;
2382 s = scan_formline(PL_bufptr);
2383 if (!PL_lex_formbrack)
a0d0e21e
LW
2384 goto rightbracket;
2385 OPERATOR(';');
79072805
LW
2386 }
2387
3280af22
NIS
2388 s = PL_bufptr;
2389 PL_oldoldbufptr = PL_oldbufptr;
2390 PL_oldbufptr = s;
607df283 2391 DEBUG_T( {
bf49b057
GS
2392 PerlIO_printf(Perl_debug_log, "### Tokener expecting %s at %s\n",
2393 exp_name[PL_expect], s);
5f80b19c 2394 } );
463ee0b2
LW
2395
2396 retry:
378cc40b
LW
2397 switch (*s) {
2398 default:
7e2040f0 2399 if (isIDFIRST_lazy_if(s,UTF))
834a4ddd 2400 goto keylookup;
cea2e8a9 2401 Perl_croak(aTHX_ "Unrecognized character \\x%02X", *s & 255);
e929a76b
LW
2402 case 4:
2403 case 26:
2404 goto fake_eof; /* emulate EOF on ^D or ^Z */
378cc40b 2405 case 0:
3280af22
NIS
2406 if (!PL_rsfp) {
2407 PL_last_uni = 0;
2408 PL_last_lop = 0;
2409 if (PL_lex_brackets)
d98d5fff 2410 yyerror("Missing right curly or square bracket");
4e553d73 2411 DEBUG_T( { PerlIO_printf(Perl_debug_log,
607df283 2412 "### Tokener got EOF\n");
5f80b19c 2413 } );
79072805 2414 TOKEN(0);
463ee0b2 2415 }
3280af22 2416 if (s++ < PL_bufend)
a687059c 2417 goto retry; /* ignore stray nulls */
3280af22
NIS
2418 PL_last_uni = 0;
2419 PL_last_lop = 0;
2420 if (!PL_in_eval && !PL_preambled) {
2421 PL_preambled = TRUE;
2422 sv_setpv(PL_linestr,incl_perldb());
2423 if (SvCUR(PL_linestr))
2424 sv_catpv(PL_linestr,";");
2425 if (PL_preambleav){
2426 while(AvFILLp(PL_preambleav) >= 0) {
2427 SV *tmpsv = av_shift(PL_preambleav);
2428 sv_catsv(PL_linestr, tmpsv);
2429 sv_catpv(PL_linestr, ";");
91b7def8 2430 sv_free(tmpsv);
2431 }
3280af22
NIS
2432 sv_free((SV*)PL_preambleav);
2433 PL_preambleav = NULL;
91b7def8 2434 }
3280af22
NIS
2435 if (PL_minus_n || PL_minus_p) {
2436 sv_catpv(PL_linestr, "LINE: while (<>) {");
2437 if (PL_minus_l)
2438 sv_catpv(PL_linestr,"chomp;");
2439 if (PL_minus_a) {
3280af22
NIS
2440 if (PL_minus_F) {
2441 if (strchr("/'\"", *PL_splitstr)
2442 && strchr(PL_splitstr + 1, *PL_splitstr))
3db68c4c 2443 Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
54310121 2444 else {
2445 char delim;
2446 s = "'~#\200\1'"; /* surely one char is unused...*/
3280af22 2447 while (s[1] && strchr(PL_splitstr, *s)) s++;
54310121 2448 delim = *s;
75c72d73 2449 Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s%c",
46fc3d4c 2450 "q" + (delim == '\''), delim);
3280af22 2451 for (s = PL_splitstr; *s; s++) {
54310121 2452 if (*s == '\\')
3280af22
NIS
2453 sv_catpvn(PL_linestr, "\\", 1);
2454 sv_catpvn(PL_linestr, s, 1);
54310121 2455 }
cea2e8a9 2456 Perl_sv_catpvf(aTHX_ PL_linestr, "%c);", delim);
54310121 2457 }
2304df62
AD
2458 }
2459 else
75c72d73 2460 sv_catpv(PL_linestr,"our @F=split(' ');");
2304df62 2461 }
79072805 2462 }
3280af22
NIS
2463 sv_catpv(PL_linestr, "\n");
2464 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2465 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 2466 PL_last_lop = PL_last_uni = Nullch;
3280af22 2467 if (PERLDB_LINE && PL_curstash != PL_debstash) {
a0d0e21e
LW
2468 SV *sv = NEWSV(85,0);
2469
2470 sv_upgrade(sv, SVt_PVMG);
3280af22 2471 sv_setsv(sv,PL_linestr);
0ac0412a
MJD
2472 (void)SvIOK_on(sv);
2473 SvIVX(sv) = 0;
57843af0 2474 av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv);
a0d0e21e 2475 }
79072805 2476 goto retry;
a687059c 2477 }
e929a76b 2478 do {
aa7440fb 2479 bof = PL_rsfp ? TRUE : FALSE;
7e28d3af
JH
2480 if ((s = filter_gets(PL_linestr, PL_rsfp, 0)) == Nullch) {
2481 fake_eof:
2482 if (PL_rsfp) {
2483 if (PL_preprocess && !PL_in_eval)
2484 (void)PerlProc_pclose(PL_rsfp);
2485 else if ((PerlIO *)PL_rsfp == PerlIO_stdin())
2486 PerlIO_clearerr(PL_rsfp);
2487 else
2488 (void)PerlIO_close(PL_rsfp);
2489 PL_rsfp = Nullfp;
2490 PL_doextract = FALSE;
2491 }
2492 if (!PL_in_eval && (PL_minus_n || PL_minus_p)) {
2493 sv_setpv(PL_linestr,PL_minus_p ? ";}continue{print" : "");
2494 sv_catpv(PL_linestr,";}");
2495 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2496 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 2497 PL_last_lop = PL_last_uni = Nullch;
7e28d3af
JH
2498 PL_minus_n = PL_minus_p = 0;
2499 goto retry;
2500 }
2501 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
207e3d1a 2502 PL_last_lop = PL_last_uni = Nullch;
7e28d3af
JH
2503 sv_setpv(PL_linestr,"");
2504 TOKEN(';'); /* not infinite loop because rsfp is NULL now */
2505 }
2506 /* if it looks like the start of a BOM, check if it in fact is */
2507 else if (bof && (!*s || *(U8*)s == 0xEF || *(U8*)s >= 0xFE)) {
226017aa 2508#ifdef PERLIO_IS_STDIO
e3f494f1
JH
2509# ifdef __GNU_LIBRARY__
2510# if __GNU_LIBRARY__ == 1 /* Linux glibc5 */
226017aa
DD
2511# define FTELL_FOR_PIPE_IS_BROKEN
2512# endif
e3f494f1
JH
2513# else
2514# ifdef __GLIBC__
2515# if __GLIBC__ == 1 /* maybe some glibc5 release had it like this? */
2516# define FTELL_FOR_PIPE_IS_BROKEN
2517# endif
2518# endif
226017aa
DD
2519# endif
2520#endif
2521#ifdef FTELL_FOR_PIPE_IS_BROKEN
2522 /* This loses the possibility to detect the bof
2523 * situation on perl -P when the libc5 is being used.
2524 * Workaround? Maybe attach some extra state to PL_rsfp?
2525 */
2526 if (!PL_preprocess)
7e28d3af 2527 bof = PerlIO_tell(PL_rsfp) == SvCUR(PL_linestr);
226017aa 2528#else
7e28d3af 2529 bof = PerlIO_tell(PL_rsfp) == SvCUR(PL_linestr);
226017aa 2530#endif
7e28d3af 2531 if (bof) {
3280af22 2532 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
7e28d3af 2533 s = swallow_bom((U8*)s);
e929a76b 2534 }
378cc40b 2535 }
3280af22 2536 if (PL_doextract) {
a0d0e21e 2537 if (*s == '#' && s[1] == '!' && instr(s,"perl"))
3280af22 2538 PL_doextract = FALSE;
a0d0e21e
LW
2539
2540 /* Incest with pod. */
2541 if (*s == '=' && strnEQ(s, "=cut", 4)) {
3280af22
NIS
2542 sv_setpv(PL_linestr, "");
2543 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2544 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 2545 PL_last_lop = PL_last_uni = Nullch;
3280af22 2546 PL_doextract = FALSE;
a0d0e21e 2547 }
4e553d73 2548 }
463ee0b2 2549 incline(s);
3280af22
NIS
2550 } while (PL_doextract);
2551 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = s;
2552 if (PERLDB_LINE && PL_curstash != PL_debstash) {
79072805 2553 SV *sv = NEWSV(85,0);
a687059c 2554
93a17b20 2555 sv_upgrade(sv, SVt_PVMG);
3280af22 2556 sv_setsv(sv,PL_linestr);
0ac0412a
MJD
2557 (void)SvIOK_on(sv);
2558 SvIVX(sv) = 0;
57843af0 2559 av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv);
a687059c 2560 }
3280af22 2561 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 2562 PL_last_lop = PL_last_uni = Nullch;
57843af0 2563 if (CopLINE(PL_curcop) == 1) {
3280af22 2564 while (s < PL_bufend && isSPACE(*s))
79072805 2565 s++;
a0d0e21e 2566 if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
79072805 2567 s++;
44a8e56a 2568 d = Nullch;
3280af22 2569 if (!PL_in_eval) {
44a8e56a 2570 if (*s == '#' && *(s+1) == '!')
2571 d = s + 2;
2572#ifdef ALTERNATE_SHEBANG
2573 else {
2574 static char as[] = ALTERNATE_SHEBANG;
2575 if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
2576 d = s + (sizeof(as) - 1);
2577 }
2578#endif /* ALTERNATE_SHEBANG */
2579 }
2580 if (d) {
b8378b72 2581 char *ipath;
774d564b 2582 char *ipathend;
b8378b72 2583
774d564b 2584 while (isSPACE(*d))
b8378b72
CS
2585 d++;
2586 ipath = d;
774d564b 2587 while (*d && !isSPACE(*d))
2588 d++;
2589 ipathend = d;
2590
2591#ifdef ARG_ZERO_IS_SCRIPT
2592 if (ipathend > ipath) {
2593 /*
2594 * HP-UX (at least) sets argv[0] to the script name,
2595 * which makes $^X incorrect. And Digital UNIX and Linux,
2596 * at least, set argv[0] to the basename of the Perl
2597 * interpreter. So, having found "#!", we'll set it right.
2598 */
ee2f7564 2599 SV *x = GvSV(gv_fetchpv("\030", TRUE, SVt_PV)); /* $^X */
774d564b 2600 assert(SvPOK(x) || SvGMAGICAL(x));
cc49e20b 2601 if (sv_eq(x, CopFILESV(PL_curcop))) {
774d564b 2602 sv_setpvn(x, ipath, ipathend - ipath);
9607fc9c 2603 SvSETMAGIC(x);
2604 }
774d564b 2605 TAINT_NOT; /* $^X is always tainted, but that's OK */
8ebc5c01 2606 }
774d564b 2607#endif /* ARG_ZERO_IS_SCRIPT */
b8378b72
CS
2608
2609 /*
2610 * Look for options.
2611 */
748a9306 2612 d = instr(s,"perl -");
84e30d1a 2613 if (!d) {
748a9306 2614 d = instr(s,"perl");
84e30d1a
GS
2615#if defined(DOSISH)
2616 /* avoid getting into infinite loops when shebang
2617 * line contains "Perl" rather than "perl" */
2618 if (!d) {
2619 for (d = ipathend-4; d >= ipath; --d) {
2620 if ((*d == 'p' || *d == 'P')
2621 && !ibcmp(d, "perl", 4))
2622 {
2623 break;
2624 }
2625 }
2626 if (d < ipath)
2627 d = Nullch;
2628 }
2629#endif
2630 }
44a8e56a 2631#ifdef ALTERNATE_SHEBANG
2632 /*
2633 * If the ALTERNATE_SHEBANG on this system starts with a
2634 * character that can be part of a Perl expression, then if
2635 * we see it but not "perl", we're probably looking at the
2636 * start of Perl code, not a request to hand off to some
2637 * other interpreter. Similarly, if "perl" is there, but
2638 * not in the first 'word' of the line, we assume the line
2639 * contains the start of the Perl program.
44a8e56a 2640 */
2641 if (d && *s != '#') {
774d564b 2642 char *c = ipath;
44a8e56a 2643 while (*c && !strchr("; \t\r\n\f\v#", *c))
2644 c++;
2645 if (c < d)
2646 d = Nullch; /* "perl" not in first word; ignore */
2647 else
2648 *s = '#'; /* Don't try to parse shebang line */
2649 }
774d564b 2650#endif /* ALTERNATE_SHEBANG */
bf4acbe4 2651#ifndef MACOS_TRADITIONAL
748a9306 2652 if (!d &&
44a8e56a 2653 *s == '#' &&
774d564b 2654 ipathend > ipath &&
3280af22 2655 !PL_minus_c &&
748a9306 2656 !instr(s,"indir") &&
3280af22 2657 instr(PL_origargv[0],"perl"))
748a9306 2658 {
9f68db38 2659 char **newargv;
9f68db38 2660
774d564b 2661 *ipathend = '\0';
2662 s = ipathend + 1;
3280af22 2663 while (s < PL_bufend && isSPACE(*s))
9f68db38 2664 s++;
3280af22
NIS
2665 if (s < PL_bufend) {
2666 Newz(899,newargv,PL_origargc+3,char*);
9f68db38 2667 newargv[1] = s;
3280af22 2668 while (s < PL_bufend && !isSPACE(*s))
9f68db38
LW
2669 s++;
2670 *s = '\0';
3280af22 2671 Copy(PL_origargv+1, newargv+2, PL_origargc+1, char*);
9f68db38
LW
2672 }
2673 else
3280af22 2674 newargv = PL_origargv;
774d564b 2675 newargv[0] = ipath;
b4748376 2676 PerlProc_execv(ipath, EXEC_ARGV_CAST(newargv));
cea2e8a9 2677 Perl_croak(aTHX_ "Can't exec %s", ipath);
9f68db38 2678 }
bf4acbe4 2679#endif
748a9306 2680 if (d) {
3280af22
NIS
2681 U32 oldpdb = PL_perldb;
2682 bool oldn = PL_minus_n;
2683 bool oldp = PL_minus_p;
748a9306
LW
2684
2685 while (*d && !isSPACE(*d)) d++;
bf4acbe4 2686 while (SPACE_OR_TAB(*d)) d++;
748a9306
LW
2687
2688 if (*d++ == '-') {
a11ec5a9 2689 bool switches_done = PL_doswitches;
8cc95fdb 2690 do {
2691 if (*d == 'M' || *d == 'm') {
2692 char *m = d;
2693 while (*d && !isSPACE(*d)) d++;
cea2e8a9 2694 Perl_croak(aTHX_ "Too late for \"-%.*s\" option",
8cc95fdb 2695 (int)(d - m), m);
2696 }
2697 d = moreswitches(d);
2698 } while (d);
155aba94
GS
2699 if ((PERLDB_LINE && !oldpdb) ||
2700 ((PL_minus_n || PL_minus_p) && !(oldn || oldp)))
b084f20b 2701 /* if we have already added "LINE: while (<>) {",
2702 we must not do it again */
748a9306 2703 {
3280af22
NIS
2704 sv_setpv(PL_linestr, "");
2705 PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = SvPVX(PL_linestr);
2706 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
207e3d1a 2707 PL_last_lop = PL_last_uni = Nullch;
3280af22 2708 PL_preambled = FALSE;
84902520 2709 if (PERLDB_LINE)
3280af22 2710 (void)gv_fetchfile(PL_origfilename);
748a9306
LW
2711 goto retry;
2712 }
a11ec5a9
RGS
2713 if (PL_doswitches && !switches_done) {
2714 int argc = PL_origargc;
2715 char **argv = PL_origargv;
2716 do {
2717 argc--,argv++;
2718 } while (argc && argv[0][0] == '-' && argv[0][1]);
2719 init_argv_symbols(argc,argv);
2720 }
a0d0e21e 2721 }
79072805 2722 }
9f68db38 2723 }
79072805 2724 }
3280af22
NIS
2725 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
2726 PL_bufptr = s;
2727 PL_lex_state = LEX_FORMLINE;
cea2e8a9 2728 return yylex();
ae986130 2729 }
378cc40b 2730 goto retry;
4fdae800 2731 case '\r':
6a27c188 2732#ifdef PERL_STRICT_CR
cea2e8a9 2733 Perl_warn(aTHX_ "Illegal character \\%03o (carriage return)", '\r');
4e553d73 2734 Perl_croak(aTHX_
cc507455 2735 "\t(Maybe you didn't strip carriage returns after a network transfer?)\n");
a868473f 2736#endif
4fdae800 2737 case ' ': case '\t': case '\f': case 013:
bf4acbe4
GS
2738#ifdef MACOS_TRADITIONAL
2739 case '\312':
2740#endif
378cc40b
LW
2741 s++;
2742 goto retry;
378cc40b 2743 case '#':
e929a76b 2744 case '\n':
3280af22 2745 if (PL_lex_state != LEX_NORMAL || (PL_in_eval && !PL_rsfp)) {
df0deb90
GS
2746 if (*s == '#' && s == PL_linestart && PL_in_eval && !PL_rsfp) {
2747 /* handle eval qq[#line 1 "foo"\n ...] */
2748 CopLINE_dec(PL_curcop);
2749 incline(s);
2750 }
3280af22 2751 d = PL_bufend;
a687059c 2752 while (s < d && *s != '\n')
378cc40b 2753 s++;
0f85fab0 2754 if (s < d)
378cc40b 2755 s++;
78c267c1 2756 else if (s > d) /* Found by Ilya: feed random input to Perl. */
a8406387 2757 Perl_croak(aTHX_ "panic: input overflow");
463ee0b2 2758 incline(s);
3280af22
NIS
2759 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
2760 PL_bufptr = s;
2761 PL_lex_state = LEX_FORMLINE;
cea2e8a9 2762 return yylex();
a687059c 2763 }
378cc40b 2764 }
a687059c 2765 else {
378cc40b 2766 *s = '\0';
3280af22 2767 PL_bufend = s;
a687059c 2768 }
378cc40b
LW
2769 goto retry;
2770 case '-':
79072805 2771 if (s[1] && isALPHA(s[1]) && !isALNUM(s[2])) {
e5edeb50
JH
2772 I32 ftst = 0;
2773
378cc40b 2774 s++;
3280af22 2775 PL_bufptr = s;
748a9306
LW
2776 tmp = *s++;
2777
bf4acbe4 2778 while (s < PL_bufend && SPACE_OR_TAB(*s))
748a9306
LW
2779 s++;
2780
2781 if (strnEQ(s,"=>",2)) {
3280af22 2782 s = force_word(PL_bufptr,WORD,FALSE,FALSE,FALSE);
4e553d73 2783 DEBUG_T( { PerlIO_printf(Perl_debug_log,
607df283 2784 "### Saw unary minus before =>, forcing word '%s'\n", s);
5f80b19c 2785 } );
748a9306
LW
2786 OPERATOR('-'); /* unary minus */
2787 }
3280af22 2788 PL_last_uni = PL_oldbufptr;
748a9306 2789 switch (tmp) {
e5edeb50
JH
2790 case 'r': ftst = OP_FTEREAD; break;
2791 case 'w': ftst = OP_FTEWRITE; break;
2792 case 'x': ftst = OP_FTEEXEC; break;
2793 case 'o': ftst = OP_FTEOWNED; break;
2794 case 'R': ftst = OP_FTRREAD; break;
2795 case 'W': ftst = OP_FTRWRITE; break;
2796 case 'X': ftst = OP_FTREXEC; break;
2797 case 'O': ftst = OP_FTROWNED; break;
2798 case 'e': ftst = OP_FTIS; break;
2799 case 'z': ftst = OP_FTZERO; break;
2800 case 's': ftst = OP_FTSIZE; break;
2801 case 'f': ftst = OP_FTFILE; break;
2802 case 'd': ftst = OP_FTDIR; break;
2803 case 'l': ftst = OP_FTLINK; break;
2804 case 'p': ftst = OP_FTPIPE; break;
2805 case 'S': ftst = OP_FTSOCK; break;
2806 case 'u': ftst = OP_FTSUID; break;
2807 case 'g': ftst = OP_FTSGID; break;
2808 case 'k': ftst = OP_FTSVTX; break;
2809 case 'b': ftst = OP_FTBLK; break;
2810 case 'c': ftst = OP_FTCHR; break;
2811 case 't': ftst = OP_FTTTY; break;
2812 case 'T': ftst = OP_FTTEXT; break;
2813 case 'B': ftst = OP_FTBINARY; break;
2814 case 'M': case 'A': case 'C':
2815 gv_fetchpv("\024",TRUE, SVt_PV);
2816 switch (tmp) {
2817 case 'M': ftst = OP_FTMTIME; break;
2818 case 'A': ftst = OP_FTATIME; break;
2819 case 'C': ftst = OP_FTCTIME; break;
2820 default: break;
2821 }
2822 break;
378cc40b 2823 default:
378cc40b
LW
2824 break;
2825 }
e5edeb50
JH
2826 if (ftst) {
2827 PL_last_lop_op = ftst;
4e553d73 2828 DEBUG_T( { PerlIO_printf(Perl_debug_log,
0844c848 2829 "### Saw file test %c\n", (int)ftst);
5f80b19c 2830 } );
e5edeb50
JH
2831 FTST(ftst);
2832 }
2833 else {
2834 /* Assume it was a minus followed by a one-letter named
2835 * subroutine call (or a -bareword), then. */
95c31fe3 2836 DEBUG_T( { PerlIO_printf(Perl_debug_log,
0844c848
RB
2837 "### %c looked like a file test but was not\n",
2838 (int)ftst);
5f80b19c 2839 } );
e5edeb50
JH
2840 s -= 2;
2841 }
378cc40b 2842 }
a687059c
LW
2843 tmp = *s++;
2844 if (*s == tmp) {
2845 s++;
3280af22 2846 if (PL_expect == XOPERATOR)
79072805
LW
2847 TERM(POSTDEC);
2848 else
2849 OPERATOR(PREDEC);
2850 }
2851 else if (*s == '>') {
2852 s++;
2853 s = skipspace(s);
7e2040f0 2854 if (isIDFIRST_lazy_if(s,UTF)) {
a0d0e21e 2855 s = force_word(s,METHOD,FALSE,TRUE,FALSE);
463ee0b2 2856 TOKEN(ARROW);
79072805 2857 }
748a9306
LW
2858 else if (*s == '$')
2859 OPERATOR(ARROW);
463ee0b2 2860 else
748a9306 2861 TERM(ARROW);
a687059c 2862 }
3280af22 2863 if (PL_expect == XOPERATOR)
79072805
LW
2864 Aop(OP_SUBTRACT);
2865 else {
3280af22 2866 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
2f3197b3 2867 check_uni();
79072805 2868 OPERATOR('-'); /* unary minus */
2f3197b3 2869 }
79072805 2870
378cc40b 2871 case '+':
a687059c
LW
2872 tmp = *s++;
2873 if (*s == tmp) {
378cc40b 2874 s++;
3280af22 2875 if (PL_expect == XOPERATOR)
79072805
LW
2876 TERM(POSTINC);
2877 else
2878 OPERATOR(PREINC);
378cc40b 2879 }
3280af22 2880 if (PL_expect == XOPERATOR)
79072805
LW
2881 Aop(OP_ADD);
2882 else {
3280af22 2883 if (isSPACE(*s) || !isSPACE(*PL_bufptr))
2f3197b3 2884 check_uni();
a687059c 2885 OPERATOR('+');
2f3197b3 2886 }
a687059c 2887
378cc40b 2888 case '*':
3280af22
NIS
2889 if (PL_expect != XOPERATOR) {
2890 s = scan_ident(s, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
2891 PL_expect = XOPERATOR;
2892 force_ident(PL_tokenbuf, '*');
2893 if (!*PL_tokenbuf)
a0d0e21e 2894 PREREF('*');
79072805 2895 TERM('*');
a687059c 2896 }
79072805
LW
2897 s++;
2898 if (*s == '*') {
a687059c 2899 s++;
79072805 2900 PWop(OP_POW);
a687059c 2901 }
79072805
LW
2902 Mop(OP_MULTIPLY);
2903
378cc40b 2904 case '%':
3280af22 2905 if (PL_expect == XOPERATOR) {
bbce6d69 2906 ++s;
2907 Mop(OP_MODULO);
a687059c 2908 }
3280af22
NIS
2909 PL_tokenbuf[0] = '%';
2910 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1, TRUE);
2911 if (!PL_tokenbuf[1]) {
2912 if (s == PL_bufend)
bbce6d69 2913 yyerror("Final % should be \\% or %name");
2914 PREREF('%');
a687059c 2915 }
3280af22 2916 PL_pending_ident = '%';
bbce6d69 2917 TERM('%');
a687059c 2918
378cc40b 2919 case '^':
79072805 2920 s++;
a0d0e21e 2921 BOop(OP_BIT_XOR);
79072805 2922 case '[':
3280af22 2923 PL_lex_brackets++;
79072805 2924 /* FALL THROUGH */
378cc40b 2925 case '~':
378cc40b 2926 case ',':
378cc40b
LW
2927 tmp = *s++;
2928 OPERATOR(tmp);
a0d0e21e
LW
2929 case ':':
2930 if (s[1] == ':') {
2931 len = 0;
2932 goto just_a_word;
2933 }
2934 s++;
09bef843
SB
2935 switch (PL_expect) {
2936 OP *attrs;
2937 case XOPERATOR:
2938 if (!PL_in_my || PL_lex_state != LEX_NORMAL)
2939 break;
2940 PL_bufptr = s; /* update in case we back off */
2941 goto grabattrs;
2942 case XATTRBLOCK:
2943 PL_expect = XBLOCK;
2944 goto grabattrs;
2945 case XATTRTERM:
2946 PL_expect = XTERMBLOCK;
2947 grabattrs:
2948 s = skipspace(s);
2949 attrs = Nullop;
7e2040f0 2950 while (isIDFIRST_lazy_if(s,UTF)) {
09bef843 2951 d = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, FALSE, &len);
f9829d6b
GS
2952 if (isLOWER(*s) && (tmp = keyword(PL_tokenbuf, len))) {
2953 if (tmp < 0) tmp = -tmp;
2954 switch (tmp) {
2955 case KEY_or:
2956 case KEY_and:
2957 case KEY_for:
2958 case KEY_unless:
2959 case KEY_if:
2960 case KEY_while:
2961 case KEY_until:
2962 goto got_attrs;
2963 default:
2964 break;
2965 }
2966 }
09bef843
SB
2967 if (*d == '(') {
2968 d = scan_str(d,TRUE,TRUE);
2969 if (!d) {
09bef843
SB
2970 /* MUST advance bufptr here to avoid bogus
2971 "at end of line" context messages from yyerror().
2972 */
2973 PL_bufptr = s + len;
2974 yyerror("Unterminated attribute parameter in attribute list");
2975 if (attrs)
2976 op_free(attrs);
2977 return 0; /* EOF indicator */
2978 }
2979 }
2980 if (PL_lex_stuff) {
2981 SV *sv = newSVpvn(s, len);
2982 sv_catsv(sv, PL_lex_stuff);
2983 attrs = append_elem(OP_LIST, attrs,
2984 newSVOP(OP_CONST, 0, sv));
2985 SvREFCNT_dec(PL_lex_stuff);
2986 PL_lex_stuff = Nullsv;
2987 }
2988 else {
78f9721b
SM
2989 if (!PL_in_my && len == 6 && strnEQ(s, "lvalue", len))
2990 CvLVALUE_on(PL_compcv);
2991 else if (!PL_in_my && len == 6 && strnEQ(s, "locked", len))
2992 CvLOCKED_on(PL_compcv);
2993 else if (!PL_in_my && len == 6 && strnEQ(s, "method", len))
2994 CvMETHOD_on(PL_compcv);
87ecf892 2995#ifdef USE_ITHREADS
c8a3bf85 2996 else if (PL_in_my == KEY_our && len == 6 && strnEQ(s, "unique", len))
7fb37951 2997 GvUNIQUE_on(cGVOPx_gv(yylval.opval));
87ecf892 2998#endif
78f9721b
SM
2999 /* After we've set the flags, it could be argued that
3000 we don't need to do the attributes.pm-based setting
3001 process, and shouldn't bother appending recognized
3002 flags. To experiment with that, uncomment the
3003 following "else": */
0256094b 3004 else
78f9721b
SM
3005 attrs = append_elem(OP_LIST, attrs,
3006 newSVOP(OP_CONST, 0,
3007 newSVpvn(s, len)));
09bef843
SB
3008 }
3009 s = skipspace(d);
0120eecf 3010 if (*s == ':' && s[1] != ':')
09bef843 3011 s = skipspace(s+1);
0120eecf
GS
3012 else if (s == d)
3013 break; /* require real whitespace or :'s */
09bef843 3014 }
f9829d6b
GS
3015 tmp = (PL_expect == XOPERATOR ? '=' : '{'); /*'}(' for vi */
3016 if (*s != ';' && *s != tmp && (tmp != '=' || *s != ')')) {
09bef843
SB
3017 char q = ((*s == '\'') ? '"' : '\'');
3018 /* If here for an expression, and parsed no attrs, back off. */
3019 if (tmp == '=' && !attrs) {
3020 s = PL_bufptr;
3021 break;
3022 }
3023 /* MUST advance bufptr here to avoid bogus "at end of line"
3024 context messages from yyerror().
3025 */
3026 PL_bufptr = s;
3027 if (!*s)
3028 yyerror("Unterminated attribute list");
3029 else
3030 yyerror(Perl_form(aTHX_ "Invalid separator character %c%c%c in attribute list",
3031 q, *s, q));
3032 if (attrs)
3033 op_free(attrs);
3034 OPERATOR(':');
3035 }
f9829d6b 3036 got_attrs:
09bef843
SB
3037 if (attrs) {
3038 PL_nextval[PL_nexttoke].opval = attrs;
3039 force_next(THING);
3040 }
3041 TOKEN(COLONATTR);
3042 }
a0d0e21e 3043 OPERATOR(':');
8990e307
LW
3044 case '(':
3045 s++;
3280af22
NIS
3046 if (PL_last_lop == PL_oldoldbufptr || PL_last_uni == PL_oldoldbufptr)
3047 PL_oldbufptr = PL_oldoldbufptr; /* allow print(STDOUT 123) */
a0d0e21e 3048 else
3280af22 3049 PL_expect = XTERM;
a0d0e21e 3050 TOKEN('(');
378cc40b 3051 case ';':
f4dd75d9 3052 CLINE;
378cc40b
LW
3053 tmp = *s++;
3054 OPERATOR(tmp);
3055 case ')':
378cc40b 3056 tmp = *s++;
16d20bd9
AD
3057 s = skipspace(s);
3058 if (*s == '{')
3059 PREBLOCK(tmp);
378cc40b 3060 TERM(tmp);
79072805
LW
3061 case ']':
3062 s++;
3280af22 3063 if (PL_lex_brackets <= 0)
d98d5fff 3064 yyerror("Unmatched right square bracket");
463ee0b2 3065 else
3280af22
NIS
3066 --PL_lex_brackets;
3067 if (PL_lex_state == LEX_INTERPNORMAL) {
3068 if (PL_lex_brackets == 0) {
a0d0e21e 3069 if (*s != '[' && *s != '{' && (*s != '-' || s[1] != '>'))
3280af22 3070 PL_lex_state = LEX_INTERPEND;
79072805
LW
3071 }
3072 }
4633a7c4 3073 TERM(']');
79072805
LW
3074 case '{':
3075 leftbracket:
79072805 3076 s++;
3280af22
NIS
3077 if (PL_lex_brackets > 100) {
3078 char* newlb = Renew(PL_lex_brackstack, PL_lex_brackets + 1, char);
3079 if (newlb != PL_lex_brackstack) {
8990e307 3080 SAVEFREEPV(newlb);
3280af22 3081 PL_lex_brackstack = newlb;
8990e307
LW
3082 }
3083 }
3280af22 3084 switch (PL_expect) {
a0d0e21e 3085 case XTERM:
3280af22 3086 if (PL_lex_formbrack) {
a0d0e21e
LW
3087 s--;
3088 PRETERMBLOCK(DO);
3089 }
3280af22
NIS
3090 if (PL_oldoldbufptr == PL_last_lop)
3091 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
a0d0e21e 3092 else
3280af22 3093 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
79072805 3094 OPERATOR(HASHBRACK);
a0d0e21e 3095 case XOPERATOR:
bf4acbe4 3096 while (s < PL_bufend && SPACE_OR_TAB(*s))
748a9306 3097 s++;
44a8e56a 3098 d = s;
3280af22
NIS
3099 PL_tokenbuf[0] = '\0';
3100 if (d < PL_bufend && *d == '-') {
3101 PL_tokenbuf[0] = '-';
44a8e56a 3102 d++;
bf4acbe4 3103 while (d < PL_bufend && SPACE_OR_TAB(*d))
44a8e56a 3104 d++;
3105 }
7e2040f0 3106 if (d < PL_bufend && isIDFIRST_lazy_if(d,UTF)) {
3280af22 3107 d = scan_word(d, PL_tokenbuf + 1, sizeof PL_tokenbuf - 1,
8903cb82 3108 FALSE, &len);
bf4acbe4 3109 while (d < PL_bufend && SPACE_OR_TAB(*d))
748a9306
LW
3110 d++;
3111 if (*d == '}') {
3280af22 3112 char minus = (PL_tokenbuf[0] == '-');
44a8e56a 3113 s = force_word(s + minus, WORD, FALSE, TRUE, FALSE);
3114 if (minus)
3115 force_next('-');
748a9306
LW
3116 }
3117 }
3118 /* FALL THROUGH */
09bef843 3119 case XATTRBLOCK:
748a9306 3120 case XBLOCK:
3280af22
NIS
3121 PL_lex_brackstack[PL_lex_brackets++] = XSTATE;
3122 PL_expect = XSTATE;
a0d0e21e 3123 break;
09bef843 3124 case XATTRTERM:
a0d0e21e 3125 case XTERMBLOCK:
3280af22
NIS
3126 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
3127 PL_expect = XSTATE;
a0d0e21e
LW
3128 break;
3129 default: {
3130 char *t;
3280af22
NIS
3131 if (PL_oldoldbufptr == PL_last_lop)
3132 PL_lex_brackstack[PL_lex_brackets++] = XTERM;
a0d0e21e 3133 else
3280af22 3134 PL_lex_brackstack[PL_lex_brackets++] = XOPERATOR;
a0d0e21e 3135 s = skipspace(s);
8452ff4b
SB
3136 if (*s == '}') {
3137 if (PL_expect == XREF && PL_lex_state == LEX_INTERPNORMAL) {
3138 PL_expect = XTERM;
3139 /* This hack is to get the ${} in the message. */
3140 PL_bufptr = s+1;
3141 yyerror("syntax error");
3142 break;
3143 }
a0d0e21e 3144 OPERATOR(HASHBRACK);
8452ff4b 3145 }
b8a4b1be
GS
3146 /* This hack serves to disambiguate a pair of curlies
3147 * as being a block or an anon hash. Normally, expectation
3148 * determines that, but in cases where we're not in a
3149 * position to expect anything in particular (like inside
3150 * eval"") we have to resolve the ambiguity. This code
3151 * covers the case where the first term in the curlies is a
3152 * quoted string. Most other cases need to be explicitly
3153 * disambiguated by prepending a `+' before the opening
3154 * curly in order to force resolution as an anon hash.
3155 *
3156 * XXX should probably propagate the outer expectation
3157 * into eval"" to rely less on this hack, but that could
3158 * potentially break current behavior of eval"".
3159 * GSAR 97-07-21
3160 */
3161 t = s;
3162 if (*s == '\'' || *s == '"' || *s == '`') {
3163 /* common case: get past first string, handling escapes */
3280af22 3164 for (t++; t < PL_bufend && *t != *s;)
b8a4b1be
GS
3165 if (*t++ == '\\' && (*t == '\\' || *t == *s))
3166 t++;
3167 t++;
a0d0e21e 3168 }
b8a4b1be 3169 else if (*s == 'q') {
3280af22 3170 if (++t < PL_bufend
b8a4b1be 3171 && (!isALNUM(*t)
3280af22 3172 || ((*t == 'q' || *t == 'x') && ++t < PL_bufend
0505442f
GS
3173 && !isALNUM(*t))))
3174 {
b8a4b1be
GS
3175 char *tmps;
3176 char open, close, term;
3177 I32 brackets = 1;
3178
3280af22 3179 while (t < PL_bufend && isSPACE(*t))
b8a4b1be
GS
3180 t++;
3181 term = *t;
3182 open = term;
3183 if (term && (tmps = strchr("([{< )]}> )]}>",term)))
3184 term = tmps[5];
3185 close = term;
3186 if (open == close)
3280af22
NIS
3187 for (t++; t < PL_bufend; t++) {
3188 if (*t == '\\' && t+1 < PL_bufend && open != '\\')
b8a4b1be 3189 t++;
6d07e5e9 3190 else if (*t == open)
b8a4b1be
GS
3191 break;
3192 }
3193 else
3280af22
NIS
3194 for (t++; t < PL_bufend; t++) {
3195 if (*t == '\\' && t+1 < PL_bufend)
b8a4b1be 3196 t++;
6d07e5e9 3197 else if (*t == close && --brackets <= 0)
b8a4b1be
GS
3198 break;
3199 else if (*t == open)
3200 brackets++;
3201 }
3202 }
3203 t++;
a0d0e21e 3204 }
7e2040f0 3205 else if (isALNUM_lazy_if(t,UTF)) {
0505442f 3206 t += UTF8SKIP(t);
7e2040f0 3207 while (t < PL_bufend && isALNUM_lazy_if(t,UTF))
0505442f 3208 t += UTF8SKIP(t);
a0d0e21e 3209 }
3280af22 3210 while (t < PL_bufend && isSPACE(*t))
a0d0e21e 3211 t++;
b8a4b1be
GS
3212 /* if comma follows first term, call it an anon hash */
3213 /* XXX it could be a comma expression with loop modifiers */
3280af22 3214 if (t < PL_bufend && ((*t == ',' && (*s == 'q' || !isLOWER(*s)))
b8a4b1be 3215 || (*t == '=' && t[1] == '>')))
a0d0e21e 3216 OPERATOR(HASHBRACK);
3280af22 3217 if (PL_expect == XREF)
4e4e412b 3218 PL_expect = XTERM;
a0d0e21e 3219 else {
3280af22
NIS
3220 PL_lex_brackstack[PL_lex_brackets-1] = XSTATE;
3221 PL_expect = XSTATE;
a0d0e21e 3222 }
8990e307 3223 }
a0d0e21e 3224 break;
463ee0b2 3225 }
57843af0 3226 yylval.ival = CopLINE(PL_curcop);
79072805 3227 if (isSPACE(*s) || *s == '#')
3280af22 3228 PL_copline = NOLINE; /* invalidate current command line number */
79072805 3229 TOKEN('{');
378cc40b 3230 case '}':
79072805
LW
3231 rightbracket:
3232 s++;
3280af22 3233 if (PL_lex_brackets <= 0)
d98d5fff 3234 yyerror("Unmatched right curly bracket");
463ee0b2 3235 else
3280af22 3236 PL_expect = (expectation)PL_lex_brackstack[--PL_lex_brackets];
c2e66d9e 3237 if (PL_lex_brackets < PL_lex_formbrack && PL_lex_state != LEX_INTERPNORMAL)
3280af22
NIS
3238 PL_lex_formbrack = 0;
3239 if (PL_lex_state == LEX_INTERPNORMAL) {
3240 if (PL_lex_brackets == 0) {
9059aa12
LW
3241 if (PL_expect & XFAKEBRACK) {
3242 PL_expect &= XENUMMASK;
3280af22
NIS
3243 PL_lex_state = LEX_INTERPEND;
3244 PL_bufptr = s;
cea2e8a9 3245 return yylex(); /* ignore fake brackets */
79072805 3246 }
fa83b5b6 3247 if (*s == '-' && s[1] == '>')
3280af22 3248 PL_lex_state = LEX_INTERPENDMAYBE;
fa83b5b6 3249 else if (*s != '[' && *s != '{')
3280af22 3250 PL_lex_state = LEX_INTERPEND;
79072805
LW
3251 }
3252 }
9059aa12
LW
3253 if (PL_expect & XFAKEBRACK) {
3254 PL_expect &= XENUMMASK;
3280af22 3255 PL_bufptr = s;
cea2e8a9 3256 return yylex(); /* ignore fake brackets */
748a9306 3257 }
79072805
LW
3258 force_next('}');
3259 TOKEN(';');
378cc40b
LW
3260 case '&':
3261 s++;
3262 tmp = *s++;
3263 if (tmp == '&')
a0d0e21e 3264 AOPERATOR(ANDAND);
378cc40b 3265 s--;
3280af22 3266 if (PL_expect == XOPERATOR) {
7e2040f0
GS
3267 if (ckWARN(WARN_SEMICOLON)
3268 && isIDFIRST_lazy_if(s,UTF) && PL_bufptr == PL_linestart)
3269 {
57843af0 3270 CopLINE_dec(PL_curcop);
cea2e8a9 3271 Perl_warner(aTHX_ WARN_SEMICOLON, PL_warn_nosemi);
57843af0 3272 CopLINE_inc(PL_curcop);
463ee0b2 3273 }
79072805 3274 BAop(OP_BIT_AND);
463ee0b2 3275 }
79072805 3276
3280af22
NIS
3277 s = scan_ident(s - 1, PL_bufend, PL_tokenbuf, sizeof PL_tokenbuf, TRUE);
3278 if (*PL_tokenbuf) {
3279 PL_expect = XOPERATOR;
3280 force_ident(PL_tokenbuf, '&');
463ee0b2 3281 }
79072805
LW
3282 else
3283 PREREF('&');
c07a80fd 3284 yylval.ival = (OPpENTERSUB_AMPER<<8);
79072805
LW
3285 TERM('&');
3286
378cc40b
LW
3287 case '|':
3288 s++;
3289 tmp = *s++;
3290 if (tmp == '|')
a0d0e21e 3291 AOPERATOR(OROR);
378cc40b 3292 s--;
79072805 3293 BOop(OP_BIT_OR);
378cc40b
LW
3294 case '=':
3295 s++;
3296 tmp = *s++;
3297 if (tmp == '=')
79072805
LW
3298 Eop(OP_EQ);
3299 if (tmp == '>')
3300 OPERATOR(',');
378cc40b 3301 if (tmp == '~')
79072805 3302 PMop(OP_MATCH);
599cee73 3303 if (ckWARN(WARN_SYNTAX) && tmp && isSPACE(*s) && strchr("+-*/%.^&|<",tmp))
cea2e8a9 3304 Perl_warner(aTHX_ WARN_SYNTAX, "Reversed %c= operator",(int)tmp);
378cc40b 3305 s--;
3280af22
NIS
3306 if (PL_expect == XSTATE && isALPHA(tmp) &&
3307 (s == PL_linestart+1 || s[-2] == '\n') )
748a9306 3308 {
3280af22
NIS
3309 if (PL_in_eval && !PL_rsfp) {
3310 d = PL_bufend;
a5f75d66
AD
3311 while (s < d) {
3312 if (*s++ == '\n') {
3313 incline(s);
3314 if (strnEQ(s,"=cut",4)) {
3315 s = strchr(s,'\n');
3316 if (s)
3317 s++;
3318 else
3319 s = d;
3320 incline(s);
3321 goto retry;
3322 }
3323 }
3324 }
3325 goto retry;
3326 }
3280af22
NIS
3327 s = PL_bufend;
3328 PL_doextract = TRUE;
a0d0e21e
LW
3329 goto retry;
3330 }
3280af22 3331 if (PL_lex_brackets < PL_lex_formbrack) {
a0d0e21e 3332 char *t;
51882d45 3333#ifdef PERL_STRICT_CR
bf4acbe4 3334 for (t = s; SPACE_OR_TAB(*t); t++) ;
51882d45 3335#else
bf4acbe4 3336 for (t = s; SPACE_OR_TAB(*t) || *t == '\r'; t++) ;
51882d45 3337#endif
a0d0e21e
LW
3338 if (*t == '\n' || *t == '#') {
3339 s--;
3280af22 3340 PL_expect = XBLOCK;
a0d0e21e
LW
3341 goto leftbracket;
3342 }
79072805 3343 }
a0d0e21e
LW
3344 yylval.ival = 0;
3345 OPERATOR(ASSIGNOP);
378cc40b
LW
3346 case '!':
3347 s++;
3348 tmp = *s++;
3349 if (tmp == '=')
79072805 3350 Eop(OP_NE);
378cc40b 3351 if (tmp == '~')
79072805 3352 PMop(OP_NOT);
378cc40b
LW
3353 s--;
3354 OPERATOR('!');
3355 case '<':
3280af22 3356 if (PL_expect != XOPERATOR) {
93a17b20 3357 if (s[1] != '<' && !strchr(s,'>'))
2f3197b3 3358 check_uni();
79072805
LW
3359 if (s[1] == '<')
3360 s = scan_heredoc(s);
3361 else
3362 s = scan_inputsymbol(s);
3363 TERM(sublex_start());
378cc40b
LW
3364 }
3365 s++;
3366 tmp = *s++;
3367 if (tmp == '<')
79072805 3368 SHop(OP_LEFT_SHIFT);
395c3793
LW
3369 if (tmp == '=') {
3370 tmp = *s++;
3371 if (tmp == '>')
79072805 3372 Eop(OP_NCMP);
395c3793 3373 s--;
79072805 3374 Rop(OP_LE);
395c3793 3375 }
378cc40b 3376 s--;
79072805 3377 Rop(OP_LT);
378cc40b
LW
3378 case '>':
3379 s++;
3380 tmp = *s++;
3381 if (tmp == '>')
79072805 3382 SHop(OP_RIGHT_SHIFT);
378cc40b 3383 if (tmp == '=')
79072805 3384 Rop(OP_GE);
378cc40b 3385 s--;
79072805 3386 Rop(OP_GT);
378cc40b
LW
3387
3388 case '$':
bbce6d69 3389 CLINE;
3390
3280af22
NIS
3391 if (PL_expect == XOPERATOR) {
3392 if (PL_lex_formbrack && PL_lex_brackets == PL_lex_formbrack) {
3393 PL_expect = XTERM;
a0d0e21e 3394 depcom();
bbce6d69 3395 return ','; /* grandfather non-comma-format format */
a0d0e21e 3396 }
8990e307 3397 }
a0d0e21e 3398
7e2040f0 3399 if (s[1] == '#' && (isIDFIRST_lazy_if(s+2,UTF) || strchr("{$:+-", s[2]))) {
3280af22 3400 PL_tokenbuf[0] = '@';
376b8730
SM
3401 s = scan_ident(s + 1, PL_bufend, PL_tokenbuf + 1,
3402 sizeof PL_tokenbuf - 1, FALSE);
3403 if (PL_expect == XOPERATOR)
3404 no_op("Array length", s);
3280af22 3405 if (!PL_tokenbuf[1])
a0d0e21e 3406 PREREF(DOLSHARP);
3280af22
NIS
3407 PL_expect = XOPERATOR;
3408 PL_pending_ident = '#';
463ee0b2 3409 TOKEN(DOLSHARP);
79072805 3410 }
bbce6d69 3411
3280af22 3412 PL_tokenbuf[0] = '$';
376b8730
SM
3413 s = scan_ident(s, PL_bufend, PL_tokenbuf + 1,
3414 sizeof PL_tokenbuf - 1, FALSE);
3415 if (PL_expect == XOPERATOR)
3416 no_op("Scalar", s);
3280af22
NIS
3417 if (!PL_tokenbuf[1]) {
3418 if (s == PL_bufend)
bbce6d69 3419 yyerror("Final $ should be \\$ or $name");
3420 PREREF('$');
8990e307 3421 }
a0d0e21e 3422
bbce6d69 3423 /* This kludge not intended to be bulletproof. */
3280af22 3424 if (PL_tokenbuf[1] == '[' && !PL_tokenbuf[2]) {
bbce6d69 3425 yylval.opval = newSVOP(OP_CONST, 0,
b448e4fe 3426 newSViv(PL_compiling.cop_arybase));
bbce6d69 3427 yylval.opval->op_private = OPpCONST_ARYBASE;
3428 TERM(THING);
3429 }
3430
ff68c719 3431 d = s;
69d2bceb 3432 tmp = (I32)*s;
3280af22 3433 if (PL_lex_state == LEX_NORMAL)
ff68c719 3434 s = skipspace(s);
3435
3280af22 3436 if ((PL_expect != XREF || PL_oldoldbufptr == PL_last_lop) && intuit_more(s)) {
bbce6d69 3437 char *t;
3438 if (*s == '[') {
3280af22 3439 PL_tokenbuf[0] = '@';
599cee73 3440 if (ckWARN(WARN_SYNTAX)) {
bbce6d69 3441 for(t = s + 1;
7e2040f0 3442 isSPACE(*t) || isALNUM_lazy_if(t,UTF) || *t == '$';
bbce6d69 3443 t++) ;
a0d0e21e 3444 if (*t++ == ',') {
3280af22
NIS
3445 PL_bufptr = skipspace(PL_bufptr);
3446 while (t < PL_bufend && *t != ']')
bbce6d69 3447 t++;
cea2e8a9 3448 Perl_warner(aTHX_ WARN_SYNTAX,
599cee73
PM
3449 "Multidimensional syntax %.*s not supported",
3450 (t - PL_bufptr) + 1, PL_bufptr);
a0d0e21e
LW
3451 }
3452 }
bbce6d69 3453 }
3454 else if (*s == '{') {
3280af22 3455 PL_tokenbuf[0] = '%';
599cee73 3456 if (ckWARN(WARN_SYNTAX) && strEQ(PL_tokenbuf+1, "SIG") &&
bbce6d69 3457 (t = strchr(s, '}')) && (t = strchr(t, '=')))
3458 {
3280af22 3459 char tmpbuf[sizeof PL_tokenbuf];
a0d0e21e
LW
3460 STRLEN len;
3461 for (t++; isSPACE(*t); t++) ;
7e2040f0 3462 if (isIDFIRST_lazy_if(t,UTF)) {
8903cb82 3463 t = scan_word(t, tmpbuf, sizeof tmpbuf, TRUE, &len);
59a6d928 3464 for (; isSPACE(*t); t++) ;
864dbfa3 3465 if (*t == ';' && get_cv(tmpbuf, FALSE))
cea2e8a9 3466 Perl_warner(aTHX_ WARN_SYNTAX,
599cee73 3467 "You need to quote \"%s\"", tmpbuf);
748a9306 3468 }
93a17b20
LW
3469 }
3470 }
2f3197b3 3471 }
bbce6d69 3472
3280af22 3473 PL_expect = XOPERATOR;
69d2bceb 3474 if (PL_lex_state == LEX_NORMAL && isSPACE((char)tmp)) {
3280af22
NIS
3475 bool islop = (PL_last_lop == PL_oldoldbufptr);
3476 if (!islop || PL_last_lop_op == OP_GREPSTART)
3477 PL_expect = XOPERATOR;
bbce6d69 3478 else if (strchr("$@\"'`q", *s))
3280af22 3479 PL_expect = XTERM; /* e.g. print $fh "foo" */
7e2040f0 3480 else if (strchr("&*<%", *s) && isIDFIRST_lazy_if(s+1,UTF))
3280af22 3481 PL_expect = XTERM; /* e.g. print $fh &sub */
7e2040f0 3482 else if (isIDFIRST_lazy_if(s,UTF)) {
3280af22 3483 char tmpbuf[sizeof PL_tokenbuf];
8903cb82 3484 scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
155aba94 3485 if ((tmp = keyword(tmpbuf, len))) {
84902520
TB
3486 /* binary operators exclude handle interpretations */
3487 switch (tmp) {
3488 case -KEY_x:
3489 case -KEY_eq:
3490 case -KEY_ne:
3491 case -KEY_gt:
3492 case -KEY_lt:
3493 case -KEY_ge:
3494 case -KEY_le:
3495 case -KEY_cmp:
3496 break;
3497 default:
3280af22 3498 PL_expect = XTERM; /* e.g. print $fh length() */
84902520
TB
3499 break;
3500 }
3501 }
68dc0745 3502 else {
3503 GV *gv = gv_fetchpv(tmpbuf, FALSE, SVt_PVCV);
3504 if (gv && GvCVu(gv))
3280af22 3505 PL_expect = XTERM; /* e.g. print $fh subr() */
93a17b20 3506 }
93a17b20 3507 }
bbce6d69 3508 else if (isDIGIT(*s))
3280af22 3509 PL_expect = XTERM; /* e.g. print $fh 3 */
bbce6d69 3510 else if (*s == '.' && isDIGIT(s[1]))
3280af22 3511 PL_expect = XTERM; /* e.g. print $fh .3 */
e0587a03 3512 else if (strchr("/?-+", *s) &&am